mpt3sas_ctl.c 110 KB
Newer Older
1 2 3 4 5
/*
 * Management Module Support for MPT (Message Passing Technology) based
 * controllers
 *
 * This code is based on drivers/scsi/mpt3sas/mpt3sas_ctl.c
6
 * Copyright (C) 2012-2014  LSI Corporation
7 8
 * Copyright (C) 2013-2014 Avago Technologies
 *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * NO WARRANTY
 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
 * solely responsible for determining the appropriateness of using and
 * distributing the Program and assumes all risks associated with its
 * exercise of rights under this Agreement, including but not limited to
 * the risks and costs of program errors, damage to or loss of data,
 * programs or equipment, and unavailability or interruption of operations.

 * DISCLAIMER OF LIABILITY
 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/compat.h>
#include <linux/poll.h>

#include <linux/io.h>
#include <linux/uaccess.h>

#include "mpt3sas_base.h"
#include "mpt3sas_ctl.h"


static struct fasync_struct *async_queue;
static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);


/**
 * enum block_state - blocking state
 * @NON_BLOCKING: non blocking
 * @BLOCKING: blocking
 *
 * These states are for ioctls that need to wait for a response
 * from firmware, so they probably require sleep.
 */
enum block_state {
	NON_BLOCKING,
	BLOCKING,
};

/**
 * _ctl_display_some_debug - debug routine
 * @ioc: per adapter object
 * @smid: system request message index
 * @calling_function_name: string pass from calling function
 * @mpi_reply: reply message frame
 * Context: none.
 *
 * Function for displaying debug info helpful when debugging issues
 * in this module.
 */
static void
_ctl_display_some_debug(struct MPT3SAS_ADAPTER *ioc, u16 smid,
	char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
{
	Mpi2ConfigRequest_t *mpi_request;
	char *desc = NULL;

	if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
		return;

	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
	switch (mpi_request->Function) {
	case MPI2_FUNCTION_SCSI_IO_REQUEST:
	{
		Mpi2SCSIIORequest_t *scsi_request =
		    (Mpi2SCSIIORequest_t *)mpi_request;

		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
		    "scsi_io, cmd(0x%02x), cdb_len(%d)",
		    scsi_request->CDB.CDB32[0],
		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
		desc = ioc->tmp_string;
		break;
	}
	case MPI2_FUNCTION_SCSI_TASK_MGMT:
		desc = "task_mgmt";
		break;
	case MPI2_FUNCTION_IOC_INIT:
		desc = "ioc_init";
		break;
	case MPI2_FUNCTION_IOC_FACTS:
		desc = "ioc_facts";
		break;
	case MPI2_FUNCTION_CONFIG:
	{
		Mpi2ConfigRequest_t *config_request =
		    (Mpi2ConfigRequest_t *)mpi_request;

		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
		    "config, type(0x%02x), ext_type(0x%02x), number(%d)",
		    (config_request->Header.PageType &
		     MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
		    config_request->Header.PageNumber);
		desc = ioc->tmp_string;
		break;
	}
	case MPI2_FUNCTION_PORT_FACTS:
		desc = "port_facts";
		break;
	case MPI2_FUNCTION_PORT_ENABLE:
		desc = "port_enable";
		break;
	case MPI2_FUNCTION_EVENT_NOTIFICATION:
		desc = "event_notification";
		break;
	case MPI2_FUNCTION_FW_DOWNLOAD:
		desc = "fw_download";
		break;
	case MPI2_FUNCTION_FW_UPLOAD:
		desc = "fw_upload";
		break;
	case MPI2_FUNCTION_RAID_ACTION:
		desc = "raid_action";
		break;
	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
	{
		Mpi2SCSIIORequest_t *scsi_request =
		    (Mpi2SCSIIORequest_t *)mpi_request;

		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
		    "raid_pass, cmd(0x%02x), cdb_len(%d)",
		    scsi_request->CDB.CDB32[0],
		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
		desc = ioc->tmp_string;
		break;
	}
	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
		desc = "sas_iounit_cntl";
		break;
	case MPI2_FUNCTION_SATA_PASSTHROUGH:
		desc = "sata_pass";
		break;
	case MPI2_FUNCTION_DIAG_BUFFER_POST:
		desc = "diag_buffer_post";
		break;
	case MPI2_FUNCTION_DIAG_RELEASE:
		desc = "diag_release";
		break;
	case MPI2_FUNCTION_SMP_PASSTHROUGH:
		desc = "smp_passthrough";
		break;
	}

	if (!desc)
		return;

188
	ioc_info(ioc, "%s: %s, smid(%d)\n", calling_function_name, desc, smid);
189 190 191 192 193

	if (!mpi_reply)
		return;

	if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
194 195 196
		ioc_info(ioc, "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
			 le16_to_cpu(mpi_reply->IOCStatus),
			 le32_to_cpu(mpi_reply->IOCLogInfo));
197 198 199 200 201 202 203

	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
	    mpi_request->Function ==
	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
		Mpi2SCSIIOReply_t *scsi_reply =
		    (Mpi2SCSIIOReply_t *)mpi_reply;
		struct _sas_device *sas_device = NULL;
204
		struct _pcie_device *pcie_device = NULL;
205

206
		sas_device = mpt3sas_get_sdev_by_handle(ioc,
207 208
		    le16_to_cpu(scsi_reply->DevHandle));
		if (sas_device) {
209 210 211 212 213 214
			ioc_warn(ioc, "\tsas_address(0x%016llx), phy(%d)\n",
				 (u64)sas_device->sas_address,
				 sas_device->phy);
			ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
				 (u64)sas_device->enclosure_logical_id,
				 sas_device->slot);
215 216 217 218 219 220
			sas_device_put(sas_device);
		}
		if (!sas_device) {
			pcie_device = mpt3sas_get_pdev_by_handle(ioc,
				le16_to_cpu(scsi_reply->DevHandle));
			if (pcie_device) {
221 222 223
				ioc_warn(ioc, "\tWWID(0x%016llx), port(%d)\n",
					 (unsigned long long)pcie_device->wwid,
					 pcie_device->port_num);
224
				if (pcie_device->enclosure_handle != 0)
225 226 227
					ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
						 (u64)pcie_device->enclosure_logical_id,
						 pcie_device->slot);
228 229
				pcie_device_put(pcie_device);
			}
230 231
		}
		if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
232 233 234
			ioc_info(ioc, "\tscsi_state(0x%02x), scsi_status(0x%02x)\n",
				 scsi_reply->SCSIState,
				 scsi_reply->SCSIStatus);
235 236 237 238 239 240 241 242 243 244 245 246 247
	}
}

/**
 * mpt3sas_ctl_done - ctl module completion routine
 * @ioc: per adapter object
 * @smid: system request message index
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 * Context: none.
 *
 * The callback handler when using ioc->ctl_cb_idx.
 *
248 249
 * Return: 1 meaning mf should be freed from _base_interrupt
 *         0 means the mf is freed from this function.
250 251 252 253 254 255 256
 */
u8
mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
	u32 reply)
{
	MPI2DefaultReply_t *mpi_reply;
	Mpi2SCSIIOReply_t *scsiio_reply;
257
	Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	const void *sense_data;
	u32 sz;

	if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
		return 1;
	if (ioc->ctl_cmds.smid != smid)
		return 1;
	ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
	if (mpi_reply) {
		memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
		ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
		/* get sense data */
		if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
		    mpi_reply->Function ==
		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
			scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
			if (scsiio_reply->SCSIState &
			    MPI2_SCSI_STATE_AUTOSENSE_VALID) {
				sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
				    le32_to_cpu(scsiio_reply->SenseCount));
				sense_data = mpt3sas_base_get_sense_buffer(ioc,
				    smid);
				memcpy(ioc->ctl_cmds.sense, sense_data, sz);
			}
		}
284 285 286 287 288 289 290 291
		/*
		 * Get Error Response data for NVMe device. The ctl_cmds.sense
		 * buffer is used to store the Error Response data.
		 */
		if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
			nvme_error_reply =
			    (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
			sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
292
			    le16_to_cpu(nvme_error_reply->ErrorResponseCount));
293 294 295
			sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
			memcpy(ioc->ctl_cmds.sense, sense_data, sz);
		}
296
	}
297

298 299 300 301 302 303 304 305 306 307 308 309 310 311
	_ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
	ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
	complete(&ioc->ctl_cmds.done);
	return 1;
}

/**
 * _ctl_check_event_type - determines when an event needs logging
 * @ioc: per adapter object
 * @event: firmware event
 *
 * The bitmask in ioc->event_type[] indicates which events should be
 * be saved in the driver event_log.  This bitmask is set by application.
 *
312
 * Return: 1 when event should be captured, or zero means no match.
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
 */
static int
_ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
{
	u16 i;
	u32 desired_event;

	if (event >= 128 || !event || !ioc->event_log)
		return 0;

	desired_event = (1 << (event % 32));
	if (!desired_event)
		desired_event = 1;
	i = event / 32;
	return desired_event & ioc->event_type[i];
}

/**
 * mpt3sas_ctl_add_to_event_log - add event
 * @ioc: per adapter object
 * @mpi_reply: reply message frame
 */
void
mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
	Mpi2EventNotificationReply_t *mpi_reply)
{
	struct MPT3_IOCTL_EVENTS *event_log;
	u16 event;
	int i;
	u32 sz, event_data_sz;
	u8 send_aen = 0;

	if (!ioc->event_log)
		return;

	event = le16_to_cpu(mpi_reply->Event);

	if (_ctl_check_event_type(ioc, event)) {

		/* insert entry into circular event_log */
		i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
		event_log = ioc->event_log;
		event_log[i].event = event;
		event_log[i].context = ioc->event_context++;

		event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
		sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
		memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
		memcpy(event_log[i].data, mpi_reply->EventData, sz);
		send_aen = 1;
	}

	/* This aen_event_read_flag flag is set until the
	 * application has read the event log.
	 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
	 */
	if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
	    (send_aen && !ioc->aen_event_read_flag)) {
		ioc->aen_event_read_flag = 1;
		wake_up_interruptible(&ctl_poll_wait);
		if (async_queue)
			kill_fasync(&async_queue, SIGIO, POLL_IN);
	}
}

/**
 * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
 * @ioc: per adapter object
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 * Context: interrupt.
 *
 * This function merely adds a new work task into ioc->firmware_event_thread.
 * The tasks are worked from _firmware_event_work in user context.
 *
388 389
 * Return: 1 meaning mf should be freed from _base_interrupt
 *         0 means the mf is freed from this function.
390 391 392 393 394 395 396 397
 */
u8
mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
	u32 reply)
{
	Mpi2EventNotificationReply_t *mpi_reply;

	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
398 399
	if (mpi_reply)
		mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
400 401 402 403 404
	return 1;
}

/**
 * _ctl_verify_adapter - validates ioc_number passed from application
405
 * @ioc_number: ?
406
 * @iocpp: The ioc pointer is returned in this.
407
 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
408
 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
409
 *
410
 * Return: (-1) means error, else ioc_number.
411 412
 */
static int
413 414
_ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
							int mpi_version)
415 416
{
	struct MPT3SAS_ADAPTER *ioc;
417
	int version = 0;
418 419
	/* global ioc lock to protect controller on list operations */
	spin_lock(&gioc_lock);
420 421 422
	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
		if (ioc->id != ioc_number)
			continue;
423 424 425
		/* Check whether this ioctl command is from right
		 * ioctl device or not, if not continue the search.
		 */
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
		version = ioc->hba_mpi_version_belonged;
		/* MPI25_VERSION and MPI26_VERSION uses same ioctl
		 * device.
		 */
		if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
			if ((version == MPI25_VERSION) ||
				(version == MPI26_VERSION))
				goto out;
			else
				continue;
		} else {
			if (version != mpi_version)
				continue;
		}
out:
441
		spin_unlock(&gioc_lock);
442 443 444
		*iocpp = ioc;
		return ioc_number;
	}
445
	spin_unlock(&gioc_lock);
446 447 448 449 450 451 452 453 454 455
	*iocpp = NULL;
	return -1;
}

/**
 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
 * @ioc: per adapter object
 *
 * The handler for doing any required cleanup or initialization.
 */
456
void mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
457 458 459 460
{
	int i;
	u8 issue_reset;

461
	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
462 463 464 465 466 467 468
	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
		if (!(ioc->diag_buffer_status[i] &
		      MPT3_DIAG_BUFFER_IS_REGISTERED))
			continue;
		if ((ioc->diag_buffer_status[i] &
		     MPT3_DIAG_BUFFER_IS_RELEASED))
			continue;
469 470 471 472 473 474 475

		/*
		 * add a log message to indicate the release
		 */
		ioc_info(ioc,
		    "%s: Releasing the trace buffer due to adapter reset.",
		    __func__);
476 477 478 479 480 481 482 483 484 485 486 487
		mpt3sas_send_diag_release(ioc, i, &issue_reset);
	}
}

/**
 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
 * @ioc: per adapter object
 *
 * The handler for doing any required cleanup or initialization.
 */
void mpt3sas_ctl_after_reset_handler(struct MPT3SAS_ADAPTER *ioc)
{
488
	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_AFTER_RESET\n", __func__));
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
	if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
		ioc->ctl_cmds.status |= MPT3_CMD_RESET;
		mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
		complete(&ioc->ctl_cmds.done);
	}
}

/**
 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
 * @ioc: per adapter object
 *
 * The handler for doing any required cleanup or initialization.
 */
void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
{
	int i;

506
	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
507

508 509 510 511 512 513 514 515 516
	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
		if (!(ioc->diag_buffer_status[i] &
		      MPT3_DIAG_BUFFER_IS_REGISTERED))
			continue;
		if ((ioc->diag_buffer_status[i] &
		     MPT3_DIAG_BUFFER_IS_RELEASED))
			continue;
		ioc->diag_buffer_status[i] |=
			MPT3_DIAG_BUFFER_IS_DIAG_RESET;
517 518 519 520
	}
}

/**
521
 * _ctl_fasync -
522 523 524
 * @fd: ?
 * @filep: ?
 * @mode: ?
525 526 527
 *
 * Called when application request fasyn callback handler.
 */
528
static int
529
_ctl_fasync(int fd, struct file *filep, int mode)
530 531 532 533 534
{
	return fasync_helper(fd, filep, mode, &async_queue);
}

/**
535
 * _ctl_poll -
536 537
 * @filep: ?
 * @wait: ?
538 539
 *
 */
540
static __poll_t
541
_ctl_poll(struct file *filep, poll_table *wait)
542 543 544 545 546
{
	struct MPT3SAS_ADAPTER *ioc;

	poll_wait(filep, &ctl_poll_wait, wait);

547 548
	/* global ioc lock to protect controller on list operations */
	spin_lock(&gioc_lock);
549
	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
550 551
		if (ioc->aen_event_read_flag) {
			spin_unlock(&gioc_lock);
552
			return EPOLLIN | EPOLLRDNORM;
553
		}
554
	}
555
	spin_unlock(&gioc_lock);
556 557 558 559 560 561
	return 0;
}

/**
 * _ctl_set_task_mid - assign an active smid to tm request
 * @ioc: per adapter object
562 563
 * @karg: (struct mpt3_ioctl_command)
 * @tm_request: pointer to mf from user space
564
 *
565
 * Return: 0 when an smid if found, else fail.
566 567 568 569 570 571 572
 * during failure, the reply frame is filled.
 */
static int
_ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
	Mpi2SCSITaskManagementRequest_t *tm_request)
{
	u8 found = 0;
573
	u16 smid;
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
	u16 handle;
	struct scsi_cmnd *scmd;
	struct MPT3SAS_DEVICE *priv_data;
	Mpi2SCSITaskManagementReply_t *tm_reply;
	u32 sz;
	u32 lun;
	char *desc = NULL;

	if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
		desc = "abort_task";
	else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
		desc = "query_task";
	else
		return 0;

	lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);

	handle = le16_to_cpu(tm_request->DevHandle);
592 593 594 595 596
	for (smid = ioc->scsiio_depth; smid && !found; smid--) {
		struct scsiio_tracker *st;

		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
		if (!scmd)
597 598 599 600 601 602 603 604
			continue;
		if (lun != scmd->device->lun)
			continue;
		priv_data = scmd->device->hostdata;
		if (priv_data->sas_target == NULL)
			continue;
		if (priv_data->sas_target->handle != handle)
			continue;
605
		st = scsi_cmd_priv(scmd);
606 607 608 609 610 611 612 613 614 615

		/*
		 * If the given TaskMID from the user space is zero, then the
		 * first outstanding smid will be picked up.  Otherwise,
		 * targeted smid will be the one.
		 */
		if (!tm_request->TaskMID || tm_request->TaskMID == st->smid) {
			tm_request->TaskMID = cpu_to_le16(st->smid);
			found = 1;
		}
616 617 618
	}

	if (!found) {
619 620 621 622
		dctlprintk(ioc,
			   ioc_info(ioc, "%s: handle(0x%04x), lun(%d), no active mid!!\n",
				    desc, le16_to_cpu(tm_request->DevHandle),
				    lun));
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
		tm_reply = ioc->ctl_cmds.reply;
		tm_reply->DevHandle = tm_request->DevHandle;
		tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
		tm_reply->TaskType = tm_request->TaskType;
		tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
		tm_reply->VP_ID = tm_request->VP_ID;
		tm_reply->VF_ID = tm_request->VF_ID;
		sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
		if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
		    sz))
			pr_err("failure at %s:%d/%s()!\n", __FILE__,
			    __LINE__, __func__);
		return 1;
	}

638 639 640 641
	dctlprintk(ioc,
		   ioc_info(ioc, "%s: handle(0x%04x), lun(%d), task_mid(%d)\n",
			    desc, le16_to_cpu(tm_request->DevHandle), lun,
			    le16_to_cpu(tm_request->TaskMID)));
642 643 644 645 646 647
	return 0;
}

/**
 * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
 * @ioc: per adapter object
648 649
 * @karg: (struct mpt3_ioctl_command)
 * @mf: pointer to mf in user space
650 651 652 653 654 655 656
 */
static long
_ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
	void __user *mf)
{
	MPI2RequestHeader_t *mpi_request = NULL, *request;
	MPI2DefaultReply_t *mpi_reply;
657
	Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
658
	struct _pcie_device *pcie_device = NULL;
659
	u16 smid;
660
	u8 timeout;
661
	u8 issue_reset;
662
	u32 sz, sz_arg;
663 664 665 666 667 668 669 670
	void *psge;
	void *data_out = NULL;
	dma_addr_t data_out_dma = 0;
	size_t data_out_sz = 0;
	void *data_in = NULL;
	dma_addr_t data_in_dma = 0;
	size_t data_in_sz = 0;
	long ret;
671
	u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
672 673 674 675

	issue_reset = 0;

	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
676
		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
677 678 679 680
		ret = -EAGAIN;
		goto out;
	}

681 682 683
	ret = mpt3sas_wait_for_ioc(ioc,	IOC_OPERATIONAL_WAIT_COUNT);
	if (ret)
		goto out;
684 685 686

	mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
	if (!mpi_request) {
687 688
		ioc_err(ioc, "%s: failed obtaining a memory for mpi_request\n",
			__func__);
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
		ret = -ENOMEM;
		goto out;
	}

	/* Check for overflow and wraparound */
	if (karg.data_sge_offset * 4 > ioc->request_sz ||
	    karg.data_sge_offset > (UINT_MAX / 4)) {
		ret = -EINVAL;
		goto out;
	}

	/* copy in request message frame from user */
	if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
		pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
		    __func__);
		ret = -EFAULT;
		goto out;
	}

	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
		smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
		if (!smid) {
711
			ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
712 713 714 715
			ret = -EAGAIN;
			goto out;
		}
	} else {
716 717
		/* Use first reserved smid for passthrough ioctls */
		smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
718 719 720 721 722 723
	}

	ret = 0;
	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
	request = mpt3sas_base_get_msg_frame(ioc, smid);
724
	memset(request, 0, ioc->request_sz);
725 726 727 728 729 730
	memcpy(request, mpi_request, karg.data_sge_offset*4);
	ioc->ctl_cmds.smid = smid;
	data_out_sz = karg.data_out_size;
	data_in_sz = karg.data_in_size;

	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
731 732
	    mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
	    mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
733 734
	    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
	    mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
735 736 737 738

		device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
		if (!device_handle || (device_handle >
		    ioc->facts.MaxDevHandle)) {
739 740 741 742 743 744 745 746
			ret = -EINVAL;
			mpt3sas_base_free_smid(ioc, smid);
			goto out;
		}
	}

	/* obtain dma-able memory for data transfer */
	if (data_out_sz) /* WRITE */ {
747 748
		data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz,
				&data_out_dma, GFP_KERNEL);
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
		if (!data_out) {
			pr_err("failure at %s:%d/%s()!\n", __FILE__,
			    __LINE__, __func__);
			ret = -ENOMEM;
			mpt3sas_base_free_smid(ioc, smid);
			goto out;
		}
		if (copy_from_user(data_out, karg.data_out_buf_ptr,
			data_out_sz)) {
			pr_err("failure at %s:%d/%s()!\n", __FILE__,
			    __LINE__, __func__);
			ret =  -EFAULT;
			mpt3sas_base_free_smid(ioc, smid);
			goto out;
		}
	}

	if (data_in_sz) /* READ */ {
767 768
		data_in = dma_alloc_coherent(&ioc->pdev->dev, data_in_sz,
				&data_in_dma, GFP_KERNEL);
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
		if (!data_in) {
			pr_err("failure at %s:%d/%s()!\n", __FILE__,
			    __LINE__, __func__);
			ret = -ENOMEM;
			mpt3sas_base_free_smid(ioc, smid);
			goto out;
		}
	}

	psge = (void *)request + (karg.data_sge_offset*4);

	/* send command to firmware */
	_ctl_display_some_debug(ioc, smid, "ctl_request", NULL);

	init_completion(&ioc->ctl_cmds.done);
	switch (mpi_request->Function) {
785 786 787 788 789 790 791 792 793 794 795 796
	case MPI2_FUNCTION_NVME_ENCAPSULATED:
	{
		nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
		/*
		 * Get the Physical Address of the sense buffer.
		 * Use Error Response buffer address field to hold the sense
		 * buffer address.
		 * Clear the internal sense buffer, which will potentially hold
		 * the Completion Queue Entry on return, or 0 if no Entry.
		 * Build the PRPs and set direction bits.
		 * Send the request.
		 */
797 798
		nvme_encap_request->ErrorResponseBaseAddress =
		    cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
799
		nvme_encap_request->ErrorResponseBaseAddress |=
800 801
		   cpu_to_le64(le32_to_cpu(
		   mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
802
		nvme_encap_request->ErrorResponseAllocationLength =
803
					cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
804 805 806 807
		memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
		ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
		    data_out_dma, data_out_sz, data_in_dma, data_in_sz);
		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
808 809 810
			dtmprintk(ioc,
				  ioc_info(ioc, "handle(0x%04x): ioctl failed due to device removal in progress\n",
					   device_handle));
811 812 813 814
			mpt3sas_base_free_smid(ioc, smid);
			ret = -EINVAL;
			goto out;
		}
815
		mpt3sas_base_put_smid_nvme_encap(ioc, smid);
816 817
		break;
	}
818 819 820 821 822 823 824 825 826
	case MPI2_FUNCTION_SCSI_IO_REQUEST:
	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
	{
		Mpi2SCSIIORequest_t *scsiio_request =
		    (Mpi2SCSIIORequest_t *)request;
		scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
		scsiio_request->SenseBufferLowAddress =
		    mpt3sas_base_get_sense_buffer_dma(ioc, smid);
		memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
827
		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
828 829 830
			dtmprintk(ioc,
				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
					   device_handle));
831 832 833 834
			mpt3sas_base_free_smid(ioc, smid);
			ret = -EINVAL;
			goto out;
		}
835 836 837
		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
		    data_in_dma, data_in_sz);
		if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
838
			ioc->put_smid_scsi_io(ioc, smid, device_handle);
839
		else
840
			ioc->put_smid_default(ioc, smid);
841 842 843 844 845 846 847
		break;
	}
	case MPI2_FUNCTION_SCSI_TASK_MGMT:
	{
		Mpi2SCSITaskManagementRequest_t *tm_request =
		    (Mpi2SCSITaskManagementRequest_t *)request;

848 849 850 851
		dtmprintk(ioc,
			  ioc_info(ioc, "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
				   le16_to_cpu(tm_request->DevHandle),
				   tm_request->TaskType));
852
		ioc->got_task_abort_from_ioctl = 1;
853 854 855 856 857 858
		if (tm_request->TaskType ==
		    MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
		    tm_request->TaskType ==
		    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
			if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
				mpt3sas_base_free_smid(ioc, smid);
859
				ioc->got_task_abort_from_ioctl = 0;
860 861 862
				goto out;
			}
		}
863
		ioc->got_task_abort_from_ioctl = 0;
864

865
		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
866 867 868
			dtmprintk(ioc,
				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
					   device_handle));
869 870 871 872
			mpt3sas_base_free_smid(ioc, smid);
			ret = -EINVAL;
			goto out;
		}
873 874 875 876
		mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
		    tm_request->DevHandle));
		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
		    data_in_dma, data_in_sz);
877
		ioc->put_smid_hi_priority(ioc, smid, 0);
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
		break;
	}
	case MPI2_FUNCTION_SMP_PASSTHROUGH:
	{
		Mpi2SmpPassthroughRequest_t *smp_request =
		    (Mpi2SmpPassthroughRequest_t *)mpi_request;
		u8 *data;

		/* ioc determines which port to use */
		smp_request->PhysicalPort = 0xFF;
		if (smp_request->PassthroughFlags &
		    MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
			data = (u8 *)&smp_request->SGL;
		else {
			if (unlikely(data_out == NULL)) {
				pr_err("failure at %s:%d/%s()!\n",
				    __FILE__, __LINE__, __func__);
				mpt3sas_base_free_smid(ioc, smid);
				ret = -EINVAL;
				goto out;
			}
			data = data_out;
		}

		if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
			ioc->ioc_link_reset_in_progress = 1;
			ioc->ignore_loginfos = 1;
		}
		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
		    data_in_sz);
908
		ioc->put_smid_default(ioc, smid);
909 910 911
		break;
	}
	case MPI2_FUNCTION_SATA_PASSTHROUGH:
912 913
	{
		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
914 915 916
			dtmprintk(ioc,
				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
					   device_handle));
917 918 919 920 921 922
			mpt3sas_base_free_smid(ioc, smid);
			ret = -EINVAL;
			goto out;
		}
		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
		    data_in_sz);
923
		ioc->put_smid_default(ioc, smid);
924 925
		break;
	}
926 927 928 929 930
	case MPI2_FUNCTION_FW_DOWNLOAD:
	case MPI2_FUNCTION_FW_UPLOAD:
	{
		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
		    data_in_sz);
931
		ioc->put_smid_default(ioc, smid);
932 933 934 935 936 937 938
		break;
	}
	case MPI2_FUNCTION_TOOLBOX:
	{
		Mpi2ToolboxCleanRequest_t *toolbox_request =
			(Mpi2ToolboxCleanRequest_t *)mpi_request;

939 940 941
		if ((toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
		    || (toolbox_request->Tool ==
		    MPI26_TOOLBOX_BACKEND_PCIE_LANE_MARGIN))
942 943
			ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
				data_in_dma, data_in_sz);
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
		else if (toolbox_request->Tool ==
				MPI2_TOOLBOX_MEMORY_MOVE_TOOL) {
			Mpi2ToolboxMemMoveRequest_t *mem_move_request =
					(Mpi2ToolboxMemMoveRequest_t *)request;
			Mpi2SGESimple64_t tmp, *src = NULL, *dst = NULL;

			ioc->build_sg_mpi(ioc, psge, data_out_dma,
					data_out_sz, data_in_dma, data_in_sz);
			if (data_out_sz && !data_in_sz) {
				dst =
				    (Mpi2SGESimple64_t *)&mem_move_request->SGL;
				src = (void *)dst + ioc->sge_size;

				memcpy(&tmp, src, ioc->sge_size);
				memcpy(src, dst, ioc->sge_size);
				memcpy(dst, &tmp, ioc->sge_size);
			}
			if (ioc->logging_level & MPT_DEBUG_TM) {
				ioc_info(ioc,
				  "Mpi2ToolboxMemMoveRequest_t request msg\n");
				_debug_dump_mf(mem_move_request,
							ioc->request_sz/4);
			}
		} else
968
			ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
969
			    data_in_dma, data_in_sz);
970
		ioc->put_smid_default(ioc, smid);
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
		break;
	}
	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
	{
		Mpi2SasIoUnitControlRequest_t *sasiounit_request =
		    (Mpi2SasIoUnitControlRequest_t *)mpi_request;

		if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
		    || sasiounit_request->Operation ==
		    MPI2_SAS_OP_PHY_LINK_RESET) {
			ioc->ioc_link_reset_in_progress = 1;
			ioc->ignore_loginfos = 1;
		}
		/* drop to default case for posting the request */
	}
986
		/* fall through */
987 988 989
	default:
		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
		    data_in_dma, data_in_sz);
990
		ioc->put_smid_default(ioc, smid);
991 992 993 994 995 996 997
		break;
	}

	if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
		timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
	else
		timeout = karg.timeout;
998
	wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
		Mpi2SCSITaskManagementRequest_t *tm_request =
		    (Mpi2SCSITaskManagementRequest_t *)mpi_request;
		mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
		    tm_request->DevHandle));
		mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
	} else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
	    mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
		ioc->ioc_link_reset_in_progress) {
		ioc->ioc_link_reset_in_progress = 0;
		ioc->ignore_loginfos = 0;
	}
	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1012 1013 1014 1015
		issue_reset =
			mpt3sas_base_check_cmd_timeout(ioc,
				ioc->ctl_cmds.status, mpi_request,
				karg.data_sge_offset);
1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
		goto issue_host_reset;
	}

	mpi_reply = ioc->ctl_cmds.reply;

	if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
	    (ioc->logging_level & MPT_DEBUG_TM)) {
		Mpi2SCSITaskManagementReply_t *tm_reply =
		    (Mpi2SCSITaskManagementReply_t *)mpi_reply;

1026 1027 1028 1029
		ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
			 le16_to_cpu(tm_reply->IOCStatus),
			 le32_to_cpu(tm_reply->IOCLogInfo),
			 le32_to_cpu(tm_reply->TerminationCount));
1030
	}
1031

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	/* copy out xdata to user */
	if (data_in_sz) {
		if (copy_to_user(karg.data_in_buf_ptr, data_in,
		    data_in_sz)) {
			pr_err("failure at %s:%d/%s()!\n", __FILE__,
			    __LINE__, __func__);
			ret = -ENODATA;
			goto out;
		}
	}

	/* copy out reply message frame to user */
	if (karg.max_reply_bytes) {
		sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
		if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
		    sz)) {
			pr_err("failure at %s:%d/%s()!\n", __FILE__,
			    __LINE__, __func__);
			ret = -ENODATA;
			goto out;
		}
	}

1055
	/* copy out sense/NVMe Error Response to user */
1056 1057
	if (karg.max_sense_bytes && (mpi_request->Function ==
	    MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
1058 1059 1060
	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
	    MPI2_FUNCTION_NVME_ENCAPSULATED)) {
		if (karg.sense_data_ptr == NULL) {
1061
			ioc_info(ioc, "Response buffer provided by application is NULL; Response data will not be returned\n");
1062 1063 1064 1065 1066 1067
			goto out;
		}
		sz_arg = (mpi_request->Function ==
		MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
							SCSI_SENSE_BUFFERSIZE;
		sz = min_t(u32, karg.max_sense_bytes, sz_arg);
1068 1069 1070
		if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
		    sz)) {
			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1071
				__LINE__, __func__);
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
			ret = -ENODATA;
			goto out;
		}
	}

 issue_host_reset:
	if (issue_reset) {
		ret = -ENODATA;
		if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
		    mpi_request->Function ==
		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
		    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
1084 1085
			ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
				 le16_to_cpu(mpi_request->FunctionDependent1));
1086
			mpt3sas_halt_firmware(ioc);
1087 1088
			pcie_device = mpt3sas_get_pdev_by_handle(ioc,
				le16_to_cpu(mpi_request->FunctionDependent1));
1089 1090 1091
			if (pcie_device && (!ioc->tm_custom_handling) &&
			    (!(mpt3sas_scsih_is_pcie_scsi_device(
			    pcie_device->device_info))))
1092 1093 1094 1095
				mpt3sas_scsih_issue_locked_tm(ioc,
				  le16_to_cpu(mpi_request->FunctionDependent1),
				  0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
				  0, pcie_device->reset_timeout,
1096
			MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE);
1097 1098 1099 1100 1101
			else
				mpt3sas_scsih_issue_locked_tm(ioc,
				  le16_to_cpu(mpi_request->FunctionDependent1),
				  0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
				  0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
1102
		} else
1103
			mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1104 1105 1106
	}

 out:
1107 1108
	if (pcie_device)
		pcie_device_put(pcie_device);
1109 1110 1111

	/* free memory associated with sg buffers */
	if (data_in)
1112
		dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
1113 1114 1115
		    data_in_dma);

	if (data_out)
1116
		dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
		    data_out_dma);

	kfree(mpi_request);
	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
	return ret;
}

/**
 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
 * @ioc: per adapter object
1127
 * @arg: user space buffer containing ioctl content
1128 1129 1130 1131 1132 1133
 */
static long
_ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
{
	struct mpt3_ioctl_iocinfo karg;

1134 1135
	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
				 __func__));
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148

	memset(&karg, 0 , sizeof(karg));
	if (ioc->pfacts)
		karg.port_number = ioc->pfacts[0].PortNumber;
	karg.hw_rev = ioc->pdev->revision;
	karg.pci_id = ioc->pdev->device;
	karg.subsystem_device = ioc->pdev->subsystem_device;
	karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
	karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
	karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
	karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
	karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
	karg.firmware_version = ioc->facts.FWVersion.Word;
1149
	strcpy(karg.driver_version, ioc->driver_name);
1150
	strcat(karg.driver_version, "-");
1151 1152
	switch  (ioc->hba_mpi_version_belonged) {
	case MPI2_VERSION:
1153 1154 1155 1156
		if (ioc->is_warpdrive)
			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
		else
			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1157 1158 1159
		strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
		break;
	case MPI25_VERSION:
1160
	case MPI26_VERSION:
1161 1162 1163 1164
		if (ioc->is_gen35_ioc)
			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
		else
			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1165 1166 1167
		strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
		break;
	}
1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);

	if (copy_to_user(arg, &karg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}
	return 0;
}

/**
 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
 * @ioc: per adapter object
1181
 * @arg: user space buffer containing ioctl content
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
 */
static long
_ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
{
	struct mpt3_ioctl_eventquery karg;

	if (copy_from_user(&karg, arg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

1194 1195
	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
				 __func__));
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211

	karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
	memcpy(karg.event_types, ioc->event_type,
	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));

	if (copy_to_user(arg, &karg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}
	return 0;
}

/**
 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
 * @ioc: per adapter object
1212
 * @arg: user space buffer containing ioctl content
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
 */
static long
_ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
{
	struct mpt3_ioctl_eventenable karg;

	if (copy_from_user(&karg, arg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

1225 1226
	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
				 __func__));
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249

	memcpy(ioc->event_type, karg.event_types,
	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
	mpt3sas_base_validate_event_type(ioc, ioc->event_type);

	if (ioc->event_log)
		return 0;
	/* initialize event_log */
	ioc->event_context = 0;
	ioc->aen_event_read_flag = 0;
	ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
	    sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
	if (!ioc->event_log) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -ENOMEM;
	}
	return 0;
}

/**
 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
 * @ioc: per adapter object
1250
 * @arg: user space buffer containing ioctl content
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
 */
static long
_ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
{
	struct mpt3_ioctl_eventreport karg;
	u32 number_bytes, max_events, max;
	struct mpt3_ioctl_eventreport __user *uarg = arg;

	if (copy_from_user(&karg, arg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

1265 1266
	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
				 __func__));
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293

	number_bytes = karg.hdr.max_data_size -
	    sizeof(struct mpt3_ioctl_header);
	max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
	max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);

	/* If fewer than 1 event is requested, there must have
	 * been some type of error.
	 */
	if (!max || !ioc->event_log)
		return -ENODATA;

	number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
	if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

	/* reset flag so SIGIO can restart */
	ioc->aen_event_read_flag = 0;
	return 0;
}

/**
 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
 * @ioc: per adapter object
1294
 * @arg: user space buffer containing ioctl content
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
 */
static long
_ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
{
	struct mpt3_ioctl_diag_reset karg;
	int retval;

	if (copy_from_user(&karg, arg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

	if (ioc->shost_recovery || ioc->pci_error_recovery ||
	    ioc->is_driver_loading)
		return -EAGAIN;

1312 1313
	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
				 __func__));
1314

1315
	retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1316
	ioc_info(ioc, "host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
	return 0;
}

/**
 * _ctl_btdh_search_sas_device - searching for sas device
 * @ioc: per adapter object
 * @btdh: btdh ioctl payload
 */
static int
_ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
	struct mpt3_ioctl_btdh_mapping *btdh)
{
	struct _sas_device *sas_device;
	unsigned long flags;
	int rc = 0;

	if (list_empty(&ioc->sas_device_list))
		return rc;

	spin_lock_irqsave(&ioc->sas_device_lock, flags);
	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
		    btdh->handle == sas_device->handle) {
			btdh->bus = sas_device->channel;
			btdh->id = sas_device->id;
			rc = 1;
			goto out;
		} else if (btdh->bus == sas_device->channel && btdh->id ==
		    sas_device->id && btdh->handle == 0xFFFF) {
			btdh->handle = sas_device->handle;
			rc = 1;
			goto out;
		}
	}
 out:
	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
	return rc;
}

1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
/**
 * _ctl_btdh_search_pcie_device - searching for pcie device
 * @ioc: per adapter object
 * @btdh: btdh ioctl payload
 */
static int
_ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
	struct mpt3_ioctl_btdh_mapping *btdh)
{
	struct _pcie_device *pcie_device;
	unsigned long flags;
	int rc = 0;

	if (list_empty(&ioc->pcie_device_list))
		return rc;

	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
			   btdh->handle == pcie_device->handle) {
			btdh->bus = pcie_device->channel;
			btdh->id = pcie_device->id;
			rc = 1;
			goto out;
		} else if (btdh->bus == pcie_device->channel && btdh->id ==
			   pcie_device->id && btdh->handle == 0xFFFF) {
			btdh->handle = pcie_device->handle;
			rc = 1;
			goto out;
		}
	}
 out:
	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
	return rc;
}

1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430
/**
 * _ctl_btdh_search_raid_device - searching for raid device
 * @ioc: per adapter object
 * @btdh: btdh ioctl payload
 */
static int
_ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
	struct mpt3_ioctl_btdh_mapping *btdh)
{
	struct _raid_device *raid_device;
	unsigned long flags;
	int rc = 0;

	if (list_empty(&ioc->raid_device_list))
		return rc;

	spin_lock_irqsave(&ioc->raid_device_lock, flags);
	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
		    btdh->handle == raid_device->handle) {
			btdh->bus = raid_device->channel;
			btdh->id = raid_device->id;
			rc = 1;
			goto out;
		} else if (btdh->bus == raid_device->channel && btdh->id ==
		    raid_device->id && btdh->handle == 0xFFFF) {
			btdh->handle = raid_device->handle;
			rc = 1;
			goto out;
		}
	}
 out:
	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
	return rc;
}

/**
 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
 * @ioc: per adapter object
1431
 * @arg: user space buffer containing ioctl content
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
 */
static long
_ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
{
	struct mpt3_ioctl_btdh_mapping karg;
	int rc;

	if (copy_from_user(&karg, arg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

1445 1446
	dctlprintk(ioc, ioc_info(ioc, "%s\n",
				 __func__));
1447 1448

	rc = _ctl_btdh_search_sas_device(ioc, &karg);
1449 1450
	if (!rc)
		rc = _ctl_btdh_search_pcie_device(ioc, &karg);
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
	if (!rc)
		_ctl_btdh_search_raid_device(ioc, &karg);

	if (copy_to_user(arg, &karg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}
	return 0;
}

/**
 * _ctl_diag_capability - return diag buffer capability
 * @ioc: per adapter object
 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
 *
 * returns 1 when diag buffer support is enabled in firmware
 */
static u8
_ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
{
	u8 rc = 0;

	switch (buffer_type) {
	case MPI2_DIAG_BUF_TYPE_TRACE:
		if (ioc->facts.IOCCapabilities &
		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
			rc = 1;
		break;
	case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
		if (ioc->facts.IOCCapabilities &
		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
			rc = 1;
		break;
	case MPI2_DIAG_BUF_TYPE_EXTENDED:
		if (ioc->facts.IOCCapabilities &
		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
			rc = 1;
	}

	return rc;
}

1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
/**
 * _ctl_diag_get_bufftype - return diag buffer type
 *              either TRACE, SNAPSHOT, or EXTENDED
 * @ioc: per adapter object
 * @unique_id: specifies the unique_id for the buffer
 *
 * returns MPT3_DIAG_UID_NOT_FOUND if the id not found
 */
static u8
_ctl_diag_get_bufftype(struct MPT3SAS_ADAPTER *ioc, u32 unique_id)
{
	u8  index;

	for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
		if (ioc->unique_id[index] == unique_id)
			return index;
	}

	return MPT3_DIAG_UID_NOT_FOUND;
}
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536

/**
 * _ctl_diag_register_2 - wrapper for registering diag buffer support
 * @ioc: per adapter object
 * @diag_register: the diag_register struct passed in from user space
 *
 */
static long
_ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
	struct mpt3_diag_register *diag_register)
{
	int rc, i;
	void *request_data = NULL;
	dma_addr_t request_data_dma;
	u32 request_data_sz = 0;
	Mpi2DiagBufferPostRequest_t *mpi_request;
	Mpi2DiagBufferPostReply_t *mpi_reply;
	u8 buffer_type;
	u16 smid;
	u16 ioc_status;
	u32 ioc_state;
	u8 issue_reset = 0;

1537 1538
	dctlprintk(ioc, ioc_info(ioc, "%s\n",
				 __func__));
1539 1540 1541

	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1542 1543
		ioc_err(ioc, "%s: failed due to ioc not operational\n",
			__func__);
1544 1545 1546 1547 1548
		rc = -EAGAIN;
		goto out;
	}

	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1549
		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
1550 1551 1552 1553 1554 1555
		rc = -EAGAIN;
		goto out;
	}

	buffer_type = diag_register->buffer_type;
	if (!_ctl_diag_capability(ioc, buffer_type)) {
1556 1557
		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
			__func__, buffer_type);
1558 1559 1560
		return -EPERM;
	}

1561 1562 1563 1564 1565 1566 1567
	if (diag_register->unique_id == 0) {
		ioc_err(ioc,
		    "%s: Invalid UID(0x%08x), buffer_type(0x%02x)\n", __func__,
		    diag_register->unique_id, buffer_type);
		return -EINVAL;
	}

1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
	if ((ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_APP_OWNED) &&
	    !(ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_RELEASED)) {
		ioc_err(ioc,
		    "%s: buffer_type(0x%02x) is already registered by application with UID(0x%08x)\n",
		    __func__, buffer_type, ioc->unique_id[buffer_type]);
		return -EINVAL;
	}

1578 1579
	if (ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_REGISTERED) {
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
		/*
		 * If driver posts buffer initially, then an application wants
		 * to Register that buffer (own it) without Releasing first,
		 * the application Register command MUST have the same buffer
		 * type and size in the Register command (obtained from the
		 * Query command). Otherwise that Register command will be
		 * failed. If the application has released the buffer but wants
		 * to re-register it, it should be allowed as long as the
		 * Unique-Id/Size match.
		 */

		if (ioc->unique_id[buffer_type] == MPT3DIAGBUFFUNIQUEID &&
		    ioc->diag_buffer_sz[buffer_type] ==
		    diag_register->requested_buffer_size) {

			if (!(ioc->diag_buffer_status[buffer_type] &
			     MPT3_DIAG_BUFFER_IS_RELEASED)) {
				dctlprintk(ioc, ioc_info(ioc,
				    "%s: diag_buffer (%d) ownership changed. old-ID(0x%08x), new-ID(0x%08x)\n",
				    __func__, buffer_type,
				    ioc->unique_id[buffer_type],
				    diag_register->unique_id));

				/*
				 * Application wants to own the buffer with
				 * the same size.
				 */
				ioc->unique_id[buffer_type] =
				    diag_register->unique_id;
				rc = 0; /* success */
				goto out;
			}
		} else if (ioc->unique_id[buffer_type] !=
		    MPT3DIAGBUFFUNIQUEID) {
			if (ioc->unique_id[buffer_type] !=
			    diag_register->unique_id ||
			    ioc->diag_buffer_sz[buffer_type] !=
			    diag_register->requested_buffer_size ||
			    !(ioc->diag_buffer_status[buffer_type] &
			    MPT3_DIAG_BUFFER_IS_RELEASED)) {
				ioc_err(ioc,
				    "%s: already has a registered buffer for buffer_type(0x%02x)\n",
				    __func__, buffer_type);
				return -EINVAL;
			}
		} else {
			ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
			    __func__, buffer_type);
			return -EINVAL;
		}
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
	} else if (ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {

		if (ioc->unique_id[buffer_type] != MPT3DIAGBUFFUNIQUEID ||
		    ioc->diag_buffer_sz[buffer_type] !=
		    diag_register->requested_buffer_size) {

			ioc_err(ioc,
			    "%s: already a buffer is allocated for buffer_type(0x%02x) of size %d bytes, so please try registering again with same size\n",
			     __func__, buffer_type,
			    ioc->diag_buffer_sz[buffer_type]);
			return -EINVAL;
		}
1643 1644 1645
	}

	if (diag_register->requested_buffer_size % 4)  {
1646 1647
		ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
			__func__);
1648 1649 1650 1651 1652
		return -EINVAL;
	}

	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
	if (!smid) {
1653
		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
		rc = -EAGAIN;
		goto out;
	}

	rc = 0;
	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
	ioc->ctl_cmds.smid = smid;

	request_data = ioc->diag_buffer[buffer_type];
	request_data_sz = diag_register->requested_buffer_size;
	ioc->unique_id[buffer_type] = diag_register->unique_id;
1667 1668
	ioc->diag_buffer_status[buffer_type] &=
	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1669 1670 1671 1672 1673 1674 1675
	memcpy(ioc->product_specific[buffer_type],
	    diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
	ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;

	if (request_data) {
		request_data_dma = ioc->diag_buffer_dma[buffer_type];
		if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1676 1677 1678
			dma_free_coherent(&ioc->pdev->dev,
					ioc->diag_buffer_sz[buffer_type],
					request_data, request_data_dma);
1679 1680 1681 1682 1683 1684 1685
			request_data = NULL;
		}
	}

	if (request_data == NULL) {
		ioc->diag_buffer_sz[buffer_type] = 0;
		ioc->diag_buffer_dma[buffer_type] = 0;
1686 1687
		request_data = dma_alloc_coherent(&ioc->pdev->dev,
				request_data_sz, &request_data_dma, GFP_KERNEL);
1688
		if (request_data == NULL) {
1689 1690
			ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
				__func__, request_data_sz);
1691
			mpt3sas_base_free_smid(ioc, smid);
1692 1693
			rc = -ENOMEM;
			goto out;
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
		}
		ioc->diag_buffer[buffer_type] = request_data;
		ioc->diag_buffer_sz[buffer_type] = request_data_sz;
		ioc->diag_buffer_dma[buffer_type] = request_data_dma;
	}

	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
	mpi_request->BufferType = diag_register->buffer_type;
	mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
	mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
	mpi_request->BufferLength = cpu_to_le32(request_data_sz);
	mpi_request->VF_ID = 0; /* TODO */
	mpi_request->VP_ID = 0;

1708 1709 1710 1711 1712
	dctlprintk(ioc,
		   ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
			    __func__, request_data,
			    (unsigned long long)request_data_dma,
			    le32_to_cpu(mpi_request->BufferLength)));
1713 1714 1715 1716 1717 1718

	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
		mpi_request->ProductSpecific[i] =
			cpu_to_le32(ioc->product_specific[buffer_type][i]);

	init_completion(&ioc->ctl_cmds.done);
1719
	ioc->put_smid_default(ioc, smid);
1720
	wait_for_completion_timeout(&ioc->ctl_cmds.done,
1721 1722 1723
	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);

	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1724 1725 1726 1727
		issue_reset =
			mpt3sas_base_check_cmd_timeout(ioc,
				ioc->ctl_cmds.status, mpi_request,
				sizeof(Mpi2DiagBufferPostRequest_t)/4);
1728 1729 1730 1731 1732
		goto issue_host_reset;
	}

	/* process the completed Reply Message Frame */
	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1733
		ioc_err(ioc, "%s: no reply message\n", __func__);
1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
		rc = -EFAULT;
		goto out;
	}

	mpi_reply = ioc->ctl_cmds.reply;
	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;

	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
		ioc->diag_buffer_status[buffer_type] |=
			MPT3_DIAG_BUFFER_IS_REGISTERED;
1744
		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
1745
	} else {
1746 1747 1748
		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
			 __func__,
			 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1749 1750 1751 1752 1753
		rc = -EFAULT;
	}

 issue_host_reset:
	if (issue_reset)
1754
		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1755 1756 1757

 out:

1758
	if (rc && request_data) {
1759
		dma_free_coherent(&ioc->pdev->dev, request_data_sz,
1760
		    request_data, request_data_dma);
1761 1762 1763
		ioc->diag_buffer_status[buffer_type] &=
		    ~MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
	}
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780

	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
	return rc;
}

/**
 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
 * @ioc: per adapter object
 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
 *
 * This is called when command line option diag_buffer_enable is enabled
 * at driver load time.
 */
void
mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
{
	struct mpt3_diag_register diag_register;
1781 1782 1783 1784
	u32 ret_val;
	u32 trace_buff_size = ioc->manu_pg11.HostTraceBufferMaxSizeKB<<10;
	u32 min_trace_buff_size = 0;
	u32 decr_trace_buff_size = 0;
1785 1786 1787 1788

	memset(&diag_register, 0, sizeof(struct mpt3_diag_register));

	if (bits_to_register & 1) {
1789
		ioc_info(ioc, "registering trace buffer support\n");
1790 1791 1792
		ioc->diag_trigger_master.MasterData =
		    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1793 1794 1795
		diag_register.unique_id =
		    (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
		    (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846

		if (trace_buff_size != 0) {
			diag_register.requested_buffer_size = trace_buff_size;
			min_trace_buff_size =
			    ioc->manu_pg11.HostTraceBufferMinSizeKB<<10;
			decr_trace_buff_size =
			    ioc->manu_pg11.HostTraceBufferDecrementSizeKB<<10;

			if (min_trace_buff_size > trace_buff_size) {
				/* The buff size is not set correctly */
				ioc_err(ioc,
				    "Min Trace Buff size (%d KB) greater than Max Trace Buff size (%d KB)\n",
				     min_trace_buff_size>>10,
				     trace_buff_size>>10);
				ioc_err(ioc,
				    "Using zero Min Trace Buff Size\n");
				    min_trace_buff_size = 0;
			}

			if (decr_trace_buff_size == 0) {
				/*
				 * retry the min size if decrement
				 * is not available.
				 */
				decr_trace_buff_size =
				    trace_buff_size - min_trace_buff_size;
			}
		} else {
			/* register for 2MB buffers  */
			diag_register.requested_buffer_size = 2 * (1024 * 1024);
		}

		do {
			ret_val = _ctl_diag_register_2(ioc,  &diag_register);

			if (ret_val == -ENOMEM && min_trace_buff_size &&
			    (trace_buff_size - decr_trace_buff_size) >=
			    min_trace_buff_size) {
				/* adjust the buffer size */
				trace_buff_size -= decr_trace_buff_size;
				diag_register.requested_buffer_size =
				    trace_buff_size;
			} else
				break;
		} while (true);

		if (ret_val == -ENOMEM)
			ioc_err(ioc,
			    "Cannot allocate trace buffer memory. Last memory tried = %d KB\n",
			    diag_register.requested_buffer_size>>10);
		else if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE]
1847
		    & MPT3_DIAG_BUFFER_IS_REGISTERED) {
1848 1849
			ioc_err(ioc, "Trace buffer memory %d KB allocated\n",
			    diag_register.requested_buffer_size>>10);
1850 1851 1852 1853 1854
			if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
				ioc->diag_buffer_status[
				    MPI2_DIAG_BUF_TYPE_TRACE] |=
				    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
		}
1855 1856 1857
	}

	if (bits_to_register & 2) {
1858
		ioc_info(ioc, "registering snapshot buffer support\n");
1859 1860 1861 1862 1863 1864 1865 1866
		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
		/* register for 2MB buffers  */
		diag_register.requested_buffer_size = 2 * (1024 * 1024);
		diag_register.unique_id = 0x7075901;
		_ctl_diag_register_2(ioc,  &diag_register);
	}

	if (bits_to_register & 4) {
1867
		ioc_info(ioc, "registering extended buffer support\n");
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
		/* register for 2MB buffers  */
		diag_register.requested_buffer_size = 2 * (1024 * 1024);
		diag_register.unique_id = 0x7075901;
		_ctl_diag_register_2(ioc,  &diag_register);
	}
}

/**
 * _ctl_diag_register - application register with driver
 * @ioc: per adapter object
1879
 * @arg: user space buffer containing ioctl content
1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
 *
 * This will allow the driver to setup any required buffers that will be
 * needed by firmware to communicate with the driver.
 */
static long
_ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
{
	struct mpt3_diag_register karg;
	long rc;

	if (copy_from_user(&karg, arg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

	rc = _ctl_diag_register_2(ioc, &karg);
1897 1898 1899 1900 1901 1902

	if (!rc && (ioc->diag_buffer_status[karg.buffer_type] &
	    MPT3_DIAG_BUFFER_IS_REGISTERED))
		ioc->diag_buffer_status[karg.buffer_type] |=
		    MPT3_DIAG_BUFFER_IS_APP_OWNED;

1903 1904 1905 1906 1907 1908
	return rc;
}

/**
 * _ctl_diag_unregister - application unregister with driver
 * @ioc: per adapter object
1909
 * @arg: user space buffer containing ioctl content
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
 *
 * This will allow the driver to cleanup any memory allocated for diag
 * messages and to free up any resources.
 */
static long
_ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
{
	struct mpt3_diag_unregister karg;
	void *request_data;
	dma_addr_t request_data_dma;
	u32 request_data_sz;
	u8 buffer_type;

	if (copy_from_user(&karg, arg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

1929 1930
	dctlprintk(ioc, ioc_info(ioc, "%s\n",
				 __func__));
1931

1932 1933 1934 1935 1936 1937 1938
	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
		    __func__, karg.unique_id);
		return -EINVAL;
	}

1939
	if (!_ctl_diag_capability(ioc, buffer_type)) {
1940 1941
		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
			__func__, buffer_type);
1942 1943 1944 1945 1946
		return -EPERM;
	}

	if ((ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1947 1948
		ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
			__func__, buffer_type);
1949 1950 1951 1952
		return -EINVAL;
	}
	if ((ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
1953 1954
		ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
			__func__, buffer_type);
1955 1956 1957 1958
		return -EINVAL;
	}

	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1959 1960
		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
			__func__, karg.unique_id);
1961 1962 1963 1964 1965
		return -EINVAL;
	}

	request_data = ioc->diag_buffer[buffer_type];
	if (!request_data) {
1966 1967
		ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
			__func__, buffer_type);
1968 1969 1970
		return -ENOMEM;
	}

1971 1972 1973
	if (ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
		ioc->unique_id[buffer_type] = MPT3DIAGBUFFUNIQUEID;
1974 1975
		ioc->diag_buffer_status[buffer_type] &=
		    ~MPT3_DIAG_BUFFER_IS_APP_OWNED;
1976 1977 1978 1979 1980 1981 1982 1983 1984 1985
		ioc->diag_buffer_status[buffer_type] &=
		    ~MPT3_DIAG_BUFFER_IS_REGISTERED;
	} else {
		request_data_sz = ioc->diag_buffer_sz[buffer_type];
		request_data_dma = ioc->diag_buffer_dma[buffer_type];
		dma_free_coherent(&ioc->pdev->dev, request_data_sz,
				request_data, request_data_dma);
		ioc->diag_buffer[buffer_type] = NULL;
		ioc->diag_buffer_status[buffer_type] = 0;
	}
1986 1987 1988 1989 1990 1991
	return 0;
}

/**
 * _ctl_diag_query - query relevant info associated with diag buffers
 * @ioc: per adapter object
1992
 * @arg: user space buffer containing ioctl content
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011
 *
 * The application will send only buffer_type and unique_id.  Driver will
 * inspect unique_id first, if valid, fill in all the info.  If unique_id is
 * 0x00, the driver will return info specified by Buffer Type.
 */
static long
_ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
{
	struct mpt3_diag_query karg;
	void *request_data;
	int i;
	u8 buffer_type;

	if (copy_from_user(&karg, arg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

2012 2013
	dctlprintk(ioc, ioc_info(ioc, "%s\n",
				 __func__));
2014 2015 2016 2017 2018

	karg.application_flags = 0;
	buffer_type = karg.buffer_type;

	if (!_ctl_diag_capability(ioc, buffer_type)) {
2019 2020
		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
			__func__, buffer_type);
2021 2022 2023
		return -EPERM;
	}

2024 2025 2026 2027 2028 2029 2030 2031
	if (!(ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED)) {
		if ((ioc->diag_buffer_status[buffer_type] &
		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
			ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
				__func__, buffer_type);
			return -EINVAL;
		}
2032 2033
	}

2034
	if (karg.unique_id) {
2035
		if (karg.unique_id != ioc->unique_id[buffer_type]) {
2036 2037
			ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
				__func__, karg.unique_id);
2038 2039 2040 2041 2042 2043
			return -EINVAL;
		}
	}

	request_data = ioc->diag_buffer[buffer_type];
	if (!request_data) {
2044 2045
		ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
			__func__, buffer_type);
2046 2047 2048
		return -ENOMEM;
	}

2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059
	if ((ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_REGISTERED))
		karg.application_flags |= MPT3_APP_FLAGS_BUFFER_VALID;

	if (!(ioc->diag_buffer_status[buffer_type] &
	     MPT3_DIAG_BUFFER_IS_RELEASED))
		karg.application_flags |= MPT3_APP_FLAGS_FW_BUFFER_ACCESS;

	if (!(ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED))
		karg.application_flags |= MPT3_APP_FLAGS_DYNAMIC_BUFFER_ALLOC;
2060

2061 2062 2063 2064
	if ((ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_APP_OWNED))
		karg.application_flags |= MPT3_APP_FLAGS_APP_OWNED;

2065 2066 2067 2068 2069 2070 2071 2072 2073 2074
	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
		karg.product_specific[i] =
		    ioc->product_specific[buffer_type][i];

	karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
	karg.driver_added_buffer_size = 0;
	karg.unique_id = ioc->unique_id[buffer_type];
	karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];

	if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
2075 2076
		ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
			__func__, arg);
2077 2078 2079 2080 2081 2082 2083 2084
		return -EFAULT;
	}
	return 0;
}

/**
 * mpt3sas_send_diag_release - Diag Release Message
 * @ioc: per adapter object
2085 2086
 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
 * @issue_reset: specifies whether host reset is required.
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
 *
 */
int
mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
	u8 *issue_reset)
{
	Mpi2DiagReleaseRequest_t *mpi_request;
	Mpi2DiagReleaseReply_t *mpi_reply;
	u16 smid;
	u16 ioc_status;
	u32 ioc_state;
	int rc;

2100 2101
	dctlprintk(ioc, ioc_info(ioc, "%s\n",
				 __func__));
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111

	rc = 0;
	*issue_reset = 0;

	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
		if (ioc->diag_buffer_status[buffer_type] &
		    MPT3_DIAG_BUFFER_IS_REGISTERED)
			ioc->diag_buffer_status[buffer_type] |=
			    MPT3_DIAG_BUFFER_IS_RELEASED;
2112 2113 2114
		dctlprintk(ioc,
			   ioc_info(ioc, "%s: skipping due to FAULT state\n",
				    __func__));
2115 2116 2117 2118 2119
		rc = -EAGAIN;
		goto out;
	}

	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2120
		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
2121 2122 2123 2124 2125 2126
		rc = -EAGAIN;
		goto out;
	}

	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
	if (!smid) {
2127
		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142
		rc = -EAGAIN;
		goto out;
	}

	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
	ioc->ctl_cmds.smid = smid;

	mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
	mpi_request->BufferType = buffer_type;
	mpi_request->VF_ID = 0; /* TODO */
	mpi_request->VP_ID = 0;

	init_completion(&ioc->ctl_cmds.done);
2143
	ioc->put_smid_default(ioc, smid);
2144
	wait_for_completion_timeout(&ioc->ctl_cmds.done,
2145 2146 2147
	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);

	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2148 2149 2150
		*issue_reset = mpt3sas_base_check_cmd_timeout(ioc,
				ioc->ctl_cmds.status, mpi_request,
				sizeof(Mpi2DiagReleaseRequest_t)/4);
2151 2152 2153 2154 2155 2156
		rc = -EFAULT;
		goto out;
	}

	/* process the completed Reply Message Frame */
	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2157
		ioc_err(ioc, "%s: no reply message\n", __func__);
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
		rc = -EFAULT;
		goto out;
	}

	mpi_reply = ioc->ctl_cmds.reply;
	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;

	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
		ioc->diag_buffer_status[buffer_type] |=
		    MPT3_DIAG_BUFFER_IS_RELEASED;
2168
		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
2169
	} else {
2170 2171 2172
		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
			 __func__,
			 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2173 2174 2175 2176 2177 2178 2179 2180 2181 2182
		rc = -EFAULT;
	}

 out:
	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
	return rc;
}

/**
 * _ctl_diag_release - request to send Diag Release Message to firmware
2183 2184
 * @ioc: ?
 * @arg: user space buffer containing ioctl content
2185 2186 2187
 *
 * This allows ownership of the specified buffer to returned to the driver,
 * allowing an application to read the buffer without fear that firmware is
2188
 * overwriting information in the buffer.
2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
 */
static long
_ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
{
	struct mpt3_diag_release karg;
	void *request_data;
	int rc;
	u8 buffer_type;
	u8 issue_reset = 0;

	if (copy_from_user(&karg, arg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

2205 2206
	dctlprintk(ioc, ioc_info(ioc, "%s\n",
				 __func__));
2207

2208 2209 2210 2211 2212 2213 2214
	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
		    __func__, karg.unique_id);
		return -EINVAL;
	}

2215
	if (!_ctl_diag_capability(ioc, buffer_type)) {
2216 2217
		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
			__func__, buffer_type);
2218 2219 2220 2221 2222
		return -EPERM;
	}

	if ((ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2223 2224
		ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
			__func__, buffer_type);
2225 2226 2227 2228
		return -EINVAL;
	}

	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2229 2230
		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
			__func__, karg.unique_id);
2231 2232 2233 2234 2235
		return -EINVAL;
	}

	if (ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_RELEASED) {
2236 2237
		ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
			__func__, buffer_type);
2238
		return -EINVAL;
2239 2240 2241 2242 2243
	}

	request_data = ioc->diag_buffer[buffer_type];

	if (!request_data) {
2244 2245
		ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
			__func__, buffer_type);
2246 2247 2248 2249 2250 2251 2252 2253 2254 2255
		return -ENOMEM;
	}

	/* buffers were released by due to host reset */
	if ((ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
		ioc->diag_buffer_status[buffer_type] |=
		    MPT3_DIAG_BUFFER_IS_RELEASED;
		ioc->diag_buffer_status[buffer_type] &=
		    ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
2256 2257
		ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
			__func__, buffer_type);
2258 2259 2260 2261 2262 2263
		return 0;
	}

	rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);

	if (issue_reset)
2264
		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2265 2266 2267 2268 2269 2270 2271

	return rc;
}

/**
 * _ctl_diag_read_buffer - request for copy of the diag buffer
 * @ioc: per adapter object
2272
 * @arg: user space buffer containing ioctl content
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
 */
static long
_ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
{
	struct mpt3_diag_read_buffer karg;
	struct mpt3_diag_read_buffer __user *uarg = arg;
	void *request_data, *diag_data;
	Mpi2DiagBufferPostRequest_t *mpi_request;
	Mpi2DiagBufferPostReply_t *mpi_reply;
	int rc, i;
	u8 buffer_type;
2284
	unsigned long request_size, copy_size;
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
	u16 smid;
	u16 ioc_status;
	u8 issue_reset = 0;

	if (copy_from_user(&karg, arg, sizeof(karg))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

2295 2296
	dctlprintk(ioc, ioc_info(ioc, "%s\n",
				 __func__));
2297

2298 2299 2300 2301 2302 2303 2304
	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
		    __func__, karg.unique_id);
		return -EINVAL;
	}

2305
	if (!_ctl_diag_capability(ioc, buffer_type)) {
2306 2307
		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
			__func__, buffer_type);
2308 2309 2310 2311
		return -EPERM;
	}

	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2312 2313
		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
			__func__, karg.unique_id);
2314 2315 2316 2317 2318
		return -EINVAL;
	}

	request_data = ioc->diag_buffer[buffer_type];
	if (!request_data) {
2319 2320
		ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
			__func__, buffer_type);
2321 2322 2323 2324 2325 2326
		return -ENOMEM;
	}

	request_size = ioc->diag_buffer_sz[buffer_type];

	if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2327 2328
		ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
			__func__);
2329 2330 2331 2332 2333 2334 2335
		return -EINVAL;
	}

	if (karg.starting_offset > request_size)
		return -EINVAL;

	diag_data = (void *)(request_data + karg.starting_offset);
2336 2337 2338 2339
	dctlprintk(ioc,
		   ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
			    __func__, diag_data, karg.starting_offset,
			    karg.bytes_to_read));
2340 2341 2342 2343 2344 2345 2346 2347 2348 2349

	/* Truncate data on requests that are too large */
	if ((diag_data + karg.bytes_to_read < diag_data) ||
	    (diag_data + karg.bytes_to_read > request_data + request_size))
		copy_size = request_size - karg.starting_offset;
	else
		copy_size = karg.bytes_to_read;

	if (copy_to_user((void __user *)uarg->diagnostic_data,
	    diag_data, copy_size)) {
2350 2351
		ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
			__func__, diag_data);
2352 2353 2354 2355 2356 2357
		return -EFAULT;
	}

	if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
		return 0;

2358 2359 2360
	dctlprintk(ioc,
		   ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
			    __func__, buffer_type));
2361 2362
	if ((ioc->diag_buffer_status[buffer_type] &
	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2363 2364 2365
		dctlprintk(ioc,
			   ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
				    __func__, buffer_type));
2366 2367 2368 2369 2370 2371
		return 0;
	}
	/* Get a free request frame and save the message context.
	*/

	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2372
		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
2373 2374 2375 2376 2377 2378
		rc = -EAGAIN;
		goto out;
	}

	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
	if (!smid) {
2379
		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402
		rc = -EAGAIN;
		goto out;
	}

	rc = 0;
	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
	ioc->ctl_cmds.smid = smid;

	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
	mpi_request->BufferType = buffer_type;
	mpi_request->BufferLength =
	    cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
	mpi_request->BufferAddress =
	    cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
		mpi_request->ProductSpecific[i] =
			cpu_to_le32(ioc->product_specific[buffer_type][i]);
	mpi_request->VF_ID = 0; /* TODO */
	mpi_request->VP_ID = 0;

	init_completion(&ioc->ctl_cmds.done);
2403
	ioc->put_smid_default(ioc, smid);
2404
	wait_for_completion_timeout(&ioc->ctl_cmds.done,
2405 2406 2407
	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);

	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2408 2409 2410 2411
		issue_reset =
			mpt3sas_base_check_cmd_timeout(ioc,
				ioc->ctl_cmds.status, mpi_request,
				sizeof(Mpi2DiagBufferPostRequest_t)/4);
2412 2413 2414 2415 2416
		goto issue_host_reset;
	}

	/* process the completed Reply Message Frame */
	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2417
		ioc_err(ioc, "%s: no reply message\n", __func__);
2418 2419 2420 2421 2422 2423 2424 2425 2426 2427
		rc = -EFAULT;
		goto out;
	}

	mpi_reply = ioc->ctl_cmds.reply;
	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;

	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
		ioc->diag_buffer_status[buffer_type] |=
		    MPT3_DIAG_BUFFER_IS_REGISTERED;
2428 2429
		ioc->diag_buffer_status[buffer_type] &=
		    ~MPT3_DIAG_BUFFER_IS_RELEASED;
2430
		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
2431
	} else {
2432 2433 2434
		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
			 __func__, ioc_status,
			 le32_to_cpu(mpi_reply->IOCLogInfo));
2435 2436 2437 2438 2439
		rc = -EFAULT;
	}

 issue_host_reset:
	if (issue_reset)
2440
		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453

 out:

	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
	return rc;
}



#ifdef CONFIG_COMPAT
/**
 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
 * @ioc: per adapter object
2454 2455
 * @cmd: ioctl opcode
 * @arg: (struct mpt3_ioctl_command32)
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497
 *
 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
 */
static long
_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
	void __user *arg)
{
	struct mpt3_ioctl_command32 karg32;
	struct mpt3_ioctl_command32 __user *uarg;
	struct mpt3_ioctl_command karg;

	if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
		return -EINVAL;

	uarg = (struct mpt3_ioctl_command32 __user *) arg;

	if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

	memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
	karg.hdr.ioc_number = karg32.hdr.ioc_number;
	karg.hdr.port_number = karg32.hdr.port_number;
	karg.hdr.max_data_size = karg32.hdr.max_data_size;
	karg.timeout = karg32.timeout;
	karg.max_reply_bytes = karg32.max_reply_bytes;
	karg.data_in_size = karg32.data_in_size;
	karg.data_out_size = karg32.data_out_size;
	karg.max_sense_bytes = karg32.max_sense_bytes;
	karg.data_sge_offset = karg32.data_sge_offset;
	karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
	karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
	karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
	karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
	return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
}
#endif

/**
 * _ctl_ioctl_main - main ioctl entry point
2498 2499 2500 2501
 * @file:  (struct file)
 * @cmd:  ioctl opcode
 * @arg:  user space data buffer
 * @compat:  handles 32 bit applications in 64bit os
2502
 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
2503
 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
2504 2505 2506
 */
static long
_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2507
	u8 compat, u16 mpi_version)
2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521
{
	struct MPT3SAS_ADAPTER *ioc;
	struct mpt3_ioctl_header ioctl_header;
	enum block_state state;
	long ret = -EINVAL;

	/* get IOCTL header */
	if (copy_from_user(&ioctl_header, (char __user *)arg,
	    sizeof(struct mpt3_ioctl_header))) {
		pr_err("failure at %s:%d/%s()!\n",
		    __FILE__, __LINE__, __func__);
		return -EFAULT;
	}

2522 2523
	if (_ctl_verify_adapter(ioctl_header.ioc_number,
				&ioc, mpi_version) == -1 || !ioc)
2524 2525
		return -ENODEV;

2526 2527 2528
	/* pci_access_mutex lock acquired by ioctl path */
	mutex_lock(&ioc->pci_access_mutex);

2529
	if (ioc->shost_recovery || ioc->pci_error_recovery ||
2530 2531 2532 2533
	    ioc->is_driver_loading || ioc->remove_host) {
		ret = -EAGAIN;
		goto out_unlock_pciaccess;
	}
2534 2535 2536

	state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
	if (state == NON_BLOCKING) {
2537 2538 2539 2540 2541 2542 2543 2544
		if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
			ret = -EAGAIN;
			goto out_unlock_pciaccess;
		}
	} else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
		ret = -ERESTARTSYS;
		goto out_unlock_pciaccess;
	}
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572


	switch (cmd) {
	case MPT3IOCINFO:
		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
			ret = _ctl_getiocinfo(ioc, arg);
		break;
#ifdef CONFIG_COMPAT
	case MPT3COMMAND32:
#endif
	case MPT3COMMAND:
	{
		struct mpt3_ioctl_command __user *uarg;
		struct mpt3_ioctl_command karg;

#ifdef CONFIG_COMPAT
		if (compat) {
			ret = _ctl_compat_mpt_command(ioc, cmd, arg);
			break;
		}
#endif
		if (copy_from_user(&karg, arg, sizeof(karg))) {
			pr_err("failure at %s:%d/%s()!\n",
			    __FILE__, __LINE__, __func__);
			ret = -EFAULT;
			break;
		}

2573 2574 2575 2576
		if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
			ret = -EINVAL;
			break;
		}
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622
		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
			uarg = arg;
			ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
		}
		break;
	}
	case MPT3EVENTQUERY:
		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
			ret = _ctl_eventquery(ioc, arg);
		break;
	case MPT3EVENTENABLE:
		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
			ret = _ctl_eventenable(ioc, arg);
		break;
	case MPT3EVENTREPORT:
		ret = _ctl_eventreport(ioc, arg);
		break;
	case MPT3HARDRESET:
		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
			ret = _ctl_do_reset(ioc, arg);
		break;
	case MPT3BTDHMAPPING:
		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
			ret = _ctl_btdh_mapping(ioc, arg);
		break;
	case MPT3DIAGREGISTER:
		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
			ret = _ctl_diag_register(ioc, arg);
		break;
	case MPT3DIAGUNREGISTER:
		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
			ret = _ctl_diag_unregister(ioc, arg);
		break;
	case MPT3DIAGQUERY:
		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
			ret = _ctl_diag_query(ioc, arg);
		break;
	case MPT3DIAGRELEASE:
		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
			ret = _ctl_diag_release(ioc, arg);
		break;
	case MPT3DIAGREADBUFFER:
		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
			ret = _ctl_diag_read_buffer(ioc, arg);
		break;
	default:
2623 2624 2625
		dctlprintk(ioc,
			   ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
				    cmd));
2626 2627 2628 2629
		break;
	}

	mutex_unlock(&ioc->ctl_cmds.mutex);
2630 2631
out_unlock_pciaccess:
	mutex_unlock(&ioc->pci_access_mutex);
2632 2633 2634 2635
	return ret;
}

/**
2636
 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
2637 2638 2639
 * @file: (struct file)
 * @cmd: ioctl opcode
 * @arg: ?
2640
 */
2641
static long
2642
_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2643 2644 2645
{
	long ret;

2646 2647
	/* pass MPI25_VERSION | MPI26_VERSION value,
	 * to indicate that this ioctl cmd
2648 2649
	 * came from mpt3ctl ioctl device.
	 */
2650 2651
	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
		MPI25_VERSION | MPI26_VERSION);
2652 2653 2654
	return ret;
}

2655 2656
/**
 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
2657 2658 2659
 * @file: (struct file)
 * @cmd: ioctl opcode
 * @arg: ?
2660
 */
2661
static long
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671
_ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	long ret;

	/* pass MPI2_VERSION value, to indicate that this ioctl cmd
	 * came from mpt2ctl ioctl device.
	 */
	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
	return ret;
}
2672 2673
#ifdef CONFIG_COMPAT
/**
2674
 *_ ctl_ioctl_compat - main ioctl entry point (compat)
2675 2676 2677
 * @file: ?
 * @cmd: ?
 * @arg: ?
2678 2679 2680
 *
 * This routine handles 32 bit applications in 64bit os.
 */
2681
static long
2682
_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2683 2684 2685
{
	long ret;

2686 2687
	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
		MPI25_VERSION | MPI26_VERSION);
2688 2689 2690 2691 2692
	return ret;
}

/**
 *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
2693 2694 2695
 * @file: ?
 * @cmd: ?
 * @arg: ?
2696 2697 2698
 *
 * This routine handles 32 bit applications in 64bit os.
 */
2699
static long
2700 2701 2702 2703 2704
_ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
{
	long ret;

	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
2705 2706 2707 2708 2709 2710
	return ret;
}
#endif

/* scsi host attributes */
/**
2711
 * version_fw_show - firmware version
2712 2713 2714
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2715 2716 2717 2718
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2719
version_fw_show(struct device *cdev, struct device_attribute *attr,
2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
	    (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
	    (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
	    (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
	    ioc->facts.FWVersion.Word & 0x000000FF);
}
2731
static DEVICE_ATTR_RO(version_fw);
2732 2733

/**
2734
 * version_bios_show - bios version
2735 2736 2737
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2738 2739 2740 2741
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2742
version_bios_show(struct device *cdev, struct device_attribute *attr,
2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);

	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
	    (version & 0xFF000000) >> 24,
	    (version & 0x00FF0000) >> 16,
	    (version & 0x0000FF00) >> 8,
	    version & 0x000000FF);
}
2756
static DEVICE_ATTR_RO(version_bios);
2757 2758

/**
2759
 * version_mpi_show - MPI (message passing interface) version
2760 2761 2762
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2763 2764 2765 2766
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2767
version_mpi_show(struct device *cdev, struct device_attribute *attr,
2768 2769 2770 2771 2772 2773 2774 2775
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
	    ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
}
2776
static DEVICE_ATTR_RO(version_mpi);
2777 2778

/**
2779
 * version_product_show - product name
2780 2781 2782
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2783 2784 2785 2786
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2787
version_product_show(struct device *cdev, struct device_attribute *attr,
2788 2789 2790 2791 2792 2793 2794
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
}
2795
static DEVICE_ATTR_RO(version_product);
2796 2797

/**
2798
 * version_nvdata_persistent_show - ndvata persistent version
2799 2800 2801
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2802 2803 2804 2805
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2806
version_nvdata_persistent_show(struct device *cdev,
2807 2808 2809 2810 2811 2812 2813 2814
	struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "%08xh\n",
	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
}
2815
static DEVICE_ATTR_RO(version_nvdata_persistent);
2816 2817

/**
2818
 * version_nvdata_default_show - nvdata default version
2819 2820 2821
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2822 2823 2824 2825
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2826
version_nvdata_default_show(struct device *cdev, struct device_attribute
2827 2828 2829 2830 2831 2832 2833 2834
	*attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "%08xh\n",
	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
}
2835
static DEVICE_ATTR_RO(version_nvdata_default);
2836 2837

/**
2838
 * board_name_show - board name
2839 2840 2841
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2842 2843 2844 2845
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2846
board_name_show(struct device *cdev, struct device_attribute *attr,
2847 2848 2849 2850 2851 2852 2853
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
}
2854
static DEVICE_ATTR_RO(board_name);
2855 2856

/**
2857
 * board_assembly_show - board assembly name
2858 2859 2860
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2861 2862 2863 2864
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2865
board_assembly_show(struct device *cdev, struct device_attribute *attr,
2866 2867 2868 2869 2870 2871 2872
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
}
2873
static DEVICE_ATTR_RO(board_assembly);
2874 2875

/**
2876
 * board_tracer_show - board tracer number
2877 2878 2879
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2880 2881 2882 2883
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2884
board_tracer_show(struct device *cdev, struct device_attribute *attr,
2885 2886 2887 2888 2889 2890 2891
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
}
2892
static DEVICE_ATTR_RO(board_tracer);
2893 2894

/**
2895
 * io_delay_show - io missing delay
2896 2897 2898
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2899 2900 2901 2902 2903 2904 2905
 *
 * This is for firmware implemention for deboucing device
 * removal events.
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2906
io_delay_show(struct device *cdev, struct device_attribute *attr,
2907 2908 2909 2910 2911 2912 2913
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
}
2914
static DEVICE_ATTR_RO(io_delay);
2915 2916

/**
2917
 * device_delay_show - device missing delay
2918 2919 2920
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2921 2922 2923 2924 2925 2926 2927
 *
 * This is for firmware implemention for deboucing device
 * removal events.
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2928
device_delay_show(struct device *cdev, struct device_attribute *attr,
2929 2930 2931 2932 2933 2934 2935
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
}
2936
static DEVICE_ATTR_RO(device_delay);
2937 2938

/**
2939
 * fw_queue_depth_show - global credits
2940 2941 2942
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2943 2944 2945 2946 2947 2948
 *
 * This is firmware queue depth limit
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2949
fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2950 2951 2952 2953 2954 2955 2956
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
}
2957
static DEVICE_ATTR_RO(fw_queue_depth);
2958 2959

/**
2960
 * sas_address_show - sas address
2961 2962 2963
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2964 2965 2966 2967 2968 2969
 *
 * This is the controller sas address
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
2970
host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2971 2972 2973 2974 2975 2976 2977 2978 2979
	char *buf)

{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
	    (unsigned long long)ioc->sas_hba.sas_address);
}
2980
static DEVICE_ATTR_RO(host_sas_address);
2981 2982

/**
2983
 * logging_level_show - logging level
2984 2985 2986
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
2987 2988 2989 2990
 *
 * A sysfs 'read/write' shost attribute.
 */
static ssize_t
2991
logging_level_show(struct device *cdev, struct device_attribute *attr,
2992 2993 2994 2995 2996 2997 2998 2999
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
}
static ssize_t
3000
logging_level_store(struct device *cdev, struct device_attribute *attr,
3001 3002 3003 3004 3005 3006 3007 3008 3009 3010
	const char *buf, size_t count)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	int val = 0;

	if (sscanf(buf, "%x", &val) != 1)
		return -EINVAL;

	ioc->logging_level = val;
3011 3012
	ioc_info(ioc, "logging_level=%08xh\n",
		 ioc->logging_level);
3013 3014
	return strlen(buf);
}
3015
static DEVICE_ATTR_RW(logging_level);
3016 3017

/**
3018
 * fwfault_debug_show - show/store fwfault_debug
3019 3020 3021
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3022 3023 3024 3025 3026
 *
 * mpt3sas_fwfault_debug is command line option
 * A sysfs 'read/write' shost attribute.
 */
static ssize_t
3027
fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
3028 3029 3030 3031 3032 3033 3034 3035
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
}
static ssize_t
3036
fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046
	const char *buf, size_t count)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	int val = 0;

	if (sscanf(buf, "%d", &val) != 1)
		return -EINVAL;

	ioc->fwfault_debug = val;
3047 3048
	ioc_info(ioc, "fwfault_debug=%d\n",
		 ioc->fwfault_debug);
3049 3050
	return strlen(buf);
}
3051
static DEVICE_ATTR_RW(fwfault_debug);
3052 3053

/**
3054
 * ioc_reset_count_show - ioc reset count
3055 3056 3057
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3058 3059 3060 3061 3062 3063
 *
 * This is firmware queue depth limit
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
3064
ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
3065 3066 3067 3068 3069 3070 3071
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
}
3072
static DEVICE_ATTR_RO(ioc_reset_count);
3073 3074

/**
3075
 * reply_queue_count_show - number of reply queues
3076 3077 3078
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3079 3080 3081 3082 3083 3084
 *
 * This is number of reply queues
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
3085
reply_queue_count_show(struct device *cdev,
3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099
	struct device_attribute *attr, char *buf)
{
	u8 reply_queue_count;
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	if ((ioc->facts.IOCCapabilities &
	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
		reply_queue_count = ioc->reply_queue_count;
	else
		reply_queue_count = 1;

	return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
}
3100
static DEVICE_ATTR_RO(reply_queue_count);
3101

3102
/**
3103
 * BRM_status_show - Backup Rail Monitor Status
3104 3105 3106
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3107 3108 3109 3110 3111 3112
 *
 * This is number of reply queues
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
3113
BRM_status_show(struct device *cdev, struct device_attribute *attr,
3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
	Mpi2ConfigReply_t mpi_reply;
	u16 backup_rail_monitor_status = 0;
	u16 ioc_status;
	int sz;
	ssize_t rc = 0;

	if (!ioc->is_warpdrive) {
3126 3127
		ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
			__func__);
3128 3129
		goto out;
	}
3130 3131 3132 3133 3134 3135
	/* pci_access_mutex lock acquired by sysfs show path */
	mutex_lock(&ioc->pci_access_mutex);
	if (ioc->pci_error_recovery || ioc->remove_host) {
		mutex_unlock(&ioc->pci_access_mutex);
		return 0;
	}
3136 3137 3138 3139 3140

	/* allocate upto GPIOVal 36 entries */
	sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
	io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
	if (!io_unit_pg3) {
3141 3142
		ioc_err(ioc, "%s: failed allocating memory for iounit_pg3: (%d) bytes\n",
			__func__, sz);
3143 3144 3145 3146 3147
		goto out;
	}

	if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
	    0) {
3148 3149
		ioc_err(ioc, "%s: failed reading iounit_pg3\n",
			__func__);
3150 3151 3152 3153 3154
		goto out;
	}

	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3155 3156
		ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
			__func__, ioc_status);
3157 3158 3159 3160
		goto out;
	}

	if (io_unit_pg3->GPIOCount < 25) {
3161 3162
		ioc_err(ioc, "%s: iounit_pg3->GPIOCount less than 25 entries, detected (%d) entries\n",
			__func__, io_unit_pg3->GPIOCount);
3163 3164 3165 3166 3167 3168 3169 3170 3171
		goto out;
	}

	/* BRM status is in bit zero of GPIOVal[24] */
	backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
	rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));

 out:
	kfree(io_unit_pg3);
3172
	mutex_unlock(&ioc->pci_access_mutex);
3173 3174
	return rc;
}
3175
static DEVICE_ATTR_RO(BRM_status);
3176

3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187
struct DIAG_BUFFER_START {
	__le32	Size;
	__le32	DiagVersion;
	u8	BufferType;
	u8	Reserved[3];
	__le32	Reserved1;
	__le32	Reserved2;
	__le32	Reserved3;
};

/**
3188
 * host_trace_buffer_size_show - host buffer size (trace only)
3189 3190 3191
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3192 3193 3194 3195
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
3196
host_trace_buffer_size_show(struct device *cdev,
3197 3198 3199 3200 3201 3202 3203 3204
	struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	u32 size = 0;
	struct DIAG_BUFFER_START *request_data;

	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3205 3206
		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
			__func__);
3207 3208 3209 3210 3211
		return 0;
	}

	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3212 3213
		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
			__func__);
3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227
		return 0;
	}

	request_data = (struct DIAG_BUFFER_START *)
	    ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
	if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
	    le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
	    le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
	    le32_to_cpu(request_data->Reserved3) == 0x4742444c)
		size = le32_to_cpu(request_data->Size);

	ioc->ring_buffer_sz = size;
	return snprintf(buf, PAGE_SIZE, "%d\n", size);
}
3228
static DEVICE_ATTR_RO(host_trace_buffer_size);
3229 3230

/**
3231
 * host_trace_buffer_show - firmware ring buffer (trace only)
3232 3233 3234
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3235 3236 3237 3238 3239 3240 3241 3242
 *
 * A sysfs 'read/write' shost attribute.
 *
 * You will only be able to read 4k bytes of ring buffer at a time.
 * In order to read beyond 4k bytes, you will have to write out the
 * offset to the same attribute, it will move the pointer.
 */
static ssize_t
3243
host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
3244 3245 3246 3247 3248 3249 3250 3251
	char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	void *request_data;
	u32 size;

	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3252 3253
		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
			__func__);
3254 3255 3256 3257 3258
		return 0;
	}

	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3259 3260
		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
			__func__);
3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274
		return 0;
	}

	if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
		return 0;

	size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
	size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
	request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
	memcpy(buf, request_data, size);
	return size;
}

static ssize_t
3275
host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287
	const char *buf, size_t count)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	int val = 0;

	if (sscanf(buf, "%d", &val) != 1)
		return -EINVAL;

	ioc->ring_buffer_offset = val;
	return strlen(buf);
}
3288
static DEVICE_ATTR_RW(host_trace_buffer);
3289 3290 3291 3292 3293


/*****************************************/

/**
3294
 * host_trace_buffer_enable_show - firmware ring buffer (trace only)
3295 3296 3297
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3298 3299 3300 3301 3302 3303
 *
 * A sysfs 'read/write' shost attribute.
 *
 * This is a mechnism to post/release host_trace_buffers
 */
static ssize_t
3304
host_trace_buffer_enable_show(struct device *cdev,
3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321
	struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
	   ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
		return snprintf(buf, PAGE_SIZE, "off\n");
	else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
	    MPT3_DIAG_BUFFER_IS_RELEASED))
		return snprintf(buf, PAGE_SIZE, "release\n");
	else
		return snprintf(buf, PAGE_SIZE, "post\n");
}

static ssize_t
3322
host_trace_buffer_enable_store(struct device *cdev,
3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347
	struct device_attribute *attr, const char *buf, size_t count)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	char str[10] = "";
	struct mpt3_diag_register diag_register;
	u8 issue_reset = 0;

	/* don't allow post/release occurr while recovery is active */
	if (ioc->shost_recovery || ioc->remove_host ||
	    ioc->pci_error_recovery || ioc->is_driver_loading)
		return -EBUSY;

	if (sscanf(buf, "%9s", str) != 1)
		return -EINVAL;

	if (!strcmp(str, "post")) {
		/* exit out if host buffers are already posted */
		if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
		    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
		    MPT3_DIAG_BUFFER_IS_REGISTERED) &&
		    ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
		    MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
			goto out;
		memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3348
		ioc_info(ioc, "posting host trace buffers\n");
3349
		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3350 3351 3352 3353 3354 3355

		if (ioc->manu_pg11.HostTraceBufferMaxSizeKB != 0 &&
		    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0) {
			/* post the same buffer allocated previously */
			diag_register.requested_buffer_size =
			    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE];
3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374
		} else {
			/*
			 * Free the diag buffer memory which was previously
			 * allocated by an application.
			 */
			if ((ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0)
			    &&
			    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
			    MPT3_DIAG_BUFFER_IS_APP_OWNED)) {
				pci_free_consistent(ioc->pdev,
				    ioc->diag_buffer_sz[
				    MPI2_DIAG_BUF_TYPE_TRACE],
				    ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE],
				    ioc->diag_buffer_dma[
				    MPI2_DIAG_BUF_TYPE_TRACE]);
				ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE] =
				    NULL;
			}

3375
			diag_register.requested_buffer_size = (1024 * 1024);
3376
		}
3377

3378 3379 3380
		diag_register.unique_id =
		    (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
		    (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
3381 3382
		ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
		_ctl_diag_register_2(ioc,  &diag_register);
3383 3384 3385 3386 3387 3388 3389 3390 3391 3392
		if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
		    MPT3_DIAG_BUFFER_IS_REGISTERED) {
			ioc_info(ioc,
			    "Trace buffer %d KB allocated through sysfs\n",
			    diag_register.requested_buffer_size>>10);
			if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
				ioc->diag_buffer_status[
				    MPI2_DIAG_BUF_TYPE_TRACE] |=
				    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
		}
3393 3394 3395 3396 3397 3398 3399 3400 3401 3402
	} else if (!strcmp(str, "release")) {
		/* exit out if host buffers are already released */
		if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
			goto out;
		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
			goto out;
		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
		    MPT3_DIAG_BUFFER_IS_RELEASED))
			goto out;
3403
		ioc_info(ioc, "releasing host trace buffer\n");
3404 3405 3406 3407 3408 3409 3410
		mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
		    &issue_reset);
	}

 out:
	return strlen(buf);
}
3411
static DEVICE_ATTR_RW(host_trace_buffer_enable);
3412 3413 3414 3415

/*********** diagnostic trigger suppport *********************************/

/**
3416
 * diag_trigger_master_show - show the diag_trigger_master attribute
3417 3418 3419
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3420 3421 3422 3423
 *
 * A sysfs 'read/write' shost attribute.
 */
static ssize_t
3424
diag_trigger_master_show(struct device *cdev,
3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440
	struct device_attribute *attr, char *buf)

{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	unsigned long flags;
	ssize_t rc;

	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
	rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
	memcpy(buf, &ioc->diag_trigger_master, rc);
	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
	return rc;
}

/**
3441
 * diag_trigger_master_store - store the diag_trigger_master attribute
3442 3443 3444 3445
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
 * @count: ?
3446 3447 3448 3449
 *
 * A sysfs 'read/write' shost attribute.
 */
static ssize_t
3450
diag_trigger_master_store(struct device *cdev,
3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468
	struct device_attribute *attr, const char *buf, size_t count)

{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	unsigned long flags;
	ssize_t rc;

	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
	rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
	memset(&ioc->diag_trigger_master, 0,
	    sizeof(struct SL_WH_MASTER_TRIGGER_T));
	memcpy(&ioc->diag_trigger_master, buf, rc);
	ioc->diag_trigger_master.MasterData |=
	    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
	return rc;
}
3469
static DEVICE_ATTR_RW(diag_trigger_master);
3470 3471 3472


/**
3473
 * diag_trigger_event_show - show the diag_trigger_event attribute
3474 3475 3476
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3477 3478 3479 3480
 *
 * A sysfs 'read/write' shost attribute.
 */
static ssize_t
3481
diag_trigger_event_show(struct device *cdev,
3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496
	struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	unsigned long flags;
	ssize_t rc;

	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
	rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
	memcpy(buf, &ioc->diag_trigger_event, rc);
	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
	return rc;
}

/**
3497
 * diag_trigger_event_store - store the diag_trigger_event attribute
3498 3499 3500 3501
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
 * @count: ?
3502 3503 3504 3505
 *
 * A sysfs 'read/write' shost attribute.
 */
static ssize_t
3506
diag_trigger_event_store(struct device *cdev,
3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524
	struct device_attribute *attr, const char *buf, size_t count)

{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	unsigned long flags;
	ssize_t sz;

	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
	sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
	memset(&ioc->diag_trigger_event, 0,
	    sizeof(struct SL_WH_EVENT_TRIGGERS_T));
	memcpy(&ioc->diag_trigger_event, buf, sz);
	if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
		ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
	return sz;
}
3525
static DEVICE_ATTR_RW(diag_trigger_event);
3526 3527 3528


/**
3529
 * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
3530 3531 3532
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3533 3534 3535 3536
 *
 * A sysfs 'read/write' shost attribute.
 */
static ssize_t
3537
diag_trigger_scsi_show(struct device *cdev,
3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552
	struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	unsigned long flags;
	ssize_t rc;

	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
	rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
	memcpy(buf, &ioc->diag_trigger_scsi, rc);
	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
	return rc;
}

/**
3553
 * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
3554 3555 3556 3557
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
 * @count: ?
3558 3559 3560 3561
 *
 * A sysfs 'read/write' shost attribute.
 */
static ssize_t
3562
diag_trigger_scsi_store(struct device *cdev,
3563 3564 3565 3566 3567 3568 3569 3570
	struct device_attribute *attr, const char *buf, size_t count)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	unsigned long flags;
	ssize_t sz;

	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3571 3572
	sz = min(sizeof(ioc->diag_trigger_scsi), count);
	memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
3573 3574 3575 3576 3577 3578
	memcpy(&ioc->diag_trigger_scsi, buf, sz);
	if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
		ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
	return sz;
}
3579
static DEVICE_ATTR_RW(diag_trigger_scsi);
3580 3581 3582


/**
3583
 * diag_trigger_scsi_show - show the diag_trigger_mpi attribute
3584 3585 3586
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3587 3588 3589 3590
 *
 * A sysfs 'read/write' shost attribute.
 */
static ssize_t
3591
diag_trigger_mpi_show(struct device *cdev,
3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606
	struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	unsigned long flags;
	ssize_t rc;

	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
	rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
	memcpy(buf, &ioc->diag_trigger_mpi, rc);
	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
	return rc;
}

/**
3607
 * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
3608 3609 3610 3611
 * @cdev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
 * @count: ?
3612 3613 3614 3615
 *
 * A sysfs 'read/write' shost attribute.
 */
static ssize_t
3616
diag_trigger_mpi_store(struct device *cdev,
3617 3618 3619 3620 3621 3622 3623 3624 3625 3626
	struct device_attribute *attr, const char *buf, size_t count)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	unsigned long flags;
	ssize_t sz;

	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
	sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
	memset(&ioc->diag_trigger_mpi, 0,
3627
	    sizeof(ioc->diag_trigger_mpi));
3628 3629 3630 3631 3632 3633 3634
	memcpy(&ioc->diag_trigger_mpi, buf, sz);
	if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
		ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
	return sz;
}

3635
static DEVICE_ATTR_RW(diag_trigger_mpi);
3636 3637 3638 3639 3640

/*********** diagnostic trigger suppport *** END ****************************/

/*****************************************/

3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658
/**
 * drv_support_bitmap_show - driver supported feature bitmap
 * @cdev - pointer to embedded class device
 * @buf - the buffer returned
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
drv_support_bitmap_show(struct device *cdev,
	struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "0x%08x\n", ioc->drv_support_bitmap);
}
static DEVICE_ATTR_RO(drv_support_bitmap);

3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759
/**
 * enable_sdev_max_qd_show - display whether sdev max qd is enabled/disabled
 * @cdev - pointer to embedded class device
 * @buf - the buffer returned
 *
 * A sysfs read/write shost attribute. This attribute is used to set the
 * targets queue depth to HBA IO queue depth if this attribute is enabled.
 */
static ssize_t
enable_sdev_max_qd_show(struct device *cdev,
	struct device_attribute *attr, char *buf)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);

	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->enable_sdev_max_qd);
}

/**
 * enable_sdev_max_qd_store - Enable/disable sdev max qd
 * @cdev - pointer to embedded class device
 * @buf - the buffer returned
 *
 * A sysfs read/write shost attribute. This attribute is used to set the
 * targets queue depth to HBA IO queue depth if this attribute is enabled.
 * If this attribute is disabled then targets will have corresponding default
 * queue depth.
 */
static ssize_t
enable_sdev_max_qd_store(struct device *cdev,
	struct device_attribute *attr, const char *buf, size_t count)
{
	struct Scsi_Host *shost = class_to_shost(cdev);
	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
	struct MPT3SAS_DEVICE *sas_device_priv_data;
	struct MPT3SAS_TARGET *sas_target_priv_data;
	int val = 0;
	struct scsi_device *sdev;
	struct _raid_device *raid_device;
	int qdepth;

	if (kstrtoint(buf, 0, &val) != 0)
		return -EINVAL;

	switch (val) {
	case 0:
		ioc->enable_sdev_max_qd = 0;
		shost_for_each_device(sdev, ioc->shost) {
			sas_device_priv_data = sdev->hostdata;
			if (!sas_device_priv_data)
				continue;
			sas_target_priv_data = sas_device_priv_data->sas_target;
			if (!sas_target_priv_data)
				continue;

			if (sas_target_priv_data->flags &
			    MPT_TARGET_FLAGS_VOLUME) {
				raid_device =
				    mpt3sas_raid_device_find_by_handle(ioc,
				    sas_target_priv_data->handle);

				switch (raid_device->volume_type) {
				case MPI2_RAID_VOL_TYPE_RAID0:
					if (raid_device->device_info &
					    MPI2_SAS_DEVICE_INFO_SSP_TARGET)
						qdepth =
						    MPT3SAS_SAS_QUEUE_DEPTH;
					else
						qdepth =
						    MPT3SAS_SATA_QUEUE_DEPTH;
					break;
				case MPI2_RAID_VOL_TYPE_RAID1E:
				case MPI2_RAID_VOL_TYPE_RAID1:
				case MPI2_RAID_VOL_TYPE_RAID10:
				case MPI2_RAID_VOL_TYPE_UNKNOWN:
				default:
					qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
				}
			} else if (sas_target_priv_data->flags &
			    MPT_TARGET_FLAGS_PCIE_DEVICE)
				qdepth = MPT3SAS_NVME_QUEUE_DEPTH;
			else
				qdepth = MPT3SAS_SAS_QUEUE_DEPTH;

			mpt3sas_scsih_change_queue_depth(sdev, qdepth);
		}
		break;
	case 1:
		ioc->enable_sdev_max_qd = 1;
		shost_for_each_device(sdev, ioc->shost)
			mpt3sas_scsih_change_queue_depth(sdev,
			    shost->can_queue);
		break;
	default:
		return -EINVAL;
	}

	return strlen(buf);
}
static DEVICE_ATTR_RW(enable_sdev_max_qd);

3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784
struct device_attribute *mpt3sas_host_attrs[] = {
	&dev_attr_version_fw,
	&dev_attr_version_bios,
	&dev_attr_version_mpi,
	&dev_attr_version_product,
	&dev_attr_version_nvdata_persistent,
	&dev_attr_version_nvdata_default,
	&dev_attr_board_name,
	&dev_attr_board_assembly,
	&dev_attr_board_tracer,
	&dev_attr_io_delay,
	&dev_attr_device_delay,
	&dev_attr_logging_level,
	&dev_attr_fwfault_debug,
	&dev_attr_fw_queue_depth,
	&dev_attr_host_sas_address,
	&dev_attr_ioc_reset_count,
	&dev_attr_host_trace_buffer_size,
	&dev_attr_host_trace_buffer,
	&dev_attr_host_trace_buffer_enable,
	&dev_attr_reply_queue_count,
	&dev_attr_diag_trigger_master,
	&dev_attr_diag_trigger_event,
	&dev_attr_diag_trigger_scsi,
	&dev_attr_diag_trigger_mpi,
3785
	&dev_attr_drv_support_bitmap,
3786
	&dev_attr_BRM_status,
3787
	&dev_attr_enable_sdev_max_qd,
3788 3789 3790 3791 3792 3793
	NULL,
};

/* device attributes */

/**
3794
 * sas_address_show - sas address
3795 3796 3797
 * @dev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3798 3799 3800 3801 3802 3803
 *
 * This is the sas address for the target
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
3804
sas_address_show(struct device *dev, struct device_attribute *attr,
3805 3806 3807 3808 3809 3810 3811 3812
	char *buf)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;

	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
	    (unsigned long long)sas_device_priv_data->sas_target->sas_address);
}
3813
static DEVICE_ATTR_RO(sas_address);
3814 3815

/**
3816
 * sas_device_handle_show - device handle
3817 3818 3819
 * @dev: pointer to embedded class device
 * @attr: ?
 * @buf: the buffer returned
3820 3821 3822 3823 3824 3825
 *
 * This is the firmware assigned device handle
 *
 * A sysfs 'read-only' shost attribute.
 */
static ssize_t
3826
sas_device_handle_show(struct device *dev, struct device_attribute *attr,
3827 3828 3829 3830 3831 3832 3833 3834
	char *buf)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;

	return snprintf(buf, PAGE_SIZE, "0x%04x\n",
	    sas_device_priv_data->sas_target->handle);
}
3835
static DEVICE_ATTR_RO(sas_device_handle);
3836

3837
/**
3838
 * sas_ncq_io_prio_show - send prioritized io commands to device
3839 3840 3841
 * @dev: pointer to embedded device
 * @attr: ?
 * @buf: the buffer returned
3842 3843 3844 3845
 *
 * A sysfs 'read/write' sdev attribute, only works with SATA
 */
static ssize_t
3846
sas_ncq_prio_enable_show(struct device *dev,
3847 3848 3849 3850 3851 3852 3853 3854 3855 3856
				 struct device_attribute *attr, char *buf)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;

	return snprintf(buf, PAGE_SIZE, "%d\n",
			sas_device_priv_data->ncq_prio_enable);
}

static ssize_t
3857
sas_ncq_prio_enable_store(struct device *dev,
3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873
				  struct device_attribute *attr,
				  const char *buf, size_t count)
{
	struct scsi_device *sdev = to_scsi_device(dev);
	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
	bool ncq_prio_enable = 0;

	if (kstrtobool(buf, &ncq_prio_enable))
		return -EINVAL;

	if (!scsih_ncq_prio_supp(sdev))
		return -EINVAL;

	sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
	return strlen(buf);
}
3874
static DEVICE_ATTR_RW(sas_ncq_prio_enable);
3875

3876 3877 3878
struct device_attribute *mpt3sas_dev_attrs[] = {
	&dev_attr_sas_address,
	&dev_attr_sas_device_handle,
3879
	&dev_attr_sas_ncq_prio_enable,
3880 3881 3882
	NULL,
};

3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916
/* file operations table for mpt3ctl device */
static const struct file_operations ctl_fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = _ctl_ioctl,
	.poll = _ctl_poll,
	.fasync = _ctl_fasync,
#ifdef CONFIG_COMPAT
	.compat_ioctl = _ctl_ioctl_compat,
#endif
};

/* file operations table for mpt2ctl device */
static const struct file_operations ctl_gen2_fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = _ctl_mpt2_ioctl,
	.poll = _ctl_poll,
	.fasync = _ctl_fasync,
#ifdef CONFIG_COMPAT
	.compat_ioctl = _ctl_mpt2_ioctl_compat,
#endif
};

static struct miscdevice ctl_dev = {
	.minor  = MPT3SAS_MINOR,
	.name   = MPT3SAS_DEV_NAME,
	.fops   = &ctl_fops,
};

static struct miscdevice gen2_ctl_dev = {
	.minor  = MPT2SAS_MINOR,
	.name   = MPT2SAS_DEV_NAME,
	.fops   = &ctl_gen2_fops,
};

3917
/**
3918
 * mpt3sas_ctl_init - main entry point for ctl.
3919
 * @hbas_to_enumerate: ?
3920 3921
 */
void
3922
mpt3sas_ctl_init(ushort hbas_to_enumerate)
3923 3924
{
	async_queue = NULL;
3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941

	/* Don't register mpt3ctl ioctl device if
	 * hbas_to_enumarate is one.
	 */
	if (hbas_to_enumerate != 1)
		if (misc_register(&ctl_dev) < 0)
			pr_err("%s can't register misc device [minor=%d]\n",
			    MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);

	/* Don't register mpt3ctl ioctl device if
	 * hbas_to_enumarate is two.
	 */
	if (hbas_to_enumerate != 2)
		if (misc_register(&gen2_ctl_dev) < 0)
			pr_err("%s can't register misc device [minor=%d]\n",
			    MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);

3942 3943 3944 3945
	init_waitqueue_head(&ctl_poll_wait);
}

/**
3946
 * mpt3sas_ctl_exit - exit point for ctl
3947
 * @hbas_to_enumerate: ?
3948 3949
 */
void
3950
mpt3sas_ctl_exit(ushort hbas_to_enumerate)
3951 3952 3953 3954 3955 3956 3957 3958 3959 3960
{
	struct MPT3SAS_ADAPTER *ioc;
	int i;

	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {

		/* free memory associated to diag buffers */
		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
			if (!ioc->diag_buffer[i])
				continue;
3961 3962 3963 3964
			dma_free_coherent(&ioc->pdev->dev,
					  ioc->diag_buffer_sz[i],
					  ioc->diag_buffer[i],
					  ioc->diag_buffer_dma[i]);
3965 3966 3967 3968 3969 3970
			ioc->diag_buffer[i] = NULL;
			ioc->diag_buffer_status[i] = 0;
		}

		kfree(ioc->event_log);
	}
3971 3972 3973 3974
	if (hbas_to_enumerate != 1)
		misc_deregister(&ctl_dev);
	if (hbas_to_enumerate != 2)
		misc_deregister(&gen2_ctl_dev);
3975
}