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