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