blob: 054d5231c974771e5af6a47fe4f8e62e074d2578 [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
6 * Copyright (C) 2012 LSI Corporation
7 * (mailto:DL-MPTFusionLinux@lsi.com)
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * NO WARRANTY
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
29
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 * USA.
43 */
44
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053045#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/errno.h>
48#include <linux/init.h>
49#include <linux/slab.h>
50#include <linux/types.h>
51#include <linux/pci.h>
52#include <linux/delay.h>
53#include <linux/compat.h>
54#include <linux/poll.h>
55
56#include <linux/io.h>
57#include <linux/uaccess.h>
58
59#include "mpt3sas_base.h"
60#include "mpt3sas_ctl.h"
61
62
63static struct fasync_struct *async_queue;
64static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
65
66
67/**
68 * enum block_state - blocking state
69 * @NON_BLOCKING: non blocking
70 * @BLOCKING: blocking
71 *
72 * These states are for ioctls that need to wait for a response
73 * from firmware, so they probably require sleep.
74 */
75enum block_state {
76 NON_BLOCKING,
77 BLOCKING,
78};
79
80#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
81/**
82 * _ctl_sas_device_find_by_handle - sas device search
83 * @ioc: per adapter object
84 * @handle: sas device handle (assigned by firmware)
85 * Context: Calling function should acquire ioc->sas_device_lock
86 *
87 * This searches for sas_device based on sas_address, then return sas_device
88 * object.
89 */
90static struct _sas_device *
91_ctl_sas_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
92{
93 struct _sas_device *sas_device, *r;
94
95 r = NULL;
96 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
97 if (sas_device->handle != handle)
98 continue;
99 r = sas_device;
100 goto out;
101 }
102
103 out:
104 return r;
105}
106
107/**
108 * _ctl_display_some_debug - debug routine
109 * @ioc: per adapter object
110 * @smid: system request message index
111 * @calling_function_name: string pass from calling function
112 * @mpi_reply: reply message frame
113 * Context: none.
114 *
115 * Function for displaying debug info helpful when debugging issues
116 * in this module.
117 */
118static void
119_ctl_display_some_debug(struct MPT3SAS_ADAPTER *ioc, u16 smid,
120 char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
121{
122 Mpi2ConfigRequest_t *mpi_request;
123 char *desc = NULL;
124
125 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
126 return;
127
128 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
129 switch (mpi_request->Function) {
130 case MPI2_FUNCTION_SCSI_IO_REQUEST:
131 {
132 Mpi2SCSIIORequest_t *scsi_request =
133 (Mpi2SCSIIORequest_t *)mpi_request;
134
135 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
136 "scsi_io, cmd(0x%02x), cdb_len(%d)",
137 scsi_request->CDB.CDB32[0],
138 le16_to_cpu(scsi_request->IoFlags) & 0xF);
139 desc = ioc->tmp_string;
140 break;
141 }
142 case MPI2_FUNCTION_SCSI_TASK_MGMT:
143 desc = "task_mgmt";
144 break;
145 case MPI2_FUNCTION_IOC_INIT:
146 desc = "ioc_init";
147 break;
148 case MPI2_FUNCTION_IOC_FACTS:
149 desc = "ioc_facts";
150 break;
151 case MPI2_FUNCTION_CONFIG:
152 {
153 Mpi2ConfigRequest_t *config_request =
154 (Mpi2ConfigRequest_t *)mpi_request;
155
156 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
157 "config, type(0x%02x), ext_type(0x%02x), number(%d)",
158 (config_request->Header.PageType &
159 MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
160 config_request->Header.PageNumber);
161 desc = ioc->tmp_string;
162 break;
163 }
164 case MPI2_FUNCTION_PORT_FACTS:
165 desc = "port_facts";
166 break;
167 case MPI2_FUNCTION_PORT_ENABLE:
168 desc = "port_enable";
169 break;
170 case MPI2_FUNCTION_EVENT_NOTIFICATION:
171 desc = "event_notification";
172 break;
173 case MPI2_FUNCTION_FW_DOWNLOAD:
174 desc = "fw_download";
175 break;
176 case MPI2_FUNCTION_FW_UPLOAD:
177 desc = "fw_upload";
178 break;
179 case MPI2_FUNCTION_RAID_ACTION:
180 desc = "raid_action";
181 break;
182 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
183 {
184 Mpi2SCSIIORequest_t *scsi_request =
185 (Mpi2SCSIIORequest_t *)mpi_request;
186
187 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
188 "raid_pass, cmd(0x%02x), cdb_len(%d)",
189 scsi_request->CDB.CDB32[0],
190 le16_to_cpu(scsi_request->IoFlags) & 0xF);
191 desc = ioc->tmp_string;
192 break;
193 }
194 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
195 desc = "sas_iounit_cntl";
196 break;
197 case MPI2_FUNCTION_SATA_PASSTHROUGH:
198 desc = "sata_pass";
199 break;
200 case MPI2_FUNCTION_DIAG_BUFFER_POST:
201 desc = "diag_buffer_post";
202 break;
203 case MPI2_FUNCTION_DIAG_RELEASE:
204 desc = "diag_release";
205 break;
206 case MPI2_FUNCTION_SMP_PASSTHROUGH:
207 desc = "smp_passthrough";
208 break;
209 }
210
211 if (!desc)
212 return;
213
214 pr_info(MPT3SAS_FMT "%s: %s, smid(%d)\n",
215 ioc->name, calling_function_name, desc, smid);
216
217 if (!mpi_reply)
218 return;
219
220 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
221 pr_info(MPT3SAS_FMT
222 "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
223 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
224 le32_to_cpu(mpi_reply->IOCLogInfo));
225
226 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
227 mpi_request->Function ==
228 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
229 Mpi2SCSIIOReply_t *scsi_reply =
230 (Mpi2SCSIIOReply_t *)mpi_reply;
231 struct _sas_device *sas_device = NULL;
232 unsigned long flags;
233
234 spin_lock_irqsave(&ioc->sas_device_lock, flags);
235 sas_device = _ctl_sas_device_find_by_handle(ioc,
236 le16_to_cpu(scsi_reply->DevHandle));
237 if (sas_device) {
238 pr_warn(MPT3SAS_FMT "\tsas_address(0x%016llx), phy(%d)\n",
239 ioc->name, (unsigned long long)
240 sas_device->sas_address, sas_device->phy);
241 pr_warn(MPT3SAS_FMT
242 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
243 ioc->name, (unsigned long long)
244 sas_device->enclosure_logical_id, sas_device->slot);
245 }
246 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
247 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
248 pr_info(MPT3SAS_FMT
249 "\tscsi_state(0x%02x), scsi_status"
250 "(0x%02x)\n", ioc->name,
251 scsi_reply->SCSIState,
252 scsi_reply->SCSIStatus);
253 }
254}
255
256#endif
257
258/**
259 * mpt3sas_ctl_done - ctl module completion routine
260 * @ioc: per adapter object
261 * @smid: system request message index
262 * @msix_index: MSIX table index supplied by the OS
263 * @reply: reply message frame(lower 32bit addr)
264 * Context: none.
265 *
266 * The callback handler when using ioc->ctl_cb_idx.
267 *
268 * Return 1 meaning mf should be freed from _base_interrupt
269 * 0 means the mf is freed from this function.
270 */
271u8
272mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
273 u32 reply)
274{
275 MPI2DefaultReply_t *mpi_reply;
276 Mpi2SCSIIOReply_t *scsiio_reply;
277 const void *sense_data;
278 u32 sz;
279
280 if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
281 return 1;
282 if (ioc->ctl_cmds.smid != smid)
283 return 1;
284 ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
285 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
286 if (mpi_reply) {
287 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
288 ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
289 /* get sense data */
290 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
291 mpi_reply->Function ==
292 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
293 scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
294 if (scsiio_reply->SCSIState &
295 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
296 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
297 le32_to_cpu(scsiio_reply->SenseCount));
298 sense_data = mpt3sas_base_get_sense_buffer(ioc,
299 smid);
300 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
301 }
302 }
303 }
304#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
305 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
306#endif
307 ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
308 complete(&ioc->ctl_cmds.done);
309 return 1;
310}
311
312/**
313 * _ctl_check_event_type - determines when an event needs logging
314 * @ioc: per adapter object
315 * @event: firmware event
316 *
317 * The bitmask in ioc->event_type[] indicates which events should be
318 * be saved in the driver event_log. This bitmask is set by application.
319 *
320 * Returns 1 when event should be captured, or zero means no match.
321 */
322static int
323_ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
324{
325 u16 i;
326 u32 desired_event;
327
328 if (event >= 128 || !event || !ioc->event_log)
329 return 0;
330
331 desired_event = (1 << (event % 32));
332 if (!desired_event)
333 desired_event = 1;
334 i = event / 32;
335 return desired_event & ioc->event_type[i];
336}
337
338/**
339 * mpt3sas_ctl_add_to_event_log - add event
340 * @ioc: per adapter object
341 * @mpi_reply: reply message frame
342 *
343 * Return nothing.
344 */
345void
346mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
347 Mpi2EventNotificationReply_t *mpi_reply)
348{
349 struct MPT3_IOCTL_EVENTS *event_log;
350 u16 event;
351 int i;
352 u32 sz, event_data_sz;
353 u8 send_aen = 0;
354
355 if (!ioc->event_log)
356 return;
357
358 event = le16_to_cpu(mpi_reply->Event);
359
360 if (_ctl_check_event_type(ioc, event)) {
361
362 /* insert entry into circular event_log */
363 i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
364 event_log = ioc->event_log;
365 event_log[i].event = event;
366 event_log[i].context = ioc->event_context++;
367
368 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
369 sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
370 memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
371 memcpy(event_log[i].data, mpi_reply->EventData, sz);
372 send_aen = 1;
373 }
374
375 /* This aen_event_read_flag flag is set until the
376 * application has read the event log.
377 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
378 */
379 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
380 (send_aen && !ioc->aen_event_read_flag)) {
381 ioc->aen_event_read_flag = 1;
382 wake_up_interruptible(&ctl_poll_wait);
383 if (async_queue)
384 kill_fasync(&async_queue, SIGIO, POLL_IN);
385 }
386}
387
388/**
389 * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
390 * @ioc: per adapter object
391 * @msix_index: MSIX table index supplied by the OS
392 * @reply: reply message frame(lower 32bit addr)
393 * Context: interrupt.
394 *
395 * This function merely adds a new work task into ioc->firmware_event_thread.
396 * The tasks are worked from _firmware_event_work in user context.
397 *
398 * Return 1 meaning mf should be freed from _base_interrupt
399 * 0 means the mf is freed from this function.
400 */
401u8
402mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
403 u32 reply)
404{
405 Mpi2EventNotificationReply_t *mpi_reply;
406
407 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
408 mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
409 return 1;
410}
411
412/**
413 * _ctl_verify_adapter - validates ioc_number passed from application
414 * @ioc: per adapter object
415 * @iocpp: The ioc pointer is returned in this.
416 *
417 * Return (-1) means error, else ioc_number.
418 */
419static int
420_ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp)
421{
422 struct MPT3SAS_ADAPTER *ioc;
423
424 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
425 if (ioc->id != ioc_number)
426 continue;
427 *iocpp = ioc;
428 return ioc_number;
429 }
430 *iocpp = NULL;
431 return -1;
432}
433
434/**
435 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
436 * @ioc: per adapter object
437 * @reset_phase: phase
438 *
439 * The handler for doing any required cleanup or initialization.
440 *
441 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
442 * MPT3_IOC_DONE_RESET
443 */
444void
445mpt3sas_ctl_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
446{
447 int i;
448 u8 issue_reset;
449
450 switch (reset_phase) {
451 case MPT3_IOC_PRE_RESET:
452 dtmprintk(ioc, pr_info(MPT3SAS_FMT
453 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
454 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
455 if (!(ioc->diag_buffer_status[i] &
456 MPT3_DIAG_BUFFER_IS_REGISTERED))
457 continue;
458 if ((ioc->diag_buffer_status[i] &
459 MPT3_DIAG_BUFFER_IS_RELEASED))
460 continue;
461 mpt3sas_send_diag_release(ioc, i, &issue_reset);
462 }
463 break;
464 case MPT3_IOC_AFTER_RESET:
465 dtmprintk(ioc, pr_info(MPT3SAS_FMT
466 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
467 if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
468 ioc->ctl_cmds.status |= MPT3_CMD_RESET;
469 mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
470 complete(&ioc->ctl_cmds.done);
471 }
472 break;
473 case MPT3_IOC_DONE_RESET:
474 dtmprintk(ioc, pr_info(MPT3SAS_FMT
475 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
476
477 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
478 if (!(ioc->diag_buffer_status[i] &
479 MPT3_DIAG_BUFFER_IS_REGISTERED))
480 continue;
481 if ((ioc->diag_buffer_status[i] &
482 MPT3_DIAG_BUFFER_IS_RELEASED))
483 continue;
484 ioc->diag_buffer_status[i] |=
485 MPT3_DIAG_BUFFER_IS_DIAG_RESET;
486 }
487 break;
488 }
489}
490
491/**
492 * _ctl_fasync -
493 * @fd -
494 * @filep -
495 * @mode -
496 *
497 * Called when application request fasyn callback handler.
498 */
499static int
500_ctl_fasync(int fd, struct file *filep, int mode)
501{
502 return fasync_helper(fd, filep, mode, &async_queue);
503}
504
505/**
506 * _ctl_release -
507 * @inode -
508 * @filep -
509 *
510 * Called when application releases the fasyn callback handler.
511 */
512static int
513_ctl_release(struct inode *inode, struct file *filep)
514{
515 return fasync_helper(-1, filep, 0, &async_queue);
516}
517
518/**
519 * _ctl_poll -
520 * @file -
521 * @wait -
522 *
523 */
524static unsigned int
525_ctl_poll(struct file *filep, poll_table *wait)
526{
527 struct MPT3SAS_ADAPTER *ioc;
528
529 poll_wait(filep, &ctl_poll_wait, wait);
530
531 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
532 if (ioc->aen_event_read_flag)
533 return POLLIN | POLLRDNORM;
534 }
535 return 0;
536}
537
538/**
539 * _ctl_set_task_mid - assign an active smid to tm request
540 * @ioc: per adapter object
541 * @karg - (struct mpt3_ioctl_command)
542 * @tm_request - pointer to mf from user space
543 *
544 * Returns 0 when an smid if found, else fail.
545 * during failure, the reply frame is filled.
546 */
547static int
548_ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
549 Mpi2SCSITaskManagementRequest_t *tm_request)
550{
551 u8 found = 0;
552 u16 i;
553 u16 handle;
554 struct scsi_cmnd *scmd;
555 struct MPT3SAS_DEVICE *priv_data;
556 unsigned long flags;
557 Mpi2SCSITaskManagementReply_t *tm_reply;
558 u32 sz;
559 u32 lun;
560 char *desc = NULL;
561
562 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
563 desc = "abort_task";
564 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
565 desc = "query_task";
566 else
567 return 0;
568
569 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
570
571 handle = le16_to_cpu(tm_request->DevHandle);
572 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
573 for (i = ioc->scsiio_depth; i && !found; i--) {
574 scmd = ioc->scsi_lookup[i - 1].scmd;
575 if (scmd == NULL || scmd->device == NULL ||
576 scmd->device->hostdata == NULL)
577 continue;
578 if (lun != scmd->device->lun)
579 continue;
580 priv_data = scmd->device->hostdata;
581 if (priv_data->sas_target == NULL)
582 continue;
583 if (priv_data->sas_target->handle != handle)
584 continue;
585 tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
586 found = 1;
587 }
588 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
589
590 if (!found) {
591 dctlprintk(ioc, pr_info(MPT3SAS_FMT
592 "%s: handle(0x%04x), lun(%d), no active mid!!\n",
593 ioc->name,
594 desc, le16_to_cpu(tm_request->DevHandle), lun));
595 tm_reply = ioc->ctl_cmds.reply;
596 tm_reply->DevHandle = tm_request->DevHandle;
597 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
598 tm_reply->TaskType = tm_request->TaskType;
599 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
600 tm_reply->VP_ID = tm_request->VP_ID;
601 tm_reply->VF_ID = tm_request->VF_ID;
602 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
603 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
604 sz))
605 pr_err("failure at %s:%d/%s()!\n", __FILE__,
606 __LINE__, __func__);
607 return 1;
608 }
609
610 dctlprintk(ioc, pr_info(MPT3SAS_FMT
611 "%s: handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
612 desc, le16_to_cpu(tm_request->DevHandle), lun,
613 le16_to_cpu(tm_request->TaskMID)));
614 return 0;
615}
616
617/**
618 * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
619 * @ioc: per adapter object
620 * @karg - (struct mpt3_ioctl_command)
621 * @mf - pointer to mf in user space
622 */
623static long
624_ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
625 void __user *mf)
626{
627 MPI2RequestHeader_t *mpi_request = NULL, *request;
628 MPI2DefaultReply_t *mpi_reply;
629 u32 ioc_state;
630 u16 ioc_status;
631 u16 smid;
632 unsigned long timeout, timeleft;
633 u8 issue_reset;
634 u32 sz;
635 void *psge;
636 void *data_out = NULL;
637 dma_addr_t data_out_dma = 0;
638 size_t data_out_sz = 0;
639 void *data_in = NULL;
640 dma_addr_t data_in_dma = 0;
641 size_t data_in_sz = 0;
642 long ret;
643 u16 wait_state_count;
644
645 issue_reset = 0;
646
647 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
648 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
649 ioc->name, __func__);
650 ret = -EAGAIN;
651 goto out;
652 }
653
654 wait_state_count = 0;
655 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
656 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
657 if (wait_state_count++ == 10) {
658 pr_err(MPT3SAS_FMT
659 "%s: failed due to ioc not operational\n",
660 ioc->name, __func__);
661 ret = -EFAULT;
662 goto out;
663 }
664 ssleep(1);
665 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
666 pr_info(MPT3SAS_FMT
667 "%s: waiting for operational state(count=%d)\n",
668 ioc->name,
669 __func__, wait_state_count);
670 }
671 if (wait_state_count)
672 pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
673 ioc->name, __func__);
674
675 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
676 if (!mpi_request) {
677 pr_err(MPT3SAS_FMT
678 "%s: failed obtaining a memory for mpi_request\n",
679 ioc->name, __func__);
680 ret = -ENOMEM;
681 goto out;
682 }
683
684 /* Check for overflow and wraparound */
685 if (karg.data_sge_offset * 4 > ioc->request_sz ||
686 karg.data_sge_offset > (UINT_MAX / 4)) {
687 ret = -EINVAL;
688 goto out;
689 }
690
691 /* copy in request message frame from user */
692 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
693 pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
694 __func__);
695 ret = -EFAULT;
696 goto out;
697 }
698
699 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
700 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
701 if (!smid) {
702 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
703 ioc->name, __func__);
704 ret = -EAGAIN;
705 goto out;
706 }
707 } else {
708
709 smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
710 if (!smid) {
711 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
712 ioc->name, __func__);
713 ret = -EAGAIN;
714 goto out;
715 }
716 }
717
718 ret = 0;
719 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
720 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
721 request = mpt3sas_base_get_msg_frame(ioc, smid);
722 memcpy(request, mpi_request, karg.data_sge_offset*4);
723 ioc->ctl_cmds.smid = smid;
724 data_out_sz = karg.data_out_size;
725 data_in_sz = karg.data_in_size;
726
727 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
728 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
729 if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
730 le16_to_cpu(mpi_request->FunctionDependent1) >
731 ioc->facts.MaxDevHandle) {
732 ret = -EINVAL;
733 mpt3sas_base_free_smid(ioc, smid);
734 goto out;
735 }
736 }
737
738 /* obtain dma-able memory for data transfer */
739 if (data_out_sz) /* WRITE */ {
740 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
741 &data_out_dma);
742 if (!data_out) {
743 pr_err("failure at %s:%d/%s()!\n", __FILE__,
744 __LINE__, __func__);
745 ret = -ENOMEM;
746 mpt3sas_base_free_smid(ioc, smid);
747 goto out;
748 }
749 if (copy_from_user(data_out, karg.data_out_buf_ptr,
750 data_out_sz)) {
751 pr_err("failure at %s:%d/%s()!\n", __FILE__,
752 __LINE__, __func__);
753 ret = -EFAULT;
754 mpt3sas_base_free_smid(ioc, smid);
755 goto out;
756 }
757 }
758
759 if (data_in_sz) /* READ */ {
760 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
761 &data_in_dma);
762 if (!data_in) {
763 pr_err("failure at %s:%d/%s()!\n", __FILE__,
764 __LINE__, __func__);
765 ret = -ENOMEM;
766 mpt3sas_base_free_smid(ioc, smid);
767 goto out;
768 }
769 }
770
771 psge = (void *)request + (karg.data_sge_offset*4);
772
773 /* send command to firmware */
774#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
775 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
776#endif
777
778 init_completion(&ioc->ctl_cmds.done);
779 switch (mpi_request->Function) {
780 case MPI2_FUNCTION_SCSI_IO_REQUEST:
781 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
782 {
783 Mpi2SCSIIORequest_t *scsiio_request =
784 (Mpi2SCSIIORequest_t *)request;
785 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
786 scsiio_request->SenseBufferLowAddress =
787 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
788 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
789 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
790 data_in_dma, data_in_sz);
791
792 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
793 mpt3sas_base_put_smid_scsi_io(ioc, smid,
794 le16_to_cpu(mpi_request->FunctionDependent1));
795 else
796 mpt3sas_base_put_smid_default(ioc, smid);
797 break;
798 }
799 case MPI2_FUNCTION_SCSI_TASK_MGMT:
800 {
801 Mpi2SCSITaskManagementRequest_t *tm_request =
802 (Mpi2SCSITaskManagementRequest_t *)request;
803
804 dtmprintk(ioc, pr_info(MPT3SAS_FMT
805 "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
806 ioc->name,
807 le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
808
809 if (tm_request->TaskType ==
810 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
811 tm_request->TaskType ==
812 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
813 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
814 mpt3sas_base_free_smid(ioc, smid);
815 goto out;
816 }
817 }
818
819 mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
820 tm_request->DevHandle));
821 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
822 data_in_dma, data_in_sz);
823 mpt3sas_base_put_smid_hi_priority(ioc, smid);
824 break;
825 }
826 case MPI2_FUNCTION_SMP_PASSTHROUGH:
827 {
828 Mpi2SmpPassthroughRequest_t *smp_request =
829 (Mpi2SmpPassthroughRequest_t *)mpi_request;
830 u8 *data;
831
832 /* ioc determines which port to use */
833 smp_request->PhysicalPort = 0xFF;
834 if (smp_request->PassthroughFlags &
835 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
836 data = (u8 *)&smp_request->SGL;
837 else {
838 if (unlikely(data_out == NULL)) {
839 pr_err("failure at %s:%d/%s()!\n",
840 __FILE__, __LINE__, __func__);
841 mpt3sas_base_free_smid(ioc, smid);
842 ret = -EINVAL;
843 goto out;
844 }
845 data = data_out;
846 }
847
848 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
849 ioc->ioc_link_reset_in_progress = 1;
850 ioc->ignore_loginfos = 1;
851 }
852 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
853 data_in_sz);
854 mpt3sas_base_put_smid_default(ioc, smid);
855 break;
856 }
857 case MPI2_FUNCTION_SATA_PASSTHROUGH:
858 case MPI2_FUNCTION_FW_DOWNLOAD:
859 case MPI2_FUNCTION_FW_UPLOAD:
860 {
861 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
862 data_in_sz);
863 mpt3sas_base_put_smid_default(ioc, smid);
864 break;
865 }
866 case MPI2_FUNCTION_TOOLBOX:
867 {
868 Mpi2ToolboxCleanRequest_t *toolbox_request =
869 (Mpi2ToolboxCleanRequest_t *)mpi_request;
870
871 if (toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL) {
872 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
873 data_in_dma, data_in_sz);
874 } else {
875 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
876 data_in_dma, data_in_sz);
877 }
878 mpt3sas_base_put_smid_default(ioc, smid);
879 break;
880 }
881 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
882 {
883 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
884 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
885
886 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
887 || sasiounit_request->Operation ==
888 MPI2_SAS_OP_PHY_LINK_RESET) {
889 ioc->ioc_link_reset_in_progress = 1;
890 ioc->ignore_loginfos = 1;
891 }
892 /* drop to default case for posting the request */
893 }
894 default:
895 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
896 data_in_dma, data_in_sz);
897 mpt3sas_base_put_smid_default(ioc, smid);
898 break;
899 }
900
901 if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
902 timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
903 else
904 timeout = karg.timeout;
905 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
906 timeout*HZ);
907 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
908 Mpi2SCSITaskManagementRequest_t *tm_request =
909 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
910 mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
911 tm_request->DevHandle));
912 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
913 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
914 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
915 ioc->ioc_link_reset_in_progress) {
916 ioc->ioc_link_reset_in_progress = 0;
917 ioc->ignore_loginfos = 0;
918 }
919 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
920 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
921 __func__);
922 _debug_dump_mf(mpi_request, karg.data_sge_offset);
923 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
924 issue_reset = 1;
925 goto issue_host_reset;
926 }
927
928 mpi_reply = ioc->ctl_cmds.reply;
929 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
930
931#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
932 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
933 (ioc->logging_level & MPT_DEBUG_TM)) {
934 Mpi2SCSITaskManagementReply_t *tm_reply =
935 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
936
937 pr_info(MPT3SAS_FMT "TASK_MGMT: " \
938 "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
939 "TerminationCount(0x%08x)\n", ioc->name,
940 le16_to_cpu(tm_reply->IOCStatus),
941 le32_to_cpu(tm_reply->IOCLogInfo),
942 le32_to_cpu(tm_reply->TerminationCount));
943 }
944#endif
945 /* copy out xdata to user */
946 if (data_in_sz) {
947 if (copy_to_user(karg.data_in_buf_ptr, data_in,
948 data_in_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 reply message frame to user */
957 if (karg.max_reply_bytes) {
958 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
959 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
960 sz)) {
961 pr_err("failure at %s:%d/%s()!\n", __FILE__,
962 __LINE__, __func__);
963 ret = -ENODATA;
964 goto out;
965 }
966 }
967
968 /* copy out sense to user */
969 if (karg.max_sense_bytes && (mpi_request->Function ==
970 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
971 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
972 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
973 if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
974 sz)) {
975 pr_err("failure at %s:%d/%s()!\n", __FILE__,
976 __LINE__, __func__);
977 ret = -ENODATA;
978 goto out;
979 }
980 }
981
982 issue_host_reset:
983 if (issue_reset) {
984 ret = -ENODATA;
985 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
986 mpi_request->Function ==
987 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
988 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
989 pr_info(MPT3SAS_FMT "issue target reset: handle = (0x%04x)\n",
990 ioc->name,
991 le16_to_cpu(mpi_request->FunctionDependent1));
992 mpt3sas_halt_firmware(ioc);
993 mpt3sas_scsih_issue_tm(ioc,
994 le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
995 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30,
996 0, TM_MUTEX_ON);
997 } else
998 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
999 FORCE_BIG_HAMMER);
1000 }
1001
1002 out:
1003
1004 /* free memory associated with sg buffers */
1005 if (data_in)
1006 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1007 data_in_dma);
1008
1009 if (data_out)
1010 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1011 data_out_dma);
1012
1013 kfree(mpi_request);
1014 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1015 return ret;
1016}
1017
1018/**
1019 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1020 * @ioc: per adapter object
1021 * @arg - user space buffer containing ioctl content
1022 */
1023static long
1024_ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1025{
1026 struct mpt3_ioctl_iocinfo karg;
1027
1028 if (copy_from_user(&karg, arg, sizeof(karg))) {
1029 pr_err("failure at %s:%d/%s()!\n",
1030 __FILE__, __LINE__, __func__);
1031 return -EFAULT;
1032 }
1033
1034 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1035 __func__));
1036
1037 memset(&karg, 0 , sizeof(karg));
1038 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1039 if (ioc->pfacts)
1040 karg.port_number = ioc->pfacts[0].PortNumber;
1041 karg.hw_rev = ioc->pdev->revision;
1042 karg.pci_id = ioc->pdev->device;
1043 karg.subsystem_device = ioc->pdev->subsystem_device;
1044 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1045 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1046 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1047 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1048 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1049 karg.firmware_version = ioc->facts.FWVersion.Word;
1050 strcpy(karg.driver_version, MPT3SAS_DRIVER_NAME);
1051 strcat(karg.driver_version, "-");
1052 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1053 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/**
2313 * _ctl_ioctl - main ioctl entry point (unlocked)
2314 * @file - (struct file)
2315 * @cmd - ioctl opcode
2316 * @arg -
2317 */
2318static long
2319_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2320{
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/**
2329 * _ctl_ioctl_compat - main ioctl entry point (compat)
2330 * @file -
2331 * @cmd -
2332 * @arg -
2333 *
2334 * This routine handles 32 bit applications in 64bit os.
2335 */
2336static long
2337_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2338{
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
3233static const struct file_operations ctl_fops = {
3234 .owner = THIS_MODULE,
3235 .unlocked_ioctl = _ctl_ioctl,
3236 .release = _ctl_release,
3237 .poll = _ctl_poll,
3238 .fasync = _ctl_fasync,
3239#ifdef CONFIG_COMPAT
3240 .compat_ioctl = _ctl_ioctl_compat,
3241#endif
3242};
3243
3244static struct miscdevice ctl_dev = {
3245 .minor = MPT3SAS_MINOR,
3246 .name = MPT3SAS_DEV_NAME,
3247 .fops = &ctl_fops,
3248};
3249
3250/**
3251 * mpt3sas_ctl_init - main entry point for ctl.
3252 *
3253 */
3254void
3255mpt3sas_ctl_init(void)
3256{
3257 async_queue = NULL;
3258 if (misc_register(&ctl_dev) < 0)
3259 pr_err("%s can't register misc device [minor=%d]\n",
3260 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3261
3262 init_waitqueue_head(&ctl_poll_wait);
3263}
3264
3265/**
3266 * mpt3sas_ctl_exit - exit point for ctl
3267 *
3268 */
3269void
3270mpt3sas_ctl_exit(void)
3271{
3272 struct MPT3SAS_ADAPTER *ioc;
3273 int i;
3274
3275 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3276
3277 /* free memory associated to diag buffers */
3278 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3279 if (!ioc->diag_buffer[i])
3280 continue;
3281 if (!(ioc->diag_buffer_status[i] &
3282 MPT3_DIAG_BUFFER_IS_REGISTERED))
3283 continue;
3284 if ((ioc->diag_buffer_status[i] &
3285 MPT3_DIAG_BUFFER_IS_RELEASED))
3286 continue;
3287 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3288 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3289 ioc->diag_buffer[i] = NULL;
3290 ioc->diag_buffer_status[i] = 0;
3291 }
3292
3293 kfree(ioc->event_log);
3294 }
3295 misc_deregister(&ctl_dev);
3296}