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