2 * Management Module Support for MPT (Message Passing Technology) based
5 * This code is based on drivers/scsi/mpt3sas/mpt3sas_ctl.c
6 * Copyright (C) 2012-2014 LSI Corporation
7 * Copyright (C) 2013-2014 Avago Technologies
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/compat.h>
55 #include <linux/poll.h>
58 #include <linux/uaccess.h>
60 #include "mpt3sas_base.h"
61 #include "mpt3sas_ctl.h"
64 static struct fasync_struct *async_queue;
65 static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
69 * enum block_state - blocking state
70 * @NON_BLOCKING: non blocking
73 * These states are for ioctls that need to wait for a response
74 * from firmware, so they probably require sleep.
82 * _ctl_display_some_debug - debug routine
83 * @ioc: per adapter object
84 * @smid: system request message index
85 * @calling_function_name: string pass from calling function
86 * @mpi_reply: reply message frame
89 * Function for displaying debug info helpful when debugging issues
93 _ctl_display_some_debug(struct MPT3SAS_ADAPTER *ioc, u16 smid,
94 char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
96 Mpi2ConfigRequest_t *mpi_request;
99 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
102 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
103 switch (mpi_request->Function) {
104 case MPI2_FUNCTION_SCSI_IO_REQUEST:
106 Mpi2SCSIIORequest_t *scsi_request =
107 (Mpi2SCSIIORequest_t *)mpi_request;
109 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
110 "scsi_io, cmd(0x%02x), cdb_len(%d)",
111 scsi_request->CDB.CDB32[0],
112 le16_to_cpu(scsi_request->IoFlags) & 0xF);
113 desc = ioc->tmp_string;
116 case MPI2_FUNCTION_SCSI_TASK_MGMT:
119 case MPI2_FUNCTION_IOC_INIT:
122 case MPI2_FUNCTION_IOC_FACTS:
125 case MPI2_FUNCTION_CONFIG:
127 Mpi2ConfigRequest_t *config_request =
128 (Mpi2ConfigRequest_t *)mpi_request;
130 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
131 "config, type(0x%02x), ext_type(0x%02x), number(%d)",
132 (config_request->Header.PageType &
133 MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
134 config_request->Header.PageNumber);
135 desc = ioc->tmp_string;
138 case MPI2_FUNCTION_PORT_FACTS:
141 case MPI2_FUNCTION_PORT_ENABLE:
142 desc = "port_enable";
144 case MPI2_FUNCTION_EVENT_NOTIFICATION:
145 desc = "event_notification";
147 case MPI2_FUNCTION_FW_DOWNLOAD:
148 desc = "fw_download";
150 case MPI2_FUNCTION_FW_UPLOAD:
153 case MPI2_FUNCTION_RAID_ACTION:
154 desc = "raid_action";
156 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
158 Mpi2SCSIIORequest_t *scsi_request =
159 (Mpi2SCSIIORequest_t *)mpi_request;
161 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
162 "raid_pass, cmd(0x%02x), cdb_len(%d)",
163 scsi_request->CDB.CDB32[0],
164 le16_to_cpu(scsi_request->IoFlags) & 0xF);
165 desc = ioc->tmp_string;
168 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
169 desc = "sas_iounit_cntl";
171 case MPI2_FUNCTION_SATA_PASSTHROUGH:
174 case MPI2_FUNCTION_DIAG_BUFFER_POST:
175 desc = "diag_buffer_post";
177 case MPI2_FUNCTION_DIAG_RELEASE:
178 desc = "diag_release";
180 case MPI2_FUNCTION_SMP_PASSTHROUGH:
181 desc = "smp_passthrough";
188 pr_info(MPT3SAS_FMT "%s: %s, smid(%d)\n",
189 ioc->name, calling_function_name, desc, smid);
194 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
196 "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
197 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
198 le32_to_cpu(mpi_reply->IOCLogInfo));
200 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
201 mpi_request->Function ==
202 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
203 Mpi2SCSIIOReply_t *scsi_reply =
204 (Mpi2SCSIIOReply_t *)mpi_reply;
205 struct _sas_device *sas_device = NULL;
206 struct _pcie_device *pcie_device = NULL;
208 sas_device = mpt3sas_get_sdev_by_handle(ioc,
209 le16_to_cpu(scsi_reply->DevHandle));
211 pr_warn(MPT3SAS_FMT "\tsas_address(0x%016llx), phy(%d)\n",
212 ioc->name, (unsigned long long)
213 sas_device->sas_address, sas_device->phy);
215 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
216 ioc->name, (unsigned long long)
217 sas_device->enclosure_logical_id, sas_device->slot);
218 sas_device_put(sas_device);
221 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
222 le16_to_cpu(scsi_reply->DevHandle));
225 "\tWWID(0x%016llx), port(%d)\n", ioc->name,
226 (unsigned long long)pcie_device->wwid,
227 pcie_device->port_num);
228 if (pcie_device->enclosure_handle != 0)
230 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
231 ioc->name, (unsigned long long)
232 pcie_device->enclosure_logical_id,
234 pcie_device_put(pcie_device);
237 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
239 "\tscsi_state(0x%02x), scsi_status"
240 "(0x%02x)\n", ioc->name,
241 scsi_reply->SCSIState,
242 scsi_reply->SCSIStatus);
247 * mpt3sas_ctl_done - ctl module completion routine
248 * @ioc: per adapter object
249 * @smid: system request message index
250 * @msix_index: MSIX table index supplied by the OS
251 * @reply: reply message frame(lower 32bit addr)
254 * The callback handler when using ioc->ctl_cb_idx.
256 * Return 1 meaning mf should be freed from _base_interrupt
257 * 0 means the mf is freed from this function.
260 mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
263 MPI2DefaultReply_t *mpi_reply;
264 Mpi2SCSIIOReply_t *scsiio_reply;
265 Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
266 const void *sense_data;
269 if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
271 if (ioc->ctl_cmds.smid != smid)
273 ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
274 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
276 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
277 ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
279 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
280 mpi_reply->Function ==
281 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
282 scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
283 if (scsiio_reply->SCSIState &
284 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
285 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
286 le32_to_cpu(scsiio_reply->SenseCount));
287 sense_data = mpt3sas_base_get_sense_buffer(ioc,
289 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
293 * Get Error Response data for NVMe device. The ctl_cmds.sense
294 * buffer is used to store the Error Response data.
296 if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
298 (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
299 sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
300 le16_to_cpu(nvme_error_reply->ErrorResponseCount));
301 sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
302 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
306 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
307 ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
308 complete(&ioc->ctl_cmds.done);
313 * _ctl_check_event_type - determines when an event needs logging
314 * @ioc: per adapter object
315 * @event: firmware event
317 * The bitmask in ioc->event_type[] indicates which events should be
318 * be saved in the driver event_log. This bitmask is set by application.
320 * Returns 1 when event should be captured, or zero means no match.
323 _ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
328 if (event >= 128 || !event || !ioc->event_log)
331 desired_event = (1 << (event % 32));
335 return desired_event & ioc->event_type[i];
339 * mpt3sas_ctl_add_to_event_log - add event
340 * @ioc: per adapter object
341 * @mpi_reply: reply message frame
346 mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
347 Mpi2EventNotificationReply_t *mpi_reply)
349 struct MPT3_IOCTL_EVENTS *event_log;
352 u32 sz, event_data_sz;
358 event = le16_to_cpu(mpi_reply->Event);
360 if (_ctl_check_event_type(ioc, event)) {
362 /* insert entry into circular event_log */
363 i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
364 event_log = ioc->event_log;
365 event_log[i].event = event;
366 event_log[i].context = ioc->event_context++;
368 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
369 sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
370 memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
371 memcpy(event_log[i].data, mpi_reply->EventData, sz);
375 /* This aen_event_read_flag flag is set until the
376 * application has read the event log.
377 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
379 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
380 (send_aen && !ioc->aen_event_read_flag)) {
381 ioc->aen_event_read_flag = 1;
382 wake_up_interruptible(&ctl_poll_wait);
384 kill_fasync(&async_queue, SIGIO, POLL_IN);
389 * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
390 * @ioc: per adapter object
391 * @msix_index: MSIX table index supplied by the OS
392 * @reply: reply message frame(lower 32bit addr)
393 * Context: interrupt.
395 * This function merely adds a new work task into ioc->firmware_event_thread.
396 * The tasks are worked from _firmware_event_work in user context.
398 * Return 1 meaning mf should be freed from _base_interrupt
399 * 0 means the mf is freed from this function.
402 mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
405 Mpi2EventNotificationReply_t *mpi_reply;
407 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
409 mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
414 * _ctl_verify_adapter - validates ioc_number passed from application
415 * @ioc: per adapter object
416 * @iocpp: The ioc pointer is returned in this.
417 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
418 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
420 * Return (-1) means error, else ioc_number.
423 _ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
426 struct MPT3SAS_ADAPTER *ioc;
428 /* global ioc lock to protect controller on list operations */
429 spin_lock(&gioc_lock);
430 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
431 if (ioc->id != ioc_number)
433 /* Check whether this ioctl command is from right
434 * ioctl device or not, if not continue the search.
436 version = ioc->hba_mpi_version_belonged;
437 /* MPI25_VERSION and MPI26_VERSION uses same ioctl
440 if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
441 if ((version == MPI25_VERSION) ||
442 (version == MPI26_VERSION))
447 if (version != mpi_version)
451 spin_unlock(&gioc_lock);
455 spin_unlock(&gioc_lock);
461 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
462 * @ioc: per adapter object
463 * @reset_phase: phase
465 * The handler for doing any required cleanup or initialization.
467 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
468 * MPT3_IOC_DONE_RESET
471 mpt3sas_ctl_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
476 switch (reset_phase) {
477 case MPT3_IOC_PRE_RESET:
478 dtmprintk(ioc, pr_info(MPT3SAS_FMT
479 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
480 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
481 if (!(ioc->diag_buffer_status[i] &
482 MPT3_DIAG_BUFFER_IS_REGISTERED))
484 if ((ioc->diag_buffer_status[i] &
485 MPT3_DIAG_BUFFER_IS_RELEASED))
487 mpt3sas_send_diag_release(ioc, i, &issue_reset);
490 case MPT3_IOC_AFTER_RESET:
491 dtmprintk(ioc, pr_info(MPT3SAS_FMT
492 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
493 if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
494 ioc->ctl_cmds.status |= MPT3_CMD_RESET;
495 mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
496 complete(&ioc->ctl_cmds.done);
499 case MPT3_IOC_DONE_RESET:
500 dtmprintk(ioc, pr_info(MPT3SAS_FMT
501 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
503 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
504 if (!(ioc->diag_buffer_status[i] &
505 MPT3_DIAG_BUFFER_IS_REGISTERED))
507 if ((ioc->diag_buffer_status[i] &
508 MPT3_DIAG_BUFFER_IS_RELEASED))
510 ioc->diag_buffer_status[i] |=
511 MPT3_DIAG_BUFFER_IS_DIAG_RESET;
523 * Called when application request fasyn callback handler.
526 _ctl_fasync(int fd, struct file *filep, int mode)
528 return fasync_helper(fd, filep, mode, &async_queue);
538 _ctl_poll(struct file *filep, poll_table *wait)
540 struct MPT3SAS_ADAPTER *ioc;
542 poll_wait(filep, &ctl_poll_wait, wait);
544 /* global ioc lock to protect controller on list operations */
545 spin_lock(&gioc_lock);
546 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
547 if (ioc->aen_event_read_flag) {
548 spin_unlock(&gioc_lock);
549 return EPOLLIN | EPOLLRDNORM;
552 spin_unlock(&gioc_lock);
557 * _ctl_set_task_mid - assign an active smid to tm request
558 * @ioc: per adapter object
559 * @karg - (struct mpt3_ioctl_command)
560 * @tm_request - pointer to mf from user space
562 * Returns 0 when an smid if found, else fail.
563 * during failure, the reply frame is filled.
566 _ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
567 Mpi2SCSITaskManagementRequest_t *tm_request)
572 struct scsi_cmnd *scmd;
573 struct MPT3SAS_DEVICE *priv_data;
574 Mpi2SCSITaskManagementReply_t *tm_reply;
579 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
581 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
586 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
588 handle = le16_to_cpu(tm_request->DevHandle);
589 for (smid = ioc->scsiio_depth; smid && !found; smid--) {
590 struct scsiio_tracker *st;
592 scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
595 if (lun != scmd->device->lun)
597 priv_data = scmd->device->hostdata;
598 if (priv_data->sas_target == NULL)
600 if (priv_data->sas_target->handle != handle)
602 st = scsi_cmd_priv(scmd);
603 tm_request->TaskMID = cpu_to_le16(st->smid);
608 dctlprintk(ioc, pr_info(MPT3SAS_FMT
609 "%s: handle(0x%04x), lun(%d), no active mid!!\n",
611 desc, le16_to_cpu(tm_request->DevHandle), lun));
612 tm_reply = ioc->ctl_cmds.reply;
613 tm_reply->DevHandle = tm_request->DevHandle;
614 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
615 tm_reply->TaskType = tm_request->TaskType;
616 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
617 tm_reply->VP_ID = tm_request->VP_ID;
618 tm_reply->VF_ID = tm_request->VF_ID;
619 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
620 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
622 pr_err("failure at %s:%d/%s()!\n", __FILE__,
627 dctlprintk(ioc, pr_info(MPT3SAS_FMT
628 "%s: handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
629 desc, le16_to_cpu(tm_request->DevHandle), lun,
630 le16_to_cpu(tm_request->TaskMID)));
635 * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
636 * @ioc: per adapter object
637 * @karg - (struct mpt3_ioctl_command)
638 * @mf - pointer to mf in user space
641 _ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
644 MPI2RequestHeader_t *mpi_request = NULL, *request;
645 MPI2DefaultReply_t *mpi_reply;
646 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
647 struct _pcie_device *pcie_device = NULL;
654 void *data_out = NULL;
655 dma_addr_t data_out_dma = 0;
656 size_t data_out_sz = 0;
657 void *data_in = NULL;
658 dma_addr_t data_in_dma = 0;
659 size_t data_in_sz = 0;
661 u16 wait_state_count;
662 u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
663 u8 tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
667 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
668 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
669 ioc->name, __func__);
674 wait_state_count = 0;
675 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
676 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
677 if (wait_state_count++ == 10) {
679 "%s: failed due to ioc not operational\n",
680 ioc->name, __func__);
685 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
687 "%s: waiting for operational state(count=%d)\n",
689 __func__, wait_state_count);
691 if (wait_state_count)
692 pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
693 ioc->name, __func__);
695 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
698 "%s: failed obtaining a memory for mpi_request\n",
699 ioc->name, __func__);
704 /* Check for overflow and wraparound */
705 if (karg.data_sge_offset * 4 > ioc->request_sz ||
706 karg.data_sge_offset > (UINT_MAX / 4)) {
711 /* copy in request message frame from user */
712 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
713 pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
719 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
720 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
722 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
723 ioc->name, __func__);
728 /* Use first reserved smid for passthrough ioctls */
729 smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
733 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
734 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
735 request = mpt3sas_base_get_msg_frame(ioc, smid);
736 memcpy(request, mpi_request, karg.data_sge_offset*4);
737 ioc->ctl_cmds.smid = smid;
738 data_out_sz = karg.data_out_size;
739 data_in_sz = karg.data_in_size;
741 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
742 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
743 mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
744 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
745 mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
747 device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
748 if (!device_handle || (device_handle >
749 ioc->facts.MaxDevHandle)) {
751 mpt3sas_base_free_smid(ioc, smid);
756 /* obtain dma-able memory for data transfer */
757 if (data_out_sz) /* WRITE */ {
758 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
761 pr_err("failure at %s:%d/%s()!\n", __FILE__,
764 mpt3sas_base_free_smid(ioc, smid);
767 if (copy_from_user(data_out, karg.data_out_buf_ptr,
769 pr_err("failure at %s:%d/%s()!\n", __FILE__,
772 mpt3sas_base_free_smid(ioc, smid);
777 if (data_in_sz) /* READ */ {
778 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
781 pr_err("failure at %s:%d/%s()!\n", __FILE__,
784 mpt3sas_base_free_smid(ioc, smid);
789 psge = (void *)request + (karg.data_sge_offset*4);
791 /* send command to firmware */
792 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
794 init_completion(&ioc->ctl_cmds.done);
795 switch (mpi_request->Function) {
796 case MPI2_FUNCTION_NVME_ENCAPSULATED:
798 nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
800 * Get the Physical Address of the sense buffer.
801 * Use Error Response buffer address field to hold the sense
803 * Clear the internal sense buffer, which will potentially hold
804 * the Completion Queue Entry on return, or 0 if no Entry.
805 * Build the PRPs and set direction bits.
808 nvme_encap_request->ErrorResponseBaseAddress =
809 cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
810 nvme_encap_request->ErrorResponseBaseAddress |=
811 cpu_to_le64(le32_to_cpu(
812 mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
813 nvme_encap_request->ErrorResponseAllocationLength =
814 cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
815 memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
816 ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
817 data_out_dma, data_out_sz, data_in_dma, data_in_sz);
818 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
819 dtmprintk(ioc, pr_info(MPT3SAS_FMT "handle(0x%04x) :"
820 "ioctl failed due to device removal in progress\n",
821 ioc->name, device_handle));
822 mpt3sas_base_free_smid(ioc, smid);
826 mpt3sas_base_put_smid_nvme_encap(ioc, smid);
829 case MPI2_FUNCTION_SCSI_IO_REQUEST:
830 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
832 Mpi2SCSIIORequest_t *scsiio_request =
833 (Mpi2SCSIIORequest_t *)request;
834 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
835 scsiio_request->SenseBufferLowAddress =
836 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
837 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
838 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
839 dtmprintk(ioc, pr_info(MPT3SAS_FMT
840 "handle(0x%04x) :ioctl failed due to device removal in progress\n",
841 ioc->name, device_handle));
842 mpt3sas_base_free_smid(ioc, smid);
846 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
847 data_in_dma, data_in_sz);
848 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
849 ioc->put_smid_scsi_io(ioc, smid, device_handle);
851 mpt3sas_base_put_smid_default(ioc, smid);
854 case MPI2_FUNCTION_SCSI_TASK_MGMT:
856 Mpi2SCSITaskManagementRequest_t *tm_request =
857 (Mpi2SCSITaskManagementRequest_t *)request;
859 dtmprintk(ioc, pr_info(MPT3SAS_FMT
860 "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
862 le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
863 ioc->got_task_abort_from_ioctl = 1;
864 if (tm_request->TaskType ==
865 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
866 tm_request->TaskType ==
867 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
868 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
869 mpt3sas_base_free_smid(ioc, smid);
870 ioc->got_task_abort_from_ioctl = 0;
874 ioc->got_task_abort_from_ioctl = 0;
876 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
877 dtmprintk(ioc, pr_info(MPT3SAS_FMT
878 "handle(0x%04x) :ioctl failed due to device removal in progress\n",
879 ioc->name, device_handle));
880 mpt3sas_base_free_smid(ioc, smid);
884 mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
885 tm_request->DevHandle));
886 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
887 data_in_dma, data_in_sz);
888 mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
891 case MPI2_FUNCTION_SMP_PASSTHROUGH:
893 Mpi2SmpPassthroughRequest_t *smp_request =
894 (Mpi2SmpPassthroughRequest_t *)mpi_request;
897 /* ioc determines which port to use */
898 smp_request->PhysicalPort = 0xFF;
899 if (smp_request->PassthroughFlags &
900 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
901 data = (u8 *)&smp_request->SGL;
903 if (unlikely(data_out == NULL)) {
904 pr_err("failure at %s:%d/%s()!\n",
905 __FILE__, __LINE__, __func__);
906 mpt3sas_base_free_smid(ioc, smid);
913 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
914 ioc->ioc_link_reset_in_progress = 1;
915 ioc->ignore_loginfos = 1;
917 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
919 mpt3sas_base_put_smid_default(ioc, smid);
922 case MPI2_FUNCTION_SATA_PASSTHROUGH:
924 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
925 dtmprintk(ioc, pr_info(MPT3SAS_FMT
926 "handle(0x%04x) :ioctl failed due to device removal in progress\n",
927 ioc->name, device_handle));
928 mpt3sas_base_free_smid(ioc, smid);
932 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
934 mpt3sas_base_put_smid_default(ioc, smid);
937 case MPI2_FUNCTION_FW_DOWNLOAD:
938 case MPI2_FUNCTION_FW_UPLOAD:
940 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
942 mpt3sas_base_put_smid_default(ioc, smid);
945 case MPI2_FUNCTION_TOOLBOX:
947 Mpi2ToolboxCleanRequest_t *toolbox_request =
948 (Mpi2ToolboxCleanRequest_t *)mpi_request;
950 if (toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL) {
951 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
952 data_in_dma, data_in_sz);
954 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
955 data_in_dma, data_in_sz);
957 mpt3sas_base_put_smid_default(ioc, smid);
960 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
962 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
963 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
965 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
966 || sasiounit_request->Operation ==
967 MPI2_SAS_OP_PHY_LINK_RESET) {
968 ioc->ioc_link_reset_in_progress = 1;
969 ioc->ignore_loginfos = 1;
971 /* drop to default case for posting the request */
974 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
975 data_in_dma, data_in_sz);
976 mpt3sas_base_put_smid_default(ioc, smid);
980 if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
981 timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
983 timeout = karg.timeout;
984 wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
985 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
986 Mpi2SCSITaskManagementRequest_t *tm_request =
987 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
988 mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
989 tm_request->DevHandle));
990 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
991 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
992 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
993 ioc->ioc_link_reset_in_progress) {
994 ioc->ioc_link_reset_in_progress = 0;
995 ioc->ignore_loginfos = 0;
997 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
998 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
1000 _debug_dump_mf(mpi_request, karg.data_sge_offset);
1001 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
1003 goto issue_host_reset;
1006 mpi_reply = ioc->ctl_cmds.reply;
1008 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
1009 (ioc->logging_level & MPT_DEBUG_TM)) {
1010 Mpi2SCSITaskManagementReply_t *tm_reply =
1011 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
1013 pr_info(MPT3SAS_FMT "TASK_MGMT: " \
1014 "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
1015 "TerminationCount(0x%08x)\n", ioc->name,
1016 le16_to_cpu(tm_reply->IOCStatus),
1017 le32_to_cpu(tm_reply->IOCLogInfo),
1018 le32_to_cpu(tm_reply->TerminationCount));
1021 /* copy out xdata to user */
1023 if (copy_to_user(karg.data_in_buf_ptr, data_in,
1025 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1026 __LINE__, __func__);
1032 /* copy out reply message frame to user */
1033 if (karg.max_reply_bytes) {
1034 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1035 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1037 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1038 __LINE__, __func__);
1044 /* copy out sense/NVMe Error Response to user */
1045 if (karg.max_sense_bytes && (mpi_request->Function ==
1046 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
1047 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1048 MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1049 if (karg.sense_data_ptr == NULL) {
1050 pr_info(MPT3SAS_FMT "Response buffer provided"
1051 " by application is NULL; Response data will"
1052 " not be returned.\n", ioc->name);
1055 sz_arg = (mpi_request->Function ==
1056 MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1057 SCSI_SENSE_BUFFERSIZE;
1058 sz = min_t(u32, karg.max_sense_bytes, sz_arg);
1059 if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1061 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1062 __LINE__, __func__);
1071 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1072 mpi_request->Function ==
1073 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1074 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
1075 pr_info(MPT3SAS_FMT "issue target reset: handle = (0x%04x)\n",
1077 le16_to_cpu(mpi_request->FunctionDependent1));
1078 mpt3sas_halt_firmware(ioc);
1079 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1080 le16_to_cpu(mpi_request->FunctionDependent1));
1081 if (pcie_device && (!ioc->tm_custom_handling))
1082 mpt3sas_scsih_issue_locked_tm(ioc,
1083 le16_to_cpu(mpi_request->FunctionDependent1),
1084 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1085 0, pcie_device->reset_timeout,
1088 mpt3sas_scsih_issue_locked_tm(ioc,
1089 le16_to_cpu(mpi_request->FunctionDependent1),
1090 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1091 0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
1093 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1098 pcie_device_put(pcie_device);
1100 /* free memory associated with sg buffers */
1102 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1106 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1110 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1115 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1116 * @ioc: per adapter object
1117 * @arg - user space buffer containing ioctl content
1120 _ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1122 struct mpt3_ioctl_iocinfo karg;
1124 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1127 memset(&karg, 0 , sizeof(karg));
1129 karg.port_number = ioc->pfacts[0].PortNumber;
1130 karg.hw_rev = ioc->pdev->revision;
1131 karg.pci_id = ioc->pdev->device;
1132 karg.subsystem_device = ioc->pdev->subsystem_device;
1133 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1134 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1135 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1136 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1137 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1138 karg.firmware_version = ioc->facts.FWVersion.Word;
1139 strcpy(karg.driver_version, ioc->driver_name);
1140 strcat(karg.driver_version, "-");
1141 switch (ioc->hba_mpi_version_belonged) {
1143 if (ioc->is_warpdrive)
1144 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1146 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1147 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1151 if (ioc->is_gen35_ioc)
1152 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1154 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1155 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1158 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1160 if (copy_to_user(arg, &karg, sizeof(karg))) {
1161 pr_err("failure at %s:%d/%s()!\n",
1162 __FILE__, __LINE__, __func__);
1169 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1170 * @ioc: per adapter object
1171 * @arg - user space buffer containing ioctl content
1174 _ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1176 struct mpt3_ioctl_eventquery karg;
1178 if (copy_from_user(&karg, arg, sizeof(karg))) {
1179 pr_err("failure at %s:%d/%s()!\n",
1180 __FILE__, __LINE__, __func__);
1184 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1187 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1188 memcpy(karg.event_types, ioc->event_type,
1189 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1191 if (copy_to_user(arg, &karg, sizeof(karg))) {
1192 pr_err("failure at %s:%d/%s()!\n",
1193 __FILE__, __LINE__, __func__);
1200 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1201 * @ioc: per adapter object
1202 * @arg - user space buffer containing ioctl content
1205 _ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1207 struct mpt3_ioctl_eventenable karg;
1209 if (copy_from_user(&karg, arg, sizeof(karg))) {
1210 pr_err("failure at %s:%d/%s()!\n",
1211 __FILE__, __LINE__, __func__);
1215 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1218 memcpy(ioc->event_type, karg.event_types,
1219 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1220 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1224 /* initialize event_log */
1225 ioc->event_context = 0;
1226 ioc->aen_event_read_flag = 0;
1227 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1228 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1229 if (!ioc->event_log) {
1230 pr_err("failure at %s:%d/%s()!\n",
1231 __FILE__, __LINE__, __func__);
1238 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1239 * @ioc: per adapter object
1240 * @arg - user space buffer containing ioctl content
1243 _ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1245 struct mpt3_ioctl_eventreport karg;
1246 u32 number_bytes, max_events, max;
1247 struct mpt3_ioctl_eventreport __user *uarg = arg;
1249 if (copy_from_user(&karg, arg, sizeof(karg))) {
1250 pr_err("failure at %s:%d/%s()!\n",
1251 __FILE__, __LINE__, __func__);
1255 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1258 number_bytes = karg.hdr.max_data_size -
1259 sizeof(struct mpt3_ioctl_header);
1260 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1261 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1263 /* If fewer than 1 event is requested, there must have
1264 * been some type of error.
1266 if (!max || !ioc->event_log)
1269 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1270 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1271 pr_err("failure at %s:%d/%s()!\n",
1272 __FILE__, __LINE__, __func__);
1276 /* reset flag so SIGIO can restart */
1277 ioc->aen_event_read_flag = 0;
1282 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1283 * @ioc: per adapter object
1284 * @arg - user space buffer containing ioctl content
1287 _ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1289 struct mpt3_ioctl_diag_reset karg;
1292 if (copy_from_user(&karg, arg, sizeof(karg))) {
1293 pr_err("failure at %s:%d/%s()!\n",
1294 __FILE__, __LINE__, __func__);
1298 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1299 ioc->is_driver_loading)
1302 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1305 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1306 pr_info(MPT3SAS_FMT "host reset: %s\n",
1307 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1312 * _ctl_btdh_search_sas_device - searching for sas device
1313 * @ioc: per adapter object
1314 * @btdh: btdh ioctl payload
1317 _ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1318 struct mpt3_ioctl_btdh_mapping *btdh)
1320 struct _sas_device *sas_device;
1321 unsigned long flags;
1324 if (list_empty(&ioc->sas_device_list))
1327 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1328 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1329 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1330 btdh->handle == sas_device->handle) {
1331 btdh->bus = sas_device->channel;
1332 btdh->id = sas_device->id;
1335 } else if (btdh->bus == sas_device->channel && btdh->id ==
1336 sas_device->id && btdh->handle == 0xFFFF) {
1337 btdh->handle = sas_device->handle;
1343 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1348 * _ctl_btdh_search_pcie_device - searching for pcie device
1349 * @ioc: per adapter object
1350 * @btdh: btdh ioctl payload
1353 _ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
1354 struct mpt3_ioctl_btdh_mapping *btdh)
1356 struct _pcie_device *pcie_device;
1357 unsigned long flags;
1360 if (list_empty(&ioc->pcie_device_list))
1363 spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1364 list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1365 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1366 btdh->handle == pcie_device->handle) {
1367 btdh->bus = pcie_device->channel;
1368 btdh->id = pcie_device->id;
1371 } else if (btdh->bus == pcie_device->channel && btdh->id ==
1372 pcie_device->id && btdh->handle == 0xFFFF) {
1373 btdh->handle = pcie_device->handle;
1379 spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1384 * _ctl_btdh_search_raid_device - searching for raid device
1385 * @ioc: per adapter object
1386 * @btdh: btdh ioctl payload
1389 _ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1390 struct mpt3_ioctl_btdh_mapping *btdh)
1392 struct _raid_device *raid_device;
1393 unsigned long flags;
1396 if (list_empty(&ioc->raid_device_list))
1399 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1400 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1401 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1402 btdh->handle == raid_device->handle) {
1403 btdh->bus = raid_device->channel;
1404 btdh->id = raid_device->id;
1407 } else if (btdh->bus == raid_device->channel && btdh->id ==
1408 raid_device->id && btdh->handle == 0xFFFF) {
1409 btdh->handle = raid_device->handle;
1415 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1420 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1421 * @ioc: per adapter object
1422 * @arg - user space buffer containing ioctl content
1425 _ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1427 struct mpt3_ioctl_btdh_mapping karg;
1430 if (copy_from_user(&karg, arg, sizeof(karg))) {
1431 pr_err("failure at %s:%d/%s()!\n",
1432 __FILE__, __LINE__, __func__);
1436 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1439 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1441 rc = _ctl_btdh_search_pcie_device(ioc, &karg);
1443 _ctl_btdh_search_raid_device(ioc, &karg);
1445 if (copy_to_user(arg, &karg, sizeof(karg))) {
1446 pr_err("failure at %s:%d/%s()!\n",
1447 __FILE__, __LINE__, __func__);
1454 * _ctl_diag_capability - return diag buffer capability
1455 * @ioc: per adapter object
1456 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1458 * returns 1 when diag buffer support is enabled in firmware
1461 _ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1465 switch (buffer_type) {
1466 case MPI2_DIAG_BUF_TYPE_TRACE:
1467 if (ioc->facts.IOCCapabilities &
1468 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1471 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1472 if (ioc->facts.IOCCapabilities &
1473 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1476 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1477 if (ioc->facts.IOCCapabilities &
1478 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1487 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1488 * @ioc: per adapter object
1489 * @diag_register: the diag_register struct passed in from user space
1493 _ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1494 struct mpt3_diag_register *diag_register)
1497 void *request_data = NULL;
1498 dma_addr_t request_data_dma;
1499 u32 request_data_sz = 0;
1500 Mpi2DiagBufferPostRequest_t *mpi_request;
1501 Mpi2DiagBufferPostReply_t *mpi_reply;
1508 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1511 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1512 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1514 "%s: failed due to ioc not operational\n",
1515 ioc->name, __func__);
1520 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1521 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1522 ioc->name, __func__);
1527 buffer_type = diag_register->buffer_type;
1528 if (!_ctl_diag_capability(ioc, buffer_type)) {
1530 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1531 ioc->name, __func__, buffer_type);
1535 if (ioc->diag_buffer_status[buffer_type] &
1536 MPT3_DIAG_BUFFER_IS_REGISTERED) {
1538 "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1539 ioc->name, __func__,
1544 if (diag_register->requested_buffer_size % 4) {
1546 "%s: the requested_buffer_size is not 4 byte aligned\n",
1547 ioc->name, __func__);
1551 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1553 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1554 ioc->name, __func__);
1560 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1561 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1562 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1563 ioc->ctl_cmds.smid = smid;
1565 request_data = ioc->diag_buffer[buffer_type];
1566 request_data_sz = diag_register->requested_buffer_size;
1567 ioc->unique_id[buffer_type] = diag_register->unique_id;
1568 ioc->diag_buffer_status[buffer_type] = 0;
1569 memcpy(ioc->product_specific[buffer_type],
1570 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1571 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1574 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1575 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1576 pci_free_consistent(ioc->pdev,
1577 ioc->diag_buffer_sz[buffer_type],
1578 request_data, request_data_dma);
1579 request_data = NULL;
1583 if (request_data == NULL) {
1584 ioc->diag_buffer_sz[buffer_type] = 0;
1585 ioc->diag_buffer_dma[buffer_type] = 0;
1586 request_data = pci_alloc_consistent(
1587 ioc->pdev, request_data_sz, &request_data_dma);
1588 if (request_data == NULL) {
1589 pr_err(MPT3SAS_FMT "%s: failed allocating memory" \
1590 " for diag buffers, requested size(%d)\n",
1591 ioc->name, __func__, request_data_sz);
1592 mpt3sas_base_free_smid(ioc, smid);
1595 ioc->diag_buffer[buffer_type] = request_data;
1596 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1597 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1600 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1601 mpi_request->BufferType = diag_register->buffer_type;
1602 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1603 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1604 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1605 mpi_request->VF_ID = 0; /* TODO */
1606 mpi_request->VP_ID = 0;
1608 dctlprintk(ioc, pr_info(MPT3SAS_FMT
1609 "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1610 ioc->name, __func__, request_data,
1611 (unsigned long long)request_data_dma,
1612 le32_to_cpu(mpi_request->BufferLength)));
1614 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1615 mpi_request->ProductSpecific[i] =
1616 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1618 init_completion(&ioc->ctl_cmds.done);
1619 mpt3sas_base_put_smid_default(ioc, smid);
1620 wait_for_completion_timeout(&ioc->ctl_cmds.done,
1621 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1623 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1624 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
1626 _debug_dump_mf(mpi_request,
1627 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1628 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
1630 goto issue_host_reset;
1633 /* process the completed Reply Message Frame */
1634 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1635 pr_err(MPT3SAS_FMT "%s: no reply message\n",
1636 ioc->name, __func__);
1641 mpi_reply = ioc->ctl_cmds.reply;
1642 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1644 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1645 ioc->diag_buffer_status[buffer_type] |=
1646 MPT3_DIAG_BUFFER_IS_REGISTERED;
1647 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
1648 ioc->name, __func__));
1651 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1652 ioc->name, __func__,
1653 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1659 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1663 if (rc && request_data)
1664 pci_free_consistent(ioc->pdev, request_data_sz,
1665 request_data, request_data_dma);
1667 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1672 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1673 * @ioc: per adapter object
1674 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1676 * This is called when command line option diag_buffer_enable is enabled
1677 * at driver load time.
1680 mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1682 struct mpt3_diag_register diag_register;
1684 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1686 if (bits_to_register & 1) {
1687 pr_info(MPT3SAS_FMT "registering trace buffer support\n",
1689 ioc->diag_trigger_master.MasterData =
1690 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1691 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1692 /* register for 2MB buffers */
1693 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1694 diag_register.unique_id = 0x7075900;
1695 _ctl_diag_register_2(ioc, &diag_register);
1698 if (bits_to_register & 2) {
1699 pr_info(MPT3SAS_FMT "registering snapshot buffer support\n",
1701 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1702 /* register for 2MB buffers */
1703 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1704 diag_register.unique_id = 0x7075901;
1705 _ctl_diag_register_2(ioc, &diag_register);
1708 if (bits_to_register & 4) {
1709 pr_info(MPT3SAS_FMT "registering extended buffer support\n",
1711 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1712 /* register for 2MB buffers */
1713 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1714 diag_register.unique_id = 0x7075901;
1715 _ctl_diag_register_2(ioc, &diag_register);
1720 * _ctl_diag_register - application register with driver
1721 * @ioc: per adapter object
1722 * @arg - user space buffer containing ioctl content
1724 * This will allow the driver to setup any required buffers that will be
1725 * needed by firmware to communicate with the driver.
1728 _ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1730 struct mpt3_diag_register karg;
1733 if (copy_from_user(&karg, arg, sizeof(karg))) {
1734 pr_err("failure at %s:%d/%s()!\n",
1735 __FILE__, __LINE__, __func__);
1739 rc = _ctl_diag_register_2(ioc, &karg);
1744 * _ctl_diag_unregister - application unregister with driver
1745 * @ioc: per adapter object
1746 * @arg - user space buffer containing ioctl content
1748 * This will allow the driver to cleanup any memory allocated for diag
1749 * messages and to free up any resources.
1752 _ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1754 struct mpt3_diag_unregister karg;
1756 dma_addr_t request_data_dma;
1757 u32 request_data_sz;
1760 if (copy_from_user(&karg, arg, sizeof(karg))) {
1761 pr_err("failure at %s:%d/%s()!\n",
1762 __FILE__, __LINE__, __func__);
1766 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1769 buffer_type = karg.unique_id & 0x000000ff;
1770 if (!_ctl_diag_capability(ioc, buffer_type)) {
1772 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1773 ioc->name, __func__, buffer_type);
1777 if ((ioc->diag_buffer_status[buffer_type] &
1778 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1780 "%s: buffer_type(0x%02x) is not registered\n",
1781 ioc->name, __func__, buffer_type);
1784 if ((ioc->diag_buffer_status[buffer_type] &
1785 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
1787 "%s: buffer_type(0x%02x) has not been released\n",
1788 ioc->name, __func__, buffer_type);
1792 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1794 "%s: unique_id(0x%08x) is not registered\n",
1795 ioc->name, __func__, karg.unique_id);
1799 request_data = ioc->diag_buffer[buffer_type];
1800 if (!request_data) {
1802 "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1803 ioc->name, __func__, buffer_type);
1807 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1808 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1809 pci_free_consistent(ioc->pdev, request_data_sz,
1810 request_data, request_data_dma);
1811 ioc->diag_buffer[buffer_type] = NULL;
1812 ioc->diag_buffer_status[buffer_type] = 0;
1817 * _ctl_diag_query - query relevant info associated with diag buffers
1818 * @ioc: per adapter object
1819 * @arg - user space buffer containing ioctl content
1821 * The application will send only buffer_type and unique_id. Driver will
1822 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1823 * 0x00, the driver will return info specified by Buffer Type.
1826 _ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1828 struct mpt3_diag_query karg;
1833 if (copy_from_user(&karg, arg, sizeof(karg))) {
1834 pr_err("failure at %s:%d/%s()!\n",
1835 __FILE__, __LINE__, __func__);
1839 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1842 karg.application_flags = 0;
1843 buffer_type = karg.buffer_type;
1845 if (!_ctl_diag_capability(ioc, buffer_type)) {
1847 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1848 ioc->name, __func__, buffer_type);
1852 if ((ioc->diag_buffer_status[buffer_type] &
1853 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1855 "%s: buffer_type(0x%02x) is not registered\n",
1856 ioc->name, __func__, buffer_type);
1860 if (karg.unique_id & 0xffffff00) {
1861 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1863 "%s: unique_id(0x%08x) is not registered\n",
1864 ioc->name, __func__, karg.unique_id);
1869 request_data = ioc->diag_buffer[buffer_type];
1870 if (!request_data) {
1872 "%s: doesn't have buffer for buffer_type(0x%02x)\n",
1873 ioc->name, __func__, buffer_type);
1877 if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
1878 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1879 MPT3_APP_FLAGS_BUFFER_VALID);
1881 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1882 MPT3_APP_FLAGS_BUFFER_VALID |
1883 MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
1885 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1886 karg.product_specific[i] =
1887 ioc->product_specific[buffer_type][i];
1889 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1890 karg.driver_added_buffer_size = 0;
1891 karg.unique_id = ioc->unique_id[buffer_type];
1892 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1894 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
1896 "%s: unable to write mpt3_diag_query data @ %p\n",
1897 ioc->name, __func__, arg);
1904 * mpt3sas_send_diag_release - Diag Release Message
1905 * @ioc: per adapter object
1906 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1907 * @issue_reset - specifies whether host reset is required.
1911 mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
1914 Mpi2DiagReleaseRequest_t *mpi_request;
1915 Mpi2DiagReleaseReply_t *mpi_reply;
1921 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1927 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1928 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1929 if (ioc->diag_buffer_status[buffer_type] &
1930 MPT3_DIAG_BUFFER_IS_REGISTERED)
1931 ioc->diag_buffer_status[buffer_type] |=
1932 MPT3_DIAG_BUFFER_IS_RELEASED;
1933 dctlprintk(ioc, pr_info(MPT3SAS_FMT
1934 "%s: skipping due to FAULT state\n", ioc->name,
1940 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1941 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1942 ioc->name, __func__);
1947 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1949 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1950 ioc->name, __func__);
1955 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1956 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1957 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1958 ioc->ctl_cmds.smid = smid;
1960 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1961 mpi_request->BufferType = buffer_type;
1962 mpi_request->VF_ID = 0; /* TODO */
1963 mpi_request->VP_ID = 0;
1965 init_completion(&ioc->ctl_cmds.done);
1966 mpt3sas_base_put_smid_default(ioc, smid);
1967 wait_for_completion_timeout(&ioc->ctl_cmds.done,
1968 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1970 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1971 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
1973 _debug_dump_mf(mpi_request,
1974 sizeof(Mpi2DiagReleaseRequest_t)/4);
1975 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
1981 /* process the completed Reply Message Frame */
1982 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1983 pr_err(MPT3SAS_FMT "%s: no reply message\n",
1984 ioc->name, __func__);
1989 mpi_reply = ioc->ctl_cmds.reply;
1990 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1992 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1993 ioc->diag_buffer_status[buffer_type] |=
1994 MPT3_DIAG_BUFFER_IS_RELEASED;
1995 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
1996 ioc->name, __func__));
1999 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2000 ioc->name, __func__,
2001 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2006 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2011 * _ctl_diag_release - request to send Diag Release Message to firmware
2012 * @arg - user space buffer containing ioctl content
2014 * This allows ownership of the specified buffer to returned to the driver,
2015 * allowing an application to read the buffer without fear that firmware is
2016 * overwriting information in the buffer.
2019 _ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2021 struct mpt3_diag_release karg;
2027 if (copy_from_user(&karg, arg, sizeof(karg))) {
2028 pr_err("failure at %s:%d/%s()!\n",
2029 __FILE__, __LINE__, __func__);
2033 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2036 buffer_type = karg.unique_id & 0x000000ff;
2037 if (!_ctl_diag_capability(ioc, buffer_type)) {
2039 "%s: doesn't have capability for buffer_type(0x%02x)\n",
2040 ioc->name, __func__, buffer_type);
2044 if ((ioc->diag_buffer_status[buffer_type] &
2045 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2047 "%s: buffer_type(0x%02x) is not registered\n",
2048 ioc->name, __func__, buffer_type);
2052 if (karg.unique_id != ioc->unique_id[buffer_type]) {
2054 "%s: unique_id(0x%08x) is not registered\n",
2055 ioc->name, __func__, karg.unique_id);
2059 if (ioc->diag_buffer_status[buffer_type] &
2060 MPT3_DIAG_BUFFER_IS_RELEASED) {
2062 "%s: buffer_type(0x%02x) is already released\n",
2063 ioc->name, __func__,
2068 request_data = ioc->diag_buffer[buffer_type];
2070 if (!request_data) {
2072 "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2073 ioc->name, __func__, buffer_type);
2077 /* buffers were released by due to host reset */
2078 if ((ioc->diag_buffer_status[buffer_type] &
2079 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2080 ioc->diag_buffer_status[buffer_type] |=
2081 MPT3_DIAG_BUFFER_IS_RELEASED;
2082 ioc->diag_buffer_status[buffer_type] &=
2083 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
2085 "%s: buffer_type(0x%02x) was released due to host reset\n",
2086 ioc->name, __func__, buffer_type);
2090 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2093 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2099 * _ctl_diag_read_buffer - request for copy of the diag buffer
2100 * @ioc: per adapter object
2101 * @arg - user space buffer containing ioctl content
2104 _ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2106 struct mpt3_diag_read_buffer karg;
2107 struct mpt3_diag_read_buffer __user *uarg = arg;
2108 void *request_data, *diag_data;
2109 Mpi2DiagBufferPostRequest_t *mpi_request;
2110 Mpi2DiagBufferPostReply_t *mpi_reply;
2113 unsigned long request_size, copy_size;
2118 if (copy_from_user(&karg, arg, sizeof(karg))) {
2119 pr_err("failure at %s:%d/%s()!\n",
2120 __FILE__, __LINE__, __func__);
2124 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2127 buffer_type = karg.unique_id & 0x000000ff;
2128 if (!_ctl_diag_capability(ioc, buffer_type)) {
2130 "%s: doesn't have capability for buffer_type(0x%02x)\n",
2131 ioc->name, __func__, buffer_type);
2135 if (karg.unique_id != ioc->unique_id[buffer_type]) {
2137 "%s: unique_id(0x%08x) is not registered\n",
2138 ioc->name, __func__, karg.unique_id);
2142 request_data = ioc->diag_buffer[buffer_type];
2143 if (!request_data) {
2145 "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2146 ioc->name, __func__, buffer_type);
2150 request_size = ioc->diag_buffer_sz[buffer_type];
2152 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2153 pr_err(MPT3SAS_FMT "%s: either the starting_offset " \
2154 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
2159 if (karg.starting_offset > request_size)
2162 diag_data = (void *)(request_data + karg.starting_offset);
2163 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2164 "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2165 ioc->name, __func__,
2166 diag_data, karg.starting_offset, karg.bytes_to_read));
2168 /* Truncate data on requests that are too large */
2169 if ((diag_data + karg.bytes_to_read < diag_data) ||
2170 (diag_data + karg.bytes_to_read > request_data + request_size))
2171 copy_size = request_size - karg.starting_offset;
2173 copy_size = karg.bytes_to_read;
2175 if (copy_to_user((void __user *)uarg->diagnostic_data,
2176 diag_data, copy_size)) {
2178 "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2179 ioc->name, __func__, diag_data);
2183 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2186 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2187 "%s: Reregister buffer_type(0x%02x)\n",
2188 ioc->name, __func__, buffer_type));
2189 if ((ioc->diag_buffer_status[buffer_type] &
2190 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2191 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2192 "%s: buffer_type(0x%02x) is still registered\n",
2193 ioc->name, __func__, buffer_type));
2196 /* Get a free request frame and save the message context.
2199 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2200 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
2201 ioc->name, __func__);
2206 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2208 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2209 ioc->name, __func__);
2215 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2216 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2217 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2218 ioc->ctl_cmds.smid = smid;
2220 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2221 mpi_request->BufferType = buffer_type;
2222 mpi_request->BufferLength =
2223 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2224 mpi_request->BufferAddress =
2225 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2226 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2227 mpi_request->ProductSpecific[i] =
2228 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2229 mpi_request->VF_ID = 0; /* TODO */
2230 mpi_request->VP_ID = 0;
2232 init_completion(&ioc->ctl_cmds.done);
2233 mpt3sas_base_put_smid_default(ioc, smid);
2234 wait_for_completion_timeout(&ioc->ctl_cmds.done,
2235 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2237 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2238 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
2240 _debug_dump_mf(mpi_request,
2241 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2242 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
2244 goto issue_host_reset;
2247 /* process the completed Reply Message Frame */
2248 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2249 pr_err(MPT3SAS_FMT "%s: no reply message\n",
2250 ioc->name, __func__);
2255 mpi_reply = ioc->ctl_cmds.reply;
2256 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2258 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2259 ioc->diag_buffer_status[buffer_type] |=
2260 MPT3_DIAG_BUFFER_IS_REGISTERED;
2261 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
2262 ioc->name, __func__));
2265 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2266 ioc->name, __func__,
2267 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2273 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2277 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2283 #ifdef CONFIG_COMPAT
2285 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2286 * @ioc: per adapter object
2287 * @cmd - ioctl opcode
2288 * @arg - (struct mpt3_ioctl_command32)
2290 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2293 _ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2296 struct mpt3_ioctl_command32 karg32;
2297 struct mpt3_ioctl_command32 __user *uarg;
2298 struct mpt3_ioctl_command karg;
2300 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2303 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2305 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2306 pr_err("failure at %s:%d/%s()!\n",
2307 __FILE__, __LINE__, __func__);
2311 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2312 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2313 karg.hdr.port_number = karg32.hdr.port_number;
2314 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2315 karg.timeout = karg32.timeout;
2316 karg.max_reply_bytes = karg32.max_reply_bytes;
2317 karg.data_in_size = karg32.data_in_size;
2318 karg.data_out_size = karg32.data_out_size;
2319 karg.max_sense_bytes = karg32.max_sense_bytes;
2320 karg.data_sge_offset = karg32.data_sge_offset;
2321 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2322 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2323 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2324 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2325 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2330 * _ctl_ioctl_main - main ioctl entry point
2331 * @file - (struct file)
2332 * @cmd - ioctl opcode
2333 * @arg - user space data buffer
2334 * @compat - handles 32 bit applications in 64bit os
2335 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
2336 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
2339 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2340 u8 compat, u16 mpi_version)
2342 struct MPT3SAS_ADAPTER *ioc;
2343 struct mpt3_ioctl_header ioctl_header;
2344 enum block_state state;
2347 /* get IOCTL header */
2348 if (copy_from_user(&ioctl_header, (char __user *)arg,
2349 sizeof(struct mpt3_ioctl_header))) {
2350 pr_err("failure at %s:%d/%s()!\n",
2351 __FILE__, __LINE__, __func__);
2355 if (_ctl_verify_adapter(ioctl_header.ioc_number,
2356 &ioc, mpi_version) == -1 || !ioc)
2359 /* pci_access_mutex lock acquired by ioctl path */
2360 mutex_lock(&ioc->pci_access_mutex);
2362 if (ioc->shost_recovery || ioc->pci_error_recovery ||
2363 ioc->is_driver_loading || ioc->remove_host) {
2365 goto out_unlock_pciaccess;
2368 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2369 if (state == NON_BLOCKING) {
2370 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2372 goto out_unlock_pciaccess;
2374 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2376 goto out_unlock_pciaccess;
2382 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2383 ret = _ctl_getiocinfo(ioc, arg);
2385 #ifdef CONFIG_COMPAT
2390 struct mpt3_ioctl_command __user *uarg;
2391 struct mpt3_ioctl_command karg;
2393 #ifdef CONFIG_COMPAT
2395 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2399 if (copy_from_user(&karg, arg, sizeof(karg))) {
2400 pr_err("failure at %s:%d/%s()!\n",
2401 __FILE__, __LINE__, __func__);
2406 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2408 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2412 case MPT3EVENTQUERY:
2413 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2414 ret = _ctl_eventquery(ioc, arg);
2416 case MPT3EVENTENABLE:
2417 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2418 ret = _ctl_eventenable(ioc, arg);
2420 case MPT3EVENTREPORT:
2421 ret = _ctl_eventreport(ioc, arg);
2424 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2425 ret = _ctl_do_reset(ioc, arg);
2427 case MPT3BTDHMAPPING:
2428 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2429 ret = _ctl_btdh_mapping(ioc, arg);
2431 case MPT3DIAGREGISTER:
2432 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2433 ret = _ctl_diag_register(ioc, arg);
2435 case MPT3DIAGUNREGISTER:
2436 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2437 ret = _ctl_diag_unregister(ioc, arg);
2440 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2441 ret = _ctl_diag_query(ioc, arg);
2443 case MPT3DIAGRELEASE:
2444 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2445 ret = _ctl_diag_release(ioc, arg);
2447 case MPT3DIAGREADBUFFER:
2448 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2449 ret = _ctl_diag_read_buffer(ioc, arg);
2452 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2453 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2457 mutex_unlock(&ioc->ctl_cmds.mutex);
2458 out_unlock_pciaccess:
2459 mutex_unlock(&ioc->pci_access_mutex);
2464 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
2465 * @file - (struct file)
2466 * @cmd - ioctl opcode
2470 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2474 /* pass MPI25_VERSION | MPI26_VERSION value,
2475 * to indicate that this ioctl cmd
2476 * came from mpt3ctl ioctl device.
2478 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2479 MPI25_VERSION | MPI26_VERSION);
2484 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
2485 * @file - (struct file)
2486 * @cmd - ioctl opcode
2490 _ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2494 /* pass MPI2_VERSION value, to indicate that this ioctl cmd
2495 * came from mpt2ctl ioctl device.
2497 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2500 #ifdef CONFIG_COMPAT
2502 *_ ctl_ioctl_compat - main ioctl entry point (compat)
2507 * This routine handles 32 bit applications in 64bit os.
2510 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2514 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2515 MPI25_VERSION | MPI26_VERSION);
2520 *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
2525 * This routine handles 32 bit applications in 64bit os.
2528 _ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2532 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
2537 /* scsi host attributes */
2539 * _ctl_version_fw_show - firmware version
2540 * @cdev - pointer to embedded class device
2541 * @buf - the buffer returned
2543 * A sysfs 'read-only' shost attribute.
2546 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2549 struct Scsi_Host *shost = class_to_shost(cdev);
2550 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2552 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2553 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2554 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2555 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2556 ioc->facts.FWVersion.Word & 0x000000FF);
2558 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2561 * _ctl_version_bios_show - bios version
2562 * @cdev - pointer to embedded class device
2563 * @buf - the buffer returned
2565 * A sysfs 'read-only' shost attribute.
2568 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2571 struct Scsi_Host *shost = class_to_shost(cdev);
2572 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2574 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2576 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2577 (version & 0xFF000000) >> 24,
2578 (version & 0x00FF0000) >> 16,
2579 (version & 0x0000FF00) >> 8,
2580 version & 0x000000FF);
2582 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2585 * _ctl_version_mpi_show - MPI (message passing interface) version
2586 * @cdev - pointer to embedded class device
2587 * @buf - the buffer returned
2589 * A sysfs 'read-only' shost attribute.
2592 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2595 struct Scsi_Host *shost = class_to_shost(cdev);
2596 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2598 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2599 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2601 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2604 * _ctl_version_product_show - product name
2605 * @cdev - pointer to embedded class device
2606 * @buf - the buffer returned
2608 * A sysfs 'read-only' shost attribute.
2611 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2614 struct Scsi_Host *shost = class_to_shost(cdev);
2615 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2617 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2619 static DEVICE_ATTR(version_product, S_IRUGO, _ctl_version_product_show, NULL);
2622 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2623 * @cdev - pointer to embedded class device
2624 * @buf - the buffer returned
2626 * A sysfs 'read-only' shost attribute.
2629 _ctl_version_nvdata_persistent_show(struct device *cdev,
2630 struct device_attribute *attr, char *buf)
2632 struct Scsi_Host *shost = class_to_shost(cdev);
2633 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2635 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2636 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2638 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2639 _ctl_version_nvdata_persistent_show, NULL);
2642 * _ctl_version_nvdata_default_show - nvdata default version
2643 * @cdev - pointer to embedded class device
2644 * @buf - the buffer returned
2646 * A sysfs 'read-only' shost attribute.
2649 _ctl_version_nvdata_default_show(struct device *cdev, struct device_attribute
2652 struct Scsi_Host *shost = class_to_shost(cdev);
2653 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2655 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2656 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2658 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2659 _ctl_version_nvdata_default_show, NULL);
2662 * _ctl_board_name_show - board name
2663 * @cdev - pointer to embedded class device
2664 * @buf - the buffer returned
2666 * A sysfs 'read-only' shost attribute.
2669 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2672 struct Scsi_Host *shost = class_to_shost(cdev);
2673 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2675 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2677 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2680 * _ctl_board_assembly_show - board assembly name
2681 * @cdev - pointer to embedded class device
2682 * @buf - the buffer returned
2684 * A sysfs 'read-only' shost attribute.
2687 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2690 struct Scsi_Host *shost = class_to_shost(cdev);
2691 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2693 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2695 static DEVICE_ATTR(board_assembly, S_IRUGO, _ctl_board_assembly_show, NULL);
2698 * _ctl_board_tracer_show - board tracer number
2699 * @cdev - pointer to embedded class device
2700 * @buf - the buffer returned
2702 * A sysfs 'read-only' shost attribute.
2705 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2708 struct Scsi_Host *shost = class_to_shost(cdev);
2709 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2711 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2713 static DEVICE_ATTR(board_tracer, S_IRUGO, _ctl_board_tracer_show, NULL);
2716 * _ctl_io_delay_show - io missing delay
2717 * @cdev - pointer to embedded class device
2718 * @buf - the buffer returned
2720 * This is for firmware implemention for deboucing device
2723 * A sysfs 'read-only' shost attribute.
2726 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2729 struct Scsi_Host *shost = class_to_shost(cdev);
2730 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2732 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2734 static DEVICE_ATTR(io_delay, S_IRUGO, _ctl_io_delay_show, NULL);
2737 * _ctl_device_delay_show - device missing delay
2738 * @cdev - pointer to embedded class device
2739 * @buf - the buffer returned
2741 * This is for firmware implemention for deboucing device
2744 * A sysfs 'read-only' shost attribute.
2747 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2750 struct Scsi_Host *shost = class_to_shost(cdev);
2751 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2753 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2755 static DEVICE_ATTR(device_delay, S_IRUGO, _ctl_device_delay_show, NULL);
2758 * _ctl_fw_queue_depth_show - global credits
2759 * @cdev - pointer to embedded class device
2760 * @buf - the buffer returned
2762 * This is firmware queue depth limit
2764 * A sysfs 'read-only' shost attribute.
2767 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2770 struct Scsi_Host *shost = class_to_shost(cdev);
2771 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2773 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2775 static DEVICE_ATTR(fw_queue_depth, S_IRUGO, _ctl_fw_queue_depth_show, NULL);
2778 * _ctl_sas_address_show - sas address
2779 * @cdev - pointer to embedded class device
2780 * @buf - the buffer returned
2782 * This is the controller sas address
2784 * A sysfs 'read-only' shost attribute.
2787 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2791 struct Scsi_Host *shost = class_to_shost(cdev);
2792 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2794 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2795 (unsigned long long)ioc->sas_hba.sas_address);
2797 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2798 _ctl_host_sas_address_show, NULL);
2801 * _ctl_logging_level_show - logging level
2802 * @cdev - pointer to embedded class device
2803 * @buf - the buffer returned
2805 * A sysfs 'read/write' shost attribute.
2808 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2811 struct Scsi_Host *shost = class_to_shost(cdev);
2812 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2814 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2817 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2818 const char *buf, size_t count)
2820 struct Scsi_Host *shost = class_to_shost(cdev);
2821 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2824 if (sscanf(buf, "%x", &val) != 1)
2827 ioc->logging_level = val;
2828 pr_info(MPT3SAS_FMT "logging_level=%08xh\n", ioc->name,
2829 ioc->logging_level);
2832 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR, _ctl_logging_level_show,
2833 _ctl_logging_level_store);
2836 * _ctl_fwfault_debug_show - show/store fwfault_debug
2837 * @cdev - pointer to embedded class device
2838 * @buf - the buffer returned
2840 * mpt3sas_fwfault_debug is command line option
2841 * A sysfs 'read/write' shost attribute.
2844 _ctl_fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
2847 struct Scsi_Host *shost = class_to_shost(cdev);
2848 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2850 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2853 _ctl_fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
2854 const char *buf, size_t count)
2856 struct Scsi_Host *shost = class_to_shost(cdev);
2857 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2860 if (sscanf(buf, "%d", &val) != 1)
2863 ioc->fwfault_debug = val;
2864 pr_info(MPT3SAS_FMT "fwfault_debug=%d\n", ioc->name,
2865 ioc->fwfault_debug);
2868 static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2869 _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2872 * _ctl_ioc_reset_count_show - ioc reset count
2873 * @cdev - pointer to embedded class device
2874 * @buf - the buffer returned
2876 * This is firmware queue depth limit
2878 * A sysfs 'read-only' shost attribute.
2881 _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2884 struct Scsi_Host *shost = class_to_shost(cdev);
2885 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2887 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
2889 static DEVICE_ATTR(ioc_reset_count, S_IRUGO, _ctl_ioc_reset_count_show, NULL);
2892 * _ctl_ioc_reply_queue_count_show - number of reply queues
2893 * @cdev - pointer to embedded class device
2894 * @buf - the buffer returned
2896 * This is number of reply queues
2898 * A sysfs 'read-only' shost attribute.
2901 _ctl_ioc_reply_queue_count_show(struct device *cdev,
2902 struct device_attribute *attr, char *buf)
2904 u8 reply_queue_count;
2905 struct Scsi_Host *shost = class_to_shost(cdev);
2906 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2908 if ((ioc->facts.IOCCapabilities &
2909 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2910 reply_queue_count = ioc->reply_queue_count;
2912 reply_queue_count = 1;
2914 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2916 static DEVICE_ATTR(reply_queue_count, S_IRUGO, _ctl_ioc_reply_queue_count_show,
2920 * _ctl_BRM_status_show - Backup Rail Monitor Status
2921 * @cdev - pointer to embedded class device
2922 * @buf - the buffer returned
2924 * This is number of reply queues
2926 * A sysfs 'read-only' shost attribute.
2929 _ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
2932 struct Scsi_Host *shost = class_to_shost(cdev);
2933 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2934 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2935 Mpi2ConfigReply_t mpi_reply;
2936 u16 backup_rail_monitor_status = 0;
2941 if (!ioc->is_warpdrive) {
2942 pr_err(MPT3SAS_FMT "%s: BRM attribute is only for"
2943 " warpdrive\n", ioc->name, __func__);
2946 /* pci_access_mutex lock acquired by sysfs show path */
2947 mutex_lock(&ioc->pci_access_mutex);
2948 if (ioc->pci_error_recovery || ioc->remove_host) {
2949 mutex_unlock(&ioc->pci_access_mutex);
2953 /* allocate upto GPIOVal 36 entries */
2954 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2955 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2957 pr_err(MPT3SAS_FMT "%s: failed allocating memory "
2958 "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
2962 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2965 "%s: failed reading iounit_pg3\n", ioc->name,
2970 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2971 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2972 pr_err(MPT3SAS_FMT "%s: iounit_pg3 failed with "
2973 "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
2977 if (io_unit_pg3->GPIOCount < 25) {
2978 pr_err(MPT3SAS_FMT "%s: iounit_pg3->GPIOCount less than "
2979 "25 entries, detected (%d) entries\n", ioc->name, __func__,
2980 io_unit_pg3->GPIOCount);
2984 /* BRM status is in bit zero of GPIOVal[24] */
2985 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2986 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2990 mutex_unlock(&ioc->pci_access_mutex);
2993 static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
2995 struct DIAG_BUFFER_START {
3006 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
3007 * @cdev - pointer to embedded class device
3008 * @buf - the buffer returned
3010 * A sysfs 'read-only' shost attribute.
3013 _ctl_host_trace_buffer_size_show(struct device *cdev,
3014 struct device_attribute *attr, char *buf)
3016 struct Scsi_Host *shost = class_to_shost(cdev);
3017 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3019 struct DIAG_BUFFER_START *request_data;
3021 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3023 "%s: host_trace_buffer is not registered\n",
3024 ioc->name, __func__);
3028 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3029 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3031 "%s: host_trace_buffer is not registered\n",
3032 ioc->name, __func__);
3036 request_data = (struct DIAG_BUFFER_START *)
3037 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
3038 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
3039 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
3040 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
3041 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
3042 size = le32_to_cpu(request_data->Size);
3044 ioc->ring_buffer_sz = size;
3045 return snprintf(buf, PAGE_SIZE, "%d\n", size);
3047 static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
3048 _ctl_host_trace_buffer_size_show, NULL);
3051 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
3052 * @cdev - pointer to embedded class device
3053 * @buf - the buffer returned
3055 * A sysfs 'read/write' shost attribute.
3057 * You will only be able to read 4k bytes of ring buffer at a time.
3058 * In order to read beyond 4k bytes, you will have to write out the
3059 * offset to the same attribute, it will move the pointer.
3062 _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
3065 struct Scsi_Host *shost = class_to_shost(cdev);
3066 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3070 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3072 "%s: host_trace_buffer is not registered\n",
3073 ioc->name, __func__);
3077 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3078 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3080 "%s: host_trace_buffer is not registered\n",
3081 ioc->name, __func__);
3085 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3088 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3089 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3090 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3091 memcpy(buf, request_data, size);
3096 _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
3097 const char *buf, size_t count)
3099 struct Scsi_Host *shost = class_to_shost(cdev);
3100 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3103 if (sscanf(buf, "%d", &val) != 1)
3106 ioc->ring_buffer_offset = val;
3109 static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
3110 _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
3113 /*****************************************/
3116 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
3117 * @cdev - pointer to embedded class device
3118 * @buf - the buffer returned
3120 * A sysfs 'read/write' shost attribute.
3122 * This is a mechnism to post/release host_trace_buffers
3125 _ctl_host_trace_buffer_enable_show(struct device *cdev,
3126 struct device_attribute *attr, char *buf)
3128 struct Scsi_Host *shost = class_to_shost(cdev);
3129 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3131 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3132 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3133 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3134 return snprintf(buf, PAGE_SIZE, "off\n");
3135 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3136 MPT3_DIAG_BUFFER_IS_RELEASED))
3137 return snprintf(buf, PAGE_SIZE, "release\n");
3139 return snprintf(buf, PAGE_SIZE, "post\n");
3143 _ctl_host_trace_buffer_enable_store(struct device *cdev,
3144 struct device_attribute *attr, const char *buf, size_t count)
3146 struct Scsi_Host *shost = class_to_shost(cdev);
3147 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3149 struct mpt3_diag_register diag_register;
3152 /* don't allow post/release occurr while recovery is active */
3153 if (ioc->shost_recovery || ioc->remove_host ||
3154 ioc->pci_error_recovery || ioc->is_driver_loading)
3157 if (sscanf(buf, "%9s", str) != 1)
3160 if (!strcmp(str, "post")) {
3161 /* exit out if host buffers are already posted */
3162 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3163 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3164 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3165 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3166 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3168 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3169 pr_info(MPT3SAS_FMT "posting host trace buffers\n",
3171 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3172 diag_register.requested_buffer_size = (1024 * 1024);
3173 diag_register.unique_id = 0x7075900;
3174 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3175 _ctl_diag_register_2(ioc, &diag_register);
3176 } else if (!strcmp(str, "release")) {
3177 /* exit out if host buffers are already released */
3178 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3180 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3181 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3183 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3184 MPT3_DIAG_BUFFER_IS_RELEASED))
3186 pr_info(MPT3SAS_FMT "releasing host trace buffer\n",
3188 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3195 static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
3196 _ctl_host_trace_buffer_enable_show,
3197 _ctl_host_trace_buffer_enable_store);
3199 /*********** diagnostic trigger suppport *********************************/
3202 * _ctl_diag_trigger_master_show - show the diag_trigger_master attribute
3203 * @cdev - pointer to embedded class device
3204 * @buf - the buffer returned
3206 * A sysfs 'read/write' shost attribute.
3209 _ctl_diag_trigger_master_show(struct device *cdev,
3210 struct device_attribute *attr, char *buf)
3213 struct Scsi_Host *shost = class_to_shost(cdev);
3214 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3215 unsigned long flags;
3218 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3219 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3220 memcpy(buf, &ioc->diag_trigger_master, rc);
3221 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3226 * _ctl_diag_trigger_master_store - store the diag_trigger_master attribute
3227 * @cdev - pointer to embedded class device
3228 * @buf - the buffer returned
3230 * A sysfs 'read/write' shost attribute.
3233 _ctl_diag_trigger_master_store(struct device *cdev,
3234 struct device_attribute *attr, const char *buf, size_t count)
3237 struct Scsi_Host *shost = class_to_shost(cdev);
3238 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3239 unsigned long flags;
3242 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3243 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3244 memset(&ioc->diag_trigger_master, 0,
3245 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3246 memcpy(&ioc->diag_trigger_master, buf, rc);
3247 ioc->diag_trigger_master.MasterData |=
3248 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3249 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3252 static DEVICE_ATTR(diag_trigger_master, S_IRUGO | S_IWUSR,
3253 _ctl_diag_trigger_master_show, _ctl_diag_trigger_master_store);
3257 * _ctl_diag_trigger_event_show - show the diag_trigger_event attribute
3258 * @cdev - pointer to embedded class device
3259 * @buf - the buffer returned
3261 * A sysfs 'read/write' shost attribute.
3264 _ctl_diag_trigger_event_show(struct device *cdev,
3265 struct device_attribute *attr, char *buf)
3267 struct Scsi_Host *shost = class_to_shost(cdev);
3268 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3269 unsigned long flags;
3272 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3273 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3274 memcpy(buf, &ioc->diag_trigger_event, rc);
3275 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3280 * _ctl_diag_trigger_event_store - store the diag_trigger_event attribute
3281 * @cdev - pointer to embedded class device
3282 * @buf - the buffer returned
3284 * A sysfs 'read/write' shost attribute.
3287 _ctl_diag_trigger_event_store(struct device *cdev,
3288 struct device_attribute *attr, const char *buf, size_t count)
3291 struct Scsi_Host *shost = class_to_shost(cdev);
3292 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3293 unsigned long flags;
3296 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3297 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3298 memset(&ioc->diag_trigger_event, 0,
3299 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3300 memcpy(&ioc->diag_trigger_event, buf, sz);
3301 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3302 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3303 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3306 static DEVICE_ATTR(diag_trigger_event, S_IRUGO | S_IWUSR,
3307 _ctl_diag_trigger_event_show, _ctl_diag_trigger_event_store);
3311 * _ctl_diag_trigger_scsi_show - show the diag_trigger_scsi attribute
3312 * @cdev - pointer to embedded class device
3313 * @buf - the buffer returned
3315 * A sysfs 'read/write' shost attribute.
3318 _ctl_diag_trigger_scsi_show(struct device *cdev,
3319 struct device_attribute *attr, char *buf)
3321 struct Scsi_Host *shost = class_to_shost(cdev);
3322 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3323 unsigned long flags;
3326 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3327 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3328 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3329 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3334 * _ctl_diag_trigger_scsi_store - store the diag_trigger_scsi attribute
3335 * @cdev - pointer to embedded class device
3336 * @buf - the buffer returned
3338 * A sysfs 'read/write' shost attribute.
3341 _ctl_diag_trigger_scsi_store(struct device *cdev,
3342 struct device_attribute *attr, const char *buf, size_t count)
3344 struct Scsi_Host *shost = class_to_shost(cdev);
3345 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3346 unsigned long flags;
3349 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3350 sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
3351 memset(&ioc->diag_trigger_scsi, 0,
3352 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3353 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3354 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3355 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3356 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3359 static DEVICE_ATTR(diag_trigger_scsi, S_IRUGO | S_IWUSR,
3360 _ctl_diag_trigger_scsi_show, _ctl_diag_trigger_scsi_store);
3364 * _ctl_diag_trigger_scsi_show - show the diag_trigger_mpi attribute
3365 * @cdev - pointer to embedded class device
3366 * @buf - the buffer returned
3368 * A sysfs 'read/write' shost attribute.
3371 _ctl_diag_trigger_mpi_show(struct device *cdev,
3372 struct device_attribute *attr, char *buf)
3374 struct Scsi_Host *shost = class_to_shost(cdev);
3375 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3376 unsigned long flags;
3379 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3380 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3381 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3382 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3387 * _ctl_diag_trigger_mpi_store - store the diag_trigger_mpi attribute
3388 * @cdev - pointer to embedded class device
3389 * @buf - the buffer returned
3391 * A sysfs 'read/write' shost attribute.
3394 _ctl_diag_trigger_mpi_store(struct device *cdev,
3395 struct device_attribute *attr, const char *buf, size_t count)
3397 struct Scsi_Host *shost = class_to_shost(cdev);
3398 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3399 unsigned long flags;
3402 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3403 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3404 memset(&ioc->diag_trigger_mpi, 0,
3405 sizeof(ioc->diag_trigger_mpi));
3406 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3407 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3408 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3409 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3413 static DEVICE_ATTR(diag_trigger_mpi, S_IRUGO | S_IWUSR,
3414 _ctl_diag_trigger_mpi_show, _ctl_diag_trigger_mpi_store);
3416 /*********** diagnostic trigger suppport *** END ****************************/
3418 /*****************************************/
3420 struct device_attribute *mpt3sas_host_attrs[] = {
3421 &dev_attr_version_fw,
3422 &dev_attr_version_bios,
3423 &dev_attr_version_mpi,
3424 &dev_attr_version_product,
3425 &dev_attr_version_nvdata_persistent,
3426 &dev_attr_version_nvdata_default,
3427 &dev_attr_board_name,
3428 &dev_attr_board_assembly,
3429 &dev_attr_board_tracer,
3431 &dev_attr_device_delay,
3432 &dev_attr_logging_level,
3433 &dev_attr_fwfault_debug,
3434 &dev_attr_fw_queue_depth,
3435 &dev_attr_host_sas_address,
3436 &dev_attr_ioc_reset_count,
3437 &dev_attr_host_trace_buffer_size,
3438 &dev_attr_host_trace_buffer,
3439 &dev_attr_host_trace_buffer_enable,
3440 &dev_attr_reply_queue_count,
3441 &dev_attr_diag_trigger_master,
3442 &dev_attr_diag_trigger_event,
3443 &dev_attr_diag_trigger_scsi,
3444 &dev_attr_diag_trigger_mpi,
3445 &dev_attr_BRM_status,
3449 /* device attributes */
3452 * _ctl_device_sas_address_show - sas address
3453 * @cdev - pointer to embedded class device
3454 * @buf - the buffer returned
3456 * This is the sas address for the target
3458 * A sysfs 'read-only' shost attribute.
3461 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
3464 struct scsi_device *sdev = to_scsi_device(dev);
3465 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3467 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3468 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3470 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
3473 * _ctl_device_handle_show - device handle
3474 * @cdev - pointer to embedded class device
3475 * @buf - the buffer returned
3477 * This is the firmware assigned device handle
3479 * A sysfs 'read-only' shost attribute.
3482 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
3485 struct scsi_device *sdev = to_scsi_device(dev);
3486 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3488 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3489 sas_device_priv_data->sas_target->handle);
3491 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
3494 * _ctl_device_ncq_io_prio_show - send prioritized io commands to device
3495 * @dev - pointer to embedded device
3496 * @buf - the buffer returned
3498 * A sysfs 'read/write' sdev attribute, only works with SATA
3501 _ctl_device_ncq_prio_enable_show(struct device *dev,
3502 struct device_attribute *attr, char *buf)
3504 struct scsi_device *sdev = to_scsi_device(dev);
3505 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3507 return snprintf(buf, PAGE_SIZE, "%d\n",
3508 sas_device_priv_data->ncq_prio_enable);
3512 _ctl_device_ncq_prio_enable_store(struct device *dev,
3513 struct device_attribute *attr,
3514 const char *buf, size_t count)
3516 struct scsi_device *sdev = to_scsi_device(dev);
3517 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3518 bool ncq_prio_enable = 0;
3520 if (kstrtobool(buf, &ncq_prio_enable))
3523 if (!scsih_ncq_prio_supp(sdev))
3526 sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
3529 static DEVICE_ATTR(sas_ncq_prio_enable, S_IRUGO | S_IWUSR,
3530 _ctl_device_ncq_prio_enable_show,
3531 _ctl_device_ncq_prio_enable_store);
3533 struct device_attribute *mpt3sas_dev_attrs[] = {
3534 &dev_attr_sas_address,
3535 &dev_attr_sas_device_handle,
3536 &dev_attr_sas_ncq_prio_enable,
3540 /* file operations table for mpt3ctl device */
3541 static const struct file_operations ctl_fops = {
3542 .owner = THIS_MODULE,
3543 .unlocked_ioctl = _ctl_ioctl,
3545 .fasync = _ctl_fasync,
3546 #ifdef CONFIG_COMPAT
3547 .compat_ioctl = _ctl_ioctl_compat,
3551 /* file operations table for mpt2ctl device */
3552 static const struct file_operations ctl_gen2_fops = {
3553 .owner = THIS_MODULE,
3554 .unlocked_ioctl = _ctl_mpt2_ioctl,
3556 .fasync = _ctl_fasync,
3557 #ifdef CONFIG_COMPAT
3558 .compat_ioctl = _ctl_mpt2_ioctl_compat,
3562 static struct miscdevice ctl_dev = {
3563 .minor = MPT3SAS_MINOR,
3564 .name = MPT3SAS_DEV_NAME,
3568 static struct miscdevice gen2_ctl_dev = {
3569 .minor = MPT2SAS_MINOR,
3570 .name = MPT2SAS_DEV_NAME,
3571 .fops = &ctl_gen2_fops,
3575 * mpt3sas_ctl_init - main entry point for ctl.
3579 mpt3sas_ctl_init(ushort hbas_to_enumerate)
3583 /* Don't register mpt3ctl ioctl device if
3584 * hbas_to_enumarate is one.
3586 if (hbas_to_enumerate != 1)
3587 if (misc_register(&ctl_dev) < 0)
3588 pr_err("%s can't register misc device [minor=%d]\n",
3589 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3591 /* Don't register mpt3ctl ioctl device if
3592 * hbas_to_enumarate is two.
3594 if (hbas_to_enumerate != 2)
3595 if (misc_register(&gen2_ctl_dev) < 0)
3596 pr_err("%s can't register misc device [minor=%d]\n",
3597 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3599 init_waitqueue_head(&ctl_poll_wait);
3603 * mpt3sas_ctl_exit - exit point for ctl
3607 mpt3sas_ctl_exit(ushort hbas_to_enumerate)
3609 struct MPT3SAS_ADAPTER *ioc;
3612 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3614 /* free memory associated to diag buffers */
3615 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3616 if (!ioc->diag_buffer[i])
3618 if (!(ioc->diag_buffer_status[i] &
3619 MPT3_DIAG_BUFFER_IS_REGISTERED))
3621 if ((ioc->diag_buffer_status[i] &
3622 MPT3_DIAG_BUFFER_IS_RELEASED))
3624 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3625 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3626 ioc->diag_buffer[i] = NULL;
3627 ioc->diag_buffer_status[i] = 0;
3630 kfree(ioc->event_log);
3632 if (hbas_to_enumerate != 1)
3633 misc_deregister(&ctl_dev);
3634 if (hbas_to_enumerate != 2)
3635 misc_deregister(&gen2_ctl_dev);