lkdtm: Add Control Flow Integrity test
[linux/fpc-iii.git] / drivers / scsi / mpt3sas / mpt3sas_ctl.c
blobd4ecfbbe738c730676d0ca3beddd41aeb3c50bef
1 /*
2 * Management Module Support for MPT (Message Passing Technology) based
3 * controllers
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.
20 * NO WARRANTY
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,
43 * USA.
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>
57 #include <linux/io.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);
68 /**
69 * enum block_state - blocking state
70 * @NON_BLOCKING: non blocking
71 * @BLOCKING: blocking
73 * These states are for ioctls that need to wait for a response
74 * from firmware, so they probably require sleep.
76 enum block_state {
77 NON_BLOCKING,
78 BLOCKING,
81 /**
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
87 * Context: none.
89 * Function for displaying debug info helpful when debugging issues
90 * in this module.
92 static void
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;
97 char *desc = NULL;
99 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
100 return;
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;
114 break;
116 case MPI2_FUNCTION_SCSI_TASK_MGMT:
117 desc = "task_mgmt";
118 break;
119 case MPI2_FUNCTION_IOC_INIT:
120 desc = "ioc_init";
121 break;
122 case MPI2_FUNCTION_IOC_FACTS:
123 desc = "ioc_facts";
124 break;
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;
136 break;
138 case MPI2_FUNCTION_PORT_FACTS:
139 desc = "port_facts";
140 break;
141 case MPI2_FUNCTION_PORT_ENABLE:
142 desc = "port_enable";
143 break;
144 case MPI2_FUNCTION_EVENT_NOTIFICATION:
145 desc = "event_notification";
146 break;
147 case MPI2_FUNCTION_FW_DOWNLOAD:
148 desc = "fw_download";
149 break;
150 case MPI2_FUNCTION_FW_UPLOAD:
151 desc = "fw_upload";
152 break;
153 case MPI2_FUNCTION_RAID_ACTION:
154 desc = "raid_action";
155 break;
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;
166 break;
168 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
169 desc = "sas_iounit_cntl";
170 break;
171 case MPI2_FUNCTION_SATA_PASSTHROUGH:
172 desc = "sata_pass";
173 break;
174 case MPI2_FUNCTION_DIAG_BUFFER_POST:
175 desc = "diag_buffer_post";
176 break;
177 case MPI2_FUNCTION_DIAG_RELEASE:
178 desc = "diag_release";
179 break;
180 case MPI2_FUNCTION_SMP_PASSTHROUGH:
181 desc = "smp_passthrough";
182 break;
185 if (!desc)
186 return;
188 ioc_info(ioc, "%s: %s, smid(%d)\n", calling_function_name, desc, smid);
190 if (!mpi_reply)
191 return;
193 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
194 ioc_info(ioc, "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
195 le16_to_cpu(mpi_reply->IOCStatus),
196 le32_to_cpu(mpi_reply->IOCLogInfo));
198 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
199 mpi_request->Function ==
200 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
201 Mpi2SCSIIOReply_t *scsi_reply =
202 (Mpi2SCSIIOReply_t *)mpi_reply;
203 struct _sas_device *sas_device = NULL;
204 struct _pcie_device *pcie_device = NULL;
206 sas_device = mpt3sas_get_sdev_by_handle(ioc,
207 le16_to_cpu(scsi_reply->DevHandle));
208 if (sas_device) {
209 ioc_warn(ioc, "\tsas_address(0x%016llx), phy(%d)\n",
210 (u64)sas_device->sas_address,
211 sas_device->phy);
212 ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
213 (u64)sas_device->enclosure_logical_id,
214 sas_device->slot);
215 sas_device_put(sas_device);
217 if (!sas_device) {
218 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
219 le16_to_cpu(scsi_reply->DevHandle));
220 if (pcie_device) {
221 ioc_warn(ioc, "\tWWID(0x%016llx), port(%d)\n",
222 (unsigned long long)pcie_device->wwid,
223 pcie_device->port_num);
224 if (pcie_device->enclosure_handle != 0)
225 ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
226 (u64)pcie_device->enclosure_logical_id,
227 pcie_device->slot);
228 pcie_device_put(pcie_device);
231 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
232 ioc_info(ioc, "\tscsi_state(0x%02x), scsi_status(0x%02x)\n",
233 scsi_reply->SCSIState,
234 scsi_reply->SCSIStatus);
239 * mpt3sas_ctl_done - ctl module completion routine
240 * @ioc: per adapter object
241 * @smid: system request message index
242 * @msix_index: MSIX table index supplied by the OS
243 * @reply: reply message frame(lower 32bit addr)
244 * Context: none.
246 * The callback handler when using ioc->ctl_cb_idx.
248 * Return: 1 meaning mf should be freed from _base_interrupt
249 * 0 means the mf is freed from this function.
252 mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
253 u32 reply)
255 MPI2DefaultReply_t *mpi_reply;
256 Mpi2SCSIIOReply_t *scsiio_reply;
257 Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
258 const void *sense_data;
259 u32 sz;
261 if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
262 return 1;
263 if (ioc->ctl_cmds.smid != smid)
264 return 1;
265 ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
266 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
267 if (mpi_reply) {
268 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
269 ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
270 /* get sense data */
271 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
272 mpi_reply->Function ==
273 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
274 scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
275 if (scsiio_reply->SCSIState &
276 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
277 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
278 le32_to_cpu(scsiio_reply->SenseCount));
279 sense_data = mpt3sas_base_get_sense_buffer(ioc,
280 smid);
281 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
285 * Get Error Response data for NVMe device. The ctl_cmds.sense
286 * buffer is used to store the Error Response data.
288 if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
289 nvme_error_reply =
290 (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
291 sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
292 le16_to_cpu(nvme_error_reply->ErrorResponseCount));
293 sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
294 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
298 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
299 ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
300 complete(&ioc->ctl_cmds.done);
301 return 1;
305 * _ctl_check_event_type - determines when an event needs logging
306 * @ioc: per adapter object
307 * @event: firmware event
309 * The bitmask in ioc->event_type[] indicates which events should be
310 * be saved in the driver event_log. This bitmask is set by application.
312 * Return: 1 when event should be captured, or zero means no match.
314 static int
315 _ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
317 u16 i;
318 u32 desired_event;
320 if (event >= 128 || !event || !ioc->event_log)
321 return 0;
323 desired_event = (1 << (event % 32));
324 if (!desired_event)
325 desired_event = 1;
326 i = event / 32;
327 return desired_event & ioc->event_type[i];
331 * mpt3sas_ctl_add_to_event_log - add event
332 * @ioc: per adapter object
333 * @mpi_reply: reply message frame
335 void
336 mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
337 Mpi2EventNotificationReply_t *mpi_reply)
339 struct MPT3_IOCTL_EVENTS *event_log;
340 u16 event;
341 int i;
342 u32 sz, event_data_sz;
343 u8 send_aen = 0;
345 if (!ioc->event_log)
346 return;
348 event = le16_to_cpu(mpi_reply->Event);
350 if (_ctl_check_event_type(ioc, event)) {
352 /* insert entry into circular event_log */
353 i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
354 event_log = ioc->event_log;
355 event_log[i].event = event;
356 event_log[i].context = ioc->event_context++;
358 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
359 sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
360 memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
361 memcpy(event_log[i].data, mpi_reply->EventData, sz);
362 send_aen = 1;
365 /* This aen_event_read_flag flag is set until the
366 * application has read the event log.
367 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
369 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
370 (send_aen && !ioc->aen_event_read_flag)) {
371 ioc->aen_event_read_flag = 1;
372 wake_up_interruptible(&ctl_poll_wait);
373 if (async_queue)
374 kill_fasync(&async_queue, SIGIO, POLL_IN);
379 * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
380 * @ioc: per adapter object
381 * @msix_index: MSIX table index supplied by the OS
382 * @reply: reply message frame(lower 32bit addr)
383 * Context: interrupt.
385 * This function merely adds a new work task into ioc->firmware_event_thread.
386 * The tasks are worked from _firmware_event_work in user context.
388 * Return: 1 meaning mf should be freed from _base_interrupt
389 * 0 means the mf is freed from this function.
392 mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
393 u32 reply)
395 Mpi2EventNotificationReply_t *mpi_reply;
397 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
398 if (mpi_reply)
399 mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
400 return 1;
404 * _ctl_verify_adapter - validates ioc_number passed from application
405 * @ioc_number: ?
406 * @iocpp: The ioc pointer is returned in this.
407 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
408 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
410 * Return: (-1) means error, else ioc_number.
412 static int
413 _ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
414 int mpi_version)
416 struct MPT3SAS_ADAPTER *ioc;
417 int version = 0;
418 /* global ioc lock to protect controller on list operations */
419 spin_lock(&gioc_lock);
420 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
421 if (ioc->id != ioc_number)
422 continue;
423 /* Check whether this ioctl command is from right
424 * ioctl device or not, if not continue the search.
426 version = ioc->hba_mpi_version_belonged;
427 /* MPI25_VERSION and MPI26_VERSION uses same ioctl
428 * device.
430 if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
431 if ((version == MPI25_VERSION) ||
432 (version == MPI26_VERSION))
433 goto out;
434 else
435 continue;
436 } else {
437 if (version != mpi_version)
438 continue;
440 out:
441 spin_unlock(&gioc_lock);
442 *iocpp = ioc;
443 return ioc_number;
445 spin_unlock(&gioc_lock);
446 *iocpp = NULL;
447 return -1;
451 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
452 * @ioc: per adapter object
454 * The handler for doing any required cleanup or initialization.
456 void mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
458 int i;
459 u8 issue_reset;
461 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
462 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
463 if (!(ioc->diag_buffer_status[i] &
464 MPT3_DIAG_BUFFER_IS_REGISTERED))
465 continue;
466 if ((ioc->diag_buffer_status[i] &
467 MPT3_DIAG_BUFFER_IS_RELEASED))
468 continue;
469 mpt3sas_send_diag_release(ioc, i, &issue_reset);
474 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
475 * @ioc: per adapter object
477 * The handler for doing any required cleanup or initialization.
479 void mpt3sas_ctl_after_reset_handler(struct MPT3SAS_ADAPTER *ioc)
481 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_AFTER_RESET\n", __func__));
482 if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
483 ioc->ctl_cmds.status |= MPT3_CMD_RESET;
484 mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
485 complete(&ioc->ctl_cmds.done);
490 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
491 * @ioc: per adapter object
493 * The handler for doing any required cleanup or initialization.
495 void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
497 int i;
499 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
501 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
502 if (!(ioc->diag_buffer_status[i] &
503 MPT3_DIAG_BUFFER_IS_REGISTERED))
504 continue;
505 if ((ioc->diag_buffer_status[i] &
506 MPT3_DIAG_BUFFER_IS_RELEASED))
507 continue;
508 ioc->diag_buffer_status[i] |=
509 MPT3_DIAG_BUFFER_IS_DIAG_RESET;
514 * _ctl_fasync -
515 * @fd: ?
516 * @filep: ?
517 * @mode: ?
519 * Called when application request fasyn callback handler.
521 static int
522 _ctl_fasync(int fd, struct file *filep, int mode)
524 return fasync_helper(fd, filep, mode, &async_queue);
528 * _ctl_poll -
529 * @filep: ?
530 * @wait: ?
533 static __poll_t
534 _ctl_poll(struct file *filep, poll_table *wait)
536 struct MPT3SAS_ADAPTER *ioc;
538 poll_wait(filep, &ctl_poll_wait, wait);
540 /* global ioc lock to protect controller on list operations */
541 spin_lock(&gioc_lock);
542 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
543 if (ioc->aen_event_read_flag) {
544 spin_unlock(&gioc_lock);
545 return EPOLLIN | EPOLLRDNORM;
548 spin_unlock(&gioc_lock);
549 return 0;
553 * _ctl_set_task_mid - assign an active smid to tm request
554 * @ioc: per adapter object
555 * @karg: (struct mpt3_ioctl_command)
556 * @tm_request: pointer to mf from user space
558 * Return: 0 when an smid if found, else fail.
559 * during failure, the reply frame is filled.
561 static int
562 _ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
563 Mpi2SCSITaskManagementRequest_t *tm_request)
565 u8 found = 0;
566 u16 smid;
567 u16 handle;
568 struct scsi_cmnd *scmd;
569 struct MPT3SAS_DEVICE *priv_data;
570 Mpi2SCSITaskManagementReply_t *tm_reply;
571 u32 sz;
572 u32 lun;
573 char *desc = NULL;
575 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
576 desc = "abort_task";
577 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
578 desc = "query_task";
579 else
580 return 0;
582 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
584 handle = le16_to_cpu(tm_request->DevHandle);
585 for (smid = ioc->scsiio_depth; smid && !found; smid--) {
586 struct scsiio_tracker *st;
588 scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
589 if (!scmd)
590 continue;
591 if (lun != scmd->device->lun)
592 continue;
593 priv_data = scmd->device->hostdata;
594 if (priv_data->sas_target == NULL)
595 continue;
596 if (priv_data->sas_target->handle != handle)
597 continue;
598 st = scsi_cmd_priv(scmd);
599 tm_request->TaskMID = cpu_to_le16(st->smid);
600 found = 1;
603 if (!found) {
604 dctlprintk(ioc,
605 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), no active mid!!\n",
606 desc, le16_to_cpu(tm_request->DevHandle),
607 lun));
608 tm_reply = ioc->ctl_cmds.reply;
609 tm_reply->DevHandle = tm_request->DevHandle;
610 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
611 tm_reply->TaskType = tm_request->TaskType;
612 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
613 tm_reply->VP_ID = tm_request->VP_ID;
614 tm_reply->VF_ID = tm_request->VF_ID;
615 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
616 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
617 sz))
618 pr_err("failure at %s:%d/%s()!\n", __FILE__,
619 __LINE__, __func__);
620 return 1;
623 dctlprintk(ioc,
624 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), task_mid(%d)\n",
625 desc, le16_to_cpu(tm_request->DevHandle), lun,
626 le16_to_cpu(tm_request->TaskMID)));
627 return 0;
631 * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
632 * @ioc: per adapter object
633 * @karg: (struct mpt3_ioctl_command)
634 * @mf: pointer to mf in user space
636 static long
637 _ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
638 void __user *mf)
640 MPI2RequestHeader_t *mpi_request = NULL, *request;
641 MPI2DefaultReply_t *mpi_reply;
642 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
643 struct _pcie_device *pcie_device = NULL;
644 u16 smid;
645 u8 timeout;
646 u8 issue_reset;
647 u32 sz, sz_arg;
648 void *psge;
649 void *data_out = NULL;
650 dma_addr_t data_out_dma = 0;
651 size_t data_out_sz = 0;
652 void *data_in = NULL;
653 dma_addr_t data_in_dma = 0;
654 size_t data_in_sz = 0;
655 long ret;
656 u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
657 u8 tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
659 issue_reset = 0;
661 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
662 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
663 ret = -EAGAIN;
664 goto out;
667 ret = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
668 if (ret)
669 goto out;
671 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
672 if (!mpi_request) {
673 ioc_err(ioc, "%s: failed obtaining a memory for mpi_request\n",
674 __func__);
675 ret = -ENOMEM;
676 goto out;
679 /* Check for overflow and wraparound */
680 if (karg.data_sge_offset * 4 > ioc->request_sz ||
681 karg.data_sge_offset > (UINT_MAX / 4)) {
682 ret = -EINVAL;
683 goto out;
686 /* copy in request message frame from user */
687 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
688 pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
689 __func__);
690 ret = -EFAULT;
691 goto out;
694 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
695 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
696 if (!smid) {
697 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
698 ret = -EAGAIN;
699 goto out;
701 } else {
702 /* Use first reserved smid for passthrough ioctls */
703 smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
706 ret = 0;
707 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
708 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
709 request = mpt3sas_base_get_msg_frame(ioc, smid);
710 memcpy(request, mpi_request, karg.data_sge_offset*4);
711 ioc->ctl_cmds.smid = smid;
712 data_out_sz = karg.data_out_size;
713 data_in_sz = karg.data_in_size;
715 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
716 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
717 mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
718 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
719 mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
721 device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
722 if (!device_handle || (device_handle >
723 ioc->facts.MaxDevHandle)) {
724 ret = -EINVAL;
725 mpt3sas_base_free_smid(ioc, smid);
726 goto out;
730 /* obtain dma-able memory for data transfer */
731 if (data_out_sz) /* WRITE */ {
732 data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz,
733 &data_out_dma, GFP_KERNEL);
734 if (!data_out) {
735 pr_err("failure at %s:%d/%s()!\n", __FILE__,
736 __LINE__, __func__);
737 ret = -ENOMEM;
738 mpt3sas_base_free_smid(ioc, smid);
739 goto out;
741 if (copy_from_user(data_out, karg.data_out_buf_ptr,
742 data_out_sz)) {
743 pr_err("failure at %s:%d/%s()!\n", __FILE__,
744 __LINE__, __func__);
745 ret = -EFAULT;
746 mpt3sas_base_free_smid(ioc, smid);
747 goto out;
751 if (data_in_sz) /* READ */ {
752 data_in = dma_alloc_coherent(&ioc->pdev->dev, data_in_sz,
753 &data_in_dma, GFP_KERNEL);
754 if (!data_in) {
755 pr_err("failure at %s:%d/%s()!\n", __FILE__,
756 __LINE__, __func__);
757 ret = -ENOMEM;
758 mpt3sas_base_free_smid(ioc, smid);
759 goto out;
763 psge = (void *)request + (karg.data_sge_offset*4);
765 /* send command to firmware */
766 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
768 init_completion(&ioc->ctl_cmds.done);
769 switch (mpi_request->Function) {
770 case MPI2_FUNCTION_NVME_ENCAPSULATED:
772 nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
774 * Get the Physical Address of the sense buffer.
775 * Use Error Response buffer address field to hold the sense
776 * buffer address.
777 * Clear the internal sense buffer, which will potentially hold
778 * the Completion Queue Entry on return, or 0 if no Entry.
779 * Build the PRPs and set direction bits.
780 * Send the request.
782 nvme_encap_request->ErrorResponseBaseAddress =
783 cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
784 nvme_encap_request->ErrorResponseBaseAddress |=
785 cpu_to_le64(le32_to_cpu(
786 mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
787 nvme_encap_request->ErrorResponseAllocationLength =
788 cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
789 memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
790 ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
791 data_out_dma, data_out_sz, data_in_dma, data_in_sz);
792 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
793 dtmprintk(ioc,
794 ioc_info(ioc, "handle(0x%04x): ioctl failed due to device removal in progress\n",
795 device_handle));
796 mpt3sas_base_free_smid(ioc, smid);
797 ret = -EINVAL;
798 goto out;
800 mpt3sas_base_put_smid_nvme_encap(ioc, smid);
801 break;
803 case MPI2_FUNCTION_SCSI_IO_REQUEST:
804 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
806 Mpi2SCSIIORequest_t *scsiio_request =
807 (Mpi2SCSIIORequest_t *)request;
808 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
809 scsiio_request->SenseBufferLowAddress =
810 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
811 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
812 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
813 dtmprintk(ioc,
814 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
815 device_handle));
816 mpt3sas_base_free_smid(ioc, smid);
817 ret = -EINVAL;
818 goto out;
820 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
821 data_in_dma, data_in_sz);
822 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
823 ioc->put_smid_scsi_io(ioc, smid, device_handle);
824 else
825 ioc->put_smid_default(ioc, smid);
826 break;
828 case MPI2_FUNCTION_SCSI_TASK_MGMT:
830 Mpi2SCSITaskManagementRequest_t *tm_request =
831 (Mpi2SCSITaskManagementRequest_t *)request;
833 dtmprintk(ioc,
834 ioc_info(ioc, "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
835 le16_to_cpu(tm_request->DevHandle),
836 tm_request->TaskType));
837 ioc->got_task_abort_from_ioctl = 1;
838 if (tm_request->TaskType ==
839 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
840 tm_request->TaskType ==
841 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
842 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
843 mpt3sas_base_free_smid(ioc, smid);
844 ioc->got_task_abort_from_ioctl = 0;
845 goto out;
848 ioc->got_task_abort_from_ioctl = 0;
850 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
851 dtmprintk(ioc,
852 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
853 device_handle));
854 mpt3sas_base_free_smid(ioc, smid);
855 ret = -EINVAL;
856 goto out;
858 mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
859 tm_request->DevHandle));
860 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
861 data_in_dma, data_in_sz);
862 ioc->put_smid_hi_priority(ioc, smid, 0);
863 break;
865 case MPI2_FUNCTION_SMP_PASSTHROUGH:
867 Mpi2SmpPassthroughRequest_t *smp_request =
868 (Mpi2SmpPassthroughRequest_t *)mpi_request;
869 u8 *data;
871 /* ioc determines which port to use */
872 smp_request->PhysicalPort = 0xFF;
873 if (smp_request->PassthroughFlags &
874 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
875 data = (u8 *)&smp_request->SGL;
876 else {
877 if (unlikely(data_out == NULL)) {
878 pr_err("failure at %s:%d/%s()!\n",
879 __FILE__, __LINE__, __func__);
880 mpt3sas_base_free_smid(ioc, smid);
881 ret = -EINVAL;
882 goto out;
884 data = data_out;
887 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
888 ioc->ioc_link_reset_in_progress = 1;
889 ioc->ignore_loginfos = 1;
891 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
892 data_in_sz);
893 ioc->put_smid_default(ioc, smid);
894 break;
896 case MPI2_FUNCTION_SATA_PASSTHROUGH:
898 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
899 dtmprintk(ioc,
900 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
901 device_handle));
902 mpt3sas_base_free_smid(ioc, smid);
903 ret = -EINVAL;
904 goto out;
906 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
907 data_in_sz);
908 ioc->put_smid_default(ioc, smid);
909 break;
911 case MPI2_FUNCTION_FW_DOWNLOAD:
912 case MPI2_FUNCTION_FW_UPLOAD:
914 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
915 data_in_sz);
916 ioc->put_smid_default(ioc, smid);
917 break;
919 case MPI2_FUNCTION_TOOLBOX:
921 Mpi2ToolboxCleanRequest_t *toolbox_request =
922 (Mpi2ToolboxCleanRequest_t *)mpi_request;
924 if (toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL) {
925 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
926 data_in_dma, data_in_sz);
927 } else {
928 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
929 data_in_dma, data_in_sz);
931 ioc->put_smid_default(ioc, smid);
932 break;
934 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
936 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
937 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
939 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
940 || sasiounit_request->Operation ==
941 MPI2_SAS_OP_PHY_LINK_RESET) {
942 ioc->ioc_link_reset_in_progress = 1;
943 ioc->ignore_loginfos = 1;
945 /* drop to default case for posting the request */
947 /* fall through */
948 default:
949 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
950 data_in_dma, data_in_sz);
951 ioc->put_smid_default(ioc, smid);
952 break;
955 if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
956 timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
957 else
958 timeout = karg.timeout;
959 wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
960 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
961 Mpi2SCSITaskManagementRequest_t *tm_request =
962 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
963 mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
964 tm_request->DevHandle));
965 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
966 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
967 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
968 ioc->ioc_link_reset_in_progress) {
969 ioc->ioc_link_reset_in_progress = 0;
970 ioc->ignore_loginfos = 0;
972 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
973 issue_reset =
974 mpt3sas_base_check_cmd_timeout(ioc,
975 ioc->ctl_cmds.status, mpi_request,
976 karg.data_sge_offset);
977 goto issue_host_reset;
980 mpi_reply = ioc->ctl_cmds.reply;
982 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
983 (ioc->logging_level & MPT_DEBUG_TM)) {
984 Mpi2SCSITaskManagementReply_t *tm_reply =
985 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
987 ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
988 le16_to_cpu(tm_reply->IOCStatus),
989 le32_to_cpu(tm_reply->IOCLogInfo),
990 le32_to_cpu(tm_reply->TerminationCount));
993 /* copy out xdata to user */
994 if (data_in_sz) {
995 if (copy_to_user(karg.data_in_buf_ptr, data_in,
996 data_in_sz)) {
997 pr_err("failure at %s:%d/%s()!\n", __FILE__,
998 __LINE__, __func__);
999 ret = -ENODATA;
1000 goto out;
1004 /* copy out reply message frame to user */
1005 if (karg.max_reply_bytes) {
1006 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1007 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1008 sz)) {
1009 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1010 __LINE__, __func__);
1011 ret = -ENODATA;
1012 goto out;
1016 /* copy out sense/NVMe Error Response to user */
1017 if (karg.max_sense_bytes && (mpi_request->Function ==
1018 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
1019 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1020 MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1021 if (karg.sense_data_ptr == NULL) {
1022 ioc_info(ioc, "Response buffer provided by application is NULL; Response data will not be returned\n");
1023 goto out;
1025 sz_arg = (mpi_request->Function ==
1026 MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1027 SCSI_SENSE_BUFFERSIZE;
1028 sz = min_t(u32, karg.max_sense_bytes, sz_arg);
1029 if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1030 sz)) {
1031 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1032 __LINE__, __func__);
1033 ret = -ENODATA;
1034 goto out;
1038 issue_host_reset:
1039 if (issue_reset) {
1040 ret = -ENODATA;
1041 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1042 mpi_request->Function ==
1043 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1044 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
1045 ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
1046 le16_to_cpu(mpi_request->FunctionDependent1));
1047 mpt3sas_halt_firmware(ioc);
1048 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1049 le16_to_cpu(mpi_request->FunctionDependent1));
1050 if (pcie_device && (!ioc->tm_custom_handling))
1051 mpt3sas_scsih_issue_locked_tm(ioc,
1052 le16_to_cpu(mpi_request->FunctionDependent1),
1053 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1054 0, pcie_device->reset_timeout,
1055 tr_method);
1056 else
1057 mpt3sas_scsih_issue_locked_tm(ioc,
1058 le16_to_cpu(mpi_request->FunctionDependent1),
1059 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1060 0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
1061 } else
1062 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1065 out:
1066 if (pcie_device)
1067 pcie_device_put(pcie_device);
1069 /* free memory associated with sg buffers */
1070 if (data_in)
1071 dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
1072 data_in_dma);
1074 if (data_out)
1075 dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
1076 data_out_dma);
1078 kfree(mpi_request);
1079 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1080 return ret;
1084 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1085 * @ioc: per adapter object
1086 * @arg: user space buffer containing ioctl content
1088 static long
1089 _ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1091 struct mpt3_ioctl_iocinfo karg;
1093 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1094 __func__));
1096 memset(&karg, 0 , sizeof(karg));
1097 if (ioc->pfacts)
1098 karg.port_number = ioc->pfacts[0].PortNumber;
1099 karg.hw_rev = ioc->pdev->revision;
1100 karg.pci_id = ioc->pdev->device;
1101 karg.subsystem_device = ioc->pdev->subsystem_device;
1102 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1103 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1104 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1105 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1106 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1107 karg.firmware_version = ioc->facts.FWVersion.Word;
1108 strcpy(karg.driver_version, ioc->driver_name);
1109 strcat(karg.driver_version, "-");
1110 switch (ioc->hba_mpi_version_belonged) {
1111 case MPI2_VERSION:
1112 if (ioc->is_warpdrive)
1113 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1114 else
1115 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1116 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1117 break;
1118 case MPI25_VERSION:
1119 case MPI26_VERSION:
1120 if (ioc->is_gen35_ioc)
1121 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1122 else
1123 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1124 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1125 break;
1127 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1129 if (copy_to_user(arg, &karg, sizeof(karg))) {
1130 pr_err("failure at %s:%d/%s()!\n",
1131 __FILE__, __LINE__, __func__);
1132 return -EFAULT;
1134 return 0;
1138 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1139 * @ioc: per adapter object
1140 * @arg: user space buffer containing ioctl content
1142 static long
1143 _ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1145 struct mpt3_ioctl_eventquery karg;
1147 if (copy_from_user(&karg, arg, sizeof(karg))) {
1148 pr_err("failure at %s:%d/%s()!\n",
1149 __FILE__, __LINE__, __func__);
1150 return -EFAULT;
1153 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1154 __func__));
1156 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1157 memcpy(karg.event_types, ioc->event_type,
1158 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1160 if (copy_to_user(arg, &karg, sizeof(karg))) {
1161 pr_err("failure at %s:%d/%s()!\n",
1162 __FILE__, __LINE__, __func__);
1163 return -EFAULT;
1165 return 0;
1169 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1170 * @ioc: per adapter object
1171 * @arg: user space buffer containing ioctl content
1173 static long
1174 _ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1176 struct mpt3_ioctl_eventenable karg;
1178 if (copy_from_user(&karg, arg, sizeof(karg))) {
1179 pr_err("failure at %s:%d/%s()!\n",
1180 __FILE__, __LINE__, __func__);
1181 return -EFAULT;
1184 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1185 __func__));
1187 memcpy(ioc->event_type, karg.event_types,
1188 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1189 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1191 if (ioc->event_log)
1192 return 0;
1193 /* initialize event_log */
1194 ioc->event_context = 0;
1195 ioc->aen_event_read_flag = 0;
1196 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1197 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1198 if (!ioc->event_log) {
1199 pr_err("failure at %s:%d/%s()!\n",
1200 __FILE__, __LINE__, __func__);
1201 return -ENOMEM;
1203 return 0;
1207 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1208 * @ioc: per adapter object
1209 * @arg: user space buffer containing ioctl content
1211 static long
1212 _ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1214 struct mpt3_ioctl_eventreport karg;
1215 u32 number_bytes, max_events, max;
1216 struct mpt3_ioctl_eventreport __user *uarg = arg;
1218 if (copy_from_user(&karg, arg, sizeof(karg))) {
1219 pr_err("failure at %s:%d/%s()!\n",
1220 __FILE__, __LINE__, __func__);
1221 return -EFAULT;
1224 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1225 __func__));
1227 number_bytes = karg.hdr.max_data_size -
1228 sizeof(struct mpt3_ioctl_header);
1229 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1230 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1232 /* If fewer than 1 event is requested, there must have
1233 * been some type of error.
1235 if (!max || !ioc->event_log)
1236 return -ENODATA;
1238 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1239 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1240 pr_err("failure at %s:%d/%s()!\n",
1241 __FILE__, __LINE__, __func__);
1242 return -EFAULT;
1245 /* reset flag so SIGIO can restart */
1246 ioc->aen_event_read_flag = 0;
1247 return 0;
1251 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1252 * @ioc: per adapter object
1253 * @arg: user space buffer containing ioctl content
1255 static long
1256 _ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1258 struct mpt3_ioctl_diag_reset karg;
1259 int retval;
1261 if (copy_from_user(&karg, arg, sizeof(karg))) {
1262 pr_err("failure at %s:%d/%s()!\n",
1263 __FILE__, __LINE__, __func__);
1264 return -EFAULT;
1267 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1268 ioc->is_driver_loading)
1269 return -EAGAIN;
1271 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1272 __func__));
1274 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1275 ioc_info(ioc, "host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
1276 return 0;
1280 * _ctl_btdh_search_sas_device - searching for sas device
1281 * @ioc: per adapter object
1282 * @btdh: btdh ioctl payload
1284 static int
1285 _ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1286 struct mpt3_ioctl_btdh_mapping *btdh)
1288 struct _sas_device *sas_device;
1289 unsigned long flags;
1290 int rc = 0;
1292 if (list_empty(&ioc->sas_device_list))
1293 return rc;
1295 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1296 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1297 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1298 btdh->handle == sas_device->handle) {
1299 btdh->bus = sas_device->channel;
1300 btdh->id = sas_device->id;
1301 rc = 1;
1302 goto out;
1303 } else if (btdh->bus == sas_device->channel && btdh->id ==
1304 sas_device->id && btdh->handle == 0xFFFF) {
1305 btdh->handle = sas_device->handle;
1306 rc = 1;
1307 goto out;
1310 out:
1311 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1312 return rc;
1316 * _ctl_btdh_search_pcie_device - searching for pcie device
1317 * @ioc: per adapter object
1318 * @btdh: btdh ioctl payload
1320 static int
1321 _ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
1322 struct mpt3_ioctl_btdh_mapping *btdh)
1324 struct _pcie_device *pcie_device;
1325 unsigned long flags;
1326 int rc = 0;
1328 if (list_empty(&ioc->pcie_device_list))
1329 return rc;
1331 spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1332 list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1333 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1334 btdh->handle == pcie_device->handle) {
1335 btdh->bus = pcie_device->channel;
1336 btdh->id = pcie_device->id;
1337 rc = 1;
1338 goto out;
1339 } else if (btdh->bus == pcie_device->channel && btdh->id ==
1340 pcie_device->id && btdh->handle == 0xFFFF) {
1341 btdh->handle = pcie_device->handle;
1342 rc = 1;
1343 goto out;
1346 out:
1347 spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1348 return rc;
1352 * _ctl_btdh_search_raid_device - searching for raid device
1353 * @ioc: per adapter object
1354 * @btdh: btdh ioctl payload
1356 static int
1357 _ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1358 struct mpt3_ioctl_btdh_mapping *btdh)
1360 struct _raid_device *raid_device;
1361 unsigned long flags;
1362 int rc = 0;
1364 if (list_empty(&ioc->raid_device_list))
1365 return rc;
1367 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1368 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1369 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1370 btdh->handle == raid_device->handle) {
1371 btdh->bus = raid_device->channel;
1372 btdh->id = raid_device->id;
1373 rc = 1;
1374 goto out;
1375 } else if (btdh->bus == raid_device->channel && btdh->id ==
1376 raid_device->id && btdh->handle == 0xFFFF) {
1377 btdh->handle = raid_device->handle;
1378 rc = 1;
1379 goto out;
1382 out:
1383 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1384 return rc;
1388 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1389 * @ioc: per adapter object
1390 * @arg: user space buffer containing ioctl content
1392 static long
1393 _ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1395 struct mpt3_ioctl_btdh_mapping karg;
1396 int rc;
1398 if (copy_from_user(&karg, arg, sizeof(karg))) {
1399 pr_err("failure at %s:%d/%s()!\n",
1400 __FILE__, __LINE__, __func__);
1401 return -EFAULT;
1404 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1405 __func__));
1407 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1408 if (!rc)
1409 rc = _ctl_btdh_search_pcie_device(ioc, &karg);
1410 if (!rc)
1411 _ctl_btdh_search_raid_device(ioc, &karg);
1413 if (copy_to_user(arg, &karg, sizeof(karg))) {
1414 pr_err("failure at %s:%d/%s()!\n",
1415 __FILE__, __LINE__, __func__);
1416 return -EFAULT;
1418 return 0;
1422 * _ctl_diag_capability - return diag buffer capability
1423 * @ioc: per adapter object
1424 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1426 * returns 1 when diag buffer support is enabled in firmware
1428 static u8
1429 _ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1431 u8 rc = 0;
1433 switch (buffer_type) {
1434 case MPI2_DIAG_BUF_TYPE_TRACE:
1435 if (ioc->facts.IOCCapabilities &
1436 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1437 rc = 1;
1438 break;
1439 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1440 if (ioc->facts.IOCCapabilities &
1441 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1442 rc = 1;
1443 break;
1444 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1445 if (ioc->facts.IOCCapabilities &
1446 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1447 rc = 1;
1450 return rc;
1455 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1456 * @ioc: per adapter object
1457 * @diag_register: the diag_register struct passed in from user space
1460 static long
1461 _ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1462 struct mpt3_diag_register *diag_register)
1464 int rc, i;
1465 void *request_data = NULL;
1466 dma_addr_t request_data_dma;
1467 u32 request_data_sz = 0;
1468 Mpi2DiagBufferPostRequest_t *mpi_request;
1469 Mpi2DiagBufferPostReply_t *mpi_reply;
1470 u8 buffer_type;
1471 u16 smid;
1472 u16 ioc_status;
1473 u32 ioc_state;
1474 u8 issue_reset = 0;
1476 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1477 __func__));
1479 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1480 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1481 ioc_err(ioc, "%s: failed due to ioc not operational\n",
1482 __func__);
1483 rc = -EAGAIN;
1484 goto out;
1487 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1488 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
1489 rc = -EAGAIN;
1490 goto out;
1493 buffer_type = diag_register->buffer_type;
1494 if (!_ctl_diag_capability(ioc, buffer_type)) {
1495 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1496 __func__, buffer_type);
1497 return -EPERM;
1500 if (ioc->diag_buffer_status[buffer_type] &
1501 MPT3_DIAG_BUFFER_IS_REGISTERED) {
1502 ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1503 __func__, buffer_type);
1504 return -EINVAL;
1507 if (diag_register->requested_buffer_size % 4) {
1508 ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
1509 __func__);
1510 return -EINVAL;
1513 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1514 if (!smid) {
1515 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1516 rc = -EAGAIN;
1517 goto out;
1520 rc = 0;
1521 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1522 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1523 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1524 ioc->ctl_cmds.smid = smid;
1526 request_data = ioc->diag_buffer[buffer_type];
1527 request_data_sz = diag_register->requested_buffer_size;
1528 ioc->unique_id[buffer_type] = diag_register->unique_id;
1529 ioc->diag_buffer_status[buffer_type] = 0;
1530 memcpy(ioc->product_specific[buffer_type],
1531 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1532 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1534 if (request_data) {
1535 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1536 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1537 dma_free_coherent(&ioc->pdev->dev,
1538 ioc->diag_buffer_sz[buffer_type],
1539 request_data, request_data_dma);
1540 request_data = NULL;
1544 if (request_data == NULL) {
1545 ioc->diag_buffer_sz[buffer_type] = 0;
1546 ioc->diag_buffer_dma[buffer_type] = 0;
1547 request_data = dma_alloc_coherent(&ioc->pdev->dev,
1548 request_data_sz, &request_data_dma, GFP_KERNEL);
1549 if (request_data == NULL) {
1550 ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
1551 __func__, request_data_sz);
1552 mpt3sas_base_free_smid(ioc, smid);
1553 return -ENOMEM;
1555 ioc->diag_buffer[buffer_type] = request_data;
1556 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1557 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1560 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1561 mpi_request->BufferType = diag_register->buffer_type;
1562 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1563 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1564 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1565 mpi_request->VF_ID = 0; /* TODO */
1566 mpi_request->VP_ID = 0;
1568 dctlprintk(ioc,
1569 ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1570 __func__, request_data,
1571 (unsigned long long)request_data_dma,
1572 le32_to_cpu(mpi_request->BufferLength)));
1574 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1575 mpi_request->ProductSpecific[i] =
1576 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1578 init_completion(&ioc->ctl_cmds.done);
1579 ioc->put_smid_default(ioc, smid);
1580 wait_for_completion_timeout(&ioc->ctl_cmds.done,
1581 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1583 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1584 issue_reset =
1585 mpt3sas_base_check_cmd_timeout(ioc,
1586 ioc->ctl_cmds.status, mpi_request,
1587 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1588 goto issue_host_reset;
1591 /* process the completed Reply Message Frame */
1592 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1593 ioc_err(ioc, "%s: no reply message\n", __func__);
1594 rc = -EFAULT;
1595 goto out;
1598 mpi_reply = ioc->ctl_cmds.reply;
1599 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1601 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1602 ioc->diag_buffer_status[buffer_type] |=
1603 MPT3_DIAG_BUFFER_IS_REGISTERED;
1604 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
1605 } else {
1606 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1607 __func__,
1608 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1609 rc = -EFAULT;
1612 issue_host_reset:
1613 if (issue_reset)
1614 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1616 out:
1618 if (rc && request_data)
1619 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
1620 request_data, request_data_dma);
1622 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1623 return rc;
1627 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1628 * @ioc: per adapter object
1629 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1631 * This is called when command line option diag_buffer_enable is enabled
1632 * at driver load time.
1634 void
1635 mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1637 struct mpt3_diag_register diag_register;
1639 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1641 if (bits_to_register & 1) {
1642 ioc_info(ioc, "registering trace buffer support\n");
1643 ioc->diag_trigger_master.MasterData =
1644 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1645 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1646 /* register for 2MB buffers */
1647 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1648 diag_register.unique_id = 0x7075900;
1649 _ctl_diag_register_2(ioc, &diag_register);
1652 if (bits_to_register & 2) {
1653 ioc_info(ioc, "registering snapshot buffer support\n");
1654 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1655 /* register for 2MB buffers */
1656 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1657 diag_register.unique_id = 0x7075901;
1658 _ctl_diag_register_2(ioc, &diag_register);
1661 if (bits_to_register & 4) {
1662 ioc_info(ioc, "registering extended buffer support\n");
1663 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1664 /* register for 2MB buffers */
1665 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1666 diag_register.unique_id = 0x7075901;
1667 _ctl_diag_register_2(ioc, &diag_register);
1672 * _ctl_diag_register - application register with driver
1673 * @ioc: per adapter object
1674 * @arg: user space buffer containing ioctl content
1676 * This will allow the driver to setup any required buffers that will be
1677 * needed by firmware to communicate with the driver.
1679 static long
1680 _ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1682 struct mpt3_diag_register karg;
1683 long rc;
1685 if (copy_from_user(&karg, arg, sizeof(karg))) {
1686 pr_err("failure at %s:%d/%s()!\n",
1687 __FILE__, __LINE__, __func__);
1688 return -EFAULT;
1691 rc = _ctl_diag_register_2(ioc, &karg);
1692 return rc;
1696 * _ctl_diag_unregister - application unregister with driver
1697 * @ioc: per adapter object
1698 * @arg: user space buffer containing ioctl content
1700 * This will allow the driver to cleanup any memory allocated for diag
1701 * messages and to free up any resources.
1703 static long
1704 _ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1706 struct mpt3_diag_unregister karg;
1707 void *request_data;
1708 dma_addr_t request_data_dma;
1709 u32 request_data_sz;
1710 u8 buffer_type;
1712 if (copy_from_user(&karg, arg, sizeof(karg))) {
1713 pr_err("failure at %s:%d/%s()!\n",
1714 __FILE__, __LINE__, __func__);
1715 return -EFAULT;
1718 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1719 __func__));
1721 buffer_type = karg.unique_id & 0x000000ff;
1722 if (!_ctl_diag_capability(ioc, buffer_type)) {
1723 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1724 __func__, buffer_type);
1725 return -EPERM;
1728 if ((ioc->diag_buffer_status[buffer_type] &
1729 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1730 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1731 __func__, buffer_type);
1732 return -EINVAL;
1734 if ((ioc->diag_buffer_status[buffer_type] &
1735 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
1736 ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
1737 __func__, buffer_type);
1738 return -EINVAL;
1741 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1742 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1743 __func__, karg.unique_id);
1744 return -EINVAL;
1747 request_data = ioc->diag_buffer[buffer_type];
1748 if (!request_data) {
1749 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1750 __func__, buffer_type);
1751 return -ENOMEM;
1754 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1755 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1756 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
1757 request_data, request_data_dma);
1758 ioc->diag_buffer[buffer_type] = NULL;
1759 ioc->diag_buffer_status[buffer_type] = 0;
1760 return 0;
1764 * _ctl_diag_query - query relevant info associated with diag buffers
1765 * @ioc: per adapter object
1766 * @arg: user space buffer containing ioctl content
1768 * The application will send only buffer_type and unique_id. Driver will
1769 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1770 * 0x00, the driver will return info specified by Buffer Type.
1772 static long
1773 _ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1775 struct mpt3_diag_query karg;
1776 void *request_data;
1777 int i;
1778 u8 buffer_type;
1780 if (copy_from_user(&karg, arg, sizeof(karg))) {
1781 pr_err("failure at %s:%d/%s()!\n",
1782 __FILE__, __LINE__, __func__);
1783 return -EFAULT;
1786 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1787 __func__));
1789 karg.application_flags = 0;
1790 buffer_type = karg.buffer_type;
1792 if (!_ctl_diag_capability(ioc, buffer_type)) {
1793 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1794 __func__, buffer_type);
1795 return -EPERM;
1798 if ((ioc->diag_buffer_status[buffer_type] &
1799 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1800 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1801 __func__, buffer_type);
1802 return -EINVAL;
1805 if (karg.unique_id & 0xffffff00) {
1806 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1807 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1808 __func__, karg.unique_id);
1809 return -EINVAL;
1813 request_data = ioc->diag_buffer[buffer_type];
1814 if (!request_data) {
1815 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
1816 __func__, buffer_type);
1817 return -ENOMEM;
1820 if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
1821 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1822 MPT3_APP_FLAGS_BUFFER_VALID);
1823 else
1824 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1825 MPT3_APP_FLAGS_BUFFER_VALID |
1826 MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
1828 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1829 karg.product_specific[i] =
1830 ioc->product_specific[buffer_type][i];
1832 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1833 karg.driver_added_buffer_size = 0;
1834 karg.unique_id = ioc->unique_id[buffer_type];
1835 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1837 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
1838 ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
1839 __func__, arg);
1840 return -EFAULT;
1842 return 0;
1846 * mpt3sas_send_diag_release - Diag Release Message
1847 * @ioc: per adapter object
1848 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1849 * @issue_reset: specifies whether host reset is required.
1853 mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
1854 u8 *issue_reset)
1856 Mpi2DiagReleaseRequest_t *mpi_request;
1857 Mpi2DiagReleaseReply_t *mpi_reply;
1858 u16 smid;
1859 u16 ioc_status;
1860 u32 ioc_state;
1861 int rc;
1863 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1864 __func__));
1866 rc = 0;
1867 *issue_reset = 0;
1869 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1870 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1871 if (ioc->diag_buffer_status[buffer_type] &
1872 MPT3_DIAG_BUFFER_IS_REGISTERED)
1873 ioc->diag_buffer_status[buffer_type] |=
1874 MPT3_DIAG_BUFFER_IS_RELEASED;
1875 dctlprintk(ioc,
1876 ioc_info(ioc, "%s: skipping due to FAULT state\n",
1877 __func__));
1878 rc = -EAGAIN;
1879 goto out;
1882 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1883 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
1884 rc = -EAGAIN;
1885 goto out;
1888 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1889 if (!smid) {
1890 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1891 rc = -EAGAIN;
1892 goto out;
1895 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1896 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1897 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1898 ioc->ctl_cmds.smid = smid;
1900 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1901 mpi_request->BufferType = buffer_type;
1902 mpi_request->VF_ID = 0; /* TODO */
1903 mpi_request->VP_ID = 0;
1905 init_completion(&ioc->ctl_cmds.done);
1906 ioc->put_smid_default(ioc, smid);
1907 wait_for_completion_timeout(&ioc->ctl_cmds.done,
1908 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1910 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1911 *issue_reset = mpt3sas_base_check_cmd_timeout(ioc,
1912 ioc->ctl_cmds.status, mpi_request,
1913 sizeof(Mpi2DiagReleaseRequest_t)/4);
1914 rc = -EFAULT;
1915 goto out;
1918 /* process the completed Reply Message Frame */
1919 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1920 ioc_err(ioc, "%s: no reply message\n", __func__);
1921 rc = -EFAULT;
1922 goto out;
1925 mpi_reply = ioc->ctl_cmds.reply;
1926 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1928 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1929 ioc->diag_buffer_status[buffer_type] |=
1930 MPT3_DIAG_BUFFER_IS_RELEASED;
1931 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
1932 } else {
1933 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1934 __func__,
1935 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1936 rc = -EFAULT;
1939 out:
1940 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1941 return rc;
1945 * _ctl_diag_release - request to send Diag Release Message to firmware
1946 * @ioc: ?
1947 * @arg: user space buffer containing ioctl content
1949 * This allows ownership of the specified buffer to returned to the driver,
1950 * allowing an application to read the buffer without fear that firmware is
1951 * overwriting information in the buffer.
1953 static long
1954 _ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1956 struct mpt3_diag_release karg;
1957 void *request_data;
1958 int rc;
1959 u8 buffer_type;
1960 u8 issue_reset = 0;
1962 if (copy_from_user(&karg, arg, sizeof(karg))) {
1963 pr_err("failure at %s:%d/%s()!\n",
1964 __FILE__, __LINE__, __func__);
1965 return -EFAULT;
1968 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1969 __func__));
1971 buffer_type = karg.unique_id & 0x000000ff;
1972 if (!_ctl_diag_capability(ioc, buffer_type)) {
1973 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1974 __func__, buffer_type);
1975 return -EPERM;
1978 if ((ioc->diag_buffer_status[buffer_type] &
1979 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1980 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1981 __func__, buffer_type);
1982 return -EINVAL;
1985 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1986 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1987 __func__, karg.unique_id);
1988 return -EINVAL;
1991 if (ioc->diag_buffer_status[buffer_type] &
1992 MPT3_DIAG_BUFFER_IS_RELEASED) {
1993 ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
1994 __func__, buffer_type);
1995 return 0;
1998 request_data = ioc->diag_buffer[buffer_type];
2000 if (!request_data) {
2001 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2002 __func__, buffer_type);
2003 return -ENOMEM;
2006 /* buffers were released by due to host reset */
2007 if ((ioc->diag_buffer_status[buffer_type] &
2008 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2009 ioc->diag_buffer_status[buffer_type] |=
2010 MPT3_DIAG_BUFFER_IS_RELEASED;
2011 ioc->diag_buffer_status[buffer_type] &=
2012 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
2013 ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
2014 __func__, buffer_type);
2015 return 0;
2018 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2020 if (issue_reset)
2021 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2023 return rc;
2027 * _ctl_diag_read_buffer - request for copy of the diag buffer
2028 * @ioc: per adapter object
2029 * @arg: user space buffer containing ioctl content
2031 static long
2032 _ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2034 struct mpt3_diag_read_buffer karg;
2035 struct mpt3_diag_read_buffer __user *uarg = arg;
2036 void *request_data, *diag_data;
2037 Mpi2DiagBufferPostRequest_t *mpi_request;
2038 Mpi2DiagBufferPostReply_t *mpi_reply;
2039 int rc, i;
2040 u8 buffer_type;
2041 unsigned long request_size, copy_size;
2042 u16 smid;
2043 u16 ioc_status;
2044 u8 issue_reset = 0;
2046 if (copy_from_user(&karg, arg, sizeof(karg))) {
2047 pr_err("failure at %s:%d/%s()!\n",
2048 __FILE__, __LINE__, __func__);
2049 return -EFAULT;
2052 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2053 __func__));
2055 buffer_type = karg.unique_id & 0x000000ff;
2056 if (!_ctl_diag_capability(ioc, buffer_type)) {
2057 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2058 __func__, buffer_type);
2059 return -EPERM;
2062 if (karg.unique_id != ioc->unique_id[buffer_type]) {
2063 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2064 __func__, karg.unique_id);
2065 return -EINVAL;
2068 request_data = ioc->diag_buffer[buffer_type];
2069 if (!request_data) {
2070 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2071 __func__, buffer_type);
2072 return -ENOMEM;
2075 request_size = ioc->diag_buffer_sz[buffer_type];
2077 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2078 ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
2079 __func__);
2080 return -EINVAL;
2083 if (karg.starting_offset > request_size)
2084 return -EINVAL;
2086 diag_data = (void *)(request_data + karg.starting_offset);
2087 dctlprintk(ioc,
2088 ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2089 __func__, diag_data, karg.starting_offset,
2090 karg.bytes_to_read));
2092 /* Truncate data on requests that are too large */
2093 if ((diag_data + karg.bytes_to_read < diag_data) ||
2094 (diag_data + karg.bytes_to_read > request_data + request_size))
2095 copy_size = request_size - karg.starting_offset;
2096 else
2097 copy_size = karg.bytes_to_read;
2099 if (copy_to_user((void __user *)uarg->diagnostic_data,
2100 diag_data, copy_size)) {
2101 ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2102 __func__, diag_data);
2103 return -EFAULT;
2106 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2107 return 0;
2109 dctlprintk(ioc,
2110 ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
2111 __func__, buffer_type));
2112 if ((ioc->diag_buffer_status[buffer_type] &
2113 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2114 dctlprintk(ioc,
2115 ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
2116 __func__, buffer_type));
2117 return 0;
2119 /* Get a free request frame and save the message context.
2122 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2123 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
2124 rc = -EAGAIN;
2125 goto out;
2128 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2129 if (!smid) {
2130 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2131 rc = -EAGAIN;
2132 goto out;
2135 rc = 0;
2136 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2137 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2138 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2139 ioc->ctl_cmds.smid = smid;
2141 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2142 mpi_request->BufferType = buffer_type;
2143 mpi_request->BufferLength =
2144 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2145 mpi_request->BufferAddress =
2146 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2147 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2148 mpi_request->ProductSpecific[i] =
2149 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2150 mpi_request->VF_ID = 0; /* TODO */
2151 mpi_request->VP_ID = 0;
2153 init_completion(&ioc->ctl_cmds.done);
2154 ioc->put_smid_default(ioc, smid);
2155 wait_for_completion_timeout(&ioc->ctl_cmds.done,
2156 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2158 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2159 issue_reset =
2160 mpt3sas_base_check_cmd_timeout(ioc,
2161 ioc->ctl_cmds.status, mpi_request,
2162 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2163 goto issue_host_reset;
2166 /* process the completed Reply Message Frame */
2167 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2168 ioc_err(ioc, "%s: no reply message\n", __func__);
2169 rc = -EFAULT;
2170 goto out;
2173 mpi_reply = ioc->ctl_cmds.reply;
2174 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2176 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2177 ioc->diag_buffer_status[buffer_type] |=
2178 MPT3_DIAG_BUFFER_IS_REGISTERED;
2179 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
2180 } else {
2181 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2182 __func__, ioc_status,
2183 le32_to_cpu(mpi_reply->IOCLogInfo));
2184 rc = -EFAULT;
2187 issue_host_reset:
2188 if (issue_reset)
2189 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2191 out:
2193 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2194 return rc;
2199 #ifdef CONFIG_COMPAT
2201 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2202 * @ioc: per adapter object
2203 * @cmd: ioctl opcode
2204 * @arg: (struct mpt3_ioctl_command32)
2206 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2208 static long
2209 _ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2210 void __user *arg)
2212 struct mpt3_ioctl_command32 karg32;
2213 struct mpt3_ioctl_command32 __user *uarg;
2214 struct mpt3_ioctl_command karg;
2216 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2217 return -EINVAL;
2219 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2221 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2222 pr_err("failure at %s:%d/%s()!\n",
2223 __FILE__, __LINE__, __func__);
2224 return -EFAULT;
2227 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2228 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2229 karg.hdr.port_number = karg32.hdr.port_number;
2230 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2231 karg.timeout = karg32.timeout;
2232 karg.max_reply_bytes = karg32.max_reply_bytes;
2233 karg.data_in_size = karg32.data_in_size;
2234 karg.data_out_size = karg32.data_out_size;
2235 karg.max_sense_bytes = karg32.max_sense_bytes;
2236 karg.data_sge_offset = karg32.data_sge_offset;
2237 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2238 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2239 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2240 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2241 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2243 #endif
2246 * _ctl_ioctl_main - main ioctl entry point
2247 * @file: (struct file)
2248 * @cmd: ioctl opcode
2249 * @arg: user space data buffer
2250 * @compat: handles 32 bit applications in 64bit os
2251 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
2252 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
2254 static long
2255 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2256 u8 compat, u16 mpi_version)
2258 struct MPT3SAS_ADAPTER *ioc;
2259 struct mpt3_ioctl_header ioctl_header;
2260 enum block_state state;
2261 long ret = -EINVAL;
2263 /* get IOCTL header */
2264 if (copy_from_user(&ioctl_header, (char __user *)arg,
2265 sizeof(struct mpt3_ioctl_header))) {
2266 pr_err("failure at %s:%d/%s()!\n",
2267 __FILE__, __LINE__, __func__);
2268 return -EFAULT;
2271 if (_ctl_verify_adapter(ioctl_header.ioc_number,
2272 &ioc, mpi_version) == -1 || !ioc)
2273 return -ENODEV;
2275 /* pci_access_mutex lock acquired by ioctl path */
2276 mutex_lock(&ioc->pci_access_mutex);
2278 if (ioc->shost_recovery || ioc->pci_error_recovery ||
2279 ioc->is_driver_loading || ioc->remove_host) {
2280 ret = -EAGAIN;
2281 goto out_unlock_pciaccess;
2284 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2285 if (state == NON_BLOCKING) {
2286 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2287 ret = -EAGAIN;
2288 goto out_unlock_pciaccess;
2290 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2291 ret = -ERESTARTSYS;
2292 goto out_unlock_pciaccess;
2296 switch (cmd) {
2297 case MPT3IOCINFO:
2298 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2299 ret = _ctl_getiocinfo(ioc, arg);
2300 break;
2301 #ifdef CONFIG_COMPAT
2302 case MPT3COMMAND32:
2303 #endif
2304 case MPT3COMMAND:
2306 struct mpt3_ioctl_command __user *uarg;
2307 struct mpt3_ioctl_command karg;
2309 #ifdef CONFIG_COMPAT
2310 if (compat) {
2311 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2312 break;
2314 #endif
2315 if (copy_from_user(&karg, arg, sizeof(karg))) {
2316 pr_err("failure at %s:%d/%s()!\n",
2317 __FILE__, __LINE__, __func__);
2318 ret = -EFAULT;
2319 break;
2322 if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2323 ret = -EINVAL;
2324 break;
2326 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2327 uarg = arg;
2328 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2330 break;
2332 case MPT3EVENTQUERY:
2333 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2334 ret = _ctl_eventquery(ioc, arg);
2335 break;
2336 case MPT3EVENTENABLE:
2337 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2338 ret = _ctl_eventenable(ioc, arg);
2339 break;
2340 case MPT3EVENTREPORT:
2341 ret = _ctl_eventreport(ioc, arg);
2342 break;
2343 case MPT3HARDRESET:
2344 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2345 ret = _ctl_do_reset(ioc, arg);
2346 break;
2347 case MPT3BTDHMAPPING:
2348 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2349 ret = _ctl_btdh_mapping(ioc, arg);
2350 break;
2351 case MPT3DIAGREGISTER:
2352 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2353 ret = _ctl_diag_register(ioc, arg);
2354 break;
2355 case MPT3DIAGUNREGISTER:
2356 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2357 ret = _ctl_diag_unregister(ioc, arg);
2358 break;
2359 case MPT3DIAGQUERY:
2360 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2361 ret = _ctl_diag_query(ioc, arg);
2362 break;
2363 case MPT3DIAGRELEASE:
2364 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2365 ret = _ctl_diag_release(ioc, arg);
2366 break;
2367 case MPT3DIAGREADBUFFER:
2368 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2369 ret = _ctl_diag_read_buffer(ioc, arg);
2370 break;
2371 default:
2372 dctlprintk(ioc,
2373 ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2374 cmd));
2375 break;
2378 mutex_unlock(&ioc->ctl_cmds.mutex);
2379 out_unlock_pciaccess:
2380 mutex_unlock(&ioc->pci_access_mutex);
2381 return ret;
2385 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
2386 * @file: (struct file)
2387 * @cmd: ioctl opcode
2388 * @arg: ?
2390 static long
2391 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2393 long ret;
2395 /* pass MPI25_VERSION | MPI26_VERSION value,
2396 * to indicate that this ioctl cmd
2397 * came from mpt3ctl ioctl device.
2399 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2400 MPI25_VERSION | MPI26_VERSION);
2401 return ret;
2405 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
2406 * @file: (struct file)
2407 * @cmd: ioctl opcode
2408 * @arg: ?
2410 static long
2411 _ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2413 long ret;
2415 /* pass MPI2_VERSION value, to indicate that this ioctl cmd
2416 * came from mpt2ctl ioctl device.
2418 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2419 return ret;
2421 #ifdef CONFIG_COMPAT
2423 *_ ctl_ioctl_compat - main ioctl entry point (compat)
2424 * @file: ?
2425 * @cmd: ?
2426 * @arg: ?
2428 * This routine handles 32 bit applications in 64bit os.
2430 static long
2431 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2433 long ret;
2435 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2436 MPI25_VERSION | MPI26_VERSION);
2437 return ret;
2441 *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
2442 * @file: ?
2443 * @cmd: ?
2444 * @arg: ?
2446 * This routine handles 32 bit applications in 64bit os.
2448 static long
2449 _ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2451 long ret;
2453 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
2454 return ret;
2456 #endif
2458 /* scsi host attributes */
2460 * version_fw_show - firmware version
2461 * @cdev: pointer to embedded class device
2462 * @attr: ?
2463 * @buf: the buffer returned
2465 * A sysfs 'read-only' shost attribute.
2467 static ssize_t
2468 version_fw_show(struct device *cdev, struct device_attribute *attr,
2469 char *buf)
2471 struct Scsi_Host *shost = class_to_shost(cdev);
2472 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2474 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2475 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2476 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2477 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2478 ioc->facts.FWVersion.Word & 0x000000FF);
2480 static DEVICE_ATTR_RO(version_fw);
2483 * version_bios_show - bios version
2484 * @cdev: pointer to embedded class device
2485 * @attr: ?
2486 * @buf: the buffer returned
2488 * A sysfs 'read-only' shost attribute.
2490 static ssize_t
2491 version_bios_show(struct device *cdev, struct device_attribute *attr,
2492 char *buf)
2494 struct Scsi_Host *shost = class_to_shost(cdev);
2495 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2497 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2499 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2500 (version & 0xFF000000) >> 24,
2501 (version & 0x00FF0000) >> 16,
2502 (version & 0x0000FF00) >> 8,
2503 version & 0x000000FF);
2505 static DEVICE_ATTR_RO(version_bios);
2508 * version_mpi_show - MPI (message passing interface) version
2509 * @cdev: pointer to embedded class device
2510 * @attr: ?
2511 * @buf: the buffer returned
2513 * A sysfs 'read-only' shost attribute.
2515 static ssize_t
2516 version_mpi_show(struct device *cdev, struct device_attribute *attr,
2517 char *buf)
2519 struct Scsi_Host *shost = class_to_shost(cdev);
2520 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2522 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2523 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2525 static DEVICE_ATTR_RO(version_mpi);
2528 * version_product_show - product name
2529 * @cdev: pointer to embedded class device
2530 * @attr: ?
2531 * @buf: the buffer returned
2533 * A sysfs 'read-only' shost attribute.
2535 static ssize_t
2536 version_product_show(struct device *cdev, struct device_attribute *attr,
2537 char *buf)
2539 struct Scsi_Host *shost = class_to_shost(cdev);
2540 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2542 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2544 static DEVICE_ATTR_RO(version_product);
2547 * version_nvdata_persistent_show - ndvata persistent version
2548 * @cdev: pointer to embedded class device
2549 * @attr: ?
2550 * @buf: the buffer returned
2552 * A sysfs 'read-only' shost attribute.
2554 static ssize_t
2555 version_nvdata_persistent_show(struct device *cdev,
2556 struct device_attribute *attr, char *buf)
2558 struct Scsi_Host *shost = class_to_shost(cdev);
2559 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2561 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2562 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2564 static DEVICE_ATTR_RO(version_nvdata_persistent);
2567 * version_nvdata_default_show - nvdata default version
2568 * @cdev: pointer to embedded class device
2569 * @attr: ?
2570 * @buf: the buffer returned
2572 * A sysfs 'read-only' shost attribute.
2574 static ssize_t
2575 version_nvdata_default_show(struct device *cdev, struct device_attribute
2576 *attr, char *buf)
2578 struct Scsi_Host *shost = class_to_shost(cdev);
2579 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2581 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2582 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2584 static DEVICE_ATTR_RO(version_nvdata_default);
2587 * board_name_show - board name
2588 * @cdev: pointer to embedded class device
2589 * @attr: ?
2590 * @buf: the buffer returned
2592 * A sysfs 'read-only' shost attribute.
2594 static ssize_t
2595 board_name_show(struct device *cdev, struct device_attribute *attr,
2596 char *buf)
2598 struct Scsi_Host *shost = class_to_shost(cdev);
2599 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2601 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2603 static DEVICE_ATTR_RO(board_name);
2606 * board_assembly_show - board assembly name
2607 * @cdev: pointer to embedded class device
2608 * @attr: ?
2609 * @buf: the buffer returned
2611 * A sysfs 'read-only' shost attribute.
2613 static ssize_t
2614 board_assembly_show(struct device *cdev, struct device_attribute *attr,
2615 char *buf)
2617 struct Scsi_Host *shost = class_to_shost(cdev);
2618 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2620 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2622 static DEVICE_ATTR_RO(board_assembly);
2625 * board_tracer_show - board tracer number
2626 * @cdev: pointer to embedded class device
2627 * @attr: ?
2628 * @buf: the buffer returned
2630 * A sysfs 'read-only' shost attribute.
2632 static ssize_t
2633 board_tracer_show(struct device *cdev, struct device_attribute *attr,
2634 char *buf)
2636 struct Scsi_Host *shost = class_to_shost(cdev);
2637 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2639 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2641 static DEVICE_ATTR_RO(board_tracer);
2644 * io_delay_show - io missing delay
2645 * @cdev: pointer to embedded class device
2646 * @attr: ?
2647 * @buf: the buffer returned
2649 * This is for firmware implemention for deboucing device
2650 * removal events.
2652 * A sysfs 'read-only' shost attribute.
2654 static ssize_t
2655 io_delay_show(struct device *cdev, struct device_attribute *attr,
2656 char *buf)
2658 struct Scsi_Host *shost = class_to_shost(cdev);
2659 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2661 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2663 static DEVICE_ATTR_RO(io_delay);
2666 * device_delay_show - device missing delay
2667 * @cdev: pointer to embedded class device
2668 * @attr: ?
2669 * @buf: the buffer returned
2671 * This is for firmware implemention for deboucing device
2672 * removal events.
2674 * A sysfs 'read-only' shost attribute.
2676 static ssize_t
2677 device_delay_show(struct device *cdev, struct device_attribute *attr,
2678 char *buf)
2680 struct Scsi_Host *shost = class_to_shost(cdev);
2681 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2683 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2685 static DEVICE_ATTR_RO(device_delay);
2688 * fw_queue_depth_show - global credits
2689 * @cdev: pointer to embedded class device
2690 * @attr: ?
2691 * @buf: the buffer returned
2693 * This is firmware queue depth limit
2695 * A sysfs 'read-only' shost attribute.
2697 static ssize_t
2698 fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2699 char *buf)
2701 struct Scsi_Host *shost = class_to_shost(cdev);
2702 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2704 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2706 static DEVICE_ATTR_RO(fw_queue_depth);
2709 * sas_address_show - sas address
2710 * @cdev: pointer to embedded class device
2711 * @attr: ?
2712 * @buf: the buffer returned
2714 * This is the controller sas address
2716 * A sysfs 'read-only' shost attribute.
2718 static ssize_t
2719 host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2720 char *buf)
2723 struct Scsi_Host *shost = class_to_shost(cdev);
2724 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2726 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2727 (unsigned long long)ioc->sas_hba.sas_address);
2729 static DEVICE_ATTR_RO(host_sas_address);
2732 * logging_level_show - logging level
2733 * @cdev: pointer to embedded class device
2734 * @attr: ?
2735 * @buf: the buffer returned
2737 * A sysfs 'read/write' shost attribute.
2739 static ssize_t
2740 logging_level_show(struct device *cdev, struct device_attribute *attr,
2741 char *buf)
2743 struct Scsi_Host *shost = class_to_shost(cdev);
2744 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2746 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2748 static ssize_t
2749 logging_level_store(struct device *cdev, struct device_attribute *attr,
2750 const char *buf, size_t count)
2752 struct Scsi_Host *shost = class_to_shost(cdev);
2753 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2754 int val = 0;
2756 if (sscanf(buf, "%x", &val) != 1)
2757 return -EINVAL;
2759 ioc->logging_level = val;
2760 ioc_info(ioc, "logging_level=%08xh\n",
2761 ioc->logging_level);
2762 return strlen(buf);
2764 static DEVICE_ATTR_RW(logging_level);
2767 * fwfault_debug_show - show/store fwfault_debug
2768 * @cdev: pointer to embedded class device
2769 * @attr: ?
2770 * @buf: the buffer returned
2772 * mpt3sas_fwfault_debug is command line option
2773 * A sysfs 'read/write' shost attribute.
2775 static ssize_t
2776 fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
2777 char *buf)
2779 struct Scsi_Host *shost = class_to_shost(cdev);
2780 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2782 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2784 static ssize_t
2785 fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
2786 const char *buf, size_t count)
2788 struct Scsi_Host *shost = class_to_shost(cdev);
2789 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2790 int val = 0;
2792 if (sscanf(buf, "%d", &val) != 1)
2793 return -EINVAL;
2795 ioc->fwfault_debug = val;
2796 ioc_info(ioc, "fwfault_debug=%d\n",
2797 ioc->fwfault_debug);
2798 return strlen(buf);
2800 static DEVICE_ATTR_RW(fwfault_debug);
2803 * ioc_reset_count_show - ioc reset count
2804 * @cdev: pointer to embedded class device
2805 * @attr: ?
2806 * @buf: the buffer returned
2808 * This is firmware queue depth limit
2810 * A sysfs 'read-only' shost attribute.
2812 static ssize_t
2813 ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2814 char *buf)
2816 struct Scsi_Host *shost = class_to_shost(cdev);
2817 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2819 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
2821 static DEVICE_ATTR_RO(ioc_reset_count);
2824 * reply_queue_count_show - number of reply queues
2825 * @cdev: pointer to embedded class device
2826 * @attr: ?
2827 * @buf: the buffer returned
2829 * This is number of reply queues
2831 * A sysfs 'read-only' shost attribute.
2833 static ssize_t
2834 reply_queue_count_show(struct device *cdev,
2835 struct device_attribute *attr, char *buf)
2837 u8 reply_queue_count;
2838 struct Scsi_Host *shost = class_to_shost(cdev);
2839 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2841 if ((ioc->facts.IOCCapabilities &
2842 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2843 reply_queue_count = ioc->reply_queue_count;
2844 else
2845 reply_queue_count = 1;
2847 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2849 static DEVICE_ATTR_RO(reply_queue_count);
2852 * BRM_status_show - Backup Rail Monitor Status
2853 * @cdev: pointer to embedded class device
2854 * @attr: ?
2855 * @buf: the buffer returned
2857 * This is number of reply queues
2859 * A sysfs 'read-only' shost attribute.
2861 static ssize_t
2862 BRM_status_show(struct device *cdev, struct device_attribute *attr,
2863 char *buf)
2865 struct Scsi_Host *shost = class_to_shost(cdev);
2866 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2867 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2868 Mpi2ConfigReply_t mpi_reply;
2869 u16 backup_rail_monitor_status = 0;
2870 u16 ioc_status;
2871 int sz;
2872 ssize_t rc = 0;
2874 if (!ioc->is_warpdrive) {
2875 ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
2876 __func__);
2877 goto out;
2879 /* pci_access_mutex lock acquired by sysfs show path */
2880 mutex_lock(&ioc->pci_access_mutex);
2881 if (ioc->pci_error_recovery || ioc->remove_host) {
2882 mutex_unlock(&ioc->pci_access_mutex);
2883 return 0;
2886 /* allocate upto GPIOVal 36 entries */
2887 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2888 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2889 if (!io_unit_pg3) {
2890 ioc_err(ioc, "%s: failed allocating memory for iounit_pg3: (%d) bytes\n",
2891 __func__, sz);
2892 goto out;
2895 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2896 0) {
2897 ioc_err(ioc, "%s: failed reading iounit_pg3\n",
2898 __func__);
2899 goto out;
2902 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2903 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2904 ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
2905 __func__, ioc_status);
2906 goto out;
2909 if (io_unit_pg3->GPIOCount < 25) {
2910 ioc_err(ioc, "%s: iounit_pg3->GPIOCount less than 25 entries, detected (%d) entries\n",
2911 __func__, io_unit_pg3->GPIOCount);
2912 goto out;
2915 /* BRM status is in bit zero of GPIOVal[24] */
2916 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2917 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2919 out:
2920 kfree(io_unit_pg3);
2921 mutex_unlock(&ioc->pci_access_mutex);
2922 return rc;
2924 static DEVICE_ATTR_RO(BRM_status);
2926 struct DIAG_BUFFER_START {
2927 __le32 Size;
2928 __le32 DiagVersion;
2929 u8 BufferType;
2930 u8 Reserved[3];
2931 __le32 Reserved1;
2932 __le32 Reserved2;
2933 __le32 Reserved3;
2937 * host_trace_buffer_size_show - host buffer size (trace only)
2938 * @cdev: pointer to embedded class device
2939 * @attr: ?
2940 * @buf: the buffer returned
2942 * A sysfs 'read-only' shost attribute.
2944 static ssize_t
2945 host_trace_buffer_size_show(struct device *cdev,
2946 struct device_attribute *attr, char *buf)
2948 struct Scsi_Host *shost = class_to_shost(cdev);
2949 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2950 u32 size = 0;
2951 struct DIAG_BUFFER_START *request_data;
2953 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2954 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
2955 __func__);
2956 return 0;
2959 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2960 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2961 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
2962 __func__);
2963 return 0;
2966 request_data = (struct DIAG_BUFFER_START *)
2967 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2968 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2969 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
2970 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
2971 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2972 size = le32_to_cpu(request_data->Size);
2974 ioc->ring_buffer_sz = size;
2975 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2977 static DEVICE_ATTR_RO(host_trace_buffer_size);
2980 * host_trace_buffer_show - firmware ring buffer (trace only)
2981 * @cdev: pointer to embedded class device
2982 * @attr: ?
2983 * @buf: the buffer returned
2985 * A sysfs 'read/write' shost attribute.
2987 * You will only be able to read 4k bytes of ring buffer at a time.
2988 * In order to read beyond 4k bytes, you will have to write out the
2989 * offset to the same attribute, it will move the pointer.
2991 static ssize_t
2992 host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2993 char *buf)
2995 struct Scsi_Host *shost = class_to_shost(cdev);
2996 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2997 void *request_data;
2998 u32 size;
3000 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3001 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3002 __func__);
3003 return 0;
3006 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3007 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3008 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3009 __func__);
3010 return 0;
3013 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3014 return 0;
3016 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3017 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3018 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3019 memcpy(buf, request_data, size);
3020 return size;
3023 static ssize_t
3024 host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
3025 const char *buf, size_t count)
3027 struct Scsi_Host *shost = class_to_shost(cdev);
3028 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3029 int val = 0;
3031 if (sscanf(buf, "%d", &val) != 1)
3032 return -EINVAL;
3034 ioc->ring_buffer_offset = val;
3035 return strlen(buf);
3037 static DEVICE_ATTR_RW(host_trace_buffer);
3040 /*****************************************/
3043 * host_trace_buffer_enable_show - firmware ring buffer (trace only)
3044 * @cdev: pointer to embedded class device
3045 * @attr: ?
3046 * @buf: the buffer returned
3048 * A sysfs 'read/write' shost attribute.
3050 * This is a mechnism to post/release host_trace_buffers
3052 static ssize_t
3053 host_trace_buffer_enable_show(struct device *cdev,
3054 struct device_attribute *attr, char *buf)
3056 struct Scsi_Host *shost = class_to_shost(cdev);
3057 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3059 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3060 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3061 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3062 return snprintf(buf, PAGE_SIZE, "off\n");
3063 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3064 MPT3_DIAG_BUFFER_IS_RELEASED))
3065 return snprintf(buf, PAGE_SIZE, "release\n");
3066 else
3067 return snprintf(buf, PAGE_SIZE, "post\n");
3070 static ssize_t
3071 host_trace_buffer_enable_store(struct device *cdev,
3072 struct device_attribute *attr, const char *buf, size_t count)
3074 struct Scsi_Host *shost = class_to_shost(cdev);
3075 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3076 char str[10] = "";
3077 struct mpt3_diag_register diag_register;
3078 u8 issue_reset = 0;
3080 /* don't allow post/release occurr while recovery is active */
3081 if (ioc->shost_recovery || ioc->remove_host ||
3082 ioc->pci_error_recovery || ioc->is_driver_loading)
3083 return -EBUSY;
3085 if (sscanf(buf, "%9s", str) != 1)
3086 return -EINVAL;
3088 if (!strcmp(str, "post")) {
3089 /* exit out if host buffers are already posted */
3090 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3091 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3092 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3093 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3094 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3095 goto out;
3096 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3097 ioc_info(ioc, "posting host trace buffers\n");
3098 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3099 diag_register.requested_buffer_size = (1024 * 1024);
3100 diag_register.unique_id = 0x7075900;
3101 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3102 _ctl_diag_register_2(ioc, &diag_register);
3103 } else if (!strcmp(str, "release")) {
3104 /* exit out if host buffers are already released */
3105 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3106 goto out;
3107 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3108 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3109 goto out;
3110 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3111 MPT3_DIAG_BUFFER_IS_RELEASED))
3112 goto out;
3113 ioc_info(ioc, "releasing host trace buffer\n");
3114 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3115 &issue_reset);
3118 out:
3119 return strlen(buf);
3121 static DEVICE_ATTR_RW(host_trace_buffer_enable);
3123 /*********** diagnostic trigger suppport *********************************/
3126 * diag_trigger_master_show - show the diag_trigger_master attribute
3127 * @cdev: pointer to embedded class device
3128 * @attr: ?
3129 * @buf: the buffer returned
3131 * A sysfs 'read/write' shost attribute.
3133 static ssize_t
3134 diag_trigger_master_show(struct device *cdev,
3135 struct device_attribute *attr, char *buf)
3138 struct Scsi_Host *shost = class_to_shost(cdev);
3139 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3140 unsigned long flags;
3141 ssize_t rc;
3143 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3144 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3145 memcpy(buf, &ioc->diag_trigger_master, rc);
3146 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3147 return rc;
3151 * diag_trigger_master_store - store the diag_trigger_master attribute
3152 * @cdev: pointer to embedded class device
3153 * @attr: ?
3154 * @buf: the buffer returned
3155 * @count: ?
3157 * A sysfs 'read/write' shost attribute.
3159 static ssize_t
3160 diag_trigger_master_store(struct device *cdev,
3161 struct device_attribute *attr, const char *buf, size_t count)
3164 struct Scsi_Host *shost = class_to_shost(cdev);
3165 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3166 unsigned long flags;
3167 ssize_t rc;
3169 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3170 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3171 memset(&ioc->diag_trigger_master, 0,
3172 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3173 memcpy(&ioc->diag_trigger_master, buf, rc);
3174 ioc->diag_trigger_master.MasterData |=
3175 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3176 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3177 return rc;
3179 static DEVICE_ATTR_RW(diag_trigger_master);
3183 * diag_trigger_event_show - show the diag_trigger_event attribute
3184 * @cdev: pointer to embedded class device
3185 * @attr: ?
3186 * @buf: the buffer returned
3188 * A sysfs 'read/write' shost attribute.
3190 static ssize_t
3191 diag_trigger_event_show(struct device *cdev,
3192 struct device_attribute *attr, char *buf)
3194 struct Scsi_Host *shost = class_to_shost(cdev);
3195 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3196 unsigned long flags;
3197 ssize_t rc;
3199 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3200 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3201 memcpy(buf, &ioc->diag_trigger_event, rc);
3202 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3203 return rc;
3207 * diag_trigger_event_store - store the diag_trigger_event attribute
3208 * @cdev: pointer to embedded class device
3209 * @attr: ?
3210 * @buf: the buffer returned
3211 * @count: ?
3213 * A sysfs 'read/write' shost attribute.
3215 static ssize_t
3216 diag_trigger_event_store(struct device *cdev,
3217 struct device_attribute *attr, const char *buf, size_t count)
3220 struct Scsi_Host *shost = class_to_shost(cdev);
3221 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3222 unsigned long flags;
3223 ssize_t sz;
3225 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3226 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3227 memset(&ioc->diag_trigger_event, 0,
3228 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3229 memcpy(&ioc->diag_trigger_event, buf, sz);
3230 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3231 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3232 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3233 return sz;
3235 static DEVICE_ATTR_RW(diag_trigger_event);
3239 * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
3240 * @cdev: pointer to embedded class device
3241 * @attr: ?
3242 * @buf: the buffer returned
3244 * A sysfs 'read/write' shost attribute.
3246 static ssize_t
3247 diag_trigger_scsi_show(struct device *cdev,
3248 struct device_attribute *attr, char *buf)
3250 struct Scsi_Host *shost = class_to_shost(cdev);
3251 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3252 unsigned long flags;
3253 ssize_t rc;
3255 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3256 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3257 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3258 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3259 return rc;
3263 * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
3264 * @cdev: pointer to embedded class device
3265 * @attr: ?
3266 * @buf: the buffer returned
3267 * @count: ?
3269 * A sysfs 'read/write' shost attribute.
3271 static ssize_t
3272 diag_trigger_scsi_store(struct device *cdev,
3273 struct device_attribute *attr, const char *buf, size_t count)
3275 struct Scsi_Host *shost = class_to_shost(cdev);
3276 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3277 unsigned long flags;
3278 ssize_t sz;
3280 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3281 sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
3282 memset(&ioc->diag_trigger_scsi, 0,
3283 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3284 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3285 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3286 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3287 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3288 return sz;
3290 static DEVICE_ATTR_RW(diag_trigger_scsi);
3294 * diag_trigger_scsi_show - show the diag_trigger_mpi attribute
3295 * @cdev: pointer to embedded class device
3296 * @attr: ?
3297 * @buf: the buffer returned
3299 * A sysfs 'read/write' shost attribute.
3301 static ssize_t
3302 diag_trigger_mpi_show(struct device *cdev,
3303 struct device_attribute *attr, char *buf)
3305 struct Scsi_Host *shost = class_to_shost(cdev);
3306 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3307 unsigned long flags;
3308 ssize_t rc;
3310 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3311 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3312 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3313 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3314 return rc;
3318 * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
3319 * @cdev: pointer to embedded class device
3320 * @attr: ?
3321 * @buf: the buffer returned
3322 * @count: ?
3324 * A sysfs 'read/write' shost attribute.
3326 static ssize_t
3327 diag_trigger_mpi_store(struct device *cdev,
3328 struct device_attribute *attr, const char *buf, size_t count)
3330 struct Scsi_Host *shost = class_to_shost(cdev);
3331 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3332 unsigned long flags;
3333 ssize_t sz;
3335 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3336 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3337 memset(&ioc->diag_trigger_mpi, 0,
3338 sizeof(ioc->diag_trigger_mpi));
3339 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3340 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3341 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3342 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3343 return sz;
3346 static DEVICE_ATTR_RW(diag_trigger_mpi);
3348 /*********** diagnostic trigger suppport *** END ****************************/
3350 /*****************************************/
3352 struct device_attribute *mpt3sas_host_attrs[] = {
3353 &dev_attr_version_fw,
3354 &dev_attr_version_bios,
3355 &dev_attr_version_mpi,
3356 &dev_attr_version_product,
3357 &dev_attr_version_nvdata_persistent,
3358 &dev_attr_version_nvdata_default,
3359 &dev_attr_board_name,
3360 &dev_attr_board_assembly,
3361 &dev_attr_board_tracer,
3362 &dev_attr_io_delay,
3363 &dev_attr_device_delay,
3364 &dev_attr_logging_level,
3365 &dev_attr_fwfault_debug,
3366 &dev_attr_fw_queue_depth,
3367 &dev_attr_host_sas_address,
3368 &dev_attr_ioc_reset_count,
3369 &dev_attr_host_trace_buffer_size,
3370 &dev_attr_host_trace_buffer,
3371 &dev_attr_host_trace_buffer_enable,
3372 &dev_attr_reply_queue_count,
3373 &dev_attr_diag_trigger_master,
3374 &dev_attr_diag_trigger_event,
3375 &dev_attr_diag_trigger_scsi,
3376 &dev_attr_diag_trigger_mpi,
3377 &dev_attr_BRM_status,
3378 NULL,
3381 /* device attributes */
3384 * sas_address_show - sas address
3385 * @dev: pointer to embedded class device
3386 * @attr: ?
3387 * @buf: the buffer returned
3389 * This is the sas address for the target
3391 * A sysfs 'read-only' shost attribute.
3393 static ssize_t
3394 sas_address_show(struct device *dev, struct device_attribute *attr,
3395 char *buf)
3397 struct scsi_device *sdev = to_scsi_device(dev);
3398 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3400 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3401 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3403 static DEVICE_ATTR_RO(sas_address);
3406 * sas_device_handle_show - device handle
3407 * @dev: pointer to embedded class device
3408 * @attr: ?
3409 * @buf: the buffer returned
3411 * This is the firmware assigned device handle
3413 * A sysfs 'read-only' shost attribute.
3415 static ssize_t
3416 sas_device_handle_show(struct device *dev, struct device_attribute *attr,
3417 char *buf)
3419 struct scsi_device *sdev = to_scsi_device(dev);
3420 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3422 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3423 sas_device_priv_data->sas_target->handle);
3425 static DEVICE_ATTR_RO(sas_device_handle);
3428 * sas_ncq_io_prio_show - send prioritized io commands to device
3429 * @dev: pointer to embedded device
3430 * @attr: ?
3431 * @buf: the buffer returned
3433 * A sysfs 'read/write' sdev attribute, only works with SATA
3435 static ssize_t
3436 sas_ncq_prio_enable_show(struct device *dev,
3437 struct device_attribute *attr, char *buf)
3439 struct scsi_device *sdev = to_scsi_device(dev);
3440 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3442 return snprintf(buf, PAGE_SIZE, "%d\n",
3443 sas_device_priv_data->ncq_prio_enable);
3446 static ssize_t
3447 sas_ncq_prio_enable_store(struct device *dev,
3448 struct device_attribute *attr,
3449 const char *buf, size_t count)
3451 struct scsi_device *sdev = to_scsi_device(dev);
3452 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3453 bool ncq_prio_enable = 0;
3455 if (kstrtobool(buf, &ncq_prio_enable))
3456 return -EINVAL;
3458 if (!scsih_ncq_prio_supp(sdev))
3459 return -EINVAL;
3461 sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
3462 return strlen(buf);
3464 static DEVICE_ATTR_RW(sas_ncq_prio_enable);
3466 struct device_attribute *mpt3sas_dev_attrs[] = {
3467 &dev_attr_sas_address,
3468 &dev_attr_sas_device_handle,
3469 &dev_attr_sas_ncq_prio_enable,
3470 NULL,
3473 /* file operations table for mpt3ctl device */
3474 static const struct file_operations ctl_fops = {
3475 .owner = THIS_MODULE,
3476 .unlocked_ioctl = _ctl_ioctl,
3477 .poll = _ctl_poll,
3478 .fasync = _ctl_fasync,
3479 #ifdef CONFIG_COMPAT
3480 .compat_ioctl = _ctl_ioctl_compat,
3481 #endif
3484 /* file operations table for mpt2ctl device */
3485 static const struct file_operations ctl_gen2_fops = {
3486 .owner = THIS_MODULE,
3487 .unlocked_ioctl = _ctl_mpt2_ioctl,
3488 .poll = _ctl_poll,
3489 .fasync = _ctl_fasync,
3490 #ifdef CONFIG_COMPAT
3491 .compat_ioctl = _ctl_mpt2_ioctl_compat,
3492 #endif
3495 static struct miscdevice ctl_dev = {
3496 .minor = MPT3SAS_MINOR,
3497 .name = MPT3SAS_DEV_NAME,
3498 .fops = &ctl_fops,
3501 static struct miscdevice gen2_ctl_dev = {
3502 .minor = MPT2SAS_MINOR,
3503 .name = MPT2SAS_DEV_NAME,
3504 .fops = &ctl_gen2_fops,
3508 * mpt3sas_ctl_init - main entry point for ctl.
3509 * @hbas_to_enumerate: ?
3511 void
3512 mpt3sas_ctl_init(ushort hbas_to_enumerate)
3514 async_queue = NULL;
3516 /* Don't register mpt3ctl ioctl device if
3517 * hbas_to_enumarate is one.
3519 if (hbas_to_enumerate != 1)
3520 if (misc_register(&ctl_dev) < 0)
3521 pr_err("%s can't register misc device [minor=%d]\n",
3522 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3524 /* Don't register mpt3ctl ioctl device if
3525 * hbas_to_enumarate is two.
3527 if (hbas_to_enumerate != 2)
3528 if (misc_register(&gen2_ctl_dev) < 0)
3529 pr_err("%s can't register misc device [minor=%d]\n",
3530 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3532 init_waitqueue_head(&ctl_poll_wait);
3536 * mpt3sas_ctl_exit - exit point for ctl
3537 * @hbas_to_enumerate: ?
3539 void
3540 mpt3sas_ctl_exit(ushort hbas_to_enumerate)
3542 struct MPT3SAS_ADAPTER *ioc;
3543 int i;
3545 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3547 /* free memory associated to diag buffers */
3548 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3549 if (!ioc->diag_buffer[i])
3550 continue;
3551 if (!(ioc->diag_buffer_status[i] &
3552 MPT3_DIAG_BUFFER_IS_REGISTERED))
3553 continue;
3554 if ((ioc->diag_buffer_status[i] &
3555 MPT3_DIAG_BUFFER_IS_RELEASED))
3556 continue;
3557 dma_free_coherent(&ioc->pdev->dev,
3558 ioc->diag_buffer_sz[i],
3559 ioc->diag_buffer[i],
3560 ioc->diag_buffer_dma[i]);
3561 ioc->diag_buffer[i] = NULL;
3562 ioc->diag_buffer_status[i] = 0;
3565 kfree(ioc->event_log);
3567 if (hbas_to_enumerate != 1)
3568 misc_deregister(&ctl_dev);
3569 if (hbas_to_enumerate != 2)
3570 misc_deregister(&gen2_ctl_dev);