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