2 * Management Module Support for MPT (Message Passing Technology) based
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)
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.
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.
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.
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
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,
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>
59 #include <linux/uaccess.h>
61 #include "mpt2sas_base.h"
62 #include "mpt2sas_ctl.h"
64 static struct fasync_struct *async_queue;
65 static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
67 static int _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type,
71 * enum block_state - blocking state
72 * @NON_BLOCKING: non blocking
75 * These states are for ioctls that need to wait for a response
76 * from firmware, so they probably require sleep.
83 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
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
92 * Function for displaying debug info helpfull when debugging issues
96 _ctl_display_some_debug(struct MPT2SAS_ADAPTER *ioc, u16 smid,
97 char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
99 Mpi2ConfigRequest_t *mpi_request;
102 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
105 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
106 switch (mpi_request->Function) {
107 case MPI2_FUNCTION_SCSI_IO_REQUEST:
109 Mpi2SCSIIORequest_t *scsi_request =
110 (Mpi2SCSIIORequest_t *)mpi_request;
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;
119 case MPI2_FUNCTION_SCSI_TASK_MGMT:
122 case MPI2_FUNCTION_IOC_INIT:
125 case MPI2_FUNCTION_IOC_FACTS:
128 case MPI2_FUNCTION_CONFIG:
130 Mpi2ConfigRequest_t *config_request =
131 (Mpi2ConfigRequest_t *)mpi_request;
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;
141 case MPI2_FUNCTION_PORT_FACTS:
144 case MPI2_FUNCTION_PORT_ENABLE:
145 desc = "port_enable";
147 case MPI2_FUNCTION_EVENT_NOTIFICATION:
148 desc = "event_notification";
150 case MPI2_FUNCTION_FW_DOWNLOAD:
151 desc = "fw_download";
153 case MPI2_FUNCTION_FW_UPLOAD:
156 case MPI2_FUNCTION_RAID_ACTION:
157 desc = "raid_action";
159 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
161 Mpi2SCSIIORequest_t *scsi_request =
162 (Mpi2SCSIIORequest_t *)mpi_request;
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;
171 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
172 desc = "sas_iounit_cntl";
174 case MPI2_FUNCTION_SATA_PASSTHROUGH:
177 case MPI2_FUNCTION_DIAG_BUFFER_POST:
178 desc = "diag_buffer_post";
180 case MPI2_FUNCTION_DIAG_RELEASE:
181 desc = "diag_release";
183 case MPI2_FUNCTION_SMP_PASSTHROUGH:
184 desc = "smp_passthrough";
191 printk(MPT2SAS_DEBUG_FMT "%s: %s, smid(%d)\n",
192 ioc->name, calling_function_name, desc, smid);
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));
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);
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)
226 * The callback handler when using ioc->ctl_cb_idx.
231 mpt2sas_ctl_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
234 MPI2DefaultReply_t *mpi_reply;
236 if (ioc->ctl_cmds.status == MPT2_CMD_NOT_USED)
238 if (ioc->ctl_cmds.smid != smid)
240 ioc->ctl_cmds.status |= MPT2_CMD_COMPLETE;
241 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
243 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
244 ioc->ctl_cmds.status |= MPT2_CMD_REPLY_VALID;
246 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
247 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
249 ioc->ctl_cmds.status &= ~MPT2_CMD_PENDING;
250 complete(&ioc->ctl_cmds.done);
254 * _ctl_check_event_type - determines when an event needs logging
255 * @ioc: per adapter object
256 * @event: firmware event
258 * The bitmask in ioc->event_type[] indicates which events should be
259 * be saved in the driver event_log. This bitmask is set by application.
261 * Returns 1 when event should be captured, or zero means no match.
264 _ctl_check_event_type(struct MPT2SAS_ADAPTER *ioc, u16 event)
269 if (event >= 128 || !event || !ioc->event_log)
272 desired_event = (1 << (event % 32));
276 return desired_event & ioc->event_type[i];
280 * mpt2sas_ctl_add_to_event_log - add event
281 * @ioc: per adapter object
282 * @mpi_reply: reply message frame
287 mpt2sas_ctl_add_to_event_log(struct MPT2SAS_ADAPTER *ioc,
288 Mpi2EventNotificationReply_t *mpi_reply)
290 struct MPT2_IOCTL_EVENTS *event_log;
293 u32 sz, event_data_sz;
299 event = le16_to_cpu(mpi_reply->Event);
301 if (_ctl_check_event_type(ioc, event)) {
303 /* insert entry into circular event_log */
304 i = ioc->event_context % MPT2SAS_CTL_EVENT_LOG_SIZE;
305 event_log = ioc->event_log;
306 event_log[i].event = event;
307 event_log[i].context = ioc->event_context++;
309 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
310 sz = min_t(u32, event_data_sz, MPT2_EVENT_DATA_SIZE);
311 memset(event_log[i].data, 0, MPT2_EVENT_DATA_SIZE);
312 memcpy(event_log[i].data, mpi_reply->EventData, sz);
316 /* This aen_event_read_flag flag is set until the
317 * application has read the event log.
318 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
320 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
321 (send_aen && !ioc->aen_event_read_flag)) {
322 ioc->aen_event_read_flag = 1;
323 wake_up_interruptible(&ctl_poll_wait);
325 kill_fasync(&async_queue, SIGIO, POLL_IN);
330 * mpt2sas_ctl_event_callback - firmware event handler (called at ISR time)
331 * @ioc: per adapter object
332 * @msix_index: MSIX table index supplied by the OS
333 * @reply: reply message frame(lower 32bit addr)
334 * Context: interrupt.
336 * This function merely adds a new work task into ioc->firmware_event_thread.
337 * The tasks are worked from _firmware_event_work in user context.
342 mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
345 Mpi2EventNotificationReply_t *mpi_reply;
347 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
348 mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
352 * _ctl_verify_adapter - validates ioc_number passed from application
353 * @ioc: per adapter object
354 * @iocpp: The ioc pointer is returned in this.
356 * Return (-1) means error, else ioc_number.
359 _ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
361 struct MPT2SAS_ADAPTER *ioc;
363 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
364 if (ioc->id != ioc_number)
374 * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
375 * @ioc: per adapter object
376 * @reset_phase: phase
378 * The handler for doing any required cleanup or initialization.
380 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
381 * MPT2_IOC_DONE_RESET
384 mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
389 switch (reset_phase) {
390 case MPT2_IOC_PRE_RESET:
391 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
392 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
393 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
394 if (!(ioc->diag_buffer_status[i] &
395 MPT2_DIAG_BUFFER_IS_REGISTERED))
397 if ((ioc->diag_buffer_status[i] &
398 MPT2_DIAG_BUFFER_IS_RELEASED))
400 _ctl_send_release(ioc, i, &issue_reset);
403 case MPT2_IOC_AFTER_RESET:
404 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
405 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
406 if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
407 ioc->ctl_cmds.status |= MPT2_CMD_RESET;
408 mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
409 complete(&ioc->ctl_cmds.done);
412 case MPT2_IOC_DONE_RESET:
413 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
414 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
416 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
417 if (!(ioc->diag_buffer_status[i] &
418 MPT2_DIAG_BUFFER_IS_REGISTERED))
420 if ((ioc->diag_buffer_status[i] &
421 MPT2_DIAG_BUFFER_IS_RELEASED))
423 ioc->diag_buffer_status[i] |=
424 MPT2_DIAG_BUFFER_IS_DIAG_RESET;
436 * Called when application request fasyn callback handler.
439 _ctl_fasync(int fd, struct file *filep, int mode)
441 return fasync_helper(fd, filep, mode, &async_queue);
449 * Called when application releases the fasyn callback handler.
452 _ctl_release(struct inode *inode, struct file *filep)
454 return fasync_helper(-1, filep, 0, &async_queue);
464 _ctl_poll(struct file *filep, poll_table *wait)
466 struct MPT2SAS_ADAPTER *ioc;
468 poll_wait(filep, &ctl_poll_wait, wait);
470 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
471 if (ioc->aen_event_read_flag)
472 return POLLIN | POLLRDNORM;
478 * _ctl_set_task_mid - assign an active smid to tm request
479 * @ioc: per adapter object
480 * @karg - (struct mpt2_ioctl_command)
481 * @tm_request - pointer to mf from user space
483 * Returns 0 when an smid if found, else fail.
484 * during failure, the reply frame is filled.
487 _ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
488 Mpi2SCSITaskManagementRequest_t *tm_request)
493 struct scsi_cmnd *scmd;
494 struct MPT2SAS_DEVICE *priv_data;
496 Mpi2SCSITaskManagementReply_t *tm_reply;
501 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
503 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
508 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
510 handle = le16_to_cpu(tm_request->DevHandle);
511 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
512 for (i = ioc->request_depth; i && !found; i--) {
513 scmd = ioc->scsi_lookup[i - 1].scmd;
514 if (scmd == NULL || scmd->device == NULL ||
515 scmd->device->hostdata == NULL)
517 if (lun != scmd->device->lun)
519 priv_data = scmd->device->hostdata;
520 if (priv_data->sas_target == NULL)
522 if (priv_data->sas_target->handle != handle)
524 tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
527 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
530 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
531 "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
532 desc, tm_request->DevHandle, lun));
533 tm_reply = ioc->ctl_cmds.reply;
534 tm_reply->DevHandle = tm_request->DevHandle;
535 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
536 tm_reply->TaskType = tm_request->TaskType;
537 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
538 tm_reply->VP_ID = tm_request->VP_ID;
539 tm_reply->VF_ID = tm_request->VF_ID;
540 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
541 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
543 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
548 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
549 "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
550 desc, tm_request->DevHandle, lun, tm_request->TaskMID));
555 * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
556 * @ioc: per adapter object
557 * @karg - (struct mpt2_ioctl_command)
558 * @mf - pointer to mf in user space
559 * @state - NON_BLOCKING or BLOCKING
562 _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc,
563 struct mpt2_ioctl_command karg, void __user *mf, enum block_state state)
565 MPI2RequestHeader_t *mpi_request;
566 MPI2DefaultReply_t *mpi_reply;
570 unsigned long timeout, timeleft;
574 void *priv_sense = NULL;
575 void *data_out = NULL;
576 dma_addr_t data_out_dma;
577 size_t data_out_sz = 0;
578 void *data_in = NULL;
579 dma_addr_t data_in_dma;
580 size_t data_in_sz = 0;
583 u16 wait_state_count;
587 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
589 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
592 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
593 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
594 ioc->name, __func__);
599 wait_state_count = 0;
600 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
601 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
602 if (wait_state_count++ == 10) {
603 printk(MPT2SAS_ERR_FMT
604 "%s: failed due to ioc not operational\n",
605 ioc->name, __func__);
610 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
611 printk(MPT2SAS_INFO_FMT "%s: waiting for "
612 "operational state(count=%d)\n", ioc->name,
613 __func__, wait_state_count);
615 if (wait_state_count)
616 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
617 ioc->name, __func__);
619 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
621 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
622 ioc->name, __func__);
628 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
629 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
630 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
631 ioc->ctl_cmds.smid = smid;
632 data_out_sz = karg.data_out_size;
633 data_in_sz = karg.data_in_size;
635 /* copy in request message frame from user */
636 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
637 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
640 mpt2sas_base_free_smid(ioc, smid);
644 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
645 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
646 if (!mpi_request->FunctionDependent1 ||
647 mpi_request->FunctionDependent1 >
648 cpu_to_le16(ioc->facts.MaxDevHandle)) {
650 mpt2sas_base_free_smid(ioc, smid);
655 /* obtain dma-able memory for data transfer */
656 if (data_out_sz) /* WRITE */ {
657 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
660 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
663 mpt2sas_base_free_smid(ioc, smid);
666 if (copy_from_user(data_out, karg.data_out_buf_ptr,
668 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
671 mpt2sas_base_free_smid(ioc, smid);
676 if (data_in_sz) /* READ */ {
677 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
680 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
683 mpt2sas_base_free_smid(ioc, smid);
688 /* add scatter gather elements */
689 psge = (void *)mpi_request + (karg.data_sge_offset*4);
691 if (!data_out_sz && !data_in_sz) {
692 mpt2sas_base_build_zero_len_sge(ioc, psge);
693 } else if (data_out_sz && data_in_sz) {
694 /* WRITE sgel first */
695 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
696 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
697 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
698 ioc->base_add_sg_single(psge, sgl_flags |
699 data_out_sz, data_out_dma);
702 psge += ioc->sge_size;
705 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
706 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
707 MPI2_SGE_FLAGS_END_OF_LIST);
708 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
709 ioc->base_add_sg_single(psge, sgl_flags |
710 data_in_sz, data_in_dma);
711 } else if (data_out_sz) /* WRITE */ {
712 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
713 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
714 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
715 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
716 ioc->base_add_sg_single(psge, sgl_flags |
717 data_out_sz, data_out_dma);
718 } else if (data_in_sz) /* READ */ {
719 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
720 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
721 MPI2_SGE_FLAGS_END_OF_LIST);
722 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
723 ioc->base_add_sg_single(psge, sgl_flags |
724 data_in_sz, data_in_dma);
727 /* send command to firmware */
728 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
729 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
732 switch (mpi_request->Function) {
733 case MPI2_FUNCTION_SCSI_IO_REQUEST:
734 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
736 Mpi2SCSIIORequest_t *scsiio_request =
737 (Mpi2SCSIIORequest_t *)mpi_request;
738 scsiio_request->SenseBufferLowAddress =
739 (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
740 priv_sense = mpt2sas_base_get_sense_buffer(ioc, smid);
741 memset(priv_sense, 0, SCSI_SENSE_BUFFERSIZE);
742 mpt2sas_base_put_smid_scsi_io(ioc, smid,
743 le16_to_cpu(mpi_request->FunctionDependent1));
746 case MPI2_FUNCTION_SCSI_TASK_MGMT:
748 Mpi2SCSITaskManagementRequest_t *tm_request =
749 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
751 if (tm_request->TaskType ==
752 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
753 tm_request->TaskType ==
754 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
755 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
756 mpt2sas_base_free_smid(ioc, smid);
761 mutex_lock(&ioc->tm_cmds.mutex);
762 mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
763 tm_request->DevHandle));
764 mpt2sas_base_put_smid_hi_priority(ioc, smid);
767 case MPI2_FUNCTION_SMP_PASSTHROUGH:
769 Mpi2SmpPassthroughRequest_t *smp_request =
770 (Mpi2SmpPassthroughRequest_t *)mpi_request;
773 /* ioc determines which port to use */
774 smp_request->PhysicalPort = 0xFF;
775 if (smp_request->PassthroughFlags &
776 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
777 data = (u8 *)&smp_request->SGL;
781 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
782 ioc->ioc_link_reset_in_progress = 1;
783 ioc->ignore_loginfos = 1;
785 mpt2sas_base_put_smid_default(ioc, smid);
788 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
790 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
791 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
793 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
794 || sasiounit_request->Operation ==
795 MPI2_SAS_OP_PHY_LINK_RESET) {
796 ioc->ioc_link_reset_in_progress = 1;
797 ioc->ignore_loginfos = 1;
799 mpt2sas_base_put_smid_default(ioc, smid);
803 mpt2sas_base_put_smid_default(ioc, smid);
807 if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
808 timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
810 timeout = karg.timeout;
811 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
813 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
814 Mpi2SCSITaskManagementRequest_t *tm_request =
815 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
816 mutex_unlock(&ioc->tm_cmds.mutex);
817 mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
818 tm_request->DevHandle));
819 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
820 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
821 ioc->ioc_link_reset_in_progress) {
822 ioc->ioc_link_reset_in_progress = 0;
823 ioc->ignore_loginfos = 0;
825 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
826 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
828 _debug_dump_mf(mpi_request, karg.data_sge_offset);
829 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
831 goto issue_host_reset;
834 mpi_reply = ioc->ctl_cmds.reply;
835 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
837 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
838 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
839 (ioc->logging_level & MPT_DEBUG_TM)) {
840 Mpi2SCSITaskManagementReply_t *tm_reply =
841 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
843 printk(MPT2SAS_DEBUG_FMT "TASK_MGMT: "
844 "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
845 "TerminationCount(0x%08x)\n", ioc->name,
846 tm_reply->IOCStatus, tm_reply->IOCLogInfo,
847 tm_reply->TerminationCount);
850 /* copy out xdata to user */
852 if (copy_to_user(karg.data_in_buf_ptr, data_in,
854 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
861 /* copy out reply message frame to user */
862 if (karg.max_reply_bytes) {
863 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
864 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
866 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
873 /* copy out sense to user */
874 if (karg.max_sense_bytes && (mpi_request->Function ==
875 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
876 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
877 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
878 if (copy_to_user(karg.sense_data_ptr, priv_sense, sz)) {
879 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
888 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
889 mpi_request->Function ==
890 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
891 printk(MPT2SAS_INFO_FMT "issue target reset: handle "
892 "= (0x%04x)\n", ioc->name,
893 mpi_request->FunctionDependent1);
894 mutex_lock(&ioc->tm_cmds.mutex);
895 mpt2sas_scsih_issue_tm(ioc,
896 mpi_request->FunctionDependent1, 0,
897 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
898 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
899 mutex_unlock(&ioc->tm_cmds.mutex);
901 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
907 /* free memory associated with sg buffers */
909 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
913 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
916 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
917 mutex_unlock(&ioc->ctl_cmds.mutex);
922 * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
923 * @arg - user space buffer containing ioctl content
926 _ctl_getiocinfo(void __user *arg)
928 struct mpt2_ioctl_iocinfo karg;
929 struct MPT2SAS_ADAPTER *ioc;
932 if (copy_from_user(&karg, arg, sizeof(karg))) {
933 printk(KERN_ERR "failure at %s:%d/%s()!\n",
934 __FILE__, __LINE__, __func__);
937 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
940 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
943 memset(&karg, 0 , sizeof(karg));
944 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
946 karg.port_number = ioc->pfacts[0].PortNumber;
947 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
948 karg.hw_rev = revision;
949 karg.pci_id = ioc->pdev->device;
950 karg.subsystem_device = ioc->pdev->subsystem_device;
951 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
952 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
953 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
954 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
955 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
956 karg.firmware_version = ioc->facts.FWVersion.Word;
957 strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
958 strcat(karg.driver_version, "-");
959 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
960 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
962 if (copy_to_user(arg, &karg, sizeof(karg))) {
963 printk(KERN_ERR "failure at %s:%d/%s()!\n",
964 __FILE__, __LINE__, __func__);
971 * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
972 * @arg - user space buffer containing ioctl content
975 _ctl_eventquery(void __user *arg)
977 struct mpt2_ioctl_eventquery karg;
978 struct MPT2SAS_ADAPTER *ioc;
980 if (copy_from_user(&karg, arg, sizeof(karg))) {
981 printk(KERN_ERR "failure at %s:%d/%s()!\n",
982 __FILE__, __LINE__, __func__);
985 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
988 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
991 karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
992 memcpy(karg.event_types, ioc->event_type,
993 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
995 if (copy_to_user(arg, &karg, sizeof(karg))) {
996 printk(KERN_ERR "failure at %s:%d/%s()!\n",
997 __FILE__, __LINE__, __func__);
1004 * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1005 * @arg - user space buffer containing ioctl content
1008 _ctl_eventenable(void __user *arg)
1010 struct mpt2_ioctl_eventenable karg;
1011 struct MPT2SAS_ADAPTER *ioc;
1013 if (copy_from_user(&karg, arg, sizeof(karg))) {
1014 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1015 __FILE__, __LINE__, __func__);
1018 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1021 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1026 memcpy(ioc->event_type, karg.event_types,
1027 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1028 mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1030 /* initialize event_log */
1031 ioc->event_context = 0;
1032 ioc->aen_event_read_flag = 0;
1033 ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1034 sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1035 if (!ioc->event_log) {
1036 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1037 __FILE__, __LINE__, __func__);
1044 * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1045 * @arg - user space buffer containing ioctl content
1048 _ctl_eventreport(void __user *arg)
1050 struct mpt2_ioctl_eventreport karg;
1051 struct MPT2SAS_ADAPTER *ioc;
1052 u32 number_bytes, max_events, max;
1053 struct mpt2_ioctl_eventreport __user *uarg = arg;
1055 if (copy_from_user(&karg, arg, sizeof(karg))) {
1056 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1057 __FILE__, __LINE__, __func__);
1060 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1063 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1066 number_bytes = karg.hdr.max_data_size -
1067 sizeof(struct mpt2_ioctl_header);
1068 max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1069 max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1071 /* If fewer than 1 event is requested, there must have
1072 * been some type of error.
1074 if (!max || !ioc->event_log)
1077 number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1078 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1079 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1080 __FILE__, __LINE__, __func__);
1084 /* reset flag so SIGIO can restart */
1085 ioc->aen_event_read_flag = 0;
1090 * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1091 * @arg - user space buffer containing ioctl content
1094 _ctl_do_reset(void __user *arg)
1096 struct mpt2_ioctl_diag_reset karg;
1097 struct MPT2SAS_ADAPTER *ioc;
1100 if (copy_from_user(&karg, arg, sizeof(karg))) {
1101 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1102 __FILE__, __LINE__, __func__);
1105 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1108 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1111 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1113 printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1114 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1119 * _ctl_btdh_search_sas_device - searching for sas device
1120 * @ioc: per adapter object
1121 * @btdh: btdh ioctl payload
1124 _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1125 struct mpt2_ioctl_btdh_mapping *btdh)
1127 struct _sas_device *sas_device;
1128 unsigned long flags;
1131 if (list_empty(&ioc->sas_device_list))
1134 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1135 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1136 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1137 btdh->handle == sas_device->handle) {
1138 btdh->bus = sas_device->channel;
1139 btdh->id = sas_device->id;
1142 } else if (btdh->bus == sas_device->channel && btdh->id ==
1143 sas_device->id && btdh->handle == 0xFFFF) {
1144 btdh->handle = sas_device->handle;
1150 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1155 * _ctl_btdh_search_raid_device - searching for raid device
1156 * @ioc: per adapter object
1157 * @btdh: btdh ioctl payload
1160 _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1161 struct mpt2_ioctl_btdh_mapping *btdh)
1163 struct _raid_device *raid_device;
1164 unsigned long flags;
1167 if (list_empty(&ioc->raid_device_list))
1170 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1171 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1172 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1173 btdh->handle == raid_device->handle) {
1174 btdh->bus = raid_device->channel;
1175 btdh->id = raid_device->id;
1178 } else if (btdh->bus == raid_device->channel && btdh->id ==
1179 raid_device->id && btdh->handle == 0xFFFF) {
1180 btdh->handle = raid_device->handle;
1186 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1191 * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1192 * @arg - user space buffer containing ioctl content
1195 _ctl_btdh_mapping(void __user *arg)
1197 struct mpt2_ioctl_btdh_mapping karg;
1198 struct MPT2SAS_ADAPTER *ioc;
1201 if (copy_from_user(&karg, arg, sizeof(karg))) {
1202 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1203 __FILE__, __LINE__, __func__);
1206 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1209 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1212 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1214 _ctl_btdh_search_raid_device(ioc, &karg);
1216 if (copy_to_user(arg, &karg, sizeof(karg))) {
1217 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1218 __FILE__, __LINE__, __func__);
1225 * _ctl_diag_capability - return diag buffer capability
1226 * @ioc: per adapter object
1227 * @buffer_type: specifies either TRACE or SNAPSHOT
1229 * returns 1 when diag buffer support is enabled in firmware
1232 _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1236 switch (buffer_type) {
1237 case MPI2_DIAG_BUF_TYPE_TRACE:
1238 if (ioc->facts.IOCCapabilities &
1239 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1242 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1243 if (ioc->facts.IOCCapabilities &
1244 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1253 * _ctl_diag_register - application register with driver
1254 * @arg - user space buffer containing ioctl content
1255 * @state - NON_BLOCKING or BLOCKING
1257 * This will allow the driver to setup any required buffers that will be
1258 * needed by firmware to communicate with the driver.
1261 _ctl_diag_register(void __user *arg, enum block_state state)
1263 struct mpt2_diag_register karg;
1264 struct MPT2SAS_ADAPTER *ioc;
1266 void *request_data = NULL;
1267 dma_addr_t request_data_dma;
1268 u32 request_data_sz = 0;
1269 Mpi2DiagBufferPostRequest_t *mpi_request;
1270 Mpi2DiagBufferPostReply_t *mpi_reply;
1272 unsigned long timeleft;
1277 if (copy_from_user(&karg, arg, sizeof(karg))) {
1278 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1279 __FILE__, __LINE__, __func__);
1282 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1285 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1288 buffer_type = karg.buffer_type;
1289 if (!_ctl_diag_capability(ioc, buffer_type)) {
1290 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1291 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1295 if (ioc->diag_buffer_status[buffer_type] &
1296 MPT2_DIAG_BUFFER_IS_REGISTERED) {
1297 printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1298 "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1303 if (karg.requested_buffer_size % 4) {
1304 printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1305 "is not 4 byte aligned\n", ioc->name, __func__);
1309 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1311 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1312 return -ERESTARTSYS;
1314 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1315 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1316 ioc->name, __func__);
1321 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1323 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1324 ioc->name, __func__);
1330 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1331 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1332 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1333 ioc->ctl_cmds.smid = smid;
1335 request_data = ioc->diag_buffer[buffer_type];
1336 request_data_sz = karg.requested_buffer_size;
1337 ioc->unique_id[buffer_type] = karg.unique_id;
1338 ioc->diag_buffer_status[buffer_type] = 0;
1339 memcpy(ioc->product_specific[buffer_type], karg.product_specific,
1340 MPT2_PRODUCT_SPECIFIC_DWORDS);
1341 ioc->diagnostic_flags[buffer_type] = karg.diagnostic_flags;
1344 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1345 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1346 pci_free_consistent(ioc->pdev,
1347 ioc->diag_buffer_sz[buffer_type],
1348 request_data, request_data_dma);
1349 request_data = NULL;
1353 if (request_data == NULL) {
1354 ioc->diag_buffer_sz[buffer_type] = 0;
1355 ioc->diag_buffer_dma[buffer_type] = 0;
1356 request_data = pci_alloc_consistent(
1357 ioc->pdev, request_data_sz, &request_data_dma);
1358 if (request_data == NULL) {
1359 printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1360 " for diag buffers, requested size(%d)\n",
1361 ioc->name, __func__, request_data_sz);
1362 mpt2sas_base_free_smid(ioc, smid);
1365 ioc->diag_buffer[buffer_type] = request_data;
1366 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1367 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1370 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1371 mpi_request->BufferType = karg.buffer_type;
1372 mpi_request->Flags = cpu_to_le32(karg.diagnostic_flags);
1373 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1374 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1375 mpi_request->VF_ID = 0; /* TODO */
1376 mpi_request->VP_ID = 0;
1378 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: diag_buffer(0x%p), "
1379 "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1380 (unsigned long long)request_data_dma, mpi_request->BufferLength));
1382 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1383 mpi_request->ProductSpecific[i] =
1384 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1386 mpt2sas_base_put_smid_default(ioc, smid);
1387 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1388 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1390 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1391 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1393 _debug_dump_mf(mpi_request,
1394 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1395 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1397 goto issue_host_reset;
1400 /* process the completed Reply Message Frame */
1401 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1402 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1403 ioc->name, __func__);
1408 mpi_reply = ioc->ctl_cmds.reply;
1409 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1411 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1412 ioc->diag_buffer_status[buffer_type] |=
1413 MPT2_DIAG_BUFFER_IS_REGISTERED;
1414 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1415 ioc->name, __func__));
1417 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1418 "log_info(0x%08x)\n", ioc->name, __func__,
1419 ioc_status, mpi_reply->IOCLogInfo);
1425 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1430 if (rc && request_data)
1431 pci_free_consistent(ioc->pdev, request_data_sz,
1432 request_data, request_data_dma);
1434 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1435 mutex_unlock(&ioc->ctl_cmds.mutex);
1440 * _ctl_diag_unregister - application unregister with driver
1441 * @arg - user space buffer containing ioctl content
1443 * This will allow the driver to cleanup any memory allocated for diag
1444 * messages and to free up any resources.
1447 _ctl_diag_unregister(void __user *arg)
1449 struct mpt2_diag_unregister karg;
1450 struct MPT2SAS_ADAPTER *ioc;
1452 dma_addr_t request_data_dma;
1453 u32 request_data_sz;
1456 if (copy_from_user(&karg, arg, sizeof(karg))) {
1457 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1458 __FILE__, __LINE__, __func__);
1461 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1464 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1467 buffer_type = karg.unique_id & 0x000000ff;
1468 if (!_ctl_diag_capability(ioc, buffer_type)) {
1469 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1470 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1474 if ((ioc->diag_buffer_status[buffer_type] &
1475 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1476 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1477 "registered\n", ioc->name, __func__, buffer_type);
1480 if ((ioc->diag_buffer_status[buffer_type] &
1481 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1482 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1483 "released\n", ioc->name, __func__, buffer_type);
1487 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1488 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1489 "registered\n", ioc->name, __func__, karg.unique_id);
1493 request_data = ioc->diag_buffer[buffer_type];
1494 if (!request_data) {
1495 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1496 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1500 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1501 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1502 pci_free_consistent(ioc->pdev, request_data_sz,
1503 request_data, request_data_dma);
1504 ioc->diag_buffer[buffer_type] = NULL;
1505 ioc->diag_buffer_status[buffer_type] = 0;
1510 * _ctl_diag_query - query relevant info associated with diag buffers
1511 * @arg - user space buffer containing ioctl content
1513 * The application will send only buffer_type and unique_id. Driver will
1514 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1515 * 0x00, the driver will return info specified by Buffer Type.
1518 _ctl_diag_query(void __user *arg)
1520 struct mpt2_diag_query karg;
1521 struct MPT2SAS_ADAPTER *ioc;
1526 if (copy_from_user(&karg, arg, sizeof(karg))) {
1527 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1528 __FILE__, __LINE__, __func__);
1531 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1534 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1537 karg.application_flags = 0;
1538 buffer_type = karg.buffer_type;
1540 if (!_ctl_diag_capability(ioc, buffer_type)) {
1541 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1542 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1546 if ((ioc->diag_buffer_status[buffer_type] &
1547 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1548 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1549 "registered\n", ioc->name, __func__, buffer_type);
1553 if (karg.unique_id & 0xffffff00) {
1554 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1555 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1556 "registered\n", ioc->name, __func__,
1562 request_data = ioc->diag_buffer[buffer_type];
1563 if (!request_data) {
1564 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1565 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1569 if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1570 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1571 MPT2_APP_FLAGS_BUFFER_VALID);
1573 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1574 MPT2_APP_FLAGS_BUFFER_VALID |
1575 MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1577 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1578 karg.product_specific[i] =
1579 ioc->product_specific[buffer_type][i];
1581 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1582 karg.driver_added_buffer_size = 0;
1583 karg.unique_id = ioc->unique_id[buffer_type];
1584 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1586 if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1587 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1588 "data @ %p\n", ioc->name, __func__, arg);
1595 * _ctl_send_release - Diag Release Message
1596 * @ioc: per adapter object
1597 * @buffer_type - specifies either TRACE or SNAPSHOT
1598 * @issue_reset - specifies whether host reset is required.
1602 _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1604 Mpi2DiagReleaseRequest_t *mpi_request;
1605 Mpi2DiagReleaseReply_t *mpi_reply;
1610 unsigned long timeleft;
1612 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1618 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1619 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1620 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
1621 "skipping due to FAULT state\n", ioc->name,
1627 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1628 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1629 ioc->name, __func__);
1634 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1636 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1637 ioc->name, __func__);
1642 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1643 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1644 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1645 ioc->ctl_cmds.smid = smid;
1647 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1648 mpi_request->BufferType = buffer_type;
1649 mpi_request->VF_ID = 0; /* TODO */
1650 mpi_request->VP_ID = 0;
1652 mpt2sas_base_put_smid_default(ioc, smid);
1653 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1654 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1656 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1657 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1659 _debug_dump_mf(mpi_request,
1660 sizeof(Mpi2DiagReleaseRequest_t)/4);
1661 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1667 /* process the completed Reply Message Frame */
1668 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1669 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1670 ioc->name, __func__);
1675 mpi_reply = ioc->ctl_cmds.reply;
1676 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1678 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1679 ioc->diag_buffer_status[buffer_type] |=
1680 MPT2_DIAG_BUFFER_IS_RELEASED;
1681 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1682 ioc->name, __func__));
1684 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1685 "log_info(0x%08x)\n", ioc->name, __func__,
1686 ioc_status, mpi_reply->IOCLogInfo);
1691 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1696 * _ctl_diag_release - request to send Diag Release Message to firmware
1697 * @arg - user space buffer containing ioctl content
1698 * @state - NON_BLOCKING or BLOCKING
1700 * This allows ownership of the specified buffer to returned to the driver,
1701 * allowing an application to read the buffer without fear that firmware is
1702 * overwritting information in the buffer.
1705 _ctl_diag_release(void __user *arg, enum block_state state)
1707 struct mpt2_diag_release karg;
1708 struct MPT2SAS_ADAPTER *ioc;
1714 if (copy_from_user(&karg, arg, sizeof(karg))) {
1715 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1716 __FILE__, __LINE__, __func__);
1719 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1722 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1725 buffer_type = karg.unique_id & 0x000000ff;
1726 if (!_ctl_diag_capability(ioc, buffer_type)) {
1727 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1728 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1732 if ((ioc->diag_buffer_status[buffer_type] &
1733 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1734 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1735 "registered\n", ioc->name, __func__, buffer_type);
1739 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1740 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1741 "registered\n", ioc->name, __func__, karg.unique_id);
1745 if (ioc->diag_buffer_status[buffer_type] &
1746 MPT2_DIAG_BUFFER_IS_RELEASED) {
1747 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1748 "is already released\n", ioc->name, __func__,
1753 request_data = ioc->diag_buffer[buffer_type];
1755 if (!request_data) {
1756 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1757 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1761 /* buffers were released by due to host reset */
1762 if ((ioc->diag_buffer_status[buffer_type] &
1763 MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1764 ioc->diag_buffer_status[buffer_type] |=
1765 MPT2_DIAG_BUFFER_IS_RELEASED;
1766 ioc->diag_buffer_status[buffer_type] &=
1767 ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1768 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1769 "was released due to host reset\n", ioc->name, __func__,
1774 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1776 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1777 return -ERESTARTSYS;
1779 rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1782 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1785 mutex_unlock(&ioc->ctl_cmds.mutex);
1790 * _ctl_diag_read_buffer - request for copy of the diag buffer
1791 * @arg - user space buffer containing ioctl content
1792 * @state - NON_BLOCKING or BLOCKING
1795 _ctl_diag_read_buffer(void __user *arg, enum block_state state)
1797 struct mpt2_diag_read_buffer karg;
1798 struct mpt2_diag_read_buffer __user *uarg = arg;
1799 struct MPT2SAS_ADAPTER *ioc;
1800 void *request_data, *diag_data;
1801 Mpi2DiagBufferPostRequest_t *mpi_request;
1802 Mpi2DiagBufferPostReply_t *mpi_reply;
1805 unsigned long timeleft;
1810 if (copy_from_user(&karg, arg, sizeof(karg))) {
1811 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1812 __FILE__, __LINE__, __func__);
1815 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1818 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1821 buffer_type = karg.unique_id & 0x000000ff;
1822 if (!_ctl_diag_capability(ioc, buffer_type)) {
1823 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1824 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1828 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1829 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1830 "registered\n", ioc->name, __func__, karg.unique_id);
1834 request_data = ioc->diag_buffer[buffer_type];
1835 if (!request_data) {
1836 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1837 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1841 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1842 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1843 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1848 diag_data = (void *)(request_data + karg.starting_offset);
1849 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: diag_buffer(%p), "
1850 "offset(%d), sz(%d)\n", ioc->name, __func__,
1851 diag_data, karg.starting_offset, karg.bytes_to_read));
1853 if (copy_to_user((void __user *)uarg->diagnostic_data,
1854 diag_data, karg.bytes_to_read)) {
1855 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
1856 "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
1857 __func__, diag_data);
1861 if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
1864 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: Reregister "
1865 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
1866 if ((ioc->diag_buffer_status[buffer_type] &
1867 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1868 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
1869 "buffer_type(0x%02x) is still registered\n", ioc->name,
1870 __func__, buffer_type));
1873 /* Get a free request frame and save the message context.
1875 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1877 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1878 return -ERESTARTSYS;
1880 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1881 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1882 ioc->name, __func__);
1887 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1889 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1890 ioc->name, __func__);
1896 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1897 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1898 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1899 ioc->ctl_cmds.smid = smid;
1901 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1902 mpi_request->BufferType = buffer_type;
1903 mpi_request->BufferLength =
1904 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
1905 mpi_request->BufferAddress =
1906 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
1907 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1908 mpi_request->ProductSpecific[i] =
1909 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1910 mpi_request->VF_ID = 0; /* TODO */
1911 mpi_request->VP_ID = 0;
1913 mpt2sas_base_put_smid_default(ioc, smid);
1914 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1915 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1917 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1918 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1920 _debug_dump_mf(mpi_request,
1921 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1922 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1924 goto issue_host_reset;
1927 /* process the completed Reply Message Frame */
1928 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1929 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1930 ioc->name, __func__);
1935 mpi_reply = ioc->ctl_cmds.reply;
1936 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1938 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1939 ioc->diag_buffer_status[buffer_type] |=
1940 MPT2_DIAG_BUFFER_IS_REGISTERED;
1941 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1942 ioc->name, __func__));
1944 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1945 "log_info(0x%08x)\n", ioc->name, __func__,
1946 ioc_status, mpi_reply->IOCLogInfo);
1952 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1957 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1958 mutex_unlock(&ioc->ctl_cmds.mutex);
1963 * _ctl_ioctl_main - main ioctl entry point
1964 * @file - (struct file)
1965 * @cmd - ioctl opcode
1969 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg)
1971 enum block_state state;
1974 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING :
1979 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
1980 ret = _ctl_getiocinfo(arg);
1984 struct mpt2_ioctl_command karg;
1985 struct mpt2_ioctl_command __user *uarg;
1986 struct MPT2SAS_ADAPTER *ioc;
1988 if (copy_from_user(&karg, arg, sizeof(karg))) {
1989 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1990 __FILE__, __LINE__, __func__);
1994 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
1998 if (ioc->shost_recovery)
2001 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2003 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2007 case MPT2EVENTQUERY:
2008 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2009 ret = _ctl_eventquery(arg);
2011 case MPT2EVENTENABLE:
2012 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2013 ret = _ctl_eventenable(arg);
2015 case MPT2EVENTREPORT:
2016 ret = _ctl_eventreport(arg);
2019 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2020 ret = _ctl_do_reset(arg);
2022 case MPT2BTDHMAPPING:
2023 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2024 ret = _ctl_btdh_mapping(arg);
2026 case MPT2DIAGREGISTER:
2027 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2028 ret = _ctl_diag_register(arg, state);
2030 case MPT2DIAGUNREGISTER:
2031 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2032 ret = _ctl_diag_unregister(arg);
2035 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2036 ret = _ctl_diag_query(arg);
2038 case MPT2DIAGRELEASE:
2039 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2040 ret = _ctl_diag_release(arg, state);
2042 case MPT2DIAGREADBUFFER:
2043 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2044 ret = _ctl_diag_read_buffer(arg, state);
2048 struct mpt2_ioctl_command karg;
2049 struct MPT2SAS_ADAPTER *ioc;
2051 if (copy_from_user(&karg, arg, sizeof(karg))) {
2052 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2053 __FILE__, __LINE__, __func__);
2057 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2061 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2062 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2070 * _ctl_ioctl - main ioctl entry point (unlocked)
2071 * @file - (struct file)
2072 * @cmd - ioctl opcode
2076 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2081 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2086 #ifdef CONFIG_COMPAT
2088 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2089 * @file - (struct file)
2090 * @cmd - ioctl opcode
2091 * @arg - (struct mpt2_ioctl_command32)
2093 * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2096 _ctl_compat_mpt_command(struct file *file, unsigned cmd, unsigned long arg)
2098 struct mpt2_ioctl_command32 karg32;
2099 struct mpt2_ioctl_command32 __user *uarg;
2100 struct mpt2_ioctl_command karg;
2101 struct MPT2SAS_ADAPTER *ioc;
2102 enum block_state state;
2104 if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2107 uarg = (struct mpt2_ioctl_command32 __user *) arg;
2109 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2110 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2111 __FILE__, __LINE__, __func__);
2114 if (_ctl_verify_adapter(karg32.hdr.ioc_number, &ioc) == -1 || !ioc)
2117 if (ioc->shost_recovery)
2120 memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2121 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2122 karg.hdr.port_number = karg32.hdr.port_number;
2123 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2124 karg.timeout = karg32.timeout;
2125 karg.max_reply_bytes = karg32.max_reply_bytes;
2126 karg.data_in_size = karg32.data_in_size;
2127 karg.data_out_size = karg32.data_out_size;
2128 karg.max_sense_bytes = karg32.max_sense_bytes;
2129 karg.data_sge_offset = karg32.data_sge_offset;
2130 memcpy(&karg.reply_frame_buf_ptr, &karg32.reply_frame_buf_ptr,
2132 memcpy(&karg.data_in_buf_ptr, &karg32.data_in_buf_ptr,
2134 memcpy(&karg.data_out_buf_ptr, &karg32.data_out_buf_ptr,
2136 memcpy(&karg.sense_data_ptr, &karg32.sense_data_ptr,
2138 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2139 return _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2143 * _ctl_ioctl_compat - main ioctl entry point (compat)
2148 * This routine handles 32 bit applications in 64bit os.
2151 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2156 if (cmd == MPT2COMMAND32)
2157 ret = _ctl_compat_mpt_command(file, cmd, arg);
2159 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2165 /* scsi host attributes */
2168 * _ctl_version_fw_show - firmware version
2169 * @cdev - pointer to embedded class device
2170 * @buf - the buffer returned
2172 * A sysfs 'read-only' shost attribute.
2175 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2178 struct Scsi_Host *shost = class_to_shost(cdev);
2179 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2181 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2182 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2183 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2184 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2185 ioc->facts.FWVersion.Word & 0x000000FF);
2187 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2190 * _ctl_version_bios_show - bios version
2191 * @cdev - pointer to embedded class device
2192 * @buf - the buffer returned
2194 * A sysfs 'read-only' shost attribute.
2197 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2200 struct Scsi_Host *shost = class_to_shost(cdev);
2201 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2203 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2205 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2206 (version & 0xFF000000) >> 24,
2207 (version & 0x00FF0000) >> 16,
2208 (version & 0x0000FF00) >> 8,
2209 version & 0x000000FF);
2211 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2214 * _ctl_version_mpi_show - MPI (message passing interface) version
2215 * @cdev - pointer to embedded class device
2216 * @buf - the buffer returned
2218 * A sysfs 'read-only' shost attribute.
2221 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2224 struct Scsi_Host *shost = class_to_shost(cdev);
2225 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2227 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2228 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2230 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2233 * _ctl_version_product_show - product name
2234 * @cdev - pointer to embedded class device
2235 * @buf - the buffer returned
2237 * A sysfs 'read-only' shost attribute.
2240 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2243 struct Scsi_Host *shost = class_to_shost(cdev);
2244 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2246 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2248 static DEVICE_ATTR(version_product, S_IRUGO,
2249 _ctl_version_product_show, NULL);
2252 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2253 * @cdev - pointer to embedded class device
2254 * @buf - the buffer returned
2256 * A sysfs 'read-only' shost attribute.
2259 _ctl_version_nvdata_persistent_show(struct device *cdev,
2260 struct device_attribute *attr, char *buf)
2262 struct Scsi_Host *shost = class_to_shost(cdev);
2263 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2265 return snprintf(buf, PAGE_SIZE, "%02xh\n",
2266 le16_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2268 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2269 _ctl_version_nvdata_persistent_show, NULL);
2272 * _ctl_version_nvdata_default_show - nvdata default version
2273 * @cdev - pointer to embedded class device
2274 * @buf - the buffer returned
2276 * A sysfs 'read-only' shost attribute.
2279 _ctl_version_nvdata_default_show(struct device *cdev,
2280 struct device_attribute *attr, char *buf)
2282 struct Scsi_Host *shost = class_to_shost(cdev);
2283 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2285 return snprintf(buf, PAGE_SIZE, "%02xh\n",
2286 le16_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2288 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2289 _ctl_version_nvdata_default_show, NULL);
2292 * _ctl_board_name_show - board name
2293 * @cdev - pointer to embedded class device
2294 * @buf - the buffer returned
2296 * A sysfs 'read-only' shost attribute.
2299 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2302 struct Scsi_Host *shost = class_to_shost(cdev);
2303 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2305 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2307 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2310 * _ctl_board_assembly_show - board assembly name
2311 * @cdev - pointer to embedded class device
2312 * @buf - the buffer returned
2314 * A sysfs 'read-only' shost attribute.
2317 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2320 struct Scsi_Host *shost = class_to_shost(cdev);
2321 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2323 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2325 static DEVICE_ATTR(board_assembly, S_IRUGO,
2326 _ctl_board_assembly_show, NULL);
2329 * _ctl_board_tracer_show - board tracer number
2330 * @cdev - pointer to embedded class device
2331 * @buf - the buffer returned
2333 * A sysfs 'read-only' shost attribute.
2336 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2339 struct Scsi_Host *shost = class_to_shost(cdev);
2340 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2342 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2344 static DEVICE_ATTR(board_tracer, S_IRUGO,
2345 _ctl_board_tracer_show, NULL);
2348 * _ctl_io_delay_show - io missing delay
2349 * @cdev - pointer to embedded class device
2350 * @buf - the buffer returned
2352 * This is for firmware implemention for deboucing device
2355 * A sysfs 'read-only' shost attribute.
2358 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2361 struct Scsi_Host *shost = class_to_shost(cdev);
2362 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2364 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2366 static DEVICE_ATTR(io_delay, S_IRUGO,
2367 _ctl_io_delay_show, NULL);
2370 * _ctl_device_delay_show - device missing delay
2371 * @cdev - pointer to embedded class device
2372 * @buf - the buffer returned
2374 * This is for firmware implemention for deboucing device
2377 * A sysfs 'read-only' shost attribute.
2380 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2383 struct Scsi_Host *shost = class_to_shost(cdev);
2384 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2386 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2388 static DEVICE_ATTR(device_delay, S_IRUGO,
2389 _ctl_device_delay_show, NULL);
2392 * _ctl_fw_queue_depth_show - global credits
2393 * @cdev - pointer to embedded class device
2394 * @buf - the buffer returned
2396 * This is firmware queue depth limit
2398 * A sysfs 'read-only' shost attribute.
2401 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2404 struct Scsi_Host *shost = class_to_shost(cdev);
2405 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2407 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2409 static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2410 _ctl_fw_queue_depth_show, NULL);
2413 * _ctl_sas_address_show - sas address
2414 * @cdev - pointer to embedded class device
2415 * @buf - the buffer returned
2417 * This is the controller sas address
2419 * A sysfs 'read-only' shost attribute.
2422 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2425 struct Scsi_Host *shost = class_to_shost(cdev);
2426 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2428 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2429 (unsigned long long)ioc->sas_hba.sas_address);
2431 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2432 _ctl_host_sas_address_show, NULL);
2435 * _ctl_logging_level_show - logging level
2436 * @cdev - pointer to embedded class device
2437 * @buf - the buffer returned
2439 * A sysfs 'read/write' shost attribute.
2442 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2445 struct Scsi_Host *shost = class_to_shost(cdev);
2446 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2448 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2451 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2452 const char *buf, size_t count)
2454 struct Scsi_Host *shost = class_to_shost(cdev);
2455 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2458 if (sscanf(buf, "%x", &val) != 1)
2461 ioc->logging_level = val;
2462 printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2463 ioc->logging_level);
2466 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2467 _ctl_logging_level_show, _ctl_logging_level_store);
2469 struct device_attribute *mpt2sas_host_attrs[] = {
2470 &dev_attr_version_fw,
2471 &dev_attr_version_bios,
2472 &dev_attr_version_mpi,
2473 &dev_attr_version_product,
2474 &dev_attr_version_nvdata_persistent,
2475 &dev_attr_version_nvdata_default,
2476 &dev_attr_board_name,
2477 &dev_attr_board_assembly,
2478 &dev_attr_board_tracer,
2480 &dev_attr_device_delay,
2481 &dev_attr_logging_level,
2482 &dev_attr_fw_queue_depth,
2483 &dev_attr_host_sas_address,
2487 /* device attributes */
2490 * _ctl_device_sas_address_show - sas address
2491 * @cdev - pointer to embedded class device
2492 * @buf - the buffer returned
2494 * This is the sas address for the target
2496 * A sysfs 'read-only' shost attribute.
2499 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2502 struct scsi_device *sdev = to_scsi_device(dev);
2503 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2505 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2506 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2508 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2511 * _ctl_device_handle_show - device handle
2512 * @cdev - pointer to embedded class device
2513 * @buf - the buffer returned
2515 * This is the firmware assigned device handle
2517 * A sysfs 'read-only' shost attribute.
2520 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
2523 struct scsi_device *sdev = to_scsi_device(dev);
2524 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2526 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
2527 sas_device_priv_data->sas_target->handle);
2529 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
2531 struct device_attribute *mpt2sas_dev_attrs[] = {
2532 &dev_attr_sas_address,
2533 &dev_attr_sas_device_handle,
2537 static const struct file_operations ctl_fops = {
2538 .owner = THIS_MODULE,
2539 .unlocked_ioctl = _ctl_ioctl,
2540 .release = _ctl_release,
2542 .fasync = _ctl_fasync,
2543 #ifdef CONFIG_COMPAT
2544 .compat_ioctl = _ctl_ioctl_compat,
2548 static struct miscdevice ctl_dev = {
2549 .minor = MPT2SAS_MINOR,
2550 .name = MPT2SAS_DEV_NAME,
2555 * mpt2sas_ctl_init - main entry point for ctl.
2559 mpt2sas_ctl_init(void)
2562 if (misc_register(&ctl_dev) < 0)
2563 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
2564 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
2566 init_waitqueue_head(&ctl_poll_wait);
2570 * mpt2sas_ctl_exit - exit point for ctl
2574 mpt2sas_ctl_exit(void)
2576 struct MPT2SAS_ADAPTER *ioc;
2579 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
2581 /* free memory associated to diag buffers */
2582 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
2583 if (!ioc->diag_buffer[i])
2585 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
2586 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
2587 ioc->diag_buffer[i] = NULL;
2588 ioc->diag_buffer_status[i] = 0;
2591 kfree(ioc->event_log);
2593 misc_deregister(&ctl_dev);