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