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-2014 LSI Corporation
7 * Copyright (C) 20013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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.
404 mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
407 Mpi2EventNotificationReply_t *mpi_reply;
409 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
410 if (unlikely(!mpi_reply)) {
411 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
412 ioc->name, __FILE__, __LINE__, __func__);
415 mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
420 * _ctl_verify_adapter - validates ioc_number passed from application
421 * @ioc: per adapter object
422 * @iocpp: The ioc pointer is returned in this.
424 * Return (-1) means error, else ioc_number.
427 _ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
429 struct MPT2SAS_ADAPTER *ioc;
430 /* global ioc lock to protect controller on list operations */
431 spin_lock(&gioc_lock);
432 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
433 if (ioc->id != ioc_number)
435 spin_unlock(&gioc_lock);
439 spin_unlock(&gioc_lock);
445 * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
446 * @ioc: per adapter object
447 * @reset_phase: phase
449 * The handler for doing any required cleanup or initialization.
451 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
452 * MPT2_IOC_DONE_RESET
455 mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
460 switch (reset_phase) {
461 case MPT2_IOC_PRE_RESET:
462 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
463 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
464 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
465 if (!(ioc->diag_buffer_status[i] &
466 MPT2_DIAG_BUFFER_IS_REGISTERED))
468 if ((ioc->diag_buffer_status[i] &
469 MPT2_DIAG_BUFFER_IS_RELEASED))
471 _ctl_send_release(ioc, i, &issue_reset);
474 case MPT2_IOC_AFTER_RESET:
475 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
476 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
477 if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
478 ioc->ctl_cmds.status |= MPT2_CMD_RESET;
479 mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
480 complete(&ioc->ctl_cmds.done);
483 case MPT2_IOC_DONE_RESET:
484 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
485 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
487 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
488 if (!(ioc->diag_buffer_status[i] &
489 MPT2_DIAG_BUFFER_IS_REGISTERED))
491 if ((ioc->diag_buffer_status[i] &
492 MPT2_DIAG_BUFFER_IS_RELEASED))
494 ioc->diag_buffer_status[i] |=
495 MPT2_DIAG_BUFFER_IS_DIAG_RESET;
507 * Called when application request fasyn callback handler.
510 _ctl_fasync(int fd, struct file *filep, int mode)
512 return fasync_helper(fd, filep, mode, &async_queue);
522 _ctl_poll(struct file *filep, poll_table *wait)
524 struct MPT2SAS_ADAPTER *ioc;
526 poll_wait(filep, &ctl_poll_wait, wait);
528 /* global ioc lock to protect controller on list operations */
529 spin_lock(&gioc_lock);
530 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
531 if (ioc->aen_event_read_flag) {
532 spin_unlock(&gioc_lock);
533 return POLLIN | POLLRDNORM;
536 spin_unlock(&gioc_lock);
541 * _ctl_set_task_mid - assign an active smid to tm request
542 * @ioc: per adapter object
543 * @karg - (struct mpt2_ioctl_command)
544 * @tm_request - pointer to mf from user space
546 * Returns 0 when an smid if found, else fail.
547 * during failure, the reply frame is filled.
550 _ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
551 Mpi2SCSITaskManagementRequest_t *tm_request)
556 struct scsi_cmnd *scmd;
557 struct MPT2SAS_DEVICE *priv_data;
559 Mpi2SCSITaskManagementReply_t *tm_reply;
564 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
566 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
571 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
573 handle = le16_to_cpu(tm_request->DevHandle);
574 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
575 for (i = ioc->scsiio_depth; i && !found; i--) {
576 scmd = ioc->scsi_lookup[i - 1].scmd;
577 if (scmd == NULL || scmd->device == NULL ||
578 scmd->device->hostdata == NULL)
580 if (lun != scmd->device->lun)
582 priv_data = scmd->device->hostdata;
583 if (priv_data->sas_target == NULL)
585 if (priv_data->sas_target->handle != handle)
587 tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
590 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
593 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
594 "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
595 desc, le16_to_cpu(tm_request->DevHandle), lun));
596 tm_reply = ioc->ctl_cmds.reply;
597 tm_reply->DevHandle = tm_request->DevHandle;
598 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
599 tm_reply->TaskType = tm_request->TaskType;
600 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
601 tm_reply->VP_ID = tm_request->VP_ID;
602 tm_reply->VF_ID = tm_request->VF_ID;
603 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
604 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
606 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
611 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
612 "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
613 desc, le16_to_cpu(tm_request->DevHandle), lun,
614 le16_to_cpu(tm_request->TaskMID)));
619 * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
620 * @ioc: per adapter object
621 * @karg - (struct mpt2_ioctl_command)
622 * @mf - pointer to mf in user space
625 _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command karg,
628 MPI2RequestHeader_t *mpi_request = NULL, *request;
629 MPI2DefaultReply_t *mpi_reply;
633 unsigned long timeout, timeleft;
637 void *data_out = NULL;
638 dma_addr_t data_out_dma;
639 size_t data_out_sz = 0;
640 void *data_in = NULL;
641 dma_addr_t data_in_dma;
642 size_t data_in_sz = 0;
645 u16 wait_state_count;
649 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
650 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
651 ioc->name, __func__);
656 wait_state_count = 0;
657 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
658 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
659 if (wait_state_count++ == 10) {
660 printk(MPT2SAS_ERR_FMT
661 "%s: failed due to ioc not operational\n",
662 ioc->name, __func__);
667 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
668 printk(MPT2SAS_INFO_FMT "%s: waiting for "
669 "operational state(count=%d)\n", ioc->name,
670 __func__, wait_state_count);
672 if (wait_state_count)
673 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
674 ioc->name, __func__);
676 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
678 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a memory for "
679 "mpi_request\n", ioc->name, __func__);
684 /* Check for overflow and wraparound */
685 if (karg.data_sge_offset * 4 > ioc->request_sz ||
686 karg.data_sge_offset > (UINT_MAX / 4)) {
691 /* copy in request message frame from user */
692 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
693 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
699 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
700 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
702 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
703 ioc->name, __func__);
709 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
711 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
712 ioc->name, __func__);
719 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
720 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
721 request = mpt2sas_base_get_msg_frame(ioc, smid);
722 memcpy(request, mpi_request, karg.data_sge_offset*4);
723 ioc->ctl_cmds.smid = smid;
724 data_out_sz = karg.data_out_size;
725 data_in_sz = karg.data_in_size;
727 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
728 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
729 if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
730 le16_to_cpu(mpi_request->FunctionDependent1) >
731 ioc->facts.MaxDevHandle) {
733 mpt2sas_base_free_smid(ioc, smid);
738 /* obtain dma-able memory for data transfer */
739 if (data_out_sz) /* WRITE */ {
740 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
743 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
746 mpt2sas_base_free_smid(ioc, smid);
749 if (copy_from_user(data_out, karg.data_out_buf_ptr,
751 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
754 mpt2sas_base_free_smid(ioc, smid);
759 if (data_in_sz) /* READ */ {
760 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
763 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
766 mpt2sas_base_free_smid(ioc, smid);
771 /* add scatter gather elements */
772 psge = (void *)request + (karg.data_sge_offset*4);
774 if (!data_out_sz && !data_in_sz) {
775 mpt2sas_base_build_zero_len_sge(ioc, psge);
776 } else if (data_out_sz && data_in_sz) {
777 /* WRITE sgel first */
778 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
779 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
780 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
781 ioc->base_add_sg_single(psge, sgl_flags |
782 data_out_sz, data_out_dma);
785 psge += ioc->sge_size;
788 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
789 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
790 MPI2_SGE_FLAGS_END_OF_LIST);
791 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
792 ioc->base_add_sg_single(psge, sgl_flags |
793 data_in_sz, data_in_dma);
794 } else if (data_out_sz) /* WRITE */ {
795 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
796 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
797 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
798 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
799 ioc->base_add_sg_single(psge, sgl_flags |
800 data_out_sz, data_out_dma);
801 } else if (data_in_sz) /* READ */ {
802 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
803 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
804 MPI2_SGE_FLAGS_END_OF_LIST);
805 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
806 ioc->base_add_sg_single(psge, sgl_flags |
807 data_in_sz, data_in_dma);
810 /* send command to firmware */
811 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
812 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
815 init_completion(&ioc->ctl_cmds.done);
816 switch (mpi_request->Function) {
817 case MPI2_FUNCTION_SCSI_IO_REQUEST:
818 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
820 Mpi2SCSIIORequest_t *scsiio_request =
821 (Mpi2SCSIIORequest_t *)request;
822 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
823 scsiio_request->SenseBufferLowAddress =
824 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
825 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
826 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
827 mpt2sas_base_put_smid_scsi_io(ioc, smid,
828 le16_to_cpu(mpi_request->FunctionDependent1));
830 mpt2sas_base_put_smid_default(ioc, smid);
833 case MPI2_FUNCTION_SCSI_TASK_MGMT:
835 Mpi2SCSITaskManagementRequest_t *tm_request =
836 (Mpi2SCSITaskManagementRequest_t *)request;
838 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
839 "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
840 le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
842 if (tm_request->TaskType ==
843 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
844 tm_request->TaskType ==
845 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
846 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
847 mpt2sas_base_free_smid(ioc, smid);
852 mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
853 tm_request->DevHandle));
854 mpt2sas_base_put_smid_hi_priority(ioc, smid);
857 case MPI2_FUNCTION_SMP_PASSTHROUGH:
859 Mpi2SmpPassthroughRequest_t *smp_request =
860 (Mpi2SmpPassthroughRequest_t *)mpi_request;
863 /* ioc determines which port to use */
864 smp_request->PhysicalPort = 0xFF;
865 if (smp_request->PassthroughFlags &
866 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
867 data = (u8 *)&smp_request->SGL;
869 if (unlikely(data_out == NULL)) {
870 printk(KERN_ERR "failure at %s:%d/%s()!\n",
871 __FILE__, __LINE__, __func__);
872 mpt2sas_base_free_smid(ioc, smid);
879 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
880 ioc->ioc_link_reset_in_progress = 1;
881 ioc->ignore_loginfos = 1;
883 mpt2sas_base_put_smid_default(ioc, smid);
886 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
888 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
889 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
891 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
892 || sasiounit_request->Operation ==
893 MPI2_SAS_OP_PHY_LINK_RESET) {
894 ioc->ioc_link_reset_in_progress = 1;
895 ioc->ignore_loginfos = 1;
897 mpt2sas_base_put_smid_default(ioc, smid);
901 mpt2sas_base_put_smid_default(ioc, smid);
905 if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
906 timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
908 timeout = karg.timeout;
909 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
911 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
912 Mpi2SCSITaskManagementRequest_t *tm_request =
913 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
914 mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
915 tm_request->DevHandle));
916 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
917 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
918 ioc->ioc_link_reset_in_progress) {
919 ioc->ioc_link_reset_in_progress = 0;
920 ioc->ignore_loginfos = 0;
922 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
923 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
925 _debug_dump_mf(mpi_request, karg.data_sge_offset);
926 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
928 goto issue_host_reset;
931 mpi_reply = ioc->ctl_cmds.reply;
932 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
934 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
935 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
936 (ioc->logging_level & MPT_DEBUG_TM)) {
937 Mpi2SCSITaskManagementReply_t *tm_reply =
938 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
940 printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
941 "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
942 "TerminationCount(0x%08x)\n", ioc->name,
943 le16_to_cpu(tm_reply->IOCStatus),
944 le32_to_cpu(tm_reply->IOCLogInfo),
945 le32_to_cpu(tm_reply->TerminationCount));
948 /* copy out xdata to user */
950 if (copy_to_user(karg.data_in_buf_ptr, data_in,
952 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
959 /* copy out reply message frame to user */
960 if (karg.max_reply_bytes) {
961 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
962 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
964 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
971 /* copy out sense to user */
972 if (karg.max_sense_bytes && (mpi_request->Function ==
973 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
974 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
975 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
976 if (copy_to_user(karg.sense_data_ptr,
977 ioc->ctl_cmds.sense, sz)) {
978 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
988 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
989 mpi_request->Function ==
990 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
991 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
992 printk(MPT2SAS_INFO_FMT "issue target reset: handle "
993 "= (0x%04x)\n", ioc->name,
994 le16_to_cpu(mpi_request->FunctionDependent1));
995 mpt2sas_halt_firmware(ioc);
996 mpt2sas_scsih_issue_tm(ioc,
997 le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
998 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
1000 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1002 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1008 /* free memory associated with sg buffers */
1010 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1014 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1018 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1023 * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
1024 * @ioc: per adapter object
1025 * @arg - user space buffer containing ioctl content
1028 _ctl_getiocinfo(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1030 struct mpt2_ioctl_iocinfo karg;
1032 if (copy_from_user(&karg, arg, sizeof(karg))) {
1033 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1034 __FILE__, __LINE__, __func__);
1038 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1041 memset(&karg, 0 , sizeof(karg));
1042 if (ioc->is_warpdrive)
1043 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1045 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1047 karg.port_number = ioc->pfacts[0].PortNumber;
1048 karg.hw_rev = ioc->pdev->revision;
1049 karg.pci_id = ioc->pdev->device;
1050 karg.subsystem_device = ioc->pdev->subsystem_device;
1051 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1052 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1053 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1054 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1055 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1056 karg.firmware_version = ioc->facts.FWVersion.Word;
1057 strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
1058 strcat(karg.driver_version, "-");
1059 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1060 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
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 * @ioc: per adapter object
1073 * @arg - user space buffer containing ioctl content
1076 _ctl_eventquery(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1078 struct mpt2_ioctl_eventquery karg;
1080 if (copy_from_user(&karg, arg, sizeof(karg))) {
1081 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1082 __FILE__, __LINE__, __func__);
1086 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1089 karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
1090 memcpy(karg.event_types, ioc->event_type,
1091 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1093 if (copy_to_user(arg, &karg, sizeof(karg))) {
1094 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1095 __FILE__, __LINE__, __func__);
1102 * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1103 * @ioc: per adapter object
1104 * @arg - user space buffer containing ioctl content
1107 _ctl_eventenable(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1109 struct mpt2_ioctl_eventenable karg;
1111 if (copy_from_user(&karg, arg, sizeof(karg))) {
1112 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1113 __FILE__, __LINE__, __func__);
1117 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1122 memcpy(ioc->event_type, karg.event_types,
1123 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1124 mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1126 /* initialize event_log */
1127 ioc->event_context = 0;
1128 ioc->aen_event_read_flag = 0;
1129 ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1130 sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1131 if (!ioc->event_log) {
1132 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1133 __FILE__, __LINE__, __func__);
1140 * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1141 * @ioc: per adapter object
1142 * @arg - user space buffer containing ioctl content
1145 _ctl_eventreport(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1147 struct mpt2_ioctl_eventreport karg;
1148 u32 number_bytes, max_events, max;
1149 struct mpt2_ioctl_eventreport __user *uarg = arg;
1151 if (copy_from_user(&karg, arg, sizeof(karg))) {
1152 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1153 __FILE__, __LINE__, __func__);
1157 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1160 number_bytes = karg.hdr.max_data_size -
1161 sizeof(struct mpt2_ioctl_header);
1162 max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1163 max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1165 /* If fewer than 1 event is requested, there must have
1166 * been some type of error.
1168 if (!max || !ioc->event_log)
1171 number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1172 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1173 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1174 __FILE__, __LINE__, __func__);
1178 /* reset flag so SIGIO can restart */
1179 ioc->aen_event_read_flag = 0;
1184 * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1185 * @ioc: per adapter object
1186 * @arg - user space buffer containing ioctl content
1189 _ctl_do_reset(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1191 struct mpt2_ioctl_diag_reset karg;
1194 if (copy_from_user(&karg, arg, sizeof(karg))) {
1195 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1196 __FILE__, __LINE__, __func__);
1200 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1201 ioc->is_driver_loading)
1203 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1206 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1208 printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1209 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1214 * _ctl_btdh_search_sas_device - searching for sas device
1215 * @ioc: per adapter object
1216 * @btdh: btdh ioctl payload
1219 _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1220 struct mpt2_ioctl_btdh_mapping *btdh)
1222 struct _sas_device *sas_device;
1223 unsigned long flags;
1226 if (list_empty(&ioc->sas_device_list))
1229 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1230 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1231 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1232 btdh->handle == sas_device->handle) {
1233 btdh->bus = sas_device->channel;
1234 btdh->id = sas_device->id;
1237 } else if (btdh->bus == sas_device->channel && btdh->id ==
1238 sas_device->id && btdh->handle == 0xFFFF) {
1239 btdh->handle = sas_device->handle;
1245 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1250 * _ctl_btdh_search_raid_device - searching for raid device
1251 * @ioc: per adapter object
1252 * @btdh: btdh ioctl payload
1255 _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1256 struct mpt2_ioctl_btdh_mapping *btdh)
1258 struct _raid_device *raid_device;
1259 unsigned long flags;
1262 if (list_empty(&ioc->raid_device_list))
1265 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1266 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1267 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1268 btdh->handle == raid_device->handle) {
1269 btdh->bus = raid_device->channel;
1270 btdh->id = raid_device->id;
1273 } else if (btdh->bus == raid_device->channel && btdh->id ==
1274 raid_device->id && btdh->handle == 0xFFFF) {
1275 btdh->handle = raid_device->handle;
1281 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1286 * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1287 * @ioc: per adapter object
1288 * @arg - user space buffer containing ioctl content
1291 _ctl_btdh_mapping(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1293 struct mpt2_ioctl_btdh_mapping karg;
1296 if (copy_from_user(&karg, arg, sizeof(karg))) {
1297 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1298 __FILE__, __LINE__, __func__);
1302 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1305 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1307 _ctl_btdh_search_raid_device(ioc, &karg);
1309 if (copy_to_user(arg, &karg, sizeof(karg))) {
1310 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1311 __FILE__, __LINE__, __func__);
1318 * _ctl_diag_capability - return diag buffer capability
1319 * @ioc: per adapter object
1320 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1322 * returns 1 when diag buffer support is enabled in firmware
1325 _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1329 switch (buffer_type) {
1330 case MPI2_DIAG_BUF_TYPE_TRACE:
1331 if (ioc->facts.IOCCapabilities &
1332 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1335 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1336 if (ioc->facts.IOCCapabilities &
1337 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1340 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1341 if (ioc->facts.IOCCapabilities &
1342 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1350 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1351 * @ioc: per adapter object
1352 * @diag_register: the diag_register struct passed in from user space
1356 _ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
1357 struct mpt2_diag_register *diag_register)
1360 void *request_data = NULL;
1361 dma_addr_t request_data_dma;
1362 u32 request_data_sz = 0;
1363 Mpi2DiagBufferPostRequest_t *mpi_request;
1364 Mpi2DiagBufferPostReply_t *mpi_reply;
1366 unsigned long timeleft;
1371 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1374 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1375 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1376 ioc->name, __func__);
1381 buffer_type = diag_register->buffer_type;
1382 if (!_ctl_diag_capability(ioc, buffer_type)) {
1383 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1384 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1388 if (ioc->diag_buffer_status[buffer_type] &
1389 MPT2_DIAG_BUFFER_IS_REGISTERED) {
1390 printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1391 "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1396 if (diag_register->requested_buffer_size % 4) {
1397 printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1398 "is not 4 byte aligned\n", ioc->name, __func__);
1402 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1404 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1405 ioc->name, __func__);
1411 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1412 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1413 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1414 ioc->ctl_cmds.smid = smid;
1416 request_data = ioc->diag_buffer[buffer_type];
1417 request_data_sz = diag_register->requested_buffer_size;
1418 ioc->unique_id[buffer_type] = diag_register->unique_id;
1419 ioc->diag_buffer_status[buffer_type] = 0;
1420 memcpy(ioc->product_specific[buffer_type],
1421 diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
1422 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1425 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1426 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1427 pci_free_consistent(ioc->pdev,
1428 ioc->diag_buffer_sz[buffer_type],
1429 request_data, request_data_dma);
1430 request_data = NULL;
1434 if (request_data == NULL) {
1435 ioc->diag_buffer_sz[buffer_type] = 0;
1436 ioc->diag_buffer_dma[buffer_type] = 0;
1437 request_data = pci_alloc_consistent(
1438 ioc->pdev, request_data_sz, &request_data_dma);
1439 if (request_data == NULL) {
1440 printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1441 " for diag buffers, requested size(%d)\n",
1442 ioc->name, __func__, request_data_sz);
1443 mpt2sas_base_free_smid(ioc, smid);
1446 ioc->diag_buffer[buffer_type] = request_data;
1447 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1448 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1451 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1452 mpi_request->BufferType = diag_register->buffer_type;
1453 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1454 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1455 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1456 mpi_request->VF_ID = 0; /* TODO */
1457 mpi_request->VP_ID = 0;
1459 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(0x%p), "
1460 "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1461 (unsigned long long)request_data_dma,
1462 le32_to_cpu(mpi_request->BufferLength)));
1464 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1465 mpi_request->ProductSpecific[i] =
1466 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1468 init_completion(&ioc->ctl_cmds.done);
1469 mpt2sas_base_put_smid_default(ioc, smid);
1470 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1471 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1473 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1474 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1476 _debug_dump_mf(mpi_request,
1477 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1478 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1480 goto issue_host_reset;
1483 /* process the completed Reply Message Frame */
1484 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1485 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1486 ioc->name, __func__);
1491 mpi_reply = ioc->ctl_cmds.reply;
1492 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1494 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1495 ioc->diag_buffer_status[buffer_type] |=
1496 MPT2_DIAG_BUFFER_IS_REGISTERED;
1497 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1498 ioc->name, __func__));
1500 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1501 "log_info(0x%08x)\n", ioc->name, __func__,
1502 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1508 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1513 if (rc && request_data)
1514 pci_free_consistent(ioc->pdev, request_data_sz,
1515 request_data, request_data_dma);
1517 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1522 * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
1523 * @ioc: per adapter object
1524 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1526 * This is called when command line option diag_buffer_enable is enabled
1527 * at driver load time.
1530 mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
1532 struct mpt2_diag_register diag_register;
1534 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
1536 if (bits_to_register & 1) {
1537 printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
1539 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1540 /* register for 1MB buffers */
1541 diag_register.requested_buffer_size = (1024 * 1024);
1542 diag_register.unique_id = 0x7075900;
1543 _ctl_diag_register_2(ioc, &diag_register);
1546 if (bits_to_register & 2) {
1547 printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
1549 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1550 /* register for 2MB buffers */
1551 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1552 diag_register.unique_id = 0x7075901;
1553 _ctl_diag_register_2(ioc, &diag_register);
1556 if (bits_to_register & 4) {
1557 printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
1559 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1560 /* register for 2MB buffers */
1561 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1562 diag_register.unique_id = 0x7075901;
1563 _ctl_diag_register_2(ioc, &diag_register);
1568 * _ctl_diag_register - application register with driver
1569 * @ioc: per adapter object
1570 * @arg - user space buffer containing ioctl content
1572 * This will allow the driver to setup any required buffers that will be
1573 * needed by firmware to communicate with the driver.
1576 _ctl_diag_register(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1578 struct mpt2_diag_register karg;
1581 if (copy_from_user(&karg, arg, sizeof(karg))) {
1582 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1583 __FILE__, __LINE__, __func__);
1587 rc = _ctl_diag_register_2(ioc, &karg);
1592 * _ctl_diag_unregister - application unregister with driver
1593 * @ioc: per adapter object
1594 * @arg - user space buffer containing ioctl content
1596 * This will allow the driver to cleanup any memory allocated for diag
1597 * messages and to free up any resources.
1600 _ctl_diag_unregister(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1602 struct mpt2_diag_unregister karg;
1604 dma_addr_t request_data_dma;
1605 u32 request_data_sz;
1608 if (copy_from_user(&karg, arg, sizeof(karg))) {
1609 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1610 __FILE__, __LINE__, __func__);
1614 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1617 buffer_type = karg.unique_id & 0x000000ff;
1618 if (!_ctl_diag_capability(ioc, buffer_type)) {
1619 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1620 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1624 if ((ioc->diag_buffer_status[buffer_type] &
1625 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1626 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1627 "registered\n", ioc->name, __func__, buffer_type);
1630 if ((ioc->diag_buffer_status[buffer_type] &
1631 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1632 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1633 "released\n", ioc->name, __func__, buffer_type);
1637 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1638 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1639 "registered\n", ioc->name, __func__, karg.unique_id);
1643 request_data = ioc->diag_buffer[buffer_type];
1644 if (!request_data) {
1645 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1646 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1650 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1651 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1652 pci_free_consistent(ioc->pdev, request_data_sz,
1653 request_data, request_data_dma);
1654 ioc->diag_buffer[buffer_type] = NULL;
1655 ioc->diag_buffer_status[buffer_type] = 0;
1660 * _ctl_diag_query - query relevant info associated with diag buffers
1661 * @ioc: per adapter object
1662 * @arg - user space buffer containing ioctl content
1664 * The application will send only buffer_type and unique_id. Driver will
1665 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1666 * 0x00, the driver will return info specified by Buffer Type.
1669 _ctl_diag_query(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1671 struct mpt2_diag_query karg;
1676 if (copy_from_user(&karg, arg, sizeof(karg))) {
1677 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1678 __FILE__, __LINE__, __func__);
1682 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1685 karg.application_flags = 0;
1686 buffer_type = karg.buffer_type;
1688 if (!_ctl_diag_capability(ioc, buffer_type)) {
1689 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1690 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1694 if ((ioc->diag_buffer_status[buffer_type] &
1695 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1696 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1697 "registered\n", ioc->name, __func__, buffer_type);
1701 if (karg.unique_id & 0xffffff00) {
1702 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1703 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1704 "registered\n", ioc->name, __func__,
1710 request_data = ioc->diag_buffer[buffer_type];
1711 if (!request_data) {
1712 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1713 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1717 if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1718 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1719 MPT2_APP_FLAGS_BUFFER_VALID);
1721 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1722 MPT2_APP_FLAGS_BUFFER_VALID |
1723 MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1725 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1726 karg.product_specific[i] =
1727 ioc->product_specific[buffer_type][i];
1729 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1730 karg.driver_added_buffer_size = 0;
1731 karg.unique_id = ioc->unique_id[buffer_type];
1732 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1734 if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1735 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1736 "data @ %p\n", ioc->name, __func__, arg);
1743 * _ctl_send_release - Diag Release Message
1744 * @ioc: per adapter object
1745 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1746 * @issue_reset - specifies whether host reset is required.
1750 _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1752 Mpi2DiagReleaseRequest_t *mpi_request;
1753 Mpi2DiagReleaseReply_t *mpi_reply;
1758 unsigned long timeleft;
1760 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1766 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1767 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1768 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
1769 "skipping due to FAULT state\n", ioc->name,
1775 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1776 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1777 ioc->name, __func__);
1782 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1784 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1785 ioc->name, __func__);
1790 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1791 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1792 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1793 ioc->ctl_cmds.smid = smid;
1795 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1796 mpi_request->BufferType = buffer_type;
1797 mpi_request->VF_ID = 0; /* TODO */
1798 mpi_request->VP_ID = 0;
1800 init_completion(&ioc->ctl_cmds.done);
1801 mpt2sas_base_put_smid_default(ioc, smid);
1802 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1803 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1805 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1806 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1808 _debug_dump_mf(mpi_request,
1809 sizeof(Mpi2DiagReleaseRequest_t)/4);
1810 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1816 /* process the completed Reply Message Frame */
1817 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1818 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1819 ioc->name, __func__);
1824 mpi_reply = ioc->ctl_cmds.reply;
1825 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1827 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1828 ioc->diag_buffer_status[buffer_type] |=
1829 MPT2_DIAG_BUFFER_IS_RELEASED;
1830 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1831 ioc->name, __func__));
1833 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1834 "log_info(0x%08x)\n", ioc->name, __func__,
1835 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1840 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1845 * _ctl_diag_release - request to send Diag Release Message to firmware
1846 * @arg - user space buffer containing ioctl content
1848 * This allows ownership of the specified buffer to returned to the driver,
1849 * allowing an application to read the buffer without fear that firmware is
1850 * overwritting information in the buffer.
1853 _ctl_diag_release(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1855 struct mpt2_diag_release karg;
1861 if (copy_from_user(&karg, arg, sizeof(karg))) {
1862 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1863 __FILE__, __LINE__, __func__);
1867 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1870 buffer_type = karg.unique_id & 0x000000ff;
1871 if (!_ctl_diag_capability(ioc, buffer_type)) {
1872 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1873 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1877 if ((ioc->diag_buffer_status[buffer_type] &
1878 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1879 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1880 "registered\n", ioc->name, __func__, buffer_type);
1884 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1885 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1886 "registered\n", ioc->name, __func__, karg.unique_id);
1890 if (ioc->diag_buffer_status[buffer_type] &
1891 MPT2_DIAG_BUFFER_IS_RELEASED) {
1892 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1893 "is already released\n", ioc->name, __func__,
1898 request_data = ioc->diag_buffer[buffer_type];
1900 if (!request_data) {
1901 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1902 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1906 /* buffers were released by due to host reset */
1907 if ((ioc->diag_buffer_status[buffer_type] &
1908 MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1909 ioc->diag_buffer_status[buffer_type] |=
1910 MPT2_DIAG_BUFFER_IS_RELEASED;
1911 ioc->diag_buffer_status[buffer_type] &=
1912 ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1913 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1914 "was released due to host reset\n", ioc->name, __func__,
1919 rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1922 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1929 * _ctl_diag_read_buffer - request for copy of the diag buffer
1930 * @ioc: per adapter object
1931 * @arg - user space buffer containing ioctl content
1934 _ctl_diag_read_buffer(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1936 struct mpt2_diag_read_buffer karg;
1937 struct mpt2_diag_read_buffer __user *uarg = arg;
1938 void *request_data, *diag_data;
1939 Mpi2DiagBufferPostRequest_t *mpi_request;
1940 Mpi2DiagBufferPostReply_t *mpi_reply;
1943 unsigned long timeleft, request_size, copy_size;
1948 if (copy_from_user(&karg, arg, sizeof(karg))) {
1949 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1950 __FILE__, __LINE__, __func__);
1954 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1957 buffer_type = karg.unique_id & 0x000000ff;
1958 if (!_ctl_diag_capability(ioc, buffer_type)) {
1959 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1960 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1964 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1965 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1966 "registered\n", ioc->name, __func__, karg.unique_id);
1970 request_data = ioc->diag_buffer[buffer_type];
1971 if (!request_data) {
1972 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1973 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1977 request_size = ioc->diag_buffer_sz[buffer_type];
1979 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1980 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1981 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1986 if (karg.starting_offset > request_size)
1989 diag_data = (void *)(request_data + karg.starting_offset);
1990 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
1991 "offset(%d), sz(%d)\n", ioc->name, __func__,
1992 diag_data, karg.starting_offset, karg.bytes_to_read));
1994 /* Truncate data on requests that are too large */
1995 if ((diag_data + karg.bytes_to_read < diag_data) ||
1996 (diag_data + karg.bytes_to_read > request_data + request_size))
1997 copy_size = request_size - karg.starting_offset;
1999 copy_size = karg.bytes_to_read;
2001 if (copy_to_user((void __user *)uarg->diagnostic_data,
2002 diag_data, copy_size)) {
2003 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
2004 "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
2005 __func__, diag_data);
2009 if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
2012 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
2013 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
2014 if ((ioc->diag_buffer_status[buffer_type] &
2015 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
2016 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2017 "buffer_type(0x%02x) is still registered\n", ioc->name,
2018 __func__, buffer_type));
2021 /* Get a free request frame and save the message context.
2024 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
2025 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
2026 ioc->name, __func__);
2031 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2033 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2034 ioc->name, __func__);
2040 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
2041 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2042 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2043 ioc->ctl_cmds.smid = smid;
2045 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2046 mpi_request->BufferType = buffer_type;
2047 mpi_request->BufferLength =
2048 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2049 mpi_request->BufferAddress =
2050 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2051 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
2052 mpi_request->ProductSpecific[i] =
2053 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2054 mpi_request->VF_ID = 0; /* TODO */
2055 mpi_request->VP_ID = 0;
2057 init_completion(&ioc->ctl_cmds.done);
2058 mpt2sas_base_put_smid_default(ioc, smid);
2059 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2060 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
2062 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
2063 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
2065 _debug_dump_mf(mpi_request,
2066 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2067 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
2069 goto issue_host_reset;
2072 /* process the completed Reply Message Frame */
2073 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
2074 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
2075 ioc->name, __func__);
2080 mpi_reply = ioc->ctl_cmds.reply;
2081 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2083 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2084 ioc->diag_buffer_status[buffer_type] |=
2085 MPT2_DIAG_BUFFER_IS_REGISTERED;
2086 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
2087 ioc->name, __func__));
2089 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
2090 "log_info(0x%08x)\n", ioc->name, __func__,
2091 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2097 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2102 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
2107 #ifdef CONFIG_COMPAT
2109 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2110 * @ioc: per adapter object
2111 * @cmd - ioctl opcode
2112 * @arg - (struct mpt2_ioctl_command32)
2114 * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2117 _ctl_compat_mpt_command(struct MPT2SAS_ADAPTER *ioc, unsigned cmd,
2120 struct mpt2_ioctl_command32 karg32;
2121 struct mpt2_ioctl_command32 __user *uarg;
2122 struct mpt2_ioctl_command karg;
2124 if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2127 uarg = (struct mpt2_ioctl_command32 __user *) arg;
2129 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2130 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2131 __FILE__, __LINE__, __func__);
2135 memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2136 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2137 karg.hdr.port_number = karg32.hdr.port_number;
2138 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2139 karg.timeout = karg32.timeout;
2140 karg.max_reply_bytes = karg32.max_reply_bytes;
2141 karg.data_in_size = karg32.data_in_size;
2142 karg.data_out_size = karg32.data_out_size;
2143 karg.max_sense_bytes = karg32.max_sense_bytes;
2144 karg.data_sge_offset = karg32.data_sge_offset;
2145 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2146 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2147 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2148 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2149 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2154 * _ctl_ioctl_main - main ioctl entry point
2155 * @file - (struct file)
2156 * @cmd - ioctl opcode
2158 * compat - handles 32 bit applications in 64bit os
2161 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2164 struct MPT2SAS_ADAPTER *ioc;
2165 struct mpt2_ioctl_header ioctl_header;
2166 enum block_state state;
2169 /* get IOCTL header */
2170 if (copy_from_user(&ioctl_header, (char __user *)arg,
2171 sizeof(struct mpt2_ioctl_header))) {
2172 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2173 __FILE__, __LINE__, __func__);
2177 if (_ctl_verify_adapter(ioctl_header.ioc_number, &ioc) == -1 || !ioc)
2179 /* pci_access_mutex lock acquired by ioctl path */
2180 mutex_lock(&ioc->pci_access_mutex);
2181 if (ioc->shost_recovery || ioc->pci_error_recovery ||
2182 ioc->is_driver_loading || ioc->remove_host) {
2184 goto out_unlock_pciaccess;
2187 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2188 if (state == NON_BLOCKING) {
2189 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2191 goto out_unlock_pciaccess;
2193 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2195 goto out_unlock_pciaccess;
2200 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
2201 ret = _ctl_getiocinfo(ioc, arg);
2203 #ifdef CONFIG_COMPAT
2208 struct mpt2_ioctl_command __user *uarg;
2209 struct mpt2_ioctl_command karg;
2210 #ifdef CONFIG_COMPAT
2212 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2216 if (copy_from_user(&karg, arg, sizeof(karg))) {
2217 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2218 __FILE__, __LINE__, __func__);
2223 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2225 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2229 case MPT2EVENTQUERY:
2230 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2231 ret = _ctl_eventquery(ioc, arg);
2233 case MPT2EVENTENABLE:
2234 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2235 ret = _ctl_eventenable(ioc, arg);
2237 case MPT2EVENTREPORT:
2238 ret = _ctl_eventreport(ioc, arg);
2241 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2242 ret = _ctl_do_reset(ioc, arg);
2244 case MPT2BTDHMAPPING:
2245 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2246 ret = _ctl_btdh_mapping(ioc, arg);
2248 case MPT2DIAGREGISTER:
2249 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2250 ret = _ctl_diag_register(ioc, arg);
2252 case MPT2DIAGUNREGISTER:
2253 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2254 ret = _ctl_diag_unregister(ioc, arg);
2257 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2258 ret = _ctl_diag_query(ioc, arg);
2260 case MPT2DIAGRELEASE:
2261 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2262 ret = _ctl_diag_release(ioc, arg);
2264 case MPT2DIAGREADBUFFER:
2265 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2266 ret = _ctl_diag_read_buffer(ioc, arg);
2270 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
2271 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2275 mutex_unlock(&ioc->ctl_cmds.mutex);
2276 out_unlock_pciaccess:
2277 mutex_unlock(&ioc->pci_access_mutex);
2282 * _ctl_ioctl - main ioctl entry point (unlocked)
2283 * @file - (struct file)
2284 * @cmd - ioctl opcode
2288 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2292 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0);
2295 #ifdef CONFIG_COMPAT
2297 * _ctl_ioctl_compat - main ioctl entry point (compat)
2302 * This routine handles 32 bit applications in 64bit os.
2305 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2309 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1);
2314 /* scsi host attributes */
2317 * _ctl_version_fw_show - firmware version
2318 * @cdev - pointer to embedded class device
2319 * @buf - the buffer returned
2321 * A sysfs 'read-only' shost attribute.
2324 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2327 struct Scsi_Host *shost = class_to_shost(cdev);
2328 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2330 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2331 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2332 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2333 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2334 ioc->facts.FWVersion.Word & 0x000000FF);
2336 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2339 * _ctl_version_bios_show - bios version
2340 * @cdev - pointer to embedded class device
2341 * @buf - the buffer returned
2343 * A sysfs 'read-only' shost attribute.
2346 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2349 struct Scsi_Host *shost = class_to_shost(cdev);
2350 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2352 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2354 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2355 (version & 0xFF000000) >> 24,
2356 (version & 0x00FF0000) >> 16,
2357 (version & 0x0000FF00) >> 8,
2358 version & 0x000000FF);
2360 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2363 * _ctl_version_mpi_show - MPI (message passing interface) version
2364 * @cdev - pointer to embedded class device
2365 * @buf - the buffer returned
2367 * A sysfs 'read-only' shost attribute.
2370 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2373 struct Scsi_Host *shost = class_to_shost(cdev);
2374 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2376 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2377 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2379 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2382 * _ctl_version_product_show - product name
2383 * @cdev - pointer to embedded class device
2384 * @buf - the buffer returned
2386 * A sysfs 'read-only' shost attribute.
2389 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2392 struct Scsi_Host *shost = class_to_shost(cdev);
2393 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2395 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2397 static DEVICE_ATTR(version_product, S_IRUGO,
2398 _ctl_version_product_show, NULL);
2401 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2402 * @cdev - pointer to embedded class device
2403 * @buf - the buffer returned
2405 * A sysfs 'read-only' shost attribute.
2408 _ctl_version_nvdata_persistent_show(struct device *cdev,
2409 struct device_attribute *attr, char *buf)
2411 struct Scsi_Host *shost = class_to_shost(cdev);
2412 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2414 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2415 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2417 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2418 _ctl_version_nvdata_persistent_show, NULL);
2421 * _ctl_version_nvdata_default_show - nvdata default version
2422 * @cdev - pointer to embedded class device
2423 * @buf - the buffer returned
2425 * A sysfs 'read-only' shost attribute.
2428 _ctl_version_nvdata_default_show(struct device *cdev,
2429 struct device_attribute *attr, char *buf)
2431 struct Scsi_Host *shost = class_to_shost(cdev);
2432 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2434 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2435 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2437 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2438 _ctl_version_nvdata_default_show, NULL);
2441 * _ctl_board_name_show - board name
2442 * @cdev - pointer to embedded class device
2443 * @buf - the buffer returned
2445 * A sysfs 'read-only' shost attribute.
2448 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2451 struct Scsi_Host *shost = class_to_shost(cdev);
2452 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2454 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2456 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2459 * _ctl_board_assembly_show - board assembly name
2460 * @cdev - pointer to embedded class device
2461 * @buf - the buffer returned
2463 * A sysfs 'read-only' shost attribute.
2466 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2469 struct Scsi_Host *shost = class_to_shost(cdev);
2470 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2472 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2474 static DEVICE_ATTR(board_assembly, S_IRUGO,
2475 _ctl_board_assembly_show, NULL);
2478 * _ctl_board_tracer_show - board tracer number
2479 * @cdev - pointer to embedded class device
2480 * @buf - the buffer returned
2482 * A sysfs 'read-only' shost attribute.
2485 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2488 struct Scsi_Host *shost = class_to_shost(cdev);
2489 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2491 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2493 static DEVICE_ATTR(board_tracer, S_IRUGO,
2494 _ctl_board_tracer_show, NULL);
2497 * _ctl_io_delay_show - io missing delay
2498 * @cdev - pointer to embedded class device
2499 * @buf - the buffer returned
2501 * This is for firmware implemention for deboucing device
2504 * A sysfs 'read-only' shost attribute.
2507 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2510 struct Scsi_Host *shost = class_to_shost(cdev);
2511 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2513 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2515 static DEVICE_ATTR(io_delay, S_IRUGO,
2516 _ctl_io_delay_show, NULL);
2519 * _ctl_device_delay_show - device missing delay
2520 * @cdev - pointer to embedded class device
2521 * @buf - the buffer returned
2523 * This is for firmware implemention for deboucing device
2526 * A sysfs 'read-only' shost attribute.
2529 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2532 struct Scsi_Host *shost = class_to_shost(cdev);
2533 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2535 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2537 static DEVICE_ATTR(device_delay, S_IRUGO,
2538 _ctl_device_delay_show, NULL);
2541 * _ctl_fw_queue_depth_show - global credits
2542 * @cdev - pointer to embedded class device
2543 * @buf - the buffer returned
2545 * This is firmware queue depth limit
2547 * A sysfs 'read-only' shost attribute.
2550 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2553 struct Scsi_Host *shost = class_to_shost(cdev);
2554 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2556 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2558 static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2559 _ctl_fw_queue_depth_show, NULL);
2562 * _ctl_sas_address_show - sas address
2563 * @cdev - pointer to embedded class device
2564 * @buf - the buffer returned
2566 * This is the controller sas address
2568 * A sysfs 'read-only' shost attribute.
2571 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2574 struct Scsi_Host *shost = class_to_shost(cdev);
2575 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2577 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2578 (unsigned long long)ioc->sas_hba.sas_address);
2580 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2581 _ctl_host_sas_address_show, NULL);
2584 * _ctl_logging_level_show - logging level
2585 * @cdev - pointer to embedded class device
2586 * @buf - the buffer returned
2588 * A sysfs 'read/write' shost attribute.
2591 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2594 struct Scsi_Host *shost = class_to_shost(cdev);
2595 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2597 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2600 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2601 const char *buf, size_t count)
2603 struct Scsi_Host *shost = class_to_shost(cdev);
2604 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2607 if (sscanf(buf, "%x", &val) != 1)
2610 ioc->logging_level = val;
2611 printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2612 ioc->logging_level);
2615 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2616 _ctl_logging_level_show, _ctl_logging_level_store);
2618 /* device attributes */
2620 * _ctl_fwfault_debug_show - show/store fwfault_debug
2621 * @cdev - pointer to embedded class device
2622 * @buf - the buffer returned
2624 * mpt2sas_fwfault_debug is command line option
2625 * A sysfs 'read/write' shost attribute.
2628 _ctl_fwfault_debug_show(struct device *cdev,
2629 struct device_attribute *attr, char *buf)
2631 struct Scsi_Host *shost = class_to_shost(cdev);
2632 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2634 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2637 _ctl_fwfault_debug_store(struct device *cdev,
2638 struct device_attribute *attr, const char *buf, size_t count)
2640 struct Scsi_Host *shost = class_to_shost(cdev);
2641 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2644 if (sscanf(buf, "%d", &val) != 1)
2647 ioc->fwfault_debug = val;
2648 printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
2649 ioc->fwfault_debug);
2652 static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2653 _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2657 * _ctl_ioc_reset_count_show - ioc reset count
2658 * @cdev - pointer to embedded class device
2659 * @buf - the buffer returned
2661 * This is firmware queue depth limit
2663 * A sysfs 'read-only' shost attribute.
2666 _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2669 struct Scsi_Host *shost = class_to_shost(cdev);
2670 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2672 return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
2674 static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
2675 _ctl_ioc_reset_count_show, NULL);
2678 * _ctl_ioc_reply_queue_count_show - number of reply queues
2679 * @cdev - pointer to embedded class device
2680 * @buf - the buffer returned
2682 * This is number of reply queues
2684 * A sysfs 'read-only' shost attribute.
2687 _ctl_ioc_reply_queue_count_show(struct device *cdev,
2688 struct device_attribute *attr, char *buf)
2690 u8 reply_queue_count;
2691 struct Scsi_Host *shost = class_to_shost(cdev);
2692 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2694 if ((ioc->facts.IOCCapabilities &
2695 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2696 reply_queue_count = ioc->reply_queue_count;
2698 reply_queue_count = 1;
2699 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2701 static DEVICE_ATTR(reply_queue_count, S_IRUGO,
2702 _ctl_ioc_reply_queue_count_show, NULL);
2705 * _ctl_BRM_status_show - Backup Rail Monitor Status
2706 * @cdev - pointer to embedded class device
2707 * @buf - the buffer returned
2709 * This is number of reply queues
2711 * A sysfs 'read-only' shost attribute.
2714 _ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
2717 struct Scsi_Host *shost = class_to_shost(cdev);
2718 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2719 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2720 Mpi2ConfigReply_t mpi_reply;
2721 u16 backup_rail_monitor_status = 0;
2726 if (!ioc->is_warpdrive) {
2727 printk(MPT2SAS_ERR_FMT "%s: BRM attribute is only for"\
2728 "warpdrive\n", ioc->name, __func__);
2731 /* pci_access_mutex lock acquired by sysfs show path */
2732 mutex_lock(&ioc->pci_access_mutex);
2733 if (ioc->pci_error_recovery || ioc->remove_host) {
2734 mutex_unlock(&ioc->pci_access_mutex);
2738 /* allocate upto GPIOVal 36 entries */
2739 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2740 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2742 printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"\
2743 "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
2747 if (mpt2sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2749 printk(MPT2SAS_ERR_FMT
2750 "%s: failed reading iounit_pg3\n", ioc->name,
2755 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2756 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2757 printk(MPT2SAS_ERR_FMT "%s: iounit_pg3 failed with"\
2758 "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
2762 if (io_unit_pg3->GPIOCount < 25) {
2763 printk(MPT2SAS_ERR_FMT "%s: iounit_pg3->GPIOCount less than"\
2764 "25 entries, detected (%d) entries\n", ioc->name, __func__,
2765 io_unit_pg3->GPIOCount);
2769 /* BRM status is in bit zero of GPIOVal[24] */
2770 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2771 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2775 mutex_unlock(&ioc->pci_access_mutex);
2778 static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
2780 struct DIAG_BUFFER_START {
2790 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2791 * @cdev - pointer to embedded class device
2792 * @buf - the buffer returned
2794 * A sysfs 'read-only' shost attribute.
2797 _ctl_host_trace_buffer_size_show(struct device *cdev,
2798 struct device_attribute *attr, char *buf)
2800 struct Scsi_Host *shost = class_to_shost(cdev);
2801 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2803 struct DIAG_BUFFER_START *request_data;
2805 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2806 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2807 "registered\n", ioc->name, __func__);
2811 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2812 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2813 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2814 "registered\n", ioc->name, __func__);
2818 request_data = (struct DIAG_BUFFER_START *)
2819 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2820 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2821 le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
2822 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2823 size = le32_to_cpu(request_data->Size);
2825 ioc->ring_buffer_sz = size;
2826 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2828 static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2829 _ctl_host_trace_buffer_size_show, NULL);
2832 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2833 * @cdev - pointer to embedded class device
2834 * @buf - the buffer returned
2836 * A sysfs 'read/write' shost attribute.
2838 * You will only be able to read 4k bytes of ring buffer at a time.
2839 * In order to read beyond 4k bytes, you will have to write out the
2840 * offset to the same attribute, it will move the pointer.
2843 _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2846 struct Scsi_Host *shost = class_to_shost(cdev);
2847 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2851 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2852 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2853 "registered\n", ioc->name, __func__);
2857 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2858 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2859 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2860 "registered\n", ioc->name, __func__);
2864 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2867 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2868 size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
2869 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2870 memcpy(buf, request_data, size);
2875 _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2876 const char *buf, size_t count)
2878 struct Scsi_Host *shost = class_to_shost(cdev);
2879 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2882 if (sscanf(buf, "%d", &val) != 1)
2885 ioc->ring_buffer_offset = val;
2888 static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2889 _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2891 /*****************************************/
2894 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2895 * @cdev - pointer to embedded class device
2896 * @buf - the buffer returned
2898 * A sysfs 'read/write' shost attribute.
2900 * This is a mechnism to post/release host_trace_buffers
2903 _ctl_host_trace_buffer_enable_show(struct device *cdev,
2904 struct device_attribute *attr, char *buf)
2906 struct Scsi_Host *shost = class_to_shost(cdev);
2907 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2909 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
2910 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2911 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
2912 return snprintf(buf, PAGE_SIZE, "off\n");
2913 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2914 MPT2_DIAG_BUFFER_IS_RELEASED))
2915 return snprintf(buf, PAGE_SIZE, "release\n");
2917 return snprintf(buf, PAGE_SIZE, "post\n");
2921 _ctl_host_trace_buffer_enable_store(struct device *cdev,
2922 struct device_attribute *attr, const char *buf, size_t count)
2924 struct Scsi_Host *shost = class_to_shost(cdev);
2925 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2927 struct mpt2_diag_register diag_register;
2930 if (sscanf(buf, "%9s", str) != 1)
2933 if (!strcmp(str, "post")) {
2934 /* exit out if host buffers are already posted */
2935 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
2936 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2937 MPT2_DIAG_BUFFER_IS_REGISTERED) &&
2938 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2939 MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
2941 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
2942 printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
2944 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
2945 diag_register.requested_buffer_size = (1024 * 1024);
2946 diag_register.unique_id = 0x7075900;
2947 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
2948 _ctl_diag_register_2(ioc, &diag_register);
2949 } else if (!strcmp(str, "release")) {
2950 /* exit out if host buffers are already released */
2951 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
2953 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2954 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
2956 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2957 MPT2_DIAG_BUFFER_IS_RELEASED))
2959 printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
2961 _ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
2967 static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
2968 _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
2970 struct device_attribute *mpt2sas_host_attrs[] = {
2971 &dev_attr_version_fw,
2972 &dev_attr_version_bios,
2973 &dev_attr_version_mpi,
2974 &dev_attr_version_product,
2975 &dev_attr_version_nvdata_persistent,
2976 &dev_attr_version_nvdata_default,
2977 &dev_attr_board_name,
2978 &dev_attr_board_assembly,
2979 &dev_attr_board_tracer,
2981 &dev_attr_device_delay,
2982 &dev_attr_logging_level,
2983 &dev_attr_fwfault_debug,
2984 &dev_attr_fw_queue_depth,
2985 &dev_attr_host_sas_address,
2986 &dev_attr_ioc_reset_count,
2987 &dev_attr_host_trace_buffer_size,
2988 &dev_attr_host_trace_buffer,
2989 &dev_attr_host_trace_buffer_enable,
2990 &dev_attr_reply_queue_count,
2991 &dev_attr_BRM_status,
2996 * _ctl_device_sas_address_show - sas address
2997 * @cdev - pointer to embedded class device
2998 * @buf - the buffer returned
3000 * This is the sas address for the target
3002 * A sysfs 'read-only' shost attribute.
3005 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
3008 struct scsi_device *sdev = to_scsi_device(dev);
3009 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3011 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3012 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3014 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
3017 * _ctl_device_handle_show - device handle
3018 * @cdev - pointer to embedded class device
3019 * @buf - the buffer returned
3021 * This is the firmware assigned device handle
3023 * A sysfs 'read-only' shost attribute.
3026 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
3029 struct scsi_device *sdev = to_scsi_device(dev);
3030 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3032 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3033 sas_device_priv_data->sas_target->handle);
3035 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
3037 struct device_attribute *mpt2sas_dev_attrs[] = {
3038 &dev_attr_sas_address,
3039 &dev_attr_sas_device_handle,
3043 static const struct file_operations ctl_fops = {
3044 .owner = THIS_MODULE,
3045 .unlocked_ioctl = _ctl_ioctl,
3047 .fasync = _ctl_fasync,
3048 #ifdef CONFIG_COMPAT
3049 .compat_ioctl = _ctl_ioctl_compat,
3051 .llseek = noop_llseek,
3054 static struct miscdevice ctl_dev = {
3055 .minor = MPT2SAS_MINOR,
3056 .name = MPT2SAS_DEV_NAME,
3061 * mpt2sas_ctl_init - main entry point for ctl.
3065 mpt2sas_ctl_init(void)
3068 if (misc_register(&ctl_dev) < 0)
3069 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
3070 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3072 init_waitqueue_head(&ctl_poll_wait);
3076 * mpt2sas_ctl_exit - exit point for ctl
3080 mpt2sas_ctl_exit(void)
3082 struct MPT2SAS_ADAPTER *ioc;
3085 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
3087 /* free memory associated to diag buffers */
3088 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3089 if (!ioc->diag_buffer[i])
3091 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3092 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3093 ioc->diag_buffer[i] = NULL;
3094 ioc->diag_buffer_status[i] = 0;
3097 kfree(ioc->event_log);
3099 misc_deregister(&ctl_dev);