[SCSI] mpt2sas: Fix for little endian
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / mpt2sas / mpt2sas_ctl.c
blobae55a912baf4657fa913e6bb8fc3c9731a65d644
1 /*
2 * Management Module Support for MPT (Message Passing Technology) based
3 * controllers
5 * This code is based on drivers/scsi/mpt2sas/mpt2_ctl.c
6 * Copyright (C) 2007-2009 LSI Corporation
7 * (mailto:DL-MPTFusionLinux@lsi.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * NO WARRANTY
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 * USA.
45 #include <linux/version.h>
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/slab.h>
51 #include <linux/types.h>
52 #include <linux/pci.h>
53 #include <linux/delay.h>
54 #include <linux/smp_lock.h>
55 #include <linux/compat.h>
56 #include <linux/poll.h>
58 #include <linux/io.h>
59 #include <linux/uaccess.h>
61 #include "mpt2sas_base.h"
62 #include "mpt2sas_ctl.h"
64 static struct fasync_struct *async_queue;
65 static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
67 static int _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type,
68 u8 *issue_reset);
70 /**
71 * enum block_state - blocking state
72 * @NON_BLOCKING: non blocking
73 * @BLOCKING: blocking
75 * These states are for ioctls that need to wait for a response
76 * from firmware, so they probably require sleep.
78 enum block_state {
79 NON_BLOCKING,
80 BLOCKING,
83 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
84 /**
85 * _ctl_display_some_debug - debug routine
86 * @ioc: per adapter object
87 * @smid: system request message index
88 * @calling_function_name: string pass from calling function
89 * @mpi_reply: reply message frame
90 * Context: none.
92 * Function for displaying debug info helpfull when debugging issues
93 * in this module.
95 static void
96 _ctl_display_some_debug(struct MPT2SAS_ADAPTER *ioc, u16 smid,
97 char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
99 Mpi2ConfigRequest_t *mpi_request;
100 char *desc = NULL;
102 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
103 return;
105 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
106 switch (mpi_request->Function) {
107 case MPI2_FUNCTION_SCSI_IO_REQUEST:
109 Mpi2SCSIIORequest_t *scsi_request =
110 (Mpi2SCSIIORequest_t *)mpi_request;
112 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
113 "scsi_io, cmd(0x%02x), cdb_len(%d)",
114 scsi_request->CDB.CDB32[0],
115 le16_to_cpu(scsi_request->IoFlags) & 0xF);
116 desc = ioc->tmp_string;
117 break;
119 case MPI2_FUNCTION_SCSI_TASK_MGMT:
120 desc = "task_mgmt";
121 break;
122 case MPI2_FUNCTION_IOC_INIT:
123 desc = "ioc_init";
124 break;
125 case MPI2_FUNCTION_IOC_FACTS:
126 desc = "ioc_facts";
127 break;
128 case MPI2_FUNCTION_CONFIG:
130 Mpi2ConfigRequest_t *config_request =
131 (Mpi2ConfigRequest_t *)mpi_request;
133 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
134 "config, type(0x%02x), ext_type(0x%02x), number(%d)",
135 (config_request->Header.PageType &
136 MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
137 config_request->Header.PageNumber);
138 desc = ioc->tmp_string;
139 break;
141 case MPI2_FUNCTION_PORT_FACTS:
142 desc = "port_facts";
143 break;
144 case MPI2_FUNCTION_PORT_ENABLE:
145 desc = "port_enable";
146 break;
147 case MPI2_FUNCTION_EVENT_NOTIFICATION:
148 desc = "event_notification";
149 break;
150 case MPI2_FUNCTION_FW_DOWNLOAD:
151 desc = "fw_download";
152 break;
153 case MPI2_FUNCTION_FW_UPLOAD:
154 desc = "fw_upload";
155 break;
156 case MPI2_FUNCTION_RAID_ACTION:
157 desc = "raid_action";
158 break;
159 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
161 Mpi2SCSIIORequest_t *scsi_request =
162 (Mpi2SCSIIORequest_t *)mpi_request;
164 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
165 "raid_pass, cmd(0x%02x), cdb_len(%d)",
166 scsi_request->CDB.CDB32[0],
167 le16_to_cpu(scsi_request->IoFlags) & 0xF);
168 desc = ioc->tmp_string;
169 break;
171 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
172 desc = "sas_iounit_cntl";
173 break;
174 case MPI2_FUNCTION_SATA_PASSTHROUGH:
175 desc = "sata_pass";
176 break;
177 case MPI2_FUNCTION_DIAG_BUFFER_POST:
178 desc = "diag_buffer_post";
179 break;
180 case MPI2_FUNCTION_DIAG_RELEASE:
181 desc = "diag_release";
182 break;
183 case MPI2_FUNCTION_SMP_PASSTHROUGH:
184 desc = "smp_passthrough";
185 break;
188 if (!desc)
189 return;
191 printk(MPT2SAS_DEBUG_FMT "%s: %s, smid(%d)\n",
192 ioc->name, calling_function_name, desc, smid);
194 if (!mpi_reply)
195 return;
197 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
198 printk(MPT2SAS_DEBUG_FMT
199 "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
200 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
201 le32_to_cpu(mpi_reply->IOCLogInfo));
203 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
204 mpi_request->Function ==
205 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
206 Mpi2SCSIIOReply_t *scsi_reply =
207 (Mpi2SCSIIOReply_t *)mpi_reply;
208 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
209 printk(MPT2SAS_DEBUG_FMT
210 "\tscsi_state(0x%02x), scsi_status"
211 "(0x%02x)\n", ioc->name,
212 scsi_reply->SCSIState,
213 scsi_reply->SCSIStatus);
216 #endif
219 * mpt2sas_ctl_done - ctl module completion routine
220 * @ioc: per adapter object
221 * @smid: system request message index
222 * @msix_index: MSIX table index supplied by the OS
223 * @reply: reply message frame(lower 32bit addr)
224 * Context: none.
226 * The callback handler when using ioc->ctl_cb_idx.
228 * Return 1 meaning mf should be freed from _base_interrupt
229 * 0 means the mf is freed from this function.
232 mpt2sas_ctl_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
233 u32 reply)
235 MPI2DefaultReply_t *mpi_reply;
237 if (ioc->ctl_cmds.status == MPT2_CMD_NOT_USED)
238 return 1;
239 if (ioc->ctl_cmds.smid != smid)
240 return 1;
241 ioc->ctl_cmds.status |= MPT2_CMD_COMPLETE;
242 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
243 if (mpi_reply) {
244 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
245 ioc->ctl_cmds.status |= MPT2_CMD_REPLY_VALID;
247 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
248 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
249 #endif
250 ioc->ctl_cmds.status &= ~MPT2_CMD_PENDING;
251 complete(&ioc->ctl_cmds.done);
252 return 1;
256 * _ctl_check_event_type - determines when an event needs logging
257 * @ioc: per adapter object
258 * @event: firmware event
260 * The bitmask in ioc->event_type[] indicates which events should be
261 * be saved in the driver event_log. This bitmask is set by application.
263 * Returns 1 when event should be captured, or zero means no match.
265 static int
266 _ctl_check_event_type(struct MPT2SAS_ADAPTER *ioc, u16 event)
268 u16 i;
269 u32 desired_event;
271 if (event >= 128 || !event || !ioc->event_log)
272 return 0;
274 desired_event = (1 << (event % 32));
275 if (!desired_event)
276 desired_event = 1;
277 i = event / 32;
278 return desired_event & ioc->event_type[i];
282 * mpt2sas_ctl_add_to_event_log - add event
283 * @ioc: per adapter object
284 * @mpi_reply: reply message frame
286 * Return nothing.
288 void
289 mpt2sas_ctl_add_to_event_log(struct MPT2SAS_ADAPTER *ioc,
290 Mpi2EventNotificationReply_t *mpi_reply)
292 struct MPT2_IOCTL_EVENTS *event_log;
293 u16 event;
294 int i;
295 u32 sz, event_data_sz;
296 u8 send_aen = 0;
298 if (!ioc->event_log)
299 return;
301 event = le16_to_cpu(mpi_reply->Event);
303 if (_ctl_check_event_type(ioc, event)) {
305 /* insert entry into circular event_log */
306 i = ioc->event_context % MPT2SAS_CTL_EVENT_LOG_SIZE;
307 event_log = ioc->event_log;
308 event_log[i].event = event;
309 event_log[i].context = ioc->event_context++;
311 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
312 sz = min_t(u32, event_data_sz, MPT2_EVENT_DATA_SIZE);
313 memset(event_log[i].data, 0, MPT2_EVENT_DATA_SIZE);
314 memcpy(event_log[i].data, mpi_reply->EventData, sz);
315 send_aen = 1;
318 /* This aen_event_read_flag flag is set until the
319 * application has read the event log.
320 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
322 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
323 (send_aen && !ioc->aen_event_read_flag)) {
324 ioc->aen_event_read_flag = 1;
325 wake_up_interruptible(&ctl_poll_wait);
326 if (async_queue)
327 kill_fasync(&async_queue, SIGIO, POLL_IN);
332 * mpt2sas_ctl_event_callback - firmware event handler (called at ISR time)
333 * @ioc: per adapter object
334 * @msix_index: MSIX table index supplied by the OS
335 * @reply: reply message frame(lower 32bit addr)
336 * Context: interrupt.
338 * This function merely adds a new work task into ioc->firmware_event_thread.
339 * The tasks are worked from _firmware_event_work in user context.
341 * Return 1 meaning mf should be freed from _base_interrupt
342 * 0 means the mf is freed from this function.
345 mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
346 u32 reply)
348 Mpi2EventNotificationReply_t *mpi_reply;
350 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
351 mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
352 return 1;
356 * _ctl_verify_adapter - validates ioc_number passed from application
357 * @ioc: per adapter object
358 * @iocpp: The ioc pointer is returned in this.
360 * Return (-1) means error, else ioc_number.
362 static int
363 _ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
365 struct MPT2SAS_ADAPTER *ioc;
367 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
368 if (ioc->id != ioc_number)
369 continue;
370 *iocpp = ioc;
371 return ioc_number;
373 *iocpp = NULL;
374 return -1;
378 * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
379 * @ioc: per adapter object
380 * @reset_phase: phase
382 * The handler for doing any required cleanup or initialization.
384 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
385 * MPT2_IOC_DONE_RESET
387 void
388 mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
390 int i;
391 u8 issue_reset;
393 switch (reset_phase) {
394 case MPT2_IOC_PRE_RESET:
395 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
396 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
397 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
398 if (!(ioc->diag_buffer_status[i] &
399 MPT2_DIAG_BUFFER_IS_REGISTERED))
400 continue;
401 if ((ioc->diag_buffer_status[i] &
402 MPT2_DIAG_BUFFER_IS_RELEASED))
403 continue;
404 _ctl_send_release(ioc, i, &issue_reset);
406 break;
407 case MPT2_IOC_AFTER_RESET:
408 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
409 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
410 if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
411 ioc->ctl_cmds.status |= MPT2_CMD_RESET;
412 mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
413 complete(&ioc->ctl_cmds.done);
415 break;
416 case MPT2_IOC_DONE_RESET:
417 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
418 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
420 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
421 if (!(ioc->diag_buffer_status[i] &
422 MPT2_DIAG_BUFFER_IS_REGISTERED))
423 continue;
424 if ((ioc->diag_buffer_status[i] &
425 MPT2_DIAG_BUFFER_IS_RELEASED))
426 continue;
427 ioc->diag_buffer_status[i] |=
428 MPT2_DIAG_BUFFER_IS_DIAG_RESET;
430 break;
435 * _ctl_fasync -
436 * @fd -
437 * @filep -
438 * @mode -
440 * Called when application request fasyn callback handler.
442 static int
443 _ctl_fasync(int fd, struct file *filep, int mode)
445 return fasync_helper(fd, filep, mode, &async_queue);
449 * _ctl_release -
450 * @inode -
451 * @filep -
453 * Called when application releases the fasyn callback handler.
455 static int
456 _ctl_release(struct inode *inode, struct file *filep)
458 return fasync_helper(-1, filep, 0, &async_queue);
462 * _ctl_poll -
463 * @file -
464 * @wait -
467 static unsigned int
468 _ctl_poll(struct file *filep, poll_table *wait)
470 struct MPT2SAS_ADAPTER *ioc;
472 poll_wait(filep, &ctl_poll_wait, wait);
474 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
475 if (ioc->aen_event_read_flag)
476 return POLLIN | POLLRDNORM;
478 return 0;
482 * _ctl_set_task_mid - assign an active smid to tm request
483 * @ioc: per adapter object
484 * @karg - (struct mpt2_ioctl_command)
485 * @tm_request - pointer to mf from user space
487 * Returns 0 when an smid if found, else fail.
488 * during failure, the reply frame is filled.
490 static int
491 _ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
492 Mpi2SCSITaskManagementRequest_t *tm_request)
494 u8 found = 0;
495 u16 i;
496 u16 handle;
497 struct scsi_cmnd *scmd;
498 struct MPT2SAS_DEVICE *priv_data;
499 unsigned long flags;
500 Mpi2SCSITaskManagementReply_t *tm_reply;
501 u32 sz;
502 u32 lun;
503 char *desc = NULL;
505 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
506 desc = "abort_task";
507 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
508 desc = "query_task";
509 else
510 return 0;
512 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
514 handle = le16_to_cpu(tm_request->DevHandle);
515 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
516 for (i = ioc->scsiio_depth; i && !found; i--) {
517 scmd = ioc->scsi_lookup[i - 1].scmd;
518 if (scmd == NULL || scmd->device == NULL ||
519 scmd->device->hostdata == NULL)
520 continue;
521 if (lun != scmd->device->lun)
522 continue;
523 priv_data = scmd->device->hostdata;
524 if (priv_data->sas_target == NULL)
525 continue;
526 if (priv_data->sas_target->handle != handle)
527 continue;
528 tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
529 found = 1;
531 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
533 if (!found) {
534 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
535 "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
536 desc, le16_to_cpu(tm_request->DevHandle), lun));
537 tm_reply = ioc->ctl_cmds.reply;
538 tm_reply->DevHandle = tm_request->DevHandle;
539 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
540 tm_reply->TaskType = tm_request->TaskType;
541 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
542 tm_reply->VP_ID = tm_request->VP_ID;
543 tm_reply->VF_ID = tm_request->VF_ID;
544 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
545 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
546 sz))
547 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
548 __LINE__, __func__);
549 return 1;
552 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
553 "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
554 desc, le16_to_cpu(tm_request->DevHandle), lun,
555 le16_to_cpu(tm_request->TaskMID)));
556 return 0;
560 * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
561 * @ioc: per adapter object
562 * @karg - (struct mpt2_ioctl_command)
563 * @mf - pointer to mf in user space
564 * @state - NON_BLOCKING or BLOCKING
566 static long
567 _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc,
568 struct mpt2_ioctl_command karg, void __user *mf, enum block_state state)
570 MPI2RequestHeader_t *mpi_request;
571 MPI2DefaultReply_t *mpi_reply;
572 u32 ioc_state;
573 u16 ioc_status;
574 u16 smid;
575 unsigned long timeout, timeleft;
576 u8 issue_reset;
577 u32 sz;
578 void *psge;
579 void *priv_sense = NULL;
580 void *data_out = NULL;
581 dma_addr_t data_out_dma;
582 size_t data_out_sz = 0;
583 void *data_in = NULL;
584 dma_addr_t data_in_dma;
585 size_t data_in_sz = 0;
586 u32 sgl_flags;
587 long ret;
588 u16 wait_state_count;
590 issue_reset = 0;
592 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
593 return -EAGAIN;
594 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
595 return -ERESTARTSYS;
597 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
598 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
599 ioc->name, __func__);
600 ret = -EAGAIN;
601 goto out;
604 wait_state_count = 0;
605 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
606 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
607 if (wait_state_count++ == 10) {
608 printk(MPT2SAS_ERR_FMT
609 "%s: failed due to ioc not operational\n",
610 ioc->name, __func__);
611 ret = -EFAULT;
612 goto out;
614 ssleep(1);
615 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
616 printk(MPT2SAS_INFO_FMT "%s: waiting for "
617 "operational state(count=%d)\n", ioc->name,
618 __func__, wait_state_count);
620 if (wait_state_count)
621 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
622 ioc->name, __func__);
624 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
625 if (!smid) {
626 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
627 ioc->name, __func__);
628 ret = -EAGAIN;
629 goto out;
632 ret = 0;
633 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
634 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
635 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
636 ioc->ctl_cmds.smid = smid;
637 data_out_sz = karg.data_out_size;
638 data_in_sz = karg.data_in_size;
640 /* copy in request message frame from user */
641 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
642 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
643 __func__);
644 ret = -EFAULT;
645 mpt2sas_base_free_smid(ioc, smid);
646 goto out;
649 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
650 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
651 if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
652 le16_to_cpu(mpi_request->FunctionDependent1) >
653 ioc->facts.MaxDevHandle) {
654 ret = -EINVAL;
655 mpt2sas_base_free_smid(ioc, smid);
656 goto out;
660 /* obtain dma-able memory for data transfer */
661 if (data_out_sz) /* WRITE */ {
662 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
663 &data_out_dma);
664 if (!data_out) {
665 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
666 __LINE__, __func__);
667 ret = -ENOMEM;
668 mpt2sas_base_free_smid(ioc, smid);
669 goto out;
671 if (copy_from_user(data_out, karg.data_out_buf_ptr,
672 data_out_sz)) {
673 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
674 __LINE__, __func__);
675 ret = -EFAULT;
676 mpt2sas_base_free_smid(ioc, smid);
677 goto out;
681 if (data_in_sz) /* READ */ {
682 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
683 &data_in_dma);
684 if (!data_in) {
685 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
686 __LINE__, __func__);
687 ret = -ENOMEM;
688 mpt2sas_base_free_smid(ioc, smid);
689 goto out;
693 /* add scatter gather elements */
694 psge = (void *)mpi_request + (karg.data_sge_offset*4);
696 if (!data_out_sz && !data_in_sz) {
697 mpt2sas_base_build_zero_len_sge(ioc, psge);
698 } else if (data_out_sz && data_in_sz) {
699 /* WRITE sgel first */
700 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
701 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
702 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
703 ioc->base_add_sg_single(psge, sgl_flags |
704 data_out_sz, data_out_dma);
706 /* incr sgel */
707 psge += ioc->sge_size;
709 /* READ sgel last */
710 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
711 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
712 MPI2_SGE_FLAGS_END_OF_LIST);
713 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
714 ioc->base_add_sg_single(psge, sgl_flags |
715 data_in_sz, data_in_dma);
716 } else if (data_out_sz) /* WRITE */ {
717 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
718 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
719 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
720 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
721 ioc->base_add_sg_single(psge, sgl_flags |
722 data_out_sz, data_out_dma);
723 } else if (data_in_sz) /* READ */ {
724 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
725 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
726 MPI2_SGE_FLAGS_END_OF_LIST);
727 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
728 ioc->base_add_sg_single(psge, sgl_flags |
729 data_in_sz, data_in_dma);
732 /* send command to firmware */
733 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
734 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
735 #endif
737 switch (mpi_request->Function) {
738 case MPI2_FUNCTION_SCSI_IO_REQUEST:
739 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
741 Mpi2SCSIIORequest_t *scsiio_request =
742 (Mpi2SCSIIORequest_t *)mpi_request;
743 scsiio_request->SenseBufferLowAddress =
744 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
745 priv_sense = mpt2sas_base_get_sense_buffer(ioc, smid);
746 memset(priv_sense, 0, SCSI_SENSE_BUFFERSIZE);
747 mpt2sas_base_put_smid_scsi_io(ioc, smid,
748 le16_to_cpu(mpi_request->FunctionDependent1));
749 break;
751 case MPI2_FUNCTION_SCSI_TASK_MGMT:
753 Mpi2SCSITaskManagementRequest_t *tm_request =
754 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
756 if (tm_request->TaskType ==
757 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
758 tm_request->TaskType ==
759 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
760 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
761 mpt2sas_base_free_smid(ioc, smid);
762 goto out;
766 mutex_lock(&ioc->tm_cmds.mutex);
767 mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
768 tm_request->DevHandle));
769 mpt2sas_base_put_smid_hi_priority(ioc, smid);
770 break;
772 case MPI2_FUNCTION_SMP_PASSTHROUGH:
774 Mpi2SmpPassthroughRequest_t *smp_request =
775 (Mpi2SmpPassthroughRequest_t *)mpi_request;
776 u8 *data;
778 /* ioc determines which port to use */
779 smp_request->PhysicalPort = 0xFF;
780 if (smp_request->PassthroughFlags &
781 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
782 data = (u8 *)&smp_request->SGL;
783 else
784 data = data_out;
786 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
787 ioc->ioc_link_reset_in_progress = 1;
788 ioc->ignore_loginfos = 1;
790 mpt2sas_base_put_smid_default(ioc, smid);
791 break;
793 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
795 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
796 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
798 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
799 || sasiounit_request->Operation ==
800 MPI2_SAS_OP_PHY_LINK_RESET) {
801 ioc->ioc_link_reset_in_progress = 1;
802 ioc->ignore_loginfos = 1;
804 mpt2sas_base_put_smid_default(ioc, smid);
805 break;
807 default:
808 mpt2sas_base_put_smid_default(ioc, smid);
809 break;
812 if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
813 timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
814 else
815 timeout = karg.timeout;
816 init_completion(&ioc->ctl_cmds.done);
817 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
818 timeout*HZ);
819 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
820 Mpi2SCSITaskManagementRequest_t *tm_request =
821 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
822 mutex_unlock(&ioc->tm_cmds.mutex);
823 mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
824 tm_request->DevHandle));
825 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
826 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
827 ioc->ioc_link_reset_in_progress) {
828 ioc->ioc_link_reset_in_progress = 0;
829 ioc->ignore_loginfos = 0;
831 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
832 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
833 __func__);
834 _debug_dump_mf(mpi_request, karg.data_sge_offset);
835 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
836 issue_reset = 1;
837 goto issue_host_reset;
840 mpi_reply = ioc->ctl_cmds.reply;
841 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
843 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
844 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
845 (ioc->logging_level & MPT_DEBUG_TM)) {
846 Mpi2SCSITaskManagementReply_t *tm_reply =
847 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
849 printk(MPT2SAS_DEBUG_FMT "TASK_MGMT: "
850 "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
851 "TerminationCount(0x%08x)\n", ioc->name,
852 le16_to_cpu(tm_reply->IOCStatus),
853 le32_to_cpu(tm_reply->IOCLogInfo),
854 le32_to_cpu(tm_reply->TerminationCount));
856 #endif
857 /* copy out xdata to user */
858 if (data_in_sz) {
859 if (copy_to_user(karg.data_in_buf_ptr, data_in,
860 data_in_sz)) {
861 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
862 __LINE__, __func__);
863 ret = -ENODATA;
864 goto out;
868 /* copy out reply message frame to user */
869 if (karg.max_reply_bytes) {
870 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
871 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
872 sz)) {
873 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
874 __LINE__, __func__);
875 ret = -ENODATA;
876 goto out;
880 /* copy out sense to user */
881 if (karg.max_sense_bytes && (mpi_request->Function ==
882 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
883 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
884 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
885 if (copy_to_user(karg.sense_data_ptr, priv_sense, sz)) {
886 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
887 __LINE__, __func__);
888 ret = -ENODATA;
889 goto out;
893 issue_host_reset:
894 if (issue_reset) {
895 ret = -ENODATA;
896 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
897 mpi_request->Function ==
898 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
899 printk(MPT2SAS_INFO_FMT "issue target reset: handle "
900 "= (0x%04x)\n", ioc->name,
901 le16_to_cpu(mpi_request->FunctionDependent1));
902 mpt2sas_halt_firmware(ioc);
903 mutex_lock(&ioc->tm_cmds.mutex);
904 mpt2sas_scsih_issue_tm(ioc,
905 le16_to_cpu(mpi_request->FunctionDependent1), 0,
906 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
907 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
908 mutex_unlock(&ioc->tm_cmds.mutex);
909 } else
910 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
911 FORCE_BIG_HAMMER);
914 out:
916 /* free memory associated with sg buffers */
917 if (data_in)
918 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
919 data_in_dma);
921 if (data_out)
922 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
923 data_out_dma);
925 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
926 mutex_unlock(&ioc->ctl_cmds.mutex);
927 return ret;
931 * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
932 * @arg - user space buffer containing ioctl content
934 static long
935 _ctl_getiocinfo(void __user *arg)
937 struct mpt2_ioctl_iocinfo karg;
938 struct MPT2SAS_ADAPTER *ioc;
939 u8 revision;
941 if (copy_from_user(&karg, arg, sizeof(karg))) {
942 printk(KERN_ERR "failure at %s:%d/%s()!\n",
943 __FILE__, __LINE__, __func__);
944 return -EFAULT;
946 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
947 return -ENODEV;
949 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
950 __func__));
952 memset(&karg, 0 , sizeof(karg));
953 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
954 if (ioc->pfacts)
955 karg.port_number = ioc->pfacts[0].PortNumber;
956 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
957 karg.hw_rev = revision;
958 karg.pci_id = ioc->pdev->device;
959 karg.subsystem_device = ioc->pdev->subsystem_device;
960 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
961 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
962 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
963 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
964 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
965 karg.firmware_version = ioc->facts.FWVersion.Word;
966 strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
967 strcat(karg.driver_version, "-");
968 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
969 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
971 if (copy_to_user(arg, &karg, sizeof(karg))) {
972 printk(KERN_ERR "failure at %s:%d/%s()!\n",
973 __FILE__, __LINE__, __func__);
974 return -EFAULT;
976 return 0;
980 * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
981 * @arg - user space buffer containing ioctl content
983 static long
984 _ctl_eventquery(void __user *arg)
986 struct mpt2_ioctl_eventquery karg;
987 struct MPT2SAS_ADAPTER *ioc;
989 if (copy_from_user(&karg, arg, sizeof(karg))) {
990 printk(KERN_ERR "failure at %s:%d/%s()!\n",
991 __FILE__, __LINE__, __func__);
992 return -EFAULT;
994 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
995 return -ENODEV;
997 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
998 __func__));
1000 karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
1001 memcpy(karg.event_types, ioc->event_type,
1002 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1004 if (copy_to_user(arg, &karg, sizeof(karg))) {
1005 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1006 __FILE__, __LINE__, __func__);
1007 return -EFAULT;
1009 return 0;
1013 * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1014 * @arg - user space buffer containing ioctl content
1016 static long
1017 _ctl_eventenable(void __user *arg)
1019 struct mpt2_ioctl_eventenable karg;
1020 struct MPT2SAS_ADAPTER *ioc;
1022 if (copy_from_user(&karg, arg, sizeof(karg))) {
1023 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1024 __FILE__, __LINE__, __func__);
1025 return -EFAULT;
1027 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1028 return -ENODEV;
1030 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1031 __func__));
1033 if (ioc->event_log)
1034 return 0;
1035 memcpy(ioc->event_type, karg.event_types,
1036 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1037 mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1039 /* initialize event_log */
1040 ioc->event_context = 0;
1041 ioc->aen_event_read_flag = 0;
1042 ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1043 sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1044 if (!ioc->event_log) {
1045 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1046 __FILE__, __LINE__, __func__);
1047 return -ENOMEM;
1049 return 0;
1053 * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1054 * @arg - user space buffer containing ioctl content
1056 static long
1057 _ctl_eventreport(void __user *arg)
1059 struct mpt2_ioctl_eventreport karg;
1060 struct MPT2SAS_ADAPTER *ioc;
1061 u32 number_bytes, max_events, max;
1062 struct mpt2_ioctl_eventreport __user *uarg = arg;
1064 if (copy_from_user(&karg, arg, sizeof(karg))) {
1065 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1066 __FILE__, __LINE__, __func__);
1067 return -EFAULT;
1069 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1070 return -ENODEV;
1072 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1073 __func__));
1075 number_bytes = karg.hdr.max_data_size -
1076 sizeof(struct mpt2_ioctl_header);
1077 max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1078 max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1080 /* If fewer than 1 event is requested, there must have
1081 * been some type of error.
1083 if (!max || !ioc->event_log)
1084 return -ENODATA;
1086 number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1087 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1088 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1089 __FILE__, __LINE__, __func__);
1090 return -EFAULT;
1093 /* reset flag so SIGIO can restart */
1094 ioc->aen_event_read_flag = 0;
1095 return 0;
1099 * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1100 * @arg - user space buffer containing ioctl content
1102 static long
1103 _ctl_do_reset(void __user *arg)
1105 struct mpt2_ioctl_diag_reset karg;
1106 struct MPT2SAS_ADAPTER *ioc;
1107 int retval;
1109 if (copy_from_user(&karg, arg, sizeof(karg))) {
1110 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1111 __FILE__, __LINE__, __func__);
1112 return -EFAULT;
1114 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1115 return -ENODEV;
1117 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1118 __func__));
1120 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1121 FORCE_BIG_HAMMER);
1122 printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1123 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1124 return 0;
1128 * _ctl_btdh_search_sas_device - searching for sas device
1129 * @ioc: per adapter object
1130 * @btdh: btdh ioctl payload
1132 static int
1133 _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1134 struct mpt2_ioctl_btdh_mapping *btdh)
1136 struct _sas_device *sas_device;
1137 unsigned long flags;
1138 int rc = 0;
1140 if (list_empty(&ioc->sas_device_list))
1141 return rc;
1143 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1144 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1145 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1146 btdh->handle == sas_device->handle) {
1147 btdh->bus = sas_device->channel;
1148 btdh->id = sas_device->id;
1149 rc = 1;
1150 goto out;
1151 } else if (btdh->bus == sas_device->channel && btdh->id ==
1152 sas_device->id && btdh->handle == 0xFFFF) {
1153 btdh->handle = sas_device->handle;
1154 rc = 1;
1155 goto out;
1158 out:
1159 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1160 return rc;
1164 * _ctl_btdh_search_raid_device - searching for raid device
1165 * @ioc: per adapter object
1166 * @btdh: btdh ioctl payload
1168 static int
1169 _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1170 struct mpt2_ioctl_btdh_mapping *btdh)
1172 struct _raid_device *raid_device;
1173 unsigned long flags;
1174 int rc = 0;
1176 if (list_empty(&ioc->raid_device_list))
1177 return rc;
1179 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1180 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1181 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1182 btdh->handle == raid_device->handle) {
1183 btdh->bus = raid_device->channel;
1184 btdh->id = raid_device->id;
1185 rc = 1;
1186 goto out;
1187 } else if (btdh->bus == raid_device->channel && btdh->id ==
1188 raid_device->id && btdh->handle == 0xFFFF) {
1189 btdh->handle = raid_device->handle;
1190 rc = 1;
1191 goto out;
1194 out:
1195 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1196 return rc;
1200 * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1201 * @arg - user space buffer containing ioctl content
1203 static long
1204 _ctl_btdh_mapping(void __user *arg)
1206 struct mpt2_ioctl_btdh_mapping karg;
1207 struct MPT2SAS_ADAPTER *ioc;
1208 int rc;
1210 if (copy_from_user(&karg, arg, sizeof(karg))) {
1211 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1212 __FILE__, __LINE__, __func__);
1213 return -EFAULT;
1215 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1216 return -ENODEV;
1218 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1219 __func__));
1221 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1222 if (!rc)
1223 _ctl_btdh_search_raid_device(ioc, &karg);
1225 if (copy_to_user(arg, &karg, sizeof(karg))) {
1226 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1227 __FILE__, __LINE__, __func__);
1228 return -EFAULT;
1230 return 0;
1234 * _ctl_diag_capability - return diag buffer capability
1235 * @ioc: per adapter object
1236 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1238 * returns 1 when diag buffer support is enabled in firmware
1240 static u8
1241 _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1243 u8 rc = 0;
1245 switch (buffer_type) {
1246 case MPI2_DIAG_BUF_TYPE_TRACE:
1247 if (ioc->facts.IOCCapabilities &
1248 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1249 rc = 1;
1250 break;
1251 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1252 if (ioc->facts.IOCCapabilities &
1253 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1254 rc = 1;
1255 break;
1256 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1257 if (ioc->facts.IOCCapabilities &
1258 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1259 rc = 1;
1262 return rc;
1266 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1267 * @ioc: per adapter object
1268 * @diag_register: the diag_register struct passed in from user space
1271 static long
1272 _ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
1273 struct mpt2_diag_register *diag_register)
1275 int rc, i;
1276 void *request_data = NULL;
1277 dma_addr_t request_data_dma;
1278 u32 request_data_sz = 0;
1279 Mpi2DiagBufferPostRequest_t *mpi_request;
1280 Mpi2DiagBufferPostReply_t *mpi_reply;
1281 u8 buffer_type;
1282 unsigned long timeleft;
1283 u16 smid;
1284 u16 ioc_status;
1285 u8 issue_reset = 0;
1287 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1288 __func__));
1290 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1291 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1292 ioc->name, __func__);
1293 rc = -EAGAIN;
1294 goto out;
1297 buffer_type = diag_register->buffer_type;
1298 if (!_ctl_diag_capability(ioc, buffer_type)) {
1299 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1300 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1301 return -EPERM;
1304 if (ioc->diag_buffer_status[buffer_type] &
1305 MPT2_DIAG_BUFFER_IS_REGISTERED) {
1306 printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1307 "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1308 buffer_type);
1309 return -EINVAL;
1312 if (diag_register->requested_buffer_size % 4) {
1313 printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1314 "is not 4 byte aligned\n", ioc->name, __func__);
1315 return -EINVAL;
1318 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1319 if (!smid) {
1320 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1321 ioc->name, __func__);
1322 rc = -EAGAIN;
1323 goto out;
1326 rc = 0;
1327 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1328 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1329 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1330 ioc->ctl_cmds.smid = smid;
1332 request_data = ioc->diag_buffer[buffer_type];
1333 request_data_sz = diag_register->requested_buffer_size;
1334 ioc->unique_id[buffer_type] = diag_register->unique_id;
1335 ioc->diag_buffer_status[buffer_type] = 0;
1336 memcpy(ioc->product_specific[buffer_type],
1337 diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
1338 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1340 if (request_data) {
1341 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1342 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1343 pci_free_consistent(ioc->pdev,
1344 ioc->diag_buffer_sz[buffer_type],
1345 request_data, request_data_dma);
1346 request_data = NULL;
1350 if (request_data == NULL) {
1351 ioc->diag_buffer_sz[buffer_type] = 0;
1352 ioc->diag_buffer_dma[buffer_type] = 0;
1353 request_data = pci_alloc_consistent(
1354 ioc->pdev, request_data_sz, &request_data_dma);
1355 if (request_data == NULL) {
1356 printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1357 " for diag buffers, requested size(%d)\n",
1358 ioc->name, __func__, request_data_sz);
1359 mpt2sas_base_free_smid(ioc, smid);
1360 return -ENOMEM;
1362 ioc->diag_buffer[buffer_type] = request_data;
1363 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1364 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1367 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1368 mpi_request->BufferType = diag_register->buffer_type;
1369 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1370 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1371 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1372 mpi_request->VF_ID = 0; /* TODO */
1373 mpi_request->VP_ID = 0;
1375 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: diag_buffer(0x%p), "
1376 "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1377 (unsigned long long)request_data_dma,
1378 le32_to_cpu(mpi_request->BufferLength)));
1380 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1381 mpi_request->ProductSpecific[i] =
1382 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1384 mpt2sas_base_put_smid_default(ioc, smid);
1385 init_completion(&ioc->ctl_cmds.done);
1386 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1387 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1389 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1390 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1391 __func__);
1392 _debug_dump_mf(mpi_request,
1393 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1394 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1395 issue_reset = 1;
1396 goto issue_host_reset;
1399 /* process the completed Reply Message Frame */
1400 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1401 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1402 ioc->name, __func__);
1403 rc = -EFAULT;
1404 goto out;
1407 mpi_reply = ioc->ctl_cmds.reply;
1408 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1410 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1411 ioc->diag_buffer_status[buffer_type] |=
1412 MPT2_DIAG_BUFFER_IS_REGISTERED;
1413 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1414 ioc->name, __func__));
1415 } else {
1416 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1417 "log_info(0x%08x)\n", ioc->name, __func__,
1418 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1419 rc = -EFAULT;
1422 issue_host_reset:
1423 if (issue_reset)
1424 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1425 FORCE_BIG_HAMMER);
1427 out:
1429 if (rc && request_data)
1430 pci_free_consistent(ioc->pdev, request_data_sz,
1431 request_data, request_data_dma);
1433 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1434 return rc;
1438 * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
1439 * @ioc: per adapter object
1440 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1442 * This is called when command line option diag_buffer_enable is enabled
1443 * at driver load time.
1445 void
1446 mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
1448 struct mpt2_diag_register diag_register;
1450 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
1452 if (bits_to_register & 1) {
1453 printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
1454 ioc->name);
1455 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1456 /* register for 1MB buffers */
1457 diag_register.requested_buffer_size = (1024 * 1024);
1458 diag_register.unique_id = 0x7075900;
1459 _ctl_diag_register_2(ioc, &diag_register);
1462 if (bits_to_register & 2) {
1463 printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
1464 ioc->name);
1465 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1466 /* register for 2MB buffers */
1467 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1468 diag_register.unique_id = 0x7075901;
1469 _ctl_diag_register_2(ioc, &diag_register);
1472 if (bits_to_register & 4) {
1473 printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
1474 ioc->name);
1475 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1476 /* register for 2MB buffers */
1477 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1478 diag_register.unique_id = 0x7075901;
1479 _ctl_diag_register_2(ioc, &diag_register);
1484 * _ctl_diag_register - application register with driver
1485 * @arg - user space buffer containing ioctl content
1486 * @state - NON_BLOCKING or BLOCKING
1488 * This will allow the driver to setup any required buffers that will be
1489 * needed by firmware to communicate with the driver.
1491 static long
1492 _ctl_diag_register(void __user *arg, enum block_state state)
1494 struct mpt2_diag_register karg;
1495 struct MPT2SAS_ADAPTER *ioc;
1496 long rc;
1498 if (copy_from_user(&karg, arg, sizeof(karg))) {
1499 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1500 __FILE__, __LINE__, __func__);
1501 return -EFAULT;
1503 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1504 return -ENODEV;
1506 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1507 return -EAGAIN;
1508 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1509 return -ERESTARTSYS;
1510 rc = _ctl_diag_register_2(ioc, &karg);
1511 mutex_unlock(&ioc->ctl_cmds.mutex);
1512 return rc;
1516 * _ctl_diag_unregister - application unregister with driver
1517 * @arg - user space buffer containing ioctl content
1519 * This will allow the driver to cleanup any memory allocated for diag
1520 * messages and to free up any resources.
1522 static long
1523 _ctl_diag_unregister(void __user *arg)
1525 struct mpt2_diag_unregister karg;
1526 struct MPT2SAS_ADAPTER *ioc;
1527 void *request_data;
1528 dma_addr_t request_data_dma;
1529 u32 request_data_sz;
1530 u8 buffer_type;
1532 if (copy_from_user(&karg, arg, sizeof(karg))) {
1533 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1534 __FILE__, __LINE__, __func__);
1535 return -EFAULT;
1537 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1538 return -ENODEV;
1540 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1541 __func__));
1543 buffer_type = karg.unique_id & 0x000000ff;
1544 if (!_ctl_diag_capability(ioc, buffer_type)) {
1545 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1546 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1547 return -EPERM;
1550 if ((ioc->diag_buffer_status[buffer_type] &
1551 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1552 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1553 "registered\n", ioc->name, __func__, buffer_type);
1554 return -EINVAL;
1556 if ((ioc->diag_buffer_status[buffer_type] &
1557 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1558 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1559 "released\n", ioc->name, __func__, buffer_type);
1560 return -EINVAL;
1563 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1564 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1565 "registered\n", ioc->name, __func__, karg.unique_id);
1566 return -EINVAL;
1569 request_data = ioc->diag_buffer[buffer_type];
1570 if (!request_data) {
1571 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1572 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1573 return -ENOMEM;
1576 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1577 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1578 pci_free_consistent(ioc->pdev, request_data_sz,
1579 request_data, request_data_dma);
1580 ioc->diag_buffer[buffer_type] = NULL;
1581 ioc->diag_buffer_status[buffer_type] = 0;
1582 return 0;
1586 * _ctl_diag_query - query relevant info associated with diag buffers
1587 * @arg - user space buffer containing ioctl content
1589 * The application will send only buffer_type and unique_id. Driver will
1590 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1591 * 0x00, the driver will return info specified by Buffer Type.
1593 static long
1594 _ctl_diag_query(void __user *arg)
1596 struct mpt2_diag_query karg;
1597 struct MPT2SAS_ADAPTER *ioc;
1598 void *request_data;
1599 int i;
1600 u8 buffer_type;
1602 if (copy_from_user(&karg, arg, sizeof(karg))) {
1603 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1604 __FILE__, __LINE__, __func__);
1605 return -EFAULT;
1607 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1608 return -ENODEV;
1610 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1611 __func__));
1613 karg.application_flags = 0;
1614 buffer_type = karg.buffer_type;
1616 if (!_ctl_diag_capability(ioc, buffer_type)) {
1617 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1618 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1619 return -EPERM;
1622 if ((ioc->diag_buffer_status[buffer_type] &
1623 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1624 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1625 "registered\n", ioc->name, __func__, buffer_type);
1626 return -EINVAL;
1629 if (karg.unique_id & 0xffffff00) {
1630 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1631 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1632 "registered\n", ioc->name, __func__,
1633 karg.unique_id);
1634 return -EINVAL;
1638 request_data = ioc->diag_buffer[buffer_type];
1639 if (!request_data) {
1640 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1641 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1642 return -ENOMEM;
1645 if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1646 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1647 MPT2_APP_FLAGS_BUFFER_VALID);
1648 else
1649 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1650 MPT2_APP_FLAGS_BUFFER_VALID |
1651 MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1653 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1654 karg.product_specific[i] =
1655 ioc->product_specific[buffer_type][i];
1657 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1658 karg.driver_added_buffer_size = 0;
1659 karg.unique_id = ioc->unique_id[buffer_type];
1660 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1662 if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1663 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1664 "data @ %p\n", ioc->name, __func__, arg);
1665 return -EFAULT;
1667 return 0;
1671 * _ctl_send_release - Diag Release Message
1672 * @ioc: per adapter object
1673 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1674 * @issue_reset - specifies whether host reset is required.
1677 static int
1678 _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1680 Mpi2DiagReleaseRequest_t *mpi_request;
1681 Mpi2DiagReleaseReply_t *mpi_reply;
1682 u16 smid;
1683 u16 ioc_status;
1684 u32 ioc_state;
1685 int rc;
1686 unsigned long timeleft;
1688 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1689 __func__));
1691 rc = 0;
1692 *issue_reset = 0;
1694 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1695 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1696 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
1697 "skipping due to FAULT state\n", ioc->name,
1698 __func__));
1699 rc = -EAGAIN;
1700 goto out;
1703 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1704 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1705 ioc->name, __func__);
1706 rc = -EAGAIN;
1707 goto out;
1710 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1711 if (!smid) {
1712 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1713 ioc->name, __func__);
1714 rc = -EAGAIN;
1715 goto out;
1718 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1719 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1720 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1721 ioc->ctl_cmds.smid = smid;
1723 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1724 mpi_request->BufferType = buffer_type;
1725 mpi_request->VF_ID = 0; /* TODO */
1726 mpi_request->VP_ID = 0;
1728 mpt2sas_base_put_smid_default(ioc, smid);
1729 init_completion(&ioc->ctl_cmds.done);
1730 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1731 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1733 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1734 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1735 __func__);
1736 _debug_dump_mf(mpi_request,
1737 sizeof(Mpi2DiagReleaseRequest_t)/4);
1738 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1739 *issue_reset = 1;
1740 rc = -EFAULT;
1741 goto out;
1744 /* process the completed Reply Message Frame */
1745 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1746 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1747 ioc->name, __func__);
1748 rc = -EFAULT;
1749 goto out;
1752 mpi_reply = ioc->ctl_cmds.reply;
1753 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1755 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1756 ioc->diag_buffer_status[buffer_type] |=
1757 MPT2_DIAG_BUFFER_IS_RELEASED;
1758 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1759 ioc->name, __func__));
1760 } else {
1761 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1762 "log_info(0x%08x)\n", ioc->name, __func__,
1763 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1764 rc = -EFAULT;
1767 out:
1768 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1769 return rc;
1773 * _ctl_diag_release - request to send Diag Release Message to firmware
1774 * @arg - user space buffer containing ioctl content
1775 * @state - NON_BLOCKING or BLOCKING
1777 * This allows ownership of the specified buffer to returned to the driver,
1778 * allowing an application to read the buffer without fear that firmware is
1779 * overwritting information in the buffer.
1781 static long
1782 _ctl_diag_release(void __user *arg, enum block_state state)
1784 struct mpt2_diag_release karg;
1785 struct MPT2SAS_ADAPTER *ioc;
1786 void *request_data;
1787 int rc;
1788 u8 buffer_type;
1789 u8 issue_reset = 0;
1791 if (copy_from_user(&karg, arg, sizeof(karg))) {
1792 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1793 __FILE__, __LINE__, __func__);
1794 return -EFAULT;
1796 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1797 return -ENODEV;
1799 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1800 __func__));
1802 buffer_type = karg.unique_id & 0x000000ff;
1803 if (!_ctl_diag_capability(ioc, buffer_type)) {
1804 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1805 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1806 return -EPERM;
1809 if ((ioc->diag_buffer_status[buffer_type] &
1810 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1811 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1812 "registered\n", ioc->name, __func__, buffer_type);
1813 return -EINVAL;
1816 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1817 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1818 "registered\n", ioc->name, __func__, karg.unique_id);
1819 return -EINVAL;
1822 if (ioc->diag_buffer_status[buffer_type] &
1823 MPT2_DIAG_BUFFER_IS_RELEASED) {
1824 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1825 "is already released\n", ioc->name, __func__,
1826 buffer_type);
1827 return 0;
1830 request_data = ioc->diag_buffer[buffer_type];
1832 if (!request_data) {
1833 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1834 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1835 return -ENOMEM;
1838 /* buffers were released by due to host reset */
1839 if ((ioc->diag_buffer_status[buffer_type] &
1840 MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1841 ioc->diag_buffer_status[buffer_type] |=
1842 MPT2_DIAG_BUFFER_IS_RELEASED;
1843 ioc->diag_buffer_status[buffer_type] &=
1844 ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1845 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1846 "was released due to host reset\n", ioc->name, __func__,
1847 buffer_type);
1848 return 0;
1851 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1852 return -EAGAIN;
1853 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1854 return -ERESTARTSYS;
1856 rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1858 if (issue_reset)
1859 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1860 FORCE_BIG_HAMMER);
1862 mutex_unlock(&ioc->ctl_cmds.mutex);
1863 return rc;
1867 * _ctl_diag_read_buffer - request for copy of the diag buffer
1868 * @arg - user space buffer containing ioctl content
1869 * @state - NON_BLOCKING or BLOCKING
1871 static long
1872 _ctl_diag_read_buffer(void __user *arg, enum block_state state)
1874 struct mpt2_diag_read_buffer karg;
1875 struct mpt2_diag_read_buffer __user *uarg = arg;
1876 struct MPT2SAS_ADAPTER *ioc;
1877 void *request_data, *diag_data;
1878 Mpi2DiagBufferPostRequest_t *mpi_request;
1879 Mpi2DiagBufferPostReply_t *mpi_reply;
1880 int rc, i;
1881 u8 buffer_type;
1882 unsigned long timeleft;
1883 u16 smid;
1884 u16 ioc_status;
1885 u8 issue_reset = 0;
1887 if (copy_from_user(&karg, arg, sizeof(karg))) {
1888 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1889 __FILE__, __LINE__, __func__);
1890 return -EFAULT;
1892 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1893 return -ENODEV;
1895 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1896 __func__));
1898 buffer_type = karg.unique_id & 0x000000ff;
1899 if (!_ctl_diag_capability(ioc, buffer_type)) {
1900 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1901 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1902 return -EPERM;
1905 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1906 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1907 "registered\n", ioc->name, __func__, karg.unique_id);
1908 return -EINVAL;
1911 request_data = ioc->diag_buffer[buffer_type];
1912 if (!request_data) {
1913 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1914 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1915 return -ENOMEM;
1918 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1919 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1920 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1921 __func__);
1922 return -EINVAL;
1925 diag_data = (void *)(request_data + karg.starting_offset);
1926 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: diag_buffer(%p), "
1927 "offset(%d), sz(%d)\n", ioc->name, __func__,
1928 diag_data, karg.starting_offset, karg.bytes_to_read));
1930 if (copy_to_user((void __user *)uarg->diagnostic_data,
1931 diag_data, karg.bytes_to_read)) {
1932 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
1933 "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
1934 __func__, diag_data);
1935 return -EFAULT;
1938 if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
1939 return 0;
1941 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: Reregister "
1942 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
1943 if ((ioc->diag_buffer_status[buffer_type] &
1944 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1945 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
1946 "buffer_type(0x%02x) is still registered\n", ioc->name,
1947 __func__, buffer_type));
1948 return 0;
1950 /* Get a free request frame and save the message context.
1952 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1953 return -EAGAIN;
1954 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1955 return -ERESTARTSYS;
1957 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1958 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1959 ioc->name, __func__);
1960 rc = -EAGAIN;
1961 goto out;
1964 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1965 if (!smid) {
1966 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1967 ioc->name, __func__);
1968 rc = -EAGAIN;
1969 goto out;
1972 rc = 0;
1973 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1974 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1975 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1976 ioc->ctl_cmds.smid = smid;
1978 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1979 mpi_request->BufferType = buffer_type;
1980 mpi_request->BufferLength =
1981 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
1982 mpi_request->BufferAddress =
1983 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
1984 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1985 mpi_request->ProductSpecific[i] =
1986 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1987 mpi_request->VF_ID = 0; /* TODO */
1988 mpi_request->VP_ID = 0;
1990 mpt2sas_base_put_smid_default(ioc, smid);
1991 init_completion(&ioc->ctl_cmds.done);
1992 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1993 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1995 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1996 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1997 __func__);
1998 _debug_dump_mf(mpi_request,
1999 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2000 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
2001 issue_reset = 1;
2002 goto issue_host_reset;
2005 /* process the completed Reply Message Frame */
2006 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
2007 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
2008 ioc->name, __func__);
2009 rc = -EFAULT;
2010 goto out;
2013 mpi_reply = ioc->ctl_cmds.reply;
2014 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2016 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2017 ioc->diag_buffer_status[buffer_type] |=
2018 MPT2_DIAG_BUFFER_IS_REGISTERED;
2019 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
2020 ioc->name, __func__));
2021 } else {
2022 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
2023 "log_info(0x%08x)\n", ioc->name, __func__,
2024 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2025 rc = -EFAULT;
2028 issue_host_reset:
2029 if (issue_reset)
2030 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2031 FORCE_BIG_HAMMER);
2033 out:
2035 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
2036 mutex_unlock(&ioc->ctl_cmds.mutex);
2037 return rc;
2041 * _ctl_ioctl_main - main ioctl entry point
2042 * @file - (struct file)
2043 * @cmd - ioctl opcode
2044 * @arg -
2046 static long
2047 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg)
2049 enum block_state state;
2050 long ret = -EINVAL;
2052 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING :
2053 BLOCKING;
2055 switch (cmd) {
2056 case MPT2IOCINFO:
2057 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
2058 ret = _ctl_getiocinfo(arg);
2059 break;
2060 case MPT2COMMAND:
2062 struct mpt2_ioctl_command karg;
2063 struct mpt2_ioctl_command __user *uarg;
2064 struct MPT2SAS_ADAPTER *ioc;
2066 if (copy_from_user(&karg, arg, sizeof(karg))) {
2067 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2068 __FILE__, __LINE__, __func__);
2069 return -EFAULT;
2072 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2073 !ioc)
2074 return -ENODEV;
2076 if (ioc->shost_recovery)
2077 return -EAGAIN;
2079 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2080 uarg = arg;
2081 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2083 break;
2085 case MPT2EVENTQUERY:
2086 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2087 ret = _ctl_eventquery(arg);
2088 break;
2089 case MPT2EVENTENABLE:
2090 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2091 ret = _ctl_eventenable(arg);
2092 break;
2093 case MPT2EVENTREPORT:
2094 ret = _ctl_eventreport(arg);
2095 break;
2096 case MPT2HARDRESET:
2097 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2098 ret = _ctl_do_reset(arg);
2099 break;
2100 case MPT2BTDHMAPPING:
2101 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2102 ret = _ctl_btdh_mapping(arg);
2103 break;
2104 case MPT2DIAGREGISTER:
2105 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2106 ret = _ctl_diag_register(arg, state);
2107 break;
2108 case MPT2DIAGUNREGISTER:
2109 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2110 ret = _ctl_diag_unregister(arg);
2111 break;
2112 case MPT2DIAGQUERY:
2113 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2114 ret = _ctl_diag_query(arg);
2115 break;
2116 case MPT2DIAGRELEASE:
2117 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2118 ret = _ctl_diag_release(arg, state);
2119 break;
2120 case MPT2DIAGREADBUFFER:
2121 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2122 ret = _ctl_diag_read_buffer(arg, state);
2123 break;
2124 default:
2126 struct mpt2_ioctl_command karg;
2127 struct MPT2SAS_ADAPTER *ioc;
2129 if (copy_from_user(&karg, arg, sizeof(karg))) {
2130 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2131 __FILE__, __LINE__, __func__);
2132 return -EFAULT;
2135 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2136 !ioc)
2137 return -ENODEV;
2139 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2140 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2141 break;
2144 return ret;
2148 * _ctl_ioctl - main ioctl entry point (unlocked)
2149 * @file - (struct file)
2150 * @cmd - ioctl opcode
2151 * @arg -
2153 static long
2154 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2156 long ret;
2158 lock_kernel();
2159 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2160 unlock_kernel();
2161 return ret;
2164 #ifdef CONFIG_COMPAT
2166 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2167 * @file - (struct file)
2168 * @cmd - ioctl opcode
2169 * @arg - (struct mpt2_ioctl_command32)
2171 * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2173 static long
2174 _ctl_compat_mpt_command(struct file *file, unsigned cmd, unsigned long arg)
2176 struct mpt2_ioctl_command32 karg32;
2177 struct mpt2_ioctl_command32 __user *uarg;
2178 struct mpt2_ioctl_command karg;
2179 struct MPT2SAS_ADAPTER *ioc;
2180 enum block_state state;
2182 if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2183 return -EINVAL;
2185 uarg = (struct mpt2_ioctl_command32 __user *) arg;
2187 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2188 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2189 __FILE__, __LINE__, __func__);
2190 return -EFAULT;
2192 if (_ctl_verify_adapter(karg32.hdr.ioc_number, &ioc) == -1 || !ioc)
2193 return -ENODEV;
2195 if (ioc->shost_recovery)
2196 return -EAGAIN;
2198 memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2199 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2200 karg.hdr.port_number = karg32.hdr.port_number;
2201 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2202 karg.timeout = karg32.timeout;
2203 karg.max_reply_bytes = karg32.max_reply_bytes;
2204 karg.data_in_size = karg32.data_in_size;
2205 karg.data_out_size = karg32.data_out_size;
2206 karg.max_sense_bytes = karg32.max_sense_bytes;
2207 karg.data_sge_offset = karg32.data_sge_offset;
2208 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2209 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2210 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2211 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2212 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2213 return _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2217 * _ctl_ioctl_compat - main ioctl entry point (compat)
2218 * @file -
2219 * @cmd -
2220 * @arg -
2222 * This routine handles 32 bit applications in 64bit os.
2224 static long
2225 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2227 long ret;
2229 lock_kernel();
2230 if (cmd == MPT2COMMAND32)
2231 ret = _ctl_compat_mpt_command(file, cmd, arg);
2232 else
2233 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2234 unlock_kernel();
2235 return ret;
2237 #endif
2239 /* scsi host attributes */
2242 * _ctl_version_fw_show - firmware version
2243 * @cdev - pointer to embedded class device
2244 * @buf - the buffer returned
2246 * A sysfs 'read-only' shost attribute.
2248 static ssize_t
2249 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2250 char *buf)
2252 struct Scsi_Host *shost = class_to_shost(cdev);
2253 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2255 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2256 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2257 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2258 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2259 ioc->facts.FWVersion.Word & 0x000000FF);
2261 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2264 * _ctl_version_bios_show - bios version
2265 * @cdev - pointer to embedded class device
2266 * @buf - the buffer returned
2268 * A sysfs 'read-only' shost attribute.
2270 static ssize_t
2271 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2272 char *buf)
2274 struct Scsi_Host *shost = class_to_shost(cdev);
2275 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2277 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2279 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2280 (version & 0xFF000000) >> 24,
2281 (version & 0x00FF0000) >> 16,
2282 (version & 0x0000FF00) >> 8,
2283 version & 0x000000FF);
2285 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2288 * _ctl_version_mpi_show - MPI (message passing interface) version
2289 * @cdev - pointer to embedded class device
2290 * @buf - the buffer returned
2292 * A sysfs 'read-only' shost attribute.
2294 static ssize_t
2295 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2296 char *buf)
2298 struct Scsi_Host *shost = class_to_shost(cdev);
2299 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2301 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2302 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2304 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2307 * _ctl_version_product_show - product name
2308 * @cdev - pointer to embedded class device
2309 * @buf - the buffer returned
2311 * A sysfs 'read-only' shost attribute.
2313 static ssize_t
2314 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2315 char *buf)
2317 struct Scsi_Host *shost = class_to_shost(cdev);
2318 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2320 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2322 static DEVICE_ATTR(version_product, S_IRUGO,
2323 _ctl_version_product_show, NULL);
2326 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2327 * @cdev - pointer to embedded class device
2328 * @buf - the buffer returned
2330 * A sysfs 'read-only' shost attribute.
2332 static ssize_t
2333 _ctl_version_nvdata_persistent_show(struct device *cdev,
2334 struct device_attribute *attr, char *buf)
2336 struct Scsi_Host *shost = class_to_shost(cdev);
2337 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2339 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2340 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2342 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2343 _ctl_version_nvdata_persistent_show, NULL);
2346 * _ctl_version_nvdata_default_show - nvdata default version
2347 * @cdev - pointer to embedded class device
2348 * @buf - the buffer returned
2350 * A sysfs 'read-only' shost attribute.
2352 static ssize_t
2353 _ctl_version_nvdata_default_show(struct device *cdev,
2354 struct device_attribute *attr, char *buf)
2356 struct Scsi_Host *shost = class_to_shost(cdev);
2357 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2359 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2360 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2362 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2363 _ctl_version_nvdata_default_show, NULL);
2366 * _ctl_board_name_show - board name
2367 * @cdev - pointer to embedded class device
2368 * @buf - the buffer returned
2370 * A sysfs 'read-only' shost attribute.
2372 static ssize_t
2373 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2374 char *buf)
2376 struct Scsi_Host *shost = class_to_shost(cdev);
2377 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2379 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2381 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2384 * _ctl_board_assembly_show - board assembly name
2385 * @cdev - pointer to embedded class device
2386 * @buf - the buffer returned
2388 * A sysfs 'read-only' shost attribute.
2390 static ssize_t
2391 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2392 char *buf)
2394 struct Scsi_Host *shost = class_to_shost(cdev);
2395 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2397 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2399 static DEVICE_ATTR(board_assembly, S_IRUGO,
2400 _ctl_board_assembly_show, NULL);
2403 * _ctl_board_tracer_show - board tracer number
2404 * @cdev - pointer to embedded class device
2405 * @buf - the buffer returned
2407 * A sysfs 'read-only' shost attribute.
2409 static ssize_t
2410 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2411 char *buf)
2413 struct Scsi_Host *shost = class_to_shost(cdev);
2414 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2416 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2418 static DEVICE_ATTR(board_tracer, S_IRUGO,
2419 _ctl_board_tracer_show, NULL);
2422 * _ctl_io_delay_show - io missing delay
2423 * @cdev - pointer to embedded class device
2424 * @buf - the buffer returned
2426 * This is for firmware implemention for deboucing device
2427 * removal events.
2429 * A sysfs 'read-only' shost attribute.
2431 static ssize_t
2432 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2433 char *buf)
2435 struct Scsi_Host *shost = class_to_shost(cdev);
2436 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2438 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2440 static DEVICE_ATTR(io_delay, S_IRUGO,
2441 _ctl_io_delay_show, NULL);
2444 * _ctl_device_delay_show - device missing delay
2445 * @cdev - pointer to embedded class device
2446 * @buf - the buffer returned
2448 * This is for firmware implemention for deboucing device
2449 * removal events.
2451 * A sysfs 'read-only' shost attribute.
2453 static ssize_t
2454 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2455 char *buf)
2457 struct Scsi_Host *shost = class_to_shost(cdev);
2458 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2460 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2462 static DEVICE_ATTR(device_delay, S_IRUGO,
2463 _ctl_device_delay_show, NULL);
2466 * _ctl_fw_queue_depth_show - global credits
2467 * @cdev - pointer to embedded class device
2468 * @buf - the buffer returned
2470 * This is firmware queue depth limit
2472 * A sysfs 'read-only' shost attribute.
2474 static ssize_t
2475 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2476 char *buf)
2478 struct Scsi_Host *shost = class_to_shost(cdev);
2479 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2481 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2483 static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2484 _ctl_fw_queue_depth_show, NULL);
2487 * _ctl_sas_address_show - sas address
2488 * @cdev - pointer to embedded class device
2489 * @buf - the buffer returned
2491 * This is the controller sas address
2493 * A sysfs 'read-only' shost attribute.
2495 static ssize_t
2496 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2497 char *buf)
2499 struct Scsi_Host *shost = class_to_shost(cdev);
2500 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2502 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2503 (unsigned long long)ioc->sas_hba.sas_address);
2505 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2506 _ctl_host_sas_address_show, NULL);
2509 * _ctl_logging_level_show - logging level
2510 * @cdev - pointer to embedded class device
2511 * @buf - the buffer returned
2513 * A sysfs 'read/write' shost attribute.
2515 static ssize_t
2516 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2517 char *buf)
2519 struct Scsi_Host *shost = class_to_shost(cdev);
2520 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2522 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2524 static ssize_t
2525 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2526 const char *buf, size_t count)
2528 struct Scsi_Host *shost = class_to_shost(cdev);
2529 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2530 int val = 0;
2532 if (sscanf(buf, "%x", &val) != 1)
2533 return -EINVAL;
2535 ioc->logging_level = val;
2536 printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2537 ioc->logging_level);
2538 return strlen(buf);
2540 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2541 _ctl_logging_level_show, _ctl_logging_level_store);
2543 /* device attributes */
2545 * _ctl_fwfault_debug_show - show/store fwfault_debug
2546 * @cdev - pointer to embedded class device
2547 * @buf - the buffer returned
2549 * mpt2sas_fwfault_debug is command line option
2550 * A sysfs 'read/write' shost attribute.
2552 static ssize_t
2553 _ctl_fwfault_debug_show(struct device *cdev,
2554 struct device_attribute *attr, char *buf)
2556 struct Scsi_Host *shost = class_to_shost(cdev);
2557 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2559 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2561 static ssize_t
2562 _ctl_fwfault_debug_store(struct device *cdev,
2563 struct device_attribute *attr, const char *buf, size_t count)
2565 struct Scsi_Host *shost = class_to_shost(cdev);
2566 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2567 int val = 0;
2569 if (sscanf(buf, "%d", &val) != 1)
2570 return -EINVAL;
2572 ioc->fwfault_debug = val;
2573 printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
2574 ioc->fwfault_debug);
2575 return strlen(buf);
2577 static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2578 _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2580 struct device_attribute *mpt2sas_host_attrs[] = {
2581 &dev_attr_version_fw,
2582 &dev_attr_version_bios,
2583 &dev_attr_version_mpi,
2584 &dev_attr_version_product,
2585 &dev_attr_version_nvdata_persistent,
2586 &dev_attr_version_nvdata_default,
2587 &dev_attr_board_name,
2588 &dev_attr_board_assembly,
2589 &dev_attr_board_tracer,
2590 &dev_attr_io_delay,
2591 &dev_attr_device_delay,
2592 &dev_attr_logging_level,
2593 &dev_attr_fwfault_debug,
2594 &dev_attr_fw_queue_depth,
2595 &dev_attr_host_sas_address,
2596 NULL,
2600 * _ctl_device_sas_address_show - sas address
2601 * @cdev - pointer to embedded class device
2602 * @buf - the buffer returned
2604 * This is the sas address for the target
2606 * A sysfs 'read-only' shost attribute.
2608 static ssize_t
2609 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2610 char *buf)
2612 struct scsi_device *sdev = to_scsi_device(dev);
2613 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2615 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2616 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2618 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2621 * _ctl_device_handle_show - device handle
2622 * @cdev - pointer to embedded class device
2623 * @buf - the buffer returned
2625 * This is the firmware assigned device handle
2627 * A sysfs 'read-only' shost attribute.
2629 static ssize_t
2630 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
2631 char *buf)
2633 struct scsi_device *sdev = to_scsi_device(dev);
2634 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2636 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
2637 sas_device_priv_data->sas_target->handle);
2639 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
2641 struct device_attribute *mpt2sas_dev_attrs[] = {
2642 &dev_attr_sas_address,
2643 &dev_attr_sas_device_handle,
2644 NULL,
2647 static const struct file_operations ctl_fops = {
2648 .owner = THIS_MODULE,
2649 .unlocked_ioctl = _ctl_ioctl,
2650 .release = _ctl_release,
2651 .poll = _ctl_poll,
2652 .fasync = _ctl_fasync,
2653 #ifdef CONFIG_COMPAT
2654 .compat_ioctl = _ctl_ioctl_compat,
2655 #endif
2658 static struct miscdevice ctl_dev = {
2659 .minor = MPT2SAS_MINOR,
2660 .name = MPT2SAS_DEV_NAME,
2661 .fops = &ctl_fops,
2665 * mpt2sas_ctl_init - main entry point for ctl.
2668 void
2669 mpt2sas_ctl_init(void)
2671 async_queue = NULL;
2672 if (misc_register(&ctl_dev) < 0)
2673 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
2674 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
2676 init_waitqueue_head(&ctl_poll_wait);
2680 * mpt2sas_ctl_exit - exit point for ctl
2683 void
2684 mpt2sas_ctl_exit(void)
2686 struct MPT2SAS_ADAPTER *ioc;
2687 int i;
2689 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
2691 /* free memory associated to diag buffers */
2692 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
2693 if (!ioc->diag_buffer[i])
2694 continue;
2695 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
2696 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
2697 ioc->diag_buffer[i] = NULL;
2698 ioc->diag_buffer_status[i] = 0;
2701 kfree(ioc->event_log);
2703 misc_deregister(&ctl_dev);