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