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