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