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