Merge branch 'for-4.3-rc/ti-clk-fixes' of https://github.com/t-kristo/linux-pm into...
[cascardo/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-2014  LSI Corporation
7  * Copyright (C) 20013-2014 Avago Technologies
8  *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * NO WARRANTY
21  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25  * solely responsible for determining the appropriateness of using and
26  * distributing the Program and assumes all risks associated with its
27  * exercise of rights under this Agreement, including but not limited to
28  * the risks and costs of program errors, damage to or loss of data,
29  * programs or equipment, and unavailability or interruption of operations.
30
31  * DISCLAIMER OF LIABILITY
32  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39
40  * You should have received a copy of the GNU General Public License
41  * along with this program; if not, write to the Free Software
42  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
43  * USA.
44  */
45
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 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
85 /**
86  * _ctl_sas_device_find_by_handle - sas device search
87  * @ioc: per adapter object
88  * @handle: sas device handle (assigned by firmware)
89  * Context: Calling function should acquire ioc->sas_device_lock
90  *
91  * This searches for sas_device based on sas_address, then return sas_device
92  * object.
93  */
94 static struct _sas_device *
95 _ctl_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
96 {
97         struct _sas_device *sas_device, *r;
98
99         r = NULL;
100         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
101                 if (sas_device->handle != handle)
102                         continue;
103                 r = sas_device;
104                 goto out;
105         }
106
107  out:
108         return r;
109 }
110
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 helpful 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  * Returns void.
402  */
403 void
404 mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
405         u32 reply)
406 {
407         Mpi2EventNotificationReply_t *mpi_reply;
408
409         mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
410         if (unlikely(!mpi_reply)) {
411                 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
412                     ioc->name, __FILE__, __LINE__, __func__);
413                 return;
414         }
415         mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
416         return;
417 }
418
419 /**
420  * _ctl_verify_adapter - validates ioc_number passed from application
421  * @ioc: per adapter object
422  * @iocpp: The ioc pointer is returned in this.
423  *
424  * Return (-1) means error, else ioc_number.
425  */
426 static int
427 _ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
428 {
429         struct MPT2SAS_ADAPTER *ioc;
430         /* global ioc lock to protect controller on list operations */
431         spin_lock(&gioc_lock);
432         list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
433                 if (ioc->id != ioc_number)
434                         continue;
435                 spin_unlock(&gioc_lock);
436                 *iocpp = ioc;
437                 return ioc_number;
438         }
439         spin_unlock(&gioc_lock);
440         *iocpp = NULL;
441         return -1;
442 }
443
444 /**
445  * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
446  * @ioc: per adapter object
447  * @reset_phase: phase
448  *
449  * The handler for doing any required cleanup or initialization.
450  *
451  * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
452  * MPT2_IOC_DONE_RESET
453  */
454 void
455 mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
456 {
457         int i;
458         u8 issue_reset;
459
460         switch (reset_phase) {
461         case MPT2_IOC_PRE_RESET:
462                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
463                     "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
464                 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
465                         if (!(ioc->diag_buffer_status[i] &
466                             MPT2_DIAG_BUFFER_IS_REGISTERED))
467                                 continue;
468                         if ((ioc->diag_buffer_status[i] &
469                             MPT2_DIAG_BUFFER_IS_RELEASED))
470                                 continue;
471                         _ctl_send_release(ioc, i, &issue_reset);
472                 }
473                 break;
474         case MPT2_IOC_AFTER_RESET:
475                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
476                     "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
477                 if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
478                         ioc->ctl_cmds.status |= MPT2_CMD_RESET;
479                         mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
480                         complete(&ioc->ctl_cmds.done);
481                 }
482                 break;
483         case MPT2_IOC_DONE_RESET:
484                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
485                     "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
486
487                 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
488                         if (!(ioc->diag_buffer_status[i] &
489                             MPT2_DIAG_BUFFER_IS_REGISTERED))
490                                 continue;
491                         if ((ioc->diag_buffer_status[i] &
492                             MPT2_DIAG_BUFFER_IS_RELEASED))
493                                 continue;
494                         ioc->diag_buffer_status[i] |=
495                             MPT2_DIAG_BUFFER_IS_DIAG_RESET;
496                 }
497                 break;
498         }
499 }
500
501 /**
502  * _ctl_fasync -
503  * @fd -
504  * @filep -
505  * @mode -
506  *
507  * Called when application request fasyn callback handler.
508  */
509 static int
510 _ctl_fasync(int fd, struct file *filep, int mode)
511 {
512         return fasync_helper(fd, filep, mode, &async_queue);
513 }
514
515 /**
516  * _ctl_poll -
517  * @file -
518  * @wait -
519  *
520  */
521 static unsigned int
522 _ctl_poll(struct file *filep, poll_table *wait)
523 {
524         struct MPT2SAS_ADAPTER *ioc;
525
526         poll_wait(filep, &ctl_poll_wait, wait);
527
528         /* global ioc lock to protect controller on list operations */
529         spin_lock(&gioc_lock);
530         list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
531                 if (ioc->aen_event_read_flag) {
532                         spin_unlock(&gioc_lock);
533                         return POLLIN | POLLRDNORM;
534                 }
535         }
536         spin_unlock(&gioc_lock);
537         return 0;
538 }
539
540 /**
541  * _ctl_set_task_mid - assign an active smid to tm request
542  * @ioc: per adapter object
543  * @karg - (struct mpt2_ioctl_command)
544  * @tm_request - pointer to mf from user space
545  *
546  * Returns 0 when an smid if found, else fail.
547  * during failure, the reply frame is filled.
548  */
549 static int
550 _ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
551     Mpi2SCSITaskManagementRequest_t *tm_request)
552 {
553         u8 found = 0;
554         u16 i;
555         u16 handle;
556         struct scsi_cmnd *scmd;
557         struct MPT2SAS_DEVICE *priv_data;
558         unsigned long flags;
559         Mpi2SCSITaskManagementReply_t *tm_reply;
560         u32 sz;
561         u32 lun;
562         char *desc = NULL;
563
564         if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
565                 desc = "abort_task";
566         else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
567                 desc = "query_task";
568         else
569                 return 0;
570
571         lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
572
573         handle = le16_to_cpu(tm_request->DevHandle);
574         spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
575         for (i = ioc->scsiio_depth; i && !found; i--) {
576                 scmd = ioc->scsi_lookup[i - 1].scmd;
577                 if (scmd == NULL || scmd->device == NULL ||
578                     scmd->device->hostdata == NULL)
579                         continue;
580                 if (lun != scmd->device->lun)
581                         continue;
582                 priv_data = scmd->device->hostdata;
583                 if (priv_data->sas_target == NULL)
584                         continue;
585                 if (priv_data->sas_target->handle != handle)
586                         continue;
587                 tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
588                 found = 1;
589         }
590         spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
591
592         if (!found) {
593                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
594                     "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
595                     desc, le16_to_cpu(tm_request->DevHandle), lun));
596                 tm_reply = ioc->ctl_cmds.reply;
597                 tm_reply->DevHandle = tm_request->DevHandle;
598                 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
599                 tm_reply->TaskType = tm_request->TaskType;
600                 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
601                 tm_reply->VP_ID = tm_request->VP_ID;
602                 tm_reply->VF_ID = tm_request->VF_ID;
603                 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
604                 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
605                     sz))
606                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
607                             __LINE__, __func__);
608                 return 1;
609         }
610
611         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
612             "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
613             desc, le16_to_cpu(tm_request->DevHandle), lun,
614              le16_to_cpu(tm_request->TaskMID)));
615         return 0;
616 }
617
618 /**
619  * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
620  * @ioc: per adapter object
621  * @karg - (struct mpt2_ioctl_command)
622  * @mf - pointer to mf in user space
623  */
624 static long
625 _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command karg,
626         void __user *mf)
627 {
628         MPI2RequestHeader_t *mpi_request = NULL, *request;
629         MPI2DefaultReply_t *mpi_reply;
630         u32 ioc_state;
631         u16 ioc_status;
632         u16 smid;
633         unsigned long timeout, timeleft;
634         u8 issue_reset;
635         u32 sz;
636         void *psge;
637         void *data_out = NULL;
638         dma_addr_t data_out_dma;
639         size_t data_out_sz = 0;
640         void *data_in = NULL;
641         dma_addr_t data_in_dma;
642         size_t data_in_sz = 0;
643         u32 sgl_flags;
644         long ret;
645         u16 wait_state_count;
646
647         issue_reset = 0;
648
649         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
650                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
651                     ioc->name, __func__);
652                 ret = -EAGAIN;
653                 goto out;
654         }
655
656         wait_state_count = 0;
657         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
658         while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
659                 if (wait_state_count++ == 10) {
660                         printk(MPT2SAS_ERR_FMT
661                             "%s: failed due to ioc not operational\n",
662                             ioc->name, __func__);
663                         ret = -EFAULT;
664                         goto out;
665                 }
666                 ssleep(1);
667                 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
668                 printk(MPT2SAS_INFO_FMT "%s: waiting for "
669                     "operational state(count=%d)\n", ioc->name,
670                     __func__, wait_state_count);
671         }
672         if (wait_state_count)
673                 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
674                     ioc->name, __func__);
675
676         mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
677         if (!mpi_request) {
678                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a memory for "
679                     "mpi_request\n", ioc->name, __func__);
680                 ret = -ENOMEM;
681                 goto out;
682         }
683
684         /* Check for overflow and wraparound */
685         if (karg.data_sge_offset * 4 > ioc->request_sz ||
686             karg.data_sge_offset > (UINT_MAX / 4)) {
687                 ret = -EINVAL;
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         init_completion(&ioc->ctl_cmds.done);
816         switch (mpi_request->Function) {
817         case MPI2_FUNCTION_SCSI_IO_REQUEST:
818         case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
819         {
820                 Mpi2SCSIIORequest_t *scsiio_request =
821                     (Mpi2SCSIIORequest_t *)request;
822                 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
823                 scsiio_request->SenseBufferLowAddress =
824                     mpt2sas_base_get_sense_buffer_dma(ioc, smid);
825                 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
826                 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
827                         mpt2sas_base_put_smid_scsi_io(ioc, smid,
828                             le16_to_cpu(mpi_request->FunctionDependent1));
829                 else
830                         mpt2sas_base_put_smid_default(ioc, smid);
831                 break;
832         }
833         case MPI2_FUNCTION_SCSI_TASK_MGMT:
834         {
835                 Mpi2SCSITaskManagementRequest_t *tm_request =
836                     (Mpi2SCSITaskManagementRequest_t *)request;
837
838                 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
839                     "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
840                     le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
841
842                 if (tm_request->TaskType ==
843                     MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
844                     tm_request->TaskType ==
845                     MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
846                         if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
847                                 mpt2sas_base_free_smid(ioc, smid);
848                                 goto out;
849                         }
850                 }
851
852                 mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
853                     tm_request->DevHandle));
854                 mpt2sas_base_put_smid_hi_priority(ioc, smid);
855                 break;
856         }
857         case MPI2_FUNCTION_SMP_PASSTHROUGH:
858         {
859                 Mpi2SmpPassthroughRequest_t *smp_request =
860                     (Mpi2SmpPassthroughRequest_t *)mpi_request;
861                 u8 *data;
862
863                 /* ioc determines which port to use */
864                 smp_request->PhysicalPort = 0xFF;
865                 if (smp_request->PassthroughFlags &
866                     MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
867                         data = (u8 *)&smp_request->SGL;
868                 else {
869                         if (unlikely(data_out == NULL)) {
870                                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
871                                     __FILE__, __LINE__, __func__);
872                                 mpt2sas_base_free_smid(ioc, smid);
873                                 ret = -EINVAL;
874                                 goto out;
875                         }
876                         data = data_out;
877                 }
878
879                 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
880                         ioc->ioc_link_reset_in_progress = 1;
881                         ioc->ignore_loginfos = 1;
882                 }
883                 mpt2sas_base_put_smid_default(ioc, smid);
884                 break;
885         }
886         case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
887         {
888                 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
889                     (Mpi2SasIoUnitControlRequest_t *)mpi_request;
890
891                 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
892                     || sasiounit_request->Operation ==
893                     MPI2_SAS_OP_PHY_LINK_RESET) {
894                         ioc->ioc_link_reset_in_progress = 1;
895                         ioc->ignore_loginfos = 1;
896                 }
897                 mpt2sas_base_put_smid_default(ioc, smid);
898                 break;
899         }
900         default:
901                 mpt2sas_base_put_smid_default(ioc, smid);
902                 break;
903         }
904
905         if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
906                 timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
907         else
908                 timeout = karg.timeout;
909         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
910             timeout*HZ);
911         if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
912                 Mpi2SCSITaskManagementRequest_t *tm_request =
913                     (Mpi2SCSITaskManagementRequest_t *)mpi_request;
914                 mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
915                     tm_request->DevHandle));
916         } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
917             mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
918                 ioc->ioc_link_reset_in_progress) {
919                 ioc->ioc_link_reset_in_progress = 0;
920                 ioc->ignore_loginfos = 0;
921         }
922         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
923                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
924                     __func__);
925                 _debug_dump_mf(mpi_request, karg.data_sge_offset);
926                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
927                         issue_reset = 1;
928                 goto issue_host_reset;
929         }
930
931         mpi_reply = ioc->ctl_cmds.reply;
932         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
933
934 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
935         if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
936             (ioc->logging_level & MPT_DEBUG_TM)) {
937                 Mpi2SCSITaskManagementReply_t *tm_reply =
938                     (Mpi2SCSITaskManagementReply_t *)mpi_reply;
939
940                 printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
941                     "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
942                     "TerminationCount(0x%08x)\n", ioc->name,
943                     le16_to_cpu(tm_reply->IOCStatus),
944                     le32_to_cpu(tm_reply->IOCLogInfo),
945                     le32_to_cpu(tm_reply->TerminationCount));
946         }
947 #endif
948         /* copy out xdata to user */
949         if (data_in_sz) {
950                 if (copy_to_user(karg.data_in_buf_ptr, data_in,
951                     data_in_sz)) {
952                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
953                             __LINE__, __func__);
954                         ret = -ENODATA;
955                         goto out;
956                 }
957         }
958
959         /* copy out reply message frame to user */
960         if (karg.max_reply_bytes) {
961                 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
962                 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
963                     sz)) {
964                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
965                             __LINE__, __func__);
966                         ret = -ENODATA;
967                         goto out;
968                 }
969         }
970
971         /* copy out sense to user */
972         if (karg.max_sense_bytes && (mpi_request->Function ==
973             MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
974             MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
975                 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
976                 if (copy_to_user(karg.sense_data_ptr,
977                         ioc->ctl_cmds.sense, sz)) {
978                         printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
979                             __LINE__, __func__);
980                         ret = -ENODATA;
981                         goto out;
982                 }
983         }
984
985  issue_host_reset:
986         if (issue_reset) {
987                 ret = -ENODATA;
988                 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
989                     mpi_request->Function ==
990                     MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
991                     mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
992                         printk(MPT2SAS_INFO_FMT "issue target reset: handle "
993                             "= (0x%04x)\n", ioc->name,
994                             le16_to_cpu(mpi_request->FunctionDependent1));
995                         mpt2sas_halt_firmware(ioc);
996                         mpt2sas_scsih_issue_tm(ioc,
997                             le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
998                             0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
999                             TM_MUTEX_ON);
1000                         ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
1001                 } else
1002                         mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1003                             FORCE_BIG_HAMMER);
1004         }
1005
1006  out:
1007
1008         /* free memory associated with sg buffers */
1009         if (data_in)
1010                 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1011                     data_in_dma);
1012
1013         if (data_out)
1014                 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1015                     data_out_dma);
1016
1017         kfree(mpi_request);
1018         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1019         return ret;
1020 }
1021
1022 /**
1023  * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
1024  * @ioc: per adapter object
1025  * @arg - user space buffer containing ioctl content
1026  */
1027 static long
1028 _ctl_getiocinfo(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1029 {
1030         struct mpt2_ioctl_iocinfo karg;
1031
1032         if (copy_from_user(&karg, arg, sizeof(karg))) {
1033                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1034                     __FILE__, __LINE__, __func__);
1035                 return -EFAULT;
1036         }
1037
1038         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1039             __func__));
1040
1041         memset(&karg, 0 , sizeof(karg));
1042         if (ioc->is_warpdrive)
1043                 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1044         else
1045                 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1046         if (ioc->pfacts)
1047                 karg.port_number = ioc->pfacts[0].PortNumber;
1048         karg.hw_rev = ioc->pdev->revision;
1049         karg.pci_id = ioc->pdev->device;
1050         karg.subsystem_device = ioc->pdev->subsystem_device;
1051         karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1052         karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1053         karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1054         karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1055         karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1056         karg.firmware_version = ioc->facts.FWVersion.Word;
1057         strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
1058         strcat(karg.driver_version, "-");
1059         strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1060         karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1061
1062         if (copy_to_user(arg, &karg, sizeof(karg))) {
1063                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1064                     __FILE__, __LINE__, __func__);
1065                 return -EFAULT;
1066         }
1067         return 0;
1068 }
1069
1070 /**
1071  * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
1072  * @ioc: per adapter object
1073  * @arg - user space buffer containing ioctl content
1074  */
1075 static long
1076 _ctl_eventquery(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1077 {
1078         struct mpt2_ioctl_eventquery karg;
1079
1080         if (copy_from_user(&karg, arg, sizeof(karg))) {
1081                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1082                     __FILE__, __LINE__, __func__);
1083                 return -EFAULT;
1084         }
1085
1086         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1087             __func__));
1088
1089         karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
1090         memcpy(karg.event_types, ioc->event_type,
1091             MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1092
1093         if (copy_to_user(arg, &karg, sizeof(karg))) {
1094                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1095                     __FILE__, __LINE__, __func__);
1096                 return -EFAULT;
1097         }
1098         return 0;
1099 }
1100
1101 /**
1102  * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1103  * @ioc: per adapter object
1104  * @arg - user space buffer containing ioctl content
1105  */
1106 static long
1107 _ctl_eventenable(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1108 {
1109         struct mpt2_ioctl_eventenable karg;
1110
1111         if (copy_from_user(&karg, arg, sizeof(karg))) {
1112                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1113                     __FILE__, __LINE__, __func__);
1114                 return -EFAULT;
1115         }
1116
1117         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1118             __func__));
1119
1120         if (ioc->event_log)
1121                 return 0;
1122         memcpy(ioc->event_type, karg.event_types,
1123             MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1124         mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1125
1126         /* initialize event_log */
1127         ioc->event_context = 0;
1128         ioc->aen_event_read_flag = 0;
1129         ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1130             sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1131         if (!ioc->event_log) {
1132                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1133                     __FILE__, __LINE__, __func__);
1134                 return -ENOMEM;
1135         }
1136         return 0;
1137 }
1138
1139 /**
1140  * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1141  * @ioc: per adapter object
1142  * @arg - user space buffer containing ioctl content
1143  */
1144 static long
1145 _ctl_eventreport(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1146 {
1147         struct mpt2_ioctl_eventreport karg;
1148         u32 number_bytes, max_events, max;
1149         struct mpt2_ioctl_eventreport __user *uarg = arg;
1150
1151         if (copy_from_user(&karg, arg, sizeof(karg))) {
1152                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1153                     __FILE__, __LINE__, __func__);
1154                 return -EFAULT;
1155         }
1156
1157         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1158             __func__));
1159
1160         number_bytes = karg.hdr.max_data_size -
1161             sizeof(struct mpt2_ioctl_header);
1162         max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1163         max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1164
1165         /* If fewer than 1 event is requested, there must have
1166          * been some type of error.
1167          */
1168         if (!max || !ioc->event_log)
1169                 return -ENODATA;
1170
1171         number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1172         if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1173                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1174                     __FILE__, __LINE__, __func__);
1175                 return -EFAULT;
1176         }
1177
1178         /* reset flag so SIGIO can restart */
1179         ioc->aen_event_read_flag = 0;
1180         return 0;
1181 }
1182
1183 /**
1184  * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1185  * @ioc: per adapter object
1186  * @arg - user space buffer containing ioctl content
1187  */
1188 static long
1189 _ctl_do_reset(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1190 {
1191         struct mpt2_ioctl_diag_reset karg;
1192         int retval;
1193
1194         if (copy_from_user(&karg, arg, sizeof(karg))) {
1195                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1196                     __FILE__, __LINE__, __func__);
1197                 return -EFAULT;
1198         }
1199
1200         if (ioc->shost_recovery || ioc->pci_error_recovery ||
1201                 ioc->is_driver_loading)
1202                 return -EAGAIN;
1203         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1204             __func__));
1205
1206         retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1207             FORCE_BIG_HAMMER);
1208         printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1209             ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1210         return 0;
1211 }
1212
1213 /**
1214  * _ctl_btdh_search_sas_device - searching for sas device
1215  * @ioc: per adapter object
1216  * @btdh: btdh ioctl payload
1217  */
1218 static int
1219 _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1220     struct mpt2_ioctl_btdh_mapping *btdh)
1221 {
1222         struct _sas_device *sas_device;
1223         unsigned long flags;
1224         int rc = 0;
1225
1226         if (list_empty(&ioc->sas_device_list))
1227                 return rc;
1228
1229         spin_lock_irqsave(&ioc->sas_device_lock, flags);
1230         list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1231                 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1232                     btdh->handle == sas_device->handle) {
1233                         btdh->bus = sas_device->channel;
1234                         btdh->id = sas_device->id;
1235                         rc = 1;
1236                         goto out;
1237                 } else if (btdh->bus == sas_device->channel && btdh->id ==
1238                     sas_device->id && btdh->handle == 0xFFFF) {
1239                         btdh->handle = sas_device->handle;
1240                         rc = 1;
1241                         goto out;
1242                 }
1243         }
1244  out:
1245         spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1246         return rc;
1247 }
1248
1249 /**
1250  * _ctl_btdh_search_raid_device - searching for raid device
1251  * @ioc: per adapter object
1252  * @btdh: btdh ioctl payload
1253  */
1254 static int
1255 _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1256     struct mpt2_ioctl_btdh_mapping *btdh)
1257 {
1258         struct _raid_device *raid_device;
1259         unsigned long flags;
1260         int rc = 0;
1261
1262         if (list_empty(&ioc->raid_device_list))
1263                 return rc;
1264
1265         spin_lock_irqsave(&ioc->raid_device_lock, flags);
1266         list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1267                 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1268                     btdh->handle == raid_device->handle) {
1269                         btdh->bus = raid_device->channel;
1270                         btdh->id = raid_device->id;
1271                         rc = 1;
1272                         goto out;
1273                 } else if (btdh->bus == raid_device->channel && btdh->id ==
1274                     raid_device->id && btdh->handle == 0xFFFF) {
1275                         btdh->handle = raid_device->handle;
1276                         rc = 1;
1277                         goto out;
1278                 }
1279         }
1280  out:
1281         spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1282         return rc;
1283 }
1284
1285 /**
1286  * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1287  * @ioc: per adapter object
1288  * @arg - user space buffer containing ioctl content
1289  */
1290 static long
1291 _ctl_btdh_mapping(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1292 {
1293         struct mpt2_ioctl_btdh_mapping karg;
1294         int rc;
1295
1296         if (copy_from_user(&karg, arg, sizeof(karg))) {
1297                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1298                     __FILE__, __LINE__, __func__);
1299                 return -EFAULT;
1300         }
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         init_completion(&ioc->ctl_cmds.done);
1469         mpt2sas_base_put_smid_default(ioc, smid);
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  * @ioc: per adapter object
1570  * @arg - user space buffer containing ioctl content
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(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1577 {
1578         struct mpt2_diag_register karg;
1579         long rc;
1580
1581         if (copy_from_user(&karg, arg, sizeof(karg))) {
1582                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1583                     __FILE__, __LINE__, __func__);
1584                 return -EFAULT;
1585         }
1586
1587         rc = _ctl_diag_register_2(ioc, &karg);
1588         return rc;
1589 }
1590
1591 /**
1592  * _ctl_diag_unregister - application unregister with driver
1593  * @ioc: per adapter object
1594  * @arg - user space buffer containing ioctl content
1595  *
1596  * This will allow the driver to cleanup any memory allocated for diag
1597  * messages and to free up any resources.
1598  */
1599 static long
1600 _ctl_diag_unregister(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1601 {
1602         struct mpt2_diag_unregister karg;
1603         void *request_data;
1604         dma_addr_t request_data_dma;
1605         u32 request_data_sz;
1606         u8 buffer_type;
1607
1608         if (copy_from_user(&karg, arg, sizeof(karg))) {
1609                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1610                     __FILE__, __LINE__, __func__);
1611                 return -EFAULT;
1612         }
1613
1614         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1615             __func__));
1616
1617         buffer_type = karg.unique_id & 0x000000ff;
1618         if (!_ctl_diag_capability(ioc, buffer_type)) {
1619                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1620                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1621                 return -EPERM;
1622         }
1623
1624         if ((ioc->diag_buffer_status[buffer_type] &
1625             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1626                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1627                     "registered\n", ioc->name, __func__, buffer_type);
1628                 return -EINVAL;
1629         }
1630         if ((ioc->diag_buffer_status[buffer_type] &
1631             MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1632                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1633                     "released\n", ioc->name, __func__, buffer_type);
1634                 return -EINVAL;
1635         }
1636
1637         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1638                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1639                     "registered\n", ioc->name, __func__, karg.unique_id);
1640                 return -EINVAL;
1641         }
1642
1643         request_data = ioc->diag_buffer[buffer_type];
1644         if (!request_data) {
1645                 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1646                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1647                 return -ENOMEM;
1648         }
1649
1650         request_data_sz = ioc->diag_buffer_sz[buffer_type];
1651         request_data_dma = ioc->diag_buffer_dma[buffer_type];
1652         pci_free_consistent(ioc->pdev, request_data_sz,
1653             request_data, request_data_dma);
1654         ioc->diag_buffer[buffer_type] = NULL;
1655         ioc->diag_buffer_status[buffer_type] = 0;
1656         return 0;
1657 }
1658
1659 /**
1660  * _ctl_diag_query - query relevant info associated with diag buffers
1661  * @ioc: per adapter object
1662  * @arg - user space buffer containing ioctl content
1663  *
1664  * The application will send only buffer_type and unique_id.  Driver will
1665  * inspect unique_id first, if valid, fill in all the info.  If unique_id is
1666  * 0x00, the driver will return info specified by Buffer Type.
1667  */
1668 static long
1669 _ctl_diag_query(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1670 {
1671         struct mpt2_diag_query karg;
1672         void *request_data;
1673         int i;
1674         u8 buffer_type;
1675
1676         if (copy_from_user(&karg, arg, sizeof(karg))) {
1677                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1678                     __FILE__, __LINE__, __func__);
1679                 return -EFAULT;
1680         }
1681
1682         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1683             __func__));
1684
1685         karg.application_flags = 0;
1686         buffer_type = karg.buffer_type;
1687
1688         if (!_ctl_diag_capability(ioc, buffer_type)) {
1689                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1690                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1691                 return -EPERM;
1692         }
1693
1694         if ((ioc->diag_buffer_status[buffer_type] &
1695             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1696                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1697                     "registered\n", ioc->name, __func__, buffer_type);
1698                 return -EINVAL;
1699         }
1700
1701         if (karg.unique_id & 0xffffff00) {
1702                 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1703                         printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1704                             "registered\n", ioc->name, __func__,
1705                             karg.unique_id);
1706                         return -EINVAL;
1707                 }
1708         }
1709
1710         request_data = ioc->diag_buffer[buffer_type];
1711         if (!request_data) {
1712                 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1713                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1714                 return -ENOMEM;
1715         }
1716
1717         if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1718                 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1719                     MPT2_APP_FLAGS_BUFFER_VALID);
1720         else
1721                 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1722                     MPT2_APP_FLAGS_BUFFER_VALID |
1723                     MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1724
1725         for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1726                 karg.product_specific[i] =
1727                     ioc->product_specific[buffer_type][i];
1728
1729         karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1730         karg.driver_added_buffer_size = 0;
1731         karg.unique_id = ioc->unique_id[buffer_type];
1732         karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1733
1734         if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1735                 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1736                     "data @ %p\n", ioc->name, __func__, arg);
1737                 return -EFAULT;
1738         }
1739         return 0;
1740 }
1741
1742 /**
1743  * _ctl_send_release - Diag Release Message
1744  * @ioc: per adapter object
1745  * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1746  * @issue_reset - specifies whether host reset is required.
1747  *
1748  */
1749 static int
1750 _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1751 {
1752         Mpi2DiagReleaseRequest_t *mpi_request;
1753         Mpi2DiagReleaseReply_t *mpi_reply;
1754         u16 smid;
1755         u16 ioc_status;
1756         u32 ioc_state;
1757         int rc;
1758         unsigned long timeleft;
1759
1760         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1761             __func__));
1762
1763         rc = 0;
1764         *issue_reset = 0;
1765
1766         ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1767         if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1768                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
1769                     "skipping due to FAULT state\n", ioc->name,
1770                     __func__));
1771                 rc = -EAGAIN;
1772                 goto out;
1773         }
1774
1775         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1776                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1777                     ioc->name, __func__);
1778                 rc = -EAGAIN;
1779                 goto out;
1780         }
1781
1782         smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1783         if (!smid) {
1784                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1785                     ioc->name, __func__);
1786                 rc = -EAGAIN;
1787                 goto out;
1788         }
1789
1790         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1791         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1792         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1793         ioc->ctl_cmds.smid = smid;
1794
1795         mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1796         mpi_request->BufferType = buffer_type;
1797         mpi_request->VF_ID = 0; /* TODO */
1798         mpi_request->VP_ID = 0;
1799
1800         init_completion(&ioc->ctl_cmds.done);
1801         mpt2sas_base_put_smid_default(ioc, smid);
1802         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1803             MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1804
1805         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1806                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1807                     __func__);
1808                 _debug_dump_mf(mpi_request,
1809                     sizeof(Mpi2DiagReleaseRequest_t)/4);
1810                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1811                         *issue_reset = 1;
1812                 rc = -EFAULT;
1813                 goto out;
1814         }
1815
1816         /* process the completed Reply Message Frame */
1817         if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1818                 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1819                     ioc->name, __func__);
1820                 rc = -EFAULT;
1821                 goto out;
1822         }
1823
1824         mpi_reply = ioc->ctl_cmds.reply;
1825         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1826
1827         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1828                 ioc->diag_buffer_status[buffer_type] |=
1829                     MPT2_DIAG_BUFFER_IS_RELEASED;
1830                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1831                     ioc->name, __func__));
1832         } else {
1833                 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1834                     "log_info(0x%08x)\n", ioc->name, __func__,
1835                     ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1836                 rc = -EFAULT;
1837         }
1838
1839  out:
1840         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1841         return rc;
1842 }
1843
1844 /**
1845  * _ctl_diag_release - request to send Diag Release Message to firmware
1846  * @arg - user space buffer containing ioctl content
1847  *
1848  * This allows ownership of the specified buffer to returned to the driver,
1849  * allowing an application to read the buffer without fear that firmware is
1850  * overwritting information in the buffer.
1851  */
1852 static long
1853 _ctl_diag_release(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1854 {
1855         struct mpt2_diag_release karg;
1856         void *request_data;
1857         int rc;
1858         u8 buffer_type;
1859         u8 issue_reset = 0;
1860
1861         if (copy_from_user(&karg, arg, sizeof(karg))) {
1862                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1863                     __FILE__, __LINE__, __func__);
1864                 return -EFAULT;
1865         }
1866
1867         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1868             __func__));
1869
1870         buffer_type = karg.unique_id & 0x000000ff;
1871         if (!_ctl_diag_capability(ioc, buffer_type)) {
1872                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1873                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1874                 return -EPERM;
1875         }
1876
1877         if ((ioc->diag_buffer_status[buffer_type] &
1878             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1879                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1880                     "registered\n", ioc->name, __func__, buffer_type);
1881                 return -EINVAL;
1882         }
1883
1884         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1885                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1886                     "registered\n", ioc->name, __func__, karg.unique_id);
1887                 return -EINVAL;
1888         }
1889
1890         if (ioc->diag_buffer_status[buffer_type] &
1891             MPT2_DIAG_BUFFER_IS_RELEASED) {
1892                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1893                     "is already released\n", ioc->name, __func__,
1894                     buffer_type);
1895                 return 0;
1896         }
1897
1898         request_data = ioc->diag_buffer[buffer_type];
1899
1900         if (!request_data) {
1901                 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1902                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1903                 return -ENOMEM;
1904         }
1905
1906         /* buffers were released by due to host reset */
1907         if ((ioc->diag_buffer_status[buffer_type] &
1908             MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1909                 ioc->diag_buffer_status[buffer_type] |=
1910                     MPT2_DIAG_BUFFER_IS_RELEASED;
1911                 ioc->diag_buffer_status[buffer_type] &=
1912                     ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1913                 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1914                     "was released due to host reset\n", ioc->name, __func__,
1915                     buffer_type);
1916                 return 0;
1917         }
1918
1919         rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1920
1921         if (issue_reset)
1922                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1923                     FORCE_BIG_HAMMER);
1924
1925         return rc;
1926 }
1927
1928 /**
1929  * _ctl_diag_read_buffer - request for copy of the diag buffer
1930  * @ioc: per adapter object
1931  * @arg - user space buffer containing ioctl content
1932  */
1933 static long
1934 _ctl_diag_read_buffer(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1935 {
1936         struct mpt2_diag_read_buffer karg;
1937         struct mpt2_diag_read_buffer __user *uarg = arg;
1938         void *request_data, *diag_data;
1939         Mpi2DiagBufferPostRequest_t *mpi_request;
1940         Mpi2DiagBufferPostReply_t *mpi_reply;
1941         int rc, i;
1942         u8 buffer_type;
1943         unsigned long timeleft, request_size, copy_size;
1944         u16 smid;
1945         u16 ioc_status;
1946         u8 issue_reset = 0;
1947
1948         if (copy_from_user(&karg, arg, sizeof(karg))) {
1949                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1950                     __FILE__, __LINE__, __func__);
1951                 return -EFAULT;
1952         }
1953
1954         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1955             __func__));
1956
1957         buffer_type = karg.unique_id & 0x000000ff;
1958         if (!_ctl_diag_capability(ioc, buffer_type)) {
1959                 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1960                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1961                 return -EPERM;
1962         }
1963
1964         if (karg.unique_id != ioc->unique_id[buffer_type]) {
1965                 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1966                     "registered\n", ioc->name, __func__, karg.unique_id);
1967                 return -EINVAL;
1968         }
1969
1970         request_data = ioc->diag_buffer[buffer_type];
1971         if (!request_data) {
1972                 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1973                     "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1974                 return -ENOMEM;
1975         }
1976
1977         request_size = ioc->diag_buffer_sz[buffer_type];
1978
1979         if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1980                 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1981                     "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1982                     __func__);
1983                 return -EINVAL;
1984         }
1985
1986         if (karg.starting_offset > request_size)
1987                 return -EINVAL;
1988
1989         diag_data = (void *)(request_data + karg.starting_offset);
1990         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
1991             "offset(%d), sz(%d)\n", ioc->name, __func__,
1992             diag_data, karg.starting_offset, karg.bytes_to_read));
1993
1994         /* Truncate data on requests that are too large */
1995         if ((diag_data + karg.bytes_to_read < diag_data) ||
1996             (diag_data + karg.bytes_to_read > request_data + request_size))
1997                 copy_size = request_size - karg.starting_offset;
1998         else
1999                 copy_size = karg.bytes_to_read;
2000
2001         if (copy_to_user((void __user *)uarg->diagnostic_data,
2002             diag_data, copy_size)) {
2003                 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
2004                     "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
2005                     __func__, diag_data);
2006                 return -EFAULT;
2007         }
2008
2009         if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
2010                 return 0;
2011
2012         dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
2013                 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
2014         if ((ioc->diag_buffer_status[buffer_type] &
2015             MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
2016                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2017                     "buffer_type(0x%02x) is still registered\n", ioc->name,
2018                      __func__, buffer_type));
2019                 return 0;
2020         }
2021         /* Get a free request frame and save the message context.
2022         */
2023
2024         if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
2025                 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
2026                     ioc->name, __func__);
2027                 rc = -EAGAIN;
2028                 goto out;
2029         }
2030
2031         smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2032         if (!smid) {
2033                 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2034                     ioc->name, __func__);
2035                 rc = -EAGAIN;
2036                 goto out;
2037         }
2038
2039         rc = 0;
2040         ioc->ctl_cmds.status = MPT2_CMD_PENDING;
2041         memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2042         mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2043         ioc->ctl_cmds.smid = smid;
2044
2045         mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2046         mpi_request->BufferType = buffer_type;
2047         mpi_request->BufferLength =
2048             cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2049         mpi_request->BufferAddress =
2050             cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2051         for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
2052                 mpi_request->ProductSpecific[i] =
2053                         cpu_to_le32(ioc->product_specific[buffer_type][i]);
2054         mpi_request->VF_ID = 0; /* TODO */
2055         mpi_request->VP_ID = 0;
2056
2057         init_completion(&ioc->ctl_cmds.done);
2058         mpt2sas_base_put_smid_default(ioc, smid);
2059         timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2060             MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
2061
2062         if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
2063                 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
2064                     __func__);
2065                 _debug_dump_mf(mpi_request,
2066                     sizeof(Mpi2DiagBufferPostRequest_t)/4);
2067                 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
2068                         issue_reset = 1;
2069                 goto issue_host_reset;
2070         }
2071
2072         /* process the completed Reply Message Frame */
2073         if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
2074                 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
2075                     ioc->name, __func__);
2076                 rc = -EFAULT;
2077                 goto out;
2078         }
2079
2080         mpi_reply = ioc->ctl_cmds.reply;
2081         ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2082
2083         if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2084                 ioc->diag_buffer_status[buffer_type] |=
2085                     MPT2_DIAG_BUFFER_IS_REGISTERED;
2086                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
2087                     ioc->name, __func__));
2088         } else {
2089                 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
2090                     "log_info(0x%08x)\n", ioc->name, __func__,
2091                     ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2092                 rc = -EFAULT;
2093         }
2094
2095  issue_host_reset:
2096         if (issue_reset)
2097                 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2098                     FORCE_BIG_HAMMER);
2099
2100  out:
2101
2102         ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
2103         return rc;
2104 }
2105
2106
2107 #ifdef CONFIG_COMPAT
2108 /**
2109  * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2110  * @ioc: per adapter object
2111  * @cmd - ioctl opcode
2112  * @arg - (struct mpt2_ioctl_command32)
2113  *
2114  * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2115  */
2116 static long
2117 _ctl_compat_mpt_command(struct MPT2SAS_ADAPTER *ioc, unsigned cmd,
2118         void __user *arg)
2119 {
2120         struct mpt2_ioctl_command32 karg32;
2121         struct mpt2_ioctl_command32 __user *uarg;
2122         struct mpt2_ioctl_command karg;
2123
2124         if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2125                 return -EINVAL;
2126
2127         uarg = (struct mpt2_ioctl_command32 __user *) arg;
2128
2129         if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2130                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2131                     __FILE__, __LINE__, __func__);
2132                 return -EFAULT;
2133         }
2134
2135         memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2136         karg.hdr.ioc_number = karg32.hdr.ioc_number;
2137         karg.hdr.port_number = karg32.hdr.port_number;
2138         karg.hdr.max_data_size = karg32.hdr.max_data_size;
2139         karg.timeout = karg32.timeout;
2140         karg.max_reply_bytes = karg32.max_reply_bytes;
2141         karg.data_in_size = karg32.data_in_size;
2142         karg.data_out_size = karg32.data_out_size;
2143         karg.max_sense_bytes = karg32.max_sense_bytes;
2144         karg.data_sge_offset = karg32.data_sge_offset;
2145         karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2146         karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2147         karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2148         karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2149         return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2150 }
2151 #endif
2152
2153 /**
2154  * _ctl_ioctl_main - main ioctl entry point
2155  * @file - (struct file)
2156  * @cmd - ioctl opcode
2157  * @arg -
2158  * compat - handles 32 bit applications in 64bit os
2159  */
2160 static long
2161 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2162         u8 compat)
2163 {
2164         struct MPT2SAS_ADAPTER *ioc;
2165         struct mpt2_ioctl_header ioctl_header;
2166         enum block_state state;
2167         long ret = -EINVAL;
2168
2169         /* get IOCTL header */
2170         if (copy_from_user(&ioctl_header, (char __user *)arg,
2171             sizeof(struct mpt2_ioctl_header))) {
2172                 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2173                     __FILE__, __LINE__, __func__);
2174                 return -EFAULT;
2175         }
2176
2177         if (_ctl_verify_adapter(ioctl_header.ioc_number, &ioc) == -1 || !ioc)
2178                 return -ENODEV;
2179         /* pci_access_mutex lock acquired by ioctl path */
2180         mutex_lock(&ioc->pci_access_mutex);
2181         if (ioc->shost_recovery || ioc->pci_error_recovery ||
2182                 ioc->is_driver_loading || ioc->remove_host) {
2183                 ret = -EAGAIN;
2184                 goto out_unlock_pciaccess;
2185         }
2186
2187         state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2188         if (state == NON_BLOCKING) {
2189                 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2190                         ret = -EAGAIN;
2191                         goto out_unlock_pciaccess;
2192                 }
2193         } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2194                 ret = -ERESTARTSYS;
2195                 goto out_unlock_pciaccess;
2196         }
2197
2198         switch (cmd) {
2199         case MPT2IOCINFO:
2200                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
2201                         ret = _ctl_getiocinfo(ioc, arg);
2202                 break;
2203 #ifdef CONFIG_COMPAT
2204         case MPT2COMMAND32:
2205 #endif
2206         case MPT2COMMAND:
2207         {
2208                 struct mpt2_ioctl_command __user *uarg;
2209                 struct mpt2_ioctl_command karg;
2210 #ifdef CONFIG_COMPAT
2211                 if (compat) {
2212                         ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2213                         break;
2214                 }
2215 #endif
2216                 if (copy_from_user(&karg, arg, sizeof(karg))) {
2217                         printk(KERN_ERR "failure at %s:%d/%s()!\n",
2218                             __FILE__, __LINE__, __func__);
2219                         ret = -EFAULT;
2220                         break;
2221                 }
2222
2223                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2224                         uarg = arg;
2225                         ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2226                 }
2227                 break;
2228         }
2229         case MPT2EVENTQUERY:
2230                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2231                         ret = _ctl_eventquery(ioc, arg);
2232                 break;
2233         case MPT2EVENTENABLE:
2234                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2235                         ret = _ctl_eventenable(ioc, arg);
2236                 break;
2237         case MPT2EVENTREPORT:
2238                 ret = _ctl_eventreport(ioc, arg);
2239                 break;
2240         case MPT2HARDRESET:
2241                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2242                         ret = _ctl_do_reset(ioc, arg);
2243                 break;
2244         case MPT2BTDHMAPPING:
2245                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2246                         ret = _ctl_btdh_mapping(ioc, arg);
2247                 break;
2248         case MPT2DIAGREGISTER:
2249                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2250                         ret = _ctl_diag_register(ioc, arg);
2251                 break;
2252         case MPT2DIAGUNREGISTER:
2253                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2254                         ret = _ctl_diag_unregister(ioc, arg);
2255                 break;
2256         case MPT2DIAGQUERY:
2257                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2258                         ret = _ctl_diag_query(ioc, arg);
2259                 break;
2260         case MPT2DIAGRELEASE:
2261                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2262                         ret = _ctl_diag_release(ioc, arg);
2263                 break;
2264         case MPT2DIAGREADBUFFER:
2265                 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2266                         ret = _ctl_diag_read_buffer(ioc, arg);
2267                 break;
2268         default:
2269
2270                 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
2271                     "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2272                 break;
2273         }
2274
2275         mutex_unlock(&ioc->ctl_cmds.mutex);
2276 out_unlock_pciaccess:
2277         mutex_unlock(&ioc->pci_access_mutex);
2278         return ret;
2279 }
2280
2281 /**
2282  * _ctl_ioctl - main ioctl entry point (unlocked)
2283  * @file - (struct file)
2284  * @cmd - ioctl opcode
2285  * @arg -
2286  */
2287 static long
2288 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2289 {
2290         long ret;
2291
2292         ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0);
2293         return ret;
2294 }
2295 #ifdef CONFIG_COMPAT
2296 /**
2297  * _ctl_ioctl_compat - main ioctl entry point (compat)
2298  * @file -
2299  * @cmd -
2300  * @arg -
2301  *
2302  * This routine handles 32 bit applications in 64bit os.
2303  */
2304 static long
2305 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2306 {
2307         long ret;
2308
2309         ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1);
2310         return ret;
2311 }
2312 #endif
2313
2314 /* scsi host attributes */
2315
2316 /**
2317  * _ctl_version_fw_show - firmware version
2318  * @cdev - pointer to embedded class device
2319  * @buf - the buffer returned
2320  *
2321  * A sysfs 'read-only' shost attribute.
2322  */
2323 static ssize_t
2324 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2325     char *buf)
2326 {
2327         struct Scsi_Host *shost = class_to_shost(cdev);
2328         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2329
2330         return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2331             (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2332             (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2333             (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2334             ioc->facts.FWVersion.Word & 0x000000FF);
2335 }
2336 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2337
2338 /**
2339  * _ctl_version_bios_show - bios version
2340  * @cdev - pointer to embedded class device
2341  * @buf - the buffer returned
2342  *
2343  * A sysfs 'read-only' shost attribute.
2344  */
2345 static ssize_t
2346 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2347     char *buf)
2348 {
2349         struct Scsi_Host *shost = class_to_shost(cdev);
2350         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2351
2352         u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2353
2354         return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2355             (version & 0xFF000000) >> 24,
2356             (version & 0x00FF0000) >> 16,
2357             (version & 0x0000FF00) >> 8,
2358             version & 0x000000FF);
2359 }
2360 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2361
2362 /**
2363  * _ctl_version_mpi_show - MPI (message passing interface) version
2364  * @cdev - pointer to embedded class device
2365  * @buf - the buffer returned
2366  *
2367  * A sysfs 'read-only' shost attribute.
2368  */
2369 static ssize_t
2370 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2371     char *buf)
2372 {
2373         struct Scsi_Host *shost = class_to_shost(cdev);
2374         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2375
2376         return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2377             ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2378 }
2379 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2380
2381 /**
2382  * _ctl_version_product_show - product name
2383  * @cdev - pointer to embedded class device
2384  * @buf - the buffer returned
2385  *
2386  * A sysfs 'read-only' shost attribute.
2387  */
2388 static ssize_t
2389 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2390     char *buf)
2391 {
2392         struct Scsi_Host *shost = class_to_shost(cdev);
2393         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2394
2395         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2396 }
2397 static DEVICE_ATTR(version_product, S_IRUGO,
2398    _ctl_version_product_show, NULL);
2399
2400 /**
2401  * _ctl_version_nvdata_persistent_show - ndvata persistent version
2402  * @cdev - pointer to embedded class device
2403  * @buf - the buffer returned
2404  *
2405  * A sysfs 'read-only' shost attribute.
2406  */
2407 static ssize_t
2408 _ctl_version_nvdata_persistent_show(struct device *cdev,
2409     struct device_attribute *attr, char *buf)
2410 {
2411         struct Scsi_Host *shost = class_to_shost(cdev);
2412         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2413
2414         return snprintf(buf, PAGE_SIZE, "%08xh\n",
2415             le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2416 }
2417 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2418     _ctl_version_nvdata_persistent_show, NULL);
2419
2420 /**
2421  * _ctl_version_nvdata_default_show - nvdata default version
2422  * @cdev - pointer to embedded class device
2423  * @buf - the buffer returned
2424  *
2425  * A sysfs 'read-only' shost attribute.
2426  */
2427 static ssize_t
2428 _ctl_version_nvdata_default_show(struct device *cdev,
2429     struct device_attribute *attr, char *buf)
2430 {
2431         struct Scsi_Host *shost = class_to_shost(cdev);
2432         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2433
2434         return snprintf(buf, PAGE_SIZE, "%08xh\n",
2435             le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2436 }
2437 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2438     _ctl_version_nvdata_default_show, NULL);
2439
2440 /**
2441  * _ctl_board_name_show - board name
2442  * @cdev - pointer to embedded class device
2443  * @buf - the buffer returned
2444  *
2445  * A sysfs 'read-only' shost attribute.
2446  */
2447 static ssize_t
2448 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2449     char *buf)
2450 {
2451         struct Scsi_Host *shost = class_to_shost(cdev);
2452         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2453
2454         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2455 }
2456 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2457
2458 /**
2459  * _ctl_board_assembly_show - board assembly name
2460  * @cdev - pointer to embedded class device
2461  * @buf - the buffer returned
2462  *
2463  * A sysfs 'read-only' shost attribute.
2464  */
2465 static ssize_t
2466 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2467     char *buf)
2468 {
2469         struct Scsi_Host *shost = class_to_shost(cdev);
2470         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2471
2472         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2473 }
2474 static DEVICE_ATTR(board_assembly, S_IRUGO,
2475     _ctl_board_assembly_show, NULL);
2476
2477 /**
2478  * _ctl_board_tracer_show - board tracer number
2479  * @cdev - pointer to embedded class device
2480  * @buf - the buffer returned
2481  *
2482  * A sysfs 'read-only' shost attribute.
2483  */
2484 static ssize_t
2485 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2486     char *buf)
2487 {
2488         struct Scsi_Host *shost = class_to_shost(cdev);
2489         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2490
2491         return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2492 }
2493 static DEVICE_ATTR(board_tracer, S_IRUGO,
2494     _ctl_board_tracer_show, NULL);
2495
2496 /**
2497  * _ctl_io_delay_show - io missing delay
2498  * @cdev - pointer to embedded class device
2499  * @buf - the buffer returned
2500  *
2501  * This is for firmware implemention for deboucing device
2502  * removal events.
2503  *
2504  * A sysfs 'read-only' shost attribute.
2505  */
2506 static ssize_t
2507 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2508     char *buf)
2509 {
2510         struct Scsi_Host *shost = class_to_shost(cdev);
2511         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2512
2513         return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2514 }
2515 static DEVICE_ATTR(io_delay, S_IRUGO,
2516     _ctl_io_delay_show, NULL);
2517
2518 /**
2519  * _ctl_device_delay_show - device missing delay
2520  * @cdev - pointer to embedded class device
2521  * @buf - the buffer returned
2522  *
2523  * This is for firmware implemention for deboucing device
2524  * removal events.
2525  *
2526  * A sysfs 'read-only' shost attribute.
2527  */
2528 static ssize_t
2529 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2530     char *buf)
2531 {
2532         struct Scsi_Host *shost = class_to_shost(cdev);
2533         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2534
2535         return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2536 }
2537 static DEVICE_ATTR(device_delay, S_IRUGO,
2538     _ctl_device_delay_show, NULL);
2539
2540 /**
2541  * _ctl_fw_queue_depth_show - global credits
2542  * @cdev - pointer to embedded class device
2543  * @buf - the buffer returned
2544  *
2545  * This is firmware queue depth limit
2546  *
2547  * A sysfs 'read-only' shost attribute.
2548  */
2549 static ssize_t
2550 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2551     char *buf)
2552 {
2553         struct Scsi_Host *shost = class_to_shost(cdev);
2554         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2555
2556         return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2557 }
2558 static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2559     _ctl_fw_queue_depth_show, NULL);
2560
2561 /**
2562  * _ctl_sas_address_show - sas address
2563  * @cdev - pointer to embedded class device
2564  * @buf - the buffer returned
2565  *
2566  * This is the controller sas address
2567  *
2568  * A sysfs 'read-only' shost attribute.
2569  */
2570 static ssize_t
2571 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2572     char *buf)
2573 {
2574         struct Scsi_Host *shost = class_to_shost(cdev);
2575         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2576
2577         return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2578             (unsigned long long)ioc->sas_hba.sas_address);
2579 }
2580 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2581     _ctl_host_sas_address_show, NULL);
2582
2583 /**
2584  * _ctl_logging_level_show - logging level
2585  * @cdev - pointer to embedded class device
2586  * @buf - the buffer returned
2587  *
2588  * A sysfs 'read/write' shost attribute.
2589  */
2590 static ssize_t
2591 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2592     char *buf)
2593 {
2594         struct Scsi_Host *shost = class_to_shost(cdev);
2595         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2596
2597         return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2598 }
2599 static ssize_t
2600 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2601     const char *buf, size_t count)
2602 {
2603         struct Scsi_Host *shost = class_to_shost(cdev);
2604         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2605         int val = 0;
2606
2607         if (sscanf(buf, "%x", &val) != 1)
2608                 return -EINVAL;
2609
2610         ioc->logging_level = val;
2611         printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2612             ioc->logging_level);
2613         return strlen(buf);
2614 }
2615 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2616     _ctl_logging_level_show, _ctl_logging_level_store);
2617
2618 /* device attributes */
2619 /*
2620  * _ctl_fwfault_debug_show - show/store fwfault_debug
2621  * @cdev - pointer to embedded class device
2622  * @buf - the buffer returned
2623  *
2624  * mpt2sas_fwfault_debug is command line option
2625  * A sysfs 'read/write' shost attribute.
2626  */
2627 static ssize_t
2628 _ctl_fwfault_debug_show(struct device *cdev,
2629     struct device_attribute *attr, char *buf)
2630 {
2631         struct Scsi_Host *shost = class_to_shost(cdev);
2632         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2633
2634         return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2635 }
2636 static ssize_t
2637 _ctl_fwfault_debug_store(struct device *cdev,
2638     struct device_attribute *attr, const char *buf, size_t count)
2639 {
2640         struct Scsi_Host *shost = class_to_shost(cdev);
2641         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2642         int val = 0;
2643
2644         if (sscanf(buf, "%d", &val) != 1)
2645                 return -EINVAL;
2646
2647         ioc->fwfault_debug = val;
2648         printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
2649             ioc->fwfault_debug);
2650         return strlen(buf);
2651 }
2652 static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2653     _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2654
2655
2656 /**
2657  * _ctl_ioc_reset_count_show - ioc reset count
2658  * @cdev - pointer to embedded class device
2659  * @buf - the buffer returned
2660  *
2661  * This is firmware queue depth limit
2662  *
2663  * A sysfs 'read-only' shost attribute.
2664  */
2665 static ssize_t
2666 _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2667     char *buf)
2668 {
2669         struct Scsi_Host *shost = class_to_shost(cdev);
2670         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2671
2672         return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
2673 }
2674 static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
2675     _ctl_ioc_reset_count_show, NULL);
2676
2677 /**
2678  * _ctl_ioc_reply_queue_count_show - number of reply queues
2679  * @cdev - pointer to embedded class device
2680  * @buf - the buffer returned
2681  *
2682  * This is number of reply queues
2683  *
2684  * A sysfs 'read-only' shost attribute.
2685  */
2686 static ssize_t
2687 _ctl_ioc_reply_queue_count_show(struct device *cdev,
2688          struct device_attribute *attr, char *buf)
2689 {
2690         u8 reply_queue_count;
2691         struct Scsi_Host *shost = class_to_shost(cdev);
2692         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2693
2694         if ((ioc->facts.IOCCapabilities &
2695             MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2696                 reply_queue_count = ioc->reply_queue_count;
2697         else
2698                 reply_queue_count = 1;
2699         return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2700 }
2701 static DEVICE_ATTR(reply_queue_count, S_IRUGO,
2702          _ctl_ioc_reply_queue_count_show, NULL);
2703
2704 /**
2705  * _ctl_BRM_status_show - Backup Rail Monitor Status
2706  * @cdev - pointer to embedded class device
2707  * @buf - the buffer returned
2708  *
2709  * This is number of reply queues
2710  *
2711  * A sysfs 'read-only' shost attribute.
2712  */
2713 static ssize_t
2714 _ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
2715         char *buf)
2716 {
2717         struct Scsi_Host *shost = class_to_shost(cdev);
2718         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2719         Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2720         Mpi2ConfigReply_t mpi_reply;
2721         u16 backup_rail_monitor_status = 0;
2722         u16 ioc_status;
2723         int sz;
2724         ssize_t rc = 0;
2725
2726         if (!ioc->is_warpdrive) {
2727                 printk(MPT2SAS_ERR_FMT "%s: BRM attribute is only for"\
2728                     "warpdrive\n", ioc->name, __func__);
2729                 goto out;
2730         }
2731         /* pci_access_mutex lock acquired by sysfs show path */
2732         mutex_lock(&ioc->pci_access_mutex);
2733         if (ioc->pci_error_recovery || ioc->remove_host) {
2734                 mutex_unlock(&ioc->pci_access_mutex);
2735                 return 0;
2736         }
2737
2738         /* allocate upto GPIOVal 36 entries */
2739         sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2740         io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2741         if (!io_unit_pg3) {
2742                 printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"\
2743                     "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
2744                 goto out;
2745         }
2746
2747         if (mpt2sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2748             0) {
2749                 printk(MPT2SAS_ERR_FMT
2750                     "%s: failed reading iounit_pg3\n", ioc->name,
2751                     __func__);
2752                 goto out;
2753         }
2754
2755         ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2756         if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2757                 printk(MPT2SAS_ERR_FMT "%s: iounit_pg3 failed with"\
2758                     "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
2759                 goto out;
2760         }
2761
2762         if (io_unit_pg3->GPIOCount < 25) {
2763                 printk(MPT2SAS_ERR_FMT "%s: iounit_pg3->GPIOCount less than"\
2764                      "25 entries, detected (%d) entries\n", ioc->name, __func__,
2765                     io_unit_pg3->GPIOCount);
2766                 goto out;
2767         }
2768
2769         /* BRM status is in bit zero of GPIOVal[24] */
2770         backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2771         rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2772
2773  out:
2774         kfree(io_unit_pg3);
2775         mutex_unlock(&ioc->pci_access_mutex);
2776         return rc;
2777 }
2778 static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
2779
2780 struct DIAG_BUFFER_START {
2781         __le32 Size;
2782         __le32 DiagVersion;
2783         u8 BufferType;
2784         u8 Reserved[3];
2785         __le32 Reserved1;
2786         __le32 Reserved2;
2787         __le32 Reserved3;
2788 };
2789 /**
2790  * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2791  * @cdev - pointer to embedded class device
2792  * @buf - the buffer returned
2793  *
2794  * A sysfs 'read-only' shost attribute.
2795  */
2796 static ssize_t
2797 _ctl_host_trace_buffer_size_show(struct device *cdev,
2798     struct device_attribute *attr, char *buf)
2799 {
2800         struct Scsi_Host *shost = class_to_shost(cdev);
2801         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2802         u32 size = 0;
2803         struct DIAG_BUFFER_START *request_data;
2804
2805         if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2806                 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2807                     "registered\n", ioc->name, __func__);
2808                 return 0;
2809         }
2810
2811         if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2812             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2813                 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2814                     "registered\n", ioc->name, __func__);
2815                 return 0;
2816         }
2817
2818         request_data = (struct DIAG_BUFFER_START *)
2819             ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2820         if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2821             le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
2822             le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2823                 size = le32_to_cpu(request_data->Size);
2824
2825         ioc->ring_buffer_sz = size;
2826         return snprintf(buf, PAGE_SIZE, "%d\n", size);
2827 }
2828 static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2829          _ctl_host_trace_buffer_size_show, NULL);
2830
2831 /**
2832  * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2833  * @cdev - pointer to embedded class device
2834  * @buf - the buffer returned
2835  *
2836  * A sysfs 'read/write' shost attribute.
2837  *
2838  * You will only be able to read 4k bytes of ring buffer at a time.
2839  * In order to read beyond 4k bytes, you will have to write out the
2840  * offset to the same attribute, it will move the pointer.
2841  */
2842 static ssize_t
2843 _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2844      char *buf)
2845 {
2846         struct Scsi_Host *shost = class_to_shost(cdev);
2847         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2848         void *request_data;
2849         u32 size;
2850
2851         if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2852                 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2853                     "registered\n", ioc->name, __func__);
2854                 return 0;
2855         }
2856
2857         if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2858             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2859                 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2860                     "registered\n", ioc->name, __func__);
2861                 return 0;
2862         }
2863
2864         if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2865                 return 0;
2866
2867         size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2868         size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
2869         request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2870         memcpy(buf, request_data, size);
2871         return size;
2872 }
2873
2874 static ssize_t
2875 _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2876     const char *buf, size_t count)
2877 {
2878         struct Scsi_Host *shost = class_to_shost(cdev);
2879         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2880         int val = 0;
2881
2882         if (sscanf(buf, "%d", &val) != 1)
2883                 return -EINVAL;
2884
2885         ioc->ring_buffer_offset = val;
2886         return strlen(buf);
2887 }
2888 static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2889     _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2890
2891 /*****************************************/
2892
2893 /**
2894  * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2895  * @cdev - pointer to embedded class device
2896  * @buf - the buffer returned
2897  *
2898  * A sysfs 'read/write' shost attribute.
2899  *
2900  * This is a mechnism to post/release host_trace_buffers
2901  */
2902 static ssize_t
2903 _ctl_host_trace_buffer_enable_show(struct device *cdev,
2904     struct device_attribute *attr, char *buf)
2905 {
2906         struct Scsi_Host *shost = class_to_shost(cdev);
2907         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2908
2909         if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
2910            ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2911             MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
2912                 return snprintf(buf, PAGE_SIZE, "off\n");
2913         else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2914             MPT2_DIAG_BUFFER_IS_RELEASED))
2915                 return snprintf(buf, PAGE_SIZE, "release\n");
2916         else
2917                 return snprintf(buf, PAGE_SIZE, "post\n");
2918 }
2919
2920 static ssize_t
2921 _ctl_host_trace_buffer_enable_store(struct device *cdev,
2922     struct device_attribute *attr, const char *buf, size_t count)
2923 {
2924         struct Scsi_Host *shost = class_to_shost(cdev);
2925         struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2926         char str[10] = "";
2927         struct mpt2_diag_register diag_register;
2928         u8 issue_reset = 0;
2929
2930         if (sscanf(buf, "%9s", str) != 1)
2931                 return -EINVAL;
2932
2933         if (!strcmp(str, "post")) {
2934                 /* exit out if host buffers are already posted */
2935                 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
2936                     (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2937                     MPT2_DIAG_BUFFER_IS_REGISTERED) &&
2938                     ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2939                     MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
2940                         goto out;
2941                 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
2942                 printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
2943                     ioc->name);
2944                 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
2945                 diag_register.requested_buffer_size = (1024 * 1024);
2946                 diag_register.unique_id = 0x7075900;
2947                 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
2948                 _ctl_diag_register_2(ioc,  &diag_register);
2949         } else if (!strcmp(str, "release")) {
2950                 /* exit out if host buffers are already released */
2951                 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
2952                         goto out;
2953                 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2954                     MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
2955                         goto out;
2956                 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2957                     MPT2_DIAG_BUFFER_IS_RELEASED))
2958                         goto out;
2959                 printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
2960                     ioc->name);
2961                 _ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
2962         }
2963
2964  out:
2965         return strlen(buf);
2966 }
2967 static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
2968     _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
2969
2970 struct device_attribute *mpt2sas_host_attrs[] = {
2971         &dev_attr_version_fw,
2972         &dev_attr_version_bios,
2973         &dev_attr_version_mpi,
2974         &dev_attr_version_product,
2975         &dev_attr_version_nvdata_persistent,
2976         &dev_attr_version_nvdata_default,
2977         &dev_attr_board_name,
2978         &dev_attr_board_assembly,
2979         &dev_attr_board_tracer,
2980         &dev_attr_io_delay,
2981         &dev_attr_device_delay,
2982         &dev_attr_logging_level,
2983         &dev_attr_fwfault_debug,
2984         &dev_attr_fw_queue_depth,
2985         &dev_attr_host_sas_address,
2986         &dev_attr_ioc_reset_count,
2987         &dev_attr_host_trace_buffer_size,
2988         &dev_attr_host_trace_buffer,
2989         &dev_attr_host_trace_buffer_enable,
2990         &dev_attr_reply_queue_count,
2991         &dev_attr_BRM_status,
2992         NULL,
2993 };
2994
2995 /**
2996  * _ctl_device_sas_address_show - sas address
2997  * @cdev - pointer to embedded class device
2998  * @buf - the buffer returned
2999  *
3000  * This is the sas address for the target
3001  *
3002  * A sysfs 'read-only' shost attribute.
3003  */
3004 static ssize_t
3005 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
3006     char *buf)
3007 {
3008         struct scsi_device *sdev = to_scsi_device(dev);
3009         struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3010
3011         return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3012             (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3013 }
3014 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
3015
3016 /**
3017  * _ctl_device_handle_show - device handle
3018  * @cdev - pointer to embedded class device
3019  * @buf - the buffer returned
3020  *
3021  * This is the firmware assigned device handle
3022  *
3023  * A sysfs 'read-only' shost attribute.
3024  */
3025 static ssize_t
3026 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
3027     char *buf)
3028 {
3029         struct scsi_device *sdev = to_scsi_device(dev);
3030         struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3031
3032         return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3033             sas_device_priv_data->sas_target->handle);
3034 }
3035 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
3036
3037 struct device_attribute *mpt2sas_dev_attrs[] = {
3038         &dev_attr_sas_address,
3039         &dev_attr_sas_device_handle,
3040         NULL,
3041 };
3042
3043 static const struct file_operations ctl_fops = {
3044         .owner = THIS_MODULE,
3045         .unlocked_ioctl = _ctl_ioctl,
3046         .poll = _ctl_poll,
3047         .fasync = _ctl_fasync,
3048 #ifdef CONFIG_COMPAT
3049         .compat_ioctl = _ctl_ioctl_compat,
3050 #endif
3051         .llseek = noop_llseek,
3052 };
3053
3054 static struct miscdevice ctl_dev = {
3055         .minor  = MPT2SAS_MINOR,
3056         .name   = MPT2SAS_DEV_NAME,
3057         .fops   = &ctl_fops,
3058 };
3059
3060 /**
3061  * mpt2sas_ctl_init - main entry point for ctl.
3062  *
3063  */
3064 void
3065 mpt2sas_ctl_init(void)
3066 {
3067         async_queue = NULL;
3068         if (misc_register(&ctl_dev) < 0)
3069                 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
3070                     MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3071
3072         init_waitqueue_head(&ctl_poll_wait);
3073 }
3074
3075 /**
3076  * mpt2sas_ctl_exit - exit point for ctl
3077  *
3078  */
3079 void
3080 mpt2sas_ctl_exit(void)
3081 {
3082         struct MPT2SAS_ADAPTER *ioc;
3083         int i;
3084
3085         list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
3086
3087                 /* free memory associated to diag buffers */
3088                 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3089                         if (!ioc->diag_buffer[i])
3090                                 continue;
3091                         pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3092                             ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3093                         ioc->diag_buffer[i] = NULL;
3094                         ioc->diag_buffer_status[i] = 0;
3095                 }
3096
3097                 kfree(ioc->event_log);
3098         }
3099         misc_deregister(&ctl_dev);
3100 }
3101