]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/mpt2sas/mpt2sas_ctl.c
[SCSI] mpt2sas: Timeout occurred within the HANDSHAKE logic while waiting on firmware...
[karo-tx-linux.git] / 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         init_completion(&ioc->ctl_cmds.done);
816         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
817             timeout*HZ);
818         if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
819                 Mpi2SCSITaskManagementRequest_t *tm_request =
820                     (Mpi2SCSITaskManagementRequest_t *)mpi_request;
821                 mutex_unlock(&ioc->tm_cmds.mutex);
822                 mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
823                     tm_request->DevHandle));
824         } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
825             mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
826                 ioc->ioc_link_reset_in_progress) {
827                 ioc->ioc_link_reset_in_progress = 0;
828                 ioc->ignore_loginfos = 0;
829         }
830         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
831                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
832                     __func__);
833                 _debug_dump_mf(mpi_request, karg.data_sge_offset);
834                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
835                         issue_reset = 1;
836                 goto issue_host_reset;
837         }
838
839         mpi_reply = ioc->ctl_cmds.reply;
840         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
841
842 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
843         if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
844             (ioc->logging_level & MPT_DEBUG_TM)) {
845                 Mpi2SCSITaskManagementReply_t *tm_reply =
846                     (Mpi2SCSITaskManagementReply_t *)mpi_reply;
847
848                 printk(MPT2SAS_DEBUG_FMT "TASK_MGMT: "
849                     "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
850                     "TerminationCount(0x%08x)\n", ioc->name,
851                     tm_reply->IOCStatus, tm_reply->IOCLogInfo,
852                     tm_reply->TerminationCount);
853         }
854 #endif
855         /* copy out xdata to user */
856         if (data_in_sz) {
857                 if (copy_to_user(karg.data_in_buf_ptr, data_in,
858                     data_in_sz)) {
859                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
860                             __LINE__, __func__);
861                         ret = -ENODATA;
862                         goto out;
863                 }
864         }
865
866         /* copy out reply message frame to user */
867         if (karg.max_reply_bytes) {
868                 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
869                 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
870                     sz)) {
871                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
872                             __LINE__, __func__);
873                         ret = -ENODATA;
874                         goto out;
875                 }
876         }
877
878         /* copy out sense to user */
879         if (karg.max_sense_bytes && (mpi_request->Function ==
880             MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
881             MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
882                 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
883                 if (copy_to_user(karg.sense_data_ptr, priv_sense, sz)) {
884                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
885                             __LINE__, __func__);
886                         ret = -ENODATA;
887                         goto out;
888                 }
889         }
890
891  issue_host_reset:
892         if (issue_reset) {
893                 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
894                     mpi_request->Function ==
895                     MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
896                         printk(MPT2SAS_INFO_FMT "issue target reset: handle "
897                             "= (0x%04x)\n", ioc->name,
898                             mpi_request->FunctionDependent1);
899                         mutex_lock(&ioc->tm_cmds.mutex);
900                         mpt2sas_scsih_issue_tm(ioc,
901                             mpi_request->FunctionDependent1, 0,
902                             MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
903                         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
904                         mutex_unlock(&ioc->tm_cmds.mutex);
905                 } else
906                         mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
907                             FORCE_BIG_HAMMER);
908         }
909
910  out:
911
912         /* free memory associated with sg buffers */
913         if (data_in)
914                 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
915                     data_in_dma);
916
917         if (data_out)
918                 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
919                     data_out_dma);
920
921         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
922         mutex_unlock(&ioc->ctl_cmds.mutex);
923         return ret;
924 }
925
926 /**
927  * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
928  * @arg - user space buffer containing ioctl content
929  */
930 static long
931 _ctl_getiocinfo(void __user *arg)
932 {
933         struct mpt2_ioctl_iocinfo karg;
934         struct MPT2SAS_ADAPTER *ioc;
935         u8 revision;
936
937         if (copy_from_user(&karg, arg, sizeof(karg))) {
938                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
939                     __FILE__, __LINE__, __func__);
940                 return -EFAULT;
941         }
942         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
943                 return -ENODEV;
944
945         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
946             __func__));
947
948         memset(&karg, 0 , sizeof(karg));
949         karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
950         if (ioc->pfacts)
951                 karg.port_number = ioc->pfacts[0].PortNumber;
952         pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
953         karg.hw_rev = revision;
954         karg.pci_id = ioc->pdev->device;
955         karg.subsystem_device = ioc->pdev->subsystem_device;
956         karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
957         karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
958         karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
959         karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
960         karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
961         karg.firmware_version = ioc->facts.FWVersion.Word;
962         strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
963         strcat(karg.driver_version, "-");
964         strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
965         karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
966
967         if (copy_to_user(arg, &karg, sizeof(karg))) {
968                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
969                     __FILE__, __LINE__, __func__);
970                 return -EFAULT;
971         }
972         return 0;
973 }
974
975 /**
976  * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
977  * @arg - user space buffer containing ioctl content
978  */
979 static long
980 _ctl_eventquery(void __user *arg)
981 {
982         struct mpt2_ioctl_eventquery karg;
983         struct MPT2SAS_ADAPTER *ioc;
984
985         if (copy_from_user(&karg, arg, sizeof(karg))) {
986                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
987                     __FILE__, __LINE__, __func__);
988                 return -EFAULT;
989         }
990         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
991                 return -ENODEV;
992
993         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
994             __func__));
995
996         karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
997         memcpy(karg.event_types, ioc->event_type,
998             MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
999
1000         if (copy_to_user(arg, &karg, sizeof(karg))) {
1001                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1002                     __FILE__, __LINE__, __func__);
1003                 return -EFAULT;
1004         }
1005         return 0;
1006 }
1007
1008 /**
1009  * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1010  * @arg - user space buffer containing ioctl content
1011  */
1012 static long
1013 _ctl_eventenable(void __user *arg)
1014 {
1015         struct mpt2_ioctl_eventenable karg;
1016         struct MPT2SAS_ADAPTER *ioc;
1017
1018         if (copy_from_user(&karg, arg, sizeof(karg))) {
1019                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1020                     __FILE__, __LINE__, __func__);
1021                 return -EFAULT;
1022         }
1023         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1024                 return -ENODEV;
1025
1026         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1027             __func__));
1028
1029         if (ioc->event_log)
1030                 return 0;
1031         memcpy(ioc->event_type, karg.event_types,
1032             MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1033         mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1034
1035         /* initialize event_log */
1036         ioc->event_context = 0;
1037         ioc->aen_event_read_flag = 0;
1038         ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1039             sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1040         if (!ioc->event_log) {
1041                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1042                     __FILE__, __LINE__, __func__);
1043                 return -ENOMEM;
1044         }
1045         return 0;
1046 }
1047
1048 /**
1049  * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1050  * @arg - user space buffer containing ioctl content
1051  */
1052 static long
1053 _ctl_eventreport(void __user *arg)
1054 {
1055         struct mpt2_ioctl_eventreport karg;
1056         struct MPT2SAS_ADAPTER *ioc;
1057         u32 number_bytes, max_events, max;
1058         struct mpt2_ioctl_eventreport __user *uarg = arg;
1059
1060         if (copy_from_user(&karg, arg, sizeof(karg))) {
1061                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1062                     __FILE__, __LINE__, __func__);
1063                 return -EFAULT;
1064         }
1065         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1066                 return -ENODEV;
1067
1068         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1069             __func__));
1070
1071         number_bytes = karg.hdr.max_data_size -
1072             sizeof(struct mpt2_ioctl_header);
1073         max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1074         max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1075
1076         /* If fewer than 1 event is requested, there must have
1077          * been some type of error.
1078          */
1079         if (!max || !ioc->event_log)
1080                 return -ENODATA;
1081
1082         number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1083         if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1084                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1085                     __FILE__, __LINE__, __func__);
1086                 return -EFAULT;
1087         }
1088
1089         /* reset flag so SIGIO can restart */
1090         ioc->aen_event_read_flag = 0;
1091         return 0;
1092 }
1093
1094 /**
1095  * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1096  * @arg - user space buffer containing ioctl content
1097  */
1098 static long
1099 _ctl_do_reset(void __user *arg)
1100 {
1101         struct mpt2_ioctl_diag_reset karg;
1102         struct MPT2SAS_ADAPTER *ioc;
1103         int retval;
1104
1105         if (copy_from_user(&karg, arg, sizeof(karg))) {
1106                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1107                     __FILE__, __LINE__, __func__);
1108                 return -EFAULT;
1109         }
1110         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1111                 return -ENODEV;
1112
1113         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1114             __func__));
1115
1116         retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1117             FORCE_BIG_HAMMER);
1118         printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1119             ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1120         return 0;
1121 }
1122
1123 /**
1124  * _ctl_btdh_search_sas_device - searching for sas device
1125  * @ioc: per adapter object
1126  * @btdh: btdh ioctl payload
1127  */
1128 static int
1129 _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1130     struct mpt2_ioctl_btdh_mapping *btdh)
1131 {
1132         struct _sas_device *sas_device;
1133         unsigned long flags;
1134         int rc = 0;
1135
1136         if (list_empty(&ioc->sas_device_list))
1137                 return rc;
1138
1139         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1140         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1141                 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1142                     btdh->handle == sas_device->handle) {
1143                         btdh->bus = sas_device->channel;
1144                         btdh->id = sas_device->id;
1145                         rc = 1;
1146                         goto out;
1147                 } else if (btdh->bus == sas_device->channel && btdh->id ==
1148                     sas_device->id && btdh->handle == 0xFFFF) {
1149                         btdh->handle = sas_device->handle;
1150                         rc = 1;
1151                         goto out;
1152                 }
1153         }
1154  out:
1155         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1156         return rc;
1157 }
1158
1159 /**
1160  * _ctl_btdh_search_raid_device - searching for raid device
1161  * @ioc: per adapter object
1162  * @btdh: btdh ioctl payload
1163  */
1164 static int
1165 _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1166     struct mpt2_ioctl_btdh_mapping *btdh)
1167 {
1168         struct _raid_device *raid_device;
1169         unsigned long flags;
1170         int rc = 0;
1171
1172         if (list_empty(&ioc->raid_device_list))
1173                 return rc;
1174
1175         spin_lock_irqsave(&ioc->raid_device_lock, flags);
1176         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1177                 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1178                     btdh->handle == raid_device->handle) {
1179                         btdh->bus = raid_device->channel;
1180                         btdh->id = raid_device->id;
1181                         rc = 1;
1182                         goto out;
1183                 } else if (btdh->bus == raid_device->channel && btdh->id ==
1184                     raid_device->id && btdh->handle == 0xFFFF) {
1185                         btdh->handle = raid_device->handle;
1186                         rc = 1;
1187                         goto out;
1188                 }
1189         }
1190  out:
1191         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1192         return rc;
1193 }
1194
1195 /**
1196  * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1197  * @arg - user space buffer containing ioctl content
1198  */
1199 static long
1200 _ctl_btdh_mapping(void __user *arg)
1201 {
1202         struct mpt2_ioctl_btdh_mapping karg;
1203         struct MPT2SAS_ADAPTER *ioc;
1204         int rc;
1205
1206         if (copy_from_user(&karg, arg, sizeof(karg))) {
1207                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1208                     __FILE__, __LINE__, __func__);
1209                 return -EFAULT;
1210         }
1211         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1212                 return -ENODEV;
1213
1214         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1215             __func__));
1216
1217         rc = _ctl_btdh_search_sas_device(ioc, &karg);
1218         if (!rc)
1219                 _ctl_btdh_search_raid_device(ioc, &karg);
1220
1221         if (copy_to_user(arg, &karg, sizeof(karg))) {
1222                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1223                     __FILE__, __LINE__, __func__);
1224                 return -EFAULT;
1225         }
1226         return 0;
1227 }
1228
1229 /**
1230  * _ctl_diag_capability - return diag buffer capability
1231  * @ioc: per adapter object
1232  * @buffer_type: specifies either TRACE or SNAPSHOT
1233  *
1234  * returns 1 when diag buffer support is enabled in firmware
1235  */
1236 static u8
1237 _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1238 {
1239         u8 rc = 0;
1240
1241         switch (buffer_type) {
1242         case MPI2_DIAG_BUF_TYPE_TRACE:
1243                 if (ioc->facts.IOCCapabilities &
1244                     MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1245                         rc = 1;
1246                 break;
1247         case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1248                 if (ioc->facts.IOCCapabilities &
1249                     MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1250                         rc = 1;
1251                 break;
1252         }
1253
1254         return rc;
1255 }
1256
1257 /**
1258  * _ctl_diag_register - application register with driver
1259  * @arg - user space buffer containing ioctl content
1260  * @state - NON_BLOCKING or BLOCKING
1261  *
1262  * This will allow the driver to setup any required buffers that will be
1263  * needed by firmware to communicate with the driver.
1264  */
1265 static long
1266 _ctl_diag_register(void __user *arg, enum block_state state)
1267 {
1268         struct mpt2_diag_register karg;
1269         struct MPT2SAS_ADAPTER *ioc;
1270         int rc, i;
1271         void *request_data = NULL;
1272         dma_addr_t request_data_dma;
1273         u32 request_data_sz = 0;
1274         Mpi2DiagBufferPostRequest_t *mpi_request;
1275         Mpi2DiagBufferPostReply_t *mpi_reply;
1276         u8 buffer_type;
1277         unsigned long timeleft;
1278         u16 smid;
1279         u16 ioc_status;
1280         u8 issue_reset = 0;
1281
1282         if (copy_from_user(&karg, arg, sizeof(karg))) {
1283                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1284                     __FILE__, __LINE__, __func__);
1285                 return -EFAULT;
1286         }
1287         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1288                 return -ENODEV;
1289
1290         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1291             __func__));
1292
1293         buffer_type = karg.buffer_type;
1294         if (!_ctl_diag_capability(ioc, buffer_type)) {
1295                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1296                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1297                 return -EPERM;
1298         }
1299
1300         if (ioc->diag_buffer_status[buffer_type] &
1301             MPT2_DIAG_BUFFER_IS_REGISTERED) {
1302                 printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1303                     "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1304                     buffer_type);
1305                 return -EINVAL;
1306         }
1307
1308         if (karg.requested_buffer_size % 4)  {
1309                 printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1310                     "is not 4 byte aligned\n", ioc->name, __func__);
1311                 return -EINVAL;
1312         }
1313
1314         if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1315                 return -EAGAIN;
1316         else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1317                 return -ERESTARTSYS;
1318
1319         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1320                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1321                     ioc->name, __func__);
1322                 rc = -EAGAIN;
1323                 goto out;
1324         }
1325
1326         smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1327         if (!smid) {
1328                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1329                     ioc->name, __func__);
1330                 rc = -EAGAIN;
1331                 goto out;
1332         }
1333
1334         rc = 0;
1335         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1336         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1337         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1338         ioc->ctl_cmds.smid = smid;
1339
1340         request_data = ioc->diag_buffer[buffer_type];
1341         request_data_sz = karg.requested_buffer_size;
1342         ioc->unique_id[buffer_type] = karg.unique_id;
1343         ioc->diag_buffer_status[buffer_type] = 0;
1344         memcpy(ioc->product_specific[buffer_type], karg.product_specific,
1345             MPT2_PRODUCT_SPECIFIC_DWORDS);
1346         ioc->diagnostic_flags[buffer_type] = karg.diagnostic_flags;
1347
1348         if (request_data) {
1349                 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1350                 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1351                         pci_free_consistent(ioc->pdev,
1352                             ioc->diag_buffer_sz[buffer_type],
1353                             request_data, request_data_dma);
1354                         request_data = NULL;
1355                 }
1356         }
1357
1358         if (request_data == NULL) {
1359                 ioc->diag_buffer_sz[buffer_type] = 0;
1360                 ioc->diag_buffer_dma[buffer_type] = 0;
1361                 request_data = pci_alloc_consistent(
1362                         ioc->pdev, request_data_sz, &request_data_dma);
1363                 if (request_data == NULL) {
1364                         printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1365                             " for diag buffers, requested size(%d)\n",
1366                             ioc->name, __func__, request_data_sz);
1367                         mpt2sas_base_free_smid(ioc, smid);
1368                         return -ENOMEM;
1369                 }
1370                 ioc->diag_buffer[buffer_type] = request_data;
1371                 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1372                 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1373         }
1374
1375         mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1376         mpi_request->BufferType = karg.buffer_type;
1377         mpi_request->Flags = cpu_to_le32(karg.diagnostic_flags);
1378         mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1379         mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1380         mpi_request->VF_ID = 0; /* TODO */
1381         mpi_request->VP_ID = 0;
1382
1383         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: diag_buffer(0x%p), "
1384             "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1385             (unsigned long long)request_data_dma, mpi_request->BufferLength));
1386
1387         for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1388                 mpi_request->ProductSpecific[i] =
1389                         cpu_to_le32(ioc->product_specific[buffer_type][i]);
1390
1391         mpt2sas_base_put_smid_default(ioc, smid);
1392         init_completion(&ioc->ctl_cmds.done);
1393         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1394             MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1395
1396         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1397                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1398                     __func__);
1399                 _debug_dump_mf(mpi_request,
1400                     sizeof(Mpi2DiagBufferPostRequest_t)/4);
1401                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1402                         issue_reset = 1;
1403                 goto issue_host_reset;
1404         }
1405
1406         /* process the completed Reply Message Frame */
1407         if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1408                 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1409                     ioc->name, __func__);
1410                 rc = -EFAULT;
1411                 goto out;
1412         }
1413
1414         mpi_reply = ioc->ctl_cmds.reply;
1415         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1416
1417         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1418                 ioc->diag_buffer_status[buffer_type] |=
1419                         MPT2_DIAG_BUFFER_IS_REGISTERED;
1420                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1421                     ioc->name, __func__));
1422         } else {
1423                 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1424                     "log_info(0x%08x)\n", ioc->name, __func__,
1425                     ioc_status, mpi_reply->IOCLogInfo);
1426                 rc = -EFAULT;
1427         }
1428
1429  issue_host_reset:
1430         if (issue_reset)
1431                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1432                     FORCE_BIG_HAMMER);
1433
1434  out:
1435
1436         if (rc && request_data)
1437                 pci_free_consistent(ioc->pdev, request_data_sz,
1438                     request_data, request_data_dma);
1439
1440         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1441         mutex_unlock(&ioc->ctl_cmds.mutex);
1442         return rc;
1443 }
1444
1445 /**
1446  * _ctl_diag_unregister - application unregister with driver
1447  * @arg - user space buffer containing ioctl content
1448  *
1449  * This will allow the driver to cleanup any memory allocated for diag
1450  * messages and to free up any resources.
1451  */
1452 static long
1453 _ctl_diag_unregister(void __user *arg)
1454 {
1455         struct mpt2_diag_unregister karg;
1456         struct MPT2SAS_ADAPTER *ioc;
1457         void *request_data;
1458         dma_addr_t request_data_dma;
1459         u32 request_data_sz;
1460         u8 buffer_type;
1461
1462         if (copy_from_user(&karg, arg, sizeof(karg))) {
1463                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1464                     __FILE__, __LINE__, __func__);
1465                 return -EFAULT;
1466         }
1467         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1468                 return -ENODEV;
1469
1470         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1471             __func__));
1472
1473         buffer_type = karg.unique_id & 0x000000ff;
1474         if (!_ctl_diag_capability(ioc, buffer_type)) {
1475                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1476                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1477                 return -EPERM;
1478         }
1479
1480         if ((ioc->diag_buffer_status[buffer_type] &
1481             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1482                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1483                     "registered\n", ioc->name, __func__, buffer_type);
1484                 return -EINVAL;
1485         }
1486         if ((ioc->diag_buffer_status[buffer_type] &
1487             MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1488                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1489                     "released\n", ioc->name, __func__, buffer_type);
1490                 return -EINVAL;
1491         }
1492
1493         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1494                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1495                     "registered\n", ioc->name, __func__, karg.unique_id);
1496                 return -EINVAL;
1497         }
1498
1499         request_data = ioc->diag_buffer[buffer_type];
1500         if (!request_data) {
1501                 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1502                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1503                 return -ENOMEM;
1504         }
1505
1506         request_data_sz = ioc->diag_buffer_sz[buffer_type];
1507         request_data_dma = ioc->diag_buffer_dma[buffer_type];
1508         pci_free_consistent(ioc->pdev, request_data_sz,
1509             request_data, request_data_dma);
1510         ioc->diag_buffer[buffer_type] = NULL;
1511         ioc->diag_buffer_status[buffer_type] = 0;
1512         return 0;
1513 }
1514
1515 /**
1516  * _ctl_diag_query - query relevant info associated with diag buffers
1517  * @arg - user space buffer containing ioctl content
1518  *
1519  * The application will send only buffer_type and unique_id.  Driver will
1520  * inspect unique_id first, if valid, fill in all the info.  If unique_id is
1521  * 0x00, the driver will return info specified by Buffer Type.
1522  */
1523 static long
1524 _ctl_diag_query(void __user *arg)
1525 {
1526         struct mpt2_diag_query karg;
1527         struct MPT2SAS_ADAPTER *ioc;
1528         void *request_data;
1529         int i;
1530         u8 buffer_type;
1531
1532         if (copy_from_user(&karg, arg, sizeof(karg))) {
1533                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1534                     __FILE__, __LINE__, __func__);
1535                 return -EFAULT;
1536         }
1537         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1538                 return -ENODEV;
1539
1540         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1541             __func__));
1542
1543         karg.application_flags = 0;
1544         buffer_type = karg.buffer_type;
1545
1546         if (!_ctl_diag_capability(ioc, buffer_type)) {
1547                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1548                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1549                 return -EPERM;
1550         }
1551
1552         if ((ioc->diag_buffer_status[buffer_type] &
1553             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1554                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1555                     "registered\n", ioc->name, __func__, buffer_type);
1556                 return -EINVAL;
1557         }
1558
1559         if (karg.unique_id & 0xffffff00) {
1560                 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1561                         printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1562                             "registered\n", ioc->name, __func__,
1563                             karg.unique_id);
1564                         return -EINVAL;
1565                 }
1566         }
1567
1568         request_data = ioc->diag_buffer[buffer_type];
1569         if (!request_data) {
1570                 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1571                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1572                 return -ENOMEM;
1573         }
1574
1575         if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1576                 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1577                     MPT2_APP_FLAGS_BUFFER_VALID);
1578         else
1579                 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1580                     MPT2_APP_FLAGS_BUFFER_VALID |
1581                     MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1582
1583         for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1584                 karg.product_specific[i] =
1585                     ioc->product_specific[buffer_type][i];
1586
1587         karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1588         karg.driver_added_buffer_size = 0;
1589         karg.unique_id = ioc->unique_id[buffer_type];
1590         karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1591
1592         if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1593                 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1594                     "data @ %p\n", ioc->name, __func__, arg);
1595                 return -EFAULT;
1596         }
1597         return 0;
1598 }
1599
1600 /**
1601  * _ctl_send_release - Diag Release Message
1602  * @ioc: per adapter object
1603  * @buffer_type - specifies either TRACE or SNAPSHOT
1604  * @issue_reset - specifies whether host reset is required.
1605  *
1606  */
1607 static int
1608 _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1609 {
1610         Mpi2DiagReleaseRequest_t *mpi_request;
1611         Mpi2DiagReleaseReply_t *mpi_reply;
1612         u16 smid;
1613         u16 ioc_status;
1614         u32 ioc_state;
1615         int rc;
1616         unsigned long timeleft;
1617
1618         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1619             __func__));
1620
1621         rc = 0;
1622         *issue_reset = 0;
1623
1624         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1625         if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1626                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
1627                     "skipping due to FAULT state\n", ioc->name,
1628                     __func__));
1629                 rc = -EAGAIN;
1630                 goto out;
1631         }
1632
1633         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1634                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1635                     ioc->name, __func__);
1636                 rc = -EAGAIN;
1637                 goto out;
1638         }
1639
1640         smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1641         if (!smid) {
1642                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1643                     ioc->name, __func__);
1644                 rc = -EAGAIN;
1645                 goto out;
1646         }
1647
1648         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1649         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1650         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1651         ioc->ctl_cmds.smid = smid;
1652
1653         mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1654         mpi_request->BufferType = buffer_type;
1655         mpi_request->VF_ID = 0; /* TODO */
1656         mpi_request->VP_ID = 0;
1657
1658         mpt2sas_base_put_smid_default(ioc, smid);
1659         init_completion(&ioc->ctl_cmds.done);
1660         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1661             MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1662
1663         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1664                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1665                     __func__);
1666                 _debug_dump_mf(mpi_request,
1667                     sizeof(Mpi2DiagReleaseRequest_t)/4);
1668                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1669                         *issue_reset = 1;
1670                 rc = -EFAULT;
1671                 goto out;
1672         }
1673
1674         /* process the completed Reply Message Frame */
1675         if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1676                 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1677                     ioc->name, __func__);
1678                 rc = -EFAULT;
1679                 goto out;
1680         }
1681
1682         mpi_reply = ioc->ctl_cmds.reply;
1683         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1684
1685         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1686                 ioc->diag_buffer_status[buffer_type] |=
1687                     MPT2_DIAG_BUFFER_IS_RELEASED;
1688                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1689                     ioc->name, __func__));
1690         } else {
1691                 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1692                     "log_info(0x%08x)\n", ioc->name, __func__,
1693                     ioc_status, mpi_reply->IOCLogInfo);
1694                 rc = -EFAULT;
1695         }
1696
1697  out:
1698         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1699         return rc;
1700 }
1701
1702 /**
1703  * _ctl_diag_release - request to send Diag Release Message to firmware
1704  * @arg - user space buffer containing ioctl content
1705  * @state - NON_BLOCKING or BLOCKING
1706  *
1707  * This allows ownership of the specified buffer to returned to the driver,
1708  * allowing an application to read the buffer without fear that firmware is
1709  * overwritting information in the buffer.
1710  */
1711 static long
1712 _ctl_diag_release(void __user *arg, enum block_state state)
1713 {
1714         struct mpt2_diag_release karg;
1715         struct MPT2SAS_ADAPTER *ioc;
1716         void *request_data;
1717         int rc;
1718         u8 buffer_type;
1719         u8 issue_reset = 0;
1720
1721         if (copy_from_user(&karg, arg, sizeof(karg))) {
1722                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1723                     __FILE__, __LINE__, __func__);
1724                 return -EFAULT;
1725         }
1726         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1727                 return -ENODEV;
1728
1729         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1730             __func__));
1731
1732         buffer_type = karg.unique_id & 0x000000ff;
1733         if (!_ctl_diag_capability(ioc, buffer_type)) {
1734                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1735                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1736                 return -EPERM;
1737         }
1738
1739         if ((ioc->diag_buffer_status[buffer_type] &
1740             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1741                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1742                     "registered\n", ioc->name, __func__, buffer_type);
1743                 return -EINVAL;
1744         }
1745
1746         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1747                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1748                     "registered\n", ioc->name, __func__, karg.unique_id);
1749                 return -EINVAL;
1750         }
1751
1752         if (ioc->diag_buffer_status[buffer_type] &
1753             MPT2_DIAG_BUFFER_IS_RELEASED) {
1754                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1755                     "is already released\n", ioc->name, __func__,
1756                     buffer_type);
1757                 return 0;
1758         }
1759
1760         request_data = ioc->diag_buffer[buffer_type];
1761
1762         if (!request_data) {
1763                 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1764                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1765                 return -ENOMEM;
1766         }
1767
1768         /* buffers were released by due to host reset */
1769         if ((ioc->diag_buffer_status[buffer_type] &
1770             MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1771                 ioc->diag_buffer_status[buffer_type] |=
1772                     MPT2_DIAG_BUFFER_IS_RELEASED;
1773                 ioc->diag_buffer_status[buffer_type] &=
1774                     ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1775                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1776                     "was released due to host reset\n", ioc->name, __func__,
1777                     buffer_type);
1778                 return 0;
1779         }
1780
1781         if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1782                 return -EAGAIN;
1783         else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1784                 return -ERESTARTSYS;
1785
1786         rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1787
1788         if (issue_reset)
1789                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1790                     FORCE_BIG_HAMMER);
1791
1792         mutex_unlock(&ioc->ctl_cmds.mutex);
1793         return rc;
1794 }
1795
1796 /**
1797  * _ctl_diag_read_buffer - request for copy of the diag buffer
1798  * @arg - user space buffer containing ioctl content
1799  * @state - NON_BLOCKING or BLOCKING
1800  */
1801 static long
1802 _ctl_diag_read_buffer(void __user *arg, enum block_state state)
1803 {
1804         struct mpt2_diag_read_buffer karg;
1805         struct mpt2_diag_read_buffer __user *uarg = arg;
1806         struct MPT2SAS_ADAPTER *ioc;
1807         void *request_data, *diag_data;
1808         Mpi2DiagBufferPostRequest_t *mpi_request;
1809         Mpi2DiagBufferPostReply_t *mpi_reply;
1810         int rc, i;
1811         u8 buffer_type;
1812         unsigned long timeleft;
1813         u16 smid;
1814         u16 ioc_status;
1815         u8 issue_reset = 0;
1816
1817         if (copy_from_user(&karg, arg, sizeof(karg))) {
1818                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1819                     __FILE__, __LINE__, __func__);
1820                 return -EFAULT;
1821         }
1822         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1823                 return -ENODEV;
1824
1825         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1826             __func__));
1827
1828         buffer_type = karg.unique_id & 0x000000ff;
1829         if (!_ctl_diag_capability(ioc, buffer_type)) {
1830                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1831                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1832                 return -EPERM;
1833         }
1834
1835         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1836                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1837                     "registered\n", ioc->name, __func__, karg.unique_id);
1838                 return -EINVAL;
1839         }
1840
1841         request_data = ioc->diag_buffer[buffer_type];
1842         if (!request_data) {
1843                 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1844                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1845                 return -ENOMEM;
1846         }
1847
1848         if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1849                 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1850                     "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1851                     __func__);
1852                 return -EINVAL;
1853         }
1854
1855         diag_data = (void *)(request_data + karg.starting_offset);
1856         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: diag_buffer(%p), "
1857             "offset(%d), sz(%d)\n", ioc->name, __func__,
1858             diag_data, karg.starting_offset, karg.bytes_to_read));
1859
1860         if (copy_to_user((void __user *)uarg->diagnostic_data,
1861             diag_data, karg.bytes_to_read)) {
1862                 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
1863                     "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
1864                     __func__, diag_data);
1865                 return -EFAULT;
1866         }
1867
1868         if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
1869                 return 0;
1870
1871         dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: Reregister "
1872                 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
1873         if ((ioc->diag_buffer_status[buffer_type] &
1874             MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1875                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
1876                     "buffer_type(0x%02x) is still registered\n", ioc->name,
1877                      __func__, buffer_type));
1878                 return 0;
1879         }
1880         /* Get a free request frame and save the message context.
1881         */
1882         if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1883                 return -EAGAIN;
1884         else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1885                 return -ERESTARTSYS;
1886
1887         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1888                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1889                     ioc->name, __func__);
1890                 rc = -EAGAIN;
1891                 goto out;
1892         }
1893
1894         smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1895         if (!smid) {
1896                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1897                     ioc->name, __func__);
1898                 rc = -EAGAIN;
1899                 goto out;
1900         }
1901
1902         rc = 0;
1903         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1904         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1905         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1906         ioc->ctl_cmds.smid = smid;
1907
1908         mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1909         mpi_request->BufferType = buffer_type;
1910         mpi_request->BufferLength =
1911             cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
1912         mpi_request->BufferAddress =
1913             cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
1914         for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1915                 mpi_request->ProductSpecific[i] =
1916                         cpu_to_le32(ioc->product_specific[buffer_type][i]);
1917         mpi_request->VF_ID = 0; /* TODO */
1918         mpi_request->VP_ID = 0;
1919
1920         mpt2sas_base_put_smid_default(ioc, smid);
1921         init_completion(&ioc->ctl_cmds.done);
1922         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1923             MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1924
1925         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1926                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1927                     __func__);
1928                 _debug_dump_mf(mpi_request,
1929                     sizeof(Mpi2DiagBufferPostRequest_t)/4);
1930                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1931                         issue_reset = 1;
1932                 goto issue_host_reset;
1933         }
1934
1935         /* process the completed Reply Message Frame */
1936         if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1937                 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1938                     ioc->name, __func__);
1939                 rc = -EFAULT;
1940                 goto out;
1941         }
1942
1943         mpi_reply = ioc->ctl_cmds.reply;
1944         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1945
1946         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1947                 ioc->diag_buffer_status[buffer_type] |=
1948                     MPT2_DIAG_BUFFER_IS_REGISTERED;
1949                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1950                     ioc->name, __func__));
1951         } else {
1952                 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1953                     "log_info(0x%08x)\n", ioc->name, __func__,
1954                     ioc_status, mpi_reply->IOCLogInfo);
1955                 rc = -EFAULT;
1956         }
1957
1958  issue_host_reset:
1959         if (issue_reset)
1960                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1961                     FORCE_BIG_HAMMER);
1962
1963  out:
1964
1965         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1966         mutex_unlock(&ioc->ctl_cmds.mutex);
1967         return rc;
1968 }
1969
1970 /**
1971  * _ctl_ioctl_main - main ioctl entry point
1972  * @file - (struct file)
1973  * @cmd - ioctl opcode
1974  * @arg -
1975  */
1976 static long
1977 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg)
1978 {
1979         enum block_state state;
1980         long ret = -EINVAL;
1981
1982         state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING :
1983             BLOCKING;
1984
1985         switch (cmd) {
1986         case MPT2IOCINFO:
1987                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
1988                         ret = _ctl_getiocinfo(arg);
1989                 break;
1990         case MPT2COMMAND:
1991         {
1992                 struct mpt2_ioctl_command karg;
1993                 struct mpt2_ioctl_command __user *uarg;
1994                 struct MPT2SAS_ADAPTER *ioc;
1995
1996                 if (copy_from_user(&karg, arg, sizeof(karg))) {
1997                         printk(KERN_ERR "failure at %s:%d/%s()!\n",
1998                             __FILE__, __LINE__, __func__);
1999                         return -EFAULT;
2000                 }
2001
2002                 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2003                     !ioc)
2004                         return -ENODEV;
2005
2006                 if (ioc->shost_recovery)
2007                         return -EAGAIN;
2008
2009                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2010                         uarg = arg;
2011                         ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2012                 }
2013                 break;
2014         }
2015         case MPT2EVENTQUERY:
2016                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2017                         ret = _ctl_eventquery(arg);
2018                 break;
2019         case MPT2EVENTENABLE:
2020                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2021                         ret = _ctl_eventenable(arg);
2022                 break;
2023         case MPT2EVENTREPORT:
2024                 ret = _ctl_eventreport(arg);
2025                 break;
2026         case MPT2HARDRESET:
2027                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2028                         ret = _ctl_do_reset(arg);
2029                 break;
2030         case MPT2BTDHMAPPING:
2031                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2032                         ret = _ctl_btdh_mapping(arg);
2033                 break;
2034         case MPT2DIAGREGISTER:
2035                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2036                         ret = _ctl_diag_register(arg, state);
2037                 break;
2038         case MPT2DIAGUNREGISTER:
2039                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2040                         ret = _ctl_diag_unregister(arg);
2041                 break;
2042         case MPT2DIAGQUERY:
2043                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2044                         ret = _ctl_diag_query(arg);
2045                 break;
2046         case MPT2DIAGRELEASE:
2047                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2048                         ret = _ctl_diag_release(arg, state);
2049                 break;
2050         case MPT2DIAGREADBUFFER:
2051                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2052                         ret = _ctl_diag_read_buffer(arg, state);
2053                 break;
2054         default:
2055         {
2056                 struct mpt2_ioctl_command karg;
2057                 struct MPT2SAS_ADAPTER *ioc;
2058
2059                 if (copy_from_user(&karg, arg, sizeof(karg))) {
2060                         printk(KERN_ERR "failure at %s:%d/%s()!\n",
2061                             __FILE__, __LINE__, __func__);
2062                         return -EFAULT;
2063                 }
2064
2065                 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2066                     !ioc)
2067                         return -ENODEV;
2068
2069                 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2070                     "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2071                 break;
2072         }
2073         }
2074         return ret;
2075 }
2076
2077 /**
2078  * _ctl_ioctl - main ioctl entry point (unlocked)
2079  * @file - (struct file)
2080  * @cmd - ioctl opcode
2081  * @arg -
2082  */
2083 static long
2084 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2085 {
2086         long ret;
2087
2088         lock_kernel();
2089         ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2090         unlock_kernel();
2091         return ret;
2092 }
2093
2094 #ifdef CONFIG_COMPAT
2095 /**
2096  * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2097  * @file - (struct file)
2098  * @cmd - ioctl opcode
2099  * @arg - (struct mpt2_ioctl_command32)
2100  *
2101  * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2102  */
2103 static long
2104 _ctl_compat_mpt_command(struct file *file, unsigned cmd, unsigned long arg)
2105 {
2106         struct mpt2_ioctl_command32 karg32;
2107         struct mpt2_ioctl_command32 __user *uarg;
2108         struct mpt2_ioctl_command karg;
2109         struct MPT2SAS_ADAPTER *ioc;
2110         enum block_state state;
2111
2112         if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2113                 return -EINVAL;
2114
2115         uarg = (struct mpt2_ioctl_command32 __user *) arg;
2116
2117         if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2118                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2119                     __FILE__, __LINE__, __func__);
2120                 return -EFAULT;
2121         }
2122         if (_ctl_verify_adapter(karg32.hdr.ioc_number, &ioc) == -1 || !ioc)
2123                 return -ENODEV;
2124
2125         if (ioc->shost_recovery)
2126                 return -EAGAIN;
2127
2128         memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2129         karg.hdr.ioc_number = karg32.hdr.ioc_number;
2130         karg.hdr.port_number = karg32.hdr.port_number;
2131         karg.hdr.max_data_size = karg32.hdr.max_data_size;
2132         karg.timeout = karg32.timeout;
2133         karg.max_reply_bytes = karg32.max_reply_bytes;
2134         karg.data_in_size = karg32.data_in_size;
2135         karg.data_out_size = karg32.data_out_size;
2136         karg.max_sense_bytes = karg32.max_sense_bytes;
2137         karg.data_sge_offset = karg32.data_sge_offset;
2138         memcpy(&karg.reply_frame_buf_ptr, &karg32.reply_frame_buf_ptr,
2139             sizeof(uint32_t));
2140         memcpy(&karg.data_in_buf_ptr, &karg32.data_in_buf_ptr,
2141             sizeof(uint32_t));
2142         memcpy(&karg.data_out_buf_ptr, &karg32.data_out_buf_ptr,
2143             sizeof(uint32_t));
2144         memcpy(&karg.sense_data_ptr, &karg32.sense_data_ptr,
2145             sizeof(uint32_t));
2146         state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2147         return _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2148 }
2149
2150 /**
2151  * _ctl_ioctl_compat - main ioctl entry point (compat)
2152  * @file -
2153  * @cmd -
2154  * @arg -
2155  *
2156  * This routine handles 32 bit applications in 64bit os.
2157  */
2158 static long
2159 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2160 {
2161         long ret;
2162
2163         lock_kernel();
2164         if (cmd == MPT2COMMAND32)
2165                 ret = _ctl_compat_mpt_command(file, cmd, arg);
2166         else
2167                 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2168         unlock_kernel();
2169         return ret;
2170 }
2171 #endif
2172
2173 /* scsi host attributes */
2174
2175 /**
2176  * _ctl_version_fw_show - firmware version
2177  * @cdev - pointer to embedded class device
2178  * @buf - the buffer returned
2179  *
2180  * A sysfs 'read-only' shost attribute.
2181  */
2182 static ssize_t
2183 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2184     char *buf)
2185 {
2186         struct Scsi_Host *shost = class_to_shost(cdev);
2187         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2188
2189         return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2190             (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2191             (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2192             (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2193             ioc->facts.FWVersion.Word & 0x000000FF);
2194 }
2195 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2196
2197 /**
2198  * _ctl_version_bios_show - bios version
2199  * @cdev - pointer to embedded class device
2200  * @buf - the buffer returned
2201  *
2202  * A sysfs 'read-only' shost attribute.
2203  */
2204 static ssize_t
2205 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2206     char *buf)
2207 {
2208         struct Scsi_Host *shost = class_to_shost(cdev);
2209         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2210
2211         u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2212
2213         return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2214             (version & 0xFF000000) >> 24,
2215             (version & 0x00FF0000) >> 16,
2216             (version & 0x0000FF00) >> 8,
2217             version & 0x000000FF);
2218 }
2219 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2220
2221 /**
2222  * _ctl_version_mpi_show - MPI (message passing interface) version
2223  * @cdev - pointer to embedded class device
2224  * @buf - the buffer returned
2225  *
2226  * A sysfs 'read-only' shost attribute.
2227  */
2228 static ssize_t
2229 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2230     char *buf)
2231 {
2232         struct Scsi_Host *shost = class_to_shost(cdev);
2233         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2234
2235         return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2236             ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2237 }
2238 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2239
2240 /**
2241  * _ctl_version_product_show - product name
2242  * @cdev - pointer to embedded class device
2243  * @buf - the buffer returned
2244  *
2245  * A sysfs 'read-only' shost attribute.
2246  */
2247 static ssize_t
2248 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2249     char *buf)
2250 {
2251         struct Scsi_Host *shost = class_to_shost(cdev);
2252         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2253
2254         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2255 }
2256 static DEVICE_ATTR(version_product, S_IRUGO,
2257    _ctl_version_product_show, NULL);
2258
2259 /**
2260  * _ctl_version_nvdata_persistent_show - ndvata persistent version
2261  * @cdev - pointer to embedded class device
2262  * @buf - the buffer returned
2263  *
2264  * A sysfs 'read-only' shost attribute.
2265  */
2266 static ssize_t
2267 _ctl_version_nvdata_persistent_show(struct device *cdev,
2268     struct device_attribute *attr, char *buf)
2269 {
2270         struct Scsi_Host *shost = class_to_shost(cdev);
2271         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2272
2273         return snprintf(buf, PAGE_SIZE, "%02xh\n",
2274             le16_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2275 }
2276 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2277     _ctl_version_nvdata_persistent_show, NULL);
2278
2279 /**
2280  * _ctl_version_nvdata_default_show - nvdata default version
2281  * @cdev - pointer to embedded class device
2282  * @buf - the buffer returned
2283  *
2284  * A sysfs 'read-only' shost attribute.
2285  */
2286 static ssize_t
2287 _ctl_version_nvdata_default_show(struct device *cdev,
2288     struct device_attribute *attr, char *buf)
2289 {
2290         struct Scsi_Host *shost = class_to_shost(cdev);
2291         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2292
2293         return snprintf(buf, PAGE_SIZE, "%02xh\n",
2294             le16_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2295 }
2296 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2297     _ctl_version_nvdata_default_show, NULL);
2298
2299 /**
2300  * _ctl_board_name_show - board name
2301  * @cdev - pointer to embedded class device
2302  * @buf - the buffer returned
2303  *
2304  * A sysfs 'read-only' shost attribute.
2305  */
2306 static ssize_t
2307 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2308     char *buf)
2309 {
2310         struct Scsi_Host *shost = class_to_shost(cdev);
2311         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2312
2313         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2314 }
2315 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2316
2317 /**
2318  * _ctl_board_assembly_show - board assembly name
2319  * @cdev - pointer to embedded class device
2320  * @buf - the buffer returned
2321  *
2322  * A sysfs 'read-only' shost attribute.
2323  */
2324 static ssize_t
2325 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2326     char *buf)
2327 {
2328         struct Scsi_Host *shost = class_to_shost(cdev);
2329         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2330
2331         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2332 }
2333 static DEVICE_ATTR(board_assembly, S_IRUGO,
2334     _ctl_board_assembly_show, NULL);
2335
2336 /**
2337  * _ctl_board_tracer_show - board tracer number
2338  * @cdev - pointer to embedded class device
2339  * @buf - the buffer returned
2340  *
2341  * A sysfs 'read-only' shost attribute.
2342  */
2343 static ssize_t
2344 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2345     char *buf)
2346 {
2347         struct Scsi_Host *shost = class_to_shost(cdev);
2348         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2349
2350         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2351 }
2352 static DEVICE_ATTR(board_tracer, S_IRUGO,
2353     _ctl_board_tracer_show, NULL);
2354
2355 /**
2356  * _ctl_io_delay_show - io missing delay
2357  * @cdev - pointer to embedded class device
2358  * @buf - the buffer returned
2359  *
2360  * This is for firmware implemention for deboucing device
2361  * removal events.
2362  *
2363  * A sysfs 'read-only' shost attribute.
2364  */
2365 static ssize_t
2366 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2367     char *buf)
2368 {
2369         struct Scsi_Host *shost = class_to_shost(cdev);
2370         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2371
2372         return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2373 }
2374 static DEVICE_ATTR(io_delay, S_IRUGO,
2375     _ctl_io_delay_show, NULL);
2376
2377 /**
2378  * _ctl_device_delay_show - device missing delay
2379  * @cdev - pointer to embedded class device
2380  * @buf - the buffer returned
2381  *
2382  * This is for firmware implemention for deboucing device
2383  * removal events.
2384  *
2385  * A sysfs 'read-only' shost attribute.
2386  */
2387 static ssize_t
2388 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2389     char *buf)
2390 {
2391         struct Scsi_Host *shost = class_to_shost(cdev);
2392         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2393
2394         return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2395 }
2396 static DEVICE_ATTR(device_delay, S_IRUGO,
2397     _ctl_device_delay_show, NULL);
2398
2399 /**
2400  * _ctl_fw_queue_depth_show - global credits
2401  * @cdev - pointer to embedded class device
2402  * @buf - the buffer returned
2403  *
2404  * This is firmware queue depth limit
2405  *
2406  * A sysfs 'read-only' shost attribute.
2407  */
2408 static ssize_t
2409 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2410     char *buf)
2411 {
2412         struct Scsi_Host *shost = class_to_shost(cdev);
2413         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2414
2415         return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2416 }
2417 static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2418     _ctl_fw_queue_depth_show, NULL);
2419
2420 /**
2421  * _ctl_sas_address_show - sas address
2422  * @cdev - pointer to embedded class device
2423  * @buf - the buffer returned
2424  *
2425  * This is the controller sas address
2426  *
2427  * A sysfs 'read-only' shost attribute.
2428  */
2429 static ssize_t
2430 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2431     char *buf)
2432 {
2433         struct Scsi_Host *shost = class_to_shost(cdev);
2434         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2435
2436         return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2437             (unsigned long long)ioc->sas_hba.sas_address);
2438 }
2439 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2440     _ctl_host_sas_address_show, NULL);
2441
2442 /**
2443  * _ctl_logging_level_show - logging level
2444  * @cdev - pointer to embedded class device
2445  * @buf - the buffer returned
2446  *
2447  * A sysfs 'read/write' shost attribute.
2448  */
2449 static ssize_t
2450 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2451     char *buf)
2452 {
2453         struct Scsi_Host *shost = class_to_shost(cdev);
2454         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2455
2456         return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2457 }
2458 static ssize_t
2459 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2460     const char *buf, size_t count)
2461 {
2462         struct Scsi_Host *shost = class_to_shost(cdev);
2463         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2464         int val = 0;
2465
2466         if (sscanf(buf, "%x", &val) != 1)
2467                 return -EINVAL;
2468
2469         ioc->logging_level = val;
2470         printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2471             ioc->logging_level);
2472         return strlen(buf);
2473 }
2474 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2475     _ctl_logging_level_show, _ctl_logging_level_store);
2476
2477 struct device_attribute *mpt2sas_host_attrs[] = {
2478         &dev_attr_version_fw,
2479         &dev_attr_version_bios,
2480         &dev_attr_version_mpi,
2481         &dev_attr_version_product,
2482         &dev_attr_version_nvdata_persistent,
2483         &dev_attr_version_nvdata_default,
2484         &dev_attr_board_name,
2485         &dev_attr_board_assembly,
2486         &dev_attr_board_tracer,
2487         &dev_attr_io_delay,
2488         &dev_attr_device_delay,
2489         &dev_attr_logging_level,
2490         &dev_attr_fw_queue_depth,
2491         &dev_attr_host_sas_address,
2492         NULL,
2493 };
2494
2495 /* device attributes */
2496
2497 /**
2498  * _ctl_device_sas_address_show - sas address
2499  * @cdev - pointer to embedded class device
2500  * @buf - the buffer returned
2501  *
2502  * This is the sas address for the target
2503  *
2504  * A sysfs 'read-only' shost attribute.
2505  */
2506 static ssize_t
2507 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2508     char *buf)
2509 {
2510         struct scsi_device *sdev = to_scsi_device(dev);
2511         struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2512
2513         return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2514             (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2515 }
2516 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2517
2518 /**
2519  * _ctl_device_handle_show - device handle
2520  * @cdev - pointer to embedded class device
2521  * @buf - the buffer returned
2522  *
2523  * This is the firmware assigned device handle
2524  *
2525  * A sysfs 'read-only' shost attribute.
2526  */
2527 static ssize_t
2528 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
2529     char *buf)
2530 {
2531         struct scsi_device *sdev = to_scsi_device(dev);
2532         struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2533
2534         return snprintf(buf, PAGE_SIZE, "0x%04x\n",
2535             sas_device_priv_data->sas_target->handle);
2536 }
2537 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
2538
2539 struct device_attribute *mpt2sas_dev_attrs[] = {
2540         &dev_attr_sas_address,
2541         &dev_attr_sas_device_handle,
2542         NULL,
2543 };
2544
2545 static const struct file_operations ctl_fops = {
2546         .owner = THIS_MODULE,
2547         .unlocked_ioctl = _ctl_ioctl,
2548         .release = _ctl_release,
2549         .poll = _ctl_poll,
2550         .fasync = _ctl_fasync,
2551 #ifdef CONFIG_COMPAT
2552         .compat_ioctl = _ctl_ioctl_compat,
2553 #endif
2554 };
2555
2556 static struct miscdevice ctl_dev = {
2557         .minor  = MPT2SAS_MINOR,
2558         .name   = MPT2SAS_DEV_NAME,
2559         .fops   = &ctl_fops,
2560 };
2561
2562 /**
2563  * mpt2sas_ctl_init - main entry point for ctl.
2564  *
2565  */
2566 void
2567 mpt2sas_ctl_init(void)
2568 {
2569         async_queue = NULL;
2570         if (misc_register(&ctl_dev) < 0)
2571                 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
2572                     MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
2573
2574         init_waitqueue_head(&ctl_poll_wait);
2575 }
2576
2577 /**
2578  * mpt2sas_ctl_exit - exit point for ctl
2579  *
2580  */
2581 void
2582 mpt2sas_ctl_exit(void)
2583 {
2584         struct MPT2SAS_ADAPTER *ioc;
2585         int i;
2586
2587         list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
2588
2589                 /* free memory associated to diag buffers */
2590                 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
2591                         if (!ioc->diag_buffer[i])
2592                                 continue;
2593                         pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
2594                             ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
2595                         ioc->diag_buffer[i] = NULL;
2596                         ioc->diag_buffer_status[i] = 0;
2597                 }
2598
2599                 kfree(ioc->event_log);
2600         }
2601         misc_deregister(&ctl_dev);
2602 }
2603