ARM: rockchip: fix broken build
[linux/fpc-iii.git] / drivers / scsi / mpt2sas / mpt2sas_ctl.c
blob4e509604b5716483c0c52a734b4776a637bb44b2
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-2014 LSI Corporation
7 * Copyright (C) 20013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
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/mutex.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 DEFINE_MUTEX(_ctl_mutex);
65 static struct fasync_struct *async_queue;
66 static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
68 static int _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type,
69 u8 *issue_reset);
71 /**
72 * enum block_state - blocking state
73 * @NON_BLOCKING: non blocking
74 * @BLOCKING: blocking
76 * These states are for ioctls that need to wait for a response
77 * from firmware, so they probably require sleep.
79 enum block_state {
80 NON_BLOCKING,
81 BLOCKING,
84 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
85 /**
86 * _ctl_sas_device_find_by_handle - sas device search
87 * @ioc: per adapter object
88 * @handle: sas device handle (assigned by firmware)
89 * Context: Calling function should acquire ioc->sas_device_lock
91 * This searches for sas_device based on sas_address, then return sas_device
92 * object.
94 static struct _sas_device *
95 _ctl_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
97 struct _sas_device *sas_device, *r;
99 r = NULL;
100 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
101 if (sas_device->handle != handle)
102 continue;
103 r = sas_device;
104 goto out;
107 out:
108 return r;
112 * _ctl_display_some_debug - debug routine
113 * @ioc: per adapter object
114 * @smid: system request message index
115 * @calling_function_name: string pass from calling function
116 * @mpi_reply: reply message frame
117 * Context: none.
119 * Function for displaying debug info helpful when debugging issues
120 * in this module.
122 static void
123 _ctl_display_some_debug(struct MPT2SAS_ADAPTER *ioc, u16 smid,
124 char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
126 Mpi2ConfigRequest_t *mpi_request;
127 char *desc = NULL;
129 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
130 return;
132 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
133 switch (mpi_request->Function) {
134 case MPI2_FUNCTION_SCSI_IO_REQUEST:
136 Mpi2SCSIIORequest_t *scsi_request =
137 (Mpi2SCSIIORequest_t *)mpi_request;
139 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
140 "scsi_io, cmd(0x%02x), cdb_len(%d)",
141 scsi_request->CDB.CDB32[0],
142 le16_to_cpu(scsi_request->IoFlags) & 0xF);
143 desc = ioc->tmp_string;
144 break;
146 case MPI2_FUNCTION_SCSI_TASK_MGMT:
147 desc = "task_mgmt";
148 break;
149 case MPI2_FUNCTION_IOC_INIT:
150 desc = "ioc_init";
151 break;
152 case MPI2_FUNCTION_IOC_FACTS:
153 desc = "ioc_facts";
154 break;
155 case MPI2_FUNCTION_CONFIG:
157 Mpi2ConfigRequest_t *config_request =
158 (Mpi2ConfigRequest_t *)mpi_request;
160 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
161 "config, type(0x%02x), ext_type(0x%02x), number(%d)",
162 (config_request->Header.PageType &
163 MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
164 config_request->Header.PageNumber);
165 desc = ioc->tmp_string;
166 break;
168 case MPI2_FUNCTION_PORT_FACTS:
169 desc = "port_facts";
170 break;
171 case MPI2_FUNCTION_PORT_ENABLE:
172 desc = "port_enable";
173 break;
174 case MPI2_FUNCTION_EVENT_NOTIFICATION:
175 desc = "event_notification";
176 break;
177 case MPI2_FUNCTION_FW_DOWNLOAD:
178 desc = "fw_download";
179 break;
180 case MPI2_FUNCTION_FW_UPLOAD:
181 desc = "fw_upload";
182 break;
183 case MPI2_FUNCTION_RAID_ACTION:
184 desc = "raid_action";
185 break;
186 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
188 Mpi2SCSIIORequest_t *scsi_request =
189 (Mpi2SCSIIORequest_t *)mpi_request;
191 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
192 "raid_pass, cmd(0x%02x), cdb_len(%d)",
193 scsi_request->CDB.CDB32[0],
194 le16_to_cpu(scsi_request->IoFlags) & 0xF);
195 desc = ioc->tmp_string;
196 break;
198 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
199 desc = "sas_iounit_cntl";
200 break;
201 case MPI2_FUNCTION_SATA_PASSTHROUGH:
202 desc = "sata_pass";
203 break;
204 case MPI2_FUNCTION_DIAG_BUFFER_POST:
205 desc = "diag_buffer_post";
206 break;
207 case MPI2_FUNCTION_DIAG_RELEASE:
208 desc = "diag_release";
209 break;
210 case MPI2_FUNCTION_SMP_PASSTHROUGH:
211 desc = "smp_passthrough";
212 break;
215 if (!desc)
216 return;
218 printk(MPT2SAS_INFO_FMT "%s: %s, smid(%d)\n",
219 ioc->name, calling_function_name, desc, smid);
221 if (!mpi_reply)
222 return;
224 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
225 printk(MPT2SAS_INFO_FMT
226 "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
227 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
228 le32_to_cpu(mpi_reply->IOCLogInfo));
230 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
231 mpi_request->Function ==
232 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
233 Mpi2SCSIIOReply_t *scsi_reply =
234 (Mpi2SCSIIOReply_t *)mpi_reply;
235 struct _sas_device *sas_device = NULL;
236 unsigned long flags;
238 spin_lock_irqsave(&ioc->sas_device_lock, flags);
239 sas_device = _ctl_sas_device_find_by_handle(ioc,
240 le16_to_cpu(scsi_reply->DevHandle));
241 if (sas_device) {
242 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
243 "phy(%d)\n", ioc->name, (unsigned long long)
244 sas_device->sas_address, sas_device->phy);
245 printk(MPT2SAS_WARN_FMT
246 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
247 ioc->name, sas_device->enclosure_logical_id,
248 sas_device->slot);
250 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
251 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
252 printk(MPT2SAS_INFO_FMT
253 "\tscsi_state(0x%02x), scsi_status"
254 "(0x%02x)\n", ioc->name,
255 scsi_reply->SCSIState,
256 scsi_reply->SCSIStatus);
259 #endif
262 * mpt2sas_ctl_done - ctl module completion routine
263 * @ioc: per adapter object
264 * @smid: system request message index
265 * @msix_index: MSIX table index supplied by the OS
266 * @reply: reply message frame(lower 32bit addr)
267 * Context: none.
269 * The callback handler when using ioc->ctl_cb_idx.
271 * Return 1 meaning mf should be freed from _base_interrupt
272 * 0 means the mf is freed from this function.
275 mpt2sas_ctl_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
276 u32 reply)
278 MPI2DefaultReply_t *mpi_reply;
279 Mpi2SCSIIOReply_t *scsiio_reply;
280 const void *sense_data;
281 u32 sz;
283 if (ioc->ctl_cmds.status == MPT2_CMD_NOT_USED)
284 return 1;
285 if (ioc->ctl_cmds.smid != smid)
286 return 1;
287 ioc->ctl_cmds.status |= MPT2_CMD_COMPLETE;
288 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
289 if (mpi_reply) {
290 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
291 ioc->ctl_cmds.status |= MPT2_CMD_REPLY_VALID;
292 /* get sense data */
293 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
294 mpi_reply->Function ==
295 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
296 scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
297 if (scsiio_reply->SCSIState &
298 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
299 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
300 le32_to_cpu(scsiio_reply->SenseCount));
301 sense_data = mpt2sas_base_get_sense_buffer(ioc,
302 smid);
303 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
307 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
308 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
309 #endif
310 ioc->ctl_cmds.status &= ~MPT2_CMD_PENDING;
311 complete(&ioc->ctl_cmds.done);
312 return 1;
316 * _ctl_check_event_type - determines when an event needs logging
317 * @ioc: per adapter object
318 * @event: firmware event
320 * The bitmask in ioc->event_type[] indicates which events should be
321 * be saved in the driver event_log. This bitmask is set by application.
323 * Returns 1 when event should be captured, or zero means no match.
325 static int
326 _ctl_check_event_type(struct MPT2SAS_ADAPTER *ioc, u16 event)
328 u16 i;
329 u32 desired_event;
331 if (event >= 128 || !event || !ioc->event_log)
332 return 0;
334 desired_event = (1 << (event % 32));
335 if (!desired_event)
336 desired_event = 1;
337 i = event / 32;
338 return desired_event & ioc->event_type[i];
342 * mpt2sas_ctl_add_to_event_log - add event
343 * @ioc: per adapter object
344 * @mpi_reply: reply message frame
346 * Return nothing.
348 void
349 mpt2sas_ctl_add_to_event_log(struct MPT2SAS_ADAPTER *ioc,
350 Mpi2EventNotificationReply_t *mpi_reply)
352 struct MPT2_IOCTL_EVENTS *event_log;
353 u16 event;
354 int i;
355 u32 sz, event_data_sz;
356 u8 send_aen = 0;
358 if (!ioc->event_log)
359 return;
361 event = le16_to_cpu(mpi_reply->Event);
363 if (_ctl_check_event_type(ioc, event)) {
365 /* insert entry into circular event_log */
366 i = ioc->event_context % MPT2SAS_CTL_EVENT_LOG_SIZE;
367 event_log = ioc->event_log;
368 event_log[i].event = event;
369 event_log[i].context = ioc->event_context++;
371 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
372 sz = min_t(u32, event_data_sz, MPT2_EVENT_DATA_SIZE);
373 memset(event_log[i].data, 0, MPT2_EVENT_DATA_SIZE);
374 memcpy(event_log[i].data, mpi_reply->EventData, sz);
375 send_aen = 1;
378 /* This aen_event_read_flag flag is set until the
379 * application has read the event log.
380 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
382 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
383 (send_aen && !ioc->aen_event_read_flag)) {
384 ioc->aen_event_read_flag = 1;
385 wake_up_interruptible(&ctl_poll_wait);
386 if (async_queue)
387 kill_fasync(&async_queue, SIGIO, POLL_IN);
392 * mpt2sas_ctl_event_callback - firmware event handler (called at ISR time)
393 * @ioc: per adapter object
394 * @msix_index: MSIX table index supplied by the OS
395 * @reply: reply message frame(lower 32bit addr)
396 * Context: interrupt.
398 * This function merely adds a new work task into ioc->firmware_event_thread.
399 * The tasks are worked from _firmware_event_work in user context.
401 * Returns void.
403 void
404 mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
405 u32 reply)
407 Mpi2EventNotificationReply_t *mpi_reply;
409 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
410 if (unlikely(!mpi_reply)) {
411 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
412 ioc->name, __FILE__, __LINE__, __func__);
413 return;
415 mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
416 return;
420 * _ctl_verify_adapter - validates ioc_number passed from application
421 * @ioc: per adapter object
422 * @iocpp: The ioc pointer is returned in this.
424 * Return (-1) means error, else ioc_number.
426 static int
427 _ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
429 struct MPT2SAS_ADAPTER *ioc;
431 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
432 if (ioc->id != ioc_number)
433 continue;
434 *iocpp = ioc;
435 return ioc_number;
437 *iocpp = NULL;
438 return -1;
442 * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
443 * @ioc: per adapter object
444 * @reset_phase: phase
446 * The handler for doing any required cleanup or initialization.
448 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
449 * MPT2_IOC_DONE_RESET
451 void
452 mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
454 int i;
455 u8 issue_reset;
457 switch (reset_phase) {
458 case MPT2_IOC_PRE_RESET:
459 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
460 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
461 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
462 if (!(ioc->diag_buffer_status[i] &
463 MPT2_DIAG_BUFFER_IS_REGISTERED))
464 continue;
465 if ((ioc->diag_buffer_status[i] &
466 MPT2_DIAG_BUFFER_IS_RELEASED))
467 continue;
468 _ctl_send_release(ioc, i, &issue_reset);
470 break;
471 case MPT2_IOC_AFTER_RESET:
472 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
473 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
474 if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
475 ioc->ctl_cmds.status |= MPT2_CMD_RESET;
476 mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
477 complete(&ioc->ctl_cmds.done);
479 break;
480 case MPT2_IOC_DONE_RESET:
481 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
482 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
484 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
485 if (!(ioc->diag_buffer_status[i] &
486 MPT2_DIAG_BUFFER_IS_REGISTERED))
487 continue;
488 if ((ioc->diag_buffer_status[i] &
489 MPT2_DIAG_BUFFER_IS_RELEASED))
490 continue;
491 ioc->diag_buffer_status[i] |=
492 MPT2_DIAG_BUFFER_IS_DIAG_RESET;
494 break;
499 * _ctl_fasync -
500 * @fd -
501 * @filep -
502 * @mode -
504 * Called when application request fasyn callback handler.
506 static int
507 _ctl_fasync(int fd, struct file *filep, int mode)
509 return fasync_helper(fd, filep, mode, &async_queue);
513 * _ctl_poll -
514 * @file -
515 * @wait -
518 static unsigned int
519 _ctl_poll(struct file *filep, poll_table *wait)
521 struct MPT2SAS_ADAPTER *ioc;
523 poll_wait(filep, &ctl_poll_wait, wait);
525 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
526 if (ioc->aen_event_read_flag)
527 return POLLIN | POLLRDNORM;
529 return 0;
533 * _ctl_set_task_mid - assign an active smid to tm request
534 * @ioc: per adapter object
535 * @karg - (struct mpt2_ioctl_command)
536 * @tm_request - pointer to mf from user space
538 * Returns 0 when an smid if found, else fail.
539 * during failure, the reply frame is filled.
541 static int
542 _ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
543 Mpi2SCSITaskManagementRequest_t *tm_request)
545 u8 found = 0;
546 u16 i;
547 u16 handle;
548 struct scsi_cmnd *scmd;
549 struct MPT2SAS_DEVICE *priv_data;
550 unsigned long flags;
551 Mpi2SCSITaskManagementReply_t *tm_reply;
552 u32 sz;
553 u32 lun;
554 char *desc = NULL;
556 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
557 desc = "abort_task";
558 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
559 desc = "query_task";
560 else
561 return 0;
563 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
565 handle = le16_to_cpu(tm_request->DevHandle);
566 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
567 for (i = ioc->scsiio_depth; i && !found; i--) {
568 scmd = ioc->scsi_lookup[i - 1].scmd;
569 if (scmd == NULL || scmd->device == NULL ||
570 scmd->device->hostdata == NULL)
571 continue;
572 if (lun != scmd->device->lun)
573 continue;
574 priv_data = scmd->device->hostdata;
575 if (priv_data->sas_target == NULL)
576 continue;
577 if (priv_data->sas_target->handle != handle)
578 continue;
579 tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
580 found = 1;
582 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
584 if (!found) {
585 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
586 "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
587 desc, le16_to_cpu(tm_request->DevHandle), lun));
588 tm_reply = ioc->ctl_cmds.reply;
589 tm_reply->DevHandle = tm_request->DevHandle;
590 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
591 tm_reply->TaskType = tm_request->TaskType;
592 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
593 tm_reply->VP_ID = tm_request->VP_ID;
594 tm_reply->VF_ID = tm_request->VF_ID;
595 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
596 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
597 sz))
598 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
599 __LINE__, __func__);
600 return 1;
603 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
604 "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
605 desc, le16_to_cpu(tm_request->DevHandle), lun,
606 le16_to_cpu(tm_request->TaskMID)));
607 return 0;
611 * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
612 * @ioc: per adapter object
613 * @karg - (struct mpt2_ioctl_command)
614 * @mf - pointer to mf in user space
616 static long
617 _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command karg,
618 void __user *mf)
620 MPI2RequestHeader_t *mpi_request = NULL, *request;
621 MPI2DefaultReply_t *mpi_reply;
622 u32 ioc_state;
623 u16 ioc_status;
624 u16 smid;
625 unsigned long timeout, timeleft;
626 u8 issue_reset;
627 u32 sz;
628 void *psge;
629 void *data_out = NULL;
630 dma_addr_t data_out_dma;
631 size_t data_out_sz = 0;
632 void *data_in = NULL;
633 dma_addr_t data_in_dma;
634 size_t data_in_sz = 0;
635 u32 sgl_flags;
636 long ret;
637 u16 wait_state_count;
639 issue_reset = 0;
641 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
642 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
643 ioc->name, __func__);
644 ret = -EAGAIN;
645 goto out;
648 wait_state_count = 0;
649 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
650 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
651 if (wait_state_count++ == 10) {
652 printk(MPT2SAS_ERR_FMT
653 "%s: failed due to ioc not operational\n",
654 ioc->name, __func__);
655 ret = -EFAULT;
656 goto out;
658 ssleep(1);
659 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
660 printk(MPT2SAS_INFO_FMT "%s: waiting for "
661 "operational state(count=%d)\n", ioc->name,
662 __func__, wait_state_count);
664 if (wait_state_count)
665 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
666 ioc->name, __func__);
668 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
669 if (!mpi_request) {
670 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a memory for "
671 "mpi_request\n", ioc->name, __func__);
672 ret = -ENOMEM;
673 goto out;
676 /* Check for overflow and wraparound */
677 if (karg.data_sge_offset * 4 > ioc->request_sz ||
678 karg.data_sge_offset > (UINT_MAX / 4)) {
679 ret = -EINVAL;
680 goto out;
683 /* copy in request message frame from user */
684 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
685 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
686 __func__);
687 ret = -EFAULT;
688 goto out;
691 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
692 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
693 if (!smid) {
694 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
695 ioc->name, __func__);
696 ret = -EAGAIN;
697 goto out;
699 } else {
701 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
702 if (!smid) {
703 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
704 ioc->name, __func__);
705 ret = -EAGAIN;
706 goto out;
710 ret = 0;
711 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
712 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
713 request = mpt2sas_base_get_msg_frame(ioc, smid);
714 memcpy(request, mpi_request, karg.data_sge_offset*4);
715 ioc->ctl_cmds.smid = smid;
716 data_out_sz = karg.data_out_size;
717 data_in_sz = karg.data_in_size;
719 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
720 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
721 if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
722 le16_to_cpu(mpi_request->FunctionDependent1) >
723 ioc->facts.MaxDevHandle) {
724 ret = -EINVAL;
725 mpt2sas_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 = pci_alloc_consistent(ioc->pdev, data_out_sz,
733 &data_out_dma);
734 if (!data_out) {
735 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
736 __LINE__, __func__);
737 ret = -ENOMEM;
738 mpt2sas_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 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
744 __LINE__, __func__);
745 ret = -EFAULT;
746 mpt2sas_base_free_smid(ioc, smid);
747 goto out;
751 if (data_in_sz) /* READ */ {
752 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
753 &data_in_dma);
754 if (!data_in) {
755 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
756 __LINE__, __func__);
757 ret = -ENOMEM;
758 mpt2sas_base_free_smid(ioc, smid);
759 goto out;
763 /* add scatter gather elements */
764 psge = (void *)request + (karg.data_sge_offset*4);
766 if (!data_out_sz && !data_in_sz) {
767 mpt2sas_base_build_zero_len_sge(ioc, psge);
768 } else if (data_out_sz && data_in_sz) {
769 /* WRITE sgel first */
770 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
771 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
772 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
773 ioc->base_add_sg_single(psge, sgl_flags |
774 data_out_sz, data_out_dma);
776 /* incr sgel */
777 psge += ioc->sge_size;
779 /* READ sgel last */
780 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
781 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
782 MPI2_SGE_FLAGS_END_OF_LIST);
783 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
784 ioc->base_add_sg_single(psge, sgl_flags |
785 data_in_sz, data_in_dma);
786 } else if (data_out_sz) /* WRITE */ {
787 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
788 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
789 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
790 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
791 ioc->base_add_sg_single(psge, sgl_flags |
792 data_out_sz, data_out_dma);
793 } else if (data_in_sz) /* READ */ {
794 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
795 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
796 MPI2_SGE_FLAGS_END_OF_LIST);
797 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
798 ioc->base_add_sg_single(psge, sgl_flags |
799 data_in_sz, data_in_dma);
802 /* send command to firmware */
803 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
804 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
805 #endif
807 init_completion(&ioc->ctl_cmds.done);
808 switch (mpi_request->Function) {
809 case MPI2_FUNCTION_SCSI_IO_REQUEST:
810 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
812 Mpi2SCSIIORequest_t *scsiio_request =
813 (Mpi2SCSIIORequest_t *)request;
814 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
815 scsiio_request->SenseBufferLowAddress =
816 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
817 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
818 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
819 mpt2sas_base_put_smid_scsi_io(ioc, smid,
820 le16_to_cpu(mpi_request->FunctionDependent1));
821 else
822 mpt2sas_base_put_smid_default(ioc, smid);
823 break;
825 case MPI2_FUNCTION_SCSI_TASK_MGMT:
827 Mpi2SCSITaskManagementRequest_t *tm_request =
828 (Mpi2SCSITaskManagementRequest_t *)request;
830 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
831 "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
832 le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
834 if (tm_request->TaskType ==
835 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
836 tm_request->TaskType ==
837 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
838 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
839 mpt2sas_base_free_smid(ioc, smid);
840 goto out;
844 mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
845 tm_request->DevHandle));
846 mpt2sas_base_put_smid_hi_priority(ioc, smid);
847 break;
849 case MPI2_FUNCTION_SMP_PASSTHROUGH:
851 Mpi2SmpPassthroughRequest_t *smp_request =
852 (Mpi2SmpPassthroughRequest_t *)mpi_request;
853 u8 *data;
855 /* ioc determines which port to use */
856 smp_request->PhysicalPort = 0xFF;
857 if (smp_request->PassthroughFlags &
858 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
859 data = (u8 *)&smp_request->SGL;
860 else {
861 if (unlikely(data_out == NULL)) {
862 printk(KERN_ERR "failure at %s:%d/%s()!\n",
863 __FILE__, __LINE__, __func__);
864 mpt2sas_base_free_smid(ioc, smid);
865 ret = -EINVAL;
866 goto out;
868 data = data_out;
871 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
872 ioc->ioc_link_reset_in_progress = 1;
873 ioc->ignore_loginfos = 1;
875 mpt2sas_base_put_smid_default(ioc, smid);
876 break;
878 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
880 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
881 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
883 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
884 || sasiounit_request->Operation ==
885 MPI2_SAS_OP_PHY_LINK_RESET) {
886 ioc->ioc_link_reset_in_progress = 1;
887 ioc->ignore_loginfos = 1;
889 mpt2sas_base_put_smid_default(ioc, smid);
890 break;
892 default:
893 mpt2sas_base_put_smid_default(ioc, smid);
894 break;
897 if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
898 timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
899 else
900 timeout = karg.timeout;
901 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
902 timeout*HZ);
903 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
904 Mpi2SCSITaskManagementRequest_t *tm_request =
905 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
906 mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
907 tm_request->DevHandle));
908 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
909 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
910 ioc->ioc_link_reset_in_progress) {
911 ioc->ioc_link_reset_in_progress = 0;
912 ioc->ignore_loginfos = 0;
914 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
915 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
916 __func__);
917 _debug_dump_mf(mpi_request, karg.data_sge_offset);
918 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
919 issue_reset = 1;
920 goto issue_host_reset;
923 mpi_reply = ioc->ctl_cmds.reply;
924 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
926 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
927 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
928 (ioc->logging_level & MPT_DEBUG_TM)) {
929 Mpi2SCSITaskManagementReply_t *tm_reply =
930 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
932 printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
933 "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
934 "TerminationCount(0x%08x)\n", ioc->name,
935 le16_to_cpu(tm_reply->IOCStatus),
936 le32_to_cpu(tm_reply->IOCLogInfo),
937 le32_to_cpu(tm_reply->TerminationCount));
939 #endif
940 /* copy out xdata to user */
941 if (data_in_sz) {
942 if (copy_to_user(karg.data_in_buf_ptr, data_in,
943 data_in_sz)) {
944 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
945 __LINE__, __func__);
946 ret = -ENODATA;
947 goto out;
951 /* copy out reply message frame to user */
952 if (karg.max_reply_bytes) {
953 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
954 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
955 sz)) {
956 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
957 __LINE__, __func__);
958 ret = -ENODATA;
959 goto out;
963 /* copy out sense to user */
964 if (karg.max_sense_bytes && (mpi_request->Function ==
965 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
966 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
967 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
968 if (copy_to_user(karg.sense_data_ptr,
969 ioc->ctl_cmds.sense, sz)) {
970 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
971 __LINE__, __func__);
972 ret = -ENODATA;
973 goto out;
977 issue_host_reset:
978 if (issue_reset) {
979 ret = -ENODATA;
980 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
981 mpi_request->Function ==
982 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
983 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
984 printk(MPT2SAS_INFO_FMT "issue target reset: handle "
985 "= (0x%04x)\n", ioc->name,
986 le16_to_cpu(mpi_request->FunctionDependent1));
987 mpt2sas_halt_firmware(ioc);
988 mpt2sas_scsih_issue_tm(ioc,
989 le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
990 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
991 TM_MUTEX_ON);
992 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
993 } else
994 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
995 FORCE_BIG_HAMMER);
998 out:
1000 /* free memory associated with sg buffers */
1001 if (data_in)
1002 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1003 data_in_dma);
1005 if (data_out)
1006 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1007 data_out_dma);
1009 kfree(mpi_request);
1010 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1011 return ret;
1015 * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
1016 * @ioc: per adapter object
1017 * @arg - user space buffer containing ioctl content
1019 static long
1020 _ctl_getiocinfo(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1022 struct mpt2_ioctl_iocinfo karg;
1024 if (copy_from_user(&karg, arg, sizeof(karg))) {
1025 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1026 __FILE__, __LINE__, __func__);
1027 return -EFAULT;
1030 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1031 __func__));
1033 memset(&karg, 0 , sizeof(karg));
1034 if (ioc->is_warpdrive)
1035 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1036 else
1037 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1038 if (ioc->pfacts)
1039 karg.port_number = ioc->pfacts[0].PortNumber;
1040 karg.hw_rev = ioc->pdev->revision;
1041 karg.pci_id = ioc->pdev->device;
1042 karg.subsystem_device = ioc->pdev->subsystem_device;
1043 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1044 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1045 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1046 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1047 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1048 karg.firmware_version = ioc->facts.FWVersion.Word;
1049 strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
1050 strcat(karg.driver_version, "-");
1051 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1052 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1054 if (copy_to_user(arg, &karg, sizeof(karg))) {
1055 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1056 __FILE__, __LINE__, __func__);
1057 return -EFAULT;
1059 return 0;
1063 * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
1064 * @ioc: per adapter object
1065 * @arg - user space buffer containing ioctl content
1067 static long
1068 _ctl_eventquery(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1070 struct mpt2_ioctl_eventquery karg;
1072 if (copy_from_user(&karg, arg, sizeof(karg))) {
1073 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1074 __FILE__, __LINE__, __func__);
1075 return -EFAULT;
1078 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1079 __func__));
1081 karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
1082 memcpy(karg.event_types, ioc->event_type,
1083 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1085 if (copy_to_user(arg, &karg, sizeof(karg))) {
1086 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1087 __FILE__, __LINE__, __func__);
1088 return -EFAULT;
1090 return 0;
1094 * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1095 * @ioc: per adapter object
1096 * @arg - user space buffer containing ioctl content
1098 static long
1099 _ctl_eventenable(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1101 struct mpt2_ioctl_eventenable karg;
1103 if (copy_from_user(&karg, arg, sizeof(karg))) {
1104 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1105 __FILE__, __LINE__, __func__);
1106 return -EFAULT;
1109 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1110 __func__));
1112 if (ioc->event_log)
1113 return 0;
1114 memcpy(ioc->event_type, karg.event_types,
1115 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1116 mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1118 /* initialize event_log */
1119 ioc->event_context = 0;
1120 ioc->aen_event_read_flag = 0;
1121 ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1122 sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1123 if (!ioc->event_log) {
1124 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1125 __FILE__, __LINE__, __func__);
1126 return -ENOMEM;
1128 return 0;
1132 * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1133 * @ioc: per adapter object
1134 * @arg - user space buffer containing ioctl content
1136 static long
1137 _ctl_eventreport(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1139 struct mpt2_ioctl_eventreport karg;
1140 u32 number_bytes, max_events, max;
1141 struct mpt2_ioctl_eventreport __user *uarg = arg;
1143 if (copy_from_user(&karg, arg, sizeof(karg))) {
1144 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1145 __FILE__, __LINE__, __func__);
1146 return -EFAULT;
1149 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1150 __func__));
1152 number_bytes = karg.hdr.max_data_size -
1153 sizeof(struct mpt2_ioctl_header);
1154 max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1155 max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1157 /* If fewer than 1 event is requested, there must have
1158 * been some type of error.
1160 if (!max || !ioc->event_log)
1161 return -ENODATA;
1163 number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1164 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1165 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1166 __FILE__, __LINE__, __func__);
1167 return -EFAULT;
1170 /* reset flag so SIGIO can restart */
1171 ioc->aen_event_read_flag = 0;
1172 return 0;
1176 * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1177 * @ioc: per adapter object
1178 * @arg - user space buffer containing ioctl content
1180 static long
1181 _ctl_do_reset(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1183 struct mpt2_ioctl_diag_reset karg;
1184 int retval;
1186 if (copy_from_user(&karg, arg, sizeof(karg))) {
1187 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1188 __FILE__, __LINE__, __func__);
1189 return -EFAULT;
1192 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1193 ioc->is_driver_loading)
1194 return -EAGAIN;
1195 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1196 __func__));
1198 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1199 FORCE_BIG_HAMMER);
1200 printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1201 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1202 return 0;
1206 * _ctl_btdh_search_sas_device - searching for sas device
1207 * @ioc: per adapter object
1208 * @btdh: btdh ioctl payload
1210 static int
1211 _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1212 struct mpt2_ioctl_btdh_mapping *btdh)
1214 struct _sas_device *sas_device;
1215 unsigned long flags;
1216 int rc = 0;
1218 if (list_empty(&ioc->sas_device_list))
1219 return rc;
1221 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1222 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1223 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1224 btdh->handle == sas_device->handle) {
1225 btdh->bus = sas_device->channel;
1226 btdh->id = sas_device->id;
1227 rc = 1;
1228 goto out;
1229 } else if (btdh->bus == sas_device->channel && btdh->id ==
1230 sas_device->id && btdh->handle == 0xFFFF) {
1231 btdh->handle = sas_device->handle;
1232 rc = 1;
1233 goto out;
1236 out:
1237 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1238 return rc;
1242 * _ctl_btdh_search_raid_device - searching for raid device
1243 * @ioc: per adapter object
1244 * @btdh: btdh ioctl payload
1246 static int
1247 _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1248 struct mpt2_ioctl_btdh_mapping *btdh)
1250 struct _raid_device *raid_device;
1251 unsigned long flags;
1252 int rc = 0;
1254 if (list_empty(&ioc->raid_device_list))
1255 return rc;
1257 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1258 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1259 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1260 btdh->handle == raid_device->handle) {
1261 btdh->bus = raid_device->channel;
1262 btdh->id = raid_device->id;
1263 rc = 1;
1264 goto out;
1265 } else if (btdh->bus == raid_device->channel && btdh->id ==
1266 raid_device->id && btdh->handle == 0xFFFF) {
1267 btdh->handle = raid_device->handle;
1268 rc = 1;
1269 goto out;
1272 out:
1273 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1274 return rc;
1278 * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1279 * @ioc: per adapter object
1280 * @arg - user space buffer containing ioctl content
1282 static long
1283 _ctl_btdh_mapping(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1285 struct mpt2_ioctl_btdh_mapping karg;
1286 int rc;
1288 if (copy_from_user(&karg, arg, sizeof(karg))) {
1289 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1290 __FILE__, __LINE__, __func__);
1291 return -EFAULT;
1294 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1295 __func__));
1297 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1298 if (!rc)
1299 _ctl_btdh_search_raid_device(ioc, &karg);
1301 if (copy_to_user(arg, &karg, sizeof(karg))) {
1302 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1303 __FILE__, __LINE__, __func__);
1304 return -EFAULT;
1306 return 0;
1310 * _ctl_diag_capability - return diag buffer capability
1311 * @ioc: per adapter object
1312 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1314 * returns 1 when diag buffer support is enabled in firmware
1316 static u8
1317 _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1319 u8 rc = 0;
1321 switch (buffer_type) {
1322 case MPI2_DIAG_BUF_TYPE_TRACE:
1323 if (ioc->facts.IOCCapabilities &
1324 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1325 rc = 1;
1326 break;
1327 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1328 if (ioc->facts.IOCCapabilities &
1329 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1330 rc = 1;
1331 break;
1332 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1333 if (ioc->facts.IOCCapabilities &
1334 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1335 rc = 1;
1338 return rc;
1342 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1343 * @ioc: per adapter object
1344 * @diag_register: the diag_register struct passed in from user space
1347 static long
1348 _ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
1349 struct mpt2_diag_register *diag_register)
1351 int rc, i;
1352 void *request_data = NULL;
1353 dma_addr_t request_data_dma;
1354 u32 request_data_sz = 0;
1355 Mpi2DiagBufferPostRequest_t *mpi_request;
1356 Mpi2DiagBufferPostReply_t *mpi_reply;
1357 u8 buffer_type;
1358 unsigned long timeleft;
1359 u16 smid;
1360 u16 ioc_status;
1361 u8 issue_reset = 0;
1363 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1364 __func__));
1366 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1367 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1368 ioc->name, __func__);
1369 rc = -EAGAIN;
1370 goto out;
1373 buffer_type = diag_register->buffer_type;
1374 if (!_ctl_diag_capability(ioc, buffer_type)) {
1375 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1376 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1377 return -EPERM;
1380 if (ioc->diag_buffer_status[buffer_type] &
1381 MPT2_DIAG_BUFFER_IS_REGISTERED) {
1382 printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1383 "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1384 buffer_type);
1385 return -EINVAL;
1388 if (diag_register->requested_buffer_size % 4) {
1389 printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1390 "is not 4 byte aligned\n", ioc->name, __func__);
1391 return -EINVAL;
1394 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1395 if (!smid) {
1396 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1397 ioc->name, __func__);
1398 rc = -EAGAIN;
1399 goto out;
1402 rc = 0;
1403 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1404 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1405 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1406 ioc->ctl_cmds.smid = smid;
1408 request_data = ioc->diag_buffer[buffer_type];
1409 request_data_sz = diag_register->requested_buffer_size;
1410 ioc->unique_id[buffer_type] = diag_register->unique_id;
1411 ioc->diag_buffer_status[buffer_type] = 0;
1412 memcpy(ioc->product_specific[buffer_type],
1413 diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
1414 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1416 if (request_data) {
1417 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1418 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1419 pci_free_consistent(ioc->pdev,
1420 ioc->diag_buffer_sz[buffer_type],
1421 request_data, request_data_dma);
1422 request_data = NULL;
1426 if (request_data == NULL) {
1427 ioc->diag_buffer_sz[buffer_type] = 0;
1428 ioc->diag_buffer_dma[buffer_type] = 0;
1429 request_data = pci_alloc_consistent(
1430 ioc->pdev, request_data_sz, &request_data_dma);
1431 if (request_data == NULL) {
1432 printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1433 " for diag buffers, requested size(%d)\n",
1434 ioc->name, __func__, request_data_sz);
1435 mpt2sas_base_free_smid(ioc, smid);
1436 return -ENOMEM;
1438 ioc->diag_buffer[buffer_type] = request_data;
1439 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1440 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1443 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1444 mpi_request->BufferType = diag_register->buffer_type;
1445 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1446 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1447 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1448 mpi_request->VF_ID = 0; /* TODO */
1449 mpi_request->VP_ID = 0;
1451 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(0x%p), "
1452 "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1453 (unsigned long long)request_data_dma,
1454 le32_to_cpu(mpi_request->BufferLength)));
1456 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1457 mpi_request->ProductSpecific[i] =
1458 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1460 init_completion(&ioc->ctl_cmds.done);
1461 mpt2sas_base_put_smid_default(ioc, smid);
1462 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1463 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1465 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1466 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1467 __func__);
1468 _debug_dump_mf(mpi_request,
1469 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1470 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1471 issue_reset = 1;
1472 goto issue_host_reset;
1475 /* process the completed Reply Message Frame */
1476 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1477 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1478 ioc->name, __func__);
1479 rc = -EFAULT;
1480 goto out;
1483 mpi_reply = ioc->ctl_cmds.reply;
1484 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1486 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1487 ioc->diag_buffer_status[buffer_type] |=
1488 MPT2_DIAG_BUFFER_IS_REGISTERED;
1489 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1490 ioc->name, __func__));
1491 } else {
1492 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1493 "log_info(0x%08x)\n", ioc->name, __func__,
1494 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1495 rc = -EFAULT;
1498 issue_host_reset:
1499 if (issue_reset)
1500 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1501 FORCE_BIG_HAMMER);
1503 out:
1505 if (rc && request_data)
1506 pci_free_consistent(ioc->pdev, request_data_sz,
1507 request_data, request_data_dma);
1509 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1510 return rc;
1514 * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
1515 * @ioc: per adapter object
1516 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1518 * This is called when command line option diag_buffer_enable is enabled
1519 * at driver load time.
1521 void
1522 mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
1524 struct mpt2_diag_register diag_register;
1526 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
1528 if (bits_to_register & 1) {
1529 printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
1530 ioc->name);
1531 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1532 /* register for 1MB buffers */
1533 diag_register.requested_buffer_size = (1024 * 1024);
1534 diag_register.unique_id = 0x7075900;
1535 _ctl_diag_register_2(ioc, &diag_register);
1538 if (bits_to_register & 2) {
1539 printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
1540 ioc->name);
1541 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1542 /* register for 2MB buffers */
1543 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1544 diag_register.unique_id = 0x7075901;
1545 _ctl_diag_register_2(ioc, &diag_register);
1548 if (bits_to_register & 4) {
1549 printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
1550 ioc->name);
1551 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1552 /* register for 2MB buffers */
1553 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1554 diag_register.unique_id = 0x7075901;
1555 _ctl_diag_register_2(ioc, &diag_register);
1560 * _ctl_diag_register - application register with driver
1561 * @ioc: per adapter object
1562 * @arg - user space buffer containing ioctl content
1564 * This will allow the driver to setup any required buffers that will be
1565 * needed by firmware to communicate with the driver.
1567 static long
1568 _ctl_diag_register(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1570 struct mpt2_diag_register karg;
1571 long rc;
1573 if (copy_from_user(&karg, arg, sizeof(karg))) {
1574 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1575 __FILE__, __LINE__, __func__);
1576 return -EFAULT;
1579 rc = _ctl_diag_register_2(ioc, &karg);
1580 return rc;
1584 * _ctl_diag_unregister - application unregister with driver
1585 * @ioc: per adapter object
1586 * @arg - user space buffer containing ioctl content
1588 * This will allow the driver to cleanup any memory allocated for diag
1589 * messages and to free up any resources.
1591 static long
1592 _ctl_diag_unregister(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1594 struct mpt2_diag_unregister karg;
1595 void *request_data;
1596 dma_addr_t request_data_dma;
1597 u32 request_data_sz;
1598 u8 buffer_type;
1600 if (copy_from_user(&karg, arg, sizeof(karg))) {
1601 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1602 __FILE__, __LINE__, __func__);
1603 return -EFAULT;
1606 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1607 __func__));
1609 buffer_type = karg.unique_id & 0x000000ff;
1610 if (!_ctl_diag_capability(ioc, buffer_type)) {
1611 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1612 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1613 return -EPERM;
1616 if ((ioc->diag_buffer_status[buffer_type] &
1617 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1618 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1619 "registered\n", ioc->name, __func__, buffer_type);
1620 return -EINVAL;
1622 if ((ioc->diag_buffer_status[buffer_type] &
1623 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1624 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1625 "released\n", ioc->name, __func__, buffer_type);
1626 return -EINVAL;
1629 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1630 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1631 "registered\n", ioc->name, __func__, karg.unique_id);
1632 return -EINVAL;
1635 request_data = ioc->diag_buffer[buffer_type];
1636 if (!request_data) {
1637 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1638 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1639 return -ENOMEM;
1642 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1643 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1644 pci_free_consistent(ioc->pdev, request_data_sz,
1645 request_data, request_data_dma);
1646 ioc->diag_buffer[buffer_type] = NULL;
1647 ioc->diag_buffer_status[buffer_type] = 0;
1648 return 0;
1652 * _ctl_diag_query - query relevant info associated with diag buffers
1653 * @ioc: per adapter object
1654 * @arg - user space buffer containing ioctl content
1656 * The application will send only buffer_type and unique_id. Driver will
1657 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1658 * 0x00, the driver will return info specified by Buffer Type.
1660 static long
1661 _ctl_diag_query(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1663 struct mpt2_diag_query karg;
1664 void *request_data;
1665 int i;
1666 u8 buffer_type;
1668 if (copy_from_user(&karg, arg, sizeof(karg))) {
1669 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1670 __FILE__, __LINE__, __func__);
1671 return -EFAULT;
1674 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1675 __func__));
1677 karg.application_flags = 0;
1678 buffer_type = karg.buffer_type;
1680 if (!_ctl_diag_capability(ioc, buffer_type)) {
1681 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1682 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1683 return -EPERM;
1686 if ((ioc->diag_buffer_status[buffer_type] &
1687 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1688 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1689 "registered\n", ioc->name, __func__, buffer_type);
1690 return -EINVAL;
1693 if (karg.unique_id & 0xffffff00) {
1694 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1695 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1696 "registered\n", ioc->name, __func__,
1697 karg.unique_id);
1698 return -EINVAL;
1702 request_data = ioc->diag_buffer[buffer_type];
1703 if (!request_data) {
1704 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1705 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1706 return -ENOMEM;
1709 if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1710 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1711 MPT2_APP_FLAGS_BUFFER_VALID);
1712 else
1713 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1714 MPT2_APP_FLAGS_BUFFER_VALID |
1715 MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1717 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1718 karg.product_specific[i] =
1719 ioc->product_specific[buffer_type][i];
1721 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1722 karg.driver_added_buffer_size = 0;
1723 karg.unique_id = ioc->unique_id[buffer_type];
1724 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1726 if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1727 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1728 "data @ %p\n", ioc->name, __func__, arg);
1729 return -EFAULT;
1731 return 0;
1735 * _ctl_send_release - Diag Release Message
1736 * @ioc: per adapter object
1737 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1738 * @issue_reset - specifies whether host reset is required.
1741 static int
1742 _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1744 Mpi2DiagReleaseRequest_t *mpi_request;
1745 Mpi2DiagReleaseReply_t *mpi_reply;
1746 u16 smid;
1747 u16 ioc_status;
1748 u32 ioc_state;
1749 int rc;
1750 unsigned long timeleft;
1752 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1753 __func__));
1755 rc = 0;
1756 *issue_reset = 0;
1758 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1759 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1760 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
1761 "skipping due to FAULT state\n", ioc->name,
1762 __func__));
1763 rc = -EAGAIN;
1764 goto out;
1767 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1768 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1769 ioc->name, __func__);
1770 rc = -EAGAIN;
1771 goto out;
1774 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1775 if (!smid) {
1776 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1777 ioc->name, __func__);
1778 rc = -EAGAIN;
1779 goto out;
1782 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1783 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1784 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1785 ioc->ctl_cmds.smid = smid;
1787 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1788 mpi_request->BufferType = buffer_type;
1789 mpi_request->VF_ID = 0; /* TODO */
1790 mpi_request->VP_ID = 0;
1792 init_completion(&ioc->ctl_cmds.done);
1793 mpt2sas_base_put_smid_default(ioc, smid);
1794 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1795 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1797 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1798 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1799 __func__);
1800 _debug_dump_mf(mpi_request,
1801 sizeof(Mpi2DiagReleaseRequest_t)/4);
1802 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1803 *issue_reset = 1;
1804 rc = -EFAULT;
1805 goto out;
1808 /* process the completed Reply Message Frame */
1809 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1810 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1811 ioc->name, __func__);
1812 rc = -EFAULT;
1813 goto out;
1816 mpi_reply = ioc->ctl_cmds.reply;
1817 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1819 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1820 ioc->diag_buffer_status[buffer_type] |=
1821 MPT2_DIAG_BUFFER_IS_RELEASED;
1822 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1823 ioc->name, __func__));
1824 } else {
1825 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1826 "log_info(0x%08x)\n", ioc->name, __func__,
1827 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1828 rc = -EFAULT;
1831 out:
1832 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1833 return rc;
1837 * _ctl_diag_release - request to send Diag Release Message to firmware
1838 * @arg - user space buffer containing ioctl content
1840 * This allows ownership of the specified buffer to returned to the driver,
1841 * allowing an application to read the buffer without fear that firmware is
1842 * overwritting information in the buffer.
1844 static long
1845 _ctl_diag_release(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1847 struct mpt2_diag_release karg;
1848 void *request_data;
1849 int rc;
1850 u8 buffer_type;
1851 u8 issue_reset = 0;
1853 if (copy_from_user(&karg, arg, sizeof(karg))) {
1854 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1855 __FILE__, __LINE__, __func__);
1856 return -EFAULT;
1859 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1860 __func__));
1862 buffer_type = karg.unique_id & 0x000000ff;
1863 if (!_ctl_diag_capability(ioc, buffer_type)) {
1864 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1865 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1866 return -EPERM;
1869 if ((ioc->diag_buffer_status[buffer_type] &
1870 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1871 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1872 "registered\n", ioc->name, __func__, buffer_type);
1873 return -EINVAL;
1876 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1877 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1878 "registered\n", ioc->name, __func__, karg.unique_id);
1879 return -EINVAL;
1882 if (ioc->diag_buffer_status[buffer_type] &
1883 MPT2_DIAG_BUFFER_IS_RELEASED) {
1884 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1885 "is already released\n", ioc->name, __func__,
1886 buffer_type);
1887 return 0;
1890 request_data = ioc->diag_buffer[buffer_type];
1892 if (!request_data) {
1893 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1894 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1895 return -ENOMEM;
1898 /* buffers were released by due to host reset */
1899 if ((ioc->diag_buffer_status[buffer_type] &
1900 MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1901 ioc->diag_buffer_status[buffer_type] |=
1902 MPT2_DIAG_BUFFER_IS_RELEASED;
1903 ioc->diag_buffer_status[buffer_type] &=
1904 ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1905 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1906 "was released due to host reset\n", ioc->name, __func__,
1907 buffer_type);
1908 return 0;
1911 rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1913 if (issue_reset)
1914 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1915 FORCE_BIG_HAMMER);
1917 return rc;
1921 * _ctl_diag_read_buffer - request for copy of the diag buffer
1922 * @ioc: per adapter object
1923 * @arg - user space buffer containing ioctl content
1925 static long
1926 _ctl_diag_read_buffer(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1928 struct mpt2_diag_read_buffer karg;
1929 struct mpt2_diag_read_buffer __user *uarg = arg;
1930 void *request_data, *diag_data;
1931 Mpi2DiagBufferPostRequest_t *mpi_request;
1932 Mpi2DiagBufferPostReply_t *mpi_reply;
1933 int rc, i;
1934 u8 buffer_type;
1935 unsigned long timeleft, request_size, copy_size;
1936 u16 smid;
1937 u16 ioc_status;
1938 u8 issue_reset = 0;
1940 if (copy_from_user(&karg, arg, sizeof(karg))) {
1941 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1942 __FILE__, __LINE__, __func__);
1943 return -EFAULT;
1946 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1947 __func__));
1949 buffer_type = karg.unique_id & 0x000000ff;
1950 if (!_ctl_diag_capability(ioc, buffer_type)) {
1951 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1952 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1953 return -EPERM;
1956 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1957 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1958 "registered\n", ioc->name, __func__, karg.unique_id);
1959 return -EINVAL;
1962 request_data = ioc->diag_buffer[buffer_type];
1963 if (!request_data) {
1964 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1965 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1966 return -ENOMEM;
1969 request_size = ioc->diag_buffer_sz[buffer_type];
1971 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1972 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1973 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1974 __func__);
1975 return -EINVAL;
1978 if (karg.starting_offset > request_size)
1979 return -EINVAL;
1981 diag_data = (void *)(request_data + karg.starting_offset);
1982 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
1983 "offset(%d), sz(%d)\n", ioc->name, __func__,
1984 diag_data, karg.starting_offset, karg.bytes_to_read));
1986 /* Truncate data on requests that are too large */
1987 if ((diag_data + karg.bytes_to_read < diag_data) ||
1988 (diag_data + karg.bytes_to_read > request_data + request_size))
1989 copy_size = request_size - karg.starting_offset;
1990 else
1991 copy_size = karg.bytes_to_read;
1993 if (copy_to_user((void __user *)uarg->diagnostic_data,
1994 diag_data, copy_size)) {
1995 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
1996 "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
1997 __func__, diag_data);
1998 return -EFAULT;
2001 if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
2002 return 0;
2004 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
2005 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
2006 if ((ioc->diag_buffer_status[buffer_type] &
2007 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
2008 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2009 "buffer_type(0x%02x) is still registered\n", ioc->name,
2010 __func__, buffer_type));
2011 return 0;
2013 /* Get a free request frame and save the message context.
2016 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
2017 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
2018 ioc->name, __func__);
2019 rc = -EAGAIN;
2020 goto out;
2023 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2024 if (!smid) {
2025 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2026 ioc->name, __func__);
2027 rc = -EAGAIN;
2028 goto out;
2031 rc = 0;
2032 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
2033 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2034 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2035 ioc->ctl_cmds.smid = smid;
2037 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2038 mpi_request->BufferType = buffer_type;
2039 mpi_request->BufferLength =
2040 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2041 mpi_request->BufferAddress =
2042 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2043 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
2044 mpi_request->ProductSpecific[i] =
2045 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2046 mpi_request->VF_ID = 0; /* TODO */
2047 mpi_request->VP_ID = 0;
2049 init_completion(&ioc->ctl_cmds.done);
2050 mpt2sas_base_put_smid_default(ioc, smid);
2051 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2052 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
2054 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
2055 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
2056 __func__);
2057 _debug_dump_mf(mpi_request,
2058 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2059 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
2060 issue_reset = 1;
2061 goto issue_host_reset;
2064 /* process the completed Reply Message Frame */
2065 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
2066 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
2067 ioc->name, __func__);
2068 rc = -EFAULT;
2069 goto out;
2072 mpi_reply = ioc->ctl_cmds.reply;
2073 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2075 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2076 ioc->diag_buffer_status[buffer_type] |=
2077 MPT2_DIAG_BUFFER_IS_REGISTERED;
2078 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
2079 ioc->name, __func__));
2080 } else {
2081 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
2082 "log_info(0x%08x)\n", ioc->name, __func__,
2083 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2084 rc = -EFAULT;
2087 issue_host_reset:
2088 if (issue_reset)
2089 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2090 FORCE_BIG_HAMMER);
2092 out:
2094 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
2095 return rc;
2099 #ifdef CONFIG_COMPAT
2101 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2102 * @ioc: per adapter object
2103 * @cmd - ioctl opcode
2104 * @arg - (struct mpt2_ioctl_command32)
2106 * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2108 static long
2109 _ctl_compat_mpt_command(struct MPT2SAS_ADAPTER *ioc, unsigned cmd,
2110 void __user *arg)
2112 struct mpt2_ioctl_command32 karg32;
2113 struct mpt2_ioctl_command32 __user *uarg;
2114 struct mpt2_ioctl_command karg;
2116 if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2117 return -EINVAL;
2119 uarg = (struct mpt2_ioctl_command32 __user *) arg;
2121 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2122 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2123 __FILE__, __LINE__, __func__);
2124 return -EFAULT;
2127 memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2128 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2129 karg.hdr.port_number = karg32.hdr.port_number;
2130 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2131 karg.timeout = karg32.timeout;
2132 karg.max_reply_bytes = karg32.max_reply_bytes;
2133 karg.data_in_size = karg32.data_in_size;
2134 karg.data_out_size = karg32.data_out_size;
2135 karg.max_sense_bytes = karg32.max_sense_bytes;
2136 karg.data_sge_offset = karg32.data_sge_offset;
2137 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2138 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2139 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2140 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2141 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2143 #endif
2146 * _ctl_ioctl_main - main ioctl entry point
2147 * @file - (struct file)
2148 * @cmd - ioctl opcode
2149 * @arg -
2150 * compat - handles 32 bit applications in 64bit os
2152 static long
2153 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2154 u8 compat)
2156 struct MPT2SAS_ADAPTER *ioc;
2157 struct mpt2_ioctl_header ioctl_header;
2158 enum block_state state;
2159 long ret = -EINVAL;
2161 /* get IOCTL header */
2162 if (copy_from_user(&ioctl_header, (char __user *)arg,
2163 sizeof(struct mpt2_ioctl_header))) {
2164 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2165 __FILE__, __LINE__, __func__);
2166 return -EFAULT;
2169 if (_ctl_verify_adapter(ioctl_header.ioc_number, &ioc) == -1 || !ioc)
2170 return -ENODEV;
2171 if (ioc->shost_recovery || ioc->pci_error_recovery ||
2172 ioc->is_driver_loading)
2173 return -EAGAIN;
2175 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2176 if (state == NON_BLOCKING) {
2177 if (!mutex_trylock(&ioc->ctl_cmds.mutex))
2178 return -EAGAIN;
2179 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2180 return -ERESTARTSYS;
2183 switch (cmd) {
2184 case MPT2IOCINFO:
2185 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
2186 ret = _ctl_getiocinfo(ioc, arg);
2187 break;
2188 #ifdef CONFIG_COMPAT
2189 case MPT2COMMAND32:
2190 #endif
2191 case MPT2COMMAND:
2193 struct mpt2_ioctl_command __user *uarg;
2194 struct mpt2_ioctl_command karg;
2195 #ifdef CONFIG_COMPAT
2196 if (compat) {
2197 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2198 break;
2200 #endif
2201 if (copy_from_user(&karg, arg, sizeof(karg))) {
2202 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2203 __FILE__, __LINE__, __func__);
2204 ret = -EFAULT;
2205 break;
2208 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2209 uarg = arg;
2210 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2212 break;
2214 case MPT2EVENTQUERY:
2215 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2216 ret = _ctl_eventquery(ioc, arg);
2217 break;
2218 case MPT2EVENTENABLE:
2219 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2220 ret = _ctl_eventenable(ioc, arg);
2221 break;
2222 case MPT2EVENTREPORT:
2223 ret = _ctl_eventreport(ioc, arg);
2224 break;
2225 case MPT2HARDRESET:
2226 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2227 ret = _ctl_do_reset(ioc, arg);
2228 break;
2229 case MPT2BTDHMAPPING:
2230 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2231 ret = _ctl_btdh_mapping(ioc, arg);
2232 break;
2233 case MPT2DIAGREGISTER:
2234 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2235 ret = _ctl_diag_register(ioc, arg);
2236 break;
2237 case MPT2DIAGUNREGISTER:
2238 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2239 ret = _ctl_diag_unregister(ioc, arg);
2240 break;
2241 case MPT2DIAGQUERY:
2242 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2243 ret = _ctl_diag_query(ioc, arg);
2244 break;
2245 case MPT2DIAGRELEASE:
2246 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2247 ret = _ctl_diag_release(ioc, arg);
2248 break;
2249 case MPT2DIAGREADBUFFER:
2250 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2251 ret = _ctl_diag_read_buffer(ioc, arg);
2252 break;
2253 default:
2255 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
2256 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2257 break;
2260 mutex_unlock(&ioc->ctl_cmds.mutex);
2261 return ret;
2265 * _ctl_ioctl - main ioctl entry point (unlocked)
2266 * @file - (struct file)
2267 * @cmd - ioctl opcode
2268 * @arg -
2270 static long
2271 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2273 long ret;
2275 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0);
2276 return ret;
2278 #ifdef CONFIG_COMPAT
2280 * _ctl_ioctl_compat - main ioctl entry point (compat)
2281 * @file -
2282 * @cmd -
2283 * @arg -
2285 * This routine handles 32 bit applications in 64bit os.
2287 static long
2288 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2290 long ret;
2292 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1);
2293 return ret;
2295 #endif
2297 /* scsi host attributes */
2300 * _ctl_version_fw_show - firmware version
2301 * @cdev - pointer to embedded class device
2302 * @buf - the buffer returned
2304 * A sysfs 'read-only' shost attribute.
2306 static ssize_t
2307 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2308 char *buf)
2310 struct Scsi_Host *shost = class_to_shost(cdev);
2311 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2313 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2314 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2315 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2316 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2317 ioc->facts.FWVersion.Word & 0x000000FF);
2319 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2322 * _ctl_version_bios_show - bios version
2323 * @cdev - pointer to embedded class device
2324 * @buf - the buffer returned
2326 * A sysfs 'read-only' shost attribute.
2328 static ssize_t
2329 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2330 char *buf)
2332 struct Scsi_Host *shost = class_to_shost(cdev);
2333 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2335 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2337 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2338 (version & 0xFF000000) >> 24,
2339 (version & 0x00FF0000) >> 16,
2340 (version & 0x0000FF00) >> 8,
2341 version & 0x000000FF);
2343 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2346 * _ctl_version_mpi_show - MPI (message passing interface) 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_mpi_show(struct device *cdev, struct device_attribute *attr,
2354 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, "%03x.%02x\n",
2360 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2362 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2365 * _ctl_version_product_show - product name
2366 * @cdev - pointer to embedded class device
2367 * @buf - the buffer returned
2369 * A sysfs 'read-only' shost attribute.
2371 static ssize_t
2372 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2373 char *buf)
2375 struct Scsi_Host *shost = class_to_shost(cdev);
2376 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2378 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2380 static DEVICE_ATTR(version_product, S_IRUGO,
2381 _ctl_version_product_show, NULL);
2384 * _ctl_version_nvdata_persistent_show - ndvata persistent version
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_version_nvdata_persistent_show(struct device *cdev,
2392 struct device_attribute *attr, char *buf)
2394 struct Scsi_Host *shost = class_to_shost(cdev);
2395 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2397 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2398 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2400 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2401 _ctl_version_nvdata_persistent_show, NULL);
2404 * _ctl_version_nvdata_default_show - nvdata default version
2405 * @cdev - pointer to embedded class device
2406 * @buf - the buffer returned
2408 * A sysfs 'read-only' shost attribute.
2410 static ssize_t
2411 _ctl_version_nvdata_default_show(struct device *cdev,
2412 struct device_attribute *attr, char *buf)
2414 struct Scsi_Host *shost = class_to_shost(cdev);
2415 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2417 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2418 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2420 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2421 _ctl_version_nvdata_default_show, NULL);
2424 * _ctl_board_name_show - board name
2425 * @cdev - pointer to embedded class device
2426 * @buf - the buffer returned
2428 * A sysfs 'read-only' shost attribute.
2430 static ssize_t
2431 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2432 char *buf)
2434 struct Scsi_Host *shost = class_to_shost(cdev);
2435 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2437 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2439 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2442 * _ctl_board_assembly_show - board assembly name
2443 * @cdev - pointer to embedded class device
2444 * @buf - the buffer returned
2446 * A sysfs 'read-only' shost attribute.
2448 static ssize_t
2449 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2450 char *buf)
2452 struct Scsi_Host *shost = class_to_shost(cdev);
2453 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2455 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2457 static DEVICE_ATTR(board_assembly, S_IRUGO,
2458 _ctl_board_assembly_show, NULL);
2461 * _ctl_board_tracer_show - board tracer number
2462 * @cdev - pointer to embedded class device
2463 * @buf - the buffer returned
2465 * A sysfs 'read-only' shost attribute.
2467 static ssize_t
2468 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2469 char *buf)
2471 struct Scsi_Host *shost = class_to_shost(cdev);
2472 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2474 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2476 static DEVICE_ATTR(board_tracer, S_IRUGO,
2477 _ctl_board_tracer_show, NULL);
2480 * _ctl_io_delay_show - io missing delay
2481 * @cdev - pointer to embedded class device
2482 * @buf - the buffer returned
2484 * This is for firmware implemention for deboucing device
2485 * removal events.
2487 * A sysfs 'read-only' shost attribute.
2489 static ssize_t
2490 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2491 char *buf)
2493 struct Scsi_Host *shost = class_to_shost(cdev);
2494 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2496 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2498 static DEVICE_ATTR(io_delay, S_IRUGO,
2499 _ctl_io_delay_show, NULL);
2502 * _ctl_device_delay_show - device missing delay
2503 * @cdev - pointer to embedded class device
2504 * @buf - the buffer returned
2506 * This is for firmware implemention for deboucing device
2507 * removal events.
2509 * A sysfs 'read-only' shost attribute.
2511 static ssize_t
2512 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2513 char *buf)
2515 struct Scsi_Host *shost = class_to_shost(cdev);
2516 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2518 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2520 static DEVICE_ATTR(device_delay, S_IRUGO,
2521 _ctl_device_delay_show, NULL);
2524 * _ctl_fw_queue_depth_show - global credits
2525 * @cdev - pointer to embedded class device
2526 * @buf - the buffer returned
2528 * This is firmware queue depth limit
2530 * A sysfs 'read-only' shost attribute.
2532 static ssize_t
2533 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2534 char *buf)
2536 struct Scsi_Host *shost = class_to_shost(cdev);
2537 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2539 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2541 static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2542 _ctl_fw_queue_depth_show, NULL);
2545 * _ctl_sas_address_show - sas address
2546 * @cdev - pointer to embedded class device
2547 * @buf - the buffer returned
2549 * This is the controller sas address
2551 * A sysfs 'read-only' shost attribute.
2553 static ssize_t
2554 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2555 char *buf)
2557 struct Scsi_Host *shost = class_to_shost(cdev);
2558 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2560 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2561 (unsigned long long)ioc->sas_hba.sas_address);
2563 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2564 _ctl_host_sas_address_show, NULL);
2567 * _ctl_logging_level_show - logging level
2568 * @cdev - pointer to embedded class device
2569 * @buf - the buffer returned
2571 * A sysfs 'read/write' shost attribute.
2573 static ssize_t
2574 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2575 char *buf)
2577 struct Scsi_Host *shost = class_to_shost(cdev);
2578 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2580 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2582 static ssize_t
2583 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2584 const char *buf, size_t count)
2586 struct Scsi_Host *shost = class_to_shost(cdev);
2587 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2588 int val = 0;
2590 if (sscanf(buf, "%x", &val) != 1)
2591 return -EINVAL;
2593 ioc->logging_level = val;
2594 printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2595 ioc->logging_level);
2596 return strlen(buf);
2598 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2599 _ctl_logging_level_show, _ctl_logging_level_store);
2601 /* device attributes */
2603 * _ctl_fwfault_debug_show - show/store fwfault_debug
2604 * @cdev - pointer to embedded class device
2605 * @buf - the buffer returned
2607 * mpt2sas_fwfault_debug is command line option
2608 * A sysfs 'read/write' shost attribute.
2610 static ssize_t
2611 _ctl_fwfault_debug_show(struct device *cdev,
2612 struct device_attribute *attr, char *buf)
2614 struct Scsi_Host *shost = class_to_shost(cdev);
2615 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2617 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2619 static ssize_t
2620 _ctl_fwfault_debug_store(struct device *cdev,
2621 struct device_attribute *attr, const char *buf, size_t count)
2623 struct Scsi_Host *shost = class_to_shost(cdev);
2624 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2625 int val = 0;
2627 if (sscanf(buf, "%d", &val) != 1)
2628 return -EINVAL;
2630 ioc->fwfault_debug = val;
2631 printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
2632 ioc->fwfault_debug);
2633 return strlen(buf);
2635 static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2636 _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2640 * _ctl_ioc_reset_count_show - ioc reset count
2641 * @cdev - pointer to embedded class device
2642 * @buf - the buffer returned
2644 * This is firmware queue depth limit
2646 * A sysfs 'read-only' shost attribute.
2648 static ssize_t
2649 _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2650 char *buf)
2652 struct Scsi_Host *shost = class_to_shost(cdev);
2653 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2655 return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
2657 static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
2658 _ctl_ioc_reset_count_show, NULL);
2661 * _ctl_ioc_reply_queue_count_show - number of reply queues
2662 * @cdev - pointer to embedded class device
2663 * @buf - the buffer returned
2665 * This is number of reply queues
2667 * A sysfs 'read-only' shost attribute.
2669 static ssize_t
2670 _ctl_ioc_reply_queue_count_show(struct device *cdev,
2671 struct device_attribute *attr, char *buf)
2673 u8 reply_queue_count;
2674 struct Scsi_Host *shost = class_to_shost(cdev);
2675 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2677 if ((ioc->facts.IOCCapabilities &
2678 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2679 reply_queue_count = ioc->reply_queue_count;
2680 else
2681 reply_queue_count = 1;
2682 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2684 static DEVICE_ATTR(reply_queue_count, S_IRUGO,
2685 _ctl_ioc_reply_queue_count_show, NULL);
2688 * _ctl_BRM_status_show - Backup Rail Monitor Status
2689 * @cdev - pointer to embedded class device
2690 * @buf - the buffer returned
2692 * This is number of reply queues
2694 * A sysfs 'read-only' shost attribute.
2696 static ssize_t
2697 _ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
2698 char *buf)
2700 struct Scsi_Host *shost = class_to_shost(cdev);
2701 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2702 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2703 Mpi2ConfigReply_t mpi_reply;
2704 u16 backup_rail_monitor_status = 0;
2705 u16 ioc_status;
2706 int sz;
2707 ssize_t rc = 0;
2709 if (!ioc->is_warpdrive) {
2710 printk(MPT2SAS_ERR_FMT "%s: BRM attribute is only for"\
2711 "warpdrive\n", ioc->name, __func__);
2712 goto out;
2715 /* allocate upto GPIOVal 36 entries */
2716 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2717 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2718 if (!io_unit_pg3) {
2719 printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"\
2720 "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
2721 goto out;
2724 if (mpt2sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2725 0) {
2726 printk(MPT2SAS_ERR_FMT
2727 "%s: failed reading iounit_pg3\n", ioc->name,
2728 __func__);
2729 goto out;
2732 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2733 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2734 printk(MPT2SAS_ERR_FMT "%s: iounit_pg3 failed with"\
2735 "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
2736 goto out;
2739 if (io_unit_pg3->GPIOCount < 25) {
2740 printk(MPT2SAS_ERR_FMT "%s: iounit_pg3->GPIOCount less than"\
2741 "25 entries, detected (%d) entries\n", ioc->name, __func__,
2742 io_unit_pg3->GPIOCount);
2743 goto out;
2746 /* BRM status is in bit zero of GPIOVal[24] */
2747 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2748 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2750 out:
2751 kfree(io_unit_pg3);
2752 return rc;
2754 static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
2756 struct DIAG_BUFFER_START {
2757 __le32 Size;
2758 __le32 DiagVersion;
2759 u8 BufferType;
2760 u8 Reserved[3];
2761 __le32 Reserved1;
2762 __le32 Reserved2;
2763 __le32 Reserved3;
2766 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2767 * @cdev - pointer to embedded class device
2768 * @buf - the buffer returned
2770 * A sysfs 'read-only' shost attribute.
2772 static ssize_t
2773 _ctl_host_trace_buffer_size_show(struct device *cdev,
2774 struct device_attribute *attr, char *buf)
2776 struct Scsi_Host *shost = class_to_shost(cdev);
2777 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2778 u32 size = 0;
2779 struct DIAG_BUFFER_START *request_data;
2781 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2782 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2783 "registered\n", ioc->name, __func__);
2784 return 0;
2787 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2788 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2789 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2790 "registered\n", ioc->name, __func__);
2791 return 0;
2794 request_data = (struct DIAG_BUFFER_START *)
2795 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2796 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2797 le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
2798 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2799 size = le32_to_cpu(request_data->Size);
2801 ioc->ring_buffer_sz = size;
2802 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2804 static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2805 _ctl_host_trace_buffer_size_show, NULL);
2808 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2809 * @cdev - pointer to embedded class device
2810 * @buf - the buffer returned
2812 * A sysfs 'read/write' shost attribute.
2814 * You will only be able to read 4k bytes of ring buffer at a time.
2815 * In order to read beyond 4k bytes, you will have to write out the
2816 * offset to the same attribute, it will move the pointer.
2818 static ssize_t
2819 _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2820 char *buf)
2822 struct Scsi_Host *shost = class_to_shost(cdev);
2823 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2824 void *request_data;
2825 u32 size;
2827 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2828 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2829 "registered\n", ioc->name, __func__);
2830 return 0;
2833 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2834 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2835 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2836 "registered\n", ioc->name, __func__);
2837 return 0;
2840 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2841 return 0;
2843 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2844 size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
2845 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2846 memcpy(buf, request_data, size);
2847 return size;
2850 static ssize_t
2851 _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2852 const char *buf, size_t count)
2854 struct Scsi_Host *shost = class_to_shost(cdev);
2855 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2856 int val = 0;
2858 if (sscanf(buf, "%d", &val) != 1)
2859 return -EINVAL;
2861 ioc->ring_buffer_offset = val;
2862 return strlen(buf);
2864 static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2865 _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2867 /*****************************************/
2870 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2871 * @cdev - pointer to embedded class device
2872 * @buf - the buffer returned
2874 * A sysfs 'read/write' shost attribute.
2876 * This is a mechnism to post/release host_trace_buffers
2878 static ssize_t
2879 _ctl_host_trace_buffer_enable_show(struct device *cdev,
2880 struct device_attribute *attr, char *buf)
2882 struct Scsi_Host *shost = class_to_shost(cdev);
2883 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2885 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
2886 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2887 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
2888 return snprintf(buf, PAGE_SIZE, "off\n");
2889 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2890 MPT2_DIAG_BUFFER_IS_RELEASED))
2891 return snprintf(buf, PAGE_SIZE, "release\n");
2892 else
2893 return snprintf(buf, PAGE_SIZE, "post\n");
2896 static ssize_t
2897 _ctl_host_trace_buffer_enable_store(struct device *cdev,
2898 struct device_attribute *attr, const char *buf, size_t count)
2900 struct Scsi_Host *shost = class_to_shost(cdev);
2901 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2902 char str[10] = "";
2903 struct mpt2_diag_register diag_register;
2904 u8 issue_reset = 0;
2906 if (sscanf(buf, "%9s", str) != 1)
2907 return -EINVAL;
2909 if (!strcmp(str, "post")) {
2910 /* exit out if host buffers are already posted */
2911 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
2912 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2913 MPT2_DIAG_BUFFER_IS_REGISTERED) &&
2914 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2915 MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
2916 goto out;
2917 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
2918 printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
2919 ioc->name);
2920 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
2921 diag_register.requested_buffer_size = (1024 * 1024);
2922 diag_register.unique_id = 0x7075900;
2923 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
2924 _ctl_diag_register_2(ioc, &diag_register);
2925 } else if (!strcmp(str, "release")) {
2926 /* exit out if host buffers are already released */
2927 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
2928 goto out;
2929 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2930 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
2931 goto out;
2932 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2933 MPT2_DIAG_BUFFER_IS_RELEASED))
2934 goto out;
2935 printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
2936 ioc->name);
2937 _ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
2940 out:
2941 return strlen(buf);
2943 static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
2944 _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
2946 struct device_attribute *mpt2sas_host_attrs[] = {
2947 &dev_attr_version_fw,
2948 &dev_attr_version_bios,
2949 &dev_attr_version_mpi,
2950 &dev_attr_version_product,
2951 &dev_attr_version_nvdata_persistent,
2952 &dev_attr_version_nvdata_default,
2953 &dev_attr_board_name,
2954 &dev_attr_board_assembly,
2955 &dev_attr_board_tracer,
2956 &dev_attr_io_delay,
2957 &dev_attr_device_delay,
2958 &dev_attr_logging_level,
2959 &dev_attr_fwfault_debug,
2960 &dev_attr_fw_queue_depth,
2961 &dev_attr_host_sas_address,
2962 &dev_attr_ioc_reset_count,
2963 &dev_attr_host_trace_buffer_size,
2964 &dev_attr_host_trace_buffer,
2965 &dev_attr_host_trace_buffer_enable,
2966 &dev_attr_reply_queue_count,
2967 &dev_attr_BRM_status,
2968 NULL,
2972 * _ctl_device_sas_address_show - sas address
2973 * @cdev - pointer to embedded class device
2974 * @buf - the buffer returned
2976 * This is the sas address for the target
2978 * A sysfs 'read-only' shost attribute.
2980 static ssize_t
2981 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2982 char *buf)
2984 struct scsi_device *sdev = to_scsi_device(dev);
2985 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2987 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2988 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2990 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2993 * _ctl_device_handle_show - device handle
2994 * @cdev - pointer to embedded class device
2995 * @buf - the buffer returned
2997 * This is the firmware assigned device handle
2999 * A sysfs 'read-only' shost attribute.
3001 static ssize_t
3002 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
3003 char *buf)
3005 struct scsi_device *sdev = to_scsi_device(dev);
3006 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3008 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3009 sas_device_priv_data->sas_target->handle);
3011 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
3013 struct device_attribute *mpt2sas_dev_attrs[] = {
3014 &dev_attr_sas_address,
3015 &dev_attr_sas_device_handle,
3016 NULL,
3019 static const struct file_operations ctl_fops = {
3020 .owner = THIS_MODULE,
3021 .unlocked_ioctl = _ctl_ioctl,
3022 .poll = _ctl_poll,
3023 .fasync = _ctl_fasync,
3024 #ifdef CONFIG_COMPAT
3025 .compat_ioctl = _ctl_ioctl_compat,
3026 #endif
3027 .llseek = noop_llseek,
3030 static struct miscdevice ctl_dev = {
3031 .minor = MPT2SAS_MINOR,
3032 .name = MPT2SAS_DEV_NAME,
3033 .fops = &ctl_fops,
3037 * mpt2sas_ctl_init - main entry point for ctl.
3040 void
3041 mpt2sas_ctl_init(void)
3043 async_queue = NULL;
3044 if (misc_register(&ctl_dev) < 0)
3045 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
3046 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3048 init_waitqueue_head(&ctl_poll_wait);
3052 * mpt2sas_ctl_exit - exit point for ctl
3055 void
3056 mpt2sas_ctl_exit(void)
3058 struct MPT2SAS_ADAPTER *ioc;
3059 int i;
3061 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
3063 /* free memory associated to diag buffers */
3064 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3065 if (!ioc->diag_buffer[i])
3066 continue;
3067 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3068 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3069 ioc->diag_buffer[i] = NULL;
3070 ioc->diag_buffer_status[i] = 0;
3073 kfree(ioc->event_log);
3075 misc_deregister(&ctl_dev);