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