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