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