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