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