xref: /linux/drivers/scsi/mpt3sas/mpt3sas_ctl.c (revision f45fadde91ec892fdc453e3df9469e9457152526)
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 /**
457782a1ab3SLee Jones  * mpt3sas_ctl_pre_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__);
482688c1a0aSSuganath Prabu S 		ioc->htb_rel.buffer_rel_condition =
483688c1a0aSSuganath Prabu S 		    MPT3_DIAG_BUFFER_REL_TRIGGER;
484f92363d1SSreekanth Reddy 		mpt3sas_send_diag_release(ioc, i, &issue_reset);
485f92363d1SSreekanth Reddy 	}
486c7a35705SBart Van Assche }
487c7a35705SBart Van Assche 
488c7a35705SBart Van Assche /**
489782a1ab3SLee Jones  * mpt3sas_ctl_clear_outstanding_ioctls - clears outstanding ioctl cmd.
490c7a35705SBart Van Assche  * @ioc: per adapter object
491c7a35705SBart Van Assche  *
492c7a35705SBart Van Assche  * The handler for doing any required cleanup or initialization.
493c7a35705SBart Van Assche  */
49436c6c7f7SSreekanth Reddy void mpt3sas_ctl_clear_outstanding_ioctls(struct MPT3SAS_ADAPTER *ioc)
495c7a35705SBart Van Assche {
49636c6c7f7SSreekanth Reddy 	dtmprintk(ioc,
49736c6c7f7SSreekanth Reddy 	    ioc_info(ioc, "%s: clear outstanding ioctl cmd\n", __func__));
498f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
499f92363d1SSreekanth Reddy 		ioc->ctl_cmds.status |= MPT3_CMD_RESET;
500f92363d1SSreekanth Reddy 		mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
501f92363d1SSreekanth Reddy 		complete(&ioc->ctl_cmds.done);
502f92363d1SSreekanth Reddy 	}
503c7a35705SBart Van Assche }
504c7a35705SBart Van Assche 
505c7a35705SBart Van Assche /**
506782a1ab3SLee Jones  * mpt3sas_ctl_reset_done_handler - reset callback handler (for ctl)
507c7a35705SBart Van Assche  * @ioc: per adapter object
508c7a35705SBart Van Assche  *
509c7a35705SBart Van Assche  * The handler for doing any required cleanup or initialization.
510c7a35705SBart Van Assche  */
511c7a35705SBart Van Assche void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
512c7a35705SBart Van Assche {
513c7a35705SBart Van Assche 	int i;
514c7a35705SBart Van Assche 
515919d8a3fSJoe Perches 	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
516f92363d1SSreekanth Reddy 
517f92363d1SSreekanth Reddy 	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
518f92363d1SSreekanth Reddy 		if (!(ioc->diag_buffer_status[i] &
519f92363d1SSreekanth Reddy 		      MPT3_DIAG_BUFFER_IS_REGISTERED))
520f92363d1SSreekanth Reddy 			continue;
521f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[i] &
522f92363d1SSreekanth Reddy 		     MPT3_DIAG_BUFFER_IS_RELEASED))
523f92363d1SSreekanth Reddy 			continue;
524f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[i] |=
525f92363d1SSreekanth Reddy 			MPT3_DIAG_BUFFER_IS_DIAG_RESET;
526f92363d1SSreekanth Reddy 	}
527f92363d1SSreekanth Reddy }
528f92363d1SSreekanth Reddy 
529f92363d1SSreekanth Reddy /**
530c84b06a4SSreekanth Reddy  * _ctl_fasync -
5314beb4867SBart Van Assche  * @fd: ?
5324beb4867SBart Van Assche  * @filep: ?
5334beb4867SBart Van Assche  * @mode: ?
534f92363d1SSreekanth Reddy  *
535f92363d1SSreekanth Reddy  * Called when application request fasyn callback handler.
536f92363d1SSreekanth Reddy  */
5378bbb1cf6SCalvin Owens static int
538c84b06a4SSreekanth Reddy _ctl_fasync(int fd, struct file *filep, int mode)
539f92363d1SSreekanth Reddy {
540f92363d1SSreekanth Reddy 	return fasync_helper(fd, filep, mode, &async_queue);
541f92363d1SSreekanth Reddy }
542f92363d1SSreekanth Reddy 
543f92363d1SSreekanth Reddy /**
544c84b06a4SSreekanth Reddy  * _ctl_poll -
5454beb4867SBart Van Assche  * @filep: ?
5464beb4867SBart Van Assche  * @wait: ?
547f92363d1SSreekanth Reddy  *
548f92363d1SSreekanth Reddy  */
549afc9a42bSAl Viro static __poll_t
550c84b06a4SSreekanth Reddy _ctl_poll(struct file *filep, poll_table *wait)
551f92363d1SSreekanth Reddy {
552f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc;
553f92363d1SSreekanth Reddy 
554f92363d1SSreekanth Reddy 	poll_wait(filep, &ctl_poll_wait, wait);
555f92363d1SSreekanth Reddy 
55608c4d550SSreekanth Reddy 	/* global ioc lock to protect controller on list operations */
55708c4d550SSreekanth Reddy 	spin_lock(&gioc_lock);
558f92363d1SSreekanth Reddy 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
55908c4d550SSreekanth Reddy 		if (ioc->aen_event_read_flag) {
56008c4d550SSreekanth Reddy 			spin_unlock(&gioc_lock);
561a9a08845SLinus Torvalds 			return EPOLLIN | EPOLLRDNORM;
562f92363d1SSreekanth Reddy 		}
56308c4d550SSreekanth Reddy 	}
56408c4d550SSreekanth Reddy 	spin_unlock(&gioc_lock);
565f92363d1SSreekanth Reddy 	return 0;
566f92363d1SSreekanth Reddy }
567f92363d1SSreekanth Reddy 
568f92363d1SSreekanth Reddy /**
569f92363d1SSreekanth Reddy  * _ctl_set_task_mid - assign an active smid to tm request
570f92363d1SSreekanth Reddy  * @ioc: per adapter object
5714beb4867SBart Van Assche  * @karg: (struct mpt3_ioctl_command)
5724beb4867SBart Van Assche  * @tm_request: pointer to mf from user space
573f92363d1SSreekanth Reddy  *
5744beb4867SBart Van Assche  * Return: 0 when an smid if found, else fail.
575f92363d1SSreekanth Reddy  * during failure, the reply frame is filled.
576f92363d1SSreekanth Reddy  */
577f92363d1SSreekanth Reddy static int
578f92363d1SSreekanth Reddy _ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
579f92363d1SSreekanth Reddy 	Mpi2SCSITaskManagementRequest_t *tm_request)
580f92363d1SSreekanth Reddy {
581dceaef94SDamien Le Moal 	bool found = false;
582dbec4c90SSuganath Prabu Subramani 	u16 smid;
583f92363d1SSreekanth Reddy 	u16 handle;
584f92363d1SSreekanth Reddy 	struct scsi_cmnd *scmd;
585f92363d1SSreekanth Reddy 	struct MPT3SAS_DEVICE *priv_data;
586f92363d1SSreekanth Reddy 	Mpi2SCSITaskManagementReply_t *tm_reply;
587f92363d1SSreekanth Reddy 	u32 sz;
588f92363d1SSreekanth Reddy 	u32 lun;
589f92363d1SSreekanth Reddy 	char *desc = NULL;
590f92363d1SSreekanth Reddy 
591f92363d1SSreekanth Reddy 	if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
592f92363d1SSreekanth Reddy 		desc = "abort_task";
593f92363d1SSreekanth Reddy 	else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
594f92363d1SSreekanth Reddy 		desc = "query_task";
595f92363d1SSreekanth Reddy 	else
596f92363d1SSreekanth Reddy 		return 0;
597f92363d1SSreekanth Reddy 
598f92363d1SSreekanth Reddy 	lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
599f92363d1SSreekanth Reddy 
600f92363d1SSreekanth Reddy 	handle = le16_to_cpu(tm_request->DevHandle);
601dbec4c90SSuganath Prabu Subramani 	for (smid = ioc->scsiio_depth; smid && !found; smid--) {
602dbec4c90SSuganath Prabu Subramani 		struct scsiio_tracker *st;
603dceaef94SDamien Le Moal 		__le16 task_mid;
604dbec4c90SSuganath Prabu Subramani 
605dbec4c90SSuganath Prabu Subramani 		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
606dbec4c90SSuganath Prabu Subramani 		if (!scmd)
607f92363d1SSreekanth Reddy 			continue;
608f92363d1SSreekanth Reddy 		if (lun != scmd->device->lun)
609f92363d1SSreekanth Reddy 			continue;
610f92363d1SSreekanth Reddy 		priv_data = scmd->device->hostdata;
611f92363d1SSreekanth Reddy 		if (priv_data->sas_target == NULL)
612f92363d1SSreekanth Reddy 			continue;
613f92363d1SSreekanth Reddy 		if (priv_data->sas_target->handle != handle)
614f92363d1SSreekanth Reddy 			continue;
615dbec4c90SSuganath Prabu Subramani 		st = scsi_cmd_priv(scmd);
6168f55c307SMinwoo Im 
6178f55c307SMinwoo Im 		/*
6188f55c307SMinwoo Im 		 * If the given TaskMID from the user space is zero, then the
6198f55c307SMinwoo Im 		 * first outstanding smid will be picked up.  Otherwise,
6208f55c307SMinwoo Im 		 * targeted smid will be the one.
6218f55c307SMinwoo Im 		 */
622dceaef94SDamien Le Moal 		task_mid = cpu_to_le16(st->smid);
623dceaef94SDamien Le Moal 		if (!tm_request->TaskMID)
624dceaef94SDamien Le Moal 			tm_request->TaskMID = task_mid;
625dceaef94SDamien Le Moal 		found = tm_request->TaskMID == task_mid;
6268f55c307SMinwoo Im 	}
627f92363d1SSreekanth Reddy 
628f92363d1SSreekanth Reddy 	if (!found) {
629919d8a3fSJoe Perches 		dctlprintk(ioc,
630919d8a3fSJoe Perches 			   ioc_info(ioc, "%s: handle(0x%04x), lun(%d), no active mid!!\n",
631919d8a3fSJoe Perches 				    desc, le16_to_cpu(tm_request->DevHandle),
632919d8a3fSJoe Perches 				    lun));
633f92363d1SSreekanth Reddy 		tm_reply = ioc->ctl_cmds.reply;
634f92363d1SSreekanth Reddy 		tm_reply->DevHandle = tm_request->DevHandle;
635f92363d1SSreekanth Reddy 		tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
636f92363d1SSreekanth Reddy 		tm_reply->TaskType = tm_request->TaskType;
637f92363d1SSreekanth Reddy 		tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
638f92363d1SSreekanth Reddy 		tm_reply->VP_ID = tm_request->VP_ID;
639f92363d1SSreekanth Reddy 		tm_reply->VF_ID = tm_request->VF_ID;
640f92363d1SSreekanth Reddy 		sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
641f92363d1SSreekanth Reddy 		if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
642f92363d1SSreekanth Reddy 		    sz))
643f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
644f92363d1SSreekanth Reddy 			    __LINE__, __func__);
645f92363d1SSreekanth Reddy 		return 1;
646f92363d1SSreekanth Reddy 	}
647f92363d1SSreekanth Reddy 
648919d8a3fSJoe Perches 	dctlprintk(ioc,
649919d8a3fSJoe Perches 		   ioc_info(ioc, "%s: handle(0x%04x), lun(%d), task_mid(%d)\n",
650f92363d1SSreekanth Reddy 			    desc, le16_to_cpu(tm_request->DevHandle), lun,
651f92363d1SSreekanth Reddy 			    le16_to_cpu(tm_request->TaskMID)));
652f92363d1SSreekanth Reddy 	return 0;
653f92363d1SSreekanth Reddy }
654f92363d1SSreekanth Reddy 
655f92363d1SSreekanth Reddy /**
656f92363d1SSreekanth Reddy  * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
657f92363d1SSreekanth Reddy  * @ioc: per adapter object
6584beb4867SBart Van Assche  * @karg: (struct mpt3_ioctl_command)
6594beb4867SBart Van Assche  * @mf: pointer to mf in user space
660f92363d1SSreekanth Reddy  */
661f92363d1SSreekanth Reddy static long
662f92363d1SSreekanth Reddy _ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
663f92363d1SSreekanth Reddy 	void __user *mf)
664f92363d1SSreekanth Reddy {
665f92363d1SSreekanth Reddy 	MPI2RequestHeader_t *mpi_request = NULL, *request;
666f92363d1SSreekanth Reddy 	MPI2DefaultReply_t *mpi_reply;
667aff39e61SSuganath Prabu Subramani 	Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
668c1a6c5acSChaitra P B 	struct _pcie_device *pcie_device = NULL;
669f92363d1SSreekanth Reddy 	u16 smid;
67042f68703SSuganath Prabu S 	unsigned long timeout;
671f92363d1SSreekanth Reddy 	u8 issue_reset;
672aff39e61SSuganath Prabu Subramani 	u32 sz, sz_arg;
673f92363d1SSreekanth Reddy 	void *psge;
674f92363d1SSreekanth Reddy 	void *data_out = NULL;
675f92363d1SSreekanth Reddy 	dma_addr_t data_out_dma = 0;
676f92363d1SSreekanth Reddy 	size_t data_out_sz = 0;
677f92363d1SSreekanth Reddy 	void *data_in = NULL;
678f92363d1SSreekanth Reddy 	dma_addr_t data_in_dma = 0;
679f92363d1SSreekanth Reddy 	size_t data_in_sz = 0;
680f92363d1SSreekanth Reddy 	long ret;
681c696f7b8SSuganath Prabu Subramani 	u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
682f92363d1SSreekanth Reddy 
683f92363d1SSreekanth Reddy 	issue_reset = 0;
684f92363d1SSreekanth Reddy 
685f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
686919d8a3fSJoe Perches 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
687f92363d1SSreekanth Reddy 		ret = -EAGAIN;
688f92363d1SSreekanth Reddy 		goto out;
689f92363d1SSreekanth Reddy 	}
690f92363d1SSreekanth Reddy 
691f4305749SSuganath Prabu 	ret = mpt3sas_wait_for_ioc(ioc,	IOC_OPERATIONAL_WAIT_COUNT);
692f4305749SSuganath Prabu 	if (ret)
693f92363d1SSreekanth Reddy 		goto out;
694f92363d1SSreekanth Reddy 
695f92363d1SSreekanth Reddy 	mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
696f92363d1SSreekanth Reddy 	if (!mpi_request) {
697919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed obtaining a memory for mpi_request\n",
698919d8a3fSJoe Perches 			__func__);
699f92363d1SSreekanth Reddy 		ret = -ENOMEM;
700f92363d1SSreekanth Reddy 		goto out;
701f92363d1SSreekanth Reddy 	}
702f92363d1SSreekanth Reddy 
703f92363d1SSreekanth Reddy 	/* Check for overflow and wraparound */
704f92363d1SSreekanth Reddy 	if (karg.data_sge_offset * 4 > ioc->request_sz ||
705f92363d1SSreekanth Reddy 	    karg.data_sge_offset > (UINT_MAX / 4)) {
706f92363d1SSreekanth Reddy 		ret = -EINVAL;
707f92363d1SSreekanth Reddy 		goto out;
708f92363d1SSreekanth Reddy 	}
709f92363d1SSreekanth Reddy 
710f92363d1SSreekanth Reddy 	/* copy in request message frame from user */
711f92363d1SSreekanth Reddy 	if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
712f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
713f92363d1SSreekanth Reddy 		    __func__);
714f92363d1SSreekanth Reddy 		ret = -EFAULT;
715f92363d1SSreekanth Reddy 		goto out;
716f92363d1SSreekanth Reddy 	}
717f92363d1SSreekanth Reddy 
718f92363d1SSreekanth Reddy 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
719f92363d1SSreekanth Reddy 		smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
720f92363d1SSreekanth Reddy 		if (!smid) {
721919d8a3fSJoe Perches 			ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
722f92363d1SSreekanth Reddy 			ret = -EAGAIN;
723f92363d1SSreekanth Reddy 			goto out;
724f92363d1SSreekanth Reddy 		}
725f92363d1SSreekanth Reddy 	} else {
726b0cd285eSHannes Reinecke 		/* Use first reserved smid for passthrough ioctls */
727b0cd285eSHannes Reinecke 		smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
728f92363d1SSreekanth Reddy 	}
729f92363d1SSreekanth Reddy 
730f92363d1SSreekanth Reddy 	ret = 0;
731f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
732f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
733f92363d1SSreekanth Reddy 	request = mpt3sas_base_get_msg_frame(ioc, smid);
734e224e03bSSuganath Prabu 	memset(request, 0, ioc->request_sz);
735f92363d1SSreekanth Reddy 	memcpy(request, mpi_request, karg.data_sge_offset*4);
736f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
737f92363d1SSreekanth Reddy 	data_out_sz = karg.data_out_size;
738f92363d1SSreekanth Reddy 	data_in_sz = karg.data_in_size;
739f92363d1SSreekanth Reddy 
740f92363d1SSreekanth Reddy 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
741c696f7b8SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
742c696f7b8SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
743aff39e61SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
744aff39e61SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
745c696f7b8SSuganath Prabu Subramani 
746c696f7b8SSuganath Prabu Subramani 		device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
747c696f7b8SSuganath Prabu Subramani 		if (!device_handle || (device_handle >
748c696f7b8SSuganath Prabu Subramani 		    ioc->facts.MaxDevHandle)) {
749f92363d1SSreekanth Reddy 			ret = -EINVAL;
750f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
751f92363d1SSreekanth Reddy 			goto out;
752f92363d1SSreekanth Reddy 		}
753f92363d1SSreekanth Reddy 	}
754f92363d1SSreekanth Reddy 
755f92363d1SSreekanth Reddy 	/* obtain dma-able memory for data transfer */
756f92363d1SSreekanth Reddy 	if (data_out_sz) /* WRITE */ {
7571c2048bdSChristoph Hellwig 		data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz,
7581c2048bdSChristoph Hellwig 				&data_out_dma, GFP_KERNEL);
759f92363d1SSreekanth Reddy 		if (!data_out) {
760f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
761f92363d1SSreekanth Reddy 			    __LINE__, __func__);
762f92363d1SSreekanth Reddy 			ret = -ENOMEM;
763f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
764f92363d1SSreekanth Reddy 			goto out;
765f92363d1SSreekanth Reddy 		}
766f92363d1SSreekanth Reddy 		if (copy_from_user(data_out, karg.data_out_buf_ptr,
767f92363d1SSreekanth Reddy 			data_out_sz)) {
768f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
769f92363d1SSreekanth Reddy 			    __LINE__, __func__);
770f92363d1SSreekanth Reddy 			ret =  -EFAULT;
771f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
772f92363d1SSreekanth Reddy 			goto out;
773f92363d1SSreekanth Reddy 		}
774f92363d1SSreekanth Reddy 	}
775f92363d1SSreekanth Reddy 
776f92363d1SSreekanth Reddy 	if (data_in_sz) /* READ */ {
7771c2048bdSChristoph Hellwig 		data_in = dma_alloc_coherent(&ioc->pdev->dev, data_in_sz,
7781c2048bdSChristoph Hellwig 				&data_in_dma, GFP_KERNEL);
779f92363d1SSreekanth Reddy 		if (!data_in) {
780f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
781f92363d1SSreekanth Reddy 			    __LINE__, __func__);
782f92363d1SSreekanth Reddy 			ret = -ENOMEM;
783f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
784f92363d1SSreekanth Reddy 			goto out;
785f92363d1SSreekanth Reddy 		}
786f92363d1SSreekanth Reddy 	}
787f92363d1SSreekanth Reddy 
788f92363d1SSreekanth Reddy 	psge = (void *)request + (karg.data_sge_offset*4);
789f92363d1SSreekanth Reddy 
790f92363d1SSreekanth Reddy 	/* send command to firmware */
791f92363d1SSreekanth Reddy 	_ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
792f92363d1SSreekanth Reddy 
793f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
794f92363d1SSreekanth Reddy 	switch (mpi_request->Function) {
795aff39e61SSuganath Prabu Subramani 	case MPI2_FUNCTION_NVME_ENCAPSULATED:
796aff39e61SSuganath Prabu Subramani 	{
797aff39e61SSuganath Prabu Subramani 		nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
79877fd4f2cSSreekanth Reddy 		if (!ioc->pcie_sg_lookup) {
79977fd4f2cSSreekanth Reddy 			dtmprintk(ioc, ioc_info(ioc,
80077fd4f2cSSreekanth Reddy 			    "HBA doesn't support NVMe. Rejecting NVMe Encapsulated request.\n"
80177fd4f2cSSreekanth Reddy 			    ));
80277fd4f2cSSreekanth Reddy 
80377fd4f2cSSreekanth Reddy 			if (ioc->logging_level & MPT_DEBUG_TM)
80477fd4f2cSSreekanth Reddy 				_debug_dump_mf(nvme_encap_request,
80577fd4f2cSSreekanth Reddy 				    ioc->request_sz/4);
80677fd4f2cSSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
80777fd4f2cSSreekanth Reddy 			ret = -EINVAL;
80877fd4f2cSSreekanth Reddy 			goto out;
80977fd4f2cSSreekanth Reddy 		}
810aff39e61SSuganath Prabu Subramani 		/*
811aff39e61SSuganath Prabu Subramani 		 * Get the Physical Address of the sense buffer.
812aff39e61SSuganath Prabu Subramani 		 * Use Error Response buffer address field to hold the sense
813aff39e61SSuganath Prabu Subramani 		 * buffer address.
814aff39e61SSuganath Prabu Subramani 		 * Clear the internal sense buffer, which will potentially hold
815aff39e61SSuganath Prabu Subramani 		 * the Completion Queue Entry on return, or 0 if no Entry.
816aff39e61SSuganath Prabu Subramani 		 * Build the PRPs and set direction bits.
817aff39e61SSuganath Prabu Subramani 		 * Send the request.
818aff39e61SSuganath Prabu Subramani 		 */
819cf6bf971SChaitra P B 		nvme_encap_request->ErrorResponseBaseAddress =
820cf6bf971SChaitra P B 		    cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
821aff39e61SSuganath Prabu Subramani 		nvme_encap_request->ErrorResponseBaseAddress |=
822cf6bf971SChaitra P B 		   cpu_to_le64(le32_to_cpu(
823cf6bf971SChaitra P B 		   mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
824aff39e61SSuganath Prabu Subramani 		nvme_encap_request->ErrorResponseAllocationLength =
825cf6bf971SChaitra P B 					cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
826aff39e61SSuganath Prabu Subramani 		memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
827aff39e61SSuganath Prabu Subramani 		ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
828aff39e61SSuganath Prabu Subramani 		    data_out_dma, data_out_sz, data_in_dma, data_in_sz);
829aff39e61SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
830919d8a3fSJoe Perches 			dtmprintk(ioc,
831919d8a3fSJoe Perches 				  ioc_info(ioc, "handle(0x%04x): ioctl failed due to device removal in progress\n",
832919d8a3fSJoe Perches 					   device_handle));
833aff39e61SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
834aff39e61SSuganath Prabu Subramani 			ret = -EINVAL;
835aff39e61SSuganath Prabu Subramani 			goto out;
836aff39e61SSuganath Prabu Subramani 		}
83740114bdeSSuganath Prabu S 		mpt3sas_base_put_smid_nvme_encap(ioc, smid);
838aff39e61SSuganath Prabu Subramani 		break;
839aff39e61SSuganath Prabu Subramani 	}
840f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SCSI_IO_REQUEST:
841f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
842f92363d1SSreekanth Reddy 	{
843f92363d1SSreekanth Reddy 		Mpi2SCSIIORequest_t *scsiio_request =
844f92363d1SSreekanth Reddy 		    (Mpi2SCSIIORequest_t *)request;
845f92363d1SSreekanth Reddy 		scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
846f92363d1SSreekanth Reddy 		scsiio_request->SenseBufferLowAddress =
847f92363d1SSreekanth Reddy 		    mpt3sas_base_get_sense_buffer_dma(ioc, smid);
848f92363d1SSreekanth Reddy 		memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
849c696f7b8SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
850919d8a3fSJoe Perches 			dtmprintk(ioc,
851919d8a3fSJoe Perches 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
852919d8a3fSJoe Perches 					   device_handle));
853c696f7b8SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
854c696f7b8SSuganath Prabu Subramani 			ret = -EINVAL;
855c696f7b8SSuganath Prabu Subramani 			goto out;
856c696f7b8SSuganath Prabu Subramani 		}
857f92363d1SSreekanth Reddy 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
858f92363d1SSreekanth Reddy 		    data_in_dma, data_in_sz);
859f92363d1SSreekanth Reddy 		if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
86081c16f83SSuganath Prabu Subramani 			ioc->put_smid_scsi_io(ioc, smid, device_handle);
861f92363d1SSreekanth Reddy 		else
862078a4cc1SSuganath Prabu S 			ioc->put_smid_default(ioc, smid);
863f92363d1SSreekanth Reddy 		break;
864f92363d1SSreekanth Reddy 	}
865f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SCSI_TASK_MGMT:
866f92363d1SSreekanth Reddy 	{
867f92363d1SSreekanth Reddy 		Mpi2SCSITaskManagementRequest_t *tm_request =
868f92363d1SSreekanth Reddy 		    (Mpi2SCSITaskManagementRequest_t *)request;
869f92363d1SSreekanth Reddy 
870919d8a3fSJoe Perches 		dtmprintk(ioc,
871919d8a3fSJoe Perches 			  ioc_info(ioc, "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
872919d8a3fSJoe Perches 				   le16_to_cpu(tm_request->DevHandle),
873919d8a3fSJoe Perches 				   tm_request->TaskType));
874459325c4SChaitra P B 		ioc->got_task_abort_from_ioctl = 1;
875f92363d1SSreekanth Reddy 		if (tm_request->TaskType ==
876f92363d1SSreekanth Reddy 		    MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
877f92363d1SSreekanth Reddy 		    tm_request->TaskType ==
878f92363d1SSreekanth Reddy 		    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
879f92363d1SSreekanth Reddy 			if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
880f92363d1SSreekanth Reddy 				mpt3sas_base_free_smid(ioc, smid);
881459325c4SChaitra P B 				ioc->got_task_abort_from_ioctl = 0;
882f92363d1SSreekanth Reddy 				goto out;
883f92363d1SSreekanth Reddy 			}
884f92363d1SSreekanth Reddy 		}
885459325c4SChaitra P B 		ioc->got_task_abort_from_ioctl = 0;
886f92363d1SSreekanth Reddy 
887c696f7b8SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
888919d8a3fSJoe Perches 			dtmprintk(ioc,
889919d8a3fSJoe Perches 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
890919d8a3fSJoe Perches 					   device_handle));
891c696f7b8SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
892c696f7b8SSuganath Prabu Subramani 			ret = -EINVAL;
893c696f7b8SSuganath Prabu Subramani 			goto out;
894c696f7b8SSuganath Prabu Subramani 		}
895f92363d1SSreekanth Reddy 		mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
896f92363d1SSreekanth Reddy 		    tm_request->DevHandle));
897f92363d1SSreekanth Reddy 		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
898f92363d1SSreekanth Reddy 		    data_in_dma, data_in_sz);
899078a4cc1SSuganath Prabu S 		ioc->put_smid_hi_priority(ioc, smid, 0);
900f92363d1SSreekanth Reddy 		break;
901f92363d1SSreekanth Reddy 	}
902f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SMP_PASSTHROUGH:
903f92363d1SSreekanth Reddy 	{
904f92363d1SSreekanth Reddy 		Mpi2SmpPassthroughRequest_t *smp_request =
905f92363d1SSreekanth Reddy 		    (Mpi2SmpPassthroughRequest_t *)mpi_request;
906f92363d1SSreekanth Reddy 		u8 *data;
907f92363d1SSreekanth Reddy 
908324c122fSSreekanth Reddy 		if (!ioc->multipath_on_hba) {
909f92363d1SSreekanth Reddy 			/* ioc determines which port to use */
910f92363d1SSreekanth Reddy 			smp_request->PhysicalPort = 0xFF;
911324c122fSSreekanth Reddy 		}
912f92363d1SSreekanth Reddy 		if (smp_request->PassthroughFlags &
913f92363d1SSreekanth Reddy 		    MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
914f92363d1SSreekanth Reddy 			data = (u8 *)&smp_request->SGL;
915f92363d1SSreekanth Reddy 		else {
916f92363d1SSreekanth Reddy 			if (unlikely(data_out == NULL)) {
917f92363d1SSreekanth Reddy 				pr_err("failure at %s:%d/%s()!\n",
918f92363d1SSreekanth Reddy 				    __FILE__, __LINE__, __func__);
919f92363d1SSreekanth Reddy 				mpt3sas_base_free_smid(ioc, smid);
920f92363d1SSreekanth Reddy 				ret = -EINVAL;
921f92363d1SSreekanth Reddy 				goto out;
922f92363d1SSreekanth Reddy 			}
923f92363d1SSreekanth Reddy 			data = data_out;
924f92363d1SSreekanth Reddy 		}
925f92363d1SSreekanth Reddy 
926f92363d1SSreekanth Reddy 		if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
927f92363d1SSreekanth Reddy 			ioc->ioc_link_reset_in_progress = 1;
928f92363d1SSreekanth Reddy 			ioc->ignore_loginfos = 1;
929f92363d1SSreekanth Reddy 		}
930f92363d1SSreekanth Reddy 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
931f92363d1SSreekanth Reddy 		    data_in_sz);
932078a4cc1SSuganath Prabu S 		ioc->put_smid_default(ioc, smid);
933f92363d1SSreekanth Reddy 		break;
934f92363d1SSreekanth Reddy 	}
935f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SATA_PASSTHROUGH:
936c696f7b8SSuganath Prabu Subramani 	{
937c696f7b8SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
938919d8a3fSJoe Perches 			dtmprintk(ioc,
939919d8a3fSJoe Perches 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
940919d8a3fSJoe Perches 					   device_handle));
941c696f7b8SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
942c696f7b8SSuganath Prabu Subramani 			ret = -EINVAL;
943c696f7b8SSuganath Prabu Subramani 			goto out;
944c696f7b8SSuganath Prabu Subramani 		}
945c696f7b8SSuganath Prabu Subramani 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
946c696f7b8SSuganath Prabu Subramani 		    data_in_sz);
947078a4cc1SSuganath Prabu S 		ioc->put_smid_default(ioc, smid);
948c696f7b8SSuganath Prabu Subramani 		break;
949c696f7b8SSuganath Prabu Subramani 	}
950f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_FW_DOWNLOAD:
951*f45faddeSBradley Grove 	{
952*f45faddeSBradley Grove 		if (ioc->pdev->vendor == MPI2_MFGPAGE_VENDORID_ATTO) {
953*f45faddeSBradley Grove 			ioc_info(ioc, "Firmware download not supported for ATTO HBA.\n");
954*f45faddeSBradley Grove 			ret = -EPERM;
955*f45faddeSBradley Grove 			break;
956*f45faddeSBradley Grove 		}
957*f45faddeSBradley Grove 		fallthrough;
958*f45faddeSBradley Grove 	}
959f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_FW_UPLOAD:
960f92363d1SSreekanth Reddy 	{
961f92363d1SSreekanth Reddy 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
962f92363d1SSreekanth Reddy 		    data_in_sz);
963078a4cc1SSuganath Prabu S 		ioc->put_smid_default(ioc, smid);
964f92363d1SSreekanth Reddy 		break;
965f92363d1SSreekanth Reddy 	}
966f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_TOOLBOX:
967f92363d1SSreekanth Reddy 	{
968f92363d1SSreekanth Reddy 		Mpi2ToolboxCleanRequest_t *toolbox_request =
969f92363d1SSreekanth Reddy 			(Mpi2ToolboxCleanRequest_t *)mpi_request;
970f92363d1SSreekanth Reddy 
971f23ca2cbSSuganath Prabu 		if ((toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
972f23ca2cbSSuganath Prabu 		    || (toolbox_request->Tool ==
973f23ca2cbSSuganath Prabu 		    MPI26_TOOLBOX_BACKEND_PCIE_LANE_MARGIN))
974f92363d1SSreekanth Reddy 			ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
975f92363d1SSreekanth Reddy 				data_in_dma, data_in_sz);
976ba630ea0SSuganath Prabu 		else if (toolbox_request->Tool ==
977ba630ea0SSuganath Prabu 				MPI2_TOOLBOX_MEMORY_MOVE_TOOL) {
978ba630ea0SSuganath Prabu 			Mpi2ToolboxMemMoveRequest_t *mem_move_request =
979ba630ea0SSuganath Prabu 					(Mpi2ToolboxMemMoveRequest_t *)request;
980ba630ea0SSuganath Prabu 			Mpi2SGESimple64_t tmp, *src = NULL, *dst = NULL;
981ba630ea0SSuganath Prabu 
982ba630ea0SSuganath Prabu 			ioc->build_sg_mpi(ioc, psge, data_out_dma,
983ba630ea0SSuganath Prabu 					data_out_sz, data_in_dma, data_in_sz);
984ba630ea0SSuganath Prabu 			if (data_out_sz && !data_in_sz) {
985ba630ea0SSuganath Prabu 				dst =
986ba630ea0SSuganath Prabu 				    (Mpi2SGESimple64_t *)&mem_move_request->SGL;
987ba630ea0SSuganath Prabu 				src = (void *)dst + ioc->sge_size;
988ba630ea0SSuganath Prabu 
989ba630ea0SSuganath Prabu 				memcpy(&tmp, src, ioc->sge_size);
990ba630ea0SSuganath Prabu 				memcpy(src, dst, ioc->sge_size);
991ba630ea0SSuganath Prabu 				memcpy(dst, &tmp, ioc->sge_size);
992ba630ea0SSuganath Prabu 			}
993ba630ea0SSuganath Prabu 			if (ioc->logging_level & MPT_DEBUG_TM) {
994ba630ea0SSuganath Prabu 				ioc_info(ioc,
995ba630ea0SSuganath Prabu 				  "Mpi2ToolboxMemMoveRequest_t request msg\n");
996ba630ea0SSuganath Prabu 				_debug_dump_mf(mem_move_request,
997ba630ea0SSuganath Prabu 							ioc->request_sz/4);
998ba630ea0SSuganath Prabu 			}
999ba630ea0SSuganath Prabu 		} else
1000f92363d1SSreekanth Reddy 			ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
1001f92363d1SSreekanth Reddy 			    data_in_dma, data_in_sz);
1002078a4cc1SSuganath Prabu S 		ioc->put_smid_default(ioc, smid);
1003f92363d1SSreekanth Reddy 		break;
1004f92363d1SSreekanth Reddy 	}
1005f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
1006f92363d1SSreekanth Reddy 	{
1007f92363d1SSreekanth Reddy 		Mpi2SasIoUnitControlRequest_t *sasiounit_request =
1008f92363d1SSreekanth Reddy 		    (Mpi2SasIoUnitControlRequest_t *)mpi_request;
1009f92363d1SSreekanth Reddy 
1010f92363d1SSreekanth Reddy 		if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
1011f92363d1SSreekanth Reddy 		    || sasiounit_request->Operation ==
1012f92363d1SSreekanth Reddy 		    MPI2_SAS_OP_PHY_LINK_RESET) {
1013f92363d1SSreekanth Reddy 			ioc->ioc_link_reset_in_progress = 1;
1014f92363d1SSreekanth Reddy 			ioc->ignore_loginfos = 1;
1015f92363d1SSreekanth Reddy 		}
1016f92363d1SSreekanth Reddy 		/* drop to default case for posting the request */
1017f92363d1SSreekanth Reddy 	}
1018df561f66SGustavo A. R. Silva 		fallthrough;
1019f92363d1SSreekanth Reddy 	default:
1020f92363d1SSreekanth Reddy 		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
1021f92363d1SSreekanth Reddy 		    data_in_dma, data_in_sz);
1022078a4cc1SSuganath Prabu S 		ioc->put_smid_default(ioc, smid);
1023f92363d1SSreekanth Reddy 		break;
1024f92363d1SSreekanth Reddy 	}
1025f92363d1SSreekanth Reddy 
1026f92363d1SSreekanth Reddy 	if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
1027f92363d1SSreekanth Reddy 		timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
1028f92363d1SSreekanth Reddy 	else
1029f92363d1SSreekanth Reddy 		timeout = karg.timeout;
10308bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
1031f92363d1SSreekanth Reddy 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
1032f92363d1SSreekanth Reddy 		Mpi2SCSITaskManagementRequest_t *tm_request =
1033f92363d1SSreekanth Reddy 		    (Mpi2SCSITaskManagementRequest_t *)mpi_request;
1034f92363d1SSreekanth Reddy 		mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
1035f92363d1SSreekanth Reddy 		    tm_request->DevHandle));
1036f92363d1SSreekanth Reddy 		mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
1037f92363d1SSreekanth Reddy 	} else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
1038f92363d1SSreekanth Reddy 	    mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
1039f92363d1SSreekanth Reddy 		ioc->ioc_link_reset_in_progress) {
1040f92363d1SSreekanth Reddy 		ioc->ioc_link_reset_in_progress = 0;
1041f92363d1SSreekanth Reddy 		ioc->ignore_loginfos = 0;
1042f92363d1SSreekanth Reddy 	}
1043f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1044c6bdb6a1SSreekanth Reddy 		mpt3sas_check_cmd_timeout(ioc,
1045d37306caSChaitra P B 		    ioc->ctl_cmds.status, mpi_request,
1046c6bdb6a1SSreekanth Reddy 		    karg.data_sge_offset, issue_reset);
1047f92363d1SSreekanth Reddy 		goto issue_host_reset;
1048f92363d1SSreekanth Reddy 	}
1049f92363d1SSreekanth Reddy 
1050f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
1051f92363d1SSreekanth Reddy 
1052f92363d1SSreekanth Reddy 	if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
1053f92363d1SSreekanth Reddy 	    (ioc->logging_level & MPT_DEBUG_TM)) {
1054f92363d1SSreekanth Reddy 		Mpi2SCSITaskManagementReply_t *tm_reply =
1055f92363d1SSreekanth Reddy 		    (Mpi2SCSITaskManagementReply_t *)mpi_reply;
1056f92363d1SSreekanth Reddy 
1057919d8a3fSJoe Perches 		ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
1058f92363d1SSreekanth Reddy 			 le16_to_cpu(tm_reply->IOCStatus),
1059f92363d1SSreekanth Reddy 			 le32_to_cpu(tm_reply->IOCLogInfo),
1060f92363d1SSreekanth Reddy 			 le32_to_cpu(tm_reply->TerminationCount));
1061f92363d1SSreekanth Reddy 	}
1062af009411SSreekanth Reddy 
1063f92363d1SSreekanth Reddy 	/* copy out xdata to user */
1064f92363d1SSreekanth Reddy 	if (data_in_sz) {
1065f92363d1SSreekanth Reddy 		if (copy_to_user(karg.data_in_buf_ptr, data_in,
1066f92363d1SSreekanth Reddy 		    data_in_sz)) {
1067f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1068f92363d1SSreekanth Reddy 			    __LINE__, __func__);
1069f92363d1SSreekanth Reddy 			ret = -ENODATA;
1070f92363d1SSreekanth Reddy 			goto out;
1071f92363d1SSreekanth Reddy 		}
1072f92363d1SSreekanth Reddy 	}
1073f92363d1SSreekanth Reddy 
1074f92363d1SSreekanth Reddy 	/* copy out reply message frame to user */
1075f92363d1SSreekanth Reddy 	if (karg.max_reply_bytes) {
1076f92363d1SSreekanth Reddy 		sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1077f92363d1SSreekanth Reddy 		if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1078f92363d1SSreekanth Reddy 		    sz)) {
1079f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1080f92363d1SSreekanth Reddy 			    __LINE__, __func__);
1081f92363d1SSreekanth Reddy 			ret = -ENODATA;
1082f92363d1SSreekanth Reddy 			goto out;
1083f92363d1SSreekanth Reddy 		}
1084f92363d1SSreekanth Reddy 	}
1085f92363d1SSreekanth Reddy 
1086aff39e61SSuganath Prabu Subramani 	/* copy out sense/NVMe Error Response to user */
1087f92363d1SSreekanth Reddy 	if (karg.max_sense_bytes && (mpi_request->Function ==
1088f92363d1SSreekanth Reddy 	    MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
1089aff39e61SSuganath Prabu Subramani 	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1090aff39e61SSuganath Prabu Subramani 	    MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1091aff39e61SSuganath Prabu Subramani 		if (karg.sense_data_ptr == NULL) {
1092919d8a3fSJoe Perches 			ioc_info(ioc, "Response buffer provided by application is NULL; Response data will not be returned\n");
1093aff39e61SSuganath Prabu Subramani 			goto out;
1094aff39e61SSuganath Prabu Subramani 		}
1095aff39e61SSuganath Prabu Subramani 		sz_arg = (mpi_request->Function ==
1096aff39e61SSuganath Prabu Subramani 		MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1097aff39e61SSuganath Prabu Subramani 							SCSI_SENSE_BUFFERSIZE;
1098aff39e61SSuganath Prabu Subramani 		sz = min_t(u32, karg.max_sense_bytes, sz_arg);
1099f92363d1SSreekanth Reddy 		if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1100f92363d1SSreekanth Reddy 		    sz)) {
1101f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1102f92363d1SSreekanth Reddy 				__LINE__, __func__);
1103f92363d1SSreekanth Reddy 			ret = -ENODATA;
1104f92363d1SSreekanth Reddy 			goto out;
1105f92363d1SSreekanth Reddy 		}
1106f92363d1SSreekanth Reddy 	}
1107f92363d1SSreekanth Reddy 
1108f92363d1SSreekanth Reddy  issue_host_reset:
1109f92363d1SSreekanth Reddy 	if (issue_reset) {
1110f92363d1SSreekanth Reddy 		ret = -ENODATA;
1111f92363d1SSreekanth Reddy 		if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1112f92363d1SSreekanth Reddy 		    mpi_request->Function ==
1113f92363d1SSreekanth Reddy 		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1114f92363d1SSreekanth Reddy 		    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
1115919d8a3fSJoe Perches 			ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
1116f92363d1SSreekanth Reddy 				 le16_to_cpu(mpi_request->FunctionDependent1));
1117f92363d1SSreekanth Reddy 			mpt3sas_halt_firmware(ioc);
1118c1a6c5acSChaitra P B 			pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1119c1a6c5acSChaitra P B 				le16_to_cpu(mpi_request->FunctionDependent1));
11205bb309dbSSuganath Prabu 			if (pcie_device && (!ioc->tm_custom_handling) &&
11215bb309dbSSuganath Prabu 			    (!(mpt3sas_scsih_is_pcie_scsi_device(
11225bb309dbSSuganath Prabu 			    pcie_device->device_info))))
112396902835SCalvin Owens 				mpt3sas_scsih_issue_locked_tm(ioc,
1124c1a6c5acSChaitra P B 				  le16_to_cpu(mpi_request->FunctionDependent1),
1125521e9c0bSSuganath Prabu S 				  0, 0, 0,
1126521e9c0bSSuganath Prabu S 				  MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1127c1a6c5acSChaitra P B 				  0, pcie_device->reset_timeout,
11285bb309dbSSuganath Prabu 			MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE);
1129c1a6c5acSChaitra P B 			else
1130c1a6c5acSChaitra P B 				mpt3sas_scsih_issue_locked_tm(ioc,
1131c1a6c5acSChaitra P B 				  le16_to_cpu(mpi_request->FunctionDependent1),
1132521e9c0bSSuganath Prabu S 				  0, 0, 0,
1133521e9c0bSSuganath Prabu S 				  MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1134c1a6c5acSChaitra P B 				  0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
1135f92363d1SSreekanth Reddy 		} else
113698c56ad3SCalvin Owens 			mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1137f92363d1SSreekanth Reddy 	}
1138f92363d1SSreekanth Reddy 
1139f92363d1SSreekanth Reddy  out:
1140c1a6c5acSChaitra P B 	if (pcie_device)
1141c1a6c5acSChaitra P B 		pcie_device_put(pcie_device);
1142f92363d1SSreekanth Reddy 
1143f92363d1SSreekanth Reddy 	/* free memory associated with sg buffers */
1144f92363d1SSreekanth Reddy 	if (data_in)
11451c2048bdSChristoph Hellwig 		dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
1146f92363d1SSreekanth Reddy 		    data_in_dma);
1147f92363d1SSreekanth Reddy 
1148f92363d1SSreekanth Reddy 	if (data_out)
11491c2048bdSChristoph Hellwig 		dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
1150f92363d1SSreekanth Reddy 		    data_out_dma);
1151f92363d1SSreekanth Reddy 
1152f92363d1SSreekanth Reddy 	kfree(mpi_request);
1153f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1154f92363d1SSreekanth Reddy 	return ret;
1155f92363d1SSreekanth Reddy }
1156f92363d1SSreekanth Reddy 
1157f92363d1SSreekanth Reddy /**
1158f92363d1SSreekanth Reddy  * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1159f92363d1SSreekanth Reddy  * @ioc: per adapter object
11604beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1161f92363d1SSreekanth Reddy  */
1162f92363d1SSreekanth Reddy static long
1163f92363d1SSreekanth Reddy _ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1164f92363d1SSreekanth Reddy {
1165f92363d1SSreekanth Reddy 	struct mpt3_ioctl_iocinfo karg;
1166f92363d1SSreekanth Reddy 
1167919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1168f92363d1SSreekanth Reddy 				 __func__));
1169f92363d1SSreekanth Reddy 
1170f92363d1SSreekanth Reddy 	memset(&karg, 0 , sizeof(karg));
1171f92363d1SSreekanth Reddy 	if (ioc->pfacts)
1172f92363d1SSreekanth Reddy 		karg.port_number = ioc->pfacts[0].PortNumber;
1173f92363d1SSreekanth Reddy 	karg.hw_rev = ioc->pdev->revision;
1174f92363d1SSreekanth Reddy 	karg.pci_id = ioc->pdev->device;
1175f92363d1SSreekanth Reddy 	karg.subsystem_device = ioc->pdev->subsystem_device;
1176f92363d1SSreekanth Reddy 	karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1177f92363d1SSreekanth Reddy 	karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1178f92363d1SSreekanth Reddy 	karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1179f92363d1SSreekanth Reddy 	karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1180f92363d1SSreekanth Reddy 	karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1181f92363d1SSreekanth Reddy 	karg.firmware_version = ioc->facts.FWVersion.Word;
1182c84b06a4SSreekanth Reddy 	strcpy(karg.driver_version, ioc->driver_name);
1183f92363d1SSreekanth Reddy 	strcat(karg.driver_version, "-");
1184d357e84dSSreekanth Reddy 	switch  (ioc->hba_mpi_version_belonged) {
1185d357e84dSSreekanth Reddy 	case MPI2_VERSION:
11867786ab6aSSreekanth Reddy 		if (ioc->is_warpdrive)
11877786ab6aSSreekanth Reddy 			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
11887786ab6aSSreekanth Reddy 		else
1189d357e84dSSreekanth Reddy 			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1190d357e84dSSreekanth Reddy 		strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1191d357e84dSSreekanth Reddy 		break;
1192d357e84dSSreekanth Reddy 	case MPI25_VERSION:
1193b130b0d5SSuganath prabu Subramani 	case MPI26_VERSION:
1194998f26aeSSuganath Prabu Subramani 		if (ioc->is_gen35_ioc)
1195998f26aeSSuganath Prabu Subramani 			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1196998f26aeSSuganath Prabu Subramani 		else
1197d357e84dSSreekanth Reddy 			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1198d357e84dSSreekanth Reddy 		strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1199d357e84dSSreekanth Reddy 		break;
1200d357e84dSSreekanth Reddy 	}
1201f92363d1SSreekanth Reddy 	karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1202f92363d1SSreekanth Reddy 
1203f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1204f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1205f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1206f92363d1SSreekanth Reddy 		return -EFAULT;
1207f92363d1SSreekanth Reddy 	}
1208f92363d1SSreekanth Reddy 	return 0;
1209f92363d1SSreekanth Reddy }
1210f92363d1SSreekanth Reddy 
1211f92363d1SSreekanth Reddy /**
1212f92363d1SSreekanth Reddy  * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1213f92363d1SSreekanth Reddy  * @ioc: per adapter object
12144beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1215f92363d1SSreekanth Reddy  */
1216f92363d1SSreekanth Reddy static long
1217f92363d1SSreekanth Reddy _ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1218f92363d1SSreekanth Reddy {
1219f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventquery karg;
1220f92363d1SSreekanth Reddy 
1221f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, 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 
1227919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1228f92363d1SSreekanth Reddy 				 __func__));
1229f92363d1SSreekanth Reddy 
1230f92363d1SSreekanth Reddy 	karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1231f92363d1SSreekanth Reddy 	memcpy(karg.event_types, ioc->event_type,
1232f92363d1SSreekanth Reddy 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1233f92363d1SSreekanth Reddy 
1234f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1235f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1236f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1237f92363d1SSreekanth Reddy 		return -EFAULT;
1238f92363d1SSreekanth Reddy 	}
1239f92363d1SSreekanth Reddy 	return 0;
1240f92363d1SSreekanth Reddy }
1241f92363d1SSreekanth Reddy 
1242f92363d1SSreekanth Reddy /**
1243f92363d1SSreekanth Reddy  * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1244f92363d1SSreekanth Reddy  * @ioc: per adapter object
12454beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1246f92363d1SSreekanth Reddy  */
1247f92363d1SSreekanth Reddy static long
1248f92363d1SSreekanth Reddy _ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1249f92363d1SSreekanth Reddy {
1250f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventenable karg;
1251f92363d1SSreekanth Reddy 
1252f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1253f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1254f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1255f92363d1SSreekanth Reddy 		return -EFAULT;
1256f92363d1SSreekanth Reddy 	}
1257f92363d1SSreekanth Reddy 
1258919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1259f92363d1SSreekanth Reddy 				 __func__));
1260f92363d1SSreekanth Reddy 
1261f92363d1SSreekanth Reddy 	memcpy(ioc->event_type, karg.event_types,
1262f92363d1SSreekanth Reddy 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1263f92363d1SSreekanth Reddy 	mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1264f92363d1SSreekanth Reddy 
1265f92363d1SSreekanth Reddy 	if (ioc->event_log)
1266f92363d1SSreekanth Reddy 		return 0;
1267f92363d1SSreekanth Reddy 	/* initialize event_log */
1268f92363d1SSreekanth Reddy 	ioc->event_context = 0;
1269f92363d1SSreekanth Reddy 	ioc->aen_event_read_flag = 0;
1270f92363d1SSreekanth Reddy 	ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1271f92363d1SSreekanth Reddy 	    sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1272f92363d1SSreekanth Reddy 	if (!ioc->event_log) {
1273f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1274f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1275f92363d1SSreekanth Reddy 		return -ENOMEM;
1276f92363d1SSreekanth Reddy 	}
1277f92363d1SSreekanth Reddy 	return 0;
1278f92363d1SSreekanth Reddy }
1279f92363d1SSreekanth Reddy 
1280f92363d1SSreekanth Reddy /**
1281f92363d1SSreekanth Reddy  * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1282f92363d1SSreekanth Reddy  * @ioc: per adapter object
12834beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1284f92363d1SSreekanth Reddy  */
1285f92363d1SSreekanth Reddy static long
1286f92363d1SSreekanth Reddy _ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1287f92363d1SSreekanth Reddy {
1288f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventreport karg;
1289f92363d1SSreekanth Reddy 	u32 number_bytes, max_events, max;
1290f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventreport __user *uarg = arg;
1291f92363d1SSreekanth Reddy 
1292f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1293f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1294f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1295f92363d1SSreekanth Reddy 		return -EFAULT;
1296f92363d1SSreekanth Reddy 	}
1297f92363d1SSreekanth Reddy 
1298919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1299f92363d1SSreekanth Reddy 				 __func__));
1300f92363d1SSreekanth Reddy 
1301f92363d1SSreekanth Reddy 	number_bytes = karg.hdr.max_data_size -
1302f92363d1SSreekanth Reddy 	    sizeof(struct mpt3_ioctl_header);
1303f92363d1SSreekanth Reddy 	max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1304f92363d1SSreekanth Reddy 	max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1305f92363d1SSreekanth Reddy 
1306f92363d1SSreekanth Reddy 	/* If fewer than 1 event is requested, there must have
1307f92363d1SSreekanth Reddy 	 * been some type of error.
1308f92363d1SSreekanth Reddy 	 */
1309f92363d1SSreekanth Reddy 	if (!max || !ioc->event_log)
1310f92363d1SSreekanth Reddy 		return -ENODATA;
1311f92363d1SSreekanth Reddy 
1312f92363d1SSreekanth Reddy 	number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1313f92363d1SSreekanth Reddy 	if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1314f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1315f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1316f92363d1SSreekanth Reddy 		return -EFAULT;
1317f92363d1SSreekanth Reddy 	}
1318f92363d1SSreekanth Reddy 
1319f92363d1SSreekanth Reddy 	/* reset flag so SIGIO can restart */
1320f92363d1SSreekanth Reddy 	ioc->aen_event_read_flag = 0;
1321f92363d1SSreekanth Reddy 	return 0;
1322f92363d1SSreekanth Reddy }
1323f92363d1SSreekanth Reddy 
1324f92363d1SSreekanth Reddy /**
1325f92363d1SSreekanth Reddy  * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1326f92363d1SSreekanth Reddy  * @ioc: per adapter object
13274beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1328f92363d1SSreekanth Reddy  */
1329f92363d1SSreekanth Reddy static long
1330f92363d1SSreekanth Reddy _ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1331f92363d1SSreekanth Reddy {
1332f92363d1SSreekanth Reddy 	struct mpt3_ioctl_diag_reset karg;
1333f92363d1SSreekanth Reddy 	int retval;
1334f92363d1SSreekanth Reddy 
1335f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1336f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1337f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1338f92363d1SSreekanth Reddy 		return -EFAULT;
1339f92363d1SSreekanth Reddy 	}
1340f92363d1SSreekanth Reddy 
1341f92363d1SSreekanth Reddy 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
1342f92363d1SSreekanth Reddy 	    ioc->is_driver_loading)
1343f92363d1SSreekanth Reddy 		return -EAGAIN;
1344f92363d1SSreekanth Reddy 
1345919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1346f92363d1SSreekanth Reddy 				 __func__));
1347f92363d1SSreekanth Reddy 
1348688c1a0aSSuganath Prabu S 	ioc->reset_from_user = 1;
134998c56ad3SCalvin Owens 	retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
13505b061980SSreekanth Reddy 	ioc_info(ioc,
13515b061980SSreekanth Reddy 	    "Ioctl: host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
1352f92363d1SSreekanth Reddy 	return 0;
1353f92363d1SSreekanth Reddy }
1354f92363d1SSreekanth Reddy 
1355f92363d1SSreekanth Reddy /**
1356f92363d1SSreekanth Reddy  * _ctl_btdh_search_sas_device - searching for sas device
1357f92363d1SSreekanth Reddy  * @ioc: per adapter object
1358f92363d1SSreekanth Reddy  * @btdh: btdh ioctl payload
1359f92363d1SSreekanth Reddy  */
1360f92363d1SSreekanth Reddy static int
1361f92363d1SSreekanth Reddy _ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1362f92363d1SSreekanth Reddy 	struct mpt3_ioctl_btdh_mapping *btdh)
1363f92363d1SSreekanth Reddy {
1364f92363d1SSreekanth Reddy 	struct _sas_device *sas_device;
1365f92363d1SSreekanth Reddy 	unsigned long flags;
1366f92363d1SSreekanth Reddy 	int rc = 0;
1367f92363d1SSreekanth Reddy 
1368f92363d1SSreekanth Reddy 	if (list_empty(&ioc->sas_device_list))
1369f92363d1SSreekanth Reddy 		return rc;
1370f92363d1SSreekanth Reddy 
1371f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1372f92363d1SSreekanth Reddy 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1373f92363d1SSreekanth Reddy 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1374f92363d1SSreekanth Reddy 		    btdh->handle == sas_device->handle) {
1375f92363d1SSreekanth Reddy 			btdh->bus = sas_device->channel;
1376f92363d1SSreekanth Reddy 			btdh->id = sas_device->id;
1377f92363d1SSreekanth Reddy 			rc = 1;
1378f92363d1SSreekanth Reddy 			goto out;
1379f92363d1SSreekanth Reddy 		} else if (btdh->bus == sas_device->channel && btdh->id ==
1380f92363d1SSreekanth Reddy 		    sas_device->id && btdh->handle == 0xFFFF) {
1381f92363d1SSreekanth Reddy 			btdh->handle = sas_device->handle;
1382f92363d1SSreekanth Reddy 			rc = 1;
1383f92363d1SSreekanth Reddy 			goto out;
1384f92363d1SSreekanth Reddy 		}
1385f92363d1SSreekanth Reddy 	}
1386f92363d1SSreekanth Reddy  out:
1387f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1388f92363d1SSreekanth Reddy 	return rc;
1389f92363d1SSreekanth Reddy }
1390f92363d1SSreekanth Reddy 
1391f92363d1SSreekanth Reddy /**
139245aa6a1aSSuganath Prabu Subramani  * _ctl_btdh_search_pcie_device - searching for pcie device
139345aa6a1aSSuganath Prabu Subramani  * @ioc: per adapter object
139445aa6a1aSSuganath Prabu Subramani  * @btdh: btdh ioctl payload
139545aa6a1aSSuganath Prabu Subramani  */
139645aa6a1aSSuganath Prabu Subramani static int
139745aa6a1aSSuganath Prabu Subramani _ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
139845aa6a1aSSuganath Prabu Subramani 	struct mpt3_ioctl_btdh_mapping *btdh)
139945aa6a1aSSuganath Prabu Subramani {
140045aa6a1aSSuganath Prabu Subramani 	struct _pcie_device *pcie_device;
140145aa6a1aSSuganath Prabu Subramani 	unsigned long flags;
140245aa6a1aSSuganath Prabu Subramani 	int rc = 0;
140345aa6a1aSSuganath Prabu Subramani 
140445aa6a1aSSuganath Prabu Subramani 	if (list_empty(&ioc->pcie_device_list))
140545aa6a1aSSuganath Prabu Subramani 		return rc;
140645aa6a1aSSuganath Prabu Subramani 
140745aa6a1aSSuganath Prabu Subramani 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
140845aa6a1aSSuganath Prabu Subramani 	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
140945aa6a1aSSuganath Prabu Subramani 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
141045aa6a1aSSuganath Prabu Subramani 			   btdh->handle == pcie_device->handle) {
141145aa6a1aSSuganath Prabu Subramani 			btdh->bus = pcie_device->channel;
141245aa6a1aSSuganath Prabu Subramani 			btdh->id = pcie_device->id;
141345aa6a1aSSuganath Prabu Subramani 			rc = 1;
141445aa6a1aSSuganath Prabu Subramani 			goto out;
141545aa6a1aSSuganath Prabu Subramani 		} else if (btdh->bus == pcie_device->channel && btdh->id ==
141645aa6a1aSSuganath Prabu Subramani 			   pcie_device->id && btdh->handle == 0xFFFF) {
141745aa6a1aSSuganath Prabu Subramani 			btdh->handle = pcie_device->handle;
141845aa6a1aSSuganath Prabu Subramani 			rc = 1;
141945aa6a1aSSuganath Prabu Subramani 			goto out;
142045aa6a1aSSuganath Prabu Subramani 		}
142145aa6a1aSSuganath Prabu Subramani 	}
142245aa6a1aSSuganath Prabu Subramani  out:
142345aa6a1aSSuganath Prabu Subramani 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
142445aa6a1aSSuganath Prabu Subramani 	return rc;
142545aa6a1aSSuganath Prabu Subramani }
142645aa6a1aSSuganath Prabu Subramani 
142745aa6a1aSSuganath Prabu Subramani /**
1428f92363d1SSreekanth Reddy  * _ctl_btdh_search_raid_device - searching for raid device
1429f92363d1SSreekanth Reddy  * @ioc: per adapter object
1430f92363d1SSreekanth Reddy  * @btdh: btdh ioctl payload
1431f92363d1SSreekanth Reddy  */
1432f92363d1SSreekanth Reddy static int
1433f92363d1SSreekanth Reddy _ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1434f92363d1SSreekanth Reddy 	struct mpt3_ioctl_btdh_mapping *btdh)
1435f92363d1SSreekanth Reddy {
1436f92363d1SSreekanth Reddy 	struct _raid_device *raid_device;
1437f92363d1SSreekanth Reddy 	unsigned long flags;
1438f92363d1SSreekanth Reddy 	int rc = 0;
1439f92363d1SSreekanth Reddy 
1440f92363d1SSreekanth Reddy 	if (list_empty(&ioc->raid_device_list))
1441f92363d1SSreekanth Reddy 		return rc;
1442f92363d1SSreekanth Reddy 
1443f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1444f92363d1SSreekanth Reddy 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1445f92363d1SSreekanth Reddy 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1446f92363d1SSreekanth Reddy 		    btdh->handle == raid_device->handle) {
1447f92363d1SSreekanth Reddy 			btdh->bus = raid_device->channel;
1448f92363d1SSreekanth Reddy 			btdh->id = raid_device->id;
1449f92363d1SSreekanth Reddy 			rc = 1;
1450f92363d1SSreekanth Reddy 			goto out;
1451f92363d1SSreekanth Reddy 		} else if (btdh->bus == raid_device->channel && btdh->id ==
1452f92363d1SSreekanth Reddy 		    raid_device->id && btdh->handle == 0xFFFF) {
1453f92363d1SSreekanth Reddy 			btdh->handle = raid_device->handle;
1454f92363d1SSreekanth Reddy 			rc = 1;
1455f92363d1SSreekanth Reddy 			goto out;
1456f92363d1SSreekanth Reddy 		}
1457f92363d1SSreekanth Reddy 	}
1458f92363d1SSreekanth Reddy  out:
1459f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1460f92363d1SSreekanth Reddy 	return rc;
1461f92363d1SSreekanth Reddy }
1462f92363d1SSreekanth Reddy 
1463f92363d1SSreekanth Reddy /**
1464f92363d1SSreekanth Reddy  * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1465f92363d1SSreekanth Reddy  * @ioc: per adapter object
14664beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1467f92363d1SSreekanth Reddy  */
1468f92363d1SSreekanth Reddy static long
1469f92363d1SSreekanth Reddy _ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1470f92363d1SSreekanth Reddy {
1471f92363d1SSreekanth Reddy 	struct mpt3_ioctl_btdh_mapping karg;
1472f92363d1SSreekanth Reddy 	int rc;
1473f92363d1SSreekanth Reddy 
1474f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1475f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1476f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1477f92363d1SSreekanth Reddy 		return -EFAULT;
1478f92363d1SSreekanth Reddy 	}
1479f92363d1SSreekanth Reddy 
1480919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1481f92363d1SSreekanth Reddy 				 __func__));
1482f92363d1SSreekanth Reddy 
1483f92363d1SSreekanth Reddy 	rc = _ctl_btdh_search_sas_device(ioc, &karg);
1484f92363d1SSreekanth Reddy 	if (!rc)
148545aa6a1aSSuganath Prabu Subramani 		rc = _ctl_btdh_search_pcie_device(ioc, &karg);
148645aa6a1aSSuganath Prabu Subramani 	if (!rc)
1487f92363d1SSreekanth Reddy 		_ctl_btdh_search_raid_device(ioc, &karg);
1488f92363d1SSreekanth Reddy 
1489f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1490f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1491f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1492f92363d1SSreekanth Reddy 		return -EFAULT;
1493f92363d1SSreekanth Reddy 	}
1494f92363d1SSreekanth Reddy 	return 0;
1495f92363d1SSreekanth Reddy }
1496f92363d1SSreekanth Reddy 
1497f92363d1SSreekanth Reddy /**
1498f92363d1SSreekanth Reddy  * _ctl_diag_capability - return diag buffer capability
1499f92363d1SSreekanth Reddy  * @ioc: per adapter object
1500f92363d1SSreekanth Reddy  * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1501f92363d1SSreekanth Reddy  *
1502f92363d1SSreekanth Reddy  * returns 1 when diag buffer support is enabled in firmware
1503f92363d1SSreekanth Reddy  */
1504f92363d1SSreekanth Reddy static u8
1505f92363d1SSreekanth Reddy _ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1506f92363d1SSreekanth Reddy {
1507f92363d1SSreekanth Reddy 	u8 rc = 0;
1508f92363d1SSreekanth Reddy 
1509f92363d1SSreekanth Reddy 	switch (buffer_type) {
1510f92363d1SSreekanth Reddy 	case MPI2_DIAG_BUF_TYPE_TRACE:
1511f92363d1SSreekanth Reddy 		if (ioc->facts.IOCCapabilities &
1512f92363d1SSreekanth Reddy 		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1513f92363d1SSreekanth Reddy 			rc = 1;
1514f92363d1SSreekanth Reddy 		break;
1515f92363d1SSreekanth Reddy 	case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1516f92363d1SSreekanth Reddy 		if (ioc->facts.IOCCapabilities &
1517f92363d1SSreekanth Reddy 		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1518f92363d1SSreekanth Reddy 			rc = 1;
1519f92363d1SSreekanth Reddy 		break;
1520f92363d1SSreekanth Reddy 	case MPI2_DIAG_BUF_TYPE_EXTENDED:
1521f92363d1SSreekanth Reddy 		if (ioc->facts.IOCCapabilities &
1522f92363d1SSreekanth Reddy 		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1523f92363d1SSreekanth Reddy 			rc = 1;
1524f92363d1SSreekanth Reddy 	}
1525f92363d1SSreekanth Reddy 
1526f92363d1SSreekanth Reddy 	return rc;
1527f92363d1SSreekanth Reddy }
1528f92363d1SSreekanth Reddy 
152908e7378eSSreekanth Reddy /**
153008e7378eSSreekanth Reddy  * _ctl_diag_get_bufftype - return diag buffer type
153108e7378eSSreekanth Reddy  *              either TRACE, SNAPSHOT, or EXTENDED
153208e7378eSSreekanth Reddy  * @ioc: per adapter object
153308e7378eSSreekanth Reddy  * @unique_id: specifies the unique_id for the buffer
153408e7378eSSreekanth Reddy  *
153508e7378eSSreekanth Reddy  * returns MPT3_DIAG_UID_NOT_FOUND if the id not found
153608e7378eSSreekanth Reddy  */
153708e7378eSSreekanth Reddy static u8
153808e7378eSSreekanth Reddy _ctl_diag_get_bufftype(struct MPT3SAS_ADAPTER *ioc, u32 unique_id)
153908e7378eSSreekanth Reddy {
154008e7378eSSreekanth Reddy 	u8  index;
154108e7378eSSreekanth Reddy 
154208e7378eSSreekanth Reddy 	for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
154308e7378eSSreekanth Reddy 		if (ioc->unique_id[index] == unique_id)
154408e7378eSSreekanth Reddy 			return index;
154508e7378eSSreekanth Reddy 	}
154608e7378eSSreekanth Reddy 
154708e7378eSSreekanth Reddy 	return MPT3_DIAG_UID_NOT_FOUND;
154808e7378eSSreekanth Reddy }
1549f92363d1SSreekanth Reddy 
1550f92363d1SSreekanth Reddy /**
1551f92363d1SSreekanth Reddy  * _ctl_diag_register_2 - wrapper for registering diag buffer support
1552f92363d1SSreekanth Reddy  * @ioc: per adapter object
1553f92363d1SSreekanth Reddy  * @diag_register: the diag_register struct passed in from user space
1554f92363d1SSreekanth Reddy  *
1555f92363d1SSreekanth Reddy  */
1556f92363d1SSreekanth Reddy static long
1557f92363d1SSreekanth Reddy _ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1558f92363d1SSreekanth Reddy 	struct mpt3_diag_register *diag_register)
1559f92363d1SSreekanth Reddy {
1560f92363d1SSreekanth Reddy 	int rc, i;
1561f92363d1SSreekanth Reddy 	void *request_data = NULL;
1562f92363d1SSreekanth Reddy 	dma_addr_t request_data_dma;
1563f92363d1SSreekanth Reddy 	u32 request_data_sz = 0;
1564f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostRequest_t *mpi_request;
1565f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostReply_t *mpi_reply;
1566f92363d1SSreekanth Reddy 	u8 buffer_type;
1567f92363d1SSreekanth Reddy 	u16 smid;
1568f92363d1SSreekanth Reddy 	u16 ioc_status;
1569f92363d1SSreekanth Reddy 	u32 ioc_state;
1570f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
1571f92363d1SSreekanth Reddy 
1572919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1573f92363d1SSreekanth Reddy 				 __func__));
1574f92363d1SSreekanth Reddy 
1575f92363d1SSreekanth Reddy 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1576f92363d1SSreekanth Reddy 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1577919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed due to ioc not operational\n",
1578919d8a3fSJoe Perches 			__func__);
1579f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1580f92363d1SSreekanth Reddy 		goto out;
1581f92363d1SSreekanth Reddy 	}
1582f92363d1SSreekanth Reddy 
1583f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1584919d8a3fSJoe Perches 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
1585f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1586f92363d1SSreekanth Reddy 		goto out;
1587f92363d1SSreekanth Reddy 	}
1588f92363d1SSreekanth Reddy 
1589f92363d1SSreekanth Reddy 	buffer_type = diag_register->buffer_type;
1590f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1591919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1592919d8a3fSJoe Perches 			__func__, buffer_type);
1593f92363d1SSreekanth Reddy 		return -EPERM;
1594f92363d1SSreekanth Reddy 	}
1595f92363d1SSreekanth Reddy 
159608e7378eSSreekanth Reddy 	if (diag_register->unique_id == 0) {
159708e7378eSSreekanth Reddy 		ioc_err(ioc,
159808e7378eSSreekanth Reddy 		    "%s: Invalid UID(0x%08x), buffer_type(0x%02x)\n", __func__,
159908e7378eSSreekanth Reddy 		    diag_register->unique_id, buffer_type);
160008e7378eSSreekanth Reddy 		return -EINVAL;
160108e7378eSSreekanth Reddy 	}
160208e7378eSSreekanth Reddy 
1603a8a6cbcdSSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
1604a8a6cbcdSSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_APP_OWNED) &&
1605a8a6cbcdSSreekanth Reddy 	    !(ioc->diag_buffer_status[buffer_type] &
1606a8a6cbcdSSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED)) {
1607a8a6cbcdSSreekanth Reddy 		ioc_err(ioc,
1608a8a6cbcdSSreekanth Reddy 		    "%s: buffer_type(0x%02x) is already registered by application with UID(0x%08x)\n",
1609a8a6cbcdSSreekanth Reddy 		    __func__, buffer_type, ioc->unique_id[buffer_type]);
1610a8a6cbcdSSreekanth Reddy 		return -EINVAL;
1611a8a6cbcdSSreekanth Reddy 	}
1612a8a6cbcdSSreekanth Reddy 
1613f92363d1SSreekanth Reddy 	if (ioc->diag_buffer_status[buffer_type] &
1614f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) {
161508e7378eSSreekanth Reddy 		/*
161608e7378eSSreekanth Reddy 		 * If driver posts buffer initially, then an application wants
161708e7378eSSreekanth Reddy 		 * to Register that buffer (own it) without Releasing first,
161808e7378eSSreekanth Reddy 		 * the application Register command MUST have the same buffer
161908e7378eSSreekanth Reddy 		 * type and size in the Register command (obtained from the
162008e7378eSSreekanth Reddy 		 * Query command). Otherwise that Register command will be
162108e7378eSSreekanth Reddy 		 * failed. If the application has released the buffer but wants
162208e7378eSSreekanth Reddy 		 * to re-register it, it should be allowed as long as the
162308e7378eSSreekanth Reddy 		 * Unique-Id/Size match.
162408e7378eSSreekanth Reddy 		 */
162508e7378eSSreekanth Reddy 
162608e7378eSSreekanth Reddy 		if (ioc->unique_id[buffer_type] == MPT3DIAGBUFFUNIQUEID &&
162708e7378eSSreekanth Reddy 		    ioc->diag_buffer_sz[buffer_type] ==
162808e7378eSSreekanth Reddy 		    diag_register->requested_buffer_size) {
162908e7378eSSreekanth Reddy 
163008e7378eSSreekanth Reddy 			if (!(ioc->diag_buffer_status[buffer_type] &
163108e7378eSSreekanth Reddy 			     MPT3_DIAG_BUFFER_IS_RELEASED)) {
163208e7378eSSreekanth Reddy 				dctlprintk(ioc, ioc_info(ioc,
163308e7378eSSreekanth Reddy 				    "%s: diag_buffer (%d) ownership changed. old-ID(0x%08x), new-ID(0x%08x)\n",
163408e7378eSSreekanth Reddy 				    __func__, buffer_type,
163508e7378eSSreekanth Reddy 				    ioc->unique_id[buffer_type],
163608e7378eSSreekanth Reddy 				    diag_register->unique_id));
163708e7378eSSreekanth Reddy 
163808e7378eSSreekanth Reddy 				/*
163908e7378eSSreekanth Reddy 				 * Application wants to own the buffer with
164008e7378eSSreekanth Reddy 				 * the same size.
164108e7378eSSreekanth Reddy 				 */
164208e7378eSSreekanth Reddy 				ioc->unique_id[buffer_type] =
164308e7378eSSreekanth Reddy 				    diag_register->unique_id;
164408e7378eSSreekanth Reddy 				rc = 0; /* success */
164508e7378eSSreekanth Reddy 				goto out;
164608e7378eSSreekanth Reddy 			}
164708e7378eSSreekanth Reddy 		} else if (ioc->unique_id[buffer_type] !=
164808e7378eSSreekanth Reddy 		    MPT3DIAGBUFFUNIQUEID) {
164908e7378eSSreekanth Reddy 			if (ioc->unique_id[buffer_type] !=
165008e7378eSSreekanth Reddy 			    diag_register->unique_id ||
165108e7378eSSreekanth Reddy 			    ioc->diag_buffer_sz[buffer_type] !=
165208e7378eSSreekanth Reddy 			    diag_register->requested_buffer_size ||
165308e7378eSSreekanth Reddy 			    !(ioc->diag_buffer_status[buffer_type] &
165408e7378eSSreekanth Reddy 			    MPT3_DIAG_BUFFER_IS_RELEASED)) {
165508e7378eSSreekanth Reddy 				ioc_err(ioc,
165608e7378eSSreekanth Reddy 				    "%s: already has a registered buffer for buffer_type(0x%02x)\n",
165708e7378eSSreekanth Reddy 				    __func__, buffer_type);
165808e7378eSSreekanth Reddy 				return -EINVAL;
165908e7378eSSreekanth Reddy 			}
166008e7378eSSreekanth Reddy 		} else {
1661919d8a3fSJoe Perches 			ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1662919d8a3fSJoe Perches 			    __func__, buffer_type);
1663f92363d1SSreekanth Reddy 			return -EINVAL;
1664f92363d1SSreekanth Reddy 		}
1665a066f4c3SSreekanth Reddy 	} else if (ioc->diag_buffer_status[buffer_type] &
1666a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
1667a066f4c3SSreekanth Reddy 
1668a066f4c3SSreekanth Reddy 		if (ioc->unique_id[buffer_type] != MPT3DIAGBUFFUNIQUEID ||
1669a066f4c3SSreekanth Reddy 		    ioc->diag_buffer_sz[buffer_type] !=
1670a066f4c3SSreekanth Reddy 		    diag_register->requested_buffer_size) {
1671a066f4c3SSreekanth Reddy 
1672a066f4c3SSreekanth Reddy 			ioc_err(ioc,
1673a066f4c3SSreekanth 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",
1674a066f4c3SSreekanth Reddy 			     __func__, buffer_type,
1675a066f4c3SSreekanth Reddy 			    ioc->diag_buffer_sz[buffer_type]);
1676a066f4c3SSreekanth Reddy 			return -EINVAL;
1677a066f4c3SSreekanth Reddy 		}
167808e7378eSSreekanth Reddy 	}
1679f92363d1SSreekanth Reddy 
1680f92363d1SSreekanth Reddy 	if (diag_register->requested_buffer_size % 4)  {
1681919d8a3fSJoe Perches 		ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
1682919d8a3fSJoe Perches 			__func__);
1683f92363d1SSreekanth Reddy 		return -EINVAL;
1684f92363d1SSreekanth Reddy 	}
1685f92363d1SSreekanth Reddy 
1686f92363d1SSreekanth Reddy 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1687f92363d1SSreekanth Reddy 	if (!smid) {
1688919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1689f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1690f92363d1SSreekanth Reddy 		goto out;
1691f92363d1SSreekanth Reddy 	}
1692f92363d1SSreekanth Reddy 
1693f92363d1SSreekanth Reddy 	rc = 0;
1694f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1695f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1696f92363d1SSreekanth Reddy 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1697f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
1698f92363d1SSreekanth Reddy 
1699f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
1700f92363d1SSreekanth Reddy 	request_data_sz = diag_register->requested_buffer_size;
1701f92363d1SSreekanth Reddy 	ioc->unique_id[buffer_type] = diag_register->unique_id;
1702688c1a0aSSuganath Prabu S 	/* Reset ioc variables used for additional query commands */
1703688c1a0aSSuganath Prabu S 	ioc->reset_from_user = 0;
1704688c1a0aSSuganath Prabu S 	memset(&ioc->htb_rel, 0, sizeof(struct htb_rel_query));
1705a066f4c3SSreekanth Reddy 	ioc->diag_buffer_status[buffer_type] &=
1706a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1707f92363d1SSreekanth Reddy 	memcpy(ioc->product_specific[buffer_type],
1708f92363d1SSreekanth Reddy 	    diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1709f92363d1SSreekanth Reddy 	ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1710f92363d1SSreekanth Reddy 
1711f92363d1SSreekanth Reddy 	if (request_data) {
1712f92363d1SSreekanth Reddy 		request_data_dma = ioc->diag_buffer_dma[buffer_type];
1713f92363d1SSreekanth Reddy 		if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
17141c2048bdSChristoph Hellwig 			dma_free_coherent(&ioc->pdev->dev,
1715f92363d1SSreekanth Reddy 					ioc->diag_buffer_sz[buffer_type],
1716f92363d1SSreekanth Reddy 					request_data, request_data_dma);
1717f92363d1SSreekanth Reddy 			request_data = NULL;
1718f92363d1SSreekanth Reddy 		}
1719f92363d1SSreekanth Reddy 	}
1720f92363d1SSreekanth Reddy 
1721f92363d1SSreekanth Reddy 	if (request_data == NULL) {
1722f92363d1SSreekanth Reddy 		ioc->diag_buffer_sz[buffer_type] = 0;
1723f92363d1SSreekanth Reddy 		ioc->diag_buffer_dma[buffer_type] = 0;
17241c2048bdSChristoph Hellwig 		request_data = dma_alloc_coherent(&ioc->pdev->dev,
17251c2048bdSChristoph Hellwig 				request_data_sz, &request_data_dma, GFP_KERNEL);
1726f92363d1SSreekanth Reddy 		if (request_data == NULL) {
1727919d8a3fSJoe Perches 			ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
1728919d8a3fSJoe Perches 				__func__, request_data_sz);
1729f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
1730782b2818SSreekanth Reddy 			rc = -ENOMEM;
1731782b2818SSreekanth Reddy 			goto out;
1732f92363d1SSreekanth Reddy 		}
1733f92363d1SSreekanth Reddy 		ioc->diag_buffer[buffer_type] = request_data;
1734f92363d1SSreekanth Reddy 		ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1735f92363d1SSreekanth Reddy 		ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1736f92363d1SSreekanth Reddy 	}
1737f92363d1SSreekanth Reddy 
1738f92363d1SSreekanth Reddy 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1739f92363d1SSreekanth Reddy 	mpi_request->BufferType = diag_register->buffer_type;
1740f92363d1SSreekanth Reddy 	mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1741f92363d1SSreekanth Reddy 	mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1742f92363d1SSreekanth Reddy 	mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1743f92363d1SSreekanth Reddy 	mpi_request->VF_ID = 0; /* TODO */
1744f92363d1SSreekanth Reddy 	mpi_request->VP_ID = 0;
1745f92363d1SSreekanth Reddy 
1746919d8a3fSJoe Perches 	dctlprintk(ioc,
1747919d8a3fSJoe Perches 		   ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1748919d8a3fSJoe Perches 			    __func__, request_data,
1749f92363d1SSreekanth Reddy 			    (unsigned long long)request_data_dma,
1750f92363d1SSreekanth Reddy 			    le32_to_cpu(mpi_request->BufferLength)));
1751f92363d1SSreekanth Reddy 
1752f92363d1SSreekanth Reddy 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1753f92363d1SSreekanth Reddy 		mpi_request->ProductSpecific[i] =
1754f92363d1SSreekanth Reddy 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
1755f92363d1SSreekanth Reddy 
1756f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
1757078a4cc1SSuganath Prabu S 	ioc->put_smid_default(ioc, smid);
17588bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
1759f92363d1SSreekanth Reddy 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1760f92363d1SSreekanth Reddy 
1761f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1762c6bdb6a1SSreekanth Reddy 		mpt3sas_check_cmd_timeout(ioc,
1763d37306caSChaitra P B 		    ioc->ctl_cmds.status, mpi_request,
1764c6bdb6a1SSreekanth Reddy 		    sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
1765f92363d1SSreekanth Reddy 		goto issue_host_reset;
1766f92363d1SSreekanth Reddy 	}
1767f92363d1SSreekanth Reddy 
1768f92363d1SSreekanth Reddy 	/* process the completed Reply Message Frame */
1769f92363d1SSreekanth Reddy 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1770919d8a3fSJoe Perches 		ioc_err(ioc, "%s: no reply message\n", __func__);
1771f92363d1SSreekanth Reddy 		rc = -EFAULT;
1772f92363d1SSreekanth Reddy 		goto out;
1773f92363d1SSreekanth Reddy 	}
1774f92363d1SSreekanth Reddy 
1775f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
1776f92363d1SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1777f92363d1SSreekanth Reddy 
1778f92363d1SSreekanth Reddy 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1779f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
1780f92363d1SSreekanth Reddy 			MPT3_DIAG_BUFFER_IS_REGISTERED;
1781919d8a3fSJoe Perches 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
1782f92363d1SSreekanth Reddy 	} else {
1783919d8a3fSJoe Perches 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1784919d8a3fSJoe Perches 			 __func__,
1785f92363d1SSreekanth Reddy 			 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1786f92363d1SSreekanth Reddy 		rc = -EFAULT;
1787f92363d1SSreekanth Reddy 	}
1788f92363d1SSreekanth Reddy 
1789f92363d1SSreekanth Reddy  issue_host_reset:
1790f92363d1SSreekanth Reddy 	if (issue_reset)
179198c56ad3SCalvin Owens 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1792f92363d1SSreekanth Reddy 
1793f92363d1SSreekanth Reddy  out:
1794f92363d1SSreekanth Reddy 
1795a066f4c3SSreekanth Reddy 	if (rc && request_data) {
17961c2048bdSChristoph Hellwig 		dma_free_coherent(&ioc->pdev->dev, request_data_sz,
1797f92363d1SSreekanth Reddy 		    request_data, request_data_dma);
1798a066f4c3SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] &=
1799a066f4c3SSreekanth Reddy 		    ~MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1800a066f4c3SSreekanth Reddy 	}
1801f92363d1SSreekanth Reddy 
1802f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1803f92363d1SSreekanth Reddy 	return rc;
1804f92363d1SSreekanth Reddy }
1805f92363d1SSreekanth Reddy 
1806f92363d1SSreekanth Reddy /**
1807f92363d1SSreekanth Reddy  * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1808f92363d1SSreekanth Reddy  * @ioc: per adapter object
1809f92363d1SSreekanth Reddy  * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1810f92363d1SSreekanth Reddy  *
1811f92363d1SSreekanth Reddy  * This is called when command line option diag_buffer_enable is enabled
1812f92363d1SSreekanth Reddy  * at driver load time.
1813f92363d1SSreekanth Reddy  */
1814f92363d1SSreekanth Reddy void
1815f92363d1SSreekanth Reddy mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1816f92363d1SSreekanth Reddy {
1817f92363d1SSreekanth Reddy 	struct mpt3_diag_register diag_register;
1818d04a6edfSSreekanth Reddy 	u32 ret_val;
1819d04a6edfSSreekanth Reddy 	u32 trace_buff_size = ioc->manu_pg11.HostTraceBufferMaxSizeKB<<10;
1820d04a6edfSSreekanth Reddy 	u32 min_trace_buff_size = 0;
1821d04a6edfSSreekanth Reddy 	u32 decr_trace_buff_size = 0;
1822f92363d1SSreekanth Reddy 
1823f92363d1SSreekanth Reddy 	memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1824f92363d1SSreekanth Reddy 
1825f92363d1SSreekanth Reddy 	if (bits_to_register & 1) {
1826919d8a3fSJoe Perches 		ioc_info(ioc, "registering trace buffer support\n");
1827f92363d1SSreekanth Reddy 		ioc->diag_trigger_master.MasterData =
1828f92363d1SSreekanth Reddy 		    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1829f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
183008e7378eSSreekanth Reddy 		diag_register.unique_id =
183108e7378eSSreekanth Reddy 		    (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
183208e7378eSSreekanth Reddy 		    (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
1833d04a6edfSSreekanth Reddy 
1834d04a6edfSSreekanth Reddy 		if (trace_buff_size != 0) {
1835d04a6edfSSreekanth Reddy 			diag_register.requested_buffer_size = trace_buff_size;
1836d04a6edfSSreekanth Reddy 			min_trace_buff_size =
1837d04a6edfSSreekanth Reddy 			    ioc->manu_pg11.HostTraceBufferMinSizeKB<<10;
1838d04a6edfSSreekanth Reddy 			decr_trace_buff_size =
1839d04a6edfSSreekanth Reddy 			    ioc->manu_pg11.HostTraceBufferDecrementSizeKB<<10;
1840d04a6edfSSreekanth Reddy 
1841d04a6edfSSreekanth Reddy 			if (min_trace_buff_size > trace_buff_size) {
1842d04a6edfSSreekanth Reddy 				/* The buff size is not set correctly */
1843d04a6edfSSreekanth Reddy 				ioc_err(ioc,
1844d04a6edfSSreekanth Reddy 				    "Min Trace Buff size (%d KB) greater than Max Trace Buff size (%d KB)\n",
1845d04a6edfSSreekanth Reddy 				     min_trace_buff_size>>10,
1846d04a6edfSSreekanth Reddy 				     trace_buff_size>>10);
1847d04a6edfSSreekanth Reddy 				ioc_err(ioc,
1848d04a6edfSSreekanth Reddy 				    "Using zero Min Trace Buff Size\n");
1849d04a6edfSSreekanth Reddy 				min_trace_buff_size = 0;
1850d04a6edfSSreekanth Reddy 			}
1851d04a6edfSSreekanth Reddy 
1852d04a6edfSSreekanth Reddy 			if (decr_trace_buff_size == 0) {
1853d04a6edfSSreekanth Reddy 				/*
1854d04a6edfSSreekanth Reddy 				 * retry the min size if decrement
1855d04a6edfSSreekanth Reddy 				 * is not available.
1856d04a6edfSSreekanth Reddy 				 */
1857d04a6edfSSreekanth Reddy 				decr_trace_buff_size =
1858d04a6edfSSreekanth Reddy 				    trace_buff_size - min_trace_buff_size;
1859d04a6edfSSreekanth Reddy 			}
1860d04a6edfSSreekanth Reddy 		} else {
1861f92363d1SSreekanth Reddy 			/* register for 2MB buffers  */
1862f92363d1SSreekanth Reddy 			diag_register.requested_buffer_size = 2 * (1024 * 1024);
1863d04a6edfSSreekanth Reddy 		}
1864d04a6edfSSreekanth Reddy 
1865d04a6edfSSreekanth Reddy 		do {
1866d04a6edfSSreekanth Reddy 			ret_val = _ctl_diag_register_2(ioc,  &diag_register);
1867d04a6edfSSreekanth Reddy 
1868d04a6edfSSreekanth Reddy 			if (ret_val == -ENOMEM && min_trace_buff_size &&
1869d04a6edfSSreekanth Reddy 			    (trace_buff_size - decr_trace_buff_size) >=
1870d04a6edfSSreekanth Reddy 			    min_trace_buff_size) {
1871d04a6edfSSreekanth Reddy 				/* adjust the buffer size */
1872d04a6edfSSreekanth Reddy 				trace_buff_size -= decr_trace_buff_size;
1873d04a6edfSSreekanth Reddy 				diag_register.requested_buffer_size =
1874d04a6edfSSreekanth Reddy 				    trace_buff_size;
1875d04a6edfSSreekanth Reddy 			} else
1876d04a6edfSSreekanth Reddy 				break;
1877d04a6edfSSreekanth Reddy 		} while (true);
1878d04a6edfSSreekanth Reddy 
1879d04a6edfSSreekanth Reddy 		if (ret_val == -ENOMEM)
1880d04a6edfSSreekanth Reddy 			ioc_err(ioc,
1881d04a6edfSSreekanth Reddy 			    "Cannot allocate trace buffer memory. Last memory tried = %d KB\n",
1882d04a6edfSSreekanth Reddy 			    diag_register.requested_buffer_size>>10);
1883d04a6edfSSreekanth Reddy 		else if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE]
1884a066f4c3SSreekanth Reddy 		    & MPT3_DIAG_BUFFER_IS_REGISTERED) {
1885d04a6edfSSreekanth Reddy 			ioc_err(ioc, "Trace buffer memory %d KB allocated\n",
1886d04a6edfSSreekanth Reddy 			    diag_register.requested_buffer_size>>10);
1887a066f4c3SSreekanth Reddy 			if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
1888a066f4c3SSreekanth Reddy 				ioc->diag_buffer_status[
1889a066f4c3SSreekanth Reddy 				    MPI2_DIAG_BUF_TYPE_TRACE] |=
1890a066f4c3SSreekanth Reddy 				    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1891a066f4c3SSreekanth Reddy 		}
1892f92363d1SSreekanth Reddy 	}
1893f92363d1SSreekanth Reddy 
1894f92363d1SSreekanth Reddy 	if (bits_to_register & 2) {
1895919d8a3fSJoe Perches 		ioc_info(ioc, "registering snapshot buffer support\n");
1896f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1897f92363d1SSreekanth Reddy 		/* register for 2MB buffers  */
1898f92363d1SSreekanth Reddy 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1899f92363d1SSreekanth Reddy 		diag_register.unique_id = 0x7075901;
1900f92363d1SSreekanth Reddy 		_ctl_diag_register_2(ioc,  &diag_register);
1901f92363d1SSreekanth Reddy 	}
1902f92363d1SSreekanth Reddy 
1903f92363d1SSreekanth Reddy 	if (bits_to_register & 4) {
1904919d8a3fSJoe Perches 		ioc_info(ioc, "registering extended buffer support\n");
1905f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1906f92363d1SSreekanth Reddy 		/* register for 2MB buffers  */
1907f92363d1SSreekanth Reddy 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1908f92363d1SSreekanth Reddy 		diag_register.unique_id = 0x7075901;
1909f92363d1SSreekanth Reddy 		_ctl_diag_register_2(ioc,  &diag_register);
1910f92363d1SSreekanth Reddy 	}
1911f92363d1SSreekanth Reddy }
1912f92363d1SSreekanth Reddy 
1913f92363d1SSreekanth Reddy /**
1914f92363d1SSreekanth Reddy  * _ctl_diag_register - application register with driver
1915f92363d1SSreekanth Reddy  * @ioc: per adapter object
19164beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1917f92363d1SSreekanth Reddy  *
1918f92363d1SSreekanth Reddy  * This will allow the driver to setup any required buffers that will be
1919f92363d1SSreekanth Reddy  * needed by firmware to communicate with the driver.
1920f92363d1SSreekanth Reddy  */
1921f92363d1SSreekanth Reddy static long
1922f92363d1SSreekanth Reddy _ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1923f92363d1SSreekanth Reddy {
1924f92363d1SSreekanth Reddy 	struct mpt3_diag_register karg;
1925f92363d1SSreekanth Reddy 	long rc;
1926f92363d1SSreekanth Reddy 
1927f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1928f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1929f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1930f92363d1SSreekanth Reddy 		return -EFAULT;
1931f92363d1SSreekanth Reddy 	}
1932f92363d1SSreekanth Reddy 
1933f92363d1SSreekanth Reddy 	rc = _ctl_diag_register_2(ioc, &karg);
1934a8a6cbcdSSreekanth Reddy 
1935a8a6cbcdSSreekanth Reddy 	if (!rc && (ioc->diag_buffer_status[karg.buffer_type] &
1936a8a6cbcdSSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED))
1937a8a6cbcdSSreekanth Reddy 		ioc->diag_buffer_status[karg.buffer_type] |=
1938a8a6cbcdSSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_APP_OWNED;
1939a8a6cbcdSSreekanth Reddy 
1940f92363d1SSreekanth Reddy 	return rc;
1941f92363d1SSreekanth Reddy }
1942f92363d1SSreekanth Reddy 
1943f92363d1SSreekanth Reddy /**
1944f92363d1SSreekanth Reddy  * _ctl_diag_unregister - application unregister with driver
1945f92363d1SSreekanth Reddy  * @ioc: per adapter object
19464beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
1947f92363d1SSreekanth Reddy  *
1948f92363d1SSreekanth Reddy  * This will allow the driver to cleanup any memory allocated for diag
1949f92363d1SSreekanth Reddy  * messages and to free up any resources.
1950f92363d1SSreekanth Reddy  */
1951f92363d1SSreekanth Reddy static long
1952f92363d1SSreekanth Reddy _ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1953f92363d1SSreekanth Reddy {
1954f92363d1SSreekanth Reddy 	struct mpt3_diag_unregister karg;
1955f92363d1SSreekanth Reddy 	void *request_data;
1956f92363d1SSreekanth Reddy 	dma_addr_t request_data_dma;
1957f92363d1SSreekanth Reddy 	u32 request_data_sz;
1958f92363d1SSreekanth Reddy 	u8 buffer_type;
1959f92363d1SSreekanth Reddy 
1960f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1961f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1962f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1963f92363d1SSreekanth Reddy 		return -EFAULT;
1964f92363d1SSreekanth Reddy 	}
1965f92363d1SSreekanth Reddy 
1966919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1967f92363d1SSreekanth Reddy 				 __func__));
1968f92363d1SSreekanth Reddy 
196908e7378eSSreekanth Reddy 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
197008e7378eSSreekanth Reddy 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
197108e7378eSSreekanth Reddy 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
197208e7378eSSreekanth Reddy 		    __func__, karg.unique_id);
197308e7378eSSreekanth Reddy 		return -EINVAL;
197408e7378eSSreekanth Reddy 	}
197508e7378eSSreekanth Reddy 
1976f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1977919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1978919d8a3fSJoe Perches 			__func__, buffer_type);
1979f92363d1SSreekanth Reddy 		return -EPERM;
1980f92363d1SSreekanth Reddy 	}
1981f92363d1SSreekanth Reddy 
1982f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
1983f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1984919d8a3fSJoe Perches 		ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1985919d8a3fSJoe Perches 			__func__, buffer_type);
1986f92363d1SSreekanth Reddy 		return -EINVAL;
1987f92363d1SSreekanth Reddy 	}
1988f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
1989f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
1990919d8a3fSJoe Perches 		ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
1991919d8a3fSJoe Perches 			__func__, buffer_type);
1992f92363d1SSreekanth Reddy 		return -EINVAL;
1993f92363d1SSreekanth Reddy 	}
1994f92363d1SSreekanth Reddy 
1995f92363d1SSreekanth Reddy 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1996919d8a3fSJoe Perches 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1997919d8a3fSJoe Perches 			__func__, karg.unique_id);
1998f92363d1SSreekanth Reddy 		return -EINVAL;
1999f92363d1SSreekanth Reddy 	}
2000f92363d1SSreekanth Reddy 
2001f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
2002f92363d1SSreekanth Reddy 	if (!request_data) {
2003919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2004919d8a3fSJoe Perches 			__func__, buffer_type);
2005f92363d1SSreekanth Reddy 		return -ENOMEM;
2006f92363d1SSreekanth Reddy 	}
2007f92363d1SSreekanth Reddy 
2008a066f4c3SSreekanth Reddy 	if (ioc->diag_buffer_status[buffer_type] &
2009a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
2010a066f4c3SSreekanth Reddy 		ioc->unique_id[buffer_type] = MPT3DIAGBUFFUNIQUEID;
2011a066f4c3SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] &=
2012a8a6cbcdSSreekanth Reddy 		    ~MPT3_DIAG_BUFFER_IS_APP_OWNED;
2013a8a6cbcdSSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] &=
2014a066f4c3SSreekanth Reddy 		    ~MPT3_DIAG_BUFFER_IS_REGISTERED;
2015a066f4c3SSreekanth Reddy 	} else {
2016f92363d1SSreekanth Reddy 		request_data_sz = ioc->diag_buffer_sz[buffer_type];
2017f92363d1SSreekanth Reddy 		request_data_dma = ioc->diag_buffer_dma[buffer_type];
20181c2048bdSChristoph Hellwig 		dma_free_coherent(&ioc->pdev->dev, request_data_sz,
2019f92363d1SSreekanth Reddy 				request_data, request_data_dma);
2020f92363d1SSreekanth Reddy 		ioc->diag_buffer[buffer_type] = NULL;
2021f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] = 0;
2022a066f4c3SSreekanth Reddy 	}
2023f92363d1SSreekanth Reddy 	return 0;
2024f92363d1SSreekanth Reddy }
2025f92363d1SSreekanth Reddy 
2026f92363d1SSreekanth Reddy /**
2027f92363d1SSreekanth Reddy  * _ctl_diag_query - query relevant info associated with diag buffers
2028f92363d1SSreekanth Reddy  * @ioc: per adapter object
20294beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
2030f92363d1SSreekanth Reddy  *
2031f92363d1SSreekanth Reddy  * The application will send only buffer_type and unique_id.  Driver will
2032f92363d1SSreekanth Reddy  * inspect unique_id first, if valid, fill in all the info.  If unique_id is
2033f92363d1SSreekanth Reddy  * 0x00, the driver will return info specified by Buffer Type.
2034f92363d1SSreekanth Reddy  */
2035f92363d1SSreekanth Reddy static long
2036f92363d1SSreekanth Reddy _ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2037f92363d1SSreekanth Reddy {
2038f92363d1SSreekanth Reddy 	struct mpt3_diag_query karg;
2039f92363d1SSreekanth Reddy 	void *request_data;
2040f92363d1SSreekanth Reddy 	int i;
2041f92363d1SSreekanth Reddy 	u8 buffer_type;
2042f92363d1SSreekanth Reddy 
2043f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2044f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2045f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2046f92363d1SSreekanth Reddy 		return -EFAULT;
2047f92363d1SSreekanth Reddy 	}
2048f92363d1SSreekanth Reddy 
2049919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2050f92363d1SSreekanth Reddy 				 __func__));
2051f92363d1SSreekanth Reddy 
2052f92363d1SSreekanth Reddy 	karg.application_flags = 0;
2053f92363d1SSreekanth Reddy 	buffer_type = karg.buffer_type;
2054f92363d1SSreekanth Reddy 
2055f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2056919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2057919d8a3fSJoe Perches 			__func__, buffer_type);
2058f92363d1SSreekanth Reddy 		return -EPERM;
2059f92363d1SSreekanth Reddy 	}
2060f92363d1SSreekanth Reddy 
2061a066f4c3SSreekanth Reddy 	if (!(ioc->diag_buffer_status[buffer_type] &
2062a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED)) {
2063f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[buffer_type] &
2064f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2065919d8a3fSJoe Perches 			ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2066919d8a3fSJoe Perches 				__func__, buffer_type);
2067f92363d1SSreekanth Reddy 			return -EINVAL;
2068f92363d1SSreekanth Reddy 		}
2069a066f4c3SSreekanth Reddy 	}
2070f92363d1SSreekanth Reddy 
207108e7378eSSreekanth Reddy 	if (karg.unique_id) {
2072f92363d1SSreekanth Reddy 		if (karg.unique_id != ioc->unique_id[buffer_type]) {
2073919d8a3fSJoe Perches 			ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2074919d8a3fSJoe Perches 				__func__, karg.unique_id);
2075f92363d1SSreekanth Reddy 			return -EINVAL;
2076f92363d1SSreekanth Reddy 		}
2077f92363d1SSreekanth Reddy 	}
2078f92363d1SSreekanth Reddy 
2079f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
2080f92363d1SSreekanth Reddy 	if (!request_data) {
2081919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2082919d8a3fSJoe Perches 			__func__, buffer_type);
2083f92363d1SSreekanth Reddy 		return -ENOMEM;
2084f92363d1SSreekanth Reddy 	}
2085f92363d1SSreekanth Reddy 
2086a066f4c3SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2087a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED))
2088a066f4c3SSreekanth Reddy 		karg.application_flags |= MPT3_APP_FLAGS_BUFFER_VALID;
2089a066f4c3SSreekanth Reddy 
2090a066f4c3SSreekanth Reddy 	if (!(ioc->diag_buffer_status[buffer_type] &
2091a066f4c3SSreekanth Reddy 	     MPT3_DIAG_BUFFER_IS_RELEASED))
2092a066f4c3SSreekanth Reddy 		karg.application_flags |= MPT3_APP_FLAGS_FW_BUFFER_ACCESS;
2093a066f4c3SSreekanth Reddy 
2094a066f4c3SSreekanth Reddy 	if (!(ioc->diag_buffer_status[buffer_type] &
2095a066f4c3SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED))
2096a066f4c3SSreekanth Reddy 		karg.application_flags |= MPT3_APP_FLAGS_DYNAMIC_BUFFER_ALLOC;
2097f92363d1SSreekanth Reddy 
2098a8a6cbcdSSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2099a8a6cbcdSSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_APP_OWNED))
2100a8a6cbcdSSreekanth Reddy 		karg.application_flags |= MPT3_APP_FLAGS_APP_OWNED;
2101a8a6cbcdSSreekanth Reddy 
2102f92363d1SSreekanth Reddy 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2103f92363d1SSreekanth Reddy 		karg.product_specific[i] =
2104f92363d1SSreekanth Reddy 		    ioc->product_specific[buffer_type][i];
2105f92363d1SSreekanth Reddy 
2106f92363d1SSreekanth Reddy 	karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
2107f92363d1SSreekanth Reddy 	karg.driver_added_buffer_size = 0;
2108f92363d1SSreekanth Reddy 	karg.unique_id = ioc->unique_id[buffer_type];
2109f92363d1SSreekanth Reddy 	karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
2110f92363d1SSreekanth Reddy 
2111f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
2112919d8a3fSJoe Perches 		ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
2113919d8a3fSJoe Perches 			__func__, arg);
2114f92363d1SSreekanth Reddy 		return -EFAULT;
2115f92363d1SSreekanth Reddy 	}
2116f92363d1SSreekanth Reddy 	return 0;
2117f92363d1SSreekanth Reddy }
2118f92363d1SSreekanth Reddy 
2119f92363d1SSreekanth Reddy /**
2120f92363d1SSreekanth Reddy  * mpt3sas_send_diag_release - Diag Release Message
2121f92363d1SSreekanth Reddy  * @ioc: per adapter object
21224beb4867SBart Van Assche  * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
21234beb4867SBart Van Assche  * @issue_reset: specifies whether host reset is required.
2124f92363d1SSreekanth Reddy  *
2125f92363d1SSreekanth Reddy  */
2126f92363d1SSreekanth Reddy int
2127f92363d1SSreekanth Reddy mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
2128f92363d1SSreekanth Reddy 	u8 *issue_reset)
2129f92363d1SSreekanth Reddy {
2130f92363d1SSreekanth Reddy 	Mpi2DiagReleaseRequest_t *mpi_request;
2131f92363d1SSreekanth Reddy 	Mpi2DiagReleaseReply_t *mpi_reply;
2132f92363d1SSreekanth Reddy 	u16 smid;
2133f92363d1SSreekanth Reddy 	u16 ioc_status;
2134f92363d1SSreekanth Reddy 	u32 ioc_state;
2135f92363d1SSreekanth Reddy 	int rc;
2136c6bdb6a1SSreekanth Reddy 	u8 reset_needed = 0;
2137f92363d1SSreekanth Reddy 
2138919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2139f92363d1SSreekanth Reddy 				 __func__));
2140f92363d1SSreekanth Reddy 
2141f92363d1SSreekanth Reddy 	rc = 0;
2142f92363d1SSreekanth Reddy 	*issue_reset = 0;
2143f92363d1SSreekanth Reddy 
2144c6bdb6a1SSreekanth Reddy 
2145f92363d1SSreekanth Reddy 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2146f92363d1SSreekanth Reddy 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2147f92363d1SSreekanth Reddy 		if (ioc->diag_buffer_status[buffer_type] &
2148f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED)
2149f92363d1SSreekanth Reddy 			ioc->diag_buffer_status[buffer_type] |=
2150f92363d1SSreekanth Reddy 			    MPT3_DIAG_BUFFER_IS_RELEASED;
2151919d8a3fSJoe Perches 		dctlprintk(ioc,
2152919d8a3fSJoe Perches 			   ioc_info(ioc, "%s: skipping due to FAULT state\n",
2153f92363d1SSreekanth Reddy 				    __func__));
2154f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2155f92363d1SSreekanth Reddy 		goto out;
2156f92363d1SSreekanth Reddy 	}
2157f92363d1SSreekanth Reddy 
2158f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2159919d8a3fSJoe Perches 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
2160f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2161f92363d1SSreekanth Reddy 		goto out;
2162f92363d1SSreekanth Reddy 	}
2163f92363d1SSreekanth Reddy 
2164f92363d1SSreekanth Reddy 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2165f92363d1SSreekanth Reddy 	if (!smid) {
2166919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2167f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2168f92363d1SSreekanth Reddy 		goto out;
2169f92363d1SSreekanth Reddy 	}
2170f92363d1SSreekanth Reddy 
2171f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2172f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2173f92363d1SSreekanth Reddy 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2174f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
2175f92363d1SSreekanth Reddy 
2176f92363d1SSreekanth Reddy 	mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
2177f92363d1SSreekanth Reddy 	mpi_request->BufferType = buffer_type;
2178f92363d1SSreekanth Reddy 	mpi_request->VF_ID = 0; /* TODO */
2179f92363d1SSreekanth Reddy 	mpi_request->VP_ID = 0;
2180f92363d1SSreekanth Reddy 
2181f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
2182078a4cc1SSuganath Prabu S 	ioc->put_smid_default(ioc, smid);
21838bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
2184f92363d1SSreekanth Reddy 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2185f92363d1SSreekanth Reddy 
2186f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2187c6bdb6a1SSreekanth Reddy 		mpt3sas_check_cmd_timeout(ioc,
2188d37306caSChaitra P B 		    ioc->ctl_cmds.status, mpi_request,
2189c6bdb6a1SSreekanth Reddy 		    sizeof(Mpi2DiagReleaseRequest_t)/4, reset_needed);
2190c6bdb6a1SSreekanth Reddy 		*issue_reset = reset_needed;
2191f92363d1SSreekanth Reddy 		rc = -EFAULT;
2192f92363d1SSreekanth Reddy 		goto out;
2193f92363d1SSreekanth Reddy 	}
2194f92363d1SSreekanth Reddy 
2195f92363d1SSreekanth Reddy 	/* process the completed Reply Message Frame */
2196f92363d1SSreekanth Reddy 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2197919d8a3fSJoe Perches 		ioc_err(ioc, "%s: no reply message\n", __func__);
2198f92363d1SSreekanth Reddy 		rc = -EFAULT;
2199f92363d1SSreekanth Reddy 		goto out;
2200f92363d1SSreekanth Reddy 	}
2201f92363d1SSreekanth Reddy 
2202f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
2203f92363d1SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2204f92363d1SSreekanth Reddy 
2205f92363d1SSreekanth Reddy 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2206f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
2207f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED;
2208919d8a3fSJoe Perches 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
2209f92363d1SSreekanth Reddy 	} else {
2210919d8a3fSJoe Perches 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2211919d8a3fSJoe Perches 			 __func__,
2212f92363d1SSreekanth Reddy 			 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2213f92363d1SSreekanth Reddy 		rc = -EFAULT;
2214f92363d1SSreekanth Reddy 	}
2215f92363d1SSreekanth Reddy 
2216f92363d1SSreekanth Reddy  out:
2217f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2218f92363d1SSreekanth Reddy 	return rc;
2219f92363d1SSreekanth Reddy }
2220f92363d1SSreekanth Reddy 
2221f92363d1SSreekanth Reddy /**
2222f92363d1SSreekanth Reddy  * _ctl_diag_release - request to send Diag Release Message to firmware
22234beb4867SBart Van Assche  * @ioc: ?
22244beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
2225f92363d1SSreekanth Reddy  *
2226f92363d1SSreekanth Reddy  * This allows ownership of the specified buffer to returned to the driver,
2227f92363d1SSreekanth Reddy  * allowing an application to read the buffer without fear that firmware is
22289a284e5cSMasahiro Yamada  * overwriting information in the buffer.
2229f92363d1SSreekanth Reddy  */
2230f92363d1SSreekanth Reddy static long
2231f92363d1SSreekanth Reddy _ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2232f92363d1SSreekanth Reddy {
2233f92363d1SSreekanth Reddy 	struct mpt3_diag_release karg;
2234f92363d1SSreekanth Reddy 	void *request_data;
2235f92363d1SSreekanth Reddy 	int rc;
2236f92363d1SSreekanth Reddy 	u8 buffer_type;
2237f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
2238f92363d1SSreekanth Reddy 
2239f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2240f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2241f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2242f92363d1SSreekanth Reddy 		return -EFAULT;
2243f92363d1SSreekanth Reddy 	}
2244f92363d1SSreekanth Reddy 
2245919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2246f92363d1SSreekanth Reddy 				 __func__));
2247f92363d1SSreekanth Reddy 
224808e7378eSSreekanth Reddy 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
224908e7378eSSreekanth Reddy 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
225008e7378eSSreekanth Reddy 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
225108e7378eSSreekanth Reddy 		    __func__, karg.unique_id);
225208e7378eSSreekanth Reddy 		return -EINVAL;
225308e7378eSSreekanth Reddy 	}
225408e7378eSSreekanth Reddy 
2255f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2256919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2257919d8a3fSJoe Perches 			__func__, buffer_type);
2258f92363d1SSreekanth Reddy 		return -EPERM;
2259f92363d1SSreekanth Reddy 	}
2260f92363d1SSreekanth Reddy 
2261f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2262f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2263919d8a3fSJoe Perches 		ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2264919d8a3fSJoe Perches 			__func__, buffer_type);
2265f92363d1SSreekanth Reddy 		return -EINVAL;
2266f92363d1SSreekanth Reddy 	}
2267f92363d1SSreekanth Reddy 
2268f92363d1SSreekanth Reddy 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2269919d8a3fSJoe Perches 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2270919d8a3fSJoe Perches 			__func__, karg.unique_id);
2271f92363d1SSreekanth Reddy 		return -EINVAL;
2272f92363d1SSreekanth Reddy 	}
2273f92363d1SSreekanth Reddy 
2274f92363d1SSreekanth Reddy 	if (ioc->diag_buffer_status[buffer_type] &
2275f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
2276919d8a3fSJoe Perches 		ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
2277919d8a3fSJoe Perches 			__func__, buffer_type);
227829f571f8SSreekanth Reddy 		return -EINVAL;
2279f92363d1SSreekanth Reddy 	}
2280f92363d1SSreekanth Reddy 
2281f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
2282f92363d1SSreekanth Reddy 
2283f92363d1SSreekanth Reddy 	if (!request_data) {
2284919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2285919d8a3fSJoe Perches 			__func__, buffer_type);
2286f92363d1SSreekanth Reddy 		return -ENOMEM;
2287f92363d1SSreekanth Reddy 	}
2288f92363d1SSreekanth Reddy 
2289f92363d1SSreekanth Reddy 	/* buffers were released by due to host reset */
2290f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2291f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2292f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
2293f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED;
2294f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] &=
2295f92363d1SSreekanth Reddy 		    ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
2296919d8a3fSJoe Perches 		ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
2297919d8a3fSJoe Perches 			__func__, buffer_type);
2298f92363d1SSreekanth Reddy 		return 0;
2299f92363d1SSreekanth Reddy 	}
2300f92363d1SSreekanth Reddy 
2301f92363d1SSreekanth Reddy 	rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2302f92363d1SSreekanth Reddy 
2303f92363d1SSreekanth Reddy 	if (issue_reset)
230498c56ad3SCalvin Owens 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2305f92363d1SSreekanth Reddy 
2306f92363d1SSreekanth Reddy 	return rc;
2307f92363d1SSreekanth Reddy }
2308f92363d1SSreekanth Reddy 
2309f92363d1SSreekanth Reddy /**
2310f92363d1SSreekanth Reddy  * _ctl_diag_read_buffer - request for copy of the diag buffer
2311f92363d1SSreekanth Reddy  * @ioc: per adapter object
23124beb4867SBart Van Assche  * @arg: user space buffer containing ioctl content
2313f92363d1SSreekanth Reddy  */
2314f92363d1SSreekanth Reddy static long
2315f92363d1SSreekanth Reddy _ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2316f92363d1SSreekanth Reddy {
2317f92363d1SSreekanth Reddy 	struct mpt3_diag_read_buffer karg;
2318f92363d1SSreekanth Reddy 	struct mpt3_diag_read_buffer __user *uarg = arg;
2319f92363d1SSreekanth Reddy 	void *request_data, *diag_data;
2320f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostRequest_t *mpi_request;
2321f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostReply_t *mpi_reply;
2322f92363d1SSreekanth Reddy 	int rc, i;
2323f92363d1SSreekanth Reddy 	u8 buffer_type;
23248bbb1cf6SCalvin Owens 	unsigned long request_size, copy_size;
2325f92363d1SSreekanth Reddy 	u16 smid;
2326f92363d1SSreekanth Reddy 	u16 ioc_status;
2327f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
2328f92363d1SSreekanth Reddy 
2329f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2330f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2331f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2332f92363d1SSreekanth Reddy 		return -EFAULT;
2333f92363d1SSreekanth Reddy 	}
2334f92363d1SSreekanth Reddy 
2335919d8a3fSJoe Perches 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2336f92363d1SSreekanth Reddy 				 __func__));
2337f92363d1SSreekanth Reddy 
233808e7378eSSreekanth Reddy 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
233908e7378eSSreekanth Reddy 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
234008e7378eSSreekanth Reddy 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
234108e7378eSSreekanth Reddy 		    __func__, karg.unique_id);
234208e7378eSSreekanth Reddy 		return -EINVAL;
234308e7378eSSreekanth Reddy 	}
234408e7378eSSreekanth Reddy 
2345f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2346919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2347919d8a3fSJoe Perches 			__func__, buffer_type);
2348f92363d1SSreekanth Reddy 		return -EPERM;
2349f92363d1SSreekanth Reddy 	}
2350f92363d1SSreekanth Reddy 
2351f92363d1SSreekanth Reddy 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2352919d8a3fSJoe Perches 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2353919d8a3fSJoe Perches 			__func__, karg.unique_id);
2354f92363d1SSreekanth Reddy 		return -EINVAL;
2355f92363d1SSreekanth Reddy 	}
2356f92363d1SSreekanth Reddy 
2357f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
2358f92363d1SSreekanth Reddy 	if (!request_data) {
2359919d8a3fSJoe Perches 		ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2360919d8a3fSJoe Perches 			__func__, buffer_type);
2361f92363d1SSreekanth Reddy 		return -ENOMEM;
2362f92363d1SSreekanth Reddy 	}
2363f92363d1SSreekanth Reddy 
2364f92363d1SSreekanth Reddy 	request_size = ioc->diag_buffer_sz[buffer_type];
2365f92363d1SSreekanth Reddy 
2366f92363d1SSreekanth Reddy 	if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2367919d8a3fSJoe Perches 		ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
2368f92363d1SSreekanth Reddy 			__func__);
2369f92363d1SSreekanth Reddy 		return -EINVAL;
2370f92363d1SSreekanth Reddy 	}
2371f92363d1SSreekanth Reddy 
2372f92363d1SSreekanth Reddy 	if (karg.starting_offset > request_size)
2373f92363d1SSreekanth Reddy 		return -EINVAL;
2374f92363d1SSreekanth Reddy 
2375f92363d1SSreekanth Reddy 	diag_data = (void *)(request_data + karg.starting_offset);
2376919d8a3fSJoe Perches 	dctlprintk(ioc,
2377919d8a3fSJoe Perches 		   ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2378919d8a3fSJoe Perches 			    __func__, diag_data, karg.starting_offset,
2379919d8a3fSJoe Perches 			    karg.bytes_to_read));
2380f92363d1SSreekanth Reddy 
2381f92363d1SSreekanth Reddy 	/* Truncate data on requests that are too large */
2382f92363d1SSreekanth Reddy 	if ((diag_data + karg.bytes_to_read < diag_data) ||
2383f92363d1SSreekanth Reddy 	    (diag_data + karg.bytes_to_read > request_data + request_size))
2384f92363d1SSreekanth Reddy 		copy_size = request_size - karg.starting_offset;
2385f92363d1SSreekanth Reddy 	else
2386f92363d1SSreekanth Reddy 		copy_size = karg.bytes_to_read;
2387f92363d1SSreekanth Reddy 
2388f92363d1SSreekanth Reddy 	if (copy_to_user((void __user *)uarg->diagnostic_data,
2389f92363d1SSreekanth Reddy 	    diag_data, copy_size)) {
2390919d8a3fSJoe Perches 		ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2391919d8a3fSJoe Perches 			__func__, diag_data);
2392f92363d1SSreekanth Reddy 		return -EFAULT;
2393f92363d1SSreekanth Reddy 	}
2394f92363d1SSreekanth Reddy 
2395f92363d1SSreekanth Reddy 	if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2396f92363d1SSreekanth Reddy 		return 0;
2397f92363d1SSreekanth Reddy 
2398919d8a3fSJoe Perches 	dctlprintk(ioc,
2399919d8a3fSJoe Perches 		   ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
2400919d8a3fSJoe Perches 			    __func__, buffer_type));
2401f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2402f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2403919d8a3fSJoe Perches 		dctlprintk(ioc,
2404919d8a3fSJoe Perches 			   ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
2405919d8a3fSJoe Perches 				    __func__, buffer_type));
2406f92363d1SSreekanth Reddy 		return 0;
2407f92363d1SSreekanth Reddy 	}
2408f92363d1SSreekanth Reddy 	/* Get a free request frame and save the message context.
2409f92363d1SSreekanth Reddy 	*/
2410f92363d1SSreekanth Reddy 
2411f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2412919d8a3fSJoe Perches 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
2413f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2414f92363d1SSreekanth Reddy 		goto out;
2415f92363d1SSreekanth Reddy 	}
2416f92363d1SSreekanth Reddy 
2417f92363d1SSreekanth Reddy 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2418f92363d1SSreekanth Reddy 	if (!smid) {
2419919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2420f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2421f92363d1SSreekanth Reddy 		goto out;
2422f92363d1SSreekanth Reddy 	}
2423f92363d1SSreekanth Reddy 
2424f92363d1SSreekanth Reddy 	rc = 0;
2425f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2426f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2427f92363d1SSreekanth Reddy 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2428f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
2429f92363d1SSreekanth Reddy 
2430f92363d1SSreekanth Reddy 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2431f92363d1SSreekanth Reddy 	mpi_request->BufferType = buffer_type;
2432f92363d1SSreekanth Reddy 	mpi_request->BufferLength =
2433f92363d1SSreekanth Reddy 	    cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2434f92363d1SSreekanth Reddy 	mpi_request->BufferAddress =
2435f92363d1SSreekanth Reddy 	    cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2436f92363d1SSreekanth Reddy 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2437f92363d1SSreekanth Reddy 		mpi_request->ProductSpecific[i] =
2438f92363d1SSreekanth Reddy 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
2439f92363d1SSreekanth Reddy 	mpi_request->VF_ID = 0; /* TODO */
2440f92363d1SSreekanth Reddy 	mpi_request->VP_ID = 0;
2441f92363d1SSreekanth Reddy 
2442f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
2443078a4cc1SSuganath Prabu S 	ioc->put_smid_default(ioc, smid);
24448bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
2445f92363d1SSreekanth Reddy 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2446f92363d1SSreekanth Reddy 
2447f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2448c6bdb6a1SSreekanth Reddy 		mpt3sas_check_cmd_timeout(ioc,
2449d37306caSChaitra P B 		    ioc->ctl_cmds.status, mpi_request,
2450c6bdb6a1SSreekanth Reddy 		    sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
2451f92363d1SSreekanth Reddy 		goto issue_host_reset;
2452f92363d1SSreekanth Reddy 	}
2453f92363d1SSreekanth Reddy 
2454f92363d1SSreekanth Reddy 	/* process the completed Reply Message Frame */
2455f92363d1SSreekanth Reddy 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2456919d8a3fSJoe Perches 		ioc_err(ioc, "%s: no reply message\n", __func__);
2457f92363d1SSreekanth Reddy 		rc = -EFAULT;
2458f92363d1SSreekanth Reddy 		goto out;
2459f92363d1SSreekanth Reddy 	}
2460f92363d1SSreekanth Reddy 
2461f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
2462f92363d1SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2463f92363d1SSreekanth Reddy 
2464f92363d1SSreekanth Reddy 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2465f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
2466f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED;
2467dd180e4eSSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] &=
2468dd180e4eSSreekanth Reddy 		    ~MPT3_DIAG_BUFFER_IS_RELEASED;
2469919d8a3fSJoe Perches 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
2470f92363d1SSreekanth Reddy 	} else {
2471919d8a3fSJoe Perches 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2472919d8a3fSJoe Perches 			 __func__, ioc_status,
2473919d8a3fSJoe Perches 			 le32_to_cpu(mpi_reply->IOCLogInfo));
2474f92363d1SSreekanth Reddy 		rc = -EFAULT;
2475f92363d1SSreekanth Reddy 	}
2476f92363d1SSreekanth Reddy 
2477f92363d1SSreekanth Reddy  issue_host_reset:
2478f92363d1SSreekanth Reddy 	if (issue_reset)
247998c56ad3SCalvin Owens 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2480f92363d1SSreekanth Reddy 
2481f92363d1SSreekanth Reddy  out:
2482f92363d1SSreekanth Reddy 
2483f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2484f92363d1SSreekanth Reddy 	return rc;
2485f92363d1SSreekanth Reddy }
2486f92363d1SSreekanth Reddy 
2487688c1a0aSSuganath Prabu S /**
2488688c1a0aSSuganath Prabu S  * _ctl_addnl_diag_query - query relevant info associated with diag buffers
2489688c1a0aSSuganath Prabu S  * @ioc: per adapter object
2490688c1a0aSSuganath Prabu S  * @arg: user space buffer containing ioctl content
2491688c1a0aSSuganath Prabu S  *
2492688c1a0aSSuganath Prabu S  * The application will send only unique_id.  Driver will
2493688c1a0aSSuganath Prabu S  * inspect unique_id first, if valid, fill the details related to cause
2494688c1a0aSSuganath Prabu S  * for diag buffer release.
2495688c1a0aSSuganath Prabu S  */
2496688c1a0aSSuganath Prabu S static long
2497688c1a0aSSuganath Prabu S _ctl_addnl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2498688c1a0aSSuganath Prabu S {
2499688c1a0aSSuganath Prabu S 	struct mpt3_addnl_diag_query karg;
2500688c1a0aSSuganath Prabu S 	u32 buffer_type = 0;
2501f92363d1SSreekanth Reddy 
2502688c1a0aSSuganath Prabu S 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2503688c1a0aSSuganath Prabu S 		pr_err("%s: failure at %s:%d/%s()!\n",
2504688c1a0aSSuganath Prabu S 		    ioc->name, __FILE__, __LINE__, __func__);
2505688c1a0aSSuganath Prabu S 		return -EFAULT;
2506688c1a0aSSuganath Prabu S 	}
2507688c1a0aSSuganath Prabu S 	dctlprintk(ioc, ioc_info(ioc, "%s\n",  __func__));
2508688c1a0aSSuganath Prabu S 	if (karg.unique_id == 0) {
2509688c1a0aSSuganath Prabu S 		ioc_err(ioc, "%s: unique_id is(0x%08x)\n",
2510688c1a0aSSuganath Prabu S 		    __func__, karg.unique_id);
2511688c1a0aSSuganath Prabu S 		return -EPERM;
2512688c1a0aSSuganath Prabu S 	}
2513688c1a0aSSuganath Prabu S 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2514688c1a0aSSuganath Prabu S 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2515688c1a0aSSuganath Prabu S 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2516688c1a0aSSuganath Prabu S 		    __func__, karg.unique_id);
2517688c1a0aSSuganath Prabu S 		return -EPERM;
2518688c1a0aSSuganath Prabu S 	}
251916660db3SGustavo A. R. Silva 	memset(&karg.rel_query, 0, sizeof(karg.rel_query));
2520688c1a0aSSuganath Prabu S 	if ((ioc->diag_buffer_status[buffer_type] &
2521688c1a0aSSuganath Prabu S 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2522688c1a0aSSuganath Prabu S 		ioc_info(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2523688c1a0aSSuganath Prabu S 		    __func__, buffer_type);
2524688c1a0aSSuganath Prabu S 		goto out;
2525688c1a0aSSuganath Prabu S 	}
2526688c1a0aSSuganath Prabu S 	if ((ioc->diag_buffer_status[buffer_type] &
2527688c1a0aSSuganath Prabu S 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2528688c1a0aSSuganath Prabu S 		ioc_err(ioc, "%s: buffer_type(0x%02x) is not released\n",
2529688c1a0aSSuganath Prabu S 		    __func__, buffer_type);
2530688c1a0aSSuganath Prabu S 		return -EPERM;
2531688c1a0aSSuganath Prabu S 	}
253216660db3SGustavo A. R. Silva 	memcpy(&karg.rel_query, &ioc->htb_rel, sizeof(karg.rel_query));
2533688c1a0aSSuganath Prabu S out:
2534688c1a0aSSuganath Prabu S 	if (copy_to_user(arg, &karg, sizeof(struct mpt3_addnl_diag_query))) {
2535688c1a0aSSuganath Prabu S 		ioc_err(ioc, "%s: unable to write mpt3_addnl_diag_query data @ %p\n",
2536688c1a0aSSuganath Prabu S 		    __func__, arg);
2537688c1a0aSSuganath Prabu S 		return -EFAULT;
2538688c1a0aSSuganath Prabu S 	}
2539688c1a0aSSuganath Prabu S 	return 0;
2540688c1a0aSSuganath Prabu S }
2541f92363d1SSreekanth Reddy 
2542f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2543f92363d1SSreekanth Reddy /**
2544f92363d1SSreekanth Reddy  * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2545f92363d1SSreekanth Reddy  * @ioc: per adapter object
25464beb4867SBart Van Assche  * @cmd: ioctl opcode
25474beb4867SBart Van Assche  * @arg: (struct mpt3_ioctl_command32)
2548f92363d1SSreekanth Reddy  *
2549f92363d1SSreekanth Reddy  * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2550f92363d1SSreekanth Reddy  */
2551f92363d1SSreekanth Reddy static long
2552f92363d1SSreekanth Reddy _ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2553f92363d1SSreekanth Reddy 	void __user *arg)
2554f92363d1SSreekanth Reddy {
2555f92363d1SSreekanth Reddy 	struct mpt3_ioctl_command32 karg32;
2556f92363d1SSreekanth Reddy 	struct mpt3_ioctl_command32 __user *uarg;
2557f92363d1SSreekanth Reddy 	struct mpt3_ioctl_command karg;
2558f92363d1SSreekanth Reddy 
2559f92363d1SSreekanth Reddy 	if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2560f92363d1SSreekanth Reddy 		return -EINVAL;
2561f92363d1SSreekanth Reddy 
2562f92363d1SSreekanth Reddy 	uarg = (struct mpt3_ioctl_command32 __user *) arg;
2563f92363d1SSreekanth Reddy 
2564f92363d1SSreekanth Reddy 	if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2565f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2566f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2567f92363d1SSreekanth Reddy 		return -EFAULT;
2568f92363d1SSreekanth Reddy 	}
2569f92363d1SSreekanth Reddy 
2570f92363d1SSreekanth Reddy 	memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2571f92363d1SSreekanth Reddy 	karg.hdr.ioc_number = karg32.hdr.ioc_number;
2572f92363d1SSreekanth Reddy 	karg.hdr.port_number = karg32.hdr.port_number;
2573f92363d1SSreekanth Reddy 	karg.hdr.max_data_size = karg32.hdr.max_data_size;
2574f92363d1SSreekanth Reddy 	karg.timeout = karg32.timeout;
2575f92363d1SSreekanth Reddy 	karg.max_reply_bytes = karg32.max_reply_bytes;
2576f92363d1SSreekanth Reddy 	karg.data_in_size = karg32.data_in_size;
2577f92363d1SSreekanth Reddy 	karg.data_out_size = karg32.data_out_size;
2578f92363d1SSreekanth Reddy 	karg.max_sense_bytes = karg32.max_sense_bytes;
2579f92363d1SSreekanth Reddy 	karg.data_sge_offset = karg32.data_sge_offset;
2580f92363d1SSreekanth Reddy 	karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2581f92363d1SSreekanth Reddy 	karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2582f92363d1SSreekanth Reddy 	karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2583f92363d1SSreekanth Reddy 	karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2584f92363d1SSreekanth Reddy 	return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2585f92363d1SSreekanth Reddy }
2586f92363d1SSreekanth Reddy #endif
2587f92363d1SSreekanth Reddy 
2588f92363d1SSreekanth Reddy /**
2589f92363d1SSreekanth Reddy  * _ctl_ioctl_main - main ioctl entry point
25904beb4867SBart Van Assche  * @file:  (struct file)
25914beb4867SBart Van Assche  * @cmd:  ioctl opcode
25924beb4867SBart Van Assche  * @arg:  user space data buffer
25934beb4867SBart Van Assche  * @compat:  handles 32 bit applications in 64bit os
2594c84b06a4SSreekanth Reddy  * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
2595b130b0d5SSuganath prabu Subramani  * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
2596f92363d1SSreekanth Reddy  */
2597f92363d1SSreekanth Reddy static long
2598f92363d1SSreekanth Reddy _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2599c84b06a4SSreekanth Reddy 	u8 compat, u16 mpi_version)
2600f92363d1SSreekanth Reddy {
2601f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc;
2602f92363d1SSreekanth Reddy 	struct mpt3_ioctl_header ioctl_header;
2603f92363d1SSreekanth Reddy 	enum block_state state;
2604688c1a0aSSuganath Prabu S 	long ret = -ENOIOCTLCMD;
2605f92363d1SSreekanth Reddy 
2606f92363d1SSreekanth Reddy 	/* get IOCTL header */
2607f92363d1SSreekanth Reddy 	if (copy_from_user(&ioctl_header, (char __user *)arg,
2608f92363d1SSreekanth Reddy 	    sizeof(struct mpt3_ioctl_header))) {
2609f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2610f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2611f92363d1SSreekanth Reddy 		return -EFAULT;
2612f92363d1SSreekanth Reddy 	}
2613f92363d1SSreekanth Reddy 
2614c84b06a4SSreekanth Reddy 	if (_ctl_verify_adapter(ioctl_header.ioc_number,
2615c84b06a4SSreekanth Reddy 				&ioc, mpi_version) == -1 || !ioc)
2616f92363d1SSreekanth Reddy 		return -ENODEV;
2617f92363d1SSreekanth Reddy 
261808c4d550SSreekanth Reddy 	/* pci_access_mutex lock acquired by ioctl path */
261908c4d550SSreekanth Reddy 	mutex_lock(&ioc->pci_access_mutex);
262008c4d550SSreekanth Reddy 
2621f92363d1SSreekanth Reddy 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
262208c4d550SSreekanth Reddy 	    ioc->is_driver_loading || ioc->remove_host) {
262308c4d550SSreekanth Reddy 		ret = -EAGAIN;
262408c4d550SSreekanth Reddy 		goto out_unlock_pciaccess;
262508c4d550SSreekanth Reddy 	}
2626f92363d1SSreekanth Reddy 
2627f92363d1SSreekanth Reddy 	state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2628f92363d1SSreekanth Reddy 	if (state == NON_BLOCKING) {
262908c4d550SSreekanth Reddy 		if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
263008c4d550SSreekanth Reddy 			ret = -EAGAIN;
263108c4d550SSreekanth Reddy 			goto out_unlock_pciaccess;
263208c4d550SSreekanth Reddy 		}
263308c4d550SSreekanth Reddy 	} else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
263408c4d550SSreekanth Reddy 		ret = -ERESTARTSYS;
263508c4d550SSreekanth Reddy 		goto out_unlock_pciaccess;
263608c4d550SSreekanth Reddy 	}
2637f92363d1SSreekanth Reddy 
2638f92363d1SSreekanth Reddy 
2639f92363d1SSreekanth Reddy 	switch (cmd) {
2640f92363d1SSreekanth Reddy 	case MPT3IOCINFO:
2641f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2642f92363d1SSreekanth Reddy 			ret = _ctl_getiocinfo(ioc, arg);
2643f92363d1SSreekanth Reddy 		break;
2644f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2645f92363d1SSreekanth Reddy 	case MPT3COMMAND32:
2646f92363d1SSreekanth Reddy #endif
2647f92363d1SSreekanth Reddy 	case MPT3COMMAND:
2648f92363d1SSreekanth Reddy 	{
2649f92363d1SSreekanth Reddy 		struct mpt3_ioctl_command __user *uarg;
2650f92363d1SSreekanth Reddy 		struct mpt3_ioctl_command karg;
2651f92363d1SSreekanth Reddy 
2652f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2653f92363d1SSreekanth Reddy 		if (compat) {
2654f92363d1SSreekanth Reddy 			ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2655f92363d1SSreekanth Reddy 			break;
2656f92363d1SSreekanth Reddy 		}
2657f92363d1SSreekanth Reddy #endif
2658f92363d1SSreekanth Reddy 		if (copy_from_user(&karg, arg, sizeof(karg))) {
2659f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n",
2660f92363d1SSreekanth Reddy 			    __FILE__, __LINE__, __func__);
2661f92363d1SSreekanth Reddy 			ret = -EFAULT;
2662f92363d1SSreekanth Reddy 			break;
2663f92363d1SSreekanth Reddy 		}
2664f92363d1SSreekanth Reddy 
2665f9e3ebeeSGen Zhang 		if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2666f9e3ebeeSGen Zhang 			ret = -EINVAL;
2667f9e3ebeeSGen Zhang 			break;
2668f9e3ebeeSGen Zhang 		}
2669f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2670f92363d1SSreekanth Reddy 			uarg = arg;
2671f92363d1SSreekanth Reddy 			ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2672f92363d1SSreekanth Reddy 		}
2673f92363d1SSreekanth Reddy 		break;
2674f92363d1SSreekanth Reddy 	}
2675f92363d1SSreekanth Reddy 	case MPT3EVENTQUERY:
2676f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2677f92363d1SSreekanth Reddy 			ret = _ctl_eventquery(ioc, arg);
2678f92363d1SSreekanth Reddy 		break;
2679f92363d1SSreekanth Reddy 	case MPT3EVENTENABLE:
2680f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2681f92363d1SSreekanth Reddy 			ret = _ctl_eventenable(ioc, arg);
2682f92363d1SSreekanth Reddy 		break;
2683f92363d1SSreekanth Reddy 	case MPT3EVENTREPORT:
2684f92363d1SSreekanth Reddy 		ret = _ctl_eventreport(ioc, arg);
2685f92363d1SSreekanth Reddy 		break;
2686f92363d1SSreekanth Reddy 	case MPT3HARDRESET:
2687f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2688f92363d1SSreekanth Reddy 			ret = _ctl_do_reset(ioc, arg);
2689f92363d1SSreekanth Reddy 		break;
2690f92363d1SSreekanth Reddy 	case MPT3BTDHMAPPING:
2691f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2692f92363d1SSreekanth Reddy 			ret = _ctl_btdh_mapping(ioc, arg);
2693f92363d1SSreekanth Reddy 		break;
2694f92363d1SSreekanth Reddy 	case MPT3DIAGREGISTER:
2695f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2696f92363d1SSreekanth Reddy 			ret = _ctl_diag_register(ioc, arg);
2697f92363d1SSreekanth Reddy 		break;
2698f92363d1SSreekanth Reddy 	case MPT3DIAGUNREGISTER:
2699f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2700f92363d1SSreekanth Reddy 			ret = _ctl_diag_unregister(ioc, arg);
2701f92363d1SSreekanth Reddy 		break;
2702f92363d1SSreekanth Reddy 	case MPT3DIAGQUERY:
2703f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2704f92363d1SSreekanth Reddy 			ret = _ctl_diag_query(ioc, arg);
2705f92363d1SSreekanth Reddy 		break;
2706f92363d1SSreekanth Reddy 	case MPT3DIAGRELEASE:
2707f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2708f92363d1SSreekanth Reddy 			ret = _ctl_diag_release(ioc, arg);
2709f92363d1SSreekanth Reddy 		break;
2710f92363d1SSreekanth Reddy 	case MPT3DIAGREADBUFFER:
2711f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2712f92363d1SSreekanth Reddy 			ret = _ctl_diag_read_buffer(ioc, arg);
2713f92363d1SSreekanth Reddy 		break;
2714688c1a0aSSuganath Prabu S 	case MPT3ADDNLDIAGQUERY:
2715688c1a0aSSuganath Prabu S 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_addnl_diag_query))
2716688c1a0aSSuganath Prabu S 			ret = _ctl_addnl_diag_query(ioc, arg);
2717688c1a0aSSuganath Prabu S 		break;
2718f92363d1SSreekanth Reddy 	default:
2719919d8a3fSJoe Perches 		dctlprintk(ioc,
2720919d8a3fSJoe Perches 			   ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2721919d8a3fSJoe Perches 				    cmd));
2722f92363d1SSreekanth Reddy 		break;
2723f92363d1SSreekanth Reddy 	}
2724f92363d1SSreekanth Reddy 
2725f92363d1SSreekanth Reddy 	mutex_unlock(&ioc->ctl_cmds.mutex);
272608c4d550SSreekanth Reddy out_unlock_pciaccess:
272708c4d550SSreekanth Reddy 	mutex_unlock(&ioc->pci_access_mutex);
2728f92363d1SSreekanth Reddy 	return ret;
2729f92363d1SSreekanth Reddy }
2730f92363d1SSreekanth Reddy 
2731f92363d1SSreekanth Reddy /**
2732c84b06a4SSreekanth Reddy  * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
27334beb4867SBart Van Assche  * @file: (struct file)
27344beb4867SBart Van Assche  * @cmd: ioctl opcode
27354beb4867SBart Van Assche  * @arg: ?
2736f92363d1SSreekanth Reddy  */
27378bbb1cf6SCalvin Owens static long
2738c84b06a4SSreekanth Reddy _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2739f92363d1SSreekanth Reddy {
2740f92363d1SSreekanth Reddy 	long ret;
2741f92363d1SSreekanth Reddy 
2742b130b0d5SSuganath prabu Subramani 	/* pass MPI25_VERSION | MPI26_VERSION value,
2743b130b0d5SSuganath prabu Subramani 	 * to indicate that this ioctl cmd
2744c84b06a4SSreekanth Reddy 	 * came from mpt3ctl ioctl device.
2745c84b06a4SSreekanth Reddy 	 */
2746b130b0d5SSuganath prabu Subramani 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2747b130b0d5SSuganath prabu Subramani 		MPI25_VERSION | MPI26_VERSION);
2748f92363d1SSreekanth Reddy 	return ret;
2749f92363d1SSreekanth Reddy }
2750f92363d1SSreekanth Reddy 
2751c84b06a4SSreekanth Reddy /**
2752c84b06a4SSreekanth Reddy  * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
27534beb4867SBart Van Assche  * @file: (struct file)
27544beb4867SBart Van Assche  * @cmd: ioctl opcode
27554beb4867SBart Van Assche  * @arg: ?
2756c84b06a4SSreekanth Reddy  */
27578bbb1cf6SCalvin Owens static long
2758c84b06a4SSreekanth Reddy _ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2759c84b06a4SSreekanth Reddy {
2760c84b06a4SSreekanth Reddy 	long ret;
2761c84b06a4SSreekanth Reddy 
2762c84b06a4SSreekanth Reddy 	/* pass MPI2_VERSION value, to indicate that this ioctl cmd
2763c84b06a4SSreekanth Reddy 	 * came from mpt2ctl ioctl device.
2764c84b06a4SSreekanth Reddy 	 */
2765c84b06a4SSreekanth Reddy 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2766c84b06a4SSreekanth Reddy 	return ret;
2767c84b06a4SSreekanth Reddy }
2768f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2769f92363d1SSreekanth Reddy /**
2770c84b06a4SSreekanth Reddy  * _ctl_ioctl_compat - main ioctl entry point (compat)
27714beb4867SBart Van Assche  * @file: ?
27724beb4867SBart Van Assche  * @cmd: ?
27734beb4867SBart Van Assche  * @arg: ?
2774f92363d1SSreekanth Reddy  *
2775f92363d1SSreekanth Reddy  * This routine handles 32 bit applications in 64bit os.
2776f92363d1SSreekanth Reddy  */
27778bbb1cf6SCalvin Owens static long
2778c84b06a4SSreekanth Reddy _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2779f92363d1SSreekanth Reddy {
2780f92363d1SSreekanth Reddy 	long ret;
2781f92363d1SSreekanth Reddy 
2782b130b0d5SSuganath prabu Subramani 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2783b130b0d5SSuganath prabu Subramani 		MPI25_VERSION | MPI26_VERSION);
2784c84b06a4SSreekanth Reddy 	return ret;
2785c84b06a4SSreekanth Reddy }
2786c84b06a4SSreekanth Reddy 
2787c84b06a4SSreekanth Reddy /**
2788c84b06a4SSreekanth Reddy  * _ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
27894beb4867SBart Van Assche  * @file: ?
27904beb4867SBart Van Assche  * @cmd: ?
27914beb4867SBart Van Assche  * @arg: ?
2792c84b06a4SSreekanth Reddy  *
2793c84b06a4SSreekanth Reddy  * This routine handles 32 bit applications in 64bit os.
2794c84b06a4SSreekanth Reddy  */
27958bbb1cf6SCalvin Owens static long
2796c84b06a4SSreekanth Reddy _ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2797c84b06a4SSreekanth Reddy {
2798c84b06a4SSreekanth Reddy 	long ret;
2799c84b06a4SSreekanth Reddy 
2800c84b06a4SSreekanth Reddy 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
2801f92363d1SSreekanth Reddy 	return ret;
2802f92363d1SSreekanth Reddy }
2803f92363d1SSreekanth Reddy #endif
2804f92363d1SSreekanth Reddy 
2805f92363d1SSreekanth Reddy /* scsi host attributes */
2806f92363d1SSreekanth Reddy /**
2807c9df1442STomas Henzl  * version_fw_show - firmware version
28084beb4867SBart Van Assche  * @cdev: pointer to embedded class device
28094beb4867SBart Van Assche  * @attr: ?
28104beb4867SBart Van Assche  * @buf: the buffer returned
2811f92363d1SSreekanth Reddy  *
2812f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2813f92363d1SSreekanth Reddy  */
2814f92363d1SSreekanth Reddy static ssize_t
2815c9df1442STomas Henzl version_fw_show(struct device *cdev, struct device_attribute *attr,
2816f92363d1SSreekanth Reddy 	char *buf)
2817f92363d1SSreekanth Reddy {
2818f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2819f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2820f92363d1SSreekanth Reddy 
2821f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2822f92363d1SSreekanth Reddy 	    (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2823f92363d1SSreekanth Reddy 	    (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2824f92363d1SSreekanth Reddy 	    (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2825f92363d1SSreekanth Reddy 	    ioc->facts.FWVersion.Word & 0x000000FF);
2826f92363d1SSreekanth Reddy }
2827c9df1442STomas Henzl static DEVICE_ATTR_RO(version_fw);
2828f92363d1SSreekanth Reddy 
2829f92363d1SSreekanth Reddy /**
2830c9df1442STomas Henzl  * version_bios_show - bios version
28314beb4867SBart Van Assche  * @cdev: pointer to embedded class device
28324beb4867SBart Van Assche  * @attr: ?
28334beb4867SBart Van Assche  * @buf: the buffer returned
2834f92363d1SSreekanth Reddy  *
2835f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2836f92363d1SSreekanth Reddy  */
2837f92363d1SSreekanth Reddy static ssize_t
2838c9df1442STomas Henzl version_bios_show(struct device *cdev, struct device_attribute *attr,
2839f92363d1SSreekanth Reddy 	char *buf)
2840f92363d1SSreekanth Reddy {
2841f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2842f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2843f92363d1SSreekanth Reddy 
2844f92363d1SSreekanth Reddy 	u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2845f92363d1SSreekanth Reddy 
2846f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2847f92363d1SSreekanth Reddy 	    (version & 0xFF000000) >> 24,
2848f92363d1SSreekanth Reddy 	    (version & 0x00FF0000) >> 16,
2849f92363d1SSreekanth Reddy 	    (version & 0x0000FF00) >> 8,
2850f92363d1SSreekanth Reddy 	    version & 0x000000FF);
2851f92363d1SSreekanth Reddy }
2852c9df1442STomas Henzl static DEVICE_ATTR_RO(version_bios);
2853f92363d1SSreekanth Reddy 
2854f92363d1SSreekanth Reddy /**
2855c9df1442STomas Henzl  * version_mpi_show - MPI (message passing interface) version
28564beb4867SBart Van Assche  * @cdev: pointer to embedded class device
28574beb4867SBart Van Assche  * @attr: ?
28584beb4867SBart Van Assche  * @buf: the buffer returned
2859f92363d1SSreekanth Reddy  *
2860f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2861f92363d1SSreekanth Reddy  */
2862f92363d1SSreekanth Reddy static ssize_t
2863c9df1442STomas Henzl version_mpi_show(struct device *cdev, struct device_attribute *attr,
2864f92363d1SSreekanth Reddy 	char *buf)
2865f92363d1SSreekanth Reddy {
2866f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2867f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2868f92363d1SSreekanth Reddy 
2869f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2870f92363d1SSreekanth Reddy 	    ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2871f92363d1SSreekanth Reddy }
2872c9df1442STomas Henzl static DEVICE_ATTR_RO(version_mpi);
2873f92363d1SSreekanth Reddy 
2874f92363d1SSreekanth Reddy /**
2875c9df1442STomas Henzl  * version_product_show - product name
28764beb4867SBart Van Assche  * @cdev: pointer to embedded class device
28774beb4867SBart Van Assche  * @attr: ?
28784beb4867SBart Van Assche  * @buf: the buffer returned
2879f92363d1SSreekanth Reddy  *
2880f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2881f92363d1SSreekanth Reddy  */
2882f92363d1SSreekanth Reddy static ssize_t
2883c9df1442STomas Henzl version_product_show(struct device *cdev, struct device_attribute *attr,
2884f92363d1SSreekanth Reddy 	char *buf)
2885f92363d1SSreekanth Reddy {
2886f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2887f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2888f92363d1SSreekanth Reddy 
2889f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2890f92363d1SSreekanth Reddy }
2891c9df1442STomas Henzl static DEVICE_ATTR_RO(version_product);
2892f92363d1SSreekanth Reddy 
2893f92363d1SSreekanth Reddy /**
2894c9df1442STomas Henzl  * version_nvdata_persistent_show - ndvata persistent version
28954beb4867SBart Van Assche  * @cdev: pointer to embedded class device
28964beb4867SBart Van Assche  * @attr: ?
28974beb4867SBart Van Assche  * @buf: the buffer returned
2898f92363d1SSreekanth Reddy  *
2899f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2900f92363d1SSreekanth Reddy  */
2901f92363d1SSreekanth Reddy static ssize_t
2902c9df1442STomas Henzl version_nvdata_persistent_show(struct device *cdev,
2903f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
2904f92363d1SSreekanth Reddy {
2905f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2906f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2907f92363d1SSreekanth Reddy 
2908f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2909f92363d1SSreekanth Reddy 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2910f92363d1SSreekanth Reddy }
2911c9df1442STomas Henzl static DEVICE_ATTR_RO(version_nvdata_persistent);
2912f92363d1SSreekanth Reddy 
2913f92363d1SSreekanth Reddy /**
2914c9df1442STomas Henzl  * version_nvdata_default_show - nvdata default version
29154beb4867SBart Van Assche  * @cdev: pointer to embedded class device
29164beb4867SBart Van Assche  * @attr: ?
29174beb4867SBart Van Assche  * @buf: the buffer returned
2918f92363d1SSreekanth Reddy  *
2919f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2920f92363d1SSreekanth Reddy  */
2921f92363d1SSreekanth Reddy static ssize_t
2922c9df1442STomas Henzl version_nvdata_default_show(struct device *cdev, struct device_attribute
2923f92363d1SSreekanth Reddy 	*attr, char *buf)
2924f92363d1SSreekanth Reddy {
2925f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2926f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2927f92363d1SSreekanth Reddy 
2928f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2929f92363d1SSreekanth Reddy 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2930f92363d1SSreekanth Reddy }
2931c9df1442STomas Henzl static DEVICE_ATTR_RO(version_nvdata_default);
2932f92363d1SSreekanth Reddy 
2933f92363d1SSreekanth Reddy /**
2934c9df1442STomas Henzl  * board_name_show - board name
29354beb4867SBart Van Assche  * @cdev: pointer to embedded class device
29364beb4867SBart Van Assche  * @attr: ?
29374beb4867SBart Van Assche  * @buf: the buffer returned
2938f92363d1SSreekanth Reddy  *
2939f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2940f92363d1SSreekanth Reddy  */
2941f92363d1SSreekanth Reddy static ssize_t
2942c9df1442STomas Henzl board_name_show(struct device *cdev, struct device_attribute *attr,
2943f92363d1SSreekanth Reddy 	char *buf)
2944f92363d1SSreekanth Reddy {
2945f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2946f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2947f92363d1SSreekanth Reddy 
2948f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2949f92363d1SSreekanth Reddy }
2950c9df1442STomas Henzl static DEVICE_ATTR_RO(board_name);
2951f92363d1SSreekanth Reddy 
2952f92363d1SSreekanth Reddy /**
2953c9df1442STomas Henzl  * board_assembly_show - board assembly name
29544beb4867SBart Van Assche  * @cdev: pointer to embedded class device
29554beb4867SBart Van Assche  * @attr: ?
29564beb4867SBart Van Assche  * @buf: the buffer returned
2957f92363d1SSreekanth Reddy  *
2958f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2959f92363d1SSreekanth Reddy  */
2960f92363d1SSreekanth Reddy static ssize_t
2961c9df1442STomas Henzl board_assembly_show(struct device *cdev, struct device_attribute *attr,
2962f92363d1SSreekanth Reddy 	char *buf)
2963f92363d1SSreekanth Reddy {
2964f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2965f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2966f92363d1SSreekanth Reddy 
2967f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2968f92363d1SSreekanth Reddy }
2969c9df1442STomas Henzl static DEVICE_ATTR_RO(board_assembly);
2970f92363d1SSreekanth Reddy 
2971f92363d1SSreekanth Reddy /**
2972c9df1442STomas Henzl  * board_tracer_show - board tracer number
29734beb4867SBart Van Assche  * @cdev: pointer to embedded class device
29744beb4867SBart Van Assche  * @attr: ?
29754beb4867SBart Van Assche  * @buf: the buffer returned
2976f92363d1SSreekanth Reddy  *
2977f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2978f92363d1SSreekanth Reddy  */
2979f92363d1SSreekanth Reddy static ssize_t
2980c9df1442STomas Henzl board_tracer_show(struct device *cdev, struct device_attribute *attr,
2981f92363d1SSreekanth Reddy 	char *buf)
2982f92363d1SSreekanth Reddy {
2983f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2984f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2985f92363d1SSreekanth Reddy 
2986f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2987f92363d1SSreekanth Reddy }
2988c9df1442STomas Henzl static DEVICE_ATTR_RO(board_tracer);
2989f92363d1SSreekanth Reddy 
2990f92363d1SSreekanth Reddy /**
2991c9df1442STomas Henzl  * io_delay_show - io missing delay
29924beb4867SBart Van Assche  * @cdev: pointer to embedded class device
29934beb4867SBart Van Assche  * @attr: ?
29944beb4867SBart Van Assche  * @buf: the buffer returned
2995f92363d1SSreekanth Reddy  *
2996f92363d1SSreekanth Reddy  * This is for firmware implemention for deboucing device
2997f92363d1SSreekanth Reddy  * removal events.
2998f92363d1SSreekanth Reddy  *
2999f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3000f92363d1SSreekanth Reddy  */
3001f92363d1SSreekanth Reddy static ssize_t
3002c9df1442STomas Henzl io_delay_show(struct device *cdev, struct device_attribute *attr,
3003f92363d1SSreekanth Reddy 	char *buf)
3004f92363d1SSreekanth Reddy {
3005f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3006f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3007f92363d1SSreekanth Reddy 
3008f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
3009f92363d1SSreekanth Reddy }
3010c9df1442STomas Henzl static DEVICE_ATTR_RO(io_delay);
3011f92363d1SSreekanth Reddy 
3012f92363d1SSreekanth Reddy /**
3013c9df1442STomas Henzl  * device_delay_show - device missing delay
30144beb4867SBart Van Assche  * @cdev: pointer to embedded class device
30154beb4867SBart Van Assche  * @attr: ?
30164beb4867SBart Van Assche  * @buf: the buffer returned
3017f92363d1SSreekanth Reddy  *
3018f92363d1SSreekanth Reddy  * This is for firmware implemention for deboucing device
3019f92363d1SSreekanth Reddy  * removal events.
3020f92363d1SSreekanth Reddy  *
3021f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3022f92363d1SSreekanth Reddy  */
3023f92363d1SSreekanth Reddy static ssize_t
3024c9df1442STomas Henzl device_delay_show(struct device *cdev, struct device_attribute *attr,
3025f92363d1SSreekanth Reddy 	char *buf)
3026f92363d1SSreekanth Reddy {
3027f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3028f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3029f92363d1SSreekanth Reddy 
3030f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
3031f92363d1SSreekanth Reddy }
3032c9df1442STomas Henzl static DEVICE_ATTR_RO(device_delay);
3033f92363d1SSreekanth Reddy 
3034f92363d1SSreekanth Reddy /**
3035c9df1442STomas Henzl  * fw_queue_depth_show - global credits
30364beb4867SBart Van Assche  * @cdev: pointer to embedded class device
30374beb4867SBart Van Assche  * @attr: ?
30384beb4867SBart Van Assche  * @buf: the buffer returned
3039f92363d1SSreekanth Reddy  *
3040f92363d1SSreekanth Reddy  * This is firmware queue depth limit
3041f92363d1SSreekanth Reddy  *
3042f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3043f92363d1SSreekanth Reddy  */
3044f92363d1SSreekanth Reddy static ssize_t
3045c9df1442STomas Henzl fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
3046f92363d1SSreekanth Reddy 	char *buf)
3047f92363d1SSreekanth Reddy {
3048f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3049f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3050f92363d1SSreekanth Reddy 
3051f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
3052f92363d1SSreekanth Reddy }
3053c9df1442STomas Henzl static DEVICE_ATTR_RO(fw_queue_depth);
3054f92363d1SSreekanth Reddy 
3055f92363d1SSreekanth Reddy /**
3056782a1ab3SLee Jones  * host_sas_address_show - sas address
30574beb4867SBart Van Assche  * @cdev: pointer to embedded class device
30584beb4867SBart Van Assche  * @attr: ?
30594beb4867SBart Van Assche  * @buf: the buffer returned
3060f92363d1SSreekanth Reddy  *
3061f92363d1SSreekanth Reddy  * This is the controller sas address
3062f92363d1SSreekanth Reddy  *
3063f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3064f92363d1SSreekanth Reddy  */
3065f92363d1SSreekanth Reddy static ssize_t
3066c9df1442STomas Henzl host_sas_address_show(struct device *cdev, struct device_attribute *attr,
3067f92363d1SSreekanth Reddy 	char *buf)
3068f92363d1SSreekanth Reddy 
3069f92363d1SSreekanth Reddy {
3070f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3071f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3072f92363d1SSreekanth Reddy 
3073f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3074f92363d1SSreekanth Reddy 	    (unsigned long long)ioc->sas_hba.sas_address);
3075f92363d1SSreekanth Reddy }
3076c9df1442STomas Henzl static DEVICE_ATTR_RO(host_sas_address);
3077f92363d1SSreekanth Reddy 
3078f92363d1SSreekanth Reddy /**
3079c9df1442STomas Henzl  * logging_level_show - logging level
30804beb4867SBart Van Assche  * @cdev: pointer to embedded class device
30814beb4867SBart Van Assche  * @attr: ?
30824beb4867SBart Van Assche  * @buf: the buffer returned
3083f92363d1SSreekanth Reddy  *
3084f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3085f92363d1SSreekanth Reddy  */
3086f92363d1SSreekanth Reddy static ssize_t
3087c9df1442STomas Henzl logging_level_show(struct device *cdev, struct device_attribute *attr,
3088f92363d1SSreekanth Reddy 	char *buf)
3089f92363d1SSreekanth Reddy {
3090f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3091f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3092f92363d1SSreekanth Reddy 
3093f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
3094f92363d1SSreekanth Reddy }
3095f92363d1SSreekanth Reddy static ssize_t
3096c9df1442STomas Henzl logging_level_store(struct device *cdev, struct device_attribute *attr,
3097f92363d1SSreekanth Reddy 	const char *buf, size_t count)
3098f92363d1SSreekanth Reddy {
3099f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3100f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3101f92363d1SSreekanth Reddy 	int val = 0;
3102f92363d1SSreekanth Reddy 
3103f92363d1SSreekanth Reddy 	if (sscanf(buf, "%x", &val) != 1)
3104f92363d1SSreekanth Reddy 		return -EINVAL;
3105f92363d1SSreekanth Reddy 
3106f92363d1SSreekanth Reddy 	ioc->logging_level = val;
3107919d8a3fSJoe Perches 	ioc_info(ioc, "logging_level=%08xh\n",
3108f92363d1SSreekanth Reddy 		 ioc->logging_level);
3109f92363d1SSreekanth Reddy 	return strlen(buf);
3110f92363d1SSreekanth Reddy }
3111c9df1442STomas Henzl static DEVICE_ATTR_RW(logging_level);
3112f92363d1SSreekanth Reddy 
3113f92363d1SSreekanth Reddy /**
3114c9df1442STomas Henzl  * fwfault_debug_show - show/store fwfault_debug
31154beb4867SBart Van Assche  * @cdev: pointer to embedded class device
31164beb4867SBart Van Assche  * @attr: ?
31174beb4867SBart Van Assche  * @buf: the buffer returned
3118f92363d1SSreekanth Reddy  *
3119f92363d1SSreekanth Reddy  * mpt3sas_fwfault_debug is command line option
3120f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3121f92363d1SSreekanth Reddy  */
3122f92363d1SSreekanth Reddy static ssize_t
3123c9df1442STomas Henzl fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
3124f92363d1SSreekanth Reddy 	char *buf)
3125f92363d1SSreekanth Reddy {
3126f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3127f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3128f92363d1SSreekanth Reddy 
3129f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
3130f92363d1SSreekanth Reddy }
3131f92363d1SSreekanth Reddy static ssize_t
3132c9df1442STomas Henzl fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
3133f92363d1SSreekanth Reddy 	const char *buf, size_t count)
3134f92363d1SSreekanth Reddy {
3135f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3136f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3137f92363d1SSreekanth Reddy 	int val = 0;
3138f92363d1SSreekanth Reddy 
3139f92363d1SSreekanth Reddy 	if (sscanf(buf, "%d", &val) != 1)
3140f92363d1SSreekanth Reddy 		return -EINVAL;
3141f92363d1SSreekanth Reddy 
3142f92363d1SSreekanth Reddy 	ioc->fwfault_debug = val;
3143919d8a3fSJoe Perches 	ioc_info(ioc, "fwfault_debug=%d\n",
3144f92363d1SSreekanth Reddy 		 ioc->fwfault_debug);
3145f92363d1SSreekanth Reddy 	return strlen(buf);
3146f92363d1SSreekanth Reddy }
3147c9df1442STomas Henzl static DEVICE_ATTR_RW(fwfault_debug);
3148f92363d1SSreekanth Reddy 
3149f92363d1SSreekanth Reddy /**
3150c9df1442STomas Henzl  * ioc_reset_count_show - ioc reset count
31514beb4867SBart Van Assche  * @cdev: pointer to embedded class device
31524beb4867SBart Van Assche  * @attr: ?
31534beb4867SBart Van Assche  * @buf: the buffer returned
3154f92363d1SSreekanth Reddy  *
3155f92363d1SSreekanth Reddy  * This is firmware queue depth limit
3156f92363d1SSreekanth Reddy  *
3157f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3158f92363d1SSreekanth Reddy  */
3159f92363d1SSreekanth Reddy static ssize_t
3160c9df1442STomas Henzl ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
3161f92363d1SSreekanth Reddy 	char *buf)
3162f92363d1SSreekanth Reddy {
3163f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3164f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3165f92363d1SSreekanth Reddy 
3166f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
3167f92363d1SSreekanth Reddy }
3168c9df1442STomas Henzl static DEVICE_ATTR_RO(ioc_reset_count);
3169f92363d1SSreekanth Reddy 
3170f92363d1SSreekanth Reddy /**
3171c9df1442STomas Henzl  * reply_queue_count_show - number of reply queues
31724beb4867SBart Van Assche  * @cdev: pointer to embedded class device
31734beb4867SBart Van Assche  * @attr: ?
31744beb4867SBart Van Assche  * @buf: the buffer returned
3175f92363d1SSreekanth Reddy  *
3176f92363d1SSreekanth Reddy  * This is number of reply queues
3177f92363d1SSreekanth Reddy  *
3178f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3179f92363d1SSreekanth Reddy  */
3180f92363d1SSreekanth Reddy static ssize_t
3181c9df1442STomas Henzl reply_queue_count_show(struct device *cdev,
3182f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3183f92363d1SSreekanth Reddy {
3184f92363d1SSreekanth Reddy 	u8 reply_queue_count;
3185f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3186f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3187f92363d1SSreekanth Reddy 
3188f92363d1SSreekanth Reddy 	if ((ioc->facts.IOCCapabilities &
3189f92363d1SSreekanth Reddy 	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
3190f92363d1SSreekanth Reddy 		reply_queue_count = ioc->reply_queue_count;
3191f92363d1SSreekanth Reddy 	else
3192f92363d1SSreekanth Reddy 		reply_queue_count = 1;
3193f92363d1SSreekanth Reddy 
3194f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
3195f92363d1SSreekanth Reddy }
3196c9df1442STomas Henzl static DEVICE_ATTR_RO(reply_queue_count);
3197f92363d1SSreekanth Reddy 
319842263095SSreekanth Reddy /**
3199c9df1442STomas Henzl  * BRM_status_show - Backup Rail Monitor Status
32004beb4867SBart Van Assche  * @cdev: pointer to embedded class device
32014beb4867SBart Van Assche  * @attr: ?
32024beb4867SBart Van Assche  * @buf: the buffer returned
320342263095SSreekanth Reddy  *
320442263095SSreekanth Reddy  * This is number of reply queues
320542263095SSreekanth Reddy  *
320642263095SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
320742263095SSreekanth Reddy  */
320842263095SSreekanth Reddy static ssize_t
3209c9df1442STomas Henzl BRM_status_show(struct device *cdev, struct device_attribute *attr,
321042263095SSreekanth Reddy 	char *buf)
321142263095SSreekanth Reddy {
321242263095SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
321342263095SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3214a1c4d774SGustavo A. R. Silva 	Mpi2IOUnitPage3_t io_unit_pg3;
321542263095SSreekanth Reddy 	Mpi2ConfigReply_t mpi_reply;
321642263095SSreekanth Reddy 	u16 backup_rail_monitor_status = 0;
321742263095SSreekanth Reddy 	u16 ioc_status;
321842263095SSreekanth Reddy 	int sz;
321942263095SSreekanth Reddy 	ssize_t rc = 0;
322042263095SSreekanth Reddy 
322142263095SSreekanth Reddy 	if (!ioc->is_warpdrive) {
3222919d8a3fSJoe Perches 		ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
3223919d8a3fSJoe Perches 			__func__);
3224cb551b8dSDamien Le Moal 		return 0;
322542263095SSreekanth Reddy 	}
322608c4d550SSreekanth Reddy 	/* pci_access_mutex lock acquired by sysfs show path */
322708c4d550SSreekanth Reddy 	mutex_lock(&ioc->pci_access_mutex);
32280fd18145SJohannes Thumshirn 	if (ioc->pci_error_recovery || ioc->remove_host)
32290fd18145SJohannes Thumshirn 		goto out;
323042263095SSreekanth Reddy 
3231a1c4d774SGustavo A. R. Silva 	sz = sizeof(io_unit_pg3);
3232a1c4d774SGustavo A. R. Silva 	memset(&io_unit_pg3, 0, sz);
323342263095SSreekanth Reddy 
3234a1c4d774SGustavo A. R. Silva 	if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, &io_unit_pg3, sz) !=
323542263095SSreekanth Reddy 	    0) {
3236919d8a3fSJoe Perches 		ioc_err(ioc, "%s: failed reading iounit_pg3\n",
323742263095SSreekanth Reddy 			__func__);
32380fd18145SJohannes Thumshirn 		rc = -EINVAL;
323942263095SSreekanth Reddy 		goto out;
324042263095SSreekanth Reddy 	}
324142263095SSreekanth Reddy 
324242263095SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
324342263095SSreekanth Reddy 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3244919d8a3fSJoe Perches 		ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
3245919d8a3fSJoe Perches 			__func__, ioc_status);
32460fd18145SJohannes Thumshirn 		rc = -EINVAL;
324742263095SSreekanth Reddy 		goto out;
324842263095SSreekanth Reddy 	}
324942263095SSreekanth Reddy 
3250a1c4d774SGustavo A. R. Silva 	if (io_unit_pg3.GPIOCount < 25) {
3251a1c4d774SGustavo A. R. Silva 		ioc_err(ioc, "%s: iounit_pg3.GPIOCount less than 25 entries, detected (%d) entries\n",
3252a1c4d774SGustavo A. R. Silva 			__func__, io_unit_pg3.GPIOCount);
32530fd18145SJohannes Thumshirn 		rc = -EINVAL;
325442263095SSreekanth Reddy 		goto out;
325542263095SSreekanth Reddy 	}
325642263095SSreekanth Reddy 
325742263095SSreekanth Reddy 	/* BRM status is in bit zero of GPIOVal[24] */
3258a1c4d774SGustavo A. R. Silva 	backup_rail_monitor_status = le16_to_cpu(io_unit_pg3.GPIOVal[24]);
325942263095SSreekanth Reddy 	rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
326042263095SSreekanth Reddy 
326142263095SSreekanth Reddy  out:
326208c4d550SSreekanth Reddy 	mutex_unlock(&ioc->pci_access_mutex);
326342263095SSreekanth Reddy 	return rc;
326442263095SSreekanth Reddy }
3265c9df1442STomas Henzl static DEVICE_ATTR_RO(BRM_status);
326642263095SSreekanth Reddy 
3267f92363d1SSreekanth Reddy struct DIAG_BUFFER_START {
3268f92363d1SSreekanth Reddy 	__le32	Size;
3269f92363d1SSreekanth Reddy 	__le32	DiagVersion;
3270f92363d1SSreekanth Reddy 	u8	BufferType;
3271f92363d1SSreekanth Reddy 	u8	Reserved[3];
3272f92363d1SSreekanth Reddy 	__le32	Reserved1;
3273f92363d1SSreekanth Reddy 	__le32	Reserved2;
3274f92363d1SSreekanth Reddy 	__le32	Reserved3;
3275f92363d1SSreekanth Reddy };
3276f92363d1SSreekanth Reddy 
3277f92363d1SSreekanth Reddy /**
3278c9df1442STomas Henzl  * host_trace_buffer_size_show - host buffer size (trace only)
32794beb4867SBart Van Assche  * @cdev: pointer to embedded class device
32804beb4867SBart Van Assche  * @attr: ?
32814beb4867SBart Van Assche  * @buf: the buffer returned
3282f92363d1SSreekanth Reddy  *
3283f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3284f92363d1SSreekanth Reddy  */
3285f92363d1SSreekanth Reddy static ssize_t
3286c9df1442STomas Henzl host_trace_buffer_size_show(struct device *cdev,
3287f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3288f92363d1SSreekanth Reddy {
3289f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3290f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3291f92363d1SSreekanth Reddy 	u32 size = 0;
3292f92363d1SSreekanth Reddy 	struct DIAG_BUFFER_START *request_data;
3293f92363d1SSreekanth Reddy 
3294f92363d1SSreekanth Reddy 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3295919d8a3fSJoe Perches 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3296919d8a3fSJoe Perches 			__func__);
3297f92363d1SSreekanth Reddy 		return 0;
3298f92363d1SSreekanth Reddy 	}
3299f92363d1SSreekanth Reddy 
3300f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3301f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3302919d8a3fSJoe Perches 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3303919d8a3fSJoe Perches 			__func__);
3304f92363d1SSreekanth Reddy 		return 0;
3305f92363d1SSreekanth Reddy 	}
3306f92363d1SSreekanth Reddy 
3307f92363d1SSreekanth Reddy 	request_data = (struct DIAG_BUFFER_START *)
3308f92363d1SSreekanth Reddy 	    ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
3309f92363d1SSreekanth Reddy 	if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
3310f92363d1SSreekanth Reddy 	    le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
3311f92363d1SSreekanth Reddy 	    le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
3312f92363d1SSreekanth Reddy 	    le32_to_cpu(request_data->Reserved3) == 0x4742444c)
3313f92363d1SSreekanth Reddy 		size = le32_to_cpu(request_data->Size);
3314f92363d1SSreekanth Reddy 
3315f92363d1SSreekanth Reddy 	ioc->ring_buffer_sz = size;
3316f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", size);
3317f92363d1SSreekanth Reddy }
3318c9df1442STomas Henzl static DEVICE_ATTR_RO(host_trace_buffer_size);
3319f92363d1SSreekanth Reddy 
3320f92363d1SSreekanth Reddy /**
3321c9df1442STomas Henzl  * host_trace_buffer_show - firmware ring buffer (trace only)
33224beb4867SBart Van Assche  * @cdev: pointer to embedded class device
33234beb4867SBart Van Assche  * @attr: ?
33244beb4867SBart Van Assche  * @buf: the buffer returned
3325f92363d1SSreekanth Reddy  *
3326f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3327f92363d1SSreekanth Reddy  *
3328f92363d1SSreekanth Reddy  * You will only be able to read 4k bytes of ring buffer at a time.
3329f92363d1SSreekanth Reddy  * In order to read beyond 4k bytes, you will have to write out the
3330f92363d1SSreekanth Reddy  * offset to the same attribute, it will move the pointer.
3331f92363d1SSreekanth Reddy  */
3332f92363d1SSreekanth Reddy static ssize_t
3333c9df1442STomas Henzl host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
3334f92363d1SSreekanth Reddy 	char *buf)
3335f92363d1SSreekanth Reddy {
3336f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3337f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3338f92363d1SSreekanth Reddy 	void *request_data;
3339f92363d1SSreekanth Reddy 	u32 size;
3340f92363d1SSreekanth Reddy 
3341f92363d1SSreekanth Reddy 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3342919d8a3fSJoe Perches 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3343919d8a3fSJoe Perches 			__func__);
3344f92363d1SSreekanth Reddy 		return 0;
3345f92363d1SSreekanth Reddy 	}
3346f92363d1SSreekanth Reddy 
3347f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3348f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3349919d8a3fSJoe Perches 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3350919d8a3fSJoe Perches 			__func__);
3351f92363d1SSreekanth Reddy 		return 0;
3352f92363d1SSreekanth Reddy 	}
3353f92363d1SSreekanth Reddy 
3354f92363d1SSreekanth Reddy 	if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3355f92363d1SSreekanth Reddy 		return 0;
3356f92363d1SSreekanth Reddy 
3357f92363d1SSreekanth Reddy 	size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3358f92363d1SSreekanth Reddy 	size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3359f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3360f92363d1SSreekanth Reddy 	memcpy(buf, request_data, size);
3361f92363d1SSreekanth Reddy 	return size;
3362f92363d1SSreekanth Reddy }
3363f92363d1SSreekanth Reddy 
3364f92363d1SSreekanth Reddy static ssize_t
3365c9df1442STomas Henzl host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
3366f92363d1SSreekanth Reddy 	const char *buf, size_t count)
3367f92363d1SSreekanth Reddy {
3368f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3369f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3370f92363d1SSreekanth Reddy 	int val = 0;
3371f92363d1SSreekanth Reddy 
3372f92363d1SSreekanth Reddy 	if (sscanf(buf, "%d", &val) != 1)
3373f92363d1SSreekanth Reddy 		return -EINVAL;
3374f92363d1SSreekanth Reddy 
3375f92363d1SSreekanth Reddy 	ioc->ring_buffer_offset = val;
3376f92363d1SSreekanth Reddy 	return strlen(buf);
3377f92363d1SSreekanth Reddy }
3378c9df1442STomas Henzl static DEVICE_ATTR_RW(host_trace_buffer);
3379f92363d1SSreekanth Reddy 
3380f92363d1SSreekanth Reddy 
3381f92363d1SSreekanth Reddy /*****************************************/
3382f92363d1SSreekanth Reddy 
3383f92363d1SSreekanth Reddy /**
3384c9df1442STomas Henzl  * host_trace_buffer_enable_show - firmware ring buffer (trace only)
33854beb4867SBart Van Assche  * @cdev: pointer to embedded class device
33864beb4867SBart Van Assche  * @attr: ?
33874beb4867SBart Van Assche  * @buf: the buffer returned
3388f92363d1SSreekanth Reddy  *
3389f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3390f92363d1SSreekanth Reddy  *
3391f92363d1SSreekanth Reddy  * This is a mechnism to post/release host_trace_buffers
3392f92363d1SSreekanth Reddy  */
3393f92363d1SSreekanth Reddy static ssize_t
3394c9df1442STomas Henzl host_trace_buffer_enable_show(struct device *cdev,
3395f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3396f92363d1SSreekanth Reddy {
3397f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3398f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3399f92363d1SSreekanth Reddy 
3400f92363d1SSreekanth Reddy 	if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3401f92363d1SSreekanth Reddy 	   ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3402f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3403f92363d1SSreekanth Reddy 		return snprintf(buf, PAGE_SIZE, "off\n");
3404f92363d1SSreekanth Reddy 	else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3405f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED))
3406f92363d1SSreekanth Reddy 		return snprintf(buf, PAGE_SIZE, "release\n");
3407f92363d1SSreekanth Reddy 	else
3408f92363d1SSreekanth Reddy 		return snprintf(buf, PAGE_SIZE, "post\n");
3409f92363d1SSreekanth Reddy }
3410f92363d1SSreekanth Reddy 
3411f92363d1SSreekanth Reddy static ssize_t
3412c9df1442STomas Henzl host_trace_buffer_enable_store(struct device *cdev,
3413f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3414f92363d1SSreekanth Reddy {
3415f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3416f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3417f92363d1SSreekanth Reddy 	char str[10] = "";
3418f92363d1SSreekanth Reddy 	struct mpt3_diag_register diag_register;
3419f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
3420f92363d1SSreekanth Reddy 
3421f92363d1SSreekanth Reddy 	/* don't allow post/release occurr while recovery is active */
3422f92363d1SSreekanth Reddy 	if (ioc->shost_recovery || ioc->remove_host ||
3423f92363d1SSreekanth Reddy 	    ioc->pci_error_recovery || ioc->is_driver_loading)
3424f92363d1SSreekanth Reddy 		return -EBUSY;
3425f92363d1SSreekanth Reddy 
3426f92363d1SSreekanth Reddy 	if (sscanf(buf, "%9s", str) != 1)
3427f92363d1SSreekanth Reddy 		return -EINVAL;
3428f92363d1SSreekanth Reddy 
3429f92363d1SSreekanth Reddy 	if (!strcmp(str, "post")) {
3430f92363d1SSreekanth Reddy 		/* exit out if host buffers are already posted */
3431f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3432f92363d1SSreekanth Reddy 		    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3433f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3434f92363d1SSreekanth Reddy 		    ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3435f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3436f92363d1SSreekanth Reddy 			goto out;
3437f92363d1SSreekanth Reddy 		memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3438919d8a3fSJoe Perches 		ioc_info(ioc, "posting host trace buffers\n");
3439f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3440d04a6edfSSreekanth Reddy 
3441d04a6edfSSreekanth Reddy 		if (ioc->manu_pg11.HostTraceBufferMaxSizeKB != 0 &&
3442d04a6edfSSreekanth Reddy 		    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0) {
3443d04a6edfSSreekanth Reddy 			/* post the same buffer allocated previously */
3444d04a6edfSSreekanth Reddy 			diag_register.requested_buffer_size =
3445d04a6edfSSreekanth Reddy 			    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE];
3446a8a6cbcdSSreekanth Reddy 		} else {
3447a8a6cbcdSSreekanth Reddy 			/*
3448a8a6cbcdSSreekanth Reddy 			 * Free the diag buffer memory which was previously
3449a8a6cbcdSSreekanth Reddy 			 * allocated by an application.
3450a8a6cbcdSSreekanth Reddy 			 */
3451a8a6cbcdSSreekanth Reddy 			if ((ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0)
3452a8a6cbcdSSreekanth Reddy 			    &&
3453a8a6cbcdSSreekanth Reddy 			    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3454a8a6cbcdSSreekanth Reddy 			    MPT3_DIAG_BUFFER_IS_APP_OWNED)) {
3455a5a20c4aSSuraj Upadhyay 				dma_free_coherent(&ioc->pdev->dev,
3456a5a20c4aSSuraj Upadhyay 						  ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE],
3457a8a6cbcdSSreekanth Reddy 						  ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE],
3458a5a20c4aSSuraj Upadhyay 						  ioc->diag_buffer_dma[MPI2_DIAG_BUF_TYPE_TRACE]);
3459a8a6cbcdSSreekanth Reddy 				ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE] =
3460a8a6cbcdSSreekanth Reddy 				    NULL;
3461a8a6cbcdSSreekanth Reddy 			}
3462a8a6cbcdSSreekanth Reddy 
3463f92363d1SSreekanth Reddy 			diag_register.requested_buffer_size = (1024 * 1024);
3464a8a6cbcdSSreekanth Reddy 		}
3465d04a6edfSSreekanth Reddy 
346608e7378eSSreekanth Reddy 		diag_register.unique_id =
346708e7378eSSreekanth Reddy 		    (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
346808e7378eSSreekanth Reddy 		    (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
3469f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3470f92363d1SSreekanth Reddy 		_ctl_diag_register_2(ioc,  &diag_register);
3471a066f4c3SSreekanth Reddy 		if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3472a066f4c3SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED) {
3473a066f4c3SSreekanth Reddy 			ioc_info(ioc,
3474a066f4c3SSreekanth Reddy 			    "Trace buffer %d KB allocated through sysfs\n",
3475a066f4c3SSreekanth Reddy 			    diag_register.requested_buffer_size>>10);
3476a066f4c3SSreekanth Reddy 			if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
3477a066f4c3SSreekanth Reddy 				ioc->diag_buffer_status[
3478a066f4c3SSreekanth Reddy 				    MPI2_DIAG_BUF_TYPE_TRACE] |=
3479a066f4c3SSreekanth Reddy 				    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
3480a066f4c3SSreekanth Reddy 		}
3481f92363d1SSreekanth Reddy 	} else if (!strcmp(str, "release")) {
3482f92363d1SSreekanth Reddy 		/* exit out if host buffers are already released */
3483f92363d1SSreekanth Reddy 		if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3484f92363d1SSreekanth Reddy 			goto out;
3485f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3486f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3487f92363d1SSreekanth Reddy 			goto out;
3488f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3489f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED))
3490f92363d1SSreekanth Reddy 			goto out;
3491919d8a3fSJoe Perches 		ioc_info(ioc, "releasing host trace buffer\n");
3492688c1a0aSSuganath Prabu S 		ioc->htb_rel.buffer_rel_condition = MPT3_DIAG_BUFFER_REL_SYSFS;
3493f92363d1SSreekanth Reddy 		mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3494f92363d1SSreekanth Reddy 		    &issue_reset);
3495f92363d1SSreekanth Reddy 	}
3496f92363d1SSreekanth Reddy 
3497f92363d1SSreekanth Reddy  out:
3498f92363d1SSreekanth Reddy 	return strlen(buf);
3499f92363d1SSreekanth Reddy }
3500c9df1442STomas Henzl static DEVICE_ATTR_RW(host_trace_buffer_enable);
3501f92363d1SSreekanth Reddy 
3502f92363d1SSreekanth Reddy /*********** diagnostic trigger suppport *********************************/
3503f92363d1SSreekanth Reddy 
3504f92363d1SSreekanth Reddy /**
3505c9df1442STomas Henzl  * diag_trigger_master_show - show the diag_trigger_master attribute
35064beb4867SBart Van Assche  * @cdev: pointer to embedded class device
35074beb4867SBart Van Assche  * @attr: ?
35084beb4867SBart Van Assche  * @buf: the buffer returned
3509f92363d1SSreekanth Reddy  *
3510f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3511f92363d1SSreekanth Reddy  */
3512f92363d1SSreekanth Reddy static ssize_t
3513c9df1442STomas Henzl diag_trigger_master_show(struct device *cdev,
3514f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3515f92363d1SSreekanth Reddy 
3516f92363d1SSreekanth Reddy {
3517f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3518f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3519f92363d1SSreekanth Reddy 	unsigned long flags;
3520f92363d1SSreekanth Reddy 	ssize_t rc;
3521f92363d1SSreekanth Reddy 
3522f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3523f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3524f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_master, rc);
3525f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3526f92363d1SSreekanth Reddy 	return rc;
3527f92363d1SSreekanth Reddy }
3528f92363d1SSreekanth Reddy 
3529f92363d1SSreekanth Reddy /**
3530c9df1442STomas Henzl  * diag_trigger_master_store - store the diag_trigger_master attribute
35314beb4867SBart Van Assche  * @cdev: pointer to embedded class device
35324beb4867SBart Van Assche  * @attr: ?
35334beb4867SBart Van Assche  * @buf: the buffer returned
35344beb4867SBart Van Assche  * @count: ?
3535f92363d1SSreekanth Reddy  *
3536f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3537f92363d1SSreekanth Reddy  */
3538f92363d1SSreekanth Reddy static ssize_t
3539c9df1442STomas Henzl diag_trigger_master_store(struct device *cdev,
3540f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3541f92363d1SSreekanth Reddy 
3542f92363d1SSreekanth Reddy {
3543f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3544f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
35459211faa3SSuganath Prabu S 	struct SL_WH_MASTER_TRIGGER_T *master_tg;
3546f92363d1SSreekanth Reddy 	unsigned long flags;
3547f92363d1SSreekanth Reddy 	ssize_t rc;
35489211faa3SSuganath Prabu S 	bool set = 1;
35499211faa3SSuganath Prabu S 
35509211faa3SSuganath Prabu S 	rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
35519211faa3SSuganath Prabu S 
35529211faa3SSuganath Prabu S 	if (ioc->supports_trigger_pages) {
35539211faa3SSuganath Prabu S 		master_tg = kzalloc(sizeof(struct SL_WH_MASTER_TRIGGER_T),
35549211faa3SSuganath Prabu S 		    GFP_KERNEL);
35559211faa3SSuganath Prabu S 		if (!master_tg)
35569211faa3SSuganath Prabu S 			return -ENOMEM;
35579211faa3SSuganath Prabu S 
35589211faa3SSuganath Prabu S 		memcpy(master_tg, buf, rc);
35599211faa3SSuganath Prabu S 		if (!master_tg->MasterData)
35609211faa3SSuganath Prabu S 			set = 0;
35619211faa3SSuganath Prabu S 		if (mpt3sas_config_update_driver_trigger_pg1(ioc, master_tg,
35629211faa3SSuganath Prabu S 		    set)) {
35639211faa3SSuganath Prabu S 			kfree(master_tg);
35649211faa3SSuganath Prabu S 			return -EFAULT;
35659211faa3SSuganath Prabu S 		}
35669211faa3SSuganath Prabu S 		kfree(master_tg);
35679211faa3SSuganath Prabu S 	}
3568f92363d1SSreekanth Reddy 
3569f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3570f92363d1SSreekanth Reddy 	memset(&ioc->diag_trigger_master, 0,
3571f92363d1SSreekanth Reddy 	    sizeof(struct SL_WH_MASTER_TRIGGER_T));
3572f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_master, buf, rc);
3573f92363d1SSreekanth Reddy 	ioc->diag_trigger_master.MasterData |=
3574f92363d1SSreekanth Reddy 	    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3575f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3576f92363d1SSreekanth Reddy 	return rc;
3577f92363d1SSreekanth Reddy }
3578c9df1442STomas Henzl static DEVICE_ATTR_RW(diag_trigger_master);
3579f92363d1SSreekanth Reddy 
3580f92363d1SSreekanth Reddy 
3581f92363d1SSreekanth Reddy /**
3582c9df1442STomas Henzl  * diag_trigger_event_show - show the diag_trigger_event attribute
35834beb4867SBart Van Assche  * @cdev: pointer to embedded class device
35844beb4867SBart Van Assche  * @attr: ?
35854beb4867SBart Van Assche  * @buf: the buffer returned
3586f92363d1SSreekanth Reddy  *
3587f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3588f92363d1SSreekanth Reddy  */
3589f92363d1SSreekanth Reddy static ssize_t
3590c9df1442STomas Henzl diag_trigger_event_show(struct device *cdev,
3591f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3592f92363d1SSreekanth Reddy {
3593f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3594f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3595f92363d1SSreekanth Reddy 	unsigned long flags;
3596f92363d1SSreekanth Reddy 	ssize_t rc;
3597f92363d1SSreekanth Reddy 
3598f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3599f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3600f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_event, rc);
3601f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3602f92363d1SSreekanth Reddy 	return rc;
3603f92363d1SSreekanth Reddy }
3604f92363d1SSreekanth Reddy 
3605f92363d1SSreekanth Reddy /**
3606c9df1442STomas Henzl  * diag_trigger_event_store - store the diag_trigger_event attribute
36074beb4867SBart Van Assche  * @cdev: pointer to embedded class device
36084beb4867SBart Van Assche  * @attr: ?
36094beb4867SBart Van Assche  * @buf: the buffer returned
36104beb4867SBart Van Assche  * @count: ?
3611f92363d1SSreekanth Reddy  *
3612f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3613f92363d1SSreekanth Reddy  */
3614f92363d1SSreekanth Reddy static ssize_t
3615c9df1442STomas Henzl diag_trigger_event_store(struct device *cdev,
3616f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3617f92363d1SSreekanth Reddy 
3618f92363d1SSreekanth Reddy {
3619f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3620f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
36219211faa3SSuganath Prabu S 	struct SL_WH_EVENT_TRIGGERS_T *event_tg;
3622f92363d1SSreekanth Reddy 	unsigned long flags;
3623f92363d1SSreekanth Reddy 	ssize_t sz;
36249211faa3SSuganath Prabu S 	bool set = 1;
36259211faa3SSuganath Prabu S 
36269211faa3SSuganath Prabu S 	sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
36279211faa3SSuganath Prabu S 	if (ioc->supports_trigger_pages) {
36289211faa3SSuganath Prabu S 		event_tg = kzalloc(sizeof(struct SL_WH_EVENT_TRIGGERS_T),
36299211faa3SSuganath Prabu S 		    GFP_KERNEL);
36309211faa3SSuganath Prabu S 		if (!event_tg)
36319211faa3SSuganath Prabu S 			return -ENOMEM;
36329211faa3SSuganath Prabu S 
36339211faa3SSuganath Prabu S 		memcpy(event_tg, buf, sz);
36349211faa3SSuganath Prabu S 		if (!event_tg->ValidEntries)
36359211faa3SSuganath Prabu S 			set = 0;
36369211faa3SSuganath Prabu S 		if (mpt3sas_config_update_driver_trigger_pg2(ioc, event_tg,
36379211faa3SSuganath Prabu S 		    set)) {
36389211faa3SSuganath Prabu S 			kfree(event_tg);
36399211faa3SSuganath Prabu S 			return -EFAULT;
36409211faa3SSuganath Prabu S 		}
36419211faa3SSuganath Prabu S 		kfree(event_tg);
36429211faa3SSuganath Prabu S 	}
3643f92363d1SSreekanth Reddy 
3644f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
36459211faa3SSuganath Prabu S 
3646f92363d1SSreekanth Reddy 	memset(&ioc->diag_trigger_event, 0,
3647f92363d1SSreekanth Reddy 	    sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3648f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_event, buf, sz);
3649f92363d1SSreekanth Reddy 	if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3650f92363d1SSreekanth Reddy 		ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3651f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3652f92363d1SSreekanth Reddy 	return sz;
3653f92363d1SSreekanth Reddy }
3654c9df1442STomas Henzl static DEVICE_ATTR_RW(diag_trigger_event);
3655f92363d1SSreekanth Reddy 
3656f92363d1SSreekanth Reddy 
3657f92363d1SSreekanth Reddy /**
3658c9df1442STomas Henzl  * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
36594beb4867SBart Van Assche  * @cdev: pointer to embedded class device
36604beb4867SBart Van Assche  * @attr: ?
36614beb4867SBart Van Assche  * @buf: the buffer returned
3662f92363d1SSreekanth Reddy  *
3663f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3664f92363d1SSreekanth Reddy  */
3665f92363d1SSreekanth Reddy static ssize_t
3666c9df1442STomas Henzl diag_trigger_scsi_show(struct device *cdev,
3667f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3668f92363d1SSreekanth Reddy {
3669f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3670f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3671f92363d1SSreekanth Reddy 	unsigned long flags;
3672f92363d1SSreekanth Reddy 	ssize_t rc;
3673f92363d1SSreekanth Reddy 
3674f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3675f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3676f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_scsi, rc);
3677f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3678f92363d1SSreekanth Reddy 	return rc;
3679f92363d1SSreekanth Reddy }
3680f92363d1SSreekanth Reddy 
3681f92363d1SSreekanth Reddy /**
3682c9df1442STomas Henzl  * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
36834beb4867SBart Van Assche  * @cdev: pointer to embedded class device
36844beb4867SBart Van Assche  * @attr: ?
36854beb4867SBart Van Assche  * @buf: the buffer returned
36864beb4867SBart Van Assche  * @count: ?
3687f92363d1SSreekanth Reddy  *
3688f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3689f92363d1SSreekanth Reddy  */
3690f92363d1SSreekanth Reddy static ssize_t
3691c9df1442STomas Henzl diag_trigger_scsi_store(struct device *cdev,
3692f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3693f92363d1SSreekanth Reddy {
3694f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3695f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
36969211faa3SSuganath Prabu S 	struct SL_WH_SCSI_TRIGGERS_T *scsi_tg;
3697f92363d1SSreekanth Reddy 	unsigned long flags;
3698f92363d1SSreekanth Reddy 	ssize_t sz;
36999211faa3SSuganath Prabu S 	bool set = 1;
37009211faa3SSuganath Prabu S 
37019211faa3SSuganath Prabu S 	sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
37029211faa3SSuganath Prabu S 	if (ioc->supports_trigger_pages) {
37039211faa3SSuganath Prabu S 		scsi_tg = kzalloc(sizeof(struct SL_WH_SCSI_TRIGGERS_T),
37049211faa3SSuganath Prabu S 		    GFP_KERNEL);
37059211faa3SSuganath Prabu S 		if (!scsi_tg)
37069211faa3SSuganath Prabu S 			return -ENOMEM;
37079211faa3SSuganath Prabu S 
37089211faa3SSuganath Prabu S 		memcpy(scsi_tg, buf, sz);
37099211faa3SSuganath Prabu S 		if (!scsi_tg->ValidEntries)
37109211faa3SSuganath Prabu S 			set = 0;
37119211faa3SSuganath Prabu S 		if (mpt3sas_config_update_driver_trigger_pg3(ioc, scsi_tg,
37129211faa3SSuganath Prabu S 		    set)) {
37139211faa3SSuganath Prabu S 			kfree(scsi_tg);
37149211faa3SSuganath Prabu S 			return -EFAULT;
37159211faa3SSuganath Prabu S 		}
37169211faa3SSuganath Prabu S 		kfree(scsi_tg);
37179211faa3SSuganath Prabu S 	}
3718f92363d1SSreekanth Reddy 
3719f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
37209211faa3SSuganath Prabu S 
37211de540a9SDan Carpenter 	memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
3722f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_scsi, buf, sz);
3723f92363d1SSreekanth Reddy 	if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3724f92363d1SSreekanth Reddy 		ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3725f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3726f92363d1SSreekanth Reddy 	return sz;
3727f92363d1SSreekanth Reddy }
3728c9df1442STomas Henzl static DEVICE_ATTR_RW(diag_trigger_scsi);
3729f92363d1SSreekanth Reddy 
3730f92363d1SSreekanth Reddy 
3731f92363d1SSreekanth Reddy /**
3732782a1ab3SLee Jones  * diag_trigger_mpi_show - show the diag_trigger_mpi attribute
37334beb4867SBart Van Assche  * @cdev: pointer to embedded class device
37344beb4867SBart Van Assche  * @attr: ?
37354beb4867SBart Van Assche  * @buf: the buffer returned
3736f92363d1SSreekanth Reddy  *
3737f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3738f92363d1SSreekanth Reddy  */
3739f92363d1SSreekanth Reddy static ssize_t
3740c9df1442STomas Henzl diag_trigger_mpi_show(struct device *cdev,
3741f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3742f92363d1SSreekanth Reddy {
3743f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3744f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3745f92363d1SSreekanth Reddy 	unsigned long flags;
3746f92363d1SSreekanth Reddy 	ssize_t rc;
3747f92363d1SSreekanth Reddy 
3748f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3749f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3750f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_mpi, rc);
3751f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3752f92363d1SSreekanth Reddy 	return rc;
3753f92363d1SSreekanth Reddy }
3754f92363d1SSreekanth Reddy 
3755f92363d1SSreekanth Reddy /**
3756c9df1442STomas Henzl  * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
37574beb4867SBart Van Assche  * @cdev: pointer to embedded class device
37584beb4867SBart Van Assche  * @attr: ?
37594beb4867SBart Van Assche  * @buf: the buffer returned
37604beb4867SBart Van Assche  * @count: ?
3761f92363d1SSreekanth Reddy  *
3762f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3763f92363d1SSreekanth Reddy  */
3764f92363d1SSreekanth Reddy static ssize_t
3765c9df1442STomas Henzl diag_trigger_mpi_store(struct device *cdev,
3766f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3767f92363d1SSreekanth Reddy {
3768f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3769f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
37709211faa3SSuganath Prabu S 	struct SL_WH_MPI_TRIGGERS_T *mpi_tg;
3771f92363d1SSreekanth Reddy 	unsigned long flags;
3772f92363d1SSreekanth Reddy 	ssize_t sz;
37739211faa3SSuganath Prabu S 	bool set = 1;
37749211faa3SSuganath Prabu S 
37759211faa3SSuganath Prabu S 	sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
37769211faa3SSuganath Prabu S 	if (ioc->supports_trigger_pages) {
37779211faa3SSuganath Prabu S 		mpi_tg = kzalloc(sizeof(struct SL_WH_MPI_TRIGGERS_T),
37789211faa3SSuganath Prabu S 		    GFP_KERNEL);
37799211faa3SSuganath Prabu S 		if (!mpi_tg)
37809211faa3SSuganath Prabu S 			return -ENOMEM;
37819211faa3SSuganath Prabu S 
37829211faa3SSuganath Prabu S 		memcpy(mpi_tg, buf, sz);
37839211faa3SSuganath Prabu S 		if (!mpi_tg->ValidEntries)
37849211faa3SSuganath Prabu S 			set = 0;
37859211faa3SSuganath Prabu S 		if (mpt3sas_config_update_driver_trigger_pg4(ioc, mpi_tg,
37869211faa3SSuganath Prabu S 		    set)) {
37879211faa3SSuganath Prabu S 			kfree(mpi_tg);
37889211faa3SSuganath Prabu S 			return -EFAULT;
37899211faa3SSuganath Prabu S 		}
37909211faa3SSuganath Prabu S 		kfree(mpi_tg);
37919211faa3SSuganath Prabu S 	}
3792f92363d1SSreekanth Reddy 
3793f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3794f92363d1SSreekanth Reddy 	memset(&ioc->diag_trigger_mpi, 0,
379566331e8cSDan Carpenter 	    sizeof(ioc->diag_trigger_mpi));
3796f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_mpi, buf, sz);
3797f92363d1SSreekanth Reddy 	if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3798f92363d1SSreekanth Reddy 		ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3799f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3800f92363d1SSreekanth Reddy 	return sz;
3801f92363d1SSreekanth Reddy }
3802f92363d1SSreekanth Reddy 
3803c9df1442STomas Henzl static DEVICE_ATTR_RW(diag_trigger_mpi);
3804f92363d1SSreekanth Reddy 
3805f92363d1SSreekanth Reddy /*********** diagnostic trigger suppport *** END ****************************/
3806f92363d1SSreekanth Reddy 
3807f92363d1SSreekanth Reddy /*****************************************/
3808f92363d1SSreekanth Reddy 
38093ac8e47bSSuganath Prabu /**
38103ac8e47bSSuganath Prabu  * drv_support_bitmap_show - driver supported feature bitmap
38119133d27eSDamien Le Moal  * @cdev: pointer to embedded class device
38129133d27eSDamien Le Moal  * @attr: unused
38139133d27eSDamien Le Moal  * @buf: the buffer returned
38143ac8e47bSSuganath Prabu  *
38153ac8e47bSSuganath Prabu  * A sysfs 'read-only' shost attribute.
38163ac8e47bSSuganath Prabu  */
38173ac8e47bSSuganath Prabu static ssize_t
38183ac8e47bSSuganath Prabu drv_support_bitmap_show(struct device *cdev,
38193ac8e47bSSuganath Prabu 	struct device_attribute *attr, char *buf)
38203ac8e47bSSuganath Prabu {
38213ac8e47bSSuganath Prabu 	struct Scsi_Host *shost = class_to_shost(cdev);
38223ac8e47bSSuganath Prabu 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
38233ac8e47bSSuganath Prabu 
38243ac8e47bSSuganath Prabu 	return snprintf(buf, PAGE_SIZE, "0x%08x\n", ioc->drv_support_bitmap);
38253ac8e47bSSuganath Prabu }
38263ac8e47bSSuganath Prabu static DEVICE_ATTR_RO(drv_support_bitmap);
38273ac8e47bSSuganath Prabu 
38288dc8d29aSSreekanth Reddy /**
38298dc8d29aSSreekanth Reddy  * enable_sdev_max_qd_show - display whether sdev max qd is enabled/disabled
38309133d27eSDamien Le Moal  * @cdev: pointer to embedded class device
38319133d27eSDamien Le Moal  * @attr: unused
38329133d27eSDamien Le Moal  * @buf: the buffer returned
38338dc8d29aSSreekanth Reddy  *
38348dc8d29aSSreekanth Reddy  * A sysfs read/write shost attribute. This attribute is used to set the
38358dc8d29aSSreekanth Reddy  * targets queue depth to HBA IO queue depth if this attribute is enabled.
38368dc8d29aSSreekanth Reddy  */
38378dc8d29aSSreekanth Reddy static ssize_t
38388dc8d29aSSreekanth Reddy enable_sdev_max_qd_show(struct device *cdev,
38398dc8d29aSSreekanth Reddy 	struct device_attribute *attr, char *buf)
38408dc8d29aSSreekanth Reddy {
38418dc8d29aSSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
38428dc8d29aSSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
38438dc8d29aSSreekanth Reddy 
38448dc8d29aSSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->enable_sdev_max_qd);
38458dc8d29aSSreekanth Reddy }
38468dc8d29aSSreekanth Reddy 
38478dc8d29aSSreekanth Reddy /**
38488dc8d29aSSreekanth Reddy  * enable_sdev_max_qd_store - Enable/disable sdev max qd
38499133d27eSDamien Le Moal  * @cdev: pointer to embedded class device
38509133d27eSDamien Le Moal  * @attr: unused
38519133d27eSDamien Le Moal  * @buf: the buffer returned
38529133d27eSDamien Le Moal  * @count: unused
38538dc8d29aSSreekanth Reddy  *
38548dc8d29aSSreekanth Reddy  * A sysfs read/write shost attribute. This attribute is used to set the
38558dc8d29aSSreekanth Reddy  * targets queue depth to HBA IO queue depth if this attribute is enabled.
38568dc8d29aSSreekanth Reddy  * If this attribute is disabled then targets will have corresponding default
38578dc8d29aSSreekanth Reddy  * queue depth.
38588dc8d29aSSreekanth Reddy  */
38598dc8d29aSSreekanth Reddy static ssize_t
38608dc8d29aSSreekanth Reddy enable_sdev_max_qd_store(struct device *cdev,
38618dc8d29aSSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
38628dc8d29aSSreekanth Reddy {
38638dc8d29aSSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
38648dc8d29aSSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
38658dc8d29aSSreekanth Reddy 	struct MPT3SAS_DEVICE *sas_device_priv_data;
38668dc8d29aSSreekanth Reddy 	struct MPT3SAS_TARGET *sas_target_priv_data;
38678dc8d29aSSreekanth Reddy 	int val = 0;
38688dc8d29aSSreekanth Reddy 	struct scsi_device *sdev;
38698dc8d29aSSreekanth Reddy 	struct _raid_device *raid_device;
38708dc8d29aSSreekanth Reddy 	int qdepth;
38718dc8d29aSSreekanth Reddy 
38728dc8d29aSSreekanth Reddy 	if (kstrtoint(buf, 0, &val) != 0)
38738dc8d29aSSreekanth Reddy 		return -EINVAL;
38748dc8d29aSSreekanth Reddy 
38758dc8d29aSSreekanth Reddy 	switch (val) {
38768dc8d29aSSreekanth Reddy 	case 0:
38778dc8d29aSSreekanth Reddy 		ioc->enable_sdev_max_qd = 0;
38788dc8d29aSSreekanth Reddy 		shost_for_each_device(sdev, ioc->shost) {
38798dc8d29aSSreekanth Reddy 			sas_device_priv_data = sdev->hostdata;
38808dc8d29aSSreekanth Reddy 			if (!sas_device_priv_data)
38818dc8d29aSSreekanth Reddy 				continue;
38828dc8d29aSSreekanth Reddy 			sas_target_priv_data = sas_device_priv_data->sas_target;
38838dc8d29aSSreekanth Reddy 			if (!sas_target_priv_data)
38848dc8d29aSSreekanth Reddy 				continue;
38858dc8d29aSSreekanth Reddy 
38868dc8d29aSSreekanth Reddy 			if (sas_target_priv_data->flags &
38878dc8d29aSSreekanth Reddy 			    MPT_TARGET_FLAGS_VOLUME) {
38888dc8d29aSSreekanth Reddy 				raid_device =
38898dc8d29aSSreekanth Reddy 				    mpt3sas_raid_device_find_by_handle(ioc,
38908dc8d29aSSreekanth Reddy 				    sas_target_priv_data->handle);
38918dc8d29aSSreekanth Reddy 
38928dc8d29aSSreekanth Reddy 				switch (raid_device->volume_type) {
38938dc8d29aSSreekanth Reddy 				case MPI2_RAID_VOL_TYPE_RAID0:
38948dc8d29aSSreekanth Reddy 					if (raid_device->device_info &
38958dc8d29aSSreekanth Reddy 					    MPI2_SAS_DEVICE_INFO_SSP_TARGET)
38968dc8d29aSSreekanth Reddy 						qdepth =
38978dc8d29aSSreekanth Reddy 						    MPT3SAS_SAS_QUEUE_DEPTH;
38988dc8d29aSSreekanth Reddy 					else
38998dc8d29aSSreekanth Reddy 						qdepth =
39008dc8d29aSSreekanth Reddy 						    MPT3SAS_SATA_QUEUE_DEPTH;
39018dc8d29aSSreekanth Reddy 					break;
39028dc8d29aSSreekanth Reddy 				case MPI2_RAID_VOL_TYPE_RAID1E:
39038dc8d29aSSreekanth Reddy 				case MPI2_RAID_VOL_TYPE_RAID1:
39048dc8d29aSSreekanth Reddy 				case MPI2_RAID_VOL_TYPE_RAID10:
39058dc8d29aSSreekanth Reddy 				case MPI2_RAID_VOL_TYPE_UNKNOWN:
39068dc8d29aSSreekanth Reddy 				default:
39078dc8d29aSSreekanth Reddy 					qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
39088dc8d29aSSreekanth Reddy 				}
39098dc8d29aSSreekanth Reddy 			} else if (sas_target_priv_data->flags &
39108dc8d29aSSreekanth Reddy 			    MPT_TARGET_FLAGS_PCIE_DEVICE)
3911787f2448SSuganath Prabu S 				qdepth = ioc->max_nvme_qd;
39128dc8d29aSSreekanth Reddy 			else
3913787f2448SSuganath Prabu S 				qdepth = (sas_target_priv_data->sas_dev->port_type > 1) ?
3914787f2448SSuganath Prabu S 				    ioc->max_wideport_qd : ioc->max_narrowport_qd;
39158dc8d29aSSreekanth Reddy 
39168dc8d29aSSreekanth Reddy 			mpt3sas_scsih_change_queue_depth(sdev, qdepth);
39178dc8d29aSSreekanth Reddy 		}
39188dc8d29aSSreekanth Reddy 		break;
39198dc8d29aSSreekanth Reddy 	case 1:
39208dc8d29aSSreekanth Reddy 		ioc->enable_sdev_max_qd = 1;
39218dc8d29aSSreekanth Reddy 		shost_for_each_device(sdev, ioc->shost)
39228dc8d29aSSreekanth Reddy 			mpt3sas_scsih_change_queue_depth(sdev,
39238dc8d29aSSreekanth Reddy 			    shost->can_queue);
39248dc8d29aSSreekanth Reddy 		break;
39258dc8d29aSSreekanth Reddy 	default:
39268dc8d29aSSreekanth Reddy 		return -EINVAL;
39278dc8d29aSSreekanth Reddy 	}
39288dc8d29aSSreekanth Reddy 
39298dc8d29aSSreekanth Reddy 	return strlen(buf);
39308dc8d29aSSreekanth Reddy }
39318dc8d29aSSreekanth Reddy static DEVICE_ATTR_RW(enable_sdev_max_qd);
39328dc8d29aSSreekanth Reddy 
39331bb3ca27SBart Van Assche static struct attribute *mpt3sas_host_attrs[] = {
39341bb3ca27SBart Van Assche 	&dev_attr_version_fw.attr,
39351bb3ca27SBart Van Assche 	&dev_attr_version_bios.attr,
39361bb3ca27SBart Van Assche 	&dev_attr_version_mpi.attr,
39371bb3ca27SBart Van Assche 	&dev_attr_version_product.attr,
39381bb3ca27SBart Van Assche 	&dev_attr_version_nvdata_persistent.attr,
39391bb3ca27SBart Van Assche 	&dev_attr_version_nvdata_default.attr,
39401bb3ca27SBart Van Assche 	&dev_attr_board_name.attr,
39411bb3ca27SBart Van Assche 	&dev_attr_board_assembly.attr,
39421bb3ca27SBart Van Assche 	&dev_attr_board_tracer.attr,
39431bb3ca27SBart Van Assche 	&dev_attr_io_delay.attr,
39441bb3ca27SBart Van Assche 	&dev_attr_device_delay.attr,
39451bb3ca27SBart Van Assche 	&dev_attr_logging_level.attr,
39461bb3ca27SBart Van Assche 	&dev_attr_fwfault_debug.attr,
39471bb3ca27SBart Van Assche 	&dev_attr_fw_queue_depth.attr,
39481bb3ca27SBart Van Assche 	&dev_attr_host_sas_address.attr,
39491bb3ca27SBart Van Assche 	&dev_attr_ioc_reset_count.attr,
39501bb3ca27SBart Van Assche 	&dev_attr_host_trace_buffer_size.attr,
39511bb3ca27SBart Van Assche 	&dev_attr_host_trace_buffer.attr,
39521bb3ca27SBart Van Assche 	&dev_attr_host_trace_buffer_enable.attr,
39531bb3ca27SBart Van Assche 	&dev_attr_reply_queue_count.attr,
39541bb3ca27SBart Van Assche 	&dev_attr_diag_trigger_master.attr,
39551bb3ca27SBart Van Assche 	&dev_attr_diag_trigger_event.attr,
39561bb3ca27SBart Van Assche 	&dev_attr_diag_trigger_scsi.attr,
39571bb3ca27SBart Van Assche 	&dev_attr_diag_trigger_mpi.attr,
39581bb3ca27SBart Van Assche 	&dev_attr_drv_support_bitmap.attr,
39591bb3ca27SBart Van Assche 	&dev_attr_BRM_status.attr,
39601bb3ca27SBart Van Assche 	&dev_attr_enable_sdev_max_qd.attr,
3961f92363d1SSreekanth Reddy 	NULL,
3962f92363d1SSreekanth Reddy };
3963f92363d1SSreekanth Reddy 
39641bb3ca27SBart Van Assche static const struct attribute_group mpt3sas_host_attr_group = {
39651bb3ca27SBart Van Assche 	.attrs = mpt3sas_host_attrs
39661bb3ca27SBart Van Assche };
39671bb3ca27SBart Van Assche 
39681bb3ca27SBart Van Assche const struct attribute_group *mpt3sas_host_groups[] = {
39691bb3ca27SBart Van Assche 	&mpt3sas_host_attr_group,
39701bb3ca27SBart Van Assche 	NULL
39711bb3ca27SBart Van Assche };
39721bb3ca27SBart Van Assche 
3973f92363d1SSreekanth Reddy /* device attributes */
3974f92363d1SSreekanth Reddy 
3975f92363d1SSreekanth Reddy /**
3976c9df1442STomas Henzl  * sas_address_show - sas address
39774beb4867SBart Van Assche  * @dev: pointer to embedded class device
39784beb4867SBart Van Assche  * @attr: ?
39794beb4867SBart Van Assche  * @buf: the buffer returned
3980f92363d1SSreekanth Reddy  *
3981f92363d1SSreekanth Reddy  * This is the sas address for the target
3982f92363d1SSreekanth Reddy  *
3983f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3984f92363d1SSreekanth Reddy  */
3985f92363d1SSreekanth Reddy static ssize_t
3986c9df1442STomas Henzl sas_address_show(struct device *dev, struct device_attribute *attr,
3987f92363d1SSreekanth Reddy 	char *buf)
3988f92363d1SSreekanth Reddy {
3989f92363d1SSreekanth Reddy 	struct scsi_device *sdev = to_scsi_device(dev);
3990f92363d1SSreekanth Reddy 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3991f92363d1SSreekanth Reddy 
3992f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3993f92363d1SSreekanth Reddy 	    (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3994f92363d1SSreekanth Reddy }
3995c9df1442STomas Henzl static DEVICE_ATTR_RO(sas_address);
3996f92363d1SSreekanth Reddy 
3997f92363d1SSreekanth Reddy /**
3998c9df1442STomas Henzl  * sas_device_handle_show - device handle
39994beb4867SBart Van Assche  * @dev: pointer to embedded class device
40004beb4867SBart Van Assche  * @attr: ?
40014beb4867SBart Van Assche  * @buf: the buffer returned
4002f92363d1SSreekanth Reddy  *
4003f92363d1SSreekanth Reddy  * This is the firmware assigned device handle
4004f92363d1SSreekanth Reddy  *
4005f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
4006f92363d1SSreekanth Reddy  */
4007f92363d1SSreekanth Reddy static ssize_t
4008c9df1442STomas Henzl sas_device_handle_show(struct device *dev, struct device_attribute *attr,
4009f92363d1SSreekanth Reddy 	char *buf)
4010f92363d1SSreekanth Reddy {
4011f92363d1SSreekanth Reddy 	struct scsi_device *sdev = to_scsi_device(dev);
4012f92363d1SSreekanth Reddy 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4013f92363d1SSreekanth Reddy 
4014f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "0x%04x\n",
4015f92363d1SSreekanth Reddy 	    sas_device_priv_data->sas_target->handle);
4016f92363d1SSreekanth Reddy }
4017c9df1442STomas Henzl static DEVICE_ATTR_RO(sas_device_handle);
4018f92363d1SSreekanth Reddy 
4019307d9075SAdam Manzanares /**
40204758fd91SDamien Le Moal  * sas_ncq_prio_supported_show - Indicate if device supports NCQ priority
40214758fd91SDamien Le Moal  * @dev: pointer to embedded device
40224758fd91SDamien Le Moal  * @attr: sas_ncq_prio_supported attribute descriptor
40234758fd91SDamien Le Moal  * @buf: the buffer returned
40244758fd91SDamien Le Moal  *
40254758fd91SDamien Le Moal  * A sysfs 'read-only' sdev attribute, only works with SATA
40264758fd91SDamien Le Moal  */
40274758fd91SDamien Le Moal static ssize_t
40284758fd91SDamien Le Moal sas_ncq_prio_supported_show(struct device *dev,
40294758fd91SDamien Le Moal 			    struct device_attribute *attr, char *buf)
40304758fd91SDamien Le Moal {
40314758fd91SDamien Le Moal 	struct scsi_device *sdev = to_scsi_device(dev);
40324758fd91SDamien Le Moal 
40334758fd91SDamien Le Moal 	return sysfs_emit(buf, "%d\n", scsih_ncq_prio_supp(sdev));
40344758fd91SDamien Le Moal }
40354758fd91SDamien Le Moal static DEVICE_ATTR_RO(sas_ncq_prio_supported);
40364758fd91SDamien Le Moal 
40374758fd91SDamien Le Moal /**
4038782a1ab3SLee Jones  * sas_ncq_prio_enable_show - send prioritized io commands to device
40394beb4867SBart Van Assche  * @dev: pointer to embedded device
40404beb4867SBart Van Assche  * @attr: ?
40414beb4867SBart Van Assche  * @buf: the buffer returned
4042307d9075SAdam Manzanares  *
4043307d9075SAdam Manzanares  * A sysfs 'read/write' sdev attribute, only works with SATA
4044307d9075SAdam Manzanares  */
4045307d9075SAdam Manzanares static ssize_t
4046c9df1442STomas Henzl sas_ncq_prio_enable_show(struct device *dev,
4047307d9075SAdam Manzanares 				 struct device_attribute *attr, char *buf)
4048307d9075SAdam Manzanares {
4049307d9075SAdam Manzanares 	struct scsi_device *sdev = to_scsi_device(dev);
4050307d9075SAdam Manzanares 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4051307d9075SAdam Manzanares 
4052307d9075SAdam Manzanares 	return snprintf(buf, PAGE_SIZE, "%d\n",
4053307d9075SAdam Manzanares 			sas_device_priv_data->ncq_prio_enable);
4054307d9075SAdam Manzanares }
4055307d9075SAdam Manzanares 
4056307d9075SAdam Manzanares static ssize_t
4057c9df1442STomas Henzl sas_ncq_prio_enable_store(struct device *dev,
4058307d9075SAdam Manzanares 				  struct device_attribute *attr,
4059307d9075SAdam Manzanares 				  const char *buf, size_t count)
4060307d9075SAdam Manzanares {
4061307d9075SAdam Manzanares 	struct scsi_device *sdev = to_scsi_device(dev);
4062307d9075SAdam Manzanares 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4063307d9075SAdam Manzanares 	bool ncq_prio_enable = 0;
4064307d9075SAdam Manzanares 
4065307d9075SAdam Manzanares 	if (kstrtobool(buf, &ncq_prio_enable))
4066307d9075SAdam Manzanares 		return -EINVAL;
4067307d9075SAdam Manzanares 
4068307d9075SAdam Manzanares 	if (!scsih_ncq_prio_supp(sdev))
4069307d9075SAdam Manzanares 		return -EINVAL;
4070307d9075SAdam Manzanares 
4071307d9075SAdam Manzanares 	sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
4072307d9075SAdam Manzanares 	return strlen(buf);
4073307d9075SAdam Manzanares }
4074c9df1442STomas Henzl static DEVICE_ATTR_RW(sas_ncq_prio_enable);
4075307d9075SAdam Manzanares 
40760ae8f478SJiapeng Chong static struct attribute *mpt3sas_dev_attrs[] = {
40771bb3ca27SBart Van Assche 	&dev_attr_sas_address.attr,
40781bb3ca27SBart Van Assche 	&dev_attr_sas_device_handle.attr,
40791bb3ca27SBart Van Assche 	&dev_attr_sas_ncq_prio_supported.attr,
40801bb3ca27SBart Van Assche 	&dev_attr_sas_ncq_prio_enable.attr,
4081f92363d1SSreekanth Reddy 	NULL,
4082f92363d1SSreekanth Reddy };
4083f92363d1SSreekanth Reddy 
40841bb3ca27SBart Van Assche static const struct attribute_group mpt3sas_dev_attr_group = {
40851bb3ca27SBart Van Assche 	.attrs = mpt3sas_dev_attrs
40861bb3ca27SBart Van Assche };
40871bb3ca27SBart Van Assche 
40881bb3ca27SBart Van Assche const struct attribute_group *mpt3sas_dev_groups[] = {
40891bb3ca27SBart Van Assche 	&mpt3sas_dev_attr_group,
40901bb3ca27SBart Van Assche 	NULL
40911bb3ca27SBart Van Assche };
40921bb3ca27SBart Van Assche 
4093c84b06a4SSreekanth Reddy /* file operations table for mpt3ctl device */
4094c84b06a4SSreekanth Reddy static const struct file_operations ctl_fops = {
4095c84b06a4SSreekanth Reddy 	.owner = THIS_MODULE,
4096c84b06a4SSreekanth Reddy 	.unlocked_ioctl = _ctl_ioctl,
4097c84b06a4SSreekanth Reddy 	.poll = _ctl_poll,
4098c84b06a4SSreekanth Reddy 	.fasync = _ctl_fasync,
4099c84b06a4SSreekanth Reddy #ifdef CONFIG_COMPAT
4100c84b06a4SSreekanth Reddy 	.compat_ioctl = _ctl_ioctl_compat,
4101c84b06a4SSreekanth Reddy #endif
4102c84b06a4SSreekanth Reddy };
4103c84b06a4SSreekanth Reddy 
4104c84b06a4SSreekanth Reddy /* file operations table for mpt2ctl device */
4105c84b06a4SSreekanth Reddy static const struct file_operations ctl_gen2_fops = {
4106c84b06a4SSreekanth Reddy 	.owner = THIS_MODULE,
4107c84b06a4SSreekanth Reddy 	.unlocked_ioctl = _ctl_mpt2_ioctl,
4108c84b06a4SSreekanth Reddy 	.poll = _ctl_poll,
4109c84b06a4SSreekanth Reddy 	.fasync = _ctl_fasync,
4110c84b06a4SSreekanth Reddy #ifdef CONFIG_COMPAT
4111c84b06a4SSreekanth Reddy 	.compat_ioctl = _ctl_mpt2_ioctl_compat,
4112c84b06a4SSreekanth Reddy #endif
4113c84b06a4SSreekanth Reddy };
4114c84b06a4SSreekanth Reddy 
4115c84b06a4SSreekanth Reddy static struct miscdevice ctl_dev = {
4116c84b06a4SSreekanth Reddy 	.minor  = MPT3SAS_MINOR,
4117c84b06a4SSreekanth Reddy 	.name   = MPT3SAS_DEV_NAME,
4118c84b06a4SSreekanth Reddy 	.fops   = &ctl_fops,
4119c84b06a4SSreekanth Reddy };
4120c84b06a4SSreekanth Reddy 
4121c84b06a4SSreekanth Reddy static struct miscdevice gen2_ctl_dev = {
4122c84b06a4SSreekanth Reddy 	.minor  = MPT2SAS_MINOR,
4123c84b06a4SSreekanth Reddy 	.name   = MPT2SAS_DEV_NAME,
4124c84b06a4SSreekanth Reddy 	.fops   = &ctl_gen2_fops,
4125c84b06a4SSreekanth Reddy };
4126c84b06a4SSreekanth Reddy 
4127f92363d1SSreekanth Reddy /**
4128c84b06a4SSreekanth Reddy  * mpt3sas_ctl_init - main entry point for ctl.
41294beb4867SBart Van Assche  * @hbas_to_enumerate: ?
4130f92363d1SSreekanth Reddy  */
4131f92363d1SSreekanth Reddy void
4132c84b06a4SSreekanth Reddy mpt3sas_ctl_init(ushort hbas_to_enumerate)
4133f92363d1SSreekanth Reddy {
4134f92363d1SSreekanth Reddy 	async_queue = NULL;
4135c84b06a4SSreekanth Reddy 
4136c84b06a4SSreekanth Reddy 	/* Don't register mpt3ctl ioctl device if
4137c84b06a4SSreekanth Reddy 	 * hbas_to_enumarate is one.
4138c84b06a4SSreekanth Reddy 	 */
4139c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 1)
4140c84b06a4SSreekanth Reddy 		if (misc_register(&ctl_dev) < 0)
4141c84b06a4SSreekanth Reddy 			pr_err("%s can't register misc device [minor=%d]\n",
4142c84b06a4SSreekanth Reddy 			    MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
4143c84b06a4SSreekanth Reddy 
4144c84b06a4SSreekanth Reddy 	/* Don't register mpt3ctl ioctl device if
4145c84b06a4SSreekanth Reddy 	 * hbas_to_enumarate is two.
4146c84b06a4SSreekanth Reddy 	 */
4147c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 2)
4148c84b06a4SSreekanth Reddy 		if (misc_register(&gen2_ctl_dev) < 0)
4149c84b06a4SSreekanth Reddy 			pr_err("%s can't register misc device [minor=%d]\n",
4150c84b06a4SSreekanth Reddy 			    MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
4151c84b06a4SSreekanth Reddy 
4152f92363d1SSreekanth Reddy 	init_waitqueue_head(&ctl_poll_wait);
4153f92363d1SSreekanth Reddy }
4154f92363d1SSreekanth Reddy 
4155f92363d1SSreekanth Reddy /**
4156c84b06a4SSreekanth Reddy  * mpt3sas_ctl_exit - exit point for ctl
41574beb4867SBart Van Assche  * @hbas_to_enumerate: ?
4158f92363d1SSreekanth Reddy  */
4159f92363d1SSreekanth Reddy void
4160c84b06a4SSreekanth Reddy mpt3sas_ctl_exit(ushort hbas_to_enumerate)
4161f92363d1SSreekanth Reddy {
4162f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc;
4163f92363d1SSreekanth Reddy 	int i;
4164f92363d1SSreekanth Reddy 
4165f92363d1SSreekanth Reddy 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
4166f92363d1SSreekanth Reddy 
4167f92363d1SSreekanth Reddy 		/* free memory associated to diag buffers */
4168f92363d1SSreekanth Reddy 		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
4169f92363d1SSreekanth Reddy 			if (!ioc->diag_buffer[i])
4170f92363d1SSreekanth Reddy 				continue;
41711c2048bdSChristoph Hellwig 			dma_free_coherent(&ioc->pdev->dev,
41721c2048bdSChristoph Hellwig 					  ioc->diag_buffer_sz[i],
41731c2048bdSChristoph Hellwig 					  ioc->diag_buffer[i],
41741c2048bdSChristoph Hellwig 					  ioc->diag_buffer_dma[i]);
4175f92363d1SSreekanth Reddy 			ioc->diag_buffer[i] = NULL;
4176f92363d1SSreekanth Reddy 			ioc->diag_buffer_status[i] = 0;
4177f92363d1SSreekanth Reddy 		}
4178f92363d1SSreekanth Reddy 
4179f92363d1SSreekanth Reddy 		kfree(ioc->event_log);
4180f92363d1SSreekanth Reddy 	}
4181c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 1)
4182c84b06a4SSreekanth Reddy 		misc_deregister(&ctl_dev);
4183c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 2)
4184c84b06a4SSreekanth Reddy 		misc_deregister(&gen2_ctl_dev);
4185f92363d1SSreekanth Reddy }
4186