Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[linux-2.6.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/kernel.h>
46 #include <linux/module.h>
47 #include <linux/errno.h>
48 #include <linux/init.h>
49 #include <linux/slab.h>
50 #include <linux/types.h>
51 #include <linux/pci.h>
52 #include <linux/delay.h>
53 #include <linux/mutex.h>
54 #include <linux/compat.h>
55 #include <linux/poll.h>
56
57 #include <linux/io.h>
58 #include <linux/uaccess.h>
59
60 #include "mpt2sas_base.h"
61 #include "mpt2sas_ctl.h"
62
63 static DEFINE_MUTEX(_ctl_mutex);
64 static struct fasync_struct *async_queue;
65 static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
66
67 static int _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type,
68     u8 *issue_reset);
69
70 /**
71  * enum block_state - blocking state
72  * @NON_BLOCKING: non blocking
73  * @BLOCKING: blocking
74  *
75  * These states are for ioctls that need to wait for a response
76  * from firmware, so they probably require sleep.
77  */
78 enum block_state {
79         NON_BLOCKING,
80         BLOCKING,
81 };
82
83 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
84 /**
85  * _ctl_sas_device_find_by_handle - sas device search
86  * @ioc: per adapter object
87  * @handle: sas device handle (assigned by firmware)
88  * Context: Calling function should acquire ioc->sas_device_lock
89  *
90  * This searches for sas_device based on sas_address, then return sas_device
91  * object.
92  */
93 static struct _sas_device *
94 _ctl_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
95 {
96         struct _sas_device *sas_device, *r;
97
98         r = NULL;
99         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
100                 if (sas_device->handle != handle)
101                         continue;
102                 r = sas_device;
103                 goto out;
104         }
105
106  out:
107         return r;
108 }
109
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 helpful 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 = NULL, *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         mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
683         if (!mpi_request) {
684                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a memory for "
685                     "mpi_request\n", ioc->name, __func__);
686                 ret = -ENOMEM;
687                 goto out;
688         }
689
690         /* Check for overflow and wraparound */
691         if (karg.data_sge_offset * 4 > ioc->request_sz ||
692             karg.data_sge_offset > (UINT_MAX / 4)) {
693                 ret = -EINVAL;
694                 goto out;
695         }
696
697         /* copy in request message frame from user */
698         if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
699                 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
700                     __func__);
701                 ret = -EFAULT;
702                 goto out;
703         }
704
705         if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
706                 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
707                 if (!smid) {
708                         printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
709                             ioc->name, __func__);
710                         ret = -EAGAIN;
711                         goto out;
712                 }
713         } else {
714
715                 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
716                 if (!smid) {
717                         printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
718                             ioc->name, __func__);
719                         ret = -EAGAIN;
720                         goto out;
721                 }
722         }
723
724         ret = 0;
725         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
726         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
727         request = mpt2sas_base_get_msg_frame(ioc, smid);
728         memcpy(request, mpi_request, karg.data_sge_offset*4);
729         ioc->ctl_cmds.smid = smid;
730         data_out_sz = karg.data_out_size;
731         data_in_sz = karg.data_in_size;
732
733         if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
734             mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
735                 if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
736                     le16_to_cpu(mpi_request->FunctionDependent1) >
737                     ioc->facts.MaxDevHandle) {
738                         ret = -EINVAL;
739                         mpt2sas_base_free_smid(ioc, smid);
740                         goto out;
741                 }
742         }
743
744         /* obtain dma-able memory for data transfer */
745         if (data_out_sz) /* WRITE */ {
746                 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
747                     &data_out_dma);
748                 if (!data_out) {
749                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
750                             __LINE__, __func__);
751                         ret = -ENOMEM;
752                         mpt2sas_base_free_smid(ioc, smid);
753                         goto out;
754                 }
755                 if (copy_from_user(data_out, karg.data_out_buf_ptr,
756                         data_out_sz)) {
757                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
758                             __LINE__, __func__);
759                         ret =  -EFAULT;
760                         mpt2sas_base_free_smid(ioc, smid);
761                         goto out;
762                 }
763         }
764
765         if (data_in_sz) /* READ */ {
766                 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
767                     &data_in_dma);
768                 if (!data_in) {
769                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
770                             __LINE__, __func__);
771                         ret = -ENOMEM;
772                         mpt2sas_base_free_smid(ioc, smid);
773                         goto out;
774                 }
775         }
776
777         /* add scatter gather elements */
778         psge = (void *)request + (karg.data_sge_offset*4);
779
780         if (!data_out_sz && !data_in_sz) {
781                 mpt2sas_base_build_zero_len_sge(ioc, psge);
782         } else if (data_out_sz && data_in_sz) {
783                 /* WRITE sgel first */
784                 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
785                     MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
786                 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
787                 ioc->base_add_sg_single(psge, sgl_flags |
788                     data_out_sz, data_out_dma);
789
790                 /* incr sgel */
791                 psge += ioc->sge_size;
792
793                 /* READ sgel last */
794                 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
795                     MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
796                     MPI2_SGE_FLAGS_END_OF_LIST);
797                 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
798                 ioc->base_add_sg_single(psge, sgl_flags |
799                     data_in_sz, data_in_dma);
800         } else if (data_out_sz) /* WRITE */ {
801                 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
802                     MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
803                     MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
804                 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
805                 ioc->base_add_sg_single(psge, sgl_flags |
806                     data_out_sz, data_out_dma);
807         } else if (data_in_sz) /* READ */ {
808                 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
809                     MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
810                     MPI2_SGE_FLAGS_END_OF_LIST);
811                 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
812                 ioc->base_add_sg_single(psge, sgl_flags |
813                     data_in_sz, data_in_dma);
814         }
815
816         /* send command to firmware */
817 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
818         _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
819 #endif
820
821         switch (mpi_request->Function) {
822         case MPI2_FUNCTION_SCSI_IO_REQUEST:
823         case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
824         {
825                 Mpi2SCSIIORequest_t *scsiio_request =
826                     (Mpi2SCSIIORequest_t *)request;
827                 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
828                 scsiio_request->SenseBufferLowAddress =
829                     mpt2sas_base_get_sense_buffer_dma(ioc, smid);
830                 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
831                 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
832                         mpt2sas_base_put_smid_scsi_io(ioc, smid,
833                             le16_to_cpu(mpi_request->FunctionDependent1));
834                 else
835                         mpt2sas_base_put_smid_default(ioc, smid);
836                 break;
837         }
838         case MPI2_FUNCTION_SCSI_TASK_MGMT:
839         {
840                 Mpi2SCSITaskManagementRequest_t *tm_request =
841                     (Mpi2SCSITaskManagementRequest_t *)request;
842
843                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
844                     "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
845                     le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
846
847                 if (tm_request->TaskType ==
848                     MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
849                     tm_request->TaskType ==
850                     MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
851                         if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
852                                 mpt2sas_base_free_smid(ioc, smid);
853                                 goto out;
854                         }
855                 }
856
857                 mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
858                     tm_request->DevHandle));
859                 mpt2sas_base_put_smid_hi_priority(ioc, smid);
860                 break;
861         }
862         case MPI2_FUNCTION_SMP_PASSTHROUGH:
863         {
864                 Mpi2SmpPassthroughRequest_t *smp_request =
865                     (Mpi2SmpPassthroughRequest_t *)mpi_request;
866                 u8 *data;
867
868                 /* ioc determines which port to use */
869                 smp_request->PhysicalPort = 0xFF;
870                 if (smp_request->PassthroughFlags &
871                     MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
872                         data = (u8 *)&smp_request->SGL;
873                 else
874                         data = data_out;
875
876                 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
877                         ioc->ioc_link_reset_in_progress = 1;
878                         ioc->ignore_loginfos = 1;
879                 }
880                 mpt2sas_base_put_smid_default(ioc, smid);
881                 break;
882         }
883         case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
884         {
885                 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
886                     (Mpi2SasIoUnitControlRequest_t *)mpi_request;
887
888                 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
889                     || sasiounit_request->Operation ==
890                     MPI2_SAS_OP_PHY_LINK_RESET) {
891                         ioc->ioc_link_reset_in_progress = 1;
892                         ioc->ignore_loginfos = 1;
893                 }
894                 mpt2sas_base_put_smid_default(ioc, smid);
895                 break;
896         }
897         default:
898                 mpt2sas_base_put_smid_default(ioc, smid);
899                 break;
900         }
901
902         if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
903                 timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
904         else
905                 timeout = karg.timeout;
906         init_completion(&ioc->ctl_cmds.done);
907         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
908             timeout*HZ);
909         if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
910                 Mpi2SCSITaskManagementRequest_t *tm_request =
911                     (Mpi2SCSITaskManagementRequest_t *)mpi_request;
912                 mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
913                     tm_request->DevHandle));
914         } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
915             mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
916                 ioc->ioc_link_reset_in_progress) {
917                 ioc->ioc_link_reset_in_progress = 0;
918                 ioc->ignore_loginfos = 0;
919         }
920         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
921                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
922                     __func__);
923                 _debug_dump_mf(mpi_request, karg.data_sge_offset);
924                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
925                         issue_reset = 1;
926                 goto issue_host_reset;
927         }
928
929         mpi_reply = ioc->ctl_cmds.reply;
930         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
931
932 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
933         if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
934             (ioc->logging_level & MPT_DEBUG_TM)) {
935                 Mpi2SCSITaskManagementReply_t *tm_reply =
936                     (Mpi2SCSITaskManagementReply_t *)mpi_reply;
937
938                 printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
939                     "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
940                     "TerminationCount(0x%08x)\n", ioc->name,
941                     le16_to_cpu(tm_reply->IOCStatus),
942                     le32_to_cpu(tm_reply->IOCLogInfo),
943                     le32_to_cpu(tm_reply->TerminationCount));
944         }
945 #endif
946         /* copy out xdata to user */
947         if (data_in_sz) {
948                 if (copy_to_user(karg.data_in_buf_ptr, data_in,
949                     data_in_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         /* copy out reply message frame to user */
958         if (karg.max_reply_bytes) {
959                 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
960                 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
961                     sz)) {
962                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
963                             __LINE__, __func__);
964                         ret = -ENODATA;
965                         goto out;
966                 }
967         }
968
969         /* copy out sense to user */
970         if (karg.max_sense_bytes && (mpi_request->Function ==
971             MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
972             MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
973                 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
974                 if (copy_to_user(karg.sense_data_ptr,
975                         ioc->ctl_cmds.sense, sz)) {
976                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
977                             __LINE__, __func__);
978                         ret = -ENODATA;
979                         goto out;
980                 }
981         }
982
983  issue_host_reset:
984         if (issue_reset) {
985                 ret = -ENODATA;
986                 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
987                     mpi_request->Function ==
988                     MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
989                         printk(MPT2SAS_INFO_FMT "issue target reset: handle "
990                             "= (0x%04x)\n", ioc->name,
991                             le16_to_cpu(mpi_request->FunctionDependent1));
992                         mpt2sas_halt_firmware(ioc);
993                         mpt2sas_scsih_issue_tm(ioc,
994                             le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
995                             0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
996                             0, TM_MUTEX_ON);
997                         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
998                 } else
999                         mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1000                             FORCE_BIG_HAMMER);
1001         }
1002
1003  out:
1004
1005         /* free memory associated with sg buffers */
1006         if (data_in)
1007                 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1008                     data_in_dma);
1009
1010         if (data_out)
1011                 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1012                     data_out_dma);
1013
1014         kfree(mpi_request);
1015         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1016         mutex_unlock(&ioc->ctl_cmds.mutex);
1017         return ret;
1018 }
1019
1020 /**
1021  * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
1022  * @arg - user space buffer containing ioctl content
1023  */
1024 static long
1025 _ctl_getiocinfo(void __user *arg)
1026 {
1027         struct mpt2_ioctl_iocinfo karg;
1028         struct MPT2SAS_ADAPTER *ioc;
1029         u8 revision;
1030
1031         if (copy_from_user(&karg, arg, sizeof(karg))) {
1032                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1033                     __FILE__, __LINE__, __func__);
1034                 return -EFAULT;
1035         }
1036         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1037                 return -ENODEV;
1038
1039         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1040             __func__));
1041
1042         memset(&karg, 0 , sizeof(karg));
1043         if (ioc->is_warpdrive)
1044                 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1045         else
1046                 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1047         if (ioc->pfacts)
1048                 karg.port_number = ioc->pfacts[0].PortNumber;
1049         pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
1050         karg.hw_rev = revision;
1051         karg.pci_id = ioc->pdev->device;
1052         karg.subsystem_device = ioc->pdev->subsystem_device;
1053         karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1054         karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1055         karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1056         karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1057         karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1058         karg.firmware_version = ioc->facts.FWVersion.Word;
1059         strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
1060         strcat(karg.driver_version, "-");
1061         strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1062         karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1063
1064         if (copy_to_user(arg, &karg, sizeof(karg))) {
1065                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1066                     __FILE__, __LINE__, __func__);
1067                 return -EFAULT;
1068         }
1069         return 0;
1070 }
1071
1072 /**
1073  * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
1074  * @arg - user space buffer containing ioctl content
1075  */
1076 static long
1077 _ctl_eventquery(void __user *arg)
1078 {
1079         struct mpt2_ioctl_eventquery karg;
1080         struct MPT2SAS_ADAPTER *ioc;
1081
1082         if (copy_from_user(&karg, arg, sizeof(karg))) {
1083                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1084                     __FILE__, __LINE__, __func__);
1085                 return -EFAULT;
1086         }
1087         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1088                 return -ENODEV;
1089
1090         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1091             __func__));
1092
1093         karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
1094         memcpy(karg.event_types, ioc->event_type,
1095             MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1096
1097         if (copy_to_user(arg, &karg, sizeof(karg))) {
1098                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1099                     __FILE__, __LINE__, __func__);
1100                 return -EFAULT;
1101         }
1102         return 0;
1103 }
1104
1105 /**
1106  * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1107  * @arg - user space buffer containing ioctl content
1108  */
1109 static long
1110 _ctl_eventenable(void __user *arg)
1111 {
1112         struct mpt2_ioctl_eventenable karg;
1113         struct MPT2SAS_ADAPTER *ioc;
1114
1115         if (copy_from_user(&karg, arg, sizeof(karg))) {
1116                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1117                     __FILE__, __LINE__, __func__);
1118                 return -EFAULT;
1119         }
1120         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1121                 return -ENODEV;
1122
1123         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1124             __func__));
1125
1126         if (ioc->event_log)
1127                 return 0;
1128         memcpy(ioc->event_type, karg.event_types,
1129             MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1130         mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1131
1132         /* initialize event_log */
1133         ioc->event_context = 0;
1134         ioc->aen_event_read_flag = 0;
1135         ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1136             sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1137         if (!ioc->event_log) {
1138                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1139                     __FILE__, __LINE__, __func__);
1140                 return -ENOMEM;
1141         }
1142         return 0;
1143 }
1144
1145 /**
1146  * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1147  * @arg - user space buffer containing ioctl content
1148  */
1149 static long
1150 _ctl_eventreport(void __user *arg)
1151 {
1152         struct mpt2_ioctl_eventreport karg;
1153         struct MPT2SAS_ADAPTER *ioc;
1154         u32 number_bytes, max_events, max;
1155         struct mpt2_ioctl_eventreport __user *uarg = arg;
1156
1157         if (copy_from_user(&karg, arg, sizeof(karg))) {
1158                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1159                     __FILE__, __LINE__, __func__);
1160                 return -EFAULT;
1161         }
1162         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1163                 return -ENODEV;
1164
1165         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1166             __func__));
1167
1168         number_bytes = karg.hdr.max_data_size -
1169             sizeof(struct mpt2_ioctl_header);
1170         max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1171         max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1172
1173         /* If fewer than 1 event is requested, there must have
1174          * been some type of error.
1175          */
1176         if (!max || !ioc->event_log)
1177                 return -ENODATA;
1178
1179         number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1180         if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1181                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1182                     __FILE__, __LINE__, __func__);
1183                 return -EFAULT;
1184         }
1185
1186         /* reset flag so SIGIO can restart */
1187         ioc->aen_event_read_flag = 0;
1188         return 0;
1189 }
1190
1191 /**
1192  * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1193  * @arg - user space buffer containing ioctl content
1194  */
1195 static long
1196 _ctl_do_reset(void __user *arg)
1197 {
1198         struct mpt2_ioctl_diag_reset karg;
1199         struct MPT2SAS_ADAPTER *ioc;
1200         int retval;
1201
1202         if (copy_from_user(&karg, arg, sizeof(karg))) {
1203                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1204                     __FILE__, __LINE__, __func__);
1205                 return -EFAULT;
1206         }
1207         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1208                 return -ENODEV;
1209
1210         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1211             __func__));
1212
1213         retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1214             FORCE_BIG_HAMMER);
1215         printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1216             ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1217         return 0;
1218 }
1219
1220 /**
1221  * _ctl_btdh_search_sas_device - searching for sas device
1222  * @ioc: per adapter object
1223  * @btdh: btdh ioctl payload
1224  */
1225 static int
1226 _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1227     struct mpt2_ioctl_btdh_mapping *btdh)
1228 {
1229         struct _sas_device *sas_device;
1230         unsigned long flags;
1231         int rc = 0;
1232
1233         if (list_empty(&ioc->sas_device_list))
1234                 return rc;
1235
1236         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1237         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1238                 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1239                     btdh->handle == sas_device->handle) {
1240                         btdh->bus = sas_device->channel;
1241                         btdh->id = sas_device->id;
1242                         rc = 1;
1243                         goto out;
1244                 } else if (btdh->bus == sas_device->channel && btdh->id ==
1245                     sas_device->id && btdh->handle == 0xFFFF) {
1246                         btdh->handle = sas_device->handle;
1247                         rc = 1;
1248                         goto out;
1249                 }
1250         }
1251  out:
1252         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1253         return rc;
1254 }
1255
1256 /**
1257  * _ctl_btdh_search_raid_device - searching for raid device
1258  * @ioc: per adapter object
1259  * @btdh: btdh ioctl payload
1260  */
1261 static int
1262 _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1263     struct mpt2_ioctl_btdh_mapping *btdh)
1264 {
1265         struct _raid_device *raid_device;
1266         unsigned long flags;
1267         int rc = 0;
1268
1269         if (list_empty(&ioc->raid_device_list))
1270                 return rc;
1271
1272         spin_lock_irqsave(&ioc->raid_device_lock, flags);
1273         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1274                 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1275                     btdh->handle == raid_device->handle) {
1276                         btdh->bus = raid_device->channel;
1277                         btdh->id = raid_device->id;
1278                         rc = 1;
1279                         goto out;
1280                 } else if (btdh->bus == raid_device->channel && btdh->id ==
1281                     raid_device->id && btdh->handle == 0xFFFF) {
1282                         btdh->handle = raid_device->handle;
1283                         rc = 1;
1284                         goto out;
1285                 }
1286         }
1287  out:
1288         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1289         return rc;
1290 }
1291
1292 /**
1293  * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1294  * @arg - user space buffer containing ioctl content
1295  */
1296 static long
1297 _ctl_btdh_mapping(void __user *arg)
1298 {
1299         struct mpt2_ioctl_btdh_mapping karg;
1300         struct MPT2SAS_ADAPTER *ioc;
1301         int rc;
1302
1303         if (copy_from_user(&karg, arg, sizeof(karg))) {
1304                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1305                     __FILE__, __LINE__, __func__);
1306                 return -EFAULT;
1307         }
1308         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1309                 return -ENODEV;
1310
1311         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1312             __func__));
1313
1314         rc = _ctl_btdh_search_sas_device(ioc, &karg);
1315         if (!rc)
1316                 _ctl_btdh_search_raid_device(ioc, &karg);
1317
1318         if (copy_to_user(arg, &karg, sizeof(karg))) {
1319                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1320                     __FILE__, __LINE__, __func__);
1321                 return -EFAULT;
1322         }
1323         return 0;
1324 }
1325
1326 /**
1327  * _ctl_diag_capability - return diag buffer capability
1328  * @ioc: per adapter object
1329  * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1330  *
1331  * returns 1 when diag buffer support is enabled in firmware
1332  */
1333 static u8
1334 _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1335 {
1336         u8 rc = 0;
1337
1338         switch (buffer_type) {
1339         case MPI2_DIAG_BUF_TYPE_TRACE:
1340                 if (ioc->facts.IOCCapabilities &
1341                     MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1342                         rc = 1;
1343                 break;
1344         case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1345                 if (ioc->facts.IOCCapabilities &
1346                     MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1347                         rc = 1;
1348                 break;
1349         case MPI2_DIAG_BUF_TYPE_EXTENDED:
1350                 if (ioc->facts.IOCCapabilities &
1351                     MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1352                         rc = 1;
1353         }
1354
1355         return rc;
1356 }
1357
1358 /**
1359  * _ctl_diag_register_2 - wrapper for registering diag buffer support
1360  * @ioc: per adapter object
1361  * @diag_register: the diag_register struct passed in from user space
1362  *
1363  */
1364 static long
1365 _ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
1366     struct mpt2_diag_register *diag_register)
1367 {
1368         int rc, i;
1369         void *request_data = NULL;
1370         dma_addr_t request_data_dma;
1371         u32 request_data_sz = 0;
1372         Mpi2DiagBufferPostRequest_t *mpi_request;
1373         Mpi2DiagBufferPostReply_t *mpi_reply;
1374         u8 buffer_type;
1375         unsigned long timeleft;
1376         u16 smid;
1377         u16 ioc_status;
1378         u8 issue_reset = 0;
1379
1380         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1381             __func__));
1382
1383         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1384                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1385                     ioc->name, __func__);
1386                 rc = -EAGAIN;
1387                 goto out;
1388         }
1389
1390         buffer_type = diag_register->buffer_type;
1391         if (!_ctl_diag_capability(ioc, buffer_type)) {
1392                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1393                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1394                 return -EPERM;
1395         }
1396
1397         if (ioc->diag_buffer_status[buffer_type] &
1398             MPT2_DIAG_BUFFER_IS_REGISTERED) {
1399                 printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1400                     "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1401                     buffer_type);
1402                 return -EINVAL;
1403         }
1404
1405         if (diag_register->requested_buffer_size % 4)  {
1406                 printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1407                     "is not 4 byte aligned\n", ioc->name, __func__);
1408                 return -EINVAL;
1409         }
1410
1411         smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1412         if (!smid) {
1413                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1414                     ioc->name, __func__);
1415                 rc = -EAGAIN;
1416                 goto out;
1417         }
1418
1419         rc = 0;
1420         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1421         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1422         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1423         ioc->ctl_cmds.smid = smid;
1424
1425         request_data = ioc->diag_buffer[buffer_type];
1426         request_data_sz = diag_register->requested_buffer_size;
1427         ioc->unique_id[buffer_type] = diag_register->unique_id;
1428         ioc->diag_buffer_status[buffer_type] = 0;
1429         memcpy(ioc->product_specific[buffer_type],
1430             diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
1431         ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1432
1433         if (request_data) {
1434                 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1435                 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1436                         pci_free_consistent(ioc->pdev,
1437                             ioc->diag_buffer_sz[buffer_type],
1438                             request_data, request_data_dma);
1439                         request_data = NULL;
1440                 }
1441         }
1442
1443         if (request_data == NULL) {
1444                 ioc->diag_buffer_sz[buffer_type] = 0;
1445                 ioc->diag_buffer_dma[buffer_type] = 0;
1446                 request_data = pci_alloc_consistent(
1447                         ioc->pdev, request_data_sz, &request_data_dma);
1448                 if (request_data == NULL) {
1449                         printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1450                             " for diag buffers, requested size(%d)\n",
1451                             ioc->name, __func__, request_data_sz);
1452                         mpt2sas_base_free_smid(ioc, smid);
1453                         return -ENOMEM;
1454                 }
1455                 ioc->diag_buffer[buffer_type] = request_data;
1456                 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1457                 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1458         }
1459
1460         mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1461         mpi_request->BufferType = diag_register->buffer_type;
1462         mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1463         mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1464         mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1465         mpi_request->VF_ID = 0; /* TODO */
1466         mpi_request->VP_ID = 0;
1467
1468         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(0x%p), "
1469             "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1470             (unsigned long long)request_data_dma,
1471             le32_to_cpu(mpi_request->BufferLength)));
1472
1473         for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1474                 mpi_request->ProductSpecific[i] =
1475                         cpu_to_le32(ioc->product_specific[buffer_type][i]);
1476
1477         mpt2sas_base_put_smid_default(ioc, smid);
1478         init_completion(&ioc->ctl_cmds.done);
1479         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1480             MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1481
1482         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1483                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1484                     __func__);
1485                 _debug_dump_mf(mpi_request,
1486                     sizeof(Mpi2DiagBufferPostRequest_t)/4);
1487                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1488                         issue_reset = 1;
1489                 goto issue_host_reset;
1490         }
1491
1492         /* process the completed Reply Message Frame */
1493         if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1494                 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1495                     ioc->name, __func__);
1496                 rc = -EFAULT;
1497                 goto out;
1498         }
1499
1500         mpi_reply = ioc->ctl_cmds.reply;
1501         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1502
1503         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1504                 ioc->diag_buffer_status[buffer_type] |=
1505                         MPT2_DIAG_BUFFER_IS_REGISTERED;
1506                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1507                     ioc->name, __func__));
1508         } else {
1509                 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1510                     "log_info(0x%08x)\n", ioc->name, __func__,
1511                     ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1512                 rc = -EFAULT;
1513         }
1514
1515  issue_host_reset:
1516         if (issue_reset)
1517                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1518                     FORCE_BIG_HAMMER);
1519
1520  out:
1521
1522         if (rc && request_data)
1523                 pci_free_consistent(ioc->pdev, request_data_sz,
1524                     request_data, request_data_dma);
1525
1526         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1527         return rc;
1528 }
1529
1530 /**
1531  * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
1532  * @ioc: per adapter object
1533  * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1534  *
1535  * This is called when command line option diag_buffer_enable is enabled
1536  * at driver load time.
1537  */
1538 void
1539 mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
1540 {
1541         struct mpt2_diag_register diag_register;
1542
1543         memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
1544
1545         if (bits_to_register & 1) {
1546                 printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
1547                     ioc->name);
1548                 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1549                 /* register for 1MB buffers  */
1550                 diag_register.requested_buffer_size = (1024 * 1024);
1551                 diag_register.unique_id = 0x7075900;
1552                 _ctl_diag_register_2(ioc,  &diag_register);
1553         }
1554
1555         if (bits_to_register & 2) {
1556                 printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
1557                     ioc->name);
1558                 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1559                 /* register for 2MB buffers  */
1560                 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1561                 diag_register.unique_id = 0x7075901;
1562                 _ctl_diag_register_2(ioc,  &diag_register);
1563         }
1564
1565         if (bits_to_register & 4) {
1566                 printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
1567                     ioc->name);
1568                 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1569                 /* register for 2MB buffers  */
1570                 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1571                 diag_register.unique_id = 0x7075901;
1572                 _ctl_diag_register_2(ioc,  &diag_register);
1573         }
1574 }
1575
1576 /**
1577  * _ctl_diag_register - application register with driver
1578  * @arg - user space buffer containing ioctl content
1579  * @state - NON_BLOCKING or BLOCKING
1580  *
1581  * This will allow the driver to setup any required buffers that will be
1582  * needed by firmware to communicate with the driver.
1583  */
1584 static long
1585 _ctl_diag_register(void __user *arg, enum block_state state)
1586 {
1587         struct mpt2_diag_register karg;
1588         struct MPT2SAS_ADAPTER *ioc;
1589         long rc;
1590
1591         if (copy_from_user(&karg, arg, sizeof(karg))) {
1592                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1593                     __FILE__, __LINE__, __func__);
1594                 return -EFAULT;
1595         }
1596         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1597                 return -ENODEV;
1598
1599         if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1600                 return -EAGAIN;
1601         else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1602                 return -ERESTARTSYS;
1603         rc = _ctl_diag_register_2(ioc, &karg);
1604         mutex_unlock(&ioc->ctl_cmds.mutex);
1605         return rc;
1606 }
1607
1608 /**
1609  * _ctl_diag_unregister - application unregister with driver
1610  * @arg - user space buffer containing ioctl content
1611  *
1612  * This will allow the driver to cleanup any memory allocated for diag
1613  * messages and to free up any resources.
1614  */
1615 static long
1616 _ctl_diag_unregister(void __user *arg)
1617 {
1618         struct mpt2_diag_unregister karg;
1619         struct MPT2SAS_ADAPTER *ioc;
1620         void *request_data;
1621         dma_addr_t request_data_dma;
1622         u32 request_data_sz;
1623         u8 buffer_type;
1624
1625         if (copy_from_user(&karg, arg, sizeof(karg))) {
1626                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1627                     __FILE__, __LINE__, __func__);
1628                 return -EFAULT;
1629         }
1630         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1631                 return -ENODEV;
1632
1633         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1634             __func__));
1635
1636         buffer_type = karg.unique_id & 0x000000ff;
1637         if (!_ctl_diag_capability(ioc, buffer_type)) {
1638                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1639                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1640                 return -EPERM;
1641         }
1642
1643         if ((ioc->diag_buffer_status[buffer_type] &
1644             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1645                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1646                     "registered\n", ioc->name, __func__, buffer_type);
1647                 return -EINVAL;
1648         }
1649         if ((ioc->diag_buffer_status[buffer_type] &
1650             MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1651                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1652                     "released\n", ioc->name, __func__, buffer_type);
1653                 return -EINVAL;
1654         }
1655
1656         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1657                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1658                     "registered\n", ioc->name, __func__, karg.unique_id);
1659                 return -EINVAL;
1660         }
1661
1662         request_data = ioc->diag_buffer[buffer_type];
1663         if (!request_data) {
1664                 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1665                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1666                 return -ENOMEM;
1667         }
1668
1669         request_data_sz = ioc->diag_buffer_sz[buffer_type];
1670         request_data_dma = ioc->diag_buffer_dma[buffer_type];
1671         pci_free_consistent(ioc->pdev, request_data_sz,
1672             request_data, request_data_dma);
1673         ioc->diag_buffer[buffer_type] = NULL;
1674         ioc->diag_buffer_status[buffer_type] = 0;
1675         return 0;
1676 }
1677
1678 /**
1679  * _ctl_diag_query - query relevant info associated with diag buffers
1680  * @arg - user space buffer containing ioctl content
1681  *
1682  * The application will send only buffer_type and unique_id.  Driver will
1683  * inspect unique_id first, if valid, fill in all the info.  If unique_id is
1684  * 0x00, the driver will return info specified by Buffer Type.
1685  */
1686 static long
1687 _ctl_diag_query(void __user *arg)
1688 {
1689         struct mpt2_diag_query karg;
1690         struct MPT2SAS_ADAPTER *ioc;
1691         void *request_data;
1692         int i;
1693         u8 buffer_type;
1694
1695         if (copy_from_user(&karg, arg, sizeof(karg))) {
1696                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1697                     __FILE__, __LINE__, __func__);
1698                 return -EFAULT;
1699         }
1700         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1701                 return -ENODEV;
1702
1703         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1704             __func__));
1705
1706         karg.application_flags = 0;
1707         buffer_type = karg.buffer_type;
1708
1709         if (!_ctl_diag_capability(ioc, buffer_type)) {
1710                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1711                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1712                 return -EPERM;
1713         }
1714
1715         if ((ioc->diag_buffer_status[buffer_type] &
1716             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1717                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1718                     "registered\n", ioc->name, __func__, buffer_type);
1719                 return -EINVAL;
1720         }
1721
1722         if (karg.unique_id & 0xffffff00) {
1723                 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1724                         printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1725                             "registered\n", ioc->name, __func__,
1726                             karg.unique_id);
1727                         return -EINVAL;
1728                 }
1729         }
1730
1731         request_data = ioc->diag_buffer[buffer_type];
1732         if (!request_data) {
1733                 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1734                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1735                 return -ENOMEM;
1736         }
1737
1738         if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1739                 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1740                     MPT2_APP_FLAGS_BUFFER_VALID);
1741         else
1742                 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1743                     MPT2_APP_FLAGS_BUFFER_VALID |
1744                     MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1745
1746         for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1747                 karg.product_specific[i] =
1748                     ioc->product_specific[buffer_type][i];
1749
1750         karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1751         karg.driver_added_buffer_size = 0;
1752         karg.unique_id = ioc->unique_id[buffer_type];
1753         karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1754
1755         if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1756                 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1757                     "data @ %p\n", ioc->name, __func__, arg);
1758                 return -EFAULT;
1759         }
1760         return 0;
1761 }
1762
1763 /**
1764  * _ctl_send_release - Diag Release Message
1765  * @ioc: per adapter object
1766  * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1767  * @issue_reset - specifies whether host reset is required.
1768  *
1769  */
1770 static int
1771 _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1772 {
1773         Mpi2DiagReleaseRequest_t *mpi_request;
1774         Mpi2DiagReleaseReply_t *mpi_reply;
1775         u16 smid;
1776         u16 ioc_status;
1777         u32 ioc_state;
1778         int rc;
1779         unsigned long timeleft;
1780
1781         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1782             __func__));
1783
1784         rc = 0;
1785         *issue_reset = 0;
1786
1787         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1788         if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1789                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
1790                     "skipping due to FAULT state\n", ioc->name,
1791                     __func__));
1792                 rc = -EAGAIN;
1793                 goto out;
1794         }
1795
1796         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1797                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1798                     ioc->name, __func__);
1799                 rc = -EAGAIN;
1800                 goto out;
1801         }
1802
1803         smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1804         if (!smid) {
1805                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1806                     ioc->name, __func__);
1807                 rc = -EAGAIN;
1808                 goto out;
1809         }
1810
1811         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1812         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1813         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1814         ioc->ctl_cmds.smid = smid;
1815
1816         mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1817         mpi_request->BufferType = buffer_type;
1818         mpi_request->VF_ID = 0; /* TODO */
1819         mpi_request->VP_ID = 0;
1820
1821         mpt2sas_base_put_smid_default(ioc, smid);
1822         init_completion(&ioc->ctl_cmds.done);
1823         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1824             MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1825
1826         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1827                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1828                     __func__);
1829                 _debug_dump_mf(mpi_request,
1830                     sizeof(Mpi2DiagReleaseRequest_t)/4);
1831                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1832                         *issue_reset = 1;
1833                 rc = -EFAULT;
1834                 goto out;
1835         }
1836
1837         /* process the completed Reply Message Frame */
1838         if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1839                 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1840                     ioc->name, __func__);
1841                 rc = -EFAULT;
1842                 goto out;
1843         }
1844
1845         mpi_reply = ioc->ctl_cmds.reply;
1846         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1847
1848         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1849                 ioc->diag_buffer_status[buffer_type] |=
1850                     MPT2_DIAG_BUFFER_IS_RELEASED;
1851                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1852                     ioc->name, __func__));
1853         } else {
1854                 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1855                     "log_info(0x%08x)\n", ioc->name, __func__,
1856                     ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1857                 rc = -EFAULT;
1858         }
1859
1860  out:
1861         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1862         return rc;
1863 }
1864
1865 /**
1866  * _ctl_diag_release - request to send Diag Release Message to firmware
1867  * @arg - user space buffer containing ioctl content
1868  * @state - NON_BLOCKING or BLOCKING
1869  *
1870  * This allows ownership of the specified buffer to returned to the driver,
1871  * allowing an application to read the buffer without fear that firmware is
1872  * overwritting information in the buffer.
1873  */
1874 static long
1875 _ctl_diag_release(void __user *arg, enum block_state state)
1876 {
1877         struct mpt2_diag_release karg;
1878         struct MPT2SAS_ADAPTER *ioc;
1879         void *request_data;
1880         int rc;
1881         u8 buffer_type;
1882         u8 issue_reset = 0;
1883
1884         if (copy_from_user(&karg, arg, sizeof(karg))) {
1885                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1886                     __FILE__, __LINE__, __func__);
1887                 return -EFAULT;
1888         }
1889         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1890                 return -ENODEV;
1891
1892         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1893             __func__));
1894
1895         buffer_type = karg.unique_id & 0x000000ff;
1896         if (!_ctl_diag_capability(ioc, buffer_type)) {
1897                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1898                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1899                 return -EPERM;
1900         }
1901
1902         if ((ioc->diag_buffer_status[buffer_type] &
1903             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1904                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1905                     "registered\n", ioc->name, __func__, buffer_type);
1906                 return -EINVAL;
1907         }
1908
1909         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1910                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1911                     "registered\n", ioc->name, __func__, karg.unique_id);
1912                 return -EINVAL;
1913         }
1914
1915         if (ioc->diag_buffer_status[buffer_type] &
1916             MPT2_DIAG_BUFFER_IS_RELEASED) {
1917                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1918                     "is already released\n", ioc->name, __func__,
1919                     buffer_type);
1920                 return 0;
1921         }
1922
1923         request_data = ioc->diag_buffer[buffer_type];
1924
1925         if (!request_data) {
1926                 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1927                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1928                 return -ENOMEM;
1929         }
1930
1931         /* buffers were released by due to host reset */
1932         if ((ioc->diag_buffer_status[buffer_type] &
1933             MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1934                 ioc->diag_buffer_status[buffer_type] |=
1935                     MPT2_DIAG_BUFFER_IS_RELEASED;
1936                 ioc->diag_buffer_status[buffer_type] &=
1937                     ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1938                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1939                     "was released due to host reset\n", ioc->name, __func__,
1940                     buffer_type);
1941                 return 0;
1942         }
1943
1944         if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1945                 return -EAGAIN;
1946         else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1947                 return -ERESTARTSYS;
1948
1949         rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1950
1951         if (issue_reset)
1952                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1953                     FORCE_BIG_HAMMER);
1954
1955         mutex_unlock(&ioc->ctl_cmds.mutex);
1956         return rc;
1957 }
1958
1959 /**
1960  * _ctl_diag_read_buffer - request for copy of the diag buffer
1961  * @arg - user space buffer containing ioctl content
1962  * @state - NON_BLOCKING or BLOCKING
1963  */
1964 static long
1965 _ctl_diag_read_buffer(void __user *arg, enum block_state state)
1966 {
1967         struct mpt2_diag_read_buffer karg;
1968         struct mpt2_diag_read_buffer __user *uarg = arg;
1969         struct MPT2SAS_ADAPTER *ioc;
1970         void *request_data, *diag_data;
1971         Mpi2DiagBufferPostRequest_t *mpi_request;
1972         Mpi2DiagBufferPostReply_t *mpi_reply;
1973         int rc, i;
1974         u8 buffer_type;
1975         unsigned long timeleft, request_size, copy_size;
1976         u16 smid;
1977         u16 ioc_status;
1978         u8 issue_reset = 0;
1979
1980         if (copy_from_user(&karg, arg, sizeof(karg))) {
1981                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1982                     __FILE__, __LINE__, __func__);
1983                 return -EFAULT;
1984         }
1985         if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1986                 return -ENODEV;
1987
1988         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1989             __func__));
1990
1991         buffer_type = karg.unique_id & 0x000000ff;
1992         if (!_ctl_diag_capability(ioc, buffer_type)) {
1993                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1994                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1995                 return -EPERM;
1996         }
1997
1998         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1999                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
2000                     "registered\n", ioc->name, __func__, karg.unique_id);
2001                 return -EINVAL;
2002         }
2003
2004         request_data = ioc->diag_buffer[buffer_type];
2005         if (!request_data) {
2006                 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
2007                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
2008                 return -ENOMEM;
2009         }
2010
2011         request_size = ioc->diag_buffer_sz[buffer_type];
2012
2013         if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2014                 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
2015                     "or bytes_to_read are not 4 byte aligned\n", ioc->name,
2016                     __func__);
2017                 return -EINVAL;
2018         }
2019
2020         if (karg.starting_offset > request_size)
2021                 return -EINVAL;
2022
2023         diag_data = (void *)(request_data + karg.starting_offset);
2024         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
2025             "offset(%d), sz(%d)\n", ioc->name, __func__,
2026             diag_data, karg.starting_offset, karg.bytes_to_read));
2027
2028         /* Truncate data on requests that are too large */
2029         if ((diag_data + karg.bytes_to_read < diag_data) ||
2030             (diag_data + karg.bytes_to_read > request_data + request_size))
2031                 copy_size = request_size - karg.starting_offset;
2032         else
2033                 copy_size = karg.bytes_to_read;
2034
2035         if (copy_to_user((void __user *)uarg->diagnostic_data,
2036             diag_data, copy_size)) {
2037                 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
2038                     "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
2039                     __func__, diag_data);
2040                 return -EFAULT;
2041         }
2042
2043         if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
2044                 return 0;
2045
2046         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
2047                 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
2048         if ((ioc->diag_buffer_status[buffer_type] &
2049             MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
2050                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2051                     "buffer_type(0x%02x) is still registered\n", ioc->name,
2052                      __func__, buffer_type));
2053                 return 0;
2054         }
2055         /* Get a free request frame and save the message context.
2056         */
2057         if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
2058                 return -EAGAIN;
2059         else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
2060                 return -ERESTARTSYS;
2061
2062         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
2063                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
2064                     ioc->name, __func__);
2065                 rc = -EAGAIN;
2066                 goto out;
2067         }
2068
2069         smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2070         if (!smid) {
2071                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2072                     ioc->name, __func__);
2073                 rc = -EAGAIN;
2074                 goto out;
2075         }
2076
2077         rc = 0;
2078         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
2079         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2080         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2081         ioc->ctl_cmds.smid = smid;
2082
2083         mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2084         mpi_request->BufferType = buffer_type;
2085         mpi_request->BufferLength =
2086             cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2087         mpi_request->BufferAddress =
2088             cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2089         for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
2090                 mpi_request->ProductSpecific[i] =
2091                         cpu_to_le32(ioc->product_specific[buffer_type][i]);
2092         mpi_request->VF_ID = 0; /* TODO */
2093         mpi_request->VP_ID = 0;
2094
2095         mpt2sas_base_put_smid_default(ioc, smid);
2096         init_completion(&ioc->ctl_cmds.done);
2097         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2098             MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
2099
2100         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
2101                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
2102                     __func__);
2103                 _debug_dump_mf(mpi_request,
2104                     sizeof(Mpi2DiagBufferPostRequest_t)/4);
2105                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
2106                         issue_reset = 1;
2107                 goto issue_host_reset;
2108         }
2109
2110         /* process the completed Reply Message Frame */
2111         if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
2112                 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
2113                     ioc->name, __func__);
2114                 rc = -EFAULT;
2115                 goto out;
2116         }
2117
2118         mpi_reply = ioc->ctl_cmds.reply;
2119         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2120
2121         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2122                 ioc->diag_buffer_status[buffer_type] |=
2123                     MPT2_DIAG_BUFFER_IS_REGISTERED;
2124                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
2125                     ioc->name, __func__));
2126         } else {
2127                 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
2128                     "log_info(0x%08x)\n", ioc->name, __func__,
2129                     ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2130                 rc = -EFAULT;
2131         }
2132
2133  issue_host_reset:
2134         if (issue_reset)
2135                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2136                     FORCE_BIG_HAMMER);
2137
2138  out:
2139
2140         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
2141         mutex_unlock(&ioc->ctl_cmds.mutex);
2142         return rc;
2143 }
2144
2145 /**
2146  * _ctl_ioctl_main - main ioctl entry point
2147  * @file - (struct file)
2148  * @cmd - ioctl opcode
2149  * @arg -
2150  */
2151 static long
2152 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg)
2153 {
2154         enum block_state state;
2155         long ret = -EINVAL;
2156
2157         state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING :
2158             BLOCKING;
2159
2160         switch (cmd) {
2161         case MPT2IOCINFO:
2162                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
2163                         ret = _ctl_getiocinfo(arg);
2164                 break;
2165         case MPT2COMMAND:
2166         {
2167                 struct mpt2_ioctl_command karg;
2168                 struct mpt2_ioctl_command __user *uarg;
2169                 struct MPT2SAS_ADAPTER *ioc;
2170
2171                 if (copy_from_user(&karg, arg, sizeof(karg))) {
2172                         printk(KERN_ERR "failure at %s:%d/%s()!\n",
2173                             __FILE__, __LINE__, __func__);
2174                         return -EFAULT;
2175                 }
2176
2177                 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2178                     !ioc)
2179                         return -ENODEV;
2180
2181                 if (ioc->shost_recovery || ioc->pci_error_recovery)
2182                         return -EAGAIN;
2183
2184                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2185                         uarg = arg;
2186                         ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2187                 }
2188                 break;
2189         }
2190         case MPT2EVENTQUERY:
2191                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2192                         ret = _ctl_eventquery(arg);
2193                 break;
2194         case MPT2EVENTENABLE:
2195                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2196                         ret = _ctl_eventenable(arg);
2197                 break;
2198         case MPT2EVENTREPORT:
2199                 ret = _ctl_eventreport(arg);
2200                 break;
2201         case MPT2HARDRESET:
2202                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2203                         ret = _ctl_do_reset(arg);
2204                 break;
2205         case MPT2BTDHMAPPING:
2206                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2207                         ret = _ctl_btdh_mapping(arg);
2208                 break;
2209         case MPT2DIAGREGISTER:
2210                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2211                         ret = _ctl_diag_register(arg, state);
2212                 break;
2213         case MPT2DIAGUNREGISTER:
2214                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2215                         ret = _ctl_diag_unregister(arg);
2216                 break;
2217         case MPT2DIAGQUERY:
2218                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2219                         ret = _ctl_diag_query(arg);
2220                 break;
2221         case MPT2DIAGRELEASE:
2222                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2223                         ret = _ctl_diag_release(arg, state);
2224                 break;
2225         case MPT2DIAGREADBUFFER:
2226                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2227                         ret = _ctl_diag_read_buffer(arg, state);
2228                 break;
2229         default:
2230         {
2231                 struct mpt2_ioctl_command karg;
2232                 struct MPT2SAS_ADAPTER *ioc;
2233
2234                 if (copy_from_user(&karg, arg, sizeof(karg))) {
2235                         printk(KERN_ERR "failure at %s:%d/%s()!\n",
2236                             __FILE__, __LINE__, __func__);
2237                         return -EFAULT;
2238                 }
2239
2240                 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2241                     !ioc)
2242                         return -ENODEV;
2243
2244                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
2245                     "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2246                 break;
2247         }
2248         }
2249         return ret;
2250 }
2251
2252 /**
2253  * _ctl_ioctl - main ioctl entry point (unlocked)
2254  * @file - (struct file)
2255  * @cmd - ioctl opcode
2256  * @arg -
2257  */
2258 static long
2259 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2260 {
2261         long ret;
2262
2263         mutex_lock(&_ctl_mutex);
2264         ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2265         mutex_unlock(&_ctl_mutex);
2266         return ret;
2267 }
2268
2269 #ifdef CONFIG_COMPAT
2270 /**
2271  * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2272  * @file - (struct file)
2273  * @cmd - ioctl opcode
2274  * @arg - (struct mpt2_ioctl_command32)
2275  *
2276  * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2277  */
2278 static long
2279 _ctl_compat_mpt_command(struct file *file, unsigned cmd, unsigned long arg)
2280 {
2281         struct mpt2_ioctl_command32 karg32;
2282         struct mpt2_ioctl_command32 __user *uarg;
2283         struct mpt2_ioctl_command karg;
2284         struct MPT2SAS_ADAPTER *ioc;
2285         enum block_state state;
2286
2287         if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2288                 return -EINVAL;
2289
2290         uarg = (struct mpt2_ioctl_command32 __user *) arg;
2291
2292         if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2293                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2294                     __FILE__, __LINE__, __func__);
2295                 return -EFAULT;
2296         }
2297         if (_ctl_verify_adapter(karg32.hdr.ioc_number, &ioc) == -1 || !ioc)
2298                 return -ENODEV;
2299
2300         if (ioc->shost_recovery || ioc->pci_error_recovery)
2301                 return -EAGAIN;
2302
2303         memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2304         karg.hdr.ioc_number = karg32.hdr.ioc_number;
2305         karg.hdr.port_number = karg32.hdr.port_number;
2306         karg.hdr.max_data_size = karg32.hdr.max_data_size;
2307         karg.timeout = karg32.timeout;
2308         karg.max_reply_bytes = karg32.max_reply_bytes;
2309         karg.data_in_size = karg32.data_in_size;
2310         karg.data_out_size = karg32.data_out_size;
2311         karg.max_sense_bytes = karg32.max_sense_bytes;
2312         karg.data_sge_offset = karg32.data_sge_offset;
2313         karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2314         karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2315         karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2316         karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2317         state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2318         return _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2319 }
2320
2321 /**
2322  * _ctl_ioctl_compat - main ioctl entry point (compat)
2323  * @file -
2324  * @cmd -
2325  * @arg -
2326  *
2327  * This routine handles 32 bit applications in 64bit os.
2328  */
2329 static long
2330 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2331 {
2332         long ret;
2333
2334         mutex_lock(&_ctl_mutex);
2335         if (cmd == MPT2COMMAND32)
2336                 ret = _ctl_compat_mpt_command(file, cmd, arg);
2337         else
2338                 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2339         mutex_unlock(&_ctl_mutex);
2340         return ret;
2341 }
2342 #endif
2343
2344 /* scsi host attributes */
2345
2346 /**
2347  * _ctl_version_fw_show - firmware version
2348  * @cdev - pointer to embedded class device
2349  * @buf - the buffer returned
2350  *
2351  * A sysfs 'read-only' shost attribute.
2352  */
2353 static ssize_t
2354 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2355     char *buf)
2356 {
2357         struct Scsi_Host *shost = class_to_shost(cdev);
2358         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2359
2360         return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2361             (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2362             (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2363             (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2364             ioc->facts.FWVersion.Word & 0x000000FF);
2365 }
2366 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2367
2368 /**
2369  * _ctl_version_bios_show - bios version
2370  * @cdev - pointer to embedded class device
2371  * @buf - the buffer returned
2372  *
2373  * A sysfs 'read-only' shost attribute.
2374  */
2375 static ssize_t
2376 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2377     char *buf)
2378 {
2379         struct Scsi_Host *shost = class_to_shost(cdev);
2380         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2381
2382         u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2383
2384         return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2385             (version & 0xFF000000) >> 24,
2386             (version & 0x00FF0000) >> 16,
2387             (version & 0x0000FF00) >> 8,
2388             version & 0x000000FF);
2389 }
2390 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2391
2392 /**
2393  * _ctl_version_mpi_show - MPI (message passing interface) version
2394  * @cdev - pointer to embedded class device
2395  * @buf - the buffer returned
2396  *
2397  * A sysfs 'read-only' shost attribute.
2398  */
2399 static ssize_t
2400 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2401     char *buf)
2402 {
2403         struct Scsi_Host *shost = class_to_shost(cdev);
2404         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2405
2406         return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2407             ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2408 }
2409 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2410
2411 /**
2412  * _ctl_version_product_show - product name
2413  * @cdev - pointer to embedded class device
2414  * @buf - the buffer returned
2415  *
2416  * A sysfs 'read-only' shost attribute.
2417  */
2418 static ssize_t
2419 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2420     char *buf)
2421 {
2422         struct Scsi_Host *shost = class_to_shost(cdev);
2423         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2424
2425         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2426 }
2427 static DEVICE_ATTR(version_product, S_IRUGO,
2428    _ctl_version_product_show, NULL);
2429
2430 /**
2431  * _ctl_version_nvdata_persistent_show - ndvata persistent version
2432  * @cdev - pointer to embedded class device
2433  * @buf - the buffer returned
2434  *
2435  * A sysfs 'read-only' shost attribute.
2436  */
2437 static ssize_t
2438 _ctl_version_nvdata_persistent_show(struct device *cdev,
2439     struct device_attribute *attr, char *buf)
2440 {
2441         struct Scsi_Host *shost = class_to_shost(cdev);
2442         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2443
2444         return snprintf(buf, PAGE_SIZE, "%08xh\n",
2445             le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2446 }
2447 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2448     _ctl_version_nvdata_persistent_show, NULL);
2449
2450 /**
2451  * _ctl_version_nvdata_default_show - nvdata default version
2452  * @cdev - pointer to embedded class device
2453  * @buf - the buffer returned
2454  *
2455  * A sysfs 'read-only' shost attribute.
2456  */
2457 static ssize_t
2458 _ctl_version_nvdata_default_show(struct device *cdev,
2459     struct device_attribute *attr, char *buf)
2460 {
2461         struct Scsi_Host *shost = class_to_shost(cdev);
2462         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2463
2464         return snprintf(buf, PAGE_SIZE, "%08xh\n",
2465             le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2466 }
2467 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2468     _ctl_version_nvdata_default_show, NULL);
2469
2470 /**
2471  * _ctl_board_name_show - board name
2472  * @cdev - pointer to embedded class device
2473  * @buf - the buffer returned
2474  *
2475  * A sysfs 'read-only' shost attribute.
2476  */
2477 static ssize_t
2478 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2479     char *buf)
2480 {
2481         struct Scsi_Host *shost = class_to_shost(cdev);
2482         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2483
2484         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2485 }
2486 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2487
2488 /**
2489  * _ctl_board_assembly_show - board assembly name
2490  * @cdev - pointer to embedded class device
2491  * @buf - the buffer returned
2492  *
2493  * A sysfs 'read-only' shost attribute.
2494  */
2495 static ssize_t
2496 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2497     char *buf)
2498 {
2499         struct Scsi_Host *shost = class_to_shost(cdev);
2500         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2501
2502         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2503 }
2504 static DEVICE_ATTR(board_assembly, S_IRUGO,
2505     _ctl_board_assembly_show, NULL);
2506
2507 /**
2508  * _ctl_board_tracer_show - board tracer number
2509  * @cdev - pointer to embedded class device
2510  * @buf - the buffer returned
2511  *
2512  * A sysfs 'read-only' shost attribute.
2513  */
2514 static ssize_t
2515 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2516     char *buf)
2517 {
2518         struct Scsi_Host *shost = class_to_shost(cdev);
2519         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2520
2521         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2522 }
2523 static DEVICE_ATTR(board_tracer, S_IRUGO,
2524     _ctl_board_tracer_show, NULL);
2525
2526 /**
2527  * _ctl_io_delay_show - io missing delay
2528  * @cdev - pointer to embedded class device
2529  * @buf - the buffer returned
2530  *
2531  * This is for firmware implemention for deboucing device
2532  * removal events.
2533  *
2534  * A sysfs 'read-only' shost attribute.
2535  */
2536 static ssize_t
2537 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2538     char *buf)
2539 {
2540         struct Scsi_Host *shost = class_to_shost(cdev);
2541         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2542
2543         return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2544 }
2545 static DEVICE_ATTR(io_delay, S_IRUGO,
2546     _ctl_io_delay_show, NULL);
2547
2548 /**
2549  * _ctl_device_delay_show - device missing delay
2550  * @cdev - pointer to embedded class device
2551  * @buf - the buffer returned
2552  *
2553  * This is for firmware implemention for deboucing device
2554  * removal events.
2555  *
2556  * A sysfs 'read-only' shost attribute.
2557  */
2558 static ssize_t
2559 _ctl_device_delay_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, "%02d\n", ioc->device_missing_delay);
2566 }
2567 static DEVICE_ATTR(device_delay, S_IRUGO,
2568     _ctl_device_delay_show, NULL);
2569
2570 /**
2571  * _ctl_fw_queue_depth_show - global credits
2572  * @cdev - pointer to embedded class device
2573  * @buf - the buffer returned
2574  *
2575  * This is firmware queue depth limit
2576  *
2577  * A sysfs 'read-only' shost attribute.
2578  */
2579 static ssize_t
2580 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2581     char *buf)
2582 {
2583         struct Scsi_Host *shost = class_to_shost(cdev);
2584         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2585
2586         return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2587 }
2588 static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2589     _ctl_fw_queue_depth_show, NULL);
2590
2591 /**
2592  * _ctl_sas_address_show - sas address
2593  * @cdev - pointer to embedded class device
2594  * @buf - the buffer returned
2595  *
2596  * This is the controller sas address
2597  *
2598  * A sysfs 'read-only' shost attribute.
2599  */
2600 static ssize_t
2601 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2602     char *buf)
2603 {
2604         struct Scsi_Host *shost = class_to_shost(cdev);
2605         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2606
2607         return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2608             (unsigned long long)ioc->sas_hba.sas_address);
2609 }
2610 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2611     _ctl_host_sas_address_show, NULL);
2612
2613 /**
2614  * _ctl_logging_level_show - logging level
2615  * @cdev - pointer to embedded class device
2616  * @buf - the buffer returned
2617  *
2618  * A sysfs 'read/write' shost attribute.
2619  */
2620 static ssize_t
2621 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2622     char *buf)
2623 {
2624         struct Scsi_Host *shost = class_to_shost(cdev);
2625         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2626
2627         return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2628 }
2629 static ssize_t
2630 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2631     const char *buf, size_t count)
2632 {
2633         struct Scsi_Host *shost = class_to_shost(cdev);
2634         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2635         int val = 0;
2636
2637         if (sscanf(buf, "%x", &val) != 1)
2638                 return -EINVAL;
2639
2640         ioc->logging_level = val;
2641         printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2642             ioc->logging_level);
2643         return strlen(buf);
2644 }
2645 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2646     _ctl_logging_level_show, _ctl_logging_level_store);
2647
2648 /* device attributes */
2649 /*
2650  * _ctl_fwfault_debug_show - show/store fwfault_debug
2651  * @cdev - pointer to embedded class device
2652  * @buf - the buffer returned
2653  *
2654  * mpt2sas_fwfault_debug is command line option
2655  * A sysfs 'read/write' shost attribute.
2656  */
2657 static ssize_t
2658 _ctl_fwfault_debug_show(struct device *cdev,
2659     struct device_attribute *attr, char *buf)
2660 {
2661         struct Scsi_Host *shost = class_to_shost(cdev);
2662         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2663
2664         return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2665 }
2666 static ssize_t
2667 _ctl_fwfault_debug_store(struct device *cdev,
2668     struct device_attribute *attr, const char *buf, size_t count)
2669 {
2670         struct Scsi_Host *shost = class_to_shost(cdev);
2671         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2672         int val = 0;
2673
2674         if (sscanf(buf, "%d", &val) != 1)
2675                 return -EINVAL;
2676
2677         ioc->fwfault_debug = val;
2678         printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
2679             ioc->fwfault_debug);
2680         return strlen(buf);
2681 }
2682 static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2683     _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2684
2685
2686 /**
2687  * _ctl_ioc_reset_count_show - ioc reset count
2688  * @cdev - pointer to embedded class device
2689  * @buf - the buffer returned
2690  *
2691  * This is firmware queue depth limit
2692  *
2693  * A sysfs 'read-only' shost attribute.
2694  */
2695 static ssize_t
2696 _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2697     char *buf)
2698 {
2699         struct Scsi_Host *shost = class_to_shost(cdev);
2700         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2701
2702         return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
2703 }
2704 static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
2705     _ctl_ioc_reset_count_show, NULL);
2706
2707 /**
2708  * _ctl_ioc_reply_queue_count_show - number of reply queues
2709  * @cdev - pointer to embedded class device
2710  * @buf - the buffer returned
2711  *
2712  * This is number of reply queues
2713  *
2714  * A sysfs 'read-only' shost attribute.
2715  */
2716 static ssize_t
2717 _ctl_ioc_reply_queue_count_show(struct device *cdev,
2718          struct device_attribute *attr, char *buf)
2719 {
2720         u8 reply_queue_count;
2721         struct Scsi_Host *shost = class_to_shost(cdev);
2722         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2723
2724         if ((ioc->facts.IOCCapabilities &
2725             MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2726                 reply_queue_count = ioc->reply_queue_count;
2727         else
2728                 reply_queue_count = 1;
2729         return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2730 }
2731 static DEVICE_ATTR(reply_queue_count, S_IRUGO,
2732          _ctl_ioc_reply_queue_count_show, NULL);
2733
2734 struct DIAG_BUFFER_START {
2735         __le32 Size;
2736         __le32 DiagVersion;
2737         u8 BufferType;
2738         u8 Reserved[3];
2739         __le32 Reserved1;
2740         __le32 Reserved2;
2741         __le32 Reserved3;
2742 };
2743 /**
2744  * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2745  * @cdev - pointer to embedded class device
2746  * @buf - the buffer returned
2747  *
2748  * A sysfs 'read-only' shost attribute.
2749  */
2750 static ssize_t
2751 _ctl_host_trace_buffer_size_show(struct device *cdev,
2752     struct device_attribute *attr, char *buf)
2753 {
2754         struct Scsi_Host *shost = class_to_shost(cdev);
2755         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2756         u32 size = 0;
2757         struct DIAG_BUFFER_START *request_data;
2758
2759         if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2760                 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2761                     "registered\n", ioc->name, __func__);
2762                 return 0;
2763         }
2764
2765         if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2766             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2767                 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2768                     "registered\n", ioc->name, __func__);
2769                 return 0;
2770         }
2771
2772         request_data = (struct DIAG_BUFFER_START *)
2773             ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2774         if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2775             le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
2776             le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2777                 size = le32_to_cpu(request_data->Size);
2778
2779         ioc->ring_buffer_sz = size;
2780         return snprintf(buf, PAGE_SIZE, "%d\n", size);
2781 }
2782 static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2783          _ctl_host_trace_buffer_size_show, NULL);
2784
2785 /**
2786  * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2787  * @cdev - pointer to embedded class device
2788  * @buf - the buffer returned
2789  *
2790  * A sysfs 'read/write' shost attribute.
2791  *
2792  * You will only be able to read 4k bytes of ring buffer at a time.
2793  * In order to read beyond 4k bytes, you will have to write out the
2794  * offset to the same attribute, it will move the pointer.
2795  */
2796 static ssize_t
2797 _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2798      char *buf)
2799 {
2800         struct Scsi_Host *shost = class_to_shost(cdev);
2801         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2802         void *request_data;
2803         u32 size;
2804
2805         if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2806                 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2807                     "registered\n", ioc->name, __func__);
2808                 return 0;
2809         }
2810
2811         if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2812             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2813                 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2814                     "registered\n", ioc->name, __func__);
2815                 return 0;
2816         }
2817
2818         if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2819                 return 0;
2820
2821         size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2822         size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
2823         request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2824         memcpy(buf, request_data, size);
2825         return size;
2826 }
2827
2828 static ssize_t
2829 _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2830     const char *buf, size_t count)
2831 {
2832         struct Scsi_Host *shost = class_to_shost(cdev);
2833         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2834         int val = 0;
2835
2836         if (sscanf(buf, "%d", &val) != 1)
2837                 return -EINVAL;
2838
2839         ioc->ring_buffer_offset = val;
2840         return strlen(buf);
2841 }
2842 static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2843     _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2844
2845 /*****************************************/
2846
2847 /**
2848  * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2849  * @cdev - pointer to embedded class device
2850  * @buf - the buffer returned
2851  *
2852  * A sysfs 'read/write' shost attribute.
2853  *
2854  * This is a mechnism to post/release host_trace_buffers
2855  */
2856 static ssize_t
2857 _ctl_host_trace_buffer_enable_show(struct device *cdev,
2858     struct device_attribute *attr, char *buf)
2859 {
2860         struct Scsi_Host *shost = class_to_shost(cdev);
2861         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2862
2863         if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
2864            ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2865             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
2866                 return snprintf(buf, PAGE_SIZE, "off\n");
2867         else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2868             MPT2_DIAG_BUFFER_IS_RELEASED))
2869                 return snprintf(buf, PAGE_SIZE, "release\n");
2870         else
2871                 return snprintf(buf, PAGE_SIZE, "post\n");
2872 }
2873
2874 static ssize_t
2875 _ctl_host_trace_buffer_enable_store(struct device *cdev,
2876     struct device_attribute *attr, const char *buf, size_t count)
2877 {
2878         struct Scsi_Host *shost = class_to_shost(cdev);
2879         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2880         char str[10] = "";
2881         struct mpt2_diag_register diag_register;
2882         u8 issue_reset = 0;
2883
2884         if (sscanf(buf, "%s", str) != 1)
2885                 return -EINVAL;
2886
2887         if (!strcmp(str, "post")) {
2888                 /* exit out if host buffers are already posted */
2889                 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
2890                     (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2891                     MPT2_DIAG_BUFFER_IS_REGISTERED) &&
2892                     ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2893                     MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
2894                         goto out;
2895                 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
2896                 printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
2897                     ioc->name);
2898                 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
2899                 diag_register.requested_buffer_size = (1024 * 1024);
2900                 diag_register.unique_id = 0x7075900;
2901                 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
2902                 _ctl_diag_register_2(ioc,  &diag_register);
2903         } else if (!strcmp(str, "release")) {
2904                 /* exit out if host buffers are already released */
2905                 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
2906                         goto out;
2907                 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2908                     MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
2909                         goto out;
2910                 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2911                     MPT2_DIAG_BUFFER_IS_RELEASED))
2912                         goto out;
2913                 printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
2914                     ioc->name);
2915                 _ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
2916         }
2917
2918  out:
2919         return strlen(buf);
2920 }
2921 static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
2922     _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
2923
2924 struct device_attribute *mpt2sas_host_attrs[] = {
2925         &dev_attr_version_fw,
2926         &dev_attr_version_bios,
2927         &dev_attr_version_mpi,
2928         &dev_attr_version_product,
2929         &dev_attr_version_nvdata_persistent,
2930         &dev_attr_version_nvdata_default,
2931         &dev_attr_board_name,
2932         &dev_attr_board_assembly,
2933         &dev_attr_board_tracer,
2934         &dev_attr_io_delay,
2935         &dev_attr_device_delay,
2936         &dev_attr_logging_level,
2937         &dev_attr_fwfault_debug,
2938         &dev_attr_fw_queue_depth,
2939         &dev_attr_host_sas_address,
2940         &dev_attr_ioc_reset_count,
2941         &dev_attr_host_trace_buffer_size,
2942         &dev_attr_host_trace_buffer,
2943         &dev_attr_host_trace_buffer_enable,
2944         &dev_attr_reply_queue_count,
2945         NULL,
2946 };
2947
2948 /**
2949  * _ctl_device_sas_address_show - sas address
2950  * @cdev - pointer to embedded class device
2951  * @buf - the buffer returned
2952  *
2953  * This is the sas address for the target
2954  *
2955  * A sysfs 'read-only' shost attribute.
2956  */
2957 static ssize_t
2958 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2959     char *buf)
2960 {
2961         struct scsi_device *sdev = to_scsi_device(dev);
2962         struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2963
2964         return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2965             (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2966 }
2967 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2968
2969 /**
2970  * _ctl_device_handle_show - device handle
2971  * @cdev - pointer to embedded class device
2972  * @buf - the buffer returned
2973  *
2974  * This is the firmware assigned device handle
2975  *
2976  * A sysfs 'read-only' shost attribute.
2977  */
2978 static ssize_t
2979 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
2980     char *buf)
2981 {
2982         struct scsi_device *sdev = to_scsi_device(dev);
2983         struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2984
2985         return snprintf(buf, PAGE_SIZE, "0x%04x\n",
2986             sas_device_priv_data->sas_target->handle);
2987 }
2988 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
2989
2990 struct device_attribute *mpt2sas_dev_attrs[] = {
2991         &dev_attr_sas_address,
2992         &dev_attr_sas_device_handle,
2993         NULL,
2994 };
2995
2996 static const struct file_operations ctl_fops = {
2997         .owner = THIS_MODULE,
2998         .unlocked_ioctl = _ctl_ioctl,
2999         .release = _ctl_release,
3000         .poll = _ctl_poll,
3001         .fasync = _ctl_fasync,
3002 #ifdef CONFIG_COMPAT
3003         .compat_ioctl = _ctl_ioctl_compat,
3004 #endif
3005         .llseek = noop_llseek,
3006 };
3007
3008 static struct miscdevice ctl_dev = {
3009         .minor  = MPT2SAS_MINOR,
3010         .name   = MPT2SAS_DEV_NAME,
3011         .fops   = &ctl_fops,
3012 };
3013
3014 /**
3015  * mpt2sas_ctl_init - main entry point for ctl.
3016  *
3017  */
3018 void
3019 mpt2sas_ctl_init(void)
3020 {
3021         async_queue = NULL;
3022         if (misc_register(&ctl_dev) < 0)
3023                 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
3024                     MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3025
3026         init_waitqueue_head(&ctl_poll_wait);
3027 }
3028
3029 /**
3030  * mpt2sas_ctl_exit - exit point for ctl
3031  *
3032  */
3033 void
3034 mpt2sas_ctl_exit(void)
3035 {
3036         struct MPT2SAS_ADAPTER *ioc;
3037         int i;
3038
3039         list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
3040
3041                 /* free memory associated to diag buffers */
3042                 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3043                         if (!ioc->diag_buffer[i])
3044                                 continue;
3045                         pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3046                             ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3047                         ioc->diag_buffer[i] = NULL;
3048                         ioc->diag_buffer_status[i] = 0;
3049                 }
3050
3051                 kfree(ioc->event_log);
3052         }
3053         misc_deregister(&ctl_dev);
3054 }
3055