Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / mpt2sas / mpt2sas_ctl.c
CommitLineData
635374e7
EM
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
31b7f2e2 6 * Copyright (C) 2007-2010 LSI Corporation
635374e7
EM
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
635374e7
EM
45#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/errno.h>
48#include <linux/init.h>
49#include <linux/slab.h>
50#include <linux/types.h>
51#include <linux/pci.h>
52#include <linux/delay.h>
c45d15d2 53#include <linux/mutex.h>
635374e7
EM
54#include <linux/compat.h>
55#include <linux/poll.h>
56
57#include <linux/io.h>
58#include <linux/uaccess.h>
59
60#include "mpt2sas_base.h"
61#include "mpt2sas_ctl.h"
62
c45d15d2 63static DEFINE_MUTEX(_ctl_mutex);
635374e7
EM
64static struct fasync_struct *async_queue;
65static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
66
99bb214b
EM
67static int _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type,
68 u8 *issue_reset);
69
635374e7
EM
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 */
78enum block_state {
79 NON_BLOCKING,
80 BLOCKING,
81};
82
363fa50f 83#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
7fbae67a
KD
84/**
85 * _ctl_sas_device_find_by_handle - sas device search
86 * @ioc: per adapter object
87 * @handle: sas device handle (assigned by firmware)
88 * Context: Calling function should acquire ioc->sas_device_lock
89 *
90 * This searches for sas_device based on sas_address, then return sas_device
91 * object.
92 */
93static struct _sas_device *
94_ctl_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
95{
96 struct _sas_device *sas_device, *r;
97
98 r = NULL;
99 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
100 if (sas_device->handle != handle)
101 continue;
102 r = sas_device;
103 goto out;
104 }
105
106 out:
107 return r;
108}
109
635374e7
EM
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 *
25985edc 118 * Function for displaying debug info helpful when debugging issues
635374e7
EM
119 * in this module.
120 */
121static 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
eabb08ad 217 printk(MPT2SAS_INFO_FMT "%s: %s, smid(%d)\n",
635374e7
EM
218 ioc->name, calling_function_name, desc, smid);
219
220 if (!mpi_reply)
221 return;
222
223 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
eabb08ad 224 printk(MPT2SAS_INFO_FMT
635374e7
EM
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;
7fbae67a
KD
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);
635374e7 250 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
eabb08ad 251 printk(MPT2SAS_INFO_FMT
635374e7
EM
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
7b936b02 264 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
265 * @reply: reply message frame(lower 32bit addr)
266 * Context: none.
267 *
268 * The callback handler when using ioc->ctl_cb_idx.
269 *
77e63ed4
KD
270 * Return 1 meaning mf should be freed from _base_interrupt
271 * 0 means the mf is freed from this function.
635374e7 272 */
77e63ed4 273u8
7b936b02
KD
274mpt2sas_ctl_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
275 u32 reply)
635374e7
EM
276{
277 MPI2DefaultReply_t *mpi_reply;
769578ff
KD
278 Mpi2SCSIIOReply_t *scsiio_reply;
279 const void *sense_data;
280 u32 sz;
635374e7
EM
281
282 if (ioc->ctl_cmds.status == MPT2_CMD_NOT_USED)
77e63ed4 283 return 1;
635374e7 284 if (ioc->ctl_cmds.smid != smid)
77e63ed4 285 return 1;
635374e7
EM
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;
769578ff
KD
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 }
635374e7
EM
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);
77e63ed4 311 return 1;
635374e7
EM
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 */
324static 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 */
347void
348mpt2sas_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
7b936b02 393 * @msix_index: MSIX table index supplied by the OS
635374e7
EM
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 *
77e63ed4
KD
400 * Return 1 meaning mf should be freed from _base_interrupt
401 * 0 means the mf is freed from this function.
635374e7 402 */
77e63ed4 403u8
7b936b02
KD
404mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
405 u32 reply)
635374e7
EM
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);
77e63ed4 411 return 1;
635374e7
EM
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 */
421static int
422_ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
423{
424 struct MPT2SAS_ADAPTER *ioc;
425
ba33fadf 426 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
635374e7
EM
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 */
446void
447mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
448{
99bb214b
EM
449 int i;
450 u8 issue_reset;
451
635374e7
EM
452 switch (reset_phase) {
453 case MPT2_IOC_PRE_RESET:
eabb08ad 454 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
635374e7 455 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
99bb214b
EM
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 }
635374e7
EM
465 break;
466 case MPT2_IOC_AFTER_RESET:
eabb08ad 467 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
635374e7
EM
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:
eabb08ad 476 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
635374e7 477 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
99bb214b
EM
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 }
635374e7
EM
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 */
501static 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 */
514static 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 */
526static 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
ba33fadf 533 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
635374e7
EM
534 if (ioc->aen_event_read_flag)
535 return POLLIN | POLLRDNORM;
536 }
537 return 0;
538}
539
540/**
ddf59a35 541 * _ctl_set_task_mid - assign an active smid to tm request
635374e7
EM
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 */
549static int
ddf59a35 550_ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
635374e7
EM
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;
ddf59a35
EM
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;
635374e7
EM
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);
595bb0bd 575 for (i = ioc->scsiio_depth; i && !found; i--) {
635374e7
EM
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) {
eabb08ad 593 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
ddf59a35 594 "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
e94f6747 595 desc, le16_to_cpu(tm_request->DevHandle), lun));
635374e7
EM
596 tm_reply = ioc->ctl_cmds.reply;
597 tm_reply->DevHandle = tm_request->DevHandle;
598 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
ddf59a35 599 tm_reply->TaskType = tm_request->TaskType;
635374e7
EM
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
eabb08ad 611 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
ddf59a35 612 "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
e94f6747
KD
613 desc, le16_to_cpu(tm_request->DevHandle), lun,
614 le16_to_cpu(tm_request->TaskMID)));
635374e7
EM
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 */
625static long
626_ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc,
627 struct mpt2_ioctl_command karg, void __user *mf, enum block_state state)
628{
1bbfa378 629 MPI2RequestHeader_t *mpi_request = NULL, *request;
635374e7
EM
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;
635374e7
EM
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
1bbfa378
KD
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;
635374e7
EM
687 goto out;
688 }
689
a1f74ae8
DR
690 /* Check for overflow and wraparound */
691 if (karg.data_sge_offset * 4 > ioc->request_sz ||
692 karg.data_sge_offset > (UINT_MAX / 4)) {
693 ret = -EINVAL;
694 goto out;
695 }
696
635374e7
EM
697 /* copy in request message frame from user */
698 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
699 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
700 __func__);
701 ret = -EFAULT;
635374e7
EM
702 goto out;
703 }
704
1bbfa378
KD
705 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
706 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
707 if (!smid) {
708 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
709 ioc->name, __func__);
710 ret = -EAGAIN;
711 goto out;
712 }
713 } else {
714
715 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
716 if (!smid) {
717 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
718 ioc->name, __func__);
719 ret = -EAGAIN;
720 goto out;
721 }
722 }
723
724 ret = 0;
725 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
726 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
727 request = mpt2sas_base_get_msg_frame(ioc, smid);
728 memcpy(request, mpi_request, karg.data_sge_offset*4);
729 ioc->ctl_cmds.smid = smid;
730 data_out_sz = karg.data_out_size;
731 data_in_sz = karg.data_in_size;
732
635374e7
EM
733 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
734 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
e94f6747
KD
735 if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
736 le16_to_cpu(mpi_request->FunctionDependent1) >
737 ioc->facts.MaxDevHandle) {
635374e7
EM
738 ret = -EINVAL;
739 mpt2sas_base_free_smid(ioc, smid);
740 goto out;
741 }
742 }
743
744 /* obtain dma-able memory for data transfer */
745 if (data_out_sz) /* WRITE */ {
746 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
747 &data_out_dma);
748 if (!data_out) {
749 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
750 __LINE__, __func__);
751 ret = -ENOMEM;
752 mpt2sas_base_free_smid(ioc, smid);
753 goto out;
754 }
755 if (copy_from_user(data_out, karg.data_out_buf_ptr,
756 data_out_sz)) {
757 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
758 __LINE__, __func__);
759 ret = -EFAULT;
760 mpt2sas_base_free_smid(ioc, smid);
761 goto out;
762 }
763 }
764
765 if (data_in_sz) /* READ */ {
766 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
767 &data_in_dma);
768 if (!data_in) {
769 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
770 __LINE__, __func__);
771 ret = -ENOMEM;
772 mpt2sas_base_free_smid(ioc, smid);
773 goto out;
774 }
775 }
776
777 /* add scatter gather elements */
1bbfa378 778 psge = (void *)request + (karg.data_sge_offset*4);
635374e7
EM
779
780 if (!data_out_sz && !data_in_sz) {
781 mpt2sas_base_build_zero_len_sge(ioc, psge);
782 } else if (data_out_sz && data_in_sz) {
783 /* WRITE sgel first */
784 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
785 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
786 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
787 ioc->base_add_sg_single(psge, sgl_flags |
788 data_out_sz, data_out_dma);
789
790 /* incr sgel */
791 psge += ioc->sge_size;
792
793 /* READ sgel last */
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);
797 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
798 ioc->base_add_sg_single(psge, sgl_flags |
799 data_in_sz, data_in_dma);
800 } else if (data_out_sz) /* WRITE */ {
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 | MPI2_SGE_FLAGS_HOST_TO_IOC);
804 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
805 ioc->base_add_sg_single(psge, sgl_flags |
806 data_out_sz, data_out_dma);
807 } else if (data_in_sz) /* READ */ {
808 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
809 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
810 MPI2_SGE_FLAGS_END_OF_LIST);
811 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
812 ioc->base_add_sg_single(psge, sgl_flags |
813 data_in_sz, data_in_dma);
814 }
815
816 /* send command to firmware */
817#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
818 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
819#endif
820
f01690d3 821 init_completion(&ioc->ctl_cmds.done);
635374e7
EM
822 switch (mpi_request->Function) {
823 case MPI2_FUNCTION_SCSI_IO_REQUEST:
824 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
825 {
826 Mpi2SCSIIORequest_t *scsiio_request =
1bbfa378 827 (Mpi2SCSIIORequest_t *)request;
769578ff 828 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
635374e7 829 scsiio_request->SenseBufferLowAddress =
ec9472c7 830 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
769578ff 831 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
ebda4d38
KD
832 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
833 mpt2sas_base_put_smid_scsi_io(ioc, smid,
834 le16_to_cpu(mpi_request->FunctionDependent1));
835 else
836 mpt2sas_base_put_smid_default(ioc, smid);
635374e7
EM
837 break;
838 }
839 case MPI2_FUNCTION_SCSI_TASK_MGMT:
840 {
841 Mpi2SCSITaskManagementRequest_t *tm_request =
1bbfa378 842 (Mpi2SCSITaskManagementRequest_t *)request;
635374e7 843
eabb08ad 844 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
8ed9a03a
KD
845 "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
846 le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
847
635374e7 848 if (tm_request->TaskType ==
ddf59a35
EM
849 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
850 tm_request->TaskType ==
851 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
852 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
77bdd9ee 853 mpt2sas_base_free_smid(ioc, smid);
635374e7 854 goto out;
77bdd9ee 855 }
635374e7
EM
856 }
857
635374e7
EM
858 mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
859 tm_request->DevHandle));
7b936b02 860 mpt2sas_base_put_smid_hi_priority(ioc, smid);
635374e7
EM
861 break;
862 }
863 case MPI2_FUNCTION_SMP_PASSTHROUGH:
864 {
865 Mpi2SmpPassthroughRequest_t *smp_request =
866 (Mpi2SmpPassthroughRequest_t *)mpi_request;
867 u8 *data;
868
869 /* ioc determines which port to use */
870 smp_request->PhysicalPort = 0xFF;
871 if (smp_request->PassthroughFlags &
872 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
873 data = (u8 *)&smp_request->SGL;
874 else
875 data = data_out;
876
877 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
878 ioc->ioc_link_reset_in_progress = 1;
879 ioc->ignore_loginfos = 1;
880 }
7b936b02 881 mpt2sas_base_put_smid_default(ioc, smid);
635374e7
EM
882 break;
883 }
884 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
885 {
886 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
887 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
888
889 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
890 || sasiounit_request->Operation ==
891 MPI2_SAS_OP_PHY_LINK_RESET) {
892 ioc->ioc_link_reset_in_progress = 1;
893 ioc->ignore_loginfos = 1;
894 }
7b936b02 895 mpt2sas_base_put_smid_default(ioc, smid);
635374e7
EM
896 break;
897 }
898 default:
7b936b02 899 mpt2sas_base_put_smid_default(ioc, smid);
635374e7
EM
900 break;
901 }
902
903 if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
904 timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
905 else
906 timeout = karg.timeout;
907 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
908 timeout*HZ);
909 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
910 Mpi2SCSITaskManagementRequest_t *tm_request =
911 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
635374e7
EM
912 mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
913 tm_request->DevHandle));
914 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
915 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
916 ioc->ioc_link_reset_in_progress) {
917 ioc->ioc_link_reset_in_progress = 0;
918 ioc->ignore_loginfos = 0;
919 }
920 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
921 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
922 __func__);
923 _debug_dump_mf(mpi_request, karg.data_sge_offset);
924 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
925 issue_reset = 1;
926 goto issue_host_reset;
927 }
928
929 mpi_reply = ioc->ctl_cmds.reply;
930 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
931
932#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
933 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
934 (ioc->logging_level & MPT_DEBUG_TM)) {
935 Mpi2SCSITaskManagementReply_t *tm_reply =
936 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
937
eabb08ad 938 printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
635374e7
EM
939 "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
940 "TerminationCount(0x%08x)\n", ioc->name,
463217bf
KD
941 le16_to_cpu(tm_reply->IOCStatus),
942 le32_to_cpu(tm_reply->IOCLogInfo),
943 le32_to_cpu(tm_reply->TerminationCount));
635374e7
EM
944 }
945#endif
946 /* copy out xdata to user */
947 if (data_in_sz) {
948 if (copy_to_user(karg.data_in_buf_ptr, data_in,
949 data_in_sz)) {
950 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
951 __LINE__, __func__);
952 ret = -ENODATA;
953 goto out;
954 }
955 }
956
957 /* copy out reply message frame to user */
958 if (karg.max_reply_bytes) {
959 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
960 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
961 sz)) {
962 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
963 __LINE__, __func__);
964 ret = -ENODATA;
965 goto out;
966 }
967 }
968
969 /* copy out sense to user */
970 if (karg.max_sense_bytes && (mpi_request->Function ==
971 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
972 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
973 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
769578ff
KD
974 if (copy_to_user(karg.sense_data_ptr,
975 ioc->ctl_cmds.sense, sz)) {
635374e7
EM
976 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
977 __LINE__, __func__);
978 ret = -ENODATA;
979 goto out;
980 }
981 }
982
983 issue_host_reset:
984 if (issue_reset) {
b2ff36ba 985 ret = -ENODATA;
635374e7
EM
986 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
987 mpi_request->Function ==
988 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
989 printk(MPT2SAS_INFO_FMT "issue target reset: handle "
990 "= (0x%04x)\n", ioc->name,
e94f6747 991 le16_to_cpu(mpi_request->FunctionDependent1));
fa7f3167 992 mpt2sas_halt_firmware(ioc);
635374e7 993 mpt2sas_scsih_issue_tm(ioc,
8ed9a03a
KD
994 le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
995 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
f93213de 996 0, TM_MUTEX_ON);
635374e7 997 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
635374e7
EM
998 } else
999 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1000 FORCE_BIG_HAMMER);
1001 }
1002
1003 out:
1004
1005 /* free memory associated with sg buffers */
1006 if (data_in)
1007 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1008 data_in_dma);
1009
1010 if (data_out)
1011 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1012 data_out_dma);
1013
1bbfa378 1014 kfree(mpi_request);
635374e7
EM
1015 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1016 mutex_unlock(&ioc->ctl_cmds.mutex);
1017 return ret;
1018}
1019
1020/**
1021 * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
1022 * @arg - user space buffer containing ioctl content
1023 */
1024static long
1025_ctl_getiocinfo(void __user *arg)
1026{
1027 struct mpt2_ioctl_iocinfo karg;
1028 struct MPT2SAS_ADAPTER *ioc;
635374e7
EM
1029
1030 if (copy_from_user(&karg, arg, sizeof(karg))) {
1031 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1032 __FILE__, __LINE__, __func__);
1033 return -EFAULT;
1034 }
1035 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1036 return -ENODEV;
1037
eabb08ad 1038 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
635374e7
EM
1039 __func__));
1040
1041 memset(&karg, 0 , sizeof(karg));
0bdccdb0
KD
1042 if (ioc->is_warpdrive)
1043 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1044 else
1045 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
635374e7
EM
1046 if (ioc->pfacts)
1047 karg.port_number = ioc->pfacts[0].PortNumber;
7d7311c4 1048 karg.hw_rev = ioc->pdev->revision;
635374e7
EM
1049 karg.pci_id = ioc->pdev->device;
1050 karg.subsystem_device = ioc->pdev->subsystem_device;
1051 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1052 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1053 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1054 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1055 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1056 karg.firmware_version = ioc->facts.FWVersion.Word;
e5f9bb19
EM
1057 strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
1058 strcat(karg.driver_version, "-");
1059 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
635374e7
EM
1060 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1061
1062 if (copy_to_user(arg, &karg, sizeof(karg))) {
1063 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1064 __FILE__, __LINE__, __func__);
1065 return -EFAULT;
1066 }
1067 return 0;
1068}
1069
1070/**
1071 * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
1072 * @arg - user space buffer containing ioctl content
1073 */
1074static long
1075_ctl_eventquery(void __user *arg)
1076{
1077 struct mpt2_ioctl_eventquery karg;
1078 struct MPT2SAS_ADAPTER *ioc;
1079
1080 if (copy_from_user(&karg, arg, sizeof(karg))) {
1081 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1082 __FILE__, __LINE__, __func__);
1083 return -EFAULT;
1084 }
1085 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1086 return -ENODEV;
1087
eabb08ad 1088 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
635374e7
EM
1089 __func__));
1090
1091 karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
1092 memcpy(karg.event_types, ioc->event_type,
1093 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1094
1095 if (copy_to_user(arg, &karg, sizeof(karg))) {
1096 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1097 __FILE__, __LINE__, __func__);
1098 return -EFAULT;
1099 }
1100 return 0;
1101}
1102
1103/**
1104 * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1105 * @arg - user space buffer containing ioctl content
1106 */
1107static long
1108_ctl_eventenable(void __user *arg)
1109{
1110 struct mpt2_ioctl_eventenable karg;
1111 struct MPT2SAS_ADAPTER *ioc;
1112
1113 if (copy_from_user(&karg, arg, sizeof(karg))) {
1114 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1115 __FILE__, __LINE__, __func__);
1116 return -EFAULT;
1117 }
1118 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1119 return -ENODEV;
1120
eabb08ad 1121 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
635374e7
EM
1122 __func__));
1123
1124 if (ioc->event_log)
1125 return 0;
1126 memcpy(ioc->event_type, karg.event_types,
1127 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1128 mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1129
1130 /* initialize event_log */
1131 ioc->event_context = 0;
1132 ioc->aen_event_read_flag = 0;
1133 ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1134 sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1135 if (!ioc->event_log) {
1136 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1137 __FILE__, __LINE__, __func__);
1138 return -ENOMEM;
1139 }
1140 return 0;
1141}
1142
1143/**
1144 * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1145 * @arg - user space buffer containing ioctl content
1146 */
1147static long
1148_ctl_eventreport(void __user *arg)
1149{
1150 struct mpt2_ioctl_eventreport karg;
1151 struct MPT2SAS_ADAPTER *ioc;
1152 u32 number_bytes, max_events, max;
1153 struct mpt2_ioctl_eventreport __user *uarg = arg;
1154
1155 if (copy_from_user(&karg, arg, sizeof(karg))) {
1156 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1157 __FILE__, __LINE__, __func__);
1158 return -EFAULT;
1159 }
1160 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1161 return -ENODEV;
1162
eabb08ad 1163 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
635374e7
EM
1164 __func__));
1165
1166 number_bytes = karg.hdr.max_data_size -
1167 sizeof(struct mpt2_ioctl_header);
1168 max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1169 max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1170
1171 /* If fewer than 1 event is requested, there must have
1172 * been some type of error.
1173 */
1174 if (!max || !ioc->event_log)
1175 return -ENODATA;
1176
1177 number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1178 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1179 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1180 __FILE__, __LINE__, __func__);
1181 return -EFAULT;
1182 }
1183
1184 /* reset flag so SIGIO can restart */
1185 ioc->aen_event_read_flag = 0;
1186 return 0;
1187}
1188
1189/**
1190 * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1191 * @arg - user space buffer containing ioctl content
1192 */
1193static long
1194_ctl_do_reset(void __user *arg)
1195{
1196 struct mpt2_ioctl_diag_reset karg;
1197 struct MPT2SAS_ADAPTER *ioc;
1198 int retval;
1199
1200 if (copy_from_user(&karg, arg, sizeof(karg))) {
1201 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1202 __FILE__, __LINE__, __func__);
1203 return -EFAULT;
1204 }
1205 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1206 return -ENODEV;
1207
921cd802 1208 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1209 ioc->is_driver_loading)
1210 return -EAGAIN;
eabb08ad 1211 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
635374e7
EM
1212 __func__));
1213
1214 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1215 FORCE_BIG_HAMMER);
1216 printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1217 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1218 return 0;
1219}
1220
1221/**
1222 * _ctl_btdh_search_sas_device - searching for sas device
1223 * @ioc: per adapter object
1224 * @btdh: btdh ioctl payload
1225 */
1226static int
1227_ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1228 struct mpt2_ioctl_btdh_mapping *btdh)
1229{
1230 struct _sas_device *sas_device;
1231 unsigned long flags;
1232 int rc = 0;
1233
1234 if (list_empty(&ioc->sas_device_list))
1235 return rc;
1236
1237 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1238 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1239 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1240 btdh->handle == sas_device->handle) {
1241 btdh->bus = sas_device->channel;
1242 btdh->id = sas_device->id;
1243 rc = 1;
1244 goto out;
1245 } else if (btdh->bus == sas_device->channel && btdh->id ==
1246 sas_device->id && btdh->handle == 0xFFFF) {
1247 btdh->handle = sas_device->handle;
1248 rc = 1;
1249 goto out;
1250 }
1251 }
1252 out:
1253 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1254 return rc;
1255}
1256
1257/**
1258 * _ctl_btdh_search_raid_device - searching for raid device
1259 * @ioc: per adapter object
1260 * @btdh: btdh ioctl payload
1261 */
1262static int
1263_ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1264 struct mpt2_ioctl_btdh_mapping *btdh)
1265{
1266 struct _raid_device *raid_device;
1267 unsigned long flags;
1268 int rc = 0;
1269
1270 if (list_empty(&ioc->raid_device_list))
1271 return rc;
1272
1273 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1274 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1275 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1276 btdh->handle == raid_device->handle) {
1277 btdh->bus = raid_device->channel;
1278 btdh->id = raid_device->id;
1279 rc = 1;
1280 goto out;
1281 } else if (btdh->bus == raid_device->channel && btdh->id ==
1282 raid_device->id && btdh->handle == 0xFFFF) {
1283 btdh->handle = raid_device->handle;
1284 rc = 1;
1285 goto out;
1286 }
1287 }
1288 out:
1289 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1290 return rc;
1291}
1292
1293/**
1294 * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1295 * @arg - user space buffer containing ioctl content
1296 */
1297static long
1298_ctl_btdh_mapping(void __user *arg)
1299{
1300 struct mpt2_ioctl_btdh_mapping karg;
1301 struct MPT2SAS_ADAPTER *ioc;
1302 int rc;
1303
1304 if (copy_from_user(&karg, arg, sizeof(karg))) {
1305 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1306 __FILE__, __LINE__, __func__);
1307 return -EFAULT;
1308 }
1309 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1310 return -ENODEV;
1311
eabb08ad 1312 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
635374e7
EM
1313 __func__));
1314
1315 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1316 if (!rc)
1317 _ctl_btdh_search_raid_device(ioc, &karg);
1318
1319 if (copy_to_user(arg, &karg, sizeof(karg))) {
1320 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1321 __FILE__, __LINE__, __func__);
1322 return -EFAULT;
1323 }
1324 return 0;
1325}
1326
1327/**
1328 * _ctl_diag_capability - return diag buffer capability
1329 * @ioc: per adapter object
1b01fe3a 1330 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
635374e7
EM
1331 *
1332 * returns 1 when diag buffer support is enabled in firmware
1333 */
1334static u8
1335_ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1336{
1337 u8 rc = 0;
1338
1339 switch (buffer_type) {
1340 case MPI2_DIAG_BUF_TYPE_TRACE:
1341 if (ioc->facts.IOCCapabilities &
1342 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1343 rc = 1;
1344 break;
1345 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1346 if (ioc->facts.IOCCapabilities &
1347 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1348 rc = 1;
1349 break;
1b01fe3a
KD
1350 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1351 if (ioc->facts.IOCCapabilities &
1352 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1353 rc = 1;
635374e7
EM
1354 }
1355
1356 return rc;
1357}
1358
1359/**
32e0eb56
KD
1360 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1361 * @ioc: per adapter object
1362 * @diag_register: the diag_register struct passed in from user space
635374e7 1363 *
635374e7
EM
1364 */
1365static long
32e0eb56
KD
1366_ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
1367 struct mpt2_diag_register *diag_register)
635374e7 1368{
635374e7
EM
1369 int rc, i;
1370 void *request_data = NULL;
1371 dma_addr_t request_data_dma;
1372 u32 request_data_sz = 0;
1373 Mpi2DiagBufferPostRequest_t *mpi_request;
1374 Mpi2DiagBufferPostReply_t *mpi_reply;
1375 u8 buffer_type;
1376 unsigned long timeleft;
1377 u16 smid;
1378 u16 ioc_status;
1379 u8 issue_reset = 0;
1380
eabb08ad 1381 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
635374e7
EM
1382 __func__));
1383
32e0eb56
KD
1384 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1385 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1386 ioc->name, __func__);
1387 rc = -EAGAIN;
1388 goto out;
1389 }
1390
1391 buffer_type = diag_register->buffer_type;
635374e7
EM
1392 if (!_ctl_diag_capability(ioc, buffer_type)) {
1393 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1394 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1395 return -EPERM;
1396 }
1397
1398 if (ioc->diag_buffer_status[buffer_type] &
1399 MPT2_DIAG_BUFFER_IS_REGISTERED) {
1400 printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1401 "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1402 buffer_type);
1403 return -EINVAL;
1404 }
1405
32e0eb56 1406 if (diag_register->requested_buffer_size % 4) {
635374e7
EM
1407 printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1408 "is not 4 byte aligned\n", ioc->name, __func__);
1409 return -EINVAL;
1410 }
1411
635374e7
EM
1412 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1413 if (!smid) {
1414 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1415 ioc->name, __func__);
1416 rc = -EAGAIN;
1417 goto out;
1418 }
1419
1420 rc = 0;
1421 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1422 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1423 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1424 ioc->ctl_cmds.smid = smid;
1425
1426 request_data = ioc->diag_buffer[buffer_type];
32e0eb56
KD
1427 request_data_sz = diag_register->requested_buffer_size;
1428 ioc->unique_id[buffer_type] = diag_register->unique_id;
635374e7 1429 ioc->diag_buffer_status[buffer_type] = 0;
32e0eb56
KD
1430 memcpy(ioc->product_specific[buffer_type],
1431 diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
1432 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
635374e7
EM
1433
1434 if (request_data) {
1435 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1436 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1437 pci_free_consistent(ioc->pdev,
1438 ioc->diag_buffer_sz[buffer_type],
1439 request_data, request_data_dma);
1440 request_data = NULL;
1441 }
1442 }
1443
1444 if (request_data == NULL) {
1445 ioc->diag_buffer_sz[buffer_type] = 0;
1446 ioc->diag_buffer_dma[buffer_type] = 0;
1447 request_data = pci_alloc_consistent(
1448 ioc->pdev, request_data_sz, &request_data_dma);
1449 if (request_data == NULL) {
1450 printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1451 " for diag buffers, requested size(%d)\n",
1452 ioc->name, __func__, request_data_sz);
1453 mpt2sas_base_free_smid(ioc, smid);
1454 return -ENOMEM;
1455 }
1456 ioc->diag_buffer[buffer_type] = request_data;
1457 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1458 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1459 }
1460
1461 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
32e0eb56
KD
1462 mpi_request->BufferType = diag_register->buffer_type;
1463 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
635374e7
EM
1464 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1465 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
7b936b02
KD
1466 mpi_request->VF_ID = 0; /* TODO */
1467 mpi_request->VP_ID = 0;
635374e7 1468
eabb08ad 1469 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(0x%p), "
635374e7 1470 "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
e94f6747
KD
1471 (unsigned long long)request_data_dma,
1472 le32_to_cpu(mpi_request->BufferLength)));
635374e7
EM
1473
1474 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1475 mpi_request->ProductSpecific[i] =
1476 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1477
bcfb6e6e 1478 init_completion(&ioc->ctl_cmds.done);
f01690d3 1479 mpt2sas_base_put_smid_default(ioc, smid);
635374e7
EM
1480 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1481 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1482
1483 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1484 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1485 __func__);
1486 _debug_dump_mf(mpi_request,
1487 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1488 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1489 issue_reset = 1;
1490 goto issue_host_reset;
1491 }
1492
1493 /* process the completed Reply Message Frame */
1494 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1495 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1496 ioc->name, __func__);
1497 rc = -EFAULT;
1498 goto out;
1499 }
1500
1501 mpi_reply = ioc->ctl_cmds.reply;
1502 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1503
1504 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1505 ioc->diag_buffer_status[buffer_type] |=
1506 MPT2_DIAG_BUFFER_IS_REGISTERED;
eabb08ad 1507 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
635374e7
EM
1508 ioc->name, __func__));
1509 } else {
eabb08ad 1510 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
635374e7 1511 "log_info(0x%08x)\n", ioc->name, __func__,
463217bf 1512 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
635374e7
EM
1513 rc = -EFAULT;
1514 }
1515
1516 issue_host_reset:
1517 if (issue_reset)
1518 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1519 FORCE_BIG_HAMMER);
1520
1521 out:
1522
1523 if (rc && request_data)
1524 pci_free_consistent(ioc->pdev, request_data_sz,
1525 request_data, request_data_dma);
1526
1527 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
32e0eb56
KD
1528 return rc;
1529}
1530
1531/**
1532 * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
1533 * @ioc: per adapter object
1534 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1535 *
1536 * This is called when command line option diag_buffer_enable is enabled
1537 * at driver load time.
1538 */
1539void
1540mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
1541{
1542 struct mpt2_diag_register diag_register;
1543
1544 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
1545
1546 if (bits_to_register & 1) {
1547 printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
1548 ioc->name);
1549 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1550 /* register for 1MB buffers */
1551 diag_register.requested_buffer_size = (1024 * 1024);
1552 diag_register.unique_id = 0x7075900;
1553 _ctl_diag_register_2(ioc, &diag_register);
1554 }
1555
1556 if (bits_to_register & 2) {
1557 printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
1558 ioc->name);
1559 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1560 /* register for 2MB buffers */
1561 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1562 diag_register.unique_id = 0x7075901;
1563 _ctl_diag_register_2(ioc, &diag_register);
1564 }
1b01fe3a
KD
1565
1566 if (bits_to_register & 4) {
1567 printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
1568 ioc->name);
1569 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1570 /* register for 2MB buffers */
1571 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1572 diag_register.unique_id = 0x7075901;
1573 _ctl_diag_register_2(ioc, &diag_register);
1574 }
32e0eb56
KD
1575}
1576
1577/**
1578 * _ctl_diag_register - application register with driver
1579 * @arg - user space buffer containing ioctl content
1580 * @state - NON_BLOCKING or BLOCKING
1581 *
1582 * This will allow the driver to setup any required buffers that will be
1583 * needed by firmware to communicate with the driver.
1584 */
1585static long
1586_ctl_diag_register(void __user *arg, enum block_state state)
1587{
1588 struct mpt2_diag_register karg;
1589 struct MPT2SAS_ADAPTER *ioc;
1590 long rc;
1591
1592 if (copy_from_user(&karg, arg, sizeof(karg))) {
1593 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1594 __FILE__, __LINE__, __func__);
1595 return -EFAULT;
1596 }
1597 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1598 return -ENODEV;
1599
1600 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1601 return -EAGAIN;
1602 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1603 return -ERESTARTSYS;
1604 rc = _ctl_diag_register_2(ioc, &karg);
635374e7
EM
1605 mutex_unlock(&ioc->ctl_cmds.mutex);
1606 return rc;
1607}
1608
1609/**
1610 * _ctl_diag_unregister - application unregister with driver
1611 * @arg - user space buffer containing ioctl content
1612 *
1613 * This will allow the driver to cleanup any memory allocated for diag
1614 * messages and to free up any resources.
1615 */
1616static long
1617_ctl_diag_unregister(void __user *arg)
1618{
1619 struct mpt2_diag_unregister karg;
1620 struct MPT2SAS_ADAPTER *ioc;
1621 void *request_data;
1622 dma_addr_t request_data_dma;
1623 u32 request_data_sz;
1624 u8 buffer_type;
1625
1626 if (copy_from_user(&karg, arg, sizeof(karg))) {
1627 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1628 __FILE__, __LINE__, __func__);
1629 return -EFAULT;
1630 }
1631 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1632 return -ENODEV;
1633
eabb08ad 1634 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
635374e7
EM
1635 __func__));
1636
1637 buffer_type = karg.unique_id & 0x000000ff;
1638 if (!_ctl_diag_capability(ioc, buffer_type)) {
1639 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1640 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1641 return -EPERM;
1642 }
1643
1644 if ((ioc->diag_buffer_status[buffer_type] &
1645 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1646 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1647 "registered\n", ioc->name, __func__, buffer_type);
1648 return -EINVAL;
1649 }
1650 if ((ioc->diag_buffer_status[buffer_type] &
1651 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1652 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1653 "released\n", ioc->name, __func__, buffer_type);
1654 return -EINVAL;
1655 }
1656
1657 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1658 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1659 "registered\n", ioc->name, __func__, karg.unique_id);
1660 return -EINVAL;
1661 }
1662
1663 request_data = ioc->diag_buffer[buffer_type];
1664 if (!request_data) {
1665 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1666 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1667 return -ENOMEM;
1668 }
1669
1670 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1671 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1672 pci_free_consistent(ioc->pdev, request_data_sz,
1673 request_data, request_data_dma);
1674 ioc->diag_buffer[buffer_type] = NULL;
1675 ioc->diag_buffer_status[buffer_type] = 0;
1676 return 0;
1677}
1678
1679/**
1680 * _ctl_diag_query - query relevant info associated with diag buffers
1681 * @arg - user space buffer containing ioctl content
1682 *
1683 * The application will send only buffer_type and unique_id. Driver will
1684 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1685 * 0x00, the driver will return info specified by Buffer Type.
1686 */
1687static long
1688_ctl_diag_query(void __user *arg)
1689{
1690 struct mpt2_diag_query karg;
1691 struct MPT2SAS_ADAPTER *ioc;
1692 void *request_data;
1693 int i;
1694 u8 buffer_type;
1695
1696 if (copy_from_user(&karg, arg, sizeof(karg))) {
1697 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1698 __FILE__, __LINE__, __func__);
1699 return -EFAULT;
1700 }
1701 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1702 return -ENODEV;
1703
eabb08ad 1704 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
635374e7
EM
1705 __func__));
1706
1707 karg.application_flags = 0;
1708 buffer_type = karg.buffer_type;
1709
1710 if (!_ctl_diag_capability(ioc, buffer_type)) {
1711 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1712 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1713 return -EPERM;
1714 }
1715
1716 if ((ioc->diag_buffer_status[buffer_type] &
1717 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1718 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1719 "registered\n", ioc->name, __func__, buffer_type);
1720 return -EINVAL;
1721 }
1722
1723 if (karg.unique_id & 0xffffff00) {
1724 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1725 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1726 "registered\n", ioc->name, __func__,
1727 karg.unique_id);
1728 return -EINVAL;
1729 }
1730 }
1731
1732 request_data = ioc->diag_buffer[buffer_type];
1733 if (!request_data) {
1734 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1735 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1736 return -ENOMEM;
1737 }
1738
1739 if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1740 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1741 MPT2_APP_FLAGS_BUFFER_VALID);
1742 else
1743 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1744 MPT2_APP_FLAGS_BUFFER_VALID |
1745 MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1746
1747 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1748 karg.product_specific[i] =
1749 ioc->product_specific[buffer_type][i];
1750
1751 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1752 karg.driver_added_buffer_size = 0;
1753 karg.unique_id = ioc->unique_id[buffer_type];
1754 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1755
1756 if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1757 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1758 "data @ %p\n", ioc->name, __func__, arg);
1759 return -EFAULT;
1760 }
1761 return 0;
1762}
1763
1764/**
99bb214b
EM
1765 * _ctl_send_release - Diag Release Message
1766 * @ioc: per adapter object
1b01fe3a 1767 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
99bb214b 1768 * @issue_reset - specifies whether host reset is required.
635374e7 1769 *
635374e7 1770 */
99bb214b
EM
1771static int
1772_ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
635374e7 1773{
635374e7
EM
1774 Mpi2DiagReleaseRequest_t *mpi_request;
1775 Mpi2DiagReleaseReply_t *mpi_reply;
635374e7
EM
1776 u16 smid;
1777 u16 ioc_status;
99bb214b
EM
1778 u32 ioc_state;
1779 int rc;
1780 unsigned long timeleft;
635374e7 1781
eabb08ad 1782 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
635374e7
EM
1783 __func__));
1784
99bb214b
EM
1785 rc = 0;
1786 *issue_reset = 0;
635374e7 1787
99bb214b
EM
1788 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1789 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
eabb08ad 1790 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
99bb214b
EM
1791 "skipping due to FAULT state\n", ioc->name,
1792 __func__));
1793 rc = -EAGAIN;
1794 goto out;
635374e7
EM
1795 }
1796
635374e7
EM
1797 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1798 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1799 ioc->name, __func__);
1800 rc = -EAGAIN;
1801 goto out;
1802 }
1803
1804 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1805 if (!smid) {
1806 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1807 ioc->name, __func__);
1808 rc = -EAGAIN;
1809 goto out;
1810 }
1811
635374e7
EM
1812 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1813 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1814 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1815 ioc->ctl_cmds.smid = smid;
1816
1817 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1818 mpi_request->BufferType = buffer_type;
7b936b02
KD
1819 mpi_request->VF_ID = 0; /* TODO */
1820 mpi_request->VP_ID = 0;
635374e7 1821
bcfb6e6e 1822 init_completion(&ioc->ctl_cmds.done);
f01690d3 1823 mpt2sas_base_put_smid_default(ioc, smid);
635374e7
EM
1824 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1825 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1826
1827 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1828 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1829 __func__);
1830 _debug_dump_mf(mpi_request,
1831 sizeof(Mpi2DiagReleaseRequest_t)/4);
1832 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
99bb214b
EM
1833 *issue_reset = 1;
1834 rc = -EFAULT;
1835 goto out;
635374e7
EM
1836 }
1837
1838 /* process the completed Reply Message Frame */
1839 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1840 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1841 ioc->name, __func__);
1842 rc = -EFAULT;
1843 goto out;
1844 }
1845
1846 mpi_reply = ioc->ctl_cmds.reply;
1847 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1848
1849 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1850 ioc->diag_buffer_status[buffer_type] |=
1851 MPT2_DIAG_BUFFER_IS_RELEASED;
eabb08ad 1852 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
635374e7
EM
1853 ioc->name, __func__));
1854 } else {
eabb08ad 1855 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
635374e7 1856 "log_info(0x%08x)\n", ioc->name, __func__,
463217bf 1857 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
635374e7
EM
1858 rc = -EFAULT;
1859 }
1860
99bb214b
EM
1861 out:
1862 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1863 return rc;
1864}
1865
1866/**
1867 * _ctl_diag_release - request to send Diag Release Message to firmware
1868 * @arg - user space buffer containing ioctl content
1869 * @state - NON_BLOCKING or BLOCKING
1870 *
1871 * This allows ownership of the specified buffer to returned to the driver,
1872 * allowing an application to read the buffer without fear that firmware is
1873 * overwritting information in the buffer.
1874 */
1875static long
1876_ctl_diag_release(void __user *arg, enum block_state state)
1877{
1878 struct mpt2_diag_release karg;
1879 struct MPT2SAS_ADAPTER *ioc;
1880 void *request_data;
1881 int rc;
1882 u8 buffer_type;
1883 u8 issue_reset = 0;
1884
1885 if (copy_from_user(&karg, arg, sizeof(karg))) {
1886 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1887 __FILE__, __LINE__, __func__);
1888 return -EFAULT;
1889 }
1890 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1891 return -ENODEV;
1892
eabb08ad 1893 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
99bb214b
EM
1894 __func__));
1895
1896 buffer_type = karg.unique_id & 0x000000ff;
1897 if (!_ctl_diag_capability(ioc, buffer_type)) {
1898 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1899 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1900 return -EPERM;
1901 }
1902
1903 if ((ioc->diag_buffer_status[buffer_type] &
1904 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1905 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1906 "registered\n", ioc->name, __func__, buffer_type);
1907 return -EINVAL;
1908 }
1909
1910 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1911 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1912 "registered\n", ioc->name, __func__, karg.unique_id);
1913 return -EINVAL;
1914 }
1915
1916 if (ioc->diag_buffer_status[buffer_type] &
1917 MPT2_DIAG_BUFFER_IS_RELEASED) {
1918 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1919 "is already released\n", ioc->name, __func__,
1920 buffer_type);
1921 return 0;
1922 }
1923
1924 request_data = ioc->diag_buffer[buffer_type];
1925
1926 if (!request_data) {
1927 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1928 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1929 return -ENOMEM;
1930 }
1931
1932 /* buffers were released by due to host reset */
1933 if ((ioc->diag_buffer_status[buffer_type] &
1934 MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1935 ioc->diag_buffer_status[buffer_type] |=
1936 MPT2_DIAG_BUFFER_IS_RELEASED;
1937 ioc->diag_buffer_status[buffer_type] &=
1938 ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1939 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1940 "was released due to host reset\n", ioc->name, __func__,
1941 buffer_type);
1942 return 0;
1943 }
1944
1945 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1946 return -EAGAIN;
1947 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1948 return -ERESTARTSYS;
1949
1950 rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1951
635374e7
EM
1952 if (issue_reset)
1953 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1954 FORCE_BIG_HAMMER);
1955
635374e7
EM
1956 mutex_unlock(&ioc->ctl_cmds.mutex);
1957 return rc;
1958}
1959
1960/**
1961 * _ctl_diag_read_buffer - request for copy of the diag buffer
1962 * @arg - user space buffer containing ioctl content
1963 * @state - NON_BLOCKING or BLOCKING
1964 */
1965static long
1966_ctl_diag_read_buffer(void __user *arg, enum block_state state)
1967{
1968 struct mpt2_diag_read_buffer karg;
1969 struct mpt2_diag_read_buffer __user *uarg = arg;
1970 struct MPT2SAS_ADAPTER *ioc;
1971 void *request_data, *diag_data;
1972 Mpi2DiagBufferPostRequest_t *mpi_request;
1973 Mpi2DiagBufferPostReply_t *mpi_reply;
1974 int rc, i;
1975 u8 buffer_type;
a1f74ae8 1976 unsigned long timeleft, request_size, copy_size;
635374e7
EM
1977 u16 smid;
1978 u16 ioc_status;
1979 u8 issue_reset = 0;
1980
1981 if (copy_from_user(&karg, arg, sizeof(karg))) {
1982 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1983 __FILE__, __LINE__, __func__);
1984 return -EFAULT;
1985 }
1986 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1987 return -ENODEV;
1988
eabb08ad 1989 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
635374e7
EM
1990 __func__));
1991
1992 buffer_type = karg.unique_id & 0x000000ff;
1993 if (!_ctl_diag_capability(ioc, buffer_type)) {
1994 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1995 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1996 return -EPERM;
1997 }
1998
1999 if (karg.unique_id != ioc->unique_id[buffer_type]) {
2000 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
2001 "registered\n", ioc->name, __func__, karg.unique_id);
2002 return -EINVAL;
2003 }
2004
2005 request_data = ioc->diag_buffer[buffer_type];
2006 if (!request_data) {
2007 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
2008 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
2009 return -ENOMEM;
2010 }
2011
a1f74ae8
DR
2012 request_size = ioc->diag_buffer_sz[buffer_type];
2013
635374e7
EM
2014 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2015 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
2016 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
2017 __func__);
2018 return -EINVAL;
2019 }
2020
a1f74ae8
DR
2021 if (karg.starting_offset > request_size)
2022 return -EINVAL;
2023
635374e7 2024 diag_data = (void *)(request_data + karg.starting_offset);
eabb08ad 2025 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
635374e7
EM
2026 "offset(%d), sz(%d)\n", ioc->name, __func__,
2027 diag_data, karg.starting_offset, karg.bytes_to_read));
2028
a1f74ae8
DR
2029 /* Truncate data on requests that are too large */
2030 if ((diag_data + karg.bytes_to_read < diag_data) ||
2031 (diag_data + karg.bytes_to_read > request_data + request_size))
2032 copy_size = request_size - karg.starting_offset;
2033 else
2034 copy_size = karg.bytes_to_read;
2035
635374e7 2036 if (copy_to_user((void __user *)uarg->diagnostic_data,
a1f74ae8 2037 diag_data, copy_size)) {
635374e7
EM
2038 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
2039 "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
2040 __func__, diag_data);
2041 return -EFAULT;
2042 }
2043
2044 if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
2045 return 0;
2046
eabb08ad 2047 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
635374e7
EM
2048 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
2049 if ((ioc->diag_buffer_status[buffer_type] &
2050 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
eabb08ad 2051 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
635374e7
EM
2052 "buffer_type(0x%02x) is still registered\n", ioc->name,
2053 __func__, buffer_type));
2054 return 0;
2055 }
2056 /* Get a free request frame and save the message context.
2057 */
2058 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
2059 return -EAGAIN;
2060 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
2061 return -ERESTARTSYS;
2062
2063 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
2064 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
2065 ioc->name, __func__);
2066 rc = -EAGAIN;
2067 goto out;
2068 }
2069
2070 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2071 if (!smid) {
2072 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2073 ioc->name, __func__);
2074 rc = -EAGAIN;
2075 goto out;
2076 }
2077
2078 rc = 0;
2079 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
2080 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2081 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2082 ioc->ctl_cmds.smid = smid;
2083
2084 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2085 mpi_request->BufferType = buffer_type;
2086 mpi_request->BufferLength =
2087 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2088 mpi_request->BufferAddress =
2089 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2090 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
2091 mpi_request->ProductSpecific[i] =
2092 cpu_to_le32(ioc->product_specific[buffer_type][i]);
7b936b02
KD
2093 mpi_request->VF_ID = 0; /* TODO */
2094 mpi_request->VP_ID = 0;
635374e7 2095
bcfb6e6e 2096 init_completion(&ioc->ctl_cmds.done);
f01690d3 2097 mpt2sas_base_put_smid_default(ioc, smid);
635374e7
EM
2098 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2099 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
2100
2101 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
2102 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
2103 __func__);
2104 _debug_dump_mf(mpi_request,
2105 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2106 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
2107 issue_reset = 1;
2108 goto issue_host_reset;
2109 }
2110
2111 /* process the completed Reply Message Frame */
2112 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
2113 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
2114 ioc->name, __func__);
2115 rc = -EFAULT;
2116 goto out;
2117 }
2118
2119 mpi_reply = ioc->ctl_cmds.reply;
2120 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2121
2122 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2123 ioc->diag_buffer_status[buffer_type] |=
2124 MPT2_DIAG_BUFFER_IS_REGISTERED;
eabb08ad 2125 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
635374e7
EM
2126 ioc->name, __func__));
2127 } else {
eabb08ad 2128 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
635374e7 2129 "log_info(0x%08x)\n", ioc->name, __func__,
463217bf 2130 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
635374e7
EM
2131 rc = -EFAULT;
2132 }
2133
2134 issue_host_reset:
2135 if (issue_reset)
2136 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2137 FORCE_BIG_HAMMER);
2138
2139 out:
2140
2141 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
2142 mutex_unlock(&ioc->ctl_cmds.mutex);
2143 return rc;
2144}
2145
2146/**
2147 * _ctl_ioctl_main - main ioctl entry point
2148 * @file - (struct file)
2149 * @cmd - ioctl opcode
2150 * @arg -
2151 */
2152static long
2153_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg)
2154{
2155 enum block_state state;
2156 long ret = -EINVAL;
635374e7
EM
2157
2158 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING :
2159 BLOCKING;
2160
2161 switch (cmd) {
2162 case MPT2IOCINFO:
2163 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
2164 ret = _ctl_getiocinfo(arg);
2165 break;
2166 case MPT2COMMAND:
2167 {
2168 struct mpt2_ioctl_command karg;
2169 struct mpt2_ioctl_command __user *uarg;
2170 struct MPT2SAS_ADAPTER *ioc;
2171
2172 if (copy_from_user(&karg, arg, sizeof(karg))) {
2173 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2174 __FILE__, __LINE__, __func__);
2175 return -EFAULT;
2176 }
2177
2178 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2179 !ioc)
2180 return -ENODEV;
2181
921cd802 2182 if (ioc->shost_recovery || ioc->pci_error_recovery ||
2183 ioc->is_driver_loading)
635374e7 2184 return -EAGAIN;
635374e7
EM
2185
2186 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2187 uarg = arg;
2188 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2189 }
2190 break;
2191 }
2192 case MPT2EVENTQUERY:
2193 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2194 ret = _ctl_eventquery(arg);
2195 break;
2196 case MPT2EVENTENABLE:
2197 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2198 ret = _ctl_eventenable(arg);
2199 break;
2200 case MPT2EVENTREPORT:
2201 ret = _ctl_eventreport(arg);
2202 break;
2203 case MPT2HARDRESET:
2204 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2205 ret = _ctl_do_reset(arg);
2206 break;
2207 case MPT2BTDHMAPPING:
2208 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2209 ret = _ctl_btdh_mapping(arg);
2210 break;
2211 case MPT2DIAGREGISTER:
2212 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2213 ret = _ctl_diag_register(arg, state);
2214 break;
2215 case MPT2DIAGUNREGISTER:
2216 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2217 ret = _ctl_diag_unregister(arg);
2218 break;
2219 case MPT2DIAGQUERY:
2220 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2221 ret = _ctl_diag_query(arg);
2222 break;
2223 case MPT2DIAGRELEASE:
2224 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2225 ret = _ctl_diag_release(arg, state);
2226 break;
2227 case MPT2DIAGREADBUFFER:
2228 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2229 ret = _ctl_diag_read_buffer(arg, state);
2230 break;
2231 default:
2232 {
2233 struct mpt2_ioctl_command karg;
2234 struct MPT2SAS_ADAPTER *ioc;
2235
2236 if (copy_from_user(&karg, arg, sizeof(karg))) {
2237 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2238 __FILE__, __LINE__, __func__);
2239 return -EFAULT;
2240 }
2241
2242 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2243 !ioc)
2244 return -ENODEV;
2245
eabb08ad 2246 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
635374e7
EM
2247 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2248 break;
2249 }
2250 }
2251 return ret;
2252}
2253
2254/**
2255 * _ctl_ioctl - main ioctl entry point (unlocked)
2256 * @file - (struct file)
2257 * @cmd - ioctl opcode
2258 * @arg -
2259 */
2260static long
2261_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2262{
2263 long ret;
7b936b02 2264
c45d15d2 2265 mutex_lock(&_ctl_mutex);
635374e7 2266 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
c45d15d2 2267 mutex_unlock(&_ctl_mutex);
635374e7
EM
2268 return ret;
2269}
2270
2271#ifdef CONFIG_COMPAT
2272/**
2273 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2274 * @file - (struct file)
2275 * @cmd - ioctl opcode
2276 * @arg - (struct mpt2_ioctl_command32)
2277 *
2278 * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2279 */
2280static long
2281_ctl_compat_mpt_command(struct file *file, unsigned cmd, unsigned long arg)
2282{
2283 struct mpt2_ioctl_command32 karg32;
2284 struct mpt2_ioctl_command32 __user *uarg;
2285 struct mpt2_ioctl_command karg;
2286 struct MPT2SAS_ADAPTER *ioc;
2287 enum block_state state;
635374e7
EM
2288
2289 if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2290 return -EINVAL;
2291
2292 uarg = (struct mpt2_ioctl_command32 __user *) arg;
2293
2294 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2295 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2296 __FILE__, __LINE__, __func__);
2297 return -EFAULT;
2298 }
2299 if (_ctl_verify_adapter(karg32.hdr.ioc_number, &ioc) == -1 || !ioc)
2300 return -ENODEV;
2301
921cd802 2302 if (ioc->shost_recovery || ioc->pci_error_recovery ||
2303 ioc->is_driver_loading)
635374e7 2304 return -EAGAIN;
635374e7
EM
2305
2306 memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2307 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2308 karg.hdr.port_number = karg32.hdr.port_number;
2309 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2310 karg.timeout = karg32.timeout;
2311 karg.max_reply_bytes = karg32.max_reply_bytes;
2312 karg.data_in_size = karg32.data_in_size;
2313 karg.data_out_size = karg32.data_out_size;
2314 karg.max_sense_bytes = karg32.max_sense_bytes;
2315 karg.data_sge_offset = karg32.data_sge_offset;
22c88425
KD
2316 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2317 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2318 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2319 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
635374e7
EM
2320 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2321 return _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2322}
2323
2324/**
2325 * _ctl_ioctl_compat - main ioctl entry point (compat)
2326 * @file -
2327 * @cmd -
2328 * @arg -
2329 *
2330 * This routine handles 32 bit applications in 64bit os.
2331 */
2332static long
2333_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2334{
2335 long ret;
7b936b02 2336
c45d15d2 2337 mutex_lock(&_ctl_mutex);
635374e7
EM
2338 if (cmd == MPT2COMMAND32)
2339 ret = _ctl_compat_mpt_command(file, cmd, arg);
2340 else
2341 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
c45d15d2 2342 mutex_unlock(&_ctl_mutex);
635374e7
EM
2343 return ret;
2344}
2345#endif
2346
2347/* scsi host attributes */
2348
2349/**
2350 * _ctl_version_fw_show - firmware version
2351 * @cdev - pointer to embedded class device
2352 * @buf - the buffer returned
2353 *
2354 * A sysfs 'read-only' shost attribute.
2355 */
2356static ssize_t
2357_ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2358 char *buf)
2359{
2360 struct Scsi_Host *shost = class_to_shost(cdev);
2361 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2362
2363 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2364 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2365 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2366 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2367 ioc->facts.FWVersion.Word & 0x000000FF);
2368}
2369static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2370
2371/**
2372 * _ctl_version_bios_show - bios version
2373 * @cdev - pointer to embedded class device
2374 * @buf - the buffer returned
2375 *
2376 * A sysfs 'read-only' shost attribute.
2377 */
2378static ssize_t
2379_ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2380 char *buf)
2381{
2382 struct Scsi_Host *shost = class_to_shost(cdev);
2383 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2384
2385 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2386
2387 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2388 (version & 0xFF000000) >> 24,
2389 (version & 0x00FF0000) >> 16,
2390 (version & 0x0000FF00) >> 8,
2391 version & 0x000000FF);
2392}
2393static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2394
2395/**
2396 * _ctl_version_mpi_show - MPI (message passing interface) version
2397 * @cdev - pointer to embedded class device
2398 * @buf - the buffer returned
2399 *
2400 * A sysfs 'read-only' shost attribute.
2401 */
2402static ssize_t
2403_ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2404 char *buf)
2405{
2406 struct Scsi_Host *shost = class_to_shost(cdev);
2407 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2408
2409 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2410 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2411}
2412static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2413
2414/**
2415 * _ctl_version_product_show - product name
2416 * @cdev - pointer to embedded class device
2417 * @buf - the buffer returned
2418 *
2419 * A sysfs 'read-only' shost attribute.
2420 */
2421static ssize_t
2422_ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2423 char *buf)
2424{
2425 struct Scsi_Host *shost = class_to_shost(cdev);
2426 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2427
2428 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2429}
2430static DEVICE_ATTR(version_product, S_IRUGO,
2431 _ctl_version_product_show, NULL);
2432
2433/**
2434 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2435 * @cdev - pointer to embedded class device
2436 * @buf - the buffer returned
2437 *
2438 * A sysfs 'read-only' shost attribute.
2439 */
2440static ssize_t
2441_ctl_version_nvdata_persistent_show(struct device *cdev,
2442 struct device_attribute *attr, char *buf)
2443{
2444 struct Scsi_Host *shost = class_to_shost(cdev);
2445 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2446
e94f6747
KD
2447 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2448 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
635374e7
EM
2449}
2450static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2451 _ctl_version_nvdata_persistent_show, NULL);
2452
2453/**
2454 * _ctl_version_nvdata_default_show - nvdata default version
2455 * @cdev - pointer to embedded class device
2456 * @buf - the buffer returned
2457 *
2458 * A sysfs 'read-only' shost attribute.
2459 */
2460static ssize_t
2461_ctl_version_nvdata_default_show(struct device *cdev,
2462 struct device_attribute *attr, char *buf)
2463{
2464 struct Scsi_Host *shost = class_to_shost(cdev);
2465 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2466
e94f6747
KD
2467 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2468 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
635374e7
EM
2469}
2470static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2471 _ctl_version_nvdata_default_show, NULL);
2472
2473/**
2474 * _ctl_board_name_show - board name
2475 * @cdev - pointer to embedded class device
2476 * @buf - the buffer returned
2477 *
2478 * A sysfs 'read-only' shost attribute.
2479 */
2480static ssize_t
2481_ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2482 char *buf)
2483{
2484 struct Scsi_Host *shost = class_to_shost(cdev);
2485 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2486
2487 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2488}
2489static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2490
2491/**
2492 * _ctl_board_assembly_show - board assembly name
2493 * @cdev - pointer to embedded class device
2494 * @buf - the buffer returned
2495 *
2496 * A sysfs 'read-only' shost attribute.
2497 */
2498static ssize_t
2499_ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2500 char *buf)
2501{
2502 struct Scsi_Host *shost = class_to_shost(cdev);
2503 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2504
2505 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2506}
2507static DEVICE_ATTR(board_assembly, S_IRUGO,
2508 _ctl_board_assembly_show, NULL);
2509
2510/**
2511 * _ctl_board_tracer_show - board tracer number
2512 * @cdev - pointer to embedded class device
2513 * @buf - the buffer returned
2514 *
2515 * A sysfs 'read-only' shost attribute.
2516 */
2517static ssize_t
2518_ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2519 char *buf)
2520{
2521 struct Scsi_Host *shost = class_to_shost(cdev);
2522 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2523
2524 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2525}
2526static DEVICE_ATTR(board_tracer, S_IRUGO,
2527 _ctl_board_tracer_show, NULL);
2528
2529/**
2530 * _ctl_io_delay_show - io missing delay
2531 * @cdev - pointer to embedded class device
2532 * @buf - the buffer returned
2533 *
2534 * This is for firmware implemention for deboucing device
2535 * removal events.
2536 *
2537 * A sysfs 'read-only' shost attribute.
2538 */
2539static ssize_t
2540_ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2541 char *buf)
2542{
2543 struct Scsi_Host *shost = class_to_shost(cdev);
2544 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2545
2546 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2547}
2548static DEVICE_ATTR(io_delay, S_IRUGO,
2549 _ctl_io_delay_show, NULL);
2550
2551/**
2552 * _ctl_device_delay_show - device missing delay
2553 * @cdev - pointer to embedded class device
2554 * @buf - the buffer returned
2555 *
2556 * This is for firmware implemention for deboucing device
2557 * removal events.
2558 *
2559 * A sysfs 'read-only' shost attribute.
2560 */
2561static ssize_t
2562_ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2563 char *buf)
2564{
2565 struct Scsi_Host *shost = class_to_shost(cdev);
2566 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2567
2568 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2569}
2570static DEVICE_ATTR(device_delay, S_IRUGO,
2571 _ctl_device_delay_show, NULL);
2572
2573/**
2574 * _ctl_fw_queue_depth_show - global credits
2575 * @cdev - pointer to embedded class device
2576 * @buf - the buffer returned
2577 *
2578 * This is firmware queue depth limit
2579 *
2580 * A sysfs 'read-only' shost attribute.
2581 */
2582static ssize_t
2583_ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2584 char *buf)
2585{
2586 struct Scsi_Host *shost = class_to_shost(cdev);
2587 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2588
2589 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2590}
2591static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2592 _ctl_fw_queue_depth_show, NULL);
2593
2594/**
2595 * _ctl_sas_address_show - sas address
2596 * @cdev - pointer to embedded class device
2597 * @buf - the buffer returned
2598 *
2599 * This is the controller sas address
2600 *
2601 * A sysfs 'read-only' shost attribute.
2602 */
2603static ssize_t
2604_ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2605 char *buf)
2606{
2607 struct Scsi_Host *shost = class_to_shost(cdev);
2608 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2609
2610 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2611 (unsigned long long)ioc->sas_hba.sas_address);
2612}
2613static DEVICE_ATTR(host_sas_address, S_IRUGO,
2614 _ctl_host_sas_address_show, NULL);
2615
2616/**
2617 * _ctl_logging_level_show - logging level
2618 * @cdev - pointer to embedded class device
2619 * @buf - the buffer returned
2620 *
2621 * A sysfs 'read/write' shost attribute.
2622 */
2623static ssize_t
2624_ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2625 char *buf)
2626{
2627 struct Scsi_Host *shost = class_to_shost(cdev);
2628 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2629
2630 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2631}
2632static ssize_t
2633_ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2634 const char *buf, size_t count)
2635{
2636 struct Scsi_Host *shost = class_to_shost(cdev);
2637 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2638 int val = 0;
2639
2640 if (sscanf(buf, "%x", &val) != 1)
2641 return -EINVAL;
2642
2643 ioc->logging_level = val;
2644 printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2645 ioc->logging_level);
2646 return strlen(buf);
2647}
2648static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2649 _ctl_logging_level_show, _ctl_logging_level_store);
2650
fa7f3167
KD
2651/* device attributes */
2652/*
2653 * _ctl_fwfault_debug_show - show/store fwfault_debug
2654 * @cdev - pointer to embedded class device
2655 * @buf - the buffer returned
2656 *
2657 * mpt2sas_fwfault_debug is command line option
2658 * A sysfs 'read/write' shost attribute.
2659 */
2660static ssize_t
2661_ctl_fwfault_debug_show(struct device *cdev,
2662 struct device_attribute *attr, char *buf)
2663{
2664 struct Scsi_Host *shost = class_to_shost(cdev);
2665 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2666
2667 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2668}
2669static ssize_t
2670_ctl_fwfault_debug_store(struct device *cdev,
2671 struct device_attribute *attr, const char *buf, size_t count)
2672{
2673 struct Scsi_Host *shost = class_to_shost(cdev);
2674 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2675 int val = 0;
2676
2677 if (sscanf(buf, "%d", &val) != 1)
2678 return -EINVAL;
2679
2680 ioc->fwfault_debug = val;
2681 printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
2682 ioc->fwfault_debug);
2683 return strlen(buf);
2684}
2685static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2686 _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2687
d32a8c15
KD
2688
2689/**
2690 * _ctl_ioc_reset_count_show - ioc reset count
2691 * @cdev - pointer to embedded class device
2692 * @buf - the buffer returned
2693 *
2694 * This is firmware queue depth limit
2695 *
2696 * A sysfs 'read-only' shost attribute.
2697 */
2698static ssize_t
2699_ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2700 char *buf)
2701{
2702 struct Scsi_Host *shost = class_to_shost(cdev);
2703 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2704
2705 return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
2706}
2707static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
2708 _ctl_ioc_reset_count_show, NULL);
2709
911ae943 2710/**
2711 * _ctl_ioc_reply_queue_count_show - number of reply queues
2712 * @cdev - pointer to embedded class device
2713 * @buf - the buffer returned
2714 *
2715 * This is number of reply queues
2716 *
2717 * A sysfs 'read-only' shost attribute.
2718 */
2719static ssize_t
2720_ctl_ioc_reply_queue_count_show(struct device *cdev,
2721 struct device_attribute *attr, char *buf)
2722{
2723 u8 reply_queue_count;
2724 struct Scsi_Host *shost = class_to_shost(cdev);
2725 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2726
2727 if ((ioc->facts.IOCCapabilities &
2728 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2729 reply_queue_count = ioc->reply_queue_count;
2730 else
2731 reply_queue_count = 1;
2732 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2733}
2734static DEVICE_ATTR(reply_queue_count, S_IRUGO,
2735 _ctl_ioc_reply_queue_count_show, NULL);
2736
570c67ac 2737struct DIAG_BUFFER_START {
c97951ec
KD
2738 __le32 Size;
2739 __le32 DiagVersion;
570c67ac
KD
2740 u8 BufferType;
2741 u8 Reserved[3];
c97951ec
KD
2742 __le32 Reserved1;
2743 __le32 Reserved2;
2744 __le32 Reserved3;
570c67ac
KD
2745};
2746/**
2747 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2748 * @cdev - pointer to embedded class device
2749 * @buf - the buffer returned
2750 *
2751 * A sysfs 'read-only' shost attribute.
2752 */
2753static ssize_t
2754_ctl_host_trace_buffer_size_show(struct device *cdev,
2755 struct device_attribute *attr, char *buf)
2756{
2757 struct Scsi_Host *shost = class_to_shost(cdev);
2758 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2759 u32 size = 0;
2760 struct DIAG_BUFFER_START *request_data;
2761
2762 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2763 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2764 "registered\n", ioc->name, __func__);
2765 return 0;
2766 }
2767
2768 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2769 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2770 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2771 "registered\n", ioc->name, __func__);
2772 return 0;
2773 }
2774
2775 request_data = (struct DIAG_BUFFER_START *)
2776 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2777 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2778 le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
2779 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2780 size = le32_to_cpu(request_data->Size);
2781
2782 ioc->ring_buffer_sz = size;
2783 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2784}
2785static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2786 _ctl_host_trace_buffer_size_show, NULL);
2787
2788/**
2789 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2790 * @cdev - pointer to embedded class device
2791 * @buf - the buffer returned
2792 *
2793 * A sysfs 'read/write' shost attribute.
2794 *
2795 * You will only be able to read 4k bytes of ring buffer at a time.
2796 * In order to read beyond 4k bytes, you will have to write out the
2797 * offset to the same attribute, it will move the pointer.
2798 */
2799static ssize_t
2800_ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2801 char *buf)
2802{
2803 struct Scsi_Host *shost = class_to_shost(cdev);
2804 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2805 void *request_data;
2806 u32 size;
2807
2808 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2809 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2810 "registered\n", ioc->name, __func__);
2811 return 0;
2812 }
2813
2814 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2815 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2816 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2817 "registered\n", ioc->name, __func__);
2818 return 0;
2819 }
2820
2821 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2822 return 0;
2823
2824 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2825 size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
2826 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2827 memcpy(buf, request_data, size);
2828 return size;
2829}
2830
2831static ssize_t
2832_ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2833 const char *buf, size_t count)
2834{
2835 struct Scsi_Host *shost = class_to_shost(cdev);
2836 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2837 int val = 0;
2838
2839 if (sscanf(buf, "%d", &val) != 1)
2840 return -EINVAL;
2841
2842 ioc->ring_buffer_offset = val;
2843 return strlen(buf);
2844}
2845static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2846 _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2847
2848/*****************************************/
2849
2850/**
2851 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2852 * @cdev - pointer to embedded class device
2853 * @buf - the buffer returned
2854 *
2855 * A sysfs 'read/write' shost attribute.
2856 *
2857 * This is a mechnism to post/release host_trace_buffers
2858 */
2859static ssize_t
2860_ctl_host_trace_buffer_enable_show(struct device *cdev,
2861 struct device_attribute *attr, char *buf)
2862{
2863 struct Scsi_Host *shost = class_to_shost(cdev);
2864 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2865
2866 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
2867 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2868 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
2869 return snprintf(buf, PAGE_SIZE, "off\n");
2870 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2871 MPT2_DIAG_BUFFER_IS_RELEASED))
2872 return snprintf(buf, PAGE_SIZE, "release\n");
2873 else
2874 return snprintf(buf, PAGE_SIZE, "post\n");
2875}
2876
2877static ssize_t
2878_ctl_host_trace_buffer_enable_store(struct device *cdev,
2879 struct device_attribute *attr, const char *buf, size_t count)
2880{
2881 struct Scsi_Host *shost = class_to_shost(cdev);
2882 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2883 char str[10] = "";
2884 struct mpt2_diag_register diag_register;
2885 u8 issue_reset = 0;
2886
2887 if (sscanf(buf, "%s", str) != 1)
2888 return -EINVAL;
2889
2890 if (!strcmp(str, "post")) {
2891 /* exit out if host buffers are already posted */
2892 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
2893 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2894 MPT2_DIAG_BUFFER_IS_REGISTERED) &&
2895 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2896 MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
2897 goto out;
2898 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
2899 printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
2900 ioc->name);
2901 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
2902 diag_register.requested_buffer_size = (1024 * 1024);
2903 diag_register.unique_id = 0x7075900;
2904 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
2905 _ctl_diag_register_2(ioc, &diag_register);
2906 } else if (!strcmp(str, "release")) {
2907 /* exit out if host buffers are already released */
2908 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
2909 goto out;
2910 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2911 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
2912 goto out;
2913 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2914 MPT2_DIAG_BUFFER_IS_RELEASED))
2915 goto out;
2916 printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
2917 ioc->name);
2918 _ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
2919 }
2920
2921 out:
2922 return strlen(buf);
2923}
2924static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
2925 _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
d32a8c15 2926
635374e7
EM
2927struct device_attribute *mpt2sas_host_attrs[] = {
2928 &dev_attr_version_fw,
2929 &dev_attr_version_bios,
2930 &dev_attr_version_mpi,
2931 &dev_attr_version_product,
2932 &dev_attr_version_nvdata_persistent,
2933 &dev_attr_version_nvdata_default,
2934 &dev_attr_board_name,
2935 &dev_attr_board_assembly,
2936 &dev_attr_board_tracer,
2937 &dev_attr_io_delay,
2938 &dev_attr_device_delay,
2939 &dev_attr_logging_level,
fa7f3167 2940 &dev_attr_fwfault_debug,
635374e7
EM
2941 &dev_attr_fw_queue_depth,
2942 &dev_attr_host_sas_address,
d32a8c15 2943 &dev_attr_ioc_reset_count,
570c67ac
KD
2944 &dev_attr_host_trace_buffer_size,
2945 &dev_attr_host_trace_buffer,
2946 &dev_attr_host_trace_buffer_enable,
911ae943 2947 &dev_attr_reply_queue_count,
635374e7
EM
2948 NULL,
2949};
2950
635374e7
EM
2951/**
2952 * _ctl_device_sas_address_show - sas address
2953 * @cdev - pointer to embedded class device
2954 * @buf - the buffer returned
2955 *
2956 * This is the sas address for the target
2957 *
2958 * A sysfs 'read-only' shost attribute.
2959 */
2960static ssize_t
2961_ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2962 char *buf)
2963{
2964 struct scsi_device *sdev = to_scsi_device(dev);
2965 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2966
2967 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2968 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2969}
2970static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2971
2972/**
2973 * _ctl_device_handle_show - device handle
2974 * @cdev - pointer to embedded class device
2975 * @buf - the buffer returned
2976 *
2977 * This is the firmware assigned device handle
2978 *
2979 * A sysfs 'read-only' shost attribute.
2980 */
2981static ssize_t
2982_ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
2983 char *buf)
2984{
2985 struct scsi_device *sdev = to_scsi_device(dev);
2986 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2987
2988 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
2989 sas_device_priv_data->sas_target->handle);
2990}
2991static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
2992
2993struct device_attribute *mpt2sas_dev_attrs[] = {
2994 &dev_attr_sas_address,
2995 &dev_attr_sas_device_handle,
2996 NULL,
2997};
2998
2999static const struct file_operations ctl_fops = {
3000 .owner = THIS_MODULE,
3001 .unlocked_ioctl = _ctl_ioctl,
3002 .release = _ctl_release,
3003 .poll = _ctl_poll,
3004 .fasync = _ctl_fasync,
3005#ifdef CONFIG_COMPAT
3006 .compat_ioctl = _ctl_ioctl_compat,
3007#endif
6038f373 3008 .llseek = noop_llseek,
635374e7
EM
3009};
3010
3011static struct miscdevice ctl_dev = {
3012 .minor = MPT2SAS_MINOR,
3013 .name = MPT2SAS_DEV_NAME,
3014 .fops = &ctl_fops,
3015};
3016
3017/**
3018 * mpt2sas_ctl_init - main entry point for ctl.
3019 *
3020 */
3021void
3022mpt2sas_ctl_init(void)
3023{
3024 async_queue = NULL;
3025 if (misc_register(&ctl_dev) < 0)
3026 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
3027 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3028
3029 init_waitqueue_head(&ctl_poll_wait);
3030}
3031
3032/**
3033 * mpt2sas_ctl_exit - exit point for ctl
3034 *
3035 */
3036void
3037mpt2sas_ctl_exit(void)
3038{
3039 struct MPT2SAS_ADAPTER *ioc;
3040 int i;
3041
ba33fadf 3042 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
635374e7
EM
3043
3044 /* free memory associated to diag buffers */
3045 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3046 if (!ioc->diag_buffer[i])
3047 continue;
3048 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3049 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3050 ioc->diag_buffer[i] = NULL;
3051 ioc->diag_buffer_status[i] = 0;
3052 }
3053
3054 kfree(ioc->event_log);
3055 }
3056 misc_deregister(&ctl_dev);
3057}
3058