xref: /linux/drivers/scsi/mpt3sas/mpt3sas_ctl.c (revision 42f687038bcc34aa919e0e4c29b04e4cda3f6a79)
1f92363d1SSreekanth Reddy /*
2f92363d1SSreekanth Reddy  * Management Module Support for MPT (Message Passing Technology) based
3f92363d1SSreekanth Reddy  * controllers
4f92363d1SSreekanth Reddy  *
5f92363d1SSreekanth Reddy  * This code is based on drivers/scsi/mpt3sas/mpt3sas_ctl.c
6a4ffce0dSSreekanth Reddy  * Copyright (C) 2012-2014  LSI Corporation
7a03bd153SSreekanth Reddy  * Copyright (C) 2013-2014 Avago Technologies
8a03bd153SSreekanth Reddy  *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
9f92363d1SSreekanth Reddy  *
10f92363d1SSreekanth Reddy  * This program is free software; you can redistribute it and/or
11f92363d1SSreekanth Reddy  * modify it under the terms of the GNU General Public License
12f92363d1SSreekanth Reddy  * as published by the Free Software Foundation; either version 2
13f92363d1SSreekanth Reddy  * of the License, or (at your option) any later version.
14f92363d1SSreekanth Reddy  *
15f92363d1SSreekanth Reddy  * This program is distributed in the hope that it will be useful,
16f92363d1SSreekanth Reddy  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17f92363d1SSreekanth Reddy  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18f92363d1SSreekanth Reddy  * GNU General Public License for more details.
19f92363d1SSreekanth Reddy  *
20f92363d1SSreekanth Reddy  * NO WARRANTY
21f92363d1SSreekanth Reddy  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22f92363d1SSreekanth Reddy  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23f92363d1SSreekanth Reddy  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24f92363d1SSreekanth Reddy  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25f92363d1SSreekanth Reddy  * solely responsible for determining the appropriateness of using and
26f92363d1SSreekanth Reddy  * distributing the Program and assumes all risks associated with its
27f92363d1SSreekanth Reddy  * exercise of rights under this Agreement, including but not limited to
28f92363d1SSreekanth Reddy  * the risks and costs of program errors, damage to or loss of data,
29f92363d1SSreekanth Reddy  * programs or equipment, and unavailability or interruption of operations.
30f92363d1SSreekanth Reddy 
31f92363d1SSreekanth Reddy  * DISCLAIMER OF LIABILITY
32f92363d1SSreekanth Reddy  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33f92363d1SSreekanth Reddy  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34f92363d1SSreekanth Reddy  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35f92363d1SSreekanth Reddy  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36f92363d1SSreekanth Reddy  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37f92363d1SSreekanth Reddy  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38f92363d1SSreekanth Reddy  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39f92363d1SSreekanth Reddy 
40f92363d1SSreekanth Reddy  * You should have received a copy of the GNU General Public License
41f92363d1SSreekanth Reddy  * along with this program; if not, write to the Free Software
42f92363d1SSreekanth Reddy  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
43f92363d1SSreekanth Reddy  * USA.
44f92363d1SSreekanth Reddy  */
45f92363d1SSreekanth Reddy 
46f92363d1SSreekanth Reddy #include <linux/kernel.h>
47f92363d1SSreekanth Reddy #include <linux/module.h>
48f92363d1SSreekanth Reddy #include <linux/errno.h>
49f92363d1SSreekanth Reddy #include <linux/init.h>
50f92363d1SSreekanth Reddy #include <linux/slab.h>
51f92363d1SSreekanth Reddy #include <linux/types.h>
52f92363d1SSreekanth Reddy #include <linux/pci.h>
53f92363d1SSreekanth Reddy #include <linux/delay.h>
54f92363d1SSreekanth Reddy #include <linux/compat.h>
55f92363d1SSreekanth Reddy #include <linux/poll.h>
56f92363d1SSreekanth Reddy 
57f92363d1SSreekanth Reddy #include <linux/io.h>
58f92363d1SSreekanth Reddy #include <linux/uaccess.h>
59f92363d1SSreekanth Reddy 
60f92363d1SSreekanth Reddy #include "mpt3sas_base.h"
61f92363d1SSreekanth Reddy #include "mpt3sas_ctl.h"
62f92363d1SSreekanth Reddy 
63f92363d1SSreekanth Reddy 
64f92363d1SSreekanth Reddy static struct fasync_struct *async_queue;
65f92363d1SSreekanth Reddy static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
66f92363d1SSreekanth Reddy 
67f92363d1SSreekanth Reddy 
68f92363d1SSreekanth Reddy /**
69f92363d1SSreekanth Reddy  * enum block_state - blocking state
70f92363d1SSreekanth Reddy  * @NON_BLOCKING: non blocking
71f92363d1SSreekanth Reddy  * @BLOCKING: blocking
72f92363d1SSreekanth Reddy  *
73f92363d1SSreekanth Reddy  * These states are for ioctls that need to wait for a response
74f92363d1SSreekanth Reddy  * from firmware, so they probably require sleep.
75f92363d1SSreekanth Reddy  */
76f92363d1SSreekanth Reddy enum block_state {
77f92363d1SSreekanth Reddy 	NON_BLOCKING,
78f92363d1SSreekanth Reddy 	BLOCKING,
79f92363d1SSreekanth Reddy };
80f92363d1SSreekanth Reddy 
81f92363d1SSreekanth Reddy /**
82f92363d1SSreekanth Reddy  * _ctl_display_some_debug - debug routine
83f92363d1SSreekanth Reddy  * @ioc: per adapter object
84f92363d1SSreekanth Reddy  * @smid: system request message index
85f92363d1SSreekanth Reddy  * @calling_function_name: string pass from calling function
86f92363d1SSreekanth Reddy  * @mpi_reply: reply message frame
87f92363d1SSreekanth Reddy  * Context: none.
88f92363d1SSreekanth Reddy  *
89f92363d1SSreekanth Reddy  * Function for displaying debug info helpful when debugging issues
90f92363d1SSreekanth Reddy  * in this module.
91f92363d1SSreekanth Reddy  */
92f92363d1SSreekanth Reddy static void
93f92363d1SSreekanth Reddy _ctl_display_some_debug(struct MPT3SAS_ADAPTER *ioc, u16 smid,
94f92363d1SSreekanth Reddy 	char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
95f92363d1SSreekanth Reddy {
96f92363d1SSreekanth Reddy 	Mpi2ConfigRequest_t *mpi_request;
97f92363d1SSreekanth Reddy 	char *desc = NULL;
98f92363d1SSreekanth Reddy 
99f92363d1SSreekanth Reddy 	if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
100f92363d1SSreekanth Reddy 		return;
101f92363d1SSreekanth Reddy 
102f92363d1SSreekanth Reddy 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
103f92363d1SSreekanth Reddy 	switch (mpi_request->Function) {
104f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SCSI_IO_REQUEST:
105f92363d1SSreekanth Reddy 	{
106f92363d1SSreekanth Reddy 		Mpi2SCSIIORequest_t *scsi_request =
107f92363d1SSreekanth Reddy 		    (Mpi2SCSIIORequest_t *)mpi_request;
108f92363d1SSreekanth Reddy 
109f92363d1SSreekanth Reddy 		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
110f92363d1SSreekanth Reddy 		    "scsi_io, cmd(0x%02x), cdb_len(%d)",
111f92363d1SSreekanth Reddy 		    scsi_request->CDB.CDB32[0],
112f92363d1SSreekanth Reddy 		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
113f92363d1SSreekanth Reddy 		desc = ioc->tmp_string;
114f92363d1SSreekanth Reddy 		break;
115f92363d1SSreekanth Reddy 	}
116f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SCSI_TASK_MGMT:
117f92363d1SSreekanth Reddy 		desc = "task_mgmt";
118f92363d1SSreekanth Reddy 		break;
119f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_IOC_INIT:
120f92363d1SSreekanth Reddy 		desc = "ioc_init";
121f92363d1SSreekanth Reddy 		break;
122f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_IOC_FACTS:
123f92363d1SSreekanth Reddy 		desc = "ioc_facts";
124f92363d1SSreekanth Reddy 		break;
125f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_CONFIG:
126f92363d1SSreekanth Reddy 	{
127f92363d1SSreekanth Reddy 		Mpi2ConfigRequest_t *config_request =
128f92363d1SSreekanth Reddy 		    (Mpi2ConfigRequest_t *)mpi_request;
129f92363d1SSreekanth Reddy 
130f92363d1SSreekanth Reddy 		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
131f92363d1SSreekanth Reddy 		    "config, type(0x%02x), ext_type(0x%02x), number(%d)",
132f92363d1SSreekanth Reddy 		    (config_request->Header.PageType &
133f92363d1SSreekanth Reddy 		     MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
134f92363d1SSreekanth Reddy 		    config_request->Header.PageNumber);
135f92363d1SSreekanth Reddy 		desc = ioc->tmp_string;
136f92363d1SSreekanth Reddy 		break;
137f92363d1SSreekanth Reddy 	}
138f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_PORT_FACTS:
139f92363d1SSreekanth Reddy 		desc = "port_facts";
140f92363d1SSreekanth Reddy 		break;
141f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_PORT_ENABLE:
142f92363d1SSreekanth Reddy 		desc = "port_enable";
143f92363d1SSreekanth Reddy 		break;
144f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_EVENT_NOTIFICATION:
145f92363d1SSreekanth Reddy 		desc = "event_notification";
146f92363d1SSreekanth Reddy 		break;
147f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_FW_DOWNLOAD:
148f92363d1SSreekanth Reddy 		desc = "fw_download";
149f92363d1SSreekanth Reddy 		break;
150f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_FW_UPLOAD:
151f92363d1SSreekanth Reddy 		desc = "fw_upload";
152f92363d1SSreekanth Reddy 		break;
153f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_RAID_ACTION:
154f92363d1SSreekanth Reddy 		desc = "raid_action";
155f92363d1SSreekanth Reddy 		break;
156f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
157f92363d1SSreekanth Reddy 	{
158f92363d1SSreekanth Reddy 		Mpi2SCSIIORequest_t *scsi_request =
159f92363d1SSreekanth Reddy 		    (Mpi2SCSIIORequest_t *)mpi_request;
160f92363d1SSreekanth Reddy 
161f92363d1SSreekanth Reddy 		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
162f92363d1SSreekanth Reddy 		    "raid_pass, cmd(0x%02x), cdb_len(%d)",
163f92363d1SSreekanth Reddy 		    scsi_request->CDB.CDB32[0],
164f92363d1SSreekanth Reddy 		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
165f92363d1SSreekanth Reddy 		desc = ioc->tmp_string;
166f92363d1SSreekanth Reddy 		break;
167f92363d1SSreekanth Reddy 	}
168f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
169f92363d1SSreekanth Reddy 		desc = "sas_iounit_cntl";
170f92363d1SSreekanth Reddy 		break;
171f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SATA_PASSTHROUGH:
172f92363d1SSreekanth Reddy 		desc = "sata_pass";
173f92363d1SSreekanth Reddy 		break;
174f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_DIAG_BUFFER_POST:
175f92363d1SSreekanth Reddy 		desc = "diag_buffer_post";
176f92363d1SSreekanth Reddy 		break;
177f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_DIAG_RELEASE:
178f92363d1SSreekanth Reddy 		desc = "diag_release";
179f92363d1SSreekanth Reddy 		break;
180f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SMP_PASSTHROUGH:
181f92363d1SSreekanth Reddy 		desc = "smp_passthrough";
182f92363d1SSreekanth Reddy 		break;
1835b061980SSreekanth Reddy 	case MPI2_FUNCTION_TOOLBOX:
1845b061980SSreekanth Reddy 		desc = "toolbox";
1855b061980SSreekanth Reddy 		break;
1865b061980SSreekanth Reddy 	case MPI2_FUNCTION_NVME_ENCAPSULATED:
1875b061980SSreekanth Reddy 		desc = "nvme_encapsulated";
1885b061980SSreekanth Reddy 		break;
189f92363d1SSreekanth Reddy 	}
190f92363d1SSreekanth Reddy 
191f92363d1SSreekanth Reddy 	if (!desc)
192f92363d1SSreekanth Reddy 		return;
193f92363d1SSreekanth Reddy 
194919d8a3fSJoe Perches 	ioc_info(ioc, "%s: %s, smid(%d)\n", calling_function_name, desc, smid);
195f92363d1SSreekanth Reddy 
196f92363d1SSreekanth Reddy 	if (!mpi_reply)
197f92363d1SSreekanth Reddy 		return;
198f92363d1SSreekanth Reddy 
199f92363d1SSreekanth Reddy 	if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
200919d8a3fSJoe Perches 		ioc_info(ioc, "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
201919d8a3fSJoe Perches 			 le16_to_cpu(mpi_reply->IOCStatus),
202f92363d1SSreekanth Reddy 			 le32_to_cpu(mpi_reply->IOCLogInfo));
203f92363d1SSreekanth Reddy 
204f92363d1SSreekanth Reddy 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
205f92363d1SSreekanth Reddy 	    mpi_request->Function ==
206f92363d1SSreekanth Reddy 	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
207f92363d1SSreekanth Reddy 		Mpi2SCSIIOReply_t *scsi_reply =
208f92363d1SSreekanth Reddy 		    (Mpi2SCSIIOReply_t *)mpi_reply;
209f92363d1SSreekanth Reddy 		struct _sas_device *sas_device = NULL;
21045aa6a1aSSuganath Prabu Subramani 		struct _pcie_device *pcie_device = NULL;
211f92363d1SSreekanth Reddy 
21245aa6a1aSSuganath Prabu Subramani 		sas_device = mpt3sas_get_sdev_by_handle(ioc,
213f92363d1SSreekanth Reddy 		    le16_to_cpu(scsi_reply->DevHandle));
214f92363d1SSreekanth Reddy 		if (sas_device) {
215919d8a3fSJoe Perches 			ioc_warn(ioc, "\tsas_address(0x%016llx), phy(%d)\n",
216919d8a3fSJoe Perches 				 (u64)sas_device->sas_address,
217919d8a3fSJoe Perches 				 sas_device->phy);
218919d8a3fSJoe Perches 			ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
219919d8a3fSJoe Perches 				 (u64)sas_device->enclosure_logical_id,
220919d8a3fSJoe Perches 				 sas_device->slot);
22145aa6a1aSSuganath Prabu Subramani 			sas_device_put(sas_device);
222f92363d1SSreekanth Reddy 		}
22345aa6a1aSSuganath Prabu Subramani 		if (!sas_device) {
22445aa6a1aSSuganath Prabu Subramani 			pcie_device = mpt3sas_get_pdev_by_handle(ioc,
22545aa6a1aSSuganath Prabu Subramani 				le16_to_cpu(scsi_reply->DevHandle));
22645aa6a1aSSuganath Prabu Subramani 			if (pcie_device) {
227919d8a3fSJoe Perches 				ioc_warn(ioc, "\tWWID(0x%016llx), port(%d)\n",
22845aa6a1aSSuganath Prabu Subramani 					 (unsigned long long)pcie_device->wwid,
22945aa6a1aSSuganath Prabu Subramani 					 pcie_device->port_num);
23045aa6a1aSSuganath Prabu Subramani 				if (pcie_device->enclosure_handle != 0)
231919d8a3fSJoe Perches 					ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
232919d8a3fSJoe Perches 						 (u64)pcie_device->enclosure_logical_id,
23345aa6a1aSSuganath Prabu Subramani 						 pcie_device->slot);
23445aa6a1aSSuganath Prabu Subramani 				pcie_device_put(pcie_device);
23545aa6a1aSSuganath Prabu Subramani 			}
23645aa6a1aSSuganath Prabu Subramani 		}
237f92363d1SSreekanth Reddy 		if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
238919d8a3fSJoe Perches 			ioc_info(ioc, "\tscsi_state(0x%02x), scsi_status(0x%02x)\n",
239f92363d1SSreekanth Reddy 				 scsi_reply->SCSIState,
240f92363d1SSreekanth Reddy 				 scsi_reply->SCSIStatus);
241f92363d1SSreekanth Reddy 	}
242f92363d1SSreekanth Reddy }
243f92363d1SSreekanth Reddy 
244f92363d1SSreekanth Reddy /**
245f92363d1SSreekanth Reddy  * mpt3sas_ctl_done - ctl module completion routine
246f92363d1SSreekanth Reddy  * @ioc: per adapter object
247f92363d1SSreekanth Reddy  * @smid: system request message index
248f92363d1SSreekanth Reddy  * @msix_index: MSIX table index supplied by the OS
249f92363d1SSreekanth Reddy  * @reply: reply message frame(lower 32bit addr)
250f92363d1SSreekanth Reddy  * Context: none.
251f92363d1SSreekanth Reddy  *
252f92363d1SSreekanth Reddy  * The callback handler when using ioc->ctl_cb_idx.
253f92363d1SSreekanth Reddy  *
2544beb4867SBart Van Assche  * Return: 1 meaning mf should be freed from _base_interrupt
255f92363d1SSreekanth Reddy  *         0 means the mf is freed from this function.
256f92363d1SSreekanth Reddy  */
257f92363d1SSreekanth Reddy u8
258f92363d1SSreekanth Reddy mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
259f92363d1SSreekanth Reddy 	u32 reply)
260f92363d1SSreekanth Reddy {
261f92363d1SSreekanth Reddy 	MPI2DefaultReply_t *mpi_reply;
262f92363d1SSreekanth Reddy 	Mpi2SCSIIOReply_t *scsiio_reply;
263aff39e61SSuganath Prabu Subramani 	Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
264f92363d1SSreekanth Reddy 	const void *sense_data;
265f92363d1SSreekanth Reddy 	u32 sz;
266f92363d1SSreekanth Reddy 
267f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
268f92363d1SSreekanth Reddy 		return 1;
269f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.smid != smid)
270f92363d1SSreekanth Reddy 		return 1;
271f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
272f92363d1SSreekanth Reddy 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
273f92363d1SSreekanth Reddy 	if (mpi_reply) {
274f92363d1SSreekanth Reddy 		memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
275f92363d1SSreekanth Reddy 		ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
276f92363d1SSreekanth Reddy 		/* get sense data */
277f92363d1SSreekanth Reddy 		if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
278f92363d1SSreekanth Reddy 		    mpi_reply->Function ==
279f92363d1SSreekanth Reddy 		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
280f92363d1SSreekanth Reddy 			scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
281f92363d1SSreekanth Reddy 			if (scsiio_reply->SCSIState &
282f92363d1SSreekanth Reddy 			    MPI2_SCSI_STATE_AUTOSENSE_VALID) {
283f92363d1SSreekanth Reddy 				sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
284f92363d1SSreekanth Reddy 				    le32_to_cpu(scsiio_reply->SenseCount));
285f92363d1SSreekanth Reddy 				sense_data = mpt3sas_base_get_sense_buffer(ioc,
286f92363d1SSreekanth Reddy 				    smid);
287f92363d1SSreekanth Reddy 				memcpy(ioc->ctl_cmds.sense, sense_data, sz);
288f92363d1SSreekanth Reddy 			}
289f92363d1SSreekanth Reddy 		}
290aff39e61SSuganath Prabu Subramani 		/*
291aff39e61SSuganath Prabu Subramani 		 * Get Error Response data for NVMe device. The ctl_cmds.sense
292aff39e61SSuganath Prabu Subramani 		 * buffer is used to store the Error Response data.
293aff39e61SSuganath Prabu Subramani 		 */
294aff39e61SSuganath Prabu Subramani 		if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
295aff39e61SSuganath Prabu Subramani 			nvme_error_reply =
296aff39e61SSuganath Prabu Subramani 			    (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
297aff39e61SSuganath Prabu Subramani 			sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
298cf6bf971SChaitra P B 			    le16_to_cpu(nvme_error_reply->ErrorResponseCount));
299aff39e61SSuganath Prabu Subramani 			sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
300aff39e61SSuganath Prabu Subramani 			memcpy(ioc->ctl_cmds.sense, sense_data, sz);
301aff39e61SSuganath Prabu Subramani 		}
302f92363d1SSreekanth Reddy 	}
303016d5c35SSuganath Prabu Subramani 
304f92363d1SSreekanth Reddy 	_ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
305f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
306f92363d1SSreekanth Reddy 	complete(&ioc->ctl_cmds.done);
307f92363d1SSreekanth Reddy 	return 1;
308f92363d1SSreekanth Reddy }
309f92363d1SSreekanth Reddy 
310f92363d1SSreekanth Reddy /**
311f92363d1SSreekanth Reddy  * _ctl_check_event_type - determines when an event needs logging
312f92363d1SSreekanth Reddy  * @ioc: per adapter object
313f92363d1SSreekanth Reddy  * @event: firmware event
314f92363d1SSreekanth Reddy  *
315f92363d1SSreekanth Reddy  * The bitmask in ioc->event_type[] indicates which events should be
316f92363d1SSreekanth Reddy  * be saved in the driver event_log.  This bitmask is set by application.
317f92363d1SSreekanth Reddy  *
3184beb4867SBart Van Assche  * Return: 1 when event should be captured, or zero means no match.
319f92363d1SSreekanth Reddy  */
320f92363d1SSreekanth Reddy static int
321f92363d1SSreekanth Reddy _ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
322f92363d1SSreekanth Reddy {
323f92363d1SSreekanth Reddy 	u16 i;
324f92363d1SSreekanth Reddy 	u32 desired_event;
325f92363d1SSreekanth Reddy 
326f92363d1SSreekanth Reddy 	if (event >= 128 || !event || !ioc->event_log)
327f92363d1SSreekanth Reddy 		return 0;
328f92363d1SSreekanth Reddy 
329f92363d1SSreekanth Reddy 	desired_event = (1 << (event % 32));
330f92363d1SSreekanth Reddy 	if (!desired_event)
331f92363d1SSreekanth Reddy 		desired_event = 1;
332f92363d1SSreekanth Reddy 	i = event / 32;
333f92363d1SSreekanth Reddy 	return desired_event & ioc->event_type[i];
334f92363d1SSreekanth Reddy }
335f92363d1SSreekanth Reddy 
336f92363d1SSreekanth Reddy /**
337f92363d1SSreekanth Reddy  * mpt3sas_ctl_add_to_event_log - add event
338f92363d1SSreekanth Reddy  * @ioc: per adapter object
339f92363d1SSreekanth Reddy  * @mpi_reply: reply message frame
340f92363d1SSreekanth Reddy  */
341f92363d1SSreekanth Reddy void
342f92363d1SSreekanth Reddy mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
343f92363d1SSreekanth Reddy 	Mpi2EventNotificationReply_t *mpi_reply)
344f92363d1SSreekanth Reddy {
345f92363d1SSreekanth Reddy 	struct MPT3_IOCTL_EVENTS *event_log;
346f92363d1SSreekanth Reddy 	u16 event;
347f92363d1SSreekanth Reddy 	int i;
348f92363d1SSreekanth Reddy 	u32 sz, event_data_sz;
349f92363d1SSreekanth Reddy 	u8 send_aen = 0;
350f92363d1SSreekanth Reddy 
351f92363d1SSreekanth Reddy 	if (!ioc->event_log)
352f92363d1SSreekanth Reddy 		return;
353f92363d1SSreekanth Reddy 
354f92363d1SSreekanth Reddy 	event = le16_to_cpu(mpi_reply->Event);
355f92363d1SSreekanth Reddy 
356f92363d1SSreekanth Reddy 	if (_ctl_check_event_type(ioc, event)) {
357f92363d1SSreekanth Reddy 
358f92363d1SSreekanth Reddy 		/* insert entry into circular event_log */
359f92363d1SSreekanth Reddy 		i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
360f92363d1SSreekanth Reddy 		event_log = ioc->event_log;
361f92363d1SSreekanth Reddy 		event_log[i].event = event;
362f92363d1SSreekanth Reddy 		event_log[i].context = ioc->event_context++;
363f92363d1SSreekanth Reddy 
364f92363d1SSreekanth Reddy 		event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
365f92363d1SSreekanth Reddy 		sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
366f92363d1SSreekanth Reddy 		memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
367f92363d1SSreekanth Reddy 		memcpy(event_log[i].data, mpi_reply->EventData, sz);
368f92363d1SSreekanth Reddy 		send_aen = 1;
369f92363d1SSreekanth Reddy 	}
370f92363d1SSreekanth Reddy 
371f92363d1SSreekanth Reddy 	/* This aen_event_read_flag flag is set until the
372f92363d1SSreekanth Reddy 	 * application has read the event log.
373f92363d1SSreekanth Reddy 	 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
374f92363d1SSreekanth Reddy 	 */
375f92363d1SSreekanth Reddy 	if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
376f92363d1SSreekanth Reddy 	    (send_aen && !ioc->aen_event_read_flag)) {
377f92363d1SSreekanth Reddy 		ioc->aen_event_read_flag = 1;
378f92363d1SSreekanth Reddy 		wake_up_interruptible(&ctl_poll_wait);
379f92363d1SSreekanth Reddy 		if (async_queue)
380f92363d1SSreekanth Reddy 			kill_fasync(&async_queue, SIGIO, POLL_IN);
381f92363d1SSreekanth Reddy 	}
382f92363d1SSreekanth Reddy }
383f92363d1SSreekanth Reddy 
384f92363d1SSreekanth Reddy /**
385f92363d1SSreekanth Reddy  * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
386f92363d1SSreekanth Reddy  * @ioc: per adapter object
387f92363d1SSreekanth Reddy  * @msix_index: MSIX table index supplied by the OS
388f92363d1SSreekanth Reddy  * @reply: reply message frame(lower 32bit addr)
389f92363d1SSreekanth Reddy  * Context: interrupt.
390f92363d1SSreekanth Reddy  *
391f92363d1SSreekanth Reddy  * This function merely adds a new work task into ioc->firmware_event_thread.
392f92363d1SSreekanth Reddy  * The tasks are worked from _firmware_event_work in user context.
393f92363d1SSreekanth Reddy  *
3944beb4867SBart Van Assche  * Return: 1 meaning mf should be freed from _base_interrupt
395f92363d1SSreekanth Reddy  *         0 means the mf is freed from this function.
396f92363d1SSreekanth Reddy  */
397f92363d1SSreekanth Reddy u8
398f92363d1SSreekanth Reddy mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
399f92363d1SSreekanth Reddy 	u32 reply)
400f92363d1SSreekanth Reddy {
401f92363d1SSreekanth Reddy 	Mpi2EventNotificationReply_t *mpi_reply;
402f92363d1SSreekanth Reddy 
403f92363d1SSreekanth Reddy 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
404869817f9SSuganath prabu Subramani 	if (mpi_reply)
405f92363d1SSreekanth Reddy 		mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
406f92363d1SSreekanth Reddy 	return 1;
407f92363d1SSreekanth Reddy }
408f92363d1SSreekanth Reddy 
409f92363d1SSreekanth Reddy /**
410f92363d1SSreekanth Reddy  * _ctl_verify_adapter - validates ioc_number passed from application
4114beb4867SBart Van Assche  * @ioc_number: ?
412f92363d1SSreekanth Reddy  * @iocpp: The ioc pointer is returned in this.
413c84b06a4SSreekanth Reddy  * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
414b130b0d5SSuganath prabu Subramani  * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
415f92363d1SSreekanth Reddy  *
4164beb4867SBart Van Assche  * Return: (-1) means error, else ioc_number.
417f92363d1SSreekanth Reddy  */
418f92363d1SSreekanth Reddy static int
419c84b06a4SSreekanth Reddy _ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
420c84b06a4SSreekanth Reddy 							int mpi_version)
421f92363d1SSreekanth Reddy {
422f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc;
423b130b0d5SSuganath prabu Subramani 	int version = 0;
42408c4d550SSreekanth Reddy 	/* global ioc lock to protect controller on list operations */
42508c4d550SSreekanth Reddy 	spin_lock(&gioc_lock);
426f92363d1SSreekanth Reddy 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
427f92363d1SSreekanth Reddy 		if (ioc->id != ioc_number)
428f92363d1SSreekanth Reddy 			continue;
429c84b06a4SSreekanth Reddy 		/* Check whether this ioctl command is from right
430c84b06a4SSreekanth Reddy 		 * ioctl device or not, if not continue the search.
431c84b06a4SSreekanth Reddy 		 */
432b130b0d5SSuganath prabu Subramani 		version = ioc->hba_mpi_version_belonged;
433b130b0d5SSuganath prabu Subramani 		/* MPI25_VERSION and MPI26_VERSION uses same ioctl
434b130b0d5SSuganath prabu Subramani 		 * device.
435b130b0d5SSuganath prabu Subramani 		 */
436b130b0d5SSuganath prabu Subramani 		if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
437b130b0d5SSuganath prabu Subramani 			if ((version == MPI25_VERSION) ||
438b130b0d5SSuganath prabu Subramani 				(version == MPI26_VERSION))
439b130b0d5SSuganath prabu Subramani 				goto out;
440b130b0d5SSuganath prabu Subramani 			else
441c84b06a4SSreekanth Reddy 				continue;
442b130b0d5SSuganath prabu Subramani 		} else {
443b130b0d5SSuganath prabu Subramani 			if (version != mpi_version)
444b130b0d5SSuganath prabu Subramani 				continue;
445b130b0d5SSuganath prabu Subramani 		}
446b130b0d5SSuganath prabu Subramani out:
44708c4d550SSreekanth Reddy 		spin_unlock(&gioc_lock);
448f92363d1SSreekanth Reddy 		*iocpp = ioc;
449f92363d1SSreekanth Reddy 		return ioc_number;
450f92363d1SSreekanth Reddy 	}
45108c4d550SSreekanth Reddy 	spin_unlock(&gioc_lock);
452f92363d1SSreekanth Reddy 	*iocpp = NULL;
453f92363d1SSreekanth Reddy 	return -1;
454f92363d1SSreekanth Reddy }
455f92363d1SSreekanth Reddy 
456f92363d1SSreekanth Reddy /**
457f92363d1SSreekanth Reddy  * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
458f92363d1SSreekanth Reddy  * @ioc: per adapter object
459f92363d1SSreekanth Reddy  *
460f92363d1SSreekanth Reddy  * The handler for doing any required cleanup or initialization.
461f92363d1SSreekanth Reddy  */
462c7a35705SBart Van Assche void mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
463f92363d1SSreekanth Reddy {
464f92363d1SSreekanth Reddy 	int i;
465f92363d1SSreekanth Reddy 	u8 issue_reset;
466f92363d1SSreekanth Reddy 
467919d8a3fSJoe Perches 	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
468f92363d1SSreekanth Reddy 	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
469f92363d1SSreekanth Reddy 		if (!(ioc->diag_buffer_status[i] &
470f92363d1SSreekanth Reddy 		      MPT3_DIAG_BUFFER_IS_REGISTERED))
471f92363d1SSreekanth Reddy 			continue;
472f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[i] &
473f92363d1SSreekanth Reddy 		     MPT3_DIAG_BUFFER_IS_RELEASED))
474f92363d1SSreekanth Reddy 			continue;
4754bc50dc1SSreekanth Reddy 
4764bc50dc1SSreekanth Reddy 		/*
4774bc50dc1SSreekanth Reddy 		 * add a log message to indicate the release
4784bc50dc1SSreekanth Reddy 		 */
4794bc50dc1SSreekanth Reddy 		ioc_info(ioc,
4804bc50dc1SSreekanth Reddy 		    "%s: Releasing the trace buffer due to adapter reset.",
4814bc50dc1SSreekanth Reddy 		    __func__);
482f92363d1SSreekanth Reddy 		mpt3sas_send_diag_release(ioc, i, &issue_reset);
483f92363d1SSreekanth Reddy 	}
484c7a35705SBart Van Assche }
485c7a35705SBart Van Assche 
486c7a35705SBart Van Assche /**
48736c6c7f7SSreekanth Reddy  * mpt3sas_ctl_reset_handler - clears outstanding ioctl cmd.
488c7a35705SBart Van Assche  * @ioc: per adapter object
489c7a35705SBart Van Assche  *
490c7a35705SBart Van Assche  * The handler for doing any required cleanup or initialization.
491c7a35705SBart Van Assche  */
49236c6c7f7SSreekanth Reddy void mpt3sas_ctl_clear_outstanding_ioctls(struct MPT3SAS_ADAPTER *ioc)
493c7a35705SBart Van Assche {
49436c6c7f7SSreekanth Reddy 	dtmprintk(ioc,
49536c6c7f7SSreekanth Reddy 	    ioc_info(ioc, "%s: clear outstanding ioctl cmd\n", __func__));
496f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
497f92363d1SSreekanth Reddy 		ioc->ctl_cmds.status |= MPT3_CMD_RESET;
498f92363d1SSreekanth Reddy 		mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
499f92363d1SSreekanth Reddy 		complete(&ioc->ctl_cmds.done);
500f92363d1SSreekanth Reddy 	}
501c7a35705SBart Van Assche }
502c7a35705SBart Van Assche 
503c7a35705SBart Van Assche /**
504c7a35705SBart Van Assche  * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
505c7a35705SBart Van Assche  * @ioc: per adapter object
506c7a35705SBart Van Assche  *
507c7a35705SBart Van Assche  * The handler for doing any required cleanup or initialization.
508c7a35705SBart Van Assche  */
509c7a35705SBart Van Assche void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
510c7a35705SBart Van Assche {
511c7a35705SBart Van Assche 	int i;
512c7a35705SBart Van Assche 
513919d8a3fSJoe Perches 	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
514f92363d1SSreekanth Reddy 
515f92363d1SSreekanth Reddy 	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
516f92363d1SSreekanth Reddy 		if (!(ioc->diag_buffer_status[i] &
517f92363d1SSreekanth Reddy 		      MPT3_DIAG_BUFFER_IS_REGISTERED))
518f92363d1SSreekanth Reddy 			continue;
519f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[i] &
520f92363d1SSreekanth Reddy 		     MPT3_DIAG_BUFFER_IS_RELEASED))
521f92363d1SSreekanth Reddy 			continue;
522f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[i] |=
523f92363d1SSreekanth Reddy 			MPT3_DIAG_BUFFER_IS_DIAG_RESET;
524f92363d1SSreekanth Reddy 	}
525f92363d1SSreekanth Reddy }
526f92363d1SSreekanth Reddy 
527f92363d1SSreekanth Reddy /**
528c84b06a4SSreekanth Reddy  * _ctl_fasync -
5294beb4867SBart Van Assche  * @fd: ?
5304beb4867SBart Van Assche  * @filep: ?
5314beb4867SBart Van Assche  * @mode: ?
532f92363d1SSreekanth Reddy  *
533f92363d1SSreekanth Reddy  * Called when application request fasyn callback handler.
534f92363d1SSreekanth Reddy  */
5358bbb1cf6SCalvin Owens static int
536c84b06a4SSreekanth Reddy _ctl_fasync(int fd, struct file *filep, int mode)
537f92363d1SSreekanth Reddy {
538f92363d1SSreekanth Reddy 	return fasync_helper(fd, filep, mode, &async_queue);
539f92363d1SSreekanth Reddy }
540f92363d1SSreekanth Reddy 
541f92363d1SSreekanth Reddy /**
542c84b06a4SSreekanth Reddy  * _ctl_poll -
5434beb4867SBart Van Assche  * @filep: ?
5444beb4867SBart Van Assche  * @wait: ?
545f92363d1SSreekanth Reddy  *
546f92363d1SSreekanth Reddy  */
547afc9a42bSAl Viro static __poll_t
548c84b06a4SSreekanth Reddy _ctl_poll(struct file *filep, poll_table *wait)
549f92363d1SSreekanth Reddy {
550f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc;
551f92363d1SSreekanth Reddy 
552f92363d1SSreekanth Reddy 	poll_wait(filep, &ctl_poll_wait, wait);
553f92363d1SSreekanth Reddy 
55408c4d550SSreekanth Reddy 	/* global ioc lock to protect controller on list operations */
55508c4d550SSreekanth Reddy 	spin_lock(&gioc_lock);
556f92363d1SSreekanth Reddy 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
55708c4d550SSreekanth Reddy 		if (ioc->aen_event_read_flag) {
55808c4d550SSreekanth Reddy 			spin_unlock(&gioc_lock);
559a9a08845SLinus Torvalds 			return EPOLLIN | EPOLLRDNORM;
560f92363d1SSreekanth Reddy 		}
56108c4d550SSreekanth Reddy 	}
56208c4d550SSreekanth Reddy 	spin_unlock(&gioc_lock);
563f92363d1SSreekanth Reddy 	return 0;
564f92363d1SSreekanth Reddy }
565f92363d1SSreekanth Reddy 
566f92363d1SSreekanth Reddy /**
567f92363d1SSreekanth Reddy  * _ctl_set_task_mid - assign an active smid to tm request
568f92363d1SSreekanth Reddy  * @ioc: per adapter object
5694beb4867SBart Van Assche  * @karg: (struct mpt3_ioctl_command)
5704beb4867SBart Van Assche  * @tm_request: pointer to mf from user space
571f92363d1SSreekanth Reddy  *
5724beb4867SBart Van Assche  * Return: 0 when an smid if found, else fail.
573f92363d1SSreekanth Reddy  * during failure, the reply frame is filled.
574f92363d1SSreekanth Reddy  */
575f92363d1SSreekanth Reddy static int
576f92363d1SSreekanth Reddy _ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
577f92363d1SSreekanth Reddy 	Mpi2SCSITaskManagementRequest_t *tm_request)
578f92363d1SSreekanth Reddy {
579f92363d1SSreekanth Reddy 	u8 found = 0;
580dbec4c90SSuganath Prabu Subramani 	u16 smid;
581f92363d1SSreekanth Reddy 	u16 handle;
582f92363d1SSreekanth Reddy 	struct scsi_cmnd *scmd;
583f92363d1SSreekanth Reddy 	struct MPT3SAS_DEVICE *priv_data;
584f92363d1SSreekanth Reddy 	Mpi2SCSITaskManagementReply_t *tm_reply;
585f92363d1SSreekanth Reddy 	u32 sz;
586f92363d1SSreekanth Reddy 	u32 lun;
587f92363d1SSreekanth Reddy 	char *desc = NULL;
588f92363d1SSreekanth Reddy 
589f92363d1SSreekanth Reddy 	if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
590f92363d1SSreekanth Reddy 		desc = "abort_task";
591f92363d1SSreekanth Reddy 	else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
592f92363d1SSreekanth Reddy 		desc = "query_task";
593f92363d1SSreekanth Reddy 	else
594f92363d1SSreekanth Reddy 		return 0;
595f92363d1SSreekanth Reddy 
596f92363d1SSreekanth Reddy 	lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
597f92363d1SSreekanth Reddy 
598f92363d1SSreekanth Reddy 	handle = le16_to_cpu(tm_request->DevHandle);
599dbec4c90SSuganath Prabu Subramani 	for (smid = ioc->scsiio_depth; smid && !found; smid--) {
600dbec4c90SSuganath Prabu Subramani 		struct scsiio_tracker *st;
601dbec4c90SSuganath Prabu Subramani 
602dbec4c90SSuganath Prabu Subramani 		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
603dbec4c90SSuganath Prabu Subramani 		if (!scmd)
604f92363d1SSreekanth Reddy 			continue;
605f92363d1SSreekanth Reddy 		if (lun != scmd->device->lun)
606f92363d1SSreekanth Reddy 			continue;
607f92363d1SSreekanth Reddy 		priv_data = scmd->device->hostdata;
608f92363d1SSreekanth Reddy 		if (priv_data->sas_target == NULL)
609f92363d1SSreekanth Reddy 			continue;
610f92363d1SSreekanth Reddy 		if (priv_data->sas_target->handle != handle)
611f92363d1SSreekanth Reddy 			continue;
612dbec4c90SSuganath Prabu Subramani 		st = scsi_cmd_priv(scmd);
6138f55c307SMinwoo Im 
6148f55c307SMinwoo Im 		/*
6158f55c307SMinwoo Im 		 * If the given TaskMID from the user space is zero, then the
6168f55c307SMinwoo Im 		 * first outstanding smid will be picked up.  Otherwise,
6178f55c307SMinwoo Im 		 * targeted smid will be the one.
6188f55c307SMinwoo Im 		 */
6198f55c307SMinwoo Im 		if (!tm_request->TaskMID || tm_request->TaskMID == st->smid) {
620dbec4c90SSuganath Prabu Subramani 			tm_request->TaskMID = cpu_to_le16(st->smid);
621f92363d1SSreekanth Reddy 			found = 1;
622f92363d1SSreekanth Reddy 		}
6238f55c307SMinwoo Im 	}
624f92363d1SSreekanth Reddy 
625f92363d1SSreekanth Reddy 	if (!found) {
626919d8a3fSJoe Perches 		dctlprintk(ioc,
627919d8a3fSJoe Perches 			   ioc_info(ioc, "%s: handle(0x%04x), lun(%d), no active mid!!\n",
628919d8a3fSJoe Perches 				    desc, le16_to_cpu(tm_request->DevHandle),
629919d8a3fSJoe Perches 				    lun));
630f92363d1SSreekanth Reddy 		tm_reply = ioc->ctl_cmds.reply;
631f92363d1SSreekanth Reddy 		tm_reply->DevHandle = tm_request->DevHandle;
632f92363d1SSreekanth Reddy 		tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
633f92363d1SSreekanth Reddy 		tm_reply->TaskType = tm_request->TaskType;
634f92363d1SSreekanth Reddy 		tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
635f92363d1SSreekanth Reddy 		tm_reply->VP_ID = tm_request->VP_ID;
636f92363d1SSreekanth Reddy 		tm_reply->VF_ID = tm_request->VF_ID;
637f92363d1SSreekanth Reddy 		sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
638f92363d1SSreekanth Reddy 		if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
639f92363d1SSreekanth Reddy 		    sz))
640f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
641f92363d1SSreekanth Reddy 			    __LINE__, __func__);
642f92363d1SSreekanth Reddy 		return 1;
643f92363d1SSreekanth Reddy 	}
644f92363d1SSreekanth Reddy 
645919d8a3fSJoe Perches 	dctlprintk(ioc,
646919d8a3fSJoe Perches 		   ioc_info(ioc, "%s: handle(0x%04x), lun(%d), task_mid(%d)\n",
647f92363d1SSreekanth Reddy 			    desc, le16_to_cpu(tm_request->DevHandle), lun,
648f92363d1SSreekanth Reddy 			    le16_to_cpu(tm_request->TaskMID)));
649f92363d1SSreekanth Reddy 	return 0;
650f92363d1SSreekanth Reddy }
651f92363d1SSreekanth Reddy 
652f92363d1SSreekanth Reddy /**
653f92363d1SSreekanth Reddy  * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
654f92363d1SSreekanth Reddy  * @ioc: per adapter object
6554beb4867SBart Van Assche  * @karg: (struct mpt3_ioctl_command)
6564beb4867SBart Van Assche  * @mf: pointer to mf in user space
657f92363d1SSreekanth Reddy  */
658f92363d1SSreekanth Reddy static long
659f92363d1SSreekanth Reddy _ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
660f92363d1SSreekanth Reddy 	void __user *mf)
661f92363d1SSreekanth Reddy {
662f92363d1SSreekanth Reddy 	MPI2RequestHeader_t *mpi_request = NULL, *request;
663f92363d1SSreekanth Reddy 	MPI2DefaultReply_t *mpi_reply;
664aff39e61SSuganath Prabu Subramani 	Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
665c1a6c5acSChaitra P B 	struct _pcie_device *pcie_device = NULL;
666f92363d1SSreekanth Reddy 	u16 smid;
667*42f68703SSuganath Prabu S 	unsigned long timeout;
668f92363d1SSreekanth Reddy 	u8 issue_reset;
669aff39e61SSuganath Prabu Subramani 	u32 sz, sz_arg;
670f92363d1SSreekanth Reddy 	void *psge;
671f92363d1SSreekanth Reddy 	void *data_out = NULL;
672f92363d1SSreekanth Reddy 	dma_addr_t data_out_dma = 0;
673f92363d1SSreekanth Reddy 	size_t data_out_sz = 0;
674f92363d1SSreekanth Reddy 	void *data_in = NULL;
675f92363d1SSreekanth Reddy 	dma_addr_t data_in_dma = 0;
676f92363d1SSreekanth Reddy 	size_t data_in_sz = 0;
677f92363d1SSreekanth Reddy 	long ret;
678c696f7b8SSuganath Prabu Subramani 	u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
679f92363d1SSreekanth Reddy 
680f92363d1SSreekanth Reddy 	issue_reset = 0;
681f92363d1SSreekanth Reddy 
682f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
683919d8a3fSJoe Perches 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
684f92363d1SSreekanth Reddy 		ret = -EAGAIN;
685f92363d1SSreekanth Reddy 		goto out;
686f92363d1SSreekanth Reddy 	}
687f92363d1SSreekanth Reddy 
688f4305749SSuganath Prabu 	ret = mpt3sas_wait_for_ioc(ioc,	IOC_OPERATIONAL_WAIT_COUNT);
689f4305749SSuganath Prabu 	if (ret)
690f92363d1SSreekanth Reddy 		goto out;
691f92363d1SSreekanth Reddy 
692f92363d1SSreekanth Reddy 	mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
693f92363d1SSreekanth Reddy 	if (!mpi_request) {
694919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed obtaining a memory for mpi_request\n",
695919d8a3fSJoe Perches 			__func__);
696f92363d1SSreekanth Reddy 		ret = -ENOMEM;
697f92363d1SSreekanth Reddy 		goto out;
698f92363d1SSreekanth Reddy 	}
699f92363d1SSreekanth Reddy 
700f92363d1SSreekanth Reddy 	/* Check for overflow and wraparound */
701f92363d1SSreekanth Reddy 	if (karg.data_sge_offset * 4 > ioc->request_sz ||
702f92363d1SSreekanth Reddy 	    karg.data_sge_offset > (UINT_MAX / 4)) {
703f92363d1SSreekanth Reddy 		ret = -EINVAL;
704f92363d1SSreekanth Reddy 		goto out;
705f92363d1SSreekanth Reddy 	}
706f92363d1SSreekanth Reddy 
707f92363d1SSreekanth Reddy 	/* copy in request message frame from user */
708f92363d1SSreekanth Reddy 	if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
709f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
710f92363d1SSreekanth Reddy 		    __func__);
711f92363d1SSreekanth Reddy 		ret = -EFAULT;
712f92363d1SSreekanth Reddy 		goto out;
713f92363d1SSreekanth Reddy 	}
714f92363d1SSreekanth Reddy 
715f92363d1SSreekanth Reddy 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
716f92363d1SSreekanth Reddy 		smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
717f92363d1SSreekanth Reddy 		if (!smid) {
718919d8a3fSJoe Perches 			ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
719f92363d1SSreekanth Reddy 			ret = -EAGAIN;
720f92363d1SSreekanth Reddy 			goto out;
721f92363d1SSreekanth Reddy 		}
722f92363d1SSreekanth Reddy 	} else {
723b0cd285eSHannes Reinecke 		/* Use first reserved smid for passthrough ioctls */
724b0cd285eSHannes Reinecke 		smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
725f92363d1SSreekanth Reddy 	}
726f92363d1SSreekanth Reddy 
727f92363d1SSreekanth Reddy 	ret = 0;
728f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
729f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
730f92363d1SSreekanth Reddy 	request = mpt3sas_base_get_msg_frame(ioc, smid);
731e224e03bSSuganath Prabu 	memset(request, 0, ioc->request_sz);
732f92363d1SSreekanth Reddy 	memcpy(request, mpi_request, karg.data_sge_offset*4);
733f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
734f92363d1SSreekanth Reddy 	data_out_sz = karg.data_out_size;
735f92363d1SSreekanth Reddy 	data_in_sz = karg.data_in_size;
736f92363d1SSreekanth Reddy 
737f92363d1SSreekanth Reddy 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
738c696f7b8SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
739c696f7b8SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
740aff39e61SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
741aff39e61SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
742c696f7b8SSuganath Prabu Subramani 
743c696f7b8SSuganath Prabu Subramani 		device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
744c696f7b8SSuganath Prabu Subramani 		if (!device_handle || (device_handle >
745c696f7b8SSuganath Prabu Subramani 		    ioc->facts.MaxDevHandle)) {
746f92363d1SSreekanth Reddy 			ret = -EINVAL;
747f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
748f92363d1SSreekanth Reddy 			goto out;
749f92363d1SSreekanth Reddy 		}
750f92363d1SSreekanth Reddy 	}
751f92363d1SSreekanth Reddy 
752f92363d1SSreekanth Reddy 	/* obtain dma-able memory for data transfer */
753f92363d1SSreekanth Reddy 	if (data_out_sz) /* WRITE */ {
7541c2048bdSChristoph Hellwig 		data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz,
7551c2048bdSChristoph Hellwig 				&data_out_dma, GFP_KERNEL);
756f92363d1SSreekanth Reddy 		if (!data_out) {
757f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
758f92363d1SSreekanth Reddy 			    __LINE__, __func__);
759f92363d1SSreekanth Reddy 			ret = -ENOMEM;
760f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
761f92363d1SSreekanth Reddy 			goto out;
762f92363d1SSreekanth Reddy 		}
763f92363d1SSreekanth Reddy 		if (copy_from_user(data_out, karg.data_out_buf_ptr,
764f92363d1SSreekanth Reddy 			data_out_sz)) {
765f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
766f92363d1SSreekanth Reddy 			    __LINE__, __func__);
767f92363d1SSreekanth Reddy 			ret =  -EFAULT;
768f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
769f92363d1SSreekanth Reddy 			goto out;
770f92363d1SSreekanth Reddy 		}
771f92363d1SSreekanth Reddy 	}
772f92363d1SSreekanth Reddy 
773f92363d1SSreekanth Reddy 	if (data_in_sz) /* READ */ {
7741c2048bdSChristoph Hellwig 		data_in = dma_alloc_coherent(&ioc->pdev->dev, data_in_sz,
7751c2048bdSChristoph Hellwig 				&data_in_dma, GFP_KERNEL);
776f92363d1SSreekanth Reddy 		if (!data_in) {
777f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
778f92363d1SSreekanth Reddy 			    __LINE__, __func__);
779f92363d1SSreekanth Reddy 			ret = -ENOMEM;
780f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
781f92363d1SSreekanth Reddy 			goto out;
782f92363d1SSreekanth Reddy 		}
783f92363d1SSreekanth Reddy 	}
784f92363d1SSreekanth Reddy 
785f92363d1SSreekanth Reddy 	psge = (void *)request + (karg.data_sge_offset*4);
786f92363d1SSreekanth Reddy 
787f92363d1SSreekanth Reddy 	/* send command to firmware */
788f92363d1SSreekanth Reddy 	_ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
789f92363d1SSreekanth Reddy 
790f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
791f92363d1SSreekanth Reddy 	switch (mpi_request->Function) {
792aff39e61SSuganath Prabu Subramani 	case MPI2_FUNCTION_NVME_ENCAPSULATED:
793aff39e61SSuganath Prabu Subramani 	{
794aff39e61SSuganath Prabu Subramani 		nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
79577fd4f2cSSreekanth Reddy 		if (!ioc->pcie_sg_lookup) {
79677fd4f2cSSreekanth Reddy 			dtmprintk(ioc, ioc_info(ioc,
79777fd4f2cSSreekanth Reddy 			    "HBA doesn't support NVMe. Rejecting NVMe Encapsulated request.\n"
79877fd4f2cSSreekanth Reddy 			    ));
79977fd4f2cSSreekanth Reddy 
80077fd4f2cSSreekanth Reddy 			if (ioc->logging_level & MPT_DEBUG_TM)
80177fd4f2cSSreekanth Reddy 				_debug_dump_mf(nvme_encap_request,
80277fd4f2cSSreekanth Reddy 				    ioc->request_sz/4);
80377fd4f2cSSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
80477fd4f2cSSreekanth Reddy 			ret = -EINVAL;
80577fd4f2cSSreekanth Reddy 			goto out;
80677fd4f2cSSreekanth Reddy 		}
807aff39e61SSuganath Prabu Subramani 		/*
808aff39e61SSuganath Prabu Subramani 		 * Get the Physical Address of the sense buffer.
809aff39e61SSuganath Prabu Subramani 		 * Use Error Response buffer address field to hold the sense
810aff39e61SSuganath Prabu Subramani 		 * buffer address.
811aff39e61SSuganath Prabu Subramani 		 * Clear the internal sense buffer, which will potentially hold
812aff39e61SSuganath Prabu Subramani 		 * the Completion Queue Entry on return, or 0 if no Entry.
813aff39e61SSuganath Prabu Subramani 		 * Build the PRPs and set direction bits.
814aff39e61SSuganath Prabu Subramani 		 * Send the request.
815aff39e61SSuganath Prabu Subramani 		 */
816cf6bf971SChaitra P B 		nvme_encap_request->ErrorResponseBaseAddress =
817cf6bf971SChaitra P B 		    cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
818aff39e61SSuganath Prabu Subramani 		nvme_encap_request->ErrorResponseBaseAddress |=
819cf6bf971SChaitra P B 		   cpu_to_le64(le32_to_cpu(
820cf6bf971SChaitra P B 		   mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
821aff39e61SSuganath Prabu Subramani 		nvme_encap_request->ErrorResponseAllocationLength =
822cf6bf971SChaitra P B 					cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
823aff39e61SSuganath Prabu Subramani 		memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
824aff39e61SSuganath Prabu Subramani 		ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
825aff39e61SSuganath Prabu Subramani 		    data_out_dma, data_out_sz, data_in_dma, data_in_sz);
826aff39e61SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
827919d8a3fSJoe Perches 			dtmprintk(ioc,
828919d8a3fSJoe Perches 				  ioc_info(ioc, "handle(0x%04x): ioctl failed due to device removal in progress\n",
829919d8a3fSJoe Perches 					   device_handle));
830aff39e61SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
831aff39e61SSuganath Prabu Subramani 			ret = -EINVAL;
832aff39e61SSuganath Prabu Subramani 			goto out;
833aff39e61SSuganath Prabu Subramani 		}
83440114bdeSSuganath Prabu S 		mpt3sas_base_put_smid_nvme_encap(ioc, smid);
835aff39e61SSuganath Prabu Subramani 		break;
836aff39e61SSuganath Prabu Subramani 	}
837f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SCSI_IO_REQUEST:
838f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
839f92363d1SSreekanth Reddy 	{
840f92363d1SSreekanth Reddy 		Mpi2SCSIIORequest_t *scsiio_request =
841f92363d1SSreekanth Reddy 		    (Mpi2SCSIIORequest_t *)request;
842f92363d1SSreekanth Reddy 		scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
843f92363d1SSreekanth Reddy 		scsiio_request->SenseBufferLowAddress =
844f92363d1SSreekanth Reddy 		    mpt3sas_base_get_sense_buffer_dma(ioc, smid);
845f92363d1SSreekanth Reddy 		memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
846c696f7b8SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
847919d8a3fSJoe Perches 			dtmprintk(ioc,
848919d8a3fSJoe Perches 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
849919d8a3fSJoe Perches 					   device_handle));
850c696f7b8SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
851c696f7b8SSuganath Prabu Subramani 			ret = -EINVAL;
852c696f7b8SSuganath Prabu Subramani 			goto out;
853c696f7b8SSuganath Prabu Subramani 		}
854f92363d1SSreekanth Reddy 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
855f92363d1SSreekanth Reddy 		    data_in_dma, data_in_sz);
856f92363d1SSreekanth Reddy 		if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
85781c16f83SSuganath Prabu Subramani 			ioc->put_smid_scsi_io(ioc, smid, device_handle);
858f92363d1SSreekanth Reddy 		else
859078a4cc1SSuganath Prabu S 			ioc->put_smid_default(ioc, smid);
860f92363d1SSreekanth Reddy 		break;
861f92363d1SSreekanth Reddy 	}
862f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SCSI_TASK_MGMT:
863f92363d1SSreekanth Reddy 	{
864f92363d1SSreekanth Reddy 		Mpi2SCSITaskManagementRequest_t *tm_request =
865f92363d1SSreekanth Reddy 		    (Mpi2SCSITaskManagementRequest_t *)request;
866f92363d1SSreekanth Reddy 
867919d8a3fSJoe Perches 		dtmprintk(ioc,
868919d8a3fSJoe Perches 			  ioc_info(ioc, "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
869919d8a3fSJoe Perches 				   le16_to_cpu(tm_request->DevHandle),
870919d8a3fSJoe Perches 				   tm_request->TaskType));
871459325c4SChaitra P B 		ioc->got_task_abort_from_ioctl = 1;
872f92363d1SSreekanth Reddy 		if (tm_request->TaskType ==
873f92363d1SSreekanth Reddy 		    MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
874f92363d1SSreekanth Reddy 		    tm_request->TaskType ==
875f92363d1SSreekanth Reddy 		    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
876f92363d1SSreekanth Reddy 			if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
877f92363d1SSreekanth Reddy 				mpt3sas_base_free_smid(ioc, smid);
878459325c4SChaitra P B 				ioc->got_task_abort_from_ioctl = 0;
879f92363d1SSreekanth Reddy 				goto out;
880f92363d1SSreekanth Reddy 			}
881f92363d1SSreekanth Reddy 		}
882459325c4SChaitra P B 		ioc->got_task_abort_from_ioctl = 0;
883f92363d1SSreekanth Reddy 
884c696f7b8SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
885919d8a3fSJoe Perches 			dtmprintk(ioc,
886919d8a3fSJoe Perches 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
887919d8a3fSJoe Perches 					   device_handle));
888c696f7b8SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
889c696f7b8SSuganath Prabu Subramani 			ret = -EINVAL;
890c696f7b8SSuganath Prabu Subramani 			goto out;
891c696f7b8SSuganath Prabu Subramani 		}
892f92363d1SSreekanth Reddy 		mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
893f92363d1SSreekanth Reddy 		    tm_request->DevHandle));
894f92363d1SSreekanth Reddy 		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
895f92363d1SSreekanth Reddy 		    data_in_dma, data_in_sz);
896078a4cc1SSuganath Prabu S 		ioc->put_smid_hi_priority(ioc, smid, 0);
897f92363d1SSreekanth Reddy 		break;
898f92363d1SSreekanth Reddy 	}
899f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SMP_PASSTHROUGH:
900f92363d1SSreekanth Reddy 	{
901f92363d1SSreekanth Reddy 		Mpi2SmpPassthroughRequest_t *smp_request =
902f92363d1SSreekanth Reddy 		    (Mpi2SmpPassthroughRequest_t *)mpi_request;
903f92363d1SSreekanth Reddy 		u8 *data;
904f92363d1SSreekanth Reddy 
905f92363d1SSreekanth Reddy 		/* ioc determines which port to use */
906f92363d1SSreekanth Reddy 		smp_request->PhysicalPort = 0xFF;
907f92363d1SSreekanth Reddy 		if (smp_request->PassthroughFlags &
908f92363d1SSreekanth Reddy 		    MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
909f92363d1SSreekanth Reddy 			data = (u8 *)&smp_request->SGL;
910f92363d1SSreekanth Reddy 		else {
911f92363d1SSreekanth Reddy 			if (unlikely(data_out == NULL)) {
912f92363d1SSreekanth Reddy 				pr_err("failure at %s:%d/%s()!\n",
913f92363d1SSreekanth Reddy 				    __FILE__, __LINE__, __func__);
914f92363d1SSreekanth Reddy 				mpt3sas_base_free_smid(ioc, smid);
915f92363d1SSreekanth Reddy 				ret = -EINVAL;
916f92363d1SSreekanth Reddy 				goto out;
917f92363d1SSreekanth Reddy 			}
918f92363d1SSreekanth Reddy 			data = data_out;
919f92363d1SSreekanth Reddy 		}
920f92363d1SSreekanth Reddy 
921f92363d1SSreekanth Reddy 		if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
922f92363d1SSreekanth Reddy 			ioc->ioc_link_reset_in_progress = 1;
923f92363d1SSreekanth Reddy 			ioc->ignore_loginfos = 1;
924f92363d1SSreekanth Reddy 		}
925f92363d1SSreekanth Reddy 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
926f92363d1SSreekanth Reddy 		    data_in_sz);
927078a4cc1SSuganath Prabu S 		ioc->put_smid_default(ioc, smid);
928f92363d1SSreekanth Reddy 		break;
929f92363d1SSreekanth Reddy 	}
930f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SATA_PASSTHROUGH:
931c696f7b8SSuganath Prabu Subramani 	{
932c696f7b8SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
933919d8a3fSJoe Perches 			dtmprintk(ioc,
934919d8a3fSJoe Perches 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
935919d8a3fSJoe Perches 					   device_handle));
936c696f7b8SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
937c696f7b8SSuganath Prabu Subramani 			ret = -EINVAL;
938c696f7b8SSuganath Prabu Subramani 			goto out;
939c696f7b8SSuganath Prabu Subramani 		}
940c696f7b8SSuganath Prabu Subramani 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
941c696f7b8SSuganath Prabu Subramani 		    data_in_sz);
942078a4cc1SSuganath Prabu S 		ioc->put_smid_default(ioc, smid);
943c696f7b8SSuganath Prabu Subramani 		break;
944c696f7b8SSuganath Prabu Subramani 	}
945f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_FW_DOWNLOAD:
946f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_FW_UPLOAD:
947f92363d1SSreekanth Reddy 	{
948f92363d1SSreekanth Reddy 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
949f92363d1SSreekanth Reddy 		    data_in_sz);
950078a4cc1SSuganath Prabu S 		ioc->put_smid_default(ioc, smid);
951f92363d1SSreekanth Reddy 		break;
952f92363d1SSreekanth Reddy 	}
953f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_TOOLBOX:
954f92363d1SSreekanth Reddy 	{
955f92363d1SSreekanth Reddy 		Mpi2ToolboxCleanRequest_t *toolbox_request =
956f92363d1SSreekanth Reddy 			(Mpi2ToolboxCleanRequest_t *)mpi_request;
957f92363d1SSreekanth Reddy 
958f23ca2cbSSuganath Prabu 		if ((toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
959f23ca2cbSSuganath Prabu 		    || (toolbox_request->Tool ==
960f23ca2cbSSuganath Prabu 		    MPI26_TOOLBOX_BACKEND_PCIE_LANE_MARGIN))
961f92363d1SSreekanth Reddy 			ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
962f92363d1SSreekanth Reddy 				data_in_dma, data_in_sz);
963ba630ea0SSuganath Prabu 		else if (toolbox_request->Tool ==
964ba630ea0SSuganath Prabu 				MPI2_TOOLBOX_MEMORY_MOVE_TOOL) {
965ba630ea0SSuganath Prabu 			Mpi2ToolboxMemMoveRequest_t *mem_move_request =
966ba630ea0SSuganath Prabu 					(Mpi2ToolboxMemMoveRequest_t *)request;
967ba630ea0SSuganath Prabu 			Mpi2SGESimple64_t tmp, *src = NULL, *dst = NULL;
968ba630ea0SSuganath Prabu 
969ba630ea0SSuganath Prabu 			ioc->build_sg_mpi(ioc, psge, data_out_dma,
970ba630ea0SSuganath Prabu 					data_out_sz, data_in_dma, data_in_sz);
971ba630ea0SSuganath Prabu 			if (data_out_sz && !data_in_sz) {
972ba630ea0SSuganath Prabu 				dst =
973ba630ea0SSuganath Prabu 				    (Mpi2SGESimple64_t *)&mem_move_request->SGL;
974ba630ea0SSuganath Prabu 				src = (void *)dst + ioc->sge_size;
975ba630ea0SSuganath Prabu 
976ba630ea0SSuganath Prabu 				memcpy(&tmp, src, ioc->sge_size);
977ba630ea0SSuganath Prabu 				memcpy(src, dst, ioc->sge_size);
978ba630ea0SSuganath Prabu 				memcpy(dst, &tmp, ioc->sge_size);
979ba630ea0SSuganath Prabu 			}
980ba630ea0SSuganath Prabu 			if (ioc->logging_level & MPT_DEBUG_TM) {
981ba630ea0SSuganath Prabu 				ioc_info(ioc,
982ba630ea0SSuganath Prabu 				  "Mpi2ToolboxMemMoveRequest_t request msg\n");
983ba630ea0SSuganath Prabu 				_debug_dump_mf(mem_move_request,
984ba630ea0SSuganath Prabu 							ioc->request_sz/4);
985ba630ea0SSuganath Prabu 			}
986ba630ea0SSuganath Prabu 		} else
987f92363d1SSreekanth Reddy 			ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
988f92363d1SSreekanth Reddy 			    data_in_dma, data_in_sz);
989078a4cc1SSuganath Prabu S 		ioc->put_smid_default(ioc, smid);
990f92363d1SSreekanth Reddy 		break;
991f92363d1SSreekanth Reddy 	}
992f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
993f92363d1SSreekanth Reddy 	{
994f92363d1SSreekanth Reddy 		Mpi2SasIoUnitControlRequest_t *sasiounit_request =
995f92363d1SSreekanth Reddy 		    (Mpi2SasIoUnitControlRequest_t *)mpi_request;
996f92363d1SSreekanth Reddy 
997f92363d1SSreekanth Reddy 		if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
998f92363d1SSreekanth Reddy 		    || sasiounit_request->Operation ==
999f92363d1SSreekanth Reddy 		    MPI2_SAS_OP_PHY_LINK_RESET) {
1000f92363d1SSreekanth Reddy 			ioc->ioc_link_reset_in_progress = 1;
1001f92363d1SSreekanth Reddy 			ioc->ignore_loginfos = 1;
1002f92363d1SSreekanth Reddy 		}
1003f92363d1SSreekanth Reddy 		/* drop to default case for posting the request */
1004f92363d1SSreekanth Reddy 	}
1005df561f66SGustavo A. R. Silva 		fallthrough;
1006f92363d1SSreekanth Reddy 	default:
1007f92363d1SSreekanth Reddy 		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
1008f92363d1SSreekanth Reddy 		    data_in_dma, data_in_sz);
1009078a4cc1SSuganath Prabu S 		ioc->put_smid_default(ioc, smid);
1010f92363d1SSreekanth Reddy 		break;
1011f92363d1SSreekanth Reddy 	}
1012f92363d1SSreekanth Reddy 
1013f92363d1SSreekanth Reddy 	if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
1014f92363d1SSreekanth Reddy 		timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
1015f92363d1SSreekanth Reddy 	else
1016f92363d1SSreekanth Reddy 		timeout = karg.timeout;
10178bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
1018f92363d1SSreekanth Reddy 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
1019f92363d1SSreekanth Reddy 		Mpi2SCSITaskManagementRequest_t *tm_request =
1020f92363d1SSreekanth Reddy 		    (Mpi2SCSITaskManagementRequest_t *)mpi_request;
1021f92363d1SSreekanth Reddy 		mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
1022f92363d1SSreekanth Reddy 		    tm_request->DevHandle));
1023f92363d1SSreekanth Reddy 		mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
1024f92363d1SSreekanth Reddy 	} else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
1025f92363d1SSreekanth Reddy 	    mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
1026f92363d1SSreekanth Reddy 		ioc->ioc_link_reset_in_progress) {
1027f92363d1SSreekanth Reddy 		ioc->ioc_link_reset_in_progress = 0;
1028f92363d1SSreekanth Reddy 		ioc->ignore_loginfos = 0;
1029f92363d1SSreekanth Reddy 	}
1030f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1031c6bdb6a1SSreekanth Reddy 		mpt3sas_check_cmd_timeout(ioc,
1032d37306caSChaitra P B 		    ioc->ctl_cmds.status, mpi_request,
1033c6bdb6a1SSreekanth Reddy 		    karg.data_sge_offset, issue_reset);
1034f92363d1SSreekanth Reddy 		goto issue_host_reset;
1035f92363d1SSreekanth Reddy 	}
1036f92363d1SSreekanth Reddy 
1037f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
1038f92363d1SSreekanth Reddy 
1039f92363d1SSreekanth Reddy 	if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
1040f92363d1SSreekanth Reddy 	    (ioc->logging_level & MPT_DEBUG_TM)) {
1041f92363d1SSreekanth Reddy 		Mpi2SCSITaskManagementReply_t *tm_reply =
1042f92363d1SSreekanth Reddy 		    (Mpi2SCSITaskManagementReply_t *)mpi_reply;
1043f92363d1SSreekanth Reddy 
1044919d8a3fSJoe Perches 		ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
1045f92363d1SSreekanth Reddy 			 le16_to_cpu(tm_reply->IOCStatus),
1046f92363d1SSreekanth Reddy 			 le32_to_cpu(tm_reply->IOCLogInfo),
1047f92363d1SSreekanth Reddy 			 le32_to_cpu(tm_reply->TerminationCount));
1048f92363d1SSreekanth Reddy 	}
1049af009411SSreekanth Reddy 
1050f92363d1SSreekanth Reddy 	/* copy out xdata to user */
1051f92363d1SSreekanth Reddy 	if (data_in_sz) {
1052f92363d1SSreekanth Reddy 		if (copy_to_user(karg.data_in_buf_ptr, data_in,
1053f92363d1SSreekanth Reddy 		    data_in_sz)) {
1054f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1055f92363d1SSreekanth Reddy 			    __LINE__, __func__);
1056f92363d1SSreekanth Reddy 			ret = -ENODATA;
1057f92363d1SSreekanth Reddy 			goto out;
1058f92363d1SSreekanth Reddy 		}
1059f92363d1SSreekanth Reddy 	}
1060f92363d1SSreekanth Reddy 
1061f92363d1SSreekanth Reddy 	/* copy out reply message frame to user */
1062f92363d1SSreekanth Reddy 	if (karg.max_reply_bytes) {
1063f92363d1SSreekanth Reddy 		sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1064f92363d1SSreekanth Reddy 		if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1065f92363d1SSreekanth Reddy 		    sz)) {
1066f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1067f92363d1SSreekanth Reddy 			    __LINE__, __func__);
1068f92363d1SSreekanth Reddy 			ret = -ENODATA;
1069f92363d1SSreekanth Reddy 			goto out;
1070f92363d1SSreekanth Reddy 		}
1071f92363d1SSreekanth Reddy 	}
1072f92363d1SSreekanth Reddy 
1073aff39e61SSuganath Prabu Subramani 	/* copy out sense/NVMe Error Response to user */
1074f92363d1SSreekanth Reddy 	if (karg.max_sense_bytes && (mpi_request->Function ==
1075f92363d1SSreekanth Reddy 	    MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
1076aff39e61SSuganath Prabu Subramani 	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1077aff39e61SSuganath Prabu Subramani 	    MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1078aff39e61SSuganath Prabu Subramani 		if (karg.sense_data_ptr == NULL) {
1079919d8a3fSJoe Perches 			ioc_info(ioc, "Response buffer provided by application is NULL; Response data will not be returned\n");
1080aff39e61SSuganath Prabu Subramani 			goto out;
1081aff39e61SSuganath Prabu Subramani 		}
1082aff39e61SSuganath Prabu Subramani 		sz_arg = (mpi_request->Function ==
1083aff39e61SSuganath Prabu Subramani 		MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1084aff39e61SSuganath Prabu Subramani 							SCSI_SENSE_BUFFERSIZE;
1085aff39e61SSuganath Prabu Subramani 		sz = min_t(u32, karg.max_sense_bytes, sz_arg);
1086f92363d1SSreekanth Reddy 		if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1087f92363d1SSreekanth Reddy 		    sz)) {
1088f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1089f92363d1SSreekanth Reddy 				__LINE__, __func__);
1090f92363d1SSreekanth Reddy 			ret = -ENODATA;
1091f92363d1SSreekanth Reddy 			goto out;
1092f92363d1SSreekanth Reddy 		}
1093f92363d1SSreekanth Reddy 	}
1094f92363d1SSreekanth Reddy 
1095f92363d1SSreekanth Reddy  issue_host_reset:
1096f92363d1SSreekanth Reddy 	if (issue_reset) {
1097f92363d1SSreekanth Reddy 		ret = -ENODATA;
1098f92363d1SSreekanth Reddy 		if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1099f92363d1SSreekanth Reddy 		    mpi_request->Function ==
1100f92363d1SSreekanth Reddy 		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1101f92363d1SSreekanth Reddy 		    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
1102919d8a3fSJoe Perches 			ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
1103f92363d1SSreekanth Reddy 				 le16_to_cpu(mpi_request->FunctionDependent1));
1104f92363d1SSreekanth Reddy 			mpt3sas_halt_firmware(ioc);
1105c1a6c5acSChaitra P B 			pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1106c1a6c5acSChaitra P B 				le16_to_cpu(mpi_request->FunctionDependent1));
11075bb309dbSSuganath Prabu 			if (pcie_device && (!ioc->tm_custom_handling) &&
11085bb309dbSSuganath Prabu 			    (!(mpt3sas_scsih_is_pcie_scsi_device(
11095bb309dbSSuganath Prabu 			    pcie_device->device_info))))
111096902835SCalvin Owens 				mpt3sas_scsih_issue_locked_tm(ioc,
1111c1a6c5acSChaitra P B 				  le16_to_cpu(mpi_request->FunctionDependent1),
1112521e9c0bSSuganath Prabu S 				  0, 0, 0,
1113521e9c0bSSuganath Prabu S 				  MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1114c1a6c5acSChaitra P B 				  0, pcie_device->reset_timeout,
11155bb309dbSSuganath Prabu 			MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE);
1116c1a6c5acSChaitra P B 			else
1117c1a6c5acSChaitra P B 				mpt3sas_scsih_issue_locked_tm(ioc,
1118c1a6c5acSChaitra P B 				  le16_to_cpu(mpi_request->FunctionDependent1),
1119521e9c0bSSuganath Prabu S 				  0, 0, 0,
1120521e9c0bSSuganath Prabu S 				  MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1121c1a6c5acSChaitra P B 				  0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
1122f92363d1SSreekanth Reddy 		} else
112398c56ad3SCalvin Owens 			mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1124f92363d1SSreekanth Reddy 	}
1125f92363d1SSreekanth Reddy 
1126f92363d1SSreekanth Reddy  out:
1127c1a6c5acSChaitra P B 	if (pcie_device)
1128c1a6c5acSChaitra P B 		pcie_device_put(pcie_device);
1129f92363d1SSreekanth Reddy 
1130f92363d1SSreekanth Reddy 	/* free memory associated with sg buffers */
1131f92363d1SSreekanth Reddy 	if (data_in)
11321c2048bdSChristoph Hellwig 		dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
1133f92363d1SSreekanth Reddy 		    data_in_dma);
1134f92363d1SSreekanth Reddy 
1135f92363d1SSreekanth Reddy 	if (data_out)
11361c2048bdSChristoph Hellwig 		dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
1137f92363d1SSreekanth Reddy 		    data_out_dma);
1138f92363d1SSreekanth Reddy 
1139f92363d1SSreekanth Reddy 	kfree(mpi_request);
1140f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1141f92363d1SSreekanth Reddy 	return ret;
1142f92363d1SSreekanth Reddy }
1143f92363d1SSreekanth Reddy 
1144f92363d1SSreekanth Reddy /**
1145f92363d1SSreekanth Reddy  * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1146f92363d1SSreekanth Reddy  * @ioc: per adapter object
11474beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1148f92363d1SSreekanth Reddy  */
1149f92363d1SSreekanth Reddy static long
1150f92363d1SSreekanth Reddy _ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1151f92363d1SSreekanth Reddy {
1152f92363d1SSreekanth Reddy 	struct mpt3_ioctl_iocinfo karg;
1153f92363d1SSreekanth Reddy 
1154919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1155f92363d1SSreekanth Reddy 				 __func__));
1156f92363d1SSreekanth Reddy 
1157f92363d1SSreekanth Reddy 	memset(&karg, 0 , sizeof(karg));
1158f92363d1SSreekanth Reddy 	if (ioc->pfacts)
1159f92363d1SSreekanth Reddy 		karg.port_number = ioc->pfacts[0].PortNumber;
1160f92363d1SSreekanth Reddy 	karg.hw_rev = ioc->pdev->revision;
1161f92363d1SSreekanth Reddy 	karg.pci_id = ioc->pdev->device;
1162f92363d1SSreekanth Reddy 	karg.subsystem_device = ioc->pdev->subsystem_device;
1163f92363d1SSreekanth Reddy 	karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1164f92363d1SSreekanth Reddy 	karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1165f92363d1SSreekanth Reddy 	karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1166f92363d1SSreekanth Reddy 	karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1167f92363d1SSreekanth Reddy 	karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1168f92363d1SSreekanth Reddy 	karg.firmware_version = ioc->facts.FWVersion.Word;
1169c84b06a4SSreekanth Reddy 	strcpy(karg.driver_version, ioc->driver_name);
1170f92363d1SSreekanth Reddy 	strcat(karg.driver_version, "-");
1171d357e84dSSreekanth Reddy 	switch  (ioc->hba_mpi_version_belonged) {
1172d357e84dSSreekanth Reddy 	case MPI2_VERSION:
11737786ab6aSSreekanth Reddy 		if (ioc->is_warpdrive)
11747786ab6aSSreekanth Reddy 			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
11757786ab6aSSreekanth Reddy 		else
1176d357e84dSSreekanth Reddy 			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1177d357e84dSSreekanth Reddy 		strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1178d357e84dSSreekanth Reddy 		break;
1179d357e84dSSreekanth Reddy 	case MPI25_VERSION:
1180b130b0d5SSuganath prabu Subramani 	case MPI26_VERSION:
1181998f26aeSSuganath Prabu Subramani 		if (ioc->is_gen35_ioc)
1182998f26aeSSuganath Prabu Subramani 			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1183998f26aeSSuganath Prabu Subramani 		else
1184d357e84dSSreekanth Reddy 			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1185d357e84dSSreekanth Reddy 		strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1186d357e84dSSreekanth Reddy 		break;
1187d357e84dSSreekanth Reddy 	}
1188f92363d1SSreekanth Reddy 	karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1189f92363d1SSreekanth Reddy 
1190f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1191f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1192f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1193f92363d1SSreekanth Reddy 		return -EFAULT;
1194f92363d1SSreekanth Reddy 	}
1195f92363d1SSreekanth Reddy 	return 0;
1196f92363d1SSreekanth Reddy }
1197f92363d1SSreekanth Reddy 
1198f92363d1SSreekanth Reddy /**
1199f92363d1SSreekanth Reddy  * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1200f92363d1SSreekanth Reddy  * @ioc: per adapter object
12014beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1202f92363d1SSreekanth Reddy  */
1203f92363d1SSreekanth Reddy static long
1204f92363d1SSreekanth Reddy _ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1205f92363d1SSreekanth Reddy {
1206f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventquery karg;
1207f92363d1SSreekanth Reddy 
1208f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1209f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1210f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1211f92363d1SSreekanth Reddy 		return -EFAULT;
1212f92363d1SSreekanth Reddy 	}
1213f92363d1SSreekanth Reddy 
1214919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1215f92363d1SSreekanth Reddy 				 __func__));
1216f92363d1SSreekanth Reddy 
1217f92363d1SSreekanth Reddy 	karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1218f92363d1SSreekanth Reddy 	memcpy(karg.event_types, ioc->event_type,
1219f92363d1SSreekanth Reddy 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1220f92363d1SSreekanth Reddy 
1221f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1222f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1223f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1224f92363d1SSreekanth Reddy 		return -EFAULT;
1225f92363d1SSreekanth Reddy 	}
1226f92363d1SSreekanth Reddy 	return 0;
1227f92363d1SSreekanth Reddy }
1228f92363d1SSreekanth Reddy 
1229f92363d1SSreekanth Reddy /**
1230f92363d1SSreekanth Reddy  * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1231f92363d1SSreekanth Reddy  * @ioc: per adapter object
12324beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1233f92363d1SSreekanth Reddy  */
1234f92363d1SSreekanth Reddy static long
1235f92363d1SSreekanth Reddy _ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1236f92363d1SSreekanth Reddy {
1237f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventenable karg;
1238f92363d1SSreekanth Reddy 
1239f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1240f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1241f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1242f92363d1SSreekanth Reddy 		return -EFAULT;
1243f92363d1SSreekanth Reddy 	}
1244f92363d1SSreekanth Reddy 
1245919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1246f92363d1SSreekanth Reddy 				 __func__));
1247f92363d1SSreekanth Reddy 
1248f92363d1SSreekanth Reddy 	memcpy(ioc->event_type, karg.event_types,
1249f92363d1SSreekanth Reddy 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1250f92363d1SSreekanth Reddy 	mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1251f92363d1SSreekanth Reddy 
1252f92363d1SSreekanth Reddy 	if (ioc->event_log)
1253f92363d1SSreekanth Reddy 		return 0;
1254f92363d1SSreekanth Reddy 	/* initialize event_log */
1255f92363d1SSreekanth Reddy 	ioc->event_context = 0;
1256f92363d1SSreekanth Reddy 	ioc->aen_event_read_flag = 0;
1257f92363d1SSreekanth Reddy 	ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1258f92363d1SSreekanth Reddy 	    sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1259f92363d1SSreekanth Reddy 	if (!ioc->event_log) {
1260f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1261f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1262f92363d1SSreekanth Reddy 		return -ENOMEM;
1263f92363d1SSreekanth Reddy 	}
1264f92363d1SSreekanth Reddy 	return 0;
1265f92363d1SSreekanth Reddy }
1266f92363d1SSreekanth Reddy 
1267f92363d1SSreekanth Reddy /**
1268f92363d1SSreekanth Reddy  * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1269f92363d1SSreekanth Reddy  * @ioc: per adapter object
12704beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1271f92363d1SSreekanth Reddy  */
1272f92363d1SSreekanth Reddy static long
1273f92363d1SSreekanth Reddy _ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1274f92363d1SSreekanth Reddy {
1275f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventreport karg;
1276f92363d1SSreekanth Reddy 	u32 number_bytes, max_events, max;
1277f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventreport __user *uarg = arg;
1278f92363d1SSreekanth Reddy 
1279f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1280f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1281f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1282f92363d1SSreekanth Reddy 		return -EFAULT;
1283f92363d1SSreekanth Reddy 	}
1284f92363d1SSreekanth Reddy 
1285919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1286f92363d1SSreekanth Reddy 				 __func__));
1287f92363d1SSreekanth Reddy 
1288f92363d1SSreekanth Reddy 	number_bytes = karg.hdr.max_data_size -
1289f92363d1SSreekanth Reddy 	    sizeof(struct mpt3_ioctl_header);
1290f92363d1SSreekanth Reddy 	max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1291f92363d1SSreekanth Reddy 	max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1292f92363d1SSreekanth Reddy 
1293f92363d1SSreekanth Reddy 	/* If fewer than 1 event is requested, there must have
1294f92363d1SSreekanth Reddy 	 * been some type of error.
1295f92363d1SSreekanth Reddy 	 */
1296f92363d1SSreekanth Reddy 	if (!max || !ioc->event_log)
1297f92363d1SSreekanth Reddy 		return -ENODATA;
1298f92363d1SSreekanth Reddy 
1299f92363d1SSreekanth Reddy 	number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1300f92363d1SSreekanth Reddy 	if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1301f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1302f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1303f92363d1SSreekanth Reddy 		return -EFAULT;
1304f92363d1SSreekanth Reddy 	}
1305f92363d1SSreekanth Reddy 
1306f92363d1SSreekanth Reddy 	/* reset flag so SIGIO can restart */
1307f92363d1SSreekanth Reddy 	ioc->aen_event_read_flag = 0;
1308f92363d1SSreekanth Reddy 	return 0;
1309f92363d1SSreekanth Reddy }
1310f92363d1SSreekanth Reddy 
1311f92363d1SSreekanth Reddy /**
1312f92363d1SSreekanth Reddy  * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1313f92363d1SSreekanth Reddy  * @ioc: per adapter object
13144beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1315f92363d1SSreekanth Reddy  */
1316f92363d1SSreekanth Reddy static long
1317f92363d1SSreekanth Reddy _ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1318f92363d1SSreekanth Reddy {
1319f92363d1SSreekanth Reddy 	struct mpt3_ioctl_diag_reset karg;
1320f92363d1SSreekanth Reddy 	int retval;
1321f92363d1SSreekanth Reddy 
1322f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1323f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1324f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1325f92363d1SSreekanth Reddy 		return -EFAULT;
1326f92363d1SSreekanth Reddy 	}
1327f92363d1SSreekanth Reddy 
1328f92363d1SSreekanth Reddy 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
1329f92363d1SSreekanth Reddy 	    ioc->is_driver_loading)
1330f92363d1SSreekanth Reddy 		return -EAGAIN;
1331f92363d1SSreekanth Reddy 
1332919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1333f92363d1SSreekanth Reddy 				 __func__));
1334f92363d1SSreekanth Reddy 
133598c56ad3SCalvin Owens 	retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
13365b061980SSreekanth Reddy 	ioc_info(ioc,
13375b061980SSreekanth Reddy 	    "Ioctl: host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
1338f92363d1SSreekanth Reddy 	return 0;
1339f92363d1SSreekanth Reddy }
1340f92363d1SSreekanth Reddy 
1341f92363d1SSreekanth Reddy /**
1342f92363d1SSreekanth Reddy  * _ctl_btdh_search_sas_device - searching for sas device
1343f92363d1SSreekanth Reddy  * @ioc: per adapter object
1344f92363d1SSreekanth Reddy  * @btdh: btdh ioctl payload
1345f92363d1SSreekanth Reddy  */
1346f92363d1SSreekanth Reddy static int
1347f92363d1SSreekanth Reddy _ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1348f92363d1SSreekanth Reddy 	struct mpt3_ioctl_btdh_mapping *btdh)
1349f92363d1SSreekanth Reddy {
1350f92363d1SSreekanth Reddy 	struct _sas_device *sas_device;
1351f92363d1SSreekanth Reddy 	unsigned long flags;
1352f92363d1SSreekanth Reddy 	int rc = 0;
1353f92363d1SSreekanth Reddy 
1354f92363d1SSreekanth Reddy 	if (list_empty(&ioc->sas_device_list))
1355f92363d1SSreekanth Reddy 		return rc;
1356f92363d1SSreekanth Reddy 
1357f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1358f92363d1SSreekanth Reddy 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1359f92363d1SSreekanth Reddy 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1360f92363d1SSreekanth Reddy 		    btdh->handle == sas_device->handle) {
1361f92363d1SSreekanth Reddy 			btdh->bus = sas_device->channel;
1362f92363d1SSreekanth Reddy 			btdh->id = sas_device->id;
1363f92363d1SSreekanth Reddy 			rc = 1;
1364f92363d1SSreekanth Reddy 			goto out;
1365f92363d1SSreekanth Reddy 		} else if (btdh->bus == sas_device->channel && btdh->id ==
1366f92363d1SSreekanth Reddy 		    sas_device->id && btdh->handle == 0xFFFF) {
1367f92363d1SSreekanth Reddy 			btdh->handle = sas_device->handle;
1368f92363d1SSreekanth Reddy 			rc = 1;
1369f92363d1SSreekanth Reddy 			goto out;
1370f92363d1SSreekanth Reddy 		}
1371f92363d1SSreekanth Reddy 	}
1372f92363d1SSreekanth Reddy  out:
1373f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1374f92363d1SSreekanth Reddy 	return rc;
1375f92363d1SSreekanth Reddy }
1376f92363d1SSreekanth Reddy 
1377f92363d1SSreekanth Reddy /**
137845aa6a1aSSuganath Prabu Subramani  * _ctl_btdh_search_pcie_device - searching for pcie device
137945aa6a1aSSuganath Prabu Subramani  * @ioc: per adapter object
138045aa6a1aSSuganath Prabu Subramani  * @btdh: btdh ioctl payload
138145aa6a1aSSuganath Prabu Subramani  */
138245aa6a1aSSuganath Prabu Subramani static int
138345aa6a1aSSuganath Prabu Subramani _ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
138445aa6a1aSSuganath Prabu Subramani 	struct mpt3_ioctl_btdh_mapping *btdh)
138545aa6a1aSSuganath Prabu Subramani {
138645aa6a1aSSuganath Prabu Subramani 	struct _pcie_device *pcie_device;
138745aa6a1aSSuganath Prabu Subramani 	unsigned long flags;
138845aa6a1aSSuganath Prabu Subramani 	int rc = 0;
138945aa6a1aSSuganath Prabu Subramani 
139045aa6a1aSSuganath Prabu Subramani 	if (list_empty(&ioc->pcie_device_list))
139145aa6a1aSSuganath Prabu Subramani 		return rc;
139245aa6a1aSSuganath Prabu Subramani 
139345aa6a1aSSuganath Prabu Subramani 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
139445aa6a1aSSuganath Prabu Subramani 	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
139545aa6a1aSSuganath Prabu Subramani 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
139645aa6a1aSSuganath Prabu Subramani 			   btdh->handle == pcie_device->handle) {
139745aa6a1aSSuganath Prabu Subramani 			btdh->bus = pcie_device->channel;
139845aa6a1aSSuganath Prabu Subramani 			btdh->id = pcie_device->id;
139945aa6a1aSSuganath Prabu Subramani 			rc = 1;
140045aa6a1aSSuganath Prabu Subramani 			goto out;
140145aa6a1aSSuganath Prabu Subramani 		} else if (btdh->bus == pcie_device->channel && btdh->id ==
140245aa6a1aSSuganath Prabu Subramani 			   pcie_device->id && btdh->handle == 0xFFFF) {
140345aa6a1aSSuganath Prabu Subramani 			btdh->handle = pcie_device->handle;
140445aa6a1aSSuganath Prabu Subramani 			rc = 1;
140545aa6a1aSSuganath Prabu Subramani 			goto out;
140645aa6a1aSSuganath Prabu Subramani 		}
140745aa6a1aSSuganath Prabu Subramani 	}
140845aa6a1aSSuganath Prabu Subramani  out:
140945aa6a1aSSuganath Prabu Subramani 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
141045aa6a1aSSuganath Prabu Subramani 	return rc;
141145aa6a1aSSuganath Prabu Subramani }
141245aa6a1aSSuganath Prabu Subramani 
141345aa6a1aSSuganath Prabu Subramani /**
1414f92363d1SSreekanth Reddy  * _ctl_btdh_search_raid_device - searching for raid device
1415f92363d1SSreekanth Reddy  * @ioc: per adapter object
1416f92363d1SSreekanth Reddy  * @btdh: btdh ioctl payload
1417f92363d1SSreekanth Reddy  */
1418f92363d1SSreekanth Reddy static int
1419f92363d1SSreekanth Reddy _ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1420f92363d1SSreekanth Reddy 	struct mpt3_ioctl_btdh_mapping *btdh)
1421f92363d1SSreekanth Reddy {
1422f92363d1SSreekanth Reddy 	struct _raid_device *raid_device;
1423f92363d1SSreekanth Reddy 	unsigned long flags;
1424f92363d1SSreekanth Reddy 	int rc = 0;
1425f92363d1SSreekanth Reddy 
1426f92363d1SSreekanth Reddy 	if (list_empty(&ioc->raid_device_list))
1427f92363d1SSreekanth Reddy 		return rc;
1428f92363d1SSreekanth Reddy 
1429f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1430f92363d1SSreekanth Reddy 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1431f92363d1SSreekanth Reddy 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1432f92363d1SSreekanth Reddy 		    btdh->handle == raid_device->handle) {
1433f92363d1SSreekanth Reddy 			btdh->bus = raid_device->channel;
1434f92363d1SSreekanth Reddy 			btdh->id = raid_device->id;
1435f92363d1SSreekanth Reddy 			rc = 1;
1436f92363d1SSreekanth Reddy 			goto out;
1437f92363d1SSreekanth Reddy 		} else if (btdh->bus == raid_device->channel && btdh->id ==
1438f92363d1SSreekanth Reddy 		    raid_device->id && btdh->handle == 0xFFFF) {
1439f92363d1SSreekanth Reddy 			btdh->handle = raid_device->handle;
1440f92363d1SSreekanth Reddy 			rc = 1;
1441f92363d1SSreekanth Reddy 			goto out;
1442f92363d1SSreekanth Reddy 		}
1443f92363d1SSreekanth Reddy 	}
1444f92363d1SSreekanth Reddy  out:
1445f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1446f92363d1SSreekanth Reddy 	return rc;
1447f92363d1SSreekanth Reddy }
1448f92363d1SSreekanth Reddy 
1449f92363d1SSreekanth Reddy /**
1450f92363d1SSreekanth Reddy  * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1451f92363d1SSreekanth Reddy  * @ioc: per adapter object
14524beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1453f92363d1SSreekanth Reddy  */
1454f92363d1SSreekanth Reddy static long
1455f92363d1SSreekanth Reddy _ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1456f92363d1SSreekanth Reddy {
1457f92363d1SSreekanth Reddy 	struct mpt3_ioctl_btdh_mapping karg;
1458f92363d1SSreekanth Reddy 	int rc;
1459f92363d1SSreekanth Reddy 
1460f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1461f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1462f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1463f92363d1SSreekanth Reddy 		return -EFAULT;
1464f92363d1SSreekanth Reddy 	}
1465f92363d1SSreekanth Reddy 
1466919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1467f92363d1SSreekanth Reddy 				 __func__));
1468f92363d1SSreekanth Reddy 
1469f92363d1SSreekanth Reddy 	rc = _ctl_btdh_search_sas_device(ioc, &karg);
1470f92363d1SSreekanth Reddy 	if (!rc)
147145aa6a1aSSuganath Prabu Subramani 		rc = _ctl_btdh_search_pcie_device(ioc, &karg);
147245aa6a1aSSuganath Prabu Subramani 	if (!rc)
1473f92363d1SSreekanth Reddy 		_ctl_btdh_search_raid_device(ioc, &karg);
1474f92363d1SSreekanth Reddy 
1475f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1476f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1477f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1478f92363d1SSreekanth Reddy 		return -EFAULT;
1479f92363d1SSreekanth Reddy 	}
1480f92363d1SSreekanth Reddy 	return 0;
1481f92363d1SSreekanth Reddy }
1482f92363d1SSreekanth Reddy 
1483f92363d1SSreekanth Reddy /**
1484f92363d1SSreekanth Reddy  * _ctl_diag_capability - return diag buffer capability
1485f92363d1SSreekanth Reddy  * @ioc: per adapter object
1486f92363d1SSreekanth Reddy  * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1487f92363d1SSreekanth Reddy  *
1488f92363d1SSreekanth Reddy  * returns 1 when diag buffer support is enabled in firmware
1489f92363d1SSreekanth Reddy  */
1490f92363d1SSreekanth Reddy static u8
1491f92363d1SSreekanth Reddy _ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1492f92363d1SSreekanth Reddy {
1493f92363d1SSreekanth Reddy 	u8 rc = 0;
1494f92363d1SSreekanth Reddy 
1495f92363d1SSreekanth Reddy 	switch (buffer_type) {
1496f92363d1SSreekanth Reddy 	case MPI2_DIAG_BUF_TYPE_TRACE:
1497f92363d1SSreekanth Reddy 		if (ioc->facts.IOCCapabilities &
1498f92363d1SSreekanth Reddy 		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1499f92363d1SSreekanth Reddy 			rc = 1;
1500f92363d1SSreekanth Reddy 		break;
1501f92363d1SSreekanth Reddy 	case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1502f92363d1SSreekanth Reddy 		if (ioc->facts.IOCCapabilities &
1503f92363d1SSreekanth Reddy 		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1504f92363d1SSreekanth Reddy 			rc = 1;
1505f92363d1SSreekanth Reddy 		break;
1506f92363d1SSreekanth Reddy 	case MPI2_DIAG_BUF_TYPE_EXTENDED:
1507f92363d1SSreekanth Reddy 		if (ioc->facts.IOCCapabilities &
1508f92363d1SSreekanth Reddy 		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1509f92363d1SSreekanth Reddy 			rc = 1;
1510f92363d1SSreekanth Reddy 	}
1511f92363d1SSreekanth Reddy 
1512f92363d1SSreekanth Reddy 	return rc;
1513f92363d1SSreekanth Reddy }
1514f92363d1SSreekanth Reddy 
151508e7378eSSreekanth Reddy /**
151608e7378eSSreekanth Reddy  * _ctl_diag_get_bufftype - return diag buffer type
151708e7378eSSreekanth Reddy  *              either TRACE, SNAPSHOT, or EXTENDED
151808e7378eSSreekanth Reddy  * @ioc: per adapter object
151908e7378eSSreekanth Reddy  * @unique_id: specifies the unique_id for the buffer
152008e7378eSSreekanth Reddy  *
152108e7378eSSreekanth Reddy  * returns MPT3_DIAG_UID_NOT_FOUND if the id not found
152208e7378eSSreekanth Reddy  */
152308e7378eSSreekanth Reddy static u8
152408e7378eSSreekanth Reddy _ctl_diag_get_bufftype(struct MPT3SAS_ADAPTER *ioc, u32 unique_id)
152508e7378eSSreekanth Reddy {
152608e7378eSSreekanth Reddy 	u8  index;
152708e7378eSSreekanth Reddy 
152808e7378eSSreekanth Reddy 	for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
152908e7378eSSreekanth Reddy 		if (ioc->unique_id[index] == unique_id)
153008e7378eSSreekanth Reddy 			return index;
153108e7378eSSreekanth Reddy 	}
153208e7378eSSreekanth Reddy 
153308e7378eSSreekanth Reddy 	return MPT3_DIAG_UID_NOT_FOUND;
153408e7378eSSreekanth Reddy }
1535f92363d1SSreekanth Reddy 
1536f92363d1SSreekanth Reddy /**
1537f92363d1SSreekanth Reddy  * _ctl_diag_register_2 - wrapper for registering diag buffer support
1538f92363d1SSreekanth Reddy  * @ioc: per adapter object
1539f92363d1SSreekanth Reddy  * @diag_register: the diag_register struct passed in from user space
1540f92363d1SSreekanth Reddy  *
1541f92363d1SSreekanth Reddy  */
1542f92363d1SSreekanth Reddy static long
1543f92363d1SSreekanth Reddy _ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1544f92363d1SSreekanth Reddy 	struct mpt3_diag_register *diag_register)
1545f92363d1SSreekanth Reddy {
1546f92363d1SSreekanth Reddy 	int rc, i;
1547f92363d1SSreekanth Reddy 	void *request_data = NULL;
1548f92363d1SSreekanth Reddy 	dma_addr_t request_data_dma;
1549f92363d1SSreekanth Reddy 	u32 request_data_sz = 0;
1550f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostRequest_t *mpi_request;
1551f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostReply_t *mpi_reply;
1552f92363d1SSreekanth Reddy 	u8 buffer_type;
1553f92363d1SSreekanth Reddy 	u16 smid;
1554f92363d1SSreekanth Reddy 	u16 ioc_status;
1555f92363d1SSreekanth Reddy 	u32 ioc_state;
1556f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
1557f92363d1SSreekanth Reddy 
1558919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1559f92363d1SSreekanth Reddy 				 __func__));
1560f92363d1SSreekanth Reddy 
1561f92363d1SSreekanth Reddy 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1562f92363d1SSreekanth Reddy 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1563919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed due to ioc not operational\n",
1564919d8a3fSJoe Perches 			__func__);
1565f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1566f92363d1SSreekanth Reddy 		goto out;
1567f92363d1SSreekanth Reddy 	}
1568f92363d1SSreekanth Reddy 
1569f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1570919d8a3fSJoe Perches 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
1571f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1572f92363d1SSreekanth Reddy 		goto out;
1573f92363d1SSreekanth Reddy 	}
1574f92363d1SSreekanth Reddy 
1575f92363d1SSreekanth Reddy 	buffer_type = diag_register->buffer_type;
1576f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1577919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1578919d8a3fSJoe Perches 			__func__, buffer_type);
1579f92363d1SSreekanth Reddy 		return -EPERM;
1580f92363d1SSreekanth Reddy 	}
1581f92363d1SSreekanth Reddy 
158208e7378eSSreekanth Reddy 	if (diag_register->unique_id == 0) {
158308e7378eSSreekanth Reddy 		ioc_err(ioc,
158408e7378eSSreekanth Reddy 		    "%s: Invalid UID(0x%08x), buffer_type(0x%02x)\n", __func__,
158508e7378eSSreekanth Reddy 		    diag_register->unique_id, buffer_type);
158608e7378eSSreekanth Reddy 		return -EINVAL;
158708e7378eSSreekanth Reddy 	}
158808e7378eSSreekanth Reddy 
1589a8a6cbcdSSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
1590a8a6cbcdSSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_APP_OWNED) &&
1591a8a6cbcdSSreekanth Reddy 	    !(ioc->diag_buffer_status[buffer_type] &
1592a8a6cbcdSSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED)) {
1593a8a6cbcdSSreekanth Reddy 		ioc_err(ioc,
1594a8a6cbcdSSreekanth Reddy 		    "%s: buffer_type(0x%02x) is already registered by application with UID(0x%08x)\n",
1595a8a6cbcdSSreekanth Reddy 		    __func__, buffer_type, ioc->unique_id[buffer_type]);
1596a8a6cbcdSSreekanth Reddy 		return -EINVAL;
1597a8a6cbcdSSreekanth Reddy 	}
1598a8a6cbcdSSreekanth Reddy 
1599f92363d1SSreekanth Reddy 	if (ioc->diag_buffer_status[buffer_type] &
1600f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) {
160108e7378eSSreekanth Reddy 		/*
160208e7378eSSreekanth Reddy 		 * If driver posts buffer initially, then an application wants
160308e7378eSSreekanth Reddy 		 * to Register that buffer (own it) without Releasing first,
160408e7378eSSreekanth Reddy 		 * the application Register command MUST have the same buffer
160508e7378eSSreekanth Reddy 		 * type and size in the Register command (obtained from the
160608e7378eSSreekanth Reddy 		 * Query command). Otherwise that Register command will be
160708e7378eSSreekanth Reddy 		 * failed. If the application has released the buffer but wants
160808e7378eSSreekanth Reddy 		 * to re-register it, it should be allowed as long as the
160908e7378eSSreekanth Reddy 		 * Unique-Id/Size match.
161008e7378eSSreekanth Reddy 		 */
161108e7378eSSreekanth Reddy 
161208e7378eSSreekanth Reddy 		if (ioc->unique_id[buffer_type] == MPT3DIAGBUFFUNIQUEID &&
161308e7378eSSreekanth Reddy 		    ioc->diag_buffer_sz[buffer_type] ==
161408e7378eSSreekanth Reddy 		    diag_register->requested_buffer_size) {
161508e7378eSSreekanth Reddy 
161608e7378eSSreekanth Reddy 			if (!(ioc->diag_buffer_status[buffer_type] &
161708e7378eSSreekanth Reddy 			     MPT3_DIAG_BUFFER_IS_RELEASED)) {
161808e7378eSSreekanth Reddy 				dctlprintk(ioc, ioc_info(ioc,
161908e7378eSSreekanth Reddy 				    "%s: diag_buffer (%d) ownership changed. old-ID(0x%08x), new-ID(0x%08x)\n",
162008e7378eSSreekanth Reddy 				    __func__, buffer_type,
162108e7378eSSreekanth Reddy 				    ioc->unique_id[buffer_type],
162208e7378eSSreekanth Reddy 				    diag_register->unique_id));
162308e7378eSSreekanth Reddy 
162408e7378eSSreekanth Reddy 				/*
162508e7378eSSreekanth Reddy 				 * Application wants to own the buffer with
162608e7378eSSreekanth Reddy 				 * the same size.
162708e7378eSSreekanth Reddy 				 */
162808e7378eSSreekanth Reddy 				ioc->unique_id[buffer_type] =
162908e7378eSSreekanth Reddy 				    diag_register->unique_id;
163008e7378eSSreekanth Reddy 				rc = 0; /* success */
163108e7378eSSreekanth Reddy 				goto out;
163208e7378eSSreekanth Reddy 			}
163308e7378eSSreekanth Reddy 		} else if (ioc->unique_id[buffer_type] !=
163408e7378eSSreekanth Reddy 		    MPT3DIAGBUFFUNIQUEID) {
163508e7378eSSreekanth Reddy 			if (ioc->unique_id[buffer_type] !=
163608e7378eSSreekanth Reddy 			    diag_register->unique_id ||
163708e7378eSSreekanth Reddy 			    ioc->diag_buffer_sz[buffer_type] !=
163808e7378eSSreekanth Reddy 			    diag_register->requested_buffer_size ||
163908e7378eSSreekanth Reddy 			    !(ioc->diag_buffer_status[buffer_type] &
164008e7378eSSreekanth Reddy 			    MPT3_DIAG_BUFFER_IS_RELEASED)) {
164108e7378eSSreekanth Reddy 				ioc_err(ioc,
164208e7378eSSreekanth Reddy 				    "%s: already has a registered buffer for buffer_type(0x%02x)\n",
164308e7378eSSreekanth Reddy 				    __func__, buffer_type);
164408e7378eSSreekanth Reddy 				return -EINVAL;
164508e7378eSSreekanth Reddy 			}
164608e7378eSSreekanth Reddy 		} else {
1647919d8a3fSJoe Perches 			ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1648919d8a3fSJoe Perches 			    __func__, buffer_type);
1649f92363d1SSreekanth Reddy 			return -EINVAL;
1650f92363d1SSreekanth Reddy 		}
1651a066f4c3SSreekanth Reddy 	} else if (ioc->diag_buffer_status[buffer_type] &
1652a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
1653a066f4c3SSreekanth Reddy 
1654a066f4c3SSreekanth Reddy 		if (ioc->unique_id[buffer_type] != MPT3DIAGBUFFUNIQUEID ||
1655a066f4c3SSreekanth Reddy 		    ioc->diag_buffer_sz[buffer_type] !=
1656a066f4c3SSreekanth Reddy 		    diag_register->requested_buffer_size) {
1657a066f4c3SSreekanth Reddy 
1658a066f4c3SSreekanth Reddy 			ioc_err(ioc,
1659a066f4c3SSreekanth Reddy 			    "%s: already a buffer is allocated for buffer_type(0x%02x) of size %d bytes, so please try registering again with same size\n",
1660a066f4c3SSreekanth Reddy 			     __func__, buffer_type,
1661a066f4c3SSreekanth Reddy 			    ioc->diag_buffer_sz[buffer_type]);
1662a066f4c3SSreekanth Reddy 			return -EINVAL;
1663a066f4c3SSreekanth Reddy 		}
166408e7378eSSreekanth Reddy 	}
1665f92363d1SSreekanth Reddy 
1666f92363d1SSreekanth Reddy 	if (diag_register->requested_buffer_size % 4)  {
1667919d8a3fSJoe Perches 		ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
1668919d8a3fSJoe Perches 			__func__);
1669f92363d1SSreekanth Reddy 		return -EINVAL;
1670f92363d1SSreekanth Reddy 	}
1671f92363d1SSreekanth Reddy 
1672f92363d1SSreekanth Reddy 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1673f92363d1SSreekanth Reddy 	if (!smid) {
1674919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1675f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1676f92363d1SSreekanth Reddy 		goto out;
1677f92363d1SSreekanth Reddy 	}
1678f92363d1SSreekanth Reddy 
1679f92363d1SSreekanth Reddy 	rc = 0;
1680f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1681f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1682f92363d1SSreekanth Reddy 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1683f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
1684f92363d1SSreekanth Reddy 
1685f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
1686f92363d1SSreekanth Reddy 	request_data_sz = diag_register->requested_buffer_size;
1687f92363d1SSreekanth Reddy 	ioc->unique_id[buffer_type] = diag_register->unique_id;
1688a066f4c3SSreekanth Reddy 	ioc->diag_buffer_status[buffer_type] &=
1689a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1690f92363d1SSreekanth Reddy 	memcpy(ioc->product_specific[buffer_type],
1691f92363d1SSreekanth Reddy 	    diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1692f92363d1SSreekanth Reddy 	ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1693f92363d1SSreekanth Reddy 
1694f92363d1SSreekanth Reddy 	if (request_data) {
1695f92363d1SSreekanth Reddy 		request_data_dma = ioc->diag_buffer_dma[buffer_type];
1696f92363d1SSreekanth Reddy 		if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
16971c2048bdSChristoph Hellwig 			dma_free_coherent(&ioc->pdev->dev,
1698f92363d1SSreekanth Reddy 					ioc->diag_buffer_sz[buffer_type],
1699f92363d1SSreekanth Reddy 					request_data, request_data_dma);
1700f92363d1SSreekanth Reddy 			request_data = NULL;
1701f92363d1SSreekanth Reddy 		}
1702f92363d1SSreekanth Reddy 	}
1703f92363d1SSreekanth Reddy 
1704f92363d1SSreekanth Reddy 	if (request_data == NULL) {
1705f92363d1SSreekanth Reddy 		ioc->diag_buffer_sz[buffer_type] = 0;
1706f92363d1SSreekanth Reddy 		ioc->diag_buffer_dma[buffer_type] = 0;
17071c2048bdSChristoph Hellwig 		request_data = dma_alloc_coherent(&ioc->pdev->dev,
17081c2048bdSChristoph Hellwig 				request_data_sz, &request_data_dma, GFP_KERNEL);
1709f92363d1SSreekanth Reddy 		if (request_data == NULL) {
1710919d8a3fSJoe Perches 			ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
1711919d8a3fSJoe Perches 				__func__, request_data_sz);
1712f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
1713782b2818SSreekanth Reddy 			rc = -ENOMEM;
1714782b2818SSreekanth Reddy 			goto out;
1715f92363d1SSreekanth Reddy 		}
1716f92363d1SSreekanth Reddy 		ioc->diag_buffer[buffer_type] = request_data;
1717f92363d1SSreekanth Reddy 		ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1718f92363d1SSreekanth Reddy 		ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1719f92363d1SSreekanth Reddy 	}
1720f92363d1SSreekanth Reddy 
1721f92363d1SSreekanth Reddy 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1722f92363d1SSreekanth Reddy 	mpi_request->BufferType = diag_register->buffer_type;
1723f92363d1SSreekanth Reddy 	mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1724f92363d1SSreekanth Reddy 	mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1725f92363d1SSreekanth Reddy 	mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1726f92363d1SSreekanth Reddy 	mpi_request->VF_ID = 0; /* TODO */
1727f92363d1SSreekanth Reddy 	mpi_request->VP_ID = 0;
1728f92363d1SSreekanth Reddy 
1729919d8a3fSJoe Perches 	dctlprintk(ioc,
1730919d8a3fSJoe Perches 		   ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1731919d8a3fSJoe Perches 			    __func__, request_data,
1732f92363d1SSreekanth Reddy 			    (unsigned long long)request_data_dma,
1733f92363d1SSreekanth Reddy 			    le32_to_cpu(mpi_request->BufferLength)));
1734f92363d1SSreekanth Reddy 
1735f92363d1SSreekanth Reddy 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1736f92363d1SSreekanth Reddy 		mpi_request->ProductSpecific[i] =
1737f92363d1SSreekanth Reddy 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
1738f92363d1SSreekanth Reddy 
1739f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
1740078a4cc1SSuganath Prabu S 	ioc->put_smid_default(ioc, smid);
17418bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
1742f92363d1SSreekanth Reddy 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1743f92363d1SSreekanth Reddy 
1744f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1745c6bdb6a1SSreekanth Reddy 		mpt3sas_check_cmd_timeout(ioc,
1746d37306caSChaitra P B 		    ioc->ctl_cmds.status, mpi_request,
1747c6bdb6a1SSreekanth Reddy 		    sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
1748f92363d1SSreekanth Reddy 		goto issue_host_reset;
1749f92363d1SSreekanth Reddy 	}
1750f92363d1SSreekanth Reddy 
1751f92363d1SSreekanth Reddy 	/* process the completed Reply Message Frame */
1752f92363d1SSreekanth Reddy 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1753919d8a3fSJoe Perches 		ioc_err(ioc, "%s: no reply message\n", __func__);
1754f92363d1SSreekanth Reddy 		rc = -EFAULT;
1755f92363d1SSreekanth Reddy 		goto out;
1756f92363d1SSreekanth Reddy 	}
1757f92363d1SSreekanth Reddy 
1758f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
1759f92363d1SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1760f92363d1SSreekanth Reddy 
1761f92363d1SSreekanth Reddy 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1762f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
1763f92363d1SSreekanth Reddy 			MPT3_DIAG_BUFFER_IS_REGISTERED;
1764919d8a3fSJoe Perches 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
1765f92363d1SSreekanth Reddy 	} else {
1766919d8a3fSJoe Perches 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1767919d8a3fSJoe Perches 			 __func__,
1768f92363d1SSreekanth Reddy 			 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1769f92363d1SSreekanth Reddy 		rc = -EFAULT;
1770f92363d1SSreekanth Reddy 	}
1771f92363d1SSreekanth Reddy 
1772f92363d1SSreekanth Reddy  issue_host_reset:
1773f92363d1SSreekanth Reddy 	if (issue_reset)
177498c56ad3SCalvin Owens 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1775f92363d1SSreekanth Reddy 
1776f92363d1SSreekanth Reddy  out:
1777f92363d1SSreekanth Reddy 
1778a066f4c3SSreekanth Reddy 	if (rc && request_data) {
17791c2048bdSChristoph Hellwig 		dma_free_coherent(&ioc->pdev->dev, request_data_sz,
1780f92363d1SSreekanth Reddy 		    request_data, request_data_dma);
1781a066f4c3SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] &=
1782a066f4c3SSreekanth Reddy 		    ~MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1783a066f4c3SSreekanth Reddy 	}
1784f92363d1SSreekanth Reddy 
1785f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1786f92363d1SSreekanth Reddy 	return rc;
1787f92363d1SSreekanth Reddy }
1788f92363d1SSreekanth Reddy 
1789f92363d1SSreekanth Reddy /**
1790f92363d1SSreekanth Reddy  * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1791f92363d1SSreekanth Reddy  * @ioc: per adapter object
1792f92363d1SSreekanth Reddy  * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1793f92363d1SSreekanth Reddy  *
1794f92363d1SSreekanth Reddy  * This is called when command line option diag_buffer_enable is enabled
1795f92363d1SSreekanth Reddy  * at driver load time.
1796f92363d1SSreekanth Reddy  */
1797f92363d1SSreekanth Reddy void
1798f92363d1SSreekanth Reddy mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1799f92363d1SSreekanth Reddy {
1800f92363d1SSreekanth Reddy 	struct mpt3_diag_register diag_register;
1801d04a6edfSSreekanth Reddy 	u32 ret_val;
1802d04a6edfSSreekanth Reddy 	u32 trace_buff_size = ioc->manu_pg11.HostTraceBufferMaxSizeKB<<10;
1803d04a6edfSSreekanth Reddy 	u32 min_trace_buff_size = 0;
1804d04a6edfSSreekanth Reddy 	u32 decr_trace_buff_size = 0;
1805f92363d1SSreekanth Reddy 
1806f92363d1SSreekanth Reddy 	memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1807f92363d1SSreekanth Reddy 
1808f92363d1SSreekanth Reddy 	if (bits_to_register & 1) {
1809919d8a3fSJoe Perches 		ioc_info(ioc, "registering trace buffer support\n");
1810f92363d1SSreekanth Reddy 		ioc->diag_trigger_master.MasterData =
1811f92363d1SSreekanth Reddy 		    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1812f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
181308e7378eSSreekanth Reddy 		diag_register.unique_id =
181408e7378eSSreekanth Reddy 		    (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
181508e7378eSSreekanth Reddy 		    (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
1816d04a6edfSSreekanth Reddy 
1817d04a6edfSSreekanth Reddy 		if (trace_buff_size != 0) {
1818d04a6edfSSreekanth Reddy 			diag_register.requested_buffer_size = trace_buff_size;
1819d04a6edfSSreekanth Reddy 			min_trace_buff_size =
1820d04a6edfSSreekanth Reddy 			    ioc->manu_pg11.HostTraceBufferMinSizeKB<<10;
1821d04a6edfSSreekanth Reddy 			decr_trace_buff_size =
1822d04a6edfSSreekanth Reddy 			    ioc->manu_pg11.HostTraceBufferDecrementSizeKB<<10;
1823d04a6edfSSreekanth Reddy 
1824d04a6edfSSreekanth Reddy 			if (min_trace_buff_size > trace_buff_size) {
1825d04a6edfSSreekanth Reddy 				/* The buff size is not set correctly */
1826d04a6edfSSreekanth Reddy 				ioc_err(ioc,
1827d04a6edfSSreekanth Reddy 				    "Min Trace Buff size (%d KB) greater than Max Trace Buff size (%d KB)\n",
1828d04a6edfSSreekanth Reddy 				     min_trace_buff_size>>10,
1829d04a6edfSSreekanth Reddy 				     trace_buff_size>>10);
1830d04a6edfSSreekanth Reddy 				ioc_err(ioc,
1831d04a6edfSSreekanth Reddy 				    "Using zero Min Trace Buff Size\n");
1832d04a6edfSSreekanth Reddy 				min_trace_buff_size = 0;
1833d04a6edfSSreekanth Reddy 			}
1834d04a6edfSSreekanth Reddy 
1835d04a6edfSSreekanth Reddy 			if (decr_trace_buff_size == 0) {
1836d04a6edfSSreekanth Reddy 				/*
1837d04a6edfSSreekanth Reddy 				 * retry the min size if decrement
1838d04a6edfSSreekanth Reddy 				 * is not available.
1839d04a6edfSSreekanth Reddy 				 */
1840d04a6edfSSreekanth Reddy 				decr_trace_buff_size =
1841d04a6edfSSreekanth Reddy 				    trace_buff_size - min_trace_buff_size;
1842d04a6edfSSreekanth Reddy 			}
1843d04a6edfSSreekanth Reddy 		} else {
1844f92363d1SSreekanth Reddy 			/* register for 2MB buffers  */
1845f92363d1SSreekanth Reddy 			diag_register.requested_buffer_size = 2 * (1024 * 1024);
1846d04a6edfSSreekanth Reddy 		}
1847d04a6edfSSreekanth Reddy 
1848d04a6edfSSreekanth Reddy 		do {
1849d04a6edfSSreekanth Reddy 			ret_val = _ctl_diag_register_2(ioc,  &diag_register);
1850d04a6edfSSreekanth Reddy 
1851d04a6edfSSreekanth Reddy 			if (ret_val == -ENOMEM && min_trace_buff_size &&
1852d04a6edfSSreekanth Reddy 			    (trace_buff_size - decr_trace_buff_size) >=
1853d04a6edfSSreekanth Reddy 			    min_trace_buff_size) {
1854d04a6edfSSreekanth Reddy 				/* adjust the buffer size */
1855d04a6edfSSreekanth Reddy 				trace_buff_size -= decr_trace_buff_size;
1856d04a6edfSSreekanth Reddy 				diag_register.requested_buffer_size =
1857d04a6edfSSreekanth Reddy 				    trace_buff_size;
1858d04a6edfSSreekanth Reddy 			} else
1859d04a6edfSSreekanth Reddy 				break;
1860d04a6edfSSreekanth Reddy 		} while (true);
1861d04a6edfSSreekanth Reddy 
1862d04a6edfSSreekanth Reddy 		if (ret_val == -ENOMEM)
1863d04a6edfSSreekanth Reddy 			ioc_err(ioc,
1864d04a6edfSSreekanth Reddy 			    "Cannot allocate trace buffer memory. Last memory tried = %d KB\n",
1865d04a6edfSSreekanth Reddy 			    diag_register.requested_buffer_size>>10);
1866d04a6edfSSreekanth Reddy 		else if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE]
1867a066f4c3SSreekanth Reddy 		    & MPT3_DIAG_BUFFER_IS_REGISTERED) {
1868d04a6edfSSreekanth Reddy 			ioc_err(ioc, "Trace buffer memory %d KB allocated\n",
1869d04a6edfSSreekanth Reddy 			    diag_register.requested_buffer_size>>10);
1870a066f4c3SSreekanth Reddy 			if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
1871a066f4c3SSreekanth Reddy 				ioc->diag_buffer_status[
1872a066f4c3SSreekanth Reddy 				    MPI2_DIAG_BUF_TYPE_TRACE] |=
1873a066f4c3SSreekanth Reddy 				    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1874a066f4c3SSreekanth Reddy 		}
1875f92363d1SSreekanth Reddy 	}
1876f92363d1SSreekanth Reddy 
1877f92363d1SSreekanth Reddy 	if (bits_to_register & 2) {
1878919d8a3fSJoe Perches 		ioc_info(ioc, "registering snapshot buffer support\n");
1879f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1880f92363d1SSreekanth Reddy 		/* register for 2MB buffers  */
1881f92363d1SSreekanth Reddy 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1882f92363d1SSreekanth Reddy 		diag_register.unique_id = 0x7075901;
1883f92363d1SSreekanth Reddy 		_ctl_diag_register_2(ioc,  &diag_register);
1884f92363d1SSreekanth Reddy 	}
1885f92363d1SSreekanth Reddy 
1886f92363d1SSreekanth Reddy 	if (bits_to_register & 4) {
1887919d8a3fSJoe Perches 		ioc_info(ioc, "registering extended buffer support\n");
1888f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1889f92363d1SSreekanth Reddy 		/* register for 2MB buffers  */
1890f92363d1SSreekanth Reddy 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1891f92363d1SSreekanth Reddy 		diag_register.unique_id = 0x7075901;
1892f92363d1SSreekanth Reddy 		_ctl_diag_register_2(ioc,  &diag_register);
1893f92363d1SSreekanth Reddy 	}
1894f92363d1SSreekanth Reddy }
1895f92363d1SSreekanth Reddy 
1896f92363d1SSreekanth Reddy /**
1897f92363d1SSreekanth Reddy  * _ctl_diag_register - application register with driver
1898f92363d1SSreekanth Reddy  * @ioc: per adapter object
18994beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1900f92363d1SSreekanth Reddy  *
1901f92363d1SSreekanth Reddy  * This will allow the driver to setup any required buffers that will be
1902f92363d1SSreekanth Reddy  * needed by firmware to communicate with the driver.
1903f92363d1SSreekanth Reddy  */
1904f92363d1SSreekanth Reddy static long
1905f92363d1SSreekanth Reddy _ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1906f92363d1SSreekanth Reddy {
1907f92363d1SSreekanth Reddy 	struct mpt3_diag_register karg;
1908f92363d1SSreekanth Reddy 	long rc;
1909f92363d1SSreekanth Reddy 
1910f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1911f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1912f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1913f92363d1SSreekanth Reddy 		return -EFAULT;
1914f92363d1SSreekanth Reddy 	}
1915f92363d1SSreekanth Reddy 
1916f92363d1SSreekanth Reddy 	rc = _ctl_diag_register_2(ioc, &karg);
1917a8a6cbcdSSreekanth Reddy 
1918a8a6cbcdSSreekanth Reddy 	if (!rc && (ioc->diag_buffer_status[karg.buffer_type] &
1919a8a6cbcdSSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED))
1920a8a6cbcdSSreekanth Reddy 		ioc->diag_buffer_status[karg.buffer_type] |=
1921a8a6cbcdSSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_APP_OWNED;
1922a8a6cbcdSSreekanth Reddy 
1923f92363d1SSreekanth Reddy 	return rc;
1924f92363d1SSreekanth Reddy }
1925f92363d1SSreekanth Reddy 
1926f92363d1SSreekanth Reddy /**
1927f92363d1SSreekanth Reddy  * _ctl_diag_unregister - application unregister with driver
1928f92363d1SSreekanth Reddy  * @ioc: per adapter object
19294beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1930f92363d1SSreekanth Reddy  *
1931f92363d1SSreekanth Reddy  * This will allow the driver to cleanup any memory allocated for diag
1932f92363d1SSreekanth Reddy  * messages and to free up any resources.
1933f92363d1SSreekanth Reddy  */
1934f92363d1SSreekanth Reddy static long
1935f92363d1SSreekanth Reddy _ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1936f92363d1SSreekanth Reddy {
1937f92363d1SSreekanth Reddy 	struct mpt3_diag_unregister karg;
1938f92363d1SSreekanth Reddy 	void *request_data;
1939f92363d1SSreekanth Reddy 	dma_addr_t request_data_dma;
1940f92363d1SSreekanth Reddy 	u32 request_data_sz;
1941f92363d1SSreekanth Reddy 	u8 buffer_type;
1942f92363d1SSreekanth Reddy 
1943f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1944f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1945f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1946f92363d1SSreekanth Reddy 		return -EFAULT;
1947f92363d1SSreekanth Reddy 	}
1948f92363d1SSreekanth Reddy 
1949919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1950f92363d1SSreekanth Reddy 				 __func__));
1951f92363d1SSreekanth Reddy 
195208e7378eSSreekanth Reddy 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
195308e7378eSSreekanth Reddy 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
195408e7378eSSreekanth Reddy 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
195508e7378eSSreekanth Reddy 		    __func__, karg.unique_id);
195608e7378eSSreekanth Reddy 		return -EINVAL;
195708e7378eSSreekanth Reddy 	}
195808e7378eSSreekanth Reddy 
1959f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1960919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1961919d8a3fSJoe Perches 			__func__, buffer_type);
1962f92363d1SSreekanth Reddy 		return -EPERM;
1963f92363d1SSreekanth Reddy 	}
1964f92363d1SSreekanth Reddy 
1965f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
1966f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1967919d8a3fSJoe Perches 		ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1968919d8a3fSJoe Perches 			__func__, buffer_type);
1969f92363d1SSreekanth Reddy 		return -EINVAL;
1970f92363d1SSreekanth Reddy 	}
1971f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
1972f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
1973919d8a3fSJoe Perches 		ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
1974919d8a3fSJoe Perches 			__func__, buffer_type);
1975f92363d1SSreekanth Reddy 		return -EINVAL;
1976f92363d1SSreekanth Reddy 	}
1977f92363d1SSreekanth Reddy 
1978f92363d1SSreekanth Reddy 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1979919d8a3fSJoe Perches 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1980919d8a3fSJoe Perches 			__func__, karg.unique_id);
1981f92363d1SSreekanth Reddy 		return -EINVAL;
1982f92363d1SSreekanth Reddy 	}
1983f92363d1SSreekanth Reddy 
1984f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
1985f92363d1SSreekanth Reddy 	if (!request_data) {
1986919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1987919d8a3fSJoe Perches 			__func__, buffer_type);
1988f92363d1SSreekanth Reddy 		return -ENOMEM;
1989f92363d1SSreekanth Reddy 	}
1990f92363d1SSreekanth Reddy 
1991a066f4c3SSreekanth Reddy 	if (ioc->diag_buffer_status[buffer_type] &
1992a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
1993a066f4c3SSreekanth Reddy 		ioc->unique_id[buffer_type] = MPT3DIAGBUFFUNIQUEID;
1994a066f4c3SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] &=
1995a8a6cbcdSSreekanth Reddy 		    ~MPT3_DIAG_BUFFER_IS_APP_OWNED;
1996a8a6cbcdSSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] &=
1997a066f4c3SSreekanth Reddy 		    ~MPT3_DIAG_BUFFER_IS_REGISTERED;
1998a066f4c3SSreekanth Reddy 	} else {
1999f92363d1SSreekanth Reddy 		request_data_sz = ioc->diag_buffer_sz[buffer_type];
2000f92363d1SSreekanth Reddy 		request_data_dma = ioc->diag_buffer_dma[buffer_type];
20011c2048bdSChristoph Hellwig 		dma_free_coherent(&ioc->pdev->dev, request_data_sz,
2002f92363d1SSreekanth Reddy 				request_data, request_data_dma);
2003f92363d1SSreekanth Reddy 		ioc->diag_buffer[buffer_type] = NULL;
2004f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] = 0;
2005a066f4c3SSreekanth Reddy 	}
2006f92363d1SSreekanth Reddy 	return 0;
2007f92363d1SSreekanth Reddy }
2008f92363d1SSreekanth Reddy 
2009f92363d1SSreekanth Reddy /**
2010f92363d1SSreekanth Reddy  * _ctl_diag_query - query relevant info associated with diag buffers
2011f92363d1SSreekanth Reddy  * @ioc: per adapter object
20124beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
2013f92363d1SSreekanth Reddy  *
2014f92363d1SSreekanth Reddy  * The application will send only buffer_type and unique_id.  Driver will
2015f92363d1SSreekanth Reddy  * inspect unique_id first, if valid, fill in all the info.  If unique_id is
2016f92363d1SSreekanth Reddy  * 0x00, the driver will return info specified by Buffer Type.
2017f92363d1SSreekanth Reddy  */
2018f92363d1SSreekanth Reddy static long
2019f92363d1SSreekanth Reddy _ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2020f92363d1SSreekanth Reddy {
2021f92363d1SSreekanth Reddy 	struct mpt3_diag_query karg;
2022f92363d1SSreekanth Reddy 	void *request_data;
2023f92363d1SSreekanth Reddy 	int i;
2024f92363d1SSreekanth Reddy 	u8 buffer_type;
2025f92363d1SSreekanth Reddy 
2026f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2027f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2028f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2029f92363d1SSreekanth Reddy 		return -EFAULT;
2030f92363d1SSreekanth Reddy 	}
2031f92363d1SSreekanth Reddy 
2032919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2033f92363d1SSreekanth Reddy 				 __func__));
2034f92363d1SSreekanth Reddy 
2035f92363d1SSreekanth Reddy 	karg.application_flags = 0;
2036f92363d1SSreekanth Reddy 	buffer_type = karg.buffer_type;
2037f92363d1SSreekanth Reddy 
2038f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2039919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2040919d8a3fSJoe Perches 			__func__, buffer_type);
2041f92363d1SSreekanth Reddy 		return -EPERM;
2042f92363d1SSreekanth Reddy 	}
2043f92363d1SSreekanth Reddy 
2044a066f4c3SSreekanth Reddy 	if (!(ioc->diag_buffer_status[buffer_type] &
2045a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED)) {
2046f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[buffer_type] &
2047f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2048919d8a3fSJoe Perches 			ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2049919d8a3fSJoe Perches 				__func__, buffer_type);
2050f92363d1SSreekanth Reddy 			return -EINVAL;
2051f92363d1SSreekanth Reddy 		}
2052a066f4c3SSreekanth Reddy 	}
2053f92363d1SSreekanth Reddy 
205408e7378eSSreekanth Reddy 	if (karg.unique_id) {
2055f92363d1SSreekanth Reddy 		if (karg.unique_id != ioc->unique_id[buffer_type]) {
2056919d8a3fSJoe Perches 			ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2057919d8a3fSJoe Perches 				__func__, karg.unique_id);
2058f92363d1SSreekanth Reddy 			return -EINVAL;
2059f92363d1SSreekanth Reddy 		}
2060f92363d1SSreekanth Reddy 	}
2061f92363d1SSreekanth Reddy 
2062f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
2063f92363d1SSreekanth Reddy 	if (!request_data) {
2064919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2065919d8a3fSJoe Perches 			__func__, buffer_type);
2066f92363d1SSreekanth Reddy 		return -ENOMEM;
2067f92363d1SSreekanth Reddy 	}
2068f92363d1SSreekanth Reddy 
2069a066f4c3SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2070a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED))
2071a066f4c3SSreekanth Reddy 		karg.application_flags |= MPT3_APP_FLAGS_BUFFER_VALID;
2072a066f4c3SSreekanth Reddy 
2073a066f4c3SSreekanth Reddy 	if (!(ioc->diag_buffer_status[buffer_type] &
2074a066f4c3SSreekanth Reddy 	     MPT3_DIAG_BUFFER_IS_RELEASED))
2075a066f4c3SSreekanth Reddy 		karg.application_flags |= MPT3_APP_FLAGS_FW_BUFFER_ACCESS;
2076a066f4c3SSreekanth Reddy 
2077a066f4c3SSreekanth Reddy 	if (!(ioc->diag_buffer_status[buffer_type] &
2078a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED))
2079a066f4c3SSreekanth Reddy 		karg.application_flags |= MPT3_APP_FLAGS_DYNAMIC_BUFFER_ALLOC;
2080f92363d1SSreekanth Reddy 
2081a8a6cbcdSSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2082a8a6cbcdSSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_APP_OWNED))
2083a8a6cbcdSSreekanth Reddy 		karg.application_flags |= MPT3_APP_FLAGS_APP_OWNED;
2084a8a6cbcdSSreekanth Reddy 
2085f92363d1SSreekanth Reddy 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2086f92363d1SSreekanth Reddy 		karg.product_specific[i] =
2087f92363d1SSreekanth Reddy 		    ioc->product_specific[buffer_type][i];
2088f92363d1SSreekanth Reddy 
2089f92363d1SSreekanth Reddy 	karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
2090f92363d1SSreekanth Reddy 	karg.driver_added_buffer_size = 0;
2091f92363d1SSreekanth Reddy 	karg.unique_id = ioc->unique_id[buffer_type];
2092f92363d1SSreekanth Reddy 	karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
2093f92363d1SSreekanth Reddy 
2094f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
2095919d8a3fSJoe Perches 		ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
2096919d8a3fSJoe Perches 			__func__, arg);
2097f92363d1SSreekanth Reddy 		return -EFAULT;
2098f92363d1SSreekanth Reddy 	}
2099f92363d1SSreekanth Reddy 	return 0;
2100f92363d1SSreekanth Reddy }
2101f92363d1SSreekanth Reddy 
2102f92363d1SSreekanth Reddy /**
2103f92363d1SSreekanth Reddy  * mpt3sas_send_diag_release - Diag Release Message
2104f92363d1SSreekanth Reddy  * @ioc: per adapter object
21054beb4867SBart Van Assche  * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
21064beb4867SBart Van Assche  * @issue_reset: specifies whether host reset is required.
2107f92363d1SSreekanth Reddy  *
2108f92363d1SSreekanth Reddy  */
2109f92363d1SSreekanth Reddy int
2110f92363d1SSreekanth Reddy mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
2111f92363d1SSreekanth Reddy 	u8 *issue_reset)
2112f92363d1SSreekanth Reddy {
2113f92363d1SSreekanth Reddy 	Mpi2DiagReleaseRequest_t *mpi_request;
2114f92363d1SSreekanth Reddy 	Mpi2DiagReleaseReply_t *mpi_reply;
2115f92363d1SSreekanth Reddy 	u16 smid;
2116f92363d1SSreekanth Reddy 	u16 ioc_status;
2117f92363d1SSreekanth Reddy 	u32 ioc_state;
2118f92363d1SSreekanth Reddy 	int rc;
2119c6bdb6a1SSreekanth Reddy 	u8 reset_needed = 0;
2120f92363d1SSreekanth Reddy 
2121919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2122f92363d1SSreekanth Reddy 				 __func__));
2123f92363d1SSreekanth Reddy 
2124f92363d1SSreekanth Reddy 	rc = 0;
2125f92363d1SSreekanth Reddy 	*issue_reset = 0;
2126f92363d1SSreekanth Reddy 
2127c6bdb6a1SSreekanth Reddy 
2128f92363d1SSreekanth Reddy 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2129f92363d1SSreekanth Reddy 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2130f92363d1SSreekanth Reddy 		if (ioc->diag_buffer_status[buffer_type] &
2131f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED)
2132f92363d1SSreekanth Reddy 			ioc->diag_buffer_status[buffer_type] |=
2133f92363d1SSreekanth Reddy 			    MPT3_DIAG_BUFFER_IS_RELEASED;
2134919d8a3fSJoe Perches 		dctlprintk(ioc,
2135919d8a3fSJoe Perches 			   ioc_info(ioc, "%s: skipping due to FAULT state\n",
2136f92363d1SSreekanth Reddy 				    __func__));
2137f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2138f92363d1SSreekanth Reddy 		goto out;
2139f92363d1SSreekanth Reddy 	}
2140f92363d1SSreekanth Reddy 
2141f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2142919d8a3fSJoe Perches 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
2143f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2144f92363d1SSreekanth Reddy 		goto out;
2145f92363d1SSreekanth Reddy 	}
2146f92363d1SSreekanth Reddy 
2147f92363d1SSreekanth Reddy 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2148f92363d1SSreekanth Reddy 	if (!smid) {
2149919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2150f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2151f92363d1SSreekanth Reddy 		goto out;
2152f92363d1SSreekanth Reddy 	}
2153f92363d1SSreekanth Reddy 
2154f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2155f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2156f92363d1SSreekanth Reddy 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2157f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
2158f92363d1SSreekanth Reddy 
2159f92363d1SSreekanth Reddy 	mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
2160f92363d1SSreekanth Reddy 	mpi_request->BufferType = buffer_type;
2161f92363d1SSreekanth Reddy 	mpi_request->VF_ID = 0; /* TODO */
2162f92363d1SSreekanth Reddy 	mpi_request->VP_ID = 0;
2163f92363d1SSreekanth Reddy 
2164f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
2165078a4cc1SSuganath Prabu S 	ioc->put_smid_default(ioc, smid);
21668bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
2167f92363d1SSreekanth Reddy 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2168f92363d1SSreekanth Reddy 
2169f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2170c6bdb6a1SSreekanth Reddy 		mpt3sas_check_cmd_timeout(ioc,
2171d37306caSChaitra P B 		    ioc->ctl_cmds.status, mpi_request,
2172c6bdb6a1SSreekanth Reddy 		    sizeof(Mpi2DiagReleaseRequest_t)/4, reset_needed);
2173c6bdb6a1SSreekanth Reddy 		 *issue_reset = reset_needed;
2174f92363d1SSreekanth Reddy 		rc = -EFAULT;
2175f92363d1SSreekanth Reddy 		goto out;
2176f92363d1SSreekanth Reddy 	}
2177f92363d1SSreekanth Reddy 
2178f92363d1SSreekanth Reddy 	/* process the completed Reply Message Frame */
2179f92363d1SSreekanth Reddy 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2180919d8a3fSJoe Perches 		ioc_err(ioc, "%s: no reply message\n", __func__);
2181f92363d1SSreekanth Reddy 		rc = -EFAULT;
2182f92363d1SSreekanth Reddy 		goto out;
2183f92363d1SSreekanth Reddy 	}
2184f92363d1SSreekanth Reddy 
2185f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
2186f92363d1SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2187f92363d1SSreekanth Reddy 
2188f92363d1SSreekanth Reddy 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2189f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
2190f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED;
2191919d8a3fSJoe Perches 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
2192f92363d1SSreekanth Reddy 	} else {
2193919d8a3fSJoe Perches 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2194919d8a3fSJoe Perches 			 __func__,
2195f92363d1SSreekanth Reddy 			 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2196f92363d1SSreekanth Reddy 		rc = -EFAULT;
2197f92363d1SSreekanth Reddy 	}
2198f92363d1SSreekanth Reddy 
2199f92363d1SSreekanth Reddy  out:
2200f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2201f92363d1SSreekanth Reddy 	return rc;
2202f92363d1SSreekanth Reddy }
2203f92363d1SSreekanth Reddy 
2204f92363d1SSreekanth Reddy /**
2205f92363d1SSreekanth Reddy  * _ctl_diag_release - request to send Diag Release Message to firmware
22064beb4867SBart Van Assche  * @ioc: ?
22074beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
2208f92363d1SSreekanth Reddy  *
2209f92363d1SSreekanth Reddy  * This allows ownership of the specified buffer to returned to the driver,
2210f92363d1SSreekanth Reddy  * allowing an application to read the buffer without fear that firmware is
22119a284e5cSMasahiro Yamada  * overwriting information in the buffer.
2212f92363d1SSreekanth Reddy  */
2213f92363d1SSreekanth Reddy static long
2214f92363d1SSreekanth Reddy _ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2215f92363d1SSreekanth Reddy {
2216f92363d1SSreekanth Reddy 	struct mpt3_diag_release karg;
2217f92363d1SSreekanth Reddy 	void *request_data;
2218f92363d1SSreekanth Reddy 	int rc;
2219f92363d1SSreekanth Reddy 	u8 buffer_type;
2220f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
2221f92363d1SSreekanth Reddy 
2222f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2223f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2224f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2225f92363d1SSreekanth Reddy 		return -EFAULT;
2226f92363d1SSreekanth Reddy 	}
2227f92363d1SSreekanth Reddy 
2228919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2229f92363d1SSreekanth Reddy 				 __func__));
2230f92363d1SSreekanth Reddy 
223108e7378eSSreekanth Reddy 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
223208e7378eSSreekanth Reddy 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
223308e7378eSSreekanth Reddy 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
223408e7378eSSreekanth Reddy 		    __func__, karg.unique_id);
223508e7378eSSreekanth Reddy 		return -EINVAL;
223608e7378eSSreekanth Reddy 	}
223708e7378eSSreekanth Reddy 
2238f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2239919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2240919d8a3fSJoe Perches 			__func__, buffer_type);
2241f92363d1SSreekanth Reddy 		return -EPERM;
2242f92363d1SSreekanth Reddy 	}
2243f92363d1SSreekanth Reddy 
2244f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2245f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2246919d8a3fSJoe Perches 		ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2247919d8a3fSJoe Perches 			__func__, buffer_type);
2248f92363d1SSreekanth Reddy 		return -EINVAL;
2249f92363d1SSreekanth Reddy 	}
2250f92363d1SSreekanth Reddy 
2251f92363d1SSreekanth Reddy 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2252919d8a3fSJoe Perches 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2253919d8a3fSJoe Perches 			__func__, karg.unique_id);
2254f92363d1SSreekanth Reddy 		return -EINVAL;
2255f92363d1SSreekanth Reddy 	}
2256f92363d1SSreekanth Reddy 
2257f92363d1SSreekanth Reddy 	if (ioc->diag_buffer_status[buffer_type] &
2258f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
2259919d8a3fSJoe Perches 		ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
2260919d8a3fSJoe Perches 			__func__, buffer_type);
226129f571f8SSreekanth Reddy 		return -EINVAL;
2262f92363d1SSreekanth Reddy 	}
2263f92363d1SSreekanth Reddy 
2264f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
2265f92363d1SSreekanth Reddy 
2266f92363d1SSreekanth Reddy 	if (!request_data) {
2267919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2268919d8a3fSJoe Perches 			__func__, buffer_type);
2269f92363d1SSreekanth Reddy 		return -ENOMEM;
2270f92363d1SSreekanth Reddy 	}
2271f92363d1SSreekanth Reddy 
2272f92363d1SSreekanth Reddy 	/* buffers were released by due to host reset */
2273f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2274f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2275f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
2276f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED;
2277f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] &=
2278f92363d1SSreekanth Reddy 		    ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
2279919d8a3fSJoe Perches 		ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
2280919d8a3fSJoe Perches 			__func__, buffer_type);
2281f92363d1SSreekanth Reddy 		return 0;
2282f92363d1SSreekanth Reddy 	}
2283f92363d1SSreekanth Reddy 
2284f92363d1SSreekanth Reddy 	rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2285f92363d1SSreekanth Reddy 
2286f92363d1SSreekanth Reddy 	if (issue_reset)
228798c56ad3SCalvin Owens 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2288f92363d1SSreekanth Reddy 
2289f92363d1SSreekanth Reddy 	return rc;
2290f92363d1SSreekanth Reddy }
2291f92363d1SSreekanth Reddy 
2292f92363d1SSreekanth Reddy /**
2293f92363d1SSreekanth Reddy  * _ctl_diag_read_buffer - request for copy of the diag buffer
2294f92363d1SSreekanth Reddy  * @ioc: per adapter object
22954beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
2296f92363d1SSreekanth Reddy  */
2297f92363d1SSreekanth Reddy static long
2298f92363d1SSreekanth Reddy _ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2299f92363d1SSreekanth Reddy {
2300f92363d1SSreekanth Reddy 	struct mpt3_diag_read_buffer karg;
2301f92363d1SSreekanth Reddy 	struct mpt3_diag_read_buffer __user *uarg = arg;
2302f92363d1SSreekanth Reddy 	void *request_data, *diag_data;
2303f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostRequest_t *mpi_request;
2304f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostReply_t *mpi_reply;
2305f92363d1SSreekanth Reddy 	int rc, i;
2306f92363d1SSreekanth Reddy 	u8 buffer_type;
23078bbb1cf6SCalvin Owens 	unsigned long request_size, copy_size;
2308f92363d1SSreekanth Reddy 	u16 smid;
2309f92363d1SSreekanth Reddy 	u16 ioc_status;
2310f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
2311f92363d1SSreekanth Reddy 
2312f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2313f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2314f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2315f92363d1SSreekanth Reddy 		return -EFAULT;
2316f92363d1SSreekanth Reddy 	}
2317f92363d1SSreekanth Reddy 
2318919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2319f92363d1SSreekanth Reddy 				 __func__));
2320f92363d1SSreekanth Reddy 
232108e7378eSSreekanth Reddy 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
232208e7378eSSreekanth Reddy 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
232308e7378eSSreekanth Reddy 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
232408e7378eSSreekanth Reddy 		    __func__, karg.unique_id);
232508e7378eSSreekanth Reddy 		return -EINVAL;
232608e7378eSSreekanth Reddy 	}
232708e7378eSSreekanth Reddy 
2328f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2329919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2330919d8a3fSJoe Perches 			__func__, buffer_type);
2331f92363d1SSreekanth Reddy 		return -EPERM;
2332f92363d1SSreekanth Reddy 	}
2333f92363d1SSreekanth Reddy 
2334f92363d1SSreekanth Reddy 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2335919d8a3fSJoe Perches 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2336919d8a3fSJoe Perches 			__func__, karg.unique_id);
2337f92363d1SSreekanth Reddy 		return -EINVAL;
2338f92363d1SSreekanth Reddy 	}
2339f92363d1SSreekanth Reddy 
2340f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
2341f92363d1SSreekanth Reddy 	if (!request_data) {
2342919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2343919d8a3fSJoe Perches 			__func__, buffer_type);
2344f92363d1SSreekanth Reddy 		return -ENOMEM;
2345f92363d1SSreekanth Reddy 	}
2346f92363d1SSreekanth Reddy 
2347f92363d1SSreekanth Reddy 	request_size = ioc->diag_buffer_sz[buffer_type];
2348f92363d1SSreekanth Reddy 
2349f92363d1SSreekanth Reddy 	if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2350919d8a3fSJoe Perches 		ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
2351f92363d1SSreekanth Reddy 			__func__);
2352f92363d1SSreekanth Reddy 		return -EINVAL;
2353f92363d1SSreekanth Reddy 	}
2354f92363d1SSreekanth Reddy 
2355f92363d1SSreekanth Reddy 	if (karg.starting_offset > request_size)
2356f92363d1SSreekanth Reddy 		return -EINVAL;
2357f92363d1SSreekanth Reddy 
2358f92363d1SSreekanth Reddy 	diag_data = (void *)(request_data + karg.starting_offset);
2359919d8a3fSJoe Perches 	dctlprintk(ioc,
2360919d8a3fSJoe Perches 		   ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2361919d8a3fSJoe Perches 			    __func__, diag_data, karg.starting_offset,
2362919d8a3fSJoe Perches 			    karg.bytes_to_read));
2363f92363d1SSreekanth Reddy 
2364f92363d1SSreekanth Reddy 	/* Truncate data on requests that are too large */
2365f92363d1SSreekanth Reddy 	if ((diag_data + karg.bytes_to_read < diag_data) ||
2366f92363d1SSreekanth Reddy 	    (diag_data + karg.bytes_to_read > request_data + request_size))
2367f92363d1SSreekanth Reddy 		copy_size = request_size - karg.starting_offset;
2368f92363d1SSreekanth Reddy 	else
2369f92363d1SSreekanth Reddy 		copy_size = karg.bytes_to_read;
2370f92363d1SSreekanth Reddy 
2371f92363d1SSreekanth Reddy 	if (copy_to_user((void __user *)uarg->diagnostic_data,
2372f92363d1SSreekanth Reddy 	    diag_data, copy_size)) {
2373919d8a3fSJoe Perches 		ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2374919d8a3fSJoe Perches 			__func__, diag_data);
2375f92363d1SSreekanth Reddy 		return -EFAULT;
2376f92363d1SSreekanth Reddy 	}
2377f92363d1SSreekanth Reddy 
2378f92363d1SSreekanth Reddy 	if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2379f92363d1SSreekanth Reddy 		return 0;
2380f92363d1SSreekanth Reddy 
2381919d8a3fSJoe Perches 	dctlprintk(ioc,
2382919d8a3fSJoe Perches 		   ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
2383919d8a3fSJoe Perches 			    __func__, buffer_type));
2384f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2385f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2386919d8a3fSJoe Perches 		dctlprintk(ioc,
2387919d8a3fSJoe Perches 			   ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
2388919d8a3fSJoe Perches 				    __func__, buffer_type));
2389f92363d1SSreekanth Reddy 		return 0;
2390f92363d1SSreekanth Reddy 	}
2391f92363d1SSreekanth Reddy 	/* Get a free request frame and save the message context.
2392f92363d1SSreekanth Reddy 	*/
2393f92363d1SSreekanth Reddy 
2394f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2395919d8a3fSJoe Perches 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
2396f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2397f92363d1SSreekanth Reddy 		goto out;
2398f92363d1SSreekanth Reddy 	}
2399f92363d1SSreekanth Reddy 
2400f92363d1SSreekanth Reddy 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2401f92363d1SSreekanth Reddy 	if (!smid) {
2402919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2403f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2404f92363d1SSreekanth Reddy 		goto out;
2405f92363d1SSreekanth Reddy 	}
2406f92363d1SSreekanth Reddy 
2407f92363d1SSreekanth Reddy 	rc = 0;
2408f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2409f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2410f92363d1SSreekanth Reddy 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2411f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
2412f92363d1SSreekanth Reddy 
2413f92363d1SSreekanth Reddy 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2414f92363d1SSreekanth Reddy 	mpi_request->BufferType = buffer_type;
2415f92363d1SSreekanth Reddy 	mpi_request->BufferLength =
2416f92363d1SSreekanth Reddy 	    cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2417f92363d1SSreekanth Reddy 	mpi_request->BufferAddress =
2418f92363d1SSreekanth Reddy 	    cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2419f92363d1SSreekanth Reddy 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2420f92363d1SSreekanth Reddy 		mpi_request->ProductSpecific[i] =
2421f92363d1SSreekanth Reddy 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
2422f92363d1SSreekanth Reddy 	mpi_request->VF_ID = 0; /* TODO */
2423f92363d1SSreekanth Reddy 	mpi_request->VP_ID = 0;
2424f92363d1SSreekanth Reddy 
2425f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
2426078a4cc1SSuganath Prabu S 	ioc->put_smid_default(ioc, smid);
24278bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
2428f92363d1SSreekanth Reddy 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2429f92363d1SSreekanth Reddy 
2430f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2431c6bdb6a1SSreekanth Reddy 		mpt3sas_check_cmd_timeout(ioc,
2432d37306caSChaitra P B 		    ioc->ctl_cmds.status, mpi_request,
2433c6bdb6a1SSreekanth Reddy 		    sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
2434f92363d1SSreekanth Reddy 		goto issue_host_reset;
2435f92363d1SSreekanth Reddy 	}
2436f92363d1SSreekanth Reddy 
2437f92363d1SSreekanth Reddy 	/* process the completed Reply Message Frame */
2438f92363d1SSreekanth Reddy 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2439919d8a3fSJoe Perches 		ioc_err(ioc, "%s: no reply message\n", __func__);
2440f92363d1SSreekanth Reddy 		rc = -EFAULT;
2441f92363d1SSreekanth Reddy 		goto out;
2442f92363d1SSreekanth Reddy 	}
2443f92363d1SSreekanth Reddy 
2444f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
2445f92363d1SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2446f92363d1SSreekanth Reddy 
2447f92363d1SSreekanth Reddy 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2448f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
2449f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED;
2450dd180e4eSSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] &=
2451dd180e4eSSreekanth Reddy 		    ~MPT3_DIAG_BUFFER_IS_RELEASED;
2452919d8a3fSJoe Perches 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
2453f92363d1SSreekanth Reddy 	} else {
2454919d8a3fSJoe Perches 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2455919d8a3fSJoe Perches 			 __func__, ioc_status,
2456919d8a3fSJoe Perches 			 le32_to_cpu(mpi_reply->IOCLogInfo));
2457f92363d1SSreekanth Reddy 		rc = -EFAULT;
2458f92363d1SSreekanth Reddy 	}
2459f92363d1SSreekanth Reddy 
2460f92363d1SSreekanth Reddy  issue_host_reset:
2461f92363d1SSreekanth Reddy 	if (issue_reset)
246298c56ad3SCalvin Owens 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2463f92363d1SSreekanth Reddy 
2464f92363d1SSreekanth Reddy  out:
2465f92363d1SSreekanth Reddy 
2466f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2467f92363d1SSreekanth Reddy 	return rc;
2468f92363d1SSreekanth Reddy }
2469f92363d1SSreekanth Reddy 
2470f92363d1SSreekanth Reddy 
2471f92363d1SSreekanth Reddy 
2472f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2473f92363d1SSreekanth Reddy /**
2474f92363d1SSreekanth Reddy  * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2475f92363d1SSreekanth Reddy  * @ioc: per adapter object
24764beb4867SBart Van Assche  * @cmd: ioctl opcode
24774beb4867SBart Van Assche  * @arg: (struct mpt3_ioctl_command32)
2478f92363d1SSreekanth Reddy  *
2479f92363d1SSreekanth Reddy  * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2480f92363d1SSreekanth Reddy  */
2481f92363d1SSreekanth Reddy static long
2482f92363d1SSreekanth Reddy _ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2483f92363d1SSreekanth Reddy 	void __user *arg)
2484f92363d1SSreekanth Reddy {
2485f92363d1SSreekanth Reddy 	struct mpt3_ioctl_command32 karg32;
2486f92363d1SSreekanth Reddy 	struct mpt3_ioctl_command32 __user *uarg;
2487f92363d1SSreekanth Reddy 	struct mpt3_ioctl_command karg;
2488f92363d1SSreekanth Reddy 
2489f92363d1SSreekanth Reddy 	if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2490f92363d1SSreekanth Reddy 		return -EINVAL;
2491f92363d1SSreekanth Reddy 
2492f92363d1SSreekanth Reddy 	uarg = (struct mpt3_ioctl_command32 __user *) arg;
2493f92363d1SSreekanth Reddy 
2494f92363d1SSreekanth Reddy 	if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2495f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2496f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2497f92363d1SSreekanth Reddy 		return -EFAULT;
2498f92363d1SSreekanth Reddy 	}
2499f92363d1SSreekanth Reddy 
2500f92363d1SSreekanth Reddy 	memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2501f92363d1SSreekanth Reddy 	karg.hdr.ioc_number = karg32.hdr.ioc_number;
2502f92363d1SSreekanth Reddy 	karg.hdr.port_number = karg32.hdr.port_number;
2503f92363d1SSreekanth Reddy 	karg.hdr.max_data_size = karg32.hdr.max_data_size;
2504f92363d1SSreekanth Reddy 	karg.timeout = karg32.timeout;
2505f92363d1SSreekanth Reddy 	karg.max_reply_bytes = karg32.max_reply_bytes;
2506f92363d1SSreekanth Reddy 	karg.data_in_size = karg32.data_in_size;
2507f92363d1SSreekanth Reddy 	karg.data_out_size = karg32.data_out_size;
2508f92363d1SSreekanth Reddy 	karg.max_sense_bytes = karg32.max_sense_bytes;
2509f92363d1SSreekanth Reddy 	karg.data_sge_offset = karg32.data_sge_offset;
2510f92363d1SSreekanth Reddy 	karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2511f92363d1SSreekanth Reddy 	karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2512f92363d1SSreekanth Reddy 	karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2513f92363d1SSreekanth Reddy 	karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2514f92363d1SSreekanth Reddy 	return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2515f92363d1SSreekanth Reddy }
2516f92363d1SSreekanth Reddy #endif
2517f92363d1SSreekanth Reddy 
2518f92363d1SSreekanth Reddy /**
2519f92363d1SSreekanth Reddy  * _ctl_ioctl_main - main ioctl entry point
25204beb4867SBart Van Assche  * @file:  (struct file)
25214beb4867SBart Van Assche  * @cmd:  ioctl opcode
25224beb4867SBart Van Assche  * @arg:  user space data buffer
25234beb4867SBart Van Assche  * @compat:  handles 32 bit applications in 64bit os
2524c84b06a4SSreekanth Reddy  * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
2525b130b0d5SSuganath prabu Subramani  * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
2526f92363d1SSreekanth Reddy  */
2527f92363d1SSreekanth Reddy static long
2528f92363d1SSreekanth Reddy _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2529c84b06a4SSreekanth Reddy 	u8 compat, u16 mpi_version)
2530f92363d1SSreekanth Reddy {
2531f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc;
2532f92363d1SSreekanth Reddy 	struct mpt3_ioctl_header ioctl_header;
2533f92363d1SSreekanth Reddy 	enum block_state state;
2534f92363d1SSreekanth Reddy 	long ret = -EINVAL;
2535f92363d1SSreekanth Reddy 
2536f92363d1SSreekanth Reddy 	/* get IOCTL header */
2537f92363d1SSreekanth Reddy 	if (copy_from_user(&ioctl_header, (char __user *)arg,
2538f92363d1SSreekanth Reddy 	    sizeof(struct mpt3_ioctl_header))) {
2539f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2540f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2541f92363d1SSreekanth Reddy 		return -EFAULT;
2542f92363d1SSreekanth Reddy 	}
2543f92363d1SSreekanth Reddy 
2544c84b06a4SSreekanth Reddy 	if (_ctl_verify_adapter(ioctl_header.ioc_number,
2545c84b06a4SSreekanth Reddy 				&ioc, mpi_version) == -1 || !ioc)
2546f92363d1SSreekanth Reddy 		return -ENODEV;
2547f92363d1SSreekanth Reddy 
254808c4d550SSreekanth Reddy 	/* pci_access_mutex lock acquired by ioctl path */
254908c4d550SSreekanth Reddy 	mutex_lock(&ioc->pci_access_mutex);
255008c4d550SSreekanth Reddy 
2551f92363d1SSreekanth Reddy 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
255208c4d550SSreekanth Reddy 	    ioc->is_driver_loading || ioc->remove_host) {
255308c4d550SSreekanth Reddy 		ret = -EAGAIN;
255408c4d550SSreekanth Reddy 		goto out_unlock_pciaccess;
255508c4d550SSreekanth Reddy 	}
2556f92363d1SSreekanth Reddy 
2557f92363d1SSreekanth Reddy 	state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2558f92363d1SSreekanth Reddy 	if (state == NON_BLOCKING) {
255908c4d550SSreekanth Reddy 		if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
256008c4d550SSreekanth Reddy 			ret = -EAGAIN;
256108c4d550SSreekanth Reddy 			goto out_unlock_pciaccess;
256208c4d550SSreekanth Reddy 		}
256308c4d550SSreekanth Reddy 	} else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
256408c4d550SSreekanth Reddy 		ret = -ERESTARTSYS;
256508c4d550SSreekanth Reddy 		goto out_unlock_pciaccess;
256608c4d550SSreekanth Reddy 	}
2567f92363d1SSreekanth Reddy 
2568f92363d1SSreekanth Reddy 
2569f92363d1SSreekanth Reddy 	switch (cmd) {
2570f92363d1SSreekanth Reddy 	case MPT3IOCINFO:
2571f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2572f92363d1SSreekanth Reddy 			ret = _ctl_getiocinfo(ioc, arg);
2573f92363d1SSreekanth Reddy 		break;
2574f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2575f92363d1SSreekanth Reddy 	case MPT3COMMAND32:
2576f92363d1SSreekanth Reddy #endif
2577f92363d1SSreekanth Reddy 	case MPT3COMMAND:
2578f92363d1SSreekanth Reddy 	{
2579f92363d1SSreekanth Reddy 		struct mpt3_ioctl_command __user *uarg;
2580f92363d1SSreekanth Reddy 		struct mpt3_ioctl_command karg;
2581f92363d1SSreekanth Reddy 
2582f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2583f92363d1SSreekanth Reddy 		if (compat) {
2584f92363d1SSreekanth Reddy 			ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2585f92363d1SSreekanth Reddy 			break;
2586f92363d1SSreekanth Reddy 		}
2587f92363d1SSreekanth Reddy #endif
2588f92363d1SSreekanth Reddy 		if (copy_from_user(&karg, arg, sizeof(karg))) {
2589f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n",
2590f92363d1SSreekanth Reddy 			    __FILE__, __LINE__, __func__);
2591f92363d1SSreekanth Reddy 			ret = -EFAULT;
2592f92363d1SSreekanth Reddy 			break;
2593f92363d1SSreekanth Reddy 		}
2594f92363d1SSreekanth Reddy 
2595f9e3ebeeSGen Zhang 		if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2596f9e3ebeeSGen Zhang 			ret = -EINVAL;
2597f9e3ebeeSGen Zhang 			break;
2598f9e3ebeeSGen Zhang 		}
2599f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2600f92363d1SSreekanth Reddy 			uarg = arg;
2601f92363d1SSreekanth Reddy 			ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2602f92363d1SSreekanth Reddy 		}
2603f92363d1SSreekanth Reddy 		break;
2604f92363d1SSreekanth Reddy 	}
2605f92363d1SSreekanth Reddy 	case MPT3EVENTQUERY:
2606f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2607f92363d1SSreekanth Reddy 			ret = _ctl_eventquery(ioc, arg);
2608f92363d1SSreekanth Reddy 		break;
2609f92363d1SSreekanth Reddy 	case MPT3EVENTENABLE:
2610f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2611f92363d1SSreekanth Reddy 			ret = _ctl_eventenable(ioc, arg);
2612f92363d1SSreekanth Reddy 		break;
2613f92363d1SSreekanth Reddy 	case MPT3EVENTREPORT:
2614f92363d1SSreekanth Reddy 		ret = _ctl_eventreport(ioc, arg);
2615f92363d1SSreekanth Reddy 		break;
2616f92363d1SSreekanth Reddy 	case MPT3HARDRESET:
2617f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2618f92363d1SSreekanth Reddy 			ret = _ctl_do_reset(ioc, arg);
2619f92363d1SSreekanth Reddy 		break;
2620f92363d1SSreekanth Reddy 	case MPT3BTDHMAPPING:
2621f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2622f92363d1SSreekanth Reddy 			ret = _ctl_btdh_mapping(ioc, arg);
2623f92363d1SSreekanth Reddy 		break;
2624f92363d1SSreekanth Reddy 	case MPT3DIAGREGISTER:
2625f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2626f92363d1SSreekanth Reddy 			ret = _ctl_diag_register(ioc, arg);
2627f92363d1SSreekanth Reddy 		break;
2628f92363d1SSreekanth Reddy 	case MPT3DIAGUNREGISTER:
2629f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2630f92363d1SSreekanth Reddy 			ret = _ctl_diag_unregister(ioc, arg);
2631f92363d1SSreekanth Reddy 		break;
2632f92363d1SSreekanth Reddy 	case MPT3DIAGQUERY:
2633f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2634f92363d1SSreekanth Reddy 			ret = _ctl_diag_query(ioc, arg);
2635f92363d1SSreekanth Reddy 		break;
2636f92363d1SSreekanth Reddy 	case MPT3DIAGRELEASE:
2637f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2638f92363d1SSreekanth Reddy 			ret = _ctl_diag_release(ioc, arg);
2639f92363d1SSreekanth Reddy 		break;
2640f92363d1SSreekanth Reddy 	case MPT3DIAGREADBUFFER:
2641f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2642f92363d1SSreekanth Reddy 			ret = _ctl_diag_read_buffer(ioc, arg);
2643f92363d1SSreekanth Reddy 		break;
2644f92363d1SSreekanth Reddy 	default:
2645919d8a3fSJoe Perches 		dctlprintk(ioc,
2646919d8a3fSJoe Perches 			   ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2647919d8a3fSJoe Perches 				    cmd));
2648f92363d1SSreekanth Reddy 		break;
2649f92363d1SSreekanth Reddy 	}
2650f92363d1SSreekanth Reddy 
2651f92363d1SSreekanth Reddy 	mutex_unlock(&ioc->ctl_cmds.mutex);
265208c4d550SSreekanth Reddy out_unlock_pciaccess:
265308c4d550SSreekanth Reddy 	mutex_unlock(&ioc->pci_access_mutex);
2654f92363d1SSreekanth Reddy 	return ret;
2655f92363d1SSreekanth Reddy }
2656f92363d1SSreekanth Reddy 
2657f92363d1SSreekanth Reddy /**
2658c84b06a4SSreekanth Reddy  * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
26594beb4867SBart Van Assche  * @file: (struct file)
26604beb4867SBart Van Assche  * @cmd: ioctl opcode
26614beb4867SBart Van Assche  * @arg: ?
2662f92363d1SSreekanth Reddy  */
26638bbb1cf6SCalvin Owens static long
2664c84b06a4SSreekanth Reddy _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2665f92363d1SSreekanth Reddy {
2666f92363d1SSreekanth Reddy 	long ret;
2667f92363d1SSreekanth Reddy 
2668b130b0d5SSuganath prabu Subramani 	/* pass MPI25_VERSION | MPI26_VERSION value,
2669b130b0d5SSuganath prabu Subramani 	 * to indicate that this ioctl cmd
2670c84b06a4SSreekanth Reddy 	 * came from mpt3ctl ioctl device.
2671c84b06a4SSreekanth Reddy 	 */
2672b130b0d5SSuganath prabu Subramani 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2673b130b0d5SSuganath prabu Subramani 		MPI25_VERSION | MPI26_VERSION);
2674f92363d1SSreekanth Reddy 	return ret;
2675f92363d1SSreekanth Reddy }
2676f92363d1SSreekanth Reddy 
2677c84b06a4SSreekanth Reddy /**
2678c84b06a4SSreekanth Reddy  * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
26794beb4867SBart Van Assche  * @file: (struct file)
26804beb4867SBart Van Assche  * @cmd: ioctl opcode
26814beb4867SBart Van Assche  * @arg: ?
2682c84b06a4SSreekanth Reddy  */
26838bbb1cf6SCalvin Owens static long
2684c84b06a4SSreekanth Reddy _ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2685c84b06a4SSreekanth Reddy {
2686c84b06a4SSreekanth Reddy 	long ret;
2687c84b06a4SSreekanth Reddy 
2688c84b06a4SSreekanth Reddy 	/* pass MPI2_VERSION value, to indicate that this ioctl cmd
2689c84b06a4SSreekanth Reddy 	 * came from mpt2ctl ioctl device.
2690c84b06a4SSreekanth Reddy 	 */
2691c84b06a4SSreekanth Reddy 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2692c84b06a4SSreekanth Reddy 	return ret;
2693c84b06a4SSreekanth Reddy }
2694f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2695f92363d1SSreekanth Reddy /**
2696c84b06a4SSreekanth Reddy  *_ ctl_ioctl_compat - main ioctl entry point (compat)
26974beb4867SBart Van Assche  * @file: ?
26984beb4867SBart Van Assche  * @cmd: ?
26994beb4867SBart Van Assche  * @arg: ?
2700f92363d1SSreekanth Reddy  *
2701f92363d1SSreekanth Reddy  * This routine handles 32 bit applications in 64bit os.
2702f92363d1SSreekanth Reddy  */
27038bbb1cf6SCalvin Owens static long
2704c84b06a4SSreekanth Reddy _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2705f92363d1SSreekanth Reddy {
2706f92363d1SSreekanth Reddy 	long ret;
2707f92363d1SSreekanth Reddy 
2708b130b0d5SSuganath prabu Subramani 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2709b130b0d5SSuganath prabu Subramani 		MPI25_VERSION | MPI26_VERSION);
2710c84b06a4SSreekanth Reddy 	return ret;
2711c84b06a4SSreekanth Reddy }
2712c84b06a4SSreekanth Reddy 
2713c84b06a4SSreekanth Reddy /**
2714c84b06a4SSreekanth Reddy  *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
27154beb4867SBart Van Assche  * @file: ?
27164beb4867SBart Van Assche  * @cmd: ?
27174beb4867SBart Van Assche  * @arg: ?
2718c84b06a4SSreekanth Reddy  *
2719c84b06a4SSreekanth Reddy  * This routine handles 32 bit applications in 64bit os.
2720c84b06a4SSreekanth Reddy  */
27218bbb1cf6SCalvin Owens static long
2722c84b06a4SSreekanth Reddy _ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2723c84b06a4SSreekanth Reddy {
2724c84b06a4SSreekanth Reddy 	long ret;
2725c84b06a4SSreekanth Reddy 
2726c84b06a4SSreekanth Reddy 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
2727f92363d1SSreekanth Reddy 	return ret;
2728f92363d1SSreekanth Reddy }
2729f92363d1SSreekanth Reddy #endif
2730f92363d1SSreekanth Reddy 
2731f92363d1SSreekanth Reddy /* scsi host attributes */
2732f92363d1SSreekanth Reddy /**
2733c9df1442STomas Henzl  * version_fw_show - firmware version
27344beb4867SBart Van Assche  * @cdev: pointer to embedded class device
27354beb4867SBart Van Assche  * @attr: ?
27364beb4867SBart Van Assche  * @buf: the buffer returned
2737f92363d1SSreekanth Reddy  *
2738f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2739f92363d1SSreekanth Reddy  */
2740f92363d1SSreekanth Reddy static ssize_t
2741c9df1442STomas Henzl version_fw_show(struct device *cdev, struct device_attribute *attr,
2742f92363d1SSreekanth Reddy 	char *buf)
2743f92363d1SSreekanth Reddy {
2744f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2745f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2746f92363d1SSreekanth Reddy 
2747f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2748f92363d1SSreekanth Reddy 	    (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2749f92363d1SSreekanth Reddy 	    (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2750f92363d1SSreekanth Reddy 	    (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2751f92363d1SSreekanth Reddy 	    ioc->facts.FWVersion.Word & 0x000000FF);
2752f92363d1SSreekanth Reddy }
2753c9df1442STomas Henzl static DEVICE_ATTR_RO(version_fw);
2754f92363d1SSreekanth Reddy 
2755f92363d1SSreekanth Reddy /**
2756c9df1442STomas Henzl  * version_bios_show - bios version
27574beb4867SBart Van Assche  * @cdev: pointer to embedded class device
27584beb4867SBart Van Assche  * @attr: ?
27594beb4867SBart Van Assche  * @buf: the buffer returned
2760f92363d1SSreekanth Reddy  *
2761f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2762f92363d1SSreekanth Reddy  */
2763f92363d1SSreekanth Reddy static ssize_t
2764c9df1442STomas Henzl version_bios_show(struct device *cdev, struct device_attribute *attr,
2765f92363d1SSreekanth Reddy 	char *buf)
2766f92363d1SSreekanth Reddy {
2767f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2768f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2769f92363d1SSreekanth Reddy 
2770f92363d1SSreekanth Reddy 	u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2771f92363d1SSreekanth Reddy 
2772f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2773f92363d1SSreekanth Reddy 	    (version & 0xFF000000) >> 24,
2774f92363d1SSreekanth Reddy 	    (version & 0x00FF0000) >> 16,
2775f92363d1SSreekanth Reddy 	    (version & 0x0000FF00) >> 8,
2776f92363d1SSreekanth Reddy 	    version & 0x000000FF);
2777f92363d1SSreekanth Reddy }
2778c9df1442STomas Henzl static DEVICE_ATTR_RO(version_bios);
2779f92363d1SSreekanth Reddy 
2780f92363d1SSreekanth Reddy /**
2781c9df1442STomas Henzl  * version_mpi_show - MPI (message passing interface) version
27824beb4867SBart Van Assche  * @cdev: pointer to embedded class device
27834beb4867SBart Van Assche  * @attr: ?
27844beb4867SBart Van Assche  * @buf: the buffer returned
2785f92363d1SSreekanth Reddy  *
2786f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2787f92363d1SSreekanth Reddy  */
2788f92363d1SSreekanth Reddy static ssize_t
2789c9df1442STomas Henzl version_mpi_show(struct device *cdev, struct device_attribute *attr,
2790f92363d1SSreekanth Reddy 	char *buf)
2791f92363d1SSreekanth Reddy {
2792f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2793f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2794f92363d1SSreekanth Reddy 
2795f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2796f92363d1SSreekanth Reddy 	    ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2797f92363d1SSreekanth Reddy }
2798c9df1442STomas Henzl static DEVICE_ATTR_RO(version_mpi);
2799f92363d1SSreekanth Reddy 
2800f92363d1SSreekanth Reddy /**
2801c9df1442STomas Henzl  * version_product_show - product name
28024beb4867SBart Van Assche  * @cdev: pointer to embedded class device
28034beb4867SBart Van Assche  * @attr: ?
28044beb4867SBart Van Assche  * @buf: the buffer returned
2805f92363d1SSreekanth Reddy  *
2806f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2807f92363d1SSreekanth Reddy  */
2808f92363d1SSreekanth Reddy static ssize_t
2809c9df1442STomas Henzl version_product_show(struct device *cdev, struct device_attribute *attr,
2810f92363d1SSreekanth Reddy 	char *buf)
2811f92363d1SSreekanth Reddy {
2812f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2813f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2814f92363d1SSreekanth Reddy 
2815f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2816f92363d1SSreekanth Reddy }
2817c9df1442STomas Henzl static DEVICE_ATTR_RO(version_product);
2818f92363d1SSreekanth Reddy 
2819f92363d1SSreekanth Reddy /**
2820c9df1442STomas Henzl  * version_nvdata_persistent_show - ndvata persistent version
28214beb4867SBart Van Assche  * @cdev: pointer to embedded class device
28224beb4867SBart Van Assche  * @attr: ?
28234beb4867SBart Van Assche  * @buf: the buffer returned
2824f92363d1SSreekanth Reddy  *
2825f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2826f92363d1SSreekanth Reddy  */
2827f92363d1SSreekanth Reddy static ssize_t
2828c9df1442STomas Henzl version_nvdata_persistent_show(struct device *cdev,
2829f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
2830f92363d1SSreekanth Reddy {
2831f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2832f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2833f92363d1SSreekanth Reddy 
2834f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2835f92363d1SSreekanth Reddy 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2836f92363d1SSreekanth Reddy }
2837c9df1442STomas Henzl static DEVICE_ATTR_RO(version_nvdata_persistent);
2838f92363d1SSreekanth Reddy 
2839f92363d1SSreekanth Reddy /**
2840c9df1442STomas Henzl  * version_nvdata_default_show - nvdata default version
28414beb4867SBart Van Assche  * @cdev: pointer to embedded class device
28424beb4867SBart Van Assche  * @attr: ?
28434beb4867SBart Van Assche  * @buf: the buffer returned
2844f92363d1SSreekanth Reddy  *
2845f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2846f92363d1SSreekanth Reddy  */
2847f92363d1SSreekanth Reddy static ssize_t
2848c9df1442STomas Henzl version_nvdata_default_show(struct device *cdev, struct device_attribute
2849f92363d1SSreekanth Reddy 	*attr, char *buf)
2850f92363d1SSreekanth Reddy {
2851f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2852f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2853f92363d1SSreekanth Reddy 
2854f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2855f92363d1SSreekanth Reddy 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2856f92363d1SSreekanth Reddy }
2857c9df1442STomas Henzl static DEVICE_ATTR_RO(version_nvdata_default);
2858f92363d1SSreekanth Reddy 
2859f92363d1SSreekanth Reddy /**
2860c9df1442STomas Henzl  * board_name_show - board name
28614beb4867SBart Van Assche  * @cdev: pointer to embedded class device
28624beb4867SBart Van Assche  * @attr: ?
28634beb4867SBart Van Assche  * @buf: the buffer returned
2864f92363d1SSreekanth Reddy  *
2865f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2866f92363d1SSreekanth Reddy  */
2867f92363d1SSreekanth Reddy static ssize_t
2868c9df1442STomas Henzl board_name_show(struct device *cdev, struct device_attribute *attr,
2869f92363d1SSreekanth Reddy 	char *buf)
2870f92363d1SSreekanth Reddy {
2871f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2872f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2873f92363d1SSreekanth Reddy 
2874f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2875f92363d1SSreekanth Reddy }
2876c9df1442STomas Henzl static DEVICE_ATTR_RO(board_name);
2877f92363d1SSreekanth Reddy 
2878f92363d1SSreekanth Reddy /**
2879c9df1442STomas Henzl  * board_assembly_show - board assembly name
28804beb4867SBart Van Assche  * @cdev: pointer to embedded class device
28814beb4867SBart Van Assche  * @attr: ?
28824beb4867SBart Van Assche  * @buf: the buffer returned
2883f92363d1SSreekanth Reddy  *
2884f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2885f92363d1SSreekanth Reddy  */
2886f92363d1SSreekanth Reddy static ssize_t
2887c9df1442STomas Henzl board_assembly_show(struct device *cdev, struct device_attribute *attr,
2888f92363d1SSreekanth Reddy 	char *buf)
2889f92363d1SSreekanth Reddy {
2890f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2891f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2892f92363d1SSreekanth Reddy 
2893f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2894f92363d1SSreekanth Reddy }
2895c9df1442STomas Henzl static DEVICE_ATTR_RO(board_assembly);
2896f92363d1SSreekanth Reddy 
2897f92363d1SSreekanth Reddy /**
2898c9df1442STomas Henzl  * board_tracer_show - board tracer number
28994beb4867SBart Van Assche  * @cdev: pointer to embedded class device
29004beb4867SBart Van Assche  * @attr: ?
29014beb4867SBart Van Assche  * @buf: the buffer returned
2902f92363d1SSreekanth Reddy  *
2903f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2904f92363d1SSreekanth Reddy  */
2905f92363d1SSreekanth Reddy static ssize_t
2906c9df1442STomas Henzl board_tracer_show(struct device *cdev, struct device_attribute *attr,
2907f92363d1SSreekanth Reddy 	char *buf)
2908f92363d1SSreekanth Reddy {
2909f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2910f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2911f92363d1SSreekanth Reddy 
2912f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2913f92363d1SSreekanth Reddy }
2914c9df1442STomas Henzl static DEVICE_ATTR_RO(board_tracer);
2915f92363d1SSreekanth Reddy 
2916f92363d1SSreekanth Reddy /**
2917c9df1442STomas Henzl  * io_delay_show - io missing delay
29184beb4867SBart Van Assche  * @cdev: pointer to embedded class device
29194beb4867SBart Van Assche  * @attr: ?
29204beb4867SBart Van Assche  * @buf: the buffer returned
2921f92363d1SSreekanth Reddy  *
2922f92363d1SSreekanth Reddy  * This is for firmware implemention for deboucing device
2923f92363d1SSreekanth Reddy  * removal events.
2924f92363d1SSreekanth Reddy  *
2925f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2926f92363d1SSreekanth Reddy  */
2927f92363d1SSreekanth Reddy static ssize_t
2928c9df1442STomas Henzl io_delay_show(struct device *cdev, struct device_attribute *attr,
2929f92363d1SSreekanth Reddy 	char *buf)
2930f92363d1SSreekanth Reddy {
2931f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2932f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2933f92363d1SSreekanth Reddy 
2934f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2935f92363d1SSreekanth Reddy }
2936c9df1442STomas Henzl static DEVICE_ATTR_RO(io_delay);
2937f92363d1SSreekanth Reddy 
2938f92363d1SSreekanth Reddy /**
2939c9df1442STomas Henzl  * device_delay_show - device missing delay
29404beb4867SBart Van Assche  * @cdev: pointer to embedded class device
29414beb4867SBart Van Assche  * @attr: ?
29424beb4867SBart Van Assche  * @buf: the buffer returned
2943f92363d1SSreekanth Reddy  *
2944f92363d1SSreekanth Reddy  * This is for firmware implemention for deboucing device
2945f92363d1SSreekanth Reddy  * removal events.
2946f92363d1SSreekanth Reddy  *
2947f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2948f92363d1SSreekanth Reddy  */
2949f92363d1SSreekanth Reddy static ssize_t
2950c9df1442STomas Henzl device_delay_show(struct device *cdev, struct device_attribute *attr,
2951f92363d1SSreekanth Reddy 	char *buf)
2952f92363d1SSreekanth Reddy {
2953f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2954f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2955f92363d1SSreekanth Reddy 
2956f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2957f92363d1SSreekanth Reddy }
2958c9df1442STomas Henzl static DEVICE_ATTR_RO(device_delay);
2959f92363d1SSreekanth Reddy 
2960f92363d1SSreekanth Reddy /**
2961c9df1442STomas Henzl  * fw_queue_depth_show - global credits
29624beb4867SBart Van Assche  * @cdev: pointer to embedded class device
29634beb4867SBart Van Assche  * @attr: ?
29644beb4867SBart Van Assche  * @buf: the buffer returned
2965f92363d1SSreekanth Reddy  *
2966f92363d1SSreekanth Reddy  * This is firmware queue depth limit
2967f92363d1SSreekanth Reddy  *
2968f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2969f92363d1SSreekanth Reddy  */
2970f92363d1SSreekanth Reddy static ssize_t
2971c9df1442STomas Henzl fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2972f92363d1SSreekanth Reddy 	char *buf)
2973f92363d1SSreekanth Reddy {
2974f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2975f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2976f92363d1SSreekanth Reddy 
2977f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2978f92363d1SSreekanth Reddy }
2979c9df1442STomas Henzl static DEVICE_ATTR_RO(fw_queue_depth);
2980f92363d1SSreekanth Reddy 
2981f92363d1SSreekanth Reddy /**
2982c9df1442STomas Henzl  * sas_address_show - sas address
29834beb4867SBart Van Assche  * @cdev: pointer to embedded class device
29844beb4867SBart Van Assche  * @attr: ?
29854beb4867SBart Van Assche  * @buf: the buffer returned
2986f92363d1SSreekanth Reddy  *
2987f92363d1SSreekanth Reddy  * This is the controller sas address
2988f92363d1SSreekanth Reddy  *
2989f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2990f92363d1SSreekanth Reddy  */
2991f92363d1SSreekanth Reddy static ssize_t
2992c9df1442STomas Henzl host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2993f92363d1SSreekanth Reddy 	char *buf)
2994f92363d1SSreekanth Reddy 
2995f92363d1SSreekanth Reddy {
2996f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2997f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2998f92363d1SSreekanth Reddy 
2999f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3000f92363d1SSreekanth Reddy 	    (unsigned long long)ioc->sas_hba.sas_address);
3001f92363d1SSreekanth Reddy }
3002c9df1442STomas Henzl static DEVICE_ATTR_RO(host_sas_address);
3003f92363d1SSreekanth Reddy 
3004f92363d1SSreekanth Reddy /**
3005c9df1442STomas Henzl  * logging_level_show - logging level
30064beb4867SBart Van Assche  * @cdev: pointer to embedded class device
30074beb4867SBart Van Assche  * @attr: ?
30084beb4867SBart Van Assche  * @buf: the buffer returned
3009f92363d1SSreekanth Reddy  *
3010f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3011f92363d1SSreekanth Reddy  */
3012f92363d1SSreekanth Reddy static ssize_t
3013c9df1442STomas Henzl logging_level_show(struct device *cdev, struct device_attribute *attr,
3014f92363d1SSreekanth Reddy 	char *buf)
3015f92363d1SSreekanth Reddy {
3016f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3017f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3018f92363d1SSreekanth Reddy 
3019f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
3020f92363d1SSreekanth Reddy }
3021f92363d1SSreekanth Reddy static ssize_t
3022c9df1442STomas Henzl logging_level_store(struct device *cdev, struct device_attribute *attr,
3023f92363d1SSreekanth Reddy 	const char *buf, size_t count)
3024f92363d1SSreekanth Reddy {
3025f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3026f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3027f92363d1SSreekanth Reddy 	int val = 0;
3028f92363d1SSreekanth Reddy 
3029f92363d1SSreekanth Reddy 	if (sscanf(buf, "%x", &val) != 1)
3030f92363d1SSreekanth Reddy 		return -EINVAL;
3031f92363d1SSreekanth Reddy 
3032f92363d1SSreekanth Reddy 	ioc->logging_level = val;
3033919d8a3fSJoe Perches 	ioc_info(ioc, "logging_level=%08xh\n",
3034f92363d1SSreekanth Reddy 		 ioc->logging_level);
3035f92363d1SSreekanth Reddy 	return strlen(buf);
3036f92363d1SSreekanth Reddy }
3037c9df1442STomas Henzl static DEVICE_ATTR_RW(logging_level);
3038f92363d1SSreekanth Reddy 
3039f92363d1SSreekanth Reddy /**
3040c9df1442STomas Henzl  * fwfault_debug_show - show/store fwfault_debug
30414beb4867SBart Van Assche  * @cdev: pointer to embedded class device
30424beb4867SBart Van Assche  * @attr: ?
30434beb4867SBart Van Assche  * @buf: the buffer returned
3044f92363d1SSreekanth Reddy  *
3045f92363d1SSreekanth Reddy  * mpt3sas_fwfault_debug is command line option
3046f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3047f92363d1SSreekanth Reddy  */
3048f92363d1SSreekanth Reddy static ssize_t
3049c9df1442STomas Henzl fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
3050f92363d1SSreekanth Reddy 	char *buf)
3051f92363d1SSreekanth Reddy {
3052f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3053f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3054f92363d1SSreekanth Reddy 
3055f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
3056f92363d1SSreekanth Reddy }
3057f92363d1SSreekanth Reddy static ssize_t
3058c9df1442STomas Henzl fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
3059f92363d1SSreekanth Reddy 	const char *buf, size_t count)
3060f92363d1SSreekanth Reddy {
3061f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3062f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3063f92363d1SSreekanth Reddy 	int val = 0;
3064f92363d1SSreekanth Reddy 
3065f92363d1SSreekanth Reddy 	if (sscanf(buf, "%d", &val) != 1)
3066f92363d1SSreekanth Reddy 		return -EINVAL;
3067f92363d1SSreekanth Reddy 
3068f92363d1SSreekanth Reddy 	ioc->fwfault_debug = val;
3069919d8a3fSJoe Perches 	ioc_info(ioc, "fwfault_debug=%d\n",
3070f92363d1SSreekanth Reddy 		 ioc->fwfault_debug);
3071f92363d1SSreekanth Reddy 	return strlen(buf);
3072f92363d1SSreekanth Reddy }
3073c9df1442STomas Henzl static DEVICE_ATTR_RW(fwfault_debug);
3074f92363d1SSreekanth Reddy 
3075f92363d1SSreekanth Reddy /**
3076c9df1442STomas Henzl  * ioc_reset_count_show - ioc reset count
30774beb4867SBart Van Assche  * @cdev: pointer to embedded class device
30784beb4867SBart Van Assche  * @attr: ?
30794beb4867SBart Van Assche  * @buf: the buffer returned
3080f92363d1SSreekanth Reddy  *
3081f92363d1SSreekanth Reddy  * This is firmware queue depth limit
3082f92363d1SSreekanth Reddy  *
3083f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3084f92363d1SSreekanth Reddy  */
3085f92363d1SSreekanth Reddy static ssize_t
3086c9df1442STomas Henzl ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
3087f92363d1SSreekanth Reddy 	char *buf)
3088f92363d1SSreekanth Reddy {
3089f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3090f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3091f92363d1SSreekanth Reddy 
3092f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
3093f92363d1SSreekanth Reddy }
3094c9df1442STomas Henzl static DEVICE_ATTR_RO(ioc_reset_count);
3095f92363d1SSreekanth Reddy 
3096f92363d1SSreekanth Reddy /**
3097c9df1442STomas Henzl  * reply_queue_count_show - number of reply queues
30984beb4867SBart Van Assche  * @cdev: pointer to embedded class device
30994beb4867SBart Van Assche  * @attr: ?
31004beb4867SBart Van Assche  * @buf: the buffer returned
3101f92363d1SSreekanth Reddy  *
3102f92363d1SSreekanth Reddy  * This is number of reply queues
3103f92363d1SSreekanth Reddy  *
3104f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3105f92363d1SSreekanth Reddy  */
3106f92363d1SSreekanth Reddy static ssize_t
3107c9df1442STomas Henzl reply_queue_count_show(struct device *cdev,
3108f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3109f92363d1SSreekanth Reddy {
3110f92363d1SSreekanth Reddy 	u8 reply_queue_count;
3111f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3112f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3113f92363d1SSreekanth Reddy 
3114f92363d1SSreekanth Reddy 	if ((ioc->facts.IOCCapabilities &
3115f92363d1SSreekanth Reddy 	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
3116f92363d1SSreekanth Reddy 		reply_queue_count = ioc->reply_queue_count;
3117f92363d1SSreekanth Reddy 	else
3118f92363d1SSreekanth Reddy 		reply_queue_count = 1;
3119f92363d1SSreekanth Reddy 
3120f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
3121f92363d1SSreekanth Reddy }
3122c9df1442STomas Henzl static DEVICE_ATTR_RO(reply_queue_count);
3123f92363d1SSreekanth Reddy 
312442263095SSreekanth Reddy /**
3125c9df1442STomas Henzl  * BRM_status_show - Backup Rail Monitor Status
31264beb4867SBart Van Assche  * @cdev: pointer to embedded class device
31274beb4867SBart Van Assche  * @attr: ?
31284beb4867SBart Van Assche  * @buf: the buffer returned
312942263095SSreekanth Reddy  *
313042263095SSreekanth Reddy  * This is number of reply queues
313142263095SSreekanth Reddy  *
313242263095SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
313342263095SSreekanth Reddy  */
313442263095SSreekanth Reddy static ssize_t
3135c9df1442STomas Henzl BRM_status_show(struct device *cdev, struct device_attribute *attr,
313642263095SSreekanth Reddy 	char *buf)
313742263095SSreekanth Reddy {
313842263095SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
313942263095SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
314042263095SSreekanth Reddy 	Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
314142263095SSreekanth Reddy 	Mpi2ConfigReply_t mpi_reply;
314242263095SSreekanth Reddy 	u16 backup_rail_monitor_status = 0;
314342263095SSreekanth Reddy 	u16 ioc_status;
314442263095SSreekanth Reddy 	int sz;
314542263095SSreekanth Reddy 	ssize_t rc = 0;
314642263095SSreekanth Reddy 
314742263095SSreekanth Reddy 	if (!ioc->is_warpdrive) {
3148919d8a3fSJoe Perches 		ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
3149919d8a3fSJoe Perches 			__func__);
3150cb551b8dSDamien Le Moal 		return 0;
315142263095SSreekanth Reddy 	}
315208c4d550SSreekanth Reddy 	/* pci_access_mutex lock acquired by sysfs show path */
315308c4d550SSreekanth Reddy 	mutex_lock(&ioc->pci_access_mutex);
31540fd18145SJohannes Thumshirn 	if (ioc->pci_error_recovery || ioc->remove_host)
31550fd18145SJohannes Thumshirn 		goto out;
315642263095SSreekanth Reddy 
315742263095SSreekanth Reddy 	/* allocate upto GPIOVal 36 entries */
315842263095SSreekanth Reddy 	sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
315942263095SSreekanth Reddy 	io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
316042263095SSreekanth Reddy 	if (!io_unit_pg3) {
31610fd18145SJohannes Thumshirn 		rc = -ENOMEM;
3162919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed allocating memory for iounit_pg3: (%d) bytes\n",
3163919d8a3fSJoe Perches 			__func__, sz);
316442263095SSreekanth Reddy 		goto out;
316542263095SSreekanth Reddy 	}
316642263095SSreekanth Reddy 
316742263095SSreekanth Reddy 	if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
316842263095SSreekanth Reddy 	    0) {
3169919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed reading iounit_pg3\n",
317042263095SSreekanth Reddy 			__func__);
31710fd18145SJohannes Thumshirn 		rc = -EINVAL;
317242263095SSreekanth Reddy 		goto out;
317342263095SSreekanth Reddy 	}
317442263095SSreekanth Reddy 
317542263095SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
317642263095SSreekanth Reddy 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3177919d8a3fSJoe Perches 		ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
3178919d8a3fSJoe Perches 			__func__, ioc_status);
31790fd18145SJohannes Thumshirn 		rc = -EINVAL;
318042263095SSreekanth Reddy 		goto out;
318142263095SSreekanth Reddy 	}
318242263095SSreekanth Reddy 
318342263095SSreekanth Reddy 	if (io_unit_pg3->GPIOCount < 25) {
3184919d8a3fSJoe Perches 		ioc_err(ioc, "%s: iounit_pg3->GPIOCount less than 25 entries, detected (%d) entries\n",
3185919d8a3fSJoe Perches 			__func__, io_unit_pg3->GPIOCount);
31860fd18145SJohannes Thumshirn 		rc = -EINVAL;
318742263095SSreekanth Reddy 		goto out;
318842263095SSreekanth Reddy 	}
318942263095SSreekanth Reddy 
319042263095SSreekanth Reddy 	/* BRM status is in bit zero of GPIOVal[24] */
319142263095SSreekanth Reddy 	backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
319242263095SSreekanth Reddy 	rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
319342263095SSreekanth Reddy 
319442263095SSreekanth Reddy  out:
319542263095SSreekanth Reddy 	kfree(io_unit_pg3);
319608c4d550SSreekanth Reddy 	mutex_unlock(&ioc->pci_access_mutex);
319742263095SSreekanth Reddy 	return rc;
319842263095SSreekanth Reddy }
3199c9df1442STomas Henzl static DEVICE_ATTR_RO(BRM_status);
320042263095SSreekanth Reddy 
3201f92363d1SSreekanth Reddy struct DIAG_BUFFER_START {
3202f92363d1SSreekanth Reddy 	__le32	Size;
3203f92363d1SSreekanth Reddy 	__le32	DiagVersion;
3204f92363d1SSreekanth Reddy 	u8	BufferType;
3205f92363d1SSreekanth Reddy 	u8	Reserved[3];
3206f92363d1SSreekanth Reddy 	__le32	Reserved1;
3207f92363d1SSreekanth Reddy 	__le32	Reserved2;
3208f92363d1SSreekanth Reddy 	__le32	Reserved3;
3209f92363d1SSreekanth Reddy };
3210f92363d1SSreekanth Reddy 
3211f92363d1SSreekanth Reddy /**
3212c9df1442STomas Henzl  * host_trace_buffer_size_show - host buffer size (trace only)
32134beb4867SBart Van Assche  * @cdev: pointer to embedded class device
32144beb4867SBart Van Assche  * @attr: ?
32154beb4867SBart Van Assche  * @buf: the buffer returned
3216f92363d1SSreekanth Reddy  *
3217f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3218f92363d1SSreekanth Reddy  */
3219f92363d1SSreekanth Reddy static ssize_t
3220c9df1442STomas Henzl host_trace_buffer_size_show(struct device *cdev,
3221f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3222f92363d1SSreekanth Reddy {
3223f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3224f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3225f92363d1SSreekanth Reddy 	u32 size = 0;
3226f92363d1SSreekanth Reddy 	struct DIAG_BUFFER_START *request_data;
3227f92363d1SSreekanth Reddy 
3228f92363d1SSreekanth Reddy 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3229919d8a3fSJoe Perches 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3230919d8a3fSJoe Perches 			__func__);
3231f92363d1SSreekanth Reddy 		return 0;
3232f92363d1SSreekanth Reddy 	}
3233f92363d1SSreekanth Reddy 
3234f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3235f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3236919d8a3fSJoe Perches 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3237919d8a3fSJoe Perches 			__func__);
3238f92363d1SSreekanth Reddy 		return 0;
3239f92363d1SSreekanth Reddy 	}
3240f92363d1SSreekanth Reddy 
3241f92363d1SSreekanth Reddy 	request_data = (struct DIAG_BUFFER_START *)
3242f92363d1SSreekanth Reddy 	    ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
3243f92363d1SSreekanth Reddy 	if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
3244f92363d1SSreekanth Reddy 	    le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
3245f92363d1SSreekanth Reddy 	    le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
3246f92363d1SSreekanth Reddy 	    le32_to_cpu(request_data->Reserved3) == 0x4742444c)
3247f92363d1SSreekanth Reddy 		size = le32_to_cpu(request_data->Size);
3248f92363d1SSreekanth Reddy 
3249f92363d1SSreekanth Reddy 	ioc->ring_buffer_sz = size;
3250f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", size);
3251f92363d1SSreekanth Reddy }
3252c9df1442STomas Henzl static DEVICE_ATTR_RO(host_trace_buffer_size);
3253f92363d1SSreekanth Reddy 
3254f92363d1SSreekanth Reddy /**
3255c9df1442STomas Henzl  * host_trace_buffer_show - firmware ring buffer (trace only)
32564beb4867SBart Van Assche  * @cdev: pointer to embedded class device
32574beb4867SBart Van Assche  * @attr: ?
32584beb4867SBart Van Assche  * @buf: the buffer returned
3259f92363d1SSreekanth Reddy  *
3260f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3261f92363d1SSreekanth Reddy  *
3262f92363d1SSreekanth Reddy  * You will only be able to read 4k bytes of ring buffer at a time.
3263f92363d1SSreekanth Reddy  * In order to read beyond 4k bytes, you will have to write out the
3264f92363d1SSreekanth Reddy  * offset to the same attribute, it will move the pointer.
3265f92363d1SSreekanth Reddy  */
3266f92363d1SSreekanth Reddy static ssize_t
3267c9df1442STomas Henzl host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
3268f92363d1SSreekanth Reddy 	char *buf)
3269f92363d1SSreekanth Reddy {
3270f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3271f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3272f92363d1SSreekanth Reddy 	void *request_data;
3273f92363d1SSreekanth Reddy 	u32 size;
3274f92363d1SSreekanth Reddy 
3275f92363d1SSreekanth Reddy 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3276919d8a3fSJoe Perches 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3277919d8a3fSJoe Perches 			__func__);
3278f92363d1SSreekanth Reddy 		return 0;
3279f92363d1SSreekanth Reddy 	}
3280f92363d1SSreekanth Reddy 
3281f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3282f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3283919d8a3fSJoe Perches 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3284919d8a3fSJoe Perches 			__func__);
3285f92363d1SSreekanth Reddy 		return 0;
3286f92363d1SSreekanth Reddy 	}
3287f92363d1SSreekanth Reddy 
3288f92363d1SSreekanth Reddy 	if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3289f92363d1SSreekanth Reddy 		return 0;
3290f92363d1SSreekanth Reddy 
3291f92363d1SSreekanth Reddy 	size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3292f92363d1SSreekanth Reddy 	size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3293f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3294f92363d1SSreekanth Reddy 	memcpy(buf, request_data, size);
3295f92363d1SSreekanth Reddy 	return size;
3296f92363d1SSreekanth Reddy }
3297f92363d1SSreekanth Reddy 
3298f92363d1SSreekanth Reddy static ssize_t
3299c9df1442STomas Henzl host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
3300f92363d1SSreekanth Reddy 	const char *buf, size_t count)
3301f92363d1SSreekanth Reddy {
3302f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3303f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3304f92363d1SSreekanth Reddy 	int val = 0;
3305f92363d1SSreekanth Reddy 
3306f92363d1SSreekanth Reddy 	if (sscanf(buf, "%d", &val) != 1)
3307f92363d1SSreekanth Reddy 		return -EINVAL;
3308f92363d1SSreekanth Reddy 
3309f92363d1SSreekanth Reddy 	ioc->ring_buffer_offset = val;
3310f92363d1SSreekanth Reddy 	return strlen(buf);
3311f92363d1SSreekanth Reddy }
3312c9df1442STomas Henzl static DEVICE_ATTR_RW(host_trace_buffer);
3313f92363d1SSreekanth Reddy 
3314f92363d1SSreekanth Reddy 
3315f92363d1SSreekanth Reddy /*****************************************/
3316f92363d1SSreekanth Reddy 
3317f92363d1SSreekanth Reddy /**
3318c9df1442STomas Henzl  * host_trace_buffer_enable_show - firmware ring buffer (trace only)
33194beb4867SBart Van Assche  * @cdev: pointer to embedded class device
33204beb4867SBart Van Assche  * @attr: ?
33214beb4867SBart Van Assche  * @buf: the buffer returned
3322f92363d1SSreekanth Reddy  *
3323f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3324f92363d1SSreekanth Reddy  *
3325f92363d1SSreekanth Reddy  * This is a mechnism to post/release host_trace_buffers
3326f92363d1SSreekanth Reddy  */
3327f92363d1SSreekanth Reddy static ssize_t
3328c9df1442STomas Henzl host_trace_buffer_enable_show(struct device *cdev,
3329f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3330f92363d1SSreekanth Reddy {
3331f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3332f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3333f92363d1SSreekanth Reddy 
3334f92363d1SSreekanth Reddy 	if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3335f92363d1SSreekanth Reddy 	   ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3336f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3337f92363d1SSreekanth Reddy 		return snprintf(buf, PAGE_SIZE, "off\n");
3338f92363d1SSreekanth Reddy 	else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3339f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED))
3340f92363d1SSreekanth Reddy 		return snprintf(buf, PAGE_SIZE, "release\n");
3341f92363d1SSreekanth Reddy 	else
3342f92363d1SSreekanth Reddy 		return snprintf(buf, PAGE_SIZE, "post\n");
3343f92363d1SSreekanth Reddy }
3344f92363d1SSreekanth Reddy 
3345f92363d1SSreekanth Reddy static ssize_t
3346c9df1442STomas Henzl host_trace_buffer_enable_store(struct device *cdev,
3347f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3348f92363d1SSreekanth Reddy {
3349f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3350f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3351f92363d1SSreekanth Reddy 	char str[10] = "";
3352f92363d1SSreekanth Reddy 	struct mpt3_diag_register diag_register;
3353f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
3354f92363d1SSreekanth Reddy 
3355f92363d1SSreekanth Reddy 	/* don't allow post/release occurr while recovery is active */
3356f92363d1SSreekanth Reddy 	if (ioc->shost_recovery || ioc->remove_host ||
3357f92363d1SSreekanth Reddy 	    ioc->pci_error_recovery || ioc->is_driver_loading)
3358f92363d1SSreekanth Reddy 		return -EBUSY;
3359f92363d1SSreekanth Reddy 
3360f92363d1SSreekanth Reddy 	if (sscanf(buf, "%9s", str) != 1)
3361f92363d1SSreekanth Reddy 		return -EINVAL;
3362f92363d1SSreekanth Reddy 
3363f92363d1SSreekanth Reddy 	if (!strcmp(str, "post")) {
3364f92363d1SSreekanth Reddy 		/* exit out if host buffers are already posted */
3365f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3366f92363d1SSreekanth Reddy 		    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3367f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3368f92363d1SSreekanth Reddy 		    ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3369f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3370f92363d1SSreekanth Reddy 			goto out;
3371f92363d1SSreekanth Reddy 		memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3372919d8a3fSJoe Perches 		ioc_info(ioc, "posting host trace buffers\n");
3373f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3374d04a6edfSSreekanth Reddy 
3375d04a6edfSSreekanth Reddy 		if (ioc->manu_pg11.HostTraceBufferMaxSizeKB != 0 &&
3376d04a6edfSSreekanth Reddy 		    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0) {
3377d04a6edfSSreekanth Reddy 			/* post the same buffer allocated previously */
3378d04a6edfSSreekanth Reddy 			diag_register.requested_buffer_size =
3379d04a6edfSSreekanth Reddy 			    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE];
3380a8a6cbcdSSreekanth Reddy 		} else {
3381a8a6cbcdSSreekanth Reddy 			/*
3382a8a6cbcdSSreekanth Reddy 			 * Free the diag buffer memory which was previously
3383a8a6cbcdSSreekanth Reddy 			 * allocated by an application.
3384a8a6cbcdSSreekanth Reddy 			 */
3385a8a6cbcdSSreekanth Reddy 			if ((ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0)
3386a8a6cbcdSSreekanth Reddy 			    &&
3387a8a6cbcdSSreekanth Reddy 			    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3388a8a6cbcdSSreekanth Reddy 			    MPT3_DIAG_BUFFER_IS_APP_OWNED)) {
3389a5a20c4aSSuraj Upadhyay 				dma_free_coherent(&ioc->pdev->dev,
3390a5a20c4aSSuraj Upadhyay 						  ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE],
3391a8a6cbcdSSreekanth Reddy 						  ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE],
3392a5a20c4aSSuraj Upadhyay 						  ioc->diag_buffer_dma[MPI2_DIAG_BUF_TYPE_TRACE]);
3393a8a6cbcdSSreekanth Reddy 				ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE] =
3394a8a6cbcdSSreekanth Reddy 				    NULL;
3395a8a6cbcdSSreekanth Reddy 			}
3396a8a6cbcdSSreekanth Reddy 
3397f92363d1SSreekanth Reddy 			diag_register.requested_buffer_size = (1024 * 1024);
3398a8a6cbcdSSreekanth Reddy 		}
3399d04a6edfSSreekanth Reddy 
340008e7378eSSreekanth Reddy 		diag_register.unique_id =
340108e7378eSSreekanth Reddy 		    (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
340208e7378eSSreekanth Reddy 		    (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
3403f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3404f92363d1SSreekanth Reddy 		_ctl_diag_register_2(ioc,  &diag_register);
3405a066f4c3SSreekanth Reddy 		if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3406a066f4c3SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED) {
3407a066f4c3SSreekanth Reddy 			ioc_info(ioc,
3408a066f4c3SSreekanth Reddy 			    "Trace buffer %d KB allocated through sysfs\n",
3409a066f4c3SSreekanth Reddy 			    diag_register.requested_buffer_size>>10);
3410a066f4c3SSreekanth Reddy 			if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
3411a066f4c3SSreekanth Reddy 				ioc->diag_buffer_status[
3412a066f4c3SSreekanth Reddy 				    MPI2_DIAG_BUF_TYPE_TRACE] |=
3413a066f4c3SSreekanth Reddy 				    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
3414a066f4c3SSreekanth Reddy 		}
3415f92363d1SSreekanth Reddy 	} else if (!strcmp(str, "release")) {
3416f92363d1SSreekanth Reddy 		/* exit out if host buffers are already released */
3417f92363d1SSreekanth Reddy 		if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3418f92363d1SSreekanth Reddy 			goto out;
3419f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3420f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3421f92363d1SSreekanth Reddy 			goto out;
3422f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3423f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED))
3424f92363d1SSreekanth Reddy 			goto out;
3425919d8a3fSJoe Perches 		ioc_info(ioc, "releasing host trace buffer\n");
3426f92363d1SSreekanth Reddy 		mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3427f92363d1SSreekanth Reddy 		    &issue_reset);
3428f92363d1SSreekanth Reddy 	}
3429f92363d1SSreekanth Reddy 
3430f92363d1SSreekanth Reddy  out:
3431f92363d1SSreekanth Reddy 	return strlen(buf);
3432f92363d1SSreekanth Reddy }
3433c9df1442STomas Henzl static DEVICE_ATTR_RW(host_trace_buffer_enable);
3434f92363d1SSreekanth Reddy 
3435f92363d1SSreekanth Reddy /*********** diagnostic trigger suppport *********************************/
3436f92363d1SSreekanth Reddy 
3437f92363d1SSreekanth Reddy /**
3438c9df1442STomas Henzl  * diag_trigger_master_show - show the diag_trigger_master attribute
34394beb4867SBart Van Assche  * @cdev: pointer to embedded class device
34404beb4867SBart Van Assche  * @attr: ?
34414beb4867SBart Van Assche  * @buf: the buffer returned
3442f92363d1SSreekanth Reddy  *
3443f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3444f92363d1SSreekanth Reddy  */
3445f92363d1SSreekanth Reddy static ssize_t
3446c9df1442STomas Henzl diag_trigger_master_show(struct device *cdev,
3447f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3448f92363d1SSreekanth Reddy 
3449f92363d1SSreekanth Reddy {
3450f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3451f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3452f92363d1SSreekanth Reddy 	unsigned long flags;
3453f92363d1SSreekanth Reddy 	ssize_t rc;
3454f92363d1SSreekanth Reddy 
3455f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3456f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3457f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_master, rc);
3458f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3459f92363d1SSreekanth Reddy 	return rc;
3460f92363d1SSreekanth Reddy }
3461f92363d1SSreekanth Reddy 
3462f92363d1SSreekanth Reddy /**
3463c9df1442STomas Henzl  * diag_trigger_master_store - store the diag_trigger_master attribute
34644beb4867SBart Van Assche  * @cdev: pointer to embedded class device
34654beb4867SBart Van Assche  * @attr: ?
34664beb4867SBart Van Assche  * @buf: the buffer returned
34674beb4867SBart Van Assche  * @count: ?
3468f92363d1SSreekanth Reddy  *
3469f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3470f92363d1SSreekanth Reddy  */
3471f92363d1SSreekanth Reddy static ssize_t
3472c9df1442STomas Henzl diag_trigger_master_store(struct device *cdev,
3473f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3474f92363d1SSreekanth Reddy 
3475f92363d1SSreekanth Reddy {
3476f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3477f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3478f92363d1SSreekanth Reddy 	unsigned long flags;
3479f92363d1SSreekanth Reddy 	ssize_t rc;
3480f92363d1SSreekanth Reddy 
3481f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3482f92363d1SSreekanth Reddy 	rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3483f92363d1SSreekanth Reddy 	memset(&ioc->diag_trigger_master, 0,
3484f92363d1SSreekanth Reddy 	    sizeof(struct SL_WH_MASTER_TRIGGER_T));
3485f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_master, buf, rc);
3486f92363d1SSreekanth Reddy 	ioc->diag_trigger_master.MasterData |=
3487f92363d1SSreekanth Reddy 	    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3488f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3489f92363d1SSreekanth Reddy 	return rc;
3490f92363d1SSreekanth Reddy }
3491c9df1442STomas Henzl static DEVICE_ATTR_RW(diag_trigger_master);
3492f92363d1SSreekanth Reddy 
3493f92363d1SSreekanth Reddy 
3494f92363d1SSreekanth Reddy /**
3495c9df1442STomas Henzl  * diag_trigger_event_show - show the diag_trigger_event attribute
34964beb4867SBart Van Assche  * @cdev: pointer to embedded class device
34974beb4867SBart Van Assche  * @attr: ?
34984beb4867SBart Van Assche  * @buf: the buffer returned
3499f92363d1SSreekanth Reddy  *
3500f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3501f92363d1SSreekanth Reddy  */
3502f92363d1SSreekanth Reddy static ssize_t
3503c9df1442STomas Henzl diag_trigger_event_show(struct device *cdev,
3504f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3505f92363d1SSreekanth Reddy {
3506f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3507f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3508f92363d1SSreekanth Reddy 	unsigned long flags;
3509f92363d1SSreekanth Reddy 	ssize_t rc;
3510f92363d1SSreekanth Reddy 
3511f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3512f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3513f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_event, rc);
3514f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3515f92363d1SSreekanth Reddy 	return rc;
3516f92363d1SSreekanth Reddy }
3517f92363d1SSreekanth Reddy 
3518f92363d1SSreekanth Reddy /**
3519c9df1442STomas Henzl  * diag_trigger_event_store - store the diag_trigger_event attribute
35204beb4867SBart Van Assche  * @cdev: pointer to embedded class device
35214beb4867SBart Van Assche  * @attr: ?
35224beb4867SBart Van Assche  * @buf: the buffer returned
35234beb4867SBart Van Assche  * @count: ?
3524f92363d1SSreekanth Reddy  *
3525f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3526f92363d1SSreekanth Reddy  */
3527f92363d1SSreekanth Reddy static ssize_t
3528c9df1442STomas Henzl diag_trigger_event_store(struct device *cdev,
3529f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3530f92363d1SSreekanth Reddy 
3531f92363d1SSreekanth Reddy {
3532f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3533f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3534f92363d1SSreekanth Reddy 	unsigned long flags;
3535f92363d1SSreekanth Reddy 	ssize_t sz;
3536f92363d1SSreekanth Reddy 
3537f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3538f92363d1SSreekanth Reddy 	sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3539f92363d1SSreekanth Reddy 	memset(&ioc->diag_trigger_event, 0,
3540f92363d1SSreekanth Reddy 	    sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3541f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_event, buf, sz);
3542f92363d1SSreekanth Reddy 	if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3543f92363d1SSreekanth Reddy 		ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3544f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3545f92363d1SSreekanth Reddy 	return sz;
3546f92363d1SSreekanth Reddy }
3547c9df1442STomas Henzl static DEVICE_ATTR_RW(diag_trigger_event);
3548f92363d1SSreekanth Reddy 
3549f92363d1SSreekanth Reddy 
3550f92363d1SSreekanth Reddy /**
3551c9df1442STomas Henzl  * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
35524beb4867SBart Van Assche  * @cdev: pointer to embedded class device
35534beb4867SBart Van Assche  * @attr: ?
35544beb4867SBart Van Assche  * @buf: the buffer returned
3555f92363d1SSreekanth Reddy  *
3556f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3557f92363d1SSreekanth Reddy  */
3558f92363d1SSreekanth Reddy static ssize_t
3559c9df1442STomas Henzl diag_trigger_scsi_show(struct device *cdev,
3560f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3561f92363d1SSreekanth Reddy {
3562f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3563f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3564f92363d1SSreekanth Reddy 	unsigned long flags;
3565f92363d1SSreekanth Reddy 	ssize_t rc;
3566f92363d1SSreekanth Reddy 
3567f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3568f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3569f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_scsi, rc);
3570f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3571f92363d1SSreekanth Reddy 	return rc;
3572f92363d1SSreekanth Reddy }
3573f92363d1SSreekanth Reddy 
3574f92363d1SSreekanth Reddy /**
3575c9df1442STomas Henzl  * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
35764beb4867SBart Van Assche  * @cdev: pointer to embedded class device
35774beb4867SBart Van Assche  * @attr: ?
35784beb4867SBart Van Assche  * @buf: the buffer returned
35794beb4867SBart Van Assche  * @count: ?
3580f92363d1SSreekanth Reddy  *
3581f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3582f92363d1SSreekanth Reddy  */
3583f92363d1SSreekanth Reddy static ssize_t
3584c9df1442STomas Henzl diag_trigger_scsi_store(struct device *cdev,
3585f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3586f92363d1SSreekanth Reddy {
3587f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3588f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3589f92363d1SSreekanth Reddy 	unsigned long flags;
3590f92363d1SSreekanth Reddy 	ssize_t sz;
3591f92363d1SSreekanth Reddy 
3592f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
35931de540a9SDan Carpenter 	sz = min(sizeof(ioc->diag_trigger_scsi), count);
35941de540a9SDan Carpenter 	memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
3595f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_scsi, buf, sz);
3596f92363d1SSreekanth Reddy 	if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3597f92363d1SSreekanth Reddy 		ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3598f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3599f92363d1SSreekanth Reddy 	return sz;
3600f92363d1SSreekanth Reddy }
3601c9df1442STomas Henzl static DEVICE_ATTR_RW(diag_trigger_scsi);
3602f92363d1SSreekanth Reddy 
3603f92363d1SSreekanth Reddy 
3604f92363d1SSreekanth Reddy /**
3605c9df1442STomas Henzl  * diag_trigger_scsi_show - show the diag_trigger_mpi attribute
36064beb4867SBart Van Assche  * @cdev: pointer to embedded class device
36074beb4867SBart Van Assche  * @attr: ?
36084beb4867SBart Van Assche  * @buf: the buffer returned
3609f92363d1SSreekanth Reddy  *
3610f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3611f92363d1SSreekanth Reddy  */
3612f92363d1SSreekanth Reddy static ssize_t
3613c9df1442STomas Henzl diag_trigger_mpi_show(struct device *cdev,
3614f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3615f92363d1SSreekanth Reddy {
3616f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3617f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3618f92363d1SSreekanth Reddy 	unsigned long flags;
3619f92363d1SSreekanth Reddy 	ssize_t rc;
3620f92363d1SSreekanth Reddy 
3621f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3622f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3623f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_mpi, rc);
3624f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3625f92363d1SSreekanth Reddy 	return rc;
3626f92363d1SSreekanth Reddy }
3627f92363d1SSreekanth Reddy 
3628f92363d1SSreekanth Reddy /**
3629c9df1442STomas Henzl  * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
36304beb4867SBart Van Assche  * @cdev: pointer to embedded class device
36314beb4867SBart Van Assche  * @attr: ?
36324beb4867SBart Van Assche  * @buf: the buffer returned
36334beb4867SBart Van Assche  * @count: ?
3634f92363d1SSreekanth Reddy  *
3635f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3636f92363d1SSreekanth Reddy  */
3637f92363d1SSreekanth Reddy static ssize_t
3638c9df1442STomas Henzl diag_trigger_mpi_store(struct device *cdev,
3639f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3640f92363d1SSreekanth Reddy {
3641f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3642f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3643f92363d1SSreekanth Reddy 	unsigned long flags;
3644f92363d1SSreekanth Reddy 	ssize_t sz;
3645f92363d1SSreekanth Reddy 
3646f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3647f92363d1SSreekanth Reddy 	sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3648f92363d1SSreekanth Reddy 	memset(&ioc->diag_trigger_mpi, 0,
364966331e8cSDan Carpenter 	    sizeof(ioc->diag_trigger_mpi));
3650f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_mpi, buf, sz);
3651f92363d1SSreekanth Reddy 	if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3652f92363d1SSreekanth Reddy 		ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3653f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3654f92363d1SSreekanth Reddy 	return sz;
3655f92363d1SSreekanth Reddy }
3656f92363d1SSreekanth Reddy 
3657c9df1442STomas Henzl static DEVICE_ATTR_RW(diag_trigger_mpi);
3658f92363d1SSreekanth Reddy 
3659f92363d1SSreekanth Reddy /*********** diagnostic trigger suppport *** END ****************************/
3660f92363d1SSreekanth Reddy 
3661f92363d1SSreekanth Reddy /*****************************************/
3662f92363d1SSreekanth Reddy 
36633ac8e47bSSuganath Prabu /**
36643ac8e47bSSuganath Prabu  * drv_support_bitmap_show - driver supported feature bitmap
36659133d27eSDamien Le Moal  * @cdev: pointer to embedded class device
36669133d27eSDamien Le Moal  * @attr: unused
36679133d27eSDamien Le Moal  * @buf: the buffer returned
36683ac8e47bSSuganath Prabu  *
36693ac8e47bSSuganath Prabu  * A sysfs 'read-only' shost attribute.
36703ac8e47bSSuganath Prabu  */
36713ac8e47bSSuganath Prabu static ssize_t
36723ac8e47bSSuganath Prabu drv_support_bitmap_show(struct device *cdev,
36733ac8e47bSSuganath Prabu 	struct device_attribute *attr, char *buf)
36743ac8e47bSSuganath Prabu {
36753ac8e47bSSuganath Prabu 	struct Scsi_Host *shost = class_to_shost(cdev);
36763ac8e47bSSuganath Prabu 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
36773ac8e47bSSuganath Prabu 
36783ac8e47bSSuganath Prabu 	return snprintf(buf, PAGE_SIZE, "0x%08x\n", ioc->drv_support_bitmap);
36793ac8e47bSSuganath Prabu }
36803ac8e47bSSuganath Prabu static DEVICE_ATTR_RO(drv_support_bitmap);
36813ac8e47bSSuganath Prabu 
36828dc8d29aSSreekanth Reddy /**
36838dc8d29aSSreekanth Reddy  * enable_sdev_max_qd_show - display whether sdev max qd is enabled/disabled
36849133d27eSDamien Le Moal  * @cdev: pointer to embedded class device
36859133d27eSDamien Le Moal  * @attr: unused
36869133d27eSDamien Le Moal  * @buf: the buffer returned
36878dc8d29aSSreekanth Reddy  *
36888dc8d29aSSreekanth Reddy  * A sysfs read/write shost attribute. This attribute is used to set the
36898dc8d29aSSreekanth Reddy  * targets queue depth to HBA IO queue depth if this attribute is enabled.
36908dc8d29aSSreekanth Reddy  */
36918dc8d29aSSreekanth Reddy static ssize_t
36928dc8d29aSSreekanth Reddy enable_sdev_max_qd_show(struct device *cdev,
36938dc8d29aSSreekanth Reddy 	struct device_attribute *attr, char *buf)
36948dc8d29aSSreekanth Reddy {
36958dc8d29aSSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
36968dc8d29aSSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
36978dc8d29aSSreekanth Reddy 
36988dc8d29aSSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->enable_sdev_max_qd);
36998dc8d29aSSreekanth Reddy }
37008dc8d29aSSreekanth Reddy 
37018dc8d29aSSreekanth Reddy /**
37028dc8d29aSSreekanth Reddy  * enable_sdev_max_qd_store - Enable/disable sdev max qd
37039133d27eSDamien Le Moal  * @cdev: pointer to embedded class device
37049133d27eSDamien Le Moal  * @attr: unused
37059133d27eSDamien Le Moal  * @buf: the buffer returned
37069133d27eSDamien Le Moal  * @count: unused
37078dc8d29aSSreekanth Reddy  *
37088dc8d29aSSreekanth Reddy  * A sysfs read/write shost attribute. This attribute is used to set the
37098dc8d29aSSreekanth Reddy  * targets queue depth to HBA IO queue depth if this attribute is enabled.
37108dc8d29aSSreekanth Reddy  * If this attribute is disabled then targets will have corresponding default
37118dc8d29aSSreekanth Reddy  * queue depth.
37128dc8d29aSSreekanth Reddy  */
37138dc8d29aSSreekanth Reddy static ssize_t
37148dc8d29aSSreekanth Reddy enable_sdev_max_qd_store(struct device *cdev,
37158dc8d29aSSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
37168dc8d29aSSreekanth Reddy {
37178dc8d29aSSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
37188dc8d29aSSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
37198dc8d29aSSreekanth Reddy 	struct MPT3SAS_DEVICE *sas_device_priv_data;
37208dc8d29aSSreekanth Reddy 	struct MPT3SAS_TARGET *sas_target_priv_data;
37218dc8d29aSSreekanth Reddy 	int val = 0;
37228dc8d29aSSreekanth Reddy 	struct scsi_device *sdev;
37238dc8d29aSSreekanth Reddy 	struct _raid_device *raid_device;
37248dc8d29aSSreekanth Reddy 	int qdepth;
37258dc8d29aSSreekanth Reddy 
37268dc8d29aSSreekanth Reddy 	if (kstrtoint(buf, 0, &val) != 0)
37278dc8d29aSSreekanth Reddy 		return -EINVAL;
37288dc8d29aSSreekanth Reddy 
37298dc8d29aSSreekanth Reddy 	switch (val) {
37308dc8d29aSSreekanth Reddy 	case 0:
37318dc8d29aSSreekanth Reddy 		ioc->enable_sdev_max_qd = 0;
37328dc8d29aSSreekanth Reddy 		shost_for_each_device(sdev, ioc->shost) {
37338dc8d29aSSreekanth Reddy 			sas_device_priv_data = sdev->hostdata;
37348dc8d29aSSreekanth Reddy 			if (!sas_device_priv_data)
37358dc8d29aSSreekanth Reddy 				continue;
37368dc8d29aSSreekanth Reddy 			sas_target_priv_data = sas_device_priv_data->sas_target;
37378dc8d29aSSreekanth Reddy 			if (!sas_target_priv_data)
37388dc8d29aSSreekanth Reddy 				continue;
37398dc8d29aSSreekanth Reddy 
37408dc8d29aSSreekanth Reddy 			if (sas_target_priv_data->flags &
37418dc8d29aSSreekanth Reddy 			    MPT_TARGET_FLAGS_VOLUME) {
37428dc8d29aSSreekanth Reddy 				raid_device =
37438dc8d29aSSreekanth Reddy 				    mpt3sas_raid_device_find_by_handle(ioc,
37448dc8d29aSSreekanth Reddy 				    sas_target_priv_data->handle);
37458dc8d29aSSreekanth Reddy 
37468dc8d29aSSreekanth Reddy 				switch (raid_device->volume_type) {
37478dc8d29aSSreekanth Reddy 				case MPI2_RAID_VOL_TYPE_RAID0:
37488dc8d29aSSreekanth Reddy 					if (raid_device->device_info &
37498dc8d29aSSreekanth Reddy 					    MPI2_SAS_DEVICE_INFO_SSP_TARGET)
37508dc8d29aSSreekanth Reddy 						qdepth =
37518dc8d29aSSreekanth Reddy 						    MPT3SAS_SAS_QUEUE_DEPTH;
37528dc8d29aSSreekanth Reddy 					else
37538dc8d29aSSreekanth Reddy 						qdepth =
37548dc8d29aSSreekanth Reddy 						    MPT3SAS_SATA_QUEUE_DEPTH;
37558dc8d29aSSreekanth Reddy 					break;
37568dc8d29aSSreekanth Reddy 				case MPI2_RAID_VOL_TYPE_RAID1E:
37578dc8d29aSSreekanth Reddy 				case MPI2_RAID_VOL_TYPE_RAID1:
37588dc8d29aSSreekanth Reddy 				case MPI2_RAID_VOL_TYPE_RAID10:
37598dc8d29aSSreekanth Reddy 				case MPI2_RAID_VOL_TYPE_UNKNOWN:
37608dc8d29aSSreekanth Reddy 				default:
37618dc8d29aSSreekanth Reddy 					qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
37628dc8d29aSSreekanth Reddy 				}
37638dc8d29aSSreekanth Reddy 			} else if (sas_target_priv_data->flags &
37648dc8d29aSSreekanth Reddy 			    MPT_TARGET_FLAGS_PCIE_DEVICE)
37658dc8d29aSSreekanth Reddy 				qdepth = MPT3SAS_NVME_QUEUE_DEPTH;
37668dc8d29aSSreekanth Reddy 			else
37678dc8d29aSSreekanth Reddy 				qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
37688dc8d29aSSreekanth Reddy 
37698dc8d29aSSreekanth Reddy 			mpt3sas_scsih_change_queue_depth(sdev, qdepth);
37708dc8d29aSSreekanth Reddy 		}
37718dc8d29aSSreekanth Reddy 		break;
37728dc8d29aSSreekanth Reddy 	case 1:
37738dc8d29aSSreekanth Reddy 		ioc->enable_sdev_max_qd = 1;
37748dc8d29aSSreekanth Reddy 		shost_for_each_device(sdev, ioc->shost)
37758dc8d29aSSreekanth Reddy 			mpt3sas_scsih_change_queue_depth(sdev,
37768dc8d29aSSreekanth Reddy 			    shost->can_queue);
37778dc8d29aSSreekanth Reddy 		break;
37788dc8d29aSSreekanth Reddy 	default:
37798dc8d29aSSreekanth Reddy 		return -EINVAL;
37808dc8d29aSSreekanth Reddy 	}
37818dc8d29aSSreekanth Reddy 
37828dc8d29aSSreekanth Reddy 	return strlen(buf);
37838dc8d29aSSreekanth Reddy }
37848dc8d29aSSreekanth Reddy static DEVICE_ATTR_RW(enable_sdev_max_qd);
37858dc8d29aSSreekanth Reddy 
3786f92363d1SSreekanth Reddy struct device_attribute *mpt3sas_host_attrs[] = {
3787f92363d1SSreekanth Reddy 	&dev_attr_version_fw,
3788f92363d1SSreekanth Reddy 	&dev_attr_version_bios,
3789f92363d1SSreekanth Reddy 	&dev_attr_version_mpi,
3790f92363d1SSreekanth Reddy 	&dev_attr_version_product,
3791f92363d1SSreekanth Reddy 	&dev_attr_version_nvdata_persistent,
3792f92363d1SSreekanth Reddy 	&dev_attr_version_nvdata_default,
3793f92363d1SSreekanth Reddy 	&dev_attr_board_name,
3794f92363d1SSreekanth Reddy 	&dev_attr_board_assembly,
3795f92363d1SSreekanth Reddy 	&dev_attr_board_tracer,
3796f92363d1SSreekanth Reddy 	&dev_attr_io_delay,
3797f92363d1SSreekanth Reddy 	&dev_attr_device_delay,
3798f92363d1SSreekanth Reddy 	&dev_attr_logging_level,
3799f92363d1SSreekanth Reddy 	&dev_attr_fwfault_debug,
3800f92363d1SSreekanth Reddy 	&dev_attr_fw_queue_depth,
3801f92363d1SSreekanth Reddy 	&dev_attr_host_sas_address,
3802f92363d1SSreekanth Reddy 	&dev_attr_ioc_reset_count,
3803f92363d1SSreekanth Reddy 	&dev_attr_host_trace_buffer_size,
3804f92363d1SSreekanth Reddy 	&dev_attr_host_trace_buffer,
3805f92363d1SSreekanth Reddy 	&dev_attr_host_trace_buffer_enable,
3806f92363d1SSreekanth Reddy 	&dev_attr_reply_queue_count,
3807f92363d1SSreekanth Reddy 	&dev_attr_diag_trigger_master,
3808f92363d1SSreekanth Reddy 	&dev_attr_diag_trigger_event,
3809f92363d1SSreekanth Reddy 	&dev_attr_diag_trigger_scsi,
3810f92363d1SSreekanth Reddy 	&dev_attr_diag_trigger_mpi,
38113ac8e47bSSuganath Prabu 	&dev_attr_drv_support_bitmap,
381242263095SSreekanth Reddy 	&dev_attr_BRM_status,
38138dc8d29aSSreekanth Reddy 	&dev_attr_enable_sdev_max_qd,
3814f92363d1SSreekanth Reddy 	NULL,
3815f92363d1SSreekanth Reddy };
3816f92363d1SSreekanth Reddy 
3817f92363d1SSreekanth Reddy /* device attributes */
3818f92363d1SSreekanth Reddy 
3819f92363d1SSreekanth Reddy /**
3820c9df1442STomas Henzl  * sas_address_show - sas address
38214beb4867SBart Van Assche  * @dev: pointer to embedded class device
38224beb4867SBart Van Assche  * @attr: ?
38234beb4867SBart Van Assche  * @buf: the buffer returned
3824f92363d1SSreekanth Reddy  *
3825f92363d1SSreekanth Reddy  * This is the sas address for the target
3826f92363d1SSreekanth Reddy  *
3827f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3828f92363d1SSreekanth Reddy  */
3829f92363d1SSreekanth Reddy static ssize_t
3830c9df1442STomas Henzl sas_address_show(struct device *dev, struct device_attribute *attr,
3831f92363d1SSreekanth Reddy 	char *buf)
3832f92363d1SSreekanth Reddy {
3833f92363d1SSreekanth Reddy 	struct scsi_device *sdev = to_scsi_device(dev);
3834f92363d1SSreekanth Reddy 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3835f92363d1SSreekanth Reddy 
3836f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3837f92363d1SSreekanth Reddy 	    (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3838f92363d1SSreekanth Reddy }
3839c9df1442STomas Henzl static DEVICE_ATTR_RO(sas_address);
3840f92363d1SSreekanth Reddy 
3841f92363d1SSreekanth Reddy /**
3842c9df1442STomas Henzl  * sas_device_handle_show - device handle
38434beb4867SBart Van Assche  * @dev: pointer to embedded class device
38444beb4867SBart Van Assche  * @attr: ?
38454beb4867SBart Van Assche  * @buf: the buffer returned
3846f92363d1SSreekanth Reddy  *
3847f92363d1SSreekanth Reddy  * This is the firmware assigned device handle
3848f92363d1SSreekanth Reddy  *
3849f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3850f92363d1SSreekanth Reddy  */
3851f92363d1SSreekanth Reddy static ssize_t
3852c9df1442STomas Henzl sas_device_handle_show(struct device *dev, struct device_attribute *attr,
3853f92363d1SSreekanth Reddy 	char *buf)
3854f92363d1SSreekanth Reddy {
3855f92363d1SSreekanth Reddy 	struct scsi_device *sdev = to_scsi_device(dev);
3856f92363d1SSreekanth Reddy 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3857f92363d1SSreekanth Reddy 
3858f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3859f92363d1SSreekanth Reddy 	    sas_device_priv_data->sas_target->handle);
3860f92363d1SSreekanth Reddy }
3861c9df1442STomas Henzl static DEVICE_ATTR_RO(sas_device_handle);
3862f92363d1SSreekanth Reddy 
3863307d9075SAdam Manzanares /**
3864c9df1442STomas Henzl  * sas_ncq_io_prio_show - send prioritized io commands to device
38654beb4867SBart Van Assche  * @dev: pointer to embedded device
38664beb4867SBart Van Assche  * @attr: ?
38674beb4867SBart Van Assche  * @buf: the buffer returned
3868307d9075SAdam Manzanares  *
3869307d9075SAdam Manzanares  * A sysfs 'read/write' sdev attribute, only works with SATA
3870307d9075SAdam Manzanares  */
3871307d9075SAdam Manzanares static ssize_t
3872c9df1442STomas Henzl sas_ncq_prio_enable_show(struct device *dev,
3873307d9075SAdam Manzanares 				 struct device_attribute *attr, char *buf)
3874307d9075SAdam Manzanares {
3875307d9075SAdam Manzanares 	struct scsi_device *sdev = to_scsi_device(dev);
3876307d9075SAdam Manzanares 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3877307d9075SAdam Manzanares 
3878307d9075SAdam Manzanares 	return snprintf(buf, PAGE_SIZE, "%d\n",
3879307d9075SAdam Manzanares 			sas_device_priv_data->ncq_prio_enable);
3880307d9075SAdam Manzanares }
3881307d9075SAdam Manzanares 
3882307d9075SAdam Manzanares static ssize_t
3883c9df1442STomas Henzl sas_ncq_prio_enable_store(struct device *dev,
3884307d9075SAdam Manzanares 				  struct device_attribute *attr,
3885307d9075SAdam Manzanares 				  const char *buf, size_t count)
3886307d9075SAdam Manzanares {
3887307d9075SAdam Manzanares 	struct scsi_device *sdev = to_scsi_device(dev);
3888307d9075SAdam Manzanares 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3889307d9075SAdam Manzanares 	bool ncq_prio_enable = 0;
3890307d9075SAdam Manzanares 
3891307d9075SAdam Manzanares 	if (kstrtobool(buf, &ncq_prio_enable))
3892307d9075SAdam Manzanares 		return -EINVAL;
3893307d9075SAdam Manzanares 
3894307d9075SAdam Manzanares 	if (!scsih_ncq_prio_supp(sdev))
3895307d9075SAdam Manzanares 		return -EINVAL;
3896307d9075SAdam Manzanares 
3897307d9075SAdam Manzanares 	sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
3898307d9075SAdam Manzanares 	return strlen(buf);
3899307d9075SAdam Manzanares }
3900c9df1442STomas Henzl static DEVICE_ATTR_RW(sas_ncq_prio_enable);
3901307d9075SAdam Manzanares 
3902f92363d1SSreekanth Reddy struct device_attribute *mpt3sas_dev_attrs[] = {
3903f92363d1SSreekanth Reddy 	&dev_attr_sas_address,
3904f92363d1SSreekanth Reddy 	&dev_attr_sas_device_handle,
3905307d9075SAdam Manzanares 	&dev_attr_sas_ncq_prio_enable,
3906f92363d1SSreekanth Reddy 	NULL,
3907f92363d1SSreekanth Reddy };
3908f92363d1SSreekanth Reddy 
3909c84b06a4SSreekanth Reddy /* file operations table for mpt3ctl device */
3910c84b06a4SSreekanth Reddy static const struct file_operations ctl_fops = {
3911c84b06a4SSreekanth Reddy 	.owner = THIS_MODULE,
3912c84b06a4SSreekanth Reddy 	.unlocked_ioctl = _ctl_ioctl,
3913c84b06a4SSreekanth Reddy 	.poll = _ctl_poll,
3914c84b06a4SSreekanth Reddy 	.fasync = _ctl_fasync,
3915c84b06a4SSreekanth Reddy #ifdef CONFIG_COMPAT
3916c84b06a4SSreekanth Reddy 	.compat_ioctl = _ctl_ioctl_compat,
3917c84b06a4SSreekanth Reddy #endif
3918c84b06a4SSreekanth Reddy };
3919c84b06a4SSreekanth Reddy 
3920c84b06a4SSreekanth Reddy /* file operations table for mpt2ctl device */
3921c84b06a4SSreekanth Reddy static const struct file_operations ctl_gen2_fops = {
3922c84b06a4SSreekanth Reddy 	.owner = THIS_MODULE,
3923c84b06a4SSreekanth Reddy 	.unlocked_ioctl = _ctl_mpt2_ioctl,
3924c84b06a4SSreekanth Reddy 	.poll = _ctl_poll,
3925c84b06a4SSreekanth Reddy 	.fasync = _ctl_fasync,
3926c84b06a4SSreekanth Reddy #ifdef CONFIG_COMPAT
3927c84b06a4SSreekanth Reddy 	.compat_ioctl = _ctl_mpt2_ioctl_compat,
3928c84b06a4SSreekanth Reddy #endif
3929c84b06a4SSreekanth Reddy };
3930c84b06a4SSreekanth Reddy 
3931c84b06a4SSreekanth Reddy static struct miscdevice ctl_dev = {
3932c84b06a4SSreekanth Reddy 	.minor  = MPT3SAS_MINOR,
3933c84b06a4SSreekanth Reddy 	.name   = MPT3SAS_DEV_NAME,
3934c84b06a4SSreekanth Reddy 	.fops   = &ctl_fops,
3935c84b06a4SSreekanth Reddy };
3936c84b06a4SSreekanth Reddy 
3937c84b06a4SSreekanth Reddy static struct miscdevice gen2_ctl_dev = {
3938c84b06a4SSreekanth Reddy 	.minor  = MPT2SAS_MINOR,
3939c84b06a4SSreekanth Reddy 	.name   = MPT2SAS_DEV_NAME,
3940c84b06a4SSreekanth Reddy 	.fops   = &ctl_gen2_fops,
3941c84b06a4SSreekanth Reddy };
3942c84b06a4SSreekanth Reddy 
3943f92363d1SSreekanth Reddy /**
3944c84b06a4SSreekanth Reddy  * mpt3sas_ctl_init - main entry point for ctl.
39454beb4867SBart Van Assche  * @hbas_to_enumerate: ?
3946f92363d1SSreekanth Reddy  */
3947f92363d1SSreekanth Reddy void
3948c84b06a4SSreekanth Reddy mpt3sas_ctl_init(ushort hbas_to_enumerate)
3949f92363d1SSreekanth Reddy {
3950f92363d1SSreekanth Reddy 	async_queue = NULL;
3951c84b06a4SSreekanth Reddy 
3952c84b06a4SSreekanth Reddy 	/* Don't register mpt3ctl ioctl device if
3953c84b06a4SSreekanth Reddy 	 * hbas_to_enumarate is one.
3954c84b06a4SSreekanth Reddy 	 */
3955c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 1)
3956c84b06a4SSreekanth Reddy 		if (misc_register(&ctl_dev) < 0)
3957c84b06a4SSreekanth Reddy 			pr_err("%s can't register misc device [minor=%d]\n",
3958c84b06a4SSreekanth Reddy 			    MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3959c84b06a4SSreekanth Reddy 
3960c84b06a4SSreekanth Reddy 	/* Don't register mpt3ctl ioctl device if
3961c84b06a4SSreekanth Reddy 	 * hbas_to_enumarate is two.
3962c84b06a4SSreekanth Reddy 	 */
3963c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 2)
3964c84b06a4SSreekanth Reddy 		if (misc_register(&gen2_ctl_dev) < 0)
3965c84b06a4SSreekanth Reddy 			pr_err("%s can't register misc device [minor=%d]\n",
3966c84b06a4SSreekanth Reddy 			    MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3967c84b06a4SSreekanth Reddy 
3968f92363d1SSreekanth Reddy 	init_waitqueue_head(&ctl_poll_wait);
3969f92363d1SSreekanth Reddy }
3970f92363d1SSreekanth Reddy 
3971f92363d1SSreekanth Reddy /**
3972c84b06a4SSreekanth Reddy  * mpt3sas_ctl_exit - exit point for ctl
39734beb4867SBart Van Assche  * @hbas_to_enumerate: ?
3974f92363d1SSreekanth Reddy  */
3975f92363d1SSreekanth Reddy void
3976c84b06a4SSreekanth Reddy mpt3sas_ctl_exit(ushort hbas_to_enumerate)
3977f92363d1SSreekanth Reddy {
3978f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc;
3979f92363d1SSreekanth Reddy 	int i;
3980f92363d1SSreekanth Reddy 
3981f92363d1SSreekanth Reddy 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3982f92363d1SSreekanth Reddy 
3983f92363d1SSreekanth Reddy 		/* free memory associated to diag buffers */
3984f92363d1SSreekanth Reddy 		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3985f92363d1SSreekanth Reddy 			if (!ioc->diag_buffer[i])
3986f92363d1SSreekanth Reddy 				continue;
39871c2048bdSChristoph Hellwig 			dma_free_coherent(&ioc->pdev->dev,
39881c2048bdSChristoph Hellwig 					  ioc->diag_buffer_sz[i],
39891c2048bdSChristoph Hellwig 					  ioc->diag_buffer[i],
39901c2048bdSChristoph Hellwig 					  ioc->diag_buffer_dma[i]);
3991f92363d1SSreekanth Reddy 			ioc->diag_buffer[i] = NULL;
3992f92363d1SSreekanth Reddy 			ioc->diag_buffer_status[i] = 0;
3993f92363d1SSreekanth Reddy 		}
3994f92363d1SSreekanth Reddy 
3995f92363d1SSreekanth Reddy 		kfree(ioc->event_log);
3996f92363d1SSreekanth Reddy 	}
3997c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 1)
3998c84b06a4SSreekanth Reddy 		misc_deregister(&ctl_dev);
3999c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 2)
4000c84b06a4SSreekanth Reddy 		misc_deregister(&gen2_ctl_dev);
4001f92363d1SSreekanth Reddy }
4002