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