1 /*
2  * Management Module Support for MPT (Message Passing Technology) based
3  * controllers
4  *
5  * This code is based on drivers/scsi/mpt3sas/mpt3sas_ctl.c
6  * Copyright (C) 2012-2014  LSI Corporation
7  * Copyright (C) 2013-2014 Avago Technologies
8  *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * NO WARRANTY
21  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25  * solely responsible for determining the appropriateness of using and
26  * distributing the Program and assumes all risks associated with its
27  * exercise of rights under this Agreement, including but not limited to
28  * the risks and costs of program errors, damage to or loss of data,
29  * programs or equipment, and unavailability or interruption of operations.
30 
31  * DISCLAIMER OF LIABILITY
32  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39 
40  * You should have received a copy of the GNU General Public License
41  * along with this program; if not, write to the Free Software
42  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
43  * USA.
44  */
45 
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/slab.h>
51 #include <linux/types.h>
52 #include <linux/pci.h>
53 #include <linux/delay.h>
54 #include <linux/compat.h>
55 #include <linux/poll.h>
56 
57 #include <linux/io.h>
58 #include <linux/uaccess.h>
59 
60 #include "mpt3sas_base.h"
61 #include "mpt3sas_ctl.h"
62 
63 
64 static struct fasync_struct *async_queue;
65 static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
66 
67 
68 /**
69  * enum block_state - blocking state
70  * @NON_BLOCKING: non blocking
71  * @BLOCKING: blocking
72  *
73  * These states are for ioctls that need to wait for a response
74  * from firmware, so they probably require sleep.
75  */
76 enum block_state {
77 	NON_BLOCKING,
78 	BLOCKING,
79 };
80 
81 /**
82  * _ctl_display_some_debug - debug routine
83  * @ioc: per adapter object
84  * @smid: system request message index
85  * @calling_function_name: string pass from calling function
86  * @mpi_reply: reply message frame
87  * Context: none.
88  *
89  * Function for displaying debug info helpful when debugging issues
90  * in this module.
91  */
92 static void
93 _ctl_display_some_debug(struct MPT3SAS_ADAPTER *ioc, u16 smid,
94 	char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
95 {
96 	Mpi2ConfigRequest_t *mpi_request;
97 	char *desc = NULL;
98 
99 	if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
100 		return;
101 
102 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
103 	switch (mpi_request->Function) {
104 	case MPI2_FUNCTION_SCSI_IO_REQUEST:
105 	{
106 		Mpi2SCSIIORequest_t *scsi_request =
107 		    (Mpi2SCSIIORequest_t *)mpi_request;
108 
109 		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
110 		    "scsi_io, cmd(0x%02x), cdb_len(%d)",
111 		    scsi_request->CDB.CDB32[0],
112 		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
113 		desc = ioc->tmp_string;
114 		break;
115 	}
116 	case MPI2_FUNCTION_SCSI_TASK_MGMT:
117 		desc = "task_mgmt";
118 		break;
119 	case MPI2_FUNCTION_IOC_INIT:
120 		desc = "ioc_init";
121 		break;
122 	case MPI2_FUNCTION_IOC_FACTS:
123 		desc = "ioc_facts";
124 		break;
125 	case MPI2_FUNCTION_CONFIG:
126 	{
127 		Mpi2ConfigRequest_t *config_request =
128 		    (Mpi2ConfigRequest_t *)mpi_request;
129 
130 		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
131 		    "config, type(0x%02x), ext_type(0x%02x), number(%d)",
132 		    (config_request->Header.PageType &
133 		     MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
134 		    config_request->Header.PageNumber);
135 		desc = ioc->tmp_string;
136 		break;
137 	}
138 	case MPI2_FUNCTION_PORT_FACTS:
139 		desc = "port_facts";
140 		break;
141 	case MPI2_FUNCTION_PORT_ENABLE:
142 		desc = "port_enable";
143 		break;
144 	case MPI2_FUNCTION_EVENT_NOTIFICATION:
145 		desc = "event_notification";
146 		break;
147 	case MPI2_FUNCTION_FW_DOWNLOAD:
148 		desc = "fw_download";
149 		break;
150 	case MPI2_FUNCTION_FW_UPLOAD:
151 		desc = "fw_upload";
152 		break;
153 	case MPI2_FUNCTION_RAID_ACTION:
154 		desc = "raid_action";
155 		break;
156 	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
157 	{
158 		Mpi2SCSIIORequest_t *scsi_request =
159 		    (Mpi2SCSIIORequest_t *)mpi_request;
160 
161 		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
162 		    "raid_pass, cmd(0x%02x), cdb_len(%d)",
163 		    scsi_request->CDB.CDB32[0],
164 		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
165 		desc = ioc->tmp_string;
166 		break;
167 	}
168 	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
169 		desc = "sas_iounit_cntl";
170 		break;
171 	case MPI2_FUNCTION_SATA_PASSTHROUGH:
172 		desc = "sata_pass";
173 		break;
174 	case MPI2_FUNCTION_DIAG_BUFFER_POST:
175 		desc = "diag_buffer_post";
176 		break;
177 	case MPI2_FUNCTION_DIAG_RELEASE:
178 		desc = "diag_release";
179 		break;
180 	case MPI2_FUNCTION_SMP_PASSTHROUGH:
181 		desc = "smp_passthrough";
182 		break;
183 	case MPI2_FUNCTION_TOOLBOX:
184 		desc = "toolbox";
185 		break;
186 	case MPI2_FUNCTION_NVME_ENCAPSULATED:
187 		desc = "nvme_encapsulated";
188 		break;
189 	}
190 
191 	if (!desc)
192 		return;
193 
194 	ioc_info(ioc, "%s: %s, smid(%d)\n", calling_function_name, desc, smid);
195 
196 	if (!mpi_reply)
197 		return;
198 
199 	if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
200 		ioc_info(ioc, "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
201 			 le16_to_cpu(mpi_reply->IOCStatus),
202 			 le32_to_cpu(mpi_reply->IOCLogInfo));
203 
204 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
205 	    mpi_request->Function ==
206 	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
207 		Mpi2SCSIIOReply_t *scsi_reply =
208 		    (Mpi2SCSIIOReply_t *)mpi_reply;
209 		struct _sas_device *sas_device = NULL;
210 		struct _pcie_device *pcie_device = NULL;
211 
212 		sas_device = mpt3sas_get_sdev_by_handle(ioc,
213 		    le16_to_cpu(scsi_reply->DevHandle));
214 		if (sas_device) {
215 			ioc_warn(ioc, "\tsas_address(0x%016llx), phy(%d)\n",
216 				 (u64)sas_device->sas_address,
217 				 sas_device->phy);
218 			ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
219 				 (u64)sas_device->enclosure_logical_id,
220 				 sas_device->slot);
221 			sas_device_put(sas_device);
222 		}
223 		if (!sas_device) {
224 			pcie_device = mpt3sas_get_pdev_by_handle(ioc,
225 				le16_to_cpu(scsi_reply->DevHandle));
226 			if (pcie_device) {
227 				ioc_warn(ioc, "\tWWID(0x%016llx), port(%d)\n",
228 					 (unsigned long long)pcie_device->wwid,
229 					 pcie_device->port_num);
230 				if (pcie_device->enclosure_handle != 0)
231 					ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
232 						 (u64)pcie_device->enclosure_logical_id,
233 						 pcie_device->slot);
234 				pcie_device_put(pcie_device);
235 			}
236 		}
237 		if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
238 			ioc_info(ioc, "\tscsi_state(0x%02x), scsi_status(0x%02x)\n",
239 				 scsi_reply->SCSIState,
240 				 scsi_reply->SCSIStatus);
241 	}
242 }
243 
244 /**
245  * mpt3sas_ctl_done - ctl module completion routine
246  * @ioc: per adapter object
247  * @smid: system request message index
248  * @msix_index: MSIX table index supplied by the OS
249  * @reply: reply message frame(lower 32bit addr)
250  * Context: none.
251  *
252  * The callback handler when using ioc->ctl_cb_idx.
253  *
254  * Return: 1 meaning mf should be freed from _base_interrupt
255  *         0 means the mf is freed from this function.
256  */
257 u8
258 mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
259 	u32 reply)
260 {
261 	MPI2DefaultReply_t *mpi_reply;
262 	Mpi2SCSIIOReply_t *scsiio_reply;
263 	Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
264 	const void *sense_data;
265 	u32 sz;
266 
267 	if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
268 		return 1;
269 	if (ioc->ctl_cmds.smid != smid)
270 		return 1;
271 	ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
272 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
273 	if (mpi_reply) {
274 		memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
275 		ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
276 		/* get sense data */
277 		if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
278 		    mpi_reply->Function ==
279 		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
280 			scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
281 			if (scsiio_reply->SCSIState &
282 			    MPI2_SCSI_STATE_AUTOSENSE_VALID) {
283 				sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
284 				    le32_to_cpu(scsiio_reply->SenseCount));
285 				sense_data = mpt3sas_base_get_sense_buffer(ioc,
286 				    smid);
287 				memcpy(ioc->ctl_cmds.sense, sense_data, sz);
288 			}
289 		}
290 		/*
291 		 * Get Error Response data for NVMe device. The ctl_cmds.sense
292 		 * buffer is used to store the Error Response data.
293 		 */
294 		if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
295 			nvme_error_reply =
296 			    (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
297 			sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
298 			    le16_to_cpu(nvme_error_reply->ErrorResponseCount));
299 			sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
300 			memcpy(ioc->ctl_cmds.sense, sense_data, sz);
301 		}
302 	}
303 
304 	_ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
305 	ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
306 	complete(&ioc->ctl_cmds.done);
307 	return 1;
308 }
309 
310 /**
311  * _ctl_check_event_type - determines when an event needs logging
312  * @ioc: per adapter object
313  * @event: firmware event
314  *
315  * The bitmask in ioc->event_type[] indicates which events should be
316  * be saved in the driver event_log.  This bitmask is set by application.
317  *
318  * Return: 1 when event should be captured, or zero means no match.
319  */
320 static int
321 _ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
322 {
323 	u16 i;
324 	u32 desired_event;
325 
326 	if (event >= 128 || !event || !ioc->event_log)
327 		return 0;
328 
329 	desired_event = (1 << (event % 32));
330 	if (!desired_event)
331 		desired_event = 1;
332 	i = event / 32;
333 	return desired_event & ioc->event_type[i];
334 }
335 
336 /**
337  * mpt3sas_ctl_add_to_event_log - add event
338  * @ioc: per adapter object
339  * @mpi_reply: reply message frame
340  */
341 void
342 mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
343 	Mpi2EventNotificationReply_t *mpi_reply)
344 {
345 	struct MPT3_IOCTL_EVENTS *event_log;
346 	u16 event;
347 	int i;
348 	u32 sz, event_data_sz;
349 	u8 send_aen = 0;
350 
351 	if (!ioc->event_log)
352 		return;
353 
354 	event = le16_to_cpu(mpi_reply->Event);
355 
356 	if (_ctl_check_event_type(ioc, event)) {
357 
358 		/* insert entry into circular event_log */
359 		i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
360 		event_log = ioc->event_log;
361 		event_log[i].event = event;
362 		event_log[i].context = ioc->event_context++;
363 
364 		event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
365 		sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
366 		memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
367 		memcpy(event_log[i].data, mpi_reply->EventData, sz);
368 		send_aen = 1;
369 	}
370 
371 	/* This aen_event_read_flag flag is set until the
372 	 * application has read the event log.
373 	 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
374 	 */
375 	if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
376 	    (send_aen && !ioc->aen_event_read_flag)) {
377 		ioc->aen_event_read_flag = 1;
378 		wake_up_interruptible(&ctl_poll_wait);
379 		if (async_queue)
380 			kill_fasync(&async_queue, SIGIO, POLL_IN);
381 	}
382 }
383 
384 /**
385  * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
386  * @ioc: per adapter object
387  * @msix_index: MSIX table index supplied by the OS
388  * @reply: reply message frame(lower 32bit addr)
389  * Context: interrupt.
390  *
391  * This function merely adds a new work task into ioc->firmware_event_thread.
392  * The tasks are worked from _firmware_event_work in user context.
393  *
394  * Return: 1 meaning mf should be freed from _base_interrupt
395  *         0 means the mf is freed from this function.
396  */
397 u8
398 mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
399 	u32 reply)
400 {
401 	Mpi2EventNotificationReply_t *mpi_reply;
402 
403 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
404 	if (mpi_reply)
405 		mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
406 	return 1;
407 }
408 
409 /**
410  * _ctl_verify_adapter - validates ioc_number passed from application
411  * @ioc_number: ?
412  * @iocpp: The ioc pointer is returned in this.
413  * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
414  * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
415  *
416  * Return: (-1) means error, else ioc_number.
417  */
418 static int
419 _ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
420 							int mpi_version)
421 {
422 	struct MPT3SAS_ADAPTER *ioc;
423 	int version = 0;
424 	/* global ioc lock to protect controller on list operations */
425 	spin_lock(&gioc_lock);
426 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
427 		if (ioc->id != ioc_number)
428 			continue;
429 		/* Check whether this ioctl command is from right
430 		 * ioctl device or not, if not continue the search.
431 		 */
432 		version = ioc->hba_mpi_version_belonged;
433 		/* MPI25_VERSION and MPI26_VERSION uses same ioctl
434 		 * device.
435 		 */
436 		if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
437 			if ((version == MPI25_VERSION) ||
438 				(version == MPI26_VERSION))
439 				goto out;
440 			else
441 				continue;
442 		} else {
443 			if (version != mpi_version)
444 				continue;
445 		}
446 out:
447 		spin_unlock(&gioc_lock);
448 		*iocpp = ioc;
449 		return ioc_number;
450 	}
451 	spin_unlock(&gioc_lock);
452 	*iocpp = NULL;
453 	return -1;
454 }
455 
456 /**
457  * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
458  * @ioc: per adapter object
459  *
460  * The handler for doing any required cleanup or initialization.
461  */
462 void mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
463 {
464 	int i;
465 	u8 issue_reset;
466 
467 	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
468 	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
469 		if (!(ioc->diag_buffer_status[i] &
470 		      MPT3_DIAG_BUFFER_IS_REGISTERED))
471 			continue;
472 		if ((ioc->diag_buffer_status[i] &
473 		     MPT3_DIAG_BUFFER_IS_RELEASED))
474 			continue;
475 
476 		/*
477 		 * add a log message to indicate the release
478 		 */
479 		ioc_info(ioc,
480 		    "%s: Releasing the trace buffer due to adapter reset.",
481 		    __func__);
482 		mpt3sas_send_diag_release(ioc, i, &issue_reset);
483 	}
484 }
485 
486 /**
487  * mpt3sas_ctl_reset_handler - clears outstanding ioctl cmd.
488  * @ioc: per adapter object
489  *
490  * The handler for doing any required cleanup or initialization.
491  */
492 void mpt3sas_ctl_clear_outstanding_ioctls(struct MPT3SAS_ADAPTER *ioc)
493 {
494 	dtmprintk(ioc,
495 	    ioc_info(ioc, "%s: clear outstanding ioctl cmd\n", __func__));
496 	if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
497 		ioc->ctl_cmds.status |= MPT3_CMD_RESET;
498 		mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
499 		complete(&ioc->ctl_cmds.done);
500 	}
501 }
502 
503 /**
504  * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
505  * @ioc: per adapter object
506  *
507  * The handler for doing any required cleanup or initialization.
508  */
509 void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
510 {
511 	int i;
512 
513 	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
514 
515 	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
516 		if (!(ioc->diag_buffer_status[i] &
517 		      MPT3_DIAG_BUFFER_IS_REGISTERED))
518 			continue;
519 		if ((ioc->diag_buffer_status[i] &
520 		     MPT3_DIAG_BUFFER_IS_RELEASED))
521 			continue;
522 		ioc->diag_buffer_status[i] |=
523 			MPT3_DIAG_BUFFER_IS_DIAG_RESET;
524 	}
525 }
526 
527 /**
528  * _ctl_fasync -
529  * @fd: ?
530  * @filep: ?
531  * @mode: ?
532  *
533  * Called when application request fasyn callback handler.
534  */
535 static int
536 _ctl_fasync(int fd, struct file *filep, int mode)
537 {
538 	return fasync_helper(fd, filep, mode, &async_queue);
539 }
540 
541 /**
542  * _ctl_poll -
543  * @filep: ?
544  * @wait: ?
545  *
546  */
547 static __poll_t
548 _ctl_poll(struct file *filep, poll_table *wait)
549 {
550 	struct MPT3SAS_ADAPTER *ioc;
551 
552 	poll_wait(filep, &ctl_poll_wait, wait);
553 
554 	/* global ioc lock to protect controller on list operations */
555 	spin_lock(&gioc_lock);
556 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
557 		if (ioc->aen_event_read_flag) {
558 			spin_unlock(&gioc_lock);
559 			return EPOLLIN | EPOLLRDNORM;
560 		}
561 	}
562 	spin_unlock(&gioc_lock);
563 	return 0;
564 }
565 
566 /**
567  * _ctl_set_task_mid - assign an active smid to tm request
568  * @ioc: per adapter object
569  * @karg: (struct mpt3_ioctl_command)
570  * @tm_request: pointer to mf from user space
571  *
572  * Return: 0 when an smid if found, else fail.
573  * during failure, the reply frame is filled.
574  */
575 static int
576 _ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
577 	Mpi2SCSITaskManagementRequest_t *tm_request)
578 {
579 	u8 found = 0;
580 	u16 smid;
581 	u16 handle;
582 	struct scsi_cmnd *scmd;
583 	struct MPT3SAS_DEVICE *priv_data;
584 	Mpi2SCSITaskManagementReply_t *tm_reply;
585 	u32 sz;
586 	u32 lun;
587 	char *desc = NULL;
588 
589 	if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
590 		desc = "abort_task";
591 	else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
592 		desc = "query_task";
593 	else
594 		return 0;
595 
596 	lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
597 
598 	handle = le16_to_cpu(tm_request->DevHandle);
599 	for (smid = ioc->scsiio_depth; smid && !found; smid--) {
600 		struct scsiio_tracker *st;
601 
602 		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
603 		if (!scmd)
604 			continue;
605 		if (lun != scmd->device->lun)
606 			continue;
607 		priv_data = scmd->device->hostdata;
608 		if (priv_data->sas_target == NULL)
609 			continue;
610 		if (priv_data->sas_target->handle != handle)
611 			continue;
612 		st = scsi_cmd_priv(scmd);
613 
614 		/*
615 		 * If the given TaskMID from the user space is zero, then the
616 		 * first outstanding smid will be picked up.  Otherwise,
617 		 * targeted smid will be the one.
618 		 */
619 		if (!tm_request->TaskMID || tm_request->TaskMID == st->smid) {
620 			tm_request->TaskMID = cpu_to_le16(st->smid);
621 			found = 1;
622 		}
623 	}
624 
625 	if (!found) {
626 		dctlprintk(ioc,
627 			   ioc_info(ioc, "%s: handle(0x%04x), lun(%d), no active mid!!\n",
628 				    desc, le16_to_cpu(tm_request->DevHandle),
629 				    lun));
630 		tm_reply = ioc->ctl_cmds.reply;
631 		tm_reply->DevHandle = tm_request->DevHandle;
632 		tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
633 		tm_reply->TaskType = tm_request->TaskType;
634 		tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
635 		tm_reply->VP_ID = tm_request->VP_ID;
636 		tm_reply->VF_ID = tm_request->VF_ID;
637 		sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
638 		if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
639 		    sz))
640 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
641 			    __LINE__, __func__);
642 		return 1;
643 	}
644 
645 	dctlprintk(ioc,
646 		   ioc_info(ioc, "%s: handle(0x%04x), lun(%d), task_mid(%d)\n",
647 			    desc, le16_to_cpu(tm_request->DevHandle), lun,
648 			    le16_to_cpu(tm_request->TaskMID)));
649 	return 0;
650 }
651 
652 /**
653  * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
654  * @ioc: per adapter object
655  * @karg: (struct mpt3_ioctl_command)
656  * @mf: pointer to mf in user space
657  */
658 static long
659 _ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
660 	void __user *mf)
661 {
662 	MPI2RequestHeader_t *mpi_request = NULL, *request;
663 	MPI2DefaultReply_t *mpi_reply;
664 	Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
665 	struct _pcie_device *pcie_device = NULL;
666 	u16 smid;
667 	unsigned long timeout;
668 	u8 issue_reset;
669 	u32 sz, sz_arg;
670 	void *psge;
671 	void *data_out = NULL;
672 	dma_addr_t data_out_dma = 0;
673 	size_t data_out_sz = 0;
674 	void *data_in = NULL;
675 	dma_addr_t data_in_dma = 0;
676 	size_t data_in_sz = 0;
677 	long ret;
678 	u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
679 
680 	issue_reset = 0;
681 
682 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
683 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
684 		ret = -EAGAIN;
685 		goto out;
686 	}
687 
688 	ret = mpt3sas_wait_for_ioc(ioc,	IOC_OPERATIONAL_WAIT_COUNT);
689 	if (ret)
690 		goto out;
691 
692 	mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
693 	if (!mpi_request) {
694 		ioc_err(ioc, "%s: failed obtaining a memory for mpi_request\n",
695 			__func__);
696 		ret = -ENOMEM;
697 		goto out;
698 	}
699 
700 	/* Check for overflow and wraparound */
701 	if (karg.data_sge_offset * 4 > ioc->request_sz ||
702 	    karg.data_sge_offset > (UINT_MAX / 4)) {
703 		ret = -EINVAL;
704 		goto out;
705 	}
706 
707 	/* copy in request message frame from user */
708 	if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
709 		pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
710 		    __func__);
711 		ret = -EFAULT;
712 		goto out;
713 	}
714 
715 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
716 		smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
717 		if (!smid) {
718 			ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
719 			ret = -EAGAIN;
720 			goto out;
721 		}
722 	} else {
723 		/* Use first reserved smid for passthrough ioctls */
724 		smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
725 	}
726 
727 	ret = 0;
728 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
729 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
730 	request = mpt3sas_base_get_msg_frame(ioc, smid);
731 	memset(request, 0, ioc->request_sz);
732 	memcpy(request, mpi_request, karg.data_sge_offset*4);
733 	ioc->ctl_cmds.smid = smid;
734 	data_out_sz = karg.data_out_size;
735 	data_in_sz = karg.data_in_size;
736 
737 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
738 	    mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
739 	    mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
740 	    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
741 	    mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
742 
743 		device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
744 		if (!device_handle || (device_handle >
745 		    ioc->facts.MaxDevHandle)) {
746 			ret = -EINVAL;
747 			mpt3sas_base_free_smid(ioc, smid);
748 			goto out;
749 		}
750 	}
751 
752 	/* obtain dma-able memory for data transfer */
753 	if (data_out_sz) /* WRITE */ {
754 		data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz,
755 				&data_out_dma, GFP_KERNEL);
756 		if (!data_out) {
757 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
758 			    __LINE__, __func__);
759 			ret = -ENOMEM;
760 			mpt3sas_base_free_smid(ioc, smid);
761 			goto out;
762 		}
763 		if (copy_from_user(data_out, karg.data_out_buf_ptr,
764 			data_out_sz)) {
765 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
766 			    __LINE__, __func__);
767 			ret =  -EFAULT;
768 			mpt3sas_base_free_smid(ioc, smid);
769 			goto out;
770 		}
771 	}
772 
773 	if (data_in_sz) /* READ */ {
774 		data_in = dma_alloc_coherent(&ioc->pdev->dev, data_in_sz,
775 				&data_in_dma, GFP_KERNEL);
776 		if (!data_in) {
777 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
778 			    __LINE__, __func__);
779 			ret = -ENOMEM;
780 			mpt3sas_base_free_smid(ioc, smid);
781 			goto out;
782 		}
783 	}
784 
785 	psge = (void *)request + (karg.data_sge_offset*4);
786 
787 	/* send command to firmware */
788 	_ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
789 
790 	init_completion(&ioc->ctl_cmds.done);
791 	switch (mpi_request->Function) {
792 	case MPI2_FUNCTION_NVME_ENCAPSULATED:
793 	{
794 		nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
795 		if (!ioc->pcie_sg_lookup) {
796 			dtmprintk(ioc, ioc_info(ioc,
797 			    "HBA doesn't support NVMe. Rejecting NVMe Encapsulated request.\n"
798 			    ));
799 
800 			if (ioc->logging_level & MPT_DEBUG_TM)
801 				_debug_dump_mf(nvme_encap_request,
802 				    ioc->request_sz/4);
803 			mpt3sas_base_free_smid(ioc, smid);
804 			ret = -EINVAL;
805 			goto out;
806 		}
807 		/*
808 		 * Get the Physical Address of the sense buffer.
809 		 * Use Error Response buffer address field to hold the sense
810 		 * buffer address.
811 		 * Clear the internal sense buffer, which will potentially hold
812 		 * the Completion Queue Entry on return, or 0 if no Entry.
813 		 * Build the PRPs and set direction bits.
814 		 * Send the request.
815 		 */
816 		nvme_encap_request->ErrorResponseBaseAddress =
817 		    cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
818 		nvme_encap_request->ErrorResponseBaseAddress |=
819 		   cpu_to_le64(le32_to_cpu(
820 		   mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
821 		nvme_encap_request->ErrorResponseAllocationLength =
822 					cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
823 		memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
824 		ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
825 		    data_out_dma, data_out_sz, data_in_dma, data_in_sz);
826 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
827 			dtmprintk(ioc,
828 				  ioc_info(ioc, "handle(0x%04x): ioctl failed due to device removal in progress\n",
829 					   device_handle));
830 			mpt3sas_base_free_smid(ioc, smid);
831 			ret = -EINVAL;
832 			goto out;
833 		}
834 		mpt3sas_base_put_smid_nvme_encap(ioc, smid);
835 		break;
836 	}
837 	case MPI2_FUNCTION_SCSI_IO_REQUEST:
838 	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
839 	{
840 		Mpi2SCSIIORequest_t *scsiio_request =
841 		    (Mpi2SCSIIORequest_t *)request;
842 		scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
843 		scsiio_request->SenseBufferLowAddress =
844 		    mpt3sas_base_get_sense_buffer_dma(ioc, smid);
845 		memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
846 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
847 			dtmprintk(ioc,
848 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
849 					   device_handle));
850 			mpt3sas_base_free_smid(ioc, smid);
851 			ret = -EINVAL;
852 			goto out;
853 		}
854 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
855 		    data_in_dma, data_in_sz);
856 		if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
857 			ioc->put_smid_scsi_io(ioc, smid, device_handle);
858 		else
859 			ioc->put_smid_default(ioc, smid);
860 		break;
861 	}
862 	case MPI2_FUNCTION_SCSI_TASK_MGMT:
863 	{
864 		Mpi2SCSITaskManagementRequest_t *tm_request =
865 		    (Mpi2SCSITaskManagementRequest_t *)request;
866 
867 		dtmprintk(ioc,
868 			  ioc_info(ioc, "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
869 				   le16_to_cpu(tm_request->DevHandle),
870 				   tm_request->TaskType));
871 		ioc->got_task_abort_from_ioctl = 1;
872 		if (tm_request->TaskType ==
873 		    MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
874 		    tm_request->TaskType ==
875 		    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
876 			if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
877 				mpt3sas_base_free_smid(ioc, smid);
878 				ioc->got_task_abort_from_ioctl = 0;
879 				goto out;
880 			}
881 		}
882 		ioc->got_task_abort_from_ioctl = 0;
883 
884 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
885 			dtmprintk(ioc,
886 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
887 					   device_handle));
888 			mpt3sas_base_free_smid(ioc, smid);
889 			ret = -EINVAL;
890 			goto out;
891 		}
892 		mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
893 		    tm_request->DevHandle));
894 		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
895 		    data_in_dma, data_in_sz);
896 		ioc->put_smid_hi_priority(ioc, smid, 0);
897 		break;
898 	}
899 	case MPI2_FUNCTION_SMP_PASSTHROUGH:
900 	{
901 		Mpi2SmpPassthroughRequest_t *smp_request =
902 		    (Mpi2SmpPassthroughRequest_t *)mpi_request;
903 		u8 *data;
904 
905 		if (!ioc->multipath_on_hba) {
906 			/* ioc determines which port to use */
907 			smp_request->PhysicalPort = 0xFF;
908 		}
909 		if (smp_request->PassthroughFlags &
910 		    MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
911 			data = (u8 *)&smp_request->SGL;
912 		else {
913 			if (unlikely(data_out == NULL)) {
914 				pr_err("failure at %s:%d/%s()!\n",
915 				    __FILE__, __LINE__, __func__);
916 				mpt3sas_base_free_smid(ioc, smid);
917 				ret = -EINVAL;
918 				goto out;
919 			}
920 			data = data_out;
921 		}
922 
923 		if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
924 			ioc->ioc_link_reset_in_progress = 1;
925 			ioc->ignore_loginfos = 1;
926 		}
927 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
928 		    data_in_sz);
929 		ioc->put_smid_default(ioc, smid);
930 		break;
931 	}
932 	case MPI2_FUNCTION_SATA_PASSTHROUGH:
933 	{
934 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
935 			dtmprintk(ioc,
936 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
937 					   device_handle));
938 			mpt3sas_base_free_smid(ioc, smid);
939 			ret = -EINVAL;
940 			goto out;
941 		}
942 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
943 		    data_in_sz);
944 		ioc->put_smid_default(ioc, smid);
945 		break;
946 	}
947 	case MPI2_FUNCTION_FW_DOWNLOAD:
948 	case MPI2_FUNCTION_FW_UPLOAD:
949 	{
950 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
951 		    data_in_sz);
952 		ioc->put_smid_default(ioc, smid);
953 		break;
954 	}
955 	case MPI2_FUNCTION_TOOLBOX:
956 	{
957 		Mpi2ToolboxCleanRequest_t *toolbox_request =
958 			(Mpi2ToolboxCleanRequest_t *)mpi_request;
959 
960 		if ((toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
961 		    || (toolbox_request->Tool ==
962 		    MPI26_TOOLBOX_BACKEND_PCIE_LANE_MARGIN))
963 			ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
964 				data_in_dma, data_in_sz);
965 		else if (toolbox_request->Tool ==
966 				MPI2_TOOLBOX_MEMORY_MOVE_TOOL) {
967 			Mpi2ToolboxMemMoveRequest_t *mem_move_request =
968 					(Mpi2ToolboxMemMoveRequest_t *)request;
969 			Mpi2SGESimple64_t tmp, *src = NULL, *dst = NULL;
970 
971 			ioc->build_sg_mpi(ioc, psge, data_out_dma,
972 					data_out_sz, data_in_dma, data_in_sz);
973 			if (data_out_sz && !data_in_sz) {
974 				dst =
975 				    (Mpi2SGESimple64_t *)&mem_move_request->SGL;
976 				src = (void *)dst + ioc->sge_size;
977 
978 				memcpy(&tmp, src, ioc->sge_size);
979 				memcpy(src, dst, ioc->sge_size);
980 				memcpy(dst, &tmp, ioc->sge_size);
981 			}
982 			if (ioc->logging_level & MPT_DEBUG_TM) {
983 				ioc_info(ioc,
984 				  "Mpi2ToolboxMemMoveRequest_t request msg\n");
985 				_debug_dump_mf(mem_move_request,
986 							ioc->request_sz/4);
987 			}
988 		} else
989 			ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
990 			    data_in_dma, data_in_sz);
991 		ioc->put_smid_default(ioc, smid);
992 		break;
993 	}
994 	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
995 	{
996 		Mpi2SasIoUnitControlRequest_t *sasiounit_request =
997 		    (Mpi2SasIoUnitControlRequest_t *)mpi_request;
998 
999 		if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
1000 		    || sasiounit_request->Operation ==
1001 		    MPI2_SAS_OP_PHY_LINK_RESET) {
1002 			ioc->ioc_link_reset_in_progress = 1;
1003 			ioc->ignore_loginfos = 1;
1004 		}
1005 		/* drop to default case for posting the request */
1006 	}
1007 		fallthrough;
1008 	default:
1009 		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
1010 		    data_in_dma, data_in_sz);
1011 		ioc->put_smid_default(ioc, smid);
1012 		break;
1013 	}
1014 
1015 	if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
1016 		timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
1017 	else
1018 		timeout = karg.timeout;
1019 	wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
1020 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
1021 		Mpi2SCSITaskManagementRequest_t *tm_request =
1022 		    (Mpi2SCSITaskManagementRequest_t *)mpi_request;
1023 		mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
1024 		    tm_request->DevHandle));
1025 		mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
1026 	} else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
1027 	    mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
1028 		ioc->ioc_link_reset_in_progress) {
1029 		ioc->ioc_link_reset_in_progress = 0;
1030 		ioc->ignore_loginfos = 0;
1031 	}
1032 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1033 		mpt3sas_check_cmd_timeout(ioc,
1034 		    ioc->ctl_cmds.status, mpi_request,
1035 		    karg.data_sge_offset, issue_reset);
1036 		goto issue_host_reset;
1037 	}
1038 
1039 	mpi_reply = ioc->ctl_cmds.reply;
1040 
1041 	if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
1042 	    (ioc->logging_level & MPT_DEBUG_TM)) {
1043 		Mpi2SCSITaskManagementReply_t *tm_reply =
1044 		    (Mpi2SCSITaskManagementReply_t *)mpi_reply;
1045 
1046 		ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
1047 			 le16_to_cpu(tm_reply->IOCStatus),
1048 			 le32_to_cpu(tm_reply->IOCLogInfo),
1049 			 le32_to_cpu(tm_reply->TerminationCount));
1050 	}
1051 
1052 	/* copy out xdata to user */
1053 	if (data_in_sz) {
1054 		if (copy_to_user(karg.data_in_buf_ptr, data_in,
1055 		    data_in_sz)) {
1056 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1057 			    __LINE__, __func__);
1058 			ret = -ENODATA;
1059 			goto out;
1060 		}
1061 	}
1062 
1063 	/* copy out reply message frame to user */
1064 	if (karg.max_reply_bytes) {
1065 		sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1066 		if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1067 		    sz)) {
1068 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1069 			    __LINE__, __func__);
1070 			ret = -ENODATA;
1071 			goto out;
1072 		}
1073 	}
1074 
1075 	/* copy out sense/NVMe Error Response to user */
1076 	if (karg.max_sense_bytes && (mpi_request->Function ==
1077 	    MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
1078 	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1079 	    MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1080 		if (karg.sense_data_ptr == NULL) {
1081 			ioc_info(ioc, "Response buffer provided by application is NULL; Response data will not be returned\n");
1082 			goto out;
1083 		}
1084 		sz_arg = (mpi_request->Function ==
1085 		MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1086 							SCSI_SENSE_BUFFERSIZE;
1087 		sz = min_t(u32, karg.max_sense_bytes, sz_arg);
1088 		if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1089 		    sz)) {
1090 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1091 				__LINE__, __func__);
1092 			ret = -ENODATA;
1093 			goto out;
1094 		}
1095 	}
1096 
1097  issue_host_reset:
1098 	if (issue_reset) {
1099 		ret = -ENODATA;
1100 		if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1101 		    mpi_request->Function ==
1102 		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1103 		    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
1104 			ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
1105 				 le16_to_cpu(mpi_request->FunctionDependent1));
1106 			mpt3sas_halt_firmware(ioc);
1107 			pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1108 				le16_to_cpu(mpi_request->FunctionDependent1));
1109 			if (pcie_device && (!ioc->tm_custom_handling) &&
1110 			    (!(mpt3sas_scsih_is_pcie_scsi_device(
1111 			    pcie_device->device_info))))
1112 				mpt3sas_scsih_issue_locked_tm(ioc,
1113 				  le16_to_cpu(mpi_request->FunctionDependent1),
1114 				  0, 0, 0,
1115 				  MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1116 				  0, pcie_device->reset_timeout,
1117 			MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE);
1118 			else
1119 				mpt3sas_scsih_issue_locked_tm(ioc,
1120 				  le16_to_cpu(mpi_request->FunctionDependent1),
1121 				  0, 0, 0,
1122 				  MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1123 				  0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
1124 		} else
1125 			mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1126 	}
1127 
1128  out:
1129 	if (pcie_device)
1130 		pcie_device_put(pcie_device);
1131 
1132 	/* free memory associated with sg buffers */
1133 	if (data_in)
1134 		dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
1135 		    data_in_dma);
1136 
1137 	if (data_out)
1138 		dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
1139 		    data_out_dma);
1140 
1141 	kfree(mpi_request);
1142 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1143 	return ret;
1144 }
1145 
1146 /**
1147  * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1148  * @ioc: per adapter object
1149  * @arg: user space buffer containing ioctl content
1150  */
1151 static long
1152 _ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1153 {
1154 	struct mpt3_ioctl_iocinfo karg;
1155 
1156 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1157 				 __func__));
1158 
1159 	memset(&karg, 0 , sizeof(karg));
1160 	if (ioc->pfacts)
1161 		karg.port_number = ioc->pfacts[0].PortNumber;
1162 	karg.hw_rev = ioc->pdev->revision;
1163 	karg.pci_id = ioc->pdev->device;
1164 	karg.subsystem_device = ioc->pdev->subsystem_device;
1165 	karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1166 	karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1167 	karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1168 	karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1169 	karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1170 	karg.firmware_version = ioc->facts.FWVersion.Word;
1171 	strcpy(karg.driver_version, ioc->driver_name);
1172 	strcat(karg.driver_version, "-");
1173 	switch  (ioc->hba_mpi_version_belonged) {
1174 	case MPI2_VERSION:
1175 		if (ioc->is_warpdrive)
1176 			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1177 		else
1178 			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1179 		strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1180 		break;
1181 	case MPI25_VERSION:
1182 	case MPI26_VERSION:
1183 		if (ioc->is_gen35_ioc)
1184 			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1185 		else
1186 			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1187 		strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1188 		break;
1189 	}
1190 	karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1191 
1192 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1193 		pr_err("failure at %s:%d/%s()!\n",
1194 		    __FILE__, __LINE__, __func__);
1195 		return -EFAULT;
1196 	}
1197 	return 0;
1198 }
1199 
1200 /**
1201  * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1202  * @ioc: per adapter object
1203  * @arg: user space buffer containing ioctl content
1204  */
1205 static long
1206 _ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1207 {
1208 	struct mpt3_ioctl_eventquery karg;
1209 
1210 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1211 		pr_err("failure at %s:%d/%s()!\n",
1212 		    __FILE__, __LINE__, __func__);
1213 		return -EFAULT;
1214 	}
1215 
1216 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1217 				 __func__));
1218 
1219 	karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1220 	memcpy(karg.event_types, ioc->event_type,
1221 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1222 
1223 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1224 		pr_err("failure at %s:%d/%s()!\n",
1225 		    __FILE__, __LINE__, __func__);
1226 		return -EFAULT;
1227 	}
1228 	return 0;
1229 }
1230 
1231 /**
1232  * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1233  * @ioc: per adapter object
1234  * @arg: user space buffer containing ioctl content
1235  */
1236 static long
1237 _ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1238 {
1239 	struct mpt3_ioctl_eventenable karg;
1240 
1241 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1242 		pr_err("failure at %s:%d/%s()!\n",
1243 		    __FILE__, __LINE__, __func__);
1244 		return -EFAULT;
1245 	}
1246 
1247 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1248 				 __func__));
1249 
1250 	memcpy(ioc->event_type, karg.event_types,
1251 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1252 	mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1253 
1254 	if (ioc->event_log)
1255 		return 0;
1256 	/* initialize event_log */
1257 	ioc->event_context = 0;
1258 	ioc->aen_event_read_flag = 0;
1259 	ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1260 	    sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1261 	if (!ioc->event_log) {
1262 		pr_err("failure at %s:%d/%s()!\n",
1263 		    __FILE__, __LINE__, __func__);
1264 		return -ENOMEM;
1265 	}
1266 	return 0;
1267 }
1268 
1269 /**
1270  * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1271  * @ioc: per adapter object
1272  * @arg: user space buffer containing ioctl content
1273  */
1274 static long
1275 _ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1276 {
1277 	struct mpt3_ioctl_eventreport karg;
1278 	u32 number_bytes, max_events, max;
1279 	struct mpt3_ioctl_eventreport __user *uarg = arg;
1280 
1281 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1282 		pr_err("failure at %s:%d/%s()!\n",
1283 		    __FILE__, __LINE__, __func__);
1284 		return -EFAULT;
1285 	}
1286 
1287 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1288 				 __func__));
1289 
1290 	number_bytes = karg.hdr.max_data_size -
1291 	    sizeof(struct mpt3_ioctl_header);
1292 	max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1293 	max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1294 
1295 	/* If fewer than 1 event is requested, there must have
1296 	 * been some type of error.
1297 	 */
1298 	if (!max || !ioc->event_log)
1299 		return -ENODATA;
1300 
1301 	number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1302 	if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1303 		pr_err("failure at %s:%d/%s()!\n",
1304 		    __FILE__, __LINE__, __func__);
1305 		return -EFAULT;
1306 	}
1307 
1308 	/* reset flag so SIGIO can restart */
1309 	ioc->aen_event_read_flag = 0;
1310 	return 0;
1311 }
1312 
1313 /**
1314  * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1315  * @ioc: per adapter object
1316  * @arg: user space buffer containing ioctl content
1317  */
1318 static long
1319 _ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1320 {
1321 	struct mpt3_ioctl_diag_reset karg;
1322 	int retval;
1323 
1324 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1325 		pr_err("failure at %s:%d/%s()!\n",
1326 		    __FILE__, __LINE__, __func__);
1327 		return -EFAULT;
1328 	}
1329 
1330 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
1331 	    ioc->is_driver_loading)
1332 		return -EAGAIN;
1333 
1334 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1335 				 __func__));
1336 
1337 	retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1338 	ioc_info(ioc,
1339 	    "Ioctl: host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
1340 	return 0;
1341 }
1342 
1343 /**
1344  * _ctl_btdh_search_sas_device - searching for sas device
1345  * @ioc: per adapter object
1346  * @btdh: btdh ioctl payload
1347  */
1348 static int
1349 _ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1350 	struct mpt3_ioctl_btdh_mapping *btdh)
1351 {
1352 	struct _sas_device *sas_device;
1353 	unsigned long flags;
1354 	int rc = 0;
1355 
1356 	if (list_empty(&ioc->sas_device_list))
1357 		return rc;
1358 
1359 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1360 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1361 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1362 		    btdh->handle == sas_device->handle) {
1363 			btdh->bus = sas_device->channel;
1364 			btdh->id = sas_device->id;
1365 			rc = 1;
1366 			goto out;
1367 		} else if (btdh->bus == sas_device->channel && btdh->id ==
1368 		    sas_device->id && btdh->handle == 0xFFFF) {
1369 			btdh->handle = sas_device->handle;
1370 			rc = 1;
1371 			goto out;
1372 		}
1373 	}
1374  out:
1375 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1376 	return rc;
1377 }
1378 
1379 /**
1380  * _ctl_btdh_search_pcie_device - searching for pcie device
1381  * @ioc: per adapter object
1382  * @btdh: btdh ioctl payload
1383  */
1384 static int
1385 _ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
1386 	struct mpt3_ioctl_btdh_mapping *btdh)
1387 {
1388 	struct _pcie_device *pcie_device;
1389 	unsigned long flags;
1390 	int rc = 0;
1391 
1392 	if (list_empty(&ioc->pcie_device_list))
1393 		return rc;
1394 
1395 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1396 	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1397 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1398 			   btdh->handle == pcie_device->handle) {
1399 			btdh->bus = pcie_device->channel;
1400 			btdh->id = pcie_device->id;
1401 			rc = 1;
1402 			goto out;
1403 		} else if (btdh->bus == pcie_device->channel && btdh->id ==
1404 			   pcie_device->id && btdh->handle == 0xFFFF) {
1405 			btdh->handle = pcie_device->handle;
1406 			rc = 1;
1407 			goto out;
1408 		}
1409 	}
1410  out:
1411 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1412 	return rc;
1413 }
1414 
1415 /**
1416  * _ctl_btdh_search_raid_device - searching for raid device
1417  * @ioc: per adapter object
1418  * @btdh: btdh ioctl payload
1419  */
1420 static int
1421 _ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1422 	struct mpt3_ioctl_btdh_mapping *btdh)
1423 {
1424 	struct _raid_device *raid_device;
1425 	unsigned long flags;
1426 	int rc = 0;
1427 
1428 	if (list_empty(&ioc->raid_device_list))
1429 		return rc;
1430 
1431 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1432 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1433 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1434 		    btdh->handle == raid_device->handle) {
1435 			btdh->bus = raid_device->channel;
1436 			btdh->id = raid_device->id;
1437 			rc = 1;
1438 			goto out;
1439 		} else if (btdh->bus == raid_device->channel && btdh->id ==
1440 		    raid_device->id && btdh->handle == 0xFFFF) {
1441 			btdh->handle = raid_device->handle;
1442 			rc = 1;
1443 			goto out;
1444 		}
1445 	}
1446  out:
1447 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1448 	return rc;
1449 }
1450 
1451 /**
1452  * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1453  * @ioc: per adapter object
1454  * @arg: user space buffer containing ioctl content
1455  */
1456 static long
1457 _ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1458 {
1459 	struct mpt3_ioctl_btdh_mapping karg;
1460 	int rc;
1461 
1462 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1463 		pr_err("failure at %s:%d/%s()!\n",
1464 		    __FILE__, __LINE__, __func__);
1465 		return -EFAULT;
1466 	}
1467 
1468 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1469 				 __func__));
1470 
1471 	rc = _ctl_btdh_search_sas_device(ioc, &karg);
1472 	if (!rc)
1473 		rc = _ctl_btdh_search_pcie_device(ioc, &karg);
1474 	if (!rc)
1475 		_ctl_btdh_search_raid_device(ioc, &karg);
1476 
1477 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1478 		pr_err("failure at %s:%d/%s()!\n",
1479 		    __FILE__, __LINE__, __func__);
1480 		return -EFAULT;
1481 	}
1482 	return 0;
1483 }
1484 
1485 /**
1486  * _ctl_diag_capability - return diag buffer capability
1487  * @ioc: per adapter object
1488  * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1489  *
1490  * returns 1 when diag buffer support is enabled in firmware
1491  */
1492 static u8
1493 _ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1494 {
1495 	u8 rc = 0;
1496 
1497 	switch (buffer_type) {
1498 	case MPI2_DIAG_BUF_TYPE_TRACE:
1499 		if (ioc->facts.IOCCapabilities &
1500 		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1501 			rc = 1;
1502 		break;
1503 	case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1504 		if (ioc->facts.IOCCapabilities &
1505 		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1506 			rc = 1;
1507 		break;
1508 	case MPI2_DIAG_BUF_TYPE_EXTENDED:
1509 		if (ioc->facts.IOCCapabilities &
1510 		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1511 			rc = 1;
1512 	}
1513 
1514 	return rc;
1515 }
1516 
1517 /**
1518  * _ctl_diag_get_bufftype - return diag buffer type
1519  *              either TRACE, SNAPSHOT, or EXTENDED
1520  * @ioc: per adapter object
1521  * @unique_id: specifies the unique_id for the buffer
1522  *
1523  * returns MPT3_DIAG_UID_NOT_FOUND if the id not found
1524  */
1525 static u8
1526 _ctl_diag_get_bufftype(struct MPT3SAS_ADAPTER *ioc, u32 unique_id)
1527 {
1528 	u8  index;
1529 
1530 	for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
1531 		if (ioc->unique_id[index] == unique_id)
1532 			return index;
1533 	}
1534 
1535 	return MPT3_DIAG_UID_NOT_FOUND;
1536 }
1537 
1538 /**
1539  * _ctl_diag_register_2 - wrapper for registering diag buffer support
1540  * @ioc: per adapter object
1541  * @diag_register: the diag_register struct passed in from user space
1542  *
1543  */
1544 static long
1545 _ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1546 	struct mpt3_diag_register *diag_register)
1547 {
1548 	int rc, i;
1549 	void *request_data = NULL;
1550 	dma_addr_t request_data_dma;
1551 	u32 request_data_sz = 0;
1552 	Mpi2DiagBufferPostRequest_t *mpi_request;
1553 	Mpi2DiagBufferPostReply_t *mpi_reply;
1554 	u8 buffer_type;
1555 	u16 smid;
1556 	u16 ioc_status;
1557 	u32 ioc_state;
1558 	u8 issue_reset = 0;
1559 
1560 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1561 				 __func__));
1562 
1563 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1564 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1565 		ioc_err(ioc, "%s: failed due to ioc not operational\n",
1566 			__func__);
1567 		rc = -EAGAIN;
1568 		goto out;
1569 	}
1570 
1571 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1572 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
1573 		rc = -EAGAIN;
1574 		goto out;
1575 	}
1576 
1577 	buffer_type = diag_register->buffer_type;
1578 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1579 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1580 			__func__, buffer_type);
1581 		return -EPERM;
1582 	}
1583 
1584 	if (diag_register->unique_id == 0) {
1585 		ioc_err(ioc,
1586 		    "%s: Invalid UID(0x%08x), buffer_type(0x%02x)\n", __func__,
1587 		    diag_register->unique_id, buffer_type);
1588 		return -EINVAL;
1589 	}
1590 
1591 	if ((ioc->diag_buffer_status[buffer_type] &
1592 	    MPT3_DIAG_BUFFER_IS_APP_OWNED) &&
1593 	    !(ioc->diag_buffer_status[buffer_type] &
1594 	    MPT3_DIAG_BUFFER_IS_RELEASED)) {
1595 		ioc_err(ioc,
1596 		    "%s: buffer_type(0x%02x) is already registered by application with UID(0x%08x)\n",
1597 		    __func__, buffer_type, ioc->unique_id[buffer_type]);
1598 		return -EINVAL;
1599 	}
1600 
1601 	if (ioc->diag_buffer_status[buffer_type] &
1602 	    MPT3_DIAG_BUFFER_IS_REGISTERED) {
1603 		/*
1604 		 * If driver posts buffer initially, then an application wants
1605 		 * to Register that buffer (own it) without Releasing first,
1606 		 * the application Register command MUST have the same buffer
1607 		 * type and size in the Register command (obtained from the
1608 		 * Query command). Otherwise that Register command will be
1609 		 * failed. If the application has released the buffer but wants
1610 		 * to re-register it, it should be allowed as long as the
1611 		 * Unique-Id/Size match.
1612 		 */
1613 
1614 		if (ioc->unique_id[buffer_type] == MPT3DIAGBUFFUNIQUEID &&
1615 		    ioc->diag_buffer_sz[buffer_type] ==
1616 		    diag_register->requested_buffer_size) {
1617 
1618 			if (!(ioc->diag_buffer_status[buffer_type] &
1619 			     MPT3_DIAG_BUFFER_IS_RELEASED)) {
1620 				dctlprintk(ioc, ioc_info(ioc,
1621 				    "%s: diag_buffer (%d) ownership changed. old-ID(0x%08x), new-ID(0x%08x)\n",
1622 				    __func__, buffer_type,
1623 				    ioc->unique_id[buffer_type],
1624 				    diag_register->unique_id));
1625 
1626 				/*
1627 				 * Application wants to own the buffer with
1628 				 * the same size.
1629 				 */
1630 				ioc->unique_id[buffer_type] =
1631 				    diag_register->unique_id;
1632 				rc = 0; /* success */
1633 				goto out;
1634 			}
1635 		} else if (ioc->unique_id[buffer_type] !=
1636 		    MPT3DIAGBUFFUNIQUEID) {
1637 			if (ioc->unique_id[buffer_type] !=
1638 			    diag_register->unique_id ||
1639 			    ioc->diag_buffer_sz[buffer_type] !=
1640 			    diag_register->requested_buffer_size ||
1641 			    !(ioc->diag_buffer_status[buffer_type] &
1642 			    MPT3_DIAG_BUFFER_IS_RELEASED)) {
1643 				ioc_err(ioc,
1644 				    "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1645 				    __func__, buffer_type);
1646 				return -EINVAL;
1647 			}
1648 		} else {
1649 			ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1650 			    __func__, buffer_type);
1651 			return -EINVAL;
1652 		}
1653 	} else if (ioc->diag_buffer_status[buffer_type] &
1654 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
1655 
1656 		if (ioc->unique_id[buffer_type] != MPT3DIAGBUFFUNIQUEID ||
1657 		    ioc->diag_buffer_sz[buffer_type] !=
1658 		    diag_register->requested_buffer_size) {
1659 
1660 			ioc_err(ioc,
1661 			    "%s: already a buffer is allocated for buffer_type(0x%02x) of size %d bytes, so please try registering again with same size\n",
1662 			     __func__, buffer_type,
1663 			    ioc->diag_buffer_sz[buffer_type]);
1664 			return -EINVAL;
1665 		}
1666 	}
1667 
1668 	if (diag_register->requested_buffer_size % 4)  {
1669 		ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
1670 			__func__);
1671 		return -EINVAL;
1672 	}
1673 
1674 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1675 	if (!smid) {
1676 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1677 		rc = -EAGAIN;
1678 		goto out;
1679 	}
1680 
1681 	rc = 0;
1682 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1683 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1684 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1685 	ioc->ctl_cmds.smid = smid;
1686 
1687 	request_data = ioc->diag_buffer[buffer_type];
1688 	request_data_sz = diag_register->requested_buffer_size;
1689 	ioc->unique_id[buffer_type] = diag_register->unique_id;
1690 	ioc->diag_buffer_status[buffer_type] &=
1691 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1692 	memcpy(ioc->product_specific[buffer_type],
1693 	    diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1694 	ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1695 
1696 	if (request_data) {
1697 		request_data_dma = ioc->diag_buffer_dma[buffer_type];
1698 		if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1699 			dma_free_coherent(&ioc->pdev->dev,
1700 					ioc->diag_buffer_sz[buffer_type],
1701 					request_data, request_data_dma);
1702 			request_data = NULL;
1703 		}
1704 	}
1705 
1706 	if (request_data == NULL) {
1707 		ioc->diag_buffer_sz[buffer_type] = 0;
1708 		ioc->diag_buffer_dma[buffer_type] = 0;
1709 		request_data = dma_alloc_coherent(&ioc->pdev->dev,
1710 				request_data_sz, &request_data_dma, GFP_KERNEL);
1711 		if (request_data == NULL) {
1712 			ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
1713 				__func__, request_data_sz);
1714 			mpt3sas_base_free_smid(ioc, smid);
1715 			rc = -ENOMEM;
1716 			goto out;
1717 		}
1718 		ioc->diag_buffer[buffer_type] = request_data;
1719 		ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1720 		ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1721 	}
1722 
1723 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1724 	mpi_request->BufferType = diag_register->buffer_type;
1725 	mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1726 	mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1727 	mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1728 	mpi_request->VF_ID = 0; /* TODO */
1729 	mpi_request->VP_ID = 0;
1730 
1731 	dctlprintk(ioc,
1732 		   ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1733 			    __func__, request_data,
1734 			    (unsigned long long)request_data_dma,
1735 			    le32_to_cpu(mpi_request->BufferLength)));
1736 
1737 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1738 		mpi_request->ProductSpecific[i] =
1739 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
1740 
1741 	init_completion(&ioc->ctl_cmds.done);
1742 	ioc->put_smid_default(ioc, smid);
1743 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
1744 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1745 
1746 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1747 		mpt3sas_check_cmd_timeout(ioc,
1748 		    ioc->ctl_cmds.status, mpi_request,
1749 		    sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
1750 		goto issue_host_reset;
1751 	}
1752 
1753 	/* process the completed Reply Message Frame */
1754 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1755 		ioc_err(ioc, "%s: no reply message\n", __func__);
1756 		rc = -EFAULT;
1757 		goto out;
1758 	}
1759 
1760 	mpi_reply = ioc->ctl_cmds.reply;
1761 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1762 
1763 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1764 		ioc->diag_buffer_status[buffer_type] |=
1765 			MPT3_DIAG_BUFFER_IS_REGISTERED;
1766 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
1767 	} else {
1768 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1769 			 __func__,
1770 			 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1771 		rc = -EFAULT;
1772 	}
1773 
1774  issue_host_reset:
1775 	if (issue_reset)
1776 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1777 
1778  out:
1779 
1780 	if (rc && request_data) {
1781 		dma_free_coherent(&ioc->pdev->dev, request_data_sz,
1782 		    request_data, request_data_dma);
1783 		ioc->diag_buffer_status[buffer_type] &=
1784 		    ~MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1785 	}
1786 
1787 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1788 	return rc;
1789 }
1790 
1791 /**
1792  * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1793  * @ioc: per adapter object
1794  * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1795  *
1796  * This is called when command line option diag_buffer_enable is enabled
1797  * at driver load time.
1798  */
1799 void
1800 mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1801 {
1802 	struct mpt3_diag_register diag_register;
1803 	u32 ret_val;
1804 	u32 trace_buff_size = ioc->manu_pg11.HostTraceBufferMaxSizeKB<<10;
1805 	u32 min_trace_buff_size = 0;
1806 	u32 decr_trace_buff_size = 0;
1807 
1808 	memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1809 
1810 	if (bits_to_register & 1) {
1811 		ioc_info(ioc, "registering trace buffer support\n");
1812 		ioc->diag_trigger_master.MasterData =
1813 		    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1814 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1815 		diag_register.unique_id =
1816 		    (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
1817 		    (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
1818 
1819 		if (trace_buff_size != 0) {
1820 			diag_register.requested_buffer_size = trace_buff_size;
1821 			min_trace_buff_size =
1822 			    ioc->manu_pg11.HostTraceBufferMinSizeKB<<10;
1823 			decr_trace_buff_size =
1824 			    ioc->manu_pg11.HostTraceBufferDecrementSizeKB<<10;
1825 
1826 			if (min_trace_buff_size > trace_buff_size) {
1827 				/* The buff size is not set correctly */
1828 				ioc_err(ioc,
1829 				    "Min Trace Buff size (%d KB) greater than Max Trace Buff size (%d KB)\n",
1830 				     min_trace_buff_size>>10,
1831 				     trace_buff_size>>10);
1832 				ioc_err(ioc,
1833 				    "Using zero Min Trace Buff Size\n");
1834 				min_trace_buff_size = 0;
1835 			}
1836 
1837 			if (decr_trace_buff_size == 0) {
1838 				/*
1839 				 * retry the min size if decrement
1840 				 * is not available.
1841 				 */
1842 				decr_trace_buff_size =
1843 				    trace_buff_size - min_trace_buff_size;
1844 			}
1845 		} else {
1846 			/* register for 2MB buffers  */
1847 			diag_register.requested_buffer_size = 2 * (1024 * 1024);
1848 		}
1849 
1850 		do {
1851 			ret_val = _ctl_diag_register_2(ioc,  &diag_register);
1852 
1853 			if (ret_val == -ENOMEM && min_trace_buff_size &&
1854 			    (trace_buff_size - decr_trace_buff_size) >=
1855 			    min_trace_buff_size) {
1856 				/* adjust the buffer size */
1857 				trace_buff_size -= decr_trace_buff_size;
1858 				diag_register.requested_buffer_size =
1859 				    trace_buff_size;
1860 			} else
1861 				break;
1862 		} while (true);
1863 
1864 		if (ret_val == -ENOMEM)
1865 			ioc_err(ioc,
1866 			    "Cannot allocate trace buffer memory. Last memory tried = %d KB\n",
1867 			    diag_register.requested_buffer_size>>10);
1868 		else if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE]
1869 		    & MPT3_DIAG_BUFFER_IS_REGISTERED) {
1870 			ioc_err(ioc, "Trace buffer memory %d KB allocated\n",
1871 			    diag_register.requested_buffer_size>>10);
1872 			if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
1873 				ioc->diag_buffer_status[
1874 				    MPI2_DIAG_BUF_TYPE_TRACE] |=
1875 				    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1876 		}
1877 	}
1878 
1879 	if (bits_to_register & 2) {
1880 		ioc_info(ioc, "registering snapshot buffer support\n");
1881 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1882 		/* register for 2MB buffers  */
1883 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1884 		diag_register.unique_id = 0x7075901;
1885 		_ctl_diag_register_2(ioc,  &diag_register);
1886 	}
1887 
1888 	if (bits_to_register & 4) {
1889 		ioc_info(ioc, "registering extended buffer support\n");
1890 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1891 		/* register for 2MB buffers  */
1892 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1893 		diag_register.unique_id = 0x7075901;
1894 		_ctl_diag_register_2(ioc,  &diag_register);
1895 	}
1896 }
1897 
1898 /**
1899  * _ctl_diag_register - application register with driver
1900  * @ioc: per adapter object
1901  * @arg: user space buffer containing ioctl content
1902  *
1903  * This will allow the driver to setup any required buffers that will be
1904  * needed by firmware to communicate with the driver.
1905  */
1906 static long
1907 _ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1908 {
1909 	struct mpt3_diag_register karg;
1910 	long rc;
1911 
1912 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1913 		pr_err("failure at %s:%d/%s()!\n",
1914 		    __FILE__, __LINE__, __func__);
1915 		return -EFAULT;
1916 	}
1917 
1918 	rc = _ctl_diag_register_2(ioc, &karg);
1919 
1920 	if (!rc && (ioc->diag_buffer_status[karg.buffer_type] &
1921 	    MPT3_DIAG_BUFFER_IS_REGISTERED))
1922 		ioc->diag_buffer_status[karg.buffer_type] |=
1923 		    MPT3_DIAG_BUFFER_IS_APP_OWNED;
1924 
1925 	return rc;
1926 }
1927 
1928 /**
1929  * _ctl_diag_unregister - application unregister with driver
1930  * @ioc: per adapter object
1931  * @arg: user space buffer containing ioctl content
1932  *
1933  * This will allow the driver to cleanup any memory allocated for diag
1934  * messages and to free up any resources.
1935  */
1936 static long
1937 _ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1938 {
1939 	struct mpt3_diag_unregister karg;
1940 	void *request_data;
1941 	dma_addr_t request_data_dma;
1942 	u32 request_data_sz;
1943 	u8 buffer_type;
1944 
1945 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1946 		pr_err("failure at %s:%d/%s()!\n",
1947 		    __FILE__, __LINE__, __func__);
1948 		return -EFAULT;
1949 	}
1950 
1951 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1952 				 __func__));
1953 
1954 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
1955 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
1956 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
1957 		    __func__, karg.unique_id);
1958 		return -EINVAL;
1959 	}
1960 
1961 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1962 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1963 			__func__, buffer_type);
1964 		return -EPERM;
1965 	}
1966 
1967 	if ((ioc->diag_buffer_status[buffer_type] &
1968 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1969 		ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1970 			__func__, buffer_type);
1971 		return -EINVAL;
1972 	}
1973 	if ((ioc->diag_buffer_status[buffer_type] &
1974 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
1975 		ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
1976 			__func__, buffer_type);
1977 		return -EINVAL;
1978 	}
1979 
1980 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1981 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1982 			__func__, karg.unique_id);
1983 		return -EINVAL;
1984 	}
1985 
1986 	request_data = ioc->diag_buffer[buffer_type];
1987 	if (!request_data) {
1988 		ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1989 			__func__, buffer_type);
1990 		return -ENOMEM;
1991 	}
1992 
1993 	if (ioc->diag_buffer_status[buffer_type] &
1994 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
1995 		ioc->unique_id[buffer_type] = MPT3DIAGBUFFUNIQUEID;
1996 		ioc->diag_buffer_status[buffer_type] &=
1997 		    ~MPT3_DIAG_BUFFER_IS_APP_OWNED;
1998 		ioc->diag_buffer_status[buffer_type] &=
1999 		    ~MPT3_DIAG_BUFFER_IS_REGISTERED;
2000 	} else {
2001 		request_data_sz = ioc->diag_buffer_sz[buffer_type];
2002 		request_data_dma = ioc->diag_buffer_dma[buffer_type];
2003 		dma_free_coherent(&ioc->pdev->dev, request_data_sz,
2004 				request_data, request_data_dma);
2005 		ioc->diag_buffer[buffer_type] = NULL;
2006 		ioc->diag_buffer_status[buffer_type] = 0;
2007 	}
2008 	return 0;
2009 }
2010 
2011 /**
2012  * _ctl_diag_query - query relevant info associated with diag buffers
2013  * @ioc: per adapter object
2014  * @arg: user space buffer containing ioctl content
2015  *
2016  * The application will send only buffer_type and unique_id.  Driver will
2017  * inspect unique_id first, if valid, fill in all the info.  If unique_id is
2018  * 0x00, the driver will return info specified by Buffer Type.
2019  */
2020 static long
2021 _ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2022 {
2023 	struct mpt3_diag_query karg;
2024 	void *request_data;
2025 	int i;
2026 	u8 buffer_type;
2027 
2028 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2029 		pr_err("failure at %s:%d/%s()!\n",
2030 		    __FILE__, __LINE__, __func__);
2031 		return -EFAULT;
2032 	}
2033 
2034 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2035 				 __func__));
2036 
2037 	karg.application_flags = 0;
2038 	buffer_type = karg.buffer_type;
2039 
2040 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2041 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2042 			__func__, buffer_type);
2043 		return -EPERM;
2044 	}
2045 
2046 	if (!(ioc->diag_buffer_status[buffer_type] &
2047 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED)) {
2048 		if ((ioc->diag_buffer_status[buffer_type] &
2049 		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2050 			ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2051 				__func__, buffer_type);
2052 			return -EINVAL;
2053 		}
2054 	}
2055 
2056 	if (karg.unique_id) {
2057 		if (karg.unique_id != ioc->unique_id[buffer_type]) {
2058 			ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2059 				__func__, karg.unique_id);
2060 			return -EINVAL;
2061 		}
2062 	}
2063 
2064 	request_data = ioc->diag_buffer[buffer_type];
2065 	if (!request_data) {
2066 		ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2067 			__func__, buffer_type);
2068 		return -ENOMEM;
2069 	}
2070 
2071 	if ((ioc->diag_buffer_status[buffer_type] &
2072 	    MPT3_DIAG_BUFFER_IS_REGISTERED))
2073 		karg.application_flags |= MPT3_APP_FLAGS_BUFFER_VALID;
2074 
2075 	if (!(ioc->diag_buffer_status[buffer_type] &
2076 	     MPT3_DIAG_BUFFER_IS_RELEASED))
2077 		karg.application_flags |= MPT3_APP_FLAGS_FW_BUFFER_ACCESS;
2078 
2079 	if (!(ioc->diag_buffer_status[buffer_type] &
2080 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED))
2081 		karg.application_flags |= MPT3_APP_FLAGS_DYNAMIC_BUFFER_ALLOC;
2082 
2083 	if ((ioc->diag_buffer_status[buffer_type] &
2084 	    MPT3_DIAG_BUFFER_IS_APP_OWNED))
2085 		karg.application_flags |= MPT3_APP_FLAGS_APP_OWNED;
2086 
2087 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2088 		karg.product_specific[i] =
2089 		    ioc->product_specific[buffer_type][i];
2090 
2091 	karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
2092 	karg.driver_added_buffer_size = 0;
2093 	karg.unique_id = ioc->unique_id[buffer_type];
2094 	karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
2095 
2096 	if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
2097 		ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
2098 			__func__, arg);
2099 		return -EFAULT;
2100 	}
2101 	return 0;
2102 }
2103 
2104 /**
2105  * mpt3sas_send_diag_release - Diag Release Message
2106  * @ioc: per adapter object
2107  * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
2108  * @issue_reset: specifies whether host reset is required.
2109  *
2110  */
2111 int
2112 mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
2113 	u8 *issue_reset)
2114 {
2115 	Mpi2DiagReleaseRequest_t *mpi_request;
2116 	Mpi2DiagReleaseReply_t *mpi_reply;
2117 	u16 smid;
2118 	u16 ioc_status;
2119 	u32 ioc_state;
2120 	int rc;
2121 	u8 reset_needed = 0;
2122 
2123 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2124 				 __func__));
2125 
2126 	rc = 0;
2127 	*issue_reset = 0;
2128 
2129 
2130 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2131 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2132 		if (ioc->diag_buffer_status[buffer_type] &
2133 		    MPT3_DIAG_BUFFER_IS_REGISTERED)
2134 			ioc->diag_buffer_status[buffer_type] |=
2135 			    MPT3_DIAG_BUFFER_IS_RELEASED;
2136 		dctlprintk(ioc,
2137 			   ioc_info(ioc, "%s: skipping due to FAULT state\n",
2138 				    __func__));
2139 		rc = -EAGAIN;
2140 		goto out;
2141 	}
2142 
2143 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2144 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
2145 		rc = -EAGAIN;
2146 		goto out;
2147 	}
2148 
2149 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2150 	if (!smid) {
2151 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2152 		rc = -EAGAIN;
2153 		goto out;
2154 	}
2155 
2156 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2157 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2158 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2159 	ioc->ctl_cmds.smid = smid;
2160 
2161 	mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
2162 	mpi_request->BufferType = buffer_type;
2163 	mpi_request->VF_ID = 0; /* TODO */
2164 	mpi_request->VP_ID = 0;
2165 
2166 	init_completion(&ioc->ctl_cmds.done);
2167 	ioc->put_smid_default(ioc, smid);
2168 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
2169 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2170 
2171 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2172 		mpt3sas_check_cmd_timeout(ioc,
2173 		    ioc->ctl_cmds.status, mpi_request,
2174 		    sizeof(Mpi2DiagReleaseRequest_t)/4, reset_needed);
2175 		 *issue_reset = reset_needed;
2176 		rc = -EFAULT;
2177 		goto out;
2178 	}
2179 
2180 	/* process the completed Reply Message Frame */
2181 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2182 		ioc_err(ioc, "%s: no reply message\n", __func__);
2183 		rc = -EFAULT;
2184 		goto out;
2185 	}
2186 
2187 	mpi_reply = ioc->ctl_cmds.reply;
2188 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2189 
2190 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2191 		ioc->diag_buffer_status[buffer_type] |=
2192 		    MPT3_DIAG_BUFFER_IS_RELEASED;
2193 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
2194 	} else {
2195 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2196 			 __func__,
2197 			 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2198 		rc = -EFAULT;
2199 	}
2200 
2201  out:
2202 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2203 	return rc;
2204 }
2205 
2206 /**
2207  * _ctl_diag_release - request to send Diag Release Message to firmware
2208  * @ioc: ?
2209  * @arg: user space buffer containing ioctl content
2210  *
2211  * This allows ownership of the specified buffer to returned to the driver,
2212  * allowing an application to read the buffer without fear that firmware is
2213  * overwriting information in the buffer.
2214  */
2215 static long
2216 _ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2217 {
2218 	struct mpt3_diag_release karg;
2219 	void *request_data;
2220 	int rc;
2221 	u8 buffer_type;
2222 	u8 issue_reset = 0;
2223 
2224 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2225 		pr_err("failure at %s:%d/%s()!\n",
2226 		    __FILE__, __LINE__, __func__);
2227 		return -EFAULT;
2228 	}
2229 
2230 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2231 				 __func__));
2232 
2233 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2234 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2235 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2236 		    __func__, karg.unique_id);
2237 		return -EINVAL;
2238 	}
2239 
2240 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2241 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2242 			__func__, buffer_type);
2243 		return -EPERM;
2244 	}
2245 
2246 	if ((ioc->diag_buffer_status[buffer_type] &
2247 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2248 		ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2249 			__func__, buffer_type);
2250 		return -EINVAL;
2251 	}
2252 
2253 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2254 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2255 			__func__, karg.unique_id);
2256 		return -EINVAL;
2257 	}
2258 
2259 	if (ioc->diag_buffer_status[buffer_type] &
2260 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
2261 		ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
2262 			__func__, buffer_type);
2263 		return -EINVAL;
2264 	}
2265 
2266 	request_data = ioc->diag_buffer[buffer_type];
2267 
2268 	if (!request_data) {
2269 		ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2270 			__func__, buffer_type);
2271 		return -ENOMEM;
2272 	}
2273 
2274 	/* buffers were released by due to host reset */
2275 	if ((ioc->diag_buffer_status[buffer_type] &
2276 	    MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2277 		ioc->diag_buffer_status[buffer_type] |=
2278 		    MPT3_DIAG_BUFFER_IS_RELEASED;
2279 		ioc->diag_buffer_status[buffer_type] &=
2280 		    ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
2281 		ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
2282 			__func__, buffer_type);
2283 		return 0;
2284 	}
2285 
2286 	rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2287 
2288 	if (issue_reset)
2289 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2290 
2291 	return rc;
2292 }
2293 
2294 /**
2295  * _ctl_diag_read_buffer - request for copy of the diag buffer
2296  * @ioc: per adapter object
2297  * @arg: user space buffer containing ioctl content
2298  */
2299 static long
2300 _ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2301 {
2302 	struct mpt3_diag_read_buffer karg;
2303 	struct mpt3_diag_read_buffer __user *uarg = arg;
2304 	void *request_data, *diag_data;
2305 	Mpi2DiagBufferPostRequest_t *mpi_request;
2306 	Mpi2DiagBufferPostReply_t *mpi_reply;
2307 	int rc, i;
2308 	u8 buffer_type;
2309 	unsigned long request_size, copy_size;
2310 	u16 smid;
2311 	u16 ioc_status;
2312 	u8 issue_reset = 0;
2313 
2314 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2315 		pr_err("failure at %s:%d/%s()!\n",
2316 		    __FILE__, __LINE__, __func__);
2317 		return -EFAULT;
2318 	}
2319 
2320 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2321 				 __func__));
2322 
2323 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2324 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2325 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2326 		    __func__, karg.unique_id);
2327 		return -EINVAL;
2328 	}
2329 
2330 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2331 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2332 			__func__, buffer_type);
2333 		return -EPERM;
2334 	}
2335 
2336 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2337 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2338 			__func__, karg.unique_id);
2339 		return -EINVAL;
2340 	}
2341 
2342 	request_data = ioc->diag_buffer[buffer_type];
2343 	if (!request_data) {
2344 		ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2345 			__func__, buffer_type);
2346 		return -ENOMEM;
2347 	}
2348 
2349 	request_size = ioc->diag_buffer_sz[buffer_type];
2350 
2351 	if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2352 		ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
2353 			__func__);
2354 		return -EINVAL;
2355 	}
2356 
2357 	if (karg.starting_offset > request_size)
2358 		return -EINVAL;
2359 
2360 	diag_data = (void *)(request_data + karg.starting_offset);
2361 	dctlprintk(ioc,
2362 		   ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2363 			    __func__, diag_data, karg.starting_offset,
2364 			    karg.bytes_to_read));
2365 
2366 	/* Truncate data on requests that are too large */
2367 	if ((diag_data + karg.bytes_to_read < diag_data) ||
2368 	    (diag_data + karg.bytes_to_read > request_data + request_size))
2369 		copy_size = request_size - karg.starting_offset;
2370 	else
2371 		copy_size = karg.bytes_to_read;
2372 
2373 	if (copy_to_user((void __user *)uarg->diagnostic_data,
2374 	    diag_data, copy_size)) {
2375 		ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2376 			__func__, diag_data);
2377 		return -EFAULT;
2378 	}
2379 
2380 	if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2381 		return 0;
2382 
2383 	dctlprintk(ioc,
2384 		   ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
2385 			    __func__, buffer_type));
2386 	if ((ioc->diag_buffer_status[buffer_type] &
2387 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2388 		dctlprintk(ioc,
2389 			   ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
2390 				    __func__, buffer_type));
2391 		return 0;
2392 	}
2393 	/* Get a free request frame and save the message context.
2394 	*/
2395 
2396 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2397 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
2398 		rc = -EAGAIN;
2399 		goto out;
2400 	}
2401 
2402 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2403 	if (!smid) {
2404 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2405 		rc = -EAGAIN;
2406 		goto out;
2407 	}
2408 
2409 	rc = 0;
2410 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2411 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2412 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2413 	ioc->ctl_cmds.smid = smid;
2414 
2415 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2416 	mpi_request->BufferType = buffer_type;
2417 	mpi_request->BufferLength =
2418 	    cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2419 	mpi_request->BufferAddress =
2420 	    cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2421 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2422 		mpi_request->ProductSpecific[i] =
2423 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
2424 	mpi_request->VF_ID = 0; /* TODO */
2425 	mpi_request->VP_ID = 0;
2426 
2427 	init_completion(&ioc->ctl_cmds.done);
2428 	ioc->put_smid_default(ioc, smid);
2429 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
2430 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2431 
2432 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2433 		mpt3sas_check_cmd_timeout(ioc,
2434 		    ioc->ctl_cmds.status, mpi_request,
2435 		    sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
2436 		goto issue_host_reset;
2437 	}
2438 
2439 	/* process the completed Reply Message Frame */
2440 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2441 		ioc_err(ioc, "%s: no reply message\n", __func__);
2442 		rc = -EFAULT;
2443 		goto out;
2444 	}
2445 
2446 	mpi_reply = ioc->ctl_cmds.reply;
2447 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2448 
2449 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2450 		ioc->diag_buffer_status[buffer_type] |=
2451 		    MPT3_DIAG_BUFFER_IS_REGISTERED;
2452 		ioc->diag_buffer_status[buffer_type] &=
2453 		    ~MPT3_DIAG_BUFFER_IS_RELEASED;
2454 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
2455 	} else {
2456 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2457 			 __func__, ioc_status,
2458 			 le32_to_cpu(mpi_reply->IOCLogInfo));
2459 		rc = -EFAULT;
2460 	}
2461 
2462  issue_host_reset:
2463 	if (issue_reset)
2464 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2465 
2466  out:
2467 
2468 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2469 	return rc;
2470 }
2471 
2472 
2473 
2474 #ifdef CONFIG_COMPAT
2475 /**
2476  * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2477  * @ioc: per adapter object
2478  * @cmd: ioctl opcode
2479  * @arg: (struct mpt3_ioctl_command32)
2480  *
2481  * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2482  */
2483 static long
2484 _ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2485 	void __user *arg)
2486 {
2487 	struct mpt3_ioctl_command32 karg32;
2488 	struct mpt3_ioctl_command32 __user *uarg;
2489 	struct mpt3_ioctl_command karg;
2490 
2491 	if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2492 		return -EINVAL;
2493 
2494 	uarg = (struct mpt3_ioctl_command32 __user *) arg;
2495 
2496 	if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2497 		pr_err("failure at %s:%d/%s()!\n",
2498 		    __FILE__, __LINE__, __func__);
2499 		return -EFAULT;
2500 	}
2501 
2502 	memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2503 	karg.hdr.ioc_number = karg32.hdr.ioc_number;
2504 	karg.hdr.port_number = karg32.hdr.port_number;
2505 	karg.hdr.max_data_size = karg32.hdr.max_data_size;
2506 	karg.timeout = karg32.timeout;
2507 	karg.max_reply_bytes = karg32.max_reply_bytes;
2508 	karg.data_in_size = karg32.data_in_size;
2509 	karg.data_out_size = karg32.data_out_size;
2510 	karg.max_sense_bytes = karg32.max_sense_bytes;
2511 	karg.data_sge_offset = karg32.data_sge_offset;
2512 	karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2513 	karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2514 	karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2515 	karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2516 	return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2517 }
2518 #endif
2519 
2520 /**
2521  * _ctl_ioctl_main - main ioctl entry point
2522  * @file:  (struct file)
2523  * @cmd:  ioctl opcode
2524  * @arg:  user space data buffer
2525  * @compat:  handles 32 bit applications in 64bit os
2526  * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
2527  * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
2528  */
2529 static long
2530 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2531 	u8 compat, u16 mpi_version)
2532 {
2533 	struct MPT3SAS_ADAPTER *ioc;
2534 	struct mpt3_ioctl_header ioctl_header;
2535 	enum block_state state;
2536 	long ret = -EINVAL;
2537 
2538 	/* get IOCTL header */
2539 	if (copy_from_user(&ioctl_header, (char __user *)arg,
2540 	    sizeof(struct mpt3_ioctl_header))) {
2541 		pr_err("failure at %s:%d/%s()!\n",
2542 		    __FILE__, __LINE__, __func__);
2543 		return -EFAULT;
2544 	}
2545 
2546 	if (_ctl_verify_adapter(ioctl_header.ioc_number,
2547 				&ioc, mpi_version) == -1 || !ioc)
2548 		return -ENODEV;
2549 
2550 	/* pci_access_mutex lock acquired by ioctl path */
2551 	mutex_lock(&ioc->pci_access_mutex);
2552 
2553 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
2554 	    ioc->is_driver_loading || ioc->remove_host) {
2555 		ret = -EAGAIN;
2556 		goto out_unlock_pciaccess;
2557 	}
2558 
2559 	state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2560 	if (state == NON_BLOCKING) {
2561 		if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2562 			ret = -EAGAIN;
2563 			goto out_unlock_pciaccess;
2564 		}
2565 	} else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2566 		ret = -ERESTARTSYS;
2567 		goto out_unlock_pciaccess;
2568 	}
2569 
2570 
2571 	switch (cmd) {
2572 	case MPT3IOCINFO:
2573 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2574 			ret = _ctl_getiocinfo(ioc, arg);
2575 		break;
2576 #ifdef CONFIG_COMPAT
2577 	case MPT3COMMAND32:
2578 #endif
2579 	case MPT3COMMAND:
2580 	{
2581 		struct mpt3_ioctl_command __user *uarg;
2582 		struct mpt3_ioctl_command karg;
2583 
2584 #ifdef CONFIG_COMPAT
2585 		if (compat) {
2586 			ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2587 			break;
2588 		}
2589 #endif
2590 		if (copy_from_user(&karg, arg, sizeof(karg))) {
2591 			pr_err("failure at %s:%d/%s()!\n",
2592 			    __FILE__, __LINE__, __func__);
2593 			ret = -EFAULT;
2594 			break;
2595 		}
2596 
2597 		if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2598 			ret = -EINVAL;
2599 			break;
2600 		}
2601 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2602 			uarg = arg;
2603 			ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2604 		}
2605 		break;
2606 	}
2607 	case MPT3EVENTQUERY:
2608 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2609 			ret = _ctl_eventquery(ioc, arg);
2610 		break;
2611 	case MPT3EVENTENABLE:
2612 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2613 			ret = _ctl_eventenable(ioc, arg);
2614 		break;
2615 	case MPT3EVENTREPORT:
2616 		ret = _ctl_eventreport(ioc, arg);
2617 		break;
2618 	case MPT3HARDRESET:
2619 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2620 			ret = _ctl_do_reset(ioc, arg);
2621 		break;
2622 	case MPT3BTDHMAPPING:
2623 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2624 			ret = _ctl_btdh_mapping(ioc, arg);
2625 		break;
2626 	case MPT3DIAGREGISTER:
2627 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2628 			ret = _ctl_diag_register(ioc, arg);
2629 		break;
2630 	case MPT3DIAGUNREGISTER:
2631 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2632 			ret = _ctl_diag_unregister(ioc, arg);
2633 		break;
2634 	case MPT3DIAGQUERY:
2635 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2636 			ret = _ctl_diag_query(ioc, arg);
2637 		break;
2638 	case MPT3DIAGRELEASE:
2639 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2640 			ret = _ctl_diag_release(ioc, arg);
2641 		break;
2642 	case MPT3DIAGREADBUFFER:
2643 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2644 			ret = _ctl_diag_read_buffer(ioc, arg);
2645 		break;
2646 	default:
2647 		dctlprintk(ioc,
2648 			   ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2649 				    cmd));
2650 		break;
2651 	}
2652 
2653 	mutex_unlock(&ioc->ctl_cmds.mutex);
2654 out_unlock_pciaccess:
2655 	mutex_unlock(&ioc->pci_access_mutex);
2656 	return ret;
2657 }
2658 
2659 /**
2660  * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
2661  * @file: (struct file)
2662  * @cmd: ioctl opcode
2663  * @arg: ?
2664  */
2665 static long
2666 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2667 {
2668 	long ret;
2669 
2670 	/* pass MPI25_VERSION | MPI26_VERSION value,
2671 	 * to indicate that this ioctl cmd
2672 	 * came from mpt3ctl ioctl device.
2673 	 */
2674 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2675 		MPI25_VERSION | MPI26_VERSION);
2676 	return ret;
2677 }
2678 
2679 /**
2680  * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
2681  * @file: (struct file)
2682  * @cmd: ioctl opcode
2683  * @arg: ?
2684  */
2685 static long
2686 _ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2687 {
2688 	long ret;
2689 
2690 	/* pass MPI2_VERSION value, to indicate that this ioctl cmd
2691 	 * came from mpt2ctl ioctl device.
2692 	 */
2693 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2694 	return ret;
2695 }
2696 #ifdef CONFIG_COMPAT
2697 /**
2698  *_ ctl_ioctl_compat - main ioctl entry point (compat)
2699  * @file: ?
2700  * @cmd: ?
2701  * @arg: ?
2702  *
2703  * This routine handles 32 bit applications in 64bit os.
2704  */
2705 static long
2706 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2707 {
2708 	long ret;
2709 
2710 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2711 		MPI25_VERSION | MPI26_VERSION);
2712 	return ret;
2713 }
2714 
2715 /**
2716  *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
2717  * @file: ?
2718  * @cmd: ?
2719  * @arg: ?
2720  *
2721  * This routine handles 32 bit applications in 64bit os.
2722  */
2723 static long
2724 _ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2725 {
2726 	long ret;
2727 
2728 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
2729 	return ret;
2730 }
2731 #endif
2732 
2733 /* scsi host attributes */
2734 /**
2735  * version_fw_show - firmware version
2736  * @cdev: pointer to embedded class device
2737  * @attr: ?
2738  * @buf: the buffer returned
2739  *
2740  * A sysfs 'read-only' shost attribute.
2741  */
2742 static ssize_t
2743 version_fw_show(struct device *cdev, struct device_attribute *attr,
2744 	char *buf)
2745 {
2746 	struct Scsi_Host *shost = class_to_shost(cdev);
2747 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2748 
2749 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2750 	    (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2751 	    (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2752 	    (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2753 	    ioc->facts.FWVersion.Word & 0x000000FF);
2754 }
2755 static DEVICE_ATTR_RO(version_fw);
2756 
2757 /**
2758  * version_bios_show - bios version
2759  * @cdev: pointer to embedded class device
2760  * @attr: ?
2761  * @buf: the buffer returned
2762  *
2763  * A sysfs 'read-only' shost attribute.
2764  */
2765 static ssize_t
2766 version_bios_show(struct device *cdev, struct device_attribute *attr,
2767 	char *buf)
2768 {
2769 	struct Scsi_Host *shost = class_to_shost(cdev);
2770 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2771 
2772 	u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2773 
2774 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2775 	    (version & 0xFF000000) >> 24,
2776 	    (version & 0x00FF0000) >> 16,
2777 	    (version & 0x0000FF00) >> 8,
2778 	    version & 0x000000FF);
2779 }
2780 static DEVICE_ATTR_RO(version_bios);
2781 
2782 /**
2783  * version_mpi_show - MPI (message passing interface) version
2784  * @cdev: pointer to embedded class device
2785  * @attr: ?
2786  * @buf: the buffer returned
2787  *
2788  * A sysfs 'read-only' shost attribute.
2789  */
2790 static ssize_t
2791 version_mpi_show(struct device *cdev, struct device_attribute *attr,
2792 	char *buf)
2793 {
2794 	struct Scsi_Host *shost = class_to_shost(cdev);
2795 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2796 
2797 	return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2798 	    ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2799 }
2800 static DEVICE_ATTR_RO(version_mpi);
2801 
2802 /**
2803  * version_product_show - product name
2804  * @cdev: pointer to embedded class device
2805  * @attr: ?
2806  * @buf: the buffer returned
2807  *
2808  * A sysfs 'read-only' shost attribute.
2809  */
2810 static ssize_t
2811 version_product_show(struct device *cdev, struct device_attribute *attr,
2812 	char *buf)
2813 {
2814 	struct Scsi_Host *shost = class_to_shost(cdev);
2815 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2816 
2817 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2818 }
2819 static DEVICE_ATTR_RO(version_product);
2820 
2821 /**
2822  * version_nvdata_persistent_show - ndvata persistent version
2823  * @cdev: pointer to embedded class device
2824  * @attr: ?
2825  * @buf: the buffer returned
2826  *
2827  * A sysfs 'read-only' shost attribute.
2828  */
2829 static ssize_t
2830 version_nvdata_persistent_show(struct device *cdev,
2831 	struct device_attribute *attr, char *buf)
2832 {
2833 	struct Scsi_Host *shost = class_to_shost(cdev);
2834 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2835 
2836 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2837 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2838 }
2839 static DEVICE_ATTR_RO(version_nvdata_persistent);
2840 
2841 /**
2842  * version_nvdata_default_show - nvdata default version
2843  * @cdev: pointer to embedded class device
2844  * @attr: ?
2845  * @buf: the buffer returned
2846  *
2847  * A sysfs 'read-only' shost attribute.
2848  */
2849 static ssize_t
2850 version_nvdata_default_show(struct device *cdev, struct device_attribute
2851 	*attr, char *buf)
2852 {
2853 	struct Scsi_Host *shost = class_to_shost(cdev);
2854 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2855 
2856 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2857 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2858 }
2859 static DEVICE_ATTR_RO(version_nvdata_default);
2860 
2861 /**
2862  * board_name_show - board name
2863  * @cdev: pointer to embedded class device
2864  * @attr: ?
2865  * @buf: the buffer returned
2866  *
2867  * A sysfs 'read-only' shost attribute.
2868  */
2869 static ssize_t
2870 board_name_show(struct device *cdev, struct device_attribute *attr,
2871 	char *buf)
2872 {
2873 	struct Scsi_Host *shost = class_to_shost(cdev);
2874 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2875 
2876 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2877 }
2878 static DEVICE_ATTR_RO(board_name);
2879 
2880 /**
2881  * board_assembly_show - board assembly name
2882  * @cdev: pointer to embedded class device
2883  * @attr: ?
2884  * @buf: the buffer returned
2885  *
2886  * A sysfs 'read-only' shost attribute.
2887  */
2888 static ssize_t
2889 board_assembly_show(struct device *cdev, struct device_attribute *attr,
2890 	char *buf)
2891 {
2892 	struct Scsi_Host *shost = class_to_shost(cdev);
2893 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2894 
2895 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2896 }
2897 static DEVICE_ATTR_RO(board_assembly);
2898 
2899 /**
2900  * board_tracer_show - board tracer number
2901  * @cdev: pointer to embedded class device
2902  * @attr: ?
2903  * @buf: the buffer returned
2904  *
2905  * A sysfs 'read-only' shost attribute.
2906  */
2907 static ssize_t
2908 board_tracer_show(struct device *cdev, struct device_attribute *attr,
2909 	char *buf)
2910 {
2911 	struct Scsi_Host *shost = class_to_shost(cdev);
2912 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2913 
2914 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2915 }
2916 static DEVICE_ATTR_RO(board_tracer);
2917 
2918 /**
2919  * io_delay_show - io missing delay
2920  * @cdev: pointer to embedded class device
2921  * @attr: ?
2922  * @buf: the buffer returned
2923  *
2924  * This is for firmware implemention for deboucing device
2925  * removal events.
2926  *
2927  * A sysfs 'read-only' shost attribute.
2928  */
2929 static ssize_t
2930 io_delay_show(struct device *cdev, struct device_attribute *attr,
2931 	char *buf)
2932 {
2933 	struct Scsi_Host *shost = class_to_shost(cdev);
2934 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2935 
2936 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2937 }
2938 static DEVICE_ATTR_RO(io_delay);
2939 
2940 /**
2941  * device_delay_show - device missing delay
2942  * @cdev: pointer to embedded class device
2943  * @attr: ?
2944  * @buf: the buffer returned
2945  *
2946  * This is for firmware implemention for deboucing device
2947  * removal events.
2948  *
2949  * A sysfs 'read-only' shost attribute.
2950  */
2951 static ssize_t
2952 device_delay_show(struct device *cdev, struct device_attribute *attr,
2953 	char *buf)
2954 {
2955 	struct Scsi_Host *shost = class_to_shost(cdev);
2956 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2957 
2958 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2959 }
2960 static DEVICE_ATTR_RO(device_delay);
2961 
2962 /**
2963  * fw_queue_depth_show - global credits
2964  * @cdev: pointer to embedded class device
2965  * @attr: ?
2966  * @buf: the buffer returned
2967  *
2968  * This is firmware queue depth limit
2969  *
2970  * A sysfs 'read-only' shost attribute.
2971  */
2972 static ssize_t
2973 fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2974 	char *buf)
2975 {
2976 	struct Scsi_Host *shost = class_to_shost(cdev);
2977 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2978 
2979 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2980 }
2981 static DEVICE_ATTR_RO(fw_queue_depth);
2982 
2983 /**
2984  * sas_address_show - sas address
2985  * @cdev: pointer to embedded class device
2986  * @attr: ?
2987  * @buf: the buffer returned
2988  *
2989  * This is the controller sas address
2990  *
2991  * A sysfs 'read-only' shost attribute.
2992  */
2993 static ssize_t
2994 host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2995 	char *buf)
2996 
2997 {
2998 	struct Scsi_Host *shost = class_to_shost(cdev);
2999 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3000 
3001 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3002 	    (unsigned long long)ioc->sas_hba.sas_address);
3003 }
3004 static DEVICE_ATTR_RO(host_sas_address);
3005 
3006 /**
3007  * logging_level_show - logging level
3008  * @cdev: pointer to embedded class device
3009  * @attr: ?
3010  * @buf: the buffer returned
3011  *
3012  * A sysfs 'read/write' shost attribute.
3013  */
3014 static ssize_t
3015 logging_level_show(struct device *cdev, struct device_attribute *attr,
3016 	char *buf)
3017 {
3018 	struct Scsi_Host *shost = class_to_shost(cdev);
3019 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3020 
3021 	return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
3022 }
3023 static ssize_t
3024 logging_level_store(struct device *cdev, struct device_attribute *attr,
3025 	const char *buf, size_t count)
3026 {
3027 	struct Scsi_Host *shost = class_to_shost(cdev);
3028 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3029 	int val = 0;
3030 
3031 	if (sscanf(buf, "%x", &val) != 1)
3032 		return -EINVAL;
3033 
3034 	ioc->logging_level = val;
3035 	ioc_info(ioc, "logging_level=%08xh\n",
3036 		 ioc->logging_level);
3037 	return strlen(buf);
3038 }
3039 static DEVICE_ATTR_RW(logging_level);
3040 
3041 /**
3042  * fwfault_debug_show - show/store fwfault_debug
3043  * @cdev: pointer to embedded class device
3044  * @attr: ?
3045  * @buf: the buffer returned
3046  *
3047  * mpt3sas_fwfault_debug is command line option
3048  * A sysfs 'read/write' shost attribute.
3049  */
3050 static ssize_t
3051 fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
3052 	char *buf)
3053 {
3054 	struct Scsi_Host *shost = class_to_shost(cdev);
3055 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3056 
3057 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
3058 }
3059 static ssize_t
3060 fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
3061 	const char *buf, size_t count)
3062 {
3063 	struct Scsi_Host *shost = class_to_shost(cdev);
3064 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3065 	int val = 0;
3066 
3067 	if (sscanf(buf, "%d", &val) != 1)
3068 		return -EINVAL;
3069 
3070 	ioc->fwfault_debug = val;
3071 	ioc_info(ioc, "fwfault_debug=%d\n",
3072 		 ioc->fwfault_debug);
3073 	return strlen(buf);
3074 }
3075 static DEVICE_ATTR_RW(fwfault_debug);
3076 
3077 /**
3078  * ioc_reset_count_show - ioc reset count
3079  * @cdev: pointer to embedded class device
3080  * @attr: ?
3081  * @buf: the buffer returned
3082  *
3083  * This is firmware queue depth limit
3084  *
3085  * A sysfs 'read-only' shost attribute.
3086  */
3087 static ssize_t
3088 ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
3089 	char *buf)
3090 {
3091 	struct Scsi_Host *shost = class_to_shost(cdev);
3092 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3093 
3094 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
3095 }
3096 static DEVICE_ATTR_RO(ioc_reset_count);
3097 
3098 /**
3099  * reply_queue_count_show - number of reply queues
3100  * @cdev: pointer to embedded class device
3101  * @attr: ?
3102  * @buf: the buffer returned
3103  *
3104  * This is number of reply queues
3105  *
3106  * A sysfs 'read-only' shost attribute.
3107  */
3108 static ssize_t
3109 reply_queue_count_show(struct device *cdev,
3110 	struct device_attribute *attr, char *buf)
3111 {
3112 	u8 reply_queue_count;
3113 	struct Scsi_Host *shost = class_to_shost(cdev);
3114 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3115 
3116 	if ((ioc->facts.IOCCapabilities &
3117 	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
3118 		reply_queue_count = ioc->reply_queue_count;
3119 	else
3120 		reply_queue_count = 1;
3121 
3122 	return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
3123 }
3124 static DEVICE_ATTR_RO(reply_queue_count);
3125 
3126 /**
3127  * BRM_status_show - Backup Rail Monitor Status
3128  * @cdev: pointer to embedded class device
3129  * @attr: ?
3130  * @buf: the buffer returned
3131  *
3132  * This is number of reply queues
3133  *
3134  * A sysfs 'read-only' shost attribute.
3135  */
3136 static ssize_t
3137 BRM_status_show(struct device *cdev, struct device_attribute *attr,
3138 	char *buf)
3139 {
3140 	struct Scsi_Host *shost = class_to_shost(cdev);
3141 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3142 	Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
3143 	Mpi2ConfigReply_t mpi_reply;
3144 	u16 backup_rail_monitor_status = 0;
3145 	u16 ioc_status;
3146 	int sz;
3147 	ssize_t rc = 0;
3148 
3149 	if (!ioc->is_warpdrive) {
3150 		ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
3151 			__func__);
3152 		return 0;
3153 	}
3154 	/* pci_access_mutex lock acquired by sysfs show path */
3155 	mutex_lock(&ioc->pci_access_mutex);
3156 	if (ioc->pci_error_recovery || ioc->remove_host)
3157 		goto out;
3158 
3159 	/* allocate upto GPIOVal 36 entries */
3160 	sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
3161 	io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
3162 	if (!io_unit_pg3) {
3163 		rc = -ENOMEM;
3164 		ioc_err(ioc, "%s: failed allocating memory for iounit_pg3: (%d) bytes\n",
3165 			__func__, sz);
3166 		goto out;
3167 	}
3168 
3169 	if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
3170 	    0) {
3171 		ioc_err(ioc, "%s: failed reading iounit_pg3\n",
3172 			__func__);
3173 		rc = -EINVAL;
3174 		goto out;
3175 	}
3176 
3177 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3178 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3179 		ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
3180 			__func__, ioc_status);
3181 		rc = -EINVAL;
3182 		goto out;
3183 	}
3184 
3185 	if (io_unit_pg3->GPIOCount < 25) {
3186 		ioc_err(ioc, "%s: iounit_pg3->GPIOCount less than 25 entries, detected (%d) entries\n",
3187 			__func__, io_unit_pg3->GPIOCount);
3188 		rc = -EINVAL;
3189 		goto out;
3190 	}
3191 
3192 	/* BRM status is in bit zero of GPIOVal[24] */
3193 	backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
3194 	rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
3195 
3196  out:
3197 	kfree(io_unit_pg3);
3198 	mutex_unlock(&ioc->pci_access_mutex);
3199 	return rc;
3200 }
3201 static DEVICE_ATTR_RO(BRM_status);
3202 
3203 struct DIAG_BUFFER_START {
3204 	__le32	Size;
3205 	__le32	DiagVersion;
3206 	u8	BufferType;
3207 	u8	Reserved[3];
3208 	__le32	Reserved1;
3209 	__le32	Reserved2;
3210 	__le32	Reserved3;
3211 };
3212 
3213 /**
3214  * host_trace_buffer_size_show - host buffer size (trace only)
3215  * @cdev: pointer to embedded class device
3216  * @attr: ?
3217  * @buf: the buffer returned
3218  *
3219  * A sysfs 'read-only' shost attribute.
3220  */
3221 static ssize_t
3222 host_trace_buffer_size_show(struct device *cdev,
3223 	struct device_attribute *attr, char *buf)
3224 {
3225 	struct Scsi_Host *shost = class_to_shost(cdev);
3226 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3227 	u32 size = 0;
3228 	struct DIAG_BUFFER_START *request_data;
3229 
3230 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3231 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3232 			__func__);
3233 		return 0;
3234 	}
3235 
3236 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3237 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3238 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3239 			__func__);
3240 		return 0;
3241 	}
3242 
3243 	request_data = (struct DIAG_BUFFER_START *)
3244 	    ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
3245 	if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
3246 	    le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
3247 	    le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
3248 	    le32_to_cpu(request_data->Reserved3) == 0x4742444c)
3249 		size = le32_to_cpu(request_data->Size);
3250 
3251 	ioc->ring_buffer_sz = size;
3252 	return snprintf(buf, PAGE_SIZE, "%d\n", size);
3253 }
3254 static DEVICE_ATTR_RO(host_trace_buffer_size);
3255 
3256 /**
3257  * host_trace_buffer_show - firmware ring buffer (trace only)
3258  * @cdev: pointer to embedded class device
3259  * @attr: ?
3260  * @buf: the buffer returned
3261  *
3262  * A sysfs 'read/write' shost attribute.
3263  *
3264  * You will only be able to read 4k bytes of ring buffer at a time.
3265  * In order to read beyond 4k bytes, you will have to write out the
3266  * offset to the same attribute, it will move the pointer.
3267  */
3268 static ssize_t
3269 host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
3270 	char *buf)
3271 {
3272 	struct Scsi_Host *shost = class_to_shost(cdev);
3273 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3274 	void *request_data;
3275 	u32 size;
3276 
3277 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3278 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3279 			__func__);
3280 		return 0;
3281 	}
3282 
3283 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3284 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3285 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3286 			__func__);
3287 		return 0;
3288 	}
3289 
3290 	if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3291 		return 0;
3292 
3293 	size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3294 	size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3295 	request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3296 	memcpy(buf, request_data, size);
3297 	return size;
3298 }
3299 
3300 static ssize_t
3301 host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
3302 	const char *buf, size_t count)
3303 {
3304 	struct Scsi_Host *shost = class_to_shost(cdev);
3305 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3306 	int val = 0;
3307 
3308 	if (sscanf(buf, "%d", &val) != 1)
3309 		return -EINVAL;
3310 
3311 	ioc->ring_buffer_offset = val;
3312 	return strlen(buf);
3313 }
3314 static DEVICE_ATTR_RW(host_trace_buffer);
3315 
3316 
3317 /*****************************************/
3318 
3319 /**
3320  * host_trace_buffer_enable_show - firmware ring buffer (trace only)
3321  * @cdev: pointer to embedded class device
3322  * @attr: ?
3323  * @buf: the buffer returned
3324  *
3325  * A sysfs 'read/write' shost attribute.
3326  *
3327  * This is a mechnism to post/release host_trace_buffers
3328  */
3329 static ssize_t
3330 host_trace_buffer_enable_show(struct device *cdev,
3331 	struct device_attribute *attr, char *buf)
3332 {
3333 	struct Scsi_Host *shost = class_to_shost(cdev);
3334 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3335 
3336 	if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3337 	   ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3338 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3339 		return snprintf(buf, PAGE_SIZE, "off\n");
3340 	else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3341 	    MPT3_DIAG_BUFFER_IS_RELEASED))
3342 		return snprintf(buf, PAGE_SIZE, "release\n");
3343 	else
3344 		return snprintf(buf, PAGE_SIZE, "post\n");
3345 }
3346 
3347 static ssize_t
3348 host_trace_buffer_enable_store(struct device *cdev,
3349 	struct device_attribute *attr, const char *buf, size_t count)
3350 {
3351 	struct Scsi_Host *shost = class_to_shost(cdev);
3352 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3353 	char str[10] = "";
3354 	struct mpt3_diag_register diag_register;
3355 	u8 issue_reset = 0;
3356 
3357 	/* don't allow post/release occurr while recovery is active */
3358 	if (ioc->shost_recovery || ioc->remove_host ||
3359 	    ioc->pci_error_recovery || ioc->is_driver_loading)
3360 		return -EBUSY;
3361 
3362 	if (sscanf(buf, "%9s", str) != 1)
3363 		return -EINVAL;
3364 
3365 	if (!strcmp(str, "post")) {
3366 		/* exit out if host buffers are already posted */
3367 		if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3368 		    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3369 		    MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3370 		    ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3371 		    MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3372 			goto out;
3373 		memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3374 		ioc_info(ioc, "posting host trace buffers\n");
3375 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3376 
3377 		if (ioc->manu_pg11.HostTraceBufferMaxSizeKB != 0 &&
3378 		    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0) {
3379 			/* post the same buffer allocated previously */
3380 			diag_register.requested_buffer_size =
3381 			    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE];
3382 		} else {
3383 			/*
3384 			 * Free the diag buffer memory which was previously
3385 			 * allocated by an application.
3386 			 */
3387 			if ((ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0)
3388 			    &&
3389 			    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3390 			    MPT3_DIAG_BUFFER_IS_APP_OWNED)) {
3391 				dma_free_coherent(&ioc->pdev->dev,
3392 						  ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE],
3393 						  ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE],
3394 						  ioc->diag_buffer_dma[MPI2_DIAG_BUF_TYPE_TRACE]);
3395 				ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE] =
3396 				    NULL;
3397 			}
3398 
3399 			diag_register.requested_buffer_size = (1024 * 1024);
3400 		}
3401 
3402 		diag_register.unique_id =
3403 		    (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
3404 		    (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
3405 		ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3406 		_ctl_diag_register_2(ioc,  &diag_register);
3407 		if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3408 		    MPT3_DIAG_BUFFER_IS_REGISTERED) {
3409 			ioc_info(ioc,
3410 			    "Trace buffer %d KB allocated through sysfs\n",
3411 			    diag_register.requested_buffer_size>>10);
3412 			if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
3413 				ioc->diag_buffer_status[
3414 				    MPI2_DIAG_BUF_TYPE_TRACE] |=
3415 				    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
3416 		}
3417 	} else if (!strcmp(str, "release")) {
3418 		/* exit out if host buffers are already released */
3419 		if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3420 			goto out;
3421 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3422 		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3423 			goto out;
3424 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3425 		    MPT3_DIAG_BUFFER_IS_RELEASED))
3426 			goto out;
3427 		ioc_info(ioc, "releasing host trace buffer\n");
3428 		mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3429 		    &issue_reset);
3430 	}
3431 
3432  out:
3433 	return strlen(buf);
3434 }
3435 static DEVICE_ATTR_RW(host_trace_buffer_enable);
3436 
3437 /*********** diagnostic trigger suppport *********************************/
3438 
3439 /**
3440  * diag_trigger_master_show - show the diag_trigger_master attribute
3441  * @cdev: pointer to embedded class device
3442  * @attr: ?
3443  * @buf: the buffer returned
3444  *
3445  * A sysfs 'read/write' shost attribute.
3446  */
3447 static ssize_t
3448 diag_trigger_master_show(struct device *cdev,
3449 	struct device_attribute *attr, char *buf)
3450 
3451 {
3452 	struct Scsi_Host *shost = class_to_shost(cdev);
3453 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3454 	unsigned long flags;
3455 	ssize_t rc;
3456 
3457 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3458 	rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3459 	memcpy(buf, &ioc->diag_trigger_master, rc);
3460 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3461 	return rc;
3462 }
3463 
3464 /**
3465  * diag_trigger_master_store - store the diag_trigger_master attribute
3466  * @cdev: pointer to embedded class device
3467  * @attr: ?
3468  * @buf: the buffer returned
3469  * @count: ?
3470  *
3471  * A sysfs 'read/write' shost attribute.
3472  */
3473 static ssize_t
3474 diag_trigger_master_store(struct device *cdev,
3475 	struct device_attribute *attr, const char *buf, size_t count)
3476 
3477 {
3478 	struct Scsi_Host *shost = class_to_shost(cdev);
3479 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3480 	unsigned long flags;
3481 	ssize_t rc;
3482 
3483 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3484 	rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3485 	memset(&ioc->diag_trigger_master, 0,
3486 	    sizeof(struct SL_WH_MASTER_TRIGGER_T));
3487 	memcpy(&ioc->diag_trigger_master, buf, rc);
3488 	ioc->diag_trigger_master.MasterData |=
3489 	    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3490 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3491 	return rc;
3492 }
3493 static DEVICE_ATTR_RW(diag_trigger_master);
3494 
3495 
3496 /**
3497  * diag_trigger_event_show - show the diag_trigger_event attribute
3498  * @cdev: pointer to embedded class device
3499  * @attr: ?
3500  * @buf: the buffer returned
3501  *
3502  * A sysfs 'read/write' shost attribute.
3503  */
3504 static ssize_t
3505 diag_trigger_event_show(struct device *cdev,
3506 	struct device_attribute *attr, char *buf)
3507 {
3508 	struct Scsi_Host *shost = class_to_shost(cdev);
3509 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3510 	unsigned long flags;
3511 	ssize_t rc;
3512 
3513 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3514 	rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3515 	memcpy(buf, &ioc->diag_trigger_event, rc);
3516 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3517 	return rc;
3518 }
3519 
3520 /**
3521  * diag_trigger_event_store - store the diag_trigger_event attribute
3522  * @cdev: pointer to embedded class device
3523  * @attr: ?
3524  * @buf: the buffer returned
3525  * @count: ?
3526  *
3527  * A sysfs 'read/write' shost attribute.
3528  */
3529 static ssize_t
3530 diag_trigger_event_store(struct device *cdev,
3531 	struct device_attribute *attr, const char *buf, size_t count)
3532 
3533 {
3534 	struct Scsi_Host *shost = class_to_shost(cdev);
3535 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3536 	unsigned long flags;
3537 	ssize_t sz;
3538 
3539 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3540 	sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3541 	memset(&ioc->diag_trigger_event, 0,
3542 	    sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3543 	memcpy(&ioc->diag_trigger_event, buf, sz);
3544 	if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3545 		ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3546 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3547 	return sz;
3548 }
3549 static DEVICE_ATTR_RW(diag_trigger_event);
3550 
3551 
3552 /**
3553  * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
3554  * @cdev: pointer to embedded class device
3555  * @attr: ?
3556  * @buf: the buffer returned
3557  *
3558  * A sysfs 'read/write' shost attribute.
3559  */
3560 static ssize_t
3561 diag_trigger_scsi_show(struct device *cdev,
3562 	struct device_attribute *attr, char *buf)
3563 {
3564 	struct Scsi_Host *shost = class_to_shost(cdev);
3565 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3566 	unsigned long flags;
3567 	ssize_t rc;
3568 
3569 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3570 	rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3571 	memcpy(buf, &ioc->diag_trigger_scsi, rc);
3572 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3573 	return rc;
3574 }
3575 
3576 /**
3577  * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
3578  * @cdev: pointer to embedded class device
3579  * @attr: ?
3580  * @buf: the buffer returned
3581  * @count: ?
3582  *
3583  * A sysfs 'read/write' shost attribute.
3584  */
3585 static ssize_t
3586 diag_trigger_scsi_store(struct device *cdev,
3587 	struct device_attribute *attr, const char *buf, size_t count)
3588 {
3589 	struct Scsi_Host *shost = class_to_shost(cdev);
3590 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3591 	unsigned long flags;
3592 	ssize_t sz;
3593 
3594 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3595 	sz = min(sizeof(ioc->diag_trigger_scsi), count);
3596 	memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
3597 	memcpy(&ioc->diag_trigger_scsi, buf, sz);
3598 	if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3599 		ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3600 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3601 	return sz;
3602 }
3603 static DEVICE_ATTR_RW(diag_trigger_scsi);
3604 
3605 
3606 /**
3607  * diag_trigger_scsi_show - show the diag_trigger_mpi attribute
3608  * @cdev: pointer to embedded class device
3609  * @attr: ?
3610  * @buf: the buffer returned
3611  *
3612  * A sysfs 'read/write' shost attribute.
3613  */
3614 static ssize_t
3615 diag_trigger_mpi_show(struct device *cdev,
3616 	struct device_attribute *attr, char *buf)
3617 {
3618 	struct Scsi_Host *shost = class_to_shost(cdev);
3619 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3620 	unsigned long flags;
3621 	ssize_t rc;
3622 
3623 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3624 	rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3625 	memcpy(buf, &ioc->diag_trigger_mpi, rc);
3626 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3627 	return rc;
3628 }
3629 
3630 /**
3631  * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
3632  * @cdev: pointer to embedded class device
3633  * @attr: ?
3634  * @buf: the buffer returned
3635  * @count: ?
3636  *
3637  * A sysfs 'read/write' shost attribute.
3638  */
3639 static ssize_t
3640 diag_trigger_mpi_store(struct device *cdev,
3641 	struct device_attribute *attr, const char *buf, size_t count)
3642 {
3643 	struct Scsi_Host *shost = class_to_shost(cdev);
3644 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3645 	unsigned long flags;
3646 	ssize_t sz;
3647 
3648 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3649 	sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3650 	memset(&ioc->diag_trigger_mpi, 0,
3651 	    sizeof(ioc->diag_trigger_mpi));
3652 	memcpy(&ioc->diag_trigger_mpi, buf, sz);
3653 	if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3654 		ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3655 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3656 	return sz;
3657 }
3658 
3659 static DEVICE_ATTR_RW(diag_trigger_mpi);
3660 
3661 /*********** diagnostic trigger suppport *** END ****************************/
3662 
3663 /*****************************************/
3664 
3665 /**
3666  * drv_support_bitmap_show - driver supported feature bitmap
3667  * @cdev: pointer to embedded class device
3668  * @attr: unused
3669  * @buf: the buffer returned
3670  *
3671  * A sysfs 'read-only' shost attribute.
3672  */
3673 static ssize_t
3674 drv_support_bitmap_show(struct device *cdev,
3675 	struct device_attribute *attr, char *buf)
3676 {
3677 	struct Scsi_Host *shost = class_to_shost(cdev);
3678 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3679 
3680 	return snprintf(buf, PAGE_SIZE, "0x%08x\n", ioc->drv_support_bitmap);
3681 }
3682 static DEVICE_ATTR_RO(drv_support_bitmap);
3683 
3684 /**
3685  * enable_sdev_max_qd_show - display whether sdev max qd is enabled/disabled
3686  * @cdev: pointer to embedded class device
3687  * @attr: unused
3688  * @buf: the buffer returned
3689  *
3690  * A sysfs read/write shost attribute. This attribute is used to set the
3691  * targets queue depth to HBA IO queue depth if this attribute is enabled.
3692  */
3693 static ssize_t
3694 enable_sdev_max_qd_show(struct device *cdev,
3695 	struct device_attribute *attr, char *buf)
3696 {
3697 	struct Scsi_Host *shost = class_to_shost(cdev);
3698 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3699 
3700 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->enable_sdev_max_qd);
3701 }
3702 
3703 /**
3704  * enable_sdev_max_qd_store - Enable/disable sdev max qd
3705  * @cdev: pointer to embedded class device
3706  * @attr: unused
3707  * @buf: the buffer returned
3708  * @count: unused
3709  *
3710  * A sysfs read/write shost attribute. This attribute is used to set the
3711  * targets queue depth to HBA IO queue depth if this attribute is enabled.
3712  * If this attribute is disabled then targets will have corresponding default
3713  * queue depth.
3714  */
3715 static ssize_t
3716 enable_sdev_max_qd_store(struct device *cdev,
3717 	struct device_attribute *attr, const char *buf, size_t count)
3718 {
3719 	struct Scsi_Host *shost = class_to_shost(cdev);
3720 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3721 	struct MPT3SAS_DEVICE *sas_device_priv_data;
3722 	struct MPT3SAS_TARGET *sas_target_priv_data;
3723 	int val = 0;
3724 	struct scsi_device *sdev;
3725 	struct _raid_device *raid_device;
3726 	int qdepth;
3727 
3728 	if (kstrtoint(buf, 0, &val) != 0)
3729 		return -EINVAL;
3730 
3731 	switch (val) {
3732 	case 0:
3733 		ioc->enable_sdev_max_qd = 0;
3734 		shost_for_each_device(sdev, ioc->shost) {
3735 			sas_device_priv_data = sdev->hostdata;
3736 			if (!sas_device_priv_data)
3737 				continue;
3738 			sas_target_priv_data = sas_device_priv_data->sas_target;
3739 			if (!sas_target_priv_data)
3740 				continue;
3741 
3742 			if (sas_target_priv_data->flags &
3743 			    MPT_TARGET_FLAGS_VOLUME) {
3744 				raid_device =
3745 				    mpt3sas_raid_device_find_by_handle(ioc,
3746 				    sas_target_priv_data->handle);
3747 
3748 				switch (raid_device->volume_type) {
3749 				case MPI2_RAID_VOL_TYPE_RAID0:
3750 					if (raid_device->device_info &
3751 					    MPI2_SAS_DEVICE_INFO_SSP_TARGET)
3752 						qdepth =
3753 						    MPT3SAS_SAS_QUEUE_DEPTH;
3754 					else
3755 						qdepth =
3756 						    MPT3SAS_SATA_QUEUE_DEPTH;
3757 					break;
3758 				case MPI2_RAID_VOL_TYPE_RAID1E:
3759 				case MPI2_RAID_VOL_TYPE_RAID1:
3760 				case MPI2_RAID_VOL_TYPE_RAID10:
3761 				case MPI2_RAID_VOL_TYPE_UNKNOWN:
3762 				default:
3763 					qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
3764 				}
3765 			} else if (sas_target_priv_data->flags &
3766 			    MPT_TARGET_FLAGS_PCIE_DEVICE)
3767 				qdepth = MPT3SAS_NVME_QUEUE_DEPTH;
3768 			else
3769 				qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
3770 
3771 			mpt3sas_scsih_change_queue_depth(sdev, qdepth);
3772 		}
3773 		break;
3774 	case 1:
3775 		ioc->enable_sdev_max_qd = 1;
3776 		shost_for_each_device(sdev, ioc->shost)
3777 			mpt3sas_scsih_change_queue_depth(sdev,
3778 			    shost->can_queue);
3779 		break;
3780 	default:
3781 		return -EINVAL;
3782 	}
3783 
3784 	return strlen(buf);
3785 }
3786 static DEVICE_ATTR_RW(enable_sdev_max_qd);
3787 
3788 struct device_attribute *mpt3sas_host_attrs[] = {
3789 	&dev_attr_version_fw,
3790 	&dev_attr_version_bios,
3791 	&dev_attr_version_mpi,
3792 	&dev_attr_version_product,
3793 	&dev_attr_version_nvdata_persistent,
3794 	&dev_attr_version_nvdata_default,
3795 	&dev_attr_board_name,
3796 	&dev_attr_board_assembly,
3797 	&dev_attr_board_tracer,
3798 	&dev_attr_io_delay,
3799 	&dev_attr_device_delay,
3800 	&dev_attr_logging_level,
3801 	&dev_attr_fwfault_debug,
3802 	&dev_attr_fw_queue_depth,
3803 	&dev_attr_host_sas_address,
3804 	&dev_attr_ioc_reset_count,
3805 	&dev_attr_host_trace_buffer_size,
3806 	&dev_attr_host_trace_buffer,
3807 	&dev_attr_host_trace_buffer_enable,
3808 	&dev_attr_reply_queue_count,
3809 	&dev_attr_diag_trigger_master,
3810 	&dev_attr_diag_trigger_event,
3811 	&dev_attr_diag_trigger_scsi,
3812 	&dev_attr_diag_trigger_mpi,
3813 	&dev_attr_drv_support_bitmap,
3814 	&dev_attr_BRM_status,
3815 	&dev_attr_enable_sdev_max_qd,
3816 	NULL,
3817 };
3818 
3819 /* device attributes */
3820 
3821 /**
3822  * sas_address_show - sas address
3823  * @dev: pointer to embedded class device
3824  * @attr: ?
3825  * @buf: the buffer returned
3826  *
3827  * This is the sas address for the target
3828  *
3829  * A sysfs 'read-only' shost attribute.
3830  */
3831 static ssize_t
3832 sas_address_show(struct device *dev, struct device_attribute *attr,
3833 	char *buf)
3834 {
3835 	struct scsi_device *sdev = to_scsi_device(dev);
3836 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3837 
3838 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3839 	    (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3840 }
3841 static DEVICE_ATTR_RO(sas_address);
3842 
3843 /**
3844  * sas_device_handle_show - device handle
3845  * @dev: pointer to embedded class device
3846  * @attr: ?
3847  * @buf: the buffer returned
3848  *
3849  * This is the firmware assigned device handle
3850  *
3851  * A sysfs 'read-only' shost attribute.
3852  */
3853 static ssize_t
3854 sas_device_handle_show(struct device *dev, struct device_attribute *attr,
3855 	char *buf)
3856 {
3857 	struct scsi_device *sdev = to_scsi_device(dev);
3858 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3859 
3860 	return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3861 	    sas_device_priv_data->sas_target->handle);
3862 }
3863 static DEVICE_ATTR_RO(sas_device_handle);
3864 
3865 /**
3866  * sas_ncq_io_prio_show - send prioritized io commands to device
3867  * @dev: pointer to embedded device
3868  * @attr: ?
3869  * @buf: the buffer returned
3870  *
3871  * A sysfs 'read/write' sdev attribute, only works with SATA
3872  */
3873 static ssize_t
3874 sas_ncq_prio_enable_show(struct device *dev,
3875 				 struct device_attribute *attr, char *buf)
3876 {
3877 	struct scsi_device *sdev = to_scsi_device(dev);
3878 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3879 
3880 	return snprintf(buf, PAGE_SIZE, "%d\n",
3881 			sas_device_priv_data->ncq_prio_enable);
3882 }
3883 
3884 static ssize_t
3885 sas_ncq_prio_enable_store(struct device *dev,
3886 				  struct device_attribute *attr,
3887 				  const char *buf, size_t count)
3888 {
3889 	struct scsi_device *sdev = to_scsi_device(dev);
3890 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3891 	bool ncq_prio_enable = 0;
3892 
3893 	if (kstrtobool(buf, &ncq_prio_enable))
3894 		return -EINVAL;
3895 
3896 	if (!scsih_ncq_prio_supp(sdev))
3897 		return -EINVAL;
3898 
3899 	sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
3900 	return strlen(buf);
3901 }
3902 static DEVICE_ATTR_RW(sas_ncq_prio_enable);
3903 
3904 struct device_attribute *mpt3sas_dev_attrs[] = {
3905 	&dev_attr_sas_address,
3906 	&dev_attr_sas_device_handle,
3907 	&dev_attr_sas_ncq_prio_enable,
3908 	NULL,
3909 };
3910 
3911 /* file operations table for mpt3ctl device */
3912 static const struct file_operations ctl_fops = {
3913 	.owner = THIS_MODULE,
3914 	.unlocked_ioctl = _ctl_ioctl,
3915 	.poll = _ctl_poll,
3916 	.fasync = _ctl_fasync,
3917 #ifdef CONFIG_COMPAT
3918 	.compat_ioctl = _ctl_ioctl_compat,
3919 #endif
3920 };
3921 
3922 /* file operations table for mpt2ctl device */
3923 static const struct file_operations ctl_gen2_fops = {
3924 	.owner = THIS_MODULE,
3925 	.unlocked_ioctl = _ctl_mpt2_ioctl,
3926 	.poll = _ctl_poll,
3927 	.fasync = _ctl_fasync,
3928 #ifdef CONFIG_COMPAT
3929 	.compat_ioctl = _ctl_mpt2_ioctl_compat,
3930 #endif
3931 };
3932 
3933 static struct miscdevice ctl_dev = {
3934 	.minor  = MPT3SAS_MINOR,
3935 	.name   = MPT3SAS_DEV_NAME,
3936 	.fops   = &ctl_fops,
3937 };
3938 
3939 static struct miscdevice gen2_ctl_dev = {
3940 	.minor  = MPT2SAS_MINOR,
3941 	.name   = MPT2SAS_DEV_NAME,
3942 	.fops   = &ctl_gen2_fops,
3943 };
3944 
3945 /**
3946  * mpt3sas_ctl_init - main entry point for ctl.
3947  * @hbas_to_enumerate: ?
3948  */
3949 void
3950 mpt3sas_ctl_init(ushort hbas_to_enumerate)
3951 {
3952 	async_queue = NULL;
3953 
3954 	/* Don't register mpt3ctl ioctl device if
3955 	 * hbas_to_enumarate is one.
3956 	 */
3957 	if (hbas_to_enumerate != 1)
3958 		if (misc_register(&ctl_dev) < 0)
3959 			pr_err("%s can't register misc device [minor=%d]\n",
3960 			    MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3961 
3962 	/* Don't register mpt3ctl ioctl device if
3963 	 * hbas_to_enumarate is two.
3964 	 */
3965 	if (hbas_to_enumerate != 2)
3966 		if (misc_register(&gen2_ctl_dev) < 0)
3967 			pr_err("%s can't register misc device [minor=%d]\n",
3968 			    MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3969 
3970 	init_waitqueue_head(&ctl_poll_wait);
3971 }
3972 
3973 /**
3974  * mpt3sas_ctl_exit - exit point for ctl
3975  * @hbas_to_enumerate: ?
3976  */
3977 void
3978 mpt3sas_ctl_exit(ushort hbas_to_enumerate)
3979 {
3980 	struct MPT3SAS_ADAPTER *ioc;
3981 	int i;
3982 
3983 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3984 
3985 		/* free memory associated to diag buffers */
3986 		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3987 			if (!ioc->diag_buffer[i])
3988 				continue;
3989 			dma_free_coherent(&ioc->pdev->dev,
3990 					  ioc->diag_buffer_sz[i],
3991 					  ioc->diag_buffer[i],
3992 					  ioc->diag_buffer_dma[i]);
3993 			ioc->diag_buffer[i] = NULL;
3994 			ioc->diag_buffer_status[i] = 0;
3995 		}
3996 
3997 		kfree(ioc->event_log);
3998 	}
3999 	if (hbas_to_enumerate != 1)
4000 		misc_deregister(&ctl_dev);
4001 	if (hbas_to_enumerate != 2)
4002 		misc_deregister(&gen2_ctl_dev);
4003 }
4004