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-2010 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/mutex.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 DEFINE_MUTEX(_ctl_mutex);
65 static struct fasync_struct *async_queue;
66 static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
68 static int _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type,
72 * enum block_state - blocking state
73 * @NON_BLOCKING: non blocking
76 * These states are for ioctls that need to wait for a response
77 * from firmware, so they probably require sleep.
84 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
86 * _ctl_sas_device_find_by_handle - sas device search
87 * @ioc: per adapter object
88 * @handle: sas device handle (assigned by firmware)
89 * Context: Calling function should acquire ioc->sas_device_lock
91 * This searches for sas_device based on sas_address, then return sas_device
94 static struct _sas_device *
95 _ctl_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
97 struct _sas_device *sas_device, *r;
100 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
101 if (sas_device->handle != handle)
112 * _ctl_display_some_debug - debug routine
113 * @ioc: per adapter object
114 * @smid: system request message index
115 * @calling_function_name: string pass from calling function
116 * @mpi_reply: reply message frame
119 * Function for displaying debug info helpful when debugging issues
123 _ctl_display_some_debug(struct MPT2SAS_ADAPTER *ioc, u16 smid,
124 char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
126 Mpi2ConfigRequest_t *mpi_request;
129 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
132 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
133 switch (mpi_request->Function) {
134 case MPI2_FUNCTION_SCSI_IO_REQUEST:
136 Mpi2SCSIIORequest_t *scsi_request =
137 (Mpi2SCSIIORequest_t *)mpi_request;
139 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
140 "scsi_io, cmd(0x%02x), cdb_len(%d)",
141 scsi_request->CDB.CDB32[0],
142 le16_to_cpu(scsi_request->IoFlags) & 0xF);
143 desc = ioc->tmp_string;
146 case MPI2_FUNCTION_SCSI_TASK_MGMT:
149 case MPI2_FUNCTION_IOC_INIT:
152 case MPI2_FUNCTION_IOC_FACTS:
155 case MPI2_FUNCTION_CONFIG:
157 Mpi2ConfigRequest_t *config_request =
158 (Mpi2ConfigRequest_t *)mpi_request;
160 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
161 "config, type(0x%02x), ext_type(0x%02x), number(%d)",
162 (config_request->Header.PageType &
163 MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
164 config_request->Header.PageNumber);
165 desc = ioc->tmp_string;
168 case MPI2_FUNCTION_PORT_FACTS:
171 case MPI2_FUNCTION_PORT_ENABLE:
172 desc = "port_enable";
174 case MPI2_FUNCTION_EVENT_NOTIFICATION:
175 desc = "event_notification";
177 case MPI2_FUNCTION_FW_DOWNLOAD:
178 desc = "fw_download";
180 case MPI2_FUNCTION_FW_UPLOAD:
183 case MPI2_FUNCTION_RAID_ACTION:
184 desc = "raid_action";
186 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
188 Mpi2SCSIIORequest_t *scsi_request =
189 (Mpi2SCSIIORequest_t *)mpi_request;
191 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
192 "raid_pass, cmd(0x%02x), cdb_len(%d)",
193 scsi_request->CDB.CDB32[0],
194 le16_to_cpu(scsi_request->IoFlags) & 0xF);
195 desc = ioc->tmp_string;
198 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
199 desc = "sas_iounit_cntl";
201 case MPI2_FUNCTION_SATA_PASSTHROUGH:
204 case MPI2_FUNCTION_DIAG_BUFFER_POST:
205 desc = "diag_buffer_post";
207 case MPI2_FUNCTION_DIAG_RELEASE:
208 desc = "diag_release";
210 case MPI2_FUNCTION_SMP_PASSTHROUGH:
211 desc = "smp_passthrough";
218 printk(MPT2SAS_INFO_FMT "%s: %s, smid(%d)\n",
219 ioc->name, calling_function_name, desc, smid);
224 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
225 printk(MPT2SAS_INFO_FMT
226 "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
227 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
228 le32_to_cpu(mpi_reply->IOCLogInfo));
230 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
231 mpi_request->Function ==
232 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
233 Mpi2SCSIIOReply_t *scsi_reply =
234 (Mpi2SCSIIOReply_t *)mpi_reply;
235 struct _sas_device *sas_device = NULL;
238 spin_lock_irqsave(&ioc->sas_device_lock, flags);
239 sas_device = _ctl_sas_device_find_by_handle(ioc,
240 le16_to_cpu(scsi_reply->DevHandle));
242 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
243 "phy(%d)\n", ioc->name, (unsigned long long)
244 sas_device->sas_address, sas_device->phy);
245 printk(MPT2SAS_WARN_FMT
246 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
247 ioc->name, sas_device->enclosure_logical_id,
250 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
251 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
252 printk(MPT2SAS_INFO_FMT
253 "\tscsi_state(0x%02x), scsi_status"
254 "(0x%02x)\n", ioc->name,
255 scsi_reply->SCSIState,
256 scsi_reply->SCSIStatus);
262 * mpt2sas_ctl_done - ctl module completion routine
263 * @ioc: per adapter object
264 * @smid: system request message index
265 * @msix_index: MSIX table index supplied by the OS
266 * @reply: reply message frame(lower 32bit addr)
269 * The callback handler when using ioc->ctl_cb_idx.
271 * Return 1 meaning mf should be freed from _base_interrupt
272 * 0 means the mf is freed from this function.
275 mpt2sas_ctl_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
278 MPI2DefaultReply_t *mpi_reply;
279 Mpi2SCSIIOReply_t *scsiio_reply;
280 const void *sense_data;
283 if (ioc->ctl_cmds.status == MPT2_CMD_NOT_USED)
285 if (ioc->ctl_cmds.smid != smid)
287 ioc->ctl_cmds.status |= MPT2_CMD_COMPLETE;
288 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
290 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
291 ioc->ctl_cmds.status |= MPT2_CMD_REPLY_VALID;
293 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
294 mpi_reply->Function ==
295 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
296 scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
297 if (scsiio_reply->SCSIState &
298 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
299 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
300 le32_to_cpu(scsiio_reply->SenseCount));
301 sense_data = mpt2sas_base_get_sense_buffer(ioc,
303 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
307 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
308 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
310 ioc->ctl_cmds.status &= ~MPT2_CMD_PENDING;
311 complete(&ioc->ctl_cmds.done);
316 * _ctl_check_event_type - determines when an event needs logging
317 * @ioc: per adapter object
318 * @event: firmware event
320 * The bitmask in ioc->event_type[] indicates which events should be
321 * be saved in the driver event_log. This bitmask is set by application.
323 * Returns 1 when event should be captured, or zero means no match.
326 _ctl_check_event_type(struct MPT2SAS_ADAPTER *ioc, u16 event)
331 if (event >= 128 || !event || !ioc->event_log)
334 desired_event = (1 << (event % 32));
338 return desired_event & ioc->event_type[i];
342 * mpt2sas_ctl_add_to_event_log - add event
343 * @ioc: per adapter object
344 * @mpi_reply: reply message frame
349 mpt2sas_ctl_add_to_event_log(struct MPT2SAS_ADAPTER *ioc,
350 Mpi2EventNotificationReply_t *mpi_reply)
352 struct MPT2_IOCTL_EVENTS *event_log;
355 u32 sz, event_data_sz;
361 event = le16_to_cpu(mpi_reply->Event);
363 if (_ctl_check_event_type(ioc, event)) {
365 /* insert entry into circular event_log */
366 i = ioc->event_context % MPT2SAS_CTL_EVENT_LOG_SIZE;
367 event_log = ioc->event_log;
368 event_log[i].event = event;
369 event_log[i].context = ioc->event_context++;
371 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
372 sz = min_t(u32, event_data_sz, MPT2_EVENT_DATA_SIZE);
373 memset(event_log[i].data, 0, MPT2_EVENT_DATA_SIZE);
374 memcpy(event_log[i].data, mpi_reply->EventData, sz);
378 /* This aen_event_read_flag flag is set until the
379 * application has read the event log.
380 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
382 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
383 (send_aen && !ioc->aen_event_read_flag)) {
384 ioc->aen_event_read_flag = 1;
385 wake_up_interruptible(&ctl_poll_wait);
387 kill_fasync(&async_queue, SIGIO, POLL_IN);
392 * mpt2sas_ctl_event_callback - firmware event handler (called at ISR time)
393 * @ioc: per adapter object
394 * @msix_index: MSIX table index supplied by the OS
395 * @reply: reply message frame(lower 32bit addr)
396 * Context: interrupt.
398 * This function merely adds a new work task into ioc->firmware_event_thread.
399 * The tasks are worked from _firmware_event_work in user context.
401 * Return 1 meaning mf should be freed from _base_interrupt
402 * 0 means the mf is freed from this function.
405 mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
408 Mpi2EventNotificationReply_t *mpi_reply;
410 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
411 mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
416 * _ctl_verify_adapter - validates ioc_number passed from application
417 * @ioc: per adapter object
418 * @iocpp: The ioc pointer is returned in this.
420 * Return (-1) means error, else ioc_number.
423 _ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
425 struct MPT2SAS_ADAPTER *ioc;
427 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
428 if (ioc->id != ioc_number)
438 * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
439 * @ioc: per adapter object
440 * @reset_phase: phase
442 * The handler for doing any required cleanup or initialization.
444 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
445 * MPT2_IOC_DONE_RESET
448 mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
453 switch (reset_phase) {
454 case MPT2_IOC_PRE_RESET:
455 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
456 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
457 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
458 if (!(ioc->diag_buffer_status[i] &
459 MPT2_DIAG_BUFFER_IS_REGISTERED))
461 if ((ioc->diag_buffer_status[i] &
462 MPT2_DIAG_BUFFER_IS_RELEASED))
464 _ctl_send_release(ioc, i, &issue_reset);
467 case MPT2_IOC_AFTER_RESET:
468 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
469 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
470 if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
471 ioc->ctl_cmds.status |= MPT2_CMD_RESET;
472 mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
473 complete(&ioc->ctl_cmds.done);
476 case MPT2_IOC_DONE_RESET:
477 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
478 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
480 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
481 if (!(ioc->diag_buffer_status[i] &
482 MPT2_DIAG_BUFFER_IS_REGISTERED))
484 if ((ioc->diag_buffer_status[i] &
485 MPT2_DIAG_BUFFER_IS_RELEASED))
487 ioc->diag_buffer_status[i] |=
488 MPT2_DIAG_BUFFER_IS_DIAG_RESET;
500 * Called when application request fasyn callback handler.
503 _ctl_fasync(int fd, struct file *filep, int mode)
505 return fasync_helper(fd, filep, mode, &async_queue);
513 * Called when application releases the fasyn callback handler.
516 _ctl_release(struct inode *inode, struct file *filep)
518 return fasync_helper(-1, filep, 0, &async_queue);
528 _ctl_poll(struct file *filep, poll_table *wait)
530 struct MPT2SAS_ADAPTER *ioc;
532 poll_wait(filep, &ctl_poll_wait, wait);
534 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
535 if (ioc->aen_event_read_flag)
536 return POLLIN | POLLRDNORM;
542 * _ctl_set_task_mid - assign an active smid to tm request
543 * @ioc: per adapter object
544 * @karg - (struct mpt2_ioctl_command)
545 * @tm_request - pointer to mf from user space
547 * Returns 0 when an smid if found, else fail.
548 * during failure, the reply frame is filled.
551 _ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
552 Mpi2SCSITaskManagementRequest_t *tm_request)
557 struct scsi_cmnd *scmd;
558 struct MPT2SAS_DEVICE *priv_data;
560 Mpi2SCSITaskManagementReply_t *tm_reply;
565 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
567 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
572 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
574 handle = le16_to_cpu(tm_request->DevHandle);
575 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
576 for (i = ioc->scsiio_depth; i && !found; i--) {
577 scmd = ioc->scsi_lookup[i - 1].scmd;
578 if (scmd == NULL || scmd->device == NULL ||
579 scmd->device->hostdata == NULL)
581 if (lun != scmd->device->lun)
583 priv_data = scmd->device->hostdata;
584 if (priv_data->sas_target == NULL)
586 if (priv_data->sas_target->handle != handle)
588 tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
591 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
594 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
595 "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
596 desc, le16_to_cpu(tm_request->DevHandle), lun));
597 tm_reply = ioc->ctl_cmds.reply;
598 tm_reply->DevHandle = tm_request->DevHandle;
599 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
600 tm_reply->TaskType = tm_request->TaskType;
601 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
602 tm_reply->VP_ID = tm_request->VP_ID;
603 tm_reply->VF_ID = tm_request->VF_ID;
604 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
605 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
607 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
612 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
613 "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
614 desc, le16_to_cpu(tm_request->DevHandle), lun,
615 le16_to_cpu(tm_request->TaskMID)));
620 * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
621 * @ioc: per adapter object
622 * @karg - (struct mpt2_ioctl_command)
623 * @mf - pointer to mf in user space
624 * @state - NON_BLOCKING or BLOCKING
627 _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc,
628 struct mpt2_ioctl_command karg, void __user *mf, enum block_state state)
630 MPI2RequestHeader_t *mpi_request = NULL, *request;
631 MPI2DefaultReply_t *mpi_reply;
635 unsigned long timeout, timeleft;
639 void *data_out = NULL;
640 dma_addr_t data_out_dma;
641 size_t data_out_sz = 0;
642 void *data_in = NULL;
643 dma_addr_t data_in_dma;
644 size_t data_in_sz = 0;
647 u16 wait_state_count;
651 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
653 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
656 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
657 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
658 ioc->name, __func__);
663 wait_state_count = 0;
664 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
665 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
666 if (wait_state_count++ == 10) {
667 printk(MPT2SAS_ERR_FMT
668 "%s: failed due to ioc not operational\n",
669 ioc->name, __func__);
674 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
675 printk(MPT2SAS_INFO_FMT "%s: waiting for "
676 "operational state(count=%d)\n", ioc->name,
677 __func__, wait_state_count);
679 if (wait_state_count)
680 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
681 ioc->name, __func__);
683 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
685 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a memory for "
686 "mpi_request\n", ioc->name, __func__);
691 /* Check for overflow and wraparound */
692 if (karg.data_sge_offset * 4 > ioc->request_sz ||
693 karg.data_sge_offset > (UINT_MAX / 4)) {
698 /* copy in request message frame from user */
699 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
700 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
706 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
707 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
709 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
710 ioc->name, __func__);
716 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
718 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
719 ioc->name, __func__);
726 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
727 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
728 request = mpt2sas_base_get_msg_frame(ioc, smid);
729 memcpy(request, mpi_request, karg.data_sge_offset*4);
730 ioc->ctl_cmds.smid = smid;
731 data_out_sz = karg.data_out_size;
732 data_in_sz = karg.data_in_size;
734 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
735 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
736 if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
737 le16_to_cpu(mpi_request->FunctionDependent1) >
738 ioc->facts.MaxDevHandle) {
740 mpt2sas_base_free_smid(ioc, smid);
745 /* obtain dma-able memory for data transfer */
746 if (data_out_sz) /* WRITE */ {
747 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
750 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
753 mpt2sas_base_free_smid(ioc, smid);
756 if (copy_from_user(data_out, karg.data_out_buf_ptr,
758 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
761 mpt2sas_base_free_smid(ioc, smid);
766 if (data_in_sz) /* READ */ {
767 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
770 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
773 mpt2sas_base_free_smid(ioc, smid);
778 /* add scatter gather elements */
779 psge = (void *)request + (karg.data_sge_offset*4);
781 if (!data_out_sz && !data_in_sz) {
782 mpt2sas_base_build_zero_len_sge(ioc, psge);
783 } else if (data_out_sz && data_in_sz) {
784 /* WRITE sgel first */
785 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
786 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
787 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
788 ioc->base_add_sg_single(psge, sgl_flags |
789 data_out_sz, data_out_dma);
792 psge += ioc->sge_size;
795 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
796 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
797 MPI2_SGE_FLAGS_END_OF_LIST);
798 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
799 ioc->base_add_sg_single(psge, sgl_flags |
800 data_in_sz, data_in_dma);
801 } else if (data_out_sz) /* WRITE */ {
802 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
803 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
804 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
805 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
806 ioc->base_add_sg_single(psge, sgl_flags |
807 data_out_sz, data_out_dma);
808 } else if (data_in_sz) /* READ */ {
809 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
810 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
811 MPI2_SGE_FLAGS_END_OF_LIST);
812 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
813 ioc->base_add_sg_single(psge, sgl_flags |
814 data_in_sz, data_in_dma);
817 /* send command to firmware */
818 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
819 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
822 switch (mpi_request->Function) {
823 case MPI2_FUNCTION_SCSI_IO_REQUEST:
824 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
826 Mpi2SCSIIORequest_t *scsiio_request =
827 (Mpi2SCSIIORequest_t *)request;
828 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
829 scsiio_request->SenseBufferLowAddress =
830 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
831 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
832 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
833 mpt2sas_base_put_smid_scsi_io(ioc, smid,
834 le16_to_cpu(mpi_request->FunctionDependent1));
836 mpt2sas_base_put_smid_default(ioc, smid);
839 case MPI2_FUNCTION_SCSI_TASK_MGMT:
841 Mpi2SCSITaskManagementRequest_t *tm_request =
842 (Mpi2SCSITaskManagementRequest_t *)request;
844 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
845 "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
846 le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
848 if (tm_request->TaskType ==
849 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
850 tm_request->TaskType ==
851 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
852 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
853 mpt2sas_base_free_smid(ioc, smid);
858 mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
859 tm_request->DevHandle));
860 mpt2sas_base_put_smid_hi_priority(ioc, smid);
863 case MPI2_FUNCTION_SMP_PASSTHROUGH:
865 Mpi2SmpPassthroughRequest_t *smp_request =
866 (Mpi2SmpPassthroughRequest_t *)mpi_request;
869 /* ioc determines which port to use */
870 smp_request->PhysicalPort = 0xFF;
871 if (smp_request->PassthroughFlags &
872 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
873 data = (u8 *)&smp_request->SGL;
877 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
878 ioc->ioc_link_reset_in_progress = 1;
879 ioc->ignore_loginfos = 1;
881 mpt2sas_base_put_smid_default(ioc, smid);
884 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
886 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
887 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
889 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
890 || sasiounit_request->Operation ==
891 MPI2_SAS_OP_PHY_LINK_RESET) {
892 ioc->ioc_link_reset_in_progress = 1;
893 ioc->ignore_loginfos = 1;
895 mpt2sas_base_put_smid_default(ioc, smid);
899 mpt2sas_base_put_smid_default(ioc, smid);
903 if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
904 timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
906 timeout = karg.timeout;
907 init_completion(&ioc->ctl_cmds.done);
908 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
910 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
911 Mpi2SCSITaskManagementRequest_t *tm_request =
912 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
913 mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
914 tm_request->DevHandle));
915 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
916 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
917 ioc->ioc_link_reset_in_progress) {
918 ioc->ioc_link_reset_in_progress = 0;
919 ioc->ignore_loginfos = 0;
921 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
922 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
924 _debug_dump_mf(mpi_request, karg.data_sge_offset);
925 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
927 goto issue_host_reset;
930 mpi_reply = ioc->ctl_cmds.reply;
931 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
933 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
934 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
935 (ioc->logging_level & MPT_DEBUG_TM)) {
936 Mpi2SCSITaskManagementReply_t *tm_reply =
937 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
939 printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
940 "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
941 "TerminationCount(0x%08x)\n", ioc->name,
942 le16_to_cpu(tm_reply->IOCStatus),
943 le32_to_cpu(tm_reply->IOCLogInfo),
944 le32_to_cpu(tm_reply->TerminationCount));
947 /* copy out xdata to user */
949 if (copy_to_user(karg.data_in_buf_ptr, data_in,
951 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
958 /* copy out reply message frame to user */
959 if (karg.max_reply_bytes) {
960 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
961 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
963 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
970 /* copy out sense to user */
971 if (karg.max_sense_bytes && (mpi_request->Function ==
972 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
973 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
974 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
975 if (copy_to_user(karg.sense_data_ptr,
976 ioc->ctl_cmds.sense, sz)) {
977 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
987 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
988 mpi_request->Function ==
989 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
990 printk(MPT2SAS_INFO_FMT "issue target reset: handle "
991 "= (0x%04x)\n", ioc->name,
992 le16_to_cpu(mpi_request->FunctionDependent1));
993 mpt2sas_halt_firmware(ioc);
994 mpt2sas_scsih_issue_tm(ioc,
995 le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
996 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
998 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1000 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1006 /* free memory associated with sg buffers */
1008 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1012 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1016 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1017 mutex_unlock(&ioc->ctl_cmds.mutex);
1022 * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
1023 * @arg - user space buffer containing ioctl content
1026 _ctl_getiocinfo(void __user *arg)
1028 struct mpt2_ioctl_iocinfo karg;
1029 struct MPT2SAS_ADAPTER *ioc;
1032 if (copy_from_user(&karg, arg, sizeof(karg))) {
1033 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1034 __FILE__, __LINE__, __func__);
1037 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1040 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1043 memset(&karg, 0 , sizeof(karg));
1044 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1046 karg.port_number = ioc->pfacts[0].PortNumber;
1047 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
1048 karg.hw_rev = revision;
1049 karg.pci_id = ioc->pdev->device;
1050 karg.subsystem_device = ioc->pdev->subsystem_device;
1051 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1052 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1053 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1054 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1055 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1056 karg.firmware_version = ioc->facts.FWVersion.Word;
1057 strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
1058 strcat(karg.driver_version, "-");
1059 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1060 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1062 if (copy_to_user(arg, &karg, sizeof(karg))) {
1063 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1064 __FILE__, __LINE__, __func__);
1071 * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
1072 * @arg - user space buffer containing ioctl content
1075 _ctl_eventquery(void __user *arg)
1077 struct mpt2_ioctl_eventquery karg;
1078 struct MPT2SAS_ADAPTER *ioc;
1080 if (copy_from_user(&karg, arg, sizeof(karg))) {
1081 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1082 __FILE__, __LINE__, __func__);
1085 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1088 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1091 karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
1092 memcpy(karg.event_types, ioc->event_type,
1093 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1095 if (copy_to_user(arg, &karg, sizeof(karg))) {
1096 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1097 __FILE__, __LINE__, __func__);
1104 * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1105 * @arg - user space buffer containing ioctl content
1108 _ctl_eventenable(void __user *arg)
1110 struct mpt2_ioctl_eventenable karg;
1111 struct MPT2SAS_ADAPTER *ioc;
1113 if (copy_from_user(&karg, arg, sizeof(karg))) {
1114 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1115 __FILE__, __LINE__, __func__);
1118 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1121 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1126 memcpy(ioc->event_type, karg.event_types,
1127 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1128 mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1130 /* initialize event_log */
1131 ioc->event_context = 0;
1132 ioc->aen_event_read_flag = 0;
1133 ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1134 sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1135 if (!ioc->event_log) {
1136 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1137 __FILE__, __LINE__, __func__);
1144 * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1145 * @arg - user space buffer containing ioctl content
1148 _ctl_eventreport(void __user *arg)
1150 struct mpt2_ioctl_eventreport karg;
1151 struct MPT2SAS_ADAPTER *ioc;
1152 u32 number_bytes, max_events, max;
1153 struct mpt2_ioctl_eventreport __user *uarg = arg;
1155 if (copy_from_user(&karg, arg, sizeof(karg))) {
1156 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1157 __FILE__, __LINE__, __func__);
1160 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1163 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1166 number_bytes = karg.hdr.max_data_size -
1167 sizeof(struct mpt2_ioctl_header);
1168 max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1169 max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1171 /* If fewer than 1 event is requested, there must have
1172 * been some type of error.
1174 if (!max || !ioc->event_log)
1177 number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1178 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1179 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1180 __FILE__, __LINE__, __func__);
1184 /* reset flag so SIGIO can restart */
1185 ioc->aen_event_read_flag = 0;
1190 * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1191 * @arg - user space buffer containing ioctl content
1194 _ctl_do_reset(void __user *arg)
1196 struct mpt2_ioctl_diag_reset karg;
1197 struct MPT2SAS_ADAPTER *ioc;
1200 if (copy_from_user(&karg, arg, sizeof(karg))) {
1201 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1202 __FILE__, __LINE__, __func__);
1205 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1208 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1211 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1213 printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1214 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1219 * _ctl_btdh_search_sas_device - searching for sas device
1220 * @ioc: per adapter object
1221 * @btdh: btdh ioctl payload
1224 _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1225 struct mpt2_ioctl_btdh_mapping *btdh)
1227 struct _sas_device *sas_device;
1228 unsigned long flags;
1231 if (list_empty(&ioc->sas_device_list))
1234 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1235 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1236 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1237 btdh->handle == sas_device->handle) {
1238 btdh->bus = sas_device->channel;
1239 btdh->id = sas_device->id;
1242 } else if (btdh->bus == sas_device->channel && btdh->id ==
1243 sas_device->id && btdh->handle == 0xFFFF) {
1244 btdh->handle = sas_device->handle;
1250 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1255 * _ctl_btdh_search_raid_device - searching for raid device
1256 * @ioc: per adapter object
1257 * @btdh: btdh ioctl payload
1260 _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1261 struct mpt2_ioctl_btdh_mapping *btdh)
1263 struct _raid_device *raid_device;
1264 unsigned long flags;
1267 if (list_empty(&ioc->raid_device_list))
1270 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1271 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1272 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1273 btdh->handle == raid_device->handle) {
1274 btdh->bus = raid_device->channel;
1275 btdh->id = raid_device->id;
1278 } else if (btdh->bus == raid_device->channel && btdh->id ==
1279 raid_device->id && btdh->handle == 0xFFFF) {
1280 btdh->handle = raid_device->handle;
1286 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1291 * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1292 * @arg - user space buffer containing ioctl content
1295 _ctl_btdh_mapping(void __user *arg)
1297 struct mpt2_ioctl_btdh_mapping karg;
1298 struct MPT2SAS_ADAPTER *ioc;
1301 if (copy_from_user(&karg, arg, sizeof(karg))) {
1302 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1303 __FILE__, __LINE__, __func__);
1306 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1309 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1312 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1314 _ctl_btdh_search_raid_device(ioc, &karg);
1316 if (copy_to_user(arg, &karg, sizeof(karg))) {
1317 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1318 __FILE__, __LINE__, __func__);
1325 * _ctl_diag_capability - return diag buffer capability
1326 * @ioc: per adapter object
1327 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1329 * returns 1 when diag buffer support is enabled in firmware
1332 _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1336 switch (buffer_type) {
1337 case MPI2_DIAG_BUF_TYPE_TRACE:
1338 if (ioc->facts.IOCCapabilities &
1339 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1342 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1343 if (ioc->facts.IOCCapabilities &
1344 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1347 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1348 if (ioc->facts.IOCCapabilities &
1349 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1357 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1358 * @ioc: per adapter object
1359 * @diag_register: the diag_register struct passed in from user space
1363 _ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
1364 struct mpt2_diag_register *diag_register)
1367 void *request_data = NULL;
1368 dma_addr_t request_data_dma;
1369 u32 request_data_sz = 0;
1370 Mpi2DiagBufferPostRequest_t *mpi_request;
1371 Mpi2DiagBufferPostReply_t *mpi_reply;
1373 unsigned long timeleft;
1378 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1381 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1382 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1383 ioc->name, __func__);
1388 buffer_type = diag_register->buffer_type;
1389 if (!_ctl_diag_capability(ioc, buffer_type)) {
1390 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1391 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1395 if (ioc->diag_buffer_status[buffer_type] &
1396 MPT2_DIAG_BUFFER_IS_REGISTERED) {
1397 printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1398 "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1403 if (diag_register->requested_buffer_size % 4) {
1404 printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1405 "is not 4 byte aligned\n", ioc->name, __func__);
1409 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1411 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1412 ioc->name, __func__);
1418 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1419 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1420 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1421 ioc->ctl_cmds.smid = smid;
1423 request_data = ioc->diag_buffer[buffer_type];
1424 request_data_sz = diag_register->requested_buffer_size;
1425 ioc->unique_id[buffer_type] = diag_register->unique_id;
1426 ioc->diag_buffer_status[buffer_type] = 0;
1427 memcpy(ioc->product_specific[buffer_type],
1428 diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
1429 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1432 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1433 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1434 pci_free_consistent(ioc->pdev,
1435 ioc->diag_buffer_sz[buffer_type],
1436 request_data, request_data_dma);
1437 request_data = NULL;
1441 if (request_data == NULL) {
1442 ioc->diag_buffer_sz[buffer_type] = 0;
1443 ioc->diag_buffer_dma[buffer_type] = 0;
1444 request_data = pci_alloc_consistent(
1445 ioc->pdev, request_data_sz, &request_data_dma);
1446 if (request_data == NULL) {
1447 printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1448 " for diag buffers, requested size(%d)\n",
1449 ioc->name, __func__, request_data_sz);
1450 mpt2sas_base_free_smid(ioc, smid);
1453 ioc->diag_buffer[buffer_type] = request_data;
1454 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1455 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1458 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1459 mpi_request->BufferType = diag_register->buffer_type;
1460 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1461 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1462 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1463 mpi_request->VF_ID = 0; /* TODO */
1464 mpi_request->VP_ID = 0;
1466 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(0x%p), "
1467 "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1468 (unsigned long long)request_data_dma,
1469 le32_to_cpu(mpi_request->BufferLength)));
1471 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1472 mpi_request->ProductSpecific[i] =
1473 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1475 mpt2sas_base_put_smid_default(ioc, smid);
1476 init_completion(&ioc->ctl_cmds.done);
1477 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1478 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1480 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1481 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1483 _debug_dump_mf(mpi_request,
1484 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1485 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1487 goto issue_host_reset;
1490 /* process the completed Reply Message Frame */
1491 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1492 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1493 ioc->name, __func__);
1498 mpi_reply = ioc->ctl_cmds.reply;
1499 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1501 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1502 ioc->diag_buffer_status[buffer_type] |=
1503 MPT2_DIAG_BUFFER_IS_REGISTERED;
1504 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1505 ioc->name, __func__));
1507 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1508 "log_info(0x%08x)\n", ioc->name, __func__,
1509 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1515 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1520 if (rc && request_data)
1521 pci_free_consistent(ioc->pdev, request_data_sz,
1522 request_data, request_data_dma);
1524 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1529 * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
1530 * @ioc: per adapter object
1531 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1533 * This is called when command line option diag_buffer_enable is enabled
1534 * at driver load time.
1537 mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
1539 struct mpt2_diag_register diag_register;
1541 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
1543 if (bits_to_register & 1) {
1544 printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
1546 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1547 /* register for 1MB buffers */
1548 diag_register.requested_buffer_size = (1024 * 1024);
1549 diag_register.unique_id = 0x7075900;
1550 _ctl_diag_register_2(ioc, &diag_register);
1553 if (bits_to_register & 2) {
1554 printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
1556 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1557 /* register for 2MB buffers */
1558 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1559 diag_register.unique_id = 0x7075901;
1560 _ctl_diag_register_2(ioc, &diag_register);
1563 if (bits_to_register & 4) {
1564 printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
1566 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1567 /* register for 2MB buffers */
1568 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1569 diag_register.unique_id = 0x7075901;
1570 _ctl_diag_register_2(ioc, &diag_register);
1575 * _ctl_diag_register - application register with driver
1576 * @arg - user space buffer containing ioctl content
1577 * @state - NON_BLOCKING or BLOCKING
1579 * This will allow the driver to setup any required buffers that will be
1580 * needed by firmware to communicate with the driver.
1583 _ctl_diag_register(void __user *arg, enum block_state state)
1585 struct mpt2_diag_register karg;
1586 struct MPT2SAS_ADAPTER *ioc;
1589 if (copy_from_user(&karg, arg, sizeof(karg))) {
1590 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1591 __FILE__, __LINE__, __func__);
1594 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1597 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1599 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1600 return -ERESTARTSYS;
1601 rc = _ctl_diag_register_2(ioc, &karg);
1602 mutex_unlock(&ioc->ctl_cmds.mutex);
1607 * _ctl_diag_unregister - application unregister with driver
1608 * @arg - user space buffer containing ioctl content
1610 * This will allow the driver to cleanup any memory allocated for diag
1611 * messages and to free up any resources.
1614 _ctl_diag_unregister(void __user *arg)
1616 struct mpt2_diag_unregister karg;
1617 struct MPT2SAS_ADAPTER *ioc;
1619 dma_addr_t request_data_dma;
1620 u32 request_data_sz;
1623 if (copy_from_user(&karg, arg, sizeof(karg))) {
1624 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1625 __FILE__, __LINE__, __func__);
1628 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1631 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1634 buffer_type = karg.unique_id & 0x000000ff;
1635 if (!_ctl_diag_capability(ioc, buffer_type)) {
1636 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1637 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1641 if ((ioc->diag_buffer_status[buffer_type] &
1642 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1643 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1644 "registered\n", ioc->name, __func__, buffer_type);
1647 if ((ioc->diag_buffer_status[buffer_type] &
1648 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1649 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1650 "released\n", ioc->name, __func__, buffer_type);
1654 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1655 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1656 "registered\n", ioc->name, __func__, karg.unique_id);
1660 request_data = ioc->diag_buffer[buffer_type];
1661 if (!request_data) {
1662 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1663 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1667 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1668 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1669 pci_free_consistent(ioc->pdev, request_data_sz,
1670 request_data, request_data_dma);
1671 ioc->diag_buffer[buffer_type] = NULL;
1672 ioc->diag_buffer_status[buffer_type] = 0;
1677 * _ctl_diag_query - query relevant info associated with diag buffers
1678 * @arg - user space buffer containing ioctl content
1680 * The application will send only buffer_type and unique_id. Driver will
1681 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1682 * 0x00, the driver will return info specified by Buffer Type.
1685 _ctl_diag_query(void __user *arg)
1687 struct mpt2_diag_query karg;
1688 struct MPT2SAS_ADAPTER *ioc;
1693 if (copy_from_user(&karg, arg, sizeof(karg))) {
1694 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1695 __FILE__, __LINE__, __func__);
1698 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1701 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1704 karg.application_flags = 0;
1705 buffer_type = karg.buffer_type;
1707 if (!_ctl_diag_capability(ioc, buffer_type)) {
1708 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1709 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1713 if ((ioc->diag_buffer_status[buffer_type] &
1714 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1715 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1716 "registered\n", ioc->name, __func__, buffer_type);
1720 if (karg.unique_id & 0xffffff00) {
1721 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1722 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1723 "registered\n", ioc->name, __func__,
1729 request_data = ioc->diag_buffer[buffer_type];
1730 if (!request_data) {
1731 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1732 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1736 if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1737 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1738 MPT2_APP_FLAGS_BUFFER_VALID);
1740 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1741 MPT2_APP_FLAGS_BUFFER_VALID |
1742 MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1744 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1745 karg.product_specific[i] =
1746 ioc->product_specific[buffer_type][i];
1748 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1749 karg.driver_added_buffer_size = 0;
1750 karg.unique_id = ioc->unique_id[buffer_type];
1751 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1753 if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1754 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1755 "data @ %p\n", ioc->name, __func__, arg);
1762 * _ctl_send_release - Diag Release Message
1763 * @ioc: per adapter object
1764 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1765 * @issue_reset - specifies whether host reset is required.
1769 _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1771 Mpi2DiagReleaseRequest_t *mpi_request;
1772 Mpi2DiagReleaseReply_t *mpi_reply;
1777 unsigned long timeleft;
1779 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1785 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1786 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1787 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
1788 "skipping due to FAULT state\n", ioc->name,
1794 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1795 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1796 ioc->name, __func__);
1801 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1803 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1804 ioc->name, __func__);
1809 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1810 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1811 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1812 ioc->ctl_cmds.smid = smid;
1814 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1815 mpi_request->BufferType = buffer_type;
1816 mpi_request->VF_ID = 0; /* TODO */
1817 mpi_request->VP_ID = 0;
1819 mpt2sas_base_put_smid_default(ioc, smid);
1820 init_completion(&ioc->ctl_cmds.done);
1821 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1822 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1824 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1825 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1827 _debug_dump_mf(mpi_request,
1828 sizeof(Mpi2DiagReleaseRequest_t)/4);
1829 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1835 /* process the completed Reply Message Frame */
1836 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1837 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1838 ioc->name, __func__);
1843 mpi_reply = ioc->ctl_cmds.reply;
1844 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1846 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1847 ioc->diag_buffer_status[buffer_type] |=
1848 MPT2_DIAG_BUFFER_IS_RELEASED;
1849 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1850 ioc->name, __func__));
1852 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1853 "log_info(0x%08x)\n", ioc->name, __func__,
1854 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1859 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1864 * _ctl_diag_release - request to send Diag Release Message to firmware
1865 * @arg - user space buffer containing ioctl content
1866 * @state - NON_BLOCKING or BLOCKING
1868 * This allows ownership of the specified buffer to returned to the driver,
1869 * allowing an application to read the buffer without fear that firmware is
1870 * overwritting information in the buffer.
1873 _ctl_diag_release(void __user *arg, enum block_state state)
1875 struct mpt2_diag_release karg;
1876 struct MPT2SAS_ADAPTER *ioc;
1882 if (copy_from_user(&karg, arg, sizeof(karg))) {
1883 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1884 __FILE__, __LINE__, __func__);
1887 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1890 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1893 buffer_type = karg.unique_id & 0x000000ff;
1894 if (!_ctl_diag_capability(ioc, buffer_type)) {
1895 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1896 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1900 if ((ioc->diag_buffer_status[buffer_type] &
1901 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1902 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1903 "registered\n", ioc->name, __func__, buffer_type);
1907 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1908 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1909 "registered\n", ioc->name, __func__, karg.unique_id);
1913 if (ioc->diag_buffer_status[buffer_type] &
1914 MPT2_DIAG_BUFFER_IS_RELEASED) {
1915 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1916 "is already released\n", ioc->name, __func__,
1921 request_data = ioc->diag_buffer[buffer_type];
1923 if (!request_data) {
1924 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1925 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1929 /* buffers were released by due to host reset */
1930 if ((ioc->diag_buffer_status[buffer_type] &
1931 MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1932 ioc->diag_buffer_status[buffer_type] |=
1933 MPT2_DIAG_BUFFER_IS_RELEASED;
1934 ioc->diag_buffer_status[buffer_type] &=
1935 ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1936 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1937 "was released due to host reset\n", ioc->name, __func__,
1942 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1944 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1945 return -ERESTARTSYS;
1947 rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1950 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1953 mutex_unlock(&ioc->ctl_cmds.mutex);
1958 * _ctl_diag_read_buffer - request for copy of the diag buffer
1959 * @arg - user space buffer containing ioctl content
1960 * @state - NON_BLOCKING or BLOCKING
1963 _ctl_diag_read_buffer(void __user *arg, enum block_state state)
1965 struct mpt2_diag_read_buffer karg;
1966 struct mpt2_diag_read_buffer __user *uarg = arg;
1967 struct MPT2SAS_ADAPTER *ioc;
1968 void *request_data, *diag_data;
1969 Mpi2DiagBufferPostRequest_t *mpi_request;
1970 Mpi2DiagBufferPostReply_t *mpi_reply;
1973 unsigned long timeleft, request_size, copy_size;
1978 if (copy_from_user(&karg, arg, sizeof(karg))) {
1979 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1980 __FILE__, __LINE__, __func__);
1983 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1986 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1989 buffer_type = karg.unique_id & 0x000000ff;
1990 if (!_ctl_diag_capability(ioc, buffer_type)) {
1991 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1992 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1996 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1997 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1998 "registered\n", ioc->name, __func__, karg.unique_id);
2002 request_data = ioc->diag_buffer[buffer_type];
2003 if (!request_data) {
2004 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
2005 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
2009 request_size = ioc->diag_buffer_sz[buffer_type];
2011 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2012 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
2013 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
2018 if (karg.starting_offset > request_size)
2021 diag_data = (void *)(request_data + karg.starting_offset);
2022 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
2023 "offset(%d), sz(%d)\n", ioc->name, __func__,
2024 diag_data, karg.starting_offset, karg.bytes_to_read));
2026 /* Truncate data on requests that are too large */
2027 if ((diag_data + karg.bytes_to_read < diag_data) ||
2028 (diag_data + karg.bytes_to_read > request_data + request_size))
2029 copy_size = request_size - karg.starting_offset;
2031 copy_size = karg.bytes_to_read;
2033 if (copy_to_user((void __user *)uarg->diagnostic_data,
2034 diag_data, copy_size)) {
2035 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
2036 "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
2037 __func__, diag_data);
2041 if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
2044 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
2045 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
2046 if ((ioc->diag_buffer_status[buffer_type] &
2047 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
2048 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2049 "buffer_type(0x%02x) is still registered\n", ioc->name,
2050 __func__, buffer_type));
2053 /* Get a free request frame and save the message context.
2055 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
2057 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
2058 return -ERESTARTSYS;
2060 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
2061 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
2062 ioc->name, __func__);
2067 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2069 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2070 ioc->name, __func__);
2076 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
2077 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2078 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2079 ioc->ctl_cmds.smid = smid;
2081 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2082 mpi_request->BufferType = buffer_type;
2083 mpi_request->BufferLength =
2084 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2085 mpi_request->BufferAddress =
2086 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2087 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
2088 mpi_request->ProductSpecific[i] =
2089 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2090 mpi_request->VF_ID = 0; /* TODO */
2091 mpi_request->VP_ID = 0;
2093 mpt2sas_base_put_smid_default(ioc, smid);
2094 init_completion(&ioc->ctl_cmds.done);
2095 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2096 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
2098 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
2099 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
2101 _debug_dump_mf(mpi_request,
2102 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2103 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
2105 goto issue_host_reset;
2108 /* process the completed Reply Message Frame */
2109 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
2110 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
2111 ioc->name, __func__);
2116 mpi_reply = ioc->ctl_cmds.reply;
2117 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2119 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2120 ioc->diag_buffer_status[buffer_type] |=
2121 MPT2_DIAG_BUFFER_IS_REGISTERED;
2122 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
2123 ioc->name, __func__));
2125 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
2126 "log_info(0x%08x)\n", ioc->name, __func__,
2127 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2133 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2138 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
2139 mutex_unlock(&ioc->ctl_cmds.mutex);
2144 * _ctl_ioctl_main - main ioctl entry point
2145 * @file - (struct file)
2146 * @cmd - ioctl opcode
2150 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg)
2152 enum block_state state;
2155 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING :
2160 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
2161 ret = _ctl_getiocinfo(arg);
2165 struct mpt2_ioctl_command karg;
2166 struct mpt2_ioctl_command __user *uarg;
2167 struct MPT2SAS_ADAPTER *ioc;
2169 if (copy_from_user(&karg, arg, sizeof(karg))) {
2170 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2171 __FILE__, __LINE__, __func__);
2175 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2179 if (ioc->shost_recovery || ioc->pci_error_recovery)
2182 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2184 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2188 case MPT2EVENTQUERY:
2189 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2190 ret = _ctl_eventquery(arg);
2192 case MPT2EVENTENABLE:
2193 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2194 ret = _ctl_eventenable(arg);
2196 case MPT2EVENTREPORT:
2197 ret = _ctl_eventreport(arg);
2200 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2201 ret = _ctl_do_reset(arg);
2203 case MPT2BTDHMAPPING:
2204 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2205 ret = _ctl_btdh_mapping(arg);
2207 case MPT2DIAGREGISTER:
2208 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2209 ret = _ctl_diag_register(arg, state);
2211 case MPT2DIAGUNREGISTER:
2212 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2213 ret = _ctl_diag_unregister(arg);
2216 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2217 ret = _ctl_diag_query(arg);
2219 case MPT2DIAGRELEASE:
2220 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2221 ret = _ctl_diag_release(arg, state);
2223 case MPT2DIAGREADBUFFER:
2224 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2225 ret = _ctl_diag_read_buffer(arg, state);
2229 struct mpt2_ioctl_command karg;
2230 struct MPT2SAS_ADAPTER *ioc;
2232 if (copy_from_user(&karg, arg, sizeof(karg))) {
2233 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2234 __FILE__, __LINE__, __func__);
2238 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2242 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
2243 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2251 * _ctl_ioctl - main ioctl entry point (unlocked)
2252 * @file - (struct file)
2253 * @cmd - ioctl opcode
2257 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2261 mutex_lock(&_ctl_mutex);
2262 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2263 mutex_unlock(&_ctl_mutex);
2267 #ifdef CONFIG_COMPAT
2269 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2270 * @file - (struct file)
2271 * @cmd - ioctl opcode
2272 * @arg - (struct mpt2_ioctl_command32)
2274 * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2277 _ctl_compat_mpt_command(struct file *file, unsigned cmd, unsigned long arg)
2279 struct mpt2_ioctl_command32 karg32;
2280 struct mpt2_ioctl_command32 __user *uarg;
2281 struct mpt2_ioctl_command karg;
2282 struct MPT2SAS_ADAPTER *ioc;
2283 enum block_state state;
2285 if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2288 uarg = (struct mpt2_ioctl_command32 __user *) arg;
2290 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2291 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2292 __FILE__, __LINE__, __func__);
2295 if (_ctl_verify_adapter(karg32.hdr.ioc_number, &ioc) == -1 || !ioc)
2298 if (ioc->shost_recovery || ioc->pci_error_recovery)
2301 memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2302 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2303 karg.hdr.port_number = karg32.hdr.port_number;
2304 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2305 karg.timeout = karg32.timeout;
2306 karg.max_reply_bytes = karg32.max_reply_bytes;
2307 karg.data_in_size = karg32.data_in_size;
2308 karg.data_out_size = karg32.data_out_size;
2309 karg.max_sense_bytes = karg32.max_sense_bytes;
2310 karg.data_sge_offset = karg32.data_sge_offset;
2311 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2312 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2313 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2314 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2315 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2316 return _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2320 * _ctl_ioctl_compat - main ioctl entry point (compat)
2325 * This routine handles 32 bit applications in 64bit os.
2328 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2332 mutex_lock(&_ctl_mutex);
2333 if (cmd == MPT2COMMAND32)
2334 ret = _ctl_compat_mpt_command(file, cmd, arg);
2336 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2337 mutex_unlock(&_ctl_mutex);
2342 /* scsi host attributes */
2345 * _ctl_version_fw_show - firmware version
2346 * @cdev - pointer to embedded class device
2347 * @buf - the buffer returned
2349 * A sysfs 'read-only' shost attribute.
2352 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2355 struct Scsi_Host *shost = class_to_shost(cdev);
2356 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2358 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2359 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2360 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2361 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2362 ioc->facts.FWVersion.Word & 0x000000FF);
2364 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2367 * _ctl_version_bios_show - bios version
2368 * @cdev - pointer to embedded class device
2369 * @buf - the buffer returned
2371 * A sysfs 'read-only' shost attribute.
2374 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2377 struct Scsi_Host *shost = class_to_shost(cdev);
2378 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2380 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2382 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2383 (version & 0xFF000000) >> 24,
2384 (version & 0x00FF0000) >> 16,
2385 (version & 0x0000FF00) >> 8,
2386 version & 0x000000FF);
2388 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2391 * _ctl_version_mpi_show - MPI (message passing interface) version
2392 * @cdev - pointer to embedded class device
2393 * @buf - the buffer returned
2395 * A sysfs 'read-only' shost attribute.
2398 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2401 struct Scsi_Host *shost = class_to_shost(cdev);
2402 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2404 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2405 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2407 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2410 * _ctl_version_product_show - product name
2411 * @cdev - pointer to embedded class device
2412 * @buf - the buffer returned
2414 * A sysfs 'read-only' shost attribute.
2417 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2420 struct Scsi_Host *shost = class_to_shost(cdev);
2421 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2423 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2425 static DEVICE_ATTR(version_product, S_IRUGO,
2426 _ctl_version_product_show, NULL);
2429 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2430 * @cdev - pointer to embedded class device
2431 * @buf - the buffer returned
2433 * A sysfs 'read-only' shost attribute.
2436 _ctl_version_nvdata_persistent_show(struct device *cdev,
2437 struct device_attribute *attr, char *buf)
2439 struct Scsi_Host *shost = class_to_shost(cdev);
2440 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2442 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2443 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2445 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2446 _ctl_version_nvdata_persistent_show, NULL);
2449 * _ctl_version_nvdata_default_show - nvdata default version
2450 * @cdev - pointer to embedded class device
2451 * @buf - the buffer returned
2453 * A sysfs 'read-only' shost attribute.
2456 _ctl_version_nvdata_default_show(struct device *cdev,
2457 struct device_attribute *attr, char *buf)
2459 struct Scsi_Host *shost = class_to_shost(cdev);
2460 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2462 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2463 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2465 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2466 _ctl_version_nvdata_default_show, NULL);
2469 * _ctl_board_name_show - board name
2470 * @cdev - pointer to embedded class device
2471 * @buf - the buffer returned
2473 * A sysfs 'read-only' shost attribute.
2476 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2479 struct Scsi_Host *shost = class_to_shost(cdev);
2480 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2482 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2484 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2487 * _ctl_board_assembly_show - board assembly name
2488 * @cdev - pointer to embedded class device
2489 * @buf - the buffer returned
2491 * A sysfs 'read-only' shost attribute.
2494 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2497 struct Scsi_Host *shost = class_to_shost(cdev);
2498 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2500 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2502 static DEVICE_ATTR(board_assembly, S_IRUGO,
2503 _ctl_board_assembly_show, NULL);
2506 * _ctl_board_tracer_show - board tracer number
2507 * @cdev - pointer to embedded class device
2508 * @buf - the buffer returned
2510 * A sysfs 'read-only' shost attribute.
2513 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2516 struct Scsi_Host *shost = class_to_shost(cdev);
2517 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2519 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2521 static DEVICE_ATTR(board_tracer, S_IRUGO,
2522 _ctl_board_tracer_show, NULL);
2525 * _ctl_io_delay_show - io missing delay
2526 * @cdev - pointer to embedded class device
2527 * @buf - the buffer returned
2529 * This is for firmware implemention for deboucing device
2532 * A sysfs 'read-only' shost attribute.
2535 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2538 struct Scsi_Host *shost = class_to_shost(cdev);
2539 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2541 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2543 static DEVICE_ATTR(io_delay, S_IRUGO,
2544 _ctl_io_delay_show, NULL);
2547 * _ctl_device_delay_show - device missing delay
2548 * @cdev - pointer to embedded class device
2549 * @buf - the buffer returned
2551 * This is for firmware implemention for deboucing device
2554 * A sysfs 'read-only' shost attribute.
2557 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2560 struct Scsi_Host *shost = class_to_shost(cdev);
2561 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2563 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2565 static DEVICE_ATTR(device_delay, S_IRUGO,
2566 _ctl_device_delay_show, NULL);
2569 * _ctl_fw_queue_depth_show - global credits
2570 * @cdev - pointer to embedded class device
2571 * @buf - the buffer returned
2573 * This is firmware queue depth limit
2575 * A sysfs 'read-only' shost attribute.
2578 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2581 struct Scsi_Host *shost = class_to_shost(cdev);
2582 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2584 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2586 static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2587 _ctl_fw_queue_depth_show, NULL);
2590 * _ctl_sas_address_show - sas address
2591 * @cdev - pointer to embedded class device
2592 * @buf - the buffer returned
2594 * This is the controller sas address
2596 * A sysfs 'read-only' shost attribute.
2599 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2602 struct Scsi_Host *shost = class_to_shost(cdev);
2603 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2605 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2606 (unsigned long long)ioc->sas_hba.sas_address);
2608 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2609 _ctl_host_sas_address_show, NULL);
2612 * _ctl_logging_level_show - logging level
2613 * @cdev - pointer to embedded class device
2614 * @buf - the buffer returned
2616 * A sysfs 'read/write' shost attribute.
2619 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2622 struct Scsi_Host *shost = class_to_shost(cdev);
2623 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2625 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2628 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2629 const char *buf, size_t count)
2631 struct Scsi_Host *shost = class_to_shost(cdev);
2632 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2635 if (sscanf(buf, "%x", &val) != 1)
2638 ioc->logging_level = val;
2639 printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2640 ioc->logging_level);
2643 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2644 _ctl_logging_level_show, _ctl_logging_level_store);
2646 /* device attributes */
2648 * _ctl_fwfault_debug_show - show/store fwfault_debug
2649 * @cdev - pointer to embedded class device
2650 * @buf - the buffer returned
2652 * mpt2sas_fwfault_debug is command line option
2653 * A sysfs 'read/write' shost attribute.
2656 _ctl_fwfault_debug_show(struct device *cdev,
2657 struct device_attribute *attr, char *buf)
2659 struct Scsi_Host *shost = class_to_shost(cdev);
2660 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2662 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2665 _ctl_fwfault_debug_store(struct device *cdev,
2666 struct device_attribute *attr, const char *buf, size_t count)
2668 struct Scsi_Host *shost = class_to_shost(cdev);
2669 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2672 if (sscanf(buf, "%d", &val) != 1)
2675 ioc->fwfault_debug = val;
2676 printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
2677 ioc->fwfault_debug);
2680 static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2681 _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2685 * _ctl_ioc_reset_count_show - ioc reset count
2686 * @cdev - pointer to embedded class device
2687 * @buf - the buffer returned
2689 * This is firmware queue depth limit
2691 * A sysfs 'read-only' shost attribute.
2694 _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2697 struct Scsi_Host *shost = class_to_shost(cdev);
2698 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2700 return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
2702 static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
2703 _ctl_ioc_reset_count_show, NULL);
2705 struct DIAG_BUFFER_START {
2715 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2716 * @cdev - pointer to embedded class device
2717 * @buf - the buffer returned
2719 * A sysfs 'read-only' shost attribute.
2722 _ctl_host_trace_buffer_size_show(struct device *cdev,
2723 struct device_attribute *attr, char *buf)
2725 struct Scsi_Host *shost = class_to_shost(cdev);
2726 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2728 struct DIAG_BUFFER_START *request_data;
2730 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2731 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2732 "registered\n", ioc->name, __func__);
2736 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2737 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2738 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2739 "registered\n", ioc->name, __func__);
2743 request_data = (struct DIAG_BUFFER_START *)
2744 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2745 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2746 le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
2747 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2748 size = le32_to_cpu(request_data->Size);
2750 ioc->ring_buffer_sz = size;
2751 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2753 static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2754 _ctl_host_trace_buffer_size_show, NULL);
2757 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2758 * @cdev - pointer to embedded class device
2759 * @buf - the buffer returned
2761 * A sysfs 'read/write' shost attribute.
2763 * You will only be able to read 4k bytes of ring buffer at a time.
2764 * In order to read beyond 4k bytes, you will have to write out the
2765 * offset to the same attribute, it will move the pointer.
2768 _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2771 struct Scsi_Host *shost = class_to_shost(cdev);
2772 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2776 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2777 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2778 "registered\n", ioc->name, __func__);
2782 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2783 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2784 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2785 "registered\n", ioc->name, __func__);
2789 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2792 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2793 size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
2794 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2795 memcpy(buf, request_data, size);
2800 _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2801 const char *buf, size_t count)
2803 struct Scsi_Host *shost = class_to_shost(cdev);
2804 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2807 if (sscanf(buf, "%d", &val) != 1)
2810 ioc->ring_buffer_offset = val;
2813 static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2814 _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2816 /*****************************************/
2819 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2820 * @cdev - pointer to embedded class device
2821 * @buf - the buffer returned
2823 * A sysfs 'read/write' shost attribute.
2825 * This is a mechnism to post/release host_trace_buffers
2828 _ctl_host_trace_buffer_enable_show(struct device *cdev,
2829 struct device_attribute *attr, char *buf)
2831 struct Scsi_Host *shost = class_to_shost(cdev);
2832 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2834 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
2835 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2836 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
2837 return snprintf(buf, PAGE_SIZE, "off\n");
2838 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2839 MPT2_DIAG_BUFFER_IS_RELEASED))
2840 return snprintf(buf, PAGE_SIZE, "release\n");
2842 return snprintf(buf, PAGE_SIZE, "post\n");
2846 _ctl_host_trace_buffer_enable_store(struct device *cdev,
2847 struct device_attribute *attr, const char *buf, size_t count)
2849 struct Scsi_Host *shost = class_to_shost(cdev);
2850 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2852 struct mpt2_diag_register diag_register;
2855 if (sscanf(buf, "%s", str) != 1)
2858 if (!strcmp(str, "post")) {
2859 /* exit out if host buffers are already posted */
2860 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
2861 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2862 MPT2_DIAG_BUFFER_IS_REGISTERED) &&
2863 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2864 MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
2866 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
2867 printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
2869 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
2870 diag_register.requested_buffer_size = (1024 * 1024);
2871 diag_register.unique_id = 0x7075900;
2872 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
2873 _ctl_diag_register_2(ioc, &diag_register);
2874 } else if (!strcmp(str, "release")) {
2875 /* exit out if host buffers are already released */
2876 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
2878 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2879 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
2881 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2882 MPT2_DIAG_BUFFER_IS_RELEASED))
2884 printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
2886 _ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
2892 static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
2893 _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
2895 struct device_attribute *mpt2sas_host_attrs[] = {
2896 &dev_attr_version_fw,
2897 &dev_attr_version_bios,
2898 &dev_attr_version_mpi,
2899 &dev_attr_version_product,
2900 &dev_attr_version_nvdata_persistent,
2901 &dev_attr_version_nvdata_default,
2902 &dev_attr_board_name,
2903 &dev_attr_board_assembly,
2904 &dev_attr_board_tracer,
2906 &dev_attr_device_delay,
2907 &dev_attr_logging_level,
2908 &dev_attr_fwfault_debug,
2909 &dev_attr_fw_queue_depth,
2910 &dev_attr_host_sas_address,
2911 &dev_attr_ioc_reset_count,
2912 &dev_attr_host_trace_buffer_size,
2913 &dev_attr_host_trace_buffer,
2914 &dev_attr_host_trace_buffer_enable,
2919 * _ctl_device_sas_address_show - sas address
2920 * @cdev - pointer to embedded class device
2921 * @buf - the buffer returned
2923 * This is the sas address for the target
2925 * A sysfs 'read-only' shost attribute.
2928 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2931 struct scsi_device *sdev = to_scsi_device(dev);
2932 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2934 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2935 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2937 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2940 * _ctl_device_handle_show - device handle
2941 * @cdev - pointer to embedded class device
2942 * @buf - the buffer returned
2944 * This is the firmware assigned device handle
2946 * A sysfs 'read-only' shost attribute.
2949 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
2952 struct scsi_device *sdev = to_scsi_device(dev);
2953 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2955 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
2956 sas_device_priv_data->sas_target->handle);
2958 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
2960 struct device_attribute *mpt2sas_dev_attrs[] = {
2961 &dev_attr_sas_address,
2962 &dev_attr_sas_device_handle,
2966 static const struct file_operations ctl_fops = {
2967 .owner = THIS_MODULE,
2968 .unlocked_ioctl = _ctl_ioctl,
2969 .release = _ctl_release,
2971 .fasync = _ctl_fasync,
2972 #ifdef CONFIG_COMPAT
2973 .compat_ioctl = _ctl_ioctl_compat,
2975 .llseek = noop_llseek,
2978 static struct miscdevice ctl_dev = {
2979 .minor = MPT2SAS_MINOR,
2980 .name = MPT2SAS_DEV_NAME,
2985 * mpt2sas_ctl_init - main entry point for ctl.
2989 mpt2sas_ctl_init(void)
2992 if (misc_register(&ctl_dev) < 0)
2993 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
2994 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
2996 init_waitqueue_head(&ctl_poll_wait);
3000 * mpt2sas_ctl_exit - exit point for ctl
3004 mpt2sas_ctl_exit(void)
3006 struct MPT2SAS_ADAPTER *ioc;
3009 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
3011 /* free memory associated to diag buffers */
3012 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3013 if (!ioc->diag_buffer[i])
3015 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3016 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3017 ioc->diag_buffer[i] = NULL;
3018 ioc->diag_buffer_status[i] = 0;
3021 kfree(ioc->event_log);
3023 misc_deregister(&ctl_dev);