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