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