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