xref: /linux/drivers/scsi/mpi3mr/mpi3mr_fw.c (revision 02ca7da2919ada525fb424640205110e24646b50)
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;
2477188c03fSSreekanth Reddy 	case MPI3_EVENT_ENCL_DEVICE_ADDED:
2487188c03fSSreekanth Reddy 		desc = "Enclosure Added";
2497188c03fSSreekanth Reddy 		break;
2509fc4abfeSKashyap Desai 	case MPI3_EVENT_HARD_RESET_RECEIVED:
2519fc4abfeSKashyap Desai 		desc = "Hard Reset Received";
2529fc4abfeSKashyap Desai 		break;
2539fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_PHY_COUNTER:
2549fc4abfeSKashyap Desai 		desc = "SAS PHY Counter";
2559fc4abfeSKashyap Desai 		break;
2569fc4abfeSKashyap Desai 	case MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
2579fc4abfeSKashyap Desai 		desc = "SAS Device Discovery Error";
2589fc4abfeSKashyap Desai 		break;
2599fc4abfeSKashyap Desai 	case MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
2609fc4abfeSKashyap Desai 		desc = "PCIE Topology Change List";
2619fc4abfeSKashyap Desai 		break;
2629fc4abfeSKashyap Desai 	case MPI3_EVENT_PCIE_ENUMERATION:
2639fc4abfeSKashyap Desai 	{
2649fc4abfeSKashyap Desai 		struct mpi3_event_data_pcie_enumeration *event_data =
2659fc4abfeSKashyap Desai 		    (struct mpi3_event_data_pcie_enumeration *)event_reply->event_data;
2669fc4abfeSKashyap Desai 		ioc_info(mrioc, "PCIE Enumeration: (%s)",
2679fc4abfeSKashyap Desai 		    (event_data->reason_code ==
2689fc4abfeSKashyap Desai 		    MPI3_EVENT_PCIE_ENUM_RC_STARTED) ? "start" : "stop");
2699fc4abfeSKashyap Desai 		if (event_data->enumeration_status)
2709fc4abfeSKashyap Desai 			ioc_info(mrioc, "enumeration_status(0x%08x)\n",
2719fc4abfeSKashyap Desai 			    le32_to_cpu(event_data->enumeration_status));
2729fc4abfeSKashyap Desai 		return;
2739fc4abfeSKashyap Desai 	}
2749fc4abfeSKashyap Desai 	case MPI3_EVENT_PREPARE_FOR_RESET:
2759fc4abfeSKashyap Desai 		desc = "Prepare For Reset";
2769fc4abfeSKashyap Desai 		break;
2779fc4abfeSKashyap Desai 	}
2789fc4abfeSKashyap Desai 
2799fc4abfeSKashyap Desai 	if (!desc)
2809fc4abfeSKashyap Desai 		return;
2819fc4abfeSKashyap Desai 
2829fc4abfeSKashyap Desai 	ioc_info(mrioc, "%s\n", desc);
2839fc4abfeSKashyap Desai }
2849fc4abfeSKashyap Desai 
285824a1566SKashyap Desai static void mpi3mr_handle_events(struct mpi3mr_ioc *mrioc,
286824a1566SKashyap Desai 	struct mpi3_default_reply *def_reply)
287824a1566SKashyap Desai {
288824a1566SKashyap Desai 	struct mpi3_event_notification_reply *event_reply =
289824a1566SKashyap Desai 	    (struct mpi3_event_notification_reply *)def_reply;
290824a1566SKashyap Desai 
291824a1566SKashyap Desai 	mrioc->change_count = le16_to_cpu(event_reply->ioc_change_count);
2929fc4abfeSKashyap Desai 	mpi3mr_print_event_data(mrioc, event_reply);
29313ef29eaSKashyap Desai 	mpi3mr_os_handle_events(mrioc, event_reply);
294824a1566SKashyap Desai }
295824a1566SKashyap Desai 
296824a1566SKashyap Desai static struct mpi3mr_drv_cmd *
297824a1566SKashyap Desai mpi3mr_get_drv_cmd(struct mpi3mr_ioc *mrioc, u16 host_tag,
298824a1566SKashyap Desai 	struct mpi3_default_reply *def_reply)
299824a1566SKashyap Desai {
30013ef29eaSKashyap Desai 	u16 idx;
30113ef29eaSKashyap Desai 
302824a1566SKashyap Desai 	switch (host_tag) {
303824a1566SKashyap Desai 	case MPI3MR_HOSTTAG_INITCMDS:
304824a1566SKashyap Desai 		return &mrioc->init_cmds;
30532d457d5SSreekanth Reddy 	case MPI3MR_HOSTTAG_CFG_CMDS:
30632d457d5SSreekanth Reddy 		return &mrioc->cfg_cmds;
307f5e6d5a3SSumit Saxena 	case MPI3MR_HOSTTAG_BSG_CMDS:
308f5e6d5a3SSumit Saxena 		return &mrioc->bsg_cmds;
309e844adb1SKashyap Desai 	case MPI3MR_HOSTTAG_BLK_TMS:
310e844adb1SKashyap Desai 		return &mrioc->host_tm_cmds;
31143ca1100SSumit Saxena 	case MPI3MR_HOSTTAG_PEL_ABORT:
31243ca1100SSumit Saxena 		return &mrioc->pel_abort_cmd;
31343ca1100SSumit Saxena 	case MPI3MR_HOSTTAG_PEL_WAIT:
31443ca1100SSumit Saxena 		return &mrioc->pel_cmds;
3152bd37e28SSreekanth Reddy 	case MPI3MR_HOSTTAG_TRANSPORT_CMDS:
3162bd37e28SSreekanth Reddy 		return &mrioc->transport_cmds;
317824a1566SKashyap Desai 	case MPI3MR_HOSTTAG_INVALID:
318824a1566SKashyap Desai 		if (def_reply && def_reply->function ==
319824a1566SKashyap Desai 		    MPI3_FUNCTION_EVENT_NOTIFICATION)
320824a1566SKashyap Desai 			mpi3mr_handle_events(mrioc, def_reply);
321824a1566SKashyap Desai 		return NULL;
322824a1566SKashyap Desai 	default:
323824a1566SKashyap Desai 		break;
324824a1566SKashyap Desai 	}
32513ef29eaSKashyap Desai 	if (host_tag >= MPI3MR_HOSTTAG_DEVRMCMD_MIN &&
32613ef29eaSKashyap Desai 	    host_tag <= MPI3MR_HOSTTAG_DEVRMCMD_MAX) {
32713ef29eaSKashyap Desai 		idx = host_tag - MPI3MR_HOSTTAG_DEVRMCMD_MIN;
32813ef29eaSKashyap Desai 		return &mrioc->dev_rmhs_cmds[idx];
32913ef29eaSKashyap Desai 	}
330824a1566SKashyap Desai 
331c1af985dSSreekanth Reddy 	if (host_tag >= MPI3MR_HOSTTAG_EVTACKCMD_MIN &&
332c1af985dSSreekanth Reddy 	    host_tag <= MPI3MR_HOSTTAG_EVTACKCMD_MAX) {
333c1af985dSSreekanth Reddy 		idx = host_tag - MPI3MR_HOSTTAG_EVTACKCMD_MIN;
334c1af985dSSreekanth Reddy 		return &mrioc->evtack_cmds[idx];
335c1af985dSSreekanth Reddy 	}
336c1af985dSSreekanth Reddy 
337824a1566SKashyap Desai 	return NULL;
338824a1566SKashyap Desai }
339824a1566SKashyap Desai 
340824a1566SKashyap Desai static void mpi3mr_process_admin_reply_desc(struct mpi3mr_ioc *mrioc,
341824a1566SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc, u64 *reply_dma)
342824a1566SKashyap Desai {
343824a1566SKashyap Desai 	u16 reply_desc_type, host_tag = 0;
344824a1566SKashyap Desai 	u16 ioc_status = MPI3_IOCSTATUS_SUCCESS;
345824a1566SKashyap Desai 	u32 ioc_loginfo = 0;
346824a1566SKashyap Desai 	struct mpi3_status_reply_descriptor *status_desc;
347824a1566SKashyap Desai 	struct mpi3_address_reply_descriptor *addr_desc;
348824a1566SKashyap Desai 	struct mpi3_success_reply_descriptor *success_desc;
349824a1566SKashyap Desai 	struct mpi3_default_reply *def_reply = NULL;
350824a1566SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr = NULL;
351824a1566SKashyap Desai 	struct mpi3_scsi_io_reply *scsi_reply;
352824a1566SKashyap Desai 	u8 *sense_buf = NULL;
353824a1566SKashyap Desai 
354824a1566SKashyap Desai 	*reply_dma = 0;
355824a1566SKashyap Desai 	reply_desc_type = le16_to_cpu(reply_desc->reply_flags) &
356824a1566SKashyap Desai 	    MPI3_REPLY_DESCRIPT_FLAGS_TYPE_MASK;
357824a1566SKashyap Desai 	switch (reply_desc_type) {
358824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_STATUS:
359824a1566SKashyap Desai 		status_desc = (struct mpi3_status_reply_descriptor *)reply_desc;
360824a1566SKashyap Desai 		host_tag = le16_to_cpu(status_desc->host_tag);
361824a1566SKashyap Desai 		ioc_status = le16_to_cpu(status_desc->ioc_status);
362824a1566SKashyap Desai 		if (ioc_status &
363824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
364824a1566SKashyap Desai 			ioc_loginfo = le32_to_cpu(status_desc->ioc_log_info);
365824a1566SKashyap Desai 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
366824a1566SKashyap Desai 		break;
367824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_ADDRESS_REPLY:
368824a1566SKashyap Desai 		addr_desc = (struct mpi3_address_reply_descriptor *)reply_desc;
369824a1566SKashyap Desai 		*reply_dma = le64_to_cpu(addr_desc->reply_frame_address);
370824a1566SKashyap Desai 		def_reply = mpi3mr_get_reply_virt_addr(mrioc, *reply_dma);
371824a1566SKashyap Desai 		if (!def_reply)
372824a1566SKashyap Desai 			goto out;
373824a1566SKashyap Desai 		host_tag = le16_to_cpu(def_reply->host_tag);
374824a1566SKashyap Desai 		ioc_status = le16_to_cpu(def_reply->ioc_status);
375824a1566SKashyap Desai 		if (ioc_status &
376824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_LOGINFOAVAIL)
377824a1566SKashyap Desai 			ioc_loginfo = le32_to_cpu(def_reply->ioc_log_info);
378824a1566SKashyap Desai 		ioc_status &= MPI3_REPLY_DESCRIPT_STATUS_IOCSTATUS_STATUS_MASK;
379824a1566SKashyap Desai 		if (def_reply->function == MPI3_FUNCTION_SCSI_IO) {
380824a1566SKashyap Desai 			scsi_reply = (struct mpi3_scsi_io_reply *)def_reply;
381824a1566SKashyap Desai 			sense_buf = mpi3mr_get_sensebuf_virt_addr(mrioc,
382824a1566SKashyap Desai 			    le64_to_cpu(scsi_reply->sense_data_buffer_address));
383824a1566SKashyap Desai 		}
384824a1566SKashyap Desai 		break;
385824a1566SKashyap Desai 	case MPI3_REPLY_DESCRIPT_FLAGS_TYPE_SUCCESS:
386824a1566SKashyap Desai 		success_desc = (struct mpi3_success_reply_descriptor *)reply_desc;
387824a1566SKashyap Desai 		host_tag = le16_to_cpu(success_desc->host_tag);
388824a1566SKashyap Desai 		break;
389824a1566SKashyap Desai 	default:
390824a1566SKashyap Desai 		break;
391824a1566SKashyap Desai 	}
392824a1566SKashyap Desai 
393824a1566SKashyap Desai 	cmdptr = mpi3mr_get_drv_cmd(mrioc, host_tag, def_reply);
394824a1566SKashyap Desai 	if (cmdptr) {
395824a1566SKashyap Desai 		if (cmdptr->state & MPI3MR_CMD_PENDING) {
396824a1566SKashyap Desai 			cmdptr->state |= MPI3MR_CMD_COMPLETE;
397824a1566SKashyap Desai 			cmdptr->ioc_loginfo = ioc_loginfo;
398824a1566SKashyap Desai 			cmdptr->ioc_status = ioc_status;
399824a1566SKashyap Desai 			cmdptr->state &= ~MPI3MR_CMD_PENDING;
400824a1566SKashyap Desai 			if (def_reply) {
401824a1566SKashyap Desai 				cmdptr->state |= MPI3MR_CMD_REPLY_VALID;
402824a1566SKashyap Desai 				memcpy((u8 *)cmdptr->reply, (u8 *)def_reply,
403c5758fc7SSreekanth Reddy 				    mrioc->reply_sz);
404824a1566SKashyap Desai 			}
405824a1566SKashyap Desai 			if (cmdptr->is_waiting) {
406824a1566SKashyap Desai 				complete(&cmdptr->done);
407824a1566SKashyap Desai 				cmdptr->is_waiting = 0;
408824a1566SKashyap Desai 			} else if (cmdptr->callback)
409824a1566SKashyap Desai 				cmdptr->callback(mrioc, cmdptr);
410824a1566SKashyap Desai 		}
411824a1566SKashyap Desai 	}
412824a1566SKashyap Desai out:
413824a1566SKashyap Desai 	if (sense_buf)
414824a1566SKashyap Desai 		mpi3mr_repost_sense_buf(mrioc,
415824a1566SKashyap Desai 		    le64_to_cpu(scsi_reply->sense_data_buffer_address));
416824a1566SKashyap Desai }
417824a1566SKashyap Desai 
418*02ca7da2SRanjan Kumar int mpi3mr_process_admin_reply_q(struct mpi3mr_ioc *mrioc)
419824a1566SKashyap Desai {
420824a1566SKashyap Desai 	u32 exp_phase = mrioc->admin_reply_ephase;
421824a1566SKashyap Desai 	u32 admin_reply_ci = mrioc->admin_reply_ci;
422824a1566SKashyap Desai 	u32 num_admin_replies = 0;
423824a1566SKashyap Desai 	u64 reply_dma = 0;
424824a1566SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc;
425824a1566SKashyap Desai 
426*02ca7da2SRanjan Kumar 	if (!atomic_add_unless(&mrioc->admin_reply_q_in_use, 1, 1))
427*02ca7da2SRanjan Kumar 		return 0;
428*02ca7da2SRanjan Kumar 
429824a1566SKashyap Desai 	reply_desc = (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
430824a1566SKashyap Desai 	    admin_reply_ci;
431824a1566SKashyap Desai 
432824a1566SKashyap Desai 	if ((le16_to_cpu(reply_desc->reply_flags) &
433*02ca7da2SRanjan Kumar 	    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) {
434*02ca7da2SRanjan Kumar 		atomic_dec(&mrioc->admin_reply_q_in_use);
435824a1566SKashyap Desai 		return 0;
436*02ca7da2SRanjan Kumar 	}
437824a1566SKashyap Desai 
438824a1566SKashyap Desai 	do {
439f2a79d20SSreekanth Reddy 		if (mrioc->unrecoverable)
440f2a79d20SSreekanth Reddy 			break;
441f2a79d20SSreekanth Reddy 
442824a1566SKashyap Desai 		mrioc->admin_req_ci = le16_to_cpu(reply_desc->request_queue_ci);
443824a1566SKashyap Desai 		mpi3mr_process_admin_reply_desc(mrioc, reply_desc, &reply_dma);
444824a1566SKashyap Desai 		if (reply_dma)
445824a1566SKashyap Desai 			mpi3mr_repost_reply_buf(mrioc, reply_dma);
446824a1566SKashyap Desai 		num_admin_replies++;
447824a1566SKashyap Desai 		if (++admin_reply_ci == mrioc->num_admin_replies) {
448824a1566SKashyap Desai 			admin_reply_ci = 0;
449824a1566SKashyap Desai 			exp_phase ^= 1;
450824a1566SKashyap Desai 		}
451824a1566SKashyap Desai 		reply_desc =
452824a1566SKashyap Desai 		    (struct mpi3_default_reply_descriptor *)mrioc->admin_reply_base +
453824a1566SKashyap Desai 		    admin_reply_ci;
454824a1566SKashyap Desai 		if ((le16_to_cpu(reply_desc->reply_flags) &
455824a1566SKashyap Desai 		    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
456824a1566SKashyap Desai 			break;
457824a1566SKashyap Desai 	} while (1);
458824a1566SKashyap Desai 
459824a1566SKashyap Desai 	writel(admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
460824a1566SKashyap Desai 	mrioc->admin_reply_ci = admin_reply_ci;
461824a1566SKashyap Desai 	mrioc->admin_reply_ephase = exp_phase;
462*02ca7da2SRanjan Kumar 	atomic_dec(&mrioc->admin_reply_q_in_use);
463824a1566SKashyap Desai 
464824a1566SKashyap Desai 	return num_admin_replies;
465824a1566SKashyap Desai }
466824a1566SKashyap Desai 
467023ab2a9SKashyap Desai /**
468023ab2a9SKashyap Desai  * mpi3mr_get_reply_desc - get reply descriptor frame corresponding to
469023ab2a9SKashyap Desai  *	queue's consumer index from operational reply descriptor queue.
470023ab2a9SKashyap Desai  * @op_reply_q: op_reply_qinfo object
471023ab2a9SKashyap Desai  * @reply_ci: operational reply descriptor's queue consumer index
472023ab2a9SKashyap Desai  *
473023ab2a9SKashyap Desai  * Returns reply descriptor frame address
474023ab2a9SKashyap Desai  */
475023ab2a9SKashyap Desai static inline struct mpi3_default_reply_descriptor *
476023ab2a9SKashyap Desai mpi3mr_get_reply_desc(struct op_reply_qinfo *op_reply_q, u32 reply_ci)
477023ab2a9SKashyap Desai {
478023ab2a9SKashyap Desai 	void *segment_base_addr;
479023ab2a9SKashyap Desai 	struct segments *segments = op_reply_q->q_segments;
480023ab2a9SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc = NULL;
481023ab2a9SKashyap Desai 
482023ab2a9SKashyap Desai 	segment_base_addr =
483023ab2a9SKashyap Desai 	    segments[reply_ci / op_reply_q->segment_qd].segment;
484023ab2a9SKashyap Desai 	reply_desc = (struct mpi3_default_reply_descriptor *)segment_base_addr +
485023ab2a9SKashyap Desai 	    (reply_ci % op_reply_q->segment_qd);
486023ab2a9SKashyap Desai 	return reply_desc;
487023ab2a9SKashyap Desai }
488023ab2a9SKashyap Desai 
489afd3a579SSreekanth Reddy /**
490afd3a579SSreekanth Reddy  * mpi3mr_process_op_reply_q - Operational reply queue handler
491afd3a579SSreekanth Reddy  * @mrioc: Adapter instance reference
492afd3a579SSreekanth Reddy  * @op_reply_q: Operational reply queue info
493afd3a579SSreekanth Reddy  *
494afd3a579SSreekanth Reddy  * Checks the specific operational reply queue and drains the
495afd3a579SSreekanth Reddy  * reply queue entries until the queue is empty and process the
496afd3a579SSreekanth Reddy  * individual reply descriptors.
497afd3a579SSreekanth Reddy  *
498afd3a579SSreekanth Reddy  * Return: 0 if queue is already processed,or number of reply
499afd3a579SSreekanth Reddy  *	    descriptors processed.
500afd3a579SSreekanth Reddy  */
501afd3a579SSreekanth Reddy int mpi3mr_process_op_reply_q(struct mpi3mr_ioc *mrioc,
502afd3a579SSreekanth Reddy 	struct op_reply_qinfo *op_reply_q)
503023ab2a9SKashyap Desai {
504023ab2a9SKashyap Desai 	struct op_req_qinfo *op_req_q;
505023ab2a9SKashyap Desai 	u32 exp_phase;
506023ab2a9SKashyap Desai 	u32 reply_ci;
507023ab2a9SKashyap Desai 	u32 num_op_reply = 0;
508023ab2a9SKashyap Desai 	u64 reply_dma = 0;
509023ab2a9SKashyap Desai 	struct mpi3_default_reply_descriptor *reply_desc;
510023ab2a9SKashyap Desai 	u16 req_q_idx = 0, reply_qidx;
511023ab2a9SKashyap Desai 
512023ab2a9SKashyap Desai 	reply_qidx = op_reply_q->qid - 1;
513023ab2a9SKashyap Desai 
514463429f8SKashyap Desai 	if (!atomic_add_unless(&op_reply_q->in_use, 1, 1))
515463429f8SKashyap Desai 		return 0;
516463429f8SKashyap Desai 
517023ab2a9SKashyap Desai 	exp_phase = op_reply_q->ephase;
518023ab2a9SKashyap Desai 	reply_ci = op_reply_q->ci;
519023ab2a9SKashyap Desai 
520023ab2a9SKashyap Desai 	reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
521023ab2a9SKashyap Desai 	if ((le16_to_cpu(reply_desc->reply_flags) &
522023ab2a9SKashyap Desai 	    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase) {
523463429f8SKashyap Desai 		atomic_dec(&op_reply_q->in_use);
524023ab2a9SKashyap Desai 		return 0;
525023ab2a9SKashyap Desai 	}
526023ab2a9SKashyap Desai 
527023ab2a9SKashyap Desai 	do {
528f2a79d20SSreekanth Reddy 		if (mrioc->unrecoverable)
529f2a79d20SSreekanth Reddy 			break;
530f2a79d20SSreekanth Reddy 
531023ab2a9SKashyap Desai 		req_q_idx = le16_to_cpu(reply_desc->request_queue_id) - 1;
532023ab2a9SKashyap Desai 		op_req_q = &mrioc->req_qinfo[req_q_idx];
533023ab2a9SKashyap Desai 
534023ab2a9SKashyap Desai 		WRITE_ONCE(op_req_q->ci, le16_to_cpu(reply_desc->request_queue_ci));
535023ab2a9SKashyap Desai 		mpi3mr_process_op_reply_desc(mrioc, reply_desc, &reply_dma,
536023ab2a9SKashyap Desai 		    reply_qidx);
537463429f8SKashyap Desai 		atomic_dec(&op_reply_q->pend_ios);
538023ab2a9SKashyap Desai 		if (reply_dma)
539023ab2a9SKashyap Desai 			mpi3mr_repost_reply_buf(mrioc, reply_dma);
540023ab2a9SKashyap Desai 		num_op_reply++;
541023ab2a9SKashyap Desai 
542023ab2a9SKashyap Desai 		if (++reply_ci == op_reply_q->num_replies) {
543023ab2a9SKashyap Desai 			reply_ci = 0;
544023ab2a9SKashyap Desai 			exp_phase ^= 1;
545023ab2a9SKashyap Desai 		}
546023ab2a9SKashyap Desai 
547023ab2a9SKashyap Desai 		reply_desc = mpi3mr_get_reply_desc(op_reply_q, reply_ci);
548023ab2a9SKashyap Desai 
549023ab2a9SKashyap Desai 		if ((le16_to_cpu(reply_desc->reply_flags) &
550023ab2a9SKashyap Desai 		    MPI3_REPLY_DESCRIPT_FLAGS_PHASE_MASK) != exp_phase)
551023ab2a9SKashyap Desai 			break;
5527f9f953dSSreekanth Reddy #ifndef CONFIG_PREEMPT_RT
553463429f8SKashyap Desai 		/*
554463429f8SKashyap Desai 		 * Exit completion loop to avoid CPU lockup
555463429f8SKashyap Desai 		 * Ensure remaining completion happens from threaded ISR.
556463429f8SKashyap Desai 		 */
557463429f8SKashyap Desai 		if (num_op_reply > mrioc->max_host_ios) {
558afd3a579SSreekanth Reddy 			op_reply_q->enable_irq_poll = true;
559463429f8SKashyap Desai 			break;
560463429f8SKashyap Desai 		}
5617f9f953dSSreekanth Reddy #endif
562023ab2a9SKashyap Desai 	} while (1);
563023ab2a9SKashyap Desai 
564023ab2a9SKashyap Desai 	writel(reply_ci,
565023ab2a9SKashyap Desai 	    &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].consumer_index);
566023ab2a9SKashyap Desai 	op_reply_q->ci = reply_ci;
567023ab2a9SKashyap Desai 	op_reply_q->ephase = exp_phase;
568023ab2a9SKashyap Desai 
569463429f8SKashyap Desai 	atomic_dec(&op_reply_q->in_use);
570023ab2a9SKashyap Desai 	return num_op_reply;
571023ab2a9SKashyap Desai }
572023ab2a9SKashyap Desai 
573afd3a579SSreekanth Reddy /**
574afd3a579SSreekanth Reddy  * mpi3mr_blk_mq_poll - Operational reply queue handler
575afd3a579SSreekanth Reddy  * @shost: SCSI Host reference
576afd3a579SSreekanth Reddy  * @queue_num: Request queue number (w.r.t OS it is hardware context number)
577afd3a579SSreekanth Reddy  *
578afd3a579SSreekanth Reddy  * Checks the specific operational reply queue and drains the
579afd3a579SSreekanth Reddy  * reply queue entries until the queue is empty and process the
580afd3a579SSreekanth Reddy  * individual reply descriptors.
581afd3a579SSreekanth Reddy  *
582afd3a579SSreekanth Reddy  * Return: 0 if queue is already processed,or number of reply
583afd3a579SSreekanth Reddy  *	    descriptors processed.
584afd3a579SSreekanth Reddy  */
585afd3a579SSreekanth Reddy int mpi3mr_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
586afd3a579SSreekanth Reddy {
587afd3a579SSreekanth Reddy 	int num_entries = 0;
588afd3a579SSreekanth Reddy 	struct mpi3mr_ioc *mrioc;
589afd3a579SSreekanth Reddy 
590afd3a579SSreekanth Reddy 	mrioc = (struct mpi3mr_ioc *)shost->hostdata;
591afd3a579SSreekanth Reddy 
592f2a79d20SSreekanth Reddy 	if ((mrioc->reset_in_progress || mrioc->prepare_for_reset ||
593f2a79d20SSreekanth Reddy 	    mrioc->unrecoverable))
594afd3a579SSreekanth Reddy 		return 0;
595afd3a579SSreekanth Reddy 
596afd3a579SSreekanth Reddy 	num_entries = mpi3mr_process_op_reply_q(mrioc,
597afd3a579SSreekanth Reddy 			&mrioc->op_reply_qinfo[queue_num]);
598afd3a579SSreekanth Reddy 
599afd3a579SSreekanth Reddy 	return num_entries;
600afd3a579SSreekanth Reddy }
601afd3a579SSreekanth Reddy 
602824a1566SKashyap Desai static irqreturn_t mpi3mr_isr_primary(int irq, void *privdata)
603824a1566SKashyap Desai {
604824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
605824a1566SKashyap Desai 	struct mpi3mr_ioc *mrioc;
606824a1566SKashyap Desai 	u16 midx;
607463429f8SKashyap Desai 	u32 num_admin_replies = 0, num_op_reply = 0;
608824a1566SKashyap Desai 
609824a1566SKashyap Desai 	if (!intr_info)
610824a1566SKashyap Desai 		return IRQ_NONE;
611824a1566SKashyap Desai 
612824a1566SKashyap Desai 	mrioc = intr_info->mrioc;
613824a1566SKashyap Desai 
614824a1566SKashyap Desai 	if (!mrioc->intr_enabled)
615824a1566SKashyap Desai 		return IRQ_NONE;
616824a1566SKashyap Desai 
617824a1566SKashyap Desai 	midx = intr_info->msix_index;
618824a1566SKashyap Desai 
619824a1566SKashyap Desai 	if (!midx)
620824a1566SKashyap Desai 		num_admin_replies = mpi3mr_process_admin_reply_q(mrioc);
621463429f8SKashyap Desai 	if (intr_info->op_reply_q)
622afd3a579SSreekanth Reddy 		num_op_reply = mpi3mr_process_op_reply_q(mrioc,
623afd3a579SSreekanth Reddy 		    intr_info->op_reply_q);
624824a1566SKashyap Desai 
625463429f8SKashyap Desai 	if (num_admin_replies || num_op_reply)
626824a1566SKashyap Desai 		return IRQ_HANDLED;
627824a1566SKashyap Desai 	else
628824a1566SKashyap Desai 		return IRQ_NONE;
629824a1566SKashyap Desai }
630824a1566SKashyap Desai 
6317f9f953dSSreekanth Reddy #ifndef CONFIG_PREEMPT_RT
6327f9f953dSSreekanth Reddy 
633824a1566SKashyap Desai static irqreturn_t mpi3mr_isr(int irq, void *privdata)
634824a1566SKashyap Desai {
635824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
636824a1566SKashyap Desai 	int ret;
637824a1566SKashyap Desai 
638824a1566SKashyap Desai 	if (!intr_info)
639824a1566SKashyap Desai 		return IRQ_NONE;
640824a1566SKashyap Desai 
641824a1566SKashyap Desai 	/* Call primary ISR routine */
642824a1566SKashyap Desai 	ret = mpi3mr_isr_primary(irq, privdata);
643824a1566SKashyap Desai 
644463429f8SKashyap Desai 	/*
645463429f8SKashyap Desai 	 * If more IOs are expected, schedule IRQ polling thread.
646463429f8SKashyap Desai 	 * Otherwise exit from ISR.
647463429f8SKashyap Desai 	 */
648463429f8SKashyap Desai 	if (!intr_info->op_reply_q)
649824a1566SKashyap Desai 		return ret;
650463429f8SKashyap Desai 
651463429f8SKashyap Desai 	if (!intr_info->op_reply_q->enable_irq_poll ||
652463429f8SKashyap Desai 	    !atomic_read(&intr_info->op_reply_q->pend_ios))
653463429f8SKashyap Desai 		return ret;
654463429f8SKashyap Desai 
6552e31be86SSreekanth Reddy 	disable_irq_nosync(intr_info->os_irq);
656463429f8SKashyap Desai 
657463429f8SKashyap Desai 	return IRQ_WAKE_THREAD;
658824a1566SKashyap Desai }
659824a1566SKashyap Desai 
660824a1566SKashyap Desai /**
661824a1566SKashyap Desai  * mpi3mr_isr_poll - Reply queue polling routine
662824a1566SKashyap Desai  * @irq: IRQ
663824a1566SKashyap Desai  * @privdata: Interrupt info
664824a1566SKashyap Desai  *
665824a1566SKashyap Desai  * poll for pending I/O completions in a loop until pending I/Os
666824a1566SKashyap Desai  * present or controller queue depth I/Os are processed.
667824a1566SKashyap Desai  *
668824a1566SKashyap Desai  * Return: IRQ_NONE or IRQ_HANDLED
669824a1566SKashyap Desai  */
670824a1566SKashyap Desai static irqreturn_t mpi3mr_isr_poll(int irq, void *privdata)
671824a1566SKashyap Desai {
672463429f8SKashyap Desai 	struct mpi3mr_intr_info *intr_info = privdata;
673463429f8SKashyap Desai 	struct mpi3mr_ioc *mrioc;
674463429f8SKashyap Desai 	u16 midx;
675463429f8SKashyap Desai 	u32 num_op_reply = 0;
676463429f8SKashyap Desai 
677463429f8SKashyap Desai 	if (!intr_info || !intr_info->op_reply_q)
678463429f8SKashyap Desai 		return IRQ_NONE;
679463429f8SKashyap Desai 
680463429f8SKashyap Desai 	mrioc = intr_info->mrioc;
681463429f8SKashyap Desai 	midx = intr_info->msix_index;
682463429f8SKashyap Desai 
683463429f8SKashyap Desai 	/* Poll for pending IOs completions */
684463429f8SKashyap Desai 	do {
685f2a79d20SSreekanth Reddy 		if (!mrioc->intr_enabled || mrioc->unrecoverable)
686463429f8SKashyap Desai 			break;
687463429f8SKashyap Desai 
688463429f8SKashyap Desai 		if (!midx)
689463429f8SKashyap Desai 			mpi3mr_process_admin_reply_q(mrioc);
690463429f8SKashyap Desai 		if (intr_info->op_reply_q)
691463429f8SKashyap Desai 			num_op_reply +=
692afd3a579SSreekanth Reddy 			    mpi3mr_process_op_reply_q(mrioc,
693afd3a579SSreekanth Reddy 				intr_info->op_reply_q);
694463429f8SKashyap Desai 
695afd3a579SSreekanth Reddy 		usleep_range(MPI3MR_IRQ_POLL_SLEEP, 10 * MPI3MR_IRQ_POLL_SLEEP);
696463429f8SKashyap Desai 
697463429f8SKashyap Desai 	} while (atomic_read(&intr_info->op_reply_q->pend_ios) &&
698463429f8SKashyap Desai 	    (num_op_reply < mrioc->max_host_ios));
699463429f8SKashyap Desai 
700463429f8SKashyap Desai 	intr_info->op_reply_q->enable_irq_poll = false;
7012e31be86SSreekanth Reddy 	enable_irq(intr_info->os_irq);
702463429f8SKashyap Desai 
703824a1566SKashyap Desai 	return IRQ_HANDLED;
704824a1566SKashyap Desai }
705824a1566SKashyap Desai 
7067f9f953dSSreekanth Reddy #endif
7077f9f953dSSreekanth Reddy 
708824a1566SKashyap Desai /**
709824a1566SKashyap Desai  * mpi3mr_request_irq - Request IRQ and register ISR
710824a1566SKashyap Desai  * @mrioc: Adapter instance reference
711824a1566SKashyap Desai  * @index: IRQ vector index
712824a1566SKashyap Desai  *
713824a1566SKashyap Desai  * Request threaded ISR with primary ISR and secondary
714824a1566SKashyap Desai  *
715824a1566SKashyap Desai  * Return: 0 on success and non zero on failures.
716824a1566SKashyap Desai  */
717824a1566SKashyap Desai static inline int mpi3mr_request_irq(struct mpi3mr_ioc *mrioc, u16 index)
718824a1566SKashyap Desai {
719824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
720824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info = mrioc->intr_info + index;
721824a1566SKashyap Desai 	int retval = 0;
722824a1566SKashyap Desai 
723824a1566SKashyap Desai 	intr_info->mrioc = mrioc;
724824a1566SKashyap Desai 	intr_info->msix_index = index;
725824a1566SKashyap Desai 	intr_info->op_reply_q = NULL;
726824a1566SKashyap Desai 
727824a1566SKashyap Desai 	snprintf(intr_info->name, MPI3MR_NAME_LENGTH, "%s%d-msix%d",
728824a1566SKashyap Desai 	    mrioc->driver_name, mrioc->id, index);
729824a1566SKashyap Desai 
7307f9f953dSSreekanth Reddy #ifndef CONFIG_PREEMPT_RT
731824a1566SKashyap Desai 	retval = request_threaded_irq(pci_irq_vector(pdev, index), mpi3mr_isr,
732824a1566SKashyap Desai 	    mpi3mr_isr_poll, IRQF_SHARED, intr_info->name, intr_info);
7337f9f953dSSreekanth Reddy #else
7347f9f953dSSreekanth Reddy 	retval = request_threaded_irq(pci_irq_vector(pdev, index), mpi3mr_isr_primary,
7357f9f953dSSreekanth Reddy 	    NULL, IRQF_SHARED, intr_info->name, intr_info);
7367f9f953dSSreekanth Reddy #endif
737824a1566SKashyap Desai 	if (retval) {
738824a1566SKashyap Desai 		ioc_err(mrioc, "%s: Unable to allocate interrupt %d!\n",
739824a1566SKashyap Desai 		    intr_info->name, pci_irq_vector(pdev, index));
740824a1566SKashyap Desai 		return retval;
741824a1566SKashyap Desai 	}
742824a1566SKashyap Desai 
7432e31be86SSreekanth Reddy 	intr_info->os_irq = pci_irq_vector(pdev, index);
744824a1566SKashyap Desai 	return retval;
745824a1566SKashyap Desai }
746824a1566SKashyap Desai 
747afd3a579SSreekanth Reddy static void mpi3mr_calc_poll_queues(struct mpi3mr_ioc *mrioc, u16 max_vectors)
748afd3a579SSreekanth Reddy {
749afd3a579SSreekanth Reddy 	if (!mrioc->requested_poll_qcount)
750afd3a579SSreekanth Reddy 		return;
751afd3a579SSreekanth Reddy 
752afd3a579SSreekanth Reddy 	/* Reserved for Admin and Default Queue */
753afd3a579SSreekanth Reddy 	if (max_vectors > 2 &&
754afd3a579SSreekanth Reddy 		(mrioc->requested_poll_qcount < max_vectors - 2)) {
755afd3a579SSreekanth Reddy 		ioc_info(mrioc,
756afd3a579SSreekanth Reddy 		    "enabled polled queues (%d) msix (%d)\n",
757afd3a579SSreekanth Reddy 		    mrioc->requested_poll_qcount, max_vectors);
758afd3a579SSreekanth Reddy 	} else {
759afd3a579SSreekanth Reddy 		ioc_info(mrioc,
760afd3a579SSreekanth Reddy 		    "disabled polled queues (%d) msix (%d) because of no resources for default queue\n",
761afd3a579SSreekanth Reddy 		    mrioc->requested_poll_qcount, max_vectors);
762afd3a579SSreekanth Reddy 		mrioc->requested_poll_qcount = 0;
763afd3a579SSreekanth Reddy 	}
764afd3a579SSreekanth Reddy }
765afd3a579SSreekanth Reddy 
766824a1566SKashyap Desai /**
767824a1566SKashyap Desai  * mpi3mr_setup_isr - Setup ISR for the controller
768824a1566SKashyap Desai  * @mrioc: Adapter instance reference
769824a1566SKashyap Desai  * @setup_one: Request one IRQ or more
770824a1566SKashyap Desai  *
771824a1566SKashyap Desai  * Allocate IRQ vectors and call mpi3mr_request_irq to setup ISR
772824a1566SKashyap Desai  *
773824a1566SKashyap Desai  * Return: 0 on success and non zero on failures.
774824a1566SKashyap Desai  */
775824a1566SKashyap Desai static int mpi3mr_setup_isr(struct mpi3mr_ioc *mrioc, u8 setup_one)
776824a1566SKashyap Desai {
777824a1566SKashyap Desai 	unsigned int irq_flags = PCI_IRQ_MSIX;
778afd3a579SSreekanth Reddy 	int max_vectors, min_vec;
7792938beddSDan Carpenter 	int retval;
7802938beddSDan Carpenter 	int i;
781afd3a579SSreekanth Reddy 	struct irq_affinity desc = { .pre_vectors =  1, .post_vectors = 1 };
782824a1566SKashyap Desai 
783fe6db615SSreekanth Reddy 	if (mrioc->is_intr_info_set)
784fe6db615SSreekanth Reddy 		return 0;
785fe6db615SSreekanth Reddy 
786824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
787824a1566SKashyap Desai 
788afd3a579SSreekanth Reddy 	if (setup_one || reset_devices) {
789824a1566SKashyap Desai 		max_vectors = 1;
790afd3a579SSreekanth Reddy 		retval = pci_alloc_irq_vectors(mrioc->pdev,
791afd3a579SSreekanth Reddy 		    1, max_vectors, irq_flags);
792afd3a579SSreekanth Reddy 		if (retval < 0) {
793afd3a579SSreekanth Reddy 			ioc_err(mrioc, "cannot allocate irq vectors, ret %d\n",
794afd3a579SSreekanth Reddy 			    retval);
795afd3a579SSreekanth Reddy 			goto out_failed;
796afd3a579SSreekanth Reddy 		}
797afd3a579SSreekanth Reddy 	} else {
798824a1566SKashyap Desai 		max_vectors =
799afd3a579SSreekanth Reddy 		    min_t(int, mrioc->cpu_count + 1 +
800afd3a579SSreekanth Reddy 			mrioc->requested_poll_qcount, mrioc->msix_count);
801afd3a579SSreekanth Reddy 
802afd3a579SSreekanth Reddy 		mpi3mr_calc_poll_queues(mrioc, max_vectors);
803824a1566SKashyap Desai 
804824a1566SKashyap Desai 		ioc_info(mrioc,
805824a1566SKashyap Desai 		    "MSI-X vectors supported: %d, no of cores: %d,",
806824a1566SKashyap Desai 		    mrioc->msix_count, mrioc->cpu_count);
807824a1566SKashyap Desai 		ioc_info(mrioc,
808afd3a579SSreekanth Reddy 		    "MSI-x vectors requested: %d poll_queues %d\n",
809afd3a579SSreekanth Reddy 		    max_vectors, mrioc->requested_poll_qcount);
810824a1566SKashyap Desai 
811afd3a579SSreekanth Reddy 		desc.post_vectors = mrioc->requested_poll_qcount;
812afd3a579SSreekanth Reddy 		min_vec = desc.pre_vectors + desc.post_vectors;
813824a1566SKashyap Desai 		irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES;
814824a1566SKashyap Desai 
8152938beddSDan Carpenter 		retval = pci_alloc_irq_vectors_affinity(mrioc->pdev,
816afd3a579SSreekanth Reddy 			min_vec, max_vectors, irq_flags, &desc);
817afd3a579SSreekanth Reddy 
8182938beddSDan Carpenter 		if (retval < 0) {
819afd3a579SSreekanth Reddy 			ioc_err(mrioc, "cannot allocate irq vectors, ret %d\n",
820afd3a579SSreekanth Reddy 			    retval);
821824a1566SKashyap Desai 			goto out_failed;
822824a1566SKashyap Desai 		}
823afd3a579SSreekanth Reddy 
824afd3a579SSreekanth Reddy 
825c9566231SKashyap Desai 		/*
826c9566231SKashyap Desai 		 * If only one MSI-x is allocated, then MSI-x 0 will be shared
827c9566231SKashyap Desai 		 * between Admin queue and operational queue
828c9566231SKashyap Desai 		 */
829afd3a579SSreekanth Reddy 		if (retval == min_vec)
830c9566231SKashyap Desai 			mrioc->op_reply_q_offset = 0;
831afd3a579SSreekanth Reddy 		else if (retval != (max_vectors)) {
832afd3a579SSreekanth Reddy 			ioc_info(mrioc,
833afd3a579SSreekanth Reddy 			    "allocated vectors (%d) are less than configured (%d)\n",
834afd3a579SSreekanth Reddy 			    retval, max_vectors);
835afd3a579SSreekanth Reddy 		}
836824a1566SKashyap Desai 
8372938beddSDan Carpenter 		max_vectors = retval;
838afd3a579SSreekanth Reddy 		mrioc->op_reply_q_offset = (max_vectors > 1) ? 1 : 0;
839afd3a579SSreekanth Reddy 
840afd3a579SSreekanth Reddy 		mpi3mr_calc_poll_queues(mrioc, max_vectors);
841afd3a579SSreekanth Reddy 
842824a1566SKashyap Desai 	}
843afd3a579SSreekanth Reddy 
844824a1566SKashyap Desai 	mrioc->intr_info = kzalloc(sizeof(struct mpi3mr_intr_info) * max_vectors,
845824a1566SKashyap Desai 	    GFP_KERNEL);
846824a1566SKashyap Desai 	if (!mrioc->intr_info) {
8472938beddSDan Carpenter 		retval = -ENOMEM;
848824a1566SKashyap Desai 		pci_free_irq_vectors(mrioc->pdev);
849824a1566SKashyap Desai 		goto out_failed;
850824a1566SKashyap Desai 	}
851824a1566SKashyap Desai 	for (i = 0; i < max_vectors; i++) {
852824a1566SKashyap Desai 		retval = mpi3mr_request_irq(mrioc, i);
853824a1566SKashyap Desai 		if (retval) {
854824a1566SKashyap Desai 			mrioc->intr_info_count = i;
855824a1566SKashyap Desai 			goto out_failed;
856824a1566SKashyap Desai 		}
857824a1566SKashyap Desai 	}
858fe6db615SSreekanth Reddy 	if (reset_devices || !setup_one)
859fe6db615SSreekanth Reddy 		mrioc->is_intr_info_set = true;
860824a1566SKashyap Desai 	mrioc->intr_info_count = max_vectors;
861824a1566SKashyap Desai 	mpi3mr_ioc_enable_intr(mrioc);
8622938beddSDan Carpenter 	return 0;
8632938beddSDan Carpenter 
864824a1566SKashyap Desai out_failed:
865824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
866824a1566SKashyap Desai 
867824a1566SKashyap Desai 	return retval;
868824a1566SKashyap Desai }
869824a1566SKashyap Desai 
870824a1566SKashyap Desai static const struct {
871824a1566SKashyap Desai 	enum mpi3mr_iocstate value;
872824a1566SKashyap Desai 	char *name;
873824a1566SKashyap Desai } mrioc_states[] = {
874824a1566SKashyap Desai 	{ MRIOC_STATE_READY, "ready" },
875824a1566SKashyap Desai 	{ MRIOC_STATE_FAULT, "fault" },
876824a1566SKashyap Desai 	{ MRIOC_STATE_RESET, "reset" },
877824a1566SKashyap Desai 	{ MRIOC_STATE_BECOMING_READY, "becoming ready" },
878824a1566SKashyap Desai 	{ MRIOC_STATE_RESET_REQUESTED, "reset requested" },
879824a1566SKashyap Desai 	{ MRIOC_STATE_UNRECOVERABLE, "unrecoverable error" },
880824a1566SKashyap Desai };
881824a1566SKashyap Desai 
882824a1566SKashyap Desai static const char *mpi3mr_iocstate_name(enum mpi3mr_iocstate mrioc_state)
883824a1566SKashyap Desai {
884824a1566SKashyap Desai 	int i;
885824a1566SKashyap Desai 	char *name = NULL;
886824a1566SKashyap Desai 
887824a1566SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mrioc_states); i++) {
888824a1566SKashyap Desai 		if (mrioc_states[i].value == mrioc_state) {
889824a1566SKashyap Desai 			name = mrioc_states[i].name;
890824a1566SKashyap Desai 			break;
891824a1566SKashyap Desai 		}
892824a1566SKashyap Desai 	}
893824a1566SKashyap Desai 	return name;
894824a1566SKashyap Desai }
895824a1566SKashyap Desai 
896f061178eSKashyap Desai /* Reset reason to name mapper structure*/
897f061178eSKashyap Desai static const struct {
898f061178eSKashyap Desai 	enum mpi3mr_reset_reason value;
899f061178eSKashyap Desai 	char *name;
900f061178eSKashyap Desai } mpi3mr_reset_reason_codes[] = {
901f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_BRINGUP, "timeout in bringup" },
902f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_FAULT_WATCH, "fault" },
903f5e6d5a3SSumit Saxena 	{ MPI3MR_RESET_FROM_APP, "application invocation" },
904f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EH_HOS, "error handling" },
905f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_TM_TIMEOUT, "TM timeout" },
906f5e6d5a3SSumit Saxena 	{ MPI3MR_RESET_FROM_APP_TIMEOUT, "application command timeout" },
907f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_MUR_FAILURE, "MUR failure" },
908f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_CTLR_CLEANUP, "timeout in controller cleanup" },
909f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_CIACTIV_FAULT, "component image activation fault" },
910f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_PE_TIMEOUT, "port enable timeout" },
911f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_TSU_TIMEOUT, "time stamp update timeout" },
912f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_DELREQQ_TIMEOUT, "delete request queue timeout" },
913f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_DELREPQ_TIMEOUT, "delete reply queue timeout" },
914f061178eSKashyap Desai 	{
915f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT,
916f061178eSKashyap Desai 		"create request queue timeout"
917f061178eSKashyap Desai 	},
918f061178eSKashyap Desai 	{
919f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT,
920f061178eSKashyap Desai 		"create reply queue timeout"
921f061178eSKashyap Desai 	},
922f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT, "IOC facts timeout" },
923f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_IOCINIT_TIMEOUT, "IOC init timeout" },
924f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT, "event notify timeout" },
925f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_EVTACK_TIMEOUT, "event acknowledgment timeout" },
926f061178eSKashyap Desai 	{
927f061178eSKashyap Desai 		MPI3MR_RESET_FROM_CIACTVRST_TIMER,
928f061178eSKashyap Desai 		"component image activation timeout"
929f061178eSKashyap Desai 	},
930f061178eSKashyap Desai 	{
931f061178eSKashyap Desai 		MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT,
932f061178eSKashyap Desai 		"get package version timeout"
933f061178eSKashyap Desai 	},
934f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_SYSFS, "sysfs invocation" },
935f061178eSKashyap Desai 	{ MPI3MR_RESET_FROM_SYSFS_TIMEOUT, "sysfs TM timeout" },
9365867b856SColin Ian King 	{ MPI3MR_RESET_FROM_FIRMWARE, "firmware asynchronous reset" },
93732d457d5SSreekanth Reddy 	{ MPI3MR_RESET_FROM_CFG_REQ_TIMEOUT, "configuration request timeout"},
9382bd37e28SSreekanth Reddy 	{ MPI3MR_RESET_FROM_SAS_TRANSPORT_TIMEOUT, "timeout of a SAS transport layer request" },
939f061178eSKashyap Desai };
940f061178eSKashyap Desai 
941f061178eSKashyap Desai /**
942f061178eSKashyap Desai  * mpi3mr_reset_rc_name - get reset reason code name
943f061178eSKashyap Desai  * @reason_code: reset reason code value
944f061178eSKashyap Desai  *
945f061178eSKashyap Desai  * Map reset reason to an NULL terminated ASCII string
946f061178eSKashyap Desai  *
947f061178eSKashyap Desai  * Return: name corresponding to reset reason value or NULL.
948f061178eSKashyap Desai  */
949f061178eSKashyap Desai static const char *mpi3mr_reset_rc_name(enum mpi3mr_reset_reason reason_code)
950f061178eSKashyap Desai {
951f061178eSKashyap Desai 	int i;
952f061178eSKashyap Desai 	char *name = NULL;
953f061178eSKashyap Desai 
954f061178eSKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_reason_codes); i++) {
955f061178eSKashyap Desai 		if (mpi3mr_reset_reason_codes[i].value == reason_code) {
956f061178eSKashyap Desai 			name = mpi3mr_reset_reason_codes[i].name;
957f061178eSKashyap Desai 			break;
958f061178eSKashyap Desai 		}
959f061178eSKashyap Desai 	}
960f061178eSKashyap Desai 	return name;
961f061178eSKashyap Desai }
962f061178eSKashyap Desai 
963f061178eSKashyap Desai /* Reset type to name mapper structure*/
964f061178eSKashyap Desai static const struct {
965f061178eSKashyap Desai 	u16 reset_type;
966f061178eSKashyap Desai 	char *name;
967f061178eSKashyap Desai } mpi3mr_reset_types[] = {
968f061178eSKashyap Desai 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, "soft" },
969f061178eSKashyap Desai 	{ MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, "diag fault" },
970f061178eSKashyap Desai };
971f061178eSKashyap Desai 
972f061178eSKashyap Desai /**
973f061178eSKashyap Desai  * mpi3mr_reset_type_name - get reset type name
974f061178eSKashyap Desai  * @reset_type: reset type value
975f061178eSKashyap Desai  *
976f061178eSKashyap Desai  * Map reset type to an NULL terminated ASCII string
977f061178eSKashyap Desai  *
978f061178eSKashyap Desai  * Return: name corresponding to reset type value or NULL.
979f061178eSKashyap Desai  */
980f061178eSKashyap Desai static const char *mpi3mr_reset_type_name(u16 reset_type)
981f061178eSKashyap Desai {
982f061178eSKashyap Desai 	int i;
983f061178eSKashyap Desai 	char *name = NULL;
984f061178eSKashyap Desai 
985f061178eSKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_reset_types); i++) {
986f061178eSKashyap Desai 		if (mpi3mr_reset_types[i].reset_type == reset_type) {
987f061178eSKashyap Desai 			name = mpi3mr_reset_types[i].name;
988f061178eSKashyap Desai 			break;
989f061178eSKashyap Desai 		}
990f061178eSKashyap Desai 	}
991f061178eSKashyap Desai 	return name;
992f061178eSKashyap Desai }
993f061178eSKashyap Desai 
994824a1566SKashyap Desai /**
995824a1566SKashyap Desai  * mpi3mr_print_fault_info - Display fault information
996824a1566SKashyap Desai  * @mrioc: Adapter instance reference
997824a1566SKashyap Desai  *
998824a1566SKashyap Desai  * Display the controller fault information if there is a
999824a1566SKashyap Desai  * controller fault.
1000824a1566SKashyap Desai  *
1001824a1566SKashyap Desai  * Return: Nothing.
1002824a1566SKashyap Desai  */
1003b64845a7SSreekanth Reddy void mpi3mr_print_fault_info(struct mpi3mr_ioc *mrioc)
1004824a1566SKashyap Desai {
1005824a1566SKashyap Desai 	u32 ioc_status, code, code1, code2, code3;
1006824a1566SKashyap Desai 
1007824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1008824a1566SKashyap Desai 
1009824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
1010824a1566SKashyap Desai 		code = readl(&mrioc->sysif_regs->fault);
1011824a1566SKashyap Desai 		code1 = readl(&mrioc->sysif_regs->fault_info[0]);
1012824a1566SKashyap Desai 		code2 = readl(&mrioc->sysif_regs->fault_info[1]);
1013824a1566SKashyap Desai 		code3 = readl(&mrioc->sysif_regs->fault_info[2]);
1014824a1566SKashyap Desai 
1015824a1566SKashyap Desai 		ioc_info(mrioc,
1016824a1566SKashyap Desai 		    "fault code(0x%08X): Additional code: (0x%08X:0x%08X:0x%08X)\n",
1017824a1566SKashyap Desai 		    code, code1, code2, code3);
1018824a1566SKashyap Desai 	}
1019824a1566SKashyap Desai }
1020824a1566SKashyap Desai 
1021824a1566SKashyap Desai /**
1022824a1566SKashyap Desai  * mpi3mr_get_iocstate - Get IOC State
1023824a1566SKashyap Desai  * @mrioc: Adapter instance reference
1024824a1566SKashyap Desai  *
1025824a1566SKashyap Desai  * Return a proper IOC state enum based on the IOC status and
1026824a1566SKashyap Desai  * IOC configuration and unrcoverable state of the controller.
1027824a1566SKashyap Desai  *
1028824a1566SKashyap Desai  * Return: Current IOC state.
1029824a1566SKashyap Desai  */
1030824a1566SKashyap Desai enum mpi3mr_iocstate mpi3mr_get_iocstate(struct mpi3mr_ioc *mrioc)
1031824a1566SKashyap Desai {
1032824a1566SKashyap Desai 	u32 ioc_status, ioc_config;
1033824a1566SKashyap Desai 	u8 ready, enabled;
1034824a1566SKashyap Desai 
1035824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1036824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1037824a1566SKashyap Desai 
1038824a1566SKashyap Desai 	if (mrioc->unrecoverable)
1039824a1566SKashyap Desai 		return MRIOC_STATE_UNRECOVERABLE;
1040824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)
1041824a1566SKashyap Desai 		return MRIOC_STATE_FAULT;
1042824a1566SKashyap Desai 
1043824a1566SKashyap Desai 	ready = (ioc_status & MPI3_SYSIF_IOC_STATUS_READY);
1044824a1566SKashyap Desai 	enabled = (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC);
1045824a1566SKashyap Desai 
1046824a1566SKashyap Desai 	if (ready && enabled)
1047824a1566SKashyap Desai 		return MRIOC_STATE_READY;
1048824a1566SKashyap Desai 	if ((!ready) && (!enabled))
1049824a1566SKashyap Desai 		return MRIOC_STATE_RESET;
1050824a1566SKashyap Desai 	if ((!ready) && (enabled))
1051824a1566SKashyap Desai 		return MRIOC_STATE_BECOMING_READY;
1052824a1566SKashyap Desai 
1053824a1566SKashyap Desai 	return MRIOC_STATE_RESET_REQUESTED;
1054824a1566SKashyap Desai }
1055824a1566SKashyap Desai 
1056824a1566SKashyap Desai /**
1057824a1566SKashyap Desai  * mpi3mr_clear_reset_history - clear reset history
1058824a1566SKashyap Desai  * @mrioc: Adapter instance reference
1059824a1566SKashyap Desai  *
1060824a1566SKashyap Desai  * Write the reset history bit in IOC status to clear the bit,
1061824a1566SKashyap Desai  * if it is already set.
1062824a1566SKashyap Desai  *
1063824a1566SKashyap Desai  * Return: Nothing.
1064824a1566SKashyap Desai  */
1065824a1566SKashyap Desai static inline void mpi3mr_clear_reset_history(struct mpi3mr_ioc *mrioc)
1066824a1566SKashyap Desai {
1067824a1566SKashyap Desai 	u32 ioc_status;
1068824a1566SKashyap Desai 
1069824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1070824a1566SKashyap Desai 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
1071824a1566SKashyap Desai 		writel(ioc_status, &mrioc->sysif_regs->ioc_status);
1072824a1566SKashyap Desai }
1073824a1566SKashyap Desai 
1074824a1566SKashyap Desai /**
1075824a1566SKashyap Desai  * mpi3mr_issue_and_process_mur - Message unit Reset handler
1076824a1566SKashyap Desai  * @mrioc: Adapter instance reference
1077824a1566SKashyap Desai  * @reset_reason: Reset reason code
1078824a1566SKashyap Desai  *
1079824a1566SKashyap Desai  * Issue Message unit Reset to the controller and wait for it to
1080824a1566SKashyap Desai  * be complete.
1081824a1566SKashyap Desai  *
1082824a1566SKashyap Desai  * Return: 0 on success, -1 on failure.
1083824a1566SKashyap Desai  */
1084824a1566SKashyap Desai static int mpi3mr_issue_and_process_mur(struct mpi3mr_ioc *mrioc,
1085824a1566SKashyap Desai 	u32 reset_reason)
1086824a1566SKashyap Desai {
1087824a1566SKashyap Desai 	u32 ioc_config, timeout, ioc_status;
1088824a1566SKashyap Desai 	int retval = -1;
1089824a1566SKashyap Desai 
1090824a1566SKashyap Desai 	ioc_info(mrioc, "Issuing Message unit Reset(MUR)\n");
1091824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
1092824a1566SKashyap Desai 		ioc_info(mrioc, "IOC is unrecoverable MUR not issued\n");
1093824a1566SKashyap Desai 		return retval;
1094824a1566SKashyap Desai 	}
1095824a1566SKashyap Desai 	mpi3mr_clear_reset_history(mrioc);
1096824a1566SKashyap Desai 	writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
1097824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1098824a1566SKashyap Desai 	ioc_config &= ~MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
1099824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1100824a1566SKashyap Desai 
1101b64845a7SSreekanth Reddy 	timeout = MPI3MR_RESET_ACK_TIMEOUT * 10;
1102824a1566SKashyap Desai 	do {
1103824a1566SKashyap Desai 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1104824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)) {
1105824a1566SKashyap Desai 			mpi3mr_clear_reset_history(mrioc);
1106824a1566SKashyap Desai 			break;
1107824a1566SKashyap Desai 		}
1108b64845a7SSreekanth Reddy 		if (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) {
1109b64845a7SSreekanth Reddy 			mpi3mr_print_fault_info(mrioc);
1110b64845a7SSreekanth Reddy 			break;
1111824a1566SKashyap Desai 		}
1112824a1566SKashyap Desai 		msleep(100);
1113824a1566SKashyap Desai 	} while (--timeout);
1114824a1566SKashyap Desai 
1115824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1116b64845a7SSreekanth Reddy 	if (timeout && !((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
1117b64845a7SSreekanth Reddy 	      (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT) ||
1118b64845a7SSreekanth Reddy 	      (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
1119b64845a7SSreekanth Reddy 		retval = 0;
1120824a1566SKashyap Desai 
1121824a1566SKashyap Desai 	ioc_info(mrioc, "Base IOC Sts/Config after %s MUR is (0x%x)/(0x%x)\n",
1122824a1566SKashyap Desai 	    (!retval) ? "successful" : "failed", ioc_status, ioc_config);
1123824a1566SKashyap Desai 	return retval;
1124824a1566SKashyap Desai }
1125824a1566SKashyap Desai 
1126824a1566SKashyap Desai /**
1127c5758fc7SSreekanth Reddy  * mpi3mr_revalidate_factsdata - validate IOCFacts parameters
1128c5758fc7SSreekanth Reddy  * during reset/resume
1129c5758fc7SSreekanth Reddy  * @mrioc: Adapter instance reference
1130c5758fc7SSreekanth Reddy  *
1131c5758fc7SSreekanth Reddy  * Return zero if the new IOCFacts parameters value is compatible with
1132c5758fc7SSreekanth Reddy  * older values else return -EPERM
1133c5758fc7SSreekanth Reddy  */
1134c5758fc7SSreekanth Reddy static int
1135c5758fc7SSreekanth Reddy mpi3mr_revalidate_factsdata(struct mpi3mr_ioc *mrioc)
1136c5758fc7SSreekanth Reddy {
1137c5758fc7SSreekanth Reddy 	void *removepend_bitmap;
1138c5758fc7SSreekanth Reddy 
1139c5758fc7SSreekanth Reddy 	if (mrioc->facts.reply_sz > mrioc->reply_sz) {
1140c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1141c5758fc7SSreekanth Reddy 		    "cannot increase reply size from %d to %d\n",
1142c5758fc7SSreekanth Reddy 		    mrioc->reply_sz, mrioc->facts.reply_sz);
1143c5758fc7SSreekanth Reddy 		return -EPERM;
1144c5758fc7SSreekanth Reddy 	}
1145c5758fc7SSreekanth Reddy 
1146c5758fc7SSreekanth Reddy 	if (mrioc->facts.max_op_reply_q < mrioc->num_op_reply_q) {
1147c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1148c5758fc7SSreekanth Reddy 		    "cannot reduce number of operational reply queues from %d to %d\n",
1149c5758fc7SSreekanth Reddy 		    mrioc->num_op_reply_q,
1150c5758fc7SSreekanth Reddy 		    mrioc->facts.max_op_reply_q);
1151c5758fc7SSreekanth Reddy 		return -EPERM;
1152c5758fc7SSreekanth Reddy 	}
1153c5758fc7SSreekanth Reddy 
1154c5758fc7SSreekanth Reddy 	if (mrioc->facts.max_op_req_q < mrioc->num_op_req_q) {
1155c5758fc7SSreekanth Reddy 		ioc_err(mrioc,
1156c5758fc7SSreekanth Reddy 		    "cannot reduce number of operational request queues from %d to %d\n",
1157c5758fc7SSreekanth Reddy 		    mrioc->num_op_req_q, mrioc->facts.max_op_req_q);
1158c5758fc7SSreekanth Reddy 		return -EPERM;
1159c5758fc7SSreekanth Reddy 	}
1160c5758fc7SSreekanth Reddy 
1161c4723e68SSreekanth Reddy 	if ((mrioc->sas_transport_enabled) && (mrioc->facts.ioc_capabilities &
1162c4723e68SSreekanth Reddy 	    MPI3_IOCFACTS_CAPABILITY_MULTIPATH_ENABLED))
1163c4723e68SSreekanth Reddy 		ioc_err(mrioc,
1164c4723e68SSreekanth Reddy 		    "critical error: multipath capability is enabled at the\n"
1165c4723e68SSreekanth Reddy 		    "\tcontroller while sas transport support is enabled at the\n"
1166c4723e68SSreekanth Reddy 		    "\tdriver, please reboot the system or reload the driver\n");
1167c4723e68SSreekanth Reddy 
1168339e6156SShin'ichiro Kawasaki 	if (mrioc->facts.max_devhandle > mrioc->dev_handle_bitmap_bits) {
1169339e6156SShin'ichiro Kawasaki 		removepend_bitmap = bitmap_zalloc(mrioc->facts.max_devhandle,
1170339e6156SShin'ichiro Kawasaki 						  GFP_KERNEL);
1171c5758fc7SSreekanth Reddy 		if (!removepend_bitmap) {
1172c5758fc7SSreekanth Reddy 			ioc_err(mrioc,
1173339e6156SShin'ichiro Kawasaki 				"failed to increase removepend_bitmap bits from %d to %d\n",
1174339e6156SShin'ichiro Kawasaki 				mrioc->dev_handle_bitmap_bits,
1175339e6156SShin'ichiro Kawasaki 				mrioc->facts.max_devhandle);
1176c5758fc7SSreekanth Reddy 			return -EPERM;
1177c5758fc7SSreekanth Reddy 		}
1178339e6156SShin'ichiro Kawasaki 		bitmap_free(mrioc->removepend_bitmap);
1179c5758fc7SSreekanth Reddy 		mrioc->removepend_bitmap = removepend_bitmap;
1180c5758fc7SSreekanth Reddy 		ioc_info(mrioc,
1181339e6156SShin'ichiro Kawasaki 			 "increased bits of dev_handle_bitmap from %d to %d\n",
1182339e6156SShin'ichiro Kawasaki 			 mrioc->dev_handle_bitmap_bits,
1183339e6156SShin'ichiro Kawasaki 			 mrioc->facts.max_devhandle);
1184339e6156SShin'ichiro Kawasaki 		mrioc->dev_handle_bitmap_bits = mrioc->facts.max_devhandle;
1185c5758fc7SSreekanth Reddy 	}
1186c5758fc7SSreekanth Reddy 
1187c5758fc7SSreekanth Reddy 	return 0;
1188c5758fc7SSreekanth Reddy }
1189c5758fc7SSreekanth Reddy 
1190c5758fc7SSreekanth Reddy /**
1191824a1566SKashyap Desai  * mpi3mr_bring_ioc_ready - Bring controller to ready state
1192824a1566SKashyap Desai  * @mrioc: Adapter instance reference
1193824a1566SKashyap Desai  *
1194824a1566SKashyap Desai  * Set Enable IOC bit in IOC configuration register and wait for
1195824a1566SKashyap Desai  * the controller to become ready.
1196824a1566SKashyap Desai  *
119759bd9cfeSSreekanth Reddy  * Return: 0 on success, appropriate error on failure.
1198824a1566SKashyap Desai  */
1199824a1566SKashyap Desai static int mpi3mr_bring_ioc_ready(struct mpi3mr_ioc *mrioc)
1200824a1566SKashyap Desai {
120159bd9cfeSSreekanth Reddy 	u32 ioc_config, ioc_status, timeout;
120259bd9cfeSSreekanth Reddy 	int retval = 0;
120359bd9cfeSSreekanth Reddy 	enum mpi3mr_iocstate ioc_state;
120459bd9cfeSSreekanth Reddy 	u64 base_info;
1205824a1566SKashyap Desai 
120659bd9cfeSSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
120759bd9cfeSSreekanth Reddy 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
120859bd9cfeSSreekanth Reddy 	base_info = lo_hi_readq(&mrioc->sysif_regs->ioc_information);
120959bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "ioc_status(0x%08x), ioc_config(0x%08x), ioc_info(0x%016llx) at the bringup\n",
121059bd9cfeSSreekanth Reddy 	    ioc_status, ioc_config, base_info);
121159bd9cfeSSreekanth Reddy 
121259bd9cfeSSreekanth Reddy 	/*The timeout value is in 2sec unit, changing it to seconds*/
121359bd9cfeSSreekanth Reddy 	mrioc->ready_timeout =
121459bd9cfeSSreekanth Reddy 	    ((base_info & MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_MASK) >>
121559bd9cfeSSreekanth Reddy 	    MPI3_SYSIF_IOC_INFO_LOW_TIMEOUT_SHIFT) * 2;
121659bd9cfeSSreekanth Reddy 
121759bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "ready timeout: %d seconds\n", mrioc->ready_timeout);
121859bd9cfeSSreekanth Reddy 
121959bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
122059bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "controller is in %s state during detection\n",
122159bd9cfeSSreekanth Reddy 	    mpi3mr_iocstate_name(ioc_state));
122259bd9cfeSSreekanth Reddy 
122359bd9cfeSSreekanth Reddy 	if (ioc_state == MRIOC_STATE_BECOMING_READY ||
122459bd9cfeSSreekanth Reddy 	    ioc_state == MRIOC_STATE_RESET_REQUESTED) {
122559bd9cfeSSreekanth Reddy 		timeout = mrioc->ready_timeout * 10;
122659bd9cfeSSreekanth Reddy 		do {
122759bd9cfeSSreekanth Reddy 			msleep(100);
122859bd9cfeSSreekanth Reddy 		} while (--timeout);
122959bd9cfeSSreekanth Reddy 
1230f2a79d20SSreekanth Reddy 		if (!pci_device_is_present(mrioc->pdev)) {
1231f2a79d20SSreekanth Reddy 			mrioc->unrecoverable = 1;
1232f2a79d20SSreekanth Reddy 			ioc_err(mrioc,
1233f2a79d20SSreekanth Reddy 			    "controller is not present while waiting to reset\n");
1234f2a79d20SSreekanth Reddy 			retval = -1;
1235f2a79d20SSreekanth Reddy 			goto out_device_not_present;
1236f2a79d20SSreekanth Reddy 		}
1237f2a79d20SSreekanth Reddy 
123859bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
123959bd9cfeSSreekanth Reddy 		ioc_info(mrioc,
124059bd9cfeSSreekanth Reddy 		    "controller is in %s state after waiting to reset\n",
124159bd9cfeSSreekanth Reddy 		    mpi3mr_iocstate_name(ioc_state));
124259bd9cfeSSreekanth Reddy 	}
124359bd9cfeSSreekanth Reddy 
124459bd9cfeSSreekanth Reddy 	if (ioc_state == MRIOC_STATE_READY) {
124559bd9cfeSSreekanth Reddy 		ioc_info(mrioc, "issuing message unit reset (MUR) to bring to reset state\n");
124659bd9cfeSSreekanth Reddy 		retval = mpi3mr_issue_and_process_mur(mrioc,
124759bd9cfeSSreekanth Reddy 		    MPI3MR_RESET_FROM_BRINGUP);
124859bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
124959bd9cfeSSreekanth Reddy 		if (retval)
125059bd9cfeSSreekanth Reddy 			ioc_err(mrioc,
125159bd9cfeSSreekanth Reddy 			    "message unit reset failed with error %d current state %s\n",
125259bd9cfeSSreekanth Reddy 			    retval, mpi3mr_iocstate_name(ioc_state));
125359bd9cfeSSreekanth Reddy 	}
125459bd9cfeSSreekanth Reddy 	if (ioc_state != MRIOC_STATE_RESET) {
125559bd9cfeSSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
125659bd9cfeSSreekanth Reddy 		ioc_info(mrioc, "issuing soft reset to bring to reset state\n");
125759bd9cfeSSreekanth Reddy 		retval = mpi3mr_issue_reset(mrioc,
125859bd9cfeSSreekanth Reddy 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
125959bd9cfeSSreekanth Reddy 		    MPI3MR_RESET_FROM_BRINGUP);
126059bd9cfeSSreekanth Reddy 		if (retval) {
126159bd9cfeSSreekanth Reddy 			ioc_err(mrioc,
126259bd9cfeSSreekanth Reddy 			    "soft reset failed with error %d\n", retval);
126359bd9cfeSSreekanth Reddy 			goto out_failed;
126459bd9cfeSSreekanth Reddy 		}
126559bd9cfeSSreekanth Reddy 	}
126659bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
126759bd9cfeSSreekanth Reddy 	if (ioc_state != MRIOC_STATE_RESET) {
126859bd9cfeSSreekanth Reddy 		ioc_err(mrioc,
126959bd9cfeSSreekanth Reddy 		    "cannot bring controller to reset state, current state: %s\n",
127059bd9cfeSSreekanth Reddy 		    mpi3mr_iocstate_name(ioc_state));
127159bd9cfeSSreekanth Reddy 		goto out_failed;
127259bd9cfeSSreekanth Reddy 	}
127359bd9cfeSSreekanth Reddy 	mpi3mr_clear_reset_history(mrioc);
127459bd9cfeSSreekanth Reddy 	retval = mpi3mr_setup_admin_qpair(mrioc);
127559bd9cfeSSreekanth Reddy 	if (retval) {
127659bd9cfeSSreekanth Reddy 		ioc_err(mrioc, "failed to setup admin queues: error %d\n",
127759bd9cfeSSreekanth Reddy 		    retval);
127859bd9cfeSSreekanth Reddy 		goto out_failed;
127959bd9cfeSSreekanth Reddy 	}
128059bd9cfeSSreekanth Reddy 
128159bd9cfeSSreekanth Reddy 	ioc_info(mrioc, "bringing controller to ready state\n");
1282824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1283824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC;
1284824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1285824a1566SKashyap Desai 
1286824a1566SKashyap Desai 	timeout = mrioc->ready_timeout * 10;
1287824a1566SKashyap Desai 	do {
128859bd9cfeSSreekanth Reddy 		ioc_state = mpi3mr_get_iocstate(mrioc);
128959bd9cfeSSreekanth Reddy 		if (ioc_state == MRIOC_STATE_READY) {
129059bd9cfeSSreekanth Reddy 			ioc_info(mrioc,
12915867b856SColin Ian King 			    "successfully transitioned to %s state\n",
129259bd9cfeSSreekanth Reddy 			    mpi3mr_iocstate_name(ioc_state));
1293824a1566SKashyap Desai 			return 0;
129459bd9cfeSSreekanth Reddy 		}
1295f2a79d20SSreekanth Reddy 		if (!pci_device_is_present(mrioc->pdev)) {
1296f2a79d20SSreekanth Reddy 			mrioc->unrecoverable = 1;
1297f2a79d20SSreekanth Reddy 			ioc_err(mrioc,
1298f2a79d20SSreekanth Reddy 			    "controller is not present at the bringup\n");
1299f2a79d20SSreekanth Reddy 			retval = -1;
1300f2a79d20SSreekanth Reddy 			goto out_device_not_present;
1301f2a79d20SSreekanth Reddy 		}
1302824a1566SKashyap Desai 		msleep(100);
1303824a1566SKashyap Desai 	} while (--timeout);
1304824a1566SKashyap Desai 
130559bd9cfeSSreekanth Reddy out_failed:
130659bd9cfeSSreekanth Reddy 	ioc_state = mpi3mr_get_iocstate(mrioc);
130759bd9cfeSSreekanth Reddy 	ioc_err(mrioc,
130859bd9cfeSSreekanth Reddy 	    "failed to bring to ready state,  current state: %s\n",
130959bd9cfeSSreekanth Reddy 	    mpi3mr_iocstate_name(ioc_state));
1310f2a79d20SSreekanth Reddy out_device_not_present:
131159bd9cfeSSreekanth Reddy 	return retval;
1312824a1566SKashyap Desai }
1313824a1566SKashyap Desai 
1314824a1566SKashyap Desai /**
1315f061178eSKashyap Desai  * mpi3mr_soft_reset_success - Check softreset is success or not
1316f061178eSKashyap Desai  * @ioc_status: IOC status register value
1317f061178eSKashyap Desai  * @ioc_config: IOC config register value
1318f061178eSKashyap Desai  *
1319f061178eSKashyap Desai  * Check whether the soft reset is successful or not based on
1320f061178eSKashyap Desai  * IOC status and IOC config register values.
1321f061178eSKashyap Desai  *
1322f061178eSKashyap Desai  * Return: True when the soft reset is success, false otherwise.
1323f061178eSKashyap Desai  */
1324f061178eSKashyap Desai static inline bool
1325f061178eSKashyap Desai mpi3mr_soft_reset_success(u32 ioc_status, u32 ioc_config)
1326f061178eSKashyap Desai {
1327f061178eSKashyap Desai 	if (!((ioc_status & MPI3_SYSIF_IOC_STATUS_READY) ||
1328f061178eSKashyap Desai 	    (ioc_config & MPI3_SYSIF_IOC_CONFIG_ENABLE_IOC)))
1329f061178eSKashyap Desai 		return true;
1330f061178eSKashyap Desai 	return false;
1331f061178eSKashyap Desai }
1332f061178eSKashyap Desai 
1333f061178eSKashyap Desai /**
1334f061178eSKashyap Desai  * mpi3mr_diagfault_success - Check diag fault is success or not
1335f061178eSKashyap Desai  * @mrioc: Adapter reference
1336f061178eSKashyap Desai  * @ioc_status: IOC status register value
1337f061178eSKashyap Desai  *
1338f061178eSKashyap Desai  * Check whether the controller hit diag reset fault code.
1339f061178eSKashyap Desai  *
1340f061178eSKashyap Desai  * Return: True when there is diag fault, false otherwise.
1341f061178eSKashyap Desai  */
1342f061178eSKashyap Desai static inline bool mpi3mr_diagfault_success(struct mpi3mr_ioc *mrioc,
1343f061178eSKashyap Desai 	u32 ioc_status)
1344f061178eSKashyap Desai {
1345f061178eSKashyap Desai 	u32 fault;
1346f061178eSKashyap Desai 
1347f061178eSKashyap Desai 	if (!(ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT))
1348f061178eSKashyap Desai 		return false;
1349f061178eSKashyap Desai 	fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK;
1350b64845a7SSreekanth Reddy 	if (fault == MPI3_SYSIF_FAULT_CODE_DIAG_FAULT_RESET) {
1351b64845a7SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
1352f061178eSKashyap Desai 		return true;
1353b64845a7SSreekanth Reddy 	}
1354f061178eSKashyap Desai 	return false;
1355f061178eSKashyap Desai }
1356f061178eSKashyap Desai 
1357f061178eSKashyap Desai /**
1358824a1566SKashyap Desai  * mpi3mr_set_diagsave - Set diag save bit for snapdump
1359824a1566SKashyap Desai  * @mrioc: Adapter reference
1360824a1566SKashyap Desai  *
1361824a1566SKashyap Desai  * Set diag save bit in IOC configuration register to enable
1362824a1566SKashyap Desai  * snapdump.
1363824a1566SKashyap Desai  *
1364824a1566SKashyap Desai  * Return: Nothing.
1365824a1566SKashyap Desai  */
1366824a1566SKashyap Desai static inline void mpi3mr_set_diagsave(struct mpi3mr_ioc *mrioc)
1367824a1566SKashyap Desai {
1368824a1566SKashyap Desai 	u32 ioc_config;
1369824a1566SKashyap Desai 
1370824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1371824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DIAG_SAVE;
1372824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
1373824a1566SKashyap Desai }
1374824a1566SKashyap Desai 
1375824a1566SKashyap Desai /**
1376824a1566SKashyap Desai  * mpi3mr_issue_reset - Issue reset to the controller
1377824a1566SKashyap Desai  * @mrioc: Adapter reference
1378824a1566SKashyap Desai  * @reset_type: Reset type
1379824a1566SKashyap Desai  * @reset_reason: Reset reason code
1380824a1566SKashyap Desai  *
1381f061178eSKashyap Desai  * Unlock the host diagnostic registers and write the specific
1382f061178eSKashyap Desai  * reset type to that, wait for reset acknowledgment from the
1383f061178eSKashyap Desai  * controller, if the reset is not successful retry for the
1384f061178eSKashyap Desai  * predefined number of times.
1385824a1566SKashyap Desai  *
1386824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
1387824a1566SKashyap Desai  */
1388824a1566SKashyap Desai static int mpi3mr_issue_reset(struct mpi3mr_ioc *mrioc, u16 reset_type,
1389824a1566SKashyap Desai 	u32 reset_reason)
1390824a1566SKashyap Desai {
1391f061178eSKashyap Desai 	int retval = -1;
1392b64845a7SSreekanth Reddy 	u8 unlock_retry_count = 0;
1393b64845a7SSreekanth Reddy 	u32 host_diagnostic, ioc_status, ioc_config;
1394b64845a7SSreekanth Reddy 	u32 timeout = MPI3MR_RESET_ACK_TIMEOUT * 10;
1395f061178eSKashyap Desai 
1396f061178eSKashyap Desai 	if ((reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET) &&
1397f061178eSKashyap Desai 	    (reset_type != MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT))
1398b64845a7SSreekanth Reddy 		return retval;
1399f061178eSKashyap Desai 	if (mrioc->unrecoverable)
1400b64845a7SSreekanth Reddy 		return retval;
1401b64845a7SSreekanth Reddy 	if (reset_reason == MPI3MR_RESET_FROM_FIRMWARE) {
1402b64845a7SSreekanth Reddy 		retval = 0;
1403b64845a7SSreekanth Reddy 		return retval;
1404b64845a7SSreekanth Reddy 	}
1405b64845a7SSreekanth Reddy 
1406b64845a7SSreekanth Reddy 	ioc_info(mrioc, "%s reset due to %s(0x%x)\n",
1407b64845a7SSreekanth Reddy 	    mpi3mr_reset_type_name(reset_type),
1408b64845a7SSreekanth Reddy 	    mpi3mr_reset_rc_name(reset_reason), reset_reason);
1409b64845a7SSreekanth Reddy 
1410f061178eSKashyap Desai 	mpi3mr_clear_reset_history(mrioc);
1411f061178eSKashyap Desai 	do {
1412f061178eSKashyap Desai 		ioc_info(mrioc,
1413f061178eSKashyap Desai 		    "Write magic sequence to unlock host diag register (retry=%d)\n",
1414f061178eSKashyap Desai 		    ++unlock_retry_count);
1415f061178eSKashyap Desai 		if (unlock_retry_count >= MPI3MR_HOSTDIAG_UNLOCK_RETRY_COUNT) {
1416b64845a7SSreekanth Reddy 			ioc_err(mrioc,
1417b64845a7SSreekanth Reddy 			    "%s reset failed due to unlock failure, host_diagnostic(0x%08x)\n",
1418b64845a7SSreekanth Reddy 			    mpi3mr_reset_type_name(reset_type),
1419b64845a7SSreekanth Reddy 			    host_diagnostic);
1420f061178eSKashyap Desai 			mrioc->unrecoverable = 1;
1421b64845a7SSreekanth Reddy 			return retval;
1422f061178eSKashyap Desai 		}
1423f061178eSKashyap Desai 
1424f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_FLUSH,
1425f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1426f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_1ST,
1427f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1428f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
1429f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1430f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_3RD,
1431f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1432f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_4TH,
1433f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1434f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_5TH,
1435f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1436f061178eSKashyap Desai 		writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_6TH,
1437f061178eSKashyap Desai 		    &mrioc->sysif_regs->write_sequence);
1438f061178eSKashyap Desai 		usleep_range(1000, 1100);
1439f061178eSKashyap Desai 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
1440f061178eSKashyap Desai 		ioc_info(mrioc,
1441f061178eSKashyap Desai 		    "wrote magic sequence: retry_count(%d), host_diagnostic(0x%08x)\n",
1442f061178eSKashyap Desai 		    unlock_retry_count, host_diagnostic);
1443f061178eSKashyap Desai 	} while (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_DIAG_WRITE_ENABLE));
1444f061178eSKashyap Desai 
1445f061178eSKashyap Desai 	writel(reset_reason, &mrioc->sysif_regs->scratchpad[0]);
1446f061178eSKashyap Desai 	writel(host_diagnostic | reset_type,
1447f061178eSKashyap Desai 	    &mrioc->sysif_regs->host_diagnostic);
1448b64845a7SSreekanth Reddy 	switch (reset_type) {
1449b64845a7SSreekanth Reddy 	case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET:
1450f061178eSKashyap Desai 		do {
1451f061178eSKashyap Desai 			ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1452f061178eSKashyap Desai 			ioc_config =
1453f061178eSKashyap Desai 			    readl(&mrioc->sysif_regs->ioc_configuration);
1454b64845a7SSreekanth Reddy 			if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY)
1455b64845a7SSreekanth Reddy 			    && mpi3mr_soft_reset_success(ioc_status, ioc_config)
1456b64845a7SSreekanth Reddy 			    ) {
1457b64845a7SSreekanth Reddy 				mpi3mr_clear_reset_history(mrioc);
1458f061178eSKashyap Desai 				retval = 0;
1459f061178eSKashyap Desai 				break;
1460f061178eSKashyap Desai 			}
1461f061178eSKashyap Desai 			msleep(100);
1462f061178eSKashyap Desai 		} while (--timeout);
1463b64845a7SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
1464b64845a7SSreekanth Reddy 		break;
1465b64845a7SSreekanth Reddy 	case MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT:
1466f061178eSKashyap Desai 		do {
1467f061178eSKashyap Desai 			ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1468f061178eSKashyap Desai 			if (mpi3mr_diagfault_success(mrioc, ioc_status)) {
1469f061178eSKashyap Desai 				retval = 0;
1470f061178eSKashyap Desai 				break;
1471f061178eSKashyap Desai 			}
1472f061178eSKashyap Desai 			msleep(100);
1473f061178eSKashyap Desai 		} while (--timeout);
1474b64845a7SSreekanth Reddy 		break;
1475b64845a7SSreekanth Reddy 	default:
1476b64845a7SSreekanth Reddy 		break;
1477b64845a7SSreekanth Reddy 	}
1478b64845a7SSreekanth Reddy 
1479f061178eSKashyap Desai 	writel(MPI3_SYSIF_WRITE_SEQUENCE_KEY_VALUE_2ND,
1480f061178eSKashyap Desai 	    &mrioc->sysif_regs->write_sequence);
1481f061178eSKashyap Desai 
1482f061178eSKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
1483b64845a7SSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
1484f061178eSKashyap Desai 	ioc_info(mrioc,
1485b64845a7SSreekanth Reddy 	    "ioc_status/ioc_onfig after %s reset is (0x%x)/(0x%x)\n",
1486f061178eSKashyap Desai 	    (!retval)?"successful":"failed", ioc_status,
1487f061178eSKashyap Desai 	    ioc_config);
1488b64845a7SSreekanth Reddy 	if (retval)
1489b64845a7SSreekanth Reddy 		mrioc->unrecoverable = 1;
1490f061178eSKashyap Desai 	return retval;
1491824a1566SKashyap Desai }
1492824a1566SKashyap Desai 
1493824a1566SKashyap Desai /**
1494824a1566SKashyap Desai  * mpi3mr_admin_request_post - Post request to admin queue
1495824a1566SKashyap Desai  * @mrioc: Adapter reference
1496824a1566SKashyap Desai  * @admin_req: MPI3 request
1497824a1566SKashyap Desai  * @admin_req_sz: Request size
1498824a1566SKashyap Desai  * @ignore_reset: Ignore reset in process
1499824a1566SKashyap Desai  *
1500824a1566SKashyap Desai  * Post the MPI3 request into admin request queue and
1501824a1566SKashyap Desai  * inform the controller, if the queue is full return
1502824a1566SKashyap Desai  * appropriate error.
1503824a1566SKashyap Desai  *
1504824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
1505824a1566SKashyap Desai  */
1506824a1566SKashyap Desai int mpi3mr_admin_request_post(struct mpi3mr_ioc *mrioc, void *admin_req,
1507824a1566SKashyap Desai 	u16 admin_req_sz, u8 ignore_reset)
1508824a1566SKashyap Desai {
1509824a1566SKashyap Desai 	u16 areq_pi = 0, areq_ci = 0, max_entries = 0;
1510824a1566SKashyap Desai 	int retval = 0;
1511824a1566SKashyap Desai 	unsigned long flags;
1512824a1566SKashyap Desai 	u8 *areq_entry;
1513824a1566SKashyap Desai 
1514824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
1515824a1566SKashyap Desai 		ioc_err(mrioc, "%s : Unrecoverable controller\n", __func__);
1516824a1566SKashyap Desai 		return -EFAULT;
1517824a1566SKashyap Desai 	}
1518824a1566SKashyap Desai 
1519824a1566SKashyap Desai 	spin_lock_irqsave(&mrioc->admin_req_lock, flags);
1520824a1566SKashyap Desai 	areq_pi = mrioc->admin_req_pi;
1521824a1566SKashyap Desai 	areq_ci = mrioc->admin_req_ci;
1522824a1566SKashyap Desai 	max_entries = mrioc->num_admin_req;
1523824a1566SKashyap Desai 	if ((areq_ci == (areq_pi + 1)) || ((!areq_ci) &&
1524824a1566SKashyap Desai 	    (areq_pi == (max_entries - 1)))) {
1525824a1566SKashyap Desai 		ioc_err(mrioc, "AdminReqQ full condition detected\n");
1526824a1566SKashyap Desai 		retval = -EAGAIN;
1527824a1566SKashyap Desai 		goto out;
1528824a1566SKashyap Desai 	}
1529824a1566SKashyap Desai 	if (!ignore_reset && mrioc->reset_in_progress) {
1530824a1566SKashyap Desai 		ioc_err(mrioc, "AdminReqQ submit reset in progress\n");
1531824a1566SKashyap Desai 		retval = -EAGAIN;
1532824a1566SKashyap Desai 		goto out;
1533824a1566SKashyap Desai 	}
1534824a1566SKashyap Desai 	areq_entry = (u8 *)mrioc->admin_req_base +
1535824a1566SKashyap Desai 	    (areq_pi * MPI3MR_ADMIN_REQ_FRAME_SZ);
1536824a1566SKashyap Desai 	memset(areq_entry, 0, MPI3MR_ADMIN_REQ_FRAME_SZ);
1537824a1566SKashyap Desai 	memcpy(areq_entry, (u8 *)admin_req, admin_req_sz);
1538824a1566SKashyap Desai 
1539824a1566SKashyap Desai 	if (++areq_pi == max_entries)
1540824a1566SKashyap Desai 		areq_pi = 0;
1541824a1566SKashyap Desai 	mrioc->admin_req_pi = areq_pi;
1542824a1566SKashyap Desai 
1543824a1566SKashyap Desai 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
1544824a1566SKashyap Desai 
1545824a1566SKashyap Desai out:
1546824a1566SKashyap Desai 	spin_unlock_irqrestore(&mrioc->admin_req_lock, flags);
1547824a1566SKashyap Desai 
1548824a1566SKashyap Desai 	return retval;
1549824a1566SKashyap Desai }
1550824a1566SKashyap Desai 
1551824a1566SKashyap Desai /**
1552c9566231SKashyap Desai  * mpi3mr_free_op_req_q_segments - free request memory segments
1553c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1554c9566231SKashyap Desai  * @q_idx: operational request queue index
1555c9566231SKashyap Desai  *
1556c9566231SKashyap Desai  * Free memory segments allocated for operational request queue
1557c9566231SKashyap Desai  *
1558c9566231SKashyap Desai  * Return: Nothing.
1559c9566231SKashyap Desai  */
1560c9566231SKashyap Desai static void mpi3mr_free_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
1561c9566231SKashyap Desai {
1562c9566231SKashyap Desai 	u16 j;
1563c9566231SKashyap Desai 	int size;
1564c9566231SKashyap Desai 	struct segments *segments;
1565c9566231SKashyap Desai 
1566c9566231SKashyap Desai 	segments = mrioc->req_qinfo[q_idx].q_segments;
1567c9566231SKashyap Desai 	if (!segments)
1568c9566231SKashyap Desai 		return;
1569c9566231SKashyap Desai 
1570c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1571c9566231SKashyap Desai 		size = MPI3MR_OP_REQ_Q_SEG_SIZE;
1572c9566231SKashyap Desai 		if (mrioc->req_qinfo[q_idx].q_segment_list) {
1573c9566231SKashyap Desai 			dma_free_coherent(&mrioc->pdev->dev,
1574c9566231SKashyap Desai 			    MPI3MR_MAX_SEG_LIST_SIZE,
1575c9566231SKashyap Desai 			    mrioc->req_qinfo[q_idx].q_segment_list,
1576c9566231SKashyap Desai 			    mrioc->req_qinfo[q_idx].q_segment_list_dma);
1577d44b5fefSSreekanth Reddy 			mrioc->req_qinfo[q_idx].q_segment_list = NULL;
1578c9566231SKashyap Desai 		}
1579c9566231SKashyap Desai 	} else
1580243bcc8eSSreekanth Reddy 		size = mrioc->req_qinfo[q_idx].segment_qd *
1581c9566231SKashyap Desai 		    mrioc->facts.op_req_sz;
1582c9566231SKashyap Desai 
1583c9566231SKashyap Desai 	for (j = 0; j < mrioc->req_qinfo[q_idx].num_segments; j++) {
1584c9566231SKashyap Desai 		if (!segments[j].segment)
1585c9566231SKashyap Desai 			continue;
1586c9566231SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev,
1587c9566231SKashyap Desai 		    size, segments[j].segment, segments[j].segment_dma);
1588c9566231SKashyap Desai 		segments[j].segment = NULL;
1589c9566231SKashyap Desai 	}
1590c9566231SKashyap Desai 	kfree(mrioc->req_qinfo[q_idx].q_segments);
1591c9566231SKashyap Desai 	mrioc->req_qinfo[q_idx].q_segments = NULL;
1592c9566231SKashyap Desai 	mrioc->req_qinfo[q_idx].qid = 0;
1593c9566231SKashyap Desai }
1594c9566231SKashyap Desai 
1595c9566231SKashyap Desai /**
1596c9566231SKashyap Desai  * mpi3mr_free_op_reply_q_segments - free reply memory segments
1597c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1598c9566231SKashyap Desai  * @q_idx: operational reply queue index
1599c9566231SKashyap Desai  *
1600c9566231SKashyap Desai  * Free memory segments allocated for operational reply queue
1601c9566231SKashyap Desai  *
1602c9566231SKashyap Desai  * Return: Nothing.
1603c9566231SKashyap Desai  */
1604c9566231SKashyap Desai static void mpi3mr_free_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 q_idx)
1605c9566231SKashyap Desai {
1606c9566231SKashyap Desai 	u16 j;
1607c9566231SKashyap Desai 	int size;
1608c9566231SKashyap Desai 	struct segments *segments;
1609c9566231SKashyap Desai 
1610c9566231SKashyap Desai 	segments = mrioc->op_reply_qinfo[q_idx].q_segments;
1611c9566231SKashyap Desai 	if (!segments)
1612c9566231SKashyap Desai 		return;
1613c9566231SKashyap Desai 
1614c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1615c9566231SKashyap Desai 		size = MPI3MR_OP_REP_Q_SEG_SIZE;
1616c9566231SKashyap Desai 		if (mrioc->op_reply_qinfo[q_idx].q_segment_list) {
1617c9566231SKashyap Desai 			dma_free_coherent(&mrioc->pdev->dev,
1618c9566231SKashyap Desai 			    MPI3MR_MAX_SEG_LIST_SIZE,
1619c9566231SKashyap Desai 			    mrioc->op_reply_qinfo[q_idx].q_segment_list,
1620c9566231SKashyap Desai 			    mrioc->op_reply_qinfo[q_idx].q_segment_list_dma);
1621c9566231SKashyap Desai 			mrioc->op_reply_qinfo[q_idx].q_segment_list = NULL;
1622c9566231SKashyap Desai 		}
1623c9566231SKashyap Desai 	} else
1624c9566231SKashyap Desai 		size = mrioc->op_reply_qinfo[q_idx].segment_qd *
1625c9566231SKashyap Desai 		    mrioc->op_reply_desc_sz;
1626c9566231SKashyap Desai 
1627c9566231SKashyap Desai 	for (j = 0; j < mrioc->op_reply_qinfo[q_idx].num_segments; j++) {
1628c9566231SKashyap Desai 		if (!segments[j].segment)
1629c9566231SKashyap Desai 			continue;
1630c9566231SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev,
1631c9566231SKashyap Desai 		    size, segments[j].segment, segments[j].segment_dma);
1632c9566231SKashyap Desai 		segments[j].segment = NULL;
1633c9566231SKashyap Desai 	}
1634c9566231SKashyap Desai 
1635c9566231SKashyap Desai 	kfree(mrioc->op_reply_qinfo[q_idx].q_segments);
1636c9566231SKashyap Desai 	mrioc->op_reply_qinfo[q_idx].q_segments = NULL;
1637c9566231SKashyap Desai 	mrioc->op_reply_qinfo[q_idx].qid = 0;
1638c9566231SKashyap Desai }
1639c9566231SKashyap Desai 
1640c9566231SKashyap Desai /**
1641c9566231SKashyap Desai  * mpi3mr_delete_op_reply_q - delete operational reply queue
1642c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1643c9566231SKashyap Desai  * @qidx: operational reply queue index
1644c9566231SKashyap Desai  *
1645c9566231SKashyap Desai  * Delete operatinal reply queue by issuing MPI request
1646c9566231SKashyap Desai  * through admin queue.
1647c9566231SKashyap Desai  *
1648c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1649c9566231SKashyap Desai  */
1650c9566231SKashyap Desai static int mpi3mr_delete_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
1651c9566231SKashyap Desai {
1652c9566231SKashyap Desai 	struct mpi3_delete_reply_queue_request delq_req;
1653afd3a579SSreekanth Reddy 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1654c9566231SKashyap Desai 	int retval = 0;
1655c9566231SKashyap Desai 	u16 reply_qid = 0, midx;
1656c9566231SKashyap Desai 
1657afd3a579SSreekanth Reddy 	reply_qid = op_reply_q->qid;
1658c9566231SKashyap Desai 
1659c9566231SKashyap Desai 	midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
1660c9566231SKashyap Desai 
1661c9566231SKashyap Desai 	if (!reply_qid)	{
1662c9566231SKashyap Desai 		retval = -1;
1663c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: called with invalid ReqQID\n");
1664c9566231SKashyap Desai 		goto out;
1665c9566231SKashyap Desai 	}
1666c9566231SKashyap Desai 
1667afd3a579SSreekanth Reddy 	(op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) ? mrioc->default_qcount-- :
1668afd3a579SSreekanth Reddy 	    mrioc->active_poll_qcount--;
1669afd3a579SSreekanth Reddy 
1670c9566231SKashyap Desai 	memset(&delq_req, 0, sizeof(delq_req));
1671c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
1672c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1673c9566231SKashyap Desai 		retval = -1;
1674c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: Init command is in use\n");
1675c9566231SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
1676c9566231SKashyap Desai 		goto out;
1677c9566231SKashyap Desai 	}
1678c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1679c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
1680c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
1681c9566231SKashyap Desai 	delq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1682c9566231SKashyap Desai 	delq_req.function = MPI3_FUNCTION_DELETE_REPLY_QUEUE;
1683c9566231SKashyap Desai 	delq_req.queue_id = cpu_to_le16(reply_qid);
1684c9566231SKashyap Desai 
1685c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
1686c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &delq_req, sizeof(delq_req),
1687c9566231SKashyap Desai 	    1);
1688c9566231SKashyap Desai 	if (retval) {
1689c9566231SKashyap Desai 		ioc_err(mrioc, "Issue DelRepQ: Admin Post failed\n");
1690c9566231SKashyap Desai 		goto out_unlock;
1691c9566231SKashyap Desai 	}
1692c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1693c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1694c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1695a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "delete reply queue timed out\n");
1696a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
1697c9566231SKashyap Desai 		    MPI3MR_RESET_FROM_DELREPQ_TIMEOUT);
1698c9566231SKashyap Desai 		retval = -1;
1699c9566231SKashyap Desai 		goto out_unlock;
1700c9566231SKashyap Desai 	}
1701c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1702c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
1703c9566231SKashyap Desai 		ioc_err(mrioc,
1704c9566231SKashyap Desai 		    "Issue DelRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1705c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1706c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
1707c9566231SKashyap Desai 		retval = -1;
1708c9566231SKashyap Desai 		goto out_unlock;
1709c9566231SKashyap Desai 	}
1710c9566231SKashyap Desai 	mrioc->intr_info[midx].op_reply_q = NULL;
1711c9566231SKashyap Desai 
1712c9566231SKashyap Desai 	mpi3mr_free_op_reply_q_segments(mrioc, qidx);
1713c9566231SKashyap Desai out_unlock:
1714c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1715c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
1716c9566231SKashyap Desai out:
1717c9566231SKashyap Desai 
1718c9566231SKashyap Desai 	return retval;
1719c9566231SKashyap Desai }
1720c9566231SKashyap Desai 
1721c9566231SKashyap Desai /**
1722c9566231SKashyap Desai  * mpi3mr_alloc_op_reply_q_segments -Alloc segmented reply pool
1723c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1724c9566231SKashyap Desai  * @qidx: request queue index
1725c9566231SKashyap Desai  *
1726c9566231SKashyap Desai  * Allocate segmented memory pools for operational reply
1727c9566231SKashyap Desai  * queue.
1728c9566231SKashyap Desai  *
1729c9566231SKashyap Desai  * Return: 0 on success, non-zero on failure.
1730c9566231SKashyap Desai  */
1731c9566231SKashyap Desai static int mpi3mr_alloc_op_reply_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
1732c9566231SKashyap Desai {
1733c9566231SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1734c9566231SKashyap Desai 	int i, size;
1735c9566231SKashyap Desai 	u64 *q_segment_list_entry = NULL;
1736c9566231SKashyap Desai 	struct segments *segments;
1737c9566231SKashyap Desai 
1738c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1739c9566231SKashyap Desai 		op_reply_q->segment_qd =
1740c9566231SKashyap Desai 		    MPI3MR_OP_REP_Q_SEG_SIZE / mrioc->op_reply_desc_sz;
1741c9566231SKashyap Desai 
1742c9566231SKashyap Desai 		size = MPI3MR_OP_REP_Q_SEG_SIZE;
1743c9566231SKashyap Desai 
1744c9566231SKashyap Desai 		op_reply_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
1745c9566231SKashyap Desai 		    MPI3MR_MAX_SEG_LIST_SIZE, &op_reply_q->q_segment_list_dma,
1746c9566231SKashyap Desai 		    GFP_KERNEL);
1747c9566231SKashyap Desai 		if (!op_reply_q->q_segment_list)
1748c9566231SKashyap Desai 			return -ENOMEM;
1749c9566231SKashyap Desai 		q_segment_list_entry = (u64 *)op_reply_q->q_segment_list;
1750c9566231SKashyap Desai 	} else {
1751c9566231SKashyap Desai 		op_reply_q->segment_qd = op_reply_q->num_replies;
1752c9566231SKashyap Desai 		size = op_reply_q->num_replies * mrioc->op_reply_desc_sz;
1753c9566231SKashyap Desai 	}
1754c9566231SKashyap Desai 
1755c9566231SKashyap Desai 	op_reply_q->num_segments = DIV_ROUND_UP(op_reply_q->num_replies,
1756c9566231SKashyap Desai 	    op_reply_q->segment_qd);
1757c9566231SKashyap Desai 
1758c9566231SKashyap Desai 	op_reply_q->q_segments = kcalloc(op_reply_q->num_segments,
1759c9566231SKashyap Desai 	    sizeof(struct segments), GFP_KERNEL);
1760c9566231SKashyap Desai 	if (!op_reply_q->q_segments)
1761c9566231SKashyap Desai 		return -ENOMEM;
1762c9566231SKashyap Desai 
1763c9566231SKashyap Desai 	segments = op_reply_q->q_segments;
1764c9566231SKashyap Desai 	for (i = 0; i < op_reply_q->num_segments; i++) {
1765c9566231SKashyap Desai 		segments[i].segment =
1766c9566231SKashyap Desai 		    dma_alloc_coherent(&mrioc->pdev->dev,
1767c9566231SKashyap Desai 		    size, &segments[i].segment_dma, GFP_KERNEL);
1768c9566231SKashyap Desai 		if (!segments[i].segment)
1769c9566231SKashyap Desai 			return -ENOMEM;
1770c9566231SKashyap Desai 		if (mrioc->enable_segqueue)
1771c9566231SKashyap Desai 			q_segment_list_entry[i] =
1772c9566231SKashyap Desai 			    (unsigned long)segments[i].segment_dma;
1773c9566231SKashyap Desai 	}
1774c9566231SKashyap Desai 
1775c9566231SKashyap Desai 	return 0;
1776c9566231SKashyap Desai }
1777c9566231SKashyap Desai 
1778c9566231SKashyap Desai /**
1779c9566231SKashyap Desai  * mpi3mr_alloc_op_req_q_segments - Alloc segmented req pool.
1780c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1781c9566231SKashyap Desai  * @qidx: request queue index
1782c9566231SKashyap Desai  *
1783c9566231SKashyap Desai  * Allocate segmented memory pools for operational request
1784c9566231SKashyap Desai  * queue.
1785c9566231SKashyap Desai  *
1786c9566231SKashyap Desai  * Return: 0 on success, non-zero on failure.
1787c9566231SKashyap Desai  */
1788c9566231SKashyap Desai static int mpi3mr_alloc_op_req_q_segments(struct mpi3mr_ioc *mrioc, u16 qidx)
1789c9566231SKashyap Desai {
1790c9566231SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
1791c9566231SKashyap Desai 	int i, size;
1792c9566231SKashyap Desai 	u64 *q_segment_list_entry = NULL;
1793c9566231SKashyap Desai 	struct segments *segments;
1794c9566231SKashyap Desai 
1795c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1796c9566231SKashyap Desai 		op_req_q->segment_qd =
1797c9566231SKashyap Desai 		    MPI3MR_OP_REQ_Q_SEG_SIZE / mrioc->facts.op_req_sz;
1798c9566231SKashyap Desai 
1799c9566231SKashyap Desai 		size = MPI3MR_OP_REQ_Q_SEG_SIZE;
1800c9566231SKashyap Desai 
1801c9566231SKashyap Desai 		op_req_q->q_segment_list = dma_alloc_coherent(&mrioc->pdev->dev,
1802c9566231SKashyap Desai 		    MPI3MR_MAX_SEG_LIST_SIZE, &op_req_q->q_segment_list_dma,
1803c9566231SKashyap Desai 		    GFP_KERNEL);
1804c9566231SKashyap Desai 		if (!op_req_q->q_segment_list)
1805c9566231SKashyap Desai 			return -ENOMEM;
1806c9566231SKashyap Desai 		q_segment_list_entry = (u64 *)op_req_q->q_segment_list;
1807c9566231SKashyap Desai 
1808c9566231SKashyap Desai 	} else {
1809c9566231SKashyap Desai 		op_req_q->segment_qd = op_req_q->num_requests;
1810c9566231SKashyap Desai 		size = op_req_q->num_requests * mrioc->facts.op_req_sz;
1811c9566231SKashyap Desai 	}
1812c9566231SKashyap Desai 
1813c9566231SKashyap Desai 	op_req_q->num_segments = DIV_ROUND_UP(op_req_q->num_requests,
1814c9566231SKashyap Desai 	    op_req_q->segment_qd);
1815c9566231SKashyap Desai 
1816c9566231SKashyap Desai 	op_req_q->q_segments = kcalloc(op_req_q->num_segments,
1817c9566231SKashyap Desai 	    sizeof(struct segments), GFP_KERNEL);
1818c9566231SKashyap Desai 	if (!op_req_q->q_segments)
1819c9566231SKashyap Desai 		return -ENOMEM;
1820c9566231SKashyap Desai 
1821c9566231SKashyap Desai 	segments = op_req_q->q_segments;
1822c9566231SKashyap Desai 	for (i = 0; i < op_req_q->num_segments; i++) {
1823c9566231SKashyap Desai 		segments[i].segment =
1824c9566231SKashyap Desai 		    dma_alloc_coherent(&mrioc->pdev->dev,
1825c9566231SKashyap Desai 		    size, &segments[i].segment_dma, GFP_KERNEL);
1826c9566231SKashyap Desai 		if (!segments[i].segment)
1827c9566231SKashyap Desai 			return -ENOMEM;
1828c9566231SKashyap Desai 		if (mrioc->enable_segqueue)
1829c9566231SKashyap Desai 			q_segment_list_entry[i] =
1830c9566231SKashyap Desai 			    (unsigned long)segments[i].segment_dma;
1831c9566231SKashyap Desai 	}
1832c9566231SKashyap Desai 
1833c9566231SKashyap Desai 	return 0;
1834c9566231SKashyap Desai }
1835c9566231SKashyap Desai 
1836c9566231SKashyap Desai /**
1837c9566231SKashyap Desai  * mpi3mr_create_op_reply_q - create operational reply queue
1838c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1839c9566231SKashyap Desai  * @qidx: operational reply queue index
1840c9566231SKashyap Desai  *
1841c9566231SKashyap Desai  * Create operatinal reply queue by issuing MPI request
1842c9566231SKashyap Desai  * through admin queue.
1843c9566231SKashyap Desai  *
1844c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1845c9566231SKashyap Desai  */
1846c9566231SKashyap Desai static int mpi3mr_create_op_reply_q(struct mpi3mr_ioc *mrioc, u16 qidx)
1847c9566231SKashyap Desai {
1848c9566231SKashyap Desai 	struct mpi3_create_reply_queue_request create_req;
1849c9566231SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
1850c9566231SKashyap Desai 	int retval = 0;
1851c9566231SKashyap Desai 	u16 reply_qid = 0, midx;
1852c9566231SKashyap Desai 
1853c9566231SKashyap Desai 	reply_qid = op_reply_q->qid;
1854c9566231SKashyap Desai 
1855c9566231SKashyap Desai 	midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(qidx, mrioc->op_reply_q_offset);
1856c9566231SKashyap Desai 
1857c9566231SKashyap Desai 	if (reply_qid) {
1858c9566231SKashyap Desai 		retval = -1;
1859c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: called for duplicate qid %d\n",
1860c9566231SKashyap Desai 		    reply_qid);
1861c9566231SKashyap Desai 
1862c9566231SKashyap Desai 		return retval;
1863c9566231SKashyap Desai 	}
1864c9566231SKashyap Desai 
1865c9566231SKashyap Desai 	reply_qid = qidx + 1;
1866c9566231SKashyap Desai 	op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD;
1867243bcc8eSSreekanth Reddy 	if (!mrioc->pdev->revision)
1868243bcc8eSSreekanth Reddy 		op_reply_q->num_replies = MPI3MR_OP_REP_Q_QD4K;
1869c9566231SKashyap Desai 	op_reply_q->ci = 0;
1870c9566231SKashyap Desai 	op_reply_q->ephase = 1;
1871463429f8SKashyap Desai 	atomic_set(&op_reply_q->pend_ios, 0);
1872463429f8SKashyap Desai 	atomic_set(&op_reply_q->in_use, 0);
1873463429f8SKashyap Desai 	op_reply_q->enable_irq_poll = false;
1874c9566231SKashyap Desai 
1875c9566231SKashyap Desai 	if (!op_reply_q->q_segments) {
1876c9566231SKashyap Desai 		retval = mpi3mr_alloc_op_reply_q_segments(mrioc, qidx);
1877c9566231SKashyap Desai 		if (retval) {
1878c9566231SKashyap Desai 			mpi3mr_free_op_reply_q_segments(mrioc, qidx);
1879c9566231SKashyap Desai 			goto out;
1880c9566231SKashyap Desai 		}
1881c9566231SKashyap Desai 	}
1882c9566231SKashyap Desai 
1883c9566231SKashyap Desai 	memset(&create_req, 0, sizeof(create_req));
1884c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
1885c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
1886c9566231SKashyap Desai 		retval = -1;
1887c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: Init command is in use\n");
1888f9dc034dSYang Yingliang 		goto out_unlock;
1889c9566231SKashyap Desai 	}
1890c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
1891c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
1892c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
1893c9566231SKashyap Desai 	create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
1894c9566231SKashyap Desai 	create_req.function = MPI3_FUNCTION_CREATE_REPLY_QUEUE;
1895c9566231SKashyap Desai 	create_req.queue_id = cpu_to_le16(reply_qid);
1896afd3a579SSreekanth Reddy 
1897afd3a579SSreekanth Reddy 	if (midx < (mrioc->intr_info_count - mrioc->requested_poll_qcount))
1898afd3a579SSreekanth Reddy 		op_reply_q->qtype = MPI3MR_DEFAULT_QUEUE;
1899afd3a579SSreekanth Reddy 	else
1900afd3a579SSreekanth Reddy 		op_reply_q->qtype = MPI3MR_POLL_QUEUE;
1901afd3a579SSreekanth Reddy 
1902afd3a579SSreekanth Reddy 	if (op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) {
1903afd3a579SSreekanth Reddy 		create_req.flags =
1904afd3a579SSreekanth Reddy 			MPI3_CREATE_REPLY_QUEUE_FLAGS_INT_ENABLE_ENABLE;
1905afd3a579SSreekanth Reddy 		create_req.msix_index =
1906afd3a579SSreekanth Reddy 			cpu_to_le16(mrioc->intr_info[midx].msix_index);
1907afd3a579SSreekanth Reddy 	} else {
1908afd3a579SSreekanth Reddy 		create_req.msix_index = cpu_to_le16(mrioc->intr_info_count - 1);
1909afd3a579SSreekanth Reddy 		ioc_info(mrioc, "create reply queue(polled): for qid(%d), midx(%d)\n",
1910afd3a579SSreekanth Reddy 			reply_qid, midx);
1911afd3a579SSreekanth Reddy 		if (!mrioc->active_poll_qcount)
1912afd3a579SSreekanth Reddy 			disable_irq_nosync(pci_irq_vector(mrioc->pdev,
1913afd3a579SSreekanth Reddy 			    mrioc->intr_info_count - 1));
1914afd3a579SSreekanth Reddy 	}
1915afd3a579SSreekanth Reddy 
1916c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
1917c9566231SKashyap Desai 		create_req.flags |=
1918c9566231SKashyap Desai 		    MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
1919c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1920c9566231SKashyap Desai 		    op_reply_q->q_segment_list_dma);
1921c9566231SKashyap Desai 	} else
1922c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
1923c9566231SKashyap Desai 		    op_reply_q->q_segments[0].segment_dma);
1924c9566231SKashyap Desai 
1925c9566231SKashyap Desai 	create_req.size = cpu_to_le16(op_reply_q->num_replies);
1926c9566231SKashyap Desai 
1927c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
1928c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &create_req,
1929c9566231SKashyap Desai 	    sizeof(create_req), 1);
1930c9566231SKashyap Desai 	if (retval) {
1931c9566231SKashyap Desai 		ioc_err(mrioc, "CreateRepQ: Admin Post failed\n");
1932c9566231SKashyap Desai 		goto out_unlock;
1933c9566231SKashyap Desai 	}
1934c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
1935c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
1936c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
1937a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "create reply queue timed out\n");
1938a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
1939c9566231SKashyap Desai 		    MPI3MR_RESET_FROM_CREATEREPQ_TIMEOUT);
1940c9566231SKashyap Desai 		retval = -1;
1941c9566231SKashyap Desai 		goto out_unlock;
1942c9566231SKashyap Desai 	}
1943c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
1944c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
1945c9566231SKashyap Desai 		ioc_err(mrioc,
1946c9566231SKashyap Desai 		    "CreateRepQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
1947c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
1948c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
1949c9566231SKashyap Desai 		retval = -1;
1950c9566231SKashyap Desai 		goto out_unlock;
1951c9566231SKashyap Desai 	}
1952c9566231SKashyap Desai 	op_reply_q->qid = reply_qid;
1953fe6db615SSreekanth Reddy 	if (midx < mrioc->intr_info_count)
1954c9566231SKashyap Desai 		mrioc->intr_info[midx].op_reply_q = op_reply_q;
1955c9566231SKashyap Desai 
1956afd3a579SSreekanth Reddy 	(op_reply_q->qtype == MPI3MR_DEFAULT_QUEUE) ? mrioc->default_qcount++ :
1957afd3a579SSreekanth Reddy 	    mrioc->active_poll_qcount++;
1958afd3a579SSreekanth Reddy 
1959c9566231SKashyap Desai out_unlock:
1960c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
1961c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
1962c9566231SKashyap Desai out:
1963c9566231SKashyap Desai 
1964c9566231SKashyap Desai 	return retval;
1965c9566231SKashyap Desai }
1966c9566231SKashyap Desai 
1967c9566231SKashyap Desai /**
1968c9566231SKashyap Desai  * mpi3mr_create_op_req_q - create operational request queue
1969c9566231SKashyap Desai  * @mrioc: Adapter instance reference
1970c9566231SKashyap Desai  * @idx: operational request queue index
1971c9566231SKashyap Desai  * @reply_qid: Reply queue ID
1972c9566231SKashyap Desai  *
1973c9566231SKashyap Desai  * Create operatinal request queue by issuing MPI request
1974c9566231SKashyap Desai  * through admin queue.
1975c9566231SKashyap Desai  *
1976c9566231SKashyap Desai  * Return:  0 on success, non-zero on failure.
1977c9566231SKashyap Desai  */
1978c9566231SKashyap Desai static int mpi3mr_create_op_req_q(struct mpi3mr_ioc *mrioc, u16 idx,
1979c9566231SKashyap Desai 	u16 reply_qid)
1980c9566231SKashyap Desai {
1981c9566231SKashyap Desai 	struct mpi3_create_request_queue_request create_req;
1982c9566231SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + idx;
1983c9566231SKashyap Desai 	int retval = 0;
1984c9566231SKashyap Desai 	u16 req_qid = 0;
1985c9566231SKashyap Desai 
1986c9566231SKashyap Desai 	req_qid = op_req_q->qid;
1987c9566231SKashyap Desai 
1988c9566231SKashyap Desai 	if (req_qid) {
1989c9566231SKashyap Desai 		retval = -1;
1990c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: called for duplicate qid %d\n",
1991c9566231SKashyap Desai 		    req_qid);
1992c9566231SKashyap Desai 
1993c9566231SKashyap Desai 		return retval;
1994c9566231SKashyap Desai 	}
1995c9566231SKashyap Desai 	req_qid = idx + 1;
1996c9566231SKashyap Desai 
1997c9566231SKashyap Desai 	op_req_q->num_requests = MPI3MR_OP_REQ_Q_QD;
1998c9566231SKashyap Desai 	op_req_q->ci = 0;
1999c9566231SKashyap Desai 	op_req_q->pi = 0;
2000c9566231SKashyap Desai 	op_req_q->reply_qid = reply_qid;
2001c9566231SKashyap Desai 	spin_lock_init(&op_req_q->q_lock);
2002c9566231SKashyap Desai 
2003c9566231SKashyap Desai 	if (!op_req_q->q_segments) {
2004c9566231SKashyap Desai 		retval = mpi3mr_alloc_op_req_q_segments(mrioc, idx);
2005c9566231SKashyap Desai 		if (retval) {
2006c9566231SKashyap Desai 			mpi3mr_free_op_req_q_segments(mrioc, idx);
2007c9566231SKashyap Desai 			goto out;
2008c9566231SKashyap Desai 		}
2009c9566231SKashyap Desai 	}
2010c9566231SKashyap Desai 
2011c9566231SKashyap Desai 	memset(&create_req, 0, sizeof(create_req));
2012c9566231SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
2013c9566231SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
2014c9566231SKashyap Desai 		retval = -1;
2015c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: Init command is in use\n");
2016f9dc034dSYang Yingliang 		goto out_unlock;
2017c9566231SKashyap Desai 	}
2018c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
2019c9566231SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
2020c9566231SKashyap Desai 	mrioc->init_cmds.callback = NULL;
2021c9566231SKashyap Desai 	create_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
2022c9566231SKashyap Desai 	create_req.function = MPI3_FUNCTION_CREATE_REQUEST_QUEUE;
2023c9566231SKashyap Desai 	create_req.queue_id = cpu_to_le16(req_qid);
2024c9566231SKashyap Desai 	if (mrioc->enable_segqueue) {
2025c9566231SKashyap Desai 		create_req.flags =
2026c9566231SKashyap Desai 		    MPI3_CREATE_REQUEST_QUEUE_FLAGS_SEGMENTED_SEGMENTED;
2027c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
2028c9566231SKashyap Desai 		    op_req_q->q_segment_list_dma);
2029c9566231SKashyap Desai 	} else
2030c9566231SKashyap Desai 		create_req.base_address = cpu_to_le64(
2031c9566231SKashyap Desai 		    op_req_q->q_segments[0].segment_dma);
2032c9566231SKashyap Desai 	create_req.reply_queue_id = cpu_to_le16(reply_qid);
2033c9566231SKashyap Desai 	create_req.size = cpu_to_le16(op_req_q->num_requests);
2034c9566231SKashyap Desai 
2035c9566231SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
2036c9566231SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &create_req,
2037c9566231SKashyap Desai 	    sizeof(create_req), 1);
2038c9566231SKashyap Desai 	if (retval) {
2039c9566231SKashyap Desai 		ioc_err(mrioc, "CreateReqQ: Admin Post failed\n");
2040c9566231SKashyap Desai 		goto out_unlock;
2041c9566231SKashyap Desai 	}
2042c9566231SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
2043c9566231SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
2044c9566231SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2045a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "create request queue timed out\n");
2046a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
2047a6856cc4SSreekanth Reddy 		    MPI3MR_RESET_FROM_CREATEREQQ_TIMEOUT);
2048c9566231SKashyap Desai 		retval = -1;
2049c9566231SKashyap Desai 		goto out_unlock;
2050c9566231SKashyap Desai 	}
2051c9566231SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2052c9566231SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
2053c9566231SKashyap Desai 		ioc_err(mrioc,
2054c9566231SKashyap Desai 		    "CreateReqQ: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2055c9566231SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2056c9566231SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
2057c9566231SKashyap Desai 		retval = -1;
2058c9566231SKashyap Desai 		goto out_unlock;
2059c9566231SKashyap Desai 	}
2060c9566231SKashyap Desai 	op_req_q->qid = req_qid;
2061c9566231SKashyap Desai 
2062c9566231SKashyap Desai out_unlock:
2063c9566231SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2064c9566231SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
2065c9566231SKashyap Desai out:
2066c9566231SKashyap Desai 
2067c9566231SKashyap Desai 	return retval;
2068c9566231SKashyap Desai }
2069c9566231SKashyap Desai 
2070c9566231SKashyap Desai /**
2071c9566231SKashyap Desai  * mpi3mr_create_op_queues - create operational queue pairs
2072c9566231SKashyap Desai  * @mrioc: Adapter instance reference
2073c9566231SKashyap Desai  *
2074c9566231SKashyap Desai  * Allocate memory for operational queue meta data and call
2075c9566231SKashyap Desai  * create request and reply queue functions.
2076c9566231SKashyap Desai  *
2077c9566231SKashyap Desai  * Return: 0 on success, non-zero on failures.
2078c9566231SKashyap Desai  */
2079c9566231SKashyap Desai static int mpi3mr_create_op_queues(struct mpi3mr_ioc *mrioc)
2080c9566231SKashyap Desai {
2081c9566231SKashyap Desai 	int retval = 0;
2082c9566231SKashyap Desai 	u16 num_queues = 0, i = 0, msix_count_op_q = 1;
2083c9566231SKashyap Desai 
2084c9566231SKashyap Desai 	num_queues = min_t(int, mrioc->facts.max_op_reply_q,
2085c9566231SKashyap Desai 	    mrioc->facts.max_op_req_q);
2086c9566231SKashyap Desai 
2087c9566231SKashyap Desai 	msix_count_op_q =
2088c9566231SKashyap Desai 	    mrioc->intr_info_count - mrioc->op_reply_q_offset;
2089c9566231SKashyap Desai 	if (!mrioc->num_queues)
2090c9566231SKashyap Desai 		mrioc->num_queues = min_t(int, num_queues, msix_count_op_q);
2091c5758fc7SSreekanth Reddy 	/*
2092c5758fc7SSreekanth Reddy 	 * During reset set the num_queues to the number of queues
2093c5758fc7SSreekanth Reddy 	 * that was set before the reset.
2094c5758fc7SSreekanth Reddy 	 */
2095c5758fc7SSreekanth Reddy 	num_queues = mrioc->num_op_reply_q ?
2096c5758fc7SSreekanth Reddy 	    mrioc->num_op_reply_q : mrioc->num_queues;
2097c5758fc7SSreekanth Reddy 	ioc_info(mrioc, "trying to create %d operational queue pairs\n",
2098c9566231SKashyap Desai 	    num_queues);
2099c9566231SKashyap Desai 
2100c9566231SKashyap Desai 	if (!mrioc->req_qinfo) {
2101c9566231SKashyap Desai 		mrioc->req_qinfo = kcalloc(num_queues,
2102c9566231SKashyap Desai 		    sizeof(struct op_req_qinfo), GFP_KERNEL);
2103c9566231SKashyap Desai 		if (!mrioc->req_qinfo) {
2104c9566231SKashyap Desai 			retval = -1;
2105c9566231SKashyap Desai 			goto out_failed;
2106c9566231SKashyap Desai 		}
2107c9566231SKashyap Desai 
2108c9566231SKashyap Desai 		mrioc->op_reply_qinfo = kzalloc(sizeof(struct op_reply_qinfo) *
2109c9566231SKashyap Desai 		    num_queues, GFP_KERNEL);
2110c9566231SKashyap Desai 		if (!mrioc->op_reply_qinfo) {
2111c9566231SKashyap Desai 			retval = -1;
2112c9566231SKashyap Desai 			goto out_failed;
2113c9566231SKashyap Desai 		}
2114c9566231SKashyap Desai 	}
2115c9566231SKashyap Desai 
2116c9566231SKashyap Desai 	if (mrioc->enable_segqueue)
2117c9566231SKashyap Desai 		ioc_info(mrioc,
2118c9566231SKashyap Desai 		    "allocating operational queues through segmented queues\n");
2119c9566231SKashyap Desai 
2120c9566231SKashyap Desai 	for (i = 0; i < num_queues; i++) {
2121c9566231SKashyap Desai 		if (mpi3mr_create_op_reply_q(mrioc, i)) {
2122c9566231SKashyap Desai 			ioc_err(mrioc, "Cannot create OP RepQ %d\n", i);
2123c9566231SKashyap Desai 			break;
2124c9566231SKashyap Desai 		}
2125c9566231SKashyap Desai 		if (mpi3mr_create_op_req_q(mrioc, i,
2126c9566231SKashyap Desai 		    mrioc->op_reply_qinfo[i].qid)) {
2127c9566231SKashyap Desai 			ioc_err(mrioc, "Cannot create OP ReqQ %d\n", i);
2128c9566231SKashyap Desai 			mpi3mr_delete_op_reply_q(mrioc, i);
2129c9566231SKashyap Desai 			break;
2130c9566231SKashyap Desai 		}
2131c9566231SKashyap Desai 	}
2132c9566231SKashyap Desai 
2133c9566231SKashyap Desai 	if (i == 0) {
2134c9566231SKashyap Desai 		/* Not even one queue is created successfully*/
2135c9566231SKashyap Desai 		retval = -1;
2136c9566231SKashyap Desai 		goto out_failed;
2137c9566231SKashyap Desai 	}
2138c9566231SKashyap Desai 	mrioc->num_op_reply_q = mrioc->num_op_req_q = i;
2139afd3a579SSreekanth Reddy 	ioc_info(mrioc,
2140afd3a579SSreekanth Reddy 	    "successfully created %d operational queue pairs(default/polled) queue = (%d/%d)\n",
2141afd3a579SSreekanth Reddy 	    mrioc->num_op_reply_q, mrioc->default_qcount,
2142afd3a579SSreekanth Reddy 	    mrioc->active_poll_qcount);
2143c9566231SKashyap Desai 
2144c9566231SKashyap Desai 	return retval;
2145c9566231SKashyap Desai out_failed:
2146c9566231SKashyap Desai 	kfree(mrioc->req_qinfo);
2147c9566231SKashyap Desai 	mrioc->req_qinfo = NULL;
2148c9566231SKashyap Desai 
2149c9566231SKashyap Desai 	kfree(mrioc->op_reply_qinfo);
2150c9566231SKashyap Desai 	mrioc->op_reply_qinfo = NULL;
2151c9566231SKashyap Desai 
2152c9566231SKashyap Desai 	return retval;
2153c9566231SKashyap Desai }
2154c9566231SKashyap Desai 
2155c9566231SKashyap Desai /**
2156023ab2a9SKashyap Desai  * mpi3mr_op_request_post - Post request to operational queue
2157023ab2a9SKashyap Desai  * @mrioc: Adapter reference
2158023ab2a9SKashyap Desai  * @op_req_q: Operational request queue info
2159023ab2a9SKashyap Desai  * @req: MPI3 request
2160023ab2a9SKashyap Desai  *
2161023ab2a9SKashyap Desai  * Post the MPI3 request into operational request queue and
2162023ab2a9SKashyap Desai  * inform the controller, if the queue is full return
2163023ab2a9SKashyap Desai  * appropriate error.
2164023ab2a9SKashyap Desai  *
2165023ab2a9SKashyap Desai  * Return: 0 on success, non-zero on failure.
2166023ab2a9SKashyap Desai  */
2167023ab2a9SKashyap Desai int mpi3mr_op_request_post(struct mpi3mr_ioc *mrioc,
2168023ab2a9SKashyap Desai 	struct op_req_qinfo *op_req_q, u8 *req)
2169023ab2a9SKashyap Desai {
2170023ab2a9SKashyap Desai 	u16 pi = 0, max_entries, reply_qidx = 0, midx;
2171023ab2a9SKashyap Desai 	int retval = 0;
2172023ab2a9SKashyap Desai 	unsigned long flags;
2173023ab2a9SKashyap Desai 	u8 *req_entry;
2174023ab2a9SKashyap Desai 	void *segment_base_addr;
2175023ab2a9SKashyap Desai 	u16 req_sz = mrioc->facts.op_req_sz;
2176023ab2a9SKashyap Desai 	struct segments *segments = op_req_q->q_segments;
2177023ab2a9SKashyap Desai 
2178023ab2a9SKashyap Desai 	reply_qidx = op_req_q->reply_qid - 1;
2179023ab2a9SKashyap Desai 
2180023ab2a9SKashyap Desai 	if (mrioc->unrecoverable)
2181023ab2a9SKashyap Desai 		return -EFAULT;
2182023ab2a9SKashyap Desai 
2183023ab2a9SKashyap Desai 	spin_lock_irqsave(&op_req_q->q_lock, flags);
2184023ab2a9SKashyap Desai 	pi = op_req_q->pi;
2185023ab2a9SKashyap Desai 	max_entries = op_req_q->num_requests;
2186023ab2a9SKashyap Desai 
2187023ab2a9SKashyap Desai 	if (mpi3mr_check_req_qfull(op_req_q)) {
2188023ab2a9SKashyap Desai 		midx = REPLY_QUEUE_IDX_TO_MSIX_IDX(
2189023ab2a9SKashyap Desai 		    reply_qidx, mrioc->op_reply_q_offset);
2190afd3a579SSreekanth Reddy 		mpi3mr_process_op_reply_q(mrioc, mrioc->intr_info[midx].op_reply_q);
2191023ab2a9SKashyap Desai 
2192023ab2a9SKashyap Desai 		if (mpi3mr_check_req_qfull(op_req_q)) {
2193023ab2a9SKashyap Desai 			retval = -EAGAIN;
2194023ab2a9SKashyap Desai 			goto out;
2195023ab2a9SKashyap Desai 		}
2196023ab2a9SKashyap Desai 	}
2197023ab2a9SKashyap Desai 
2198023ab2a9SKashyap Desai 	if (mrioc->reset_in_progress) {
2199023ab2a9SKashyap Desai 		ioc_err(mrioc, "OpReqQ submit reset in progress\n");
2200023ab2a9SKashyap Desai 		retval = -EAGAIN;
2201023ab2a9SKashyap Desai 		goto out;
2202023ab2a9SKashyap Desai 	}
2203023ab2a9SKashyap Desai 
2204023ab2a9SKashyap Desai 	segment_base_addr = segments[pi / op_req_q->segment_qd].segment;
2205023ab2a9SKashyap Desai 	req_entry = (u8 *)segment_base_addr +
2206023ab2a9SKashyap Desai 	    ((pi % op_req_q->segment_qd) * req_sz);
2207023ab2a9SKashyap Desai 
2208023ab2a9SKashyap Desai 	memset(req_entry, 0, req_sz);
2209023ab2a9SKashyap Desai 	memcpy(req_entry, req, MPI3MR_ADMIN_REQ_FRAME_SZ);
2210023ab2a9SKashyap Desai 
2211023ab2a9SKashyap Desai 	if (++pi == max_entries)
2212023ab2a9SKashyap Desai 		pi = 0;
2213023ab2a9SKashyap Desai 	op_req_q->pi = pi;
2214023ab2a9SKashyap Desai 
22157f9f953dSSreekanth Reddy #ifndef CONFIG_PREEMPT_RT
2216463429f8SKashyap Desai 	if (atomic_inc_return(&mrioc->op_reply_qinfo[reply_qidx].pend_ios)
2217463429f8SKashyap Desai 	    > MPI3MR_IRQ_POLL_TRIGGER_IOCOUNT)
2218463429f8SKashyap Desai 		mrioc->op_reply_qinfo[reply_qidx].enable_irq_poll = true;
22197f9f953dSSreekanth Reddy #else
22207f9f953dSSreekanth Reddy 	atomic_inc_return(&mrioc->op_reply_qinfo[reply_qidx].pend_ios);
22217f9f953dSSreekanth Reddy #endif
2222463429f8SKashyap Desai 
2223023ab2a9SKashyap Desai 	writel(op_req_q->pi,
2224023ab2a9SKashyap Desai 	    &mrioc->sysif_regs->oper_queue_indexes[reply_qidx].producer_index);
2225023ab2a9SKashyap Desai 
2226023ab2a9SKashyap Desai out:
2227023ab2a9SKashyap Desai 	spin_unlock_irqrestore(&op_req_q->q_lock, flags);
2228023ab2a9SKashyap Desai 	return retval;
2229023ab2a9SKashyap Desai }
2230023ab2a9SKashyap Desai 
2231023ab2a9SKashyap Desai /**
2232a6856cc4SSreekanth Reddy  * mpi3mr_check_rh_fault_ioc - check reset history and fault
2233a6856cc4SSreekanth Reddy  * controller
2234a6856cc4SSreekanth Reddy  * @mrioc: Adapter instance reference
22353bb3c24eSYang Li  * @reason_code: reason code for the fault.
2236a6856cc4SSreekanth Reddy  *
2237a6856cc4SSreekanth Reddy  * This routine will save snapdump and fault the controller with
2238a6856cc4SSreekanth Reddy  * the given reason code if it is not already in the fault or
2239a6856cc4SSreekanth Reddy  * not asynchronosuly reset. This will be used to handle
2240a6856cc4SSreekanth Reddy  * initilaization time faults/resets/timeout as in those cases
2241a6856cc4SSreekanth Reddy  * immediate soft reset invocation is not required.
2242a6856cc4SSreekanth Reddy  *
2243a6856cc4SSreekanth Reddy  * Return:  None.
2244a6856cc4SSreekanth Reddy  */
2245a6856cc4SSreekanth Reddy void mpi3mr_check_rh_fault_ioc(struct mpi3mr_ioc *mrioc, u32 reason_code)
2246a6856cc4SSreekanth Reddy {
2247a6856cc4SSreekanth Reddy 	u32 ioc_status, host_diagnostic, timeout;
2248a6856cc4SSreekanth Reddy 
2249f2a79d20SSreekanth Reddy 	if (mrioc->unrecoverable) {
2250f2a79d20SSreekanth Reddy 		ioc_err(mrioc, "controller is unrecoverable\n");
2251f2a79d20SSreekanth Reddy 		return;
2252f2a79d20SSreekanth Reddy 	}
2253f2a79d20SSreekanth Reddy 
2254f2a79d20SSreekanth Reddy 	if (!pci_device_is_present(mrioc->pdev)) {
2255f2a79d20SSreekanth Reddy 		mrioc->unrecoverable = 1;
2256f2a79d20SSreekanth Reddy 		ioc_err(mrioc, "controller is not present\n");
2257f2a79d20SSreekanth Reddy 		return;
2258f2a79d20SSreekanth Reddy 	}
2259f2a79d20SSreekanth Reddy 
2260a6856cc4SSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
2261a6856cc4SSreekanth Reddy 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
2262a6856cc4SSreekanth Reddy 	    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
2263a6856cc4SSreekanth Reddy 		mpi3mr_print_fault_info(mrioc);
2264a6856cc4SSreekanth Reddy 		return;
2265a6856cc4SSreekanth Reddy 	}
2266a6856cc4SSreekanth Reddy 	mpi3mr_set_diagsave(mrioc);
2267a6856cc4SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
2268a6856cc4SSreekanth Reddy 	    reason_code);
2269a6856cc4SSreekanth Reddy 	timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
2270a6856cc4SSreekanth Reddy 	do {
2271a6856cc4SSreekanth Reddy 		host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
2272a6856cc4SSreekanth Reddy 		if (!(host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
2273a6856cc4SSreekanth Reddy 			break;
2274a6856cc4SSreekanth Reddy 		msleep(100);
2275a6856cc4SSreekanth Reddy 	} while (--timeout);
2276a6856cc4SSreekanth Reddy }
2277a6856cc4SSreekanth Reddy 
2278a6856cc4SSreekanth Reddy /**
227954dfcffbSKashyap Desai  * mpi3mr_sync_timestamp - Issue time stamp sync request
228054dfcffbSKashyap Desai  * @mrioc: Adapter reference
228154dfcffbSKashyap Desai  *
228254dfcffbSKashyap Desai  * Issue IO unit control MPI request to synchornize firmware
228354dfcffbSKashyap Desai  * timestamp with host time.
228454dfcffbSKashyap Desai  *
228554dfcffbSKashyap Desai  * Return: 0 on success, non-zero on failure.
228654dfcffbSKashyap Desai  */
228754dfcffbSKashyap Desai static int mpi3mr_sync_timestamp(struct mpi3mr_ioc *mrioc)
228854dfcffbSKashyap Desai {
228954dfcffbSKashyap Desai 	ktime_t current_time;
229054dfcffbSKashyap Desai 	struct mpi3_iounit_control_request iou_ctrl;
229154dfcffbSKashyap Desai 	int retval = 0;
229254dfcffbSKashyap Desai 
229354dfcffbSKashyap Desai 	memset(&iou_ctrl, 0, sizeof(iou_ctrl));
229454dfcffbSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
229554dfcffbSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
229654dfcffbSKashyap Desai 		retval = -1;
229754dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: command is in use\n");
229854dfcffbSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
229954dfcffbSKashyap Desai 		goto out;
230054dfcffbSKashyap Desai 	}
230154dfcffbSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
230254dfcffbSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
230354dfcffbSKashyap Desai 	mrioc->init_cmds.callback = NULL;
230454dfcffbSKashyap Desai 	iou_ctrl.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
230554dfcffbSKashyap Desai 	iou_ctrl.function = MPI3_FUNCTION_IO_UNIT_CONTROL;
230654dfcffbSKashyap Desai 	iou_ctrl.operation = MPI3_CTRL_OP_UPDATE_TIMESTAMP;
230754dfcffbSKashyap Desai 	current_time = ktime_get_real();
230854dfcffbSKashyap Desai 	iou_ctrl.param64[0] = cpu_to_le64(ktime_to_ms(current_time));
230954dfcffbSKashyap Desai 
231054dfcffbSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
231154dfcffbSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iou_ctrl,
231254dfcffbSKashyap Desai 	    sizeof(iou_ctrl), 0);
231354dfcffbSKashyap Desai 	if (retval) {
231454dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: Admin Post failed\n");
231554dfcffbSKashyap Desai 		goto out_unlock;
231654dfcffbSKashyap Desai 	}
231754dfcffbSKashyap Desai 
231854dfcffbSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
231954dfcffbSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
232054dfcffbSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
232154dfcffbSKashyap Desai 		ioc_err(mrioc, "Issue IOUCTL time_stamp: command timed out\n");
232254dfcffbSKashyap Desai 		mrioc->init_cmds.is_waiting = 0;
2323fbaa9aa4SSreekanth Reddy 		if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
232454dfcffbSKashyap Desai 			mpi3mr_soft_reset_handler(mrioc,
232554dfcffbSKashyap Desai 			    MPI3MR_RESET_FROM_TSU_TIMEOUT, 1);
232654dfcffbSKashyap Desai 		retval = -1;
232754dfcffbSKashyap Desai 		goto out_unlock;
232854dfcffbSKashyap Desai 	}
232954dfcffbSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
233054dfcffbSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
233154dfcffbSKashyap Desai 		ioc_err(mrioc,
233254dfcffbSKashyap Desai 		    "Issue IOUCTL time_stamp: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
233354dfcffbSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
233454dfcffbSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
233554dfcffbSKashyap Desai 		retval = -1;
233654dfcffbSKashyap Desai 		goto out_unlock;
233754dfcffbSKashyap Desai 	}
233854dfcffbSKashyap Desai 
233954dfcffbSKashyap Desai out_unlock:
234054dfcffbSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
234154dfcffbSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
234254dfcffbSKashyap Desai 
234354dfcffbSKashyap Desai out:
234454dfcffbSKashyap Desai 	return retval;
234554dfcffbSKashyap Desai }
234654dfcffbSKashyap Desai 
234754dfcffbSKashyap Desai /**
23482ac794baSSreekanth Reddy  * mpi3mr_print_pkg_ver - display controller fw package version
23492ac794baSSreekanth Reddy  * @mrioc: Adapter reference
23502ac794baSSreekanth Reddy  *
23512ac794baSSreekanth Reddy  * Retrieve firmware package version from the component image
23522ac794baSSreekanth Reddy  * header of the controller flash and display it.
23532ac794baSSreekanth Reddy  *
23542ac794baSSreekanth Reddy  * Return: 0 on success and non-zero on failure.
23552ac794baSSreekanth Reddy  */
23562ac794baSSreekanth Reddy static int mpi3mr_print_pkg_ver(struct mpi3mr_ioc *mrioc)
23572ac794baSSreekanth Reddy {
23582ac794baSSreekanth Reddy 	struct mpi3_ci_upload_request ci_upload;
23592ac794baSSreekanth Reddy 	int retval = -1;
23602ac794baSSreekanth Reddy 	void *data = NULL;
23612ac794baSSreekanth Reddy 	dma_addr_t data_dma;
23622ac794baSSreekanth Reddy 	struct mpi3_ci_manifest_mpi *manifest;
23632ac794baSSreekanth Reddy 	u32 data_len = sizeof(struct mpi3_ci_manifest_mpi);
23642ac794baSSreekanth Reddy 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
23652ac794baSSreekanth Reddy 
23662ac794baSSreekanth Reddy 	data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
23672ac794baSSreekanth Reddy 	    GFP_KERNEL);
23682ac794baSSreekanth Reddy 	if (!data)
23692ac794baSSreekanth Reddy 		return -ENOMEM;
23702ac794baSSreekanth Reddy 
23712ac794baSSreekanth Reddy 	memset(&ci_upload, 0, sizeof(ci_upload));
23722ac794baSSreekanth Reddy 	mutex_lock(&mrioc->init_cmds.mutex);
23732ac794baSSreekanth Reddy 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
23742ac794baSSreekanth Reddy 		ioc_err(mrioc, "sending get package version failed due to command in use\n");
23752ac794baSSreekanth Reddy 		mutex_unlock(&mrioc->init_cmds.mutex);
23762ac794baSSreekanth Reddy 		goto out;
23772ac794baSSreekanth Reddy 	}
23782ac794baSSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
23792ac794baSSreekanth Reddy 	mrioc->init_cmds.is_waiting = 1;
23802ac794baSSreekanth Reddy 	mrioc->init_cmds.callback = NULL;
23812ac794baSSreekanth Reddy 	ci_upload.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
23822ac794baSSreekanth Reddy 	ci_upload.function = MPI3_FUNCTION_CI_UPLOAD;
23832ac794baSSreekanth Reddy 	ci_upload.msg_flags = MPI3_CI_UPLOAD_MSGFLAGS_LOCATION_PRIMARY;
23842ac794baSSreekanth Reddy 	ci_upload.signature1 = cpu_to_le32(MPI3_IMAGE_HEADER_SIGNATURE1_MANIFEST);
23852ac794baSSreekanth Reddy 	ci_upload.image_offset = cpu_to_le32(MPI3_IMAGE_HEADER_SIZE);
23862ac794baSSreekanth Reddy 	ci_upload.segment_size = cpu_to_le32(data_len);
23872ac794baSSreekanth Reddy 
23882ac794baSSreekanth Reddy 	mpi3mr_add_sg_single(&ci_upload.sgl, sgl_flags, data_len,
23892ac794baSSreekanth Reddy 	    data_dma);
23902ac794baSSreekanth Reddy 	init_completion(&mrioc->init_cmds.done);
23912ac794baSSreekanth Reddy 	retval = mpi3mr_admin_request_post(mrioc, &ci_upload,
23922ac794baSSreekanth Reddy 	    sizeof(ci_upload), 1);
23932ac794baSSreekanth Reddy 	if (retval) {
23942ac794baSSreekanth Reddy 		ioc_err(mrioc, "posting get package version failed\n");
23952ac794baSSreekanth Reddy 		goto out_unlock;
23962ac794baSSreekanth Reddy 	}
23972ac794baSSreekanth Reddy 	wait_for_completion_timeout(&mrioc->init_cmds.done,
23982ac794baSSreekanth Reddy 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
23992ac794baSSreekanth Reddy 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
24002ac794baSSreekanth Reddy 		ioc_err(mrioc, "get package version timed out\n");
2401a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
2402a6856cc4SSreekanth Reddy 		    MPI3MR_RESET_FROM_GETPKGVER_TIMEOUT);
24032ac794baSSreekanth Reddy 		retval = -1;
24042ac794baSSreekanth Reddy 		goto out_unlock;
24052ac794baSSreekanth Reddy 	}
24062ac794baSSreekanth Reddy 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
24072ac794baSSreekanth Reddy 	    == MPI3_IOCSTATUS_SUCCESS) {
24082ac794baSSreekanth Reddy 		manifest = (struct mpi3_ci_manifest_mpi *) data;
24092ac794baSSreekanth Reddy 		if (manifest->manifest_type == MPI3_CI_MANIFEST_TYPE_MPI) {
24102ac794baSSreekanth Reddy 			ioc_info(mrioc,
24112ac794baSSreekanth Reddy 			    "firmware package version(%d.%d.%d.%d.%05d-%05d)\n",
24122ac794baSSreekanth Reddy 			    manifest->package_version.gen_major,
24132ac794baSSreekanth Reddy 			    manifest->package_version.gen_minor,
24142ac794baSSreekanth Reddy 			    manifest->package_version.phase_major,
24152ac794baSSreekanth Reddy 			    manifest->package_version.phase_minor,
24162ac794baSSreekanth Reddy 			    manifest->package_version.customer_id,
24172ac794baSSreekanth Reddy 			    manifest->package_version.build_num);
24182ac794baSSreekanth Reddy 		}
24192ac794baSSreekanth Reddy 	}
24202ac794baSSreekanth Reddy 	retval = 0;
24212ac794baSSreekanth Reddy out_unlock:
24222ac794baSSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
24232ac794baSSreekanth Reddy 	mutex_unlock(&mrioc->init_cmds.mutex);
24242ac794baSSreekanth Reddy 
24252ac794baSSreekanth Reddy out:
24262ac794baSSreekanth Reddy 	if (data)
24272ac794baSSreekanth Reddy 		dma_free_coherent(&mrioc->pdev->dev, data_len, data,
24282ac794baSSreekanth Reddy 		    data_dma);
24292ac794baSSreekanth Reddy 	return retval;
24302ac794baSSreekanth Reddy }
24312ac794baSSreekanth Reddy 
24322ac794baSSreekanth Reddy /**
2433672ae26cSKashyap Desai  * mpi3mr_watchdog_work - watchdog thread to monitor faults
2434672ae26cSKashyap Desai  * @work: work struct
2435672ae26cSKashyap Desai  *
2436672ae26cSKashyap Desai  * Watch dog work periodically executed (1 second interval) to
2437672ae26cSKashyap Desai  * monitor firmware fault and to issue periodic timer sync to
2438672ae26cSKashyap Desai  * the firmware.
2439672ae26cSKashyap Desai  *
2440672ae26cSKashyap Desai  * Return: Nothing.
2441672ae26cSKashyap Desai  */
2442672ae26cSKashyap Desai static void mpi3mr_watchdog_work(struct work_struct *work)
2443672ae26cSKashyap Desai {
2444672ae26cSKashyap Desai 	struct mpi3mr_ioc *mrioc =
2445672ae26cSKashyap Desai 	    container_of(work, struct mpi3mr_ioc, watchdog_work.work);
2446672ae26cSKashyap Desai 	unsigned long flags;
2447672ae26cSKashyap Desai 	enum mpi3mr_iocstate ioc_state;
244878b76a07SSreekanth Reddy 	u32 fault, host_diagnostic, ioc_status;
244978b76a07SSreekanth Reddy 	u32 reset_reason = MPI3MR_RESET_FROM_FAULT_WATCH;
2450672ae26cSKashyap Desai 
2451f2a79d20SSreekanth Reddy 	if (mrioc->reset_in_progress)
2452b64845a7SSreekanth Reddy 		return;
2453b64845a7SSreekanth Reddy 
2454f2a79d20SSreekanth Reddy 	if (!mrioc->unrecoverable && !pci_device_is_present(mrioc->pdev)) {
2455f2a79d20SSreekanth Reddy 		ioc_err(mrioc, "watchdog could not detect the controller\n");
2456f2a79d20SSreekanth Reddy 		mrioc->unrecoverable = 1;
2457f2a79d20SSreekanth Reddy 	}
2458f2a79d20SSreekanth Reddy 
2459f2a79d20SSreekanth Reddy 	if (mrioc->unrecoverable) {
2460f2a79d20SSreekanth Reddy 		ioc_err(mrioc,
2461f2a79d20SSreekanth Reddy 		    "flush pending commands for unrecoverable controller\n");
2462f2a79d20SSreekanth Reddy 		mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
2463f2a79d20SSreekanth Reddy 		return;
2464f2a79d20SSreekanth Reddy 	}
2465f2a79d20SSreekanth Reddy 
246654dfcffbSKashyap Desai 	if (mrioc->ts_update_counter++ >= MPI3MR_TSUPDATE_INTERVAL) {
246754dfcffbSKashyap Desai 		mrioc->ts_update_counter = 0;
246854dfcffbSKashyap Desai 		mpi3mr_sync_timestamp(mrioc);
246954dfcffbSKashyap Desai 	}
247054dfcffbSKashyap Desai 
247178b76a07SSreekanth Reddy 	if ((mrioc->prepare_for_reset) &&
247278b76a07SSreekanth Reddy 	    ((mrioc->prepare_for_reset_timeout_counter++) >=
247378b76a07SSreekanth Reddy 	     MPI3MR_PREPARE_FOR_RESET_TIMEOUT)) {
247478b76a07SSreekanth Reddy 		mpi3mr_soft_reset_handler(mrioc,
247578b76a07SSreekanth Reddy 		    MPI3MR_RESET_FROM_CIACTVRST_TIMER, 1);
247678b76a07SSreekanth Reddy 		return;
247778b76a07SSreekanth Reddy 	}
247878b76a07SSreekanth Reddy 
247978b76a07SSreekanth Reddy 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
248078b76a07SSreekanth Reddy 	if (ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) {
248178b76a07SSreekanth Reddy 		mpi3mr_soft_reset_handler(mrioc, MPI3MR_RESET_FROM_FIRMWARE, 0);
248278b76a07SSreekanth Reddy 		return;
248378b76a07SSreekanth Reddy 	}
248478b76a07SSreekanth Reddy 
2485672ae26cSKashyap Desai 	/*Check for fault state every one second and issue Soft reset*/
2486672ae26cSKashyap Desai 	ioc_state = mpi3mr_get_iocstate(mrioc);
248778b76a07SSreekanth Reddy 	if (ioc_state != MRIOC_STATE_FAULT)
248878b76a07SSreekanth Reddy 		goto schedule_work;
248978b76a07SSreekanth Reddy 
249078b76a07SSreekanth Reddy 	fault = readl(&mrioc->sysif_regs->fault) & MPI3_SYSIF_FAULT_CODE_MASK;
2491672ae26cSKashyap Desai 	host_diagnostic = readl(&mrioc->sysif_regs->host_diagnostic);
2492672ae26cSKashyap Desai 	if (host_diagnostic & MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS) {
2493672ae26cSKashyap Desai 		if (!mrioc->diagsave_timeout) {
2494672ae26cSKashyap Desai 			mpi3mr_print_fault_info(mrioc);
249578b76a07SSreekanth Reddy 			ioc_warn(mrioc, "diag save in progress\n");
2496672ae26cSKashyap Desai 		}
249778b76a07SSreekanth Reddy 		if ((mrioc->diagsave_timeout++) <= MPI3_SYSIF_DIAG_SAVE_TIMEOUT)
2498672ae26cSKashyap Desai 			goto schedule_work;
249978b76a07SSreekanth Reddy 	}
250078b76a07SSreekanth Reddy 
2501672ae26cSKashyap Desai 	mpi3mr_print_fault_info(mrioc);
2502672ae26cSKashyap Desai 	mrioc->diagsave_timeout = 0;
2503672ae26cSKashyap Desai 
250478b76a07SSreekanth Reddy 	switch (fault) {
2505bad2f28dSSreekanth Reddy 	case MPI3_SYSIF_FAULT_CODE_COMPLETE_RESET_NEEDED:
250678b76a07SSreekanth Reddy 	case MPI3_SYSIF_FAULT_CODE_POWER_CYCLE_REQUIRED:
2507bad2f28dSSreekanth Reddy 		ioc_warn(mrioc,
250878b76a07SSreekanth Reddy 		    "controller requires system power cycle, marking controller as unrecoverable\n");
2509672ae26cSKashyap Desai 		mrioc->unrecoverable = 1;
2510f2a79d20SSreekanth Reddy 		goto schedule_work;
251178b76a07SSreekanth Reddy 	case MPI3_SYSIF_FAULT_CODE_SOFT_RESET_IN_PROGRESS:
251278b76a07SSreekanth Reddy 		return;
251378b76a07SSreekanth Reddy 	case MPI3_SYSIF_FAULT_CODE_CI_ACTIVATION_RESET:
251478b76a07SSreekanth Reddy 		reset_reason = MPI3MR_RESET_FROM_CIACTIV_FAULT;
251578b76a07SSreekanth Reddy 		break;
251678b76a07SSreekanth Reddy 	default:
251778b76a07SSreekanth Reddy 		break;
2518672ae26cSKashyap Desai 	}
251978b76a07SSreekanth Reddy 	mpi3mr_soft_reset_handler(mrioc, reset_reason, 0);
252078b76a07SSreekanth Reddy 	return;
2521672ae26cSKashyap Desai 
2522672ae26cSKashyap Desai schedule_work:
2523672ae26cSKashyap Desai 	spin_lock_irqsave(&mrioc->watchdog_lock, flags);
2524672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2525672ae26cSKashyap Desai 		queue_delayed_work(mrioc->watchdog_work_q,
2526672ae26cSKashyap Desai 		    &mrioc->watchdog_work,
2527672ae26cSKashyap Desai 		    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
2528672ae26cSKashyap Desai 	spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
2529672ae26cSKashyap Desai 	return;
2530672ae26cSKashyap Desai }
2531672ae26cSKashyap Desai 
2532672ae26cSKashyap Desai /**
2533672ae26cSKashyap Desai  * mpi3mr_start_watchdog - Start watchdog
2534672ae26cSKashyap Desai  * @mrioc: Adapter instance reference
2535672ae26cSKashyap Desai  *
2536672ae26cSKashyap Desai  * Create and start the watchdog thread to monitor controller
2537672ae26cSKashyap Desai  * faults.
2538672ae26cSKashyap Desai  *
2539672ae26cSKashyap Desai  * Return: Nothing.
2540672ae26cSKashyap Desai  */
2541672ae26cSKashyap Desai void mpi3mr_start_watchdog(struct mpi3mr_ioc *mrioc)
2542672ae26cSKashyap Desai {
2543672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2544672ae26cSKashyap Desai 		return;
2545672ae26cSKashyap Desai 
2546672ae26cSKashyap Desai 	INIT_DELAYED_WORK(&mrioc->watchdog_work, mpi3mr_watchdog_work);
2547672ae26cSKashyap Desai 	snprintf(mrioc->watchdog_work_q_name,
2548672ae26cSKashyap Desai 	    sizeof(mrioc->watchdog_work_q_name), "watchdog_%s%d", mrioc->name,
2549672ae26cSKashyap Desai 	    mrioc->id);
2550672ae26cSKashyap Desai 	mrioc->watchdog_work_q =
2551672ae26cSKashyap Desai 	    create_singlethread_workqueue(mrioc->watchdog_work_q_name);
2552672ae26cSKashyap Desai 	if (!mrioc->watchdog_work_q) {
2553672ae26cSKashyap Desai 		ioc_err(mrioc, "%s: failed (line=%d)\n", __func__, __LINE__);
2554672ae26cSKashyap Desai 		return;
2555672ae26cSKashyap Desai 	}
2556672ae26cSKashyap Desai 
2557672ae26cSKashyap Desai 	if (mrioc->watchdog_work_q)
2558672ae26cSKashyap Desai 		queue_delayed_work(mrioc->watchdog_work_q,
2559672ae26cSKashyap Desai 		    &mrioc->watchdog_work,
2560672ae26cSKashyap Desai 		    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
2561672ae26cSKashyap Desai }
2562672ae26cSKashyap Desai 
2563672ae26cSKashyap Desai /**
2564672ae26cSKashyap Desai  * mpi3mr_stop_watchdog - Stop watchdog
2565672ae26cSKashyap Desai  * @mrioc: Adapter instance reference
2566672ae26cSKashyap Desai  *
2567672ae26cSKashyap Desai  * Stop the watchdog thread created to monitor controller
2568672ae26cSKashyap Desai  * faults.
2569672ae26cSKashyap Desai  *
2570672ae26cSKashyap Desai  * Return: Nothing.
2571672ae26cSKashyap Desai  */
2572672ae26cSKashyap Desai void mpi3mr_stop_watchdog(struct mpi3mr_ioc *mrioc)
2573672ae26cSKashyap Desai {
2574672ae26cSKashyap Desai 	unsigned long flags;
2575672ae26cSKashyap Desai 	struct workqueue_struct *wq;
2576672ae26cSKashyap Desai 
2577672ae26cSKashyap Desai 	spin_lock_irqsave(&mrioc->watchdog_lock, flags);
2578672ae26cSKashyap Desai 	wq = mrioc->watchdog_work_q;
2579672ae26cSKashyap Desai 	mrioc->watchdog_work_q = NULL;
2580672ae26cSKashyap Desai 	spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
2581672ae26cSKashyap Desai 	if (wq) {
2582672ae26cSKashyap Desai 		if (!cancel_delayed_work_sync(&mrioc->watchdog_work))
2583672ae26cSKashyap Desai 			flush_workqueue(wq);
2584672ae26cSKashyap Desai 		destroy_workqueue(wq);
2585672ae26cSKashyap Desai 	}
2586672ae26cSKashyap Desai }
2587672ae26cSKashyap Desai 
2588672ae26cSKashyap Desai /**
2589824a1566SKashyap Desai  * mpi3mr_setup_admin_qpair - Setup admin queue pair
2590824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2591824a1566SKashyap Desai  *
2592824a1566SKashyap Desai  * Allocate memory for admin queue pair if required and register
2593824a1566SKashyap Desai  * the admin queue with the controller.
2594824a1566SKashyap Desai  *
2595824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2596824a1566SKashyap Desai  */
2597824a1566SKashyap Desai static int mpi3mr_setup_admin_qpair(struct mpi3mr_ioc *mrioc)
2598824a1566SKashyap Desai {
2599824a1566SKashyap Desai 	int retval = 0;
2600824a1566SKashyap Desai 	u32 num_admin_entries = 0;
2601824a1566SKashyap Desai 
2602824a1566SKashyap Desai 	mrioc->admin_req_q_sz = MPI3MR_ADMIN_REQ_Q_SIZE;
2603824a1566SKashyap Desai 	mrioc->num_admin_req = mrioc->admin_req_q_sz /
2604824a1566SKashyap Desai 	    MPI3MR_ADMIN_REQ_FRAME_SZ;
2605824a1566SKashyap Desai 	mrioc->admin_req_ci = mrioc->admin_req_pi = 0;
2606824a1566SKashyap Desai 	mrioc->admin_req_base = NULL;
2607824a1566SKashyap Desai 
2608824a1566SKashyap Desai 	mrioc->admin_reply_q_sz = MPI3MR_ADMIN_REPLY_Q_SIZE;
2609824a1566SKashyap Desai 	mrioc->num_admin_replies = mrioc->admin_reply_q_sz /
2610824a1566SKashyap Desai 	    MPI3MR_ADMIN_REPLY_FRAME_SZ;
2611824a1566SKashyap Desai 	mrioc->admin_reply_ci = 0;
2612824a1566SKashyap Desai 	mrioc->admin_reply_ephase = 1;
2613824a1566SKashyap Desai 	mrioc->admin_reply_base = NULL;
2614*02ca7da2SRanjan Kumar 	atomic_set(&mrioc->admin_reply_q_in_use, 0);
2615824a1566SKashyap Desai 
2616824a1566SKashyap Desai 	if (!mrioc->admin_req_base) {
2617824a1566SKashyap Desai 		mrioc->admin_req_base = dma_alloc_coherent(&mrioc->pdev->dev,
2618824a1566SKashyap Desai 		    mrioc->admin_req_q_sz, &mrioc->admin_req_dma, GFP_KERNEL);
2619824a1566SKashyap Desai 
2620824a1566SKashyap Desai 		if (!mrioc->admin_req_base) {
2621824a1566SKashyap Desai 			retval = -1;
2622824a1566SKashyap Desai 			goto out_failed;
2623824a1566SKashyap Desai 		}
2624824a1566SKashyap Desai 
2625824a1566SKashyap Desai 		mrioc->admin_reply_base = dma_alloc_coherent(&mrioc->pdev->dev,
2626824a1566SKashyap Desai 		    mrioc->admin_reply_q_sz, &mrioc->admin_reply_dma,
2627824a1566SKashyap Desai 		    GFP_KERNEL);
2628824a1566SKashyap Desai 
2629824a1566SKashyap Desai 		if (!mrioc->admin_reply_base) {
2630824a1566SKashyap Desai 			retval = -1;
2631824a1566SKashyap Desai 			goto out_failed;
2632824a1566SKashyap Desai 		}
2633824a1566SKashyap Desai 	}
2634824a1566SKashyap Desai 
2635824a1566SKashyap Desai 	num_admin_entries = (mrioc->num_admin_replies << 16) |
2636824a1566SKashyap Desai 	    (mrioc->num_admin_req);
2637824a1566SKashyap Desai 	writel(num_admin_entries, &mrioc->sysif_regs->admin_queue_num_entries);
2638824a1566SKashyap Desai 	mpi3mr_writeq(mrioc->admin_req_dma,
2639824a1566SKashyap Desai 	    &mrioc->sysif_regs->admin_request_queue_address);
2640824a1566SKashyap Desai 	mpi3mr_writeq(mrioc->admin_reply_dma,
2641824a1566SKashyap Desai 	    &mrioc->sysif_regs->admin_reply_queue_address);
2642824a1566SKashyap Desai 	writel(mrioc->admin_req_pi, &mrioc->sysif_regs->admin_request_queue_pi);
2643824a1566SKashyap Desai 	writel(mrioc->admin_reply_ci, &mrioc->sysif_regs->admin_reply_queue_ci);
2644824a1566SKashyap Desai 	return retval;
2645824a1566SKashyap Desai 
2646824a1566SKashyap Desai out_failed:
2647824a1566SKashyap Desai 
2648824a1566SKashyap Desai 	if (mrioc->admin_reply_base) {
2649824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
2650824a1566SKashyap Desai 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
2651824a1566SKashyap Desai 		mrioc->admin_reply_base = NULL;
2652824a1566SKashyap Desai 	}
2653824a1566SKashyap Desai 	if (mrioc->admin_req_base) {
2654824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
2655824a1566SKashyap Desai 		    mrioc->admin_req_base, mrioc->admin_req_dma);
2656824a1566SKashyap Desai 		mrioc->admin_req_base = NULL;
2657824a1566SKashyap Desai 	}
2658824a1566SKashyap Desai 	return retval;
2659824a1566SKashyap Desai }
2660824a1566SKashyap Desai 
2661824a1566SKashyap Desai /**
2662824a1566SKashyap Desai  * mpi3mr_issue_iocfacts - Send IOC Facts
2663824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2664824a1566SKashyap Desai  * @facts_data: Cached IOC facts data
2665824a1566SKashyap Desai  *
2666824a1566SKashyap Desai  * Issue IOC Facts MPI request through admin queue and wait for
2667824a1566SKashyap Desai  * the completion of it or time out.
2668824a1566SKashyap Desai  *
2669824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2670824a1566SKashyap Desai  */
2671824a1566SKashyap Desai static int mpi3mr_issue_iocfacts(struct mpi3mr_ioc *mrioc,
2672824a1566SKashyap Desai 	struct mpi3_ioc_facts_data *facts_data)
2673824a1566SKashyap Desai {
2674824a1566SKashyap Desai 	struct mpi3_ioc_facts_request iocfacts_req;
2675824a1566SKashyap Desai 	void *data = NULL;
2676824a1566SKashyap Desai 	dma_addr_t data_dma;
2677824a1566SKashyap Desai 	u32 data_len = sizeof(*facts_data);
2678824a1566SKashyap Desai 	int retval = 0;
2679824a1566SKashyap Desai 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
2680824a1566SKashyap Desai 
2681824a1566SKashyap Desai 	data = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
2682824a1566SKashyap Desai 	    GFP_KERNEL);
2683824a1566SKashyap Desai 
2684824a1566SKashyap Desai 	if (!data) {
2685824a1566SKashyap Desai 		retval = -1;
2686824a1566SKashyap Desai 		goto out;
2687824a1566SKashyap Desai 	}
2688824a1566SKashyap Desai 
2689824a1566SKashyap Desai 	memset(&iocfacts_req, 0, sizeof(iocfacts_req));
2690824a1566SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
2691824a1566SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
2692824a1566SKashyap Desai 		retval = -1;
2693824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCFacts: Init command is in use\n");
2694824a1566SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
2695824a1566SKashyap Desai 		goto out;
2696824a1566SKashyap Desai 	}
2697824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
2698824a1566SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
2699824a1566SKashyap Desai 	mrioc->init_cmds.callback = NULL;
2700824a1566SKashyap Desai 	iocfacts_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
2701824a1566SKashyap Desai 	iocfacts_req.function = MPI3_FUNCTION_IOC_FACTS;
2702824a1566SKashyap Desai 
2703824a1566SKashyap Desai 	mpi3mr_add_sg_single(&iocfacts_req.sgl, sgl_flags, data_len,
2704824a1566SKashyap Desai 	    data_dma);
2705824a1566SKashyap Desai 
2706824a1566SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
2707824a1566SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iocfacts_req,
2708824a1566SKashyap Desai 	    sizeof(iocfacts_req), 1);
2709824a1566SKashyap Desai 	if (retval) {
2710824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCFacts: Admin Post failed\n");
2711824a1566SKashyap Desai 		goto out_unlock;
2712824a1566SKashyap Desai 	}
2713824a1566SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
2714824a1566SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
2715824a1566SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
2716a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "ioc_facts timed out\n");
2717a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
2718824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_IOCFACTS_TIMEOUT);
2719824a1566SKashyap Desai 		retval = -1;
2720824a1566SKashyap Desai 		goto out_unlock;
2721824a1566SKashyap Desai 	}
2722824a1566SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
2723824a1566SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
2724824a1566SKashyap Desai 		ioc_err(mrioc,
2725824a1566SKashyap Desai 		    "Issue IOCFacts: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
2726824a1566SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
2727824a1566SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
2728824a1566SKashyap Desai 		retval = -1;
2729824a1566SKashyap Desai 		goto out_unlock;
2730824a1566SKashyap Desai 	}
2731824a1566SKashyap Desai 	memcpy(facts_data, (u8 *)data, data_len);
2732c5758fc7SSreekanth Reddy 	mpi3mr_process_factsdata(mrioc, facts_data);
2733824a1566SKashyap Desai out_unlock:
2734824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
2735824a1566SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
2736824a1566SKashyap Desai 
2737824a1566SKashyap Desai out:
2738824a1566SKashyap Desai 	if (data)
2739824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, data_len, data, data_dma);
2740824a1566SKashyap Desai 
2741824a1566SKashyap Desai 	return retval;
2742824a1566SKashyap Desai }
2743824a1566SKashyap Desai 
2744824a1566SKashyap Desai /**
2745824a1566SKashyap Desai  * mpi3mr_check_reset_dma_mask - Process IOC facts data
2746824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2747824a1566SKashyap Desai  *
2748824a1566SKashyap Desai  * Check whether the new DMA mask requested through IOCFacts by
2749824a1566SKashyap Desai  * firmware needs to be set, if so set it .
2750824a1566SKashyap Desai  *
2751824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
2752824a1566SKashyap Desai  */
2753824a1566SKashyap Desai static inline int mpi3mr_check_reset_dma_mask(struct mpi3mr_ioc *mrioc)
2754824a1566SKashyap Desai {
2755824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
2756824a1566SKashyap Desai 	int r;
2757824a1566SKashyap Desai 	u64 facts_dma_mask = DMA_BIT_MASK(mrioc->facts.dma_mask);
2758824a1566SKashyap Desai 
2759824a1566SKashyap Desai 	if (!mrioc->facts.dma_mask || (mrioc->dma_mask <= facts_dma_mask))
2760824a1566SKashyap Desai 		return 0;
2761824a1566SKashyap Desai 
2762824a1566SKashyap Desai 	ioc_info(mrioc, "Changing DMA mask from 0x%016llx to 0x%016llx\n",
2763824a1566SKashyap Desai 	    mrioc->dma_mask, facts_dma_mask);
2764824a1566SKashyap Desai 
2765824a1566SKashyap Desai 	r = dma_set_mask_and_coherent(&pdev->dev, facts_dma_mask);
2766824a1566SKashyap Desai 	if (r) {
2767824a1566SKashyap Desai 		ioc_err(mrioc, "Setting DMA mask to 0x%016llx failed: %d\n",
2768824a1566SKashyap Desai 		    facts_dma_mask, r);
2769824a1566SKashyap Desai 		return r;
2770824a1566SKashyap Desai 	}
2771824a1566SKashyap Desai 	mrioc->dma_mask = facts_dma_mask;
2772824a1566SKashyap Desai 	return r;
2773824a1566SKashyap Desai }
2774824a1566SKashyap Desai 
2775824a1566SKashyap Desai /**
2776824a1566SKashyap Desai  * mpi3mr_process_factsdata - Process IOC facts data
2777824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2778824a1566SKashyap Desai  * @facts_data: Cached IOC facts data
2779824a1566SKashyap Desai  *
2780824a1566SKashyap Desai  * Convert IOC facts data into cpu endianness and cache it in
2781824a1566SKashyap Desai  * the driver .
2782824a1566SKashyap Desai  *
2783824a1566SKashyap Desai  * Return: Nothing.
2784824a1566SKashyap Desai  */
2785824a1566SKashyap Desai static void mpi3mr_process_factsdata(struct mpi3mr_ioc *mrioc,
2786824a1566SKashyap Desai 	struct mpi3_ioc_facts_data *facts_data)
2787824a1566SKashyap Desai {
2788824a1566SKashyap Desai 	u32 ioc_config, req_sz, facts_flags;
2789824a1566SKashyap Desai 
2790824a1566SKashyap Desai 	if ((le16_to_cpu(facts_data->ioc_facts_data_length)) !=
2791824a1566SKashyap Desai 	    (sizeof(*facts_data) / 4)) {
2792824a1566SKashyap Desai 		ioc_warn(mrioc,
2793824a1566SKashyap Desai 		    "IOCFactsdata length mismatch driver_sz(%zu) firmware_sz(%d)\n",
2794824a1566SKashyap Desai 		    sizeof(*facts_data),
2795824a1566SKashyap Desai 		    le16_to_cpu(facts_data->ioc_facts_data_length) * 4);
2796824a1566SKashyap Desai 	}
2797824a1566SKashyap Desai 
2798824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
2799824a1566SKashyap Desai 	req_sz = 1 << ((ioc_config & MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ) >>
2800824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_REQ_ENT_SZ_SHIFT);
2801824a1566SKashyap Desai 	if (le16_to_cpu(facts_data->ioc_request_frame_size) != (req_sz / 4)) {
2802824a1566SKashyap Desai 		ioc_err(mrioc,
2803824a1566SKashyap Desai 		    "IOCFacts data reqFrameSize mismatch hw_size(%d) firmware_sz(%d)\n",
2804824a1566SKashyap Desai 		    req_sz / 4, le16_to_cpu(facts_data->ioc_request_frame_size));
2805824a1566SKashyap Desai 	}
2806824a1566SKashyap Desai 
2807824a1566SKashyap Desai 	memset(&mrioc->facts, 0, sizeof(mrioc->facts));
2808824a1566SKashyap Desai 
2809824a1566SKashyap Desai 	facts_flags = le32_to_cpu(facts_data->flags);
2810824a1566SKashyap Desai 	mrioc->facts.op_req_sz = req_sz;
2811824a1566SKashyap Desai 	mrioc->op_reply_desc_sz = 1 << ((ioc_config &
2812824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ) >>
2813824a1566SKashyap Desai 	    MPI3_SYSIF_IOC_CONFIG_OPER_RPY_ENT_SZ_SHIFT);
2814824a1566SKashyap Desai 
2815824a1566SKashyap Desai 	mrioc->facts.ioc_num = facts_data->ioc_number;
2816824a1566SKashyap Desai 	mrioc->facts.who_init = facts_data->who_init;
2817824a1566SKashyap Desai 	mrioc->facts.max_msix_vectors = le16_to_cpu(facts_data->max_msix_vectors);
2818824a1566SKashyap Desai 	mrioc->facts.personality = (facts_flags &
2819824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_PERSONALITY_MASK);
2820824a1566SKashyap Desai 	mrioc->facts.dma_mask = (facts_flags &
2821824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_MASK) >>
2822824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_DMA_ADDRESS_WIDTH_SHIFT;
2823824a1566SKashyap Desai 	mrioc->facts.protocol_flags = facts_data->protocol_flags;
2824824a1566SKashyap Desai 	mrioc->facts.mpi_version = le32_to_cpu(facts_data->mpi_version.word);
282504b27e53SSreekanth Reddy 	mrioc->facts.max_reqs = le16_to_cpu(facts_data->max_outstanding_requests);
2826824a1566SKashyap Desai 	mrioc->facts.product_id = le16_to_cpu(facts_data->product_id);
2827824a1566SKashyap Desai 	mrioc->facts.reply_sz = le16_to_cpu(facts_data->reply_frame_size) * 4;
2828824a1566SKashyap Desai 	mrioc->facts.exceptions = le16_to_cpu(facts_data->ioc_exceptions);
2829824a1566SKashyap Desai 	mrioc->facts.max_perids = le16_to_cpu(facts_data->max_persistent_id);
2830824a1566SKashyap Desai 	mrioc->facts.max_vds = le16_to_cpu(facts_data->max_vds);
2831824a1566SKashyap Desai 	mrioc->facts.max_hpds = le16_to_cpu(facts_data->max_host_pds);
2832ec5ebd2cSSreekanth Reddy 	mrioc->facts.max_advhpds = le16_to_cpu(facts_data->max_adv_host_pds);
2833ec5ebd2cSSreekanth Reddy 	mrioc->facts.max_raid_pds = le16_to_cpu(facts_data->max_raid_pds);
2834824a1566SKashyap Desai 	mrioc->facts.max_nvme = le16_to_cpu(facts_data->max_nvme);
2835824a1566SKashyap Desai 	mrioc->facts.max_pcie_switches =
2836ec5ebd2cSSreekanth Reddy 	    le16_to_cpu(facts_data->max_pcie_switches);
2837824a1566SKashyap Desai 	mrioc->facts.max_sasexpanders =
2838824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_sas_expanders);
2839824a1566SKashyap Desai 	mrioc->facts.max_sasinitiators =
2840824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_sas_initiators);
2841824a1566SKashyap Desai 	mrioc->facts.max_enclosures = le16_to_cpu(facts_data->max_enclosures);
2842824a1566SKashyap Desai 	mrioc->facts.min_devhandle = le16_to_cpu(facts_data->min_dev_handle);
2843824a1566SKashyap Desai 	mrioc->facts.max_devhandle = le16_to_cpu(facts_data->max_dev_handle);
2844824a1566SKashyap Desai 	mrioc->facts.max_op_req_q =
2845824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_operational_request_queues);
2846824a1566SKashyap Desai 	mrioc->facts.max_op_reply_q =
2847824a1566SKashyap Desai 	    le16_to_cpu(facts_data->max_operational_reply_queues);
2848824a1566SKashyap Desai 	mrioc->facts.ioc_capabilities =
2849824a1566SKashyap Desai 	    le32_to_cpu(facts_data->ioc_capabilities);
2850824a1566SKashyap Desai 	mrioc->facts.fw_ver.build_num =
2851824a1566SKashyap Desai 	    le16_to_cpu(facts_data->fw_version.build_num);
2852824a1566SKashyap Desai 	mrioc->facts.fw_ver.cust_id =
2853824a1566SKashyap Desai 	    le16_to_cpu(facts_data->fw_version.customer_id);
2854824a1566SKashyap Desai 	mrioc->facts.fw_ver.ph_minor = facts_data->fw_version.phase_minor;
2855824a1566SKashyap Desai 	mrioc->facts.fw_ver.ph_major = facts_data->fw_version.phase_major;
2856824a1566SKashyap Desai 	mrioc->facts.fw_ver.gen_minor = facts_data->fw_version.gen_minor;
2857824a1566SKashyap Desai 	mrioc->facts.fw_ver.gen_major = facts_data->fw_version.gen_major;
2858824a1566SKashyap Desai 	mrioc->msix_count = min_t(int, mrioc->msix_count,
2859824a1566SKashyap Desai 	    mrioc->facts.max_msix_vectors);
2860824a1566SKashyap Desai 	mrioc->facts.sge_mod_mask = facts_data->sge_modifier_mask;
2861824a1566SKashyap Desai 	mrioc->facts.sge_mod_value = facts_data->sge_modifier_value;
2862824a1566SKashyap Desai 	mrioc->facts.sge_mod_shift = facts_data->sge_modifier_shift;
2863824a1566SKashyap Desai 	mrioc->facts.shutdown_timeout =
2864824a1566SKashyap Desai 	    le16_to_cpu(facts_data->shutdown_timeout);
2865824a1566SKashyap Desai 
2866f10af057SSreekanth Reddy 	mrioc->facts.max_dev_per_tg =
2867f10af057SSreekanth Reddy 	    facts_data->max_devices_per_throttle_group;
2868f10af057SSreekanth Reddy 	mrioc->facts.io_throttle_data_length =
2869f10af057SSreekanth Reddy 	    le16_to_cpu(facts_data->io_throttle_data_length);
2870f10af057SSreekanth Reddy 	mrioc->facts.max_io_throttle_group =
2871f10af057SSreekanth Reddy 	    le16_to_cpu(facts_data->max_io_throttle_group);
2872f10af057SSreekanth Reddy 	mrioc->facts.io_throttle_low = le16_to_cpu(facts_data->io_throttle_low);
2873f10af057SSreekanth Reddy 	mrioc->facts.io_throttle_high =
2874f10af057SSreekanth Reddy 	    le16_to_cpu(facts_data->io_throttle_high);
2875f10af057SSreekanth Reddy 
2876f10af057SSreekanth Reddy 	/* Store in 512b block count */
2877f10af057SSreekanth Reddy 	if (mrioc->facts.io_throttle_data_length)
2878f10af057SSreekanth Reddy 		mrioc->io_throttle_data_length =
2879f10af057SSreekanth Reddy 		    (mrioc->facts.io_throttle_data_length * 2 * 4);
2880f10af057SSreekanth Reddy 	else
2881f10af057SSreekanth Reddy 		/* set the length to 1MB + 1K to disable throttle */
2882f10af057SSreekanth Reddy 		mrioc->io_throttle_data_length = MPI3MR_MAX_SECTORS + 2;
2883f10af057SSreekanth Reddy 
2884f10af057SSreekanth Reddy 	mrioc->io_throttle_high = (mrioc->facts.io_throttle_high * 2 * 1024);
2885f10af057SSreekanth Reddy 	mrioc->io_throttle_low = (mrioc->facts.io_throttle_low * 2 * 1024);
2886f10af057SSreekanth Reddy 
2887824a1566SKashyap Desai 	ioc_info(mrioc, "ioc_num(%d), maxopQ(%d), maxopRepQ(%d), maxdh(%d),",
2888824a1566SKashyap Desai 	    mrioc->facts.ioc_num, mrioc->facts.max_op_req_q,
2889824a1566SKashyap Desai 	    mrioc->facts.max_op_reply_q, mrioc->facts.max_devhandle);
2890824a1566SKashyap Desai 	ioc_info(mrioc,
2891ec5ebd2cSSreekanth Reddy 	    "maxreqs(%d), mindh(%d) maxvectors(%d) maxperids(%d)\n",
2892824a1566SKashyap Desai 	    mrioc->facts.max_reqs, mrioc->facts.min_devhandle,
2893ec5ebd2cSSreekanth Reddy 	    mrioc->facts.max_msix_vectors, mrioc->facts.max_perids);
2894824a1566SKashyap Desai 	ioc_info(mrioc, "SGEModMask 0x%x SGEModVal 0x%x SGEModShift 0x%x ",
2895824a1566SKashyap Desai 	    mrioc->facts.sge_mod_mask, mrioc->facts.sge_mod_value,
2896824a1566SKashyap Desai 	    mrioc->facts.sge_mod_shift);
2897824a1566SKashyap Desai 	ioc_info(mrioc, "DMA mask %d InitialPE status 0x%x\n",
2898824a1566SKashyap Desai 	    mrioc->facts.dma_mask, (facts_flags &
2899824a1566SKashyap Desai 	    MPI3_IOCFACTS_FLAGS_INITIAL_PORT_ENABLE_MASK));
2900f10af057SSreekanth Reddy 	ioc_info(mrioc,
2901f10af057SSreekanth Reddy 	    "max_dev_per_throttle_group(%d), max_throttle_groups(%d)\n",
2902f10af057SSreekanth Reddy 	    mrioc->facts.max_dev_per_tg, mrioc->facts.max_io_throttle_group);
2903f10af057SSreekanth Reddy 	ioc_info(mrioc,
2904f10af057SSreekanth Reddy 	   "io_throttle_data_len(%dKiB), io_throttle_high(%dMiB), io_throttle_low(%dMiB)\n",
2905f10af057SSreekanth Reddy 	   mrioc->facts.io_throttle_data_length * 4,
2906f10af057SSreekanth Reddy 	   mrioc->facts.io_throttle_high, mrioc->facts.io_throttle_low);
2907824a1566SKashyap Desai }
2908824a1566SKashyap Desai 
2909824a1566SKashyap Desai /**
2910824a1566SKashyap Desai  * mpi3mr_alloc_reply_sense_bufs - Send IOC Init
2911824a1566SKashyap Desai  * @mrioc: Adapter instance reference
2912824a1566SKashyap Desai  *
2913824a1566SKashyap Desai  * Allocate and initialize the reply free buffers, sense
2914824a1566SKashyap Desai  * buffers, reply free queue and sense buffer queue.
2915824a1566SKashyap Desai  *
2916824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
2917824a1566SKashyap Desai  */
2918824a1566SKashyap Desai static int mpi3mr_alloc_reply_sense_bufs(struct mpi3mr_ioc *mrioc)
2919824a1566SKashyap Desai {
2920824a1566SKashyap Desai 	int retval = 0;
2921824a1566SKashyap Desai 	u32 sz, i;
2922824a1566SKashyap Desai 
2923824a1566SKashyap Desai 	if (mrioc->init_cmds.reply)
2924e3605f65SSreekanth Reddy 		return retval;
2925824a1566SKashyap Desai 
2926c5758fc7SSreekanth Reddy 	mrioc->init_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
2927824a1566SKashyap Desai 	if (!mrioc->init_cmds.reply)
2928824a1566SKashyap Desai 		goto out_failed;
2929824a1566SKashyap Desai 
2930f5e6d5a3SSumit Saxena 	mrioc->bsg_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
2931f5e6d5a3SSumit Saxena 	if (!mrioc->bsg_cmds.reply)
2932f5e6d5a3SSumit Saxena 		goto out_failed;
2933f5e6d5a3SSumit Saxena 
29342bd37e28SSreekanth Reddy 	mrioc->transport_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
29352bd37e28SSreekanth Reddy 	if (!mrioc->transport_cmds.reply)
29362bd37e28SSreekanth Reddy 		goto out_failed;
29372bd37e28SSreekanth Reddy 
293813ef29eaSKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
2939c5758fc7SSreekanth Reddy 		mrioc->dev_rmhs_cmds[i].reply = kzalloc(mrioc->reply_sz,
294013ef29eaSKashyap Desai 		    GFP_KERNEL);
294113ef29eaSKashyap Desai 		if (!mrioc->dev_rmhs_cmds[i].reply)
294213ef29eaSKashyap Desai 			goto out_failed;
294313ef29eaSKashyap Desai 	}
294413ef29eaSKashyap Desai 
2945c1af985dSSreekanth Reddy 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
2946c1af985dSSreekanth Reddy 		mrioc->evtack_cmds[i].reply = kzalloc(mrioc->reply_sz,
2947c1af985dSSreekanth Reddy 		    GFP_KERNEL);
2948c1af985dSSreekanth Reddy 		if (!mrioc->evtack_cmds[i].reply)
2949c1af985dSSreekanth Reddy 			goto out_failed;
2950c1af985dSSreekanth Reddy 	}
2951c1af985dSSreekanth Reddy 
2952c5758fc7SSreekanth Reddy 	mrioc->host_tm_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
2953e844adb1SKashyap Desai 	if (!mrioc->host_tm_cmds.reply)
2954e844adb1SKashyap Desai 		goto out_failed;
2955e844adb1SKashyap Desai 
295643ca1100SSumit Saxena 	mrioc->pel_cmds.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
295743ca1100SSumit Saxena 	if (!mrioc->pel_cmds.reply)
295843ca1100SSumit Saxena 		goto out_failed;
295943ca1100SSumit Saxena 
296043ca1100SSumit Saxena 	mrioc->pel_abort_cmd.reply = kzalloc(mrioc->reply_sz, GFP_KERNEL);
296143ca1100SSumit Saxena 	if (!mrioc->pel_abort_cmd.reply)
296243ca1100SSumit Saxena 		goto out_failed;
296343ca1100SSumit Saxena 
2964339e6156SShin'ichiro Kawasaki 	mrioc->dev_handle_bitmap_bits = mrioc->facts.max_devhandle;
2965339e6156SShin'ichiro Kawasaki 	mrioc->removepend_bitmap = bitmap_zalloc(mrioc->dev_handle_bitmap_bits,
2966e844adb1SKashyap Desai 						 GFP_KERNEL);
2967e844adb1SKashyap Desai 	if (!mrioc->removepend_bitmap)
2968e844adb1SKashyap Desai 		goto out_failed;
2969e844adb1SKashyap Desai 
2970339e6156SShin'ichiro Kawasaki 	mrioc->devrem_bitmap = bitmap_zalloc(MPI3MR_NUM_DEVRMCMD, GFP_KERNEL);
2971e844adb1SKashyap Desai 	if (!mrioc->devrem_bitmap)
2972e844adb1SKashyap Desai 		goto out_failed;
2973e844adb1SKashyap Desai 
2974339e6156SShin'ichiro Kawasaki 	mrioc->evtack_cmds_bitmap = bitmap_zalloc(MPI3MR_NUM_EVTACKCMD,
2975c1af985dSSreekanth Reddy 						  GFP_KERNEL);
2976c1af985dSSreekanth Reddy 	if (!mrioc->evtack_cmds_bitmap)
2977c1af985dSSreekanth Reddy 		goto out_failed;
2978c1af985dSSreekanth Reddy 
2979824a1566SKashyap Desai 	mrioc->num_reply_bufs = mrioc->facts.max_reqs + MPI3MR_NUM_EVT_REPLIES;
2980824a1566SKashyap Desai 	mrioc->reply_free_qsz = mrioc->num_reply_bufs + 1;
2981824a1566SKashyap Desai 	mrioc->num_sense_bufs = mrioc->facts.max_reqs / MPI3MR_SENSEBUF_FACTOR;
2982824a1566SKashyap Desai 	mrioc->sense_buf_q_sz = mrioc->num_sense_bufs + 1;
2983824a1566SKashyap Desai 
2984824a1566SKashyap Desai 	/* reply buffer pool, 16 byte align */
2985c5758fc7SSreekanth Reddy 	sz = mrioc->num_reply_bufs * mrioc->reply_sz;
2986824a1566SKashyap Desai 	mrioc->reply_buf_pool = dma_pool_create("reply_buf pool",
2987824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 16, 0);
2988824a1566SKashyap Desai 	if (!mrioc->reply_buf_pool) {
2989824a1566SKashyap Desai 		ioc_err(mrioc, "reply buf pool: dma_pool_create failed\n");
2990824a1566SKashyap Desai 		goto out_failed;
2991824a1566SKashyap Desai 	}
2992824a1566SKashyap Desai 
2993824a1566SKashyap Desai 	mrioc->reply_buf = dma_pool_zalloc(mrioc->reply_buf_pool, GFP_KERNEL,
2994824a1566SKashyap Desai 	    &mrioc->reply_buf_dma);
2995824a1566SKashyap Desai 	if (!mrioc->reply_buf)
2996824a1566SKashyap Desai 		goto out_failed;
2997824a1566SKashyap Desai 
2998824a1566SKashyap Desai 	mrioc->reply_buf_dma_max_address = mrioc->reply_buf_dma + sz;
2999824a1566SKashyap Desai 
3000824a1566SKashyap Desai 	/* reply free queue, 8 byte align */
3001824a1566SKashyap Desai 	sz = mrioc->reply_free_qsz * 8;
3002824a1566SKashyap Desai 	mrioc->reply_free_q_pool = dma_pool_create("reply_free_q pool",
3003824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 8, 0);
3004824a1566SKashyap Desai 	if (!mrioc->reply_free_q_pool) {
3005824a1566SKashyap Desai 		ioc_err(mrioc, "reply_free_q pool: dma_pool_create failed\n");
3006824a1566SKashyap Desai 		goto out_failed;
3007824a1566SKashyap Desai 	}
3008824a1566SKashyap Desai 	mrioc->reply_free_q = dma_pool_zalloc(mrioc->reply_free_q_pool,
3009824a1566SKashyap Desai 	    GFP_KERNEL, &mrioc->reply_free_q_dma);
3010824a1566SKashyap Desai 	if (!mrioc->reply_free_q)
3011824a1566SKashyap Desai 		goto out_failed;
3012824a1566SKashyap Desai 
3013824a1566SKashyap Desai 	/* sense buffer pool,  4 byte align */
3014ec5ebd2cSSreekanth Reddy 	sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
3015824a1566SKashyap Desai 	mrioc->sense_buf_pool = dma_pool_create("sense_buf pool",
3016824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 4, 0);
3017824a1566SKashyap Desai 	if (!mrioc->sense_buf_pool) {
3018824a1566SKashyap Desai 		ioc_err(mrioc, "sense_buf pool: dma_pool_create failed\n");
3019824a1566SKashyap Desai 		goto out_failed;
3020824a1566SKashyap Desai 	}
3021824a1566SKashyap Desai 	mrioc->sense_buf = dma_pool_zalloc(mrioc->sense_buf_pool, GFP_KERNEL,
3022824a1566SKashyap Desai 	    &mrioc->sense_buf_dma);
3023824a1566SKashyap Desai 	if (!mrioc->sense_buf)
3024824a1566SKashyap Desai 		goto out_failed;
3025824a1566SKashyap Desai 
3026824a1566SKashyap Desai 	/* sense buffer queue, 8 byte align */
3027824a1566SKashyap Desai 	sz = mrioc->sense_buf_q_sz * 8;
3028824a1566SKashyap Desai 	mrioc->sense_buf_q_pool = dma_pool_create("sense_buf_q pool",
3029824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 8, 0);
3030824a1566SKashyap Desai 	if (!mrioc->sense_buf_q_pool) {
3031824a1566SKashyap Desai 		ioc_err(mrioc, "sense_buf_q pool: dma_pool_create failed\n");
3032824a1566SKashyap Desai 		goto out_failed;
3033824a1566SKashyap Desai 	}
3034824a1566SKashyap Desai 	mrioc->sense_buf_q = dma_pool_zalloc(mrioc->sense_buf_q_pool,
3035824a1566SKashyap Desai 	    GFP_KERNEL, &mrioc->sense_buf_q_dma);
3036824a1566SKashyap Desai 	if (!mrioc->sense_buf_q)
3037824a1566SKashyap Desai 		goto out_failed;
3038824a1566SKashyap Desai 
3039e3605f65SSreekanth Reddy 	return retval;
3040e3605f65SSreekanth Reddy 
3041e3605f65SSreekanth Reddy out_failed:
3042e3605f65SSreekanth Reddy 	retval = -1;
3043e3605f65SSreekanth Reddy 	return retval;
3044e3605f65SSreekanth Reddy }
3045e3605f65SSreekanth Reddy 
3046e3605f65SSreekanth Reddy /**
3047e3605f65SSreekanth Reddy  * mpimr_initialize_reply_sbuf_queues - initialize reply sense
3048e3605f65SSreekanth Reddy  * buffers
3049e3605f65SSreekanth Reddy  * @mrioc: Adapter instance reference
3050e3605f65SSreekanth Reddy  *
3051e3605f65SSreekanth Reddy  * Helper function to initialize reply and sense buffers along
3052e3605f65SSreekanth Reddy  * with some debug prints.
3053e3605f65SSreekanth Reddy  *
3054e3605f65SSreekanth Reddy  * Return:  None.
3055e3605f65SSreekanth Reddy  */
3056e3605f65SSreekanth Reddy static void mpimr_initialize_reply_sbuf_queues(struct mpi3mr_ioc *mrioc)
3057e3605f65SSreekanth Reddy {
3058e3605f65SSreekanth Reddy 	u32 sz, i;
3059e3605f65SSreekanth Reddy 	dma_addr_t phy_addr;
3060e3605f65SSreekanth Reddy 
3061c5758fc7SSreekanth Reddy 	sz = mrioc->num_reply_bufs * mrioc->reply_sz;
3062824a1566SKashyap Desai 	ioc_info(mrioc,
3063824a1566SKashyap Desai 	    "reply buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
3064c5758fc7SSreekanth Reddy 	    mrioc->reply_buf, mrioc->num_reply_bufs, mrioc->reply_sz,
3065824a1566SKashyap Desai 	    (sz / 1024), (unsigned long long)mrioc->reply_buf_dma);
3066824a1566SKashyap Desai 	sz = mrioc->reply_free_qsz * 8;
3067824a1566SKashyap Desai 	ioc_info(mrioc,
3068824a1566SKashyap Desai 	    "reply_free_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), reply_dma(0x%llx)\n",
3069824a1566SKashyap Desai 	    mrioc->reply_free_q, mrioc->reply_free_qsz, 8, (sz / 1024),
3070824a1566SKashyap Desai 	    (unsigned long long)mrioc->reply_free_q_dma);
3071ec5ebd2cSSreekanth Reddy 	sz = mrioc->num_sense_bufs * MPI3MR_SENSE_BUF_SZ;
3072824a1566SKashyap Desai 	ioc_info(mrioc,
3073824a1566SKashyap Desai 	    "sense_buf pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
3074ec5ebd2cSSreekanth Reddy 	    mrioc->sense_buf, mrioc->num_sense_bufs, MPI3MR_SENSE_BUF_SZ,
3075824a1566SKashyap Desai 	    (sz / 1024), (unsigned long long)mrioc->sense_buf_dma);
3076824a1566SKashyap Desai 	sz = mrioc->sense_buf_q_sz * 8;
3077824a1566SKashyap Desai 	ioc_info(mrioc,
3078824a1566SKashyap Desai 	    "sense_buf_q pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB), sense_dma(0x%llx)\n",
3079824a1566SKashyap Desai 	    mrioc->sense_buf_q, mrioc->sense_buf_q_sz, 8, (sz / 1024),
3080824a1566SKashyap Desai 	    (unsigned long long)mrioc->sense_buf_q_dma);
3081824a1566SKashyap Desai 
3082824a1566SKashyap Desai 	/* initialize Reply buffer Queue */
3083824a1566SKashyap Desai 	for (i = 0, phy_addr = mrioc->reply_buf_dma;
3084c5758fc7SSreekanth Reddy 	    i < mrioc->num_reply_bufs; i++, phy_addr += mrioc->reply_sz)
3085824a1566SKashyap Desai 		mrioc->reply_free_q[i] = cpu_to_le64(phy_addr);
3086824a1566SKashyap Desai 	mrioc->reply_free_q[i] = cpu_to_le64(0);
3087824a1566SKashyap Desai 
3088824a1566SKashyap Desai 	/* initialize Sense Buffer Queue */
3089824a1566SKashyap Desai 	for (i = 0, phy_addr = mrioc->sense_buf_dma;
3090ec5ebd2cSSreekanth Reddy 	    i < mrioc->num_sense_bufs; i++, phy_addr += MPI3MR_SENSE_BUF_SZ)
3091824a1566SKashyap Desai 		mrioc->sense_buf_q[i] = cpu_to_le64(phy_addr);
3092824a1566SKashyap Desai 	mrioc->sense_buf_q[i] = cpu_to_le64(0);
3093824a1566SKashyap Desai }
3094824a1566SKashyap Desai 
3095824a1566SKashyap Desai /**
3096824a1566SKashyap Desai  * mpi3mr_issue_iocinit - Send IOC Init
3097824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3098824a1566SKashyap Desai  *
3099824a1566SKashyap Desai  * Issue IOC Init MPI request through admin queue and wait for
3100824a1566SKashyap Desai  * the completion of it or time out.
3101824a1566SKashyap Desai  *
3102824a1566SKashyap Desai  * Return: 0 on success, non-zero on failures.
3103824a1566SKashyap Desai  */
3104824a1566SKashyap Desai static int mpi3mr_issue_iocinit(struct mpi3mr_ioc *mrioc)
3105824a1566SKashyap Desai {
3106824a1566SKashyap Desai 	struct mpi3_ioc_init_request iocinit_req;
3107824a1566SKashyap Desai 	struct mpi3_driver_info_layout *drv_info;
3108824a1566SKashyap Desai 	dma_addr_t data_dma;
3109824a1566SKashyap Desai 	u32 data_len = sizeof(*drv_info);
3110824a1566SKashyap Desai 	int retval = 0;
3111824a1566SKashyap Desai 	ktime_t current_time;
3112824a1566SKashyap Desai 
3113824a1566SKashyap Desai 	drv_info = dma_alloc_coherent(&mrioc->pdev->dev, data_len, &data_dma,
3114824a1566SKashyap Desai 	    GFP_KERNEL);
3115824a1566SKashyap Desai 	if (!drv_info) {
3116824a1566SKashyap Desai 		retval = -1;
3117824a1566SKashyap Desai 		goto out;
3118824a1566SKashyap Desai 	}
3119e3605f65SSreekanth Reddy 	mpimr_initialize_reply_sbuf_queues(mrioc);
3120e3605f65SSreekanth Reddy 
3121824a1566SKashyap Desai 	drv_info->information_length = cpu_to_le32(data_len);
3122aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_signature, "Broadcom", sizeof(drv_info->driver_signature));
3123aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->os_name, utsname()->sysname, sizeof(drv_info->os_name));
3124aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->os_version, utsname()->release, sizeof(drv_info->os_version));
3125aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_name, MPI3MR_DRIVER_NAME, sizeof(drv_info->driver_name));
3126aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_version, MPI3MR_DRIVER_VERSION, sizeof(drv_info->driver_version));
3127aa0dc6a7SSreekanth Reddy 	strscpy(drv_info->driver_release_date, MPI3MR_DRIVER_RELDATE,
3128aa0dc6a7SSreekanth Reddy 	    sizeof(drv_info->driver_release_date));
3129824a1566SKashyap Desai 	drv_info->driver_capabilities = 0;
3130824a1566SKashyap Desai 	memcpy((u8 *)&mrioc->driver_info, (u8 *)drv_info,
3131824a1566SKashyap Desai 	    sizeof(mrioc->driver_info));
3132824a1566SKashyap Desai 
3133824a1566SKashyap Desai 	memset(&iocinit_req, 0, sizeof(iocinit_req));
3134824a1566SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
3135824a1566SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
3136824a1566SKashyap Desai 		retval = -1;
3137824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCInit: Init command is in use\n");
3138824a1566SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
3139824a1566SKashyap Desai 		goto out;
3140824a1566SKashyap Desai 	}
3141824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
3142824a1566SKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
3143824a1566SKashyap Desai 	mrioc->init_cmds.callback = NULL;
3144824a1566SKashyap Desai 	iocinit_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
3145824a1566SKashyap Desai 	iocinit_req.function = MPI3_FUNCTION_IOC_INIT;
3146824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.dev = MPI3_VERSION_DEV;
3147824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.unit = MPI3_VERSION_UNIT;
3148824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.major = MPI3_VERSION_MAJOR;
3149824a1566SKashyap Desai 	iocinit_req.mpi_version.mpi3_version.minor = MPI3_VERSION_MINOR;
3150824a1566SKashyap Desai 	iocinit_req.who_init = MPI3_WHOINIT_HOST_DRIVER;
3151824a1566SKashyap Desai 	iocinit_req.reply_free_queue_depth = cpu_to_le16(mrioc->reply_free_qsz);
3152824a1566SKashyap Desai 	iocinit_req.reply_free_queue_address =
3153824a1566SKashyap Desai 	    cpu_to_le64(mrioc->reply_free_q_dma);
3154ec5ebd2cSSreekanth Reddy 	iocinit_req.sense_buffer_length = cpu_to_le16(MPI3MR_SENSE_BUF_SZ);
3155824a1566SKashyap Desai 	iocinit_req.sense_buffer_free_queue_depth =
3156824a1566SKashyap Desai 	    cpu_to_le16(mrioc->sense_buf_q_sz);
3157824a1566SKashyap Desai 	iocinit_req.sense_buffer_free_queue_address =
3158824a1566SKashyap Desai 	    cpu_to_le64(mrioc->sense_buf_q_dma);
3159824a1566SKashyap Desai 	iocinit_req.driver_information_address = cpu_to_le64(data_dma);
3160824a1566SKashyap Desai 
3161824a1566SKashyap Desai 	current_time = ktime_get_real();
3162824a1566SKashyap Desai 	iocinit_req.time_stamp = cpu_to_le64(ktime_to_ms(current_time));
3163824a1566SKashyap Desai 
3164824a1566SKashyap Desai 	init_completion(&mrioc->init_cmds.done);
3165824a1566SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &iocinit_req,
3166824a1566SKashyap Desai 	    sizeof(iocinit_req), 1);
3167824a1566SKashyap Desai 	if (retval) {
3168824a1566SKashyap Desai 		ioc_err(mrioc, "Issue IOCInit: Admin Post failed\n");
3169824a1566SKashyap Desai 		goto out_unlock;
3170824a1566SKashyap Desai 	}
3171824a1566SKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
3172824a1566SKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
3173824a1566SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3174a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
3175824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_IOCINIT_TIMEOUT);
3176a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "ioc_init timed out\n");
3177824a1566SKashyap Desai 		retval = -1;
3178824a1566SKashyap Desai 		goto out_unlock;
3179824a1566SKashyap Desai 	}
3180824a1566SKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
3181824a1566SKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
3182824a1566SKashyap Desai 		ioc_err(mrioc,
3183824a1566SKashyap Desai 		    "Issue IOCInit: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
3184824a1566SKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
3185824a1566SKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
3186824a1566SKashyap Desai 		retval = -1;
3187824a1566SKashyap Desai 		goto out_unlock;
3188824a1566SKashyap Desai 	}
3189824a1566SKashyap Desai 
3190e3605f65SSreekanth Reddy 	mrioc->reply_free_queue_host_index = mrioc->num_reply_bufs;
3191e3605f65SSreekanth Reddy 	writel(mrioc->reply_free_queue_host_index,
3192e3605f65SSreekanth Reddy 	    &mrioc->sysif_regs->reply_free_host_index);
3193e3605f65SSreekanth Reddy 
3194e3605f65SSreekanth Reddy 	mrioc->sbq_host_index = mrioc->num_sense_bufs;
3195e3605f65SSreekanth Reddy 	writel(mrioc->sbq_host_index,
3196e3605f65SSreekanth Reddy 	    &mrioc->sysif_regs->sense_buffer_free_host_index);
3197824a1566SKashyap Desai out_unlock:
3198824a1566SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3199824a1566SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
3200824a1566SKashyap Desai 
3201824a1566SKashyap Desai out:
3202824a1566SKashyap Desai 	if (drv_info)
3203824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, data_len, drv_info,
3204824a1566SKashyap Desai 		    data_dma);
3205824a1566SKashyap Desai 
3206824a1566SKashyap Desai 	return retval;
3207824a1566SKashyap Desai }
3208824a1566SKashyap Desai 
3209824a1566SKashyap Desai /**
321013ef29eaSKashyap Desai  * mpi3mr_unmask_events - Unmask events in event mask bitmap
321113ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
321213ef29eaSKashyap Desai  * @event: MPI event ID
321313ef29eaSKashyap Desai  *
321413ef29eaSKashyap Desai  * Un mask the specific event by resetting the event_mask
321513ef29eaSKashyap Desai  * bitmap.
321613ef29eaSKashyap Desai  *
321713ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
321813ef29eaSKashyap Desai  */
321913ef29eaSKashyap Desai static void mpi3mr_unmask_events(struct mpi3mr_ioc *mrioc, u16 event)
322013ef29eaSKashyap Desai {
322113ef29eaSKashyap Desai 	u32 desired_event;
322213ef29eaSKashyap Desai 	u8 word;
322313ef29eaSKashyap Desai 
322413ef29eaSKashyap Desai 	if (event >= 128)
322513ef29eaSKashyap Desai 		return;
322613ef29eaSKashyap Desai 
322713ef29eaSKashyap Desai 	desired_event = (1 << (event % 32));
322813ef29eaSKashyap Desai 	word = event / 32;
322913ef29eaSKashyap Desai 
323013ef29eaSKashyap Desai 	mrioc->event_masks[word] &= ~desired_event;
323113ef29eaSKashyap Desai }
323213ef29eaSKashyap Desai 
323313ef29eaSKashyap Desai /**
323413ef29eaSKashyap Desai  * mpi3mr_issue_event_notification - Send event notification
323513ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
323613ef29eaSKashyap Desai  *
323713ef29eaSKashyap Desai  * Issue event notification MPI request through admin queue and
323813ef29eaSKashyap Desai  * wait for the completion of it or time out.
323913ef29eaSKashyap Desai  *
324013ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
324113ef29eaSKashyap Desai  */
324213ef29eaSKashyap Desai static int mpi3mr_issue_event_notification(struct mpi3mr_ioc *mrioc)
324313ef29eaSKashyap Desai {
324413ef29eaSKashyap Desai 	struct mpi3_event_notification_request evtnotify_req;
324513ef29eaSKashyap Desai 	int retval = 0;
324613ef29eaSKashyap Desai 	u8 i;
324713ef29eaSKashyap Desai 
324813ef29eaSKashyap Desai 	memset(&evtnotify_req, 0, sizeof(evtnotify_req));
324913ef29eaSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
325013ef29eaSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
325113ef29eaSKashyap Desai 		retval = -1;
325213ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: Init command is in use\n");
325313ef29eaSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
325413ef29eaSKashyap Desai 		goto out;
325513ef29eaSKashyap Desai 	}
325613ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
325713ef29eaSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
325813ef29eaSKashyap Desai 	mrioc->init_cmds.callback = NULL;
325913ef29eaSKashyap Desai 	evtnotify_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
326013ef29eaSKashyap Desai 	evtnotify_req.function = MPI3_FUNCTION_EVENT_NOTIFICATION;
326113ef29eaSKashyap Desai 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
326213ef29eaSKashyap Desai 		evtnotify_req.event_masks[i] =
326313ef29eaSKashyap Desai 		    cpu_to_le32(mrioc->event_masks[i]);
326413ef29eaSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
326513ef29eaSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &evtnotify_req,
326613ef29eaSKashyap Desai 	    sizeof(evtnotify_req), 1);
326713ef29eaSKashyap Desai 	if (retval) {
326813ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: Admin Post failed\n");
326913ef29eaSKashyap Desai 		goto out_unlock;
327013ef29eaSKashyap Desai 	}
327113ef29eaSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
327213ef29eaSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
327313ef29eaSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3274a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "event notification timed out\n");
3275a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
327613ef29eaSKashyap Desai 		    MPI3MR_RESET_FROM_EVTNOTIFY_TIMEOUT);
327713ef29eaSKashyap Desai 		retval = -1;
327813ef29eaSKashyap Desai 		goto out_unlock;
327913ef29eaSKashyap Desai 	}
328013ef29eaSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
328113ef29eaSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
328213ef29eaSKashyap Desai 		ioc_err(mrioc,
328313ef29eaSKashyap Desai 		    "Issue EvtNotify: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
328413ef29eaSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
328513ef29eaSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
328613ef29eaSKashyap Desai 		retval = -1;
328713ef29eaSKashyap Desai 		goto out_unlock;
328813ef29eaSKashyap Desai 	}
328913ef29eaSKashyap Desai 
329013ef29eaSKashyap Desai out_unlock:
329113ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
329213ef29eaSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
329313ef29eaSKashyap Desai out:
329413ef29eaSKashyap Desai 	return retval;
329513ef29eaSKashyap Desai }
329613ef29eaSKashyap Desai 
329713ef29eaSKashyap Desai /**
3298c1af985dSSreekanth Reddy  * mpi3mr_process_event_ack - Process event acknowledgment
329913ef29eaSKashyap Desai  * @mrioc: Adapter instance reference
330013ef29eaSKashyap Desai  * @event: MPI3 event ID
3301c1af985dSSreekanth Reddy  * @event_ctx: event context
330213ef29eaSKashyap Desai  *
330313ef29eaSKashyap Desai  * Send event acknowledgment through admin queue and wait for
330413ef29eaSKashyap Desai  * it to complete.
330513ef29eaSKashyap Desai  *
330613ef29eaSKashyap Desai  * Return: 0 on success, non-zero on failures.
330713ef29eaSKashyap Desai  */
3308c1af985dSSreekanth Reddy int mpi3mr_process_event_ack(struct mpi3mr_ioc *mrioc, u8 event,
330913ef29eaSKashyap Desai 	u32 event_ctx)
331013ef29eaSKashyap Desai {
331113ef29eaSKashyap Desai 	struct mpi3_event_ack_request evtack_req;
331213ef29eaSKashyap Desai 	int retval = 0;
331313ef29eaSKashyap Desai 
331413ef29eaSKashyap Desai 	memset(&evtack_req, 0, sizeof(evtack_req));
331513ef29eaSKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
331613ef29eaSKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
331713ef29eaSKashyap Desai 		retval = -1;
331813ef29eaSKashyap Desai 		ioc_err(mrioc, "Send EvtAck: Init command is in use\n");
331913ef29eaSKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
332013ef29eaSKashyap Desai 		goto out;
332113ef29eaSKashyap Desai 	}
332213ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
332313ef29eaSKashyap Desai 	mrioc->init_cmds.is_waiting = 1;
332413ef29eaSKashyap Desai 	mrioc->init_cmds.callback = NULL;
332513ef29eaSKashyap Desai 	evtack_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
332613ef29eaSKashyap Desai 	evtack_req.function = MPI3_FUNCTION_EVENT_ACK;
332713ef29eaSKashyap Desai 	evtack_req.event = event;
332813ef29eaSKashyap Desai 	evtack_req.event_context = cpu_to_le32(event_ctx);
332913ef29eaSKashyap Desai 
333013ef29eaSKashyap Desai 	init_completion(&mrioc->init_cmds.done);
333113ef29eaSKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &evtack_req,
333213ef29eaSKashyap Desai 	    sizeof(evtack_req), 1);
333313ef29eaSKashyap Desai 	if (retval) {
333413ef29eaSKashyap Desai 		ioc_err(mrioc, "Send EvtAck: Admin Post failed\n");
333513ef29eaSKashyap Desai 		goto out_unlock;
333613ef29eaSKashyap Desai 	}
333713ef29eaSKashyap Desai 	wait_for_completion_timeout(&mrioc->init_cmds.done,
333813ef29eaSKashyap Desai 	    (MPI3MR_INTADMCMD_TIMEOUT * HZ));
333913ef29eaSKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
334013ef29eaSKashyap Desai 		ioc_err(mrioc, "Issue EvtNotify: command timed out\n");
3341fbaa9aa4SSreekanth Reddy 		if (!(mrioc->init_cmds.state & MPI3MR_CMD_RESET))
334213ef29eaSKashyap Desai 			mpi3mr_soft_reset_handler(mrioc,
334313ef29eaSKashyap Desai 			    MPI3MR_RESET_FROM_EVTACK_TIMEOUT, 1);
334413ef29eaSKashyap Desai 		retval = -1;
334513ef29eaSKashyap Desai 		goto out_unlock;
334613ef29eaSKashyap Desai 	}
334713ef29eaSKashyap Desai 	if ((mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK)
334813ef29eaSKashyap Desai 	    != MPI3_IOCSTATUS_SUCCESS) {
334913ef29eaSKashyap Desai 		ioc_err(mrioc,
335013ef29eaSKashyap Desai 		    "Send EvtAck: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
335113ef29eaSKashyap Desai 		    (mrioc->init_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK),
335213ef29eaSKashyap Desai 		    mrioc->init_cmds.ioc_loginfo);
335313ef29eaSKashyap Desai 		retval = -1;
335413ef29eaSKashyap Desai 		goto out_unlock;
335513ef29eaSKashyap Desai 	}
335613ef29eaSKashyap Desai 
335713ef29eaSKashyap Desai out_unlock:
335813ef29eaSKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
335913ef29eaSKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
336013ef29eaSKashyap Desai out:
336113ef29eaSKashyap Desai 	return retval;
336213ef29eaSKashyap Desai }
336313ef29eaSKashyap Desai 
336413ef29eaSKashyap Desai /**
3365824a1566SKashyap Desai  * mpi3mr_alloc_chain_bufs - Allocate chain buffers
3366824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3367824a1566SKashyap Desai  *
3368824a1566SKashyap Desai  * Allocate chain buffers and set a bitmap to indicate free
3369824a1566SKashyap Desai  * chain buffers. Chain buffers are used to pass the SGE
3370824a1566SKashyap Desai  * information along with MPI3 SCSI IO requests for host I/O.
3371824a1566SKashyap Desai  *
3372824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure
3373824a1566SKashyap Desai  */
3374824a1566SKashyap Desai static int mpi3mr_alloc_chain_bufs(struct mpi3mr_ioc *mrioc)
3375824a1566SKashyap Desai {
3376824a1566SKashyap Desai 	int retval = 0;
3377824a1566SKashyap Desai 	u32 sz, i;
3378824a1566SKashyap Desai 	u16 num_chains;
3379824a1566SKashyap Desai 
3380fe6db615SSreekanth Reddy 	if (mrioc->chain_sgl_list)
3381fe6db615SSreekanth Reddy 		return retval;
3382fe6db615SSreekanth Reddy 
3383824a1566SKashyap Desai 	num_chains = mrioc->max_host_ios / MPI3MR_CHAINBUF_FACTOR;
3384824a1566SKashyap Desai 
338574e1f30aSKashyap Desai 	if (prot_mask & (SHOST_DIX_TYPE0_PROTECTION
338674e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE1_PROTECTION
338774e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE2_PROTECTION
338874e1f30aSKashyap Desai 	    | SHOST_DIX_TYPE3_PROTECTION))
338974e1f30aSKashyap Desai 		num_chains += (num_chains / MPI3MR_CHAINBUFDIX_FACTOR);
339074e1f30aSKashyap Desai 
3391824a1566SKashyap Desai 	mrioc->chain_buf_count = num_chains;
3392824a1566SKashyap Desai 	sz = sizeof(struct chain_element) * num_chains;
3393824a1566SKashyap Desai 	mrioc->chain_sgl_list = kzalloc(sz, GFP_KERNEL);
3394824a1566SKashyap Desai 	if (!mrioc->chain_sgl_list)
3395824a1566SKashyap Desai 		goto out_failed;
3396824a1566SKashyap Desai 
3397824a1566SKashyap Desai 	sz = MPI3MR_PAGE_SIZE_4K;
3398824a1566SKashyap Desai 	mrioc->chain_buf_pool = dma_pool_create("chain_buf pool",
3399824a1566SKashyap Desai 	    &mrioc->pdev->dev, sz, 16, 0);
3400824a1566SKashyap Desai 	if (!mrioc->chain_buf_pool) {
3401824a1566SKashyap Desai 		ioc_err(mrioc, "chain buf pool: dma_pool_create failed\n");
3402824a1566SKashyap Desai 		goto out_failed;
3403824a1566SKashyap Desai 	}
3404824a1566SKashyap Desai 
3405824a1566SKashyap Desai 	for (i = 0; i < num_chains; i++) {
3406824a1566SKashyap Desai 		mrioc->chain_sgl_list[i].addr =
3407824a1566SKashyap Desai 		    dma_pool_zalloc(mrioc->chain_buf_pool, GFP_KERNEL,
3408824a1566SKashyap Desai 		    &mrioc->chain_sgl_list[i].dma_addr);
3409824a1566SKashyap Desai 
3410824a1566SKashyap Desai 		if (!mrioc->chain_sgl_list[i].addr)
3411824a1566SKashyap Desai 			goto out_failed;
3412824a1566SKashyap Desai 	}
3413339e6156SShin'ichiro Kawasaki 	mrioc->chain_bitmap = bitmap_zalloc(num_chains, GFP_KERNEL);
3414824a1566SKashyap Desai 	if (!mrioc->chain_bitmap)
3415824a1566SKashyap Desai 		goto out_failed;
3416824a1566SKashyap Desai 	return retval;
3417824a1566SKashyap Desai out_failed:
3418824a1566SKashyap Desai 	retval = -1;
3419824a1566SKashyap Desai 	return retval;
3420824a1566SKashyap Desai }
3421824a1566SKashyap Desai 
3422824a1566SKashyap Desai /**
3423023ab2a9SKashyap Desai  * mpi3mr_port_enable_complete - Mark port enable complete
3424023ab2a9SKashyap Desai  * @mrioc: Adapter instance reference
3425023ab2a9SKashyap Desai  * @drv_cmd: Internal command tracker
3426023ab2a9SKashyap Desai  *
3427023ab2a9SKashyap Desai  * Call back for asynchronous port enable request sets the
3428023ab2a9SKashyap Desai  * driver command to indicate port enable request is complete.
3429023ab2a9SKashyap Desai  *
3430023ab2a9SKashyap Desai  * Return: Nothing
3431023ab2a9SKashyap Desai  */
3432023ab2a9SKashyap Desai static void mpi3mr_port_enable_complete(struct mpi3mr_ioc *mrioc,
3433023ab2a9SKashyap Desai 	struct mpi3mr_drv_cmd *drv_cmd)
3434023ab2a9SKashyap Desai {
3435023ab2a9SKashyap Desai 	drv_cmd->callback = NULL;
3436023ab2a9SKashyap Desai 	mrioc->scan_started = 0;
3437f2a79d20SSreekanth Reddy 	if (drv_cmd->state & MPI3MR_CMD_RESET)
3438f2a79d20SSreekanth Reddy 		mrioc->scan_failed = MPI3_IOCSTATUS_INTERNAL_ERROR;
3439f2a79d20SSreekanth Reddy 	else
3440f2a79d20SSreekanth Reddy 		mrioc->scan_failed = drv_cmd->ioc_status;
3441f2a79d20SSreekanth Reddy 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
3442023ab2a9SKashyap Desai }
3443023ab2a9SKashyap Desai 
3444023ab2a9SKashyap Desai /**
3445023ab2a9SKashyap Desai  * mpi3mr_issue_port_enable - Issue Port Enable
3446023ab2a9SKashyap Desai  * @mrioc: Adapter instance reference
3447023ab2a9SKashyap Desai  * @async: Flag to wait for completion or not
3448023ab2a9SKashyap Desai  *
3449023ab2a9SKashyap Desai  * Issue Port Enable MPI request through admin queue and if the
3450023ab2a9SKashyap Desai  * async flag is not set wait for the completion of the port
3451023ab2a9SKashyap Desai  * enable or time out.
3452023ab2a9SKashyap Desai  *
3453023ab2a9SKashyap Desai  * Return: 0 on success, non-zero on failures.
3454023ab2a9SKashyap Desai  */
3455023ab2a9SKashyap Desai int mpi3mr_issue_port_enable(struct mpi3mr_ioc *mrioc, u8 async)
3456023ab2a9SKashyap Desai {
3457023ab2a9SKashyap Desai 	struct mpi3_port_enable_request pe_req;
3458023ab2a9SKashyap Desai 	int retval = 0;
3459023ab2a9SKashyap Desai 	u32 pe_timeout = MPI3MR_PORTENABLE_TIMEOUT;
3460023ab2a9SKashyap Desai 
3461023ab2a9SKashyap Desai 	memset(&pe_req, 0, sizeof(pe_req));
3462023ab2a9SKashyap Desai 	mutex_lock(&mrioc->init_cmds.mutex);
3463023ab2a9SKashyap Desai 	if (mrioc->init_cmds.state & MPI3MR_CMD_PENDING) {
3464023ab2a9SKashyap Desai 		retval = -1;
3465023ab2a9SKashyap Desai 		ioc_err(mrioc, "Issue PortEnable: Init command is in use\n");
3466023ab2a9SKashyap Desai 		mutex_unlock(&mrioc->init_cmds.mutex);
3467023ab2a9SKashyap Desai 		goto out;
3468023ab2a9SKashyap Desai 	}
3469023ab2a9SKashyap Desai 	mrioc->init_cmds.state = MPI3MR_CMD_PENDING;
3470023ab2a9SKashyap Desai 	if (async) {
3471023ab2a9SKashyap Desai 		mrioc->init_cmds.is_waiting = 0;
3472023ab2a9SKashyap Desai 		mrioc->init_cmds.callback = mpi3mr_port_enable_complete;
3473023ab2a9SKashyap Desai 	} else {
3474023ab2a9SKashyap Desai 		mrioc->init_cmds.is_waiting = 1;
3475023ab2a9SKashyap Desai 		mrioc->init_cmds.callback = NULL;
3476023ab2a9SKashyap Desai 		init_completion(&mrioc->init_cmds.done);
3477023ab2a9SKashyap Desai 	}
3478023ab2a9SKashyap Desai 	pe_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_INITCMDS);
3479023ab2a9SKashyap Desai 	pe_req.function = MPI3_FUNCTION_PORT_ENABLE;
3480023ab2a9SKashyap Desai 
3481023ab2a9SKashyap Desai 	retval = mpi3mr_admin_request_post(mrioc, &pe_req, sizeof(pe_req), 1);
3482023ab2a9SKashyap Desai 	if (retval) {
3483023ab2a9SKashyap Desai 		ioc_err(mrioc, "Issue PortEnable: Admin Post failed\n");
3484023ab2a9SKashyap Desai 		goto out_unlock;
3485023ab2a9SKashyap Desai 	}
3486a6856cc4SSreekanth Reddy 	if (async) {
3487a6856cc4SSreekanth Reddy 		mutex_unlock(&mrioc->init_cmds.mutex);
3488a6856cc4SSreekanth Reddy 		goto out;
3489a6856cc4SSreekanth Reddy 	}
3490a6856cc4SSreekanth Reddy 
3491a6856cc4SSreekanth Reddy 	wait_for_completion_timeout(&mrioc->init_cmds.done, (pe_timeout * HZ));
3492023ab2a9SKashyap Desai 	if (!(mrioc->init_cmds.state & MPI3MR_CMD_COMPLETE)) {
3493a6856cc4SSreekanth Reddy 		ioc_err(mrioc, "port enable timed out\n");
3494023ab2a9SKashyap Desai 		retval = -1;
3495a6856cc4SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc, MPI3MR_RESET_FROM_PE_TIMEOUT);
3496023ab2a9SKashyap Desai 		goto out_unlock;
3497023ab2a9SKashyap Desai 	}
3498023ab2a9SKashyap Desai 	mpi3mr_port_enable_complete(mrioc, &mrioc->init_cmds);
3499a6856cc4SSreekanth Reddy 
3500023ab2a9SKashyap Desai out_unlock:
3501a6856cc4SSreekanth Reddy 	mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
3502023ab2a9SKashyap Desai 	mutex_unlock(&mrioc->init_cmds.mutex);
3503023ab2a9SKashyap Desai out:
3504023ab2a9SKashyap Desai 	return retval;
3505023ab2a9SKashyap Desai }
3506023ab2a9SKashyap Desai 
3507ff9561e9SKashyap Desai /* Protocol type to name mapper structure */
3508ff9561e9SKashyap Desai static const struct {
3509ff9561e9SKashyap Desai 	u8 protocol;
3510ff9561e9SKashyap Desai 	char *name;
3511ff9561e9SKashyap Desai } mpi3mr_protocols[] = {
3512ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_SCSI_INITIATOR, "Initiator" },
3513ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_SCSI_TARGET, "Target" },
3514ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_PROTOCOL_NVME, "NVMe attachment" },
3515ff9561e9SKashyap Desai };
3516ff9561e9SKashyap Desai 
3517ff9561e9SKashyap Desai /* Capability to name mapper structure*/
3518ff9561e9SKashyap Desai static const struct {
3519ff9561e9SKashyap Desai 	u32 capability;
3520ff9561e9SKashyap Desai 	char *name;
3521ff9561e9SKashyap Desai } mpi3mr_capabilities[] = {
3522ff9561e9SKashyap Desai 	{ MPI3_IOCFACTS_CAPABILITY_RAID_CAPABLE, "RAID" },
3523c4723e68SSreekanth Reddy 	{ MPI3_IOCFACTS_CAPABILITY_MULTIPATH_ENABLED, "MultiPath" },
3524ff9561e9SKashyap Desai };
3525ff9561e9SKashyap Desai 
3526ff9561e9SKashyap Desai /**
3527ff9561e9SKashyap Desai  * mpi3mr_print_ioc_info - Display controller information
3528ff9561e9SKashyap Desai  * @mrioc: Adapter instance reference
3529ff9561e9SKashyap Desai  *
3530ff9561e9SKashyap Desai  * Display controller personalit, capability, supported
3531ff9561e9SKashyap Desai  * protocols etc.
3532ff9561e9SKashyap Desai  *
3533ff9561e9SKashyap Desai  * Return: Nothing
3534ff9561e9SKashyap Desai  */
3535ff9561e9SKashyap Desai static void
3536ff9561e9SKashyap Desai mpi3mr_print_ioc_info(struct mpi3mr_ioc *mrioc)
3537ff9561e9SKashyap Desai {
353876a4f7ccSDan Carpenter 	int i = 0, bytes_written = 0;
3539ff9561e9SKashyap Desai 	char personality[16];
3540ff9561e9SKashyap Desai 	char protocol[50] = {0};
3541ff9561e9SKashyap Desai 	char capabilities[100] = {0};
3542ff9561e9SKashyap Desai 	struct mpi3mr_compimg_ver *fwver = &mrioc->facts.fw_ver;
3543ff9561e9SKashyap Desai 
3544ff9561e9SKashyap Desai 	switch (mrioc->facts.personality) {
3545ff9561e9SKashyap Desai 	case MPI3_IOCFACTS_FLAGS_PERSONALITY_EHBA:
3546ff9561e9SKashyap Desai 		strncpy(personality, "Enhanced HBA", sizeof(personality));
3547ff9561e9SKashyap Desai 		break;
3548ff9561e9SKashyap Desai 	case MPI3_IOCFACTS_FLAGS_PERSONALITY_RAID_DDR:
3549ff9561e9SKashyap Desai 		strncpy(personality, "RAID", sizeof(personality));
3550ff9561e9SKashyap Desai 		break;
3551ff9561e9SKashyap Desai 	default:
3552ff9561e9SKashyap Desai 		strncpy(personality, "Unknown", sizeof(personality));
3553ff9561e9SKashyap Desai 		break;
3554ff9561e9SKashyap Desai 	}
3555ff9561e9SKashyap Desai 
3556ff9561e9SKashyap Desai 	ioc_info(mrioc, "Running in %s Personality", personality);
3557ff9561e9SKashyap Desai 
3558ff9561e9SKashyap Desai 	ioc_info(mrioc, "FW version(%d.%d.%d.%d.%d.%d)\n",
3559ff9561e9SKashyap Desai 	    fwver->gen_major, fwver->gen_minor, fwver->ph_major,
3560ff9561e9SKashyap Desai 	    fwver->ph_minor, fwver->cust_id, fwver->build_num);
3561ff9561e9SKashyap Desai 
3562ff9561e9SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_protocols); i++) {
3563ff9561e9SKashyap Desai 		if (mrioc->facts.protocol_flags &
3564ff9561e9SKashyap Desai 		    mpi3mr_protocols[i].protocol) {
356530e99f05SDan Carpenter 			bytes_written += scnprintf(protocol + bytes_written,
356676a4f7ccSDan Carpenter 				    sizeof(protocol) - bytes_written, "%s%s",
356776a4f7ccSDan Carpenter 				    bytes_written ? "," : "",
3568ff9561e9SKashyap Desai 				    mpi3mr_protocols[i].name);
3569ff9561e9SKashyap Desai 		}
3570ff9561e9SKashyap Desai 	}
3571ff9561e9SKashyap Desai 
357276a4f7ccSDan Carpenter 	bytes_written = 0;
3573ff9561e9SKashyap Desai 	for (i = 0; i < ARRAY_SIZE(mpi3mr_capabilities); i++) {
3574ff9561e9SKashyap Desai 		if (mrioc->facts.protocol_flags &
3575ff9561e9SKashyap Desai 		    mpi3mr_capabilities[i].capability) {
357630e99f05SDan Carpenter 			bytes_written += scnprintf(capabilities + bytes_written,
357776a4f7ccSDan Carpenter 				    sizeof(capabilities) - bytes_written, "%s%s",
357876a4f7ccSDan Carpenter 				    bytes_written ? "," : "",
3579ff9561e9SKashyap Desai 				    mpi3mr_capabilities[i].name);
3580ff9561e9SKashyap Desai 		}
3581ff9561e9SKashyap Desai 	}
3582ff9561e9SKashyap Desai 
3583ff9561e9SKashyap Desai 	ioc_info(mrioc, "Protocol=(%s), Capabilities=(%s)\n",
3584ff9561e9SKashyap Desai 		 protocol, capabilities);
3585ff9561e9SKashyap Desai }
3586ff9561e9SKashyap Desai 
3587023ab2a9SKashyap Desai /**
3588824a1566SKashyap Desai  * mpi3mr_cleanup_resources - Free PCI resources
3589824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3590824a1566SKashyap Desai  *
3591824a1566SKashyap Desai  * Unmap PCI device memory and disable PCI device.
3592824a1566SKashyap Desai  *
3593824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3594824a1566SKashyap Desai  */
3595824a1566SKashyap Desai void mpi3mr_cleanup_resources(struct mpi3mr_ioc *mrioc)
3596824a1566SKashyap Desai {
3597824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
3598824a1566SKashyap Desai 
3599824a1566SKashyap Desai 	mpi3mr_cleanup_isr(mrioc);
3600824a1566SKashyap Desai 
3601824a1566SKashyap Desai 	if (mrioc->sysif_regs) {
3602824a1566SKashyap Desai 		iounmap((void __iomem *)mrioc->sysif_regs);
3603824a1566SKashyap Desai 		mrioc->sysif_regs = NULL;
3604824a1566SKashyap Desai 	}
3605824a1566SKashyap Desai 
3606824a1566SKashyap Desai 	if (pci_is_enabled(pdev)) {
3607824a1566SKashyap Desai 		if (mrioc->bars)
3608824a1566SKashyap Desai 			pci_release_selected_regions(pdev, mrioc->bars);
3609824a1566SKashyap Desai 		pci_disable_device(pdev);
3610824a1566SKashyap Desai 	}
3611824a1566SKashyap Desai }
3612824a1566SKashyap Desai 
3613824a1566SKashyap Desai /**
3614824a1566SKashyap Desai  * mpi3mr_setup_resources - Enable PCI resources
3615824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3616824a1566SKashyap Desai  *
3617824a1566SKashyap Desai  * Enable PCI device memory, MSI-x registers and set DMA mask.
3618824a1566SKashyap Desai  *
3619824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3620824a1566SKashyap Desai  */
3621824a1566SKashyap Desai int mpi3mr_setup_resources(struct mpi3mr_ioc *mrioc)
3622824a1566SKashyap Desai {
3623824a1566SKashyap Desai 	struct pci_dev *pdev = mrioc->pdev;
3624824a1566SKashyap Desai 	u32 memap_sz = 0;
3625824a1566SKashyap Desai 	int i, retval = 0, capb = 0;
3626824a1566SKashyap Desai 	u16 message_control;
3627824a1566SKashyap Desai 	u64 dma_mask = mrioc->dma_mask ? mrioc->dma_mask :
3628d347a951SSreekanth Reddy 	    ((sizeof(dma_addr_t) > 4) ? DMA_BIT_MASK(64) : DMA_BIT_MASK(32));
3629824a1566SKashyap Desai 
3630824a1566SKashyap Desai 	if (pci_enable_device_mem(pdev)) {
3631824a1566SKashyap Desai 		ioc_err(mrioc, "pci_enable_device_mem: failed\n");
3632824a1566SKashyap Desai 		retval = -ENODEV;
3633824a1566SKashyap Desai 		goto out_failed;
3634824a1566SKashyap Desai 	}
3635824a1566SKashyap Desai 
3636824a1566SKashyap Desai 	capb = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3637824a1566SKashyap Desai 	if (!capb) {
3638824a1566SKashyap Desai 		ioc_err(mrioc, "Unable to find MSI-X Capabilities\n");
3639824a1566SKashyap Desai 		retval = -ENODEV;
3640824a1566SKashyap Desai 		goto out_failed;
3641824a1566SKashyap Desai 	}
3642824a1566SKashyap Desai 	mrioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
3643824a1566SKashyap Desai 
3644824a1566SKashyap Desai 	if (pci_request_selected_regions(pdev, mrioc->bars,
3645824a1566SKashyap Desai 	    mrioc->driver_name)) {
3646824a1566SKashyap Desai 		ioc_err(mrioc, "pci_request_selected_regions: failed\n");
3647824a1566SKashyap Desai 		retval = -ENODEV;
3648824a1566SKashyap Desai 		goto out_failed;
3649824a1566SKashyap Desai 	}
3650824a1566SKashyap Desai 
3651824a1566SKashyap Desai 	for (i = 0; (i < DEVICE_COUNT_RESOURCE); i++) {
3652824a1566SKashyap Desai 		if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
3653824a1566SKashyap Desai 			mrioc->sysif_regs_phys = pci_resource_start(pdev, i);
3654824a1566SKashyap Desai 			memap_sz = pci_resource_len(pdev, i);
3655824a1566SKashyap Desai 			mrioc->sysif_regs =
3656824a1566SKashyap Desai 			    ioremap(mrioc->sysif_regs_phys, memap_sz);
3657824a1566SKashyap Desai 			break;
3658824a1566SKashyap Desai 		}
3659824a1566SKashyap Desai 	}
3660824a1566SKashyap Desai 
3661824a1566SKashyap Desai 	pci_set_master(pdev);
3662824a1566SKashyap Desai 
3663824a1566SKashyap Desai 	retval = dma_set_mask_and_coherent(&pdev->dev, dma_mask);
3664824a1566SKashyap Desai 	if (retval) {
3665824a1566SKashyap Desai 		if (dma_mask != DMA_BIT_MASK(32)) {
3666824a1566SKashyap Desai 			ioc_warn(mrioc, "Setting 64 bit DMA mask failed\n");
3667824a1566SKashyap Desai 			dma_mask = DMA_BIT_MASK(32);
3668824a1566SKashyap Desai 			retval = dma_set_mask_and_coherent(&pdev->dev,
3669824a1566SKashyap Desai 			    dma_mask);
3670824a1566SKashyap Desai 		}
3671824a1566SKashyap Desai 		if (retval) {
3672824a1566SKashyap Desai 			mrioc->dma_mask = 0;
3673824a1566SKashyap Desai 			ioc_err(mrioc, "Setting 32 bit DMA mask also failed\n");
3674824a1566SKashyap Desai 			goto out_failed;
3675824a1566SKashyap Desai 		}
3676824a1566SKashyap Desai 	}
3677824a1566SKashyap Desai 	mrioc->dma_mask = dma_mask;
3678824a1566SKashyap Desai 
3679824a1566SKashyap Desai 	if (!mrioc->sysif_regs) {
3680824a1566SKashyap Desai 		ioc_err(mrioc,
3681824a1566SKashyap Desai 		    "Unable to map adapter memory or resource not found\n");
3682824a1566SKashyap Desai 		retval = -EINVAL;
3683824a1566SKashyap Desai 		goto out_failed;
3684824a1566SKashyap Desai 	}
3685824a1566SKashyap Desai 
3686824a1566SKashyap Desai 	pci_read_config_word(pdev, capb + 2, &message_control);
3687824a1566SKashyap Desai 	mrioc->msix_count = (message_control & 0x3FF) + 1;
3688824a1566SKashyap Desai 
3689824a1566SKashyap Desai 	pci_save_state(pdev);
3690824a1566SKashyap Desai 
3691824a1566SKashyap Desai 	pci_set_drvdata(pdev, mrioc->shost);
3692824a1566SKashyap Desai 
3693824a1566SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
3694824a1566SKashyap Desai 
3695824a1566SKashyap Desai 	ioc_info(mrioc, "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
3696824a1566SKashyap Desai 	    (unsigned long long)mrioc->sysif_regs_phys,
3697824a1566SKashyap Desai 	    mrioc->sysif_regs, memap_sz);
3698824a1566SKashyap Desai 	ioc_info(mrioc, "Number of MSI-X vectors found in capabilities: (%d)\n",
3699824a1566SKashyap Desai 	    mrioc->msix_count);
3700afd3a579SSreekanth Reddy 
3701afd3a579SSreekanth Reddy 	if (!reset_devices && poll_queues > 0)
3702afd3a579SSreekanth Reddy 		mrioc->requested_poll_qcount = min_t(int, poll_queues,
3703afd3a579SSreekanth Reddy 				mrioc->msix_count - 2);
3704824a1566SKashyap Desai 	return retval;
3705824a1566SKashyap Desai 
3706824a1566SKashyap Desai out_failed:
3707824a1566SKashyap Desai 	mpi3mr_cleanup_resources(mrioc);
3708824a1566SKashyap Desai 	return retval;
3709824a1566SKashyap Desai }
3710824a1566SKashyap Desai 
3711824a1566SKashyap Desai /**
3712e3605f65SSreekanth Reddy  * mpi3mr_enable_events - Enable required events
3713e3605f65SSreekanth Reddy  * @mrioc: Adapter instance reference
3714e3605f65SSreekanth Reddy  *
3715e3605f65SSreekanth Reddy  * This routine unmasks the events required by the driver by
3716e3605f65SSreekanth Reddy  * sennding appropriate event mask bitmapt through an event
3717e3605f65SSreekanth Reddy  * notification request.
3718e3605f65SSreekanth Reddy  *
3719e3605f65SSreekanth Reddy  * Return: 0 on success and non-zero on failure.
3720e3605f65SSreekanth Reddy  */
3721e3605f65SSreekanth Reddy static int mpi3mr_enable_events(struct mpi3mr_ioc *mrioc)
3722e3605f65SSreekanth Reddy {
3723e3605f65SSreekanth Reddy 	int retval = 0;
3724e3605f65SSreekanth Reddy 	u32  i;
3725e3605f65SSreekanth Reddy 
3726e3605f65SSreekanth Reddy 	for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3727e3605f65SSreekanth Reddy 		mrioc->event_masks[i] = -1;
3728e3605f65SSreekanth Reddy 
3729e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_ADDED);
3730e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_INFO_CHANGED);
3731e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_DEVICE_STATUS_CHANGE);
3732e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENCL_DEVICE_STATUS_CHANGE);
37337188c03fSSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENCL_DEVICE_ADDED);
3734e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
3735e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DISCOVERY);
3736e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_DEVICE_DISCOVERY_ERROR);
3737e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_SAS_BROADCAST_PRIMITIVE);
3738e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
3739e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PCIE_ENUMERATION);
374078b76a07SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_PREPARE_FOR_RESET);
3741e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_CABLE_MGMT);
3742e3605f65SSreekanth Reddy 	mpi3mr_unmask_events(mrioc, MPI3_EVENT_ENERGY_PACK_CHANGE);
3743e3605f65SSreekanth Reddy 
3744e3605f65SSreekanth Reddy 	retval = mpi3mr_issue_event_notification(mrioc);
3745e3605f65SSreekanth Reddy 	if (retval)
3746e3605f65SSreekanth Reddy 		ioc_err(mrioc, "failed to issue event notification %d\n",
3747e3605f65SSreekanth Reddy 		    retval);
3748e3605f65SSreekanth Reddy 	return retval;
3749e3605f65SSreekanth Reddy }
3750e3605f65SSreekanth Reddy 
3751e3605f65SSreekanth Reddy /**
3752824a1566SKashyap Desai  * mpi3mr_init_ioc - Initialize the controller
3753824a1566SKashyap Desai  * @mrioc: Adapter instance reference
3754824a1566SKashyap Desai  *
3755824a1566SKashyap Desai  * This the controller initialization routine, executed either
3756824a1566SKashyap Desai  * after soft reset or from pci probe callback.
3757824a1566SKashyap Desai  * Setup the required resources, memory map the controller
3758824a1566SKashyap Desai  * registers, create admin and operational reply queue pairs,
3759824a1566SKashyap Desai  * allocate required memory for reply pool, sense buffer pool,
3760824a1566SKashyap Desai  * issue IOC init request to the firmware, unmask the events and
3761824a1566SKashyap Desai  * issue port enable to discover SAS/SATA/NVMe devies and RAID
3762824a1566SKashyap Desai  * volumes.
3763824a1566SKashyap Desai  *
3764824a1566SKashyap Desai  * Return: 0 on success and non-zero on failure.
3765824a1566SKashyap Desai  */
3766fe6db615SSreekanth Reddy int mpi3mr_init_ioc(struct mpi3mr_ioc *mrioc)
3767824a1566SKashyap Desai {
3768824a1566SKashyap Desai 	int retval = 0;
3769fe6db615SSreekanth Reddy 	u8 retry = 0;
3770824a1566SKashyap Desai 	struct mpi3_ioc_facts_data facts_data;
3771f10af057SSreekanth Reddy 	u32 sz;
3772824a1566SKashyap Desai 
3773fe6db615SSreekanth Reddy retry_init:
3774824a1566SKashyap Desai 	retval = mpi3mr_bring_ioc_ready(mrioc);
3775824a1566SKashyap Desai 	if (retval) {
3776824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to bring ioc ready: error %d\n",
3777824a1566SKashyap Desai 		    retval);
3778fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3779824a1566SKashyap Desai 	}
3780824a1566SKashyap Desai 
3781824a1566SKashyap Desai 	retval = mpi3mr_setup_isr(mrioc, 1);
3782824a1566SKashyap Desai 	if (retval) {
3783824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to setup ISR error %d\n",
3784824a1566SKashyap Desai 		    retval);
3785fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3786824a1566SKashyap Desai 	}
3787824a1566SKashyap Desai 
3788824a1566SKashyap Desai 	retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
3789824a1566SKashyap Desai 	if (retval) {
3790824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to Issue IOC Facts %d\n",
3791824a1566SKashyap Desai 		    retval);
3792824a1566SKashyap Desai 		goto out_failed;
3793824a1566SKashyap Desai 	}
3794824a1566SKashyap Desai 
3795c5758fc7SSreekanth Reddy 	mrioc->max_host_ios = mrioc->facts.max_reqs - MPI3MR_INTERNAL_CMDS_RESVD;
3796c5758fc7SSreekanth Reddy 
3797f10af057SSreekanth Reddy 	mrioc->num_io_throttle_group = mrioc->facts.max_io_throttle_group;
3798f10af057SSreekanth Reddy 	atomic_set(&mrioc->pend_large_data_sz, 0);
3799f10af057SSreekanth Reddy 
3800c5758fc7SSreekanth Reddy 	if (reset_devices)
3801c5758fc7SSreekanth Reddy 		mrioc->max_host_ios = min_t(int, mrioc->max_host_ios,
3802c5758fc7SSreekanth Reddy 		    MPI3MR_HOST_IOS_KDUMP);
3803c5758fc7SSreekanth Reddy 
3804c4723e68SSreekanth Reddy 	if (!(mrioc->facts.ioc_capabilities &
3805c4723e68SSreekanth Reddy 	    MPI3_IOCFACTS_CAPABILITY_MULTIPATH_ENABLED)) {
3806c4723e68SSreekanth Reddy 		mrioc->sas_transport_enabled = 1;
3807626665e9SSreekanth Reddy 		mrioc->scsi_device_channel = 1;
3808626665e9SSreekanth Reddy 		mrioc->shost->max_channel = 1;
3809176d4aa6SSreekanth Reddy 		mrioc->shost->transportt = mpi3mr_transport_template;
3810c4723e68SSreekanth Reddy 	}
3811c4723e68SSreekanth Reddy 
3812c5758fc7SSreekanth Reddy 	mrioc->reply_sz = mrioc->facts.reply_sz;
3813fe6db615SSreekanth Reddy 
3814824a1566SKashyap Desai 	retval = mpi3mr_check_reset_dma_mask(mrioc);
3815824a1566SKashyap Desai 	if (retval) {
3816824a1566SKashyap Desai 		ioc_err(mrioc, "Resetting dma mask failed %d\n",
3817824a1566SKashyap Desai 		    retval);
3818fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3819fb9b0457SKashyap Desai 	}
3820824a1566SKashyap Desai 
3821ff9561e9SKashyap Desai 	mpi3mr_print_ioc_info(mrioc);
3822ff9561e9SKashyap Desai 
382332d457d5SSreekanth Reddy 	dprint_init(mrioc, "allocating config page buffers\n");
382432d457d5SSreekanth Reddy 	mrioc->cfg_page = dma_alloc_coherent(&mrioc->pdev->dev,
382532d457d5SSreekanth Reddy 	    MPI3MR_DEFAULT_CFG_PAGE_SZ, &mrioc->cfg_page_dma, GFP_KERNEL);
382632d457d5SSreekanth Reddy 	if (!mrioc->cfg_page)
382732d457d5SSreekanth Reddy 		goto out_failed_noretry;
382832d457d5SSreekanth Reddy 
382932d457d5SSreekanth Reddy 	mrioc->cfg_page_sz = MPI3MR_DEFAULT_CFG_PAGE_SZ;
383032d457d5SSreekanth Reddy 
3831824a1566SKashyap Desai 	retval = mpi3mr_alloc_reply_sense_bufs(mrioc);
3832824a1566SKashyap Desai 	if (retval) {
3833824a1566SKashyap Desai 		ioc_err(mrioc,
3834824a1566SKashyap Desai 		    "%s :Failed to allocated reply sense buffers %d\n",
3835824a1566SKashyap Desai 		    __func__, retval);
3836fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3837824a1566SKashyap Desai 	}
3838824a1566SKashyap Desai 
3839824a1566SKashyap Desai 	retval = mpi3mr_alloc_chain_bufs(mrioc);
3840824a1566SKashyap Desai 	if (retval) {
3841824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to allocated chain buffers %d\n",
3842824a1566SKashyap Desai 		    retval);
3843fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3844fb9b0457SKashyap Desai 	}
3845824a1566SKashyap Desai 
3846824a1566SKashyap Desai 	retval = mpi3mr_issue_iocinit(mrioc);
3847824a1566SKashyap Desai 	if (retval) {
3848824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to Issue IOC Init %d\n",
3849824a1566SKashyap Desai 		    retval);
3850824a1566SKashyap Desai 		goto out_failed;
3851824a1566SKashyap Desai 	}
3852824a1566SKashyap Desai 
38532ac794baSSreekanth Reddy 	retval = mpi3mr_print_pkg_ver(mrioc);
38542ac794baSSreekanth Reddy 	if (retval) {
38552ac794baSSreekanth Reddy 		ioc_err(mrioc, "failed to get package version\n");
38562ac794baSSreekanth Reddy 		goto out_failed;
38572ac794baSSreekanth Reddy 	}
38582ac794baSSreekanth Reddy 
3859824a1566SKashyap Desai 	retval = mpi3mr_setup_isr(mrioc, 0);
3860824a1566SKashyap Desai 	if (retval) {
3861824a1566SKashyap Desai 		ioc_err(mrioc, "Failed to re-setup ISR, error %d\n",
3862824a1566SKashyap Desai 		    retval);
3863fe6db615SSreekanth Reddy 		goto out_failed_noretry;
3864fb9b0457SKashyap Desai 	}
3865824a1566SKashyap Desai 
3866c9566231SKashyap Desai 	retval = mpi3mr_create_op_queues(mrioc);
3867c9566231SKashyap Desai 	if (retval) {
3868c9566231SKashyap Desai 		ioc_err(mrioc, "Failed to create OpQueues error %d\n",
3869c9566231SKashyap Desai 		    retval);
3870c9566231SKashyap Desai 		goto out_failed;
3871c9566231SKashyap Desai 	}
3872c9566231SKashyap Desai 
387343ca1100SSumit Saxena 	if (!mrioc->pel_seqnum_virt) {
387443ca1100SSumit Saxena 		dprint_init(mrioc, "allocating memory for pel_seqnum_virt\n");
387543ca1100SSumit Saxena 		mrioc->pel_seqnum_sz = sizeof(struct mpi3_pel_seq);
387643ca1100SSumit Saxena 		mrioc->pel_seqnum_virt = dma_alloc_coherent(&mrioc->pdev->dev,
387743ca1100SSumit Saxena 		    mrioc->pel_seqnum_sz, &mrioc->pel_seqnum_dma,
387843ca1100SSumit Saxena 		    GFP_KERNEL);
3879bc7896d3SDan Carpenter 		if (!mrioc->pel_seqnum_virt) {
3880bc7896d3SDan Carpenter 			retval = -ENOMEM;
388143ca1100SSumit Saxena 			goto out_failed_noretry;
388243ca1100SSumit Saxena 		}
3883bc7896d3SDan Carpenter 	}
388443ca1100SSumit Saxena 
3885f10af057SSreekanth Reddy 	if (!mrioc->throttle_groups && mrioc->num_io_throttle_group) {
3886f10af057SSreekanth Reddy 		dprint_init(mrioc, "allocating memory for throttle groups\n");
3887f10af057SSreekanth Reddy 		sz = sizeof(struct mpi3mr_throttle_group_info);
3888c863a2dcSJules Irenge 		mrioc->throttle_groups = kcalloc(mrioc->num_io_throttle_group, sz, GFP_KERNEL);
3889f10af057SSreekanth Reddy 		if (!mrioc->throttle_groups)
3890f10af057SSreekanth Reddy 			goto out_failed_noretry;
3891f10af057SSreekanth Reddy 	}
3892f10af057SSreekanth Reddy 
3893e3605f65SSreekanth Reddy 	retval = mpi3mr_enable_events(mrioc);
389413ef29eaSKashyap Desai 	if (retval) {
3895e3605f65SSreekanth Reddy 		ioc_err(mrioc, "failed to enable events %d\n",
389613ef29eaSKashyap Desai 		    retval);
389713ef29eaSKashyap Desai 		goto out_failed;
389813ef29eaSKashyap Desai 	}
389913ef29eaSKashyap Desai 
3900fe6db615SSreekanth Reddy 	ioc_info(mrioc, "controller initialization completed successfully\n");
3901824a1566SKashyap Desai 	return retval;
3902824a1566SKashyap Desai out_failed:
3903fe6db615SSreekanth Reddy 	if (retry < 2) {
3904fe6db615SSreekanth Reddy 		retry++;
3905fe6db615SSreekanth Reddy 		ioc_warn(mrioc, "retrying controller initialization, retry_count:%d\n",
3906fe6db615SSreekanth Reddy 		    retry);
3907fe6db615SSreekanth Reddy 		mpi3mr_memset_buffers(mrioc);
3908fe6db615SSreekanth Reddy 		goto retry_init;
3909fe6db615SSreekanth Reddy 	}
3910fe6db615SSreekanth Reddy out_failed_noretry:
3911fe6db615SSreekanth Reddy 	ioc_err(mrioc, "controller initialization failed\n");
3912fe6db615SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
3913fe6db615SSreekanth Reddy 	    MPI3MR_RESET_FROM_CTLR_CLEANUP);
3914fe6db615SSreekanth Reddy 	mrioc->unrecoverable = 1;
3915824a1566SKashyap Desai 	return retval;
3916824a1566SKashyap Desai }
3917824a1566SKashyap Desai 
3918c0b00a93SSreekanth Reddy /**
3919c0b00a93SSreekanth Reddy  * mpi3mr_reinit_ioc - Re-Initialize the controller
3920c0b00a93SSreekanth Reddy  * @mrioc: Adapter instance reference
3921c0b00a93SSreekanth Reddy  * @is_resume: Called from resume or reset path
3922c0b00a93SSreekanth Reddy  *
3923c0b00a93SSreekanth Reddy  * This the controller re-initialization routine, executed from
3924c0b00a93SSreekanth Reddy  * the soft reset handler or resume callback. Creates
3925c0b00a93SSreekanth Reddy  * operational reply queue pairs, allocate required memory for
3926c0b00a93SSreekanth Reddy  * reply pool, sense buffer pool, issue IOC init request to the
3927c0b00a93SSreekanth Reddy  * firmware, unmask the events and issue port enable to discover
3928c0b00a93SSreekanth Reddy  * SAS/SATA/NVMe devices and RAID volumes.
3929c0b00a93SSreekanth Reddy  *
3930c0b00a93SSreekanth Reddy  * Return: 0 on success and non-zero on failure.
3931c0b00a93SSreekanth Reddy  */
3932fe6db615SSreekanth Reddy int mpi3mr_reinit_ioc(struct mpi3mr_ioc *mrioc, u8 is_resume)
3933fe6db615SSreekanth Reddy {
3934c0b00a93SSreekanth Reddy 	int retval = 0;
3935c0b00a93SSreekanth Reddy 	u8 retry = 0;
3936c0b00a93SSreekanth Reddy 	struct mpi3_ioc_facts_data facts_data;
3937f2a79d20SSreekanth Reddy 	u32 pe_timeout, ioc_status;
3938fe6db615SSreekanth Reddy 
3939c0b00a93SSreekanth Reddy retry_init:
3940f2a79d20SSreekanth Reddy 	pe_timeout =
3941f2a79d20SSreekanth Reddy 	    (MPI3MR_PORTENABLE_TIMEOUT / MPI3MR_PORTENABLE_POLL_INTERVAL);
3942f2a79d20SSreekanth Reddy 
3943c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "bringing up the controller to ready state\n");
3944c0b00a93SSreekanth Reddy 	retval = mpi3mr_bring_ioc_ready(mrioc);
3945c0b00a93SSreekanth Reddy 	if (retval) {
3946c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to bring to ready state\n");
3947c0b00a93SSreekanth Reddy 		goto out_failed_noretry;
3948c0b00a93SSreekanth Reddy 	}
3949c0b00a93SSreekanth Reddy 
3950c0b00a93SSreekanth Reddy 	if (is_resume) {
3951c0b00a93SSreekanth Reddy 		dprint_reset(mrioc, "setting up single ISR\n");
3952c0b00a93SSreekanth Reddy 		retval = mpi3mr_setup_isr(mrioc, 1);
3953c0b00a93SSreekanth Reddy 		if (retval) {
3954c0b00a93SSreekanth Reddy 			ioc_err(mrioc, "failed to setup ISR\n");
3955c0b00a93SSreekanth Reddy 			goto out_failed_noretry;
3956c0b00a93SSreekanth Reddy 		}
3957c0b00a93SSreekanth Reddy 	} else
3958c0b00a93SSreekanth Reddy 		mpi3mr_ioc_enable_intr(mrioc);
3959c0b00a93SSreekanth Reddy 
3960c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "getting ioc_facts\n");
3961c0b00a93SSreekanth Reddy 	retval = mpi3mr_issue_iocfacts(mrioc, &facts_data);
3962c0b00a93SSreekanth Reddy 	if (retval) {
3963c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to get ioc_facts\n");
3964c0b00a93SSreekanth Reddy 		goto out_failed;
3965c0b00a93SSreekanth Reddy 	}
3966c0b00a93SSreekanth Reddy 
3967c5758fc7SSreekanth Reddy 	dprint_reset(mrioc, "validating ioc_facts\n");
3968c5758fc7SSreekanth Reddy 	retval = mpi3mr_revalidate_factsdata(mrioc);
3969c5758fc7SSreekanth Reddy 	if (retval) {
3970c5758fc7SSreekanth Reddy 		ioc_err(mrioc, "failed to revalidate ioc_facts data\n");
3971c5758fc7SSreekanth Reddy 		goto out_failed_noretry;
3972c5758fc7SSreekanth Reddy 	}
3973c0b00a93SSreekanth Reddy 
3974c0b00a93SSreekanth Reddy 	mpi3mr_print_ioc_info(mrioc);
3975c0b00a93SSreekanth Reddy 
3976c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "sending ioc_init\n");
3977c0b00a93SSreekanth Reddy 	retval = mpi3mr_issue_iocinit(mrioc);
3978c0b00a93SSreekanth Reddy 	if (retval) {
3979c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to send ioc_init\n");
3980c0b00a93SSreekanth Reddy 		goto out_failed;
3981c0b00a93SSreekanth Reddy 	}
3982c0b00a93SSreekanth Reddy 
3983c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "getting package version\n");
3984c0b00a93SSreekanth Reddy 	retval = mpi3mr_print_pkg_ver(mrioc);
3985c0b00a93SSreekanth Reddy 	if (retval) {
3986c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to get package version\n");
3987c0b00a93SSreekanth Reddy 		goto out_failed;
3988c0b00a93SSreekanth Reddy 	}
3989c0b00a93SSreekanth Reddy 
3990c0b00a93SSreekanth Reddy 	if (is_resume) {
3991c0b00a93SSreekanth Reddy 		dprint_reset(mrioc, "setting up multiple ISR\n");
3992c0b00a93SSreekanth Reddy 		retval = mpi3mr_setup_isr(mrioc, 0);
3993c0b00a93SSreekanth Reddy 		if (retval) {
3994c0b00a93SSreekanth Reddy 			ioc_err(mrioc, "failed to re-setup ISR\n");
3995c0b00a93SSreekanth Reddy 			goto out_failed_noretry;
3996c0b00a93SSreekanth Reddy 		}
3997c0b00a93SSreekanth Reddy 	}
3998c0b00a93SSreekanth Reddy 
3999c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "creating operational queue pairs\n");
4000c0b00a93SSreekanth Reddy 	retval = mpi3mr_create_op_queues(mrioc);
4001c0b00a93SSreekanth Reddy 	if (retval) {
4002c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to create operational queue pairs\n");
4003c0b00a93SSreekanth Reddy 		goto out_failed;
4004c0b00a93SSreekanth Reddy 	}
4005c0b00a93SSreekanth Reddy 
400643ca1100SSumit Saxena 	if (!mrioc->pel_seqnum_virt) {
400743ca1100SSumit Saxena 		dprint_reset(mrioc, "allocating memory for pel_seqnum_virt\n");
400843ca1100SSumit Saxena 		mrioc->pel_seqnum_sz = sizeof(struct mpi3_pel_seq);
400943ca1100SSumit Saxena 		mrioc->pel_seqnum_virt = dma_alloc_coherent(&mrioc->pdev->dev,
401043ca1100SSumit Saxena 		    mrioc->pel_seqnum_sz, &mrioc->pel_seqnum_dma,
401143ca1100SSumit Saxena 		    GFP_KERNEL);
4012bc7896d3SDan Carpenter 		if (!mrioc->pel_seqnum_virt) {
4013bc7896d3SDan Carpenter 			retval = -ENOMEM;
401443ca1100SSumit Saxena 			goto out_failed_noretry;
401543ca1100SSumit Saxena 		}
4016bc7896d3SDan Carpenter 	}
401743ca1100SSumit Saxena 
4018c0b00a93SSreekanth Reddy 	if (mrioc->shost->nr_hw_queues > mrioc->num_op_reply_q) {
4019c0b00a93SSreekanth Reddy 		ioc_err(mrioc,
40205867b856SColin Ian King 		    "cannot create minimum number of operational queues expected:%d created:%d\n",
4021c0b00a93SSreekanth Reddy 		    mrioc->shost->nr_hw_queues, mrioc->num_op_reply_q);
4022c0b00a93SSreekanth Reddy 		goto out_failed_noretry;
4023c0b00a93SSreekanth Reddy 	}
4024c0b00a93SSreekanth Reddy 
4025c0b00a93SSreekanth Reddy 	dprint_reset(mrioc, "enabling events\n");
4026c0b00a93SSreekanth Reddy 	retval = mpi3mr_enable_events(mrioc);
4027c0b00a93SSreekanth Reddy 	if (retval) {
4028c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to enable events\n");
4029c0b00a93SSreekanth Reddy 		goto out_failed;
4030c0b00a93SSreekanth Reddy 	}
4031c0b00a93SSreekanth Reddy 
40322745ce0eSSreekanth Reddy 	mrioc->device_refresh_on = 1;
40332745ce0eSSreekanth Reddy 	mpi3mr_add_event_wait_for_device_refresh(mrioc);
40342745ce0eSSreekanth Reddy 
4035c0b00a93SSreekanth Reddy 	ioc_info(mrioc, "sending port enable\n");
4036f2a79d20SSreekanth Reddy 	retval = mpi3mr_issue_port_enable(mrioc, 1);
4037c0b00a93SSreekanth Reddy 	if (retval) {
4038c0b00a93SSreekanth Reddy 		ioc_err(mrioc, "failed to issue port enable\n");
4039c0b00a93SSreekanth Reddy 		goto out_failed;
4040c0b00a93SSreekanth Reddy 	}
4041f2a79d20SSreekanth Reddy 	do {
4042f2a79d20SSreekanth Reddy 		ssleep(MPI3MR_PORTENABLE_POLL_INTERVAL);
4043f2a79d20SSreekanth Reddy 		if (mrioc->init_cmds.state == MPI3MR_CMD_NOTUSED)
4044f2a79d20SSreekanth Reddy 			break;
4045f2a79d20SSreekanth Reddy 		if (!pci_device_is_present(mrioc->pdev))
4046f2a79d20SSreekanth Reddy 			mrioc->unrecoverable = 1;
4047f2a79d20SSreekanth Reddy 		if (mrioc->unrecoverable) {
4048f2a79d20SSreekanth Reddy 			retval = -1;
4049f2a79d20SSreekanth Reddy 			goto out_failed_noretry;
4050f2a79d20SSreekanth Reddy 		}
4051f2a79d20SSreekanth Reddy 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4052f2a79d20SSreekanth Reddy 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_RESET_HISTORY) ||
4053f2a79d20SSreekanth Reddy 		    (ioc_status & MPI3_SYSIF_IOC_STATUS_FAULT)) {
4054f2a79d20SSreekanth Reddy 			mpi3mr_print_fault_info(mrioc);
4055f2a79d20SSreekanth Reddy 			mrioc->init_cmds.is_waiting = 0;
4056f2a79d20SSreekanth Reddy 			mrioc->init_cmds.callback = NULL;
4057f2a79d20SSreekanth Reddy 			mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4058f2a79d20SSreekanth Reddy 			goto out_failed;
4059f2a79d20SSreekanth Reddy 		}
4060f2a79d20SSreekanth Reddy 	} while (--pe_timeout);
4061f2a79d20SSreekanth Reddy 
4062f2a79d20SSreekanth Reddy 	if (!pe_timeout) {
4063f2a79d20SSreekanth Reddy 		ioc_err(mrioc, "port enable timed out\n");
4064f2a79d20SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
4065f2a79d20SSreekanth Reddy 		    MPI3MR_RESET_FROM_PE_TIMEOUT);
4066f2a79d20SSreekanth Reddy 		mrioc->init_cmds.is_waiting = 0;
4067f2a79d20SSreekanth Reddy 		mrioc->init_cmds.callback = NULL;
4068f2a79d20SSreekanth Reddy 		mrioc->init_cmds.state = MPI3MR_CMD_NOTUSED;
4069f2a79d20SSreekanth Reddy 		goto out_failed;
4070f2a79d20SSreekanth Reddy 	} else if (mrioc->scan_failed) {
4071f2a79d20SSreekanth Reddy 		ioc_err(mrioc,
4072f2a79d20SSreekanth Reddy 		    "port enable failed with status=0x%04x\n",
4073f2a79d20SSreekanth Reddy 		    mrioc->scan_failed);
4074f2a79d20SSreekanth Reddy 	} else
4075f2a79d20SSreekanth Reddy 		ioc_info(mrioc, "port enable completed successfully\n");
4076c0b00a93SSreekanth Reddy 
4077c0b00a93SSreekanth Reddy 	ioc_info(mrioc, "controller %s completed successfully\n",
4078c0b00a93SSreekanth Reddy 	    (is_resume)?"resume":"re-initialization");
4079c0b00a93SSreekanth Reddy 	return retval;
4080c0b00a93SSreekanth Reddy out_failed:
4081c0b00a93SSreekanth Reddy 	if (retry < 2) {
4082c0b00a93SSreekanth Reddy 		retry++;
4083c0b00a93SSreekanth Reddy 		ioc_warn(mrioc, "retrying controller %s, retry_count:%d\n",
4084c0b00a93SSreekanth Reddy 		    (is_resume)?"resume":"re-initialization", retry);
4085c0b00a93SSreekanth Reddy 		mpi3mr_memset_buffers(mrioc);
4086c0b00a93SSreekanth Reddy 		goto retry_init;
4087c0b00a93SSreekanth Reddy 	}
4088c0b00a93SSreekanth Reddy out_failed_noretry:
4089c0b00a93SSreekanth Reddy 	ioc_err(mrioc, "controller %s is failed\n",
4090c0b00a93SSreekanth Reddy 	    (is_resume)?"resume":"re-initialization");
4091c0b00a93SSreekanth Reddy 	mpi3mr_issue_reset(mrioc, MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT,
4092c0b00a93SSreekanth Reddy 	    MPI3MR_RESET_FROM_CTLR_CLEANUP);
4093c0b00a93SSreekanth Reddy 	mrioc->unrecoverable = 1;
4094c0b00a93SSreekanth Reddy 	return retval;
4095fe6db615SSreekanth Reddy }
4096fe6db615SSreekanth Reddy 
4097824a1566SKashyap Desai /**
4098fb9b0457SKashyap Desai  * mpi3mr_memset_op_reply_q_buffers - memset the operational reply queue's
4099fb9b0457SKashyap Desai  *					segments
4100fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4101fb9b0457SKashyap Desai  * @qidx: Operational reply queue index
4102fb9b0457SKashyap Desai  *
4103fb9b0457SKashyap Desai  * Return: Nothing.
4104fb9b0457SKashyap Desai  */
4105fb9b0457SKashyap Desai static void mpi3mr_memset_op_reply_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
4106fb9b0457SKashyap Desai {
4107fb9b0457SKashyap Desai 	struct op_reply_qinfo *op_reply_q = mrioc->op_reply_qinfo + qidx;
4108fb9b0457SKashyap Desai 	struct segments *segments;
4109fb9b0457SKashyap Desai 	int i, size;
4110fb9b0457SKashyap Desai 
4111fb9b0457SKashyap Desai 	if (!op_reply_q->q_segments)
4112fb9b0457SKashyap Desai 		return;
4113fb9b0457SKashyap Desai 
4114fb9b0457SKashyap Desai 	size = op_reply_q->segment_qd * mrioc->op_reply_desc_sz;
4115fb9b0457SKashyap Desai 	segments = op_reply_q->q_segments;
4116fb9b0457SKashyap Desai 	for (i = 0; i < op_reply_q->num_segments; i++)
4117fb9b0457SKashyap Desai 		memset(segments[i].segment, 0, size);
4118fb9b0457SKashyap Desai }
4119fb9b0457SKashyap Desai 
4120fb9b0457SKashyap Desai /**
4121fb9b0457SKashyap Desai  * mpi3mr_memset_op_req_q_buffers - memset the operational request queue's
4122fb9b0457SKashyap Desai  *					segments
4123fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4124fb9b0457SKashyap Desai  * @qidx: Operational request queue index
4125fb9b0457SKashyap Desai  *
4126fb9b0457SKashyap Desai  * Return: Nothing.
4127fb9b0457SKashyap Desai  */
4128fb9b0457SKashyap Desai static void mpi3mr_memset_op_req_q_buffers(struct mpi3mr_ioc *mrioc, u16 qidx)
4129fb9b0457SKashyap Desai {
4130fb9b0457SKashyap Desai 	struct op_req_qinfo *op_req_q = mrioc->req_qinfo + qidx;
4131fb9b0457SKashyap Desai 	struct segments *segments;
4132fb9b0457SKashyap Desai 	int i, size;
4133fb9b0457SKashyap Desai 
4134fb9b0457SKashyap Desai 	if (!op_req_q->q_segments)
4135fb9b0457SKashyap Desai 		return;
4136fb9b0457SKashyap Desai 
4137fb9b0457SKashyap Desai 	size = op_req_q->segment_qd * mrioc->facts.op_req_sz;
4138fb9b0457SKashyap Desai 	segments = op_req_q->q_segments;
4139fb9b0457SKashyap Desai 	for (i = 0; i < op_req_q->num_segments; i++)
4140fb9b0457SKashyap Desai 		memset(segments[i].segment, 0, size);
4141fb9b0457SKashyap Desai }
4142fb9b0457SKashyap Desai 
4143fb9b0457SKashyap Desai /**
4144fb9b0457SKashyap Desai  * mpi3mr_memset_buffers - memset memory for a controller
4145fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4146fb9b0457SKashyap Desai  *
4147fb9b0457SKashyap Desai  * clear all the memory allocated for a controller, typically
4148fb9b0457SKashyap Desai  * called post reset to reuse the memory allocated during the
4149fb9b0457SKashyap Desai  * controller init.
4150fb9b0457SKashyap Desai  *
4151fb9b0457SKashyap Desai  * Return: Nothing.
4152fb9b0457SKashyap Desai  */
41530da66348SKashyap Desai void mpi3mr_memset_buffers(struct mpi3mr_ioc *mrioc)
4154fb9b0457SKashyap Desai {
4155fb9b0457SKashyap Desai 	u16 i;
4156f10af057SSreekanth Reddy 	struct mpi3mr_throttle_group_info *tg;
4157fb9b0457SKashyap Desai 
4158fe6db615SSreekanth Reddy 	mrioc->change_count = 0;
4159afd3a579SSreekanth Reddy 	mrioc->active_poll_qcount = 0;
4160afd3a579SSreekanth Reddy 	mrioc->default_qcount = 0;
4161fe6db615SSreekanth Reddy 	if (mrioc->admin_req_base)
4162fb9b0457SKashyap Desai 		memset(mrioc->admin_req_base, 0, mrioc->admin_req_q_sz);
4163fe6db615SSreekanth Reddy 	if (mrioc->admin_reply_base)
4164fb9b0457SKashyap Desai 		memset(mrioc->admin_reply_base, 0, mrioc->admin_reply_q_sz);
4165*02ca7da2SRanjan Kumar 	atomic_set(&mrioc->admin_reply_q_in_use, 0);
4166fb9b0457SKashyap Desai 
4167fe6db615SSreekanth Reddy 	if (mrioc->init_cmds.reply) {
4168fb9b0457SKashyap Desai 		memset(mrioc->init_cmds.reply, 0, sizeof(*mrioc->init_cmds.reply));
4169f5e6d5a3SSumit Saxena 		memset(mrioc->bsg_cmds.reply, 0,
4170f5e6d5a3SSumit Saxena 		    sizeof(*mrioc->bsg_cmds.reply));
4171e844adb1SKashyap Desai 		memset(mrioc->host_tm_cmds.reply, 0,
4172e844adb1SKashyap Desai 		    sizeof(*mrioc->host_tm_cmds.reply));
417343ca1100SSumit Saxena 		memset(mrioc->pel_cmds.reply, 0,
417443ca1100SSumit Saxena 		    sizeof(*mrioc->pel_cmds.reply));
417543ca1100SSumit Saxena 		memset(mrioc->pel_abort_cmd.reply, 0,
417643ca1100SSumit Saxena 		    sizeof(*mrioc->pel_abort_cmd.reply));
41772bd37e28SSreekanth Reddy 		memset(mrioc->transport_cmds.reply, 0,
41782bd37e28SSreekanth Reddy 		    sizeof(*mrioc->transport_cmds.reply));
4179fb9b0457SKashyap Desai 		for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++)
4180fb9b0457SKashyap Desai 			memset(mrioc->dev_rmhs_cmds[i].reply, 0,
4181fb9b0457SKashyap Desai 			    sizeof(*mrioc->dev_rmhs_cmds[i].reply));
4182c1af985dSSreekanth Reddy 		for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++)
4183c1af985dSSreekanth Reddy 			memset(mrioc->evtack_cmds[i].reply, 0,
4184c1af985dSSreekanth Reddy 			    sizeof(*mrioc->evtack_cmds[i].reply));
4185339e6156SShin'ichiro Kawasaki 		bitmap_clear(mrioc->removepend_bitmap, 0,
4186339e6156SShin'ichiro Kawasaki 			     mrioc->dev_handle_bitmap_bits);
4187339e6156SShin'ichiro Kawasaki 		bitmap_clear(mrioc->devrem_bitmap, 0, MPI3MR_NUM_DEVRMCMD);
4188339e6156SShin'ichiro Kawasaki 		bitmap_clear(mrioc->evtack_cmds_bitmap, 0,
4189339e6156SShin'ichiro Kawasaki 			     MPI3MR_NUM_EVTACKCMD);
4190fe6db615SSreekanth Reddy 	}
4191fb9b0457SKashyap Desai 
4192fb9b0457SKashyap Desai 	for (i = 0; i < mrioc->num_queues; i++) {
4193fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].qid = 0;
4194fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].ci = 0;
4195fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].num_replies = 0;
4196fb9b0457SKashyap Desai 		mrioc->op_reply_qinfo[i].ephase = 0;
4197463429f8SKashyap Desai 		atomic_set(&mrioc->op_reply_qinfo[i].pend_ios, 0);
4198463429f8SKashyap Desai 		atomic_set(&mrioc->op_reply_qinfo[i].in_use, 0);
4199fb9b0457SKashyap Desai 		mpi3mr_memset_op_reply_q_buffers(mrioc, i);
4200fb9b0457SKashyap Desai 
4201fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].ci = 0;
4202fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].pi = 0;
4203fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].num_requests = 0;
4204fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].qid = 0;
4205fb9b0457SKashyap Desai 		mrioc->req_qinfo[i].reply_qid = 0;
4206fb9b0457SKashyap Desai 		spin_lock_init(&mrioc->req_qinfo[i].q_lock);
4207fb9b0457SKashyap Desai 		mpi3mr_memset_op_req_q_buffers(mrioc, i);
4208fb9b0457SKashyap Desai 	}
4209f10af057SSreekanth Reddy 
4210f10af057SSreekanth Reddy 	atomic_set(&mrioc->pend_large_data_sz, 0);
4211f10af057SSreekanth Reddy 	if (mrioc->throttle_groups) {
4212f10af057SSreekanth Reddy 		tg = mrioc->throttle_groups;
4213f10af057SSreekanth Reddy 		for (i = 0; i < mrioc->num_io_throttle_group; i++, tg++) {
4214f10af057SSreekanth Reddy 			tg->id = 0;
4215cf1ce8b7SSreekanth Reddy 			tg->fw_qd = 0;
4216cf1ce8b7SSreekanth Reddy 			tg->modified_qd = 0;
4217f10af057SSreekanth Reddy 			tg->io_divert = 0;
4218cf1ce8b7SSreekanth Reddy 			tg->need_qd_reduction = 0;
4219f10af057SSreekanth Reddy 			tg->high = 0;
4220f10af057SSreekanth Reddy 			tg->low = 0;
4221cf1ce8b7SSreekanth Reddy 			tg->qd_reduction = 0;
4222f10af057SSreekanth Reddy 			atomic_set(&tg->pend_large_data_sz, 0);
4223f10af057SSreekanth Reddy 		}
4224f10af057SSreekanth Reddy 	}
4225fb9b0457SKashyap Desai }
4226fb9b0457SKashyap Desai 
4227fb9b0457SKashyap Desai /**
4228824a1566SKashyap Desai  * mpi3mr_free_mem - Free memory allocated for a controller
4229824a1566SKashyap Desai  * @mrioc: Adapter instance reference
4230824a1566SKashyap Desai  *
4231824a1566SKashyap Desai  * Free all the memory allocated for a controller.
4232824a1566SKashyap Desai  *
4233824a1566SKashyap Desai  * Return: Nothing.
4234824a1566SKashyap Desai  */
4235fe6db615SSreekanth Reddy void mpi3mr_free_mem(struct mpi3mr_ioc *mrioc)
4236824a1566SKashyap Desai {
4237824a1566SKashyap Desai 	u16 i;
4238824a1566SKashyap Desai 	struct mpi3mr_intr_info *intr_info;
4239824a1566SKashyap Desai 
4240130fc180SSreekanth Reddy 	mpi3mr_free_enclosure_list(mrioc);
4241130fc180SSreekanth Reddy 
4242824a1566SKashyap Desai 	if (mrioc->sense_buf_pool) {
4243824a1566SKashyap Desai 		if (mrioc->sense_buf)
4244824a1566SKashyap Desai 			dma_pool_free(mrioc->sense_buf_pool, mrioc->sense_buf,
4245824a1566SKashyap Desai 			    mrioc->sense_buf_dma);
4246824a1566SKashyap Desai 		dma_pool_destroy(mrioc->sense_buf_pool);
4247824a1566SKashyap Desai 		mrioc->sense_buf = NULL;
4248824a1566SKashyap Desai 		mrioc->sense_buf_pool = NULL;
4249824a1566SKashyap Desai 	}
4250824a1566SKashyap Desai 	if (mrioc->sense_buf_q_pool) {
4251824a1566SKashyap Desai 		if (mrioc->sense_buf_q)
4252824a1566SKashyap Desai 			dma_pool_free(mrioc->sense_buf_q_pool,
4253824a1566SKashyap Desai 			    mrioc->sense_buf_q, mrioc->sense_buf_q_dma);
4254824a1566SKashyap Desai 		dma_pool_destroy(mrioc->sense_buf_q_pool);
4255824a1566SKashyap Desai 		mrioc->sense_buf_q = NULL;
4256824a1566SKashyap Desai 		mrioc->sense_buf_q_pool = NULL;
4257824a1566SKashyap Desai 	}
4258824a1566SKashyap Desai 
4259824a1566SKashyap Desai 	if (mrioc->reply_buf_pool) {
4260824a1566SKashyap Desai 		if (mrioc->reply_buf)
4261824a1566SKashyap Desai 			dma_pool_free(mrioc->reply_buf_pool, mrioc->reply_buf,
4262824a1566SKashyap Desai 			    mrioc->reply_buf_dma);
4263824a1566SKashyap Desai 		dma_pool_destroy(mrioc->reply_buf_pool);
4264824a1566SKashyap Desai 		mrioc->reply_buf = NULL;
4265824a1566SKashyap Desai 		mrioc->reply_buf_pool = NULL;
4266824a1566SKashyap Desai 	}
4267824a1566SKashyap Desai 	if (mrioc->reply_free_q_pool) {
4268824a1566SKashyap Desai 		if (mrioc->reply_free_q)
4269824a1566SKashyap Desai 			dma_pool_free(mrioc->reply_free_q_pool,
4270824a1566SKashyap Desai 			    mrioc->reply_free_q, mrioc->reply_free_q_dma);
4271824a1566SKashyap Desai 		dma_pool_destroy(mrioc->reply_free_q_pool);
4272824a1566SKashyap Desai 		mrioc->reply_free_q = NULL;
4273824a1566SKashyap Desai 		mrioc->reply_free_q_pool = NULL;
4274824a1566SKashyap Desai 	}
4275824a1566SKashyap Desai 
4276c9566231SKashyap Desai 	for (i = 0; i < mrioc->num_op_req_q; i++)
4277c9566231SKashyap Desai 		mpi3mr_free_op_req_q_segments(mrioc, i);
4278c9566231SKashyap Desai 
4279c9566231SKashyap Desai 	for (i = 0; i < mrioc->num_op_reply_q; i++)
4280c9566231SKashyap Desai 		mpi3mr_free_op_reply_q_segments(mrioc, i);
4281c9566231SKashyap Desai 
4282824a1566SKashyap Desai 	for (i = 0; i < mrioc->intr_info_count; i++) {
4283824a1566SKashyap Desai 		intr_info = mrioc->intr_info + i;
4284824a1566SKashyap Desai 		intr_info->op_reply_q = NULL;
4285824a1566SKashyap Desai 	}
4286824a1566SKashyap Desai 
4287824a1566SKashyap Desai 	kfree(mrioc->req_qinfo);
4288824a1566SKashyap Desai 	mrioc->req_qinfo = NULL;
4289824a1566SKashyap Desai 	mrioc->num_op_req_q = 0;
4290824a1566SKashyap Desai 
4291824a1566SKashyap Desai 	kfree(mrioc->op_reply_qinfo);
4292824a1566SKashyap Desai 	mrioc->op_reply_qinfo = NULL;
4293824a1566SKashyap Desai 	mrioc->num_op_reply_q = 0;
4294824a1566SKashyap Desai 
4295824a1566SKashyap Desai 	kfree(mrioc->init_cmds.reply);
4296824a1566SKashyap Desai 	mrioc->init_cmds.reply = NULL;
4297824a1566SKashyap Desai 
4298f5e6d5a3SSumit Saxena 	kfree(mrioc->bsg_cmds.reply);
4299f5e6d5a3SSumit Saxena 	mrioc->bsg_cmds.reply = NULL;
4300f5e6d5a3SSumit Saxena 
4301e844adb1SKashyap Desai 	kfree(mrioc->host_tm_cmds.reply);
4302e844adb1SKashyap Desai 	mrioc->host_tm_cmds.reply = NULL;
4303e844adb1SKashyap Desai 
430443ca1100SSumit Saxena 	kfree(mrioc->pel_cmds.reply);
430543ca1100SSumit Saxena 	mrioc->pel_cmds.reply = NULL;
430643ca1100SSumit Saxena 
430743ca1100SSumit Saxena 	kfree(mrioc->pel_abort_cmd.reply);
430843ca1100SSumit Saxena 	mrioc->pel_abort_cmd.reply = NULL;
430943ca1100SSumit Saxena 
4310c1af985dSSreekanth Reddy 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
4311c1af985dSSreekanth Reddy 		kfree(mrioc->evtack_cmds[i].reply);
4312c1af985dSSreekanth Reddy 		mrioc->evtack_cmds[i].reply = NULL;
4313c1af985dSSreekanth Reddy 	}
4314c1af985dSSreekanth Reddy 
4315339e6156SShin'ichiro Kawasaki 	bitmap_free(mrioc->removepend_bitmap);
4316e844adb1SKashyap Desai 	mrioc->removepend_bitmap = NULL;
4317e844adb1SKashyap Desai 
4318339e6156SShin'ichiro Kawasaki 	bitmap_free(mrioc->devrem_bitmap);
4319e844adb1SKashyap Desai 	mrioc->devrem_bitmap = NULL;
4320e844adb1SKashyap Desai 
4321339e6156SShin'ichiro Kawasaki 	bitmap_free(mrioc->evtack_cmds_bitmap);
4322c1af985dSSreekanth Reddy 	mrioc->evtack_cmds_bitmap = NULL;
4323c1af985dSSreekanth Reddy 
4324339e6156SShin'ichiro Kawasaki 	bitmap_free(mrioc->chain_bitmap);
4325824a1566SKashyap Desai 	mrioc->chain_bitmap = NULL;
4326824a1566SKashyap Desai 
43272bd37e28SSreekanth Reddy 	kfree(mrioc->transport_cmds.reply);
43282bd37e28SSreekanth Reddy 	mrioc->transport_cmds.reply = NULL;
43292bd37e28SSreekanth Reddy 
433013ef29eaSKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
433113ef29eaSKashyap Desai 		kfree(mrioc->dev_rmhs_cmds[i].reply);
433213ef29eaSKashyap Desai 		mrioc->dev_rmhs_cmds[i].reply = NULL;
433313ef29eaSKashyap Desai 	}
433413ef29eaSKashyap Desai 
4335824a1566SKashyap Desai 	if (mrioc->chain_buf_pool) {
4336824a1566SKashyap Desai 		for (i = 0; i < mrioc->chain_buf_count; i++) {
4337824a1566SKashyap Desai 			if (mrioc->chain_sgl_list[i].addr) {
4338824a1566SKashyap Desai 				dma_pool_free(mrioc->chain_buf_pool,
4339824a1566SKashyap Desai 				    mrioc->chain_sgl_list[i].addr,
4340824a1566SKashyap Desai 				    mrioc->chain_sgl_list[i].dma_addr);
4341824a1566SKashyap Desai 				mrioc->chain_sgl_list[i].addr = NULL;
4342824a1566SKashyap Desai 			}
4343824a1566SKashyap Desai 		}
4344824a1566SKashyap Desai 		dma_pool_destroy(mrioc->chain_buf_pool);
4345824a1566SKashyap Desai 		mrioc->chain_buf_pool = NULL;
4346824a1566SKashyap Desai 	}
4347824a1566SKashyap Desai 
4348824a1566SKashyap Desai 	kfree(mrioc->chain_sgl_list);
4349824a1566SKashyap Desai 	mrioc->chain_sgl_list = NULL;
4350824a1566SKashyap Desai 
4351824a1566SKashyap Desai 	if (mrioc->admin_reply_base) {
4352824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_reply_q_sz,
4353824a1566SKashyap Desai 		    mrioc->admin_reply_base, mrioc->admin_reply_dma);
4354824a1566SKashyap Desai 		mrioc->admin_reply_base = NULL;
4355824a1566SKashyap Desai 	}
4356824a1566SKashyap Desai 	if (mrioc->admin_req_base) {
4357824a1566SKashyap Desai 		dma_free_coherent(&mrioc->pdev->dev, mrioc->admin_req_q_sz,
4358824a1566SKashyap Desai 		    mrioc->admin_req_base, mrioc->admin_req_dma);
4359824a1566SKashyap Desai 		mrioc->admin_req_base = NULL;
4360824a1566SKashyap Desai 	}
436143ca1100SSumit Saxena 
436243ca1100SSumit Saxena 	if (mrioc->pel_seqnum_virt) {
436343ca1100SSumit Saxena 		dma_free_coherent(&mrioc->pdev->dev, mrioc->pel_seqnum_sz,
436443ca1100SSumit Saxena 		    mrioc->pel_seqnum_virt, mrioc->pel_seqnum_dma);
436543ca1100SSumit Saxena 		mrioc->pel_seqnum_virt = NULL;
436643ca1100SSumit Saxena 	}
436743ca1100SSumit Saxena 
436843ca1100SSumit Saxena 	kfree(mrioc->logdata_buf);
436943ca1100SSumit Saxena 	mrioc->logdata_buf = NULL;
437043ca1100SSumit Saxena 
4371824a1566SKashyap Desai }
4372824a1566SKashyap Desai 
4373824a1566SKashyap Desai /**
4374824a1566SKashyap Desai  * mpi3mr_issue_ioc_shutdown - shutdown controller
4375824a1566SKashyap Desai  * @mrioc: Adapter instance reference
4376824a1566SKashyap Desai  *
4377824a1566SKashyap Desai  * Send shutodwn notification to the controller and wait for the
4378824a1566SKashyap Desai  * shutdown_timeout for it to be completed.
4379824a1566SKashyap Desai  *
4380824a1566SKashyap Desai  * Return: Nothing.
4381824a1566SKashyap Desai  */
4382824a1566SKashyap Desai static void mpi3mr_issue_ioc_shutdown(struct mpi3mr_ioc *mrioc)
4383824a1566SKashyap Desai {
4384824a1566SKashyap Desai 	u32 ioc_config, ioc_status;
4385824a1566SKashyap Desai 	u8 retval = 1;
4386824a1566SKashyap Desai 	u32 timeout = MPI3MR_DEFAULT_SHUTDOWN_TIME * 10;
4387824a1566SKashyap Desai 
4388824a1566SKashyap Desai 	ioc_info(mrioc, "Issuing shutdown Notification\n");
4389824a1566SKashyap Desai 	if (mrioc->unrecoverable) {
4390824a1566SKashyap Desai 		ioc_warn(mrioc,
4391824a1566SKashyap Desai 		    "IOC is unrecoverable shutdown is not issued\n");
4392824a1566SKashyap Desai 		return;
4393824a1566SKashyap Desai 	}
4394824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4395824a1566SKashyap Desai 	if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
4396824a1566SKashyap Desai 	    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS) {
4397824a1566SKashyap Desai 		ioc_info(mrioc, "shutdown already in progress\n");
4398824a1566SKashyap Desai 		return;
4399824a1566SKashyap Desai 	}
4400824a1566SKashyap Desai 
4401824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
4402824a1566SKashyap Desai 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_SHUTDOWN_NORMAL;
4403ec5ebd2cSSreekanth Reddy 	ioc_config |= MPI3_SYSIF_IOC_CONFIG_DEVICE_SHUTDOWN_SEND_REQ;
4404824a1566SKashyap Desai 
4405824a1566SKashyap Desai 	writel(ioc_config, &mrioc->sysif_regs->ioc_configuration);
4406824a1566SKashyap Desai 
4407824a1566SKashyap Desai 	if (mrioc->facts.shutdown_timeout)
4408824a1566SKashyap Desai 		timeout = mrioc->facts.shutdown_timeout * 10;
4409824a1566SKashyap Desai 
4410824a1566SKashyap Desai 	do {
4411824a1566SKashyap Desai 		ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4412824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
4413824a1566SKashyap Desai 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_COMPLETE) {
4414824a1566SKashyap Desai 			retval = 0;
4415824a1566SKashyap Desai 			break;
4416824a1566SKashyap Desai 		}
4417824a1566SKashyap Desai 		msleep(100);
4418824a1566SKashyap Desai 	} while (--timeout);
4419824a1566SKashyap Desai 
4420824a1566SKashyap Desai 	ioc_status = readl(&mrioc->sysif_regs->ioc_status);
4421824a1566SKashyap Desai 	ioc_config = readl(&mrioc->sysif_regs->ioc_configuration);
4422824a1566SKashyap Desai 
4423824a1566SKashyap Desai 	if (retval) {
4424824a1566SKashyap Desai 		if ((ioc_status & MPI3_SYSIF_IOC_STATUS_SHUTDOWN_MASK)
4425824a1566SKashyap Desai 		    == MPI3_SYSIF_IOC_STATUS_SHUTDOWN_IN_PROGRESS)
4426824a1566SKashyap Desai 			ioc_warn(mrioc,
4427824a1566SKashyap Desai 			    "shutdown still in progress after timeout\n");
4428824a1566SKashyap Desai 	}
4429824a1566SKashyap Desai 
4430824a1566SKashyap Desai 	ioc_info(mrioc,
4431824a1566SKashyap Desai 	    "Base IOC Sts/Config after %s shutdown is (0x%x)/(0x%x)\n",
4432824a1566SKashyap Desai 	    (!retval) ? "successful" : "failed", ioc_status,
4433824a1566SKashyap Desai 	    ioc_config);
4434824a1566SKashyap Desai }
4435824a1566SKashyap Desai 
4436824a1566SKashyap Desai /**
4437824a1566SKashyap Desai  * mpi3mr_cleanup_ioc - Cleanup controller
4438824a1566SKashyap Desai  * @mrioc: Adapter instance reference
44393bb3c24eSYang Li  *
4440824a1566SKashyap Desai  * controller cleanup handler, Message unit reset or soft reset
4441fe6db615SSreekanth Reddy  * and shutdown notification is issued to the controller.
4442824a1566SKashyap Desai  *
4443824a1566SKashyap Desai  * Return: Nothing.
4444824a1566SKashyap Desai  */
4445fe6db615SSreekanth Reddy void mpi3mr_cleanup_ioc(struct mpi3mr_ioc *mrioc)
4446824a1566SKashyap Desai {
4447824a1566SKashyap Desai 	enum mpi3mr_iocstate ioc_state;
4448824a1566SKashyap Desai 
4449fe6db615SSreekanth Reddy 	dprint_exit(mrioc, "cleaning up the controller\n");
4450824a1566SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
4451824a1566SKashyap Desai 
4452824a1566SKashyap Desai 	ioc_state = mpi3mr_get_iocstate(mrioc);
4453824a1566SKashyap Desai 
4454824a1566SKashyap Desai 	if ((!mrioc->unrecoverable) && (!mrioc->reset_in_progress) &&
4455824a1566SKashyap Desai 	    (ioc_state == MRIOC_STATE_READY)) {
4456824a1566SKashyap Desai 		if (mpi3mr_issue_and_process_mur(mrioc,
4457824a1566SKashyap Desai 		    MPI3MR_RESET_FROM_CTLR_CLEANUP))
4458824a1566SKashyap Desai 			mpi3mr_issue_reset(mrioc,
4459824a1566SKashyap Desai 			    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET,
4460824a1566SKashyap Desai 			    MPI3MR_RESET_FROM_MUR_FAILURE);
4461824a1566SKashyap Desai 		mpi3mr_issue_ioc_shutdown(mrioc);
4462824a1566SKashyap Desai 	}
4463fe6db615SSreekanth Reddy 	dprint_exit(mrioc, "controller cleanup completed\n");
4464fb9b0457SKashyap Desai }
4465fb9b0457SKashyap Desai 
4466fb9b0457SKashyap Desai /**
4467fb9b0457SKashyap Desai  * mpi3mr_drv_cmd_comp_reset - Flush a internal driver command
4468fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4469fb9b0457SKashyap Desai  * @cmdptr: Internal command tracker
4470fb9b0457SKashyap Desai  *
4471fb9b0457SKashyap Desai  * Complete an internal driver commands with state indicating it
4472fb9b0457SKashyap Desai  * is completed due to reset.
4473fb9b0457SKashyap Desai  *
4474fb9b0457SKashyap Desai  * Return: Nothing.
4475fb9b0457SKashyap Desai  */
4476fb9b0457SKashyap Desai static inline void mpi3mr_drv_cmd_comp_reset(struct mpi3mr_ioc *mrioc,
4477fb9b0457SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr)
4478fb9b0457SKashyap Desai {
4479fb9b0457SKashyap Desai 	if (cmdptr->state & MPI3MR_CMD_PENDING) {
4480fb9b0457SKashyap Desai 		cmdptr->state |= MPI3MR_CMD_RESET;
4481fb9b0457SKashyap Desai 		cmdptr->state &= ~MPI3MR_CMD_PENDING;
4482fb9b0457SKashyap Desai 		if (cmdptr->is_waiting) {
4483fb9b0457SKashyap Desai 			complete(&cmdptr->done);
4484fb9b0457SKashyap Desai 			cmdptr->is_waiting = 0;
4485fb9b0457SKashyap Desai 		} else if (cmdptr->callback)
4486fb9b0457SKashyap Desai 			cmdptr->callback(mrioc, cmdptr);
4487fb9b0457SKashyap Desai 	}
4488fb9b0457SKashyap Desai }
4489fb9b0457SKashyap Desai 
4490fb9b0457SKashyap Desai /**
4491fb9b0457SKashyap Desai  * mpi3mr_flush_drv_cmds - Flush internaldriver commands
4492fb9b0457SKashyap Desai  * @mrioc: Adapter instance reference
4493fb9b0457SKashyap Desai  *
4494fb9b0457SKashyap Desai  * Flush all internal driver commands post reset
4495fb9b0457SKashyap Desai  *
4496fb9b0457SKashyap Desai  * Return: Nothing.
4497fb9b0457SKashyap Desai  */
4498f2a79d20SSreekanth Reddy void mpi3mr_flush_drv_cmds(struct mpi3mr_ioc *mrioc)
4499fb9b0457SKashyap Desai {
4500fb9b0457SKashyap Desai 	struct mpi3mr_drv_cmd *cmdptr;
4501fb9b0457SKashyap Desai 	u8 i;
4502fb9b0457SKashyap Desai 
4503fb9b0457SKashyap Desai 	cmdptr = &mrioc->init_cmds;
4504fb9b0457SKashyap Desai 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
450532d457d5SSreekanth Reddy 
450632d457d5SSreekanth Reddy 	cmdptr = &mrioc->cfg_cmds;
450732d457d5SSreekanth Reddy 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
450832d457d5SSreekanth Reddy 
4509f5e6d5a3SSumit Saxena 	cmdptr = &mrioc->bsg_cmds;
4510f5e6d5a3SSumit Saxena 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4511e844adb1SKashyap Desai 	cmdptr = &mrioc->host_tm_cmds;
4512e844adb1SKashyap Desai 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4513fb9b0457SKashyap Desai 
4514fb9b0457SKashyap Desai 	for (i = 0; i < MPI3MR_NUM_DEVRMCMD; i++) {
4515fb9b0457SKashyap Desai 		cmdptr = &mrioc->dev_rmhs_cmds[i];
4516fb9b0457SKashyap Desai 		mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4517fb9b0457SKashyap Desai 	}
4518c1af985dSSreekanth Reddy 
4519c1af985dSSreekanth Reddy 	for (i = 0; i < MPI3MR_NUM_EVTACKCMD; i++) {
4520c1af985dSSreekanth Reddy 		cmdptr = &mrioc->evtack_cmds[i];
4521c1af985dSSreekanth Reddy 		mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
4522c1af985dSSreekanth Reddy 	}
452343ca1100SSumit Saxena 
452443ca1100SSumit Saxena 	cmdptr = &mrioc->pel_cmds;
452543ca1100SSumit Saxena 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
452643ca1100SSumit Saxena 
452743ca1100SSumit Saxena 	cmdptr = &mrioc->pel_abort_cmd;
452843ca1100SSumit Saxena 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
452943ca1100SSumit Saxena 
45302bd37e28SSreekanth Reddy 	cmdptr = &mrioc->transport_cmds;
45312bd37e28SSreekanth Reddy 	mpi3mr_drv_cmd_comp_reset(mrioc, cmdptr);
453243ca1100SSumit Saxena }
453343ca1100SSumit Saxena 
453443ca1100SSumit Saxena /**
453543ca1100SSumit Saxena  * mpi3mr_pel_wait_post - Issue PEL Wait
453643ca1100SSumit Saxena  * @mrioc: Adapter instance reference
453743ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
453843ca1100SSumit Saxena  *
453943ca1100SSumit Saxena  * Issue PEL Wait MPI request through admin queue and return.
454043ca1100SSumit Saxena  *
454143ca1100SSumit Saxena  * Return: Nothing.
454243ca1100SSumit Saxena  */
454343ca1100SSumit Saxena static void mpi3mr_pel_wait_post(struct mpi3mr_ioc *mrioc,
454443ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
454543ca1100SSumit Saxena {
454643ca1100SSumit Saxena 	struct mpi3_pel_req_action_wait pel_wait;
454743ca1100SSumit Saxena 
454843ca1100SSumit Saxena 	mrioc->pel_abort_requested = false;
454943ca1100SSumit Saxena 
455043ca1100SSumit Saxena 	memset(&pel_wait, 0, sizeof(pel_wait));
455143ca1100SSumit Saxena 	drv_cmd->state = MPI3MR_CMD_PENDING;
455243ca1100SSumit Saxena 	drv_cmd->is_waiting = 0;
455343ca1100SSumit Saxena 	drv_cmd->callback = mpi3mr_pel_wait_complete;
455443ca1100SSumit Saxena 	drv_cmd->ioc_status = 0;
455543ca1100SSumit Saxena 	drv_cmd->ioc_loginfo = 0;
455643ca1100SSumit Saxena 	pel_wait.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT);
455743ca1100SSumit Saxena 	pel_wait.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG;
455843ca1100SSumit Saxena 	pel_wait.action = MPI3_PEL_ACTION_WAIT;
455943ca1100SSumit Saxena 	pel_wait.starting_sequence_number = cpu_to_le32(mrioc->pel_newest_seqnum);
456043ca1100SSumit Saxena 	pel_wait.locale = cpu_to_le16(mrioc->pel_locale);
456143ca1100SSumit Saxena 	pel_wait.class = cpu_to_le16(mrioc->pel_class);
456243ca1100SSumit Saxena 	pel_wait.wait_time = MPI3_PEL_WAITTIME_INFINITE_WAIT;
456343ca1100SSumit Saxena 	dprint_bsg_info(mrioc, "sending pel_wait seqnum(%d), class(%d), locale(0x%08x)\n",
456443ca1100SSumit Saxena 	    mrioc->pel_newest_seqnum, mrioc->pel_class, mrioc->pel_locale);
456543ca1100SSumit Saxena 
456643ca1100SSumit Saxena 	if (mpi3mr_admin_request_post(mrioc, &pel_wait, sizeof(pel_wait), 0)) {
456743ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
456843ca1100SSumit Saxena 			    "Issuing PELWait: Admin post failed\n");
456943ca1100SSumit Saxena 		drv_cmd->state = MPI3MR_CMD_NOTUSED;
457043ca1100SSumit Saxena 		drv_cmd->callback = NULL;
457143ca1100SSumit Saxena 		drv_cmd->retry_count = 0;
457243ca1100SSumit Saxena 		mrioc->pel_enabled = false;
457343ca1100SSumit Saxena 	}
457443ca1100SSumit Saxena }
457543ca1100SSumit Saxena 
457643ca1100SSumit Saxena /**
457743ca1100SSumit Saxena  * mpi3mr_pel_get_seqnum_post - Issue PEL Get Sequence number
457843ca1100SSumit Saxena  * @mrioc: Adapter instance reference
457943ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
458043ca1100SSumit Saxena  *
458143ca1100SSumit Saxena  * Issue PEL get sequence number MPI request through admin queue
458243ca1100SSumit Saxena  * and return.
458343ca1100SSumit Saxena  *
458443ca1100SSumit Saxena  * Return: 0 on success, non-zero on failure.
458543ca1100SSumit Saxena  */
458643ca1100SSumit Saxena int mpi3mr_pel_get_seqnum_post(struct mpi3mr_ioc *mrioc,
458743ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
458843ca1100SSumit Saxena {
458943ca1100SSumit Saxena 	struct mpi3_pel_req_action_get_sequence_numbers pel_getseq_req;
459043ca1100SSumit Saxena 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
459143ca1100SSumit Saxena 	int retval = 0;
459243ca1100SSumit Saxena 
459343ca1100SSumit Saxena 	memset(&pel_getseq_req, 0, sizeof(pel_getseq_req));
459443ca1100SSumit Saxena 	mrioc->pel_cmds.state = MPI3MR_CMD_PENDING;
459543ca1100SSumit Saxena 	mrioc->pel_cmds.is_waiting = 0;
459643ca1100SSumit Saxena 	mrioc->pel_cmds.ioc_status = 0;
459743ca1100SSumit Saxena 	mrioc->pel_cmds.ioc_loginfo = 0;
459843ca1100SSumit Saxena 	mrioc->pel_cmds.callback = mpi3mr_pel_get_seqnum_complete;
459943ca1100SSumit Saxena 	pel_getseq_req.host_tag = cpu_to_le16(MPI3MR_HOSTTAG_PEL_WAIT);
460043ca1100SSumit Saxena 	pel_getseq_req.function = MPI3_FUNCTION_PERSISTENT_EVENT_LOG;
460143ca1100SSumit Saxena 	pel_getseq_req.action = MPI3_PEL_ACTION_GET_SEQNUM;
460243ca1100SSumit Saxena 	mpi3mr_add_sg_single(&pel_getseq_req.sgl, sgl_flags,
460343ca1100SSumit Saxena 	    mrioc->pel_seqnum_sz, mrioc->pel_seqnum_dma);
460443ca1100SSumit Saxena 
460543ca1100SSumit Saxena 	retval = mpi3mr_admin_request_post(mrioc, &pel_getseq_req,
460643ca1100SSumit Saxena 			sizeof(pel_getseq_req), 0);
460743ca1100SSumit Saxena 	if (retval) {
460843ca1100SSumit Saxena 		if (drv_cmd) {
460943ca1100SSumit Saxena 			drv_cmd->state = MPI3MR_CMD_NOTUSED;
461043ca1100SSumit Saxena 			drv_cmd->callback = NULL;
461143ca1100SSumit Saxena 			drv_cmd->retry_count = 0;
461243ca1100SSumit Saxena 		}
461343ca1100SSumit Saxena 		mrioc->pel_enabled = false;
461443ca1100SSumit Saxena 	}
461543ca1100SSumit Saxena 
461643ca1100SSumit Saxena 	return retval;
461743ca1100SSumit Saxena }
461843ca1100SSumit Saxena 
461943ca1100SSumit Saxena /**
462043ca1100SSumit Saxena  * mpi3mr_pel_wait_complete - PELWait Completion callback
462143ca1100SSumit Saxena  * @mrioc: Adapter instance reference
462243ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
462343ca1100SSumit Saxena  *
462443ca1100SSumit Saxena  * This is a callback handler for the PELWait request and
462543ca1100SSumit Saxena  * firmware completes a PELWait request when it is aborted or a
462643ca1100SSumit Saxena  * new PEL entry is available. This sends AEN to the application
462743ca1100SSumit Saxena  * and if the PELwait completion is not due to PELAbort then
462843ca1100SSumit Saxena  * this will send a request for new PEL Sequence number
462943ca1100SSumit Saxena  *
463043ca1100SSumit Saxena  * Return: Nothing.
463143ca1100SSumit Saxena  */
463243ca1100SSumit Saxena static void mpi3mr_pel_wait_complete(struct mpi3mr_ioc *mrioc,
463343ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
463443ca1100SSumit Saxena {
463543ca1100SSumit Saxena 	struct mpi3_pel_reply *pel_reply = NULL;
463643ca1100SSumit Saxena 	u16 ioc_status, pe_log_status;
463743ca1100SSumit Saxena 	bool do_retry = false;
463843ca1100SSumit Saxena 
463943ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_RESET)
464043ca1100SSumit Saxena 		goto cleanup_drv_cmd;
464143ca1100SSumit Saxena 
464243ca1100SSumit Saxena 	ioc_status = drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK;
464343ca1100SSumit Saxena 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
464443ca1100SSumit Saxena 		ioc_err(mrioc, "%s: Failed ioc_status(0x%04x) Loginfo(0x%08x)\n",
464543ca1100SSumit Saxena 			__func__, ioc_status, drv_cmd->ioc_loginfo);
464643ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
464743ca1100SSumit Saxena 		    "pel_wait: failed with ioc_status(0x%04x), log_info(0x%08x)\n",
464843ca1100SSumit Saxena 		    ioc_status, drv_cmd->ioc_loginfo);
464943ca1100SSumit Saxena 		do_retry = true;
465043ca1100SSumit Saxena 	}
465143ca1100SSumit Saxena 
465243ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
465343ca1100SSumit Saxena 		pel_reply = (struct mpi3_pel_reply *)drv_cmd->reply;
465443ca1100SSumit Saxena 
465543ca1100SSumit Saxena 	if (!pel_reply) {
465643ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
465743ca1100SSumit Saxena 		    "pel_wait: failed due to no reply\n");
465843ca1100SSumit Saxena 		goto out_failed;
465943ca1100SSumit Saxena 	}
466043ca1100SSumit Saxena 
466143ca1100SSumit Saxena 	pe_log_status = le16_to_cpu(pel_reply->pe_log_status);
466243ca1100SSumit Saxena 	if ((pe_log_status != MPI3_PEL_STATUS_SUCCESS) &&
466343ca1100SSumit Saxena 	    (pe_log_status != MPI3_PEL_STATUS_ABORTED)) {
466443ca1100SSumit Saxena 		ioc_err(mrioc, "%s: Failed pe_log_status(0x%04x)\n",
466543ca1100SSumit Saxena 			__func__, pe_log_status);
466643ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
466743ca1100SSumit Saxena 		    "pel_wait: failed due to pel_log_status(0x%04x)\n",
466843ca1100SSumit Saxena 		    pe_log_status);
466943ca1100SSumit Saxena 		do_retry = true;
467043ca1100SSumit Saxena 	}
467143ca1100SSumit Saxena 
467243ca1100SSumit Saxena 	if (do_retry) {
467343ca1100SSumit Saxena 		if (drv_cmd->retry_count < MPI3MR_PEL_RETRY_COUNT) {
467443ca1100SSumit Saxena 			drv_cmd->retry_count++;
467543ca1100SSumit Saxena 			dprint_bsg_err(mrioc, "pel_wait: retrying(%d)\n",
467643ca1100SSumit Saxena 			    drv_cmd->retry_count);
467743ca1100SSumit Saxena 			mpi3mr_pel_wait_post(mrioc, drv_cmd);
467843ca1100SSumit Saxena 			return;
467943ca1100SSumit Saxena 		}
468043ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
468143ca1100SSumit Saxena 		    "pel_wait: failed after all retries(%d)\n",
468243ca1100SSumit Saxena 		    drv_cmd->retry_count);
468343ca1100SSumit Saxena 		goto out_failed;
468443ca1100SSumit Saxena 	}
468543ca1100SSumit Saxena 	atomic64_inc(&event_counter);
468643ca1100SSumit Saxena 	if (!mrioc->pel_abort_requested) {
468743ca1100SSumit Saxena 		mrioc->pel_cmds.retry_count = 0;
468843ca1100SSumit Saxena 		mpi3mr_pel_get_seqnum_post(mrioc, &mrioc->pel_cmds);
468943ca1100SSumit Saxena 	}
469043ca1100SSumit Saxena 
469143ca1100SSumit Saxena 	return;
469243ca1100SSumit Saxena out_failed:
469343ca1100SSumit Saxena 	mrioc->pel_enabled = false;
469443ca1100SSumit Saxena cleanup_drv_cmd:
469543ca1100SSumit Saxena 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
469643ca1100SSumit Saxena 	drv_cmd->callback = NULL;
469743ca1100SSumit Saxena 	drv_cmd->retry_count = 0;
469843ca1100SSumit Saxena }
469943ca1100SSumit Saxena 
470043ca1100SSumit Saxena /**
470143ca1100SSumit Saxena  * mpi3mr_pel_get_seqnum_complete - PELGetSeqNum Completion callback
470243ca1100SSumit Saxena  * @mrioc: Adapter instance reference
470343ca1100SSumit Saxena  * @drv_cmd: Internal command tracker
470443ca1100SSumit Saxena  *
470543ca1100SSumit Saxena  * This is a callback handler for the PEL get sequence number
470643ca1100SSumit Saxena  * request and a new PEL wait request will be issued to the
470743ca1100SSumit Saxena  * firmware from this
470843ca1100SSumit Saxena  *
470943ca1100SSumit Saxena  * Return: Nothing.
471043ca1100SSumit Saxena  */
471143ca1100SSumit Saxena void mpi3mr_pel_get_seqnum_complete(struct mpi3mr_ioc *mrioc,
471243ca1100SSumit Saxena 	struct mpi3mr_drv_cmd *drv_cmd)
471343ca1100SSumit Saxena {
471443ca1100SSumit Saxena 	struct mpi3_pel_reply *pel_reply = NULL;
471543ca1100SSumit Saxena 	struct mpi3_pel_seq *pel_seqnum_virt;
471643ca1100SSumit Saxena 	u16 ioc_status;
471743ca1100SSumit Saxena 	bool do_retry = false;
471843ca1100SSumit Saxena 
471943ca1100SSumit Saxena 	pel_seqnum_virt = (struct mpi3_pel_seq *)mrioc->pel_seqnum_virt;
472043ca1100SSumit Saxena 
472143ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_RESET)
472243ca1100SSumit Saxena 		goto cleanup_drv_cmd;
472343ca1100SSumit Saxena 
472443ca1100SSumit Saxena 	ioc_status = drv_cmd->ioc_status & MPI3_IOCSTATUS_STATUS_MASK;
472543ca1100SSumit Saxena 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
472643ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
472743ca1100SSumit Saxena 		    "pel_get_seqnum: failed with ioc_status(0x%04x), log_info(0x%08x)\n",
472843ca1100SSumit Saxena 		    ioc_status, drv_cmd->ioc_loginfo);
472943ca1100SSumit Saxena 		do_retry = true;
473043ca1100SSumit Saxena 	}
473143ca1100SSumit Saxena 
473243ca1100SSumit Saxena 	if (drv_cmd->state & MPI3MR_CMD_REPLY_VALID)
473343ca1100SSumit Saxena 		pel_reply = (struct mpi3_pel_reply *)drv_cmd->reply;
473443ca1100SSumit Saxena 	if (!pel_reply) {
473543ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
473643ca1100SSumit Saxena 		    "pel_get_seqnum: failed due to no reply\n");
473743ca1100SSumit Saxena 		goto out_failed;
473843ca1100SSumit Saxena 	}
473943ca1100SSumit Saxena 
474043ca1100SSumit Saxena 	if (le16_to_cpu(pel_reply->pe_log_status) != MPI3_PEL_STATUS_SUCCESS) {
474143ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
474243ca1100SSumit Saxena 		    "pel_get_seqnum: failed due to pel_log_status(0x%04x)\n",
474343ca1100SSumit Saxena 		    le16_to_cpu(pel_reply->pe_log_status));
474443ca1100SSumit Saxena 		do_retry = true;
474543ca1100SSumit Saxena 	}
474643ca1100SSumit Saxena 
474743ca1100SSumit Saxena 	if (do_retry) {
474843ca1100SSumit Saxena 		if (drv_cmd->retry_count < MPI3MR_PEL_RETRY_COUNT) {
474943ca1100SSumit Saxena 			drv_cmd->retry_count++;
475043ca1100SSumit Saxena 			dprint_bsg_err(mrioc,
475143ca1100SSumit Saxena 			    "pel_get_seqnum: retrying(%d)\n",
475243ca1100SSumit Saxena 			    drv_cmd->retry_count);
475343ca1100SSumit Saxena 			mpi3mr_pel_get_seqnum_post(mrioc, drv_cmd);
475443ca1100SSumit Saxena 			return;
475543ca1100SSumit Saxena 		}
475643ca1100SSumit Saxena 
475743ca1100SSumit Saxena 		dprint_bsg_err(mrioc,
475843ca1100SSumit Saxena 		    "pel_get_seqnum: failed after all retries(%d)\n",
475943ca1100SSumit Saxena 		    drv_cmd->retry_count);
476043ca1100SSumit Saxena 		goto out_failed;
476143ca1100SSumit Saxena 	}
476243ca1100SSumit Saxena 	mrioc->pel_newest_seqnum = le32_to_cpu(pel_seqnum_virt->newest) + 1;
476343ca1100SSumit Saxena 	drv_cmd->retry_count = 0;
476443ca1100SSumit Saxena 	mpi3mr_pel_wait_post(mrioc, drv_cmd);
476543ca1100SSumit Saxena 
476643ca1100SSumit Saxena 	return;
476743ca1100SSumit Saxena out_failed:
476843ca1100SSumit Saxena 	mrioc->pel_enabled = false;
476943ca1100SSumit Saxena cleanup_drv_cmd:
477043ca1100SSumit Saxena 	drv_cmd->state = MPI3MR_CMD_NOTUSED;
477143ca1100SSumit Saxena 	drv_cmd->callback = NULL;
477243ca1100SSumit Saxena 	drv_cmd->retry_count = 0;
4773fb9b0457SKashyap Desai }
4774fb9b0457SKashyap Desai 
4775fb9b0457SKashyap Desai /**
4776824a1566SKashyap Desai  * mpi3mr_soft_reset_handler - Reset the controller
4777824a1566SKashyap Desai  * @mrioc: Adapter instance reference
4778824a1566SKashyap Desai  * @reset_reason: Reset reason code
4779824a1566SKashyap Desai  * @snapdump: Flag to generate snapdump in firmware or not
4780824a1566SKashyap Desai  *
4781fb9b0457SKashyap Desai  * This is an handler for recovering controller by issuing soft
4782fb9b0457SKashyap Desai  * reset are diag fault reset.  This is a blocking function and
4783fb9b0457SKashyap Desai  * when one reset is executed if any other resets they will be
4784f5e6d5a3SSumit Saxena  * blocked. All BSG requests will be blocked during the reset. If
4785fb9b0457SKashyap Desai  * controller reset is successful then the controller will be
4786fb9b0457SKashyap Desai  * reinitalized, otherwise the controller will be marked as not
4787fb9b0457SKashyap Desai  * recoverable
4788fb9b0457SKashyap Desai  *
4789fb9b0457SKashyap Desai  * In snapdump bit is set, the controller is issued with diag
4790fb9b0457SKashyap Desai  * fault reset so that the firmware can create a snap dump and
4791fb9b0457SKashyap Desai  * post that the firmware will result in F000 fault and the
4792fb9b0457SKashyap Desai  * driver will issue soft reset to recover from that.
4793824a1566SKashyap Desai  *
4794824a1566SKashyap Desai  * Return: 0 on success, non-zero on failure.
4795824a1566SKashyap Desai  */
4796824a1566SKashyap Desai int mpi3mr_soft_reset_handler(struct mpi3mr_ioc *mrioc,
4797824a1566SKashyap Desai 	u32 reset_reason, u8 snapdump)
4798824a1566SKashyap Desai {
4799fb9b0457SKashyap Desai 	int retval = 0, i;
4800fb9b0457SKashyap Desai 	unsigned long flags;
4801fb9b0457SKashyap Desai 	u32 host_diagnostic, timeout = MPI3_SYSIF_DIAG_SAVE_TIMEOUT * 10;
4802fb9b0457SKashyap Desai 
4803b64845a7SSreekanth Reddy 	/* Block the reset handler until diag save in progress*/
4804b64845a7SSreekanth Reddy 	dprint_reset(mrioc,
4805b64845a7SSreekanth Reddy 	    "soft_reset_handler: check and block on diagsave_timeout(%d)\n",
4806b64845a7SSreekanth Reddy 	    mrioc->diagsave_timeout);
4807b64845a7SSreekanth Reddy 	while (mrioc->diagsave_timeout)
4808b64845a7SSreekanth Reddy 		ssleep(1);
4809fb9b0457SKashyap Desai 	/*
4810fb9b0457SKashyap Desai 	 * Block new resets until the currently executing one is finished and
4811fb9b0457SKashyap Desai 	 * return the status of the existing reset for all blocked resets
4812fb9b0457SKashyap Desai 	 */
4813b64845a7SSreekanth Reddy 	dprint_reset(mrioc, "soft_reset_handler: acquiring reset_mutex\n");
4814fb9b0457SKashyap Desai 	if (!mutex_trylock(&mrioc->reset_mutex)) {
4815b64845a7SSreekanth Reddy 		ioc_info(mrioc,
4816b64845a7SSreekanth Reddy 		    "controller reset triggered by %s is blocked due to another reset in progress\n",
4817b64845a7SSreekanth Reddy 		    mpi3mr_reset_rc_name(reset_reason));
4818b64845a7SSreekanth Reddy 		do {
4819b64845a7SSreekanth Reddy 			ssleep(1);
4820b64845a7SSreekanth Reddy 		} while (mrioc->reset_in_progress == 1);
4821b64845a7SSreekanth Reddy 		ioc_info(mrioc,
4822b64845a7SSreekanth Reddy 		    "returning previous reset result(%d) for the reset triggered by %s\n",
4823b64845a7SSreekanth Reddy 		    mrioc->prev_reset_result,
4824b64845a7SSreekanth Reddy 		    mpi3mr_reset_rc_name(reset_reason));
4825b64845a7SSreekanth Reddy 		return mrioc->prev_reset_result;
4826fb9b0457SKashyap Desai 	}
4827b64845a7SSreekanth Reddy 	ioc_info(mrioc, "controller reset is triggered by %s\n",
4828b64845a7SSreekanth Reddy 	    mpi3mr_reset_rc_name(reset_reason));
4829b64845a7SSreekanth Reddy 
48302745ce0eSSreekanth Reddy 	mrioc->device_refresh_on = 0;
4831fb9b0457SKashyap Desai 	mrioc->reset_in_progress = 1;
4832f5e6d5a3SSumit Saxena 	mrioc->stop_bsgs = 1;
4833b64845a7SSreekanth Reddy 	mrioc->prev_reset_result = -1;
4834fb9b0457SKashyap Desai 
4835fb9b0457SKashyap Desai 	if ((!snapdump) && (reset_reason != MPI3MR_RESET_FROM_FAULT_WATCH) &&
4836b64845a7SSreekanth Reddy 	    (reset_reason != MPI3MR_RESET_FROM_FIRMWARE) &&
4837fb9b0457SKashyap Desai 	    (reset_reason != MPI3MR_RESET_FROM_CIACTIV_FAULT)) {
4838fb9b0457SKashyap Desai 		for (i = 0; i < MPI3_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4839fb9b0457SKashyap Desai 			mrioc->event_masks[i] = -1;
4840fb9b0457SKashyap Desai 
4841b64845a7SSreekanth Reddy 		dprint_reset(mrioc, "soft_reset_handler: masking events\n");
4842b64845a7SSreekanth Reddy 		mpi3mr_issue_event_notification(mrioc);
4843fb9b0457SKashyap Desai 	}
4844fb9b0457SKashyap Desai 
484544dc724fSKashyap Desai 	mpi3mr_wait_for_host_io(mrioc, MPI3MR_RESET_HOST_IOWAIT_TIMEOUT);
484644dc724fSKashyap Desai 
4847fb9b0457SKashyap Desai 	mpi3mr_ioc_disable_intr(mrioc);
4848fb9b0457SKashyap Desai 
4849fb9b0457SKashyap Desai 	if (snapdump) {
4850fb9b0457SKashyap Desai 		mpi3mr_set_diagsave(mrioc);
4851fb9b0457SKashyap Desai 		retval = mpi3mr_issue_reset(mrioc,
4852fb9b0457SKashyap Desai 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
4853fb9b0457SKashyap Desai 		if (!retval) {
4854fb9b0457SKashyap Desai 			do {
4855fb9b0457SKashyap Desai 				host_diagnostic =
4856fb9b0457SKashyap Desai 				    readl(&mrioc->sysif_regs->host_diagnostic);
4857fb9b0457SKashyap Desai 				if (!(host_diagnostic &
4858fb9b0457SKashyap Desai 				    MPI3_SYSIF_HOST_DIAG_SAVE_IN_PROGRESS))
4859fb9b0457SKashyap Desai 					break;
4860fb9b0457SKashyap Desai 				msleep(100);
4861fb9b0457SKashyap Desai 			} while (--timeout);
4862fb9b0457SKashyap Desai 		}
4863fb9b0457SKashyap Desai 	}
4864fb9b0457SKashyap Desai 
4865fb9b0457SKashyap Desai 	retval = mpi3mr_issue_reset(mrioc,
4866fb9b0457SKashyap Desai 	    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_SOFT_RESET, reset_reason);
4867fb9b0457SKashyap Desai 	if (retval) {
4868fb9b0457SKashyap Desai 		ioc_err(mrioc, "Failed to issue soft reset to the ioc\n");
4869fb9b0457SKashyap Desai 		goto out;
4870fb9b0457SKashyap Desai 	}
4871f10af057SSreekanth Reddy 	if (mrioc->num_io_throttle_group !=
4872f10af057SSreekanth Reddy 	    mrioc->facts.max_io_throttle_group) {
4873f10af057SSreekanth Reddy 		ioc_err(mrioc,
4874f10af057SSreekanth Reddy 		    "max io throttle group doesn't match old(%d), new(%d)\n",
4875f10af057SSreekanth Reddy 		    mrioc->num_io_throttle_group,
4876f10af057SSreekanth Reddy 		    mrioc->facts.max_io_throttle_group);
48772a8a0147SDan Carpenter 		retval = -EPERM;
48782a8a0147SDan Carpenter 		goto out;
4879f10af057SSreekanth Reddy 	}
4880fb9b0457SKashyap Desai 
4881c1af985dSSreekanth Reddy 	mpi3mr_flush_delayed_cmd_lists(mrioc);
4882fb9b0457SKashyap Desai 	mpi3mr_flush_drv_cmds(mrioc);
4883339e6156SShin'ichiro Kawasaki 	bitmap_clear(mrioc->devrem_bitmap, 0, MPI3MR_NUM_DEVRMCMD);
4884339e6156SShin'ichiro Kawasaki 	bitmap_clear(mrioc->removepend_bitmap, 0,
4885339e6156SShin'ichiro Kawasaki 		     mrioc->dev_handle_bitmap_bits);
4886339e6156SShin'ichiro Kawasaki 	bitmap_clear(mrioc->evtack_cmds_bitmap, 0, MPI3MR_NUM_EVTACKCMD);
4887fb9b0457SKashyap Desai 	mpi3mr_flush_host_io(mrioc);
4888580e6742SSreekanth Reddy 	mpi3mr_cleanup_fwevt_list(mrioc);
4889fb9b0457SKashyap Desai 	mpi3mr_invalidate_devhandles(mrioc);
4890130fc180SSreekanth Reddy 	mpi3mr_free_enclosure_list(mrioc);
4891130fc180SSreekanth Reddy 
489278b76a07SSreekanth Reddy 	if (mrioc->prepare_for_reset) {
489378b76a07SSreekanth Reddy 		mrioc->prepare_for_reset = 0;
489478b76a07SSreekanth Reddy 		mrioc->prepare_for_reset_timeout_counter = 0;
489578b76a07SSreekanth Reddy 	}
4896fb9b0457SKashyap Desai 	mpi3mr_memset_buffers(mrioc);
4897fe6db615SSreekanth Reddy 	retval = mpi3mr_reinit_ioc(mrioc, 0);
4898fb9b0457SKashyap Desai 	if (retval) {
4899fb9b0457SKashyap Desai 		pr_err(IOCNAME "reinit after soft reset failed: reason %d\n",
4900fb9b0457SKashyap Desai 		    mrioc->name, reset_reason);
4901fb9b0457SKashyap Desai 		goto out;
4902fb9b0457SKashyap Desai 	}
4903f84e8b5bSSreekanth Reddy 	ssleep(MPI3MR_RESET_TOPOLOGY_SETTLE_TIME);
4904fb9b0457SKashyap Desai 
4905fb9b0457SKashyap Desai out:
4906fb9b0457SKashyap Desai 	if (!retval) {
4907b64845a7SSreekanth Reddy 		mrioc->diagsave_timeout = 0;
4908fb9b0457SKashyap Desai 		mrioc->reset_in_progress = 0;
490943ca1100SSumit Saxena 		mrioc->pel_abort_requested = 0;
491043ca1100SSumit Saxena 		if (mrioc->pel_enabled) {
491143ca1100SSumit Saxena 			mrioc->pel_cmds.retry_count = 0;
491243ca1100SSumit Saxena 			mpi3mr_pel_wait_post(mrioc, &mrioc->pel_cmds);
491343ca1100SSumit Saxena 		}
491443ca1100SSumit Saxena 
49152745ce0eSSreekanth Reddy 		mrioc->device_refresh_on = 0;
49162745ce0eSSreekanth Reddy 
491754dfcffbSKashyap Desai 		mrioc->ts_update_counter = 0;
4918fb9b0457SKashyap Desai 		spin_lock_irqsave(&mrioc->watchdog_lock, flags);
4919fb9b0457SKashyap Desai 		if (mrioc->watchdog_work_q)
4920fb9b0457SKashyap Desai 			queue_delayed_work(mrioc->watchdog_work_q,
4921fb9b0457SKashyap Desai 			    &mrioc->watchdog_work,
4922fb9b0457SKashyap Desai 			    msecs_to_jiffies(MPI3MR_WATCHDOG_INTERVAL));
4923fb9b0457SKashyap Desai 		spin_unlock_irqrestore(&mrioc->watchdog_lock, flags);
4924f5e6d5a3SSumit Saxena 		mrioc->stop_bsgs = 0;
492543ca1100SSumit Saxena 		if (mrioc->pel_enabled)
492643ca1100SSumit Saxena 			atomic64_inc(&event_counter);
4927fb9b0457SKashyap Desai 	} else {
4928fb9b0457SKashyap Desai 		mpi3mr_issue_reset(mrioc,
4929fb9b0457SKashyap Desai 		    MPI3_SYSIF_HOST_DIAG_RESET_ACTION_DIAG_FAULT, reset_reason);
49302745ce0eSSreekanth Reddy 		mrioc->device_refresh_on = 0;
4931fb9b0457SKashyap Desai 		mrioc->unrecoverable = 1;
4932fb9b0457SKashyap Desai 		mrioc->reset_in_progress = 0;
4933fb9b0457SKashyap Desai 		retval = -1;
4934f2a79d20SSreekanth Reddy 		mpi3mr_flush_cmds_for_unrecovered_controller(mrioc);
4935fb9b0457SKashyap Desai 	}
4936b64845a7SSreekanth Reddy 	mrioc->prev_reset_result = retval;
4937fb9b0457SKashyap Desai 	mutex_unlock(&mrioc->reset_mutex);
4938b64845a7SSreekanth Reddy 	ioc_info(mrioc, "controller reset is %s\n",
4939b64845a7SSreekanth Reddy 	    ((retval == 0) ? "successful" : "failed"));
4940fb9b0457SKashyap Desai 	return retval;
4941824a1566SKashyap Desai }
494232d457d5SSreekanth Reddy 
494332d457d5SSreekanth Reddy 
494432d457d5SSreekanth Reddy /**
494532d457d5SSreekanth Reddy  * mpi3mr_free_config_dma_memory - free memory for config page
494632d457d5SSreekanth Reddy  * @mrioc: Adapter instance reference
494732d457d5SSreekanth Reddy  * @mem_desc: memory descriptor structure
494832d457d5SSreekanth Reddy  *
494932d457d5SSreekanth Reddy  * Check whether the size of the buffer specified by the memory
495032d457d5SSreekanth Reddy  * descriptor is greater than the default page size if so then
495132d457d5SSreekanth Reddy  * free the memory pointed by the descriptor.
495232d457d5SSreekanth Reddy  *
495332d457d5SSreekanth Reddy  * Return: Nothing.
495432d457d5SSreekanth Reddy  */
495532d457d5SSreekanth Reddy static void mpi3mr_free_config_dma_memory(struct mpi3mr_ioc *mrioc,
495632d457d5SSreekanth Reddy 	struct dma_memory_desc *mem_desc)
495732d457d5SSreekanth Reddy {
495832d457d5SSreekanth Reddy 	if ((mem_desc->size > mrioc->cfg_page_sz) && mem_desc->addr) {
495932d457d5SSreekanth Reddy 		dma_free_coherent(&mrioc->pdev->dev, mem_desc->size,
496032d457d5SSreekanth Reddy 		    mem_desc->addr, mem_desc->dma_addr);
496132d457d5SSreekanth Reddy 		mem_desc->addr = NULL;
496232d457d5SSreekanth Reddy 	}
496332d457d5SSreekanth Reddy }
496432d457d5SSreekanth Reddy 
496532d457d5SSreekanth Reddy /**
496632d457d5SSreekanth Reddy  * mpi3mr_alloc_config_dma_memory - Alloc memory for config page
496732d457d5SSreekanth Reddy  * @mrioc: Adapter instance reference
496832d457d5SSreekanth Reddy  * @mem_desc: Memory descriptor to hold dma memory info
496932d457d5SSreekanth Reddy  *
497032d457d5SSreekanth Reddy  * This function allocates new dmaable memory or provides the
497132d457d5SSreekanth Reddy  * default config page dmaable memory based on the memory size
497232d457d5SSreekanth Reddy  * described by the descriptor.
497332d457d5SSreekanth Reddy  *
497432d457d5SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
497532d457d5SSreekanth Reddy  */
497632d457d5SSreekanth Reddy static int mpi3mr_alloc_config_dma_memory(struct mpi3mr_ioc *mrioc,
497732d457d5SSreekanth Reddy 	struct dma_memory_desc *mem_desc)
497832d457d5SSreekanth Reddy {
497932d457d5SSreekanth Reddy 	if (mem_desc->size > mrioc->cfg_page_sz) {
498032d457d5SSreekanth Reddy 		mem_desc->addr = dma_alloc_coherent(&mrioc->pdev->dev,
498132d457d5SSreekanth Reddy 		    mem_desc->size, &mem_desc->dma_addr, GFP_KERNEL);
498232d457d5SSreekanth Reddy 		if (!mem_desc->addr)
498332d457d5SSreekanth Reddy 			return -ENOMEM;
498432d457d5SSreekanth Reddy 	} else {
498532d457d5SSreekanth Reddy 		mem_desc->addr = mrioc->cfg_page;
498632d457d5SSreekanth Reddy 		mem_desc->dma_addr = mrioc->cfg_page_dma;
498732d457d5SSreekanth Reddy 		memset(mem_desc->addr, 0, mrioc->cfg_page_sz);
498832d457d5SSreekanth Reddy 	}
498932d457d5SSreekanth Reddy 	return 0;
499032d457d5SSreekanth Reddy }
499132d457d5SSreekanth Reddy 
499232d457d5SSreekanth Reddy /**
499332d457d5SSreekanth Reddy  * mpi3mr_post_cfg_req - Issue config requests and wait
499432d457d5SSreekanth Reddy  * @mrioc: Adapter instance reference
499532d457d5SSreekanth Reddy  * @cfg_req: Configuration request
499632d457d5SSreekanth Reddy  * @timeout: Timeout in seconds
499732d457d5SSreekanth Reddy  * @ioc_status: Pointer to return ioc status
499832d457d5SSreekanth Reddy  *
499932d457d5SSreekanth Reddy  * A generic function for posting MPI3 configuration request to
500032d457d5SSreekanth Reddy  * the firmware. This blocks for the completion of request for
500132d457d5SSreekanth Reddy  * timeout seconds and if the request times out this function
500232d457d5SSreekanth Reddy  * faults the controller with proper reason code.
500332d457d5SSreekanth Reddy  *
500432d457d5SSreekanth Reddy  * On successful completion of the request this function returns
500532d457d5SSreekanth Reddy  * appropriate ioc status from the firmware back to the caller.
500632d457d5SSreekanth Reddy  *
500732d457d5SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
500832d457d5SSreekanth Reddy  */
500932d457d5SSreekanth Reddy static int mpi3mr_post_cfg_req(struct mpi3mr_ioc *mrioc,
501032d457d5SSreekanth Reddy 	struct mpi3_config_request *cfg_req, int timeout, u16 *ioc_status)
501132d457d5SSreekanth Reddy {
501232d457d5SSreekanth Reddy 	int retval = 0;
501332d457d5SSreekanth Reddy 
501432d457d5SSreekanth Reddy 	mutex_lock(&mrioc->cfg_cmds.mutex);
501532d457d5SSreekanth Reddy 	if (mrioc->cfg_cmds.state & MPI3MR_CMD_PENDING) {
501632d457d5SSreekanth Reddy 		retval = -1;
501732d457d5SSreekanth Reddy 		ioc_err(mrioc, "sending config request failed due to command in use\n");
501832d457d5SSreekanth Reddy 		mutex_unlock(&mrioc->cfg_cmds.mutex);
501932d457d5SSreekanth Reddy 		goto out;
502032d457d5SSreekanth Reddy 	}
502132d457d5SSreekanth Reddy 	mrioc->cfg_cmds.state = MPI3MR_CMD_PENDING;
502232d457d5SSreekanth Reddy 	mrioc->cfg_cmds.is_waiting = 1;
502332d457d5SSreekanth Reddy 	mrioc->cfg_cmds.callback = NULL;
502432d457d5SSreekanth Reddy 	mrioc->cfg_cmds.ioc_status = 0;
502532d457d5SSreekanth Reddy 	mrioc->cfg_cmds.ioc_loginfo = 0;
502632d457d5SSreekanth Reddy 
502732d457d5SSreekanth Reddy 	cfg_req->host_tag = cpu_to_le16(MPI3MR_HOSTTAG_CFG_CMDS);
502832d457d5SSreekanth Reddy 	cfg_req->function = MPI3_FUNCTION_CONFIG;
502932d457d5SSreekanth Reddy 
503032d457d5SSreekanth Reddy 	init_completion(&mrioc->cfg_cmds.done);
503132d457d5SSreekanth Reddy 	dprint_cfg_info(mrioc, "posting config request\n");
503232d457d5SSreekanth Reddy 	if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO)
503332d457d5SSreekanth Reddy 		dprint_dump(cfg_req, sizeof(struct mpi3_config_request),
503432d457d5SSreekanth Reddy 		    "mpi3_cfg_req");
503532d457d5SSreekanth Reddy 	retval = mpi3mr_admin_request_post(mrioc, cfg_req, sizeof(*cfg_req), 1);
503632d457d5SSreekanth Reddy 	if (retval) {
503732d457d5SSreekanth Reddy 		ioc_err(mrioc, "posting config request failed\n");
503832d457d5SSreekanth Reddy 		goto out_unlock;
503932d457d5SSreekanth Reddy 	}
504032d457d5SSreekanth Reddy 	wait_for_completion_timeout(&mrioc->cfg_cmds.done, (timeout * HZ));
504132d457d5SSreekanth Reddy 	if (!(mrioc->cfg_cmds.state & MPI3MR_CMD_COMPLETE)) {
504232d457d5SSreekanth Reddy 		mpi3mr_check_rh_fault_ioc(mrioc,
504332d457d5SSreekanth Reddy 		    MPI3MR_RESET_FROM_CFG_REQ_TIMEOUT);
504432d457d5SSreekanth Reddy 		ioc_err(mrioc, "config request timed out\n");
504532d457d5SSreekanth Reddy 		retval = -1;
504632d457d5SSreekanth Reddy 		goto out_unlock;
504732d457d5SSreekanth Reddy 	}
504832d457d5SSreekanth Reddy 	*ioc_status = mrioc->cfg_cmds.ioc_status & MPI3_IOCSTATUS_STATUS_MASK;
504932d457d5SSreekanth Reddy 	if ((*ioc_status) != MPI3_IOCSTATUS_SUCCESS)
505032d457d5SSreekanth Reddy 		dprint_cfg_err(mrioc,
505132d457d5SSreekanth Reddy 		    "cfg_page request returned with ioc_status(0x%04x), log_info(0x%08x)\n",
505232d457d5SSreekanth Reddy 		    *ioc_status, mrioc->cfg_cmds.ioc_loginfo);
505332d457d5SSreekanth Reddy 
505432d457d5SSreekanth Reddy out_unlock:
505532d457d5SSreekanth Reddy 	mrioc->cfg_cmds.state = MPI3MR_CMD_NOTUSED;
505632d457d5SSreekanth Reddy 	mutex_unlock(&mrioc->cfg_cmds.mutex);
505732d457d5SSreekanth Reddy 
505832d457d5SSreekanth Reddy out:
505932d457d5SSreekanth Reddy 	return retval;
506032d457d5SSreekanth Reddy }
506132d457d5SSreekanth Reddy 
506232d457d5SSreekanth Reddy /**
506332d457d5SSreekanth Reddy  * mpi3mr_process_cfg_req - config page request processor
506432d457d5SSreekanth Reddy  * @mrioc: Adapter instance reference
506532d457d5SSreekanth Reddy  * @cfg_req: Configuration request
506632d457d5SSreekanth Reddy  * @cfg_hdr: Configuration page header
506732d457d5SSreekanth Reddy  * @timeout: Timeout in seconds
506832d457d5SSreekanth Reddy  * @ioc_status: Pointer to return ioc status
506932d457d5SSreekanth Reddy  * @cfg_buf: Memory pointer to copy config page or header
507032d457d5SSreekanth Reddy  * @cfg_buf_sz: Size of the memory to get config page or header
507132d457d5SSreekanth Reddy  *
507232d457d5SSreekanth Reddy  * This is handler for config page read, write and config page
507332d457d5SSreekanth Reddy  * header read operations.
507432d457d5SSreekanth Reddy  *
507532d457d5SSreekanth Reddy  * This function expects the cfg_req to be populated with page
507632d457d5SSreekanth Reddy  * type, page number, action for the header read and with page
507732d457d5SSreekanth Reddy  * address for all other operations.
507832d457d5SSreekanth Reddy  *
507932d457d5SSreekanth Reddy  * The cfg_hdr can be passed as null for reading required header
508032d457d5SSreekanth Reddy  * details for read/write pages the cfg_hdr should point valid
508132d457d5SSreekanth Reddy  * configuration page header.
508232d457d5SSreekanth Reddy  *
508332d457d5SSreekanth Reddy  * This allocates dmaable memory based on the size of the config
508432d457d5SSreekanth Reddy  * buffer and set the SGE of the cfg_req.
508532d457d5SSreekanth Reddy  *
508632d457d5SSreekanth Reddy  * For write actions, the config page data has to be passed in
508732d457d5SSreekanth Reddy  * the cfg_buf and size of the data has to be mentioned in the
508832d457d5SSreekanth Reddy  * cfg_buf_sz.
508932d457d5SSreekanth Reddy  *
509032d457d5SSreekanth Reddy  * For read/header actions, on successful completion of the
509132d457d5SSreekanth Reddy  * request with successful ioc_status the data will be copied
509232d457d5SSreekanth Reddy  * into the cfg_buf limited to a minimum of actual page size and
509332d457d5SSreekanth Reddy  * cfg_buf_sz
509432d457d5SSreekanth Reddy  *
509532d457d5SSreekanth Reddy  *
509632d457d5SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
509732d457d5SSreekanth Reddy  */
509832d457d5SSreekanth Reddy static int mpi3mr_process_cfg_req(struct mpi3mr_ioc *mrioc,
509932d457d5SSreekanth Reddy 	struct mpi3_config_request *cfg_req,
510032d457d5SSreekanth Reddy 	struct mpi3_config_page_header *cfg_hdr, int timeout, u16 *ioc_status,
510132d457d5SSreekanth Reddy 	void *cfg_buf, u32 cfg_buf_sz)
510232d457d5SSreekanth Reddy {
510332d457d5SSreekanth Reddy 	struct dma_memory_desc mem_desc;
510432d457d5SSreekanth Reddy 	int retval = -1;
510532d457d5SSreekanth Reddy 	u8 invalid_action = 0;
510632d457d5SSreekanth Reddy 	u8 sgl_flags = MPI3MR_SGEFLAGS_SYSTEM_SIMPLE_END_OF_LIST;
510732d457d5SSreekanth Reddy 
510832d457d5SSreekanth Reddy 	memset(&mem_desc, 0, sizeof(struct dma_memory_desc));
510932d457d5SSreekanth Reddy 
511032d457d5SSreekanth Reddy 	if (cfg_req->action == MPI3_CONFIG_ACTION_PAGE_HEADER)
511132d457d5SSreekanth Reddy 		mem_desc.size = sizeof(struct mpi3_config_page_header);
511232d457d5SSreekanth Reddy 	else {
511332d457d5SSreekanth Reddy 		if (!cfg_hdr) {
511432d457d5SSreekanth Reddy 			ioc_err(mrioc, "null config header passed for config action(%d), page_type(0x%02x), page_num(%d)\n",
511532d457d5SSreekanth Reddy 			    cfg_req->action, cfg_req->page_type,
511632d457d5SSreekanth Reddy 			    cfg_req->page_number);
511732d457d5SSreekanth Reddy 			goto out;
511832d457d5SSreekanth Reddy 		}
511932d457d5SSreekanth Reddy 		switch (cfg_hdr->page_attribute & MPI3_CONFIG_PAGEATTR_MASK) {
512032d457d5SSreekanth Reddy 		case MPI3_CONFIG_PAGEATTR_READ_ONLY:
512132d457d5SSreekanth Reddy 			if (cfg_req->action
512232d457d5SSreekanth Reddy 			    != MPI3_CONFIG_ACTION_READ_CURRENT)
512332d457d5SSreekanth Reddy 				invalid_action = 1;
512432d457d5SSreekanth Reddy 			break;
512532d457d5SSreekanth Reddy 		case MPI3_CONFIG_PAGEATTR_CHANGEABLE:
512632d457d5SSreekanth Reddy 			if ((cfg_req->action ==
512732d457d5SSreekanth Reddy 			     MPI3_CONFIG_ACTION_READ_PERSISTENT) ||
512832d457d5SSreekanth Reddy 			    (cfg_req->action ==
512932d457d5SSreekanth Reddy 			     MPI3_CONFIG_ACTION_WRITE_PERSISTENT))
513032d457d5SSreekanth Reddy 				invalid_action = 1;
513132d457d5SSreekanth Reddy 			break;
513232d457d5SSreekanth Reddy 		case MPI3_CONFIG_PAGEATTR_PERSISTENT:
513332d457d5SSreekanth Reddy 		default:
513432d457d5SSreekanth Reddy 			break;
513532d457d5SSreekanth Reddy 		}
513632d457d5SSreekanth Reddy 		if (invalid_action) {
513732d457d5SSreekanth Reddy 			ioc_err(mrioc,
513832d457d5SSreekanth Reddy 			    "config action(%d) is not allowed for page_type(0x%02x), page_num(%d) with page_attribute(0x%02x)\n",
513932d457d5SSreekanth Reddy 			    cfg_req->action, cfg_req->page_type,
514032d457d5SSreekanth Reddy 			    cfg_req->page_number, cfg_hdr->page_attribute);
514132d457d5SSreekanth Reddy 			goto out;
514232d457d5SSreekanth Reddy 		}
514332d457d5SSreekanth Reddy 		mem_desc.size = le16_to_cpu(cfg_hdr->page_length) * 4;
514432d457d5SSreekanth Reddy 		cfg_req->page_length = cfg_hdr->page_length;
514532d457d5SSreekanth Reddy 		cfg_req->page_version = cfg_hdr->page_version;
514632d457d5SSreekanth Reddy 	}
514732d457d5SSreekanth Reddy 	if (mpi3mr_alloc_config_dma_memory(mrioc, &mem_desc))
514832d457d5SSreekanth Reddy 		goto out;
514932d457d5SSreekanth Reddy 
515032d457d5SSreekanth Reddy 	mpi3mr_add_sg_single(&cfg_req->sgl, sgl_flags, mem_desc.size,
515132d457d5SSreekanth Reddy 	    mem_desc.dma_addr);
515232d457d5SSreekanth Reddy 
515332d457d5SSreekanth Reddy 	if ((cfg_req->action == MPI3_CONFIG_ACTION_WRITE_PERSISTENT) ||
515432d457d5SSreekanth Reddy 	    (cfg_req->action == MPI3_CONFIG_ACTION_WRITE_CURRENT)) {
515532d457d5SSreekanth Reddy 		memcpy(mem_desc.addr, cfg_buf, min_t(u16, mem_desc.size,
515632d457d5SSreekanth Reddy 		    cfg_buf_sz));
515732d457d5SSreekanth Reddy 		dprint_cfg_info(mrioc, "config buffer to be written\n");
515832d457d5SSreekanth Reddy 		if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO)
515932d457d5SSreekanth Reddy 			dprint_dump(mem_desc.addr, mem_desc.size, "cfg_buf");
516032d457d5SSreekanth Reddy 	}
516132d457d5SSreekanth Reddy 
516232d457d5SSreekanth Reddy 	if (mpi3mr_post_cfg_req(mrioc, cfg_req, timeout, ioc_status))
516332d457d5SSreekanth Reddy 		goto out;
516432d457d5SSreekanth Reddy 
516532d457d5SSreekanth Reddy 	retval = 0;
516632d457d5SSreekanth Reddy 	if ((*ioc_status == MPI3_IOCSTATUS_SUCCESS) &&
516732d457d5SSreekanth Reddy 	    (cfg_req->action != MPI3_CONFIG_ACTION_WRITE_PERSISTENT) &&
516832d457d5SSreekanth Reddy 	    (cfg_req->action != MPI3_CONFIG_ACTION_WRITE_CURRENT)) {
516932d457d5SSreekanth Reddy 		memcpy(cfg_buf, mem_desc.addr, min_t(u16, mem_desc.size,
517032d457d5SSreekanth Reddy 		    cfg_buf_sz));
517132d457d5SSreekanth Reddy 		dprint_cfg_info(mrioc, "config buffer read\n");
517232d457d5SSreekanth Reddy 		if (mrioc->logging_level & MPI3_DEBUG_CFG_INFO)
517332d457d5SSreekanth Reddy 			dprint_dump(mem_desc.addr, mem_desc.size, "cfg_buf");
517432d457d5SSreekanth Reddy 	}
517532d457d5SSreekanth Reddy 
517632d457d5SSreekanth Reddy out:
517732d457d5SSreekanth Reddy 	mpi3mr_free_config_dma_memory(mrioc, &mem_desc);
517832d457d5SSreekanth Reddy 	return retval;
517932d457d5SSreekanth Reddy }
518064a8d931SSreekanth Reddy 
518164a8d931SSreekanth Reddy /**
518264a8d931SSreekanth Reddy  * mpi3mr_cfg_get_dev_pg0 - Read current device page0
518364a8d931SSreekanth Reddy  * @mrioc: Adapter instance reference
518464a8d931SSreekanth Reddy  * @ioc_status: Pointer to return ioc status
518564a8d931SSreekanth Reddy  * @dev_pg0: Pointer to return device page 0
518664a8d931SSreekanth Reddy  * @pg_sz: Size of the memory allocated to the page pointer
518764a8d931SSreekanth Reddy  * @form: The form to be used for addressing the page
518864a8d931SSreekanth Reddy  * @form_spec: Form specific information like device handle
518964a8d931SSreekanth Reddy  *
519064a8d931SSreekanth Reddy  * This is handler for config page read for a specific device
519164a8d931SSreekanth Reddy  * page0. The ioc_status has the controller returned ioc_status.
519264a8d931SSreekanth Reddy  * This routine doesn't check ioc_status to decide whether the
519364a8d931SSreekanth Reddy  * page read is success or not and it is the callers
519464a8d931SSreekanth Reddy  * responsibility.
519564a8d931SSreekanth Reddy  *
519664a8d931SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
519764a8d931SSreekanth Reddy  */
519864a8d931SSreekanth Reddy int mpi3mr_cfg_get_dev_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
519964a8d931SSreekanth Reddy 	struct mpi3_device_page0 *dev_pg0, u16 pg_sz, u32 form, u32 form_spec)
520064a8d931SSreekanth Reddy {
520164a8d931SSreekanth Reddy 	struct mpi3_config_page_header cfg_hdr;
520264a8d931SSreekanth Reddy 	struct mpi3_config_request cfg_req;
520364a8d931SSreekanth Reddy 	u32 page_address;
520464a8d931SSreekanth Reddy 
520564a8d931SSreekanth Reddy 	memset(dev_pg0, 0, pg_sz);
520664a8d931SSreekanth Reddy 	memset(&cfg_hdr, 0, sizeof(cfg_hdr));
520764a8d931SSreekanth Reddy 	memset(&cfg_req, 0, sizeof(cfg_req));
520864a8d931SSreekanth Reddy 
520964a8d931SSreekanth Reddy 	cfg_req.function = MPI3_FUNCTION_CONFIG;
521064a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
521164a8d931SSreekanth Reddy 	cfg_req.page_type = MPI3_CONFIG_PAGETYPE_DEVICE;
521264a8d931SSreekanth Reddy 	cfg_req.page_number = 0;
521364a8d931SSreekanth Reddy 	cfg_req.page_address = 0;
521464a8d931SSreekanth Reddy 
521564a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
521664a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
521764a8d931SSreekanth Reddy 		ioc_err(mrioc, "device page0 header read failed\n");
521864a8d931SSreekanth Reddy 		goto out_failed;
521964a8d931SSreekanth Reddy 	}
522064a8d931SSreekanth Reddy 	if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
522164a8d931SSreekanth Reddy 		ioc_err(mrioc, "device page0 header read failed with ioc_status(0x%04x)\n",
522264a8d931SSreekanth Reddy 		    *ioc_status);
522364a8d931SSreekanth Reddy 		goto out_failed;
522464a8d931SSreekanth Reddy 	}
522564a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
522664a8d931SSreekanth Reddy 	page_address = ((form & MPI3_DEVICE_PGAD_FORM_MASK) |
522764a8d931SSreekanth Reddy 	    (form_spec & MPI3_DEVICE_PGAD_HANDLE_MASK));
522864a8d931SSreekanth Reddy 	cfg_req.page_address = cpu_to_le32(page_address);
522964a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
523064a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, dev_pg0, pg_sz)) {
523164a8d931SSreekanth Reddy 		ioc_err(mrioc, "device page0 read failed\n");
523264a8d931SSreekanth Reddy 		goto out_failed;
523364a8d931SSreekanth Reddy 	}
523464a8d931SSreekanth Reddy 	return 0;
523564a8d931SSreekanth Reddy out_failed:
523664a8d931SSreekanth Reddy 	return -1;
523764a8d931SSreekanth Reddy }
523864a8d931SSreekanth Reddy 
523964a8d931SSreekanth Reddy 
524064a8d931SSreekanth Reddy /**
524164a8d931SSreekanth Reddy  * mpi3mr_cfg_get_sas_phy_pg0 - Read current SAS Phy page0
524264a8d931SSreekanth Reddy  * @mrioc: Adapter instance reference
524364a8d931SSreekanth Reddy  * @ioc_status: Pointer to return ioc status
524464a8d931SSreekanth Reddy  * @phy_pg0: Pointer to return SAS Phy page 0
524564a8d931SSreekanth Reddy  * @pg_sz: Size of the memory allocated to the page pointer
524664a8d931SSreekanth Reddy  * @form: The form to be used for addressing the page
524764a8d931SSreekanth Reddy  * @form_spec: Form specific information like phy number
524864a8d931SSreekanth Reddy  *
524964a8d931SSreekanth Reddy  * This is handler for config page read for a specific SAS Phy
525064a8d931SSreekanth Reddy  * page0. The ioc_status has the controller returned ioc_status.
525164a8d931SSreekanth Reddy  * This routine doesn't check ioc_status to decide whether the
525264a8d931SSreekanth Reddy  * page read is success or not and it is the callers
525364a8d931SSreekanth Reddy  * responsibility.
525464a8d931SSreekanth Reddy  *
525564a8d931SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
525664a8d931SSreekanth Reddy  */
525764a8d931SSreekanth Reddy int mpi3mr_cfg_get_sas_phy_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
525864a8d931SSreekanth Reddy 	struct mpi3_sas_phy_page0 *phy_pg0, u16 pg_sz, u32 form,
525964a8d931SSreekanth Reddy 	u32 form_spec)
526064a8d931SSreekanth Reddy {
526164a8d931SSreekanth Reddy 	struct mpi3_config_page_header cfg_hdr;
526264a8d931SSreekanth Reddy 	struct mpi3_config_request cfg_req;
526364a8d931SSreekanth Reddy 	u32 page_address;
526464a8d931SSreekanth Reddy 
526564a8d931SSreekanth Reddy 	memset(phy_pg0, 0, pg_sz);
526664a8d931SSreekanth Reddy 	memset(&cfg_hdr, 0, sizeof(cfg_hdr));
526764a8d931SSreekanth Reddy 	memset(&cfg_req, 0, sizeof(cfg_req));
526864a8d931SSreekanth Reddy 
526964a8d931SSreekanth Reddy 	cfg_req.function = MPI3_FUNCTION_CONFIG;
527064a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
527164a8d931SSreekanth Reddy 	cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_PHY;
527264a8d931SSreekanth Reddy 	cfg_req.page_number = 0;
527364a8d931SSreekanth Reddy 	cfg_req.page_address = 0;
527464a8d931SSreekanth Reddy 
527564a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
527664a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
527764a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas phy page0 header read failed\n");
527864a8d931SSreekanth Reddy 		goto out_failed;
527964a8d931SSreekanth Reddy 	}
528064a8d931SSreekanth Reddy 	if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
528164a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas phy page0 header read failed with ioc_status(0x%04x)\n",
528264a8d931SSreekanth Reddy 		    *ioc_status);
528364a8d931SSreekanth Reddy 		goto out_failed;
528464a8d931SSreekanth Reddy 	}
528564a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
528664a8d931SSreekanth Reddy 	page_address = ((form & MPI3_SAS_PHY_PGAD_FORM_MASK) |
528764a8d931SSreekanth Reddy 	    (form_spec & MPI3_SAS_PHY_PGAD_PHY_NUMBER_MASK));
528864a8d931SSreekanth Reddy 	cfg_req.page_address = cpu_to_le32(page_address);
528964a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
529064a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, phy_pg0, pg_sz)) {
529164a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas phy page0 read failed\n");
529264a8d931SSreekanth Reddy 		goto out_failed;
529364a8d931SSreekanth Reddy 	}
529464a8d931SSreekanth Reddy 	return 0;
529564a8d931SSreekanth Reddy out_failed:
529664a8d931SSreekanth Reddy 	return -1;
529764a8d931SSreekanth Reddy }
529864a8d931SSreekanth Reddy 
529964a8d931SSreekanth Reddy /**
530064a8d931SSreekanth Reddy  * mpi3mr_cfg_get_sas_phy_pg1 - Read current SAS Phy page1
530164a8d931SSreekanth Reddy  * @mrioc: Adapter instance reference
530264a8d931SSreekanth Reddy  * @ioc_status: Pointer to return ioc status
530364a8d931SSreekanth Reddy  * @phy_pg1: Pointer to return SAS Phy page 1
530464a8d931SSreekanth Reddy  * @pg_sz: Size of the memory allocated to the page pointer
530564a8d931SSreekanth Reddy  * @form: The form to be used for addressing the page
530664a8d931SSreekanth Reddy  * @form_spec: Form specific information like phy number
530764a8d931SSreekanth Reddy  *
530864a8d931SSreekanth Reddy  * This is handler for config page read for a specific SAS Phy
530964a8d931SSreekanth Reddy  * page1. The ioc_status has the controller returned ioc_status.
531064a8d931SSreekanth Reddy  * This routine doesn't check ioc_status to decide whether the
531164a8d931SSreekanth Reddy  * page read is success or not and it is the callers
531264a8d931SSreekanth Reddy  * responsibility.
531364a8d931SSreekanth Reddy  *
531464a8d931SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
531564a8d931SSreekanth Reddy  */
531664a8d931SSreekanth Reddy int mpi3mr_cfg_get_sas_phy_pg1(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
531764a8d931SSreekanth Reddy 	struct mpi3_sas_phy_page1 *phy_pg1, u16 pg_sz, u32 form,
531864a8d931SSreekanth Reddy 	u32 form_spec)
531964a8d931SSreekanth Reddy {
532064a8d931SSreekanth Reddy 	struct mpi3_config_page_header cfg_hdr;
532164a8d931SSreekanth Reddy 	struct mpi3_config_request cfg_req;
532264a8d931SSreekanth Reddy 	u32 page_address;
532364a8d931SSreekanth Reddy 
532464a8d931SSreekanth Reddy 	memset(phy_pg1, 0, pg_sz);
532564a8d931SSreekanth Reddy 	memset(&cfg_hdr, 0, sizeof(cfg_hdr));
532664a8d931SSreekanth Reddy 	memset(&cfg_req, 0, sizeof(cfg_req));
532764a8d931SSreekanth Reddy 
532864a8d931SSreekanth Reddy 	cfg_req.function = MPI3_FUNCTION_CONFIG;
532964a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
533064a8d931SSreekanth Reddy 	cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_PHY;
533164a8d931SSreekanth Reddy 	cfg_req.page_number = 1;
533264a8d931SSreekanth Reddy 	cfg_req.page_address = 0;
533364a8d931SSreekanth Reddy 
533464a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
533564a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
533664a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas phy page1 header read failed\n");
533764a8d931SSreekanth Reddy 		goto out_failed;
533864a8d931SSreekanth Reddy 	}
533964a8d931SSreekanth Reddy 	if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
534064a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas phy page1 header read failed with ioc_status(0x%04x)\n",
534164a8d931SSreekanth Reddy 		    *ioc_status);
534264a8d931SSreekanth Reddy 		goto out_failed;
534364a8d931SSreekanth Reddy 	}
534464a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
534564a8d931SSreekanth Reddy 	page_address = ((form & MPI3_SAS_PHY_PGAD_FORM_MASK) |
534664a8d931SSreekanth Reddy 	    (form_spec & MPI3_SAS_PHY_PGAD_PHY_NUMBER_MASK));
534764a8d931SSreekanth Reddy 	cfg_req.page_address = cpu_to_le32(page_address);
534864a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
534964a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, phy_pg1, pg_sz)) {
535064a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas phy page1 read failed\n");
535164a8d931SSreekanth Reddy 		goto out_failed;
535264a8d931SSreekanth Reddy 	}
535364a8d931SSreekanth Reddy 	return 0;
535464a8d931SSreekanth Reddy out_failed:
535564a8d931SSreekanth Reddy 	return -1;
535664a8d931SSreekanth Reddy }
535764a8d931SSreekanth Reddy 
535864a8d931SSreekanth Reddy 
535964a8d931SSreekanth Reddy /**
536064a8d931SSreekanth Reddy  * mpi3mr_cfg_get_sas_exp_pg0 - Read current SAS Expander page0
536164a8d931SSreekanth Reddy  * @mrioc: Adapter instance reference
536264a8d931SSreekanth Reddy  * @ioc_status: Pointer to return ioc status
536364a8d931SSreekanth Reddy  * @exp_pg0: Pointer to return SAS Expander page 0
536464a8d931SSreekanth Reddy  * @pg_sz: Size of the memory allocated to the page pointer
536564a8d931SSreekanth Reddy  * @form: The form to be used for addressing the page
536664a8d931SSreekanth Reddy  * @form_spec: Form specific information like device handle
536764a8d931SSreekanth Reddy  *
536864a8d931SSreekanth Reddy  * This is handler for config page read for a specific SAS
536964a8d931SSreekanth Reddy  * Expander page0. The ioc_status has the controller returned
537064a8d931SSreekanth Reddy  * ioc_status. This routine doesn't check ioc_status to decide
537164a8d931SSreekanth Reddy  * whether the page read is success or not and it is the callers
537264a8d931SSreekanth Reddy  * responsibility.
537364a8d931SSreekanth Reddy  *
537464a8d931SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
537564a8d931SSreekanth Reddy  */
537664a8d931SSreekanth Reddy int mpi3mr_cfg_get_sas_exp_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
537764a8d931SSreekanth Reddy 	struct mpi3_sas_expander_page0 *exp_pg0, u16 pg_sz, u32 form,
537864a8d931SSreekanth Reddy 	u32 form_spec)
537964a8d931SSreekanth Reddy {
538064a8d931SSreekanth Reddy 	struct mpi3_config_page_header cfg_hdr;
538164a8d931SSreekanth Reddy 	struct mpi3_config_request cfg_req;
538264a8d931SSreekanth Reddy 	u32 page_address;
538364a8d931SSreekanth Reddy 
538464a8d931SSreekanth Reddy 	memset(exp_pg0, 0, pg_sz);
538564a8d931SSreekanth Reddy 	memset(&cfg_hdr, 0, sizeof(cfg_hdr));
538664a8d931SSreekanth Reddy 	memset(&cfg_req, 0, sizeof(cfg_req));
538764a8d931SSreekanth Reddy 
538864a8d931SSreekanth Reddy 	cfg_req.function = MPI3_FUNCTION_CONFIG;
538964a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
539064a8d931SSreekanth Reddy 	cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_EXPANDER;
539164a8d931SSreekanth Reddy 	cfg_req.page_number = 0;
539264a8d931SSreekanth Reddy 	cfg_req.page_address = 0;
539364a8d931SSreekanth Reddy 
539464a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
539564a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
539664a8d931SSreekanth Reddy 		ioc_err(mrioc, "expander page0 header read failed\n");
539764a8d931SSreekanth Reddy 		goto out_failed;
539864a8d931SSreekanth Reddy 	}
539964a8d931SSreekanth Reddy 	if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
540064a8d931SSreekanth Reddy 		ioc_err(mrioc, "expander page0 header read failed with ioc_status(0x%04x)\n",
540164a8d931SSreekanth Reddy 		    *ioc_status);
540264a8d931SSreekanth Reddy 		goto out_failed;
540364a8d931SSreekanth Reddy 	}
540464a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
540564a8d931SSreekanth Reddy 	page_address = ((form & MPI3_SAS_EXPAND_PGAD_FORM_MASK) |
540664a8d931SSreekanth Reddy 	    (form_spec & (MPI3_SAS_EXPAND_PGAD_PHYNUM_MASK |
540764a8d931SSreekanth Reddy 	    MPI3_SAS_EXPAND_PGAD_HANDLE_MASK)));
540864a8d931SSreekanth Reddy 	cfg_req.page_address = cpu_to_le32(page_address);
540964a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
541064a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, exp_pg0, pg_sz)) {
541164a8d931SSreekanth Reddy 		ioc_err(mrioc, "expander page0 read failed\n");
541264a8d931SSreekanth Reddy 		goto out_failed;
541364a8d931SSreekanth Reddy 	}
541464a8d931SSreekanth Reddy 	return 0;
541564a8d931SSreekanth Reddy out_failed:
541664a8d931SSreekanth Reddy 	return -1;
541764a8d931SSreekanth Reddy }
541864a8d931SSreekanth Reddy 
541964a8d931SSreekanth Reddy /**
542064a8d931SSreekanth Reddy  * mpi3mr_cfg_get_sas_exp_pg1 - Read current SAS Expander page1
542164a8d931SSreekanth Reddy  * @mrioc: Adapter instance reference
542264a8d931SSreekanth Reddy  * @ioc_status: Pointer to return ioc status
542364a8d931SSreekanth Reddy  * @exp_pg1: Pointer to return SAS Expander page 1
542464a8d931SSreekanth Reddy  * @pg_sz: Size of the memory allocated to the page pointer
542564a8d931SSreekanth Reddy  * @form: The form to be used for addressing the page
542664a8d931SSreekanth Reddy  * @form_spec: Form specific information like phy number
542764a8d931SSreekanth Reddy  *
542864a8d931SSreekanth Reddy  * This is handler for config page read for a specific SAS
542964a8d931SSreekanth Reddy  * Expander page1. The ioc_status has the controller returned
543064a8d931SSreekanth Reddy  * ioc_status. This routine doesn't check ioc_status to decide
543164a8d931SSreekanth Reddy  * whether the page read is success or not and it is the callers
543264a8d931SSreekanth Reddy  * responsibility.
543364a8d931SSreekanth Reddy  *
543464a8d931SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
543564a8d931SSreekanth Reddy  */
543664a8d931SSreekanth Reddy int mpi3mr_cfg_get_sas_exp_pg1(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
543764a8d931SSreekanth Reddy 	struct mpi3_sas_expander_page1 *exp_pg1, u16 pg_sz, u32 form,
543864a8d931SSreekanth Reddy 	u32 form_spec)
543964a8d931SSreekanth Reddy {
544064a8d931SSreekanth Reddy 	struct mpi3_config_page_header cfg_hdr;
544164a8d931SSreekanth Reddy 	struct mpi3_config_request cfg_req;
544264a8d931SSreekanth Reddy 	u32 page_address;
544364a8d931SSreekanth Reddy 
544464a8d931SSreekanth Reddy 	memset(exp_pg1, 0, pg_sz);
544564a8d931SSreekanth Reddy 	memset(&cfg_hdr, 0, sizeof(cfg_hdr));
544664a8d931SSreekanth Reddy 	memset(&cfg_req, 0, sizeof(cfg_req));
544764a8d931SSreekanth Reddy 
544864a8d931SSreekanth Reddy 	cfg_req.function = MPI3_FUNCTION_CONFIG;
544964a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
545064a8d931SSreekanth Reddy 	cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_EXPANDER;
545164a8d931SSreekanth Reddy 	cfg_req.page_number = 1;
545264a8d931SSreekanth Reddy 	cfg_req.page_address = 0;
545364a8d931SSreekanth Reddy 
545464a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
545564a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
545664a8d931SSreekanth Reddy 		ioc_err(mrioc, "expander page1 header read failed\n");
545764a8d931SSreekanth Reddy 		goto out_failed;
545864a8d931SSreekanth Reddy 	}
545964a8d931SSreekanth Reddy 	if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
546064a8d931SSreekanth Reddy 		ioc_err(mrioc, "expander page1 header read failed with ioc_status(0x%04x)\n",
546164a8d931SSreekanth Reddy 		    *ioc_status);
546264a8d931SSreekanth Reddy 		goto out_failed;
546364a8d931SSreekanth Reddy 	}
546464a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
546564a8d931SSreekanth Reddy 	page_address = ((form & MPI3_SAS_EXPAND_PGAD_FORM_MASK) |
546664a8d931SSreekanth Reddy 	    (form_spec & (MPI3_SAS_EXPAND_PGAD_PHYNUM_MASK |
546764a8d931SSreekanth Reddy 	    MPI3_SAS_EXPAND_PGAD_HANDLE_MASK)));
546864a8d931SSreekanth Reddy 	cfg_req.page_address = cpu_to_le32(page_address);
546964a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
547064a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, exp_pg1, pg_sz)) {
547164a8d931SSreekanth Reddy 		ioc_err(mrioc, "expander page1 read failed\n");
547264a8d931SSreekanth Reddy 		goto out_failed;
547364a8d931SSreekanth Reddy 	}
547464a8d931SSreekanth Reddy 	return 0;
547564a8d931SSreekanth Reddy out_failed:
547664a8d931SSreekanth Reddy 	return -1;
547764a8d931SSreekanth Reddy }
547864a8d931SSreekanth Reddy 
547964a8d931SSreekanth Reddy /**
548064a8d931SSreekanth Reddy  * mpi3mr_cfg_get_enclosure_pg0 - Read current Enclosure page0
548164a8d931SSreekanth Reddy  * @mrioc: Adapter instance reference
548264a8d931SSreekanth Reddy  * @ioc_status: Pointer to return ioc status
548364a8d931SSreekanth Reddy  * @encl_pg0: Pointer to return Enclosure page 0
548464a8d931SSreekanth Reddy  * @pg_sz: Size of the memory allocated to the page pointer
548564a8d931SSreekanth Reddy  * @form: The form to be used for addressing the page
548664a8d931SSreekanth Reddy  * @form_spec: Form specific information like device handle
548764a8d931SSreekanth Reddy  *
548864a8d931SSreekanth Reddy  * This is handler for config page read for a specific Enclosure
548964a8d931SSreekanth Reddy  * page0. The ioc_status has the controller returned ioc_status.
549064a8d931SSreekanth Reddy  * This routine doesn't check ioc_status to decide whether the
549164a8d931SSreekanth Reddy  * page read is success or not and it is the callers
549264a8d931SSreekanth Reddy  * responsibility.
549364a8d931SSreekanth Reddy  *
549464a8d931SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
549564a8d931SSreekanth Reddy  */
549664a8d931SSreekanth Reddy int mpi3mr_cfg_get_enclosure_pg0(struct mpi3mr_ioc *mrioc, u16 *ioc_status,
549764a8d931SSreekanth Reddy 	struct mpi3_enclosure_page0 *encl_pg0, u16 pg_sz, u32 form,
549864a8d931SSreekanth Reddy 	u32 form_spec)
549964a8d931SSreekanth Reddy {
550064a8d931SSreekanth Reddy 	struct mpi3_config_page_header cfg_hdr;
550164a8d931SSreekanth Reddy 	struct mpi3_config_request cfg_req;
550264a8d931SSreekanth Reddy 	u32 page_address;
550364a8d931SSreekanth Reddy 
550464a8d931SSreekanth Reddy 	memset(encl_pg0, 0, pg_sz);
550564a8d931SSreekanth Reddy 	memset(&cfg_hdr, 0, sizeof(cfg_hdr));
550664a8d931SSreekanth Reddy 	memset(&cfg_req, 0, sizeof(cfg_req));
550764a8d931SSreekanth Reddy 
550864a8d931SSreekanth Reddy 	cfg_req.function = MPI3_FUNCTION_CONFIG;
550964a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
551064a8d931SSreekanth Reddy 	cfg_req.page_type = MPI3_CONFIG_PAGETYPE_ENCLOSURE;
551164a8d931SSreekanth Reddy 	cfg_req.page_number = 0;
551264a8d931SSreekanth Reddy 	cfg_req.page_address = 0;
551364a8d931SSreekanth Reddy 
551464a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
551564a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
551664a8d931SSreekanth Reddy 		ioc_err(mrioc, "enclosure page0 header read failed\n");
551764a8d931SSreekanth Reddy 		goto out_failed;
551864a8d931SSreekanth Reddy 	}
551964a8d931SSreekanth Reddy 	if (*ioc_status != MPI3_IOCSTATUS_SUCCESS) {
552064a8d931SSreekanth Reddy 		ioc_err(mrioc, "enclosure page0 header read failed with ioc_status(0x%04x)\n",
552164a8d931SSreekanth Reddy 		    *ioc_status);
552264a8d931SSreekanth Reddy 		goto out_failed;
552364a8d931SSreekanth Reddy 	}
552464a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
552564a8d931SSreekanth Reddy 	page_address = ((form & MPI3_ENCLOS_PGAD_FORM_MASK) |
552664a8d931SSreekanth Reddy 	    (form_spec & MPI3_ENCLOS_PGAD_HANDLE_MASK));
552764a8d931SSreekanth Reddy 	cfg_req.page_address = cpu_to_le32(page_address);
552864a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
552964a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, ioc_status, encl_pg0, pg_sz)) {
553064a8d931SSreekanth Reddy 		ioc_err(mrioc, "enclosure page0 read failed\n");
553164a8d931SSreekanth Reddy 		goto out_failed;
553264a8d931SSreekanth Reddy 	}
553364a8d931SSreekanth Reddy 	return 0;
553464a8d931SSreekanth Reddy out_failed:
553564a8d931SSreekanth Reddy 	return -1;
553664a8d931SSreekanth Reddy }
553764a8d931SSreekanth Reddy 
553864a8d931SSreekanth Reddy 
553964a8d931SSreekanth Reddy /**
554064a8d931SSreekanth Reddy  * mpi3mr_cfg_get_sas_io_unit_pg0 - Read current SASIOUnit page0
554164a8d931SSreekanth Reddy  * @mrioc: Adapter instance reference
554264a8d931SSreekanth Reddy  * @sas_io_unit_pg0: Pointer to return SAS IO Unit page 0
554364a8d931SSreekanth Reddy  * @pg_sz: Size of the memory allocated to the page pointer
554464a8d931SSreekanth Reddy  *
554564a8d931SSreekanth Reddy  * This is handler for config page read for the SAS IO Unit
554664a8d931SSreekanth Reddy  * page0. This routine checks ioc_status to decide whether the
554764a8d931SSreekanth Reddy  * page read is success or not.
554864a8d931SSreekanth Reddy  *
554964a8d931SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
555064a8d931SSreekanth Reddy  */
555164a8d931SSreekanth Reddy int mpi3mr_cfg_get_sas_io_unit_pg0(struct mpi3mr_ioc *mrioc,
555264a8d931SSreekanth Reddy 	struct mpi3_sas_io_unit_page0 *sas_io_unit_pg0, u16 pg_sz)
555364a8d931SSreekanth Reddy {
555464a8d931SSreekanth Reddy 	struct mpi3_config_page_header cfg_hdr;
555564a8d931SSreekanth Reddy 	struct mpi3_config_request cfg_req;
555664a8d931SSreekanth Reddy 	u16 ioc_status = 0;
555764a8d931SSreekanth Reddy 
555864a8d931SSreekanth Reddy 	memset(sas_io_unit_pg0, 0, pg_sz);
555964a8d931SSreekanth Reddy 	memset(&cfg_hdr, 0, sizeof(cfg_hdr));
556064a8d931SSreekanth Reddy 	memset(&cfg_req, 0, sizeof(cfg_req));
556164a8d931SSreekanth Reddy 
556264a8d931SSreekanth Reddy 	cfg_req.function = MPI3_FUNCTION_CONFIG;
556364a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
556464a8d931SSreekanth Reddy 	cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_IO_UNIT;
556564a8d931SSreekanth Reddy 	cfg_req.page_number = 0;
556664a8d931SSreekanth Reddy 	cfg_req.page_address = 0;
556764a8d931SSreekanth Reddy 
556864a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
556964a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
557064a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page0 header read failed\n");
557164a8d931SSreekanth Reddy 		goto out_failed;
557264a8d931SSreekanth Reddy 	}
557364a8d931SSreekanth Reddy 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
557464a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page0 header read failed with ioc_status(0x%04x)\n",
557564a8d931SSreekanth Reddy 		    ioc_status);
557664a8d931SSreekanth Reddy 		goto out_failed;
557764a8d931SSreekanth Reddy 	}
557864a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
557964a8d931SSreekanth Reddy 
558064a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
558164a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg0, pg_sz)) {
558264a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page0 read failed\n");
558364a8d931SSreekanth Reddy 		goto out_failed;
558464a8d931SSreekanth Reddy 	}
558564a8d931SSreekanth Reddy 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
558664a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page0 read failed with ioc_status(0x%04x)\n",
558764a8d931SSreekanth Reddy 		    ioc_status);
558864a8d931SSreekanth Reddy 		goto out_failed;
558964a8d931SSreekanth Reddy 	}
559064a8d931SSreekanth Reddy 	return 0;
559164a8d931SSreekanth Reddy out_failed:
559264a8d931SSreekanth Reddy 	return -1;
559364a8d931SSreekanth Reddy }
559464a8d931SSreekanth Reddy 
559564a8d931SSreekanth Reddy /**
559664a8d931SSreekanth Reddy  * mpi3mr_cfg_get_sas_io_unit_pg1 - Read current SASIOUnit page1
559764a8d931SSreekanth Reddy  * @mrioc: Adapter instance reference
559864a8d931SSreekanth Reddy  * @sas_io_unit_pg1: Pointer to return SAS IO Unit page 1
559964a8d931SSreekanth Reddy  * @pg_sz: Size of the memory allocated to the page pointer
560064a8d931SSreekanth Reddy  *
560164a8d931SSreekanth Reddy  * This is handler for config page read for the SAS IO Unit
560264a8d931SSreekanth Reddy  * page1. This routine checks ioc_status to decide whether the
560364a8d931SSreekanth Reddy  * page read is success or not.
560464a8d931SSreekanth Reddy  *
560564a8d931SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
560664a8d931SSreekanth Reddy  */
560764a8d931SSreekanth Reddy int mpi3mr_cfg_get_sas_io_unit_pg1(struct mpi3mr_ioc *mrioc,
560864a8d931SSreekanth Reddy 	struct mpi3_sas_io_unit_page1 *sas_io_unit_pg1, u16 pg_sz)
560964a8d931SSreekanth Reddy {
561064a8d931SSreekanth Reddy 	struct mpi3_config_page_header cfg_hdr;
561164a8d931SSreekanth Reddy 	struct mpi3_config_request cfg_req;
561264a8d931SSreekanth Reddy 	u16 ioc_status = 0;
561364a8d931SSreekanth Reddy 
561464a8d931SSreekanth Reddy 	memset(sas_io_unit_pg1, 0, pg_sz);
561564a8d931SSreekanth Reddy 	memset(&cfg_hdr, 0, sizeof(cfg_hdr));
561664a8d931SSreekanth Reddy 	memset(&cfg_req, 0, sizeof(cfg_req));
561764a8d931SSreekanth Reddy 
561864a8d931SSreekanth Reddy 	cfg_req.function = MPI3_FUNCTION_CONFIG;
561964a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
562064a8d931SSreekanth Reddy 	cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_IO_UNIT;
562164a8d931SSreekanth Reddy 	cfg_req.page_number = 1;
562264a8d931SSreekanth Reddy 	cfg_req.page_address = 0;
562364a8d931SSreekanth Reddy 
562464a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
562564a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
562664a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page1 header read failed\n");
562764a8d931SSreekanth Reddy 		goto out_failed;
562864a8d931SSreekanth Reddy 	}
562964a8d931SSreekanth Reddy 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
563064a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page1 header read failed with ioc_status(0x%04x)\n",
563164a8d931SSreekanth Reddy 		    ioc_status);
563264a8d931SSreekanth Reddy 		goto out_failed;
563364a8d931SSreekanth Reddy 	}
563464a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
563564a8d931SSreekanth Reddy 
563664a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
563764a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg1, pg_sz)) {
563864a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page1 read failed\n");
563964a8d931SSreekanth Reddy 		goto out_failed;
564064a8d931SSreekanth Reddy 	}
564164a8d931SSreekanth Reddy 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
564264a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page1 read failed with ioc_status(0x%04x)\n",
564364a8d931SSreekanth Reddy 		    ioc_status);
564464a8d931SSreekanth Reddy 		goto out_failed;
564564a8d931SSreekanth Reddy 	}
564664a8d931SSreekanth Reddy 	return 0;
564764a8d931SSreekanth Reddy out_failed:
564864a8d931SSreekanth Reddy 	return -1;
564964a8d931SSreekanth Reddy }
565064a8d931SSreekanth Reddy 
565164a8d931SSreekanth Reddy /**
565264a8d931SSreekanth Reddy  * mpi3mr_cfg_set_sas_io_unit_pg1 - Write SASIOUnit page1
565364a8d931SSreekanth Reddy  * @mrioc: Adapter instance reference
565464a8d931SSreekanth Reddy  * @sas_io_unit_pg1: Pointer to the SAS IO Unit page 1 to write
565564a8d931SSreekanth Reddy  * @pg_sz: Size of the memory allocated to the page pointer
565664a8d931SSreekanth Reddy  *
565764a8d931SSreekanth Reddy  * This is handler for config page write for the SAS IO Unit
565864a8d931SSreekanth Reddy  * page1. This routine checks ioc_status to decide whether the
565964a8d931SSreekanth Reddy  * page read is success or not. This will modify both current
566064a8d931SSreekanth Reddy  * and persistent page.
566164a8d931SSreekanth Reddy  *
566264a8d931SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
566364a8d931SSreekanth Reddy  */
566464a8d931SSreekanth Reddy int mpi3mr_cfg_set_sas_io_unit_pg1(struct mpi3mr_ioc *mrioc,
566564a8d931SSreekanth Reddy 	struct mpi3_sas_io_unit_page1 *sas_io_unit_pg1, u16 pg_sz)
566664a8d931SSreekanth Reddy {
566764a8d931SSreekanth Reddy 	struct mpi3_config_page_header cfg_hdr;
566864a8d931SSreekanth Reddy 	struct mpi3_config_request cfg_req;
566964a8d931SSreekanth Reddy 	u16 ioc_status = 0;
567064a8d931SSreekanth Reddy 
567164a8d931SSreekanth Reddy 	memset(&cfg_hdr, 0, sizeof(cfg_hdr));
567264a8d931SSreekanth Reddy 	memset(&cfg_req, 0, sizeof(cfg_req));
567364a8d931SSreekanth Reddy 
567464a8d931SSreekanth Reddy 	cfg_req.function = MPI3_FUNCTION_CONFIG;
567564a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
567664a8d931SSreekanth Reddy 	cfg_req.page_type = MPI3_CONFIG_PAGETYPE_SAS_IO_UNIT;
567764a8d931SSreekanth Reddy 	cfg_req.page_number = 1;
567864a8d931SSreekanth Reddy 	cfg_req.page_address = 0;
567964a8d931SSreekanth Reddy 
568064a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
568164a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
568264a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page1 header read failed\n");
568364a8d931SSreekanth Reddy 		goto out_failed;
568464a8d931SSreekanth Reddy 	}
568564a8d931SSreekanth Reddy 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
568664a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page1 header read failed with ioc_status(0x%04x)\n",
568764a8d931SSreekanth Reddy 		    ioc_status);
568864a8d931SSreekanth Reddy 		goto out_failed;
568964a8d931SSreekanth Reddy 	}
569064a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_WRITE_CURRENT;
569164a8d931SSreekanth Reddy 
569264a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
569364a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg1, pg_sz)) {
569464a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page1 write current failed\n");
569564a8d931SSreekanth Reddy 		goto out_failed;
569664a8d931SSreekanth Reddy 	}
569764a8d931SSreekanth Reddy 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
569864a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page1 write current failed with ioc_status(0x%04x)\n",
569964a8d931SSreekanth Reddy 		    ioc_status);
570064a8d931SSreekanth Reddy 		goto out_failed;
570164a8d931SSreekanth Reddy 	}
570264a8d931SSreekanth Reddy 
570364a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_WRITE_PERSISTENT;
570464a8d931SSreekanth Reddy 
570564a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
570664a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, sas_io_unit_pg1, pg_sz)) {
570764a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page1 write persistent failed\n");
570864a8d931SSreekanth Reddy 		goto out_failed;
570964a8d931SSreekanth Reddy 	}
571064a8d931SSreekanth Reddy 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
571164a8d931SSreekanth Reddy 		ioc_err(mrioc, "sas io unit page1 write persistent failed with ioc_status(0x%04x)\n",
571264a8d931SSreekanth Reddy 		    ioc_status);
571364a8d931SSreekanth Reddy 		goto out_failed;
571464a8d931SSreekanth Reddy 	}
571564a8d931SSreekanth Reddy 	return 0;
571664a8d931SSreekanth Reddy out_failed:
571764a8d931SSreekanth Reddy 	return -1;
571864a8d931SSreekanth Reddy }
571964a8d931SSreekanth Reddy 
572064a8d931SSreekanth Reddy /**
572164a8d931SSreekanth Reddy  * mpi3mr_cfg_get_driver_pg1 - Read current Driver page1
572264a8d931SSreekanth Reddy  * @mrioc: Adapter instance reference
572364a8d931SSreekanth Reddy  * @driver_pg1: Pointer to return Driver page 1
572464a8d931SSreekanth Reddy  * @pg_sz: Size of the memory allocated to the page pointer
572564a8d931SSreekanth Reddy  *
572664a8d931SSreekanth Reddy  * This is handler for config page read for the Driver page1.
572764a8d931SSreekanth Reddy  * This routine checks ioc_status to decide whether the page
572864a8d931SSreekanth Reddy  * read is success or not.
572964a8d931SSreekanth Reddy  *
573064a8d931SSreekanth Reddy  * Return: 0 on success, non-zero on failure.
573164a8d931SSreekanth Reddy  */
573264a8d931SSreekanth Reddy int mpi3mr_cfg_get_driver_pg1(struct mpi3mr_ioc *mrioc,
573364a8d931SSreekanth Reddy 	struct mpi3_driver_page1 *driver_pg1, u16 pg_sz)
573464a8d931SSreekanth Reddy {
573564a8d931SSreekanth Reddy 	struct mpi3_config_page_header cfg_hdr;
573664a8d931SSreekanth Reddy 	struct mpi3_config_request cfg_req;
573764a8d931SSreekanth Reddy 	u16 ioc_status = 0;
573864a8d931SSreekanth Reddy 
573964a8d931SSreekanth Reddy 	memset(driver_pg1, 0, pg_sz);
574064a8d931SSreekanth Reddy 	memset(&cfg_hdr, 0, sizeof(cfg_hdr));
574164a8d931SSreekanth Reddy 	memset(&cfg_req, 0, sizeof(cfg_req));
574264a8d931SSreekanth Reddy 
574364a8d931SSreekanth Reddy 	cfg_req.function = MPI3_FUNCTION_CONFIG;
574464a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_PAGE_HEADER;
574564a8d931SSreekanth Reddy 	cfg_req.page_type = MPI3_CONFIG_PAGETYPE_DRIVER;
574664a8d931SSreekanth Reddy 	cfg_req.page_number = 1;
574764a8d931SSreekanth Reddy 	cfg_req.page_address = 0;
574864a8d931SSreekanth Reddy 
574964a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, NULL,
575064a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, &cfg_hdr, sizeof(cfg_hdr))) {
575164a8d931SSreekanth Reddy 		ioc_err(mrioc, "driver page1 header read failed\n");
575264a8d931SSreekanth Reddy 		goto out_failed;
575364a8d931SSreekanth Reddy 	}
575464a8d931SSreekanth Reddy 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
575564a8d931SSreekanth Reddy 		ioc_err(mrioc, "driver page1 header read failed with ioc_status(0x%04x)\n",
575664a8d931SSreekanth Reddy 		    ioc_status);
575764a8d931SSreekanth Reddy 		goto out_failed;
575864a8d931SSreekanth Reddy 	}
575964a8d931SSreekanth Reddy 	cfg_req.action = MPI3_CONFIG_ACTION_READ_CURRENT;
576064a8d931SSreekanth Reddy 
576164a8d931SSreekanth Reddy 	if (mpi3mr_process_cfg_req(mrioc, &cfg_req, &cfg_hdr,
576264a8d931SSreekanth Reddy 	    MPI3MR_INTADMCMD_TIMEOUT, &ioc_status, driver_pg1, pg_sz)) {
576364a8d931SSreekanth Reddy 		ioc_err(mrioc, "driver page1 read failed\n");
576464a8d931SSreekanth Reddy 		goto out_failed;
576564a8d931SSreekanth Reddy 	}
576664a8d931SSreekanth Reddy 	if (ioc_status != MPI3_IOCSTATUS_SUCCESS) {
576764a8d931SSreekanth Reddy 		ioc_err(mrioc, "driver page1 read failed with ioc_status(0x%04x)\n",
576864a8d931SSreekanth Reddy 		    ioc_status);
576964a8d931SSreekanth Reddy 		goto out_failed;
577064a8d931SSreekanth Reddy 	}
577164a8d931SSreekanth Reddy 	return 0;
577264a8d931SSreekanth Reddy out_failed:
577364a8d931SSreekanth Reddy 	return -1;
577464a8d931SSreekanth Reddy }
5775