xref: /linux/drivers/scsi/mpt3sas/mpt3sas_ctl.c (revision c7a357058882637548dd0908b743c674a23ce0e2)
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;
183f92363d1SSreekanth Reddy 	}
184f92363d1SSreekanth Reddy 
185f92363d1SSreekanth Reddy 	if (!desc)
186f92363d1SSreekanth Reddy 		return;
187f92363d1SSreekanth Reddy 
188f92363d1SSreekanth Reddy 	pr_info(MPT3SAS_FMT "%s: %s, smid(%d)\n",
189f92363d1SSreekanth Reddy 	    ioc->name, calling_function_name, desc, smid);
190f92363d1SSreekanth Reddy 
191f92363d1SSreekanth Reddy 	if (!mpi_reply)
192f92363d1SSreekanth Reddy 		return;
193f92363d1SSreekanth Reddy 
194f92363d1SSreekanth Reddy 	if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
195f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT
196f92363d1SSreekanth Reddy 		    "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
197f92363d1SSreekanth Reddy 		    ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
198f92363d1SSreekanth Reddy 		    le32_to_cpu(mpi_reply->IOCLogInfo));
199f92363d1SSreekanth Reddy 
200f92363d1SSreekanth Reddy 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
201f92363d1SSreekanth Reddy 	    mpi_request->Function ==
202f92363d1SSreekanth Reddy 	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
203f92363d1SSreekanth Reddy 		Mpi2SCSIIOReply_t *scsi_reply =
204f92363d1SSreekanth Reddy 		    (Mpi2SCSIIOReply_t *)mpi_reply;
205f92363d1SSreekanth Reddy 		struct _sas_device *sas_device = NULL;
20645aa6a1aSSuganath Prabu Subramani 		struct _pcie_device *pcie_device = NULL;
207f92363d1SSreekanth Reddy 
20845aa6a1aSSuganath Prabu Subramani 		sas_device = mpt3sas_get_sdev_by_handle(ioc,
209f92363d1SSreekanth Reddy 		    le16_to_cpu(scsi_reply->DevHandle));
210f92363d1SSreekanth Reddy 		if (sas_device) {
211f92363d1SSreekanth Reddy 			pr_warn(MPT3SAS_FMT "\tsas_address(0x%016llx), phy(%d)\n",
212f92363d1SSreekanth Reddy 				ioc->name, (unsigned long long)
213f92363d1SSreekanth Reddy 			    sas_device->sas_address, sas_device->phy);
214f92363d1SSreekanth Reddy 			pr_warn(MPT3SAS_FMT
215f92363d1SSreekanth Reddy 			    "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
216f92363d1SSreekanth Reddy 			    ioc->name, (unsigned long long)
217f92363d1SSreekanth Reddy 			    sas_device->enclosure_logical_id, sas_device->slot);
21845aa6a1aSSuganath Prabu Subramani 			sas_device_put(sas_device);
219f92363d1SSreekanth Reddy 		}
22045aa6a1aSSuganath Prabu Subramani 		if (!sas_device) {
22145aa6a1aSSuganath Prabu Subramani 			pcie_device = mpt3sas_get_pdev_by_handle(ioc,
22245aa6a1aSSuganath Prabu Subramani 				le16_to_cpu(scsi_reply->DevHandle));
22345aa6a1aSSuganath Prabu Subramani 			if (pcie_device) {
22445aa6a1aSSuganath Prabu Subramani 				pr_warn(MPT3SAS_FMT
22545aa6a1aSSuganath Prabu Subramani 				    "\tWWID(0x%016llx), port(%d)\n", ioc->name,
22645aa6a1aSSuganath Prabu Subramani 				    (unsigned long long)pcie_device->wwid,
22745aa6a1aSSuganath Prabu Subramani 				    pcie_device->port_num);
22845aa6a1aSSuganath Prabu Subramani 				if (pcie_device->enclosure_handle != 0)
22945aa6a1aSSuganath Prabu Subramani 					pr_warn(MPT3SAS_FMT
23045aa6a1aSSuganath Prabu Subramani 					    "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
23145aa6a1aSSuganath Prabu Subramani 					    ioc->name, (unsigned long long)
23245aa6a1aSSuganath Prabu Subramani 					    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)
238f92363d1SSreekanth Reddy 			pr_info(MPT3SAS_FMT
239f92363d1SSreekanth Reddy 			    "\tscsi_state(0x%02x), scsi_status"
240f92363d1SSreekanth Reddy 			    "(0x%02x)\n", ioc->name,
241f92363d1SSreekanth Reddy 			    scsi_reply->SCSIState,
242f92363d1SSreekanth Reddy 			    scsi_reply->SCSIStatus);
243f92363d1SSreekanth Reddy 	}
244f92363d1SSreekanth Reddy }
245f92363d1SSreekanth Reddy 
246f92363d1SSreekanth Reddy /**
247f92363d1SSreekanth Reddy  * mpt3sas_ctl_done - ctl module completion routine
248f92363d1SSreekanth Reddy  * @ioc: per adapter object
249f92363d1SSreekanth Reddy  * @smid: system request message index
250f92363d1SSreekanth Reddy  * @msix_index: MSIX table index supplied by the OS
251f92363d1SSreekanth Reddy  * @reply: reply message frame(lower 32bit addr)
252f92363d1SSreekanth Reddy  * Context: none.
253f92363d1SSreekanth Reddy  *
254f92363d1SSreekanth Reddy  * The callback handler when using ioc->ctl_cb_idx.
255f92363d1SSreekanth Reddy  *
256f92363d1SSreekanth Reddy  * Return 1 meaning mf should be freed from _base_interrupt
257f92363d1SSreekanth Reddy  *        0 means the mf is freed from this function.
258f92363d1SSreekanth Reddy  */
259f92363d1SSreekanth Reddy u8
260f92363d1SSreekanth Reddy mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
261f92363d1SSreekanth Reddy 	u32 reply)
262f92363d1SSreekanth Reddy {
263f92363d1SSreekanth Reddy 	MPI2DefaultReply_t *mpi_reply;
264f92363d1SSreekanth Reddy 	Mpi2SCSIIOReply_t *scsiio_reply;
265aff39e61SSuganath Prabu Subramani 	Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
266f92363d1SSreekanth Reddy 	const void *sense_data;
267f92363d1SSreekanth Reddy 	u32 sz;
268f92363d1SSreekanth Reddy 
269f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
270f92363d1SSreekanth Reddy 		return 1;
271f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.smid != smid)
272f92363d1SSreekanth Reddy 		return 1;
273f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
274f92363d1SSreekanth Reddy 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
275f92363d1SSreekanth Reddy 	if (mpi_reply) {
276f92363d1SSreekanth Reddy 		memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
277f92363d1SSreekanth Reddy 		ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
278f92363d1SSreekanth Reddy 		/* get sense data */
279f92363d1SSreekanth Reddy 		if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
280f92363d1SSreekanth Reddy 		    mpi_reply->Function ==
281f92363d1SSreekanth Reddy 		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
282f92363d1SSreekanth Reddy 			scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
283f92363d1SSreekanth Reddy 			if (scsiio_reply->SCSIState &
284f92363d1SSreekanth Reddy 			    MPI2_SCSI_STATE_AUTOSENSE_VALID) {
285f92363d1SSreekanth Reddy 				sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
286f92363d1SSreekanth Reddy 				    le32_to_cpu(scsiio_reply->SenseCount));
287f92363d1SSreekanth Reddy 				sense_data = mpt3sas_base_get_sense_buffer(ioc,
288f92363d1SSreekanth Reddy 				    smid);
289f92363d1SSreekanth Reddy 				memcpy(ioc->ctl_cmds.sense, sense_data, sz);
290f92363d1SSreekanth Reddy 			}
291f92363d1SSreekanth Reddy 		}
292aff39e61SSuganath Prabu Subramani 		/*
293aff39e61SSuganath Prabu Subramani 		 * Get Error Response data for NVMe device. The ctl_cmds.sense
294aff39e61SSuganath Prabu Subramani 		 * buffer is used to store the Error Response data.
295aff39e61SSuganath Prabu Subramani 		 */
296aff39e61SSuganath Prabu Subramani 		if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
297aff39e61SSuganath Prabu Subramani 			nvme_error_reply =
298aff39e61SSuganath Prabu Subramani 			    (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
299aff39e61SSuganath Prabu Subramani 			sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
300cf6bf971SChaitra P B 			    le16_to_cpu(nvme_error_reply->ErrorResponseCount));
301aff39e61SSuganath Prabu Subramani 			sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
302aff39e61SSuganath Prabu Subramani 			memcpy(ioc->ctl_cmds.sense, sense_data, sz);
303aff39e61SSuganath Prabu Subramani 		}
304f92363d1SSreekanth Reddy 	}
305016d5c35SSuganath Prabu Subramani 
306f92363d1SSreekanth Reddy 	_ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
307f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
308f92363d1SSreekanth Reddy 	complete(&ioc->ctl_cmds.done);
309f92363d1SSreekanth Reddy 	return 1;
310f92363d1SSreekanth Reddy }
311f92363d1SSreekanth Reddy 
312f92363d1SSreekanth Reddy /**
313f92363d1SSreekanth Reddy  * _ctl_check_event_type - determines when an event needs logging
314f92363d1SSreekanth Reddy  * @ioc: per adapter object
315f92363d1SSreekanth Reddy  * @event: firmware event
316f92363d1SSreekanth Reddy  *
317f92363d1SSreekanth Reddy  * The bitmask in ioc->event_type[] indicates which events should be
318f92363d1SSreekanth Reddy  * be saved in the driver event_log.  This bitmask is set by application.
319f92363d1SSreekanth Reddy  *
320f92363d1SSreekanth Reddy  * Returns 1 when event should be captured, or zero means no match.
321f92363d1SSreekanth Reddy  */
322f92363d1SSreekanth Reddy static int
323f92363d1SSreekanth Reddy _ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
324f92363d1SSreekanth Reddy {
325f92363d1SSreekanth Reddy 	u16 i;
326f92363d1SSreekanth Reddy 	u32 desired_event;
327f92363d1SSreekanth Reddy 
328f92363d1SSreekanth Reddy 	if (event >= 128 || !event || !ioc->event_log)
329f92363d1SSreekanth Reddy 		return 0;
330f92363d1SSreekanth Reddy 
331f92363d1SSreekanth Reddy 	desired_event = (1 << (event % 32));
332f92363d1SSreekanth Reddy 	if (!desired_event)
333f92363d1SSreekanth Reddy 		desired_event = 1;
334f92363d1SSreekanth Reddy 	i = event / 32;
335f92363d1SSreekanth Reddy 	return desired_event & ioc->event_type[i];
336f92363d1SSreekanth Reddy }
337f92363d1SSreekanth Reddy 
338f92363d1SSreekanth Reddy /**
339f92363d1SSreekanth Reddy  * mpt3sas_ctl_add_to_event_log - add event
340f92363d1SSreekanth Reddy  * @ioc: per adapter object
341f92363d1SSreekanth Reddy  * @mpi_reply: reply message frame
342f92363d1SSreekanth Reddy  *
343f92363d1SSreekanth Reddy  * Return nothing.
344f92363d1SSreekanth Reddy  */
345f92363d1SSreekanth Reddy void
346f92363d1SSreekanth Reddy mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
347f92363d1SSreekanth Reddy 	Mpi2EventNotificationReply_t *mpi_reply)
348f92363d1SSreekanth Reddy {
349f92363d1SSreekanth Reddy 	struct MPT3_IOCTL_EVENTS *event_log;
350f92363d1SSreekanth Reddy 	u16 event;
351f92363d1SSreekanth Reddy 	int i;
352f92363d1SSreekanth Reddy 	u32 sz, event_data_sz;
353f92363d1SSreekanth Reddy 	u8 send_aen = 0;
354f92363d1SSreekanth Reddy 
355f92363d1SSreekanth Reddy 	if (!ioc->event_log)
356f92363d1SSreekanth Reddy 		return;
357f92363d1SSreekanth Reddy 
358f92363d1SSreekanth Reddy 	event = le16_to_cpu(mpi_reply->Event);
359f92363d1SSreekanth Reddy 
360f92363d1SSreekanth Reddy 	if (_ctl_check_event_type(ioc, event)) {
361f92363d1SSreekanth Reddy 
362f92363d1SSreekanth Reddy 		/* insert entry into circular event_log */
363f92363d1SSreekanth Reddy 		i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
364f92363d1SSreekanth Reddy 		event_log = ioc->event_log;
365f92363d1SSreekanth Reddy 		event_log[i].event = event;
366f92363d1SSreekanth Reddy 		event_log[i].context = ioc->event_context++;
367f92363d1SSreekanth Reddy 
368f92363d1SSreekanth Reddy 		event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
369f92363d1SSreekanth Reddy 		sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
370f92363d1SSreekanth Reddy 		memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
371f92363d1SSreekanth Reddy 		memcpy(event_log[i].data, mpi_reply->EventData, sz);
372f92363d1SSreekanth Reddy 		send_aen = 1;
373f92363d1SSreekanth Reddy 	}
374f92363d1SSreekanth Reddy 
375f92363d1SSreekanth Reddy 	/* This aen_event_read_flag flag is set until the
376f92363d1SSreekanth Reddy 	 * application has read the event log.
377f92363d1SSreekanth Reddy 	 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
378f92363d1SSreekanth Reddy 	 */
379f92363d1SSreekanth Reddy 	if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
380f92363d1SSreekanth Reddy 	    (send_aen && !ioc->aen_event_read_flag)) {
381f92363d1SSreekanth Reddy 		ioc->aen_event_read_flag = 1;
382f92363d1SSreekanth Reddy 		wake_up_interruptible(&ctl_poll_wait);
383f92363d1SSreekanth Reddy 		if (async_queue)
384f92363d1SSreekanth Reddy 			kill_fasync(&async_queue, SIGIO, POLL_IN);
385f92363d1SSreekanth Reddy 	}
386f92363d1SSreekanth Reddy }
387f92363d1SSreekanth Reddy 
388f92363d1SSreekanth Reddy /**
389f92363d1SSreekanth Reddy  * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
390f92363d1SSreekanth Reddy  * @ioc: per adapter object
391f92363d1SSreekanth Reddy  * @msix_index: MSIX table index supplied by the OS
392f92363d1SSreekanth Reddy  * @reply: reply message frame(lower 32bit addr)
393f92363d1SSreekanth Reddy  * Context: interrupt.
394f92363d1SSreekanth Reddy  *
395f92363d1SSreekanth Reddy  * This function merely adds a new work task into ioc->firmware_event_thread.
396f92363d1SSreekanth Reddy  * The tasks are worked from _firmware_event_work in user context.
397f92363d1SSreekanth Reddy  *
398f92363d1SSreekanth Reddy  * Return 1 meaning mf should be freed from _base_interrupt
399f92363d1SSreekanth Reddy  *        0 means the mf is freed from this function.
400f92363d1SSreekanth Reddy  */
401f92363d1SSreekanth Reddy u8
402f92363d1SSreekanth Reddy mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
403f92363d1SSreekanth Reddy 	u32 reply)
404f92363d1SSreekanth Reddy {
405f92363d1SSreekanth Reddy 	Mpi2EventNotificationReply_t *mpi_reply;
406f92363d1SSreekanth Reddy 
407f92363d1SSreekanth Reddy 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
408869817f9SSuganath prabu Subramani 	if (mpi_reply)
409f92363d1SSreekanth Reddy 		mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
410f92363d1SSreekanth Reddy 	return 1;
411f92363d1SSreekanth Reddy }
412f92363d1SSreekanth Reddy 
413f92363d1SSreekanth Reddy /**
414f92363d1SSreekanth Reddy  * _ctl_verify_adapter - validates ioc_number passed from application
415f92363d1SSreekanth Reddy  * @ioc: per adapter object
416f92363d1SSreekanth Reddy  * @iocpp: The ioc pointer is returned in this.
417c84b06a4SSreekanth Reddy  * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
418b130b0d5SSuganath prabu Subramani  * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
419f92363d1SSreekanth Reddy  *
420f92363d1SSreekanth Reddy  * Return (-1) means error, else ioc_number.
421f92363d1SSreekanth Reddy  */
422f92363d1SSreekanth Reddy static int
423c84b06a4SSreekanth Reddy _ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
424c84b06a4SSreekanth Reddy 							int mpi_version)
425f92363d1SSreekanth Reddy {
426f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc;
427b130b0d5SSuganath prabu Subramani 	int version = 0;
42808c4d550SSreekanth Reddy 	/* global ioc lock to protect controller on list operations */
42908c4d550SSreekanth Reddy 	spin_lock(&gioc_lock);
430f92363d1SSreekanth Reddy 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
431f92363d1SSreekanth Reddy 		if (ioc->id != ioc_number)
432f92363d1SSreekanth Reddy 			continue;
433c84b06a4SSreekanth Reddy 		/* Check whether this ioctl command is from right
434c84b06a4SSreekanth Reddy 		 * ioctl device or not, if not continue the search.
435c84b06a4SSreekanth Reddy 		 */
436b130b0d5SSuganath prabu Subramani 		version = ioc->hba_mpi_version_belonged;
437b130b0d5SSuganath prabu Subramani 		/* MPI25_VERSION and MPI26_VERSION uses same ioctl
438b130b0d5SSuganath prabu Subramani 		 * device.
439b130b0d5SSuganath prabu Subramani 		 */
440b130b0d5SSuganath prabu Subramani 		if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
441b130b0d5SSuganath prabu Subramani 			if ((version == MPI25_VERSION) ||
442b130b0d5SSuganath prabu Subramani 				(version == MPI26_VERSION))
443b130b0d5SSuganath prabu Subramani 				goto out;
444b130b0d5SSuganath prabu Subramani 			else
445c84b06a4SSreekanth Reddy 				continue;
446b130b0d5SSuganath prabu Subramani 		} else {
447b130b0d5SSuganath prabu Subramani 			if (version != mpi_version)
448b130b0d5SSuganath prabu Subramani 				continue;
449b130b0d5SSuganath prabu Subramani 		}
450b130b0d5SSuganath prabu Subramani out:
45108c4d550SSreekanth Reddy 		spin_unlock(&gioc_lock);
452f92363d1SSreekanth Reddy 		*iocpp = ioc;
453f92363d1SSreekanth Reddy 		return ioc_number;
454f92363d1SSreekanth Reddy 	}
45508c4d550SSreekanth Reddy 	spin_unlock(&gioc_lock);
456f92363d1SSreekanth Reddy 	*iocpp = NULL;
457f92363d1SSreekanth Reddy 	return -1;
458f92363d1SSreekanth Reddy }
459f92363d1SSreekanth Reddy 
460f92363d1SSreekanth Reddy /**
461f92363d1SSreekanth Reddy  * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
462f92363d1SSreekanth Reddy  * @ioc: per adapter object
463f92363d1SSreekanth Reddy  *
464f92363d1SSreekanth Reddy  * The handler for doing any required cleanup or initialization.
465f92363d1SSreekanth Reddy  */
466*c7a35705SBart Van Assche void mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
467f92363d1SSreekanth Reddy {
468f92363d1SSreekanth Reddy 	int i;
469f92363d1SSreekanth Reddy 	u8 issue_reset;
470f92363d1SSreekanth Reddy 
471f92363d1SSreekanth Reddy 	dtmprintk(ioc, pr_info(MPT3SAS_FMT
472f92363d1SSreekanth Reddy 			"%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
473f92363d1SSreekanth Reddy 	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
474f92363d1SSreekanth Reddy 		if (!(ioc->diag_buffer_status[i] &
475f92363d1SSreekanth Reddy 		      MPT3_DIAG_BUFFER_IS_REGISTERED))
476f92363d1SSreekanth Reddy 			continue;
477f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[i] &
478f92363d1SSreekanth Reddy 		     MPT3_DIAG_BUFFER_IS_RELEASED))
479f92363d1SSreekanth Reddy 			continue;
480f92363d1SSreekanth Reddy 		mpt3sas_send_diag_release(ioc, i, &issue_reset);
481f92363d1SSreekanth Reddy 	}
482*c7a35705SBart Van Assche }
483*c7a35705SBart Van Assche 
484*c7a35705SBart Van Assche /**
485*c7a35705SBart Van Assche  * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
486*c7a35705SBart Van Assche  * @ioc: per adapter object
487*c7a35705SBart Van Assche  *
488*c7a35705SBart Van Assche  * The handler for doing any required cleanup or initialization.
489*c7a35705SBart Van Assche  */
490*c7a35705SBart Van Assche void mpt3sas_ctl_after_reset_handler(struct MPT3SAS_ADAPTER *ioc)
491*c7a35705SBart Van Assche {
492f92363d1SSreekanth Reddy 	dtmprintk(ioc, pr_info(MPT3SAS_FMT
493f92363d1SSreekanth Reddy 			"%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
494f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
495f92363d1SSreekanth Reddy 		ioc->ctl_cmds.status |= MPT3_CMD_RESET;
496f92363d1SSreekanth Reddy 		mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
497f92363d1SSreekanth Reddy 		complete(&ioc->ctl_cmds.done);
498f92363d1SSreekanth Reddy 	}
499*c7a35705SBart Van Assche }
500*c7a35705SBart Van Assche 
501*c7a35705SBart Van Assche /**
502*c7a35705SBart Van Assche  * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
503*c7a35705SBart Van Assche  * @ioc: per adapter object
504*c7a35705SBart Van Assche  *
505*c7a35705SBart Van Assche  * The handler for doing any required cleanup or initialization.
506*c7a35705SBart Van Assche  */
507*c7a35705SBart Van Assche void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
508*c7a35705SBart Van Assche {
509*c7a35705SBart Van Assche 	int i;
510*c7a35705SBart Van Assche 
511f92363d1SSreekanth Reddy 	dtmprintk(ioc, pr_info(MPT3SAS_FMT
512f92363d1SSreekanth Reddy 			"%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
513f92363d1SSreekanth Reddy 
514f92363d1SSreekanth Reddy 	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
515f92363d1SSreekanth Reddy 		if (!(ioc->diag_buffer_status[i] &
516f92363d1SSreekanth Reddy 		      MPT3_DIAG_BUFFER_IS_REGISTERED))
517f92363d1SSreekanth Reddy 			continue;
518f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[i] &
519f92363d1SSreekanth Reddy 		     MPT3_DIAG_BUFFER_IS_RELEASED))
520f92363d1SSreekanth Reddy 			continue;
521f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[i] |=
522f92363d1SSreekanth Reddy 			MPT3_DIAG_BUFFER_IS_DIAG_RESET;
523f92363d1SSreekanth Reddy 	}
524f92363d1SSreekanth Reddy }
525f92363d1SSreekanth Reddy 
526f92363d1SSreekanth Reddy /**
527c84b06a4SSreekanth Reddy  * _ctl_fasync -
528f92363d1SSreekanth Reddy  * @fd -
529f92363d1SSreekanth Reddy  * @filep -
530f92363d1SSreekanth Reddy  * @mode -
531f92363d1SSreekanth Reddy  *
532f92363d1SSreekanth Reddy  * Called when application request fasyn callback handler.
533f92363d1SSreekanth Reddy  */
5348bbb1cf6SCalvin Owens static int
535c84b06a4SSreekanth Reddy _ctl_fasync(int fd, struct file *filep, int mode)
536f92363d1SSreekanth Reddy {
537f92363d1SSreekanth Reddy 	return fasync_helper(fd, filep, mode, &async_queue);
538f92363d1SSreekanth Reddy }
539f92363d1SSreekanth Reddy 
540f92363d1SSreekanth Reddy /**
541c84b06a4SSreekanth Reddy  * _ctl_poll -
542f92363d1SSreekanth Reddy  * @file -
543f92363d1SSreekanth Reddy  * @wait -
544f92363d1SSreekanth Reddy  *
545f92363d1SSreekanth Reddy  */
546afc9a42bSAl Viro static __poll_t
547c84b06a4SSreekanth Reddy _ctl_poll(struct file *filep, poll_table *wait)
548f92363d1SSreekanth Reddy {
549f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc;
550f92363d1SSreekanth Reddy 
551f92363d1SSreekanth Reddy 	poll_wait(filep, &ctl_poll_wait, wait);
552f92363d1SSreekanth Reddy 
55308c4d550SSreekanth Reddy 	/* global ioc lock to protect controller on list operations */
55408c4d550SSreekanth Reddy 	spin_lock(&gioc_lock);
555f92363d1SSreekanth Reddy 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
55608c4d550SSreekanth Reddy 		if (ioc->aen_event_read_flag) {
55708c4d550SSreekanth Reddy 			spin_unlock(&gioc_lock);
558a9a08845SLinus Torvalds 			return EPOLLIN | EPOLLRDNORM;
559f92363d1SSreekanth Reddy 		}
56008c4d550SSreekanth Reddy 	}
56108c4d550SSreekanth Reddy 	spin_unlock(&gioc_lock);
562f92363d1SSreekanth Reddy 	return 0;
563f92363d1SSreekanth Reddy }
564f92363d1SSreekanth Reddy 
565f92363d1SSreekanth Reddy /**
566f92363d1SSreekanth Reddy  * _ctl_set_task_mid - assign an active smid to tm request
567f92363d1SSreekanth Reddy  * @ioc: per adapter object
568f92363d1SSreekanth Reddy  * @karg - (struct mpt3_ioctl_command)
569f92363d1SSreekanth Reddy  * @tm_request - pointer to mf from user space
570f92363d1SSreekanth Reddy  *
571f92363d1SSreekanth Reddy  * Returns 0 when an smid if found, else fail.
572f92363d1SSreekanth Reddy  * during failure, the reply frame is filled.
573f92363d1SSreekanth Reddy  */
574f92363d1SSreekanth Reddy static int
575f92363d1SSreekanth Reddy _ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
576f92363d1SSreekanth Reddy 	Mpi2SCSITaskManagementRequest_t *tm_request)
577f92363d1SSreekanth Reddy {
578f92363d1SSreekanth Reddy 	u8 found = 0;
579dbec4c90SSuganath Prabu Subramani 	u16 smid;
580f92363d1SSreekanth Reddy 	u16 handle;
581f92363d1SSreekanth Reddy 	struct scsi_cmnd *scmd;
582f92363d1SSreekanth Reddy 	struct MPT3SAS_DEVICE *priv_data;
583f92363d1SSreekanth Reddy 	Mpi2SCSITaskManagementReply_t *tm_reply;
584f92363d1SSreekanth Reddy 	u32 sz;
585f92363d1SSreekanth Reddy 	u32 lun;
586f92363d1SSreekanth Reddy 	char *desc = NULL;
587f92363d1SSreekanth Reddy 
588f92363d1SSreekanth Reddy 	if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
589f92363d1SSreekanth Reddy 		desc = "abort_task";
590f92363d1SSreekanth Reddy 	else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
591f92363d1SSreekanth Reddy 		desc = "query_task";
592f92363d1SSreekanth Reddy 	else
593f92363d1SSreekanth Reddy 		return 0;
594f92363d1SSreekanth Reddy 
595f92363d1SSreekanth Reddy 	lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
596f92363d1SSreekanth Reddy 
597f92363d1SSreekanth Reddy 	handle = le16_to_cpu(tm_request->DevHandle);
598dbec4c90SSuganath Prabu Subramani 	for (smid = ioc->scsiio_depth; smid && !found; smid--) {
599dbec4c90SSuganath Prabu Subramani 		struct scsiio_tracker *st;
600dbec4c90SSuganath Prabu Subramani 
601dbec4c90SSuganath Prabu Subramani 		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
602dbec4c90SSuganath Prabu Subramani 		if (!scmd)
603f92363d1SSreekanth Reddy 			continue;
604f92363d1SSreekanth Reddy 		if (lun != scmd->device->lun)
605f92363d1SSreekanth Reddy 			continue;
606f92363d1SSreekanth Reddy 		priv_data = scmd->device->hostdata;
607f92363d1SSreekanth Reddy 		if (priv_data->sas_target == NULL)
608f92363d1SSreekanth Reddy 			continue;
609f92363d1SSreekanth Reddy 		if (priv_data->sas_target->handle != handle)
610f92363d1SSreekanth Reddy 			continue;
611dbec4c90SSuganath Prabu Subramani 		st = scsi_cmd_priv(scmd);
612dbec4c90SSuganath Prabu Subramani 		tm_request->TaskMID = cpu_to_le16(st->smid);
613f92363d1SSreekanth Reddy 		found = 1;
614f92363d1SSreekanth Reddy 	}
615f92363d1SSreekanth Reddy 
616f92363d1SSreekanth Reddy 	if (!found) {
617f92363d1SSreekanth Reddy 		dctlprintk(ioc, pr_info(MPT3SAS_FMT
618f92363d1SSreekanth Reddy 			"%s: handle(0x%04x), lun(%d), no active mid!!\n",
619f92363d1SSreekanth Reddy 			ioc->name,
620f92363d1SSreekanth Reddy 		    desc, le16_to_cpu(tm_request->DevHandle), lun));
621f92363d1SSreekanth Reddy 		tm_reply = ioc->ctl_cmds.reply;
622f92363d1SSreekanth Reddy 		tm_reply->DevHandle = tm_request->DevHandle;
623f92363d1SSreekanth Reddy 		tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
624f92363d1SSreekanth Reddy 		tm_reply->TaskType = tm_request->TaskType;
625f92363d1SSreekanth Reddy 		tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
626f92363d1SSreekanth Reddy 		tm_reply->VP_ID = tm_request->VP_ID;
627f92363d1SSreekanth Reddy 		tm_reply->VF_ID = tm_request->VF_ID;
628f92363d1SSreekanth Reddy 		sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
629f92363d1SSreekanth Reddy 		if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
630f92363d1SSreekanth Reddy 		    sz))
631f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
632f92363d1SSreekanth Reddy 			    __LINE__, __func__);
633f92363d1SSreekanth Reddy 		return 1;
634f92363d1SSreekanth Reddy 	}
635f92363d1SSreekanth Reddy 
636f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT
637f92363d1SSreekanth Reddy 		"%s: handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
638f92363d1SSreekanth Reddy 	    desc, le16_to_cpu(tm_request->DevHandle), lun,
639f92363d1SSreekanth Reddy 	     le16_to_cpu(tm_request->TaskMID)));
640f92363d1SSreekanth Reddy 	return 0;
641f92363d1SSreekanth Reddy }
642f92363d1SSreekanth Reddy 
643f92363d1SSreekanth Reddy /**
644f92363d1SSreekanth Reddy  * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
645f92363d1SSreekanth Reddy  * @ioc: per adapter object
646f92363d1SSreekanth Reddy  * @karg - (struct mpt3_ioctl_command)
647f92363d1SSreekanth Reddy  * @mf - pointer to mf in user space
648f92363d1SSreekanth Reddy  */
649f92363d1SSreekanth Reddy static long
650f92363d1SSreekanth Reddy _ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
651f92363d1SSreekanth Reddy 	void __user *mf)
652f92363d1SSreekanth Reddy {
653f92363d1SSreekanth Reddy 	MPI2RequestHeader_t *mpi_request = NULL, *request;
654f92363d1SSreekanth Reddy 	MPI2DefaultReply_t *mpi_reply;
655aff39e61SSuganath Prabu Subramani 	Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
656c1a6c5acSChaitra P B 	struct _pcie_device *pcie_device = NULL;
657f92363d1SSreekanth Reddy 	u32 ioc_state;
658f92363d1SSreekanth Reddy 	u16 smid;
659c1a6c5acSChaitra P B 	u8 timeout;
660f92363d1SSreekanth Reddy 	u8 issue_reset;
661aff39e61SSuganath Prabu Subramani 	u32 sz, sz_arg;
662f92363d1SSreekanth Reddy 	void *psge;
663f92363d1SSreekanth Reddy 	void *data_out = NULL;
664f92363d1SSreekanth Reddy 	dma_addr_t data_out_dma = 0;
665f92363d1SSreekanth Reddy 	size_t data_out_sz = 0;
666f92363d1SSreekanth Reddy 	void *data_in = NULL;
667f92363d1SSreekanth Reddy 	dma_addr_t data_in_dma = 0;
668f92363d1SSreekanth Reddy 	size_t data_in_sz = 0;
669f92363d1SSreekanth Reddy 	long ret;
670f92363d1SSreekanth Reddy 	u16 wait_state_count;
671c696f7b8SSuganath Prabu Subramani 	u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
672c1a6c5acSChaitra P B 	u8 tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
673f92363d1SSreekanth Reddy 
674f92363d1SSreekanth Reddy 	issue_reset = 0;
675f92363d1SSreekanth Reddy 
676f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
677f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
678f92363d1SSreekanth Reddy 		    ioc->name, __func__);
679f92363d1SSreekanth Reddy 		ret = -EAGAIN;
680f92363d1SSreekanth Reddy 		goto out;
681f92363d1SSreekanth Reddy 	}
682f92363d1SSreekanth Reddy 
683f92363d1SSreekanth Reddy 	wait_state_count = 0;
684f92363d1SSreekanth Reddy 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
685f92363d1SSreekanth Reddy 	while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
686f92363d1SSreekanth Reddy 		if (wait_state_count++ == 10) {
687f92363d1SSreekanth Reddy 			pr_err(MPT3SAS_FMT
688f92363d1SSreekanth Reddy 			    "%s: failed due to ioc not operational\n",
689f92363d1SSreekanth Reddy 			    ioc->name, __func__);
690f92363d1SSreekanth Reddy 			ret = -EFAULT;
691f92363d1SSreekanth Reddy 			goto out;
692f92363d1SSreekanth Reddy 		}
693f92363d1SSreekanth Reddy 		ssleep(1);
694f92363d1SSreekanth Reddy 		ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
695f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT
696f92363d1SSreekanth Reddy 			"%s: waiting for operational state(count=%d)\n",
697f92363d1SSreekanth Reddy 			ioc->name,
698f92363d1SSreekanth Reddy 		    __func__, wait_state_count);
699f92363d1SSreekanth Reddy 	}
700f92363d1SSreekanth Reddy 	if (wait_state_count)
701f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
702f92363d1SSreekanth Reddy 		    ioc->name, __func__);
703f92363d1SSreekanth Reddy 
704f92363d1SSreekanth Reddy 	mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
705f92363d1SSreekanth Reddy 	if (!mpi_request) {
706f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
707f92363d1SSreekanth Reddy 			"%s: failed obtaining a memory for mpi_request\n",
708f92363d1SSreekanth Reddy 			ioc->name, __func__);
709f92363d1SSreekanth Reddy 		ret = -ENOMEM;
710f92363d1SSreekanth Reddy 		goto out;
711f92363d1SSreekanth Reddy 	}
712f92363d1SSreekanth Reddy 
713f92363d1SSreekanth Reddy 	/* Check for overflow and wraparound */
714f92363d1SSreekanth Reddy 	if (karg.data_sge_offset * 4 > ioc->request_sz ||
715f92363d1SSreekanth Reddy 	    karg.data_sge_offset > (UINT_MAX / 4)) {
716f92363d1SSreekanth Reddy 		ret = -EINVAL;
717f92363d1SSreekanth Reddy 		goto out;
718f92363d1SSreekanth Reddy 	}
719f92363d1SSreekanth Reddy 
720f92363d1SSreekanth Reddy 	/* copy in request message frame from user */
721f92363d1SSreekanth Reddy 	if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
722f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
723f92363d1SSreekanth Reddy 		    __func__);
724f92363d1SSreekanth Reddy 		ret = -EFAULT;
725f92363d1SSreekanth Reddy 		goto out;
726f92363d1SSreekanth Reddy 	}
727f92363d1SSreekanth Reddy 
728f92363d1SSreekanth Reddy 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
729f92363d1SSreekanth Reddy 		smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
730f92363d1SSreekanth Reddy 		if (!smid) {
731f92363d1SSreekanth Reddy 			pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
732f92363d1SSreekanth Reddy 			    ioc->name, __func__);
733f92363d1SSreekanth Reddy 			ret = -EAGAIN;
734f92363d1SSreekanth Reddy 			goto out;
735f92363d1SSreekanth Reddy 		}
736f92363d1SSreekanth Reddy 	} else {
737b0cd285eSHannes Reinecke 		/* Use first reserved smid for passthrough ioctls */
738b0cd285eSHannes Reinecke 		smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
739f92363d1SSreekanth Reddy 	}
740f92363d1SSreekanth Reddy 
741f92363d1SSreekanth Reddy 	ret = 0;
742f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
743f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
744f92363d1SSreekanth Reddy 	request = mpt3sas_base_get_msg_frame(ioc, smid);
745f92363d1SSreekanth Reddy 	memcpy(request, mpi_request, karg.data_sge_offset*4);
746f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
747f92363d1SSreekanth Reddy 	data_out_sz = karg.data_out_size;
748f92363d1SSreekanth Reddy 	data_in_sz = karg.data_in_size;
749f92363d1SSreekanth Reddy 
750f92363d1SSreekanth Reddy 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
751c696f7b8SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
752c696f7b8SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
753aff39e61SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
754aff39e61SSuganath Prabu Subramani 	    mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
755c696f7b8SSuganath Prabu Subramani 
756c696f7b8SSuganath Prabu Subramani 		device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
757c696f7b8SSuganath Prabu Subramani 		if (!device_handle || (device_handle >
758c696f7b8SSuganath Prabu Subramani 		    ioc->facts.MaxDevHandle)) {
759f92363d1SSreekanth Reddy 			ret = -EINVAL;
760f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
761f92363d1SSreekanth Reddy 			goto out;
762f92363d1SSreekanth Reddy 		}
763f92363d1SSreekanth Reddy 	}
764f92363d1SSreekanth Reddy 
765f92363d1SSreekanth Reddy 	/* obtain dma-able memory for data transfer */
766f92363d1SSreekanth Reddy 	if (data_out_sz) /* WRITE */ {
767f92363d1SSreekanth Reddy 		data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
768f92363d1SSreekanth Reddy 		    &data_out_dma);
769f92363d1SSreekanth Reddy 		if (!data_out) {
770f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
771f92363d1SSreekanth Reddy 			    __LINE__, __func__);
772f92363d1SSreekanth Reddy 			ret = -ENOMEM;
773f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
774f92363d1SSreekanth Reddy 			goto out;
775f92363d1SSreekanth Reddy 		}
776f92363d1SSreekanth Reddy 		if (copy_from_user(data_out, karg.data_out_buf_ptr,
777f92363d1SSreekanth Reddy 			data_out_sz)) {
778f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
779f92363d1SSreekanth Reddy 			    __LINE__, __func__);
780f92363d1SSreekanth Reddy 			ret =  -EFAULT;
781f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
782f92363d1SSreekanth Reddy 			goto out;
783f92363d1SSreekanth Reddy 		}
784f92363d1SSreekanth Reddy 	}
785f92363d1SSreekanth Reddy 
786f92363d1SSreekanth Reddy 	if (data_in_sz) /* READ */ {
787f92363d1SSreekanth Reddy 		data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
788f92363d1SSreekanth Reddy 		    &data_in_dma);
789f92363d1SSreekanth Reddy 		if (!data_in) {
790f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
791f92363d1SSreekanth Reddy 			    __LINE__, __func__);
792f92363d1SSreekanth Reddy 			ret = -ENOMEM;
793f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
794f92363d1SSreekanth Reddy 			goto out;
795f92363d1SSreekanth Reddy 		}
796f92363d1SSreekanth Reddy 	}
797f92363d1SSreekanth Reddy 
798f92363d1SSreekanth Reddy 	psge = (void *)request + (karg.data_sge_offset*4);
799f92363d1SSreekanth Reddy 
800f92363d1SSreekanth Reddy 	/* send command to firmware */
801f92363d1SSreekanth Reddy 	_ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
802f92363d1SSreekanth Reddy 
803f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
804f92363d1SSreekanth Reddy 	switch (mpi_request->Function) {
805aff39e61SSuganath Prabu Subramani 	case MPI2_FUNCTION_NVME_ENCAPSULATED:
806aff39e61SSuganath Prabu Subramani 	{
807aff39e61SSuganath Prabu Subramani 		nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
808aff39e61SSuganath Prabu Subramani 		/*
809aff39e61SSuganath Prabu Subramani 		 * Get the Physical Address of the sense buffer.
810aff39e61SSuganath Prabu Subramani 		 * Use Error Response buffer address field to hold the sense
811aff39e61SSuganath Prabu Subramani 		 * buffer address.
812aff39e61SSuganath Prabu Subramani 		 * Clear the internal sense buffer, which will potentially hold
813aff39e61SSuganath Prabu Subramani 		 * the Completion Queue Entry on return, or 0 if no Entry.
814aff39e61SSuganath Prabu Subramani 		 * Build the PRPs and set direction bits.
815aff39e61SSuganath Prabu Subramani 		 * Send the request.
816aff39e61SSuganath Prabu Subramani 		 */
817cf6bf971SChaitra P B 		nvme_encap_request->ErrorResponseBaseAddress =
818cf6bf971SChaitra P B 		    cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
819aff39e61SSuganath Prabu Subramani 		nvme_encap_request->ErrorResponseBaseAddress |=
820cf6bf971SChaitra P B 		   cpu_to_le64(le32_to_cpu(
821cf6bf971SChaitra P B 		   mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
822aff39e61SSuganath Prabu Subramani 		nvme_encap_request->ErrorResponseAllocationLength =
823cf6bf971SChaitra P B 					cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
824aff39e61SSuganath Prabu Subramani 		memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
825aff39e61SSuganath Prabu Subramani 		ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
826aff39e61SSuganath Prabu Subramani 		    data_out_dma, data_out_sz, data_in_dma, data_in_sz);
827aff39e61SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
828aff39e61SSuganath Prabu Subramani 			dtmprintk(ioc, pr_info(MPT3SAS_FMT "handle(0x%04x) :"
829aff39e61SSuganath Prabu Subramani 			    "ioctl failed due to device removal in progress\n",
830aff39e61SSuganath Prabu Subramani 			    ioc->name, device_handle));
831aff39e61SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
832aff39e61SSuganath Prabu Subramani 			ret = -EINVAL;
833aff39e61SSuganath Prabu Subramani 			goto out;
834aff39e61SSuganath Prabu Subramani 		}
83540114bdeSSuganath Prabu S 		mpt3sas_base_put_smid_nvme_encap(ioc, smid);
836aff39e61SSuganath Prabu Subramani 		break;
837aff39e61SSuganath Prabu Subramani 	}
838f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SCSI_IO_REQUEST:
839f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
840f92363d1SSreekanth Reddy 	{
841f92363d1SSreekanth Reddy 		Mpi2SCSIIORequest_t *scsiio_request =
842f92363d1SSreekanth Reddy 		    (Mpi2SCSIIORequest_t *)request;
843f92363d1SSreekanth Reddy 		scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
844f92363d1SSreekanth Reddy 		scsiio_request->SenseBufferLowAddress =
845f92363d1SSreekanth Reddy 		    mpt3sas_base_get_sense_buffer_dma(ioc, smid);
846f92363d1SSreekanth Reddy 		memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
847c696f7b8SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
848c696f7b8SSuganath Prabu Subramani 			dtmprintk(ioc, pr_info(MPT3SAS_FMT
849c696f7b8SSuganath Prabu Subramani 				"handle(0x%04x) :ioctl failed due to device removal in progress\n",
850c696f7b8SSuganath Prabu Subramani 				ioc->name, device_handle));
851c696f7b8SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
852c696f7b8SSuganath Prabu Subramani 			ret = -EINVAL;
853c696f7b8SSuganath Prabu Subramani 			goto out;
854c696f7b8SSuganath Prabu Subramani 		}
855f92363d1SSreekanth Reddy 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
856f92363d1SSreekanth Reddy 		    data_in_dma, data_in_sz);
857f92363d1SSreekanth Reddy 		if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
85881c16f83SSuganath Prabu Subramani 			ioc->put_smid_scsi_io(ioc, smid, device_handle);
859f92363d1SSreekanth Reddy 		else
86040114bdeSSuganath Prabu S 			mpt3sas_base_put_smid_default(ioc, smid);
861f92363d1SSreekanth Reddy 		break;
862f92363d1SSreekanth Reddy 	}
863f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SCSI_TASK_MGMT:
864f92363d1SSreekanth Reddy 	{
865f92363d1SSreekanth Reddy 		Mpi2SCSITaskManagementRequest_t *tm_request =
866f92363d1SSreekanth Reddy 		    (Mpi2SCSITaskManagementRequest_t *)request;
867f92363d1SSreekanth Reddy 
868f92363d1SSreekanth Reddy 		dtmprintk(ioc, pr_info(MPT3SAS_FMT
869f92363d1SSreekanth Reddy 			"TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
870f92363d1SSreekanth Reddy 			ioc->name,
871f92363d1SSreekanth Reddy 		    le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
872459325c4SChaitra P B 		ioc->got_task_abort_from_ioctl = 1;
873f92363d1SSreekanth Reddy 		if (tm_request->TaskType ==
874f92363d1SSreekanth Reddy 		    MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
875f92363d1SSreekanth Reddy 		    tm_request->TaskType ==
876f92363d1SSreekanth Reddy 		    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
877f92363d1SSreekanth Reddy 			if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
878f92363d1SSreekanth Reddy 				mpt3sas_base_free_smid(ioc, smid);
879459325c4SChaitra P B 				ioc->got_task_abort_from_ioctl = 0;
880f92363d1SSreekanth Reddy 				goto out;
881f92363d1SSreekanth Reddy 			}
882f92363d1SSreekanth Reddy 		}
883459325c4SChaitra P B 		ioc->got_task_abort_from_ioctl = 0;
884f92363d1SSreekanth Reddy 
885c696f7b8SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
886c696f7b8SSuganath Prabu Subramani 			dtmprintk(ioc, pr_info(MPT3SAS_FMT
887c696f7b8SSuganath Prabu Subramani 				"handle(0x%04x) :ioctl failed due to device removal in progress\n",
888c696f7b8SSuganath Prabu Subramani 				ioc->name, device_handle));
889c696f7b8SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
890c696f7b8SSuganath Prabu Subramani 			ret = -EINVAL;
891c696f7b8SSuganath Prabu Subramani 			goto out;
892c696f7b8SSuganath Prabu Subramani 		}
893f92363d1SSreekanth Reddy 		mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
894f92363d1SSreekanth Reddy 		    tm_request->DevHandle));
895f92363d1SSreekanth Reddy 		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
896f92363d1SSreekanth Reddy 		    data_in_dma, data_in_sz);
89740114bdeSSuganath Prabu S 		mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
898f92363d1SSreekanth Reddy 		break;
899f92363d1SSreekanth Reddy 	}
900f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SMP_PASSTHROUGH:
901f92363d1SSreekanth Reddy 	{
902f92363d1SSreekanth Reddy 		Mpi2SmpPassthroughRequest_t *smp_request =
903f92363d1SSreekanth Reddy 		    (Mpi2SmpPassthroughRequest_t *)mpi_request;
904f92363d1SSreekanth Reddy 		u8 *data;
905f92363d1SSreekanth Reddy 
906f92363d1SSreekanth Reddy 		/* ioc determines which port to use */
907f92363d1SSreekanth Reddy 		smp_request->PhysicalPort = 0xFF;
908f92363d1SSreekanth Reddy 		if (smp_request->PassthroughFlags &
909f92363d1SSreekanth Reddy 		    MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
910f92363d1SSreekanth Reddy 			data = (u8 *)&smp_request->SGL;
911f92363d1SSreekanth Reddy 		else {
912f92363d1SSreekanth Reddy 			if (unlikely(data_out == NULL)) {
913f92363d1SSreekanth Reddy 				pr_err("failure at %s:%d/%s()!\n",
914f92363d1SSreekanth Reddy 				    __FILE__, __LINE__, __func__);
915f92363d1SSreekanth Reddy 				mpt3sas_base_free_smid(ioc, smid);
916f92363d1SSreekanth Reddy 				ret = -EINVAL;
917f92363d1SSreekanth Reddy 				goto out;
918f92363d1SSreekanth Reddy 			}
919f92363d1SSreekanth Reddy 			data = data_out;
920f92363d1SSreekanth Reddy 		}
921f92363d1SSreekanth Reddy 
922f92363d1SSreekanth Reddy 		if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
923f92363d1SSreekanth Reddy 			ioc->ioc_link_reset_in_progress = 1;
924f92363d1SSreekanth Reddy 			ioc->ignore_loginfos = 1;
925f92363d1SSreekanth Reddy 		}
926f92363d1SSreekanth Reddy 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
927f92363d1SSreekanth Reddy 		    data_in_sz);
92840114bdeSSuganath Prabu S 		mpt3sas_base_put_smid_default(ioc, smid);
929f92363d1SSreekanth Reddy 		break;
930f92363d1SSreekanth Reddy 	}
931f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SATA_PASSTHROUGH:
932c696f7b8SSuganath Prabu Subramani 	{
933c696f7b8SSuganath Prabu Subramani 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
934c696f7b8SSuganath Prabu Subramani 			dtmprintk(ioc, pr_info(MPT3SAS_FMT
935c696f7b8SSuganath Prabu Subramani 				"handle(0x%04x) :ioctl failed due to device removal in progress\n",
936c696f7b8SSuganath Prabu Subramani 				ioc->name, device_handle));
937c696f7b8SSuganath Prabu Subramani 			mpt3sas_base_free_smid(ioc, smid);
938c696f7b8SSuganath Prabu Subramani 			ret = -EINVAL;
939c696f7b8SSuganath Prabu Subramani 			goto out;
940c696f7b8SSuganath Prabu Subramani 		}
941c696f7b8SSuganath Prabu Subramani 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
942c696f7b8SSuganath Prabu Subramani 		    data_in_sz);
94340114bdeSSuganath Prabu S 		mpt3sas_base_put_smid_default(ioc, smid);
944c696f7b8SSuganath Prabu Subramani 		break;
945c696f7b8SSuganath Prabu Subramani 	}
946f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_FW_DOWNLOAD:
947f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_FW_UPLOAD:
948f92363d1SSreekanth Reddy 	{
949f92363d1SSreekanth Reddy 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
950f92363d1SSreekanth Reddy 		    data_in_sz);
95140114bdeSSuganath Prabu S 		mpt3sas_base_put_smid_default(ioc, smid);
952f92363d1SSreekanth Reddy 		break;
953f92363d1SSreekanth Reddy 	}
954f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_TOOLBOX:
955f92363d1SSreekanth Reddy 	{
956f92363d1SSreekanth Reddy 		Mpi2ToolboxCleanRequest_t *toolbox_request =
957f92363d1SSreekanth Reddy 			(Mpi2ToolboxCleanRequest_t *)mpi_request;
958f92363d1SSreekanth Reddy 
959f92363d1SSreekanth Reddy 		if (toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL) {
960f92363d1SSreekanth Reddy 			ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
961f92363d1SSreekanth Reddy 				data_in_dma, data_in_sz);
962f92363d1SSreekanth Reddy 		} else {
963f92363d1SSreekanth Reddy 			ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
964f92363d1SSreekanth Reddy 				data_in_dma, data_in_sz);
965f92363d1SSreekanth Reddy 		}
96640114bdeSSuganath Prabu S 		mpt3sas_base_put_smid_default(ioc, smid);
967f92363d1SSreekanth Reddy 		break;
968f92363d1SSreekanth Reddy 	}
969f92363d1SSreekanth Reddy 	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
970f92363d1SSreekanth Reddy 	{
971f92363d1SSreekanth Reddy 		Mpi2SasIoUnitControlRequest_t *sasiounit_request =
972f92363d1SSreekanth Reddy 		    (Mpi2SasIoUnitControlRequest_t *)mpi_request;
973f92363d1SSreekanth Reddy 
974f92363d1SSreekanth Reddy 		if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
975f92363d1SSreekanth Reddy 		    || sasiounit_request->Operation ==
976f92363d1SSreekanth Reddy 		    MPI2_SAS_OP_PHY_LINK_RESET) {
977f92363d1SSreekanth Reddy 			ioc->ioc_link_reset_in_progress = 1;
978f92363d1SSreekanth Reddy 			ioc->ignore_loginfos = 1;
979f92363d1SSreekanth Reddy 		}
980f92363d1SSreekanth Reddy 		/* drop to default case for posting the request */
981f92363d1SSreekanth Reddy 	}
982eb0c7af2SBart Van Assche 		/* fall through */
983f92363d1SSreekanth Reddy 	default:
984f92363d1SSreekanth Reddy 		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
985f92363d1SSreekanth Reddy 		    data_in_dma, data_in_sz);
98640114bdeSSuganath Prabu S 		mpt3sas_base_put_smid_default(ioc, smid);
987f92363d1SSreekanth Reddy 		break;
988f92363d1SSreekanth Reddy 	}
989f92363d1SSreekanth Reddy 
990f92363d1SSreekanth Reddy 	if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
991f92363d1SSreekanth Reddy 		timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
992f92363d1SSreekanth Reddy 	else
993f92363d1SSreekanth Reddy 		timeout = karg.timeout;
9948bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
995f92363d1SSreekanth Reddy 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
996f92363d1SSreekanth Reddy 		Mpi2SCSITaskManagementRequest_t *tm_request =
997f92363d1SSreekanth Reddy 		    (Mpi2SCSITaskManagementRequest_t *)mpi_request;
998f92363d1SSreekanth Reddy 		mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
999f92363d1SSreekanth Reddy 		    tm_request->DevHandle));
1000f92363d1SSreekanth Reddy 		mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
1001f92363d1SSreekanth Reddy 	} else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
1002f92363d1SSreekanth Reddy 	    mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
1003f92363d1SSreekanth Reddy 		ioc->ioc_link_reset_in_progress) {
1004f92363d1SSreekanth Reddy 		ioc->ioc_link_reset_in_progress = 0;
1005f92363d1SSreekanth Reddy 		ioc->ignore_loginfos = 0;
1006f92363d1SSreekanth Reddy 	}
1007f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1008d37306caSChaitra P B 		issue_reset =
1009d37306caSChaitra P B 			mpt3sas_base_check_cmd_timeout(ioc,
1010d37306caSChaitra P B 				ioc->ctl_cmds.status, mpi_request,
1011d37306caSChaitra P B 				karg.data_sge_offset);
1012f92363d1SSreekanth Reddy 		goto issue_host_reset;
1013f92363d1SSreekanth Reddy 	}
1014f92363d1SSreekanth Reddy 
1015f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
1016f92363d1SSreekanth Reddy 
1017f92363d1SSreekanth Reddy 	if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
1018f92363d1SSreekanth Reddy 	    (ioc->logging_level & MPT_DEBUG_TM)) {
1019f92363d1SSreekanth Reddy 		Mpi2SCSITaskManagementReply_t *tm_reply =
1020f92363d1SSreekanth Reddy 		    (Mpi2SCSITaskManagementReply_t *)mpi_reply;
1021f92363d1SSreekanth Reddy 
1022f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT "TASK_MGMT: " \
1023f92363d1SSreekanth Reddy 		    "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
1024f92363d1SSreekanth Reddy 		    "TerminationCount(0x%08x)\n", ioc->name,
1025f92363d1SSreekanth Reddy 		    le16_to_cpu(tm_reply->IOCStatus),
1026f92363d1SSreekanth Reddy 		    le32_to_cpu(tm_reply->IOCLogInfo),
1027f92363d1SSreekanth Reddy 		    le32_to_cpu(tm_reply->TerminationCount));
1028f92363d1SSreekanth Reddy 	}
1029af009411SSreekanth Reddy 
1030f92363d1SSreekanth Reddy 	/* copy out xdata to user */
1031f92363d1SSreekanth Reddy 	if (data_in_sz) {
1032f92363d1SSreekanth Reddy 		if (copy_to_user(karg.data_in_buf_ptr, data_in,
1033f92363d1SSreekanth Reddy 		    data_in_sz)) {
1034f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1035f92363d1SSreekanth Reddy 			    __LINE__, __func__);
1036f92363d1SSreekanth Reddy 			ret = -ENODATA;
1037f92363d1SSreekanth Reddy 			goto out;
1038f92363d1SSreekanth Reddy 		}
1039f92363d1SSreekanth Reddy 	}
1040f92363d1SSreekanth Reddy 
1041f92363d1SSreekanth Reddy 	/* copy out reply message frame to user */
1042f92363d1SSreekanth Reddy 	if (karg.max_reply_bytes) {
1043f92363d1SSreekanth Reddy 		sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1044f92363d1SSreekanth Reddy 		if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1045f92363d1SSreekanth Reddy 		    sz)) {
1046f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1047f92363d1SSreekanth Reddy 			    __LINE__, __func__);
1048f92363d1SSreekanth Reddy 			ret = -ENODATA;
1049f92363d1SSreekanth Reddy 			goto out;
1050f92363d1SSreekanth Reddy 		}
1051f92363d1SSreekanth Reddy 	}
1052f92363d1SSreekanth Reddy 
1053aff39e61SSuganath Prabu Subramani 	/* copy out sense/NVMe Error Response to user */
1054f92363d1SSreekanth Reddy 	if (karg.max_sense_bytes && (mpi_request->Function ==
1055f92363d1SSreekanth Reddy 	    MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
1056aff39e61SSuganath Prabu Subramani 	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1057aff39e61SSuganath Prabu Subramani 	    MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1058aff39e61SSuganath Prabu Subramani 		if (karg.sense_data_ptr == NULL) {
1059aff39e61SSuganath Prabu Subramani 			pr_info(MPT3SAS_FMT "Response buffer provided"
1060aff39e61SSuganath Prabu Subramani 			    " by application is NULL; Response data will"
1061aff39e61SSuganath Prabu Subramani 			    " not be returned.\n", ioc->name);
1062aff39e61SSuganath Prabu Subramani 			goto out;
1063aff39e61SSuganath Prabu Subramani 		}
1064aff39e61SSuganath Prabu Subramani 		sz_arg = (mpi_request->Function ==
1065aff39e61SSuganath Prabu Subramani 		MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1066aff39e61SSuganath Prabu Subramani 							SCSI_SENSE_BUFFERSIZE;
1067aff39e61SSuganath Prabu Subramani 		sz = min_t(u32, karg.max_sense_bytes, sz_arg);
1068f92363d1SSreekanth Reddy 		if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1069f92363d1SSreekanth Reddy 		    sz)) {
1070f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1071f92363d1SSreekanth Reddy 				__LINE__, __func__);
1072f92363d1SSreekanth Reddy 			ret = -ENODATA;
1073f92363d1SSreekanth Reddy 			goto out;
1074f92363d1SSreekanth Reddy 		}
1075f92363d1SSreekanth Reddy 	}
1076f92363d1SSreekanth Reddy 
1077f92363d1SSreekanth Reddy  issue_host_reset:
1078f92363d1SSreekanth Reddy 	if (issue_reset) {
1079f92363d1SSreekanth Reddy 		ret = -ENODATA;
1080f92363d1SSreekanth Reddy 		if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1081f92363d1SSreekanth Reddy 		    mpi_request->Function ==
1082f92363d1SSreekanth Reddy 		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1083f92363d1SSreekanth Reddy 		    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
1084f92363d1SSreekanth Reddy 			pr_info(MPT3SAS_FMT "issue target reset: handle = (0x%04x)\n",
1085f92363d1SSreekanth Reddy 				ioc->name,
1086f92363d1SSreekanth Reddy 				le16_to_cpu(mpi_request->FunctionDependent1));
1087f92363d1SSreekanth Reddy 			mpt3sas_halt_firmware(ioc);
1088c1a6c5acSChaitra P B 			pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1089c1a6c5acSChaitra P B 				le16_to_cpu(mpi_request->FunctionDependent1));
1090c1a6c5acSChaitra P B 			if (pcie_device && (!ioc->tm_custom_handling))
109196902835SCalvin Owens 				mpt3sas_scsih_issue_locked_tm(ioc,
1092c1a6c5acSChaitra P B 				  le16_to_cpu(mpi_request->FunctionDependent1),
1093c1a6c5acSChaitra P B 				  0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1094c1a6c5acSChaitra P B 				  0, pcie_device->reset_timeout,
1095c1a6c5acSChaitra P B 				  tr_method);
1096c1a6c5acSChaitra P B 			else
1097c1a6c5acSChaitra P B 				mpt3sas_scsih_issue_locked_tm(ioc,
1098c1a6c5acSChaitra P B 				  le16_to_cpu(mpi_request->FunctionDependent1),
1099c1a6c5acSChaitra P B 				  0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1100c1a6c5acSChaitra P B 				  0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
1101f92363d1SSreekanth Reddy 		} else
110298c56ad3SCalvin Owens 			mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1103f92363d1SSreekanth Reddy 	}
1104f92363d1SSreekanth Reddy 
1105f92363d1SSreekanth Reddy  out:
1106c1a6c5acSChaitra P B 	if (pcie_device)
1107c1a6c5acSChaitra P B 		pcie_device_put(pcie_device);
1108f92363d1SSreekanth Reddy 
1109f92363d1SSreekanth Reddy 	/* free memory associated with sg buffers */
1110f92363d1SSreekanth Reddy 	if (data_in)
1111f92363d1SSreekanth Reddy 		pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1112f92363d1SSreekanth Reddy 		    data_in_dma);
1113f92363d1SSreekanth Reddy 
1114f92363d1SSreekanth Reddy 	if (data_out)
1115f92363d1SSreekanth Reddy 		pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1116f92363d1SSreekanth Reddy 		    data_out_dma);
1117f92363d1SSreekanth Reddy 
1118f92363d1SSreekanth Reddy 	kfree(mpi_request);
1119f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1120f92363d1SSreekanth Reddy 	return ret;
1121f92363d1SSreekanth Reddy }
1122f92363d1SSreekanth Reddy 
1123f92363d1SSreekanth Reddy /**
1124f92363d1SSreekanth Reddy  * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1125f92363d1SSreekanth Reddy  * @ioc: per adapter object
1126f92363d1SSreekanth Reddy  * @arg - user space buffer containing ioctl content
1127f92363d1SSreekanth Reddy  */
1128f92363d1SSreekanth Reddy static long
1129f92363d1SSreekanth Reddy _ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1130f92363d1SSreekanth Reddy {
1131f92363d1SSreekanth Reddy 	struct mpt3_ioctl_iocinfo karg;
1132f92363d1SSreekanth Reddy 
1133f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1134f92363d1SSreekanth Reddy 	    __func__));
1135f92363d1SSreekanth Reddy 
1136f92363d1SSreekanth Reddy 	memset(&karg, 0 , sizeof(karg));
1137f92363d1SSreekanth Reddy 	if (ioc->pfacts)
1138f92363d1SSreekanth Reddy 		karg.port_number = ioc->pfacts[0].PortNumber;
1139f92363d1SSreekanth Reddy 	karg.hw_rev = ioc->pdev->revision;
1140f92363d1SSreekanth Reddy 	karg.pci_id = ioc->pdev->device;
1141f92363d1SSreekanth Reddy 	karg.subsystem_device = ioc->pdev->subsystem_device;
1142f92363d1SSreekanth Reddy 	karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1143f92363d1SSreekanth Reddy 	karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1144f92363d1SSreekanth Reddy 	karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1145f92363d1SSreekanth Reddy 	karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1146f92363d1SSreekanth Reddy 	karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1147f92363d1SSreekanth Reddy 	karg.firmware_version = ioc->facts.FWVersion.Word;
1148c84b06a4SSreekanth Reddy 	strcpy(karg.driver_version, ioc->driver_name);
1149f92363d1SSreekanth Reddy 	strcat(karg.driver_version, "-");
1150d357e84dSSreekanth Reddy 	switch  (ioc->hba_mpi_version_belonged) {
1151d357e84dSSreekanth Reddy 	case MPI2_VERSION:
11527786ab6aSSreekanth Reddy 		if (ioc->is_warpdrive)
11537786ab6aSSreekanth Reddy 			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
11547786ab6aSSreekanth Reddy 		else
1155d357e84dSSreekanth Reddy 			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1156d357e84dSSreekanth Reddy 		strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1157d357e84dSSreekanth Reddy 		break;
1158d357e84dSSreekanth Reddy 	case MPI25_VERSION:
1159b130b0d5SSuganath prabu Subramani 	case MPI26_VERSION:
1160998f26aeSSuganath Prabu Subramani 		if (ioc->is_gen35_ioc)
1161998f26aeSSuganath Prabu Subramani 			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1162998f26aeSSuganath Prabu Subramani 		else
1163d357e84dSSreekanth Reddy 			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1164d357e84dSSreekanth Reddy 		strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1165d357e84dSSreekanth Reddy 		break;
1166d357e84dSSreekanth Reddy 	}
1167f92363d1SSreekanth Reddy 	karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1168f92363d1SSreekanth Reddy 
1169f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1170f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1171f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1172f92363d1SSreekanth Reddy 		return -EFAULT;
1173f92363d1SSreekanth Reddy 	}
1174f92363d1SSreekanth Reddy 	return 0;
1175f92363d1SSreekanth Reddy }
1176f92363d1SSreekanth Reddy 
1177f92363d1SSreekanth Reddy /**
1178f92363d1SSreekanth Reddy  * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1179f92363d1SSreekanth Reddy  * @ioc: per adapter object
1180f92363d1SSreekanth Reddy  * @arg - user space buffer containing ioctl content
1181f92363d1SSreekanth Reddy  */
1182f92363d1SSreekanth Reddy static long
1183f92363d1SSreekanth Reddy _ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1184f92363d1SSreekanth Reddy {
1185f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventquery karg;
1186f92363d1SSreekanth Reddy 
1187f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1188f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1189f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1190f92363d1SSreekanth Reddy 		return -EFAULT;
1191f92363d1SSreekanth Reddy 	}
1192f92363d1SSreekanth Reddy 
1193f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1194f92363d1SSreekanth Reddy 	    __func__));
1195f92363d1SSreekanth Reddy 
1196f92363d1SSreekanth Reddy 	karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1197f92363d1SSreekanth Reddy 	memcpy(karg.event_types, ioc->event_type,
1198f92363d1SSreekanth Reddy 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1199f92363d1SSreekanth Reddy 
1200f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1201f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1202f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1203f92363d1SSreekanth Reddy 		return -EFAULT;
1204f92363d1SSreekanth Reddy 	}
1205f92363d1SSreekanth Reddy 	return 0;
1206f92363d1SSreekanth Reddy }
1207f92363d1SSreekanth Reddy 
1208f92363d1SSreekanth Reddy /**
1209f92363d1SSreekanth Reddy  * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1210f92363d1SSreekanth Reddy  * @ioc: per adapter object
1211f92363d1SSreekanth Reddy  * @arg - user space buffer containing ioctl content
1212f92363d1SSreekanth Reddy  */
1213f92363d1SSreekanth Reddy static long
1214f92363d1SSreekanth Reddy _ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1215f92363d1SSreekanth Reddy {
1216f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventenable karg;
1217f92363d1SSreekanth Reddy 
1218f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1219f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1220f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1221f92363d1SSreekanth Reddy 		return -EFAULT;
1222f92363d1SSreekanth Reddy 	}
1223f92363d1SSreekanth Reddy 
1224f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1225f92363d1SSreekanth Reddy 	    __func__));
1226f92363d1SSreekanth Reddy 
1227f92363d1SSreekanth Reddy 	memcpy(ioc->event_type, karg.event_types,
1228f92363d1SSreekanth Reddy 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1229f92363d1SSreekanth Reddy 	mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1230f92363d1SSreekanth Reddy 
1231f92363d1SSreekanth Reddy 	if (ioc->event_log)
1232f92363d1SSreekanth Reddy 		return 0;
1233f92363d1SSreekanth Reddy 	/* initialize event_log */
1234f92363d1SSreekanth Reddy 	ioc->event_context = 0;
1235f92363d1SSreekanth Reddy 	ioc->aen_event_read_flag = 0;
1236f92363d1SSreekanth Reddy 	ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1237f92363d1SSreekanth Reddy 	    sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1238f92363d1SSreekanth Reddy 	if (!ioc->event_log) {
1239f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1240f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1241f92363d1SSreekanth Reddy 		return -ENOMEM;
1242f92363d1SSreekanth Reddy 	}
1243f92363d1SSreekanth Reddy 	return 0;
1244f92363d1SSreekanth Reddy }
1245f92363d1SSreekanth Reddy 
1246f92363d1SSreekanth Reddy /**
1247f92363d1SSreekanth Reddy  * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1248f92363d1SSreekanth Reddy  * @ioc: per adapter object
1249f92363d1SSreekanth Reddy  * @arg - user space buffer containing ioctl content
1250f92363d1SSreekanth Reddy  */
1251f92363d1SSreekanth Reddy static long
1252f92363d1SSreekanth Reddy _ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1253f92363d1SSreekanth Reddy {
1254f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventreport karg;
1255f92363d1SSreekanth Reddy 	u32 number_bytes, max_events, max;
1256f92363d1SSreekanth Reddy 	struct mpt3_ioctl_eventreport __user *uarg = arg;
1257f92363d1SSreekanth Reddy 
1258f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1259f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1260f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1261f92363d1SSreekanth Reddy 		return -EFAULT;
1262f92363d1SSreekanth Reddy 	}
1263f92363d1SSreekanth Reddy 
1264f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1265f92363d1SSreekanth Reddy 	    __func__));
1266f92363d1SSreekanth Reddy 
1267f92363d1SSreekanth Reddy 	number_bytes = karg.hdr.max_data_size -
1268f92363d1SSreekanth Reddy 	    sizeof(struct mpt3_ioctl_header);
1269f92363d1SSreekanth Reddy 	max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1270f92363d1SSreekanth Reddy 	max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1271f92363d1SSreekanth Reddy 
1272f92363d1SSreekanth Reddy 	/* If fewer than 1 event is requested, there must have
1273f92363d1SSreekanth Reddy 	 * been some type of error.
1274f92363d1SSreekanth Reddy 	 */
1275f92363d1SSreekanth Reddy 	if (!max || !ioc->event_log)
1276f92363d1SSreekanth Reddy 		return -ENODATA;
1277f92363d1SSreekanth Reddy 
1278f92363d1SSreekanth Reddy 	number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1279f92363d1SSreekanth Reddy 	if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1280f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1281f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1282f92363d1SSreekanth Reddy 		return -EFAULT;
1283f92363d1SSreekanth Reddy 	}
1284f92363d1SSreekanth Reddy 
1285f92363d1SSreekanth Reddy 	/* reset flag so SIGIO can restart */
1286f92363d1SSreekanth Reddy 	ioc->aen_event_read_flag = 0;
1287f92363d1SSreekanth Reddy 	return 0;
1288f92363d1SSreekanth Reddy }
1289f92363d1SSreekanth Reddy 
1290f92363d1SSreekanth Reddy /**
1291f92363d1SSreekanth Reddy  * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1292f92363d1SSreekanth Reddy  * @ioc: per adapter object
1293f92363d1SSreekanth Reddy  * @arg - user space buffer containing ioctl content
1294f92363d1SSreekanth Reddy  */
1295f92363d1SSreekanth Reddy static long
1296f92363d1SSreekanth Reddy _ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1297f92363d1SSreekanth Reddy {
1298f92363d1SSreekanth Reddy 	struct mpt3_ioctl_diag_reset karg;
1299f92363d1SSreekanth Reddy 	int retval;
1300f92363d1SSreekanth Reddy 
1301f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1302f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1303f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1304f92363d1SSreekanth Reddy 		return -EFAULT;
1305f92363d1SSreekanth Reddy 	}
1306f92363d1SSreekanth Reddy 
1307f92363d1SSreekanth Reddy 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
1308f92363d1SSreekanth Reddy 	    ioc->is_driver_loading)
1309f92363d1SSreekanth Reddy 		return -EAGAIN;
1310f92363d1SSreekanth Reddy 
1311f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1312f92363d1SSreekanth Reddy 	    __func__));
1313f92363d1SSreekanth Reddy 
131498c56ad3SCalvin Owens 	retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1315f92363d1SSreekanth Reddy 	pr_info(MPT3SAS_FMT "host reset: %s\n",
1316f92363d1SSreekanth Reddy 	    ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1317f92363d1SSreekanth Reddy 	return 0;
1318f92363d1SSreekanth Reddy }
1319f92363d1SSreekanth Reddy 
1320f92363d1SSreekanth Reddy /**
1321f92363d1SSreekanth Reddy  * _ctl_btdh_search_sas_device - searching for sas device
1322f92363d1SSreekanth Reddy  * @ioc: per adapter object
1323f92363d1SSreekanth Reddy  * @btdh: btdh ioctl payload
1324f92363d1SSreekanth Reddy  */
1325f92363d1SSreekanth Reddy static int
1326f92363d1SSreekanth Reddy _ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1327f92363d1SSreekanth Reddy 	struct mpt3_ioctl_btdh_mapping *btdh)
1328f92363d1SSreekanth Reddy {
1329f92363d1SSreekanth Reddy 	struct _sas_device *sas_device;
1330f92363d1SSreekanth Reddy 	unsigned long flags;
1331f92363d1SSreekanth Reddy 	int rc = 0;
1332f92363d1SSreekanth Reddy 
1333f92363d1SSreekanth Reddy 	if (list_empty(&ioc->sas_device_list))
1334f92363d1SSreekanth Reddy 		return rc;
1335f92363d1SSreekanth Reddy 
1336f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1337f92363d1SSreekanth Reddy 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1338f92363d1SSreekanth Reddy 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1339f92363d1SSreekanth Reddy 		    btdh->handle == sas_device->handle) {
1340f92363d1SSreekanth Reddy 			btdh->bus = sas_device->channel;
1341f92363d1SSreekanth Reddy 			btdh->id = sas_device->id;
1342f92363d1SSreekanth Reddy 			rc = 1;
1343f92363d1SSreekanth Reddy 			goto out;
1344f92363d1SSreekanth Reddy 		} else if (btdh->bus == sas_device->channel && btdh->id ==
1345f92363d1SSreekanth Reddy 		    sas_device->id && btdh->handle == 0xFFFF) {
1346f92363d1SSreekanth Reddy 			btdh->handle = sas_device->handle;
1347f92363d1SSreekanth Reddy 			rc = 1;
1348f92363d1SSreekanth Reddy 			goto out;
1349f92363d1SSreekanth Reddy 		}
1350f92363d1SSreekanth Reddy 	}
1351f92363d1SSreekanth Reddy  out:
1352f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1353f92363d1SSreekanth Reddy 	return rc;
1354f92363d1SSreekanth Reddy }
1355f92363d1SSreekanth Reddy 
1356f92363d1SSreekanth Reddy /**
135745aa6a1aSSuganath Prabu Subramani  * _ctl_btdh_search_pcie_device - searching for pcie device
135845aa6a1aSSuganath Prabu Subramani  * @ioc: per adapter object
135945aa6a1aSSuganath Prabu Subramani  * @btdh: btdh ioctl payload
136045aa6a1aSSuganath Prabu Subramani  */
136145aa6a1aSSuganath Prabu Subramani static int
136245aa6a1aSSuganath Prabu Subramani _ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
136345aa6a1aSSuganath Prabu Subramani 	struct mpt3_ioctl_btdh_mapping *btdh)
136445aa6a1aSSuganath Prabu Subramani {
136545aa6a1aSSuganath Prabu Subramani 	struct _pcie_device *pcie_device;
136645aa6a1aSSuganath Prabu Subramani 	unsigned long flags;
136745aa6a1aSSuganath Prabu Subramani 	int rc = 0;
136845aa6a1aSSuganath Prabu Subramani 
136945aa6a1aSSuganath Prabu Subramani 	if (list_empty(&ioc->pcie_device_list))
137045aa6a1aSSuganath Prabu Subramani 		return rc;
137145aa6a1aSSuganath Prabu Subramani 
137245aa6a1aSSuganath Prabu Subramani 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
137345aa6a1aSSuganath Prabu Subramani 	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
137445aa6a1aSSuganath Prabu Subramani 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
137545aa6a1aSSuganath Prabu Subramani 			   btdh->handle == pcie_device->handle) {
137645aa6a1aSSuganath Prabu Subramani 			btdh->bus = pcie_device->channel;
137745aa6a1aSSuganath Prabu Subramani 			btdh->id = pcie_device->id;
137845aa6a1aSSuganath Prabu Subramani 			rc = 1;
137945aa6a1aSSuganath Prabu Subramani 			goto out;
138045aa6a1aSSuganath Prabu Subramani 		} else if (btdh->bus == pcie_device->channel && btdh->id ==
138145aa6a1aSSuganath Prabu Subramani 			   pcie_device->id && btdh->handle == 0xFFFF) {
138245aa6a1aSSuganath Prabu Subramani 			btdh->handle = pcie_device->handle;
138345aa6a1aSSuganath Prabu Subramani 			rc = 1;
138445aa6a1aSSuganath Prabu Subramani 			goto out;
138545aa6a1aSSuganath Prabu Subramani 		}
138645aa6a1aSSuganath Prabu Subramani 	}
138745aa6a1aSSuganath Prabu Subramani  out:
138845aa6a1aSSuganath Prabu Subramani 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
138945aa6a1aSSuganath Prabu Subramani 	return rc;
139045aa6a1aSSuganath Prabu Subramani }
139145aa6a1aSSuganath Prabu Subramani 
139245aa6a1aSSuganath Prabu Subramani /**
1393f92363d1SSreekanth Reddy  * _ctl_btdh_search_raid_device - searching for raid device
1394f92363d1SSreekanth Reddy  * @ioc: per adapter object
1395f92363d1SSreekanth Reddy  * @btdh: btdh ioctl payload
1396f92363d1SSreekanth Reddy  */
1397f92363d1SSreekanth Reddy static int
1398f92363d1SSreekanth Reddy _ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1399f92363d1SSreekanth Reddy 	struct mpt3_ioctl_btdh_mapping *btdh)
1400f92363d1SSreekanth Reddy {
1401f92363d1SSreekanth Reddy 	struct _raid_device *raid_device;
1402f92363d1SSreekanth Reddy 	unsigned long flags;
1403f92363d1SSreekanth Reddy 	int rc = 0;
1404f92363d1SSreekanth Reddy 
1405f92363d1SSreekanth Reddy 	if (list_empty(&ioc->raid_device_list))
1406f92363d1SSreekanth Reddy 		return rc;
1407f92363d1SSreekanth Reddy 
1408f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1409f92363d1SSreekanth Reddy 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1410f92363d1SSreekanth Reddy 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1411f92363d1SSreekanth Reddy 		    btdh->handle == raid_device->handle) {
1412f92363d1SSreekanth Reddy 			btdh->bus = raid_device->channel;
1413f92363d1SSreekanth Reddy 			btdh->id = raid_device->id;
1414f92363d1SSreekanth Reddy 			rc = 1;
1415f92363d1SSreekanth Reddy 			goto out;
1416f92363d1SSreekanth Reddy 		} else if (btdh->bus == raid_device->channel && btdh->id ==
1417f92363d1SSreekanth Reddy 		    raid_device->id && btdh->handle == 0xFFFF) {
1418f92363d1SSreekanth Reddy 			btdh->handle = raid_device->handle;
1419f92363d1SSreekanth Reddy 			rc = 1;
1420f92363d1SSreekanth Reddy 			goto out;
1421f92363d1SSreekanth Reddy 		}
1422f92363d1SSreekanth Reddy 	}
1423f92363d1SSreekanth Reddy  out:
1424f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1425f92363d1SSreekanth Reddy 	return rc;
1426f92363d1SSreekanth Reddy }
1427f92363d1SSreekanth Reddy 
1428f92363d1SSreekanth Reddy /**
1429f92363d1SSreekanth Reddy  * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1430f92363d1SSreekanth Reddy  * @ioc: per adapter object
1431f92363d1SSreekanth Reddy  * @arg - user space buffer containing ioctl content
1432f92363d1SSreekanth Reddy  */
1433f92363d1SSreekanth Reddy static long
1434f92363d1SSreekanth Reddy _ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1435f92363d1SSreekanth Reddy {
1436f92363d1SSreekanth Reddy 	struct mpt3_ioctl_btdh_mapping karg;
1437f92363d1SSreekanth Reddy 	int rc;
1438f92363d1SSreekanth Reddy 
1439f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1440f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1441f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1442f92363d1SSreekanth Reddy 		return -EFAULT;
1443f92363d1SSreekanth Reddy 	}
1444f92363d1SSreekanth Reddy 
1445f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1446f92363d1SSreekanth Reddy 	    __func__));
1447f92363d1SSreekanth Reddy 
1448f92363d1SSreekanth Reddy 	rc = _ctl_btdh_search_sas_device(ioc, &karg);
1449f92363d1SSreekanth Reddy 	if (!rc)
145045aa6a1aSSuganath Prabu Subramani 		rc = _ctl_btdh_search_pcie_device(ioc, &karg);
145145aa6a1aSSuganath Prabu Subramani 	if (!rc)
1452f92363d1SSreekanth Reddy 		_ctl_btdh_search_raid_device(ioc, &karg);
1453f92363d1SSreekanth Reddy 
1454f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1455f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1456f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1457f92363d1SSreekanth Reddy 		return -EFAULT;
1458f92363d1SSreekanth Reddy 	}
1459f92363d1SSreekanth Reddy 	return 0;
1460f92363d1SSreekanth Reddy }
1461f92363d1SSreekanth Reddy 
1462f92363d1SSreekanth Reddy /**
1463f92363d1SSreekanth Reddy  * _ctl_diag_capability - return diag buffer capability
1464f92363d1SSreekanth Reddy  * @ioc: per adapter object
1465f92363d1SSreekanth Reddy  * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1466f92363d1SSreekanth Reddy  *
1467f92363d1SSreekanth Reddy  * returns 1 when diag buffer support is enabled in firmware
1468f92363d1SSreekanth Reddy  */
1469f92363d1SSreekanth Reddy static u8
1470f92363d1SSreekanth Reddy _ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1471f92363d1SSreekanth Reddy {
1472f92363d1SSreekanth Reddy 	u8 rc = 0;
1473f92363d1SSreekanth Reddy 
1474f92363d1SSreekanth Reddy 	switch (buffer_type) {
1475f92363d1SSreekanth Reddy 	case MPI2_DIAG_BUF_TYPE_TRACE:
1476f92363d1SSreekanth Reddy 		if (ioc->facts.IOCCapabilities &
1477f92363d1SSreekanth Reddy 		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1478f92363d1SSreekanth Reddy 			rc = 1;
1479f92363d1SSreekanth Reddy 		break;
1480f92363d1SSreekanth Reddy 	case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1481f92363d1SSreekanth Reddy 		if (ioc->facts.IOCCapabilities &
1482f92363d1SSreekanth Reddy 		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1483f92363d1SSreekanth Reddy 			rc = 1;
1484f92363d1SSreekanth Reddy 		break;
1485f92363d1SSreekanth Reddy 	case MPI2_DIAG_BUF_TYPE_EXTENDED:
1486f92363d1SSreekanth Reddy 		if (ioc->facts.IOCCapabilities &
1487f92363d1SSreekanth Reddy 		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1488f92363d1SSreekanth Reddy 			rc = 1;
1489f92363d1SSreekanth Reddy 	}
1490f92363d1SSreekanth Reddy 
1491f92363d1SSreekanth Reddy 	return rc;
1492f92363d1SSreekanth Reddy }
1493f92363d1SSreekanth Reddy 
1494f92363d1SSreekanth Reddy 
1495f92363d1SSreekanth Reddy /**
1496f92363d1SSreekanth Reddy  * _ctl_diag_register_2 - wrapper for registering diag buffer support
1497f92363d1SSreekanth Reddy  * @ioc: per adapter object
1498f92363d1SSreekanth Reddy  * @diag_register: the diag_register struct passed in from user space
1499f92363d1SSreekanth Reddy  *
1500f92363d1SSreekanth Reddy  */
1501f92363d1SSreekanth Reddy static long
1502f92363d1SSreekanth Reddy _ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1503f92363d1SSreekanth Reddy 	struct mpt3_diag_register *diag_register)
1504f92363d1SSreekanth Reddy {
1505f92363d1SSreekanth Reddy 	int rc, i;
1506f92363d1SSreekanth Reddy 	void *request_data = NULL;
1507f92363d1SSreekanth Reddy 	dma_addr_t request_data_dma;
1508f92363d1SSreekanth Reddy 	u32 request_data_sz = 0;
1509f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostRequest_t *mpi_request;
1510f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostReply_t *mpi_reply;
1511f92363d1SSreekanth Reddy 	u8 buffer_type;
1512f92363d1SSreekanth Reddy 	u16 smid;
1513f92363d1SSreekanth Reddy 	u16 ioc_status;
1514f92363d1SSreekanth Reddy 	u32 ioc_state;
1515f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
1516f92363d1SSreekanth Reddy 
1517f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1518f92363d1SSreekanth Reddy 	    __func__));
1519f92363d1SSreekanth Reddy 
1520f92363d1SSreekanth Reddy 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1521f92363d1SSreekanth Reddy 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1522f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1523f92363d1SSreekanth Reddy 		    "%s: failed due to ioc not operational\n",
1524f92363d1SSreekanth Reddy 		    ioc->name, __func__);
1525f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1526f92363d1SSreekanth Reddy 		goto out;
1527f92363d1SSreekanth Reddy 	}
1528f92363d1SSreekanth Reddy 
1529f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1530f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1531f92363d1SSreekanth Reddy 		    ioc->name, __func__);
1532f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1533f92363d1SSreekanth Reddy 		goto out;
1534f92363d1SSreekanth Reddy 	}
1535f92363d1SSreekanth Reddy 
1536f92363d1SSreekanth Reddy 	buffer_type = diag_register->buffer_type;
1537f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1538f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1539f92363d1SSreekanth Reddy 			"%s: doesn't have capability for buffer_type(0x%02x)\n",
1540f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
1541f92363d1SSreekanth Reddy 		return -EPERM;
1542f92363d1SSreekanth Reddy 	}
1543f92363d1SSreekanth Reddy 
1544f92363d1SSreekanth Reddy 	if (ioc->diag_buffer_status[buffer_type] &
1545f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) {
1546f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1547f92363d1SSreekanth Reddy 			"%s: already has a registered buffer for buffer_type(0x%02x)\n",
1548f92363d1SSreekanth Reddy 			ioc->name, __func__,
1549f92363d1SSreekanth Reddy 		    buffer_type);
1550f92363d1SSreekanth Reddy 		return -EINVAL;
1551f92363d1SSreekanth Reddy 	}
1552f92363d1SSreekanth Reddy 
1553f92363d1SSreekanth Reddy 	if (diag_register->requested_buffer_size % 4)  {
1554f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1555f92363d1SSreekanth Reddy 			"%s: the requested_buffer_size is not 4 byte aligned\n",
1556f92363d1SSreekanth Reddy 			ioc->name, __func__);
1557f92363d1SSreekanth Reddy 		return -EINVAL;
1558f92363d1SSreekanth Reddy 	}
1559f92363d1SSreekanth Reddy 
1560f92363d1SSreekanth Reddy 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1561f92363d1SSreekanth Reddy 	if (!smid) {
1562f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1563f92363d1SSreekanth Reddy 		    ioc->name, __func__);
1564f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1565f92363d1SSreekanth Reddy 		goto out;
1566f92363d1SSreekanth Reddy 	}
1567f92363d1SSreekanth Reddy 
1568f92363d1SSreekanth Reddy 	rc = 0;
1569f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1570f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1571f92363d1SSreekanth Reddy 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1572f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
1573f92363d1SSreekanth Reddy 
1574f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
1575f92363d1SSreekanth Reddy 	request_data_sz = diag_register->requested_buffer_size;
1576f92363d1SSreekanth Reddy 	ioc->unique_id[buffer_type] = diag_register->unique_id;
1577f92363d1SSreekanth Reddy 	ioc->diag_buffer_status[buffer_type] = 0;
1578f92363d1SSreekanth Reddy 	memcpy(ioc->product_specific[buffer_type],
1579f92363d1SSreekanth Reddy 	    diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1580f92363d1SSreekanth Reddy 	ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1581f92363d1SSreekanth Reddy 
1582f92363d1SSreekanth Reddy 	if (request_data) {
1583f92363d1SSreekanth Reddy 		request_data_dma = ioc->diag_buffer_dma[buffer_type];
1584f92363d1SSreekanth Reddy 		if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1585f92363d1SSreekanth Reddy 			pci_free_consistent(ioc->pdev,
1586f92363d1SSreekanth Reddy 			    ioc->diag_buffer_sz[buffer_type],
1587f92363d1SSreekanth Reddy 			    request_data, request_data_dma);
1588f92363d1SSreekanth Reddy 			request_data = NULL;
1589f92363d1SSreekanth Reddy 		}
1590f92363d1SSreekanth Reddy 	}
1591f92363d1SSreekanth Reddy 
1592f92363d1SSreekanth Reddy 	if (request_data == NULL) {
1593f92363d1SSreekanth Reddy 		ioc->diag_buffer_sz[buffer_type] = 0;
1594f92363d1SSreekanth Reddy 		ioc->diag_buffer_dma[buffer_type] = 0;
1595f92363d1SSreekanth Reddy 		request_data = pci_alloc_consistent(
1596f92363d1SSreekanth Reddy 			ioc->pdev, request_data_sz, &request_data_dma);
1597f92363d1SSreekanth Reddy 		if (request_data == NULL) {
1598f92363d1SSreekanth Reddy 			pr_err(MPT3SAS_FMT "%s: failed allocating memory" \
1599f92363d1SSreekanth Reddy 			    " for diag buffers, requested size(%d)\n",
1600f92363d1SSreekanth Reddy 			    ioc->name, __func__, request_data_sz);
1601f92363d1SSreekanth Reddy 			mpt3sas_base_free_smid(ioc, smid);
1602f92363d1SSreekanth Reddy 			return -ENOMEM;
1603f92363d1SSreekanth Reddy 		}
1604f92363d1SSreekanth Reddy 		ioc->diag_buffer[buffer_type] = request_data;
1605f92363d1SSreekanth Reddy 		ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1606f92363d1SSreekanth Reddy 		ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1607f92363d1SSreekanth Reddy 	}
1608f92363d1SSreekanth Reddy 
1609f92363d1SSreekanth Reddy 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1610f92363d1SSreekanth Reddy 	mpi_request->BufferType = diag_register->buffer_type;
1611f92363d1SSreekanth Reddy 	mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1612f92363d1SSreekanth Reddy 	mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1613f92363d1SSreekanth Reddy 	mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1614f92363d1SSreekanth Reddy 	mpi_request->VF_ID = 0; /* TODO */
1615f92363d1SSreekanth Reddy 	mpi_request->VP_ID = 0;
1616f92363d1SSreekanth Reddy 
1617f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT
1618f92363d1SSreekanth Reddy 		"%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1619f92363d1SSreekanth Reddy 		ioc->name, __func__, request_data,
1620f92363d1SSreekanth Reddy 	    (unsigned long long)request_data_dma,
1621f92363d1SSreekanth Reddy 	    le32_to_cpu(mpi_request->BufferLength)));
1622f92363d1SSreekanth Reddy 
1623f92363d1SSreekanth Reddy 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1624f92363d1SSreekanth Reddy 		mpi_request->ProductSpecific[i] =
1625f92363d1SSreekanth Reddy 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
1626f92363d1SSreekanth Reddy 
1627f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
162840114bdeSSuganath Prabu S 	mpt3sas_base_put_smid_default(ioc, smid);
16298bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
1630f92363d1SSreekanth Reddy 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1631f92363d1SSreekanth Reddy 
1632f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1633d37306caSChaitra P B 		issue_reset =
1634d37306caSChaitra P B 			mpt3sas_base_check_cmd_timeout(ioc,
1635d37306caSChaitra P B 				ioc->ctl_cmds.status, mpi_request,
1636f92363d1SSreekanth Reddy 				sizeof(Mpi2DiagBufferPostRequest_t)/4);
1637f92363d1SSreekanth Reddy 		goto issue_host_reset;
1638f92363d1SSreekanth Reddy 	}
1639f92363d1SSreekanth Reddy 
1640f92363d1SSreekanth Reddy 	/* process the completed Reply Message Frame */
1641f92363d1SSreekanth Reddy 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1642f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: no reply message\n",
1643f92363d1SSreekanth Reddy 		    ioc->name, __func__);
1644f92363d1SSreekanth Reddy 		rc = -EFAULT;
1645f92363d1SSreekanth Reddy 		goto out;
1646f92363d1SSreekanth Reddy 	}
1647f92363d1SSreekanth Reddy 
1648f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
1649f92363d1SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1650f92363d1SSreekanth Reddy 
1651f92363d1SSreekanth Reddy 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1652f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
1653f92363d1SSreekanth Reddy 			MPT3_DIAG_BUFFER_IS_REGISTERED;
1654f92363d1SSreekanth Reddy 		dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
1655f92363d1SSreekanth Reddy 		    ioc->name, __func__));
1656f92363d1SSreekanth Reddy 	} else {
1657f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT
1658f92363d1SSreekanth Reddy 			"%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1659f92363d1SSreekanth Reddy 			ioc->name, __func__,
1660f92363d1SSreekanth Reddy 		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1661f92363d1SSreekanth Reddy 		rc = -EFAULT;
1662f92363d1SSreekanth Reddy 	}
1663f92363d1SSreekanth Reddy 
1664f92363d1SSreekanth Reddy  issue_host_reset:
1665f92363d1SSreekanth Reddy 	if (issue_reset)
166698c56ad3SCalvin Owens 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1667f92363d1SSreekanth Reddy 
1668f92363d1SSreekanth Reddy  out:
1669f92363d1SSreekanth Reddy 
1670f92363d1SSreekanth Reddy 	if (rc && request_data)
1671f92363d1SSreekanth Reddy 		pci_free_consistent(ioc->pdev, request_data_sz,
1672f92363d1SSreekanth Reddy 		    request_data, request_data_dma);
1673f92363d1SSreekanth Reddy 
1674f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1675f92363d1SSreekanth Reddy 	return rc;
1676f92363d1SSreekanth Reddy }
1677f92363d1SSreekanth Reddy 
1678f92363d1SSreekanth Reddy /**
1679f92363d1SSreekanth Reddy  * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1680f92363d1SSreekanth Reddy  * @ioc: per adapter object
1681f92363d1SSreekanth Reddy  * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1682f92363d1SSreekanth Reddy  *
1683f92363d1SSreekanth Reddy  * This is called when command line option diag_buffer_enable is enabled
1684f92363d1SSreekanth Reddy  * at driver load time.
1685f92363d1SSreekanth Reddy  */
1686f92363d1SSreekanth Reddy void
1687f92363d1SSreekanth Reddy mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1688f92363d1SSreekanth Reddy {
1689f92363d1SSreekanth Reddy 	struct mpt3_diag_register diag_register;
1690f92363d1SSreekanth Reddy 
1691f92363d1SSreekanth Reddy 	memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1692f92363d1SSreekanth Reddy 
1693f92363d1SSreekanth Reddy 	if (bits_to_register & 1) {
1694f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT "registering trace buffer support\n",
1695f92363d1SSreekanth Reddy 		    ioc->name);
1696f92363d1SSreekanth Reddy 		ioc->diag_trigger_master.MasterData =
1697f92363d1SSreekanth Reddy 		    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1698f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1699f92363d1SSreekanth Reddy 		/* register for 2MB buffers  */
1700f92363d1SSreekanth Reddy 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1701f92363d1SSreekanth Reddy 		diag_register.unique_id = 0x7075900;
1702f92363d1SSreekanth Reddy 		_ctl_diag_register_2(ioc,  &diag_register);
1703f92363d1SSreekanth Reddy 	}
1704f92363d1SSreekanth Reddy 
1705f92363d1SSreekanth Reddy 	if (bits_to_register & 2) {
1706f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT "registering snapshot buffer support\n",
1707f92363d1SSreekanth Reddy 		    ioc->name);
1708f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1709f92363d1SSreekanth Reddy 		/* register for 2MB buffers  */
1710f92363d1SSreekanth Reddy 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1711f92363d1SSreekanth Reddy 		diag_register.unique_id = 0x7075901;
1712f92363d1SSreekanth Reddy 		_ctl_diag_register_2(ioc,  &diag_register);
1713f92363d1SSreekanth Reddy 	}
1714f92363d1SSreekanth Reddy 
1715f92363d1SSreekanth Reddy 	if (bits_to_register & 4) {
1716f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT "registering extended buffer support\n",
1717f92363d1SSreekanth Reddy 		    ioc->name);
1718f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1719f92363d1SSreekanth Reddy 		/* register for 2MB buffers  */
1720f92363d1SSreekanth Reddy 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1721f92363d1SSreekanth Reddy 		diag_register.unique_id = 0x7075901;
1722f92363d1SSreekanth Reddy 		_ctl_diag_register_2(ioc,  &diag_register);
1723f92363d1SSreekanth Reddy 	}
1724f92363d1SSreekanth Reddy }
1725f92363d1SSreekanth Reddy 
1726f92363d1SSreekanth Reddy /**
1727f92363d1SSreekanth Reddy  * _ctl_diag_register - application register with driver
1728f92363d1SSreekanth Reddy  * @ioc: per adapter object
1729f92363d1SSreekanth Reddy  * @arg - user space buffer containing ioctl content
1730f92363d1SSreekanth Reddy  *
1731f92363d1SSreekanth Reddy  * This will allow the driver to setup any required buffers that will be
1732f92363d1SSreekanth Reddy  * needed by firmware to communicate with the driver.
1733f92363d1SSreekanth Reddy  */
1734f92363d1SSreekanth Reddy static long
1735f92363d1SSreekanth Reddy _ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1736f92363d1SSreekanth Reddy {
1737f92363d1SSreekanth Reddy 	struct mpt3_diag_register karg;
1738f92363d1SSreekanth Reddy 	long rc;
1739f92363d1SSreekanth Reddy 
1740f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1741f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1742f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1743f92363d1SSreekanth Reddy 		return -EFAULT;
1744f92363d1SSreekanth Reddy 	}
1745f92363d1SSreekanth Reddy 
1746f92363d1SSreekanth Reddy 	rc = _ctl_diag_register_2(ioc, &karg);
1747f92363d1SSreekanth Reddy 	return rc;
1748f92363d1SSreekanth Reddy }
1749f92363d1SSreekanth Reddy 
1750f92363d1SSreekanth Reddy /**
1751f92363d1SSreekanth Reddy  * _ctl_diag_unregister - application unregister with driver
1752f92363d1SSreekanth Reddy  * @ioc: per adapter object
1753f92363d1SSreekanth Reddy  * @arg - user space buffer containing ioctl content
1754f92363d1SSreekanth Reddy  *
1755f92363d1SSreekanth Reddy  * This will allow the driver to cleanup any memory allocated for diag
1756f92363d1SSreekanth Reddy  * messages and to free up any resources.
1757f92363d1SSreekanth Reddy  */
1758f92363d1SSreekanth Reddy static long
1759f92363d1SSreekanth Reddy _ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1760f92363d1SSreekanth Reddy {
1761f92363d1SSreekanth Reddy 	struct mpt3_diag_unregister karg;
1762f92363d1SSreekanth Reddy 	void *request_data;
1763f92363d1SSreekanth Reddy 	dma_addr_t request_data_dma;
1764f92363d1SSreekanth Reddy 	u32 request_data_sz;
1765f92363d1SSreekanth Reddy 	u8 buffer_type;
1766f92363d1SSreekanth Reddy 
1767f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1768f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1769f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1770f92363d1SSreekanth Reddy 		return -EFAULT;
1771f92363d1SSreekanth Reddy 	}
1772f92363d1SSreekanth Reddy 
1773f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1774f92363d1SSreekanth Reddy 	    __func__));
1775f92363d1SSreekanth Reddy 
1776f92363d1SSreekanth Reddy 	buffer_type = karg.unique_id & 0x000000ff;
1777f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1778f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1779f92363d1SSreekanth Reddy 			"%s: doesn't have capability for buffer_type(0x%02x)\n",
1780f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
1781f92363d1SSreekanth Reddy 		return -EPERM;
1782f92363d1SSreekanth Reddy 	}
1783f92363d1SSreekanth Reddy 
1784f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
1785f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1786f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1787f92363d1SSreekanth Reddy 			"%s: buffer_type(0x%02x) is not registered\n",
1788f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
1789f92363d1SSreekanth Reddy 		return -EINVAL;
1790f92363d1SSreekanth Reddy 	}
1791f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
1792f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
1793f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1794f92363d1SSreekanth Reddy 			"%s: buffer_type(0x%02x) has not been released\n",
1795f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
1796f92363d1SSreekanth Reddy 		return -EINVAL;
1797f92363d1SSreekanth Reddy 	}
1798f92363d1SSreekanth Reddy 
1799f92363d1SSreekanth Reddy 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1800f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1801f92363d1SSreekanth Reddy 			"%s: unique_id(0x%08x) is not registered\n",
1802f92363d1SSreekanth Reddy 			ioc->name, __func__, karg.unique_id);
1803f92363d1SSreekanth Reddy 		return -EINVAL;
1804f92363d1SSreekanth Reddy 	}
1805f92363d1SSreekanth Reddy 
1806f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
1807f92363d1SSreekanth Reddy 	if (!request_data) {
1808f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1809f92363d1SSreekanth Reddy 			"%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1810f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
1811f92363d1SSreekanth Reddy 		return -ENOMEM;
1812f92363d1SSreekanth Reddy 	}
1813f92363d1SSreekanth Reddy 
1814f92363d1SSreekanth Reddy 	request_data_sz = ioc->diag_buffer_sz[buffer_type];
1815f92363d1SSreekanth Reddy 	request_data_dma = ioc->diag_buffer_dma[buffer_type];
1816f92363d1SSreekanth Reddy 	pci_free_consistent(ioc->pdev, request_data_sz,
1817f92363d1SSreekanth Reddy 	    request_data, request_data_dma);
1818f92363d1SSreekanth Reddy 	ioc->diag_buffer[buffer_type] = NULL;
1819f92363d1SSreekanth Reddy 	ioc->diag_buffer_status[buffer_type] = 0;
1820f92363d1SSreekanth Reddy 	return 0;
1821f92363d1SSreekanth Reddy }
1822f92363d1SSreekanth Reddy 
1823f92363d1SSreekanth Reddy /**
1824f92363d1SSreekanth Reddy  * _ctl_diag_query - query relevant info associated with diag buffers
1825f92363d1SSreekanth Reddy  * @ioc: per adapter object
1826f92363d1SSreekanth Reddy  * @arg - user space buffer containing ioctl content
1827f92363d1SSreekanth Reddy  *
1828f92363d1SSreekanth Reddy  * The application will send only buffer_type and unique_id.  Driver will
1829f92363d1SSreekanth Reddy  * inspect unique_id first, if valid, fill in all the info.  If unique_id is
1830f92363d1SSreekanth Reddy  * 0x00, the driver will return info specified by Buffer Type.
1831f92363d1SSreekanth Reddy  */
1832f92363d1SSreekanth Reddy static long
1833f92363d1SSreekanth Reddy _ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1834f92363d1SSreekanth Reddy {
1835f92363d1SSreekanth Reddy 	struct mpt3_diag_query karg;
1836f92363d1SSreekanth Reddy 	void *request_data;
1837f92363d1SSreekanth Reddy 	int i;
1838f92363d1SSreekanth Reddy 	u8 buffer_type;
1839f92363d1SSreekanth Reddy 
1840f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1841f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
1842f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
1843f92363d1SSreekanth Reddy 		return -EFAULT;
1844f92363d1SSreekanth Reddy 	}
1845f92363d1SSreekanth Reddy 
1846f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1847f92363d1SSreekanth Reddy 	    __func__));
1848f92363d1SSreekanth Reddy 
1849f92363d1SSreekanth Reddy 	karg.application_flags = 0;
1850f92363d1SSreekanth Reddy 	buffer_type = karg.buffer_type;
1851f92363d1SSreekanth Reddy 
1852f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1853f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1854f92363d1SSreekanth Reddy 			"%s: doesn't have capability for buffer_type(0x%02x)\n",
1855f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
1856f92363d1SSreekanth Reddy 		return -EPERM;
1857f92363d1SSreekanth Reddy 	}
1858f92363d1SSreekanth Reddy 
1859f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
1860f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1861f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1862f92363d1SSreekanth Reddy 			"%s: buffer_type(0x%02x) is not registered\n",
1863f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
1864f92363d1SSreekanth Reddy 		return -EINVAL;
1865f92363d1SSreekanth Reddy 	}
1866f92363d1SSreekanth Reddy 
1867f92363d1SSreekanth Reddy 	if (karg.unique_id & 0xffffff00) {
1868f92363d1SSreekanth Reddy 		if (karg.unique_id != ioc->unique_id[buffer_type]) {
1869f92363d1SSreekanth Reddy 			pr_err(MPT3SAS_FMT
1870f92363d1SSreekanth Reddy 				"%s: unique_id(0x%08x) is not registered\n",
1871f92363d1SSreekanth Reddy 				ioc->name, __func__, karg.unique_id);
1872f92363d1SSreekanth Reddy 			return -EINVAL;
1873f92363d1SSreekanth Reddy 		}
1874f92363d1SSreekanth Reddy 	}
1875f92363d1SSreekanth Reddy 
1876f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
1877f92363d1SSreekanth Reddy 	if (!request_data) {
1878f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1879f92363d1SSreekanth Reddy 			"%s: doesn't have buffer for buffer_type(0x%02x)\n",
1880f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
1881f92363d1SSreekanth Reddy 		return -ENOMEM;
1882f92363d1SSreekanth Reddy 	}
1883f92363d1SSreekanth Reddy 
1884f92363d1SSreekanth Reddy 	if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
1885f92363d1SSreekanth Reddy 		karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1886f92363d1SSreekanth Reddy 		    MPT3_APP_FLAGS_BUFFER_VALID);
1887f92363d1SSreekanth Reddy 	else
1888f92363d1SSreekanth Reddy 		karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1889f92363d1SSreekanth Reddy 		    MPT3_APP_FLAGS_BUFFER_VALID |
1890f92363d1SSreekanth Reddy 		    MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
1891f92363d1SSreekanth Reddy 
1892f92363d1SSreekanth Reddy 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1893f92363d1SSreekanth Reddy 		karg.product_specific[i] =
1894f92363d1SSreekanth Reddy 		    ioc->product_specific[buffer_type][i];
1895f92363d1SSreekanth Reddy 
1896f92363d1SSreekanth Reddy 	karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1897f92363d1SSreekanth Reddy 	karg.driver_added_buffer_size = 0;
1898f92363d1SSreekanth Reddy 	karg.unique_id = ioc->unique_id[buffer_type];
1899f92363d1SSreekanth Reddy 	karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1900f92363d1SSreekanth Reddy 
1901f92363d1SSreekanth Reddy 	if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
1902f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
1903f92363d1SSreekanth Reddy 			"%s: unable to write mpt3_diag_query data @ %p\n",
1904f92363d1SSreekanth Reddy 			ioc->name, __func__, arg);
1905f92363d1SSreekanth Reddy 		return -EFAULT;
1906f92363d1SSreekanth Reddy 	}
1907f92363d1SSreekanth Reddy 	return 0;
1908f92363d1SSreekanth Reddy }
1909f92363d1SSreekanth Reddy 
1910f92363d1SSreekanth Reddy /**
1911f92363d1SSreekanth Reddy  * mpt3sas_send_diag_release - Diag Release Message
1912f92363d1SSreekanth Reddy  * @ioc: per adapter object
1913f92363d1SSreekanth Reddy  * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1914f92363d1SSreekanth Reddy  * @issue_reset - specifies whether host reset is required.
1915f92363d1SSreekanth Reddy  *
1916f92363d1SSreekanth Reddy  */
1917f92363d1SSreekanth Reddy int
1918f92363d1SSreekanth Reddy mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
1919f92363d1SSreekanth Reddy 	u8 *issue_reset)
1920f92363d1SSreekanth Reddy {
1921f92363d1SSreekanth Reddy 	Mpi2DiagReleaseRequest_t *mpi_request;
1922f92363d1SSreekanth Reddy 	Mpi2DiagReleaseReply_t *mpi_reply;
1923f92363d1SSreekanth Reddy 	u16 smid;
1924f92363d1SSreekanth Reddy 	u16 ioc_status;
1925f92363d1SSreekanth Reddy 	u32 ioc_state;
1926f92363d1SSreekanth Reddy 	int rc;
1927f92363d1SSreekanth Reddy 
1928f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1929f92363d1SSreekanth Reddy 	    __func__));
1930f92363d1SSreekanth Reddy 
1931f92363d1SSreekanth Reddy 	rc = 0;
1932f92363d1SSreekanth Reddy 	*issue_reset = 0;
1933f92363d1SSreekanth Reddy 
1934f92363d1SSreekanth Reddy 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1935f92363d1SSreekanth Reddy 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1936f92363d1SSreekanth Reddy 		if (ioc->diag_buffer_status[buffer_type] &
1937f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED)
1938f92363d1SSreekanth Reddy 			ioc->diag_buffer_status[buffer_type] |=
1939f92363d1SSreekanth Reddy 			    MPT3_DIAG_BUFFER_IS_RELEASED;
1940f92363d1SSreekanth Reddy 		dctlprintk(ioc, pr_info(MPT3SAS_FMT
1941f92363d1SSreekanth Reddy 			"%s: skipping due to FAULT state\n", ioc->name,
1942f92363d1SSreekanth Reddy 		    __func__));
1943f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1944f92363d1SSreekanth Reddy 		goto out;
1945f92363d1SSreekanth Reddy 	}
1946f92363d1SSreekanth Reddy 
1947f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1948f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1949f92363d1SSreekanth Reddy 		    ioc->name, __func__);
1950f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1951f92363d1SSreekanth Reddy 		goto out;
1952f92363d1SSreekanth Reddy 	}
1953f92363d1SSreekanth Reddy 
1954f92363d1SSreekanth Reddy 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1955f92363d1SSreekanth Reddy 	if (!smid) {
1956f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1957f92363d1SSreekanth Reddy 		    ioc->name, __func__);
1958f92363d1SSreekanth Reddy 		rc = -EAGAIN;
1959f92363d1SSreekanth Reddy 		goto out;
1960f92363d1SSreekanth Reddy 	}
1961f92363d1SSreekanth Reddy 
1962f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1963f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1964f92363d1SSreekanth Reddy 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1965f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
1966f92363d1SSreekanth Reddy 
1967f92363d1SSreekanth Reddy 	mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1968f92363d1SSreekanth Reddy 	mpi_request->BufferType = buffer_type;
1969f92363d1SSreekanth Reddy 	mpi_request->VF_ID = 0; /* TODO */
1970f92363d1SSreekanth Reddy 	mpi_request->VP_ID = 0;
1971f92363d1SSreekanth Reddy 
1972f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
197340114bdeSSuganath Prabu S 	mpt3sas_base_put_smid_default(ioc, smid);
19748bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
1975f92363d1SSreekanth Reddy 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1976f92363d1SSreekanth Reddy 
1977f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1978d37306caSChaitra P B 		*issue_reset = mpt3sas_base_check_cmd_timeout(ioc,
1979d37306caSChaitra P B 				ioc->ctl_cmds.status, mpi_request,
1980f92363d1SSreekanth Reddy 				sizeof(Mpi2DiagReleaseRequest_t)/4);
1981f92363d1SSreekanth Reddy 		rc = -EFAULT;
1982f92363d1SSreekanth Reddy 		goto out;
1983f92363d1SSreekanth Reddy 	}
1984f92363d1SSreekanth Reddy 
1985f92363d1SSreekanth Reddy 	/* process the completed Reply Message Frame */
1986f92363d1SSreekanth Reddy 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1987f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: no reply message\n",
1988f92363d1SSreekanth Reddy 		    ioc->name, __func__);
1989f92363d1SSreekanth Reddy 		rc = -EFAULT;
1990f92363d1SSreekanth Reddy 		goto out;
1991f92363d1SSreekanth Reddy 	}
1992f92363d1SSreekanth Reddy 
1993f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
1994f92363d1SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1995f92363d1SSreekanth Reddy 
1996f92363d1SSreekanth Reddy 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1997f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
1998f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED;
1999f92363d1SSreekanth Reddy 		dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
2000f92363d1SSreekanth Reddy 		    ioc->name, __func__));
2001f92363d1SSreekanth Reddy 	} else {
2002f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT
2003f92363d1SSreekanth Reddy 			"%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2004f92363d1SSreekanth Reddy 			ioc->name, __func__,
2005f92363d1SSreekanth Reddy 		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2006f92363d1SSreekanth Reddy 		rc = -EFAULT;
2007f92363d1SSreekanth Reddy 	}
2008f92363d1SSreekanth Reddy 
2009f92363d1SSreekanth Reddy  out:
2010f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2011f92363d1SSreekanth Reddy 	return rc;
2012f92363d1SSreekanth Reddy }
2013f92363d1SSreekanth Reddy 
2014f92363d1SSreekanth Reddy /**
2015f92363d1SSreekanth Reddy  * _ctl_diag_release - request to send Diag Release Message to firmware
2016f92363d1SSreekanth Reddy  * @arg - user space buffer containing ioctl content
2017f92363d1SSreekanth Reddy  *
2018f92363d1SSreekanth Reddy  * This allows ownership of the specified buffer to returned to the driver,
2019f92363d1SSreekanth Reddy  * allowing an application to read the buffer without fear that firmware is
20209a284e5cSMasahiro Yamada  * overwriting information in the buffer.
2021f92363d1SSreekanth Reddy  */
2022f92363d1SSreekanth Reddy static long
2023f92363d1SSreekanth Reddy _ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2024f92363d1SSreekanth Reddy {
2025f92363d1SSreekanth Reddy 	struct mpt3_diag_release karg;
2026f92363d1SSreekanth Reddy 	void *request_data;
2027f92363d1SSreekanth Reddy 	int rc;
2028f92363d1SSreekanth Reddy 	u8 buffer_type;
2029f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
2030f92363d1SSreekanth Reddy 
2031f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2032f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2033f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2034f92363d1SSreekanth Reddy 		return -EFAULT;
2035f92363d1SSreekanth Reddy 	}
2036f92363d1SSreekanth Reddy 
2037f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2038f92363d1SSreekanth Reddy 	    __func__));
2039f92363d1SSreekanth Reddy 
2040f92363d1SSreekanth Reddy 	buffer_type = karg.unique_id & 0x000000ff;
2041f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2042f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
2043f92363d1SSreekanth Reddy 			"%s: doesn't have capability for buffer_type(0x%02x)\n",
2044f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
2045f92363d1SSreekanth Reddy 		return -EPERM;
2046f92363d1SSreekanth Reddy 	}
2047f92363d1SSreekanth Reddy 
2048f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2049f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2050f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
2051f92363d1SSreekanth Reddy 			"%s: buffer_type(0x%02x) is not registered\n",
2052f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
2053f92363d1SSreekanth Reddy 		return -EINVAL;
2054f92363d1SSreekanth Reddy 	}
2055f92363d1SSreekanth Reddy 
2056f92363d1SSreekanth Reddy 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2057f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
2058f92363d1SSreekanth Reddy 			"%s: unique_id(0x%08x) is not registered\n",
2059f92363d1SSreekanth Reddy 			ioc->name, __func__, karg.unique_id);
2060f92363d1SSreekanth Reddy 		return -EINVAL;
2061f92363d1SSreekanth Reddy 	}
2062f92363d1SSreekanth Reddy 
2063f92363d1SSreekanth Reddy 	if (ioc->diag_buffer_status[buffer_type] &
2064f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
2065f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
2066f92363d1SSreekanth Reddy 			"%s: buffer_type(0x%02x) is already released\n",
2067f92363d1SSreekanth Reddy 			ioc->name, __func__,
2068f92363d1SSreekanth Reddy 		    buffer_type);
2069f92363d1SSreekanth Reddy 		return 0;
2070f92363d1SSreekanth Reddy 	}
2071f92363d1SSreekanth Reddy 
2072f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
2073f92363d1SSreekanth Reddy 
2074f92363d1SSreekanth Reddy 	if (!request_data) {
2075f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
2076f92363d1SSreekanth Reddy 			"%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2077f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
2078f92363d1SSreekanth Reddy 		return -ENOMEM;
2079f92363d1SSreekanth Reddy 	}
2080f92363d1SSreekanth Reddy 
2081f92363d1SSreekanth Reddy 	/* buffers were released by due to host reset */
2082f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2083f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2084f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
2085f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED;
2086f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] &=
2087f92363d1SSreekanth Reddy 		    ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
2088f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
2089f92363d1SSreekanth Reddy 			"%s: buffer_type(0x%02x) was released due to host reset\n",
2090f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
2091f92363d1SSreekanth Reddy 		return 0;
2092f92363d1SSreekanth Reddy 	}
2093f92363d1SSreekanth Reddy 
2094f92363d1SSreekanth Reddy 	rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2095f92363d1SSreekanth Reddy 
2096f92363d1SSreekanth Reddy 	if (issue_reset)
209798c56ad3SCalvin Owens 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2098f92363d1SSreekanth Reddy 
2099f92363d1SSreekanth Reddy 	return rc;
2100f92363d1SSreekanth Reddy }
2101f92363d1SSreekanth Reddy 
2102f92363d1SSreekanth Reddy /**
2103f92363d1SSreekanth Reddy  * _ctl_diag_read_buffer - request for copy of the diag buffer
2104f92363d1SSreekanth Reddy  * @ioc: per adapter object
2105f92363d1SSreekanth Reddy  * @arg - user space buffer containing ioctl content
2106f92363d1SSreekanth Reddy  */
2107f92363d1SSreekanth Reddy static long
2108f92363d1SSreekanth Reddy _ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2109f92363d1SSreekanth Reddy {
2110f92363d1SSreekanth Reddy 	struct mpt3_diag_read_buffer karg;
2111f92363d1SSreekanth Reddy 	struct mpt3_diag_read_buffer __user *uarg = arg;
2112f92363d1SSreekanth Reddy 	void *request_data, *diag_data;
2113f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostRequest_t *mpi_request;
2114f92363d1SSreekanth Reddy 	Mpi2DiagBufferPostReply_t *mpi_reply;
2115f92363d1SSreekanth Reddy 	int rc, i;
2116f92363d1SSreekanth Reddy 	u8 buffer_type;
21178bbb1cf6SCalvin Owens 	unsigned long request_size, copy_size;
2118f92363d1SSreekanth Reddy 	u16 smid;
2119f92363d1SSreekanth Reddy 	u16 ioc_status;
2120f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
2121f92363d1SSreekanth Reddy 
2122f92363d1SSreekanth Reddy 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2123f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2124f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2125f92363d1SSreekanth Reddy 		return -EFAULT;
2126f92363d1SSreekanth Reddy 	}
2127f92363d1SSreekanth Reddy 
2128f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2129f92363d1SSreekanth Reddy 	    __func__));
2130f92363d1SSreekanth Reddy 
2131f92363d1SSreekanth Reddy 	buffer_type = karg.unique_id & 0x000000ff;
2132f92363d1SSreekanth Reddy 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2133f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
2134f92363d1SSreekanth Reddy 			"%s: doesn't have capability for buffer_type(0x%02x)\n",
2135f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
2136f92363d1SSreekanth Reddy 		return -EPERM;
2137f92363d1SSreekanth Reddy 	}
2138f92363d1SSreekanth Reddy 
2139f92363d1SSreekanth Reddy 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2140f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
2141f92363d1SSreekanth Reddy 			"%s: unique_id(0x%08x) is not registered\n",
2142f92363d1SSreekanth Reddy 			ioc->name, __func__, karg.unique_id);
2143f92363d1SSreekanth Reddy 		return -EINVAL;
2144f92363d1SSreekanth Reddy 	}
2145f92363d1SSreekanth Reddy 
2146f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[buffer_type];
2147f92363d1SSreekanth Reddy 	if (!request_data) {
2148f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
2149f92363d1SSreekanth Reddy 			"%s: doesn't have buffer for buffer_type(0x%02x)\n",
2150f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type);
2151f92363d1SSreekanth Reddy 		return -ENOMEM;
2152f92363d1SSreekanth Reddy 	}
2153f92363d1SSreekanth Reddy 
2154f92363d1SSreekanth Reddy 	request_size = ioc->diag_buffer_sz[buffer_type];
2155f92363d1SSreekanth Reddy 
2156f92363d1SSreekanth Reddy 	if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2157f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: either the starting_offset " \
2158f92363d1SSreekanth Reddy 		    "or bytes_to_read are not 4 byte aligned\n", ioc->name,
2159f92363d1SSreekanth Reddy 		    __func__);
2160f92363d1SSreekanth Reddy 		return -EINVAL;
2161f92363d1SSreekanth Reddy 	}
2162f92363d1SSreekanth Reddy 
2163f92363d1SSreekanth Reddy 	if (karg.starting_offset > request_size)
2164f92363d1SSreekanth Reddy 		return -EINVAL;
2165f92363d1SSreekanth Reddy 
2166f92363d1SSreekanth Reddy 	diag_data = (void *)(request_data + karg.starting_offset);
2167f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT
2168f92363d1SSreekanth Reddy 		"%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2169f92363d1SSreekanth Reddy 		ioc->name, __func__,
2170f92363d1SSreekanth Reddy 	    diag_data, karg.starting_offset, karg.bytes_to_read));
2171f92363d1SSreekanth Reddy 
2172f92363d1SSreekanth Reddy 	/* Truncate data on requests that are too large */
2173f92363d1SSreekanth Reddy 	if ((diag_data + karg.bytes_to_read < diag_data) ||
2174f92363d1SSreekanth Reddy 	    (diag_data + karg.bytes_to_read > request_data + request_size))
2175f92363d1SSreekanth Reddy 		copy_size = request_size - karg.starting_offset;
2176f92363d1SSreekanth Reddy 	else
2177f92363d1SSreekanth Reddy 		copy_size = karg.bytes_to_read;
2178f92363d1SSreekanth Reddy 
2179f92363d1SSreekanth Reddy 	if (copy_to_user((void __user *)uarg->diagnostic_data,
2180f92363d1SSreekanth Reddy 	    diag_data, copy_size)) {
2181f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
2182f92363d1SSreekanth Reddy 			"%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2183f92363d1SSreekanth Reddy 			ioc->name, __func__, diag_data);
2184f92363d1SSreekanth Reddy 		return -EFAULT;
2185f92363d1SSreekanth Reddy 	}
2186f92363d1SSreekanth Reddy 
2187f92363d1SSreekanth Reddy 	if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2188f92363d1SSreekanth Reddy 		return 0;
2189f92363d1SSreekanth Reddy 
2190f92363d1SSreekanth Reddy 	dctlprintk(ioc, pr_info(MPT3SAS_FMT
2191f92363d1SSreekanth Reddy 		"%s: Reregister buffer_type(0x%02x)\n",
2192f92363d1SSreekanth Reddy 		ioc->name, __func__, buffer_type));
2193f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[buffer_type] &
2194f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2195f92363d1SSreekanth Reddy 		dctlprintk(ioc, pr_info(MPT3SAS_FMT
2196f92363d1SSreekanth Reddy 			"%s: buffer_type(0x%02x) is still registered\n",
2197f92363d1SSreekanth Reddy 			ioc->name, __func__, buffer_type));
2198f92363d1SSreekanth Reddy 		return 0;
2199f92363d1SSreekanth Reddy 	}
2200f92363d1SSreekanth Reddy 	/* Get a free request frame and save the message context.
2201f92363d1SSreekanth Reddy 	*/
2202f92363d1SSreekanth Reddy 
2203f92363d1SSreekanth Reddy 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2204f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
2205f92363d1SSreekanth Reddy 		    ioc->name, __func__);
2206f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2207f92363d1SSreekanth Reddy 		goto out;
2208f92363d1SSreekanth Reddy 	}
2209f92363d1SSreekanth Reddy 
2210f92363d1SSreekanth Reddy 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2211f92363d1SSreekanth Reddy 	if (!smid) {
2212f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2213f92363d1SSreekanth Reddy 		    ioc->name, __func__);
2214f92363d1SSreekanth Reddy 		rc = -EAGAIN;
2215f92363d1SSreekanth Reddy 		goto out;
2216f92363d1SSreekanth Reddy 	}
2217f92363d1SSreekanth Reddy 
2218f92363d1SSreekanth Reddy 	rc = 0;
2219f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2220f92363d1SSreekanth Reddy 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2221f92363d1SSreekanth Reddy 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2222f92363d1SSreekanth Reddy 	ioc->ctl_cmds.smid = smid;
2223f92363d1SSreekanth Reddy 
2224f92363d1SSreekanth Reddy 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2225f92363d1SSreekanth Reddy 	mpi_request->BufferType = buffer_type;
2226f92363d1SSreekanth Reddy 	mpi_request->BufferLength =
2227f92363d1SSreekanth Reddy 	    cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2228f92363d1SSreekanth Reddy 	mpi_request->BufferAddress =
2229f92363d1SSreekanth Reddy 	    cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2230f92363d1SSreekanth Reddy 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2231f92363d1SSreekanth Reddy 		mpi_request->ProductSpecific[i] =
2232f92363d1SSreekanth Reddy 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
2233f92363d1SSreekanth Reddy 	mpi_request->VF_ID = 0; /* TODO */
2234f92363d1SSreekanth Reddy 	mpi_request->VP_ID = 0;
2235f92363d1SSreekanth Reddy 
2236f92363d1SSreekanth Reddy 	init_completion(&ioc->ctl_cmds.done);
223740114bdeSSuganath Prabu S 	mpt3sas_base_put_smid_default(ioc, smid);
22388bbb1cf6SCalvin Owens 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
2239f92363d1SSreekanth Reddy 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2240f92363d1SSreekanth Reddy 
2241f92363d1SSreekanth Reddy 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2242d37306caSChaitra P B 		issue_reset =
2243d37306caSChaitra P B 			mpt3sas_base_check_cmd_timeout(ioc,
2244d37306caSChaitra P B 				ioc->ctl_cmds.status, mpi_request,
2245f92363d1SSreekanth Reddy 				sizeof(Mpi2DiagBufferPostRequest_t)/4);
2246f92363d1SSreekanth Reddy 		goto issue_host_reset;
2247f92363d1SSreekanth Reddy 	}
2248f92363d1SSreekanth Reddy 
2249f92363d1SSreekanth Reddy 	/* process the completed Reply Message Frame */
2250f92363d1SSreekanth Reddy 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2251f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: no reply message\n",
2252f92363d1SSreekanth Reddy 		    ioc->name, __func__);
2253f92363d1SSreekanth Reddy 		rc = -EFAULT;
2254f92363d1SSreekanth Reddy 		goto out;
2255f92363d1SSreekanth Reddy 	}
2256f92363d1SSreekanth Reddy 
2257f92363d1SSreekanth Reddy 	mpi_reply = ioc->ctl_cmds.reply;
2258f92363d1SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2259f92363d1SSreekanth Reddy 
2260f92363d1SSreekanth Reddy 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2261f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[buffer_type] |=
2262f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED;
2263f92363d1SSreekanth Reddy 		dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
2264f92363d1SSreekanth Reddy 		    ioc->name, __func__));
2265f92363d1SSreekanth Reddy 	} else {
2266f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT
2267f92363d1SSreekanth Reddy 			"%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2268f92363d1SSreekanth Reddy 			ioc->name, __func__,
2269f92363d1SSreekanth Reddy 		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2270f92363d1SSreekanth Reddy 		rc = -EFAULT;
2271f92363d1SSreekanth Reddy 	}
2272f92363d1SSreekanth Reddy 
2273f92363d1SSreekanth Reddy  issue_host_reset:
2274f92363d1SSreekanth Reddy 	if (issue_reset)
227598c56ad3SCalvin Owens 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2276f92363d1SSreekanth Reddy 
2277f92363d1SSreekanth Reddy  out:
2278f92363d1SSreekanth Reddy 
2279f92363d1SSreekanth Reddy 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2280f92363d1SSreekanth Reddy 	return rc;
2281f92363d1SSreekanth Reddy }
2282f92363d1SSreekanth Reddy 
2283f92363d1SSreekanth Reddy 
2284f92363d1SSreekanth Reddy 
2285f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2286f92363d1SSreekanth Reddy /**
2287f92363d1SSreekanth Reddy  * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2288f92363d1SSreekanth Reddy  * @ioc: per adapter object
2289f92363d1SSreekanth Reddy  * @cmd - ioctl opcode
2290f92363d1SSreekanth Reddy  * @arg - (struct mpt3_ioctl_command32)
2291f92363d1SSreekanth Reddy  *
2292f92363d1SSreekanth Reddy  * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2293f92363d1SSreekanth Reddy  */
2294f92363d1SSreekanth Reddy static long
2295f92363d1SSreekanth Reddy _ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2296f92363d1SSreekanth Reddy 	void __user *arg)
2297f92363d1SSreekanth Reddy {
2298f92363d1SSreekanth Reddy 	struct mpt3_ioctl_command32 karg32;
2299f92363d1SSreekanth Reddy 	struct mpt3_ioctl_command32 __user *uarg;
2300f92363d1SSreekanth Reddy 	struct mpt3_ioctl_command karg;
2301f92363d1SSreekanth Reddy 
2302f92363d1SSreekanth Reddy 	if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2303f92363d1SSreekanth Reddy 		return -EINVAL;
2304f92363d1SSreekanth Reddy 
2305f92363d1SSreekanth Reddy 	uarg = (struct mpt3_ioctl_command32 __user *) arg;
2306f92363d1SSreekanth Reddy 
2307f92363d1SSreekanth Reddy 	if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2308f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2309f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2310f92363d1SSreekanth Reddy 		return -EFAULT;
2311f92363d1SSreekanth Reddy 	}
2312f92363d1SSreekanth Reddy 
2313f92363d1SSreekanth Reddy 	memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2314f92363d1SSreekanth Reddy 	karg.hdr.ioc_number = karg32.hdr.ioc_number;
2315f92363d1SSreekanth Reddy 	karg.hdr.port_number = karg32.hdr.port_number;
2316f92363d1SSreekanth Reddy 	karg.hdr.max_data_size = karg32.hdr.max_data_size;
2317f92363d1SSreekanth Reddy 	karg.timeout = karg32.timeout;
2318f92363d1SSreekanth Reddy 	karg.max_reply_bytes = karg32.max_reply_bytes;
2319f92363d1SSreekanth Reddy 	karg.data_in_size = karg32.data_in_size;
2320f92363d1SSreekanth Reddy 	karg.data_out_size = karg32.data_out_size;
2321f92363d1SSreekanth Reddy 	karg.max_sense_bytes = karg32.max_sense_bytes;
2322f92363d1SSreekanth Reddy 	karg.data_sge_offset = karg32.data_sge_offset;
2323f92363d1SSreekanth Reddy 	karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2324f92363d1SSreekanth Reddy 	karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2325f92363d1SSreekanth Reddy 	karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2326f92363d1SSreekanth Reddy 	karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2327f92363d1SSreekanth Reddy 	return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2328f92363d1SSreekanth Reddy }
2329f92363d1SSreekanth Reddy #endif
2330f92363d1SSreekanth Reddy 
2331f92363d1SSreekanth Reddy /**
2332f92363d1SSreekanth Reddy  * _ctl_ioctl_main - main ioctl entry point
2333f92363d1SSreekanth Reddy  * @file - (struct file)
2334f92363d1SSreekanth Reddy  * @cmd - ioctl opcode
2335c84b06a4SSreekanth Reddy  * @arg - user space data buffer
2336c84b06a4SSreekanth Reddy  * @compat - handles 32 bit applications in 64bit os
2337c84b06a4SSreekanth Reddy  * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
2338b130b0d5SSuganath prabu Subramani  * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
2339f92363d1SSreekanth Reddy  */
2340f92363d1SSreekanth Reddy static long
2341f92363d1SSreekanth Reddy _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2342c84b06a4SSreekanth Reddy 	u8 compat, u16 mpi_version)
2343f92363d1SSreekanth Reddy {
2344f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc;
2345f92363d1SSreekanth Reddy 	struct mpt3_ioctl_header ioctl_header;
2346f92363d1SSreekanth Reddy 	enum block_state state;
2347f92363d1SSreekanth Reddy 	long ret = -EINVAL;
2348f92363d1SSreekanth Reddy 
2349f92363d1SSreekanth Reddy 	/* get IOCTL header */
2350f92363d1SSreekanth Reddy 	if (copy_from_user(&ioctl_header, (char __user *)arg,
2351f92363d1SSreekanth Reddy 	    sizeof(struct mpt3_ioctl_header))) {
2352f92363d1SSreekanth Reddy 		pr_err("failure at %s:%d/%s()!\n",
2353f92363d1SSreekanth Reddy 		    __FILE__, __LINE__, __func__);
2354f92363d1SSreekanth Reddy 		return -EFAULT;
2355f92363d1SSreekanth Reddy 	}
2356f92363d1SSreekanth Reddy 
2357c84b06a4SSreekanth Reddy 	if (_ctl_verify_adapter(ioctl_header.ioc_number,
2358c84b06a4SSreekanth Reddy 				&ioc, mpi_version) == -1 || !ioc)
2359f92363d1SSreekanth Reddy 		return -ENODEV;
2360f92363d1SSreekanth Reddy 
236108c4d550SSreekanth Reddy 	/* pci_access_mutex lock acquired by ioctl path */
236208c4d550SSreekanth Reddy 	mutex_lock(&ioc->pci_access_mutex);
236308c4d550SSreekanth Reddy 
2364f92363d1SSreekanth Reddy 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
236508c4d550SSreekanth Reddy 	    ioc->is_driver_loading || ioc->remove_host) {
236608c4d550SSreekanth Reddy 		ret = -EAGAIN;
236708c4d550SSreekanth Reddy 		goto out_unlock_pciaccess;
236808c4d550SSreekanth Reddy 	}
2369f92363d1SSreekanth Reddy 
2370f92363d1SSreekanth Reddy 	state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2371f92363d1SSreekanth Reddy 	if (state == NON_BLOCKING) {
237208c4d550SSreekanth Reddy 		if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
237308c4d550SSreekanth Reddy 			ret = -EAGAIN;
237408c4d550SSreekanth Reddy 			goto out_unlock_pciaccess;
237508c4d550SSreekanth Reddy 		}
237608c4d550SSreekanth Reddy 	} else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
237708c4d550SSreekanth Reddy 		ret = -ERESTARTSYS;
237808c4d550SSreekanth Reddy 		goto out_unlock_pciaccess;
237908c4d550SSreekanth Reddy 	}
2380f92363d1SSreekanth Reddy 
2381f92363d1SSreekanth Reddy 
2382f92363d1SSreekanth Reddy 	switch (cmd) {
2383f92363d1SSreekanth Reddy 	case MPT3IOCINFO:
2384f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2385f92363d1SSreekanth Reddy 			ret = _ctl_getiocinfo(ioc, arg);
2386f92363d1SSreekanth Reddy 		break;
2387f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2388f92363d1SSreekanth Reddy 	case MPT3COMMAND32:
2389f92363d1SSreekanth Reddy #endif
2390f92363d1SSreekanth Reddy 	case MPT3COMMAND:
2391f92363d1SSreekanth Reddy 	{
2392f92363d1SSreekanth Reddy 		struct mpt3_ioctl_command __user *uarg;
2393f92363d1SSreekanth Reddy 		struct mpt3_ioctl_command karg;
2394f92363d1SSreekanth Reddy 
2395f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2396f92363d1SSreekanth Reddy 		if (compat) {
2397f92363d1SSreekanth Reddy 			ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2398f92363d1SSreekanth Reddy 			break;
2399f92363d1SSreekanth Reddy 		}
2400f92363d1SSreekanth Reddy #endif
2401f92363d1SSreekanth Reddy 		if (copy_from_user(&karg, arg, sizeof(karg))) {
2402f92363d1SSreekanth Reddy 			pr_err("failure at %s:%d/%s()!\n",
2403f92363d1SSreekanth Reddy 			    __FILE__, __LINE__, __func__);
2404f92363d1SSreekanth Reddy 			ret = -EFAULT;
2405f92363d1SSreekanth Reddy 			break;
2406f92363d1SSreekanth Reddy 		}
2407f92363d1SSreekanth Reddy 
2408f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2409f92363d1SSreekanth Reddy 			uarg = arg;
2410f92363d1SSreekanth Reddy 			ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2411f92363d1SSreekanth Reddy 		}
2412f92363d1SSreekanth Reddy 		break;
2413f92363d1SSreekanth Reddy 	}
2414f92363d1SSreekanth Reddy 	case MPT3EVENTQUERY:
2415f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2416f92363d1SSreekanth Reddy 			ret = _ctl_eventquery(ioc, arg);
2417f92363d1SSreekanth Reddy 		break;
2418f92363d1SSreekanth Reddy 	case MPT3EVENTENABLE:
2419f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2420f92363d1SSreekanth Reddy 			ret = _ctl_eventenable(ioc, arg);
2421f92363d1SSreekanth Reddy 		break;
2422f92363d1SSreekanth Reddy 	case MPT3EVENTREPORT:
2423f92363d1SSreekanth Reddy 		ret = _ctl_eventreport(ioc, arg);
2424f92363d1SSreekanth Reddy 		break;
2425f92363d1SSreekanth Reddy 	case MPT3HARDRESET:
2426f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2427f92363d1SSreekanth Reddy 			ret = _ctl_do_reset(ioc, arg);
2428f92363d1SSreekanth Reddy 		break;
2429f92363d1SSreekanth Reddy 	case MPT3BTDHMAPPING:
2430f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2431f92363d1SSreekanth Reddy 			ret = _ctl_btdh_mapping(ioc, arg);
2432f92363d1SSreekanth Reddy 		break;
2433f92363d1SSreekanth Reddy 	case MPT3DIAGREGISTER:
2434f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2435f92363d1SSreekanth Reddy 			ret = _ctl_diag_register(ioc, arg);
2436f92363d1SSreekanth Reddy 		break;
2437f92363d1SSreekanth Reddy 	case MPT3DIAGUNREGISTER:
2438f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2439f92363d1SSreekanth Reddy 			ret = _ctl_diag_unregister(ioc, arg);
2440f92363d1SSreekanth Reddy 		break;
2441f92363d1SSreekanth Reddy 	case MPT3DIAGQUERY:
2442f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2443f92363d1SSreekanth Reddy 			ret = _ctl_diag_query(ioc, arg);
2444f92363d1SSreekanth Reddy 		break;
2445f92363d1SSreekanth Reddy 	case MPT3DIAGRELEASE:
2446f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2447f92363d1SSreekanth Reddy 			ret = _ctl_diag_release(ioc, arg);
2448f92363d1SSreekanth Reddy 		break;
2449f92363d1SSreekanth Reddy 	case MPT3DIAGREADBUFFER:
2450f92363d1SSreekanth Reddy 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2451f92363d1SSreekanth Reddy 			ret = _ctl_diag_read_buffer(ioc, arg);
2452f92363d1SSreekanth Reddy 		break;
2453f92363d1SSreekanth Reddy 	default:
2454f92363d1SSreekanth Reddy 		dctlprintk(ioc, pr_info(MPT3SAS_FMT
2455f92363d1SSreekanth Reddy 		    "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2456f92363d1SSreekanth Reddy 		break;
2457f92363d1SSreekanth Reddy 	}
2458f92363d1SSreekanth Reddy 
2459f92363d1SSreekanth Reddy 	mutex_unlock(&ioc->ctl_cmds.mutex);
246008c4d550SSreekanth Reddy out_unlock_pciaccess:
246108c4d550SSreekanth Reddy 	mutex_unlock(&ioc->pci_access_mutex);
2462f92363d1SSreekanth Reddy 	return ret;
2463f92363d1SSreekanth Reddy }
2464f92363d1SSreekanth Reddy 
2465f92363d1SSreekanth Reddy /**
2466c84b06a4SSreekanth Reddy  * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
2467f92363d1SSreekanth Reddy  * @file - (struct file)
2468f92363d1SSreekanth Reddy  * @cmd - ioctl opcode
2469f92363d1SSreekanth Reddy  * @arg -
2470f92363d1SSreekanth Reddy  */
24718bbb1cf6SCalvin Owens static long
2472c84b06a4SSreekanth Reddy _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2473f92363d1SSreekanth Reddy {
2474f92363d1SSreekanth Reddy 	long ret;
2475f92363d1SSreekanth Reddy 
2476b130b0d5SSuganath prabu Subramani 	/* pass MPI25_VERSION | MPI26_VERSION value,
2477b130b0d5SSuganath prabu Subramani 	 * to indicate that this ioctl cmd
2478c84b06a4SSreekanth Reddy 	 * came from mpt3ctl ioctl device.
2479c84b06a4SSreekanth Reddy 	 */
2480b130b0d5SSuganath prabu Subramani 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2481b130b0d5SSuganath prabu Subramani 		MPI25_VERSION | MPI26_VERSION);
2482f92363d1SSreekanth Reddy 	return ret;
2483f92363d1SSreekanth Reddy }
2484f92363d1SSreekanth Reddy 
2485c84b06a4SSreekanth Reddy /**
2486c84b06a4SSreekanth Reddy  * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
2487c84b06a4SSreekanth Reddy  * @file - (struct file)
2488c84b06a4SSreekanth Reddy  * @cmd - ioctl opcode
2489c84b06a4SSreekanth Reddy  * @arg -
2490c84b06a4SSreekanth Reddy  */
24918bbb1cf6SCalvin Owens static long
2492c84b06a4SSreekanth Reddy _ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2493c84b06a4SSreekanth Reddy {
2494c84b06a4SSreekanth Reddy 	long ret;
2495c84b06a4SSreekanth Reddy 
2496c84b06a4SSreekanth Reddy 	/* pass MPI2_VERSION value, to indicate that this ioctl cmd
2497c84b06a4SSreekanth Reddy 	 * came from mpt2ctl ioctl device.
2498c84b06a4SSreekanth Reddy 	 */
2499c84b06a4SSreekanth Reddy 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2500c84b06a4SSreekanth Reddy 	return ret;
2501c84b06a4SSreekanth Reddy }
2502f92363d1SSreekanth Reddy #ifdef CONFIG_COMPAT
2503f92363d1SSreekanth Reddy /**
2504c84b06a4SSreekanth Reddy  *_ ctl_ioctl_compat - main ioctl entry point (compat)
2505f92363d1SSreekanth Reddy  * @file -
2506f92363d1SSreekanth Reddy  * @cmd -
2507f92363d1SSreekanth Reddy  * @arg -
2508f92363d1SSreekanth Reddy  *
2509f92363d1SSreekanth Reddy  * This routine handles 32 bit applications in 64bit os.
2510f92363d1SSreekanth Reddy  */
25118bbb1cf6SCalvin Owens static long
2512c84b06a4SSreekanth Reddy _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2513f92363d1SSreekanth Reddy {
2514f92363d1SSreekanth Reddy 	long ret;
2515f92363d1SSreekanth Reddy 
2516b130b0d5SSuganath prabu Subramani 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2517b130b0d5SSuganath prabu Subramani 		MPI25_VERSION | MPI26_VERSION);
2518c84b06a4SSreekanth Reddy 	return ret;
2519c84b06a4SSreekanth Reddy }
2520c84b06a4SSreekanth Reddy 
2521c84b06a4SSreekanth Reddy /**
2522c84b06a4SSreekanth Reddy  *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
2523c84b06a4SSreekanth Reddy  * @file -
2524c84b06a4SSreekanth Reddy  * @cmd -
2525c84b06a4SSreekanth Reddy  * @arg -
2526c84b06a4SSreekanth Reddy  *
2527c84b06a4SSreekanth Reddy  * This routine handles 32 bit applications in 64bit os.
2528c84b06a4SSreekanth Reddy  */
25298bbb1cf6SCalvin Owens static long
2530c84b06a4SSreekanth Reddy _ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2531c84b06a4SSreekanth Reddy {
2532c84b06a4SSreekanth Reddy 	long ret;
2533c84b06a4SSreekanth Reddy 
2534c84b06a4SSreekanth Reddy 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
2535f92363d1SSreekanth Reddy 	return ret;
2536f92363d1SSreekanth Reddy }
2537f92363d1SSreekanth Reddy #endif
2538f92363d1SSreekanth Reddy 
2539f92363d1SSreekanth Reddy /* scsi host attributes */
2540f92363d1SSreekanth Reddy /**
2541f92363d1SSreekanth Reddy  * _ctl_version_fw_show - firmware version
2542f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2543f92363d1SSreekanth Reddy  * @buf - the buffer returned
2544f92363d1SSreekanth Reddy  *
2545f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2546f92363d1SSreekanth Reddy  */
2547f92363d1SSreekanth Reddy static ssize_t
2548f92363d1SSreekanth Reddy _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2549f92363d1SSreekanth Reddy 	char *buf)
2550f92363d1SSreekanth Reddy {
2551f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2552f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2553f92363d1SSreekanth Reddy 
2554f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2555f92363d1SSreekanth Reddy 	    (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2556f92363d1SSreekanth Reddy 	    (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2557f92363d1SSreekanth Reddy 	    (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2558f92363d1SSreekanth Reddy 	    ioc->facts.FWVersion.Word & 0x000000FF);
2559f92363d1SSreekanth Reddy }
2560f92363d1SSreekanth Reddy static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2561f92363d1SSreekanth Reddy 
2562f92363d1SSreekanth Reddy /**
2563f92363d1SSreekanth Reddy  * _ctl_version_bios_show - bios version
2564f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2565f92363d1SSreekanth Reddy  * @buf - the buffer returned
2566f92363d1SSreekanth Reddy  *
2567f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2568f92363d1SSreekanth Reddy  */
2569f92363d1SSreekanth Reddy static ssize_t
2570f92363d1SSreekanth Reddy _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2571f92363d1SSreekanth Reddy 	char *buf)
2572f92363d1SSreekanth Reddy {
2573f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2574f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2575f92363d1SSreekanth Reddy 
2576f92363d1SSreekanth Reddy 	u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2577f92363d1SSreekanth Reddy 
2578f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2579f92363d1SSreekanth Reddy 	    (version & 0xFF000000) >> 24,
2580f92363d1SSreekanth Reddy 	    (version & 0x00FF0000) >> 16,
2581f92363d1SSreekanth Reddy 	    (version & 0x0000FF00) >> 8,
2582f92363d1SSreekanth Reddy 	    version & 0x000000FF);
2583f92363d1SSreekanth Reddy }
2584f92363d1SSreekanth Reddy static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2585f92363d1SSreekanth Reddy 
2586f92363d1SSreekanth Reddy /**
2587f92363d1SSreekanth Reddy  * _ctl_version_mpi_show - MPI (message passing interface) version
2588f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2589f92363d1SSreekanth Reddy  * @buf - the buffer returned
2590f92363d1SSreekanth Reddy  *
2591f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2592f92363d1SSreekanth Reddy  */
2593f92363d1SSreekanth Reddy static ssize_t
2594f92363d1SSreekanth Reddy _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2595f92363d1SSreekanth Reddy 	char *buf)
2596f92363d1SSreekanth Reddy {
2597f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2598f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2599f92363d1SSreekanth Reddy 
2600f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2601f92363d1SSreekanth Reddy 	    ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2602f92363d1SSreekanth Reddy }
2603f92363d1SSreekanth Reddy static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2604f92363d1SSreekanth Reddy 
2605f92363d1SSreekanth Reddy /**
2606f92363d1SSreekanth Reddy  * _ctl_version_product_show - product name
2607f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2608f92363d1SSreekanth Reddy  * @buf - the buffer returned
2609f92363d1SSreekanth Reddy  *
2610f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2611f92363d1SSreekanth Reddy  */
2612f92363d1SSreekanth Reddy static ssize_t
2613f92363d1SSreekanth Reddy _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2614f92363d1SSreekanth Reddy 	char *buf)
2615f92363d1SSreekanth Reddy {
2616f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2617f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2618f92363d1SSreekanth Reddy 
2619f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2620f92363d1SSreekanth Reddy }
2621f92363d1SSreekanth Reddy static DEVICE_ATTR(version_product, S_IRUGO, _ctl_version_product_show, NULL);
2622f92363d1SSreekanth Reddy 
2623f92363d1SSreekanth Reddy /**
2624f92363d1SSreekanth Reddy  * _ctl_version_nvdata_persistent_show - ndvata persistent version
2625f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2626f92363d1SSreekanth Reddy  * @buf - the buffer returned
2627f92363d1SSreekanth Reddy  *
2628f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2629f92363d1SSreekanth Reddy  */
2630f92363d1SSreekanth Reddy static ssize_t
2631f92363d1SSreekanth Reddy _ctl_version_nvdata_persistent_show(struct device *cdev,
2632f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
2633f92363d1SSreekanth Reddy {
2634f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2635f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2636f92363d1SSreekanth Reddy 
2637f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2638f92363d1SSreekanth Reddy 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2639f92363d1SSreekanth Reddy }
2640f92363d1SSreekanth Reddy static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2641f92363d1SSreekanth Reddy 	_ctl_version_nvdata_persistent_show, NULL);
2642f92363d1SSreekanth Reddy 
2643f92363d1SSreekanth Reddy /**
2644f92363d1SSreekanth Reddy  * _ctl_version_nvdata_default_show - nvdata default version
2645f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2646f92363d1SSreekanth Reddy  * @buf - the buffer returned
2647f92363d1SSreekanth Reddy  *
2648f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2649f92363d1SSreekanth Reddy  */
2650f92363d1SSreekanth Reddy static ssize_t
2651f92363d1SSreekanth Reddy _ctl_version_nvdata_default_show(struct device *cdev, struct device_attribute
2652f92363d1SSreekanth Reddy 	*attr, char *buf)
2653f92363d1SSreekanth Reddy {
2654f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2655f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2656f92363d1SSreekanth Reddy 
2657f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2658f92363d1SSreekanth Reddy 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2659f92363d1SSreekanth Reddy }
2660f92363d1SSreekanth Reddy static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2661f92363d1SSreekanth Reddy 	_ctl_version_nvdata_default_show, NULL);
2662f92363d1SSreekanth Reddy 
2663f92363d1SSreekanth Reddy /**
2664f92363d1SSreekanth Reddy  * _ctl_board_name_show - board name
2665f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2666f92363d1SSreekanth Reddy  * @buf - the buffer returned
2667f92363d1SSreekanth Reddy  *
2668f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2669f92363d1SSreekanth Reddy  */
2670f92363d1SSreekanth Reddy static ssize_t
2671f92363d1SSreekanth Reddy _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2672f92363d1SSreekanth Reddy 	char *buf)
2673f92363d1SSreekanth Reddy {
2674f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2675f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2676f92363d1SSreekanth Reddy 
2677f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2678f92363d1SSreekanth Reddy }
2679f92363d1SSreekanth Reddy static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2680f92363d1SSreekanth Reddy 
2681f92363d1SSreekanth Reddy /**
2682f92363d1SSreekanth Reddy  * _ctl_board_assembly_show - board assembly name
2683f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2684f92363d1SSreekanth Reddy  * @buf - the buffer returned
2685f92363d1SSreekanth Reddy  *
2686f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2687f92363d1SSreekanth Reddy  */
2688f92363d1SSreekanth Reddy static ssize_t
2689f92363d1SSreekanth Reddy _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2690f92363d1SSreekanth Reddy 	char *buf)
2691f92363d1SSreekanth Reddy {
2692f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2693f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2694f92363d1SSreekanth Reddy 
2695f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2696f92363d1SSreekanth Reddy }
2697f92363d1SSreekanth Reddy static DEVICE_ATTR(board_assembly, S_IRUGO, _ctl_board_assembly_show, NULL);
2698f92363d1SSreekanth Reddy 
2699f92363d1SSreekanth Reddy /**
2700f92363d1SSreekanth Reddy  * _ctl_board_tracer_show - board tracer number
2701f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2702f92363d1SSreekanth Reddy  * @buf - the buffer returned
2703f92363d1SSreekanth Reddy  *
2704f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2705f92363d1SSreekanth Reddy  */
2706f92363d1SSreekanth Reddy static ssize_t
2707f92363d1SSreekanth Reddy _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2708f92363d1SSreekanth Reddy 	char *buf)
2709f92363d1SSreekanth Reddy {
2710f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2711f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2712f92363d1SSreekanth Reddy 
2713f92363d1SSreekanth Reddy 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2714f92363d1SSreekanth Reddy }
2715f92363d1SSreekanth Reddy static DEVICE_ATTR(board_tracer, S_IRUGO, _ctl_board_tracer_show, NULL);
2716f92363d1SSreekanth Reddy 
2717f92363d1SSreekanth Reddy /**
2718f92363d1SSreekanth Reddy  * _ctl_io_delay_show - io missing delay
2719f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2720f92363d1SSreekanth Reddy  * @buf - the buffer returned
2721f92363d1SSreekanth Reddy  *
2722f92363d1SSreekanth Reddy  * This is for firmware implemention for deboucing device
2723f92363d1SSreekanth Reddy  * removal events.
2724f92363d1SSreekanth Reddy  *
2725f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2726f92363d1SSreekanth Reddy  */
2727f92363d1SSreekanth Reddy static ssize_t
2728f92363d1SSreekanth Reddy _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2729f92363d1SSreekanth Reddy 	char *buf)
2730f92363d1SSreekanth Reddy {
2731f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2732f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2733f92363d1SSreekanth Reddy 
2734f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2735f92363d1SSreekanth Reddy }
2736f92363d1SSreekanth Reddy static DEVICE_ATTR(io_delay, S_IRUGO, _ctl_io_delay_show, NULL);
2737f92363d1SSreekanth Reddy 
2738f92363d1SSreekanth Reddy /**
2739f92363d1SSreekanth Reddy  * _ctl_device_delay_show - device missing delay
2740f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2741f92363d1SSreekanth Reddy  * @buf - the buffer returned
2742f92363d1SSreekanth Reddy  *
2743f92363d1SSreekanth Reddy  * This is for firmware implemention for deboucing device
2744f92363d1SSreekanth Reddy  * removal events.
2745f92363d1SSreekanth Reddy  *
2746f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2747f92363d1SSreekanth Reddy  */
2748f92363d1SSreekanth Reddy static ssize_t
2749f92363d1SSreekanth Reddy _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2750f92363d1SSreekanth Reddy 	char *buf)
2751f92363d1SSreekanth Reddy {
2752f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2753f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2754f92363d1SSreekanth Reddy 
2755f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2756f92363d1SSreekanth Reddy }
2757f92363d1SSreekanth Reddy static DEVICE_ATTR(device_delay, S_IRUGO, _ctl_device_delay_show, NULL);
2758f92363d1SSreekanth Reddy 
2759f92363d1SSreekanth Reddy /**
2760f92363d1SSreekanth Reddy  * _ctl_fw_queue_depth_show - global credits
2761f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2762f92363d1SSreekanth Reddy  * @buf - the buffer returned
2763f92363d1SSreekanth Reddy  *
2764f92363d1SSreekanth Reddy  * This is firmware queue depth limit
2765f92363d1SSreekanth Reddy  *
2766f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2767f92363d1SSreekanth Reddy  */
2768f92363d1SSreekanth Reddy static ssize_t
2769f92363d1SSreekanth Reddy _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2770f92363d1SSreekanth Reddy 	char *buf)
2771f92363d1SSreekanth Reddy {
2772f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2773f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2774f92363d1SSreekanth Reddy 
2775f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2776f92363d1SSreekanth Reddy }
2777f92363d1SSreekanth Reddy static DEVICE_ATTR(fw_queue_depth, S_IRUGO, _ctl_fw_queue_depth_show, NULL);
2778f92363d1SSreekanth Reddy 
2779f92363d1SSreekanth Reddy /**
2780f92363d1SSreekanth Reddy  * _ctl_sas_address_show - sas address
2781f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2782f92363d1SSreekanth Reddy  * @buf - the buffer returned
2783f92363d1SSreekanth Reddy  *
2784f92363d1SSreekanth Reddy  * This is the controller sas address
2785f92363d1SSreekanth Reddy  *
2786f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2787f92363d1SSreekanth Reddy  */
2788f92363d1SSreekanth Reddy static ssize_t
2789f92363d1SSreekanth Reddy _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2790f92363d1SSreekanth Reddy 	char *buf)
2791f92363d1SSreekanth Reddy 
2792f92363d1SSreekanth Reddy {
2793f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2794f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2795f92363d1SSreekanth Reddy 
2796f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2797f92363d1SSreekanth Reddy 	    (unsigned long long)ioc->sas_hba.sas_address);
2798f92363d1SSreekanth Reddy }
2799f92363d1SSreekanth Reddy static DEVICE_ATTR(host_sas_address, S_IRUGO,
2800f92363d1SSreekanth Reddy 	_ctl_host_sas_address_show, NULL);
2801f92363d1SSreekanth Reddy 
2802f92363d1SSreekanth Reddy /**
2803f92363d1SSreekanth Reddy  * _ctl_logging_level_show - logging level
2804f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2805f92363d1SSreekanth Reddy  * @buf - the buffer returned
2806f92363d1SSreekanth Reddy  *
2807f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
2808f92363d1SSreekanth Reddy  */
2809f92363d1SSreekanth Reddy static ssize_t
2810f92363d1SSreekanth Reddy _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2811f92363d1SSreekanth Reddy 	char *buf)
2812f92363d1SSreekanth Reddy {
2813f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2814f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2815f92363d1SSreekanth Reddy 
2816f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2817f92363d1SSreekanth Reddy }
2818f92363d1SSreekanth Reddy static ssize_t
2819f92363d1SSreekanth Reddy _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2820f92363d1SSreekanth Reddy 	const char *buf, size_t count)
2821f92363d1SSreekanth Reddy {
2822f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2823f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2824f92363d1SSreekanth Reddy 	int val = 0;
2825f92363d1SSreekanth Reddy 
2826f92363d1SSreekanth Reddy 	if (sscanf(buf, "%x", &val) != 1)
2827f92363d1SSreekanth Reddy 		return -EINVAL;
2828f92363d1SSreekanth Reddy 
2829f92363d1SSreekanth Reddy 	ioc->logging_level = val;
2830f92363d1SSreekanth Reddy 	pr_info(MPT3SAS_FMT "logging_level=%08xh\n", ioc->name,
2831f92363d1SSreekanth Reddy 	    ioc->logging_level);
2832f92363d1SSreekanth Reddy 	return strlen(buf);
2833f92363d1SSreekanth Reddy }
2834f92363d1SSreekanth Reddy static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR, _ctl_logging_level_show,
2835f92363d1SSreekanth Reddy 	_ctl_logging_level_store);
2836f92363d1SSreekanth Reddy 
2837f92363d1SSreekanth Reddy /**
2838f92363d1SSreekanth Reddy  * _ctl_fwfault_debug_show - show/store fwfault_debug
2839f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2840f92363d1SSreekanth Reddy  * @buf - the buffer returned
2841f92363d1SSreekanth Reddy  *
2842f92363d1SSreekanth Reddy  * mpt3sas_fwfault_debug is command line option
2843f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
2844f92363d1SSreekanth Reddy  */
2845f92363d1SSreekanth Reddy static ssize_t
2846f92363d1SSreekanth Reddy _ctl_fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
2847f92363d1SSreekanth Reddy 	char *buf)
2848f92363d1SSreekanth Reddy {
2849f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2850f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2851f92363d1SSreekanth Reddy 
2852f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2853f92363d1SSreekanth Reddy }
2854f92363d1SSreekanth Reddy static ssize_t
2855f92363d1SSreekanth Reddy _ctl_fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
2856f92363d1SSreekanth Reddy 	const char *buf, size_t count)
2857f92363d1SSreekanth Reddy {
2858f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2859f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2860f92363d1SSreekanth Reddy 	int val = 0;
2861f92363d1SSreekanth Reddy 
2862f92363d1SSreekanth Reddy 	if (sscanf(buf, "%d", &val) != 1)
2863f92363d1SSreekanth Reddy 		return -EINVAL;
2864f92363d1SSreekanth Reddy 
2865f92363d1SSreekanth Reddy 	ioc->fwfault_debug = val;
2866f92363d1SSreekanth Reddy 	pr_info(MPT3SAS_FMT "fwfault_debug=%d\n", ioc->name,
2867f92363d1SSreekanth Reddy 	    ioc->fwfault_debug);
2868f92363d1SSreekanth Reddy 	return strlen(buf);
2869f92363d1SSreekanth Reddy }
2870f92363d1SSreekanth Reddy static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2871f92363d1SSreekanth Reddy 	_ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2872f92363d1SSreekanth Reddy 
2873f92363d1SSreekanth Reddy /**
2874f92363d1SSreekanth Reddy  * _ctl_ioc_reset_count_show - ioc reset count
2875f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2876f92363d1SSreekanth Reddy  * @buf - the buffer returned
2877f92363d1SSreekanth Reddy  *
2878f92363d1SSreekanth Reddy  * This is firmware queue depth limit
2879f92363d1SSreekanth Reddy  *
2880f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2881f92363d1SSreekanth Reddy  */
2882f92363d1SSreekanth Reddy static ssize_t
2883f92363d1SSreekanth Reddy _ctl_ioc_reset_count_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, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
2890f92363d1SSreekanth Reddy }
2891f92363d1SSreekanth Reddy static DEVICE_ATTR(ioc_reset_count, S_IRUGO, _ctl_ioc_reset_count_show, NULL);
2892f92363d1SSreekanth Reddy 
2893f92363d1SSreekanth Reddy /**
2894f92363d1SSreekanth Reddy  * _ctl_ioc_reply_queue_count_show - number of reply queues
2895f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
2896f92363d1SSreekanth Reddy  * @buf - the buffer returned
2897f92363d1SSreekanth Reddy  *
2898f92363d1SSreekanth Reddy  * This is number of reply queues
2899f92363d1SSreekanth Reddy  *
2900f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
2901f92363d1SSreekanth Reddy  */
2902f92363d1SSreekanth Reddy static ssize_t
2903f92363d1SSreekanth Reddy _ctl_ioc_reply_queue_count_show(struct device *cdev,
2904f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
2905f92363d1SSreekanth Reddy {
2906f92363d1SSreekanth Reddy 	u8 reply_queue_count;
2907f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
2908f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2909f92363d1SSreekanth Reddy 
2910f92363d1SSreekanth Reddy 	if ((ioc->facts.IOCCapabilities &
2911f92363d1SSreekanth Reddy 	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2912f92363d1SSreekanth Reddy 		reply_queue_count = ioc->reply_queue_count;
2913f92363d1SSreekanth Reddy 	else
2914f92363d1SSreekanth Reddy 		reply_queue_count = 1;
2915f92363d1SSreekanth Reddy 
2916f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2917f92363d1SSreekanth Reddy }
2918f92363d1SSreekanth Reddy static DEVICE_ATTR(reply_queue_count, S_IRUGO, _ctl_ioc_reply_queue_count_show,
2919f92363d1SSreekanth Reddy 	NULL);
2920f92363d1SSreekanth Reddy 
292142263095SSreekanth Reddy /**
292242263095SSreekanth Reddy  * _ctl_BRM_status_show - Backup Rail Monitor Status
292342263095SSreekanth Reddy  * @cdev - pointer to embedded class device
292442263095SSreekanth Reddy  * @buf - the buffer returned
292542263095SSreekanth Reddy  *
292642263095SSreekanth Reddy  * This is number of reply queues
292742263095SSreekanth Reddy  *
292842263095SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
292942263095SSreekanth Reddy  */
293042263095SSreekanth Reddy static ssize_t
293142263095SSreekanth Reddy _ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
293242263095SSreekanth Reddy 	char *buf)
293342263095SSreekanth Reddy {
293442263095SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
293542263095SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
293642263095SSreekanth Reddy 	Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
293742263095SSreekanth Reddy 	Mpi2ConfigReply_t mpi_reply;
293842263095SSreekanth Reddy 	u16 backup_rail_monitor_status = 0;
293942263095SSreekanth Reddy 	u16 ioc_status;
294042263095SSreekanth Reddy 	int sz;
294142263095SSreekanth Reddy 	ssize_t rc = 0;
294242263095SSreekanth Reddy 
294342263095SSreekanth Reddy 	if (!ioc->is_warpdrive) {
294442263095SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: BRM attribute is only for"
294542263095SSreekanth Reddy 		    " warpdrive\n", ioc->name, __func__);
294642263095SSreekanth Reddy 		goto out;
294742263095SSreekanth Reddy 	}
294808c4d550SSreekanth Reddy 	/* pci_access_mutex lock acquired by sysfs show path */
294908c4d550SSreekanth Reddy 	mutex_lock(&ioc->pci_access_mutex);
295008c4d550SSreekanth Reddy 	if (ioc->pci_error_recovery || ioc->remove_host) {
295108c4d550SSreekanth Reddy 		mutex_unlock(&ioc->pci_access_mutex);
295208c4d550SSreekanth Reddy 		return 0;
295308c4d550SSreekanth Reddy 	}
295442263095SSreekanth Reddy 
295542263095SSreekanth Reddy 	/* allocate upto GPIOVal 36 entries */
295642263095SSreekanth Reddy 	sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
295742263095SSreekanth Reddy 	io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
295842263095SSreekanth Reddy 	if (!io_unit_pg3) {
295942263095SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: failed allocating memory "
296042263095SSreekanth Reddy 		    "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
296142263095SSreekanth Reddy 		goto out;
296242263095SSreekanth Reddy 	}
296342263095SSreekanth Reddy 
296442263095SSreekanth Reddy 	if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
296542263095SSreekanth Reddy 	    0) {
296642263095SSreekanth Reddy 		pr_err(MPT3SAS_FMT
296742263095SSreekanth Reddy 		    "%s: failed reading iounit_pg3\n", ioc->name,
296842263095SSreekanth Reddy 		    __func__);
296942263095SSreekanth Reddy 		goto out;
297042263095SSreekanth Reddy 	}
297142263095SSreekanth Reddy 
297242263095SSreekanth Reddy 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
297342263095SSreekanth Reddy 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
297442263095SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: iounit_pg3 failed with "
297542263095SSreekanth Reddy 		    "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
297642263095SSreekanth Reddy 		goto out;
297742263095SSreekanth Reddy 	}
297842263095SSreekanth Reddy 
297942263095SSreekanth Reddy 	if (io_unit_pg3->GPIOCount < 25) {
298042263095SSreekanth Reddy 		pr_err(MPT3SAS_FMT "%s: iounit_pg3->GPIOCount less than "
298142263095SSreekanth Reddy 		     "25 entries, detected (%d) entries\n", ioc->name, __func__,
298242263095SSreekanth Reddy 		    io_unit_pg3->GPIOCount);
298342263095SSreekanth Reddy 		goto out;
298442263095SSreekanth Reddy 	}
298542263095SSreekanth Reddy 
298642263095SSreekanth Reddy 	/* BRM status is in bit zero of GPIOVal[24] */
298742263095SSreekanth Reddy 	backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
298842263095SSreekanth Reddy 	rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
298942263095SSreekanth Reddy 
299042263095SSreekanth Reddy  out:
299142263095SSreekanth Reddy 	kfree(io_unit_pg3);
299208c4d550SSreekanth Reddy 	mutex_unlock(&ioc->pci_access_mutex);
299342263095SSreekanth Reddy 	return rc;
299442263095SSreekanth Reddy }
299542263095SSreekanth Reddy static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
299642263095SSreekanth Reddy 
2997f92363d1SSreekanth Reddy struct DIAG_BUFFER_START {
2998f92363d1SSreekanth Reddy 	__le32	Size;
2999f92363d1SSreekanth Reddy 	__le32	DiagVersion;
3000f92363d1SSreekanth Reddy 	u8	BufferType;
3001f92363d1SSreekanth Reddy 	u8	Reserved[3];
3002f92363d1SSreekanth Reddy 	__le32	Reserved1;
3003f92363d1SSreekanth Reddy 	__le32	Reserved2;
3004f92363d1SSreekanth Reddy 	__le32	Reserved3;
3005f92363d1SSreekanth Reddy };
3006f92363d1SSreekanth Reddy 
3007f92363d1SSreekanth Reddy /**
3008f92363d1SSreekanth Reddy  * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
3009f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3010f92363d1SSreekanth Reddy  * @buf - the buffer returned
3011f92363d1SSreekanth Reddy  *
3012f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3013f92363d1SSreekanth Reddy  */
3014f92363d1SSreekanth Reddy static ssize_t
3015f92363d1SSreekanth Reddy _ctl_host_trace_buffer_size_show(struct device *cdev,
3016f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3017f92363d1SSreekanth Reddy {
3018f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3019f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3020f92363d1SSreekanth Reddy 	u32 size = 0;
3021f92363d1SSreekanth Reddy 	struct DIAG_BUFFER_START *request_data;
3022f92363d1SSreekanth Reddy 
3023f92363d1SSreekanth Reddy 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3024f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
3025f92363d1SSreekanth Reddy 			"%s: host_trace_buffer is not registered\n",
3026f92363d1SSreekanth Reddy 			ioc->name, __func__);
3027f92363d1SSreekanth Reddy 		return 0;
3028f92363d1SSreekanth Reddy 	}
3029f92363d1SSreekanth Reddy 
3030f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3031f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3032f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
3033f92363d1SSreekanth Reddy 			"%s: host_trace_buffer is not registered\n",
3034f92363d1SSreekanth Reddy 			ioc->name, __func__);
3035f92363d1SSreekanth Reddy 		return 0;
3036f92363d1SSreekanth Reddy 	}
3037f92363d1SSreekanth Reddy 
3038f92363d1SSreekanth Reddy 	request_data = (struct DIAG_BUFFER_START *)
3039f92363d1SSreekanth Reddy 	    ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
3040f92363d1SSreekanth Reddy 	if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
3041f92363d1SSreekanth Reddy 	    le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
3042f92363d1SSreekanth Reddy 	    le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
3043f92363d1SSreekanth Reddy 	    le32_to_cpu(request_data->Reserved3) == 0x4742444c)
3044f92363d1SSreekanth Reddy 		size = le32_to_cpu(request_data->Size);
3045f92363d1SSreekanth Reddy 
3046f92363d1SSreekanth Reddy 	ioc->ring_buffer_sz = size;
3047f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "%d\n", size);
3048f92363d1SSreekanth Reddy }
3049f92363d1SSreekanth Reddy static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
3050f92363d1SSreekanth Reddy 	_ctl_host_trace_buffer_size_show, NULL);
3051f92363d1SSreekanth Reddy 
3052f92363d1SSreekanth Reddy /**
3053f92363d1SSreekanth Reddy  * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
3054f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3055f92363d1SSreekanth Reddy  * @buf - the buffer returned
3056f92363d1SSreekanth Reddy  *
3057f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3058f92363d1SSreekanth Reddy  *
3059f92363d1SSreekanth Reddy  * You will only be able to read 4k bytes of ring buffer at a time.
3060f92363d1SSreekanth Reddy  * In order to read beyond 4k bytes, you will have to write out the
3061f92363d1SSreekanth Reddy  * offset to the same attribute, it will move the pointer.
3062f92363d1SSreekanth Reddy  */
3063f92363d1SSreekanth Reddy static ssize_t
3064f92363d1SSreekanth Reddy _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
3065f92363d1SSreekanth Reddy 	char *buf)
3066f92363d1SSreekanth Reddy {
3067f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3068f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3069f92363d1SSreekanth Reddy 	void *request_data;
3070f92363d1SSreekanth Reddy 	u32 size;
3071f92363d1SSreekanth Reddy 
3072f92363d1SSreekanth Reddy 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3073f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
3074f92363d1SSreekanth Reddy 			"%s: host_trace_buffer is not registered\n",
3075f92363d1SSreekanth Reddy 			ioc->name, __func__);
3076f92363d1SSreekanth Reddy 		return 0;
3077f92363d1SSreekanth Reddy 	}
3078f92363d1SSreekanth Reddy 
3079f92363d1SSreekanth Reddy 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3080f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3081f92363d1SSreekanth Reddy 		pr_err(MPT3SAS_FMT
3082f92363d1SSreekanth Reddy 			"%s: host_trace_buffer is not registered\n",
3083f92363d1SSreekanth Reddy 			ioc->name, __func__);
3084f92363d1SSreekanth Reddy 		return 0;
3085f92363d1SSreekanth Reddy 	}
3086f92363d1SSreekanth Reddy 
3087f92363d1SSreekanth Reddy 	if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3088f92363d1SSreekanth Reddy 		return 0;
3089f92363d1SSreekanth Reddy 
3090f92363d1SSreekanth Reddy 	size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3091f92363d1SSreekanth Reddy 	size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3092f92363d1SSreekanth Reddy 	request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3093f92363d1SSreekanth Reddy 	memcpy(buf, request_data, size);
3094f92363d1SSreekanth Reddy 	return size;
3095f92363d1SSreekanth Reddy }
3096f92363d1SSreekanth Reddy 
3097f92363d1SSreekanth Reddy static ssize_t
3098f92363d1SSreekanth Reddy _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
3099f92363d1SSreekanth Reddy 	const char *buf, size_t count)
3100f92363d1SSreekanth Reddy {
3101f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3102f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3103f92363d1SSreekanth Reddy 	int val = 0;
3104f92363d1SSreekanth Reddy 
3105f92363d1SSreekanth Reddy 	if (sscanf(buf, "%d", &val) != 1)
3106f92363d1SSreekanth Reddy 		return -EINVAL;
3107f92363d1SSreekanth Reddy 
3108f92363d1SSreekanth Reddy 	ioc->ring_buffer_offset = val;
3109f92363d1SSreekanth Reddy 	return strlen(buf);
3110f92363d1SSreekanth Reddy }
3111f92363d1SSreekanth Reddy static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
3112f92363d1SSreekanth Reddy 	_ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
3113f92363d1SSreekanth Reddy 
3114f92363d1SSreekanth Reddy 
3115f92363d1SSreekanth Reddy /*****************************************/
3116f92363d1SSreekanth Reddy 
3117f92363d1SSreekanth Reddy /**
3118f92363d1SSreekanth Reddy  * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
3119f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3120f92363d1SSreekanth Reddy  * @buf - the buffer returned
3121f92363d1SSreekanth Reddy  *
3122f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3123f92363d1SSreekanth Reddy  *
3124f92363d1SSreekanth Reddy  * This is a mechnism to post/release host_trace_buffers
3125f92363d1SSreekanth Reddy  */
3126f92363d1SSreekanth Reddy static ssize_t
3127f92363d1SSreekanth Reddy _ctl_host_trace_buffer_enable_show(struct device *cdev,
3128f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3129f92363d1SSreekanth Reddy {
3130f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3131f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3132f92363d1SSreekanth Reddy 
3133f92363d1SSreekanth Reddy 	if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3134f92363d1SSreekanth Reddy 	   ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3135f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3136f92363d1SSreekanth Reddy 		return snprintf(buf, PAGE_SIZE, "off\n");
3137f92363d1SSreekanth Reddy 	else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3138f92363d1SSreekanth Reddy 	    MPT3_DIAG_BUFFER_IS_RELEASED))
3139f92363d1SSreekanth Reddy 		return snprintf(buf, PAGE_SIZE, "release\n");
3140f92363d1SSreekanth Reddy 	else
3141f92363d1SSreekanth Reddy 		return snprintf(buf, PAGE_SIZE, "post\n");
3142f92363d1SSreekanth Reddy }
3143f92363d1SSreekanth Reddy 
3144f92363d1SSreekanth Reddy static ssize_t
3145f92363d1SSreekanth Reddy _ctl_host_trace_buffer_enable_store(struct device *cdev,
3146f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3147f92363d1SSreekanth Reddy {
3148f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3149f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3150f92363d1SSreekanth Reddy 	char str[10] = "";
3151f92363d1SSreekanth Reddy 	struct mpt3_diag_register diag_register;
3152f92363d1SSreekanth Reddy 	u8 issue_reset = 0;
3153f92363d1SSreekanth Reddy 
3154f92363d1SSreekanth Reddy 	/* don't allow post/release occurr while recovery is active */
3155f92363d1SSreekanth Reddy 	if (ioc->shost_recovery || ioc->remove_host ||
3156f92363d1SSreekanth Reddy 	    ioc->pci_error_recovery || ioc->is_driver_loading)
3157f92363d1SSreekanth Reddy 		return -EBUSY;
3158f92363d1SSreekanth Reddy 
3159f92363d1SSreekanth Reddy 	if (sscanf(buf, "%9s", str) != 1)
3160f92363d1SSreekanth Reddy 		return -EINVAL;
3161f92363d1SSreekanth Reddy 
3162f92363d1SSreekanth Reddy 	if (!strcmp(str, "post")) {
3163f92363d1SSreekanth Reddy 		/* exit out if host buffers are already posted */
3164f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3165f92363d1SSreekanth Reddy 		    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3166f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3167f92363d1SSreekanth Reddy 		    ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3168f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3169f92363d1SSreekanth Reddy 			goto out;
3170f92363d1SSreekanth Reddy 		memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3171f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT "posting host trace buffers\n",
3172f92363d1SSreekanth Reddy 		    ioc->name);
3173f92363d1SSreekanth Reddy 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3174f92363d1SSreekanth Reddy 		diag_register.requested_buffer_size = (1024 * 1024);
3175f92363d1SSreekanth Reddy 		diag_register.unique_id = 0x7075900;
3176f92363d1SSreekanth Reddy 		ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3177f92363d1SSreekanth Reddy 		_ctl_diag_register_2(ioc,  &diag_register);
3178f92363d1SSreekanth Reddy 	} else if (!strcmp(str, "release")) {
3179f92363d1SSreekanth Reddy 		/* exit out if host buffers are already released */
3180f92363d1SSreekanth Reddy 		if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3181f92363d1SSreekanth Reddy 			goto out;
3182f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3183f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3184f92363d1SSreekanth Reddy 			goto out;
3185f92363d1SSreekanth Reddy 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3186f92363d1SSreekanth Reddy 		    MPT3_DIAG_BUFFER_IS_RELEASED))
3187f92363d1SSreekanth Reddy 			goto out;
3188f92363d1SSreekanth Reddy 		pr_info(MPT3SAS_FMT "releasing host trace buffer\n",
3189f92363d1SSreekanth Reddy 		    ioc->name);
3190f92363d1SSreekanth Reddy 		mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3191f92363d1SSreekanth Reddy 		    &issue_reset);
3192f92363d1SSreekanth Reddy 	}
3193f92363d1SSreekanth Reddy 
3194f92363d1SSreekanth Reddy  out:
3195f92363d1SSreekanth Reddy 	return strlen(buf);
3196f92363d1SSreekanth Reddy }
3197f92363d1SSreekanth Reddy static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
3198f92363d1SSreekanth Reddy 	_ctl_host_trace_buffer_enable_show,
3199f92363d1SSreekanth Reddy 	_ctl_host_trace_buffer_enable_store);
3200f92363d1SSreekanth Reddy 
3201f92363d1SSreekanth Reddy /*********** diagnostic trigger suppport *********************************/
3202f92363d1SSreekanth Reddy 
3203f92363d1SSreekanth Reddy /**
3204f92363d1SSreekanth Reddy  * _ctl_diag_trigger_master_show - show the diag_trigger_master attribute
3205f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3206f92363d1SSreekanth Reddy  * @buf - the buffer returned
3207f92363d1SSreekanth Reddy  *
3208f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3209f92363d1SSreekanth Reddy  */
3210f92363d1SSreekanth Reddy static ssize_t
3211f92363d1SSreekanth Reddy _ctl_diag_trigger_master_show(struct device *cdev,
3212f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3213f92363d1SSreekanth Reddy 
3214f92363d1SSreekanth Reddy {
3215f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3216f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3217f92363d1SSreekanth Reddy 	unsigned long flags;
3218f92363d1SSreekanth Reddy 	ssize_t rc;
3219f92363d1SSreekanth Reddy 
3220f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3221f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3222f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_master, rc);
3223f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3224f92363d1SSreekanth Reddy 	return rc;
3225f92363d1SSreekanth Reddy }
3226f92363d1SSreekanth Reddy 
3227f92363d1SSreekanth Reddy /**
3228f92363d1SSreekanth Reddy  * _ctl_diag_trigger_master_store - store the diag_trigger_master attribute
3229f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3230f92363d1SSreekanth Reddy  * @buf - the buffer returned
3231f92363d1SSreekanth Reddy  *
3232f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3233f92363d1SSreekanth Reddy  */
3234f92363d1SSreekanth Reddy static ssize_t
3235f92363d1SSreekanth Reddy _ctl_diag_trigger_master_store(struct device *cdev,
3236f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3237f92363d1SSreekanth Reddy 
3238f92363d1SSreekanth Reddy {
3239f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3240f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3241f92363d1SSreekanth Reddy 	unsigned long flags;
3242f92363d1SSreekanth Reddy 	ssize_t rc;
3243f92363d1SSreekanth Reddy 
3244f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3245f92363d1SSreekanth Reddy 	rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3246f92363d1SSreekanth Reddy 	memset(&ioc->diag_trigger_master, 0,
3247f92363d1SSreekanth Reddy 	    sizeof(struct SL_WH_MASTER_TRIGGER_T));
3248f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_master, buf, rc);
3249f92363d1SSreekanth Reddy 	ioc->diag_trigger_master.MasterData |=
3250f92363d1SSreekanth Reddy 	    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3251f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3252f92363d1SSreekanth Reddy 	return rc;
3253f92363d1SSreekanth Reddy }
3254f92363d1SSreekanth Reddy static DEVICE_ATTR(diag_trigger_master, S_IRUGO | S_IWUSR,
3255f92363d1SSreekanth Reddy 	_ctl_diag_trigger_master_show, _ctl_diag_trigger_master_store);
3256f92363d1SSreekanth Reddy 
3257f92363d1SSreekanth Reddy 
3258f92363d1SSreekanth Reddy /**
3259f92363d1SSreekanth Reddy  * _ctl_diag_trigger_event_show - show the diag_trigger_event attribute
3260f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3261f92363d1SSreekanth Reddy  * @buf - the buffer returned
3262f92363d1SSreekanth Reddy  *
3263f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3264f92363d1SSreekanth Reddy  */
3265f92363d1SSreekanth Reddy static ssize_t
3266f92363d1SSreekanth Reddy _ctl_diag_trigger_event_show(struct device *cdev,
3267f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3268f92363d1SSreekanth Reddy {
3269f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3270f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3271f92363d1SSreekanth Reddy 	unsigned long flags;
3272f92363d1SSreekanth Reddy 	ssize_t rc;
3273f92363d1SSreekanth Reddy 
3274f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3275f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3276f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_event, rc);
3277f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3278f92363d1SSreekanth Reddy 	return rc;
3279f92363d1SSreekanth Reddy }
3280f92363d1SSreekanth Reddy 
3281f92363d1SSreekanth Reddy /**
3282f92363d1SSreekanth Reddy  * _ctl_diag_trigger_event_store - store the diag_trigger_event attribute
3283f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3284f92363d1SSreekanth Reddy  * @buf - the buffer returned
3285f92363d1SSreekanth Reddy  *
3286f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3287f92363d1SSreekanth Reddy  */
3288f92363d1SSreekanth Reddy static ssize_t
3289f92363d1SSreekanth Reddy _ctl_diag_trigger_event_store(struct device *cdev,
3290f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3291f92363d1SSreekanth Reddy 
3292f92363d1SSreekanth Reddy {
3293f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3294f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3295f92363d1SSreekanth Reddy 	unsigned long flags;
3296f92363d1SSreekanth Reddy 	ssize_t sz;
3297f92363d1SSreekanth Reddy 
3298f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3299f92363d1SSreekanth Reddy 	sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3300f92363d1SSreekanth Reddy 	memset(&ioc->diag_trigger_event, 0,
3301f92363d1SSreekanth Reddy 	    sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3302f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_event, buf, sz);
3303f92363d1SSreekanth Reddy 	if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3304f92363d1SSreekanth Reddy 		ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3305f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3306f92363d1SSreekanth Reddy 	return sz;
3307f92363d1SSreekanth Reddy }
3308f92363d1SSreekanth Reddy static DEVICE_ATTR(diag_trigger_event, S_IRUGO | S_IWUSR,
3309f92363d1SSreekanth Reddy 	_ctl_diag_trigger_event_show, _ctl_diag_trigger_event_store);
3310f92363d1SSreekanth Reddy 
3311f92363d1SSreekanth Reddy 
3312f92363d1SSreekanth Reddy /**
3313f92363d1SSreekanth Reddy  * _ctl_diag_trigger_scsi_show - show the diag_trigger_scsi attribute
3314f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3315f92363d1SSreekanth Reddy  * @buf - the buffer returned
3316f92363d1SSreekanth Reddy  *
3317f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3318f92363d1SSreekanth Reddy  */
3319f92363d1SSreekanth Reddy static ssize_t
3320f92363d1SSreekanth Reddy _ctl_diag_trigger_scsi_show(struct device *cdev,
3321f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3322f92363d1SSreekanth Reddy {
3323f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3324f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3325f92363d1SSreekanth Reddy 	unsigned long flags;
3326f92363d1SSreekanth Reddy 	ssize_t rc;
3327f92363d1SSreekanth Reddy 
3328f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3329f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3330f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_scsi, rc);
3331f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3332f92363d1SSreekanth Reddy 	return rc;
3333f92363d1SSreekanth Reddy }
3334f92363d1SSreekanth Reddy 
3335f92363d1SSreekanth Reddy /**
3336f92363d1SSreekanth Reddy  * _ctl_diag_trigger_scsi_store - store the diag_trigger_scsi attribute
3337f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3338f92363d1SSreekanth Reddy  * @buf - the buffer returned
3339f92363d1SSreekanth Reddy  *
3340f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3341f92363d1SSreekanth Reddy  */
3342f92363d1SSreekanth Reddy static ssize_t
3343f92363d1SSreekanth Reddy _ctl_diag_trigger_scsi_store(struct device *cdev,
3344f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3345f92363d1SSreekanth Reddy {
3346f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3347f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3348f92363d1SSreekanth Reddy 	unsigned long flags;
3349f92363d1SSreekanth Reddy 	ssize_t sz;
3350f92363d1SSreekanth Reddy 
3351f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3352f92363d1SSreekanth Reddy 	sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
3353f92363d1SSreekanth Reddy 	memset(&ioc->diag_trigger_scsi, 0,
3354f92363d1SSreekanth Reddy 	    sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3355f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_scsi, buf, sz);
3356f92363d1SSreekanth Reddy 	if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3357f92363d1SSreekanth Reddy 		ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3358f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3359f92363d1SSreekanth Reddy 	return sz;
3360f92363d1SSreekanth Reddy }
3361f92363d1SSreekanth Reddy static DEVICE_ATTR(diag_trigger_scsi, S_IRUGO | S_IWUSR,
3362f92363d1SSreekanth Reddy 	_ctl_diag_trigger_scsi_show, _ctl_diag_trigger_scsi_store);
3363f92363d1SSreekanth Reddy 
3364f92363d1SSreekanth Reddy 
3365f92363d1SSreekanth Reddy /**
3366f92363d1SSreekanth Reddy  * _ctl_diag_trigger_scsi_show - show the diag_trigger_mpi attribute
3367f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3368f92363d1SSreekanth Reddy  * @buf - the buffer returned
3369f92363d1SSreekanth Reddy  *
3370f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3371f92363d1SSreekanth Reddy  */
3372f92363d1SSreekanth Reddy static ssize_t
3373f92363d1SSreekanth Reddy _ctl_diag_trigger_mpi_show(struct device *cdev,
3374f92363d1SSreekanth Reddy 	struct device_attribute *attr, char *buf)
3375f92363d1SSreekanth Reddy {
3376f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3377f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3378f92363d1SSreekanth Reddy 	unsigned long flags;
3379f92363d1SSreekanth Reddy 	ssize_t rc;
3380f92363d1SSreekanth Reddy 
3381f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3382f92363d1SSreekanth Reddy 	rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3383f92363d1SSreekanth Reddy 	memcpy(buf, &ioc->diag_trigger_mpi, rc);
3384f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3385f92363d1SSreekanth Reddy 	return rc;
3386f92363d1SSreekanth Reddy }
3387f92363d1SSreekanth Reddy 
3388f92363d1SSreekanth Reddy /**
3389f92363d1SSreekanth Reddy  * _ctl_diag_trigger_mpi_store - store the diag_trigger_mpi attribute
3390f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3391f92363d1SSreekanth Reddy  * @buf - the buffer returned
3392f92363d1SSreekanth Reddy  *
3393f92363d1SSreekanth Reddy  * A sysfs 'read/write' shost attribute.
3394f92363d1SSreekanth Reddy  */
3395f92363d1SSreekanth Reddy static ssize_t
3396f92363d1SSreekanth Reddy _ctl_diag_trigger_mpi_store(struct device *cdev,
3397f92363d1SSreekanth Reddy 	struct device_attribute *attr, const char *buf, size_t count)
3398f92363d1SSreekanth Reddy {
3399f92363d1SSreekanth Reddy 	struct Scsi_Host *shost = class_to_shost(cdev);
3400f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3401f92363d1SSreekanth Reddy 	unsigned long flags;
3402f92363d1SSreekanth Reddy 	ssize_t sz;
3403f92363d1SSreekanth Reddy 
3404f92363d1SSreekanth Reddy 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3405f92363d1SSreekanth Reddy 	sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3406f92363d1SSreekanth Reddy 	memset(&ioc->diag_trigger_mpi, 0,
340766331e8cSDan Carpenter 	    sizeof(ioc->diag_trigger_mpi));
3408f92363d1SSreekanth Reddy 	memcpy(&ioc->diag_trigger_mpi, buf, sz);
3409f92363d1SSreekanth Reddy 	if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3410f92363d1SSreekanth Reddy 		ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3411f92363d1SSreekanth Reddy 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3412f92363d1SSreekanth Reddy 	return sz;
3413f92363d1SSreekanth Reddy }
3414f92363d1SSreekanth Reddy 
3415f92363d1SSreekanth Reddy static DEVICE_ATTR(diag_trigger_mpi, S_IRUGO | S_IWUSR,
3416f92363d1SSreekanth Reddy 	_ctl_diag_trigger_mpi_show, _ctl_diag_trigger_mpi_store);
3417f92363d1SSreekanth Reddy 
3418f92363d1SSreekanth Reddy /*********** diagnostic trigger suppport *** END ****************************/
3419f92363d1SSreekanth Reddy 
3420f92363d1SSreekanth Reddy /*****************************************/
3421f92363d1SSreekanth Reddy 
3422f92363d1SSreekanth Reddy struct device_attribute *mpt3sas_host_attrs[] = {
3423f92363d1SSreekanth Reddy 	&dev_attr_version_fw,
3424f92363d1SSreekanth Reddy 	&dev_attr_version_bios,
3425f92363d1SSreekanth Reddy 	&dev_attr_version_mpi,
3426f92363d1SSreekanth Reddy 	&dev_attr_version_product,
3427f92363d1SSreekanth Reddy 	&dev_attr_version_nvdata_persistent,
3428f92363d1SSreekanth Reddy 	&dev_attr_version_nvdata_default,
3429f92363d1SSreekanth Reddy 	&dev_attr_board_name,
3430f92363d1SSreekanth Reddy 	&dev_attr_board_assembly,
3431f92363d1SSreekanth Reddy 	&dev_attr_board_tracer,
3432f92363d1SSreekanth Reddy 	&dev_attr_io_delay,
3433f92363d1SSreekanth Reddy 	&dev_attr_device_delay,
3434f92363d1SSreekanth Reddy 	&dev_attr_logging_level,
3435f92363d1SSreekanth Reddy 	&dev_attr_fwfault_debug,
3436f92363d1SSreekanth Reddy 	&dev_attr_fw_queue_depth,
3437f92363d1SSreekanth Reddy 	&dev_attr_host_sas_address,
3438f92363d1SSreekanth Reddy 	&dev_attr_ioc_reset_count,
3439f92363d1SSreekanth Reddy 	&dev_attr_host_trace_buffer_size,
3440f92363d1SSreekanth Reddy 	&dev_attr_host_trace_buffer,
3441f92363d1SSreekanth Reddy 	&dev_attr_host_trace_buffer_enable,
3442f92363d1SSreekanth Reddy 	&dev_attr_reply_queue_count,
3443f92363d1SSreekanth Reddy 	&dev_attr_diag_trigger_master,
3444f92363d1SSreekanth Reddy 	&dev_attr_diag_trigger_event,
3445f92363d1SSreekanth Reddy 	&dev_attr_diag_trigger_scsi,
3446f92363d1SSreekanth Reddy 	&dev_attr_diag_trigger_mpi,
344742263095SSreekanth Reddy 	&dev_attr_BRM_status,
3448f92363d1SSreekanth Reddy 	NULL,
3449f92363d1SSreekanth Reddy };
3450f92363d1SSreekanth Reddy 
3451f92363d1SSreekanth Reddy /* device attributes */
3452f92363d1SSreekanth Reddy 
3453f92363d1SSreekanth Reddy /**
3454f92363d1SSreekanth Reddy  * _ctl_device_sas_address_show - sas address
3455f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3456f92363d1SSreekanth Reddy  * @buf - the buffer returned
3457f92363d1SSreekanth Reddy  *
3458f92363d1SSreekanth Reddy  * This is the sas address for the target
3459f92363d1SSreekanth Reddy  *
3460f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3461f92363d1SSreekanth Reddy  */
3462f92363d1SSreekanth Reddy static ssize_t
3463f92363d1SSreekanth Reddy _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
3464f92363d1SSreekanth Reddy 	char *buf)
3465f92363d1SSreekanth Reddy {
3466f92363d1SSreekanth Reddy 	struct scsi_device *sdev = to_scsi_device(dev);
3467f92363d1SSreekanth Reddy 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3468f92363d1SSreekanth Reddy 
3469f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3470f92363d1SSreekanth Reddy 	    (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3471f92363d1SSreekanth Reddy }
3472f92363d1SSreekanth Reddy static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
3473f92363d1SSreekanth Reddy 
3474f92363d1SSreekanth Reddy /**
3475f92363d1SSreekanth Reddy  * _ctl_device_handle_show - device handle
3476f92363d1SSreekanth Reddy  * @cdev - pointer to embedded class device
3477f92363d1SSreekanth Reddy  * @buf - the buffer returned
3478f92363d1SSreekanth Reddy  *
3479f92363d1SSreekanth Reddy  * This is the firmware assigned device handle
3480f92363d1SSreekanth Reddy  *
3481f92363d1SSreekanth Reddy  * A sysfs 'read-only' shost attribute.
3482f92363d1SSreekanth Reddy  */
3483f92363d1SSreekanth Reddy static ssize_t
3484f92363d1SSreekanth Reddy _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
3485f92363d1SSreekanth Reddy 	char *buf)
3486f92363d1SSreekanth Reddy {
3487f92363d1SSreekanth Reddy 	struct scsi_device *sdev = to_scsi_device(dev);
3488f92363d1SSreekanth Reddy 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3489f92363d1SSreekanth Reddy 
3490f92363d1SSreekanth Reddy 	return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3491f92363d1SSreekanth Reddy 	    sas_device_priv_data->sas_target->handle);
3492f92363d1SSreekanth Reddy }
3493f92363d1SSreekanth Reddy static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
3494f92363d1SSreekanth Reddy 
3495307d9075SAdam Manzanares /**
3496307d9075SAdam Manzanares  * _ctl_device_ncq_io_prio_show - send prioritized io commands to device
3497307d9075SAdam Manzanares  * @dev - pointer to embedded device
3498307d9075SAdam Manzanares  * @buf - the buffer returned
3499307d9075SAdam Manzanares  *
3500307d9075SAdam Manzanares  * A sysfs 'read/write' sdev attribute, only works with SATA
3501307d9075SAdam Manzanares  */
3502307d9075SAdam Manzanares static ssize_t
3503307d9075SAdam Manzanares _ctl_device_ncq_prio_enable_show(struct device *dev,
3504307d9075SAdam Manzanares 				 struct device_attribute *attr, char *buf)
3505307d9075SAdam Manzanares {
3506307d9075SAdam Manzanares 	struct scsi_device *sdev = to_scsi_device(dev);
3507307d9075SAdam Manzanares 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3508307d9075SAdam Manzanares 
3509307d9075SAdam Manzanares 	return snprintf(buf, PAGE_SIZE, "%d\n",
3510307d9075SAdam Manzanares 			sas_device_priv_data->ncq_prio_enable);
3511307d9075SAdam Manzanares }
3512307d9075SAdam Manzanares 
3513307d9075SAdam Manzanares static ssize_t
3514307d9075SAdam Manzanares _ctl_device_ncq_prio_enable_store(struct device *dev,
3515307d9075SAdam Manzanares 				  struct device_attribute *attr,
3516307d9075SAdam Manzanares 				  const char *buf, size_t count)
3517307d9075SAdam Manzanares {
3518307d9075SAdam Manzanares 	struct scsi_device *sdev = to_scsi_device(dev);
3519307d9075SAdam Manzanares 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3520307d9075SAdam Manzanares 	bool ncq_prio_enable = 0;
3521307d9075SAdam Manzanares 
3522307d9075SAdam Manzanares 	if (kstrtobool(buf, &ncq_prio_enable))
3523307d9075SAdam Manzanares 		return -EINVAL;
3524307d9075SAdam Manzanares 
3525307d9075SAdam Manzanares 	if (!scsih_ncq_prio_supp(sdev))
3526307d9075SAdam Manzanares 		return -EINVAL;
3527307d9075SAdam Manzanares 
3528307d9075SAdam Manzanares 	sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
3529307d9075SAdam Manzanares 	return strlen(buf);
3530307d9075SAdam Manzanares }
3531307d9075SAdam Manzanares static DEVICE_ATTR(sas_ncq_prio_enable, S_IRUGO | S_IWUSR,
3532307d9075SAdam Manzanares 		   _ctl_device_ncq_prio_enable_show,
3533307d9075SAdam Manzanares 		   _ctl_device_ncq_prio_enable_store);
3534307d9075SAdam Manzanares 
3535f92363d1SSreekanth Reddy struct device_attribute *mpt3sas_dev_attrs[] = {
3536f92363d1SSreekanth Reddy 	&dev_attr_sas_address,
3537f92363d1SSreekanth Reddy 	&dev_attr_sas_device_handle,
3538307d9075SAdam Manzanares 	&dev_attr_sas_ncq_prio_enable,
3539f92363d1SSreekanth Reddy 	NULL,
3540f92363d1SSreekanth Reddy };
3541f92363d1SSreekanth Reddy 
3542c84b06a4SSreekanth Reddy /* file operations table for mpt3ctl device */
3543c84b06a4SSreekanth Reddy static const struct file_operations ctl_fops = {
3544c84b06a4SSreekanth Reddy 	.owner = THIS_MODULE,
3545c84b06a4SSreekanth Reddy 	.unlocked_ioctl = _ctl_ioctl,
3546c84b06a4SSreekanth Reddy 	.poll = _ctl_poll,
3547c84b06a4SSreekanth Reddy 	.fasync = _ctl_fasync,
3548c84b06a4SSreekanth Reddy #ifdef CONFIG_COMPAT
3549c84b06a4SSreekanth Reddy 	.compat_ioctl = _ctl_ioctl_compat,
3550c84b06a4SSreekanth Reddy #endif
3551c84b06a4SSreekanth Reddy };
3552c84b06a4SSreekanth Reddy 
3553c84b06a4SSreekanth Reddy /* file operations table for mpt2ctl device */
3554c84b06a4SSreekanth Reddy static const struct file_operations ctl_gen2_fops = {
3555c84b06a4SSreekanth Reddy 	.owner = THIS_MODULE,
3556c84b06a4SSreekanth Reddy 	.unlocked_ioctl = _ctl_mpt2_ioctl,
3557c84b06a4SSreekanth Reddy 	.poll = _ctl_poll,
3558c84b06a4SSreekanth Reddy 	.fasync = _ctl_fasync,
3559c84b06a4SSreekanth Reddy #ifdef CONFIG_COMPAT
3560c84b06a4SSreekanth Reddy 	.compat_ioctl = _ctl_mpt2_ioctl_compat,
3561c84b06a4SSreekanth Reddy #endif
3562c84b06a4SSreekanth Reddy };
3563c84b06a4SSreekanth Reddy 
3564c84b06a4SSreekanth Reddy static struct miscdevice ctl_dev = {
3565c84b06a4SSreekanth Reddy 	.minor  = MPT3SAS_MINOR,
3566c84b06a4SSreekanth Reddy 	.name   = MPT3SAS_DEV_NAME,
3567c84b06a4SSreekanth Reddy 	.fops   = &ctl_fops,
3568c84b06a4SSreekanth Reddy };
3569c84b06a4SSreekanth Reddy 
3570c84b06a4SSreekanth Reddy static struct miscdevice gen2_ctl_dev = {
3571c84b06a4SSreekanth Reddy 	.minor  = MPT2SAS_MINOR,
3572c84b06a4SSreekanth Reddy 	.name   = MPT2SAS_DEV_NAME,
3573c84b06a4SSreekanth Reddy 	.fops   = &ctl_gen2_fops,
3574c84b06a4SSreekanth Reddy };
3575c84b06a4SSreekanth Reddy 
3576f92363d1SSreekanth Reddy /**
3577c84b06a4SSreekanth Reddy  * mpt3sas_ctl_init - main entry point for ctl.
3578f92363d1SSreekanth Reddy  *
3579f92363d1SSreekanth Reddy  */
3580f92363d1SSreekanth Reddy void
3581c84b06a4SSreekanth Reddy mpt3sas_ctl_init(ushort hbas_to_enumerate)
3582f92363d1SSreekanth Reddy {
3583f92363d1SSreekanth Reddy 	async_queue = NULL;
3584c84b06a4SSreekanth Reddy 
3585c84b06a4SSreekanth Reddy 	/* Don't register mpt3ctl ioctl device if
3586c84b06a4SSreekanth Reddy 	 * hbas_to_enumarate is one.
3587c84b06a4SSreekanth Reddy 	 */
3588c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 1)
3589c84b06a4SSreekanth Reddy 		if (misc_register(&ctl_dev) < 0)
3590c84b06a4SSreekanth Reddy 			pr_err("%s can't register misc device [minor=%d]\n",
3591c84b06a4SSreekanth Reddy 			    MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3592c84b06a4SSreekanth Reddy 
3593c84b06a4SSreekanth Reddy 	/* Don't register mpt3ctl ioctl device if
3594c84b06a4SSreekanth Reddy 	 * hbas_to_enumarate is two.
3595c84b06a4SSreekanth Reddy 	 */
3596c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 2)
3597c84b06a4SSreekanth Reddy 		if (misc_register(&gen2_ctl_dev) < 0)
3598c84b06a4SSreekanth Reddy 			pr_err("%s can't register misc device [minor=%d]\n",
3599c84b06a4SSreekanth Reddy 			    MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3600c84b06a4SSreekanth Reddy 
3601f92363d1SSreekanth Reddy 	init_waitqueue_head(&ctl_poll_wait);
3602f92363d1SSreekanth Reddy }
3603f92363d1SSreekanth Reddy 
3604f92363d1SSreekanth Reddy /**
3605c84b06a4SSreekanth Reddy  * mpt3sas_ctl_exit - exit point for ctl
3606f92363d1SSreekanth Reddy  *
3607f92363d1SSreekanth Reddy  */
3608f92363d1SSreekanth Reddy void
3609c84b06a4SSreekanth Reddy mpt3sas_ctl_exit(ushort hbas_to_enumerate)
3610f92363d1SSreekanth Reddy {
3611f92363d1SSreekanth Reddy 	struct MPT3SAS_ADAPTER *ioc;
3612f92363d1SSreekanth Reddy 	int i;
3613f92363d1SSreekanth Reddy 
3614f92363d1SSreekanth Reddy 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3615f92363d1SSreekanth Reddy 
3616f92363d1SSreekanth Reddy 		/* free memory associated to diag buffers */
3617f92363d1SSreekanth Reddy 		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3618f92363d1SSreekanth Reddy 			if (!ioc->diag_buffer[i])
3619f92363d1SSreekanth Reddy 				continue;
3620f92363d1SSreekanth Reddy 			if (!(ioc->diag_buffer_status[i] &
3621f92363d1SSreekanth Reddy 			    MPT3_DIAG_BUFFER_IS_REGISTERED))
3622f92363d1SSreekanth Reddy 				continue;
3623f92363d1SSreekanth Reddy 			if ((ioc->diag_buffer_status[i] &
3624f92363d1SSreekanth Reddy 			    MPT3_DIAG_BUFFER_IS_RELEASED))
3625f92363d1SSreekanth Reddy 				continue;
3626f92363d1SSreekanth Reddy 			pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3627f92363d1SSreekanth Reddy 			ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3628f92363d1SSreekanth Reddy 			ioc->diag_buffer[i] = NULL;
3629f92363d1SSreekanth Reddy 			ioc->diag_buffer_status[i] = 0;
3630f92363d1SSreekanth Reddy 		}
3631f92363d1SSreekanth Reddy 
3632f92363d1SSreekanth Reddy 		kfree(ioc->event_log);
3633f92363d1SSreekanth Reddy 	}
3634c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 1)
3635c84b06a4SSreekanth Reddy 		misc_deregister(&ctl_dev);
3636c84b06a4SSreekanth Reddy 	if (hbas_to_enumerate != 2)
3637c84b06a4SSreekanth Reddy 		misc_deregister(&gen2_ctl_dev);
3638f92363d1SSreekanth Reddy }
3639