blob: 1c62db8d80c719977f3db8cc30d39d920c7a3056 [file] [log] [blame]
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301/*
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
Sreekanth Reddya4ffce02014-09-12 15:35:29 +05306 * Copyright (C) 2012-2014 LSI Corporation
Sreekanth Reddya03bd152015-01-12 11:39:02 +05307 * Copyright (C) 2013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309 *
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
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053046#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
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053081/**
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
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530256/**
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 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530302 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530303 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/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +0530488 * ctl_fasync -
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530489 * @fd -
490 * @filep -
491 * @mode -
492 *
493 * Called when application request fasyn callback handler.
494 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +0530495int
496ctl_fasync(int fd, struct file *filep, int mode)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530497{
498 return fasync_helper(fd, filep, mode, &async_queue);
499}
500
501/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +0530502 * ctl_poll -
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530503 * @file -
504 * @wait -
505 *
506 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +0530507unsigned int
508ctl_poll(struct file *filep, poll_table *wait)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530509{
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 */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530757 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530758
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
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530912 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 }
Sreekanth Reddyaf009412015-11-11 17:30:23 +0530924
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530925 /* 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,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -0400976 TM_MUTEX_ON);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530977 } 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));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301018 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;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301029 strcpy(karg.driver_version, driver_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301030 strcat(karg.driver_version, "-");
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301031 switch (ioc->hba_mpi_version_belonged) {
1032 case MPI2_VERSION:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301033 if (ioc->is_warpdrive)
1034 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1035 else
1036 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301037 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1038 break;
1039 case MPI25_VERSION:
1040 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1041 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1042 break;
1043 }
1044 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
1045 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1046 else
1047 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301048 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1049
1050 if (copy_to_user(arg, &karg, sizeof(karg))) {
1051 pr_err("failure at %s:%d/%s()!\n",
1052 __FILE__, __LINE__, __func__);
1053 return -EFAULT;
1054 }
1055 return 0;
1056}
1057
1058/**
1059 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1060 * @ioc: per adapter object
1061 * @arg - user space buffer containing ioctl content
1062 */
1063static long
1064_ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1065{
1066 struct mpt3_ioctl_eventquery karg;
1067
1068 if (copy_from_user(&karg, arg, sizeof(karg))) {
1069 pr_err("failure at %s:%d/%s()!\n",
1070 __FILE__, __LINE__, __func__);
1071 return -EFAULT;
1072 }
1073
1074 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1075 __func__));
1076
1077 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1078 memcpy(karg.event_types, ioc->event_type,
1079 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1080
1081 if (copy_to_user(arg, &karg, sizeof(karg))) {
1082 pr_err("failure at %s:%d/%s()!\n",
1083 __FILE__, __LINE__, __func__);
1084 return -EFAULT;
1085 }
1086 return 0;
1087}
1088
1089/**
1090 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1091 * @ioc: per adapter object
1092 * @arg - user space buffer containing ioctl content
1093 */
1094static long
1095_ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1096{
1097 struct mpt3_ioctl_eventenable karg;
1098
1099 if (copy_from_user(&karg, arg, sizeof(karg))) {
1100 pr_err("failure at %s:%d/%s()!\n",
1101 __FILE__, __LINE__, __func__);
1102 return -EFAULT;
1103 }
1104
1105 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1106 __func__));
1107
1108 memcpy(ioc->event_type, karg.event_types,
1109 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1110 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1111
1112 if (ioc->event_log)
1113 return 0;
1114 /* initialize event_log */
1115 ioc->event_context = 0;
1116 ioc->aen_event_read_flag = 0;
1117 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1118 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1119 if (!ioc->event_log) {
1120 pr_err("failure at %s:%d/%s()!\n",
1121 __FILE__, __LINE__, __func__);
1122 return -ENOMEM;
1123 }
1124 return 0;
1125}
1126
1127/**
1128 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1129 * @ioc: per adapter object
1130 * @arg - user space buffer containing ioctl content
1131 */
1132static long
1133_ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1134{
1135 struct mpt3_ioctl_eventreport karg;
1136 u32 number_bytes, max_events, max;
1137 struct mpt3_ioctl_eventreport __user *uarg = arg;
1138
1139 if (copy_from_user(&karg, arg, sizeof(karg))) {
1140 pr_err("failure at %s:%d/%s()!\n",
1141 __FILE__, __LINE__, __func__);
1142 return -EFAULT;
1143 }
1144
1145 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1146 __func__));
1147
1148 number_bytes = karg.hdr.max_data_size -
1149 sizeof(struct mpt3_ioctl_header);
1150 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1151 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1152
1153 /* If fewer than 1 event is requested, there must have
1154 * been some type of error.
1155 */
1156 if (!max || !ioc->event_log)
1157 return -ENODATA;
1158
1159 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1160 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1161 pr_err("failure at %s:%d/%s()!\n",
1162 __FILE__, __LINE__, __func__);
1163 return -EFAULT;
1164 }
1165
1166 /* reset flag so SIGIO can restart */
1167 ioc->aen_event_read_flag = 0;
1168 return 0;
1169}
1170
1171/**
1172 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1173 * @ioc: per adapter object
1174 * @arg - user space buffer containing ioctl content
1175 */
1176static long
1177_ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1178{
1179 struct mpt3_ioctl_diag_reset karg;
1180 int retval;
1181
1182 if (copy_from_user(&karg, arg, sizeof(karg))) {
1183 pr_err("failure at %s:%d/%s()!\n",
1184 __FILE__, __LINE__, __func__);
1185 return -EFAULT;
1186 }
1187
1188 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1189 ioc->is_driver_loading)
1190 return -EAGAIN;
1191
1192 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1193 __func__));
1194
1195 retval = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1196 FORCE_BIG_HAMMER);
1197 pr_info(MPT3SAS_FMT "host reset: %s\n",
1198 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1199 return 0;
1200}
1201
1202/**
1203 * _ctl_btdh_search_sas_device - searching for sas device
1204 * @ioc: per adapter object
1205 * @btdh: btdh ioctl payload
1206 */
1207static int
1208_ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1209 struct mpt3_ioctl_btdh_mapping *btdh)
1210{
1211 struct _sas_device *sas_device;
1212 unsigned long flags;
1213 int rc = 0;
1214
1215 if (list_empty(&ioc->sas_device_list))
1216 return rc;
1217
1218 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1219 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1220 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1221 btdh->handle == sas_device->handle) {
1222 btdh->bus = sas_device->channel;
1223 btdh->id = sas_device->id;
1224 rc = 1;
1225 goto out;
1226 } else if (btdh->bus == sas_device->channel && btdh->id ==
1227 sas_device->id && btdh->handle == 0xFFFF) {
1228 btdh->handle = sas_device->handle;
1229 rc = 1;
1230 goto out;
1231 }
1232 }
1233 out:
1234 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1235 return rc;
1236}
1237
1238/**
1239 * _ctl_btdh_search_raid_device - searching for raid device
1240 * @ioc: per adapter object
1241 * @btdh: btdh ioctl payload
1242 */
1243static int
1244_ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1245 struct mpt3_ioctl_btdh_mapping *btdh)
1246{
1247 struct _raid_device *raid_device;
1248 unsigned long flags;
1249 int rc = 0;
1250
1251 if (list_empty(&ioc->raid_device_list))
1252 return rc;
1253
1254 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1255 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1256 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1257 btdh->handle == raid_device->handle) {
1258 btdh->bus = raid_device->channel;
1259 btdh->id = raid_device->id;
1260 rc = 1;
1261 goto out;
1262 } else if (btdh->bus == raid_device->channel && btdh->id ==
1263 raid_device->id && btdh->handle == 0xFFFF) {
1264 btdh->handle = raid_device->handle;
1265 rc = 1;
1266 goto out;
1267 }
1268 }
1269 out:
1270 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1271 return rc;
1272}
1273
1274/**
1275 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1276 * @ioc: per adapter object
1277 * @arg - user space buffer containing ioctl content
1278 */
1279static long
1280_ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1281{
1282 struct mpt3_ioctl_btdh_mapping karg;
1283 int rc;
1284
1285 if (copy_from_user(&karg, arg, sizeof(karg))) {
1286 pr_err("failure at %s:%d/%s()!\n",
1287 __FILE__, __LINE__, __func__);
1288 return -EFAULT;
1289 }
1290
1291 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1292 __func__));
1293
1294 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1295 if (!rc)
1296 _ctl_btdh_search_raid_device(ioc, &karg);
1297
1298 if (copy_to_user(arg, &karg, sizeof(karg))) {
1299 pr_err("failure at %s:%d/%s()!\n",
1300 __FILE__, __LINE__, __func__);
1301 return -EFAULT;
1302 }
1303 return 0;
1304}
1305
1306/**
1307 * _ctl_diag_capability - return diag buffer capability
1308 * @ioc: per adapter object
1309 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1310 *
1311 * returns 1 when diag buffer support is enabled in firmware
1312 */
1313static u8
1314_ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1315{
1316 u8 rc = 0;
1317
1318 switch (buffer_type) {
1319 case MPI2_DIAG_BUF_TYPE_TRACE:
1320 if (ioc->facts.IOCCapabilities &
1321 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1322 rc = 1;
1323 break;
1324 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1325 if (ioc->facts.IOCCapabilities &
1326 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1327 rc = 1;
1328 break;
1329 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1330 if (ioc->facts.IOCCapabilities &
1331 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1332 rc = 1;
1333 }
1334
1335 return rc;
1336}
1337
1338
1339/**
1340 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1341 * @ioc: per adapter object
1342 * @diag_register: the diag_register struct passed in from user space
1343 *
1344 */
1345static long
1346_ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1347 struct mpt3_diag_register *diag_register)
1348{
1349 int rc, i;
1350 void *request_data = NULL;
1351 dma_addr_t request_data_dma;
1352 u32 request_data_sz = 0;
1353 Mpi2DiagBufferPostRequest_t *mpi_request;
1354 Mpi2DiagBufferPostReply_t *mpi_reply;
1355 u8 buffer_type;
1356 unsigned long timeleft;
1357 u16 smid;
1358 u16 ioc_status;
1359 u32 ioc_state;
1360 u8 issue_reset = 0;
1361
1362 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1363 __func__));
1364
1365 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1366 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1367 pr_err(MPT3SAS_FMT
1368 "%s: failed due to ioc not operational\n",
1369 ioc->name, __func__);
1370 rc = -EAGAIN;
1371 goto out;
1372 }
1373
1374 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1375 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1376 ioc->name, __func__);
1377 rc = -EAGAIN;
1378 goto out;
1379 }
1380
1381 buffer_type = diag_register->buffer_type;
1382 if (!_ctl_diag_capability(ioc, buffer_type)) {
1383 pr_err(MPT3SAS_FMT
1384 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1385 ioc->name, __func__, buffer_type);
1386 return -EPERM;
1387 }
1388
1389 if (ioc->diag_buffer_status[buffer_type] &
1390 MPT3_DIAG_BUFFER_IS_REGISTERED) {
1391 pr_err(MPT3SAS_FMT
1392 "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1393 ioc->name, __func__,
1394 buffer_type);
1395 return -EINVAL;
1396 }
1397
1398 if (diag_register->requested_buffer_size % 4) {
1399 pr_err(MPT3SAS_FMT
1400 "%s: the requested_buffer_size is not 4 byte aligned\n",
1401 ioc->name, __func__);
1402 return -EINVAL;
1403 }
1404
1405 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1406 if (!smid) {
1407 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1408 ioc->name, __func__);
1409 rc = -EAGAIN;
1410 goto out;
1411 }
1412
1413 rc = 0;
1414 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1415 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1416 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1417 ioc->ctl_cmds.smid = smid;
1418
1419 request_data = ioc->diag_buffer[buffer_type];
1420 request_data_sz = diag_register->requested_buffer_size;
1421 ioc->unique_id[buffer_type] = diag_register->unique_id;
1422 ioc->diag_buffer_status[buffer_type] = 0;
1423 memcpy(ioc->product_specific[buffer_type],
1424 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1425 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1426
1427 if (request_data) {
1428 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1429 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1430 pci_free_consistent(ioc->pdev,
1431 ioc->diag_buffer_sz[buffer_type],
1432 request_data, request_data_dma);
1433 request_data = NULL;
1434 }
1435 }
1436
1437 if (request_data == NULL) {
1438 ioc->diag_buffer_sz[buffer_type] = 0;
1439 ioc->diag_buffer_dma[buffer_type] = 0;
1440 request_data = pci_alloc_consistent(
1441 ioc->pdev, request_data_sz, &request_data_dma);
1442 if (request_data == NULL) {
1443 pr_err(MPT3SAS_FMT "%s: failed allocating memory" \
1444 " for diag buffers, requested size(%d)\n",
1445 ioc->name, __func__, request_data_sz);
1446 mpt3sas_base_free_smid(ioc, smid);
1447 return -ENOMEM;
1448 }
1449 ioc->diag_buffer[buffer_type] = request_data;
1450 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1451 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1452 }
1453
1454 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1455 mpi_request->BufferType = diag_register->buffer_type;
1456 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1457 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1458 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1459 mpi_request->VF_ID = 0; /* TODO */
1460 mpi_request->VP_ID = 0;
1461
1462 dctlprintk(ioc, pr_info(MPT3SAS_FMT
1463 "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1464 ioc->name, __func__, request_data,
1465 (unsigned long long)request_data_dma,
1466 le32_to_cpu(mpi_request->BufferLength)));
1467
1468 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1469 mpi_request->ProductSpecific[i] =
1470 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1471
1472 init_completion(&ioc->ctl_cmds.done);
1473 mpt3sas_base_put_smid_default(ioc, smid);
1474 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1475 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1476
1477 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1478 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
1479 __func__);
1480 _debug_dump_mf(mpi_request,
1481 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1482 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
1483 issue_reset = 1;
1484 goto issue_host_reset;
1485 }
1486
1487 /* process the completed Reply Message Frame */
1488 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1489 pr_err(MPT3SAS_FMT "%s: no reply message\n",
1490 ioc->name, __func__);
1491 rc = -EFAULT;
1492 goto out;
1493 }
1494
1495 mpi_reply = ioc->ctl_cmds.reply;
1496 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1497
1498 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1499 ioc->diag_buffer_status[buffer_type] |=
1500 MPT3_DIAG_BUFFER_IS_REGISTERED;
1501 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
1502 ioc->name, __func__));
1503 } else {
1504 pr_info(MPT3SAS_FMT
1505 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1506 ioc->name, __func__,
1507 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1508 rc = -EFAULT;
1509 }
1510
1511 issue_host_reset:
1512 if (issue_reset)
1513 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1514 FORCE_BIG_HAMMER);
1515
1516 out:
1517
1518 if (rc && request_data)
1519 pci_free_consistent(ioc->pdev, request_data_sz,
1520 request_data, request_data_dma);
1521
1522 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1523 return rc;
1524}
1525
1526/**
1527 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1528 * @ioc: per adapter object
1529 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1530 *
1531 * This is called when command line option diag_buffer_enable is enabled
1532 * at driver load time.
1533 */
1534void
1535mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1536{
1537 struct mpt3_diag_register diag_register;
1538
1539 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1540
1541 if (bits_to_register & 1) {
1542 pr_info(MPT3SAS_FMT "registering trace buffer support\n",
1543 ioc->name);
1544 ioc->diag_trigger_master.MasterData =
1545 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1546 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1547 /* register for 2MB buffers */
1548 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1549 diag_register.unique_id = 0x7075900;
1550 _ctl_diag_register_2(ioc, &diag_register);
1551 }
1552
1553 if (bits_to_register & 2) {
1554 pr_info(MPT3SAS_FMT "registering snapshot buffer support\n",
1555 ioc->name);
1556 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1557 /* register for 2MB buffers */
1558 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1559 diag_register.unique_id = 0x7075901;
1560 _ctl_diag_register_2(ioc, &diag_register);
1561 }
1562
1563 if (bits_to_register & 4) {
1564 pr_info(MPT3SAS_FMT "registering extended buffer support\n",
1565 ioc->name);
1566 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1567 /* register for 2MB buffers */
1568 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1569 diag_register.unique_id = 0x7075901;
1570 _ctl_diag_register_2(ioc, &diag_register);
1571 }
1572}
1573
1574/**
1575 * _ctl_diag_register - application register with driver
1576 * @ioc: per adapter object
1577 * @arg - user space buffer containing ioctl content
1578 *
1579 * This will allow the driver to setup any required buffers that will be
1580 * needed by firmware to communicate with the driver.
1581 */
1582static long
1583_ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1584{
1585 struct mpt3_diag_register karg;
1586 long rc;
1587
1588 if (copy_from_user(&karg, arg, sizeof(karg))) {
1589 pr_err("failure at %s:%d/%s()!\n",
1590 __FILE__, __LINE__, __func__);
1591 return -EFAULT;
1592 }
1593
1594 rc = _ctl_diag_register_2(ioc, &karg);
1595 return rc;
1596}
1597
1598/**
1599 * _ctl_diag_unregister - application unregister with driver
1600 * @ioc: per adapter object
1601 * @arg - user space buffer containing ioctl content
1602 *
1603 * This will allow the driver to cleanup any memory allocated for diag
1604 * messages and to free up any resources.
1605 */
1606static long
1607_ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1608{
1609 struct mpt3_diag_unregister karg;
1610 void *request_data;
1611 dma_addr_t request_data_dma;
1612 u32 request_data_sz;
1613 u8 buffer_type;
1614
1615 if (copy_from_user(&karg, arg, sizeof(karg))) {
1616 pr_err("failure at %s:%d/%s()!\n",
1617 __FILE__, __LINE__, __func__);
1618 return -EFAULT;
1619 }
1620
1621 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1622 __func__));
1623
1624 buffer_type = karg.unique_id & 0x000000ff;
1625 if (!_ctl_diag_capability(ioc, buffer_type)) {
1626 pr_err(MPT3SAS_FMT
1627 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1628 ioc->name, __func__, buffer_type);
1629 return -EPERM;
1630 }
1631
1632 if ((ioc->diag_buffer_status[buffer_type] &
1633 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1634 pr_err(MPT3SAS_FMT
1635 "%s: buffer_type(0x%02x) is not registered\n",
1636 ioc->name, __func__, buffer_type);
1637 return -EINVAL;
1638 }
1639 if ((ioc->diag_buffer_status[buffer_type] &
1640 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
1641 pr_err(MPT3SAS_FMT
1642 "%s: buffer_type(0x%02x) has not been released\n",
1643 ioc->name, __func__, buffer_type);
1644 return -EINVAL;
1645 }
1646
1647 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1648 pr_err(MPT3SAS_FMT
1649 "%s: unique_id(0x%08x) is not registered\n",
1650 ioc->name, __func__, karg.unique_id);
1651 return -EINVAL;
1652 }
1653
1654 request_data = ioc->diag_buffer[buffer_type];
1655 if (!request_data) {
1656 pr_err(MPT3SAS_FMT
1657 "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1658 ioc->name, __func__, buffer_type);
1659 return -ENOMEM;
1660 }
1661
1662 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1663 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1664 pci_free_consistent(ioc->pdev, request_data_sz,
1665 request_data, request_data_dma);
1666 ioc->diag_buffer[buffer_type] = NULL;
1667 ioc->diag_buffer_status[buffer_type] = 0;
1668 return 0;
1669}
1670
1671/**
1672 * _ctl_diag_query - query relevant info associated with diag buffers
1673 * @ioc: per adapter object
1674 * @arg - user space buffer containing ioctl content
1675 *
1676 * The application will send only buffer_type and unique_id. Driver will
1677 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1678 * 0x00, the driver will return info specified by Buffer Type.
1679 */
1680static long
1681_ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1682{
1683 struct mpt3_diag_query karg;
1684 void *request_data;
1685 int i;
1686 u8 buffer_type;
1687
1688 if (copy_from_user(&karg, arg, sizeof(karg))) {
1689 pr_err("failure at %s:%d/%s()!\n",
1690 __FILE__, __LINE__, __func__);
1691 return -EFAULT;
1692 }
1693
1694 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1695 __func__));
1696
1697 karg.application_flags = 0;
1698 buffer_type = karg.buffer_type;
1699
1700 if (!_ctl_diag_capability(ioc, buffer_type)) {
1701 pr_err(MPT3SAS_FMT
1702 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1703 ioc->name, __func__, buffer_type);
1704 return -EPERM;
1705 }
1706
1707 if ((ioc->diag_buffer_status[buffer_type] &
1708 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1709 pr_err(MPT3SAS_FMT
1710 "%s: buffer_type(0x%02x) is not registered\n",
1711 ioc->name, __func__, buffer_type);
1712 return -EINVAL;
1713 }
1714
1715 if (karg.unique_id & 0xffffff00) {
1716 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1717 pr_err(MPT3SAS_FMT
1718 "%s: unique_id(0x%08x) is not registered\n",
1719 ioc->name, __func__, karg.unique_id);
1720 return -EINVAL;
1721 }
1722 }
1723
1724 request_data = ioc->diag_buffer[buffer_type];
1725 if (!request_data) {
1726 pr_err(MPT3SAS_FMT
1727 "%s: doesn't have buffer for buffer_type(0x%02x)\n",
1728 ioc->name, __func__, buffer_type);
1729 return -ENOMEM;
1730 }
1731
1732 if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
1733 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1734 MPT3_APP_FLAGS_BUFFER_VALID);
1735 else
1736 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1737 MPT3_APP_FLAGS_BUFFER_VALID |
1738 MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
1739
1740 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1741 karg.product_specific[i] =
1742 ioc->product_specific[buffer_type][i];
1743
1744 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1745 karg.driver_added_buffer_size = 0;
1746 karg.unique_id = ioc->unique_id[buffer_type];
1747 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1748
1749 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
1750 pr_err(MPT3SAS_FMT
1751 "%s: unable to write mpt3_diag_query data @ %p\n",
1752 ioc->name, __func__, arg);
1753 return -EFAULT;
1754 }
1755 return 0;
1756}
1757
1758/**
1759 * mpt3sas_send_diag_release - Diag Release Message
1760 * @ioc: per adapter object
1761 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1762 * @issue_reset - specifies whether host reset is required.
1763 *
1764 */
1765int
1766mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
1767 u8 *issue_reset)
1768{
1769 Mpi2DiagReleaseRequest_t *mpi_request;
1770 Mpi2DiagReleaseReply_t *mpi_reply;
1771 u16 smid;
1772 u16 ioc_status;
1773 u32 ioc_state;
1774 int rc;
1775 unsigned long timeleft;
1776
1777 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1778 __func__));
1779
1780 rc = 0;
1781 *issue_reset = 0;
1782
1783 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1784 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1785 if (ioc->diag_buffer_status[buffer_type] &
1786 MPT3_DIAG_BUFFER_IS_REGISTERED)
1787 ioc->diag_buffer_status[buffer_type] |=
1788 MPT3_DIAG_BUFFER_IS_RELEASED;
1789 dctlprintk(ioc, pr_info(MPT3SAS_FMT
1790 "%s: skipping due to FAULT state\n", ioc->name,
1791 __func__));
1792 rc = -EAGAIN;
1793 goto out;
1794 }
1795
1796 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1797 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1798 ioc->name, __func__);
1799 rc = -EAGAIN;
1800 goto out;
1801 }
1802
1803 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1804 if (!smid) {
1805 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1806 ioc->name, __func__);
1807 rc = -EAGAIN;
1808 goto out;
1809 }
1810
1811 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1812 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1813 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1814 ioc->ctl_cmds.smid = smid;
1815
1816 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1817 mpi_request->BufferType = buffer_type;
1818 mpi_request->VF_ID = 0; /* TODO */
1819 mpi_request->VP_ID = 0;
1820
1821 init_completion(&ioc->ctl_cmds.done);
1822 mpt3sas_base_put_smid_default(ioc, smid);
1823 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1824 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1825
1826 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1827 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
1828 __func__);
1829 _debug_dump_mf(mpi_request,
1830 sizeof(Mpi2DiagReleaseRequest_t)/4);
1831 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
1832 *issue_reset = 1;
1833 rc = -EFAULT;
1834 goto out;
1835 }
1836
1837 /* process the completed Reply Message Frame */
1838 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1839 pr_err(MPT3SAS_FMT "%s: no reply message\n",
1840 ioc->name, __func__);
1841 rc = -EFAULT;
1842 goto out;
1843 }
1844
1845 mpi_reply = ioc->ctl_cmds.reply;
1846 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1847
1848 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1849 ioc->diag_buffer_status[buffer_type] |=
1850 MPT3_DIAG_BUFFER_IS_RELEASED;
1851 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
1852 ioc->name, __func__));
1853 } else {
1854 pr_info(MPT3SAS_FMT
1855 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1856 ioc->name, __func__,
1857 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1858 rc = -EFAULT;
1859 }
1860
1861 out:
1862 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1863 return rc;
1864}
1865
1866/**
1867 * _ctl_diag_release - request to send Diag Release Message to firmware
1868 * @arg - user space buffer containing ioctl content
1869 *
1870 * This allows ownership of the specified buffer to returned to the driver,
1871 * allowing an application to read the buffer without fear that firmware is
1872 * overwritting information in the buffer.
1873 */
1874static long
1875_ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1876{
1877 struct mpt3_diag_release karg;
1878 void *request_data;
1879 int rc;
1880 u8 buffer_type;
1881 u8 issue_reset = 0;
1882
1883 if (copy_from_user(&karg, arg, sizeof(karg))) {
1884 pr_err("failure at %s:%d/%s()!\n",
1885 __FILE__, __LINE__, __func__);
1886 return -EFAULT;
1887 }
1888
1889 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1890 __func__));
1891
1892 buffer_type = karg.unique_id & 0x000000ff;
1893 if (!_ctl_diag_capability(ioc, buffer_type)) {
1894 pr_err(MPT3SAS_FMT
1895 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1896 ioc->name, __func__, buffer_type);
1897 return -EPERM;
1898 }
1899
1900 if ((ioc->diag_buffer_status[buffer_type] &
1901 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1902 pr_err(MPT3SAS_FMT
1903 "%s: buffer_type(0x%02x) is not registered\n",
1904 ioc->name, __func__, buffer_type);
1905 return -EINVAL;
1906 }
1907
1908 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1909 pr_err(MPT3SAS_FMT
1910 "%s: unique_id(0x%08x) is not registered\n",
1911 ioc->name, __func__, karg.unique_id);
1912 return -EINVAL;
1913 }
1914
1915 if (ioc->diag_buffer_status[buffer_type] &
1916 MPT3_DIAG_BUFFER_IS_RELEASED) {
1917 pr_err(MPT3SAS_FMT
1918 "%s: buffer_type(0x%02x) is already released\n",
1919 ioc->name, __func__,
1920 buffer_type);
1921 return 0;
1922 }
1923
1924 request_data = ioc->diag_buffer[buffer_type];
1925
1926 if (!request_data) {
1927 pr_err(MPT3SAS_FMT
1928 "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1929 ioc->name, __func__, buffer_type);
1930 return -ENOMEM;
1931 }
1932
1933 /* buffers were released by due to host reset */
1934 if ((ioc->diag_buffer_status[buffer_type] &
1935 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
1936 ioc->diag_buffer_status[buffer_type] |=
1937 MPT3_DIAG_BUFFER_IS_RELEASED;
1938 ioc->diag_buffer_status[buffer_type] &=
1939 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
1940 pr_err(MPT3SAS_FMT
1941 "%s: buffer_type(0x%02x) was released due to host reset\n",
1942 ioc->name, __func__, buffer_type);
1943 return 0;
1944 }
1945
1946 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
1947
1948 if (issue_reset)
1949 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1950 FORCE_BIG_HAMMER);
1951
1952 return rc;
1953}
1954
1955/**
1956 * _ctl_diag_read_buffer - request for copy of the diag buffer
1957 * @ioc: per adapter object
1958 * @arg - user space buffer containing ioctl content
1959 */
1960static long
1961_ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1962{
1963 struct mpt3_diag_read_buffer karg;
1964 struct mpt3_diag_read_buffer __user *uarg = arg;
1965 void *request_data, *diag_data;
1966 Mpi2DiagBufferPostRequest_t *mpi_request;
1967 Mpi2DiagBufferPostReply_t *mpi_reply;
1968 int rc, i;
1969 u8 buffer_type;
1970 unsigned long timeleft, request_size, copy_size;
1971 u16 smid;
1972 u16 ioc_status;
1973 u8 issue_reset = 0;
1974
1975 if (copy_from_user(&karg, arg, sizeof(karg))) {
1976 pr_err("failure at %s:%d/%s()!\n",
1977 __FILE__, __LINE__, __func__);
1978 return -EFAULT;
1979 }
1980
1981 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1982 __func__));
1983
1984 buffer_type = karg.unique_id & 0x000000ff;
1985 if (!_ctl_diag_capability(ioc, buffer_type)) {
1986 pr_err(MPT3SAS_FMT
1987 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1988 ioc->name, __func__, buffer_type);
1989 return -EPERM;
1990 }
1991
1992 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1993 pr_err(MPT3SAS_FMT
1994 "%s: unique_id(0x%08x) is not registered\n",
1995 ioc->name, __func__, karg.unique_id);
1996 return -EINVAL;
1997 }
1998
1999 request_data = ioc->diag_buffer[buffer_type];
2000 if (!request_data) {
2001 pr_err(MPT3SAS_FMT
2002 "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2003 ioc->name, __func__, buffer_type);
2004 return -ENOMEM;
2005 }
2006
2007 request_size = ioc->diag_buffer_sz[buffer_type];
2008
2009 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2010 pr_err(MPT3SAS_FMT "%s: either the starting_offset " \
2011 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
2012 __func__);
2013 return -EINVAL;
2014 }
2015
2016 if (karg.starting_offset > request_size)
2017 return -EINVAL;
2018
2019 diag_data = (void *)(request_data + karg.starting_offset);
2020 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2021 "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2022 ioc->name, __func__,
2023 diag_data, karg.starting_offset, karg.bytes_to_read));
2024
2025 /* Truncate data on requests that are too large */
2026 if ((diag_data + karg.bytes_to_read < diag_data) ||
2027 (diag_data + karg.bytes_to_read > request_data + request_size))
2028 copy_size = request_size - karg.starting_offset;
2029 else
2030 copy_size = karg.bytes_to_read;
2031
2032 if (copy_to_user((void __user *)uarg->diagnostic_data,
2033 diag_data, copy_size)) {
2034 pr_err(MPT3SAS_FMT
2035 "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2036 ioc->name, __func__, diag_data);
2037 return -EFAULT;
2038 }
2039
2040 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2041 return 0;
2042
2043 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2044 "%s: Reregister buffer_type(0x%02x)\n",
2045 ioc->name, __func__, buffer_type));
2046 if ((ioc->diag_buffer_status[buffer_type] &
2047 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2048 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2049 "%s: buffer_type(0x%02x) is still registered\n",
2050 ioc->name, __func__, buffer_type));
2051 return 0;
2052 }
2053 /* Get a free request frame and save the message context.
2054 */
2055
2056 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2057 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
2058 ioc->name, __func__);
2059 rc = -EAGAIN;
2060 goto out;
2061 }
2062
2063 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2064 if (!smid) {
2065 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2066 ioc->name, __func__);
2067 rc = -EAGAIN;
2068 goto out;
2069 }
2070
2071 rc = 0;
2072 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2073 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2074 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2075 ioc->ctl_cmds.smid = smid;
2076
2077 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2078 mpi_request->BufferType = buffer_type;
2079 mpi_request->BufferLength =
2080 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2081 mpi_request->BufferAddress =
2082 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2083 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2084 mpi_request->ProductSpecific[i] =
2085 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2086 mpi_request->VF_ID = 0; /* TODO */
2087 mpi_request->VP_ID = 0;
2088
2089 init_completion(&ioc->ctl_cmds.done);
2090 mpt3sas_base_put_smid_default(ioc, smid);
2091 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2092 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2093
2094 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2095 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
2096 __func__);
2097 _debug_dump_mf(mpi_request,
2098 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2099 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
2100 issue_reset = 1;
2101 goto issue_host_reset;
2102 }
2103
2104 /* process the completed Reply Message Frame */
2105 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2106 pr_err(MPT3SAS_FMT "%s: no reply message\n",
2107 ioc->name, __func__);
2108 rc = -EFAULT;
2109 goto out;
2110 }
2111
2112 mpi_reply = ioc->ctl_cmds.reply;
2113 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2114
2115 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2116 ioc->diag_buffer_status[buffer_type] |=
2117 MPT3_DIAG_BUFFER_IS_REGISTERED;
2118 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
2119 ioc->name, __func__));
2120 } else {
2121 pr_info(MPT3SAS_FMT
2122 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2123 ioc->name, __func__,
2124 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2125 rc = -EFAULT;
2126 }
2127
2128 issue_host_reset:
2129 if (issue_reset)
2130 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2131 FORCE_BIG_HAMMER);
2132
2133 out:
2134
2135 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2136 return rc;
2137}
2138
2139
2140
2141#ifdef CONFIG_COMPAT
2142/**
2143 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2144 * @ioc: per adapter object
2145 * @cmd - ioctl opcode
2146 * @arg - (struct mpt3_ioctl_command32)
2147 *
2148 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2149 */
2150static long
2151_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2152 void __user *arg)
2153{
2154 struct mpt3_ioctl_command32 karg32;
2155 struct mpt3_ioctl_command32 __user *uarg;
2156 struct mpt3_ioctl_command karg;
2157
2158 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2159 return -EINVAL;
2160
2161 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2162
2163 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2164 pr_err("failure at %s:%d/%s()!\n",
2165 __FILE__, __LINE__, __func__);
2166 return -EFAULT;
2167 }
2168
2169 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2170 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2171 karg.hdr.port_number = karg32.hdr.port_number;
2172 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2173 karg.timeout = karg32.timeout;
2174 karg.max_reply_bytes = karg32.max_reply_bytes;
2175 karg.data_in_size = karg32.data_in_size;
2176 karg.data_out_size = karg32.data_out_size;
2177 karg.max_sense_bytes = karg32.max_sense_bytes;
2178 karg.data_sge_offset = karg32.data_sge_offset;
2179 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2180 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2181 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2182 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2183 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2184}
2185#endif
2186
2187/**
2188 * _ctl_ioctl_main - main ioctl entry point
2189 * @file - (struct file)
2190 * @cmd - ioctl opcode
2191 * @arg -
2192 * compat - handles 32 bit applications in 64bit os
2193 */
2194static long
2195_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2196 u8 compat)
2197{
2198 struct MPT3SAS_ADAPTER *ioc;
2199 struct mpt3_ioctl_header ioctl_header;
2200 enum block_state state;
2201 long ret = -EINVAL;
2202
2203 /* get IOCTL header */
2204 if (copy_from_user(&ioctl_header, (char __user *)arg,
2205 sizeof(struct mpt3_ioctl_header))) {
2206 pr_err("failure at %s:%d/%s()!\n",
2207 __FILE__, __LINE__, __func__);
2208 return -EFAULT;
2209 }
2210
2211 if (_ctl_verify_adapter(ioctl_header.ioc_number, &ioc) == -1 || !ioc)
2212 return -ENODEV;
2213
2214 if (ioc->shost_recovery || ioc->pci_error_recovery ||
2215 ioc->is_driver_loading)
2216 return -EAGAIN;
2217
2218 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2219 if (state == NON_BLOCKING) {
2220 if (!mutex_trylock(&ioc->ctl_cmds.mutex))
2221 return -EAGAIN;
2222 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
2223 return -ERESTARTSYS;
2224
2225
2226 switch (cmd) {
2227 case MPT3IOCINFO:
2228 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2229 ret = _ctl_getiocinfo(ioc, arg);
2230 break;
2231#ifdef CONFIG_COMPAT
2232 case MPT3COMMAND32:
2233#endif
2234 case MPT3COMMAND:
2235 {
2236 struct mpt3_ioctl_command __user *uarg;
2237 struct mpt3_ioctl_command karg;
2238
2239#ifdef CONFIG_COMPAT
2240 if (compat) {
2241 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2242 break;
2243 }
2244#endif
2245 if (copy_from_user(&karg, arg, sizeof(karg))) {
2246 pr_err("failure at %s:%d/%s()!\n",
2247 __FILE__, __LINE__, __func__);
2248 ret = -EFAULT;
2249 break;
2250 }
2251
2252 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2253 uarg = arg;
2254 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2255 }
2256 break;
2257 }
2258 case MPT3EVENTQUERY:
2259 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2260 ret = _ctl_eventquery(ioc, arg);
2261 break;
2262 case MPT3EVENTENABLE:
2263 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2264 ret = _ctl_eventenable(ioc, arg);
2265 break;
2266 case MPT3EVENTREPORT:
2267 ret = _ctl_eventreport(ioc, arg);
2268 break;
2269 case MPT3HARDRESET:
2270 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2271 ret = _ctl_do_reset(ioc, arg);
2272 break;
2273 case MPT3BTDHMAPPING:
2274 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2275 ret = _ctl_btdh_mapping(ioc, arg);
2276 break;
2277 case MPT3DIAGREGISTER:
2278 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2279 ret = _ctl_diag_register(ioc, arg);
2280 break;
2281 case MPT3DIAGUNREGISTER:
2282 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2283 ret = _ctl_diag_unregister(ioc, arg);
2284 break;
2285 case MPT3DIAGQUERY:
2286 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2287 ret = _ctl_diag_query(ioc, arg);
2288 break;
2289 case MPT3DIAGRELEASE:
2290 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2291 ret = _ctl_diag_release(ioc, arg);
2292 break;
2293 case MPT3DIAGREADBUFFER:
2294 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2295 ret = _ctl_diag_read_buffer(ioc, arg);
2296 break;
2297 default:
2298 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2299 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2300 break;
2301 }
2302
2303 mutex_unlock(&ioc->ctl_cmds.mutex);
2304 return ret;
2305}
2306
2307/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302308 * ctl_ioctl - main ioctl entry point (unlocked)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302309 * @file - (struct file)
2310 * @cmd - ioctl opcode
2311 * @arg -
2312 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302313long
2314ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302315{
2316 long ret;
2317
2318 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0);
2319 return ret;
2320}
2321
2322#ifdef CONFIG_COMPAT
2323/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302324 * ctl_ioctl_compat - main ioctl entry point (compat)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302325 * @file -
2326 * @cmd -
2327 * @arg -
2328 *
2329 * This routine handles 32 bit applications in 64bit os.
2330 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302331long
2332ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302333{
2334 long ret;
2335
2336 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1);
2337 return ret;
2338}
2339#endif
2340
2341/* scsi host attributes */
2342/**
2343 * _ctl_version_fw_show - firmware version
2344 * @cdev - pointer to embedded class device
2345 * @buf - the buffer returned
2346 *
2347 * A sysfs 'read-only' shost attribute.
2348 */
2349static ssize_t
2350_ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2351 char *buf)
2352{
2353 struct Scsi_Host *shost = class_to_shost(cdev);
2354 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2355
2356 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2357 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2358 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2359 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2360 ioc->facts.FWVersion.Word & 0x000000FF);
2361}
2362static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2363
2364/**
2365 * _ctl_version_bios_show - bios version
2366 * @cdev - pointer to embedded class device
2367 * @buf - the buffer returned
2368 *
2369 * A sysfs 'read-only' shost attribute.
2370 */
2371static ssize_t
2372_ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2373 char *buf)
2374{
2375 struct Scsi_Host *shost = class_to_shost(cdev);
2376 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2377
2378 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2379
2380 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2381 (version & 0xFF000000) >> 24,
2382 (version & 0x00FF0000) >> 16,
2383 (version & 0x0000FF00) >> 8,
2384 version & 0x000000FF);
2385}
2386static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2387
2388/**
2389 * _ctl_version_mpi_show - MPI (message passing interface) version
2390 * @cdev - pointer to embedded class device
2391 * @buf - the buffer returned
2392 *
2393 * A sysfs 'read-only' shost attribute.
2394 */
2395static ssize_t
2396_ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2397 char *buf)
2398{
2399 struct Scsi_Host *shost = class_to_shost(cdev);
2400 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2401
2402 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2403 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2404}
2405static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2406
2407/**
2408 * _ctl_version_product_show - product name
2409 * @cdev - pointer to embedded class device
2410 * @buf - the buffer returned
2411 *
2412 * A sysfs 'read-only' shost attribute.
2413 */
2414static ssize_t
2415_ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2416 char *buf)
2417{
2418 struct Scsi_Host *shost = class_to_shost(cdev);
2419 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2420
2421 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2422}
2423static DEVICE_ATTR(version_product, S_IRUGO, _ctl_version_product_show, NULL);
2424
2425/**
2426 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2427 * @cdev - pointer to embedded class device
2428 * @buf - the buffer returned
2429 *
2430 * A sysfs 'read-only' shost attribute.
2431 */
2432static ssize_t
2433_ctl_version_nvdata_persistent_show(struct device *cdev,
2434 struct device_attribute *attr, char *buf)
2435{
2436 struct Scsi_Host *shost = class_to_shost(cdev);
2437 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2438
2439 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2440 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2441}
2442static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2443 _ctl_version_nvdata_persistent_show, NULL);
2444
2445/**
2446 * _ctl_version_nvdata_default_show - nvdata default version
2447 * @cdev - pointer to embedded class device
2448 * @buf - the buffer returned
2449 *
2450 * A sysfs 'read-only' shost attribute.
2451 */
2452static ssize_t
2453_ctl_version_nvdata_default_show(struct device *cdev, struct device_attribute
2454 *attr, char *buf)
2455{
2456 struct Scsi_Host *shost = class_to_shost(cdev);
2457 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2458
2459 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2460 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2461}
2462static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2463 _ctl_version_nvdata_default_show, NULL);
2464
2465/**
2466 * _ctl_board_name_show - board name
2467 * @cdev - pointer to embedded class device
2468 * @buf - the buffer returned
2469 *
2470 * A sysfs 'read-only' shost attribute.
2471 */
2472static ssize_t
2473_ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2474 char *buf)
2475{
2476 struct Scsi_Host *shost = class_to_shost(cdev);
2477 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2478
2479 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2480}
2481static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2482
2483/**
2484 * _ctl_board_assembly_show - board assembly name
2485 * @cdev - pointer to embedded class device
2486 * @buf - the buffer returned
2487 *
2488 * A sysfs 'read-only' shost attribute.
2489 */
2490static ssize_t
2491_ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2492 char *buf)
2493{
2494 struct Scsi_Host *shost = class_to_shost(cdev);
2495 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2496
2497 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2498}
2499static DEVICE_ATTR(board_assembly, S_IRUGO, _ctl_board_assembly_show, NULL);
2500
2501/**
2502 * _ctl_board_tracer_show - board tracer number
2503 * @cdev - pointer to embedded class device
2504 * @buf - the buffer returned
2505 *
2506 * A sysfs 'read-only' shost attribute.
2507 */
2508static ssize_t
2509_ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2510 char *buf)
2511{
2512 struct Scsi_Host *shost = class_to_shost(cdev);
2513 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2514
2515 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2516}
2517static DEVICE_ATTR(board_tracer, S_IRUGO, _ctl_board_tracer_show, NULL);
2518
2519/**
2520 * _ctl_io_delay_show - io missing delay
2521 * @cdev - pointer to embedded class device
2522 * @buf - the buffer returned
2523 *
2524 * This is for firmware implemention for deboucing device
2525 * removal events.
2526 *
2527 * A sysfs 'read-only' shost attribute.
2528 */
2529static ssize_t
2530_ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2531 char *buf)
2532{
2533 struct Scsi_Host *shost = class_to_shost(cdev);
2534 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2535
2536 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2537}
2538static DEVICE_ATTR(io_delay, S_IRUGO, _ctl_io_delay_show, NULL);
2539
2540/**
2541 * _ctl_device_delay_show - device missing delay
2542 * @cdev - pointer to embedded class device
2543 * @buf - the buffer returned
2544 *
2545 * This is for firmware implemention for deboucing device
2546 * removal events.
2547 *
2548 * A sysfs 'read-only' shost attribute.
2549 */
2550static ssize_t
2551_ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2552 char *buf)
2553{
2554 struct Scsi_Host *shost = class_to_shost(cdev);
2555 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2556
2557 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2558}
2559static DEVICE_ATTR(device_delay, S_IRUGO, _ctl_device_delay_show, NULL);
2560
2561/**
2562 * _ctl_fw_queue_depth_show - global credits
2563 * @cdev - pointer to embedded class device
2564 * @buf - the buffer returned
2565 *
2566 * This is firmware queue depth limit
2567 *
2568 * A sysfs 'read-only' shost attribute.
2569 */
2570static ssize_t
2571_ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2572 char *buf)
2573{
2574 struct Scsi_Host *shost = class_to_shost(cdev);
2575 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2576
2577 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2578}
2579static DEVICE_ATTR(fw_queue_depth, S_IRUGO, _ctl_fw_queue_depth_show, NULL);
2580
2581/**
2582 * _ctl_sas_address_show - sas address
2583 * @cdev - pointer to embedded class device
2584 * @buf - the buffer returned
2585 *
2586 * This is the controller sas address
2587 *
2588 * A sysfs 'read-only' shost attribute.
2589 */
2590static ssize_t
2591_ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2592 char *buf)
2593
2594{
2595 struct Scsi_Host *shost = class_to_shost(cdev);
2596 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2597
2598 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2599 (unsigned long long)ioc->sas_hba.sas_address);
2600}
2601static DEVICE_ATTR(host_sas_address, S_IRUGO,
2602 _ctl_host_sas_address_show, NULL);
2603
2604/**
2605 * _ctl_logging_level_show - logging level
2606 * @cdev - pointer to embedded class device
2607 * @buf - the buffer returned
2608 *
2609 * A sysfs 'read/write' shost attribute.
2610 */
2611static ssize_t
2612_ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2613 char *buf)
2614{
2615 struct Scsi_Host *shost = class_to_shost(cdev);
2616 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2617
2618 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2619}
2620static ssize_t
2621_ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2622 const char *buf, size_t count)
2623{
2624 struct Scsi_Host *shost = class_to_shost(cdev);
2625 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2626 int val = 0;
2627
2628 if (sscanf(buf, "%x", &val) != 1)
2629 return -EINVAL;
2630
2631 ioc->logging_level = val;
2632 pr_info(MPT3SAS_FMT "logging_level=%08xh\n", ioc->name,
2633 ioc->logging_level);
2634 return strlen(buf);
2635}
2636static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR, _ctl_logging_level_show,
2637 _ctl_logging_level_store);
2638
2639/**
2640 * _ctl_fwfault_debug_show - show/store fwfault_debug
2641 * @cdev - pointer to embedded class device
2642 * @buf - the buffer returned
2643 *
2644 * mpt3sas_fwfault_debug is command line option
2645 * A sysfs 'read/write' shost attribute.
2646 */
2647static ssize_t
2648_ctl_fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
2649 char *buf)
2650{
2651 struct Scsi_Host *shost = class_to_shost(cdev);
2652 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2653
2654 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2655}
2656static ssize_t
2657_ctl_fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
2658 const char *buf, size_t count)
2659{
2660 struct Scsi_Host *shost = class_to_shost(cdev);
2661 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2662 int val = 0;
2663
2664 if (sscanf(buf, "%d", &val) != 1)
2665 return -EINVAL;
2666
2667 ioc->fwfault_debug = val;
2668 pr_info(MPT3SAS_FMT "fwfault_debug=%d\n", ioc->name,
2669 ioc->fwfault_debug);
2670 return strlen(buf);
2671}
2672static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2673 _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2674
2675/**
2676 * _ctl_ioc_reset_count_show - ioc reset count
2677 * @cdev - pointer to embedded class device
2678 * @buf - the buffer returned
2679 *
2680 * This is firmware queue depth limit
2681 *
2682 * A sysfs 'read-only' shost attribute.
2683 */
2684static ssize_t
2685_ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2686 char *buf)
2687{
2688 struct Scsi_Host *shost = class_to_shost(cdev);
2689 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2690
2691 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
2692}
2693static DEVICE_ATTR(ioc_reset_count, S_IRUGO, _ctl_ioc_reset_count_show, NULL);
2694
2695/**
2696 * _ctl_ioc_reply_queue_count_show - number of reply queues
2697 * @cdev - pointer to embedded class device
2698 * @buf - the buffer returned
2699 *
2700 * This is number of reply queues
2701 *
2702 * A sysfs 'read-only' shost attribute.
2703 */
2704static ssize_t
2705_ctl_ioc_reply_queue_count_show(struct device *cdev,
2706 struct device_attribute *attr, char *buf)
2707{
2708 u8 reply_queue_count;
2709 struct Scsi_Host *shost = class_to_shost(cdev);
2710 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2711
2712 if ((ioc->facts.IOCCapabilities &
2713 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2714 reply_queue_count = ioc->reply_queue_count;
2715 else
2716 reply_queue_count = 1;
2717
2718 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2719}
2720static DEVICE_ATTR(reply_queue_count, S_IRUGO, _ctl_ioc_reply_queue_count_show,
2721 NULL);
2722
Sreekanth Reddy42263092015-11-11 17:30:29 +05302723#ifdef SCSI_MPT2SAS
2724/**
2725 * _ctl_BRM_status_show - Backup Rail Monitor Status
2726 * @cdev - pointer to embedded class device
2727 * @buf - the buffer returned
2728 *
2729 * This is number of reply queues
2730 *
2731 * A sysfs 'read-only' shost attribute.
2732 */
2733static ssize_t
2734_ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
2735 char *buf)
2736{
2737 struct Scsi_Host *shost = class_to_shost(cdev);
2738 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2739 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2740 Mpi2ConfigReply_t mpi_reply;
2741 u16 backup_rail_monitor_status = 0;
2742 u16 ioc_status;
2743 int sz;
2744 ssize_t rc = 0;
2745
2746 if (!ioc->is_warpdrive) {
2747 pr_err(MPT3SAS_FMT "%s: BRM attribute is only for"
2748 " warpdrive\n", ioc->name, __func__);
2749 goto out;
2750 }
2751
2752 /* allocate upto GPIOVal 36 entries */
2753 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2754 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2755 if (!io_unit_pg3) {
2756 pr_err(MPT3SAS_FMT "%s: failed allocating memory "
2757 "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
2758 goto out;
2759 }
2760
2761 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2762 0) {
2763 pr_err(MPT3SAS_FMT
2764 "%s: failed reading iounit_pg3\n", ioc->name,
2765 __func__);
2766 goto out;
2767 }
2768
2769 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2770 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2771 pr_err(MPT3SAS_FMT "%s: iounit_pg3 failed with "
2772 "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
2773 goto out;
2774 }
2775
2776 if (io_unit_pg3->GPIOCount < 25) {
2777 pr_err(MPT3SAS_FMT "%s: iounit_pg3->GPIOCount less than "
2778 "25 entries, detected (%d) entries\n", ioc->name, __func__,
2779 io_unit_pg3->GPIOCount);
2780 goto out;
2781 }
2782
2783 /* BRM status is in bit zero of GPIOVal[24] */
2784 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2785 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2786
2787 out:
2788 kfree(io_unit_pg3);
2789 return rc;
2790}
2791static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
2792#endif
2793
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302794struct DIAG_BUFFER_START {
2795 __le32 Size;
2796 __le32 DiagVersion;
2797 u8 BufferType;
2798 u8 Reserved[3];
2799 __le32 Reserved1;
2800 __le32 Reserved2;
2801 __le32 Reserved3;
2802};
2803
2804/**
2805 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2806 * @cdev - pointer to embedded class device
2807 * @buf - the buffer returned
2808 *
2809 * A sysfs 'read-only' shost attribute.
2810 */
2811static ssize_t
2812_ctl_host_trace_buffer_size_show(struct device *cdev,
2813 struct device_attribute *attr, char *buf)
2814{
2815 struct Scsi_Host *shost = class_to_shost(cdev);
2816 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2817 u32 size = 0;
2818 struct DIAG_BUFFER_START *request_data;
2819
2820 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2821 pr_err(MPT3SAS_FMT
2822 "%s: host_trace_buffer is not registered\n",
2823 ioc->name, __func__);
2824 return 0;
2825 }
2826
2827 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2828 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2829 pr_err(MPT3SAS_FMT
2830 "%s: host_trace_buffer is not registered\n",
2831 ioc->name, __func__);
2832 return 0;
2833 }
2834
2835 request_data = (struct DIAG_BUFFER_START *)
2836 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2837 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2838 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
2839 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
2840 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2841 size = le32_to_cpu(request_data->Size);
2842
2843 ioc->ring_buffer_sz = size;
2844 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2845}
2846static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2847 _ctl_host_trace_buffer_size_show, NULL);
2848
2849/**
2850 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2851 * @cdev - pointer to embedded class device
2852 * @buf - the buffer returned
2853 *
2854 * A sysfs 'read/write' shost attribute.
2855 *
2856 * You will only be able to read 4k bytes of ring buffer at a time.
2857 * In order to read beyond 4k bytes, you will have to write out the
2858 * offset to the same attribute, it will move the pointer.
2859 */
2860static ssize_t
2861_ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2862 char *buf)
2863{
2864 struct Scsi_Host *shost = class_to_shost(cdev);
2865 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2866 void *request_data;
2867 u32 size;
2868
2869 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2870 pr_err(MPT3SAS_FMT
2871 "%s: host_trace_buffer is not registered\n",
2872 ioc->name, __func__);
2873 return 0;
2874 }
2875
2876 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2877 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2878 pr_err(MPT3SAS_FMT
2879 "%s: host_trace_buffer is not registered\n",
2880 ioc->name, __func__);
2881 return 0;
2882 }
2883
2884 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2885 return 0;
2886
2887 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2888 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2889 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2890 memcpy(buf, request_data, size);
2891 return size;
2892}
2893
2894static ssize_t
2895_ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2896 const char *buf, size_t count)
2897{
2898 struct Scsi_Host *shost = class_to_shost(cdev);
2899 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2900 int val = 0;
2901
2902 if (sscanf(buf, "%d", &val) != 1)
2903 return -EINVAL;
2904
2905 ioc->ring_buffer_offset = val;
2906 return strlen(buf);
2907}
2908static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2909 _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2910
2911
2912/*****************************************/
2913
2914/**
2915 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2916 * @cdev - pointer to embedded class device
2917 * @buf - the buffer returned
2918 *
2919 * A sysfs 'read/write' shost attribute.
2920 *
2921 * This is a mechnism to post/release host_trace_buffers
2922 */
2923static ssize_t
2924_ctl_host_trace_buffer_enable_show(struct device *cdev,
2925 struct device_attribute *attr, char *buf)
2926{
2927 struct Scsi_Host *shost = class_to_shost(cdev);
2928 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2929
2930 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
2931 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2932 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
2933 return snprintf(buf, PAGE_SIZE, "off\n");
2934 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2935 MPT3_DIAG_BUFFER_IS_RELEASED))
2936 return snprintf(buf, PAGE_SIZE, "release\n");
2937 else
2938 return snprintf(buf, PAGE_SIZE, "post\n");
2939}
2940
2941static ssize_t
2942_ctl_host_trace_buffer_enable_store(struct device *cdev,
2943 struct device_attribute *attr, const char *buf, size_t count)
2944{
2945 struct Scsi_Host *shost = class_to_shost(cdev);
2946 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2947 char str[10] = "";
2948 struct mpt3_diag_register diag_register;
2949 u8 issue_reset = 0;
2950
2951 /* don't allow post/release occurr while recovery is active */
2952 if (ioc->shost_recovery || ioc->remove_host ||
2953 ioc->pci_error_recovery || ioc->is_driver_loading)
2954 return -EBUSY;
2955
2956 if (sscanf(buf, "%9s", str) != 1)
2957 return -EINVAL;
2958
2959 if (!strcmp(str, "post")) {
2960 /* exit out if host buffers are already posted */
2961 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
2962 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2963 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
2964 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2965 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
2966 goto out;
2967 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
2968 pr_info(MPT3SAS_FMT "posting host trace buffers\n",
2969 ioc->name);
2970 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
2971 diag_register.requested_buffer_size = (1024 * 1024);
2972 diag_register.unique_id = 0x7075900;
2973 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
2974 _ctl_diag_register_2(ioc, &diag_register);
2975 } else if (!strcmp(str, "release")) {
2976 /* exit out if host buffers are already released */
2977 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
2978 goto out;
2979 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2980 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
2981 goto out;
2982 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2983 MPT3_DIAG_BUFFER_IS_RELEASED))
2984 goto out;
2985 pr_info(MPT3SAS_FMT "releasing host trace buffer\n",
2986 ioc->name);
2987 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
2988 &issue_reset);
2989 }
2990
2991 out:
2992 return strlen(buf);
2993}
2994static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
2995 _ctl_host_trace_buffer_enable_show,
2996 _ctl_host_trace_buffer_enable_store);
2997
2998/*********** diagnostic trigger suppport *********************************/
2999
3000/**
3001 * _ctl_diag_trigger_master_show - show the diag_trigger_master attribute
3002 * @cdev - pointer to embedded class device
3003 * @buf - the buffer returned
3004 *
3005 * A sysfs 'read/write' shost attribute.
3006 */
3007static ssize_t
3008_ctl_diag_trigger_master_show(struct device *cdev,
3009 struct device_attribute *attr, char *buf)
3010
3011{
3012 struct Scsi_Host *shost = class_to_shost(cdev);
3013 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3014 unsigned long flags;
3015 ssize_t rc;
3016
3017 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3018 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3019 memcpy(buf, &ioc->diag_trigger_master, rc);
3020 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3021 return rc;
3022}
3023
3024/**
3025 * _ctl_diag_trigger_master_store - store the diag_trigger_master attribute
3026 * @cdev - pointer to embedded class device
3027 * @buf - the buffer returned
3028 *
3029 * A sysfs 'read/write' shost attribute.
3030 */
3031static ssize_t
3032_ctl_diag_trigger_master_store(struct device *cdev,
3033 struct device_attribute *attr, const char *buf, size_t count)
3034
3035{
3036 struct Scsi_Host *shost = class_to_shost(cdev);
3037 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3038 unsigned long flags;
3039 ssize_t rc;
3040
3041 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3042 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3043 memset(&ioc->diag_trigger_master, 0,
3044 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3045 memcpy(&ioc->diag_trigger_master, buf, rc);
3046 ioc->diag_trigger_master.MasterData |=
3047 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3048 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3049 return rc;
3050}
3051static DEVICE_ATTR(diag_trigger_master, S_IRUGO | S_IWUSR,
3052 _ctl_diag_trigger_master_show, _ctl_diag_trigger_master_store);
3053
3054
3055/**
3056 * _ctl_diag_trigger_event_show - show the diag_trigger_event attribute
3057 * @cdev - pointer to embedded class device
3058 * @buf - the buffer returned
3059 *
3060 * A sysfs 'read/write' shost attribute.
3061 */
3062static ssize_t
3063_ctl_diag_trigger_event_show(struct device *cdev,
3064 struct device_attribute *attr, char *buf)
3065{
3066 struct Scsi_Host *shost = class_to_shost(cdev);
3067 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3068 unsigned long flags;
3069 ssize_t rc;
3070
3071 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3072 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3073 memcpy(buf, &ioc->diag_trigger_event, rc);
3074 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3075 return rc;
3076}
3077
3078/**
3079 * _ctl_diag_trigger_event_store - store the diag_trigger_event attribute
3080 * @cdev - pointer to embedded class device
3081 * @buf - the buffer returned
3082 *
3083 * A sysfs 'read/write' shost attribute.
3084 */
3085static ssize_t
3086_ctl_diag_trigger_event_store(struct device *cdev,
3087 struct device_attribute *attr, const char *buf, size_t count)
3088
3089{
3090 struct Scsi_Host *shost = class_to_shost(cdev);
3091 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3092 unsigned long flags;
3093 ssize_t sz;
3094
3095 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3096 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3097 memset(&ioc->diag_trigger_event, 0,
3098 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3099 memcpy(&ioc->diag_trigger_event, buf, sz);
3100 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3101 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3102 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3103 return sz;
3104}
3105static DEVICE_ATTR(diag_trigger_event, S_IRUGO | S_IWUSR,
3106 _ctl_diag_trigger_event_show, _ctl_diag_trigger_event_store);
3107
3108
3109/**
3110 * _ctl_diag_trigger_scsi_show - show the diag_trigger_scsi attribute
3111 * @cdev - pointer to embedded class device
3112 * @buf - the buffer returned
3113 *
3114 * A sysfs 'read/write' shost attribute.
3115 */
3116static ssize_t
3117_ctl_diag_trigger_scsi_show(struct device *cdev,
3118 struct device_attribute *attr, char *buf)
3119{
3120 struct Scsi_Host *shost = class_to_shost(cdev);
3121 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3122 unsigned long flags;
3123 ssize_t rc;
3124
3125 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3126 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3127 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3128 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3129 return rc;
3130}
3131
3132/**
3133 * _ctl_diag_trigger_scsi_store - store the diag_trigger_scsi attribute
3134 * @cdev - pointer to embedded class device
3135 * @buf - the buffer returned
3136 *
3137 * A sysfs 'read/write' shost attribute.
3138 */
3139static ssize_t
3140_ctl_diag_trigger_scsi_store(struct device *cdev,
3141 struct device_attribute *attr, const char *buf, size_t count)
3142{
3143 struct Scsi_Host *shost = class_to_shost(cdev);
3144 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3145 unsigned long flags;
3146 ssize_t sz;
3147
3148 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3149 sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
3150 memset(&ioc->diag_trigger_scsi, 0,
3151 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3152 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3153 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3154 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3155 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3156 return sz;
3157}
3158static DEVICE_ATTR(diag_trigger_scsi, S_IRUGO | S_IWUSR,
3159 _ctl_diag_trigger_scsi_show, _ctl_diag_trigger_scsi_store);
3160
3161
3162/**
3163 * _ctl_diag_trigger_scsi_show - show the diag_trigger_mpi attribute
3164 * @cdev - pointer to embedded class device
3165 * @buf - the buffer returned
3166 *
3167 * A sysfs 'read/write' shost attribute.
3168 */
3169static ssize_t
3170_ctl_diag_trigger_mpi_show(struct device *cdev,
3171 struct device_attribute *attr, char *buf)
3172{
3173 struct Scsi_Host *shost = class_to_shost(cdev);
3174 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3175 unsigned long flags;
3176 ssize_t rc;
3177
3178 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3179 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3180 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3181 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3182 return rc;
3183}
3184
3185/**
3186 * _ctl_diag_trigger_mpi_store - store the diag_trigger_mpi attribute
3187 * @cdev - pointer to embedded class device
3188 * @buf - the buffer returned
3189 *
3190 * A sysfs 'read/write' shost attribute.
3191 */
3192static ssize_t
3193_ctl_diag_trigger_mpi_store(struct device *cdev,
3194 struct device_attribute *attr, const char *buf, size_t count)
3195{
3196 struct Scsi_Host *shost = class_to_shost(cdev);
3197 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3198 unsigned long flags;
3199 ssize_t sz;
3200
3201 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3202 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3203 memset(&ioc->diag_trigger_mpi, 0,
Dan Carpenter66331e82012-12-07 13:56:22 +03003204 sizeof(ioc->diag_trigger_mpi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303205 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3206 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3207 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3208 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3209 return sz;
3210}
3211
3212static DEVICE_ATTR(diag_trigger_mpi, S_IRUGO | S_IWUSR,
3213 _ctl_diag_trigger_mpi_show, _ctl_diag_trigger_mpi_store);
3214
3215/*********** diagnostic trigger suppport *** END ****************************/
3216
3217
3218
3219/*****************************************/
3220
3221struct device_attribute *mpt3sas_host_attrs[] = {
3222 &dev_attr_version_fw,
3223 &dev_attr_version_bios,
3224 &dev_attr_version_mpi,
3225 &dev_attr_version_product,
3226 &dev_attr_version_nvdata_persistent,
3227 &dev_attr_version_nvdata_default,
3228 &dev_attr_board_name,
3229 &dev_attr_board_assembly,
3230 &dev_attr_board_tracer,
3231 &dev_attr_io_delay,
3232 &dev_attr_device_delay,
3233 &dev_attr_logging_level,
3234 &dev_attr_fwfault_debug,
3235 &dev_attr_fw_queue_depth,
3236 &dev_attr_host_sas_address,
3237 &dev_attr_ioc_reset_count,
3238 &dev_attr_host_trace_buffer_size,
3239 &dev_attr_host_trace_buffer,
3240 &dev_attr_host_trace_buffer_enable,
3241 &dev_attr_reply_queue_count,
3242 &dev_attr_diag_trigger_master,
3243 &dev_attr_diag_trigger_event,
3244 &dev_attr_diag_trigger_scsi,
3245 &dev_attr_diag_trigger_mpi,
Sreekanth Reddy42263092015-11-11 17:30:29 +05303246#ifdef SCSI_MPT2SAS
3247 &dev_attr_BRM_status,
3248#endif
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303249 NULL,
3250};
3251
3252/* device attributes */
3253
3254/**
3255 * _ctl_device_sas_address_show - sas address
3256 * @cdev - pointer to embedded class device
3257 * @buf - the buffer returned
3258 *
3259 * This is the sas address for the target
3260 *
3261 * A sysfs 'read-only' shost attribute.
3262 */
3263static ssize_t
3264_ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
3265 char *buf)
3266{
3267 struct scsi_device *sdev = to_scsi_device(dev);
3268 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3269
3270 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3271 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3272}
3273static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
3274
3275/**
3276 * _ctl_device_handle_show - device handle
3277 * @cdev - pointer to embedded class device
3278 * @buf - the buffer returned
3279 *
3280 * This is the firmware assigned device handle
3281 *
3282 * A sysfs 'read-only' shost attribute.
3283 */
3284static ssize_t
3285_ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
3286 char *buf)
3287{
3288 struct scsi_device *sdev = to_scsi_device(dev);
3289 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3290
3291 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3292 sas_device_priv_data->sas_target->handle);
3293}
3294static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
3295
3296struct device_attribute *mpt3sas_dev_attrs[] = {
3297 &dev_attr_sas_address,
3298 &dev_attr_sas_device_handle,
3299 NULL,
3300};
3301
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303302/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05303303 * ctl_init - main entry point for ctl.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303304 *
3305 */
3306void
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05303307ctl_init(void)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303308{
3309 async_queue = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303310 init_waitqueue_head(&ctl_poll_wait);
3311}
3312
3313/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05303314 * ctl_exit - exit point for ctl
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303315 *
3316 */
3317void
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05303318ctl_exit(void)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303319{
3320 struct MPT3SAS_ADAPTER *ioc;
3321 int i;
3322
3323 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3324
3325 /* free memory associated to diag buffers */
3326 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3327 if (!ioc->diag_buffer[i])
3328 continue;
3329 if (!(ioc->diag_buffer_status[i] &
3330 MPT3_DIAG_BUFFER_IS_REGISTERED))
3331 continue;
3332 if ((ioc->diag_buffer_status[i] &
3333 MPT3_DIAG_BUFFER_IS_RELEASED))
3334 continue;
3335 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3336 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3337 ioc->diag_buffer[i] = NULL;
3338 ioc->diag_buffer_status[i] = 0;
3339 }
3340
3341 kfree(ioc->event_log);
3342 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303343}