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