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
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/slab.h>
51 #include <linux/types.h>
52 #include <linux/pci.h>
53 #include <linux/delay.h>
54 #include <linux/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_sas_device_find_by_handle - sas device search
83 * @ioc: per adapter object
84 * @handle: sas device handle (assigned by firmware)
85 * Context: Calling function should acquire ioc->sas_device_lock
87 * This searches for sas_device based on sas_address, then return sas_device
90 static struct _sas_device *
91 _ctl_sas_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
93 struct _sas_device *sas_device, *r;
96 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
97 if (sas_device->handle != handle)
108 * _ctl_display_some_debug - debug routine
109 * @ioc: per adapter object
110 * @smid: system request message index
111 * @calling_function_name: string pass from calling function
112 * @mpi_reply: reply message frame
115 * Function for displaying debug info helpful when debugging issues
119 _ctl_display_some_debug(struct MPT3SAS_ADAPTER *ioc, u16 smid,
120 char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
122 Mpi2ConfigRequest_t *mpi_request;
125 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
128 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
129 switch (mpi_request->Function) {
130 case MPI2_FUNCTION_SCSI_IO_REQUEST:
132 Mpi2SCSIIORequest_t *scsi_request =
133 (Mpi2SCSIIORequest_t *)mpi_request;
135 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
136 "scsi_io, cmd(0x%02x), cdb_len(%d)",
137 scsi_request->CDB.CDB32[0],
138 le16_to_cpu(scsi_request->IoFlags) & 0xF);
139 desc = ioc->tmp_string;
142 case MPI2_FUNCTION_SCSI_TASK_MGMT:
145 case MPI2_FUNCTION_IOC_INIT:
148 case MPI2_FUNCTION_IOC_FACTS:
151 case MPI2_FUNCTION_CONFIG:
153 Mpi2ConfigRequest_t *config_request =
154 (Mpi2ConfigRequest_t *)mpi_request;
156 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
157 "config, type(0x%02x), ext_type(0x%02x), number(%d)",
158 (config_request->Header.PageType &
159 MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
160 config_request->Header.PageNumber);
161 desc = ioc->tmp_string;
164 case MPI2_FUNCTION_PORT_FACTS:
167 case MPI2_FUNCTION_PORT_ENABLE:
168 desc = "port_enable";
170 case MPI2_FUNCTION_EVENT_NOTIFICATION:
171 desc = "event_notification";
173 case MPI2_FUNCTION_FW_DOWNLOAD:
174 desc = "fw_download";
176 case MPI2_FUNCTION_FW_UPLOAD:
179 case MPI2_FUNCTION_RAID_ACTION:
180 desc = "raid_action";
182 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
184 Mpi2SCSIIORequest_t *scsi_request =
185 (Mpi2SCSIIORequest_t *)mpi_request;
187 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
188 "raid_pass, cmd(0x%02x), cdb_len(%d)",
189 scsi_request->CDB.CDB32[0],
190 le16_to_cpu(scsi_request->IoFlags) & 0xF);
191 desc = ioc->tmp_string;
194 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
195 desc = "sas_iounit_cntl";
197 case MPI2_FUNCTION_SATA_PASSTHROUGH:
200 case MPI2_FUNCTION_DIAG_BUFFER_POST:
201 desc = "diag_buffer_post";
203 case MPI2_FUNCTION_DIAG_RELEASE:
204 desc = "diag_release";
206 case MPI2_FUNCTION_SMP_PASSTHROUGH:
207 desc = "smp_passthrough";
214 pr_info(MPT3SAS_FMT "%s: %s, smid(%d)\n",
215 ioc->name, calling_function_name, desc, smid);
220 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
222 "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
223 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
224 le32_to_cpu(mpi_reply->IOCLogInfo));
226 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
227 mpi_request->Function ==
228 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
229 Mpi2SCSIIOReply_t *scsi_reply =
230 (Mpi2SCSIIOReply_t *)mpi_reply;
231 struct _sas_device *sas_device = NULL;
234 spin_lock_irqsave(&ioc->sas_device_lock, flags);
235 sas_device = _ctl_sas_device_find_by_handle(ioc,
236 le16_to_cpu(scsi_reply->DevHandle));
238 pr_warn(MPT3SAS_FMT "\tsas_address(0x%016llx), phy(%d)\n",
239 ioc->name, (unsigned long long)
240 sas_device->sas_address, sas_device->phy);
242 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
243 ioc->name, (unsigned long long)
244 sas_device->enclosure_logical_id, sas_device->slot);
246 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
247 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
249 "\tscsi_state(0x%02x), scsi_status"
250 "(0x%02x)\n", ioc->name,
251 scsi_reply->SCSIState,
252 scsi_reply->SCSIStatus);
257 * mpt3sas_ctl_done - ctl module completion routine
258 * @ioc: per adapter object
259 * @smid: system request message index
260 * @msix_index: MSIX table index supplied by the OS
261 * @reply: reply message frame(lower 32bit addr)
264 * The callback handler when using ioc->ctl_cb_idx.
266 * Return 1 meaning mf should be freed from _base_interrupt
267 * 0 means the mf is freed from this function.
270 mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
273 MPI2DefaultReply_t *mpi_reply;
274 Mpi2SCSIIOReply_t *scsiio_reply;
275 const void *sense_data;
278 if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
280 if (ioc->ctl_cmds.smid != smid)
282 ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
283 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
285 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
286 ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
288 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
289 mpi_reply->Function ==
290 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
291 scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
292 if (scsiio_reply->SCSIState &
293 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
294 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
295 le32_to_cpu(scsiio_reply->SenseCount));
296 sense_data = mpt3sas_base_get_sense_buffer(ioc,
298 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
302 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
303 ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
304 complete(&ioc->ctl_cmds.done);
309 * _ctl_check_event_type - determines when an event needs logging
310 * @ioc: per adapter object
311 * @event: firmware event
313 * The bitmask in ioc->event_type[] indicates which events should be
314 * be saved in the driver event_log. This bitmask is set by application.
316 * Returns 1 when event should be captured, or zero means no match.
319 _ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
324 if (event >= 128 || !event || !ioc->event_log)
327 desired_event = (1 << (event % 32));
331 return desired_event & ioc->event_type[i];
335 * mpt3sas_ctl_add_to_event_log - add event
336 * @ioc: per adapter object
337 * @mpi_reply: reply message frame
342 mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
343 Mpi2EventNotificationReply_t *mpi_reply)
345 struct MPT3_IOCTL_EVENTS *event_log;
348 u32 sz, event_data_sz;
354 event = le16_to_cpu(mpi_reply->Event);
356 if (_ctl_check_event_type(ioc, event)) {
358 /* insert entry into circular event_log */
359 i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
360 event_log = ioc->event_log;
361 event_log[i].event = event;
362 event_log[i].context = ioc->event_context++;
364 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
365 sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
366 memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
367 memcpy(event_log[i].data, mpi_reply->EventData, sz);
371 /* This aen_event_read_flag flag is set until the
372 * application has read the event log.
373 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
375 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
376 (send_aen && !ioc->aen_event_read_flag)) {
377 ioc->aen_event_read_flag = 1;
378 wake_up_interruptible(&ctl_poll_wait);
380 kill_fasync(&async_queue, SIGIO, POLL_IN);
385 * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
386 * @ioc: per adapter object
387 * @msix_index: MSIX table index supplied by the OS
388 * @reply: reply message frame(lower 32bit addr)
389 * Context: interrupt.
391 * This function merely adds a new work task into ioc->firmware_event_thread.
392 * The tasks are worked from _firmware_event_work in user context.
394 * Return 1 meaning mf should be freed from _base_interrupt
395 * 0 means the mf is freed from this function.
398 mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
401 Mpi2EventNotificationReply_t *mpi_reply;
403 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
404 mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
409 * _ctl_verify_adapter - validates ioc_number passed from application
410 * @ioc: per adapter object
411 * @iocpp: The ioc pointer is returned in this.
412 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
413 * MPI25_VERSION for mpt3ctl ioctl device.
415 * Return (-1) means error, else ioc_number.
418 _ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
421 struct MPT3SAS_ADAPTER *ioc;
422 /* global ioc lock to protect controller on list operations */
423 spin_lock(&gioc_lock);
424 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
425 if (ioc->id != ioc_number)
427 /* Check whether this ioctl command is from right
428 * ioctl device or not, if not continue the search.
430 if (ioc->hba_mpi_version_belonged != mpi_version)
432 spin_unlock(&gioc_lock);
436 spin_unlock(&gioc_lock);
442 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
443 * @ioc: per adapter object
444 * @reset_phase: phase
446 * The handler for doing any required cleanup or initialization.
448 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
449 * MPT3_IOC_DONE_RESET
452 mpt3sas_ctl_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
457 switch (reset_phase) {
458 case MPT3_IOC_PRE_RESET:
459 dtmprintk(ioc, pr_info(MPT3SAS_FMT
460 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
461 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
462 if (!(ioc->diag_buffer_status[i] &
463 MPT3_DIAG_BUFFER_IS_REGISTERED))
465 if ((ioc->diag_buffer_status[i] &
466 MPT3_DIAG_BUFFER_IS_RELEASED))
468 mpt3sas_send_diag_release(ioc, i, &issue_reset);
471 case MPT3_IOC_AFTER_RESET:
472 dtmprintk(ioc, pr_info(MPT3SAS_FMT
473 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
474 if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
475 ioc->ctl_cmds.status |= MPT3_CMD_RESET;
476 mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
477 complete(&ioc->ctl_cmds.done);
480 case MPT3_IOC_DONE_RESET:
481 dtmprintk(ioc, pr_info(MPT3SAS_FMT
482 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
484 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
485 if (!(ioc->diag_buffer_status[i] &
486 MPT3_DIAG_BUFFER_IS_REGISTERED))
488 if ((ioc->diag_buffer_status[i] &
489 MPT3_DIAG_BUFFER_IS_RELEASED))
491 ioc->diag_buffer_status[i] |=
492 MPT3_DIAG_BUFFER_IS_DIAG_RESET;
504 * Called when application request fasyn callback handler.
507 _ctl_fasync(int fd, struct file *filep, int mode)
509 return fasync_helper(fd, filep, mode, &async_queue);
519 _ctl_poll(struct file *filep, poll_table *wait)
521 struct MPT3SAS_ADAPTER *ioc;
523 poll_wait(filep, &ctl_poll_wait, wait);
525 /* global ioc lock to protect controller on list operations */
526 spin_lock(&gioc_lock);
527 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
528 if (ioc->aen_event_read_flag) {
529 spin_unlock(&gioc_lock);
530 return POLLIN | POLLRDNORM;
533 spin_unlock(&gioc_lock);
538 * _ctl_set_task_mid - assign an active smid to tm request
539 * @ioc: per adapter object
540 * @karg - (struct mpt3_ioctl_command)
541 * @tm_request - pointer to mf from user space
543 * Returns 0 when an smid if found, else fail.
544 * during failure, the reply frame is filled.
547 _ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
548 Mpi2SCSITaskManagementRequest_t *tm_request)
553 struct scsi_cmnd *scmd;
554 struct MPT3SAS_DEVICE *priv_data;
556 Mpi2SCSITaskManagementReply_t *tm_reply;
561 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
563 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
568 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
570 handle = le16_to_cpu(tm_request->DevHandle);
571 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
572 for (i = ioc->scsiio_depth; i && !found; i--) {
573 scmd = ioc->scsi_lookup[i - 1].scmd;
574 if (scmd == NULL || scmd->device == NULL ||
575 scmd->device->hostdata == NULL)
577 if (lun != scmd->device->lun)
579 priv_data = scmd->device->hostdata;
580 if (priv_data->sas_target == NULL)
582 if (priv_data->sas_target->handle != handle)
584 tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
587 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
590 dctlprintk(ioc, pr_info(MPT3SAS_FMT
591 "%s: handle(0x%04x), lun(%d), no active mid!!\n",
593 desc, le16_to_cpu(tm_request->DevHandle), lun));
594 tm_reply = ioc->ctl_cmds.reply;
595 tm_reply->DevHandle = tm_request->DevHandle;
596 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
597 tm_reply->TaskType = tm_request->TaskType;
598 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
599 tm_reply->VP_ID = tm_request->VP_ID;
600 tm_reply->VF_ID = tm_request->VF_ID;
601 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
602 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
604 pr_err("failure at %s:%d/%s()!\n", __FILE__,
609 dctlprintk(ioc, pr_info(MPT3SAS_FMT
610 "%s: handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
611 desc, le16_to_cpu(tm_request->DevHandle), lun,
612 le16_to_cpu(tm_request->TaskMID)));
617 * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
618 * @ioc: per adapter object
619 * @karg - (struct mpt3_ioctl_command)
620 * @mf - pointer to mf in user space
623 _ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
626 MPI2RequestHeader_t *mpi_request = NULL, *request;
627 MPI2DefaultReply_t *mpi_reply;
631 unsigned long timeout, timeleft;
635 void *data_out = NULL;
636 dma_addr_t data_out_dma = 0;
637 size_t data_out_sz = 0;
638 void *data_in = NULL;
639 dma_addr_t data_in_dma = 0;
640 size_t data_in_sz = 0;
642 u16 wait_state_count;
646 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
647 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
648 ioc->name, __func__);
653 wait_state_count = 0;
654 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
655 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
656 if (wait_state_count++ == 10) {
658 "%s: failed due to ioc not operational\n",
659 ioc->name, __func__);
664 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
666 "%s: waiting for operational state(count=%d)\n",
668 __func__, wait_state_count);
670 if (wait_state_count)
671 pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
672 ioc->name, __func__);
674 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
677 "%s: failed obtaining a memory for mpi_request\n",
678 ioc->name, __func__);
683 /* Check for overflow and wraparound */
684 if (karg.data_sge_offset * 4 > ioc->request_sz ||
685 karg.data_sge_offset > (UINT_MAX / 4)) {
690 /* copy in request message frame from user */
691 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
692 pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
698 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
699 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
701 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
702 ioc->name, __func__);
708 smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
710 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
711 ioc->name, __func__);
718 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
719 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
720 request = mpt3sas_base_get_msg_frame(ioc, smid);
721 memcpy(request, mpi_request, karg.data_sge_offset*4);
722 ioc->ctl_cmds.smid = smid;
723 data_out_sz = karg.data_out_size;
724 data_in_sz = karg.data_in_size;
726 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
727 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
728 if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
729 le16_to_cpu(mpi_request->FunctionDependent1) >
730 ioc->facts.MaxDevHandle) {
732 mpt3sas_base_free_smid(ioc, smid);
737 /* obtain dma-able memory for data transfer */
738 if (data_out_sz) /* WRITE */ {
739 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
742 pr_err("failure at %s:%d/%s()!\n", __FILE__,
745 mpt3sas_base_free_smid(ioc, smid);
748 if (copy_from_user(data_out, karg.data_out_buf_ptr,
750 pr_err("failure at %s:%d/%s()!\n", __FILE__,
753 mpt3sas_base_free_smid(ioc, smid);
758 if (data_in_sz) /* READ */ {
759 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
762 pr_err("failure at %s:%d/%s()!\n", __FILE__,
765 mpt3sas_base_free_smid(ioc, smid);
770 psge = (void *)request + (karg.data_sge_offset*4);
772 /* send command to firmware */
773 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
775 init_completion(&ioc->ctl_cmds.done);
776 switch (mpi_request->Function) {
777 case MPI2_FUNCTION_SCSI_IO_REQUEST:
778 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
780 Mpi2SCSIIORequest_t *scsiio_request =
781 (Mpi2SCSIIORequest_t *)request;
782 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
783 scsiio_request->SenseBufferLowAddress =
784 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
785 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
786 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
787 data_in_dma, data_in_sz);
789 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
790 mpt3sas_base_put_smid_scsi_io(ioc, smid,
791 le16_to_cpu(mpi_request->FunctionDependent1));
793 mpt3sas_base_put_smid_default(ioc, smid);
796 case MPI2_FUNCTION_SCSI_TASK_MGMT:
798 Mpi2SCSITaskManagementRequest_t *tm_request =
799 (Mpi2SCSITaskManagementRequest_t *)request;
801 dtmprintk(ioc, pr_info(MPT3SAS_FMT
802 "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
804 le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
806 if (tm_request->TaskType ==
807 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
808 tm_request->TaskType ==
809 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
810 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
811 mpt3sas_base_free_smid(ioc, smid);
816 mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
817 tm_request->DevHandle));
818 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
819 data_in_dma, data_in_sz);
820 mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
823 case MPI2_FUNCTION_SMP_PASSTHROUGH:
825 Mpi2SmpPassthroughRequest_t *smp_request =
826 (Mpi2SmpPassthroughRequest_t *)mpi_request;
829 /* ioc determines which port to use */
830 smp_request->PhysicalPort = 0xFF;
831 if (smp_request->PassthroughFlags &
832 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
833 data = (u8 *)&smp_request->SGL;
835 if (unlikely(data_out == NULL)) {
836 pr_err("failure at %s:%d/%s()!\n",
837 __FILE__, __LINE__, __func__);
838 mpt3sas_base_free_smid(ioc, smid);
845 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
846 ioc->ioc_link_reset_in_progress = 1;
847 ioc->ignore_loginfos = 1;
849 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
851 mpt3sas_base_put_smid_default(ioc, smid);
854 case MPI2_FUNCTION_SATA_PASSTHROUGH:
855 case MPI2_FUNCTION_FW_DOWNLOAD:
856 case MPI2_FUNCTION_FW_UPLOAD:
858 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
860 mpt3sas_base_put_smid_default(ioc, smid);
863 case MPI2_FUNCTION_TOOLBOX:
865 Mpi2ToolboxCleanRequest_t *toolbox_request =
866 (Mpi2ToolboxCleanRequest_t *)mpi_request;
868 if (toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL) {
869 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
870 data_in_dma, data_in_sz);
872 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
873 data_in_dma, data_in_sz);
875 mpt3sas_base_put_smid_default(ioc, smid);
878 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
880 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
881 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
883 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
884 || sasiounit_request->Operation ==
885 MPI2_SAS_OP_PHY_LINK_RESET) {
886 ioc->ioc_link_reset_in_progress = 1;
887 ioc->ignore_loginfos = 1;
889 /* drop to default case for posting the request */
892 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
893 data_in_dma, data_in_sz);
894 mpt3sas_base_put_smid_default(ioc, smid);
898 if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
899 timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
901 timeout = karg.timeout;
902 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
904 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
905 Mpi2SCSITaskManagementRequest_t *tm_request =
906 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
907 mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
908 tm_request->DevHandle));
909 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
910 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
911 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
912 ioc->ioc_link_reset_in_progress) {
913 ioc->ioc_link_reset_in_progress = 0;
914 ioc->ignore_loginfos = 0;
916 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
917 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
919 _debug_dump_mf(mpi_request, karg.data_sge_offset);
920 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
922 goto issue_host_reset;
925 mpi_reply = ioc->ctl_cmds.reply;
926 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
928 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
929 (ioc->logging_level & MPT_DEBUG_TM)) {
930 Mpi2SCSITaskManagementReply_t *tm_reply =
931 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
933 pr_info(MPT3SAS_FMT "TASK_MGMT: " \
934 "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
935 "TerminationCount(0x%08x)\n", ioc->name,
936 le16_to_cpu(tm_reply->IOCStatus),
937 le32_to_cpu(tm_reply->IOCLogInfo),
938 le32_to_cpu(tm_reply->TerminationCount));
941 /* copy out xdata to user */
943 if (copy_to_user(karg.data_in_buf_ptr, data_in,
945 pr_err("failure at %s:%d/%s()!\n", __FILE__,
952 /* copy out reply message frame to user */
953 if (karg.max_reply_bytes) {
954 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
955 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
957 pr_err("failure at %s:%d/%s()!\n", __FILE__,
964 /* copy out sense to user */
965 if (karg.max_sense_bytes && (mpi_request->Function ==
966 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
967 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
968 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
969 if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
971 pr_err("failure at %s:%d/%s()!\n", __FILE__,
981 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
982 mpi_request->Function ==
983 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
984 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
985 pr_info(MPT3SAS_FMT "issue target reset: handle = (0x%04x)\n",
987 le16_to_cpu(mpi_request->FunctionDependent1));
988 mpt3sas_halt_firmware(ioc);
989 mpt3sas_scsih_issue_tm(ioc,
990 le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
991 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30,
994 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1000 /* free memory associated with sg buffers */
1002 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1006 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1010 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1015 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1016 * @ioc: per adapter object
1017 * @arg - user space buffer containing ioctl content
1020 _ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1022 struct mpt3_ioctl_iocinfo karg;
1024 if (copy_from_user(&karg, arg, sizeof(karg))) {
1025 pr_err("failure at %s:%d/%s()!\n",
1026 __FILE__, __LINE__, __func__);
1030 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1033 memset(&karg, 0 , sizeof(karg));
1035 karg.port_number = ioc->pfacts[0].PortNumber;
1036 karg.hw_rev = ioc->pdev->revision;
1037 karg.pci_id = ioc->pdev->device;
1038 karg.subsystem_device = ioc->pdev->subsystem_device;
1039 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1040 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1041 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1042 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1043 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1044 karg.firmware_version = ioc->facts.FWVersion.Word;
1045 strcpy(karg.driver_version, ioc->driver_name);
1046 strcat(karg.driver_version, "-");
1047 switch (ioc->hba_mpi_version_belonged) {
1049 if (ioc->is_warpdrive)
1050 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1052 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1053 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1056 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1057 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1060 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1062 if (copy_to_user(arg, &karg, sizeof(karg))) {
1063 pr_err("failure at %s:%d/%s()!\n",
1064 __FILE__, __LINE__, __func__);
1071 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1072 * @ioc: per adapter object
1073 * @arg - user space buffer containing ioctl content
1076 _ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1078 struct mpt3_ioctl_eventquery karg;
1080 if (copy_from_user(&karg, arg, sizeof(karg))) {
1081 pr_err("failure at %s:%d/%s()!\n",
1082 __FILE__, __LINE__, __func__);
1086 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1089 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1090 memcpy(karg.event_types, ioc->event_type,
1091 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1093 if (copy_to_user(arg, &karg, sizeof(karg))) {
1094 pr_err("failure at %s:%d/%s()!\n",
1095 __FILE__, __LINE__, __func__);
1102 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1103 * @ioc: per adapter object
1104 * @arg - user space buffer containing ioctl content
1107 _ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1109 struct mpt3_ioctl_eventenable karg;
1111 if (copy_from_user(&karg, arg, sizeof(karg))) {
1112 pr_err("failure at %s:%d/%s()!\n",
1113 __FILE__, __LINE__, __func__);
1117 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1120 memcpy(ioc->event_type, karg.event_types,
1121 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1122 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1126 /* initialize event_log */
1127 ioc->event_context = 0;
1128 ioc->aen_event_read_flag = 0;
1129 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1130 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1131 if (!ioc->event_log) {
1132 pr_err("failure at %s:%d/%s()!\n",
1133 __FILE__, __LINE__, __func__);
1140 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1141 * @ioc: per adapter object
1142 * @arg - user space buffer containing ioctl content
1145 _ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1147 struct mpt3_ioctl_eventreport karg;
1148 u32 number_bytes, max_events, max;
1149 struct mpt3_ioctl_eventreport __user *uarg = arg;
1151 if (copy_from_user(&karg, arg, sizeof(karg))) {
1152 pr_err("failure at %s:%d/%s()!\n",
1153 __FILE__, __LINE__, __func__);
1157 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1160 number_bytes = karg.hdr.max_data_size -
1161 sizeof(struct mpt3_ioctl_header);
1162 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1163 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1165 /* If fewer than 1 event is requested, there must have
1166 * been some type of error.
1168 if (!max || !ioc->event_log)
1171 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1172 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1173 pr_err("failure at %s:%d/%s()!\n",
1174 __FILE__, __LINE__, __func__);
1178 /* reset flag so SIGIO can restart */
1179 ioc->aen_event_read_flag = 0;
1184 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1185 * @ioc: per adapter object
1186 * @arg - user space buffer containing ioctl content
1189 _ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1191 struct mpt3_ioctl_diag_reset karg;
1194 if (copy_from_user(&karg, arg, sizeof(karg))) {
1195 pr_err("failure at %s:%d/%s()!\n",
1196 __FILE__, __LINE__, __func__);
1200 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1201 ioc->is_driver_loading)
1204 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1207 retval = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1209 pr_info(MPT3SAS_FMT "host reset: %s\n",
1210 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1215 * _ctl_btdh_search_sas_device - searching for sas device
1216 * @ioc: per adapter object
1217 * @btdh: btdh ioctl payload
1220 _ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1221 struct mpt3_ioctl_btdh_mapping *btdh)
1223 struct _sas_device *sas_device;
1224 unsigned long flags;
1227 if (list_empty(&ioc->sas_device_list))
1230 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1231 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1232 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1233 btdh->handle == sas_device->handle) {
1234 btdh->bus = sas_device->channel;
1235 btdh->id = sas_device->id;
1238 } else if (btdh->bus == sas_device->channel && btdh->id ==
1239 sas_device->id && btdh->handle == 0xFFFF) {
1240 btdh->handle = sas_device->handle;
1246 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1251 * _ctl_btdh_search_raid_device - searching for raid device
1252 * @ioc: per adapter object
1253 * @btdh: btdh ioctl payload
1256 _ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1257 struct mpt3_ioctl_btdh_mapping *btdh)
1259 struct _raid_device *raid_device;
1260 unsigned long flags;
1263 if (list_empty(&ioc->raid_device_list))
1266 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1267 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1268 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1269 btdh->handle == raid_device->handle) {
1270 btdh->bus = raid_device->channel;
1271 btdh->id = raid_device->id;
1274 } else if (btdh->bus == raid_device->channel && btdh->id ==
1275 raid_device->id && btdh->handle == 0xFFFF) {
1276 btdh->handle = raid_device->handle;
1282 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1287 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1288 * @ioc: per adapter object
1289 * @arg - user space buffer containing ioctl content
1292 _ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1294 struct mpt3_ioctl_btdh_mapping karg;
1297 if (copy_from_user(&karg, arg, sizeof(karg))) {
1298 pr_err("failure at %s:%d/%s()!\n",
1299 __FILE__, __LINE__, __func__);
1303 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1306 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1308 _ctl_btdh_search_raid_device(ioc, &karg);
1310 if (copy_to_user(arg, &karg, sizeof(karg))) {
1311 pr_err("failure at %s:%d/%s()!\n",
1312 __FILE__, __LINE__, __func__);
1319 * _ctl_diag_capability - return diag buffer capability
1320 * @ioc: per adapter object
1321 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1323 * returns 1 when diag buffer support is enabled in firmware
1326 _ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1330 switch (buffer_type) {
1331 case MPI2_DIAG_BUF_TYPE_TRACE:
1332 if (ioc->facts.IOCCapabilities &
1333 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1336 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1337 if (ioc->facts.IOCCapabilities &
1338 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1341 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1342 if (ioc->facts.IOCCapabilities &
1343 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1352 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1353 * @ioc: per adapter object
1354 * @diag_register: the diag_register struct passed in from user space
1358 _ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1359 struct mpt3_diag_register *diag_register)
1362 void *request_data = NULL;
1363 dma_addr_t request_data_dma;
1364 u32 request_data_sz = 0;
1365 Mpi2DiagBufferPostRequest_t *mpi_request;
1366 Mpi2DiagBufferPostReply_t *mpi_reply;
1368 unsigned long timeleft;
1374 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1377 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1378 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1380 "%s: failed due to ioc not operational\n",
1381 ioc->name, __func__);
1386 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1387 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1388 ioc->name, __func__);
1393 buffer_type = diag_register->buffer_type;
1394 if (!_ctl_diag_capability(ioc, buffer_type)) {
1396 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1397 ioc->name, __func__, buffer_type);
1401 if (ioc->diag_buffer_status[buffer_type] &
1402 MPT3_DIAG_BUFFER_IS_REGISTERED) {
1404 "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1405 ioc->name, __func__,
1410 if (diag_register->requested_buffer_size % 4) {
1412 "%s: the requested_buffer_size is not 4 byte aligned\n",
1413 ioc->name, __func__);
1417 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1419 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1420 ioc->name, __func__);
1426 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1427 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1428 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1429 ioc->ctl_cmds.smid = smid;
1431 request_data = ioc->diag_buffer[buffer_type];
1432 request_data_sz = diag_register->requested_buffer_size;
1433 ioc->unique_id[buffer_type] = diag_register->unique_id;
1434 ioc->diag_buffer_status[buffer_type] = 0;
1435 memcpy(ioc->product_specific[buffer_type],
1436 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1437 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1440 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1441 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1442 pci_free_consistent(ioc->pdev,
1443 ioc->diag_buffer_sz[buffer_type],
1444 request_data, request_data_dma);
1445 request_data = NULL;
1449 if (request_data == NULL) {
1450 ioc->diag_buffer_sz[buffer_type] = 0;
1451 ioc->diag_buffer_dma[buffer_type] = 0;
1452 request_data = pci_alloc_consistent(
1453 ioc->pdev, request_data_sz, &request_data_dma);
1454 if (request_data == NULL) {
1455 pr_err(MPT3SAS_FMT "%s: failed allocating memory" \
1456 " for diag buffers, requested size(%d)\n",
1457 ioc->name, __func__, request_data_sz);
1458 mpt3sas_base_free_smid(ioc, smid);
1462 ioc->diag_buffer[buffer_type] = request_data;
1463 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1464 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1467 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1468 mpi_request->BufferType = diag_register->buffer_type;
1469 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1470 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1471 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1472 mpi_request->VF_ID = 0; /* TODO */
1473 mpi_request->VP_ID = 0;
1475 dctlprintk(ioc, pr_info(MPT3SAS_FMT
1476 "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1477 ioc->name, __func__, request_data,
1478 (unsigned long long)request_data_dma,
1479 le32_to_cpu(mpi_request->BufferLength)));
1481 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1482 mpi_request->ProductSpecific[i] =
1483 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1485 init_completion(&ioc->ctl_cmds.done);
1486 mpt3sas_base_put_smid_default(ioc, smid);
1487 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1488 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1490 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1491 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
1493 _debug_dump_mf(mpi_request,
1494 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1495 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
1497 goto issue_host_reset;
1500 /* process the completed Reply Message Frame */
1501 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1502 pr_err(MPT3SAS_FMT "%s: no reply message\n",
1503 ioc->name, __func__);
1508 mpi_reply = ioc->ctl_cmds.reply;
1509 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1511 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1512 ioc->diag_buffer_status[buffer_type] |=
1513 MPT3_DIAG_BUFFER_IS_REGISTERED;
1514 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
1515 ioc->name, __func__));
1518 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1519 ioc->name, __func__,
1520 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1526 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1531 if (rc && request_data)
1532 pci_free_consistent(ioc->pdev, request_data_sz,
1533 request_data, request_data_dma);
1535 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1540 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1541 * @ioc: per adapter object
1542 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1544 * This is called when command line option diag_buffer_enable is enabled
1545 * at driver load time.
1548 mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1550 struct mpt3_diag_register diag_register;
1552 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1554 if (bits_to_register & 1) {
1555 pr_info(MPT3SAS_FMT "registering trace buffer support\n",
1557 ioc->diag_trigger_master.MasterData =
1558 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1559 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1560 /* register for 2MB buffers */
1561 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1562 diag_register.unique_id = 0x7075900;
1563 _ctl_diag_register_2(ioc, &diag_register);
1566 if (bits_to_register & 2) {
1567 pr_info(MPT3SAS_FMT "registering snapshot buffer support\n",
1569 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1570 /* register for 2MB buffers */
1571 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1572 diag_register.unique_id = 0x7075901;
1573 _ctl_diag_register_2(ioc, &diag_register);
1576 if (bits_to_register & 4) {
1577 pr_info(MPT3SAS_FMT "registering extended buffer support\n",
1579 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1580 /* register for 2MB buffers */
1581 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1582 diag_register.unique_id = 0x7075901;
1583 _ctl_diag_register_2(ioc, &diag_register);
1588 * _ctl_diag_register - application register with driver
1589 * @ioc: per adapter object
1590 * @arg - user space buffer containing ioctl content
1592 * This will allow the driver to setup any required buffers that will be
1593 * needed by firmware to communicate with the driver.
1596 _ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1598 struct mpt3_diag_register karg;
1601 if (copy_from_user(&karg, arg, sizeof(karg))) {
1602 pr_err("failure at %s:%d/%s()!\n",
1603 __FILE__, __LINE__, __func__);
1607 rc = _ctl_diag_register_2(ioc, &karg);
1612 * _ctl_diag_unregister - application unregister with driver
1613 * @ioc: per adapter object
1614 * @arg - user space buffer containing ioctl content
1616 * This will allow the driver to cleanup any memory allocated for diag
1617 * messages and to free up any resources.
1620 _ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1622 struct mpt3_diag_unregister karg;
1624 dma_addr_t request_data_dma;
1625 u32 request_data_sz;
1628 if (copy_from_user(&karg, arg, sizeof(karg))) {
1629 pr_err("failure at %s:%d/%s()!\n",
1630 __FILE__, __LINE__, __func__);
1634 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1637 buffer_type = karg.unique_id & 0x000000ff;
1638 if (!_ctl_diag_capability(ioc, buffer_type)) {
1640 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1641 ioc->name, __func__, buffer_type);
1645 if ((ioc->diag_buffer_status[buffer_type] &
1646 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1648 "%s: buffer_type(0x%02x) is not registered\n",
1649 ioc->name, __func__, buffer_type);
1652 if ((ioc->diag_buffer_status[buffer_type] &
1653 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
1655 "%s: buffer_type(0x%02x) has not been released\n",
1656 ioc->name, __func__, buffer_type);
1660 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1662 "%s: unique_id(0x%08x) is not registered\n",
1663 ioc->name, __func__, karg.unique_id);
1667 request_data = ioc->diag_buffer[buffer_type];
1668 if (!request_data) {
1670 "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1671 ioc->name, __func__, buffer_type);
1675 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1676 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1677 pci_free_consistent(ioc->pdev, request_data_sz,
1678 request_data, request_data_dma);
1679 ioc->diag_buffer[buffer_type] = NULL;
1680 ioc->diag_buffer_status[buffer_type] = 0;
1685 * _ctl_diag_query - query relevant info associated with diag buffers
1686 * @ioc: per adapter object
1687 * @arg - user space buffer containing ioctl content
1689 * The application will send only buffer_type and unique_id. Driver will
1690 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1691 * 0x00, the driver will return info specified by Buffer Type.
1694 _ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1696 struct mpt3_diag_query karg;
1701 if (copy_from_user(&karg, arg, sizeof(karg))) {
1702 pr_err("failure at %s:%d/%s()!\n",
1703 __FILE__, __LINE__, __func__);
1707 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1710 karg.application_flags = 0;
1711 buffer_type = karg.buffer_type;
1713 if (!_ctl_diag_capability(ioc, buffer_type)) {
1715 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1716 ioc->name, __func__, buffer_type);
1720 if ((ioc->diag_buffer_status[buffer_type] &
1721 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1723 "%s: buffer_type(0x%02x) is not registered\n",
1724 ioc->name, __func__, buffer_type);
1728 if (karg.unique_id & 0xffffff00) {
1729 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1731 "%s: unique_id(0x%08x) is not registered\n",
1732 ioc->name, __func__, karg.unique_id);
1737 request_data = ioc->diag_buffer[buffer_type];
1738 if (!request_data) {
1740 "%s: doesn't have buffer for buffer_type(0x%02x)\n",
1741 ioc->name, __func__, buffer_type);
1745 if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
1746 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1747 MPT3_APP_FLAGS_BUFFER_VALID);
1749 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1750 MPT3_APP_FLAGS_BUFFER_VALID |
1751 MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
1753 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1754 karg.product_specific[i] =
1755 ioc->product_specific[buffer_type][i];
1757 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1758 karg.driver_added_buffer_size = 0;
1759 karg.unique_id = ioc->unique_id[buffer_type];
1760 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1762 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
1764 "%s: unable to write mpt3_diag_query data @ %p\n",
1765 ioc->name, __func__, arg);
1772 * mpt3sas_send_diag_release - Diag Release Message
1773 * @ioc: per adapter object
1774 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1775 * @issue_reset - specifies whether host reset is required.
1779 mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
1782 Mpi2DiagReleaseRequest_t *mpi_request;
1783 Mpi2DiagReleaseReply_t *mpi_reply;
1788 unsigned long timeleft;
1790 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1796 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1797 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1798 if (ioc->diag_buffer_status[buffer_type] &
1799 MPT3_DIAG_BUFFER_IS_REGISTERED)
1800 ioc->diag_buffer_status[buffer_type] |=
1801 MPT3_DIAG_BUFFER_IS_RELEASED;
1802 dctlprintk(ioc, pr_info(MPT3SAS_FMT
1803 "%s: skipping due to FAULT state\n", ioc->name,
1809 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1810 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1811 ioc->name, __func__);
1816 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1818 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1819 ioc->name, __func__);
1824 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1825 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1826 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1827 ioc->ctl_cmds.smid = smid;
1829 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1830 mpi_request->BufferType = buffer_type;
1831 mpi_request->VF_ID = 0; /* TODO */
1832 mpi_request->VP_ID = 0;
1834 init_completion(&ioc->ctl_cmds.done);
1835 mpt3sas_base_put_smid_default(ioc, smid);
1836 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1837 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1839 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1840 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
1842 _debug_dump_mf(mpi_request,
1843 sizeof(Mpi2DiagReleaseRequest_t)/4);
1844 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
1850 /* process the completed Reply Message Frame */
1851 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1852 pr_err(MPT3SAS_FMT "%s: no reply message\n",
1853 ioc->name, __func__);
1858 mpi_reply = ioc->ctl_cmds.reply;
1859 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1861 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1862 ioc->diag_buffer_status[buffer_type] |=
1863 MPT3_DIAG_BUFFER_IS_RELEASED;
1864 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
1865 ioc->name, __func__));
1868 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1869 ioc->name, __func__,
1870 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1875 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1880 * _ctl_diag_release - request to send Diag Release Message to firmware
1881 * @arg - user space buffer containing ioctl content
1883 * This allows ownership of the specified buffer to returned to the driver,
1884 * allowing an application to read the buffer without fear that firmware is
1885 * overwritting information in the buffer.
1888 _ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1890 struct mpt3_diag_release karg;
1896 if (copy_from_user(&karg, arg, sizeof(karg))) {
1897 pr_err("failure at %s:%d/%s()!\n",
1898 __FILE__, __LINE__, __func__);
1902 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1905 buffer_type = karg.unique_id & 0x000000ff;
1906 if (!_ctl_diag_capability(ioc, buffer_type)) {
1908 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1909 ioc->name, __func__, buffer_type);
1913 if ((ioc->diag_buffer_status[buffer_type] &
1914 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1916 "%s: buffer_type(0x%02x) is not registered\n",
1917 ioc->name, __func__, buffer_type);
1921 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1923 "%s: unique_id(0x%08x) is not registered\n",
1924 ioc->name, __func__, karg.unique_id);
1928 if (ioc->diag_buffer_status[buffer_type] &
1929 MPT3_DIAG_BUFFER_IS_RELEASED) {
1931 "%s: buffer_type(0x%02x) is already released\n",
1932 ioc->name, __func__,
1937 request_data = ioc->diag_buffer[buffer_type];
1939 if (!request_data) {
1941 "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1942 ioc->name, __func__, buffer_type);
1946 /* buffers were released by due to host reset */
1947 if ((ioc->diag_buffer_status[buffer_type] &
1948 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
1949 ioc->diag_buffer_status[buffer_type] |=
1950 MPT3_DIAG_BUFFER_IS_RELEASED;
1951 ioc->diag_buffer_status[buffer_type] &=
1952 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
1954 "%s: buffer_type(0x%02x) was released due to host reset\n",
1955 ioc->name, __func__, buffer_type);
1959 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
1962 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1969 * _ctl_diag_read_buffer - request for copy of the diag buffer
1970 * @ioc: per adapter object
1971 * @arg - user space buffer containing ioctl content
1974 _ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1976 struct mpt3_diag_read_buffer karg;
1977 struct mpt3_diag_read_buffer __user *uarg = arg;
1978 void *request_data, *diag_data;
1979 Mpi2DiagBufferPostRequest_t *mpi_request;
1980 Mpi2DiagBufferPostReply_t *mpi_reply;
1983 unsigned long timeleft, request_size, copy_size;
1988 if (copy_from_user(&karg, arg, sizeof(karg))) {
1989 pr_err("failure at %s:%d/%s()!\n",
1990 __FILE__, __LINE__, __func__);
1994 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1997 buffer_type = karg.unique_id & 0x000000ff;
1998 if (!_ctl_diag_capability(ioc, buffer_type)) {
2000 "%s: doesn't have capability for buffer_type(0x%02x)\n",
2001 ioc->name, __func__, buffer_type);
2005 if (karg.unique_id != ioc->unique_id[buffer_type]) {
2007 "%s: unique_id(0x%08x) is not registered\n",
2008 ioc->name, __func__, karg.unique_id);
2012 request_data = ioc->diag_buffer[buffer_type];
2013 if (!request_data) {
2015 "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2016 ioc->name, __func__, buffer_type);
2020 request_size = ioc->diag_buffer_sz[buffer_type];
2022 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2023 pr_err(MPT3SAS_FMT "%s: either the starting_offset " \
2024 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
2029 if (karg.starting_offset > request_size)
2032 diag_data = (void *)(request_data + karg.starting_offset);
2033 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2034 "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2035 ioc->name, __func__,
2036 diag_data, karg.starting_offset, karg.bytes_to_read));
2038 /* Truncate data on requests that are too large */
2039 if ((diag_data + karg.bytes_to_read < diag_data) ||
2040 (diag_data + karg.bytes_to_read > request_data + request_size))
2041 copy_size = request_size - karg.starting_offset;
2043 copy_size = karg.bytes_to_read;
2045 if (copy_to_user((void __user *)uarg->diagnostic_data,
2046 diag_data, copy_size)) {
2048 "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2049 ioc->name, __func__, diag_data);
2053 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2056 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2057 "%s: Reregister buffer_type(0x%02x)\n",
2058 ioc->name, __func__, buffer_type));
2059 if ((ioc->diag_buffer_status[buffer_type] &
2060 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2061 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2062 "%s: buffer_type(0x%02x) is still registered\n",
2063 ioc->name, __func__, buffer_type));
2066 /* Get a free request frame and save the message context.
2069 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2070 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
2071 ioc->name, __func__);
2076 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2078 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2079 ioc->name, __func__);
2085 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2086 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2087 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2088 ioc->ctl_cmds.smid = smid;
2090 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2091 mpi_request->BufferType = buffer_type;
2092 mpi_request->BufferLength =
2093 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2094 mpi_request->BufferAddress =
2095 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2096 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2097 mpi_request->ProductSpecific[i] =
2098 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2099 mpi_request->VF_ID = 0; /* TODO */
2100 mpi_request->VP_ID = 0;
2102 init_completion(&ioc->ctl_cmds.done);
2103 mpt3sas_base_put_smid_default(ioc, smid);
2104 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2105 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2107 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2108 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
2110 _debug_dump_mf(mpi_request,
2111 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2112 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
2114 goto issue_host_reset;
2117 /* process the completed Reply Message Frame */
2118 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2119 pr_err(MPT3SAS_FMT "%s: no reply message\n",
2120 ioc->name, __func__);
2125 mpi_reply = ioc->ctl_cmds.reply;
2126 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2128 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2129 ioc->diag_buffer_status[buffer_type] |=
2130 MPT3_DIAG_BUFFER_IS_REGISTERED;
2131 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
2132 ioc->name, __func__));
2135 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2136 ioc->name, __func__,
2137 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2143 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2148 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2154 #ifdef CONFIG_COMPAT
2156 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2157 * @ioc: per adapter object
2158 * @cmd - ioctl opcode
2159 * @arg - (struct mpt3_ioctl_command32)
2161 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2164 _ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2167 struct mpt3_ioctl_command32 karg32;
2168 struct mpt3_ioctl_command32 __user *uarg;
2169 struct mpt3_ioctl_command karg;
2171 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2174 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2176 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2177 pr_err("failure at %s:%d/%s()!\n",
2178 __FILE__, __LINE__, __func__);
2182 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2183 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2184 karg.hdr.port_number = karg32.hdr.port_number;
2185 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2186 karg.timeout = karg32.timeout;
2187 karg.max_reply_bytes = karg32.max_reply_bytes;
2188 karg.data_in_size = karg32.data_in_size;
2189 karg.data_out_size = karg32.data_out_size;
2190 karg.max_sense_bytes = karg32.max_sense_bytes;
2191 karg.data_sge_offset = karg32.data_sge_offset;
2192 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2193 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2194 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2195 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2196 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2201 * _ctl_ioctl_main - main ioctl entry point
2202 * @file - (struct file)
2203 * @cmd - ioctl opcode
2204 * @arg - user space data buffer
2205 * @compat - handles 32 bit applications in 64bit os
2206 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
2207 * MPI25_VERSION for mpt3ctl ioctl device.
2210 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2211 u8 compat, u16 mpi_version)
2213 struct MPT3SAS_ADAPTER *ioc;
2214 struct mpt3_ioctl_header ioctl_header;
2215 enum block_state state;
2218 /* get IOCTL header */
2219 if (copy_from_user(&ioctl_header, (char __user *)arg,
2220 sizeof(struct mpt3_ioctl_header))) {
2221 pr_err("failure at %s:%d/%s()!\n",
2222 __FILE__, __LINE__, __func__);
2226 if (_ctl_verify_adapter(ioctl_header.ioc_number,
2227 &ioc, mpi_version) == -1 || !ioc)
2230 /* pci_access_mutex lock acquired by ioctl path */
2231 mutex_lock(&ioc->pci_access_mutex);
2233 if (ioc->shost_recovery || ioc->pci_error_recovery ||
2234 ioc->is_driver_loading || ioc->remove_host) {
2236 goto out_unlock_pciaccess;
2239 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2240 if (state == NON_BLOCKING) {
2241 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2243 goto out_unlock_pciaccess;
2245 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2247 goto out_unlock_pciaccess;
2253 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2254 ret = _ctl_getiocinfo(ioc, arg);
2256 #ifdef CONFIG_COMPAT
2261 struct mpt3_ioctl_command __user *uarg;
2262 struct mpt3_ioctl_command karg;
2264 #ifdef CONFIG_COMPAT
2266 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2270 if (copy_from_user(&karg, arg, sizeof(karg))) {
2271 pr_err("failure at %s:%d/%s()!\n",
2272 __FILE__, __LINE__, __func__);
2277 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2279 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2283 case MPT3EVENTQUERY:
2284 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2285 ret = _ctl_eventquery(ioc, arg);
2287 case MPT3EVENTENABLE:
2288 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2289 ret = _ctl_eventenable(ioc, arg);
2291 case MPT3EVENTREPORT:
2292 ret = _ctl_eventreport(ioc, arg);
2295 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2296 ret = _ctl_do_reset(ioc, arg);
2298 case MPT3BTDHMAPPING:
2299 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2300 ret = _ctl_btdh_mapping(ioc, arg);
2302 case MPT3DIAGREGISTER:
2303 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2304 ret = _ctl_diag_register(ioc, arg);
2306 case MPT3DIAGUNREGISTER:
2307 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2308 ret = _ctl_diag_unregister(ioc, arg);
2311 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2312 ret = _ctl_diag_query(ioc, arg);
2314 case MPT3DIAGRELEASE:
2315 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2316 ret = _ctl_diag_release(ioc, arg);
2318 case MPT3DIAGREADBUFFER:
2319 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2320 ret = _ctl_diag_read_buffer(ioc, arg);
2323 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2324 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2328 mutex_unlock(&ioc->ctl_cmds.mutex);
2329 out_unlock_pciaccess:
2330 mutex_unlock(&ioc->pci_access_mutex);
2335 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
2336 * @file - (struct file)
2337 * @cmd - ioctl opcode
2341 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2345 /* pass MPI25_VERSION value, to indicate that this ioctl cmd
2346 * came from mpt3ctl ioctl device.
2348 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI25_VERSION);
2353 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
2354 * @file - (struct file)
2355 * @cmd - ioctl opcode
2359 _ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2363 /* pass MPI2_VERSION value, to indicate that this ioctl cmd
2364 * came from mpt2ctl ioctl device.
2366 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2369 #ifdef CONFIG_COMPAT
2371 *_ ctl_ioctl_compat - main ioctl entry point (compat)
2376 * This routine handles 32 bit applications in 64bit os.
2379 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2383 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI25_VERSION);
2388 *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
2393 * This routine handles 32 bit applications in 64bit os.
2396 _ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2400 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
2405 /* scsi host attributes */
2407 * _ctl_version_fw_show - firmware version
2408 * @cdev - pointer to embedded class device
2409 * @buf - the buffer returned
2411 * A sysfs 'read-only' shost attribute.
2414 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2417 struct Scsi_Host *shost = class_to_shost(cdev);
2418 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2420 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2421 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2422 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2423 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2424 ioc->facts.FWVersion.Word & 0x000000FF);
2426 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2429 * _ctl_version_bios_show - bios version
2430 * @cdev - pointer to embedded class device
2431 * @buf - the buffer returned
2433 * A sysfs 'read-only' shost attribute.
2436 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2439 struct Scsi_Host *shost = class_to_shost(cdev);
2440 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2442 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2444 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2445 (version & 0xFF000000) >> 24,
2446 (version & 0x00FF0000) >> 16,
2447 (version & 0x0000FF00) >> 8,
2448 version & 0x000000FF);
2450 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2453 * _ctl_version_mpi_show - MPI (message passing interface) version
2454 * @cdev - pointer to embedded class device
2455 * @buf - the buffer returned
2457 * A sysfs 'read-only' shost attribute.
2460 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2463 struct Scsi_Host *shost = class_to_shost(cdev);
2464 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2466 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2467 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2469 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2472 * _ctl_version_product_show - product name
2473 * @cdev - pointer to embedded class device
2474 * @buf - the buffer returned
2476 * A sysfs 'read-only' shost attribute.
2479 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2482 struct Scsi_Host *shost = class_to_shost(cdev);
2483 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2485 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2487 static DEVICE_ATTR(version_product, S_IRUGO, _ctl_version_product_show, NULL);
2490 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2491 * @cdev - pointer to embedded class device
2492 * @buf - the buffer returned
2494 * A sysfs 'read-only' shost attribute.
2497 _ctl_version_nvdata_persistent_show(struct device *cdev,
2498 struct device_attribute *attr, char *buf)
2500 struct Scsi_Host *shost = class_to_shost(cdev);
2501 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2503 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2504 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2506 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2507 _ctl_version_nvdata_persistent_show, NULL);
2510 * _ctl_version_nvdata_default_show - nvdata default version
2511 * @cdev - pointer to embedded class device
2512 * @buf - the buffer returned
2514 * A sysfs 'read-only' shost attribute.
2517 _ctl_version_nvdata_default_show(struct device *cdev, struct device_attribute
2520 struct Scsi_Host *shost = class_to_shost(cdev);
2521 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2523 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2524 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2526 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2527 _ctl_version_nvdata_default_show, NULL);
2530 * _ctl_board_name_show - board name
2531 * @cdev - pointer to embedded class device
2532 * @buf - the buffer returned
2534 * A sysfs 'read-only' shost attribute.
2537 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2540 struct Scsi_Host *shost = class_to_shost(cdev);
2541 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2543 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2545 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2548 * _ctl_board_assembly_show - board assembly name
2549 * @cdev - pointer to embedded class device
2550 * @buf - the buffer returned
2552 * A sysfs 'read-only' shost attribute.
2555 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2558 struct Scsi_Host *shost = class_to_shost(cdev);
2559 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2561 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2563 static DEVICE_ATTR(board_assembly, S_IRUGO, _ctl_board_assembly_show, NULL);
2566 * _ctl_board_tracer_show - board tracer number
2567 * @cdev - pointer to embedded class device
2568 * @buf - the buffer returned
2570 * A sysfs 'read-only' shost attribute.
2573 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2576 struct Scsi_Host *shost = class_to_shost(cdev);
2577 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2579 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2581 static DEVICE_ATTR(board_tracer, S_IRUGO, _ctl_board_tracer_show, NULL);
2584 * _ctl_io_delay_show - io missing delay
2585 * @cdev - pointer to embedded class device
2586 * @buf - the buffer returned
2588 * This is for firmware implemention for deboucing device
2591 * A sysfs 'read-only' shost attribute.
2594 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2597 struct Scsi_Host *shost = class_to_shost(cdev);
2598 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2600 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2602 static DEVICE_ATTR(io_delay, S_IRUGO, _ctl_io_delay_show, NULL);
2605 * _ctl_device_delay_show - device missing delay
2606 * @cdev - pointer to embedded class device
2607 * @buf - the buffer returned
2609 * This is for firmware implemention for deboucing device
2612 * A sysfs 'read-only' shost attribute.
2615 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2618 struct Scsi_Host *shost = class_to_shost(cdev);
2619 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2621 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2623 static DEVICE_ATTR(device_delay, S_IRUGO, _ctl_device_delay_show, NULL);
2626 * _ctl_fw_queue_depth_show - global credits
2627 * @cdev - pointer to embedded class device
2628 * @buf - the buffer returned
2630 * This is firmware queue depth limit
2632 * A sysfs 'read-only' shost attribute.
2635 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2638 struct Scsi_Host *shost = class_to_shost(cdev);
2639 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2641 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2643 static DEVICE_ATTR(fw_queue_depth, S_IRUGO, _ctl_fw_queue_depth_show, NULL);
2646 * _ctl_sas_address_show - sas address
2647 * @cdev - pointer to embedded class device
2648 * @buf - the buffer returned
2650 * This is the controller sas address
2652 * A sysfs 'read-only' shost attribute.
2655 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2659 struct Scsi_Host *shost = class_to_shost(cdev);
2660 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2662 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2663 (unsigned long long)ioc->sas_hba.sas_address);
2665 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2666 _ctl_host_sas_address_show, NULL);
2669 * _ctl_logging_level_show - logging level
2670 * @cdev - pointer to embedded class device
2671 * @buf - the buffer returned
2673 * A sysfs 'read/write' shost attribute.
2676 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2679 struct Scsi_Host *shost = class_to_shost(cdev);
2680 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2682 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2685 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2686 const char *buf, size_t count)
2688 struct Scsi_Host *shost = class_to_shost(cdev);
2689 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2692 if (sscanf(buf, "%x", &val) != 1)
2695 ioc->logging_level = val;
2696 pr_info(MPT3SAS_FMT "logging_level=%08xh\n", ioc->name,
2697 ioc->logging_level);
2700 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR, _ctl_logging_level_show,
2701 _ctl_logging_level_store);
2704 * _ctl_fwfault_debug_show - show/store fwfault_debug
2705 * @cdev - pointer to embedded class device
2706 * @buf - the buffer returned
2708 * mpt3sas_fwfault_debug is command line option
2709 * A sysfs 'read/write' shost attribute.
2712 _ctl_fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
2715 struct Scsi_Host *shost = class_to_shost(cdev);
2716 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2718 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2721 _ctl_fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
2722 const char *buf, size_t count)
2724 struct Scsi_Host *shost = class_to_shost(cdev);
2725 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2728 if (sscanf(buf, "%d", &val) != 1)
2731 ioc->fwfault_debug = val;
2732 pr_info(MPT3SAS_FMT "fwfault_debug=%d\n", ioc->name,
2733 ioc->fwfault_debug);
2736 static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2737 _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2740 * _ctl_ioc_reset_count_show - ioc reset count
2741 * @cdev - pointer to embedded class device
2742 * @buf - the buffer returned
2744 * This is firmware queue depth limit
2746 * A sysfs 'read-only' shost attribute.
2749 _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2752 struct Scsi_Host *shost = class_to_shost(cdev);
2753 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2755 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
2757 static DEVICE_ATTR(ioc_reset_count, S_IRUGO, _ctl_ioc_reset_count_show, NULL);
2760 * _ctl_ioc_reply_queue_count_show - number of reply queues
2761 * @cdev - pointer to embedded class device
2762 * @buf - the buffer returned
2764 * This is number of reply queues
2766 * A sysfs 'read-only' shost attribute.
2769 _ctl_ioc_reply_queue_count_show(struct device *cdev,
2770 struct device_attribute *attr, char *buf)
2772 u8 reply_queue_count;
2773 struct Scsi_Host *shost = class_to_shost(cdev);
2774 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2776 if ((ioc->facts.IOCCapabilities &
2777 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2778 reply_queue_count = ioc->reply_queue_count;
2780 reply_queue_count = 1;
2782 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2784 static DEVICE_ATTR(reply_queue_count, S_IRUGO, _ctl_ioc_reply_queue_count_show,
2788 * _ctl_BRM_status_show - Backup Rail Monitor Status
2789 * @cdev - pointer to embedded class device
2790 * @buf - the buffer returned
2792 * This is number of reply queues
2794 * A sysfs 'read-only' shost attribute.
2797 _ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
2800 struct Scsi_Host *shost = class_to_shost(cdev);
2801 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2802 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2803 Mpi2ConfigReply_t mpi_reply;
2804 u16 backup_rail_monitor_status = 0;
2809 if (!ioc->is_warpdrive) {
2810 pr_err(MPT3SAS_FMT "%s: BRM attribute is only for"
2811 " warpdrive\n", ioc->name, __func__);
2814 /* pci_access_mutex lock acquired by sysfs show path */
2815 mutex_lock(&ioc->pci_access_mutex);
2816 if (ioc->pci_error_recovery || ioc->remove_host) {
2817 mutex_unlock(&ioc->pci_access_mutex);
2821 /* allocate upto GPIOVal 36 entries */
2822 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2823 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2825 pr_err(MPT3SAS_FMT "%s: failed allocating memory "
2826 "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
2830 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2833 "%s: failed reading iounit_pg3\n", ioc->name,
2838 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2839 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2840 pr_err(MPT3SAS_FMT "%s: iounit_pg3 failed with "
2841 "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
2845 if (io_unit_pg3->GPIOCount < 25) {
2846 pr_err(MPT3SAS_FMT "%s: iounit_pg3->GPIOCount less than "
2847 "25 entries, detected (%d) entries\n", ioc->name, __func__,
2848 io_unit_pg3->GPIOCount);
2852 /* BRM status is in bit zero of GPIOVal[24] */
2853 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2854 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2858 mutex_unlock(&ioc->pci_access_mutex);
2861 static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
2863 struct DIAG_BUFFER_START {
2874 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2875 * @cdev - pointer to embedded class device
2876 * @buf - the buffer returned
2878 * A sysfs 'read-only' shost attribute.
2881 _ctl_host_trace_buffer_size_show(struct device *cdev,
2882 struct device_attribute *attr, char *buf)
2884 struct Scsi_Host *shost = class_to_shost(cdev);
2885 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2887 struct DIAG_BUFFER_START *request_data;
2889 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2891 "%s: host_trace_buffer is not registered\n",
2892 ioc->name, __func__);
2896 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2897 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2899 "%s: host_trace_buffer is not registered\n",
2900 ioc->name, __func__);
2904 request_data = (struct DIAG_BUFFER_START *)
2905 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2906 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2907 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
2908 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
2909 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2910 size = le32_to_cpu(request_data->Size);
2912 ioc->ring_buffer_sz = size;
2913 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2915 static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2916 _ctl_host_trace_buffer_size_show, NULL);
2919 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2920 * @cdev - pointer to embedded class device
2921 * @buf - the buffer returned
2923 * A sysfs 'read/write' shost attribute.
2925 * You will only be able to read 4k bytes of ring buffer at a time.
2926 * In order to read beyond 4k bytes, you will have to write out the
2927 * offset to the same attribute, it will move the pointer.
2930 _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2933 struct Scsi_Host *shost = class_to_shost(cdev);
2934 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2938 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2940 "%s: host_trace_buffer is not registered\n",
2941 ioc->name, __func__);
2945 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2946 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2948 "%s: host_trace_buffer is not registered\n",
2949 ioc->name, __func__);
2953 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2956 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2957 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2958 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2959 memcpy(buf, request_data, size);
2964 _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2965 const char *buf, size_t count)
2967 struct Scsi_Host *shost = class_to_shost(cdev);
2968 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2971 if (sscanf(buf, "%d", &val) != 1)
2974 ioc->ring_buffer_offset = val;
2977 static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2978 _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2981 /*****************************************/
2984 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2985 * @cdev - pointer to embedded class device
2986 * @buf - the buffer returned
2988 * A sysfs 'read/write' shost attribute.
2990 * This is a mechnism to post/release host_trace_buffers
2993 _ctl_host_trace_buffer_enable_show(struct device *cdev,
2994 struct device_attribute *attr, char *buf)
2996 struct Scsi_Host *shost = class_to_shost(cdev);
2997 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2999 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3000 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3001 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3002 return snprintf(buf, PAGE_SIZE, "off\n");
3003 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3004 MPT3_DIAG_BUFFER_IS_RELEASED))
3005 return snprintf(buf, PAGE_SIZE, "release\n");
3007 return snprintf(buf, PAGE_SIZE, "post\n");
3011 _ctl_host_trace_buffer_enable_store(struct device *cdev,
3012 struct device_attribute *attr, const char *buf, size_t count)
3014 struct Scsi_Host *shost = class_to_shost(cdev);
3015 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3017 struct mpt3_diag_register diag_register;
3020 /* don't allow post/release occurr while recovery is active */
3021 if (ioc->shost_recovery || ioc->remove_host ||
3022 ioc->pci_error_recovery || ioc->is_driver_loading)
3025 if (sscanf(buf, "%9s", str) != 1)
3028 if (!strcmp(str, "post")) {
3029 /* exit out if host buffers are already posted */
3030 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3031 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3032 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3033 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3034 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3036 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3037 pr_info(MPT3SAS_FMT "posting host trace buffers\n",
3039 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3040 diag_register.requested_buffer_size = (1024 * 1024);
3041 diag_register.unique_id = 0x7075900;
3042 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3043 _ctl_diag_register_2(ioc, &diag_register);
3044 } else if (!strcmp(str, "release")) {
3045 /* exit out if host buffers are already released */
3046 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3048 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3049 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3051 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3052 MPT3_DIAG_BUFFER_IS_RELEASED))
3054 pr_info(MPT3SAS_FMT "releasing host trace buffer\n",
3056 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3063 static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
3064 _ctl_host_trace_buffer_enable_show,
3065 _ctl_host_trace_buffer_enable_store);
3067 /*********** diagnostic trigger suppport *********************************/
3070 * _ctl_diag_trigger_master_show - show the diag_trigger_master attribute
3071 * @cdev - pointer to embedded class device
3072 * @buf - the buffer returned
3074 * A sysfs 'read/write' shost attribute.
3077 _ctl_diag_trigger_master_show(struct device *cdev,
3078 struct device_attribute *attr, char *buf)
3081 struct Scsi_Host *shost = class_to_shost(cdev);
3082 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3083 unsigned long flags;
3086 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3087 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3088 memcpy(buf, &ioc->diag_trigger_master, rc);
3089 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3094 * _ctl_diag_trigger_master_store - store the diag_trigger_master attribute
3095 * @cdev - pointer to embedded class device
3096 * @buf - the buffer returned
3098 * A sysfs 'read/write' shost attribute.
3101 _ctl_diag_trigger_master_store(struct device *cdev,
3102 struct device_attribute *attr, const char *buf, size_t count)
3105 struct Scsi_Host *shost = class_to_shost(cdev);
3106 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3107 unsigned long flags;
3110 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3111 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3112 memset(&ioc->diag_trigger_master, 0,
3113 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3114 memcpy(&ioc->diag_trigger_master, buf, rc);
3115 ioc->diag_trigger_master.MasterData |=
3116 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3117 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3120 static DEVICE_ATTR(diag_trigger_master, S_IRUGO | S_IWUSR,
3121 _ctl_diag_trigger_master_show, _ctl_diag_trigger_master_store);
3125 * _ctl_diag_trigger_event_show - show the diag_trigger_event attribute
3126 * @cdev - pointer to embedded class device
3127 * @buf - the buffer returned
3129 * A sysfs 'read/write' shost attribute.
3132 _ctl_diag_trigger_event_show(struct device *cdev,
3133 struct device_attribute *attr, char *buf)
3135 struct Scsi_Host *shost = class_to_shost(cdev);
3136 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3137 unsigned long flags;
3140 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3141 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3142 memcpy(buf, &ioc->diag_trigger_event, rc);
3143 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3148 * _ctl_diag_trigger_event_store - store the diag_trigger_event attribute
3149 * @cdev - pointer to embedded class device
3150 * @buf - the buffer returned
3152 * A sysfs 'read/write' shost attribute.
3155 _ctl_diag_trigger_event_store(struct device *cdev,
3156 struct device_attribute *attr, const char *buf, size_t count)
3159 struct Scsi_Host *shost = class_to_shost(cdev);
3160 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3161 unsigned long flags;
3164 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3165 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3166 memset(&ioc->diag_trigger_event, 0,
3167 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3168 memcpy(&ioc->diag_trigger_event, buf, sz);
3169 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3170 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3171 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3174 static DEVICE_ATTR(diag_trigger_event, S_IRUGO | S_IWUSR,
3175 _ctl_diag_trigger_event_show, _ctl_diag_trigger_event_store);
3179 * _ctl_diag_trigger_scsi_show - show the diag_trigger_scsi attribute
3180 * @cdev - pointer to embedded class device
3181 * @buf - the buffer returned
3183 * A sysfs 'read/write' shost attribute.
3186 _ctl_diag_trigger_scsi_show(struct device *cdev,
3187 struct device_attribute *attr, char *buf)
3189 struct Scsi_Host *shost = class_to_shost(cdev);
3190 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3191 unsigned long flags;
3194 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3195 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3196 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3197 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3202 * _ctl_diag_trigger_scsi_store - store the diag_trigger_scsi attribute
3203 * @cdev - pointer to embedded class device
3204 * @buf - the buffer returned
3206 * A sysfs 'read/write' shost attribute.
3209 _ctl_diag_trigger_scsi_store(struct device *cdev,
3210 struct device_attribute *attr, const char *buf, size_t count)
3212 struct Scsi_Host *shost = class_to_shost(cdev);
3213 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3214 unsigned long flags;
3217 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3218 sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
3219 memset(&ioc->diag_trigger_scsi, 0,
3220 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3221 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3222 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3223 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3224 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3227 static DEVICE_ATTR(diag_trigger_scsi, S_IRUGO | S_IWUSR,
3228 _ctl_diag_trigger_scsi_show, _ctl_diag_trigger_scsi_store);
3232 * _ctl_diag_trigger_scsi_show - show the diag_trigger_mpi attribute
3233 * @cdev - pointer to embedded class device
3234 * @buf - the buffer returned
3236 * A sysfs 'read/write' shost attribute.
3239 _ctl_diag_trigger_mpi_show(struct device *cdev,
3240 struct device_attribute *attr, char *buf)
3242 struct Scsi_Host *shost = class_to_shost(cdev);
3243 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3244 unsigned long flags;
3247 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3248 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3249 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3250 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3255 * _ctl_diag_trigger_mpi_store - store the diag_trigger_mpi attribute
3256 * @cdev - pointer to embedded class device
3257 * @buf - the buffer returned
3259 * A sysfs 'read/write' shost attribute.
3262 _ctl_diag_trigger_mpi_store(struct device *cdev,
3263 struct device_attribute *attr, const char *buf, size_t count)
3265 struct Scsi_Host *shost = class_to_shost(cdev);
3266 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3267 unsigned long flags;
3270 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3271 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3272 memset(&ioc->diag_trigger_mpi, 0,
3273 sizeof(ioc->diag_trigger_mpi));
3274 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3275 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3276 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3277 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3281 static DEVICE_ATTR(diag_trigger_mpi, S_IRUGO | S_IWUSR,
3282 _ctl_diag_trigger_mpi_show, _ctl_diag_trigger_mpi_store);
3284 /*********** diagnostic trigger suppport *** END ****************************/
3288 /*****************************************/
3290 struct device_attribute *mpt3sas_host_attrs[] = {
3291 &dev_attr_version_fw,
3292 &dev_attr_version_bios,
3293 &dev_attr_version_mpi,
3294 &dev_attr_version_product,
3295 &dev_attr_version_nvdata_persistent,
3296 &dev_attr_version_nvdata_default,
3297 &dev_attr_board_name,
3298 &dev_attr_board_assembly,
3299 &dev_attr_board_tracer,
3301 &dev_attr_device_delay,
3302 &dev_attr_logging_level,
3303 &dev_attr_fwfault_debug,
3304 &dev_attr_fw_queue_depth,
3305 &dev_attr_host_sas_address,
3306 &dev_attr_ioc_reset_count,
3307 &dev_attr_host_trace_buffer_size,
3308 &dev_attr_host_trace_buffer,
3309 &dev_attr_host_trace_buffer_enable,
3310 &dev_attr_reply_queue_count,
3311 &dev_attr_diag_trigger_master,
3312 &dev_attr_diag_trigger_event,
3313 &dev_attr_diag_trigger_scsi,
3314 &dev_attr_diag_trigger_mpi,
3315 &dev_attr_BRM_status,
3319 /* device attributes */
3322 * _ctl_device_sas_address_show - sas address
3323 * @cdev - pointer to embedded class device
3324 * @buf - the buffer returned
3326 * This is the sas address for the target
3328 * A sysfs 'read-only' shost attribute.
3331 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
3334 struct scsi_device *sdev = to_scsi_device(dev);
3335 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3337 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3338 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3340 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
3343 * _ctl_device_handle_show - device handle
3344 * @cdev - pointer to embedded class device
3345 * @buf - the buffer returned
3347 * This is the firmware assigned device handle
3349 * A sysfs 'read-only' shost attribute.
3352 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
3355 struct scsi_device *sdev = to_scsi_device(dev);
3356 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3358 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3359 sas_device_priv_data->sas_target->handle);
3361 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
3363 struct device_attribute *mpt3sas_dev_attrs[] = {
3364 &dev_attr_sas_address,
3365 &dev_attr_sas_device_handle,
3369 /* file operations table for mpt3ctl device */
3370 static const struct file_operations ctl_fops = {
3371 .owner = THIS_MODULE,
3372 .unlocked_ioctl = _ctl_ioctl,
3374 .fasync = _ctl_fasync,
3375 #ifdef CONFIG_COMPAT
3376 .compat_ioctl = _ctl_ioctl_compat,
3380 /* file operations table for mpt2ctl device */
3381 static const struct file_operations ctl_gen2_fops = {
3382 .owner = THIS_MODULE,
3383 .unlocked_ioctl = _ctl_mpt2_ioctl,
3385 .fasync = _ctl_fasync,
3386 #ifdef CONFIG_COMPAT
3387 .compat_ioctl = _ctl_mpt2_ioctl_compat,
3391 static struct miscdevice ctl_dev = {
3392 .minor = MPT3SAS_MINOR,
3393 .name = MPT3SAS_DEV_NAME,
3397 static struct miscdevice gen2_ctl_dev = {
3398 .minor = MPT2SAS_MINOR,
3399 .name = MPT2SAS_DEV_NAME,
3400 .fops = &ctl_gen2_fops,
3404 * mpt3sas_ctl_init - main entry point for ctl.
3408 mpt3sas_ctl_init(ushort hbas_to_enumerate)
3412 /* Don't register mpt3ctl ioctl device if
3413 * hbas_to_enumarate is one.
3415 if (hbas_to_enumerate != 1)
3416 if (misc_register(&ctl_dev) < 0)
3417 pr_err("%s can't register misc device [minor=%d]\n",
3418 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3420 /* Don't register mpt3ctl ioctl device if
3421 * hbas_to_enumarate is two.
3423 if (hbas_to_enumerate != 2)
3424 if (misc_register(&gen2_ctl_dev) < 0)
3425 pr_err("%s can't register misc device [minor=%d]\n",
3426 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3428 init_waitqueue_head(&ctl_poll_wait);
3432 * mpt3sas_ctl_exit - exit point for ctl
3436 mpt3sas_ctl_exit(ushort hbas_to_enumerate)
3438 struct MPT3SAS_ADAPTER *ioc;
3441 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3443 /* free memory associated to diag buffers */
3444 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3445 if (!ioc->diag_buffer[i])
3447 if (!(ioc->diag_buffer_status[i] &
3448 MPT3_DIAG_BUFFER_IS_REGISTERED))
3450 if ((ioc->diag_buffer_status[i] &
3451 MPT3_DIAG_BUFFER_IS_RELEASED))
3453 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3454 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3455 ioc->diag_buffer[i] = NULL;
3456 ioc->diag_buffer_status[i] = 0;
3459 kfree(ioc->event_log);
3461 if (hbas_to_enumerate != 1)
3462 misc_deregister(&ctl_dev);
3463 if (hbas_to_enumerate != 2)
3464 misc_deregister(&gen2_ctl_dev);