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