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