]> Pileus Git - ~andy/linux/blob - drivers/scsi/mpt2sas/mpt2sas_ctl.c
[SCSI] mpt2sas: Target Reset will be issued from Interrupt context.
[~andy/linux] / drivers / scsi / mpt2sas / mpt2sas_ctl.c
1 /*
2  * Management Module Support for MPT (Message Passing Technology) based
3  * controllers
4  *
5  * This code is based on drivers/scsi/mpt2sas/mpt2_ctl.c
6  * Copyright (C) 2007-2009  LSI Corporation
7  *  (mailto:DL-MPTFusionLinux@lsi.com)
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * NO WARRANTY
20  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24  * solely responsible for determining the appropriateness of using and
25  * distributing the Program and assumes all risks associated with its
26  * exercise of rights under this Agreement, including but not limited to
27  * the risks and costs of program errors, damage to or loss of data,
28  * programs or equipment, and unavailability or interruption of operations.
29
30  * DISCLAIMER OF LIABILITY
31  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38
39  * You should have received a copy of the GNU General Public License
40  * along with this program; if not, write to the Free Software
41  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
42  * USA.
43  */
44
45 #include <linux/version.h>
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/slab.h>
51 #include <linux/types.h>
52 #include <linux/pci.h>
53 #include <linux/delay.h>
54 #include <linux/smp_lock.h>
55 #include <linux/compat.h>
56 #include <linux/poll.h>
57
58 #include <linux/io.h>
59 #include <linux/uaccess.h>
60
61 #include "mpt2sas_base.h"
62 #include "mpt2sas_ctl.h"
63
64 static struct fasync_struct *async_queue;
65 static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
66
67 static int _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type,
68     u8 *issue_reset);
69
70 /**
71  * enum block_state - blocking state
72  * @NON_BLOCKING: non blocking
73  * @BLOCKING: blocking
74  *
75  * These states are for ioctls that need to wait for a response
76  * from firmware, so they probably require sleep.
77  */
78 enum block_state {
79         NON_BLOCKING,
80         BLOCKING,
81 };
82
83 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
84 /**
85  * _ctl_display_some_debug - debug routine
86  * @ioc: per adapter object
87  * @smid: system request message index
88  * @calling_function_name: string pass from calling function
89  * @mpi_reply: reply message frame
90  * Context: none.
91  *
92  * Function for displaying debug info helpfull when debugging issues
93  * in this module.
94  */
95 static void
96 _ctl_display_some_debug(struct MPT2SAS_ADAPTER *ioc, u16 smid,
97     char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
98 {
99         Mpi2ConfigRequest_t *mpi_request;
100         char *desc = NULL;
101
102         if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
103                 return;
104
105         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
106         switch (mpi_request->Function) {
107         case MPI2_FUNCTION_SCSI_IO_REQUEST:
108         {
109                 Mpi2SCSIIORequest_t *scsi_request =
110                     (Mpi2SCSIIORequest_t *)mpi_request;
111
112                 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
113                     "scsi_io, cmd(0x%02x), cdb_len(%d)",
114                     scsi_request->CDB.CDB32[0],
115                     le16_to_cpu(scsi_request->IoFlags) & 0xF);
116                 desc = ioc->tmp_string;
117                 break;
118         }
119         case MPI2_FUNCTION_SCSI_TASK_MGMT:
120                 desc = "task_mgmt";
121                 break;
122         case MPI2_FUNCTION_IOC_INIT:
123                 desc = "ioc_init";
124                 break;
125         case MPI2_FUNCTION_IOC_FACTS:
126                 desc = "ioc_facts";
127                 break;
128         case MPI2_FUNCTION_CONFIG:
129         {
130                 Mpi2ConfigRequest_t *config_request =
131                     (Mpi2ConfigRequest_t *)mpi_request;
132
133                 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
134                     "config, type(0x%02x), ext_type(0x%02x), number(%d)",
135                     (config_request->Header.PageType &
136                      MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
137                     config_request->Header.PageNumber);
138                 desc = ioc->tmp_string;
139                 break;
140         }
141         case MPI2_FUNCTION_PORT_FACTS:
142                 desc = "port_facts";
143                 break;
144         case MPI2_FUNCTION_PORT_ENABLE:
145                 desc = "port_enable";
146                 break;
147         case MPI2_FUNCTION_EVENT_NOTIFICATION:
148                 desc = "event_notification";
149                 break;
150         case MPI2_FUNCTION_FW_DOWNLOAD:
151                 desc = "fw_download";
152                 break;
153         case MPI2_FUNCTION_FW_UPLOAD:
154                 desc = "fw_upload";
155                 break;
156         case MPI2_FUNCTION_RAID_ACTION:
157                 desc = "raid_action";
158                 break;
159         case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
160         {
161                 Mpi2SCSIIORequest_t *scsi_request =
162                     (Mpi2SCSIIORequest_t *)mpi_request;
163
164                 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
165                     "raid_pass, cmd(0x%02x), cdb_len(%d)",
166                     scsi_request->CDB.CDB32[0],
167                     le16_to_cpu(scsi_request->IoFlags) & 0xF);
168                 desc = ioc->tmp_string;
169                 break;
170         }
171         case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
172                 desc = "sas_iounit_cntl";
173                 break;
174         case MPI2_FUNCTION_SATA_PASSTHROUGH:
175                 desc = "sata_pass";
176                 break;
177         case MPI2_FUNCTION_DIAG_BUFFER_POST:
178                 desc = "diag_buffer_post";
179                 break;
180         case MPI2_FUNCTION_DIAG_RELEASE:
181                 desc = "diag_release";
182                 break;
183         case MPI2_FUNCTION_SMP_PASSTHROUGH:
184                 desc = "smp_passthrough";
185                 break;
186         }
187
188         if (!desc)
189                 return;
190
191         printk(MPT2SAS_DEBUG_FMT "%s: %s, smid(%d)\n",
192             ioc->name, calling_function_name, desc, smid);
193
194         if (!mpi_reply)
195                 return;
196
197         if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
198                 printk(MPT2SAS_DEBUG_FMT
199                     "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
200                     ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
201                     le32_to_cpu(mpi_reply->IOCLogInfo));
202
203         if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
204             mpi_request->Function ==
205             MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
206                 Mpi2SCSIIOReply_t *scsi_reply =
207                     (Mpi2SCSIIOReply_t *)mpi_reply;
208                 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
209                         printk(MPT2SAS_DEBUG_FMT
210                             "\tscsi_state(0x%02x), scsi_status"
211                             "(0x%02x)\n", ioc->name,
212                             scsi_reply->SCSIState,
213                             scsi_reply->SCSIStatus);
214         }
215 }
216 #endif
217
218 /**
219  * mpt2sas_ctl_done - ctl module completion routine
220  * @ioc: per adapter object
221  * @smid: system request message index
222  * @msix_index: MSIX table index supplied by the OS
223  * @reply: reply message frame(lower 32bit addr)
224  * Context: none.
225  *
226  * The callback handler when using ioc->ctl_cb_idx.
227  *
228  * Return 1 meaning mf should be freed from _base_interrupt
229  *        0 means the mf is freed from this function.
230  */
231 u8
232 mpt2sas_ctl_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
233         u32 reply)
234 {
235         MPI2DefaultReply_t *mpi_reply;
236
237         if (ioc->ctl_cmds.status == MPT2_CMD_NOT_USED)
238                 return 1;
239         if (ioc->ctl_cmds.smid != smid)
240                 return 1;
241         ioc->ctl_cmds.status |= MPT2_CMD_COMPLETE;
242         mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
243         if (mpi_reply) {
244                 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
245                 ioc->ctl_cmds.status |= MPT2_CMD_REPLY_VALID;
246         }
247 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
248         _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
249 #endif
250         ioc->ctl_cmds.status &= ~MPT2_CMD_PENDING;
251         complete(&ioc->ctl_cmds.done);
252         return 1;
253 }
254
255 /**
256  * _ctl_check_event_type - determines when an event needs logging
257  * @ioc: per adapter object
258  * @event: firmware event
259  *
260  * The bitmask in ioc->event_type[] indicates which events should be
261  * be saved in the driver event_log.  This bitmask is set by application.
262  *
263  * Returns 1 when event should be captured, or zero means no match.
264  */
265 static int
266 _ctl_check_event_type(struct MPT2SAS_ADAPTER *ioc, u16 event)
267 {
268         u16 i;
269         u32 desired_event;
270
271         if (event >= 128 || !event || !ioc->event_log)
272                 return 0;
273
274         desired_event = (1 << (event % 32));
275         if (!desired_event)
276                 desired_event = 1;
277         i = event / 32;
278         return desired_event & ioc->event_type[i];
279 }
280
281 /**
282  * mpt2sas_ctl_add_to_event_log - add event
283  * @ioc: per adapter object
284  * @mpi_reply: reply message frame
285  *
286  * Return nothing.
287  */
288 void
289 mpt2sas_ctl_add_to_event_log(struct MPT2SAS_ADAPTER *ioc,
290     Mpi2EventNotificationReply_t *mpi_reply)
291 {
292         struct MPT2_IOCTL_EVENTS *event_log;
293         u16 event;
294         int i;
295         u32 sz, event_data_sz;
296         u8 send_aen = 0;
297
298         if (!ioc->event_log)
299                 return;
300
301         event = le16_to_cpu(mpi_reply->Event);
302
303         if (_ctl_check_event_type(ioc, event)) {
304
305                 /* insert entry into circular event_log */
306                 i = ioc->event_context % MPT2SAS_CTL_EVENT_LOG_SIZE;
307                 event_log = ioc->event_log;
308                 event_log[i].event = event;
309                 event_log[i].context = ioc->event_context++;
310
311                 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
312                 sz = min_t(u32, event_data_sz, MPT2_EVENT_DATA_SIZE);
313                 memset(event_log[i].data, 0, MPT2_EVENT_DATA_SIZE);
314                 memcpy(event_log[i].data, mpi_reply->EventData, sz);
315                 send_aen = 1;
316         }
317
318         /* This aen_event_read_flag flag is set until the
319          * application has read the event log.
320          * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
321          */
322         if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
323             (send_aen && !ioc->aen_event_read_flag)) {
324                 ioc->aen_event_read_flag = 1;
325                 wake_up_interruptible(&ctl_poll_wait);
326                 if (async_queue)
327                         kill_fasync(&async_queue, SIGIO, POLL_IN);
328         }
329 }
330
331 /**
332  * mpt2sas_ctl_event_callback - firmware event handler (called at ISR time)
333  * @ioc: per adapter object
334  * @msix_index: MSIX table index supplied by the OS
335  * @reply: reply message frame(lower 32bit addr)
336  * Context: interrupt.
337  *
338  * This function merely adds a new work task into ioc->firmware_event_thread.
339  * The tasks are worked from _firmware_event_work in user context.
340  *
341  * Return 1 meaning mf should be freed from _base_interrupt
342  *        0 means the mf is freed from this function.
343  */
344 u8
345 mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
346         u32 reply)
347 {
348         Mpi2EventNotificationReply_t *mpi_reply;
349
350         mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
351         mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
352         return 1;
353 }
354
355 /**
356  * _ctl_verify_adapter - validates ioc_number passed from application
357  * @ioc: per adapter object
358  * @iocpp: The ioc pointer is returned in this.
359  *
360  * Return (-1) means error, else ioc_number.
361  */
362 static int
363 _ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
364 {
365         struct MPT2SAS_ADAPTER *ioc;
366
367         list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
368                 if (ioc->id != ioc_number)
369                         continue;
370                 *iocpp = ioc;
371                 return ioc_number;
372         }
373         *iocpp = NULL;
374         return -1;
375 }
376
377 /**
378  * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
379  * @ioc: per adapter object
380  * @reset_phase: phase
381  *
382  * The handler for doing any required cleanup or initialization.
383  *
384  * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
385  * MPT2_IOC_DONE_RESET
386  */
387 void
388 mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
389 {
390         int i;
391         u8 issue_reset;
392
393         switch (reset_phase) {
394         case MPT2_IOC_PRE_RESET:
395                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
396                     "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
397                 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
398                         if (!(ioc->diag_buffer_status[i] &
399                             MPT2_DIAG_BUFFER_IS_REGISTERED))
400                                 continue;
401                         if ((ioc->diag_buffer_status[i] &
402                             MPT2_DIAG_BUFFER_IS_RELEASED))
403                                 continue;
404                         _ctl_send_release(ioc, i, &issue_reset);
405                 }
406                 break;
407         case MPT2_IOC_AFTER_RESET:
408                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
409                     "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
410                 if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
411                         ioc->ctl_cmds.status |= MPT2_CMD_RESET;
412                         mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
413                         complete(&ioc->ctl_cmds.done);
414                 }
415                 break;
416         case MPT2_IOC_DONE_RESET:
417                 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
418                     "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
419
420                 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
421                         if (!(ioc->diag_buffer_status[i] &
422                             MPT2_DIAG_BUFFER_IS_REGISTERED))
423                                 continue;
424                         if ((ioc->diag_buffer_status[i] &
425                             MPT2_DIAG_BUFFER_IS_RELEASED))
426                                 continue;
427                         ioc->diag_buffer_status[i] |=
428                             MPT2_DIAG_BUFFER_IS_DIAG_RESET;
429                 }
430                 break;
431         }
432 }
433
434 /**
435  * _ctl_fasync -
436  * @fd -
437  * @filep -
438  * @mode -
439  *
440  * Called when application request fasyn callback handler.
441  */
442 static int
443 _ctl_fasync(int fd, struct file *filep, int mode)
444 {
445         return fasync_helper(fd, filep, mode, &async_queue);
446 }
447
448 /**
449  * _ctl_release -
450  * @inode -
451  * @filep -
452  *
453  * Called when application releases the fasyn callback handler.
454  */
455 static int
456 _ctl_release(struct inode *inode, struct file *filep)
457 {
458         return fasync_helper(-1, filep, 0, &async_queue);
459 }
460
461 /**
462  * _ctl_poll -
463  * @file -
464  * @wait -
465  *
466  */
467 static unsigned int
468 _ctl_poll(struct file *filep, poll_table *wait)
469 {
470         struct MPT2SAS_ADAPTER *ioc;
471
472         poll_wait(filep, &ctl_poll_wait, wait);
473
474         list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
475                 if (ioc->aen_event_read_flag)
476                         return POLLIN | POLLRDNORM;
477         }
478         return 0;
479 }
480
481 /**
482  * _ctl_set_task_mid - assign an active smid to tm request
483  * @ioc: per adapter object
484  * @karg - (struct mpt2_ioctl_command)
485  * @tm_request - pointer to mf from user space
486  *
487  * Returns 0 when an smid if found, else fail.
488  * during failure, the reply frame is filled.
489  */
490 static int
491 _ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
492     Mpi2SCSITaskManagementRequest_t *tm_request)
493 {
494         u8 found = 0;
495         u16 i;
496         u16 handle;
497         struct scsi_cmnd *scmd;
498         struct MPT2SAS_DEVICE *priv_data;
499         unsigned long flags;
500         Mpi2SCSITaskManagementReply_t *tm_reply;
501         u32 sz;
502         u32 lun;
503         char *desc = NULL;
504
505         if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
506                 desc = "abort_task";
507         else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
508                 desc = "query_task";
509         else
510                 return 0;
511
512         lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
513
514         handle = le16_to_cpu(tm_request->DevHandle);
515         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
516         for (i = ioc->scsiio_depth; i && !found; i--) {
517                 scmd = ioc->scsi_lookup[i - 1].scmd;
518                 if (scmd == NULL || scmd->device == NULL ||
519                     scmd->device->hostdata == NULL)
520                         continue;
521                 if (lun != scmd->device->lun)
522                         continue;
523                 priv_data = scmd->device->hostdata;
524                 if (priv_data->sas_target == NULL)
525                         continue;
526                 if (priv_data->sas_target->handle != handle)
527                         continue;
528                 tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
529                 found = 1;
530         }
531         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
532
533         if (!found) {
534                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
535                     "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
536                     desc, tm_request->DevHandle, lun));
537                 tm_reply = ioc->ctl_cmds.reply;
538                 tm_reply->DevHandle = tm_request->DevHandle;
539                 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
540                 tm_reply->TaskType = tm_request->TaskType;
541                 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
542                 tm_reply->VP_ID = tm_request->VP_ID;
543                 tm_reply->VF_ID = tm_request->VF_ID;
544                 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
545                 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
546                     sz))
547                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
548                             __LINE__, __func__);
549                 return 1;
550         }
551
552         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
553             "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
554             desc, tm_request->DevHandle, lun, tm_request->TaskMID));
555         return 0;
556 }
557
558 /**
559  * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
560  * @ioc: per adapter object
561  * @karg - (struct mpt2_ioctl_command)
562  * @mf - pointer to mf in user space
563  * @state - NON_BLOCKING or BLOCKING
564  */
565 static long
566 _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc,
567     struct mpt2_ioctl_command karg, void __user *mf, enum block_state state)
568 {
569         MPI2RequestHeader_t *mpi_request;
570         MPI2DefaultReply_t *mpi_reply;
571         u32 ioc_state;
572         u16 ioc_status;
573         u16 smid;
574         unsigned long timeout, timeleft;
575         u8 issue_reset;
576         u32 sz;
577         void *psge;
578         void *priv_sense = NULL;
579         void *data_out = NULL;
580         dma_addr_t data_out_dma;
581         size_t data_out_sz = 0;
582         void *data_in = NULL;
583         dma_addr_t data_in_dma;
584         size_t data_in_sz = 0;
585         u32 sgl_flags;
586         long ret;
587         u16 wait_state_count;
588
589         issue_reset = 0;
590
591         if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
592                 return -EAGAIN;
593         else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
594                 return -ERESTARTSYS;
595
596         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
597                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
598                     ioc->name, __func__);
599                 ret = -EAGAIN;
600                 goto out;
601         }
602
603         wait_state_count = 0;
604         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
605         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
606                 if (wait_state_count++ == 10) {
607                         printk(MPT2SAS_ERR_FMT
608                             "%s: failed due to ioc not operational\n",
609                             ioc->name, __func__);
610                         ret = -EFAULT;
611                         goto out;
612                 }
613                 ssleep(1);
614                 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
615                 printk(MPT2SAS_INFO_FMT "%s: waiting for "
616                     "operational state(count=%d)\n", ioc->name,
617                     __func__, wait_state_count);
618         }
619         if (wait_state_count)
620                 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
621                     ioc->name, __func__);
622
623         smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
624         if (!smid) {
625                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
626                     ioc->name, __func__);
627                 ret = -EAGAIN;
628                 goto out;
629         }
630
631         ret = 0;
632         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
633         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
634         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
635         ioc->ctl_cmds.smid = smid;
636         data_out_sz = karg.data_out_size;
637         data_in_sz = karg.data_in_size;
638
639         /* copy in request message frame from user */
640         if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
641                 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
642                     __func__);
643                 ret = -EFAULT;
644                 mpt2sas_base_free_smid(ioc, smid);
645                 goto out;
646         }
647
648         if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
649             mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
650                 if (!mpi_request->FunctionDependent1 ||
651                     mpi_request->FunctionDependent1 >
652                     cpu_to_le16(ioc->facts.MaxDevHandle)) {
653                         ret = -EINVAL;
654                         mpt2sas_base_free_smid(ioc, smid);
655                         goto out;
656                 }
657         }
658
659         /* obtain dma-able memory for data transfer */
660         if (data_out_sz) /* WRITE */ {
661                 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
662                     &data_out_dma);
663                 if (!data_out) {
664                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
665                             __LINE__, __func__);
666                         ret = -ENOMEM;
667                         mpt2sas_base_free_smid(ioc, smid);
668                         goto out;
669                 }
670                 if (copy_from_user(data_out, karg.data_out_buf_ptr,
671                         data_out_sz)) {
672                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
673                             __LINE__, __func__);
674                         ret =  -EFAULT;
675                         mpt2sas_base_free_smid(ioc, smid);
676                         goto out;
677                 }
678         }
679
680         if (data_in_sz) /* READ */ {
681                 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
682                     &data_in_dma);
683                 if (!data_in) {
684                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
685                             __LINE__, __func__);
686                         ret = -ENOMEM;
687                         mpt2sas_base_free_smid(ioc, smid);
688                         goto out;
689                 }
690         }
691
692         /* add scatter gather elements */
693         psge = (void *)mpi_request + (karg.data_sge_offset*4);
694
695         if (!data_out_sz && !data_in_sz) {
696                 mpt2sas_base_build_zero_len_sge(ioc, psge);
697         } else if (data_out_sz && data_in_sz) {
698                 /* WRITE sgel first */
699                 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
700                     MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
701                 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
702                 ioc->base_add_sg_single(psge, sgl_flags |
703                     data_out_sz, data_out_dma);
704
705                 /* incr sgel */
706                 psge += ioc->sge_size;
707
708                 /* READ sgel last */
709                 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
710                     MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
711                     MPI2_SGE_FLAGS_END_OF_LIST);
712                 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
713                 ioc->base_add_sg_single(psge, sgl_flags |
714                     data_in_sz, data_in_dma);
715         } else if (data_out_sz) /* WRITE */ {
716                 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
717                     MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
718                     MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
719                 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
720                 ioc->base_add_sg_single(psge, sgl_flags |
721                     data_out_sz, data_out_dma);
722         } else if (data_in_sz) /* READ */ {
723                 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
724                     MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
725                     MPI2_SGE_FLAGS_END_OF_LIST);
726                 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
727                 ioc->base_add_sg_single(psge, sgl_flags |
728                     data_in_sz, data_in_dma);
729         }
730
731         /* send command to firmware */
732 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
733         _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
734 #endif
735
736         switch (mpi_request->Function) {
737         case MPI2_FUNCTION_SCSI_IO_REQUEST:
738         case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
739         {
740                 Mpi2SCSIIORequest_t *scsiio_request =
741                     (Mpi2SCSIIORequest_t *)mpi_request;
742                 scsiio_request->SenseBufferLowAddress =
743                     (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
744                 priv_sense = mpt2sas_base_get_sense_buffer(ioc, smid);
745                 memset(priv_sense, 0, SCSI_SENSE_BUFFERSIZE);
746                 mpt2sas_base_put_smid_scsi_io(ioc, smid,
747                     le16_to_cpu(mpi_request->FunctionDependent1));
748                 break;
749         }
750         case MPI2_FUNCTION_SCSI_TASK_MGMT:
751         {
752                 Mpi2SCSITaskManagementRequest_t *tm_request =
753                     (Mpi2SCSITaskManagementRequest_t *)mpi_request;
754
755                 if (tm_request->TaskType ==
756                     MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
757                     tm_request->TaskType ==
758                     MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
759                         if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
760                                 mpt2sas_base_free_smid(ioc, smid);
761                                 goto out;
762                         }
763                 }
764
765                 mutex_lock(&ioc->tm_cmds.mutex);
766                 mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
767                     tm_request->DevHandle));
768                 mpt2sas_base_put_smid_hi_priority(ioc, smid);
769                 break;
770         }
771         case MPI2_FUNCTION_SMP_PASSTHROUGH:
772         {
773                 Mpi2SmpPassthroughRequest_t *smp_request =
774                     (Mpi2SmpPassthroughRequest_t *)mpi_request;
775                 u8 *data;
776
777                 /* ioc determines which port to use */
778                 smp_request->PhysicalPort = 0xFF;
779                 if (smp_request->PassthroughFlags &
780                     MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
781                         data = (u8 *)&smp_request->SGL;
782                 else
783                         data = data_out;
784
785                 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
786                         ioc->ioc_link_reset_in_progress = 1;
787                         ioc->ignore_loginfos = 1;
788                 }
789                 mpt2sas_base_put_smid_default(ioc, smid);
790                 break;
791         }
792         case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
793         {
794                 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
795                     (Mpi2SasIoUnitControlRequest_t *)mpi_request;
796
797                 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
798                     || sasiounit_request->Operation ==
799                     MPI2_SAS_OP_PHY_LINK_RESET) {
800                         ioc->ioc_link_reset_in_progress = 1;
801                         ioc->ignore_loginfos = 1;
802                 }
803                 mpt2sas_base_put_smid_default(ioc, smid);
804                 break;
805         }
806         default:
807                 mpt2sas_base_put_smid_default(ioc, smid);
808                 break;
809         }
810
811         if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
812                 timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
813         else
814                 timeout = karg.timeout;
815         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
816             timeout*HZ);
817         if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
818                 Mpi2SCSITaskManagementRequest_t *tm_request =
819                     (Mpi2SCSITaskManagementRequest_t *)mpi_request;
820                 mutex_unlock(&ioc->tm_cmds.mutex);
821                 mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
822                     tm_request->DevHandle));
823         } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
824             mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
825                 ioc->ioc_link_reset_in_progress) {
826                 ioc->ioc_link_reset_in_progress = 0;
827                 ioc->ignore_loginfos = 0;
828         }
829         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
830                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
831                     __func__);
832                 _debug_dump_mf(mpi_request, karg.data_sge_offset);
833                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
834                         issue_reset = 1;
835                 goto issue_host_reset;
836         }
837
838         mpi_reply = ioc->ctl_cmds.reply;
839         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
840
841 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
842         if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
843             (ioc->logging_level & MPT_DEBUG_TM)) {
844                 Mpi2SCSITaskManagementReply_t *tm_reply =
845                     (Mpi2SCSITaskManagementReply_t *)mpi_reply;
846
847                 printk(MPT2SAS_DEBUG_FMT "TASK_MGMT: "
848                     "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
849                     "TerminationCount(0x%08x)\n", ioc->name,
850                     tm_reply->IOCStatus, tm_reply->IOCLogInfo,
851                     tm_reply->TerminationCount);
852         }
853 #endif
854         /* copy out xdata to user */
855         if (data_in_sz) {
856                 if (copy_to_user(karg.data_in_buf_ptr, data_in,
857                     data_in_sz)) {
858                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
859                             __LINE__, __func__);
860                         ret = -ENODATA;
861                         goto out;
862                 }
863         }
864
865         /* copy out reply message frame to user */
866         if (karg.max_reply_bytes) {
867                 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
868                 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
869                     sz)) {
870                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
871                             __LINE__, __func__);
872                         ret = -ENODATA;
873                         goto out;
874                 }
875         }
876
877         /* copy out sense to user */
878         if (karg.max_sense_bytes && (mpi_request->Function ==
879             MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
880             MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
881                 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
882                 if (copy_to_user(karg.sense_data_ptr, priv_sense, sz)) {
883                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
884                             __LINE__, __func__);
885                         ret = -ENODATA;
886                         goto out;
887                 }
888         }
889
890  issue_host_reset:
891         if (issue_reset) {
892                 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
893                     mpi_request->Function ==
894                     MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
895                         printk(MPT2SAS_INFO_FMT "issue target reset: handle "
896                             "= (0x%04x)\n", ioc->name,
897                             mpi_request->FunctionDependent1);
898                         mutex_lock(&ioc->tm_cmds.mutex);
899                         mpt2sas_scsih_issue_tm(ioc,
900                             mpi_request->FunctionDependent1, 0,
901                             MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
902                         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
903                         mutex_unlock(&ioc->tm_cmds.mutex);
904                 } else
905                         mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
906                             FORCE_BIG_HAMMER);
907         }
908
909  out:
910
911         /* free memory associated with sg buffers */
912         if (data_in)
913                 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
914                     data_in_dma);
915
916         if (data_out)
917                 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
918                     data_out_dma);
919
920         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
921         mutex_unlock(&ioc->ctl_cmds.mutex);
922         return ret;
923 }
924
925 /**
926  * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
927  * @arg - user space buffer containing ioctl content
928  */
929 static long
930 _ctl_getiocinfo(void __user *arg)
931 {
932         struct mpt2_ioctl_iocinfo karg;
933         struct MPT2SAS_ADAPTER *ioc;
934         u8 revision;
935
936         if (copy_from_user(&karg, arg, sizeof(karg))) {
937                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
938                     __FILE__, __LINE__, __func__);
939                 return -EFAULT;
940         }
941         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
942                 return -ENODEV;
943
944         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
945             __func__));
946
947         memset(&karg, 0 , sizeof(karg));
948         karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
949         if (ioc->pfacts)
950                 karg.port_number = ioc->pfacts[0].PortNumber;
951         pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
952         karg.hw_rev = revision;
953         karg.pci_id = ioc->pdev->device;
954         karg.subsystem_device = ioc->pdev->subsystem_device;
955         karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
956         karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
957         karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
958         karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
959         karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
960         karg.firmware_version = ioc->facts.FWVersion.Word;
961         strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
962         strcat(karg.driver_version, "-");
963         strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
964         karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
965
966         if (copy_to_user(arg, &karg, sizeof(karg))) {
967                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
968                     __FILE__, __LINE__, __func__);
969                 return -EFAULT;
970         }
971         return 0;
972 }
973
974 /**
975  * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
976  * @arg - user space buffer containing ioctl content
977  */
978 static long
979 _ctl_eventquery(void __user *arg)
980 {
981         struct mpt2_ioctl_eventquery karg;
982         struct MPT2SAS_ADAPTER *ioc;
983
984         if (copy_from_user(&karg, arg, sizeof(karg))) {
985                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
986                     __FILE__, __LINE__, __func__);
987                 return -EFAULT;
988         }
989         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
990                 return -ENODEV;
991
992         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
993             __func__));
994
995         karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
996         memcpy(karg.event_types, ioc->event_type,
997             MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
998
999         if (copy_to_user(arg, &karg, sizeof(karg))) {
1000                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1001                     __FILE__, __LINE__, __func__);
1002                 return -EFAULT;
1003         }
1004         return 0;
1005 }
1006
1007 /**
1008  * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1009  * @arg - user space buffer containing ioctl content
1010  */
1011 static long
1012 _ctl_eventenable(void __user *arg)
1013 {
1014         struct mpt2_ioctl_eventenable karg;
1015         struct MPT2SAS_ADAPTER *ioc;
1016
1017         if (copy_from_user(&karg, arg, sizeof(karg))) {
1018                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1019                     __FILE__, __LINE__, __func__);
1020                 return -EFAULT;
1021         }
1022         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1023                 return -ENODEV;
1024
1025         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1026             __func__));
1027
1028         if (ioc->event_log)
1029                 return 0;
1030         memcpy(ioc->event_type, karg.event_types,
1031             MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1032         mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1033
1034         /* initialize event_log */
1035         ioc->event_context = 0;
1036         ioc->aen_event_read_flag = 0;
1037         ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1038             sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1039         if (!ioc->event_log) {
1040                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1041                     __FILE__, __LINE__, __func__);
1042                 return -ENOMEM;
1043         }
1044         return 0;
1045 }
1046
1047 /**
1048  * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1049  * @arg - user space buffer containing ioctl content
1050  */
1051 static long
1052 _ctl_eventreport(void __user *arg)
1053 {
1054         struct mpt2_ioctl_eventreport karg;
1055         struct MPT2SAS_ADAPTER *ioc;
1056         u32 number_bytes, max_events, max;
1057         struct mpt2_ioctl_eventreport __user *uarg = arg;
1058
1059         if (copy_from_user(&karg, arg, sizeof(karg))) {
1060                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1061                     __FILE__, __LINE__, __func__);
1062                 return -EFAULT;
1063         }
1064         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1065                 return -ENODEV;
1066
1067         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1068             __func__));
1069
1070         number_bytes = karg.hdr.max_data_size -
1071             sizeof(struct mpt2_ioctl_header);
1072         max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1073         max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1074
1075         /* If fewer than 1 event is requested, there must have
1076          * been some type of error.
1077          */
1078         if (!max || !ioc->event_log)
1079                 return -ENODATA;
1080
1081         number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1082         if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1083                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1084                     __FILE__, __LINE__, __func__);
1085                 return -EFAULT;
1086         }
1087
1088         /* reset flag so SIGIO can restart */
1089         ioc->aen_event_read_flag = 0;
1090         return 0;
1091 }
1092
1093 /**
1094  * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1095  * @arg - user space buffer containing ioctl content
1096  */
1097 static long
1098 _ctl_do_reset(void __user *arg)
1099 {
1100         struct mpt2_ioctl_diag_reset karg;
1101         struct MPT2SAS_ADAPTER *ioc;
1102         int retval;
1103
1104         if (copy_from_user(&karg, arg, sizeof(karg))) {
1105                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1106                     __FILE__, __LINE__, __func__);
1107                 return -EFAULT;
1108         }
1109         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1110                 return -ENODEV;
1111
1112         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1113             __func__));
1114
1115         retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1116             FORCE_BIG_HAMMER);
1117         printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1118             ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1119         return 0;
1120 }
1121
1122 /**
1123  * _ctl_btdh_search_sas_device - searching for sas device
1124  * @ioc: per adapter object
1125  * @btdh: btdh ioctl payload
1126  */
1127 static int
1128 _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1129     struct mpt2_ioctl_btdh_mapping *btdh)
1130 {
1131         struct _sas_device *sas_device;
1132         unsigned long flags;
1133         int rc = 0;
1134
1135         if (list_empty(&ioc->sas_device_list))
1136                 return rc;
1137
1138         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1139         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1140                 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1141                     btdh->handle == sas_device->handle) {
1142                         btdh->bus = sas_device->channel;
1143                         btdh->id = sas_device->id;
1144                         rc = 1;
1145                         goto out;
1146                 } else if (btdh->bus == sas_device->channel && btdh->id ==
1147                     sas_device->id && btdh->handle == 0xFFFF) {
1148                         btdh->handle = sas_device->handle;
1149                         rc = 1;
1150                         goto out;
1151                 }
1152         }
1153  out:
1154         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1155         return rc;
1156 }
1157
1158 /**
1159  * _ctl_btdh_search_raid_device - searching for raid device
1160  * @ioc: per adapter object
1161  * @btdh: btdh ioctl payload
1162  */
1163 static int
1164 _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1165     struct mpt2_ioctl_btdh_mapping *btdh)
1166 {
1167         struct _raid_device *raid_device;
1168         unsigned long flags;
1169         int rc = 0;
1170
1171         if (list_empty(&ioc->raid_device_list))
1172                 return rc;
1173
1174         spin_lock_irqsave(&ioc->raid_device_lock, flags);
1175         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1176                 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1177                     btdh->handle == raid_device->handle) {
1178                         btdh->bus = raid_device->channel;
1179                         btdh->id = raid_device->id;
1180                         rc = 1;
1181                         goto out;
1182                 } else if (btdh->bus == raid_device->channel && btdh->id ==
1183                     raid_device->id && btdh->handle == 0xFFFF) {
1184                         btdh->handle = raid_device->handle;
1185                         rc = 1;
1186                         goto out;
1187                 }
1188         }
1189  out:
1190         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1191         return rc;
1192 }
1193
1194 /**
1195  * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1196  * @arg - user space buffer containing ioctl content
1197  */
1198 static long
1199 _ctl_btdh_mapping(void __user *arg)
1200 {
1201         struct mpt2_ioctl_btdh_mapping karg;
1202         struct MPT2SAS_ADAPTER *ioc;
1203         int rc;
1204
1205         if (copy_from_user(&karg, arg, sizeof(karg))) {
1206                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1207                     __FILE__, __LINE__, __func__);
1208                 return -EFAULT;
1209         }
1210         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1211                 return -ENODEV;
1212
1213         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1214             __func__));
1215
1216         rc = _ctl_btdh_search_sas_device(ioc, &karg);
1217         if (!rc)
1218                 _ctl_btdh_search_raid_device(ioc, &karg);
1219
1220         if (copy_to_user(arg, &karg, sizeof(karg))) {
1221                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1222                     __FILE__, __LINE__, __func__);
1223                 return -EFAULT;
1224         }
1225         return 0;
1226 }
1227
1228 /**
1229  * _ctl_diag_capability - return diag buffer capability
1230  * @ioc: per adapter object
1231  * @buffer_type: specifies either TRACE or SNAPSHOT
1232  *
1233  * returns 1 when diag buffer support is enabled in firmware
1234  */
1235 static u8
1236 _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1237 {
1238         u8 rc = 0;
1239
1240         switch (buffer_type) {
1241         case MPI2_DIAG_BUF_TYPE_TRACE:
1242                 if (ioc->facts.IOCCapabilities &
1243                     MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1244                         rc = 1;
1245                 break;
1246         case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1247                 if (ioc->facts.IOCCapabilities &
1248                     MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1249                         rc = 1;
1250                 break;
1251         }
1252
1253         return rc;
1254 }
1255
1256 /**
1257  * _ctl_diag_register - application register with driver
1258  * @arg - user space buffer containing ioctl content
1259  * @state - NON_BLOCKING or BLOCKING
1260  *
1261  * This will allow the driver to setup any required buffers that will be
1262  * needed by firmware to communicate with the driver.
1263  */
1264 static long
1265 _ctl_diag_register(void __user *arg, enum block_state state)
1266 {
1267         struct mpt2_diag_register karg;
1268         struct MPT2SAS_ADAPTER *ioc;
1269         int rc, i;
1270         void *request_data = NULL;
1271         dma_addr_t request_data_dma;
1272         u32 request_data_sz = 0;
1273         Mpi2DiagBufferPostRequest_t *mpi_request;
1274         Mpi2DiagBufferPostReply_t *mpi_reply;
1275         u8 buffer_type;
1276         unsigned long timeleft;
1277         u16 smid;
1278         u16 ioc_status;
1279         u8 issue_reset = 0;
1280
1281         if (copy_from_user(&karg, arg, sizeof(karg))) {
1282                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1283                     __FILE__, __LINE__, __func__);
1284                 return -EFAULT;
1285         }
1286         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1287                 return -ENODEV;
1288
1289         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1290             __func__));
1291
1292         buffer_type = karg.buffer_type;
1293         if (!_ctl_diag_capability(ioc, buffer_type)) {
1294                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1295                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1296                 return -EPERM;
1297         }
1298
1299         if (ioc->diag_buffer_status[buffer_type] &
1300             MPT2_DIAG_BUFFER_IS_REGISTERED) {
1301                 printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1302                     "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1303                     buffer_type);
1304                 return -EINVAL;
1305         }
1306
1307         if (karg.requested_buffer_size % 4)  {
1308                 printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1309                     "is not 4 byte aligned\n", ioc->name, __func__);
1310                 return -EINVAL;
1311         }
1312
1313         if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1314                 return -EAGAIN;
1315         else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1316                 return -ERESTARTSYS;
1317
1318         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1319                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1320                     ioc->name, __func__);
1321                 rc = -EAGAIN;
1322                 goto out;
1323         }
1324
1325         smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1326         if (!smid) {
1327                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1328                     ioc->name, __func__);
1329                 rc = -EAGAIN;
1330                 goto out;
1331         }
1332
1333         rc = 0;
1334         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1335         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1336         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1337         ioc->ctl_cmds.smid = smid;
1338
1339         request_data = ioc->diag_buffer[buffer_type];
1340         request_data_sz = karg.requested_buffer_size;
1341         ioc->unique_id[buffer_type] = karg.unique_id;
1342         ioc->diag_buffer_status[buffer_type] = 0;
1343         memcpy(ioc->product_specific[buffer_type], karg.product_specific,
1344             MPT2_PRODUCT_SPECIFIC_DWORDS);
1345         ioc->diagnostic_flags[buffer_type] = karg.diagnostic_flags;
1346
1347         if (request_data) {
1348                 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1349                 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1350                         pci_free_consistent(ioc->pdev,
1351                             ioc->diag_buffer_sz[buffer_type],
1352                             request_data, request_data_dma);
1353                         request_data = NULL;
1354                 }
1355         }
1356
1357         if (request_data == NULL) {
1358                 ioc->diag_buffer_sz[buffer_type] = 0;
1359                 ioc->diag_buffer_dma[buffer_type] = 0;
1360                 request_data = pci_alloc_consistent(
1361                         ioc->pdev, request_data_sz, &request_data_dma);
1362                 if (request_data == NULL) {
1363                         printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1364                             " for diag buffers, requested size(%d)\n",
1365                             ioc->name, __func__, request_data_sz);
1366                         mpt2sas_base_free_smid(ioc, smid);
1367                         return -ENOMEM;
1368                 }
1369                 ioc->diag_buffer[buffer_type] = request_data;
1370                 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1371                 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1372         }
1373
1374         mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1375         mpi_request->BufferType = karg.buffer_type;
1376         mpi_request->Flags = cpu_to_le32(karg.diagnostic_flags);
1377         mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1378         mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1379         mpi_request->VF_ID = 0; /* TODO */
1380         mpi_request->VP_ID = 0;
1381
1382         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: diag_buffer(0x%p), "
1383             "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1384             (unsigned long long)request_data_dma, mpi_request->BufferLength));
1385
1386         for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1387                 mpi_request->ProductSpecific[i] =
1388                         cpu_to_le32(ioc->product_specific[buffer_type][i]);
1389
1390         mpt2sas_base_put_smid_default(ioc, smid);
1391         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1392             MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1393
1394         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1395                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1396                     __func__);
1397                 _debug_dump_mf(mpi_request,
1398                     sizeof(Mpi2DiagBufferPostRequest_t)/4);
1399                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1400                         issue_reset = 1;
1401                 goto issue_host_reset;
1402         }
1403
1404         /* process the completed Reply Message Frame */
1405         if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1406                 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1407                     ioc->name, __func__);
1408                 rc = -EFAULT;
1409                 goto out;
1410         }
1411
1412         mpi_reply = ioc->ctl_cmds.reply;
1413         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1414
1415         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1416                 ioc->diag_buffer_status[buffer_type] |=
1417                         MPT2_DIAG_BUFFER_IS_REGISTERED;
1418                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1419                     ioc->name, __func__));
1420         } else {
1421                 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1422                     "log_info(0x%08x)\n", ioc->name, __func__,
1423                     ioc_status, mpi_reply->IOCLogInfo);
1424                 rc = -EFAULT;
1425         }
1426
1427  issue_host_reset:
1428         if (issue_reset)
1429                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1430                     FORCE_BIG_HAMMER);
1431
1432  out:
1433
1434         if (rc && request_data)
1435                 pci_free_consistent(ioc->pdev, request_data_sz,
1436                     request_data, request_data_dma);
1437
1438         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1439         mutex_unlock(&ioc->ctl_cmds.mutex);
1440         return rc;
1441 }
1442
1443 /**
1444  * _ctl_diag_unregister - application unregister with driver
1445  * @arg - user space buffer containing ioctl content
1446  *
1447  * This will allow the driver to cleanup any memory allocated for diag
1448  * messages and to free up any resources.
1449  */
1450 static long
1451 _ctl_diag_unregister(void __user *arg)
1452 {
1453         struct mpt2_diag_unregister karg;
1454         struct MPT2SAS_ADAPTER *ioc;
1455         void *request_data;
1456         dma_addr_t request_data_dma;
1457         u32 request_data_sz;
1458         u8 buffer_type;
1459
1460         if (copy_from_user(&karg, arg, sizeof(karg))) {
1461                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1462                     __FILE__, __LINE__, __func__);
1463                 return -EFAULT;
1464         }
1465         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1466                 return -ENODEV;
1467
1468         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1469             __func__));
1470
1471         buffer_type = karg.unique_id & 0x000000ff;
1472         if (!_ctl_diag_capability(ioc, buffer_type)) {
1473                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1474                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1475                 return -EPERM;
1476         }
1477
1478         if ((ioc->diag_buffer_status[buffer_type] &
1479             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1480                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1481                     "registered\n", ioc->name, __func__, buffer_type);
1482                 return -EINVAL;
1483         }
1484         if ((ioc->diag_buffer_status[buffer_type] &
1485             MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1486                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1487                     "released\n", ioc->name, __func__, buffer_type);
1488                 return -EINVAL;
1489         }
1490
1491         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1492                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1493                     "registered\n", ioc->name, __func__, karg.unique_id);
1494                 return -EINVAL;
1495         }
1496
1497         request_data = ioc->diag_buffer[buffer_type];
1498         if (!request_data) {
1499                 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1500                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1501                 return -ENOMEM;
1502         }
1503
1504         request_data_sz = ioc->diag_buffer_sz[buffer_type];
1505         request_data_dma = ioc->diag_buffer_dma[buffer_type];
1506         pci_free_consistent(ioc->pdev, request_data_sz,
1507             request_data, request_data_dma);
1508         ioc->diag_buffer[buffer_type] = NULL;
1509         ioc->diag_buffer_status[buffer_type] = 0;
1510         return 0;
1511 }
1512
1513 /**
1514  * _ctl_diag_query - query relevant info associated with diag buffers
1515  * @arg - user space buffer containing ioctl content
1516  *
1517  * The application will send only buffer_type and unique_id.  Driver will
1518  * inspect unique_id first, if valid, fill in all the info.  If unique_id is
1519  * 0x00, the driver will return info specified by Buffer Type.
1520  */
1521 static long
1522 _ctl_diag_query(void __user *arg)
1523 {
1524         struct mpt2_diag_query karg;
1525         struct MPT2SAS_ADAPTER *ioc;
1526         void *request_data;
1527         int i;
1528         u8 buffer_type;
1529
1530         if (copy_from_user(&karg, arg, sizeof(karg))) {
1531                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1532                     __FILE__, __LINE__, __func__);
1533                 return -EFAULT;
1534         }
1535         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1536                 return -ENODEV;
1537
1538         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1539             __func__));
1540
1541         karg.application_flags = 0;
1542         buffer_type = karg.buffer_type;
1543
1544         if (!_ctl_diag_capability(ioc, buffer_type)) {
1545                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1546                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1547                 return -EPERM;
1548         }
1549
1550         if ((ioc->diag_buffer_status[buffer_type] &
1551             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1552                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1553                     "registered\n", ioc->name, __func__, buffer_type);
1554                 return -EINVAL;
1555         }
1556
1557         if (karg.unique_id & 0xffffff00) {
1558                 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1559                         printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1560                             "registered\n", ioc->name, __func__,
1561                             karg.unique_id);
1562                         return -EINVAL;
1563                 }
1564         }
1565
1566         request_data = ioc->diag_buffer[buffer_type];
1567         if (!request_data) {
1568                 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1569                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1570                 return -ENOMEM;
1571         }
1572
1573         if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1574                 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1575                     MPT2_APP_FLAGS_BUFFER_VALID);
1576         else
1577                 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1578                     MPT2_APP_FLAGS_BUFFER_VALID |
1579                     MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1580
1581         for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1582                 karg.product_specific[i] =
1583                     ioc->product_specific[buffer_type][i];
1584
1585         karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1586         karg.driver_added_buffer_size = 0;
1587         karg.unique_id = ioc->unique_id[buffer_type];
1588         karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1589
1590         if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1591                 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1592                     "data @ %p\n", ioc->name, __func__, arg);
1593                 return -EFAULT;
1594         }
1595         return 0;
1596 }
1597
1598 /**
1599  * _ctl_send_release - Diag Release Message
1600  * @ioc: per adapter object
1601  * @buffer_type - specifies either TRACE or SNAPSHOT
1602  * @issue_reset - specifies whether host reset is required.
1603  *
1604  */
1605 static int
1606 _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1607 {
1608         Mpi2DiagReleaseRequest_t *mpi_request;
1609         Mpi2DiagReleaseReply_t *mpi_reply;
1610         u16 smid;
1611         u16 ioc_status;
1612         u32 ioc_state;
1613         int rc;
1614         unsigned long timeleft;
1615
1616         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1617             __func__));
1618
1619         rc = 0;
1620         *issue_reset = 0;
1621
1622         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1623         if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1624                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
1625                     "skipping due to FAULT state\n", ioc->name,
1626                     __func__));
1627                 rc = -EAGAIN;
1628                 goto out;
1629         }
1630
1631         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1632                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1633                     ioc->name, __func__);
1634                 rc = -EAGAIN;
1635                 goto out;
1636         }
1637
1638         smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1639         if (!smid) {
1640                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1641                     ioc->name, __func__);
1642                 rc = -EAGAIN;
1643                 goto out;
1644         }
1645
1646         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1647         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1648         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1649         ioc->ctl_cmds.smid = smid;
1650
1651         mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1652         mpi_request->BufferType = buffer_type;
1653         mpi_request->VF_ID = 0; /* TODO */
1654         mpi_request->VP_ID = 0;
1655
1656         mpt2sas_base_put_smid_default(ioc, smid);
1657         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1658             MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1659
1660         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1661                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1662                     __func__);
1663                 _debug_dump_mf(mpi_request,
1664                     sizeof(Mpi2DiagReleaseRequest_t)/4);
1665                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1666                         *issue_reset = 1;
1667                 rc = -EFAULT;
1668                 goto out;
1669         }
1670
1671         /* process the completed Reply Message Frame */
1672         if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1673                 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1674                     ioc->name, __func__);
1675                 rc = -EFAULT;
1676                 goto out;
1677         }
1678
1679         mpi_reply = ioc->ctl_cmds.reply;
1680         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1681
1682         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1683                 ioc->diag_buffer_status[buffer_type] |=
1684                     MPT2_DIAG_BUFFER_IS_RELEASED;
1685                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1686                     ioc->name, __func__));
1687         } else {
1688                 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1689                     "log_info(0x%08x)\n", ioc->name, __func__,
1690                     ioc_status, mpi_reply->IOCLogInfo);
1691                 rc = -EFAULT;
1692         }
1693
1694  out:
1695         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1696         return rc;
1697 }
1698
1699 /**
1700  * _ctl_diag_release - request to send Diag Release Message to firmware
1701  * @arg - user space buffer containing ioctl content
1702  * @state - NON_BLOCKING or BLOCKING
1703  *
1704  * This allows ownership of the specified buffer to returned to the driver,
1705  * allowing an application to read the buffer without fear that firmware is
1706  * overwritting information in the buffer.
1707  */
1708 static long
1709 _ctl_diag_release(void __user *arg, enum block_state state)
1710 {
1711         struct mpt2_diag_release karg;
1712         struct MPT2SAS_ADAPTER *ioc;
1713         void *request_data;
1714         int rc;
1715         u8 buffer_type;
1716         u8 issue_reset = 0;
1717
1718         if (copy_from_user(&karg, arg, sizeof(karg))) {
1719                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1720                     __FILE__, __LINE__, __func__);
1721                 return -EFAULT;
1722         }
1723         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1724                 return -ENODEV;
1725
1726         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1727             __func__));
1728
1729         buffer_type = karg.unique_id & 0x000000ff;
1730         if (!_ctl_diag_capability(ioc, buffer_type)) {
1731                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1732                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1733                 return -EPERM;
1734         }
1735
1736         if ((ioc->diag_buffer_status[buffer_type] &
1737             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1738                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1739                     "registered\n", ioc->name, __func__, buffer_type);
1740                 return -EINVAL;
1741         }
1742
1743         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1744                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1745                     "registered\n", ioc->name, __func__, karg.unique_id);
1746                 return -EINVAL;
1747         }
1748
1749         if (ioc->diag_buffer_status[buffer_type] &
1750             MPT2_DIAG_BUFFER_IS_RELEASED) {
1751                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1752                     "is already released\n", ioc->name, __func__,
1753                     buffer_type);
1754                 return 0;
1755         }
1756
1757         request_data = ioc->diag_buffer[buffer_type];
1758
1759         if (!request_data) {
1760                 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1761                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1762                 return -ENOMEM;
1763         }
1764
1765         /* buffers were released by due to host reset */
1766         if ((ioc->diag_buffer_status[buffer_type] &
1767             MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1768                 ioc->diag_buffer_status[buffer_type] |=
1769                     MPT2_DIAG_BUFFER_IS_RELEASED;
1770                 ioc->diag_buffer_status[buffer_type] &=
1771                     ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1772                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1773                     "was released due to host reset\n", ioc->name, __func__,
1774                     buffer_type);
1775                 return 0;
1776         }
1777
1778         if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1779                 return -EAGAIN;
1780         else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1781                 return -ERESTARTSYS;
1782
1783         rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1784
1785         if (issue_reset)
1786                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1787                     FORCE_BIG_HAMMER);
1788
1789         mutex_unlock(&ioc->ctl_cmds.mutex);
1790         return rc;
1791 }
1792
1793 /**
1794  * _ctl_diag_read_buffer - request for copy of the diag buffer
1795  * @arg - user space buffer containing ioctl content
1796  * @state - NON_BLOCKING or BLOCKING
1797  */
1798 static long
1799 _ctl_diag_read_buffer(void __user *arg, enum block_state state)
1800 {
1801         struct mpt2_diag_read_buffer karg;
1802         struct mpt2_diag_read_buffer __user *uarg = arg;
1803         struct MPT2SAS_ADAPTER *ioc;
1804         void *request_data, *diag_data;
1805         Mpi2DiagBufferPostRequest_t *mpi_request;
1806         Mpi2DiagBufferPostReply_t *mpi_reply;
1807         int rc, i;
1808         u8 buffer_type;
1809         unsigned long timeleft;
1810         u16 smid;
1811         u16 ioc_status;
1812         u8 issue_reset = 0;
1813
1814         if (copy_from_user(&karg, arg, sizeof(karg))) {
1815                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1816                     __FILE__, __LINE__, __func__);
1817                 return -EFAULT;
1818         }
1819         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1820                 return -ENODEV;
1821
1822         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1823             __func__));
1824
1825         buffer_type = karg.unique_id & 0x000000ff;
1826         if (!_ctl_diag_capability(ioc, buffer_type)) {
1827                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1828                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1829                 return -EPERM;
1830         }
1831
1832         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1833                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1834                     "registered\n", ioc->name, __func__, karg.unique_id);
1835                 return -EINVAL;
1836         }
1837
1838         request_data = ioc->diag_buffer[buffer_type];
1839         if (!request_data) {
1840                 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1841                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1842                 return -ENOMEM;
1843         }
1844
1845         if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1846                 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1847                     "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1848                     __func__);
1849                 return -EINVAL;
1850         }
1851
1852         diag_data = (void *)(request_data + karg.starting_offset);
1853         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: diag_buffer(%p), "
1854             "offset(%d), sz(%d)\n", ioc->name, __func__,
1855             diag_data, karg.starting_offset, karg.bytes_to_read));
1856
1857         if (copy_to_user((void __user *)uarg->diagnostic_data,
1858             diag_data, karg.bytes_to_read)) {
1859                 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
1860                     "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
1861                     __func__, diag_data);
1862                 return -EFAULT;
1863         }
1864
1865         if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
1866                 return 0;
1867
1868         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: Reregister "
1869                 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
1870         if ((ioc->diag_buffer_status[buffer_type] &
1871             MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1872                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
1873                     "buffer_type(0x%02x) is still registered\n", ioc->name,
1874                      __func__, buffer_type));
1875                 return 0;
1876         }
1877         /* Get a free request frame and save the message context.
1878         */
1879         if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1880                 return -EAGAIN;
1881         else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1882                 return -ERESTARTSYS;
1883
1884         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1885                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1886                     ioc->name, __func__);
1887                 rc = -EAGAIN;
1888                 goto out;
1889         }
1890
1891         smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1892         if (!smid) {
1893                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1894                     ioc->name, __func__);
1895                 rc = -EAGAIN;
1896                 goto out;
1897         }
1898
1899         rc = 0;
1900         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1901         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1902         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1903         ioc->ctl_cmds.smid = smid;
1904
1905         mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1906         mpi_request->BufferType = buffer_type;
1907         mpi_request->BufferLength =
1908             cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
1909         mpi_request->BufferAddress =
1910             cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
1911         for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1912                 mpi_request->ProductSpecific[i] =
1913                         cpu_to_le32(ioc->product_specific[buffer_type][i]);
1914         mpi_request->VF_ID = 0; /* TODO */
1915         mpi_request->VP_ID = 0;
1916
1917         mpt2sas_base_put_smid_default(ioc, smid);
1918         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1919             MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1920
1921         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1922                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1923                     __func__);
1924                 _debug_dump_mf(mpi_request,
1925                     sizeof(Mpi2DiagBufferPostRequest_t)/4);
1926                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1927                         issue_reset = 1;
1928                 goto issue_host_reset;
1929         }
1930
1931         /* process the completed Reply Message Frame */
1932         if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1933                 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1934                     ioc->name, __func__);
1935                 rc = -EFAULT;
1936                 goto out;
1937         }
1938
1939         mpi_reply = ioc->ctl_cmds.reply;
1940         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1941
1942         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1943                 ioc->diag_buffer_status[buffer_type] |=
1944                     MPT2_DIAG_BUFFER_IS_REGISTERED;
1945                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1946                     ioc->name, __func__));
1947         } else {
1948                 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1949                     "log_info(0x%08x)\n", ioc->name, __func__,
1950                     ioc_status, mpi_reply->IOCLogInfo);
1951                 rc = -EFAULT;
1952         }
1953
1954  issue_host_reset:
1955         if (issue_reset)
1956                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1957                     FORCE_BIG_HAMMER);
1958
1959  out:
1960
1961         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1962         mutex_unlock(&ioc->ctl_cmds.mutex);
1963         return rc;
1964 }
1965
1966 /**
1967  * _ctl_ioctl_main - main ioctl entry point
1968  * @file - (struct file)
1969  * @cmd - ioctl opcode
1970  * @arg -
1971  */
1972 static long
1973 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg)
1974 {
1975         enum block_state state;
1976         long ret = -EINVAL;
1977
1978         state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING :
1979             BLOCKING;
1980
1981         switch (cmd) {
1982         case MPT2IOCINFO:
1983                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
1984                         ret = _ctl_getiocinfo(arg);
1985                 break;
1986         case MPT2COMMAND:
1987         {
1988                 struct mpt2_ioctl_command karg;
1989                 struct mpt2_ioctl_command __user *uarg;
1990                 struct MPT2SAS_ADAPTER *ioc;
1991
1992                 if (copy_from_user(&karg, arg, sizeof(karg))) {
1993                         printk(KERN_ERR "failure at %s:%d/%s()!\n",
1994                             __FILE__, __LINE__, __func__);
1995                         return -EFAULT;
1996                 }
1997
1998                 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
1999                     !ioc)
2000                         return -ENODEV;
2001
2002                 if (ioc->shost_recovery)
2003                         return -EAGAIN;
2004
2005                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2006                         uarg = arg;
2007                         ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2008                 }
2009                 break;
2010         }
2011         case MPT2EVENTQUERY:
2012                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2013                         ret = _ctl_eventquery(arg);
2014                 break;
2015         case MPT2EVENTENABLE:
2016                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2017                         ret = _ctl_eventenable(arg);
2018                 break;
2019         case MPT2EVENTREPORT:
2020                 ret = _ctl_eventreport(arg);
2021                 break;
2022         case MPT2HARDRESET:
2023                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2024                         ret = _ctl_do_reset(arg);
2025                 break;
2026         case MPT2BTDHMAPPING:
2027                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2028                         ret = _ctl_btdh_mapping(arg);
2029                 break;
2030         case MPT2DIAGREGISTER:
2031                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2032                         ret = _ctl_diag_register(arg, state);
2033                 break;
2034         case MPT2DIAGUNREGISTER:
2035                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2036                         ret = _ctl_diag_unregister(arg);
2037                 break;
2038         case MPT2DIAGQUERY:
2039                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2040                         ret = _ctl_diag_query(arg);
2041                 break;
2042         case MPT2DIAGRELEASE:
2043                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2044                         ret = _ctl_diag_release(arg, state);
2045                 break;
2046         case MPT2DIAGREADBUFFER:
2047                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2048                         ret = _ctl_diag_read_buffer(arg, state);
2049                 break;
2050         default:
2051         {
2052                 struct mpt2_ioctl_command karg;
2053                 struct MPT2SAS_ADAPTER *ioc;
2054
2055                 if (copy_from_user(&karg, arg, sizeof(karg))) {
2056                         printk(KERN_ERR "failure at %s:%d/%s()!\n",
2057                             __FILE__, __LINE__, __func__);
2058                         return -EFAULT;
2059                 }
2060
2061                 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2062                     !ioc)
2063                         return -ENODEV;
2064
2065                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2066                     "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2067                 break;
2068         }
2069         }
2070         return ret;
2071 }
2072
2073 /**
2074  * _ctl_ioctl - main ioctl entry point (unlocked)
2075  * @file - (struct file)
2076  * @cmd - ioctl opcode
2077  * @arg -
2078  */
2079 static long
2080 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2081 {
2082         long ret;
2083
2084         lock_kernel();
2085         ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2086         unlock_kernel();
2087         return ret;
2088 }
2089
2090 #ifdef CONFIG_COMPAT
2091 /**
2092  * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2093  * @file - (struct file)
2094  * @cmd - ioctl opcode
2095  * @arg - (struct mpt2_ioctl_command32)
2096  *
2097  * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2098  */
2099 static long
2100 _ctl_compat_mpt_command(struct file *file, unsigned cmd, unsigned long arg)
2101 {
2102         struct mpt2_ioctl_command32 karg32;
2103         struct mpt2_ioctl_command32 __user *uarg;
2104         struct mpt2_ioctl_command karg;
2105         struct MPT2SAS_ADAPTER *ioc;
2106         enum block_state state;
2107
2108         if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2109                 return -EINVAL;
2110
2111         uarg = (struct mpt2_ioctl_command32 __user *) arg;
2112
2113         if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2114                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2115                     __FILE__, __LINE__, __func__);
2116                 return -EFAULT;
2117         }
2118         if (_ctl_verify_adapter(karg32.hdr.ioc_number, &ioc) == -1 || !ioc)
2119                 return -ENODEV;
2120
2121         if (ioc->shost_recovery)
2122                 return -EAGAIN;
2123
2124         memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2125         karg.hdr.ioc_number = karg32.hdr.ioc_number;
2126         karg.hdr.port_number = karg32.hdr.port_number;
2127         karg.hdr.max_data_size = karg32.hdr.max_data_size;
2128         karg.timeout = karg32.timeout;
2129         karg.max_reply_bytes = karg32.max_reply_bytes;
2130         karg.data_in_size = karg32.data_in_size;
2131         karg.data_out_size = karg32.data_out_size;
2132         karg.max_sense_bytes = karg32.max_sense_bytes;
2133         karg.data_sge_offset = karg32.data_sge_offset;
2134         memcpy(&karg.reply_frame_buf_ptr, &karg32.reply_frame_buf_ptr,
2135             sizeof(uint32_t));
2136         memcpy(&karg.data_in_buf_ptr, &karg32.data_in_buf_ptr,
2137             sizeof(uint32_t));
2138         memcpy(&karg.data_out_buf_ptr, &karg32.data_out_buf_ptr,
2139             sizeof(uint32_t));
2140         memcpy(&karg.sense_data_ptr, &karg32.sense_data_ptr,
2141             sizeof(uint32_t));
2142         state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2143         return _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2144 }
2145
2146 /**
2147  * _ctl_ioctl_compat - main ioctl entry point (compat)
2148  * @file -
2149  * @cmd -
2150  * @arg -
2151  *
2152  * This routine handles 32 bit applications in 64bit os.
2153  */
2154 static long
2155 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2156 {
2157         long ret;
2158
2159         lock_kernel();
2160         if (cmd == MPT2COMMAND32)
2161                 ret = _ctl_compat_mpt_command(file, cmd, arg);
2162         else
2163                 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2164         unlock_kernel();
2165         return ret;
2166 }
2167 #endif
2168
2169 /* scsi host attributes */
2170
2171 /**
2172  * _ctl_version_fw_show - firmware version
2173  * @cdev - pointer to embedded class device
2174  * @buf - the buffer returned
2175  *
2176  * A sysfs 'read-only' shost attribute.
2177  */
2178 static ssize_t
2179 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2180     char *buf)
2181 {
2182         struct Scsi_Host *shost = class_to_shost(cdev);
2183         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2184
2185         return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2186             (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2187             (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2188             (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2189             ioc->facts.FWVersion.Word & 0x000000FF);
2190 }
2191 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2192
2193 /**
2194  * _ctl_version_bios_show - bios version
2195  * @cdev - pointer to embedded class device
2196  * @buf - the buffer returned
2197  *
2198  * A sysfs 'read-only' shost attribute.
2199  */
2200 static ssize_t
2201 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2202     char *buf)
2203 {
2204         struct Scsi_Host *shost = class_to_shost(cdev);
2205         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2206
2207         u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2208
2209         return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2210             (version & 0xFF000000) >> 24,
2211             (version & 0x00FF0000) >> 16,
2212             (version & 0x0000FF00) >> 8,
2213             version & 0x000000FF);
2214 }
2215 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2216
2217 /**
2218  * _ctl_version_mpi_show - MPI (message passing interface) version
2219  * @cdev - pointer to embedded class device
2220  * @buf - the buffer returned
2221  *
2222  * A sysfs 'read-only' shost attribute.
2223  */
2224 static ssize_t
2225 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2226     char *buf)
2227 {
2228         struct Scsi_Host *shost = class_to_shost(cdev);
2229         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2230
2231         return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2232             ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2233 }
2234 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2235
2236 /**
2237  * _ctl_version_product_show - product name
2238  * @cdev - pointer to embedded class device
2239  * @buf - the buffer returned
2240  *
2241  * A sysfs 'read-only' shost attribute.
2242  */
2243 static ssize_t
2244 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2245     char *buf)
2246 {
2247         struct Scsi_Host *shost = class_to_shost(cdev);
2248         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2249
2250         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2251 }
2252 static DEVICE_ATTR(version_product, S_IRUGO,
2253    _ctl_version_product_show, NULL);
2254
2255 /**
2256  * _ctl_version_nvdata_persistent_show - ndvata persistent version
2257  * @cdev - pointer to embedded class device
2258  * @buf - the buffer returned
2259  *
2260  * A sysfs 'read-only' shost attribute.
2261  */
2262 static ssize_t
2263 _ctl_version_nvdata_persistent_show(struct device *cdev,
2264     struct device_attribute *attr, char *buf)
2265 {
2266         struct Scsi_Host *shost = class_to_shost(cdev);
2267         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2268
2269         return snprintf(buf, PAGE_SIZE, "%02xh\n",
2270             le16_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2271 }
2272 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2273     _ctl_version_nvdata_persistent_show, NULL);
2274
2275 /**
2276  * _ctl_version_nvdata_default_show - nvdata default version
2277  * @cdev - pointer to embedded class device
2278  * @buf - the buffer returned
2279  *
2280  * A sysfs 'read-only' shost attribute.
2281  */
2282 static ssize_t
2283 _ctl_version_nvdata_default_show(struct device *cdev,
2284     struct device_attribute *attr, char *buf)
2285 {
2286         struct Scsi_Host *shost = class_to_shost(cdev);
2287         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2288
2289         return snprintf(buf, PAGE_SIZE, "%02xh\n",
2290             le16_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2291 }
2292 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2293     _ctl_version_nvdata_default_show, NULL);
2294
2295 /**
2296  * _ctl_board_name_show - board name
2297  * @cdev - pointer to embedded class device
2298  * @buf - the buffer returned
2299  *
2300  * A sysfs 'read-only' shost attribute.
2301  */
2302 static ssize_t
2303 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2304     char *buf)
2305 {
2306         struct Scsi_Host *shost = class_to_shost(cdev);
2307         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2308
2309         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2310 }
2311 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2312
2313 /**
2314  * _ctl_board_assembly_show - board assembly name
2315  * @cdev - pointer to embedded class device
2316  * @buf - the buffer returned
2317  *
2318  * A sysfs 'read-only' shost attribute.
2319  */
2320 static ssize_t
2321 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2322     char *buf)
2323 {
2324         struct Scsi_Host *shost = class_to_shost(cdev);
2325         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2326
2327         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2328 }
2329 static DEVICE_ATTR(board_assembly, S_IRUGO,
2330     _ctl_board_assembly_show, NULL);
2331
2332 /**
2333  * _ctl_board_tracer_show - board tracer number
2334  * @cdev - pointer to embedded class device
2335  * @buf - the buffer returned
2336  *
2337  * A sysfs 'read-only' shost attribute.
2338  */
2339 static ssize_t
2340 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2341     char *buf)
2342 {
2343         struct Scsi_Host *shost = class_to_shost(cdev);
2344         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2345
2346         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2347 }
2348 static DEVICE_ATTR(board_tracer, S_IRUGO,
2349     _ctl_board_tracer_show, NULL);
2350
2351 /**
2352  * _ctl_io_delay_show - io missing delay
2353  * @cdev - pointer to embedded class device
2354  * @buf - the buffer returned
2355  *
2356  * This is for firmware implemention for deboucing device
2357  * removal events.
2358  *
2359  * A sysfs 'read-only' shost attribute.
2360  */
2361 static ssize_t
2362 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2363     char *buf)
2364 {
2365         struct Scsi_Host *shost = class_to_shost(cdev);
2366         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2367
2368         return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2369 }
2370 static DEVICE_ATTR(io_delay, S_IRUGO,
2371     _ctl_io_delay_show, NULL);
2372
2373 /**
2374  * _ctl_device_delay_show - device missing delay
2375  * @cdev - pointer to embedded class device
2376  * @buf - the buffer returned
2377  *
2378  * This is for firmware implemention for deboucing device
2379  * removal events.
2380  *
2381  * A sysfs 'read-only' shost attribute.
2382  */
2383 static ssize_t
2384 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2385     char *buf)
2386 {
2387         struct Scsi_Host *shost = class_to_shost(cdev);
2388         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2389
2390         return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2391 }
2392 static DEVICE_ATTR(device_delay, S_IRUGO,
2393     _ctl_device_delay_show, NULL);
2394
2395 /**
2396  * _ctl_fw_queue_depth_show - global credits
2397  * @cdev - pointer to embedded class device
2398  * @buf - the buffer returned
2399  *
2400  * This is firmware queue depth limit
2401  *
2402  * A sysfs 'read-only' shost attribute.
2403  */
2404 static ssize_t
2405 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2406     char *buf)
2407 {
2408         struct Scsi_Host *shost = class_to_shost(cdev);
2409         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2410
2411         return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2412 }
2413 static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2414     _ctl_fw_queue_depth_show, NULL);
2415
2416 /**
2417  * _ctl_sas_address_show - sas address
2418  * @cdev - pointer to embedded class device
2419  * @buf - the buffer returned
2420  *
2421  * This is the controller sas address
2422  *
2423  * A sysfs 'read-only' shost attribute.
2424  */
2425 static ssize_t
2426 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2427     char *buf)
2428 {
2429         struct Scsi_Host *shost = class_to_shost(cdev);
2430         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2431
2432         return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2433             (unsigned long long)ioc->sas_hba.sas_address);
2434 }
2435 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2436     _ctl_host_sas_address_show, NULL);
2437
2438 /**
2439  * _ctl_logging_level_show - logging level
2440  * @cdev - pointer to embedded class device
2441  * @buf - the buffer returned
2442  *
2443  * A sysfs 'read/write' shost attribute.
2444  */
2445 static ssize_t
2446 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2447     char *buf)
2448 {
2449         struct Scsi_Host *shost = class_to_shost(cdev);
2450         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2451
2452         return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2453 }
2454 static ssize_t
2455 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2456     const char *buf, size_t count)
2457 {
2458         struct Scsi_Host *shost = class_to_shost(cdev);
2459         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2460         int val = 0;
2461
2462         if (sscanf(buf, "%x", &val) != 1)
2463                 return -EINVAL;
2464
2465         ioc->logging_level = val;
2466         printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2467             ioc->logging_level);
2468         return strlen(buf);
2469 }
2470 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2471     _ctl_logging_level_show, _ctl_logging_level_store);
2472
2473 struct device_attribute *mpt2sas_host_attrs[] = {
2474         &dev_attr_version_fw,
2475         &dev_attr_version_bios,
2476         &dev_attr_version_mpi,
2477         &dev_attr_version_product,
2478         &dev_attr_version_nvdata_persistent,
2479         &dev_attr_version_nvdata_default,
2480         &dev_attr_board_name,
2481         &dev_attr_board_assembly,
2482         &dev_attr_board_tracer,
2483         &dev_attr_io_delay,
2484         &dev_attr_device_delay,
2485         &dev_attr_logging_level,
2486         &dev_attr_fw_queue_depth,
2487         &dev_attr_host_sas_address,
2488         NULL,
2489 };
2490
2491 /* device attributes */
2492
2493 /**
2494  * _ctl_device_sas_address_show - sas address
2495  * @cdev - pointer to embedded class device
2496  * @buf - the buffer returned
2497  *
2498  * This is the sas address for the target
2499  *
2500  * A sysfs 'read-only' shost attribute.
2501  */
2502 static ssize_t
2503 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2504     char *buf)
2505 {
2506         struct scsi_device *sdev = to_scsi_device(dev);
2507         struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2508
2509         return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2510             (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2511 }
2512 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2513
2514 /**
2515  * _ctl_device_handle_show - device handle
2516  * @cdev - pointer to embedded class device
2517  * @buf - the buffer returned
2518  *
2519  * This is the firmware assigned device handle
2520  *
2521  * A sysfs 'read-only' shost attribute.
2522  */
2523 static ssize_t
2524 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
2525     char *buf)
2526 {
2527         struct scsi_device *sdev = to_scsi_device(dev);
2528         struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2529
2530         return snprintf(buf, PAGE_SIZE, "0x%04x\n",
2531             sas_device_priv_data->sas_target->handle);
2532 }
2533 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
2534
2535 struct device_attribute *mpt2sas_dev_attrs[] = {
2536         &dev_attr_sas_address,
2537         &dev_attr_sas_device_handle,
2538         NULL,
2539 };
2540
2541 static const struct file_operations ctl_fops = {
2542         .owner = THIS_MODULE,
2543         .unlocked_ioctl = _ctl_ioctl,
2544         .release = _ctl_release,
2545         .poll = _ctl_poll,
2546         .fasync = _ctl_fasync,
2547 #ifdef CONFIG_COMPAT
2548         .compat_ioctl = _ctl_ioctl_compat,
2549 #endif
2550 };
2551
2552 static struct miscdevice ctl_dev = {
2553         .minor  = MPT2SAS_MINOR,
2554         .name   = MPT2SAS_DEV_NAME,
2555         .fops   = &ctl_fops,
2556 };
2557
2558 /**
2559  * mpt2sas_ctl_init - main entry point for ctl.
2560  *
2561  */
2562 void
2563 mpt2sas_ctl_init(void)
2564 {
2565         async_queue = NULL;
2566         if (misc_register(&ctl_dev) < 0)
2567                 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
2568                     MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
2569
2570         init_waitqueue_head(&ctl_poll_wait);
2571 }
2572
2573 /**
2574  * mpt2sas_ctl_exit - exit point for ctl
2575  *
2576  */
2577 void
2578 mpt2sas_ctl_exit(void)
2579 {
2580         struct MPT2SAS_ADAPTER *ioc;
2581         int i;
2582
2583         list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
2584
2585                 /* free memory associated to diag buffers */
2586                 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
2587                         if (!ioc->diag_buffer[i])
2588                                 continue;
2589                         pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
2590                             ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
2591                         ioc->diag_buffer[i] = NULL;
2592                         ioc->diag_buffer_status[i] = 0;
2593                 }
2594
2595                 kfree(ioc->event_log);
2596         }
2597         misc_deregister(&ctl_dev);
2598 }
2599