Linux Audio

Check our new training course

Loading...
v3.15
   1/*
   2 * Management Module Support for MPT (Message Passing Technology) based
   3 * controllers
   4 *
   5 * This code is based on drivers/scsi/mpt2sas/mpt2_ctl.c
   6 * Copyright (C) 2007-2013  LSI Corporation
   7 *  (mailto:DL-MPTFusionLinux@lsi.com)
   8 *
   9 * This program is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU General Public License
  11 * as published by the Free Software Foundation; either version 2
  12 * of the License, or (at your option) any later version.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 * GNU General Public License for more details.
  18 *
  19 * NO WARRANTY
  20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24 * solely responsible for determining the appropriateness of using and
  25 * distributing the Program and assumes all risks associated with its
  26 * exercise of rights under this Agreement, including but not limited to
  27 * the risks and costs of program errors, damage to or loss of data,
  28 * programs or equipment, and unavailability or interruption of operations.
  29
  30 * DISCLAIMER OF LIABILITY
  31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  38
  39 * You should have received a copy of the GNU General Public License
  40 * along with this program; if not, write to the Free Software
  41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
  42 * USA.
  43 */
  44
  45#include <linux/kernel.h>
  46#include <linux/module.h>
  47#include <linux/errno.h>
  48#include <linux/init.h>
  49#include <linux/slab.h>
  50#include <linux/types.h>
  51#include <linux/pci.h>
  52#include <linux/delay.h>
  53#include <linux/mutex.h>
  54#include <linux/compat.h>
  55#include <linux/poll.h>
  56
  57#include <linux/io.h>
  58#include <linux/uaccess.h>
  59
  60#include "mpt2sas_base.h"
  61#include "mpt2sas_ctl.h"
  62
  63static DEFINE_MUTEX(_ctl_mutex);
  64static struct fasync_struct *async_queue;
  65static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
  66
  67static int _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type,
  68    u8 *issue_reset);
  69
  70/**
  71 * enum block_state - blocking state
  72 * @NON_BLOCKING: non blocking
  73 * @BLOCKING: blocking
  74 *
  75 * These states are for ioctls that need to wait for a response
  76 * from firmware, so they probably require sleep.
  77 */
  78enum block_state {
  79	NON_BLOCKING,
  80	BLOCKING,
  81};
  82
  83#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  84/**
  85 * _ctl_sas_device_find_by_handle - sas device search
  86 * @ioc: per adapter object
  87 * @handle: sas device handle (assigned by firmware)
  88 * Context: Calling function should acquire ioc->sas_device_lock
  89 *
  90 * This searches for sas_device based on sas_address, then return sas_device
  91 * object.
  92 */
  93static struct _sas_device *
  94_ctl_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  95{
  96	struct _sas_device *sas_device, *r;
  97
  98	r = NULL;
  99	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
 100		if (sas_device->handle != handle)
 101			continue;
 102		r = sas_device;
 103		goto out;
 104	}
 105
 106 out:
 107	return r;
 108}
 109
 110/**
 111 * _ctl_display_some_debug - debug routine
 112 * @ioc: per adapter object
 113 * @smid: system request message index
 114 * @calling_function_name: string pass from calling function
 115 * @mpi_reply: reply message frame
 116 * Context: none.
 117 *
 118 * Function for displaying debug info helpful when debugging issues
 119 * in this module.
 120 */
 121static void
 122_ctl_display_some_debug(struct MPT2SAS_ADAPTER *ioc, u16 smid,
 123    char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
 124{
 125	Mpi2ConfigRequest_t *mpi_request;
 126	char *desc = NULL;
 127
 128	if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
 129		return;
 130
 131	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
 132	switch (mpi_request->Function) {
 133	case MPI2_FUNCTION_SCSI_IO_REQUEST:
 134	{
 135		Mpi2SCSIIORequest_t *scsi_request =
 136		    (Mpi2SCSIIORequest_t *)mpi_request;
 137
 138		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
 139		    "scsi_io, cmd(0x%02x), cdb_len(%d)",
 140		    scsi_request->CDB.CDB32[0],
 141		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
 142		desc = ioc->tmp_string;
 143		break;
 144	}
 145	case MPI2_FUNCTION_SCSI_TASK_MGMT:
 146		desc = "task_mgmt";
 147		break;
 148	case MPI2_FUNCTION_IOC_INIT:
 149		desc = "ioc_init";
 150		break;
 151	case MPI2_FUNCTION_IOC_FACTS:
 152		desc = "ioc_facts";
 153		break;
 154	case MPI2_FUNCTION_CONFIG:
 155	{
 156		Mpi2ConfigRequest_t *config_request =
 157		    (Mpi2ConfigRequest_t *)mpi_request;
 158
 159		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
 160		    "config, type(0x%02x), ext_type(0x%02x), number(%d)",
 161		    (config_request->Header.PageType &
 162		     MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
 163		    config_request->Header.PageNumber);
 164		desc = ioc->tmp_string;
 165		break;
 166	}
 167	case MPI2_FUNCTION_PORT_FACTS:
 168		desc = "port_facts";
 169		break;
 170	case MPI2_FUNCTION_PORT_ENABLE:
 171		desc = "port_enable";
 172		break;
 173	case MPI2_FUNCTION_EVENT_NOTIFICATION:
 174		desc = "event_notification";
 175		break;
 176	case MPI2_FUNCTION_FW_DOWNLOAD:
 177		desc = "fw_download";
 178		break;
 179	case MPI2_FUNCTION_FW_UPLOAD:
 180		desc = "fw_upload";
 181		break;
 182	case MPI2_FUNCTION_RAID_ACTION:
 183		desc = "raid_action";
 184		break;
 185	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
 186	{
 187		Mpi2SCSIIORequest_t *scsi_request =
 188		    (Mpi2SCSIIORequest_t *)mpi_request;
 189
 190		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
 191		    "raid_pass, cmd(0x%02x), cdb_len(%d)",
 192		    scsi_request->CDB.CDB32[0],
 193		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
 194		desc = ioc->tmp_string;
 195		break;
 196	}
 197	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
 198		desc = "sas_iounit_cntl";
 199		break;
 200	case MPI2_FUNCTION_SATA_PASSTHROUGH:
 201		desc = "sata_pass";
 202		break;
 203	case MPI2_FUNCTION_DIAG_BUFFER_POST:
 204		desc = "diag_buffer_post";
 205		break;
 206	case MPI2_FUNCTION_DIAG_RELEASE:
 207		desc = "diag_release";
 208		break;
 209	case MPI2_FUNCTION_SMP_PASSTHROUGH:
 210		desc = "smp_passthrough";
 211		break;
 212	}
 213
 214	if (!desc)
 215		return;
 216
 217	printk(MPT2SAS_INFO_FMT "%s: %s, smid(%d)\n",
 218	    ioc->name, calling_function_name, desc, smid);
 219
 220	if (!mpi_reply)
 221		return;
 222
 223	if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
 224		printk(MPT2SAS_INFO_FMT
 225		    "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
 226		    ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
 227		    le32_to_cpu(mpi_reply->IOCLogInfo));
 228
 229	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
 230	    mpi_request->Function ==
 231	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
 232		Mpi2SCSIIOReply_t *scsi_reply =
 233		    (Mpi2SCSIIOReply_t *)mpi_reply;
 234		struct _sas_device *sas_device = NULL;
 235		unsigned long flags;
 236
 237		spin_lock_irqsave(&ioc->sas_device_lock, flags);
 238		sas_device = _ctl_sas_device_find_by_handle(ioc,
 239		    le16_to_cpu(scsi_reply->DevHandle));
 240		if (sas_device) {
 241			printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
 242			    "phy(%d)\n", ioc->name, (unsigned long long)
 243			    sas_device->sas_address, sas_device->phy);
 244			printk(MPT2SAS_WARN_FMT
 245			    "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
 246			    ioc->name, sas_device->enclosure_logical_id,
 247			    sas_device->slot);
 248		}
 249		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 250		if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
 251			printk(MPT2SAS_INFO_FMT
 252			    "\tscsi_state(0x%02x), scsi_status"
 253			    "(0x%02x)\n", ioc->name,
 254			    scsi_reply->SCSIState,
 255			    scsi_reply->SCSIStatus);
 256	}
 257}
 258#endif
 259
 260/**
 261 * mpt2sas_ctl_done - ctl module completion routine
 262 * @ioc: per adapter object
 263 * @smid: system request message index
 264 * @msix_index: MSIX table index supplied by the OS
 265 * @reply: reply message frame(lower 32bit addr)
 266 * Context: none.
 267 *
 268 * The callback handler when using ioc->ctl_cb_idx.
 269 *
 270 * Return 1 meaning mf should be freed from _base_interrupt
 271 *        0 means the mf is freed from this function.
 272 */
 273u8
 274mpt2sas_ctl_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
 275	u32 reply)
 276{
 277	MPI2DefaultReply_t *mpi_reply;
 278	Mpi2SCSIIOReply_t *scsiio_reply;
 279	const void *sense_data;
 280	u32 sz;
 281
 282	if (ioc->ctl_cmds.status == MPT2_CMD_NOT_USED)
 283		return 1;
 284	if (ioc->ctl_cmds.smid != smid)
 285		return 1;
 286	ioc->ctl_cmds.status |= MPT2_CMD_COMPLETE;
 287	mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
 288	if (mpi_reply) {
 289		memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
 290		ioc->ctl_cmds.status |= MPT2_CMD_REPLY_VALID;
 291		/* get sense data */
 292		if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
 293		    mpi_reply->Function ==
 294		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
 295			scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
 296			if (scsiio_reply->SCSIState &
 297			    MPI2_SCSI_STATE_AUTOSENSE_VALID) {
 298				sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
 299				    le32_to_cpu(scsiio_reply->SenseCount));
 300				sense_data = mpt2sas_base_get_sense_buffer(ioc,
 301				    smid);
 302				memcpy(ioc->ctl_cmds.sense, sense_data, sz);
 303			}
 304		}
 305	}
 306#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
 307	_ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
 308#endif
 309	ioc->ctl_cmds.status &= ~MPT2_CMD_PENDING;
 310	complete(&ioc->ctl_cmds.done);
 311	return 1;
 312}
 313
 314/**
 315 * _ctl_check_event_type - determines when an event needs logging
 316 * @ioc: per adapter object
 317 * @event: firmware event
 318 *
 319 * The bitmask in ioc->event_type[] indicates which events should be
 320 * be saved in the driver event_log.  This bitmask is set by application.
 321 *
 322 * Returns 1 when event should be captured, or zero means no match.
 323 */
 324static int
 325_ctl_check_event_type(struct MPT2SAS_ADAPTER *ioc, u16 event)
 326{
 327	u16 i;
 328	u32 desired_event;
 329
 330	if (event >= 128 || !event || !ioc->event_log)
 331		return 0;
 332
 333	desired_event = (1 << (event % 32));
 334	if (!desired_event)
 335		desired_event = 1;
 336	i = event / 32;
 337	return desired_event & ioc->event_type[i];
 338}
 339
 340/**
 341 * mpt2sas_ctl_add_to_event_log - add event
 342 * @ioc: per adapter object
 343 * @mpi_reply: reply message frame
 344 *
 345 * Return nothing.
 346 */
 347void
 348mpt2sas_ctl_add_to_event_log(struct MPT2SAS_ADAPTER *ioc,
 349    Mpi2EventNotificationReply_t *mpi_reply)
 350{
 351	struct MPT2_IOCTL_EVENTS *event_log;
 352	u16 event;
 353	int i;
 354	u32 sz, event_data_sz;
 355	u8 send_aen = 0;
 356
 357	if (!ioc->event_log)
 358		return;
 359
 360	event = le16_to_cpu(mpi_reply->Event);
 361
 362	if (_ctl_check_event_type(ioc, event)) {
 363
 364		/* insert entry into circular event_log */
 365		i = ioc->event_context % MPT2SAS_CTL_EVENT_LOG_SIZE;
 366		event_log = ioc->event_log;
 367		event_log[i].event = event;
 368		event_log[i].context = ioc->event_context++;
 369
 370		event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
 371		sz = min_t(u32, event_data_sz, MPT2_EVENT_DATA_SIZE);
 372		memset(event_log[i].data, 0, MPT2_EVENT_DATA_SIZE);
 373		memcpy(event_log[i].data, mpi_reply->EventData, sz);
 374		send_aen = 1;
 375	}
 376
 377	/* This aen_event_read_flag flag is set until the
 378	 * application has read the event log.
 379	 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
 380	 */
 381	if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
 382	    (send_aen && !ioc->aen_event_read_flag)) {
 383		ioc->aen_event_read_flag = 1;
 384		wake_up_interruptible(&ctl_poll_wait);
 385		if (async_queue)
 386			kill_fasync(&async_queue, SIGIO, POLL_IN);
 387	}
 388}
 389
 390/**
 391 * mpt2sas_ctl_event_callback - firmware event handler (called at ISR time)
 392 * @ioc: per adapter object
 393 * @msix_index: MSIX table index supplied by the OS
 394 * @reply: reply message frame(lower 32bit addr)
 395 * Context: interrupt.
 396 *
 397 * This function merely adds a new work task into ioc->firmware_event_thread.
 398 * The tasks are worked from _firmware_event_work in user context.
 399 *
 400 * Returns void.
 
 401 */
 402void
 403mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
 404	u32 reply)
 405{
 406	Mpi2EventNotificationReply_t *mpi_reply;
 407
 408	mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
 409	if (unlikely(!mpi_reply)) {
 410		printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
 411		    ioc->name, __FILE__, __LINE__, __func__);
 412		return;
 413	}
 414	mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
 415	return;
 416}
 417
 418/**
 419 * _ctl_verify_adapter - validates ioc_number passed from application
 420 * @ioc: per adapter object
 421 * @iocpp: The ioc pointer is returned in this.
 422 *
 423 * Return (-1) means error, else ioc_number.
 424 */
 425static int
 426_ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
 427{
 428	struct MPT2SAS_ADAPTER *ioc;
 429
 430	list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
 431		if (ioc->id != ioc_number)
 432			continue;
 433		*iocpp = ioc;
 434		return ioc_number;
 435	}
 436	*iocpp = NULL;
 437	return -1;
 438}
 439
 440/**
 441 * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
 442 * @ioc: per adapter object
 443 * @reset_phase: phase
 444 *
 445 * The handler for doing any required cleanup or initialization.
 446 *
 447 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
 448 * MPT2_IOC_DONE_RESET
 449 */
 450void
 451mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
 452{
 453	int i;
 454	u8 issue_reset;
 455
 456	switch (reset_phase) {
 457	case MPT2_IOC_PRE_RESET:
 458		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
 459		    "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
 460		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
 461			if (!(ioc->diag_buffer_status[i] &
 462			    MPT2_DIAG_BUFFER_IS_REGISTERED))
 463				continue;
 464			if ((ioc->diag_buffer_status[i] &
 465			    MPT2_DIAG_BUFFER_IS_RELEASED))
 466				continue;
 467			_ctl_send_release(ioc, i, &issue_reset);
 468		}
 469		break;
 470	case MPT2_IOC_AFTER_RESET:
 471		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
 472		    "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
 473		if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
 474			ioc->ctl_cmds.status |= MPT2_CMD_RESET;
 475			mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
 476			complete(&ioc->ctl_cmds.done);
 477		}
 478		break;
 479	case MPT2_IOC_DONE_RESET:
 480		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
 481		    "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
 482
 483		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
 484			if (!(ioc->diag_buffer_status[i] &
 485			    MPT2_DIAG_BUFFER_IS_REGISTERED))
 486				continue;
 487			if ((ioc->diag_buffer_status[i] &
 488			    MPT2_DIAG_BUFFER_IS_RELEASED))
 489				continue;
 490			ioc->diag_buffer_status[i] |=
 491			    MPT2_DIAG_BUFFER_IS_DIAG_RESET;
 492		}
 493		break;
 494	}
 495}
 496
 497/**
 498 * _ctl_fasync -
 499 * @fd -
 500 * @filep -
 501 * @mode -
 502 *
 503 * Called when application request fasyn callback handler.
 504 */
 505static int
 506_ctl_fasync(int fd, struct file *filep, int mode)
 507{
 508	return fasync_helper(fd, filep, mode, &async_queue);
 509}
 510
 511/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 512 * _ctl_poll -
 513 * @file -
 514 * @wait -
 515 *
 516 */
 517static unsigned int
 518_ctl_poll(struct file *filep, poll_table *wait)
 519{
 520	struct MPT2SAS_ADAPTER *ioc;
 521
 522	poll_wait(filep, &ctl_poll_wait, wait);
 523
 524	list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
 525		if (ioc->aen_event_read_flag)
 526			return POLLIN | POLLRDNORM;
 527	}
 528	return 0;
 529}
 530
 531/**
 532 * _ctl_set_task_mid - assign an active smid to tm request
 533 * @ioc: per adapter object
 534 * @karg - (struct mpt2_ioctl_command)
 535 * @tm_request - pointer to mf from user space
 536 *
 537 * Returns 0 when an smid if found, else fail.
 538 * during failure, the reply frame is filled.
 539 */
 540static int
 541_ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
 542    Mpi2SCSITaskManagementRequest_t *tm_request)
 543{
 544	u8 found = 0;
 545	u16 i;
 546	u16 handle;
 547	struct scsi_cmnd *scmd;
 548	struct MPT2SAS_DEVICE *priv_data;
 549	unsigned long flags;
 550	Mpi2SCSITaskManagementReply_t *tm_reply;
 551	u32 sz;
 552	u32 lun;
 553	char *desc = NULL;
 554
 555	if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
 556		desc = "abort_task";
 557	else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
 558		desc = "query_task";
 559	else
 560		return 0;
 561
 562	lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
 563
 564	handle = le16_to_cpu(tm_request->DevHandle);
 565	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
 566	for (i = ioc->scsiio_depth; i && !found; i--) {
 567		scmd = ioc->scsi_lookup[i - 1].scmd;
 568		if (scmd == NULL || scmd->device == NULL ||
 569		    scmd->device->hostdata == NULL)
 570			continue;
 571		if (lun != scmd->device->lun)
 572			continue;
 573		priv_data = scmd->device->hostdata;
 574		if (priv_data->sas_target == NULL)
 575			continue;
 576		if (priv_data->sas_target->handle != handle)
 577			continue;
 578		tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
 579		found = 1;
 580	}
 581	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
 582
 583	if (!found) {
 584		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
 585		    "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
 586		    desc, le16_to_cpu(tm_request->DevHandle), lun));
 587		tm_reply = ioc->ctl_cmds.reply;
 588		tm_reply->DevHandle = tm_request->DevHandle;
 589		tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
 590		tm_reply->TaskType = tm_request->TaskType;
 591		tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
 592		tm_reply->VP_ID = tm_request->VP_ID;
 593		tm_reply->VF_ID = tm_request->VF_ID;
 594		sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
 595		if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
 596		    sz))
 597			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 598			    __LINE__, __func__);
 599		return 1;
 600	}
 601
 602	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
 603	    "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
 604	    desc, le16_to_cpu(tm_request->DevHandle), lun,
 605	     le16_to_cpu(tm_request->TaskMID)));
 606	return 0;
 607}
 608
 609/**
 610 * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
 611 * @ioc: per adapter object
 612 * @karg - (struct mpt2_ioctl_command)
 613 * @mf - pointer to mf in user space
 614 */
 615static long
 616_ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command karg,
 617	void __user *mf)
 618{
 619	MPI2RequestHeader_t *mpi_request = NULL, *request;
 620	MPI2DefaultReply_t *mpi_reply;
 621	u32 ioc_state;
 622	u16 ioc_status;
 623	u16 smid;
 624	unsigned long timeout, timeleft;
 625	u8 issue_reset;
 626	u32 sz;
 627	void *psge;
 628	void *data_out = NULL;
 629	dma_addr_t data_out_dma;
 630	size_t data_out_sz = 0;
 631	void *data_in = NULL;
 632	dma_addr_t data_in_dma;
 633	size_t data_in_sz = 0;
 634	u32 sgl_flags;
 635	long ret;
 636	u16 wait_state_count;
 637
 638	issue_reset = 0;
 639
 640	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
 641		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
 642		    ioc->name, __func__);
 643		ret = -EAGAIN;
 644		goto out;
 645	}
 646
 647	wait_state_count = 0;
 648	ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
 649	while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
 650		if (wait_state_count++ == 10) {
 651			printk(MPT2SAS_ERR_FMT
 652			    "%s: failed due to ioc not operational\n",
 653			    ioc->name, __func__);
 654			ret = -EFAULT;
 655			goto out;
 656		}
 657		ssleep(1);
 658		ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
 659		printk(MPT2SAS_INFO_FMT "%s: waiting for "
 660		    "operational state(count=%d)\n", ioc->name,
 661		    __func__, wait_state_count);
 662	}
 663	if (wait_state_count)
 664		printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
 665		    ioc->name, __func__);
 666
 667	mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
 668	if (!mpi_request) {
 669		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a memory for "
 670		    "mpi_request\n", ioc->name, __func__);
 671		ret = -ENOMEM;
 672		goto out;
 673	}
 674
 675	/* Check for overflow and wraparound */
 676	if (karg.data_sge_offset * 4 > ioc->request_sz ||
 677	    karg.data_sge_offset > (UINT_MAX / 4)) {
 678		ret = -EINVAL;
 679		goto out;
 680	}
 681
 682	/* copy in request message frame from user */
 683	if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
 684		printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
 685		    __func__);
 686		ret = -EFAULT;
 687		goto out;
 688	}
 689
 690	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
 691		smid = mpt2sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
 692		if (!smid) {
 693			printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
 694			    ioc->name, __func__);
 695			ret = -EAGAIN;
 696			goto out;
 697		}
 698	} else {
 699
 700		smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
 701		if (!smid) {
 702			printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
 703			    ioc->name, __func__);
 704			ret = -EAGAIN;
 705			goto out;
 706		}
 707	}
 708
 709	ret = 0;
 710	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
 711	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
 712	request = mpt2sas_base_get_msg_frame(ioc, smid);
 713	memcpy(request, mpi_request, karg.data_sge_offset*4);
 714	ioc->ctl_cmds.smid = smid;
 715	data_out_sz = karg.data_out_size;
 716	data_in_sz = karg.data_in_size;
 717
 718	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
 719	    mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
 720		if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
 721		    le16_to_cpu(mpi_request->FunctionDependent1) >
 722		    ioc->facts.MaxDevHandle) {
 723			ret = -EINVAL;
 724			mpt2sas_base_free_smid(ioc, smid);
 725			goto out;
 726		}
 727	}
 728
 729	/* obtain dma-able memory for data transfer */
 730	if (data_out_sz) /* WRITE */ {
 731		data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
 732		    &data_out_dma);
 733		if (!data_out) {
 734			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 735			    __LINE__, __func__);
 736			ret = -ENOMEM;
 737			mpt2sas_base_free_smid(ioc, smid);
 738			goto out;
 739		}
 740		if (copy_from_user(data_out, karg.data_out_buf_ptr,
 741			data_out_sz)) {
 742			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 743			    __LINE__, __func__);
 744			ret =  -EFAULT;
 745			mpt2sas_base_free_smid(ioc, smid);
 746			goto out;
 747		}
 748	}
 749
 750	if (data_in_sz) /* READ */ {
 751		data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
 752		    &data_in_dma);
 753		if (!data_in) {
 754			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 755			    __LINE__, __func__);
 756			ret = -ENOMEM;
 757			mpt2sas_base_free_smid(ioc, smid);
 758			goto out;
 759		}
 760	}
 761
 762	/* add scatter gather elements */
 763	psge = (void *)request + (karg.data_sge_offset*4);
 764
 765	if (!data_out_sz && !data_in_sz) {
 766		mpt2sas_base_build_zero_len_sge(ioc, psge);
 767	} else if (data_out_sz && data_in_sz) {
 768		/* WRITE sgel first */
 769		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
 770		    MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
 771		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
 772		ioc->base_add_sg_single(psge, sgl_flags |
 773		    data_out_sz, data_out_dma);
 774
 775		/* incr sgel */
 776		psge += ioc->sge_size;
 777
 778		/* READ sgel last */
 779		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
 780		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
 781		    MPI2_SGE_FLAGS_END_OF_LIST);
 782		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
 783		ioc->base_add_sg_single(psge, sgl_flags |
 784		    data_in_sz, data_in_dma);
 785	} else if (data_out_sz) /* WRITE */ {
 786		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
 787		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
 788		    MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
 789		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
 790		ioc->base_add_sg_single(psge, sgl_flags |
 791		    data_out_sz, data_out_dma);
 792	} else if (data_in_sz) /* READ */ {
 793		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
 794		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
 795		    MPI2_SGE_FLAGS_END_OF_LIST);
 796		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
 797		ioc->base_add_sg_single(psge, sgl_flags |
 798		    data_in_sz, data_in_dma);
 799	}
 800
 801	/* send command to firmware */
 802#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
 803	_ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
 804#endif
 805
 806	init_completion(&ioc->ctl_cmds.done);
 807	switch (mpi_request->Function) {
 808	case MPI2_FUNCTION_SCSI_IO_REQUEST:
 809	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
 810	{
 811		Mpi2SCSIIORequest_t *scsiio_request =
 812		    (Mpi2SCSIIORequest_t *)request;
 813		scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
 814		scsiio_request->SenseBufferLowAddress =
 815		    mpt2sas_base_get_sense_buffer_dma(ioc, smid);
 816		memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
 817		if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
 818			mpt2sas_base_put_smid_scsi_io(ioc, smid,
 819			    le16_to_cpu(mpi_request->FunctionDependent1));
 820		else
 821			mpt2sas_base_put_smid_default(ioc, smid);
 822		break;
 823	}
 824	case MPI2_FUNCTION_SCSI_TASK_MGMT:
 825	{
 826		Mpi2SCSITaskManagementRequest_t *tm_request =
 827		    (Mpi2SCSITaskManagementRequest_t *)request;
 828
 829		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
 830		    "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
 831		    le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
 832
 833		if (tm_request->TaskType ==
 834		    MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
 835		    tm_request->TaskType ==
 836		    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
 837			if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
 838				mpt2sas_base_free_smid(ioc, smid);
 839				goto out;
 840			}
 841		}
 842
 843		mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
 844		    tm_request->DevHandle));
 845		mpt2sas_base_put_smid_hi_priority(ioc, smid);
 846		break;
 847	}
 848	case MPI2_FUNCTION_SMP_PASSTHROUGH:
 849	{
 850		Mpi2SmpPassthroughRequest_t *smp_request =
 851		    (Mpi2SmpPassthroughRequest_t *)mpi_request;
 852		u8 *data;
 853
 854		/* ioc determines which port to use */
 855		smp_request->PhysicalPort = 0xFF;
 856		if (smp_request->PassthroughFlags &
 857		    MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
 858			data = (u8 *)&smp_request->SGL;
 859		else {
 860			if (unlikely(data_out == NULL)) {
 861				printk(KERN_ERR "failure at %s:%d/%s()!\n",
 862				    __FILE__, __LINE__, __func__);
 863				mpt2sas_base_free_smid(ioc, smid);
 864				ret = -EINVAL;
 865				goto out;
 866			}
 867			data = data_out;
 868		}
 869
 870		if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
 871			ioc->ioc_link_reset_in_progress = 1;
 872			ioc->ignore_loginfos = 1;
 873		}
 874		mpt2sas_base_put_smid_default(ioc, smid);
 875		break;
 876	}
 877	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
 878	{
 879		Mpi2SasIoUnitControlRequest_t *sasiounit_request =
 880		    (Mpi2SasIoUnitControlRequest_t *)mpi_request;
 881
 882		if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
 883		    || sasiounit_request->Operation ==
 884		    MPI2_SAS_OP_PHY_LINK_RESET) {
 885			ioc->ioc_link_reset_in_progress = 1;
 886			ioc->ignore_loginfos = 1;
 887		}
 888		mpt2sas_base_put_smid_default(ioc, smid);
 889		break;
 890	}
 891	default:
 892		mpt2sas_base_put_smid_default(ioc, smid);
 893		break;
 894	}
 895
 896	if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
 897		timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
 898	else
 899		timeout = karg.timeout;
 900	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
 901	    timeout*HZ);
 902	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
 903		Mpi2SCSITaskManagementRequest_t *tm_request =
 904		    (Mpi2SCSITaskManagementRequest_t *)mpi_request;
 905		mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
 906		    tm_request->DevHandle));
 907	} else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
 908	    mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
 909		ioc->ioc_link_reset_in_progress) {
 910		ioc->ioc_link_reset_in_progress = 0;
 911		ioc->ignore_loginfos = 0;
 912	}
 913	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
 914		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
 915		    __func__);
 916		_debug_dump_mf(mpi_request, karg.data_sge_offset);
 917		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
 918			issue_reset = 1;
 919		goto issue_host_reset;
 920	}
 921
 922	mpi_reply = ioc->ctl_cmds.reply;
 923	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
 924
 925#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
 926	if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
 927	    (ioc->logging_level & MPT_DEBUG_TM)) {
 928		Mpi2SCSITaskManagementReply_t *tm_reply =
 929		    (Mpi2SCSITaskManagementReply_t *)mpi_reply;
 930
 931		printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
 932		    "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
 933		    "TerminationCount(0x%08x)\n", ioc->name,
 934		    le16_to_cpu(tm_reply->IOCStatus),
 935		    le32_to_cpu(tm_reply->IOCLogInfo),
 936		    le32_to_cpu(tm_reply->TerminationCount));
 937	}
 938#endif
 939	/* copy out xdata to user */
 940	if (data_in_sz) {
 941		if (copy_to_user(karg.data_in_buf_ptr, data_in,
 942		    data_in_sz)) {
 943			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 944			    __LINE__, __func__);
 945			ret = -ENODATA;
 946			goto out;
 947		}
 948	}
 949
 950	/* copy out reply message frame to user */
 951	if (karg.max_reply_bytes) {
 952		sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
 953		if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
 954		    sz)) {
 955			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 956			    __LINE__, __func__);
 957			ret = -ENODATA;
 958			goto out;
 959		}
 960	}
 961
 962	/* copy out sense to user */
 963	if (karg.max_sense_bytes && (mpi_request->Function ==
 964	    MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
 965	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
 966		sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
 967		if (copy_to_user(karg.sense_data_ptr,
 968			ioc->ctl_cmds.sense, sz)) {
 969			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 970			    __LINE__, __func__);
 971			ret = -ENODATA;
 972			goto out;
 973		}
 974	}
 975
 976 issue_host_reset:
 977	if (issue_reset) {
 978		ret = -ENODATA;
 979		if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
 980		    mpi_request->Function ==
 981		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
 982		    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
 983			printk(MPT2SAS_INFO_FMT "issue target reset: handle "
 984			    "= (0x%04x)\n", ioc->name,
 985			    le16_to_cpu(mpi_request->FunctionDependent1));
 986			mpt2sas_halt_firmware(ioc);
 987			mpt2sas_scsih_issue_tm(ioc,
 988			    le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
 989			    0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
 990			    0, TM_MUTEX_ON);
 991			ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
 992		} else
 993			mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
 994			    FORCE_BIG_HAMMER);
 995	}
 996
 997 out:
 998
 999	/* free memory associated with sg buffers */
1000	if (data_in)
1001		pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1002		    data_in_dma);
1003
1004	if (data_out)
1005		pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1006		    data_out_dma);
1007
1008	kfree(mpi_request);
1009	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1010	return ret;
1011}
1012
1013/**
1014 * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
1015 * @ioc: per adapter object
1016 * @arg - user space buffer containing ioctl content
1017 */
1018static long
1019_ctl_getiocinfo(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1020{
1021	struct mpt2_ioctl_iocinfo karg;
1022
1023	if (copy_from_user(&karg, arg, sizeof(karg))) {
1024		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1025		    __FILE__, __LINE__, __func__);
1026		return -EFAULT;
1027	}
1028
1029	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1030	    __func__));
1031
1032	memset(&karg, 0 , sizeof(karg));
1033	if (ioc->is_warpdrive)
1034		karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1035	else
1036		karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1037	if (ioc->pfacts)
1038		karg.port_number = ioc->pfacts[0].PortNumber;
1039	karg.hw_rev = ioc->pdev->revision;
1040	karg.pci_id = ioc->pdev->device;
1041	karg.subsystem_device = ioc->pdev->subsystem_device;
1042	karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1043	karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1044	karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1045	karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1046	karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1047	karg.firmware_version = ioc->facts.FWVersion.Word;
1048	strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
1049	strcat(karg.driver_version, "-");
1050	strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1051	karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1052
1053	if (copy_to_user(arg, &karg, sizeof(karg))) {
1054		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1055		    __FILE__, __LINE__, __func__);
1056		return -EFAULT;
1057	}
1058	return 0;
1059}
1060
1061/**
1062 * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
1063 * @ioc: per adapter object
1064 * @arg - user space buffer containing ioctl content
1065 */
1066static long
1067_ctl_eventquery(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1068{
1069	struct mpt2_ioctl_eventquery karg;
1070
1071	if (copy_from_user(&karg, arg, sizeof(karg))) {
1072		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1073		    __FILE__, __LINE__, __func__);
1074		return -EFAULT;
1075	}
1076
1077	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1078	    __func__));
1079
1080	karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
1081	memcpy(karg.event_types, ioc->event_type,
1082	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1083
1084	if (copy_to_user(arg, &karg, sizeof(karg))) {
1085		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1086		    __FILE__, __LINE__, __func__);
1087		return -EFAULT;
1088	}
1089	return 0;
1090}
1091
1092/**
1093 * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1094 * @ioc: per adapter object
1095 * @arg - user space buffer containing ioctl content
1096 */
1097static long
1098_ctl_eventenable(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1099{
1100	struct mpt2_ioctl_eventenable karg;
1101
1102	if (copy_from_user(&karg, arg, sizeof(karg))) {
1103		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1104		    __FILE__, __LINE__, __func__);
1105		return -EFAULT;
1106	}
1107
1108	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1109	    __func__));
1110
1111	if (ioc->event_log)
1112		return 0;
1113	memcpy(ioc->event_type, karg.event_types,
1114	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1115	mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1116
1117	/* initialize event_log */
1118	ioc->event_context = 0;
1119	ioc->aen_event_read_flag = 0;
1120	ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1121	    sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1122	if (!ioc->event_log) {
1123		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1124		    __FILE__, __LINE__, __func__);
1125		return -ENOMEM;
1126	}
1127	return 0;
1128}
1129
1130/**
1131 * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1132 * @ioc: per adapter object
1133 * @arg - user space buffer containing ioctl content
1134 */
1135static long
1136_ctl_eventreport(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1137{
1138	struct mpt2_ioctl_eventreport karg;
1139	u32 number_bytes, max_events, max;
1140	struct mpt2_ioctl_eventreport __user *uarg = arg;
1141
1142	if (copy_from_user(&karg, arg, sizeof(karg))) {
1143		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1144		    __FILE__, __LINE__, __func__);
1145		return -EFAULT;
1146	}
1147
1148	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1149	    __func__));
1150
1151	number_bytes = karg.hdr.max_data_size -
1152	    sizeof(struct mpt2_ioctl_header);
1153	max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1154	max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1155
1156	/* If fewer than 1 event is requested, there must have
1157	 * been some type of error.
1158	 */
1159	if (!max || !ioc->event_log)
1160		return -ENODATA;
1161
1162	number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1163	if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1164		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1165		    __FILE__, __LINE__, __func__);
1166		return -EFAULT;
1167	}
1168
1169	/* reset flag so SIGIO can restart */
1170	ioc->aen_event_read_flag = 0;
1171	return 0;
1172}
1173
1174/**
1175 * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1176 * @ioc: per adapter object
1177 * @arg - user space buffer containing ioctl content
1178 */
1179static long
1180_ctl_do_reset(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1181{
1182	struct mpt2_ioctl_diag_reset karg;
1183	int retval;
1184
1185	if (copy_from_user(&karg, arg, sizeof(karg))) {
1186		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1187		    __FILE__, __LINE__, __func__);
1188		return -EFAULT;
1189	}
1190
1191	if (ioc->shost_recovery || ioc->pci_error_recovery ||
1192		ioc->is_driver_loading)
1193		return -EAGAIN;
1194	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1195	    __func__));
1196
1197	retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1198	    FORCE_BIG_HAMMER);
1199	printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1200	    ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1201	return 0;
1202}
1203
1204/**
1205 * _ctl_btdh_search_sas_device - searching for sas device
1206 * @ioc: per adapter object
1207 * @btdh: btdh ioctl payload
1208 */
1209static int
1210_ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1211    struct mpt2_ioctl_btdh_mapping *btdh)
1212{
1213	struct _sas_device *sas_device;
1214	unsigned long flags;
1215	int rc = 0;
1216
1217	if (list_empty(&ioc->sas_device_list))
1218		return rc;
1219
1220	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1221	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1222		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1223		    btdh->handle == sas_device->handle) {
1224			btdh->bus = sas_device->channel;
1225			btdh->id = sas_device->id;
1226			rc = 1;
1227			goto out;
1228		} else if (btdh->bus == sas_device->channel && btdh->id ==
1229		    sas_device->id && btdh->handle == 0xFFFF) {
1230			btdh->handle = sas_device->handle;
1231			rc = 1;
1232			goto out;
1233		}
1234	}
1235 out:
1236	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1237	return rc;
1238}
1239
1240/**
1241 * _ctl_btdh_search_raid_device - searching for raid device
1242 * @ioc: per adapter object
1243 * @btdh: btdh ioctl payload
1244 */
1245static int
1246_ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1247    struct mpt2_ioctl_btdh_mapping *btdh)
1248{
1249	struct _raid_device *raid_device;
1250	unsigned long flags;
1251	int rc = 0;
1252
1253	if (list_empty(&ioc->raid_device_list))
1254		return rc;
1255
1256	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1257	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1258		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1259		    btdh->handle == raid_device->handle) {
1260			btdh->bus = raid_device->channel;
1261			btdh->id = raid_device->id;
1262			rc = 1;
1263			goto out;
1264		} else if (btdh->bus == raid_device->channel && btdh->id ==
1265		    raid_device->id && btdh->handle == 0xFFFF) {
1266			btdh->handle = raid_device->handle;
1267			rc = 1;
1268			goto out;
1269		}
1270	}
1271 out:
1272	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1273	return rc;
1274}
1275
1276/**
1277 * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1278 * @ioc: per adapter object
1279 * @arg - user space buffer containing ioctl content
1280 */
1281static long
1282_ctl_btdh_mapping(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1283{
1284	struct mpt2_ioctl_btdh_mapping karg;
1285	int rc;
1286
1287	if (copy_from_user(&karg, arg, sizeof(karg))) {
1288		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1289		    __FILE__, __LINE__, __func__);
1290		return -EFAULT;
1291	}
1292
1293	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1294	    __func__));
1295
1296	rc = _ctl_btdh_search_sas_device(ioc, &karg);
1297	if (!rc)
1298		_ctl_btdh_search_raid_device(ioc, &karg);
1299
1300	if (copy_to_user(arg, &karg, sizeof(karg))) {
1301		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1302		    __FILE__, __LINE__, __func__);
1303		return -EFAULT;
1304	}
1305	return 0;
1306}
1307
1308/**
1309 * _ctl_diag_capability - return diag buffer capability
1310 * @ioc: per adapter object
1311 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1312 *
1313 * returns 1 when diag buffer support is enabled in firmware
1314 */
1315static u8
1316_ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1317{
1318	u8 rc = 0;
1319
1320	switch (buffer_type) {
1321	case MPI2_DIAG_BUF_TYPE_TRACE:
1322		if (ioc->facts.IOCCapabilities &
1323		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1324			rc = 1;
1325		break;
1326	case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1327		if (ioc->facts.IOCCapabilities &
1328		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1329			rc = 1;
1330		break;
1331	case MPI2_DIAG_BUF_TYPE_EXTENDED:
1332		if (ioc->facts.IOCCapabilities &
1333		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1334			rc = 1;
1335	}
1336
1337	return rc;
1338}
1339
1340/**
1341 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1342 * @ioc: per adapter object
1343 * @diag_register: the diag_register struct passed in from user space
1344 *
1345 */
1346static long
1347_ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
1348    struct mpt2_diag_register *diag_register)
1349{
1350	int rc, i;
1351	void *request_data = NULL;
1352	dma_addr_t request_data_dma;
1353	u32 request_data_sz = 0;
1354	Mpi2DiagBufferPostRequest_t *mpi_request;
1355	Mpi2DiagBufferPostReply_t *mpi_reply;
1356	u8 buffer_type;
1357	unsigned long timeleft;
1358	u16 smid;
1359	u16 ioc_status;
1360	u8 issue_reset = 0;
1361
1362	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1363	    __func__));
1364
1365	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1366		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1367		    ioc->name, __func__);
1368		rc = -EAGAIN;
1369		goto out;
1370	}
1371
1372	buffer_type = diag_register->buffer_type;
1373	if (!_ctl_diag_capability(ioc, buffer_type)) {
1374		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1375		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1376		return -EPERM;
1377	}
1378
1379	if (ioc->diag_buffer_status[buffer_type] &
1380	    MPT2_DIAG_BUFFER_IS_REGISTERED) {
1381		printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1382		    "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1383		    buffer_type);
1384		return -EINVAL;
1385	}
1386
1387	if (diag_register->requested_buffer_size % 4)  {
1388		printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1389		    "is not 4 byte aligned\n", ioc->name, __func__);
1390		return -EINVAL;
1391	}
1392
1393	smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1394	if (!smid) {
1395		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1396		    ioc->name, __func__);
1397		rc = -EAGAIN;
1398		goto out;
1399	}
1400
1401	rc = 0;
1402	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1403	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1404	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1405	ioc->ctl_cmds.smid = smid;
1406
1407	request_data = ioc->diag_buffer[buffer_type];
1408	request_data_sz = diag_register->requested_buffer_size;
1409	ioc->unique_id[buffer_type] = diag_register->unique_id;
1410	ioc->diag_buffer_status[buffer_type] = 0;
1411	memcpy(ioc->product_specific[buffer_type],
1412	    diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
1413	ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1414
1415	if (request_data) {
1416		request_data_dma = ioc->diag_buffer_dma[buffer_type];
1417		if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1418			pci_free_consistent(ioc->pdev,
1419			    ioc->diag_buffer_sz[buffer_type],
1420			    request_data, request_data_dma);
1421			request_data = NULL;
1422		}
1423	}
1424
1425	if (request_data == NULL) {
1426		ioc->diag_buffer_sz[buffer_type] = 0;
1427		ioc->diag_buffer_dma[buffer_type] = 0;
1428		request_data = pci_alloc_consistent(
1429			ioc->pdev, request_data_sz, &request_data_dma);
1430		if (request_data == NULL) {
1431			printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1432			    " for diag buffers, requested size(%d)\n",
1433			    ioc->name, __func__, request_data_sz);
1434			mpt2sas_base_free_smid(ioc, smid);
1435			return -ENOMEM;
1436		}
1437		ioc->diag_buffer[buffer_type] = request_data;
1438		ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1439		ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1440	}
1441
1442	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1443	mpi_request->BufferType = diag_register->buffer_type;
1444	mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1445	mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1446	mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1447	mpi_request->VF_ID = 0; /* TODO */
1448	mpi_request->VP_ID = 0;
1449
1450	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(0x%p), "
1451	    "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1452	    (unsigned long long)request_data_dma,
1453	    le32_to_cpu(mpi_request->BufferLength)));
1454
1455	for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1456		mpi_request->ProductSpecific[i] =
1457			cpu_to_le32(ioc->product_specific[buffer_type][i]);
1458
1459	init_completion(&ioc->ctl_cmds.done);
1460	mpt2sas_base_put_smid_default(ioc, smid);
1461	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1462	    MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1463
1464	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1465		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1466		    __func__);
1467		_debug_dump_mf(mpi_request,
1468		    sizeof(Mpi2DiagBufferPostRequest_t)/4);
1469		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1470			issue_reset = 1;
1471		goto issue_host_reset;
1472	}
1473
1474	/* process the completed Reply Message Frame */
1475	if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1476		printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1477		    ioc->name, __func__);
1478		rc = -EFAULT;
1479		goto out;
1480	}
1481
1482	mpi_reply = ioc->ctl_cmds.reply;
1483	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1484
1485	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1486		ioc->diag_buffer_status[buffer_type] |=
1487			MPT2_DIAG_BUFFER_IS_REGISTERED;
1488		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1489		    ioc->name, __func__));
1490	} else {
1491		printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1492		    "log_info(0x%08x)\n", ioc->name, __func__,
1493		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1494		rc = -EFAULT;
1495	}
1496
1497 issue_host_reset:
1498	if (issue_reset)
1499		mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1500		    FORCE_BIG_HAMMER);
1501
1502 out:
1503
1504	if (rc && request_data)
1505		pci_free_consistent(ioc->pdev, request_data_sz,
1506		    request_data, request_data_dma);
1507
1508	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1509	return rc;
1510}
1511
1512/**
1513 * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
1514 * @ioc: per adapter object
1515 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1516 *
1517 * This is called when command line option diag_buffer_enable is enabled
1518 * at driver load time.
1519 */
1520void
1521mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
1522{
1523	struct mpt2_diag_register diag_register;
1524
1525	memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
1526
1527	if (bits_to_register & 1) {
1528		printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
1529		    ioc->name);
1530		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1531		/* register for 1MB buffers  */
1532		diag_register.requested_buffer_size = (1024 * 1024);
1533		diag_register.unique_id = 0x7075900;
1534		_ctl_diag_register_2(ioc,  &diag_register);
1535	}
1536
1537	if (bits_to_register & 2) {
1538		printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
1539		    ioc->name);
1540		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1541		/* register for 2MB buffers  */
1542		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1543		diag_register.unique_id = 0x7075901;
1544		_ctl_diag_register_2(ioc,  &diag_register);
1545	}
1546
1547	if (bits_to_register & 4) {
1548		printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
1549		    ioc->name);
1550		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1551		/* register for 2MB buffers  */
1552		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1553		diag_register.unique_id = 0x7075901;
1554		_ctl_diag_register_2(ioc,  &diag_register);
1555	}
1556}
1557
1558/**
1559 * _ctl_diag_register - application register with driver
1560 * @ioc: per adapter object
1561 * @arg - user space buffer containing ioctl content
1562 *
1563 * This will allow the driver to setup any required buffers that will be
1564 * needed by firmware to communicate with the driver.
1565 */
1566static long
1567_ctl_diag_register(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1568{
1569	struct mpt2_diag_register karg;
1570	long rc;
1571
1572	if (copy_from_user(&karg, arg, sizeof(karg))) {
1573		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1574		    __FILE__, __LINE__, __func__);
1575		return -EFAULT;
1576	}
1577
1578	rc = _ctl_diag_register_2(ioc, &karg);
1579	return rc;
1580}
1581
1582/**
1583 * _ctl_diag_unregister - application unregister with driver
1584 * @ioc: per adapter object
1585 * @arg - user space buffer containing ioctl content
1586 *
1587 * This will allow the driver to cleanup any memory allocated for diag
1588 * messages and to free up any resources.
1589 */
1590static long
1591_ctl_diag_unregister(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1592{
1593	struct mpt2_diag_unregister karg;
1594	void *request_data;
1595	dma_addr_t request_data_dma;
1596	u32 request_data_sz;
1597	u8 buffer_type;
1598
1599	if (copy_from_user(&karg, arg, sizeof(karg))) {
1600		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1601		    __FILE__, __LINE__, __func__);
1602		return -EFAULT;
1603	}
1604
1605	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1606	    __func__));
1607
1608	buffer_type = karg.unique_id & 0x000000ff;
1609	if (!_ctl_diag_capability(ioc, buffer_type)) {
1610		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1611		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1612		return -EPERM;
1613	}
1614
1615	if ((ioc->diag_buffer_status[buffer_type] &
1616	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1617		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1618		    "registered\n", ioc->name, __func__, buffer_type);
1619		return -EINVAL;
1620	}
1621	if ((ioc->diag_buffer_status[buffer_type] &
1622	    MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1623		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1624		    "released\n", ioc->name, __func__, buffer_type);
1625		return -EINVAL;
1626	}
1627
1628	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1629		printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1630		    "registered\n", ioc->name, __func__, karg.unique_id);
1631		return -EINVAL;
1632	}
1633
1634	request_data = ioc->diag_buffer[buffer_type];
1635	if (!request_data) {
1636		printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1637		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1638		return -ENOMEM;
1639	}
1640
1641	request_data_sz = ioc->diag_buffer_sz[buffer_type];
1642	request_data_dma = ioc->diag_buffer_dma[buffer_type];
1643	pci_free_consistent(ioc->pdev, request_data_sz,
1644	    request_data, request_data_dma);
1645	ioc->diag_buffer[buffer_type] = NULL;
1646	ioc->diag_buffer_status[buffer_type] = 0;
1647	return 0;
1648}
1649
1650/**
1651 * _ctl_diag_query - query relevant info associated with diag buffers
1652 * @ioc: per adapter object
1653 * @arg - user space buffer containing ioctl content
1654 *
1655 * The application will send only buffer_type and unique_id.  Driver will
1656 * inspect unique_id first, if valid, fill in all the info.  If unique_id is
1657 * 0x00, the driver will return info specified by Buffer Type.
1658 */
1659static long
1660_ctl_diag_query(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1661{
1662	struct mpt2_diag_query karg;
1663	void *request_data;
1664	int i;
1665	u8 buffer_type;
1666
1667	if (copy_from_user(&karg, arg, sizeof(karg))) {
1668		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1669		    __FILE__, __LINE__, __func__);
1670		return -EFAULT;
1671	}
1672
1673	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1674	    __func__));
1675
1676	karg.application_flags = 0;
1677	buffer_type = karg.buffer_type;
1678
1679	if (!_ctl_diag_capability(ioc, buffer_type)) {
1680		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1681		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1682		return -EPERM;
1683	}
1684
1685	if ((ioc->diag_buffer_status[buffer_type] &
1686	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1687		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1688		    "registered\n", ioc->name, __func__, buffer_type);
1689		return -EINVAL;
1690	}
1691
1692	if (karg.unique_id & 0xffffff00) {
1693		if (karg.unique_id != ioc->unique_id[buffer_type]) {
1694			printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1695			    "registered\n", ioc->name, __func__,
1696			    karg.unique_id);
1697			return -EINVAL;
1698		}
1699	}
1700
1701	request_data = ioc->diag_buffer[buffer_type];
1702	if (!request_data) {
1703		printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1704		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1705		return -ENOMEM;
1706	}
1707
1708	if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1709		karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1710		    MPT2_APP_FLAGS_BUFFER_VALID);
1711	else
1712		karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1713		    MPT2_APP_FLAGS_BUFFER_VALID |
1714		    MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1715
1716	for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1717		karg.product_specific[i] =
1718		    ioc->product_specific[buffer_type][i];
1719
1720	karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1721	karg.driver_added_buffer_size = 0;
1722	karg.unique_id = ioc->unique_id[buffer_type];
1723	karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1724
1725	if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1726		printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1727		    "data @ %p\n", ioc->name, __func__, arg);
1728		return -EFAULT;
1729	}
1730	return 0;
1731}
1732
1733/**
1734 * _ctl_send_release - Diag Release Message
1735 * @ioc: per adapter object
1736 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1737 * @issue_reset - specifies whether host reset is required.
1738 *
1739 */
1740static int
1741_ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1742{
1743	Mpi2DiagReleaseRequest_t *mpi_request;
1744	Mpi2DiagReleaseReply_t *mpi_reply;
1745	u16 smid;
1746	u16 ioc_status;
1747	u32 ioc_state;
1748	int rc;
1749	unsigned long timeleft;
1750
1751	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1752	    __func__));
1753
1754	rc = 0;
1755	*issue_reset = 0;
1756
1757	ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1758	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1759		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
1760		    "skipping due to FAULT state\n", ioc->name,
1761		    __func__));
1762		rc = -EAGAIN;
1763		goto out;
1764	}
1765
1766	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1767		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1768		    ioc->name, __func__);
1769		rc = -EAGAIN;
1770		goto out;
1771	}
1772
1773	smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1774	if (!smid) {
1775		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1776		    ioc->name, __func__);
1777		rc = -EAGAIN;
1778		goto out;
1779	}
1780
1781	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1782	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1783	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1784	ioc->ctl_cmds.smid = smid;
1785
1786	mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1787	mpi_request->BufferType = buffer_type;
1788	mpi_request->VF_ID = 0; /* TODO */
1789	mpi_request->VP_ID = 0;
1790
1791	init_completion(&ioc->ctl_cmds.done);
1792	mpt2sas_base_put_smid_default(ioc, smid);
1793	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1794	    MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1795
1796	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1797		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1798		    __func__);
1799		_debug_dump_mf(mpi_request,
1800		    sizeof(Mpi2DiagReleaseRequest_t)/4);
1801		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1802			*issue_reset = 1;
1803		rc = -EFAULT;
1804		goto out;
1805	}
1806
1807	/* process the completed Reply Message Frame */
1808	if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1809		printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1810		    ioc->name, __func__);
1811		rc = -EFAULT;
1812		goto out;
1813	}
1814
1815	mpi_reply = ioc->ctl_cmds.reply;
1816	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1817
1818	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1819		ioc->diag_buffer_status[buffer_type] |=
1820		    MPT2_DIAG_BUFFER_IS_RELEASED;
1821		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1822		    ioc->name, __func__));
1823	} else {
1824		printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1825		    "log_info(0x%08x)\n", ioc->name, __func__,
1826		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1827		rc = -EFAULT;
1828	}
1829
1830 out:
1831	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1832	return rc;
1833}
1834
1835/**
1836 * _ctl_diag_release - request to send Diag Release Message to firmware
1837 * @arg - user space buffer containing ioctl content
1838 *
1839 * This allows ownership of the specified buffer to returned to the driver,
1840 * allowing an application to read the buffer without fear that firmware is
1841 * overwritting information in the buffer.
1842 */
1843static long
1844_ctl_diag_release(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1845{
1846	struct mpt2_diag_release karg;
1847	void *request_data;
1848	int rc;
1849	u8 buffer_type;
1850	u8 issue_reset = 0;
1851
1852	if (copy_from_user(&karg, arg, sizeof(karg))) {
1853		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1854		    __FILE__, __LINE__, __func__);
1855		return -EFAULT;
1856	}
1857
1858	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1859	    __func__));
1860
1861	buffer_type = karg.unique_id & 0x000000ff;
1862	if (!_ctl_diag_capability(ioc, buffer_type)) {
1863		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1864		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1865		return -EPERM;
1866	}
1867
1868	if ((ioc->diag_buffer_status[buffer_type] &
1869	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1870		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1871		    "registered\n", ioc->name, __func__, buffer_type);
1872		return -EINVAL;
1873	}
1874
1875	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1876		printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1877		    "registered\n", ioc->name, __func__, karg.unique_id);
1878		return -EINVAL;
1879	}
1880
1881	if (ioc->diag_buffer_status[buffer_type] &
1882	    MPT2_DIAG_BUFFER_IS_RELEASED) {
1883		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1884		    "is already released\n", ioc->name, __func__,
1885		    buffer_type);
1886		return 0;
1887	}
1888
1889	request_data = ioc->diag_buffer[buffer_type];
1890
1891	if (!request_data) {
1892		printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1893		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1894		return -ENOMEM;
1895	}
1896
1897	/* buffers were released by due to host reset */
1898	if ((ioc->diag_buffer_status[buffer_type] &
1899	    MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1900		ioc->diag_buffer_status[buffer_type] |=
1901		    MPT2_DIAG_BUFFER_IS_RELEASED;
1902		ioc->diag_buffer_status[buffer_type] &=
1903		    ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1904		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1905		    "was released due to host reset\n", ioc->name, __func__,
1906		    buffer_type);
1907		return 0;
1908	}
1909
1910	rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1911
1912	if (issue_reset)
1913		mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1914		    FORCE_BIG_HAMMER);
1915
1916	return rc;
1917}
1918
1919/**
1920 * _ctl_diag_read_buffer - request for copy of the diag buffer
1921 * @ioc: per adapter object
1922 * @arg - user space buffer containing ioctl content
1923 */
1924static long
1925_ctl_diag_read_buffer(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1926{
1927	struct mpt2_diag_read_buffer karg;
1928	struct mpt2_diag_read_buffer __user *uarg = arg;
1929	void *request_data, *diag_data;
1930	Mpi2DiagBufferPostRequest_t *mpi_request;
1931	Mpi2DiagBufferPostReply_t *mpi_reply;
1932	int rc, i;
1933	u8 buffer_type;
1934	unsigned long timeleft, request_size, copy_size;
1935	u16 smid;
1936	u16 ioc_status;
1937	u8 issue_reset = 0;
1938
1939	if (copy_from_user(&karg, arg, sizeof(karg))) {
1940		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1941		    __FILE__, __LINE__, __func__);
1942		return -EFAULT;
1943	}
1944
1945	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1946	    __func__));
1947
1948	buffer_type = karg.unique_id & 0x000000ff;
1949	if (!_ctl_diag_capability(ioc, buffer_type)) {
1950		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1951		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1952		return -EPERM;
1953	}
1954
1955	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1956		printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1957		    "registered\n", ioc->name, __func__, karg.unique_id);
1958		return -EINVAL;
1959	}
1960
1961	request_data = ioc->diag_buffer[buffer_type];
1962	if (!request_data) {
1963		printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1964		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1965		return -ENOMEM;
1966	}
1967
1968	request_size = ioc->diag_buffer_sz[buffer_type];
1969
1970	if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1971		printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1972		    "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1973		    __func__);
1974		return -EINVAL;
1975	}
1976
1977	if (karg.starting_offset > request_size)
1978		return -EINVAL;
1979
1980	diag_data = (void *)(request_data + karg.starting_offset);
1981	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
1982	    "offset(%d), sz(%d)\n", ioc->name, __func__,
1983	    diag_data, karg.starting_offset, karg.bytes_to_read));
1984
1985	/* Truncate data on requests that are too large */
1986	if ((diag_data + karg.bytes_to_read < diag_data) ||
1987	    (diag_data + karg.bytes_to_read > request_data + request_size))
1988		copy_size = request_size - karg.starting_offset;
1989	else
1990		copy_size = karg.bytes_to_read;
1991
1992	if (copy_to_user((void __user *)uarg->diagnostic_data,
1993	    diag_data, copy_size)) {
1994		printk(MPT2SAS_ERR_FMT "%s: Unable to write "
1995		    "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
1996		    __func__, diag_data);
1997		return -EFAULT;
1998	}
1999
2000	if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
2001		return 0;
2002
2003	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
2004		"buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
2005	if ((ioc->diag_buffer_status[buffer_type] &
2006	    MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
2007		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2008		    "buffer_type(0x%02x) is still registered\n", ioc->name,
2009		     __func__, buffer_type));
2010		return 0;
2011	}
2012	/* Get a free request frame and save the message context.
2013	*/
2014
2015	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
2016		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
2017		    ioc->name, __func__);
2018		rc = -EAGAIN;
2019		goto out;
2020	}
2021
2022	smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2023	if (!smid) {
2024		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2025		    ioc->name, __func__);
2026		rc = -EAGAIN;
2027		goto out;
2028	}
2029
2030	rc = 0;
2031	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
2032	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2033	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2034	ioc->ctl_cmds.smid = smid;
2035
2036	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2037	mpi_request->BufferType = buffer_type;
2038	mpi_request->BufferLength =
2039	    cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2040	mpi_request->BufferAddress =
2041	    cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2042	for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
2043		mpi_request->ProductSpecific[i] =
2044			cpu_to_le32(ioc->product_specific[buffer_type][i]);
2045	mpi_request->VF_ID = 0; /* TODO */
2046	mpi_request->VP_ID = 0;
2047
2048	init_completion(&ioc->ctl_cmds.done);
2049	mpt2sas_base_put_smid_default(ioc, smid);
2050	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2051	    MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
2052
2053	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
2054		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
2055		    __func__);
2056		_debug_dump_mf(mpi_request,
2057		    sizeof(Mpi2DiagBufferPostRequest_t)/4);
2058		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
2059			issue_reset = 1;
2060		goto issue_host_reset;
2061	}
2062
2063	/* process the completed Reply Message Frame */
2064	if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
2065		printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
2066		    ioc->name, __func__);
2067		rc = -EFAULT;
2068		goto out;
2069	}
2070
2071	mpi_reply = ioc->ctl_cmds.reply;
2072	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2073
2074	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2075		ioc->diag_buffer_status[buffer_type] |=
2076		    MPT2_DIAG_BUFFER_IS_REGISTERED;
2077		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
2078		    ioc->name, __func__));
2079	} else {
2080		printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
2081		    "log_info(0x%08x)\n", ioc->name, __func__,
2082		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2083		rc = -EFAULT;
2084	}
2085
2086 issue_host_reset:
2087	if (issue_reset)
2088		mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2089		    FORCE_BIG_HAMMER);
2090
2091 out:
2092
2093	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
2094	return rc;
2095}
2096
2097
2098#ifdef CONFIG_COMPAT
2099/**
2100 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2101 * @ioc: per adapter object
2102 * @cmd - ioctl opcode
2103 * @arg - (struct mpt2_ioctl_command32)
2104 *
2105 * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2106 */
2107static long
2108_ctl_compat_mpt_command(struct MPT2SAS_ADAPTER *ioc, unsigned cmd,
2109	void __user *arg)
2110{
2111	struct mpt2_ioctl_command32 karg32;
2112	struct mpt2_ioctl_command32 __user *uarg;
2113	struct mpt2_ioctl_command karg;
2114
2115	if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2116		return -EINVAL;
2117
2118	uarg = (struct mpt2_ioctl_command32 __user *) arg;
2119
2120	if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2121		printk(KERN_ERR "failure at %s:%d/%s()!\n",
2122		    __FILE__, __LINE__, __func__);
2123		return -EFAULT;
2124	}
2125
2126	memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2127	karg.hdr.ioc_number = karg32.hdr.ioc_number;
2128	karg.hdr.port_number = karg32.hdr.port_number;
2129	karg.hdr.max_data_size = karg32.hdr.max_data_size;
2130	karg.timeout = karg32.timeout;
2131	karg.max_reply_bytes = karg32.max_reply_bytes;
2132	karg.data_in_size = karg32.data_in_size;
2133	karg.data_out_size = karg32.data_out_size;
2134	karg.max_sense_bytes = karg32.max_sense_bytes;
2135	karg.data_sge_offset = karg32.data_sge_offset;
2136	karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2137	karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2138	karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2139	karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2140	return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2141}
2142#endif
2143
2144/**
2145 * _ctl_ioctl_main - main ioctl entry point
2146 * @file - (struct file)
2147 * @cmd - ioctl opcode
2148 * @arg -
2149 * compat - handles 32 bit applications in 64bit os
2150 */
2151static long
2152_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2153	u8 compat)
2154{
2155	struct MPT2SAS_ADAPTER *ioc;
2156	struct mpt2_ioctl_header ioctl_header;
2157	enum block_state state;
2158	long ret = -EINVAL;
2159
2160	/* get IOCTL header */
2161	if (copy_from_user(&ioctl_header, (char __user *)arg,
2162	    sizeof(struct mpt2_ioctl_header))) {
2163		printk(KERN_ERR "failure at %s:%d/%s()!\n",
2164		    __FILE__, __LINE__, __func__);
2165		return -EFAULT;
2166	}
2167
2168	if (_ctl_verify_adapter(ioctl_header.ioc_number, &ioc) == -1 || !ioc)
2169		return -ENODEV;
2170	if (ioc->shost_recovery || ioc->pci_error_recovery ||
2171	    ioc->is_driver_loading)
2172		return -EAGAIN;
2173
2174	state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2175	if (state == NON_BLOCKING) {
2176		if (!mutex_trylock(&ioc->ctl_cmds.mutex))
2177			return -EAGAIN;
2178	} else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2179		return -ERESTARTSYS;
2180	}
2181
2182	switch (cmd) {
2183	case MPT2IOCINFO:
2184		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
2185			ret = _ctl_getiocinfo(ioc, arg);
2186		break;
2187#ifdef CONFIG_COMPAT
2188	case MPT2COMMAND32:
2189#endif
2190	case MPT2COMMAND:
2191	{
2192		struct mpt2_ioctl_command __user *uarg;
2193		struct mpt2_ioctl_command karg;
2194#ifdef CONFIG_COMPAT
2195		if (compat) {
2196			ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2197			break;
2198		}
2199#endif
2200		if (copy_from_user(&karg, arg, sizeof(karg))) {
2201			printk(KERN_ERR "failure at %s:%d/%s()!\n",
2202			    __FILE__, __LINE__, __func__);
2203			ret = -EFAULT;
2204			break;
2205		}
2206
2207		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2208			uarg = arg;
2209			ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2210		}
2211		break;
2212	}
2213	case MPT2EVENTQUERY:
2214		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2215			ret = _ctl_eventquery(ioc, arg);
2216		break;
2217	case MPT2EVENTENABLE:
2218		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2219			ret = _ctl_eventenable(ioc, arg);
2220		break;
2221	case MPT2EVENTREPORT:
2222		ret = _ctl_eventreport(ioc, arg);
2223		break;
2224	case MPT2HARDRESET:
2225		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2226			ret = _ctl_do_reset(ioc, arg);
2227		break;
2228	case MPT2BTDHMAPPING:
2229		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2230			ret = _ctl_btdh_mapping(ioc, arg);
2231		break;
2232	case MPT2DIAGREGISTER:
2233		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2234			ret = _ctl_diag_register(ioc, arg);
2235		break;
2236	case MPT2DIAGUNREGISTER:
2237		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2238			ret = _ctl_diag_unregister(ioc, arg);
2239		break;
2240	case MPT2DIAGQUERY:
2241		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2242			ret = _ctl_diag_query(ioc, arg);
2243		break;
2244	case MPT2DIAGRELEASE:
2245		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2246			ret = _ctl_diag_release(ioc, arg);
2247		break;
2248	case MPT2DIAGREADBUFFER:
2249		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2250			ret = _ctl_diag_read_buffer(ioc, arg);
2251		break;
2252	default:
2253
2254		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
2255		    "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2256		break;
2257	}
2258
2259	mutex_unlock(&ioc->ctl_cmds.mutex);
2260	return ret;
2261}
2262
2263/**
2264 * _ctl_ioctl - main ioctl entry point (unlocked)
2265 * @file - (struct file)
2266 * @cmd - ioctl opcode
2267 * @arg -
2268 */
2269static long
2270_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2271{
2272	long ret;
2273
2274	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0);
2275	return ret;
2276}
2277#ifdef CONFIG_COMPAT
2278/**
2279 * _ctl_ioctl_compat - main ioctl entry point (compat)
2280 * @file -
2281 * @cmd -
2282 * @arg -
2283 *
2284 * This routine handles 32 bit applications in 64bit os.
2285 */
2286static long
2287_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2288{
2289	long ret;
2290
2291	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1);
2292	return ret;
2293}
2294#endif
2295
2296/* scsi host attributes */
2297
2298/**
2299 * _ctl_version_fw_show - firmware version
2300 * @cdev - pointer to embedded class device
2301 * @buf - the buffer returned
2302 *
2303 * A sysfs 'read-only' shost attribute.
2304 */
2305static ssize_t
2306_ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2307    char *buf)
2308{
2309	struct Scsi_Host *shost = class_to_shost(cdev);
2310	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2311
2312	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2313	    (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2314	    (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2315	    (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2316	    ioc->facts.FWVersion.Word & 0x000000FF);
2317}
2318static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2319
2320/**
2321 * _ctl_version_bios_show - bios version
2322 * @cdev - pointer to embedded class device
2323 * @buf - the buffer returned
2324 *
2325 * A sysfs 'read-only' shost attribute.
2326 */
2327static ssize_t
2328_ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2329    char *buf)
2330{
2331	struct Scsi_Host *shost = class_to_shost(cdev);
2332	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2333
2334	u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2335
2336	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2337	    (version & 0xFF000000) >> 24,
2338	    (version & 0x00FF0000) >> 16,
2339	    (version & 0x0000FF00) >> 8,
2340	    version & 0x000000FF);
2341}
2342static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2343
2344/**
2345 * _ctl_version_mpi_show - MPI (message passing interface) version
2346 * @cdev - pointer to embedded class device
2347 * @buf - the buffer returned
2348 *
2349 * A sysfs 'read-only' shost attribute.
2350 */
2351static ssize_t
2352_ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2353    char *buf)
2354{
2355	struct Scsi_Host *shost = class_to_shost(cdev);
2356	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2357
2358	return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2359	    ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2360}
2361static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2362
2363/**
2364 * _ctl_version_product_show - product name
2365 * @cdev - pointer to embedded class device
2366 * @buf - the buffer returned
2367 *
2368 * A sysfs 'read-only' shost attribute.
2369 */
2370static ssize_t
2371_ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2372    char *buf)
2373{
2374	struct Scsi_Host *shost = class_to_shost(cdev);
2375	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2376
2377	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2378}
2379static DEVICE_ATTR(version_product, S_IRUGO,
2380   _ctl_version_product_show, NULL);
2381
2382/**
2383 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2384 * @cdev - pointer to embedded class device
2385 * @buf - the buffer returned
2386 *
2387 * A sysfs 'read-only' shost attribute.
2388 */
2389static ssize_t
2390_ctl_version_nvdata_persistent_show(struct device *cdev,
2391    struct device_attribute *attr, char *buf)
2392{
2393	struct Scsi_Host *shost = class_to_shost(cdev);
2394	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2395
2396	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2397	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2398}
2399static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2400    _ctl_version_nvdata_persistent_show, NULL);
2401
2402/**
2403 * _ctl_version_nvdata_default_show - nvdata default version
2404 * @cdev - pointer to embedded class device
2405 * @buf - the buffer returned
2406 *
2407 * A sysfs 'read-only' shost attribute.
2408 */
2409static ssize_t
2410_ctl_version_nvdata_default_show(struct device *cdev,
2411    struct device_attribute *attr, char *buf)
2412{
2413	struct Scsi_Host *shost = class_to_shost(cdev);
2414	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2415
2416	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2417	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2418}
2419static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2420    _ctl_version_nvdata_default_show, NULL);
2421
2422/**
2423 * _ctl_board_name_show - board name
2424 * @cdev - pointer to embedded class device
2425 * @buf - the buffer returned
2426 *
2427 * A sysfs 'read-only' shost attribute.
2428 */
2429static ssize_t
2430_ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2431    char *buf)
2432{
2433	struct Scsi_Host *shost = class_to_shost(cdev);
2434	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2435
2436	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2437}
2438static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2439
2440/**
2441 * _ctl_board_assembly_show - board assembly name
2442 * @cdev - pointer to embedded class device
2443 * @buf - the buffer returned
2444 *
2445 * A sysfs 'read-only' shost attribute.
2446 */
2447static ssize_t
2448_ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2449    char *buf)
2450{
2451	struct Scsi_Host *shost = class_to_shost(cdev);
2452	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2453
2454	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2455}
2456static DEVICE_ATTR(board_assembly, S_IRUGO,
2457    _ctl_board_assembly_show, NULL);
2458
2459/**
2460 * _ctl_board_tracer_show - board tracer number
2461 * @cdev - pointer to embedded class device
2462 * @buf - the buffer returned
2463 *
2464 * A sysfs 'read-only' shost attribute.
2465 */
2466static ssize_t
2467_ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2468    char *buf)
2469{
2470	struct Scsi_Host *shost = class_to_shost(cdev);
2471	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2472
2473	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2474}
2475static DEVICE_ATTR(board_tracer, S_IRUGO,
2476    _ctl_board_tracer_show, NULL);
2477
2478/**
2479 * _ctl_io_delay_show - io missing delay
2480 * @cdev - pointer to embedded class device
2481 * @buf - the buffer returned
2482 *
2483 * This is for firmware implemention for deboucing device
2484 * removal events.
2485 *
2486 * A sysfs 'read-only' shost attribute.
2487 */
2488static ssize_t
2489_ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2490    char *buf)
2491{
2492	struct Scsi_Host *shost = class_to_shost(cdev);
2493	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2494
2495	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2496}
2497static DEVICE_ATTR(io_delay, S_IRUGO,
2498    _ctl_io_delay_show, NULL);
2499
2500/**
2501 * _ctl_device_delay_show - device missing delay
2502 * @cdev - pointer to embedded class device
2503 * @buf - the buffer returned
2504 *
2505 * This is for firmware implemention for deboucing device
2506 * removal events.
2507 *
2508 * A sysfs 'read-only' shost attribute.
2509 */
2510static ssize_t
2511_ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2512    char *buf)
2513{
2514	struct Scsi_Host *shost = class_to_shost(cdev);
2515	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2516
2517	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2518}
2519static DEVICE_ATTR(device_delay, S_IRUGO,
2520    _ctl_device_delay_show, NULL);
2521
2522/**
2523 * _ctl_fw_queue_depth_show - global credits
2524 * @cdev - pointer to embedded class device
2525 * @buf - the buffer returned
2526 *
2527 * This is firmware queue depth limit
2528 *
2529 * A sysfs 'read-only' shost attribute.
2530 */
2531static ssize_t
2532_ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2533    char *buf)
2534{
2535	struct Scsi_Host *shost = class_to_shost(cdev);
2536	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2537
2538	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2539}
2540static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2541    _ctl_fw_queue_depth_show, NULL);
2542
2543/**
2544 * _ctl_sas_address_show - sas address
2545 * @cdev - pointer to embedded class device
2546 * @buf - the buffer returned
2547 *
2548 * This is the controller sas address
2549 *
2550 * A sysfs 'read-only' shost attribute.
2551 */
2552static ssize_t
2553_ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2554    char *buf)
2555{
2556	struct Scsi_Host *shost = class_to_shost(cdev);
2557	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2558
2559	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2560	    (unsigned long long)ioc->sas_hba.sas_address);
2561}
2562static DEVICE_ATTR(host_sas_address, S_IRUGO,
2563    _ctl_host_sas_address_show, NULL);
2564
2565/**
2566 * _ctl_logging_level_show - logging level
2567 * @cdev - pointer to embedded class device
2568 * @buf - the buffer returned
2569 *
2570 * A sysfs 'read/write' shost attribute.
2571 */
2572static ssize_t
2573_ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2574    char *buf)
2575{
2576	struct Scsi_Host *shost = class_to_shost(cdev);
2577	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2578
2579	return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2580}
2581static ssize_t
2582_ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2583    const char *buf, size_t count)
2584{
2585	struct Scsi_Host *shost = class_to_shost(cdev);
2586	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2587	int val = 0;
2588
2589	if (sscanf(buf, "%x", &val) != 1)
2590		return -EINVAL;
2591
2592	ioc->logging_level = val;
2593	printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2594	    ioc->logging_level);
2595	return strlen(buf);
2596}
2597static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2598    _ctl_logging_level_show, _ctl_logging_level_store);
2599
2600/* device attributes */
2601/*
2602 * _ctl_fwfault_debug_show - show/store fwfault_debug
2603 * @cdev - pointer to embedded class device
2604 * @buf - the buffer returned
2605 *
2606 * mpt2sas_fwfault_debug is command line option
2607 * A sysfs 'read/write' shost attribute.
2608 */
2609static ssize_t
2610_ctl_fwfault_debug_show(struct device *cdev,
2611    struct device_attribute *attr, char *buf)
2612{
2613	struct Scsi_Host *shost = class_to_shost(cdev);
2614	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2615
2616	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2617}
2618static ssize_t
2619_ctl_fwfault_debug_store(struct device *cdev,
2620    struct device_attribute *attr, const char *buf, size_t count)
2621{
2622	struct Scsi_Host *shost = class_to_shost(cdev);
2623	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2624	int val = 0;
2625
2626	if (sscanf(buf, "%d", &val) != 1)
2627		return -EINVAL;
2628
2629	ioc->fwfault_debug = val;
2630	printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
2631	    ioc->fwfault_debug);
2632	return strlen(buf);
2633}
2634static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2635    _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2636
2637
2638/**
2639 * _ctl_ioc_reset_count_show - ioc reset count
2640 * @cdev - pointer to embedded class device
2641 * @buf - the buffer returned
2642 *
2643 * This is firmware queue depth limit
2644 *
2645 * A sysfs 'read-only' shost attribute.
2646 */
2647static ssize_t
2648_ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2649    char *buf)
2650{
2651	struct Scsi_Host *shost = class_to_shost(cdev);
2652	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2653
2654	return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
2655}
2656static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
2657    _ctl_ioc_reset_count_show, NULL);
2658
2659/**
2660 * _ctl_ioc_reply_queue_count_show - number of reply queues
2661 * @cdev - pointer to embedded class device
2662 * @buf - the buffer returned
2663 *
2664 * This is number of reply queues
2665 *
2666 * A sysfs 'read-only' shost attribute.
2667 */
2668static ssize_t
2669_ctl_ioc_reply_queue_count_show(struct device *cdev,
2670	 struct device_attribute *attr, char *buf)
2671{
2672	u8 reply_queue_count;
2673	struct Scsi_Host *shost = class_to_shost(cdev);
2674	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2675
2676	if ((ioc->facts.IOCCapabilities &
2677	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2678		reply_queue_count = ioc->reply_queue_count;
2679	else
2680		reply_queue_count = 1;
2681	return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2682}
2683static DEVICE_ATTR(reply_queue_count, S_IRUGO,
2684	 _ctl_ioc_reply_queue_count_show, NULL);
2685
2686/**
2687 * _ctl_BRM_status_show - Backup Rail Monitor Status
2688 * @cdev - pointer to embedded class device
2689 * @buf - the buffer returned
2690 *
2691 * This is number of reply queues
2692 *
2693 * A sysfs 'read-only' shost attribute.
2694 */
2695static ssize_t
2696_ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
2697	char *buf)
2698{
2699	struct Scsi_Host *shost = class_to_shost(cdev);
2700	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2701	Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2702	Mpi2ConfigReply_t mpi_reply;
2703	u16 backup_rail_monitor_status = 0;
2704	u16 ioc_status;
2705	int sz;
2706	ssize_t rc = 0;
2707
2708	if (!ioc->is_warpdrive) {
2709		printk(MPT2SAS_ERR_FMT "%s: BRM attribute is only for"\
2710		    "warpdrive\n", ioc->name, __func__);
2711		goto out;
2712	}
2713
2714	/* allocate upto GPIOVal 36 entries */
2715	sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2716	io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2717	if (!io_unit_pg3) {
2718		printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"\
2719		    "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
2720		goto out;
2721	}
2722
2723	if (mpt2sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2724	    0) {
2725		printk(MPT2SAS_ERR_FMT
2726		    "%s: failed reading iounit_pg3\n", ioc->name,
2727		    __func__);
2728		goto out;
2729	}
2730
2731	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2732	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2733		printk(MPT2SAS_ERR_FMT "%s: iounit_pg3 failed with"\
2734		    "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
2735		goto out;
2736	}
2737
2738	if (io_unit_pg3->GPIOCount < 25) {
2739		printk(MPT2SAS_ERR_FMT "%s: iounit_pg3->GPIOCount less than"\
2740		     "25 entries, detected (%d) entries\n", ioc->name, __func__,
2741		    io_unit_pg3->GPIOCount);
2742		goto out;
2743	}
2744
2745	/* BRM status is in bit zero of GPIOVal[24] */
2746	backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2747	rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2748
2749 out:
2750	kfree(io_unit_pg3);
2751	return rc;
2752}
2753static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
2754
2755struct DIAG_BUFFER_START {
2756	__le32 Size;
2757	__le32 DiagVersion;
2758	u8 BufferType;
2759	u8 Reserved[3];
2760	__le32 Reserved1;
2761	__le32 Reserved2;
2762	__le32 Reserved3;
2763};
2764/**
2765 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2766 * @cdev - pointer to embedded class device
2767 * @buf - the buffer returned
2768 *
2769 * A sysfs 'read-only' shost attribute.
2770 */
2771static ssize_t
2772_ctl_host_trace_buffer_size_show(struct device *cdev,
2773    struct device_attribute *attr, char *buf)
2774{
2775	struct Scsi_Host *shost = class_to_shost(cdev);
2776	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2777	u32 size = 0;
2778	struct DIAG_BUFFER_START *request_data;
2779
2780	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2781		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2782		    "registered\n", ioc->name, __func__);
2783		return 0;
2784	}
2785
2786	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2787	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2788		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2789		    "registered\n", ioc->name, __func__);
2790		return 0;
2791	}
2792
2793	request_data = (struct DIAG_BUFFER_START *)
2794	    ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2795	if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2796	    le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
2797	    le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2798		size = le32_to_cpu(request_data->Size);
2799
2800	ioc->ring_buffer_sz = size;
2801	return snprintf(buf, PAGE_SIZE, "%d\n", size);
2802}
2803static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2804	 _ctl_host_trace_buffer_size_show, NULL);
2805
2806/**
2807 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2808 * @cdev - pointer to embedded class device
2809 * @buf - the buffer returned
2810 *
2811 * A sysfs 'read/write' shost attribute.
2812 *
2813 * You will only be able to read 4k bytes of ring buffer at a time.
2814 * In order to read beyond 4k bytes, you will have to write out the
2815 * offset to the same attribute, it will move the pointer.
2816 */
2817static ssize_t
2818_ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2819     char *buf)
2820{
2821	struct Scsi_Host *shost = class_to_shost(cdev);
2822	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2823	void *request_data;
2824	u32 size;
2825
2826	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2827		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2828		    "registered\n", ioc->name, __func__);
2829		return 0;
2830	}
2831
2832	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2833	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2834		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2835		    "registered\n", ioc->name, __func__);
2836		return 0;
2837	}
2838
2839	if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2840		return 0;
2841
2842	size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2843	size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
2844	request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2845	memcpy(buf, request_data, size);
2846	return size;
2847}
2848
2849static ssize_t
2850_ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2851    const char *buf, size_t count)
2852{
2853	struct Scsi_Host *shost = class_to_shost(cdev);
2854	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2855	int val = 0;
2856
2857	if (sscanf(buf, "%d", &val) != 1)
2858		return -EINVAL;
2859
2860	ioc->ring_buffer_offset = val;
2861	return strlen(buf);
2862}
2863static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2864    _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2865
2866/*****************************************/
2867
2868/**
2869 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2870 * @cdev - pointer to embedded class device
2871 * @buf - the buffer returned
2872 *
2873 * A sysfs 'read/write' shost attribute.
2874 *
2875 * This is a mechnism to post/release host_trace_buffers
2876 */
2877static ssize_t
2878_ctl_host_trace_buffer_enable_show(struct device *cdev,
2879    struct device_attribute *attr, char *buf)
2880{
2881	struct Scsi_Host *shost = class_to_shost(cdev);
2882	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2883
2884	if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
2885	   ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2886	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
2887		return snprintf(buf, PAGE_SIZE, "off\n");
2888	else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2889	    MPT2_DIAG_BUFFER_IS_RELEASED))
2890		return snprintf(buf, PAGE_SIZE, "release\n");
2891	else
2892		return snprintf(buf, PAGE_SIZE, "post\n");
2893}
2894
2895static ssize_t
2896_ctl_host_trace_buffer_enable_store(struct device *cdev,
2897    struct device_attribute *attr, const char *buf, size_t count)
2898{
2899	struct Scsi_Host *shost = class_to_shost(cdev);
2900	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2901	char str[10] = "";
2902	struct mpt2_diag_register diag_register;
2903	u8 issue_reset = 0;
2904
2905	if (sscanf(buf, "%9s", str) != 1)
2906		return -EINVAL;
2907
2908	if (!strcmp(str, "post")) {
2909		/* exit out if host buffers are already posted */
2910		if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
2911		    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2912		    MPT2_DIAG_BUFFER_IS_REGISTERED) &&
2913		    ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2914		    MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
2915			goto out;
2916		memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
2917		printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
2918		    ioc->name);
2919		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
2920		diag_register.requested_buffer_size = (1024 * 1024);
2921		diag_register.unique_id = 0x7075900;
2922		ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
2923		_ctl_diag_register_2(ioc,  &diag_register);
2924	} else if (!strcmp(str, "release")) {
2925		/* exit out if host buffers are already released */
2926		if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
2927			goto out;
2928		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2929		    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
2930			goto out;
2931		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2932		    MPT2_DIAG_BUFFER_IS_RELEASED))
2933			goto out;
2934		printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
2935		    ioc->name);
2936		_ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
2937	}
2938
2939 out:
2940	return strlen(buf);
2941}
2942static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
2943    _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
2944
2945struct device_attribute *mpt2sas_host_attrs[] = {
2946	&dev_attr_version_fw,
2947	&dev_attr_version_bios,
2948	&dev_attr_version_mpi,
2949	&dev_attr_version_product,
2950	&dev_attr_version_nvdata_persistent,
2951	&dev_attr_version_nvdata_default,
2952	&dev_attr_board_name,
2953	&dev_attr_board_assembly,
2954	&dev_attr_board_tracer,
2955	&dev_attr_io_delay,
2956	&dev_attr_device_delay,
2957	&dev_attr_logging_level,
2958	&dev_attr_fwfault_debug,
2959	&dev_attr_fw_queue_depth,
2960	&dev_attr_host_sas_address,
2961	&dev_attr_ioc_reset_count,
2962	&dev_attr_host_trace_buffer_size,
2963	&dev_attr_host_trace_buffer,
2964	&dev_attr_host_trace_buffer_enable,
2965	&dev_attr_reply_queue_count,
2966	&dev_attr_BRM_status,
2967	NULL,
2968};
2969
2970/**
2971 * _ctl_device_sas_address_show - sas address
2972 * @cdev - pointer to embedded class device
2973 * @buf - the buffer returned
2974 *
2975 * This is the sas address for the target
2976 *
2977 * A sysfs 'read-only' shost attribute.
2978 */
2979static ssize_t
2980_ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2981    char *buf)
2982{
2983	struct scsi_device *sdev = to_scsi_device(dev);
2984	struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2985
2986	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2987	    (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2988}
2989static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2990
2991/**
2992 * _ctl_device_handle_show - device handle
2993 * @cdev - pointer to embedded class device
2994 * @buf - the buffer returned
2995 *
2996 * This is the firmware assigned device handle
2997 *
2998 * A sysfs 'read-only' shost attribute.
2999 */
3000static ssize_t
3001_ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
3002    char *buf)
3003{
3004	struct scsi_device *sdev = to_scsi_device(dev);
3005	struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3006
3007	return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3008	    sas_device_priv_data->sas_target->handle);
3009}
3010static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
3011
3012struct device_attribute *mpt2sas_dev_attrs[] = {
3013	&dev_attr_sas_address,
3014	&dev_attr_sas_device_handle,
3015	NULL,
3016};
3017
3018static const struct file_operations ctl_fops = {
3019	.owner = THIS_MODULE,
3020	.unlocked_ioctl = _ctl_ioctl,
 
3021	.poll = _ctl_poll,
3022	.fasync = _ctl_fasync,
3023#ifdef CONFIG_COMPAT
3024	.compat_ioctl = _ctl_ioctl_compat,
3025#endif
3026	.llseek = noop_llseek,
3027};
3028
3029static struct miscdevice ctl_dev = {
3030	.minor  = MPT2SAS_MINOR,
3031	.name   = MPT2SAS_DEV_NAME,
3032	.fops   = &ctl_fops,
3033};
3034
3035/**
3036 * mpt2sas_ctl_init - main entry point for ctl.
3037 *
3038 */
3039void
3040mpt2sas_ctl_init(void)
3041{
3042	async_queue = NULL;
3043	if (misc_register(&ctl_dev) < 0)
3044		printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
3045		    MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3046
3047	init_waitqueue_head(&ctl_poll_wait);
3048}
3049
3050/**
3051 * mpt2sas_ctl_exit - exit point for ctl
3052 *
3053 */
3054void
3055mpt2sas_ctl_exit(void)
3056{
3057	struct MPT2SAS_ADAPTER *ioc;
3058	int i;
3059
3060	list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
3061
3062		/* free memory associated to diag buffers */
3063		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3064			if (!ioc->diag_buffer[i])
3065				continue;
3066			pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3067			    ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3068			ioc->diag_buffer[i] = NULL;
3069			ioc->diag_buffer_status[i] = 0;
3070		}
3071
3072		kfree(ioc->event_log);
3073	}
3074	misc_deregister(&ctl_dev);
3075}
3076
v3.5.6
   1/*
   2 * Management Module Support for MPT (Message Passing Technology) based
   3 * controllers
   4 *
   5 * This code is based on drivers/scsi/mpt2sas/mpt2_ctl.c
   6 * Copyright (C) 2007-2010  LSI Corporation
   7 *  (mailto:DL-MPTFusionLinux@lsi.com)
   8 *
   9 * This program is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU General Public License
  11 * as published by the Free Software Foundation; either version 2
  12 * of the License, or (at your option) any later version.
  13 *
  14 * This program is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17 * GNU General Public License for more details.
  18 *
  19 * NO WARRANTY
  20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
  21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
  22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
  23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
  24 * solely responsible for determining the appropriateness of using and
  25 * distributing the Program and assumes all risks associated with its
  26 * exercise of rights under this Agreement, including but not limited to
  27 * the risks and costs of program errors, damage to or loss of data,
  28 * programs or equipment, and unavailability or interruption of operations.
  29
  30 * DISCLAIMER OF LIABILITY
  31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  38
  39 * You should have received a copy of the GNU General Public License
  40 * along with this program; if not, write to the Free Software
  41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
  42 * USA.
  43 */
  44
  45#include <linux/kernel.h>
  46#include <linux/module.h>
  47#include <linux/errno.h>
  48#include <linux/init.h>
  49#include <linux/slab.h>
  50#include <linux/types.h>
  51#include <linux/pci.h>
  52#include <linux/delay.h>
  53#include <linux/mutex.h>
  54#include <linux/compat.h>
  55#include <linux/poll.h>
  56
  57#include <linux/io.h>
  58#include <linux/uaccess.h>
  59
  60#include "mpt2sas_base.h"
  61#include "mpt2sas_ctl.h"
  62
  63static DEFINE_MUTEX(_ctl_mutex);
  64static struct fasync_struct *async_queue;
  65static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
  66
  67static int _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type,
  68    u8 *issue_reset);
  69
  70/**
  71 * enum block_state - blocking state
  72 * @NON_BLOCKING: non blocking
  73 * @BLOCKING: blocking
  74 *
  75 * These states are for ioctls that need to wait for a response
  76 * from firmware, so they probably require sleep.
  77 */
  78enum block_state {
  79	NON_BLOCKING,
  80	BLOCKING,
  81};
  82
  83#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
  84/**
  85 * _ctl_sas_device_find_by_handle - sas device search
  86 * @ioc: per adapter object
  87 * @handle: sas device handle (assigned by firmware)
  88 * Context: Calling function should acquire ioc->sas_device_lock
  89 *
  90 * This searches for sas_device based on sas_address, then return sas_device
  91 * object.
  92 */
  93static struct _sas_device *
  94_ctl_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
  95{
  96	struct _sas_device *sas_device, *r;
  97
  98	r = NULL;
  99	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
 100		if (sas_device->handle != handle)
 101			continue;
 102		r = sas_device;
 103		goto out;
 104	}
 105
 106 out:
 107	return r;
 108}
 109
 110/**
 111 * _ctl_display_some_debug - debug routine
 112 * @ioc: per adapter object
 113 * @smid: system request message index
 114 * @calling_function_name: string pass from calling function
 115 * @mpi_reply: reply message frame
 116 * Context: none.
 117 *
 118 * Function for displaying debug info helpful when debugging issues
 119 * in this module.
 120 */
 121static void
 122_ctl_display_some_debug(struct MPT2SAS_ADAPTER *ioc, u16 smid,
 123    char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
 124{
 125	Mpi2ConfigRequest_t *mpi_request;
 126	char *desc = NULL;
 127
 128	if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
 129		return;
 130
 131	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
 132	switch (mpi_request->Function) {
 133	case MPI2_FUNCTION_SCSI_IO_REQUEST:
 134	{
 135		Mpi2SCSIIORequest_t *scsi_request =
 136		    (Mpi2SCSIIORequest_t *)mpi_request;
 137
 138		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
 139		    "scsi_io, cmd(0x%02x), cdb_len(%d)",
 140		    scsi_request->CDB.CDB32[0],
 141		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
 142		desc = ioc->tmp_string;
 143		break;
 144	}
 145	case MPI2_FUNCTION_SCSI_TASK_MGMT:
 146		desc = "task_mgmt";
 147		break;
 148	case MPI2_FUNCTION_IOC_INIT:
 149		desc = "ioc_init";
 150		break;
 151	case MPI2_FUNCTION_IOC_FACTS:
 152		desc = "ioc_facts";
 153		break;
 154	case MPI2_FUNCTION_CONFIG:
 155	{
 156		Mpi2ConfigRequest_t *config_request =
 157		    (Mpi2ConfigRequest_t *)mpi_request;
 158
 159		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
 160		    "config, type(0x%02x), ext_type(0x%02x), number(%d)",
 161		    (config_request->Header.PageType &
 162		     MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
 163		    config_request->Header.PageNumber);
 164		desc = ioc->tmp_string;
 165		break;
 166	}
 167	case MPI2_FUNCTION_PORT_FACTS:
 168		desc = "port_facts";
 169		break;
 170	case MPI2_FUNCTION_PORT_ENABLE:
 171		desc = "port_enable";
 172		break;
 173	case MPI2_FUNCTION_EVENT_NOTIFICATION:
 174		desc = "event_notification";
 175		break;
 176	case MPI2_FUNCTION_FW_DOWNLOAD:
 177		desc = "fw_download";
 178		break;
 179	case MPI2_FUNCTION_FW_UPLOAD:
 180		desc = "fw_upload";
 181		break;
 182	case MPI2_FUNCTION_RAID_ACTION:
 183		desc = "raid_action";
 184		break;
 185	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
 186	{
 187		Mpi2SCSIIORequest_t *scsi_request =
 188		    (Mpi2SCSIIORequest_t *)mpi_request;
 189
 190		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
 191		    "raid_pass, cmd(0x%02x), cdb_len(%d)",
 192		    scsi_request->CDB.CDB32[0],
 193		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
 194		desc = ioc->tmp_string;
 195		break;
 196	}
 197	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
 198		desc = "sas_iounit_cntl";
 199		break;
 200	case MPI2_FUNCTION_SATA_PASSTHROUGH:
 201		desc = "sata_pass";
 202		break;
 203	case MPI2_FUNCTION_DIAG_BUFFER_POST:
 204		desc = "diag_buffer_post";
 205		break;
 206	case MPI2_FUNCTION_DIAG_RELEASE:
 207		desc = "diag_release";
 208		break;
 209	case MPI2_FUNCTION_SMP_PASSTHROUGH:
 210		desc = "smp_passthrough";
 211		break;
 212	}
 213
 214	if (!desc)
 215		return;
 216
 217	printk(MPT2SAS_INFO_FMT "%s: %s, smid(%d)\n",
 218	    ioc->name, calling_function_name, desc, smid);
 219
 220	if (!mpi_reply)
 221		return;
 222
 223	if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
 224		printk(MPT2SAS_INFO_FMT
 225		    "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
 226		    ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
 227		    le32_to_cpu(mpi_reply->IOCLogInfo));
 228
 229	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
 230	    mpi_request->Function ==
 231	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
 232		Mpi2SCSIIOReply_t *scsi_reply =
 233		    (Mpi2SCSIIOReply_t *)mpi_reply;
 234		struct _sas_device *sas_device = NULL;
 235		unsigned long flags;
 236
 237		spin_lock_irqsave(&ioc->sas_device_lock, flags);
 238		sas_device = _ctl_sas_device_find_by_handle(ioc,
 239		    le16_to_cpu(scsi_reply->DevHandle));
 240		if (sas_device) {
 241			printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
 242			    "phy(%d)\n", ioc->name, (unsigned long long)
 243			    sas_device->sas_address, sas_device->phy);
 244			printk(MPT2SAS_WARN_FMT
 245			    "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
 246			    ioc->name, sas_device->enclosure_logical_id,
 247			    sas_device->slot);
 248		}
 249		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
 250		if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
 251			printk(MPT2SAS_INFO_FMT
 252			    "\tscsi_state(0x%02x), scsi_status"
 253			    "(0x%02x)\n", ioc->name,
 254			    scsi_reply->SCSIState,
 255			    scsi_reply->SCSIStatus);
 256	}
 257}
 258#endif
 259
 260/**
 261 * mpt2sas_ctl_done - ctl module completion routine
 262 * @ioc: per adapter object
 263 * @smid: system request message index
 264 * @msix_index: MSIX table index supplied by the OS
 265 * @reply: reply message frame(lower 32bit addr)
 266 * Context: none.
 267 *
 268 * The callback handler when using ioc->ctl_cb_idx.
 269 *
 270 * Return 1 meaning mf should be freed from _base_interrupt
 271 *        0 means the mf is freed from this function.
 272 */
 273u8
 274mpt2sas_ctl_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
 275	u32 reply)
 276{
 277	MPI2DefaultReply_t *mpi_reply;
 278	Mpi2SCSIIOReply_t *scsiio_reply;
 279	const void *sense_data;
 280	u32 sz;
 281
 282	if (ioc->ctl_cmds.status == MPT2_CMD_NOT_USED)
 283		return 1;
 284	if (ioc->ctl_cmds.smid != smid)
 285		return 1;
 286	ioc->ctl_cmds.status |= MPT2_CMD_COMPLETE;
 287	mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
 288	if (mpi_reply) {
 289		memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
 290		ioc->ctl_cmds.status |= MPT2_CMD_REPLY_VALID;
 291		/* get sense data */
 292		if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
 293		    mpi_reply->Function ==
 294		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
 295			scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
 296			if (scsiio_reply->SCSIState &
 297			    MPI2_SCSI_STATE_AUTOSENSE_VALID) {
 298				sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
 299				    le32_to_cpu(scsiio_reply->SenseCount));
 300				sense_data = mpt2sas_base_get_sense_buffer(ioc,
 301				    smid);
 302				memcpy(ioc->ctl_cmds.sense, sense_data, sz);
 303			}
 304		}
 305	}
 306#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
 307	_ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
 308#endif
 309	ioc->ctl_cmds.status &= ~MPT2_CMD_PENDING;
 310	complete(&ioc->ctl_cmds.done);
 311	return 1;
 312}
 313
 314/**
 315 * _ctl_check_event_type - determines when an event needs logging
 316 * @ioc: per adapter object
 317 * @event: firmware event
 318 *
 319 * The bitmask in ioc->event_type[] indicates which events should be
 320 * be saved in the driver event_log.  This bitmask is set by application.
 321 *
 322 * Returns 1 when event should be captured, or zero means no match.
 323 */
 324static int
 325_ctl_check_event_type(struct MPT2SAS_ADAPTER *ioc, u16 event)
 326{
 327	u16 i;
 328	u32 desired_event;
 329
 330	if (event >= 128 || !event || !ioc->event_log)
 331		return 0;
 332
 333	desired_event = (1 << (event % 32));
 334	if (!desired_event)
 335		desired_event = 1;
 336	i = event / 32;
 337	return desired_event & ioc->event_type[i];
 338}
 339
 340/**
 341 * mpt2sas_ctl_add_to_event_log - add event
 342 * @ioc: per adapter object
 343 * @mpi_reply: reply message frame
 344 *
 345 * Return nothing.
 346 */
 347void
 348mpt2sas_ctl_add_to_event_log(struct MPT2SAS_ADAPTER *ioc,
 349    Mpi2EventNotificationReply_t *mpi_reply)
 350{
 351	struct MPT2_IOCTL_EVENTS *event_log;
 352	u16 event;
 353	int i;
 354	u32 sz, event_data_sz;
 355	u8 send_aen = 0;
 356
 357	if (!ioc->event_log)
 358		return;
 359
 360	event = le16_to_cpu(mpi_reply->Event);
 361
 362	if (_ctl_check_event_type(ioc, event)) {
 363
 364		/* insert entry into circular event_log */
 365		i = ioc->event_context % MPT2SAS_CTL_EVENT_LOG_SIZE;
 366		event_log = ioc->event_log;
 367		event_log[i].event = event;
 368		event_log[i].context = ioc->event_context++;
 369
 370		event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
 371		sz = min_t(u32, event_data_sz, MPT2_EVENT_DATA_SIZE);
 372		memset(event_log[i].data, 0, MPT2_EVENT_DATA_SIZE);
 373		memcpy(event_log[i].data, mpi_reply->EventData, sz);
 374		send_aen = 1;
 375	}
 376
 377	/* This aen_event_read_flag flag is set until the
 378	 * application has read the event log.
 379	 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
 380	 */
 381	if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
 382	    (send_aen && !ioc->aen_event_read_flag)) {
 383		ioc->aen_event_read_flag = 1;
 384		wake_up_interruptible(&ctl_poll_wait);
 385		if (async_queue)
 386			kill_fasync(&async_queue, SIGIO, POLL_IN);
 387	}
 388}
 389
 390/**
 391 * mpt2sas_ctl_event_callback - firmware event handler (called at ISR time)
 392 * @ioc: per adapter object
 393 * @msix_index: MSIX table index supplied by the OS
 394 * @reply: reply message frame(lower 32bit addr)
 395 * Context: interrupt.
 396 *
 397 * This function merely adds a new work task into ioc->firmware_event_thread.
 398 * The tasks are worked from _firmware_event_work in user context.
 399 *
 400 * Return 1 meaning mf should be freed from _base_interrupt
 401 *        0 means the mf is freed from this function.
 402 */
 403u8
 404mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
 405	u32 reply)
 406{
 407	Mpi2EventNotificationReply_t *mpi_reply;
 408
 409	mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
 
 
 
 
 
 410	mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
 411	return 1;
 412}
 413
 414/**
 415 * _ctl_verify_adapter - validates ioc_number passed from application
 416 * @ioc: per adapter object
 417 * @iocpp: The ioc pointer is returned in this.
 418 *
 419 * Return (-1) means error, else ioc_number.
 420 */
 421static int
 422_ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
 423{
 424	struct MPT2SAS_ADAPTER *ioc;
 425
 426	list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
 427		if (ioc->id != ioc_number)
 428			continue;
 429		*iocpp = ioc;
 430		return ioc_number;
 431	}
 432	*iocpp = NULL;
 433	return -1;
 434}
 435
 436/**
 437 * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
 438 * @ioc: per adapter object
 439 * @reset_phase: phase
 440 *
 441 * The handler for doing any required cleanup or initialization.
 442 *
 443 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
 444 * MPT2_IOC_DONE_RESET
 445 */
 446void
 447mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
 448{
 449	int i;
 450	u8 issue_reset;
 451
 452	switch (reset_phase) {
 453	case MPT2_IOC_PRE_RESET:
 454		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
 455		    "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
 456		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
 457			if (!(ioc->diag_buffer_status[i] &
 458			    MPT2_DIAG_BUFFER_IS_REGISTERED))
 459				continue;
 460			if ((ioc->diag_buffer_status[i] &
 461			    MPT2_DIAG_BUFFER_IS_RELEASED))
 462				continue;
 463			_ctl_send_release(ioc, i, &issue_reset);
 464		}
 465		break;
 466	case MPT2_IOC_AFTER_RESET:
 467		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
 468		    "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
 469		if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
 470			ioc->ctl_cmds.status |= MPT2_CMD_RESET;
 471			mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
 472			complete(&ioc->ctl_cmds.done);
 473		}
 474		break;
 475	case MPT2_IOC_DONE_RESET:
 476		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
 477		    "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
 478
 479		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
 480			if (!(ioc->diag_buffer_status[i] &
 481			    MPT2_DIAG_BUFFER_IS_REGISTERED))
 482				continue;
 483			if ((ioc->diag_buffer_status[i] &
 484			    MPT2_DIAG_BUFFER_IS_RELEASED))
 485				continue;
 486			ioc->diag_buffer_status[i] |=
 487			    MPT2_DIAG_BUFFER_IS_DIAG_RESET;
 488		}
 489		break;
 490	}
 491}
 492
 493/**
 494 * _ctl_fasync -
 495 * @fd -
 496 * @filep -
 497 * @mode -
 498 *
 499 * Called when application request fasyn callback handler.
 500 */
 501static int
 502_ctl_fasync(int fd, struct file *filep, int mode)
 503{
 504	return fasync_helper(fd, filep, mode, &async_queue);
 505}
 506
 507/**
 508 * _ctl_release -
 509 * @inode -
 510 * @filep -
 511 *
 512 * Called when application releases the fasyn callback handler.
 513 */
 514static int
 515_ctl_release(struct inode *inode, struct file *filep)
 516{
 517	return fasync_helper(-1, filep, 0, &async_queue);
 518}
 519
 520/**
 521 * _ctl_poll -
 522 * @file -
 523 * @wait -
 524 *
 525 */
 526static unsigned int
 527_ctl_poll(struct file *filep, poll_table *wait)
 528{
 529	struct MPT2SAS_ADAPTER *ioc;
 530
 531	poll_wait(filep, &ctl_poll_wait, wait);
 532
 533	list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
 534		if (ioc->aen_event_read_flag)
 535			return POLLIN | POLLRDNORM;
 536	}
 537	return 0;
 538}
 539
 540/**
 541 * _ctl_set_task_mid - assign an active smid to tm request
 542 * @ioc: per adapter object
 543 * @karg - (struct mpt2_ioctl_command)
 544 * @tm_request - pointer to mf from user space
 545 *
 546 * Returns 0 when an smid if found, else fail.
 547 * during failure, the reply frame is filled.
 548 */
 549static int
 550_ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
 551    Mpi2SCSITaskManagementRequest_t *tm_request)
 552{
 553	u8 found = 0;
 554	u16 i;
 555	u16 handle;
 556	struct scsi_cmnd *scmd;
 557	struct MPT2SAS_DEVICE *priv_data;
 558	unsigned long flags;
 559	Mpi2SCSITaskManagementReply_t *tm_reply;
 560	u32 sz;
 561	u32 lun;
 562	char *desc = NULL;
 563
 564	if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
 565		desc = "abort_task";
 566	else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
 567		desc = "query_task";
 568	else
 569		return 0;
 570
 571	lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
 572
 573	handle = le16_to_cpu(tm_request->DevHandle);
 574	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
 575	for (i = ioc->scsiio_depth; i && !found; i--) {
 576		scmd = ioc->scsi_lookup[i - 1].scmd;
 577		if (scmd == NULL || scmd->device == NULL ||
 578		    scmd->device->hostdata == NULL)
 579			continue;
 580		if (lun != scmd->device->lun)
 581			continue;
 582		priv_data = scmd->device->hostdata;
 583		if (priv_data->sas_target == NULL)
 584			continue;
 585		if (priv_data->sas_target->handle != handle)
 586			continue;
 587		tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
 588		found = 1;
 589	}
 590	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
 591
 592	if (!found) {
 593		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
 594		    "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
 595		    desc, le16_to_cpu(tm_request->DevHandle), lun));
 596		tm_reply = ioc->ctl_cmds.reply;
 597		tm_reply->DevHandle = tm_request->DevHandle;
 598		tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
 599		tm_reply->TaskType = tm_request->TaskType;
 600		tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
 601		tm_reply->VP_ID = tm_request->VP_ID;
 602		tm_reply->VF_ID = tm_request->VF_ID;
 603		sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
 604		if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
 605		    sz))
 606			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 607			    __LINE__, __func__);
 608		return 1;
 609	}
 610
 611	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
 612	    "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
 613	    desc, le16_to_cpu(tm_request->DevHandle), lun,
 614	     le16_to_cpu(tm_request->TaskMID)));
 615	return 0;
 616}
 617
 618/**
 619 * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
 620 * @ioc: per adapter object
 621 * @karg - (struct mpt2_ioctl_command)
 622 * @mf - pointer to mf in user space
 623 */
 624static long
 625_ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command karg,
 626	void __user *mf)
 627{
 628	MPI2RequestHeader_t *mpi_request = NULL, *request;
 629	MPI2DefaultReply_t *mpi_reply;
 630	u32 ioc_state;
 631	u16 ioc_status;
 632	u16 smid;
 633	unsigned long timeout, timeleft;
 634	u8 issue_reset;
 635	u32 sz;
 636	void *psge;
 637	void *data_out = NULL;
 638	dma_addr_t data_out_dma;
 639	size_t data_out_sz = 0;
 640	void *data_in = NULL;
 641	dma_addr_t data_in_dma;
 642	size_t data_in_sz = 0;
 643	u32 sgl_flags;
 644	long ret;
 645	u16 wait_state_count;
 646
 647	issue_reset = 0;
 648
 649	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
 650		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
 651		    ioc->name, __func__);
 652		ret = -EAGAIN;
 653		goto out;
 654	}
 655
 656	wait_state_count = 0;
 657	ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
 658	while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
 659		if (wait_state_count++ == 10) {
 660			printk(MPT2SAS_ERR_FMT
 661			    "%s: failed due to ioc not operational\n",
 662			    ioc->name, __func__);
 663			ret = -EFAULT;
 664			goto out;
 665		}
 666		ssleep(1);
 667		ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
 668		printk(MPT2SAS_INFO_FMT "%s: waiting for "
 669		    "operational state(count=%d)\n", ioc->name,
 670		    __func__, wait_state_count);
 671	}
 672	if (wait_state_count)
 673		printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
 674		    ioc->name, __func__);
 675
 676	mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
 677	if (!mpi_request) {
 678		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a memory for "
 679		    "mpi_request\n", ioc->name, __func__);
 680		ret = -ENOMEM;
 681		goto out;
 682	}
 683
 684	/* Check for overflow and wraparound */
 685	if (karg.data_sge_offset * 4 > ioc->request_sz ||
 686	    karg.data_sge_offset > (UINT_MAX / 4)) {
 687		ret = -EINVAL;
 688		goto out;
 689	}
 690
 691	/* copy in request message frame from user */
 692	if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
 693		printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
 694		    __func__);
 695		ret = -EFAULT;
 696		goto out;
 697	}
 698
 699	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
 700		smid = mpt2sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
 701		if (!smid) {
 702			printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
 703			    ioc->name, __func__);
 704			ret = -EAGAIN;
 705			goto out;
 706		}
 707	} else {
 708
 709		smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
 710		if (!smid) {
 711			printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
 712			    ioc->name, __func__);
 713			ret = -EAGAIN;
 714			goto out;
 715		}
 716	}
 717
 718	ret = 0;
 719	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
 720	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
 721	request = mpt2sas_base_get_msg_frame(ioc, smid);
 722	memcpy(request, mpi_request, karg.data_sge_offset*4);
 723	ioc->ctl_cmds.smid = smid;
 724	data_out_sz = karg.data_out_size;
 725	data_in_sz = karg.data_in_size;
 726
 727	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
 728	    mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
 729		if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
 730		    le16_to_cpu(mpi_request->FunctionDependent1) >
 731		    ioc->facts.MaxDevHandle) {
 732			ret = -EINVAL;
 733			mpt2sas_base_free_smid(ioc, smid);
 734			goto out;
 735		}
 736	}
 737
 738	/* obtain dma-able memory for data transfer */
 739	if (data_out_sz) /* WRITE */ {
 740		data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
 741		    &data_out_dma);
 742		if (!data_out) {
 743			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 744			    __LINE__, __func__);
 745			ret = -ENOMEM;
 746			mpt2sas_base_free_smid(ioc, smid);
 747			goto out;
 748		}
 749		if (copy_from_user(data_out, karg.data_out_buf_ptr,
 750			data_out_sz)) {
 751			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 752			    __LINE__, __func__);
 753			ret =  -EFAULT;
 754			mpt2sas_base_free_smid(ioc, smid);
 755			goto out;
 756		}
 757	}
 758
 759	if (data_in_sz) /* READ */ {
 760		data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
 761		    &data_in_dma);
 762		if (!data_in) {
 763			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 764			    __LINE__, __func__);
 765			ret = -ENOMEM;
 766			mpt2sas_base_free_smid(ioc, smid);
 767			goto out;
 768		}
 769	}
 770
 771	/* add scatter gather elements */
 772	psge = (void *)request + (karg.data_sge_offset*4);
 773
 774	if (!data_out_sz && !data_in_sz) {
 775		mpt2sas_base_build_zero_len_sge(ioc, psge);
 776	} else if (data_out_sz && data_in_sz) {
 777		/* WRITE sgel first */
 778		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
 779		    MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
 780		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
 781		ioc->base_add_sg_single(psge, sgl_flags |
 782		    data_out_sz, data_out_dma);
 783
 784		/* incr sgel */
 785		psge += ioc->sge_size;
 786
 787		/* READ sgel last */
 788		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
 789		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
 790		    MPI2_SGE_FLAGS_END_OF_LIST);
 791		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
 792		ioc->base_add_sg_single(psge, sgl_flags |
 793		    data_in_sz, data_in_dma);
 794	} else if (data_out_sz) /* WRITE */ {
 795		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
 796		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
 797		    MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
 798		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
 799		ioc->base_add_sg_single(psge, sgl_flags |
 800		    data_out_sz, data_out_dma);
 801	} else if (data_in_sz) /* READ */ {
 802		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
 803		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
 804		    MPI2_SGE_FLAGS_END_OF_LIST);
 805		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
 806		ioc->base_add_sg_single(psge, sgl_flags |
 807		    data_in_sz, data_in_dma);
 808	}
 809
 810	/* send command to firmware */
 811#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
 812	_ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
 813#endif
 814
 815	init_completion(&ioc->ctl_cmds.done);
 816	switch (mpi_request->Function) {
 817	case MPI2_FUNCTION_SCSI_IO_REQUEST:
 818	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
 819	{
 820		Mpi2SCSIIORequest_t *scsiio_request =
 821		    (Mpi2SCSIIORequest_t *)request;
 822		scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
 823		scsiio_request->SenseBufferLowAddress =
 824		    mpt2sas_base_get_sense_buffer_dma(ioc, smid);
 825		memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
 826		if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
 827			mpt2sas_base_put_smid_scsi_io(ioc, smid,
 828			    le16_to_cpu(mpi_request->FunctionDependent1));
 829		else
 830			mpt2sas_base_put_smid_default(ioc, smid);
 831		break;
 832	}
 833	case MPI2_FUNCTION_SCSI_TASK_MGMT:
 834	{
 835		Mpi2SCSITaskManagementRequest_t *tm_request =
 836		    (Mpi2SCSITaskManagementRequest_t *)request;
 837
 838		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
 839		    "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
 840		    le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
 841
 842		if (tm_request->TaskType ==
 843		    MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
 844		    tm_request->TaskType ==
 845		    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
 846			if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
 847				mpt2sas_base_free_smid(ioc, smid);
 848				goto out;
 849			}
 850		}
 851
 852		mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
 853		    tm_request->DevHandle));
 854		mpt2sas_base_put_smid_hi_priority(ioc, smid);
 855		break;
 856	}
 857	case MPI2_FUNCTION_SMP_PASSTHROUGH:
 858	{
 859		Mpi2SmpPassthroughRequest_t *smp_request =
 860		    (Mpi2SmpPassthroughRequest_t *)mpi_request;
 861		u8 *data;
 862
 863		/* ioc determines which port to use */
 864		smp_request->PhysicalPort = 0xFF;
 865		if (smp_request->PassthroughFlags &
 866		    MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
 867			data = (u8 *)&smp_request->SGL;
 868		else {
 869			if (unlikely(data_out == NULL)) {
 870				printk(KERN_ERR "failure at %s:%d/%s()!\n",
 871				    __FILE__, __LINE__, __func__);
 872				mpt2sas_base_free_smid(ioc, smid);
 873				ret = -EINVAL;
 874				goto out;
 875			}
 876			data = data_out;
 877		}
 878
 879		if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
 880			ioc->ioc_link_reset_in_progress = 1;
 881			ioc->ignore_loginfos = 1;
 882		}
 883		mpt2sas_base_put_smid_default(ioc, smid);
 884		break;
 885	}
 886	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
 887	{
 888		Mpi2SasIoUnitControlRequest_t *sasiounit_request =
 889		    (Mpi2SasIoUnitControlRequest_t *)mpi_request;
 890
 891		if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
 892		    || sasiounit_request->Operation ==
 893		    MPI2_SAS_OP_PHY_LINK_RESET) {
 894			ioc->ioc_link_reset_in_progress = 1;
 895			ioc->ignore_loginfos = 1;
 896		}
 897		mpt2sas_base_put_smid_default(ioc, smid);
 898		break;
 899	}
 900	default:
 901		mpt2sas_base_put_smid_default(ioc, smid);
 902		break;
 903	}
 904
 905	if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
 906		timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
 907	else
 908		timeout = karg.timeout;
 909	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
 910	    timeout*HZ);
 911	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
 912		Mpi2SCSITaskManagementRequest_t *tm_request =
 913		    (Mpi2SCSITaskManagementRequest_t *)mpi_request;
 914		mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
 915		    tm_request->DevHandle));
 916	} else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
 917	    mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
 918		ioc->ioc_link_reset_in_progress) {
 919		ioc->ioc_link_reset_in_progress = 0;
 920		ioc->ignore_loginfos = 0;
 921	}
 922	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
 923		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
 924		    __func__);
 925		_debug_dump_mf(mpi_request, karg.data_sge_offset);
 926		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
 927			issue_reset = 1;
 928		goto issue_host_reset;
 929	}
 930
 931	mpi_reply = ioc->ctl_cmds.reply;
 932	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
 933
 934#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
 935	if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
 936	    (ioc->logging_level & MPT_DEBUG_TM)) {
 937		Mpi2SCSITaskManagementReply_t *tm_reply =
 938		    (Mpi2SCSITaskManagementReply_t *)mpi_reply;
 939
 940		printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
 941		    "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
 942		    "TerminationCount(0x%08x)\n", ioc->name,
 943		    le16_to_cpu(tm_reply->IOCStatus),
 944		    le32_to_cpu(tm_reply->IOCLogInfo),
 945		    le32_to_cpu(tm_reply->TerminationCount));
 946	}
 947#endif
 948	/* copy out xdata to user */
 949	if (data_in_sz) {
 950		if (copy_to_user(karg.data_in_buf_ptr, data_in,
 951		    data_in_sz)) {
 952			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 953			    __LINE__, __func__);
 954			ret = -ENODATA;
 955			goto out;
 956		}
 957	}
 958
 959	/* copy out reply message frame to user */
 960	if (karg.max_reply_bytes) {
 961		sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
 962		if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
 963		    sz)) {
 964			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 965			    __LINE__, __func__);
 966			ret = -ENODATA;
 967			goto out;
 968		}
 969	}
 970
 971	/* copy out sense to user */
 972	if (karg.max_sense_bytes && (mpi_request->Function ==
 973	    MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
 974	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
 975		sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
 976		if (copy_to_user(karg.sense_data_ptr,
 977			ioc->ctl_cmds.sense, sz)) {
 978			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
 979			    __LINE__, __func__);
 980			ret = -ENODATA;
 981			goto out;
 982		}
 983	}
 984
 985 issue_host_reset:
 986	if (issue_reset) {
 987		ret = -ENODATA;
 988		if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
 989		    mpi_request->Function ==
 990		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
 991		    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
 992			printk(MPT2SAS_INFO_FMT "issue target reset: handle "
 993			    "= (0x%04x)\n", ioc->name,
 994			    le16_to_cpu(mpi_request->FunctionDependent1));
 995			mpt2sas_halt_firmware(ioc);
 996			mpt2sas_scsih_issue_tm(ioc,
 997			    le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
 998			    0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
 999			    0, TM_MUTEX_ON);
1000			ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1001		} else
1002			mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1003			    FORCE_BIG_HAMMER);
1004	}
1005
1006 out:
1007
1008	/* free memory associated with sg buffers */
1009	if (data_in)
1010		pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1011		    data_in_dma);
1012
1013	if (data_out)
1014		pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1015		    data_out_dma);
1016
1017	kfree(mpi_request);
1018	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1019	return ret;
1020}
1021
1022/**
1023 * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
1024 * @ioc: per adapter object
1025 * @arg - user space buffer containing ioctl content
1026 */
1027static long
1028_ctl_getiocinfo(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1029{
1030	struct mpt2_ioctl_iocinfo karg;
1031
1032	if (copy_from_user(&karg, arg, sizeof(karg))) {
1033		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1034		    __FILE__, __LINE__, __func__);
1035		return -EFAULT;
1036	}
1037
1038	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1039	    __func__));
1040
1041	memset(&karg, 0 , sizeof(karg));
1042	if (ioc->is_warpdrive)
1043		karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1044	else
1045		karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1046	if (ioc->pfacts)
1047		karg.port_number = ioc->pfacts[0].PortNumber;
1048	karg.hw_rev = ioc->pdev->revision;
1049	karg.pci_id = ioc->pdev->device;
1050	karg.subsystem_device = ioc->pdev->subsystem_device;
1051	karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1052	karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1053	karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1054	karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1055	karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1056	karg.firmware_version = ioc->facts.FWVersion.Word;
1057	strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
1058	strcat(karg.driver_version, "-");
1059	strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1060	karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1061
1062	if (copy_to_user(arg, &karg, sizeof(karg))) {
1063		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1064		    __FILE__, __LINE__, __func__);
1065		return -EFAULT;
1066	}
1067	return 0;
1068}
1069
1070/**
1071 * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
1072 * @ioc: per adapter object
1073 * @arg - user space buffer containing ioctl content
1074 */
1075static long
1076_ctl_eventquery(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1077{
1078	struct mpt2_ioctl_eventquery karg;
1079
1080	if (copy_from_user(&karg, arg, sizeof(karg))) {
1081		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1082		    __FILE__, __LINE__, __func__);
1083		return -EFAULT;
1084	}
1085
1086	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1087	    __func__));
1088
1089	karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
1090	memcpy(karg.event_types, ioc->event_type,
1091	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1092
1093	if (copy_to_user(arg, &karg, sizeof(karg))) {
1094		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1095		    __FILE__, __LINE__, __func__);
1096		return -EFAULT;
1097	}
1098	return 0;
1099}
1100
1101/**
1102 * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1103 * @ioc: per adapter object
1104 * @arg - user space buffer containing ioctl content
1105 */
1106static long
1107_ctl_eventenable(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1108{
1109	struct mpt2_ioctl_eventenable karg;
1110
1111	if (copy_from_user(&karg, arg, sizeof(karg))) {
1112		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1113		    __FILE__, __LINE__, __func__);
1114		return -EFAULT;
1115	}
1116
1117	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1118	    __func__));
1119
1120	if (ioc->event_log)
1121		return 0;
1122	memcpy(ioc->event_type, karg.event_types,
1123	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1124	mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1125
1126	/* initialize event_log */
1127	ioc->event_context = 0;
1128	ioc->aen_event_read_flag = 0;
1129	ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1130	    sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1131	if (!ioc->event_log) {
1132		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1133		    __FILE__, __LINE__, __func__);
1134		return -ENOMEM;
1135	}
1136	return 0;
1137}
1138
1139/**
1140 * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1141 * @ioc: per adapter object
1142 * @arg - user space buffer containing ioctl content
1143 */
1144static long
1145_ctl_eventreport(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1146{
1147	struct mpt2_ioctl_eventreport karg;
1148	u32 number_bytes, max_events, max;
1149	struct mpt2_ioctl_eventreport __user *uarg = arg;
1150
1151	if (copy_from_user(&karg, arg, sizeof(karg))) {
1152		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1153		    __FILE__, __LINE__, __func__);
1154		return -EFAULT;
1155	}
1156
1157	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1158	    __func__));
1159
1160	number_bytes = karg.hdr.max_data_size -
1161	    sizeof(struct mpt2_ioctl_header);
1162	max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1163	max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1164
1165	/* If fewer than 1 event is requested, there must have
1166	 * been some type of error.
1167	 */
1168	if (!max || !ioc->event_log)
1169		return -ENODATA;
1170
1171	number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1172	if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1173		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1174		    __FILE__, __LINE__, __func__);
1175		return -EFAULT;
1176	}
1177
1178	/* reset flag so SIGIO can restart */
1179	ioc->aen_event_read_flag = 0;
1180	return 0;
1181}
1182
1183/**
1184 * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1185 * @ioc: per adapter object
1186 * @arg - user space buffer containing ioctl content
1187 */
1188static long
1189_ctl_do_reset(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1190{
1191	struct mpt2_ioctl_diag_reset karg;
1192	int retval;
1193
1194	if (copy_from_user(&karg, arg, sizeof(karg))) {
1195		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1196		    __FILE__, __LINE__, __func__);
1197		return -EFAULT;
1198	}
1199
1200	if (ioc->shost_recovery || ioc->pci_error_recovery ||
1201		ioc->is_driver_loading)
1202		return -EAGAIN;
1203	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1204	    __func__));
1205
1206	retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1207	    FORCE_BIG_HAMMER);
1208	printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1209	    ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1210	return 0;
1211}
1212
1213/**
1214 * _ctl_btdh_search_sas_device - searching for sas device
1215 * @ioc: per adapter object
1216 * @btdh: btdh ioctl payload
1217 */
1218static int
1219_ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1220    struct mpt2_ioctl_btdh_mapping *btdh)
1221{
1222	struct _sas_device *sas_device;
1223	unsigned long flags;
1224	int rc = 0;
1225
1226	if (list_empty(&ioc->sas_device_list))
1227		return rc;
1228
1229	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1230	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1231		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1232		    btdh->handle == sas_device->handle) {
1233			btdh->bus = sas_device->channel;
1234			btdh->id = sas_device->id;
1235			rc = 1;
1236			goto out;
1237		} else if (btdh->bus == sas_device->channel && btdh->id ==
1238		    sas_device->id && btdh->handle == 0xFFFF) {
1239			btdh->handle = sas_device->handle;
1240			rc = 1;
1241			goto out;
1242		}
1243	}
1244 out:
1245	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1246	return rc;
1247}
1248
1249/**
1250 * _ctl_btdh_search_raid_device - searching for raid device
1251 * @ioc: per adapter object
1252 * @btdh: btdh ioctl payload
1253 */
1254static int
1255_ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1256    struct mpt2_ioctl_btdh_mapping *btdh)
1257{
1258	struct _raid_device *raid_device;
1259	unsigned long flags;
1260	int rc = 0;
1261
1262	if (list_empty(&ioc->raid_device_list))
1263		return rc;
1264
1265	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1266	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1267		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1268		    btdh->handle == raid_device->handle) {
1269			btdh->bus = raid_device->channel;
1270			btdh->id = raid_device->id;
1271			rc = 1;
1272			goto out;
1273		} else if (btdh->bus == raid_device->channel && btdh->id ==
1274		    raid_device->id && btdh->handle == 0xFFFF) {
1275			btdh->handle = raid_device->handle;
1276			rc = 1;
1277			goto out;
1278		}
1279	}
1280 out:
1281	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1282	return rc;
1283}
1284
1285/**
1286 * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1287 * @ioc: per adapter object
1288 * @arg - user space buffer containing ioctl content
1289 */
1290static long
1291_ctl_btdh_mapping(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1292{
1293	struct mpt2_ioctl_btdh_mapping karg;
1294	int rc;
1295
1296	if (copy_from_user(&karg, arg, sizeof(karg))) {
1297		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1298		    __FILE__, __LINE__, __func__);
1299		return -EFAULT;
1300	}
1301
1302	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1303	    __func__));
1304
1305	rc = _ctl_btdh_search_sas_device(ioc, &karg);
1306	if (!rc)
1307		_ctl_btdh_search_raid_device(ioc, &karg);
1308
1309	if (copy_to_user(arg, &karg, sizeof(karg))) {
1310		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1311		    __FILE__, __LINE__, __func__);
1312		return -EFAULT;
1313	}
1314	return 0;
1315}
1316
1317/**
1318 * _ctl_diag_capability - return diag buffer capability
1319 * @ioc: per adapter object
1320 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1321 *
1322 * returns 1 when diag buffer support is enabled in firmware
1323 */
1324static u8
1325_ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1326{
1327	u8 rc = 0;
1328
1329	switch (buffer_type) {
1330	case MPI2_DIAG_BUF_TYPE_TRACE:
1331		if (ioc->facts.IOCCapabilities &
1332		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1333			rc = 1;
1334		break;
1335	case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1336		if (ioc->facts.IOCCapabilities &
1337		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1338			rc = 1;
1339		break;
1340	case MPI2_DIAG_BUF_TYPE_EXTENDED:
1341		if (ioc->facts.IOCCapabilities &
1342		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1343			rc = 1;
1344	}
1345
1346	return rc;
1347}
1348
1349/**
1350 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1351 * @ioc: per adapter object
1352 * @diag_register: the diag_register struct passed in from user space
1353 *
1354 */
1355static long
1356_ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
1357    struct mpt2_diag_register *diag_register)
1358{
1359	int rc, i;
1360	void *request_data = NULL;
1361	dma_addr_t request_data_dma;
1362	u32 request_data_sz = 0;
1363	Mpi2DiagBufferPostRequest_t *mpi_request;
1364	Mpi2DiagBufferPostReply_t *mpi_reply;
1365	u8 buffer_type;
1366	unsigned long timeleft;
1367	u16 smid;
1368	u16 ioc_status;
1369	u8 issue_reset = 0;
1370
1371	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1372	    __func__));
1373
1374	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1375		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1376		    ioc->name, __func__);
1377		rc = -EAGAIN;
1378		goto out;
1379	}
1380
1381	buffer_type = diag_register->buffer_type;
1382	if (!_ctl_diag_capability(ioc, buffer_type)) {
1383		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1384		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1385		return -EPERM;
1386	}
1387
1388	if (ioc->diag_buffer_status[buffer_type] &
1389	    MPT2_DIAG_BUFFER_IS_REGISTERED) {
1390		printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1391		    "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1392		    buffer_type);
1393		return -EINVAL;
1394	}
1395
1396	if (diag_register->requested_buffer_size % 4)  {
1397		printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1398		    "is not 4 byte aligned\n", ioc->name, __func__);
1399		return -EINVAL;
1400	}
1401
1402	smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1403	if (!smid) {
1404		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1405		    ioc->name, __func__);
1406		rc = -EAGAIN;
1407		goto out;
1408	}
1409
1410	rc = 0;
1411	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1412	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1413	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1414	ioc->ctl_cmds.smid = smid;
1415
1416	request_data = ioc->diag_buffer[buffer_type];
1417	request_data_sz = diag_register->requested_buffer_size;
1418	ioc->unique_id[buffer_type] = diag_register->unique_id;
1419	ioc->diag_buffer_status[buffer_type] = 0;
1420	memcpy(ioc->product_specific[buffer_type],
1421	    diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
1422	ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1423
1424	if (request_data) {
1425		request_data_dma = ioc->diag_buffer_dma[buffer_type];
1426		if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1427			pci_free_consistent(ioc->pdev,
1428			    ioc->diag_buffer_sz[buffer_type],
1429			    request_data, request_data_dma);
1430			request_data = NULL;
1431		}
1432	}
1433
1434	if (request_data == NULL) {
1435		ioc->diag_buffer_sz[buffer_type] = 0;
1436		ioc->diag_buffer_dma[buffer_type] = 0;
1437		request_data = pci_alloc_consistent(
1438			ioc->pdev, request_data_sz, &request_data_dma);
1439		if (request_data == NULL) {
1440			printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1441			    " for diag buffers, requested size(%d)\n",
1442			    ioc->name, __func__, request_data_sz);
1443			mpt2sas_base_free_smid(ioc, smid);
1444			return -ENOMEM;
1445		}
1446		ioc->diag_buffer[buffer_type] = request_data;
1447		ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1448		ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1449	}
1450
1451	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1452	mpi_request->BufferType = diag_register->buffer_type;
1453	mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1454	mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1455	mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1456	mpi_request->VF_ID = 0; /* TODO */
1457	mpi_request->VP_ID = 0;
1458
1459	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(0x%p), "
1460	    "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1461	    (unsigned long long)request_data_dma,
1462	    le32_to_cpu(mpi_request->BufferLength)));
1463
1464	for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1465		mpi_request->ProductSpecific[i] =
1466			cpu_to_le32(ioc->product_specific[buffer_type][i]);
1467
1468	init_completion(&ioc->ctl_cmds.done);
1469	mpt2sas_base_put_smid_default(ioc, smid);
1470	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1471	    MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1472
1473	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1474		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1475		    __func__);
1476		_debug_dump_mf(mpi_request,
1477		    sizeof(Mpi2DiagBufferPostRequest_t)/4);
1478		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1479			issue_reset = 1;
1480		goto issue_host_reset;
1481	}
1482
1483	/* process the completed Reply Message Frame */
1484	if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1485		printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1486		    ioc->name, __func__);
1487		rc = -EFAULT;
1488		goto out;
1489	}
1490
1491	mpi_reply = ioc->ctl_cmds.reply;
1492	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1493
1494	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1495		ioc->diag_buffer_status[buffer_type] |=
1496			MPT2_DIAG_BUFFER_IS_REGISTERED;
1497		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1498		    ioc->name, __func__));
1499	} else {
1500		printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1501		    "log_info(0x%08x)\n", ioc->name, __func__,
1502		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1503		rc = -EFAULT;
1504	}
1505
1506 issue_host_reset:
1507	if (issue_reset)
1508		mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1509		    FORCE_BIG_HAMMER);
1510
1511 out:
1512
1513	if (rc && request_data)
1514		pci_free_consistent(ioc->pdev, request_data_sz,
1515		    request_data, request_data_dma);
1516
1517	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1518	return rc;
1519}
1520
1521/**
1522 * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
1523 * @ioc: per adapter object
1524 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1525 *
1526 * This is called when command line option diag_buffer_enable is enabled
1527 * at driver load time.
1528 */
1529void
1530mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
1531{
1532	struct mpt2_diag_register diag_register;
1533
1534	memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
1535
1536	if (bits_to_register & 1) {
1537		printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
1538		    ioc->name);
1539		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1540		/* register for 1MB buffers  */
1541		diag_register.requested_buffer_size = (1024 * 1024);
1542		diag_register.unique_id = 0x7075900;
1543		_ctl_diag_register_2(ioc,  &diag_register);
1544	}
1545
1546	if (bits_to_register & 2) {
1547		printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
1548		    ioc->name);
1549		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1550		/* register for 2MB buffers  */
1551		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1552		diag_register.unique_id = 0x7075901;
1553		_ctl_diag_register_2(ioc,  &diag_register);
1554	}
1555
1556	if (bits_to_register & 4) {
1557		printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
1558		    ioc->name);
1559		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1560		/* register for 2MB buffers  */
1561		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1562		diag_register.unique_id = 0x7075901;
1563		_ctl_diag_register_2(ioc,  &diag_register);
1564	}
1565}
1566
1567/**
1568 * _ctl_diag_register - application register with driver
1569 * @ioc: per adapter object
1570 * @arg - user space buffer containing ioctl content
1571 *
1572 * This will allow the driver to setup any required buffers that will be
1573 * needed by firmware to communicate with the driver.
1574 */
1575static long
1576_ctl_diag_register(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1577{
1578	struct mpt2_diag_register karg;
1579	long rc;
1580
1581	if (copy_from_user(&karg, arg, sizeof(karg))) {
1582		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1583		    __FILE__, __LINE__, __func__);
1584		return -EFAULT;
1585	}
1586
1587	rc = _ctl_diag_register_2(ioc, &karg);
1588	return rc;
1589}
1590
1591/**
1592 * _ctl_diag_unregister - application unregister with driver
1593 * @ioc: per adapter object
1594 * @arg - user space buffer containing ioctl content
1595 *
1596 * This will allow the driver to cleanup any memory allocated for diag
1597 * messages and to free up any resources.
1598 */
1599static long
1600_ctl_diag_unregister(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1601{
1602	struct mpt2_diag_unregister karg;
1603	void *request_data;
1604	dma_addr_t request_data_dma;
1605	u32 request_data_sz;
1606	u8 buffer_type;
1607
1608	if (copy_from_user(&karg, arg, sizeof(karg))) {
1609		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1610		    __FILE__, __LINE__, __func__);
1611		return -EFAULT;
1612	}
1613
1614	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1615	    __func__));
1616
1617	buffer_type = karg.unique_id & 0x000000ff;
1618	if (!_ctl_diag_capability(ioc, buffer_type)) {
1619		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1620		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1621		return -EPERM;
1622	}
1623
1624	if ((ioc->diag_buffer_status[buffer_type] &
1625	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1626		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1627		    "registered\n", ioc->name, __func__, buffer_type);
1628		return -EINVAL;
1629	}
1630	if ((ioc->diag_buffer_status[buffer_type] &
1631	    MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1632		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1633		    "released\n", ioc->name, __func__, buffer_type);
1634		return -EINVAL;
1635	}
1636
1637	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1638		printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1639		    "registered\n", ioc->name, __func__, karg.unique_id);
1640		return -EINVAL;
1641	}
1642
1643	request_data = ioc->diag_buffer[buffer_type];
1644	if (!request_data) {
1645		printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1646		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1647		return -ENOMEM;
1648	}
1649
1650	request_data_sz = ioc->diag_buffer_sz[buffer_type];
1651	request_data_dma = ioc->diag_buffer_dma[buffer_type];
1652	pci_free_consistent(ioc->pdev, request_data_sz,
1653	    request_data, request_data_dma);
1654	ioc->diag_buffer[buffer_type] = NULL;
1655	ioc->diag_buffer_status[buffer_type] = 0;
1656	return 0;
1657}
1658
1659/**
1660 * _ctl_diag_query - query relevant info associated with diag buffers
1661 * @ioc: per adapter object
1662 * @arg - user space buffer containing ioctl content
1663 *
1664 * The application will send only buffer_type and unique_id.  Driver will
1665 * inspect unique_id first, if valid, fill in all the info.  If unique_id is
1666 * 0x00, the driver will return info specified by Buffer Type.
1667 */
1668static long
1669_ctl_diag_query(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1670{
1671	struct mpt2_diag_query karg;
1672	void *request_data;
1673	int i;
1674	u8 buffer_type;
1675
1676	if (copy_from_user(&karg, arg, sizeof(karg))) {
1677		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1678		    __FILE__, __LINE__, __func__);
1679		return -EFAULT;
1680	}
1681
1682	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1683	    __func__));
1684
1685	karg.application_flags = 0;
1686	buffer_type = karg.buffer_type;
1687
1688	if (!_ctl_diag_capability(ioc, buffer_type)) {
1689		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1690		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1691		return -EPERM;
1692	}
1693
1694	if ((ioc->diag_buffer_status[buffer_type] &
1695	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1696		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1697		    "registered\n", ioc->name, __func__, buffer_type);
1698		return -EINVAL;
1699	}
1700
1701	if (karg.unique_id & 0xffffff00) {
1702		if (karg.unique_id != ioc->unique_id[buffer_type]) {
1703			printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1704			    "registered\n", ioc->name, __func__,
1705			    karg.unique_id);
1706			return -EINVAL;
1707		}
1708	}
1709
1710	request_data = ioc->diag_buffer[buffer_type];
1711	if (!request_data) {
1712		printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1713		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1714		return -ENOMEM;
1715	}
1716
1717	if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1718		karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1719		    MPT2_APP_FLAGS_BUFFER_VALID);
1720	else
1721		karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1722		    MPT2_APP_FLAGS_BUFFER_VALID |
1723		    MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1724
1725	for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1726		karg.product_specific[i] =
1727		    ioc->product_specific[buffer_type][i];
1728
1729	karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1730	karg.driver_added_buffer_size = 0;
1731	karg.unique_id = ioc->unique_id[buffer_type];
1732	karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1733
1734	if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1735		printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1736		    "data @ %p\n", ioc->name, __func__, arg);
1737		return -EFAULT;
1738	}
1739	return 0;
1740}
1741
1742/**
1743 * _ctl_send_release - Diag Release Message
1744 * @ioc: per adapter object
1745 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1746 * @issue_reset - specifies whether host reset is required.
1747 *
1748 */
1749static int
1750_ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1751{
1752	Mpi2DiagReleaseRequest_t *mpi_request;
1753	Mpi2DiagReleaseReply_t *mpi_reply;
1754	u16 smid;
1755	u16 ioc_status;
1756	u32 ioc_state;
1757	int rc;
1758	unsigned long timeleft;
1759
1760	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1761	    __func__));
1762
1763	rc = 0;
1764	*issue_reset = 0;
1765
1766	ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1767	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1768		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
1769		    "skipping due to FAULT state\n", ioc->name,
1770		    __func__));
1771		rc = -EAGAIN;
1772		goto out;
1773	}
1774
1775	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1776		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1777		    ioc->name, __func__);
1778		rc = -EAGAIN;
1779		goto out;
1780	}
1781
1782	smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1783	if (!smid) {
1784		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1785		    ioc->name, __func__);
1786		rc = -EAGAIN;
1787		goto out;
1788	}
1789
1790	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1791	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1792	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1793	ioc->ctl_cmds.smid = smid;
1794
1795	mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1796	mpi_request->BufferType = buffer_type;
1797	mpi_request->VF_ID = 0; /* TODO */
1798	mpi_request->VP_ID = 0;
1799
1800	init_completion(&ioc->ctl_cmds.done);
1801	mpt2sas_base_put_smid_default(ioc, smid);
1802	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1803	    MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1804
1805	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1806		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1807		    __func__);
1808		_debug_dump_mf(mpi_request,
1809		    sizeof(Mpi2DiagReleaseRequest_t)/4);
1810		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1811			*issue_reset = 1;
1812		rc = -EFAULT;
1813		goto out;
1814	}
1815
1816	/* process the completed Reply Message Frame */
1817	if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1818		printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1819		    ioc->name, __func__);
1820		rc = -EFAULT;
1821		goto out;
1822	}
1823
1824	mpi_reply = ioc->ctl_cmds.reply;
1825	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1826
1827	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1828		ioc->diag_buffer_status[buffer_type] |=
1829		    MPT2_DIAG_BUFFER_IS_RELEASED;
1830		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1831		    ioc->name, __func__));
1832	} else {
1833		printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1834		    "log_info(0x%08x)\n", ioc->name, __func__,
1835		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1836		rc = -EFAULT;
1837	}
1838
1839 out:
1840	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1841	return rc;
1842}
1843
1844/**
1845 * _ctl_diag_release - request to send Diag Release Message to firmware
1846 * @arg - user space buffer containing ioctl content
1847 *
1848 * This allows ownership of the specified buffer to returned to the driver,
1849 * allowing an application to read the buffer without fear that firmware is
1850 * overwritting information in the buffer.
1851 */
1852static long
1853_ctl_diag_release(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1854{
1855	struct mpt2_diag_release karg;
1856	void *request_data;
1857	int rc;
1858	u8 buffer_type;
1859	u8 issue_reset = 0;
1860
1861	if (copy_from_user(&karg, arg, sizeof(karg))) {
1862		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1863		    __FILE__, __LINE__, __func__);
1864		return -EFAULT;
1865	}
1866
1867	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1868	    __func__));
1869
1870	buffer_type = karg.unique_id & 0x000000ff;
1871	if (!_ctl_diag_capability(ioc, buffer_type)) {
1872		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1873		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1874		return -EPERM;
1875	}
1876
1877	if ((ioc->diag_buffer_status[buffer_type] &
1878	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1879		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1880		    "registered\n", ioc->name, __func__, buffer_type);
1881		return -EINVAL;
1882	}
1883
1884	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1885		printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1886		    "registered\n", ioc->name, __func__, karg.unique_id);
1887		return -EINVAL;
1888	}
1889
1890	if (ioc->diag_buffer_status[buffer_type] &
1891	    MPT2_DIAG_BUFFER_IS_RELEASED) {
1892		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1893		    "is already released\n", ioc->name, __func__,
1894		    buffer_type);
1895		return 0;
1896	}
1897
1898	request_data = ioc->diag_buffer[buffer_type];
1899
1900	if (!request_data) {
1901		printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1902		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1903		return -ENOMEM;
1904	}
1905
1906	/* buffers were released by due to host reset */
1907	if ((ioc->diag_buffer_status[buffer_type] &
1908	    MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1909		ioc->diag_buffer_status[buffer_type] |=
1910		    MPT2_DIAG_BUFFER_IS_RELEASED;
1911		ioc->diag_buffer_status[buffer_type] &=
1912		    ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1913		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1914		    "was released due to host reset\n", ioc->name, __func__,
1915		    buffer_type);
1916		return 0;
1917	}
1918
1919	rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1920
1921	if (issue_reset)
1922		mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1923		    FORCE_BIG_HAMMER);
1924
1925	return rc;
1926}
1927
1928/**
1929 * _ctl_diag_read_buffer - request for copy of the diag buffer
1930 * @ioc: per adapter object
1931 * @arg - user space buffer containing ioctl content
1932 */
1933static long
1934_ctl_diag_read_buffer(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1935{
1936	struct mpt2_diag_read_buffer karg;
1937	struct mpt2_diag_read_buffer __user *uarg = arg;
1938	void *request_data, *diag_data;
1939	Mpi2DiagBufferPostRequest_t *mpi_request;
1940	Mpi2DiagBufferPostReply_t *mpi_reply;
1941	int rc, i;
1942	u8 buffer_type;
1943	unsigned long timeleft, request_size, copy_size;
1944	u16 smid;
1945	u16 ioc_status;
1946	u8 issue_reset = 0;
1947
1948	if (copy_from_user(&karg, arg, sizeof(karg))) {
1949		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1950		    __FILE__, __LINE__, __func__);
1951		return -EFAULT;
1952	}
1953
1954	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1955	    __func__));
1956
1957	buffer_type = karg.unique_id & 0x000000ff;
1958	if (!_ctl_diag_capability(ioc, buffer_type)) {
1959		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1960		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1961		return -EPERM;
1962	}
1963
1964	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1965		printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1966		    "registered\n", ioc->name, __func__, karg.unique_id);
1967		return -EINVAL;
1968	}
1969
1970	request_data = ioc->diag_buffer[buffer_type];
1971	if (!request_data) {
1972		printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1973		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1974		return -ENOMEM;
1975	}
1976
1977	request_size = ioc->diag_buffer_sz[buffer_type];
1978
1979	if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1980		printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1981		    "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1982		    __func__);
1983		return -EINVAL;
1984	}
1985
1986	if (karg.starting_offset > request_size)
1987		return -EINVAL;
1988
1989	diag_data = (void *)(request_data + karg.starting_offset);
1990	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
1991	    "offset(%d), sz(%d)\n", ioc->name, __func__,
1992	    diag_data, karg.starting_offset, karg.bytes_to_read));
1993
1994	/* Truncate data on requests that are too large */
1995	if ((diag_data + karg.bytes_to_read < diag_data) ||
1996	    (diag_data + karg.bytes_to_read > request_data + request_size))
1997		copy_size = request_size - karg.starting_offset;
1998	else
1999		copy_size = karg.bytes_to_read;
2000
2001	if (copy_to_user((void __user *)uarg->diagnostic_data,
2002	    diag_data, copy_size)) {
2003		printk(MPT2SAS_ERR_FMT "%s: Unable to write "
2004		    "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
2005		    __func__, diag_data);
2006		return -EFAULT;
2007	}
2008
2009	if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
2010		return 0;
2011
2012	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
2013		"buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
2014	if ((ioc->diag_buffer_status[buffer_type] &
2015	    MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
2016		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2017		    "buffer_type(0x%02x) is still registered\n", ioc->name,
2018		     __func__, buffer_type));
2019		return 0;
2020	}
2021	/* Get a free request frame and save the message context.
2022	*/
2023
2024	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
2025		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
2026		    ioc->name, __func__);
2027		rc = -EAGAIN;
2028		goto out;
2029	}
2030
2031	smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2032	if (!smid) {
2033		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2034		    ioc->name, __func__);
2035		rc = -EAGAIN;
2036		goto out;
2037	}
2038
2039	rc = 0;
2040	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
2041	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2042	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2043	ioc->ctl_cmds.smid = smid;
2044
2045	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2046	mpi_request->BufferType = buffer_type;
2047	mpi_request->BufferLength =
2048	    cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2049	mpi_request->BufferAddress =
2050	    cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2051	for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
2052		mpi_request->ProductSpecific[i] =
2053			cpu_to_le32(ioc->product_specific[buffer_type][i]);
2054	mpi_request->VF_ID = 0; /* TODO */
2055	mpi_request->VP_ID = 0;
2056
2057	init_completion(&ioc->ctl_cmds.done);
2058	mpt2sas_base_put_smid_default(ioc, smid);
2059	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2060	    MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
2061
2062	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
2063		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
2064		    __func__);
2065		_debug_dump_mf(mpi_request,
2066		    sizeof(Mpi2DiagBufferPostRequest_t)/4);
2067		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
2068			issue_reset = 1;
2069		goto issue_host_reset;
2070	}
2071
2072	/* process the completed Reply Message Frame */
2073	if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
2074		printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
2075		    ioc->name, __func__);
2076		rc = -EFAULT;
2077		goto out;
2078	}
2079
2080	mpi_reply = ioc->ctl_cmds.reply;
2081	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2082
2083	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2084		ioc->diag_buffer_status[buffer_type] |=
2085		    MPT2_DIAG_BUFFER_IS_REGISTERED;
2086		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
2087		    ioc->name, __func__));
2088	} else {
2089		printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
2090		    "log_info(0x%08x)\n", ioc->name, __func__,
2091		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2092		rc = -EFAULT;
2093	}
2094
2095 issue_host_reset:
2096	if (issue_reset)
2097		mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2098		    FORCE_BIG_HAMMER);
2099
2100 out:
2101
2102	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
2103	return rc;
2104}
2105
2106
2107#ifdef CONFIG_COMPAT
2108/**
2109 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2110 * @ioc: per adapter object
2111 * @cmd - ioctl opcode
2112 * @arg - (struct mpt2_ioctl_command32)
2113 *
2114 * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2115 */
2116static long
2117_ctl_compat_mpt_command(struct MPT2SAS_ADAPTER *ioc, unsigned cmd,
2118	void __user *arg)
2119{
2120	struct mpt2_ioctl_command32 karg32;
2121	struct mpt2_ioctl_command32 __user *uarg;
2122	struct mpt2_ioctl_command karg;
2123
2124	if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2125		return -EINVAL;
2126
2127	uarg = (struct mpt2_ioctl_command32 __user *) arg;
2128
2129	if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2130		printk(KERN_ERR "failure at %s:%d/%s()!\n",
2131		    __FILE__, __LINE__, __func__);
2132		return -EFAULT;
2133	}
2134
2135	memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2136	karg.hdr.ioc_number = karg32.hdr.ioc_number;
2137	karg.hdr.port_number = karg32.hdr.port_number;
2138	karg.hdr.max_data_size = karg32.hdr.max_data_size;
2139	karg.timeout = karg32.timeout;
2140	karg.max_reply_bytes = karg32.max_reply_bytes;
2141	karg.data_in_size = karg32.data_in_size;
2142	karg.data_out_size = karg32.data_out_size;
2143	karg.max_sense_bytes = karg32.max_sense_bytes;
2144	karg.data_sge_offset = karg32.data_sge_offset;
2145	karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2146	karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2147	karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2148	karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2149	return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2150}
2151#endif
2152
2153/**
2154 * _ctl_ioctl_main - main ioctl entry point
2155 * @file - (struct file)
2156 * @cmd - ioctl opcode
2157 * @arg -
2158 * compat - handles 32 bit applications in 64bit os
2159 */
2160static long
2161_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2162	u8 compat)
2163{
2164	struct MPT2SAS_ADAPTER *ioc;
2165	struct mpt2_ioctl_header ioctl_header;
2166	enum block_state state;
2167	long ret = -EINVAL;
2168
2169	/* get IOCTL header */
2170	if (copy_from_user(&ioctl_header, (char __user *)arg,
2171	    sizeof(struct mpt2_ioctl_header))) {
2172		printk(KERN_ERR "failure at %s:%d/%s()!\n",
2173		    __FILE__, __LINE__, __func__);
2174		return -EFAULT;
2175	}
2176
2177	if (_ctl_verify_adapter(ioctl_header.ioc_number, &ioc) == -1 || !ioc)
2178		return -ENODEV;
2179	if (ioc->shost_recovery || ioc->pci_error_recovery ||
2180	    ioc->is_driver_loading)
2181		return -EAGAIN;
2182
2183	state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2184	if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
2185		return -EAGAIN;
2186	else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
 
2187		return -ERESTARTSYS;
 
2188
2189	switch (cmd) {
2190	case MPT2IOCINFO:
2191		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
2192			ret = _ctl_getiocinfo(ioc, arg);
2193		break;
2194#ifdef CONFIG_COMPAT
2195	case MPT2COMMAND32:
2196#endif
2197	case MPT2COMMAND:
2198	{
2199		struct mpt2_ioctl_command __user *uarg;
2200		struct mpt2_ioctl_command karg;
2201#ifdef CONFIG_COMPAT
2202		if (compat) {
2203			ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2204			break;
2205		}
2206#endif
2207		if (copy_from_user(&karg, arg, sizeof(karg))) {
2208			printk(KERN_ERR "failure at %s:%d/%s()!\n",
2209			    __FILE__, __LINE__, __func__);
2210			ret = -EFAULT;
2211			break;
2212		}
2213
2214		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2215			uarg = arg;
2216			ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2217		}
2218		break;
2219	}
2220	case MPT2EVENTQUERY:
2221		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2222			ret = _ctl_eventquery(ioc, arg);
2223		break;
2224	case MPT2EVENTENABLE:
2225		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2226			ret = _ctl_eventenable(ioc, arg);
2227		break;
2228	case MPT2EVENTREPORT:
2229		ret = _ctl_eventreport(ioc, arg);
2230		break;
2231	case MPT2HARDRESET:
2232		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2233			ret = _ctl_do_reset(ioc, arg);
2234		break;
2235	case MPT2BTDHMAPPING:
2236		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2237			ret = _ctl_btdh_mapping(ioc, arg);
2238		break;
2239	case MPT2DIAGREGISTER:
2240		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2241			ret = _ctl_diag_register(ioc, arg);
2242		break;
2243	case MPT2DIAGUNREGISTER:
2244		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2245			ret = _ctl_diag_unregister(ioc, arg);
2246		break;
2247	case MPT2DIAGQUERY:
2248		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2249			ret = _ctl_diag_query(ioc, arg);
2250		break;
2251	case MPT2DIAGRELEASE:
2252		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2253			ret = _ctl_diag_release(ioc, arg);
2254		break;
2255	case MPT2DIAGREADBUFFER:
2256		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2257			ret = _ctl_diag_read_buffer(ioc, arg);
2258		break;
2259	default:
2260
2261		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
2262		    "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2263		break;
2264	}
2265
2266	mutex_unlock(&ioc->ctl_cmds.mutex);
2267	return ret;
2268}
2269
2270/**
2271 * _ctl_ioctl - main ioctl entry point (unlocked)
2272 * @file - (struct file)
2273 * @cmd - ioctl opcode
2274 * @arg -
2275 */
2276static long
2277_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2278{
2279	long ret;
2280
2281	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0);
2282	return ret;
2283}
2284#ifdef CONFIG_COMPAT
2285/**
2286 * _ctl_ioctl_compat - main ioctl entry point (compat)
2287 * @file -
2288 * @cmd -
2289 * @arg -
2290 *
2291 * This routine handles 32 bit applications in 64bit os.
2292 */
2293static long
2294_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2295{
2296	long ret;
2297
2298	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1);
2299	return ret;
2300}
2301#endif
2302
2303/* scsi host attributes */
2304
2305/**
2306 * _ctl_version_fw_show - firmware version
2307 * @cdev - pointer to embedded class device
2308 * @buf - the buffer returned
2309 *
2310 * A sysfs 'read-only' shost attribute.
2311 */
2312static ssize_t
2313_ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2314    char *buf)
2315{
2316	struct Scsi_Host *shost = class_to_shost(cdev);
2317	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2318
2319	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2320	    (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2321	    (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2322	    (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2323	    ioc->facts.FWVersion.Word & 0x000000FF);
2324}
2325static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2326
2327/**
2328 * _ctl_version_bios_show - bios version
2329 * @cdev - pointer to embedded class device
2330 * @buf - the buffer returned
2331 *
2332 * A sysfs 'read-only' shost attribute.
2333 */
2334static ssize_t
2335_ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2336    char *buf)
2337{
2338	struct Scsi_Host *shost = class_to_shost(cdev);
2339	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2340
2341	u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2342
2343	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2344	    (version & 0xFF000000) >> 24,
2345	    (version & 0x00FF0000) >> 16,
2346	    (version & 0x0000FF00) >> 8,
2347	    version & 0x000000FF);
2348}
2349static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2350
2351/**
2352 * _ctl_version_mpi_show - MPI (message passing interface) version
2353 * @cdev - pointer to embedded class device
2354 * @buf - the buffer returned
2355 *
2356 * A sysfs 'read-only' shost attribute.
2357 */
2358static ssize_t
2359_ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2360    char *buf)
2361{
2362	struct Scsi_Host *shost = class_to_shost(cdev);
2363	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2364
2365	return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2366	    ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2367}
2368static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2369
2370/**
2371 * _ctl_version_product_show - product name
2372 * @cdev - pointer to embedded class device
2373 * @buf - the buffer returned
2374 *
2375 * A sysfs 'read-only' shost attribute.
2376 */
2377static ssize_t
2378_ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2379    char *buf)
2380{
2381	struct Scsi_Host *shost = class_to_shost(cdev);
2382	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2383
2384	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2385}
2386static DEVICE_ATTR(version_product, S_IRUGO,
2387   _ctl_version_product_show, NULL);
2388
2389/**
2390 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2391 * @cdev - pointer to embedded class device
2392 * @buf - the buffer returned
2393 *
2394 * A sysfs 'read-only' shost attribute.
2395 */
2396static ssize_t
2397_ctl_version_nvdata_persistent_show(struct device *cdev,
2398    struct device_attribute *attr, char *buf)
2399{
2400	struct Scsi_Host *shost = class_to_shost(cdev);
2401	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2402
2403	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2404	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2405}
2406static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2407    _ctl_version_nvdata_persistent_show, NULL);
2408
2409/**
2410 * _ctl_version_nvdata_default_show - nvdata default version
2411 * @cdev - pointer to embedded class device
2412 * @buf - the buffer returned
2413 *
2414 * A sysfs 'read-only' shost attribute.
2415 */
2416static ssize_t
2417_ctl_version_nvdata_default_show(struct device *cdev,
2418    struct device_attribute *attr, char *buf)
2419{
2420	struct Scsi_Host *shost = class_to_shost(cdev);
2421	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2422
2423	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2424	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2425}
2426static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2427    _ctl_version_nvdata_default_show, NULL);
2428
2429/**
2430 * _ctl_board_name_show - board name
2431 * @cdev - pointer to embedded class device
2432 * @buf - the buffer returned
2433 *
2434 * A sysfs 'read-only' shost attribute.
2435 */
2436static ssize_t
2437_ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2438    char *buf)
2439{
2440	struct Scsi_Host *shost = class_to_shost(cdev);
2441	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2442
2443	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2444}
2445static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2446
2447/**
2448 * _ctl_board_assembly_show - board assembly name
2449 * @cdev - pointer to embedded class device
2450 * @buf - the buffer returned
2451 *
2452 * A sysfs 'read-only' shost attribute.
2453 */
2454static ssize_t
2455_ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2456    char *buf)
2457{
2458	struct Scsi_Host *shost = class_to_shost(cdev);
2459	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2460
2461	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2462}
2463static DEVICE_ATTR(board_assembly, S_IRUGO,
2464    _ctl_board_assembly_show, NULL);
2465
2466/**
2467 * _ctl_board_tracer_show - board tracer number
2468 * @cdev - pointer to embedded class device
2469 * @buf - the buffer returned
2470 *
2471 * A sysfs 'read-only' shost attribute.
2472 */
2473static ssize_t
2474_ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2475    char *buf)
2476{
2477	struct Scsi_Host *shost = class_to_shost(cdev);
2478	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2479
2480	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2481}
2482static DEVICE_ATTR(board_tracer, S_IRUGO,
2483    _ctl_board_tracer_show, NULL);
2484
2485/**
2486 * _ctl_io_delay_show - io missing delay
2487 * @cdev - pointer to embedded class device
2488 * @buf - the buffer returned
2489 *
2490 * This is for firmware implemention for deboucing device
2491 * removal events.
2492 *
2493 * A sysfs 'read-only' shost attribute.
2494 */
2495static ssize_t
2496_ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2497    char *buf)
2498{
2499	struct Scsi_Host *shost = class_to_shost(cdev);
2500	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2501
2502	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2503}
2504static DEVICE_ATTR(io_delay, S_IRUGO,
2505    _ctl_io_delay_show, NULL);
2506
2507/**
2508 * _ctl_device_delay_show - device missing delay
2509 * @cdev - pointer to embedded class device
2510 * @buf - the buffer returned
2511 *
2512 * This is for firmware implemention for deboucing device
2513 * removal events.
2514 *
2515 * A sysfs 'read-only' shost attribute.
2516 */
2517static ssize_t
2518_ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2519    char *buf)
2520{
2521	struct Scsi_Host *shost = class_to_shost(cdev);
2522	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2523
2524	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2525}
2526static DEVICE_ATTR(device_delay, S_IRUGO,
2527    _ctl_device_delay_show, NULL);
2528
2529/**
2530 * _ctl_fw_queue_depth_show - global credits
2531 * @cdev - pointer to embedded class device
2532 * @buf - the buffer returned
2533 *
2534 * This is firmware queue depth limit
2535 *
2536 * A sysfs 'read-only' shost attribute.
2537 */
2538static ssize_t
2539_ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2540    char *buf)
2541{
2542	struct Scsi_Host *shost = class_to_shost(cdev);
2543	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2544
2545	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2546}
2547static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2548    _ctl_fw_queue_depth_show, NULL);
2549
2550/**
2551 * _ctl_sas_address_show - sas address
2552 * @cdev - pointer to embedded class device
2553 * @buf - the buffer returned
2554 *
2555 * This is the controller sas address
2556 *
2557 * A sysfs 'read-only' shost attribute.
2558 */
2559static ssize_t
2560_ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2561    char *buf)
2562{
2563	struct Scsi_Host *shost = class_to_shost(cdev);
2564	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2565
2566	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2567	    (unsigned long long)ioc->sas_hba.sas_address);
2568}
2569static DEVICE_ATTR(host_sas_address, S_IRUGO,
2570    _ctl_host_sas_address_show, NULL);
2571
2572/**
2573 * _ctl_logging_level_show - logging level
2574 * @cdev - pointer to embedded class device
2575 * @buf - the buffer returned
2576 *
2577 * A sysfs 'read/write' shost attribute.
2578 */
2579static ssize_t
2580_ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2581    char *buf)
2582{
2583	struct Scsi_Host *shost = class_to_shost(cdev);
2584	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2585
2586	return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2587}
2588static ssize_t
2589_ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2590    const char *buf, size_t count)
2591{
2592	struct Scsi_Host *shost = class_to_shost(cdev);
2593	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2594	int val = 0;
2595
2596	if (sscanf(buf, "%x", &val) != 1)
2597		return -EINVAL;
2598
2599	ioc->logging_level = val;
2600	printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2601	    ioc->logging_level);
2602	return strlen(buf);
2603}
2604static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2605    _ctl_logging_level_show, _ctl_logging_level_store);
2606
2607/* device attributes */
2608/*
2609 * _ctl_fwfault_debug_show - show/store fwfault_debug
2610 * @cdev - pointer to embedded class device
2611 * @buf - the buffer returned
2612 *
2613 * mpt2sas_fwfault_debug is command line option
2614 * A sysfs 'read/write' shost attribute.
2615 */
2616static ssize_t
2617_ctl_fwfault_debug_show(struct device *cdev,
2618    struct device_attribute *attr, char *buf)
2619{
2620	struct Scsi_Host *shost = class_to_shost(cdev);
2621	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2622
2623	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2624}
2625static ssize_t
2626_ctl_fwfault_debug_store(struct device *cdev,
2627    struct device_attribute *attr, const char *buf, size_t count)
2628{
2629	struct Scsi_Host *shost = class_to_shost(cdev);
2630	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2631	int val = 0;
2632
2633	if (sscanf(buf, "%d", &val) != 1)
2634		return -EINVAL;
2635
2636	ioc->fwfault_debug = val;
2637	printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
2638	    ioc->fwfault_debug);
2639	return strlen(buf);
2640}
2641static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2642    _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2643
2644
2645/**
2646 * _ctl_ioc_reset_count_show - ioc reset count
2647 * @cdev - pointer to embedded class device
2648 * @buf - the buffer returned
2649 *
2650 * This is firmware queue depth limit
2651 *
2652 * A sysfs 'read-only' shost attribute.
2653 */
2654static ssize_t
2655_ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2656    char *buf)
2657{
2658	struct Scsi_Host *shost = class_to_shost(cdev);
2659	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2660
2661	return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
2662}
2663static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
2664    _ctl_ioc_reset_count_show, NULL);
2665
2666/**
2667 * _ctl_ioc_reply_queue_count_show - number of reply queues
2668 * @cdev - pointer to embedded class device
2669 * @buf - the buffer returned
2670 *
2671 * This is number of reply queues
2672 *
2673 * A sysfs 'read-only' shost attribute.
2674 */
2675static ssize_t
2676_ctl_ioc_reply_queue_count_show(struct device *cdev,
2677	 struct device_attribute *attr, char *buf)
2678{
2679	u8 reply_queue_count;
2680	struct Scsi_Host *shost = class_to_shost(cdev);
2681	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2682
2683	if ((ioc->facts.IOCCapabilities &
2684	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2685		reply_queue_count = ioc->reply_queue_count;
2686	else
2687		reply_queue_count = 1;
2688	return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2689}
2690static DEVICE_ATTR(reply_queue_count, S_IRUGO,
2691	 _ctl_ioc_reply_queue_count_show, NULL);
2692
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2693struct DIAG_BUFFER_START {
2694	__le32 Size;
2695	__le32 DiagVersion;
2696	u8 BufferType;
2697	u8 Reserved[3];
2698	__le32 Reserved1;
2699	__le32 Reserved2;
2700	__le32 Reserved3;
2701};
2702/**
2703 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2704 * @cdev - pointer to embedded class device
2705 * @buf - the buffer returned
2706 *
2707 * A sysfs 'read-only' shost attribute.
2708 */
2709static ssize_t
2710_ctl_host_trace_buffer_size_show(struct device *cdev,
2711    struct device_attribute *attr, char *buf)
2712{
2713	struct Scsi_Host *shost = class_to_shost(cdev);
2714	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2715	u32 size = 0;
2716	struct DIAG_BUFFER_START *request_data;
2717
2718	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2719		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2720		    "registered\n", ioc->name, __func__);
2721		return 0;
2722	}
2723
2724	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2725	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2726		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2727		    "registered\n", ioc->name, __func__);
2728		return 0;
2729	}
2730
2731	request_data = (struct DIAG_BUFFER_START *)
2732	    ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2733	if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2734	    le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
2735	    le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2736		size = le32_to_cpu(request_data->Size);
2737
2738	ioc->ring_buffer_sz = size;
2739	return snprintf(buf, PAGE_SIZE, "%d\n", size);
2740}
2741static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2742	 _ctl_host_trace_buffer_size_show, NULL);
2743
2744/**
2745 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2746 * @cdev - pointer to embedded class device
2747 * @buf - the buffer returned
2748 *
2749 * A sysfs 'read/write' shost attribute.
2750 *
2751 * You will only be able to read 4k bytes of ring buffer at a time.
2752 * In order to read beyond 4k bytes, you will have to write out the
2753 * offset to the same attribute, it will move the pointer.
2754 */
2755static ssize_t
2756_ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2757     char *buf)
2758{
2759	struct Scsi_Host *shost = class_to_shost(cdev);
2760	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2761	void *request_data;
2762	u32 size;
2763
2764	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2765		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2766		    "registered\n", ioc->name, __func__);
2767		return 0;
2768	}
2769
2770	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2771	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2772		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2773		    "registered\n", ioc->name, __func__);
2774		return 0;
2775	}
2776
2777	if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2778		return 0;
2779
2780	size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2781	size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
2782	request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2783	memcpy(buf, request_data, size);
2784	return size;
2785}
2786
2787static ssize_t
2788_ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2789    const char *buf, size_t count)
2790{
2791	struct Scsi_Host *shost = class_to_shost(cdev);
2792	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2793	int val = 0;
2794
2795	if (sscanf(buf, "%d", &val) != 1)
2796		return -EINVAL;
2797
2798	ioc->ring_buffer_offset = val;
2799	return strlen(buf);
2800}
2801static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2802    _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2803
2804/*****************************************/
2805
2806/**
2807 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2808 * @cdev - pointer to embedded class device
2809 * @buf - the buffer returned
2810 *
2811 * A sysfs 'read/write' shost attribute.
2812 *
2813 * This is a mechnism to post/release host_trace_buffers
2814 */
2815static ssize_t
2816_ctl_host_trace_buffer_enable_show(struct device *cdev,
2817    struct device_attribute *attr, char *buf)
2818{
2819	struct Scsi_Host *shost = class_to_shost(cdev);
2820	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2821
2822	if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
2823	   ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2824	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
2825		return snprintf(buf, PAGE_SIZE, "off\n");
2826	else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2827	    MPT2_DIAG_BUFFER_IS_RELEASED))
2828		return snprintf(buf, PAGE_SIZE, "release\n");
2829	else
2830		return snprintf(buf, PAGE_SIZE, "post\n");
2831}
2832
2833static ssize_t
2834_ctl_host_trace_buffer_enable_store(struct device *cdev,
2835    struct device_attribute *attr, const char *buf, size_t count)
2836{
2837	struct Scsi_Host *shost = class_to_shost(cdev);
2838	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2839	char str[10] = "";
2840	struct mpt2_diag_register diag_register;
2841	u8 issue_reset = 0;
2842
2843	if (sscanf(buf, "%9s", str) != 1)
2844		return -EINVAL;
2845
2846	if (!strcmp(str, "post")) {
2847		/* exit out if host buffers are already posted */
2848		if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
2849		    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2850		    MPT2_DIAG_BUFFER_IS_REGISTERED) &&
2851		    ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2852		    MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
2853			goto out;
2854		memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
2855		printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
2856		    ioc->name);
2857		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
2858		diag_register.requested_buffer_size = (1024 * 1024);
2859		diag_register.unique_id = 0x7075900;
2860		ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
2861		_ctl_diag_register_2(ioc,  &diag_register);
2862	} else if (!strcmp(str, "release")) {
2863		/* exit out if host buffers are already released */
2864		if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
2865			goto out;
2866		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2867		    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
2868			goto out;
2869		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2870		    MPT2_DIAG_BUFFER_IS_RELEASED))
2871			goto out;
2872		printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
2873		    ioc->name);
2874		_ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
2875	}
2876
2877 out:
2878	return strlen(buf);
2879}
2880static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
2881    _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
2882
2883struct device_attribute *mpt2sas_host_attrs[] = {
2884	&dev_attr_version_fw,
2885	&dev_attr_version_bios,
2886	&dev_attr_version_mpi,
2887	&dev_attr_version_product,
2888	&dev_attr_version_nvdata_persistent,
2889	&dev_attr_version_nvdata_default,
2890	&dev_attr_board_name,
2891	&dev_attr_board_assembly,
2892	&dev_attr_board_tracer,
2893	&dev_attr_io_delay,
2894	&dev_attr_device_delay,
2895	&dev_attr_logging_level,
2896	&dev_attr_fwfault_debug,
2897	&dev_attr_fw_queue_depth,
2898	&dev_attr_host_sas_address,
2899	&dev_attr_ioc_reset_count,
2900	&dev_attr_host_trace_buffer_size,
2901	&dev_attr_host_trace_buffer,
2902	&dev_attr_host_trace_buffer_enable,
2903	&dev_attr_reply_queue_count,
 
2904	NULL,
2905};
2906
2907/**
2908 * _ctl_device_sas_address_show - sas address
2909 * @cdev - pointer to embedded class device
2910 * @buf - the buffer returned
2911 *
2912 * This is the sas address for the target
2913 *
2914 * A sysfs 'read-only' shost attribute.
2915 */
2916static ssize_t
2917_ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2918    char *buf)
2919{
2920	struct scsi_device *sdev = to_scsi_device(dev);
2921	struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2922
2923	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2924	    (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2925}
2926static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2927
2928/**
2929 * _ctl_device_handle_show - device handle
2930 * @cdev - pointer to embedded class device
2931 * @buf - the buffer returned
2932 *
2933 * This is the firmware assigned device handle
2934 *
2935 * A sysfs 'read-only' shost attribute.
2936 */
2937static ssize_t
2938_ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
2939    char *buf)
2940{
2941	struct scsi_device *sdev = to_scsi_device(dev);
2942	struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2943
2944	return snprintf(buf, PAGE_SIZE, "0x%04x\n",
2945	    sas_device_priv_data->sas_target->handle);
2946}
2947static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
2948
2949struct device_attribute *mpt2sas_dev_attrs[] = {
2950	&dev_attr_sas_address,
2951	&dev_attr_sas_device_handle,
2952	NULL,
2953};
2954
2955static const struct file_operations ctl_fops = {
2956	.owner = THIS_MODULE,
2957	.unlocked_ioctl = _ctl_ioctl,
2958	.release = _ctl_release,
2959	.poll = _ctl_poll,
2960	.fasync = _ctl_fasync,
2961#ifdef CONFIG_COMPAT
2962	.compat_ioctl = _ctl_ioctl_compat,
2963#endif
2964	.llseek = noop_llseek,
2965};
2966
2967static struct miscdevice ctl_dev = {
2968	.minor  = MPT2SAS_MINOR,
2969	.name   = MPT2SAS_DEV_NAME,
2970	.fops   = &ctl_fops,
2971};
2972
2973/**
2974 * mpt2sas_ctl_init - main entry point for ctl.
2975 *
2976 */
2977void
2978mpt2sas_ctl_init(void)
2979{
2980	async_queue = NULL;
2981	if (misc_register(&ctl_dev) < 0)
2982		printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
2983		    MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
2984
2985	init_waitqueue_head(&ctl_poll_wait);
2986}
2987
2988/**
2989 * mpt2sas_ctl_exit - exit point for ctl
2990 *
2991 */
2992void
2993mpt2sas_ctl_exit(void)
2994{
2995	struct MPT2SAS_ADAPTER *ioc;
2996	int i;
2997
2998	list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
2999
3000		/* free memory associated to diag buffers */
3001		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3002			if (!ioc->diag_buffer[i])
3003				continue;
3004			pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3005			    ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3006			ioc->diag_buffer[i] = NULL;
3007			ioc->diag_buffer_status[i] = 0;
3008		}
3009
3010		kfree(ioc->event_log);
3011	}
3012	misc_deregister(&ctl_dev);
3013}
3014