]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/scsi/mpt3sas/mpt3sas_ctl.c
treewide: Use fallthrough pseudo-keyword
[mirror_ubuntu-jammy-kernel.git] / drivers / scsi / mpt3sas / mpt3sas_ctl.c
CommitLineData
f92363d1
SR
1/*
2 * Management Module Support for MPT (Message Passing Technology) based
3 * controllers
4 *
5 * This code is based on drivers/scsi/mpt3sas/mpt3sas_ctl.c
a4ffce0d 6 * Copyright (C) 2012-2014 LSI Corporation
a03bd153
SR
7 * Copyright (C) 2013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
f92363d1
SR
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * NO WARRANTY
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
30
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
43 * USA.
44 */
45
f92363d1
SR
46#include <linux/kernel.h>
47#include <linux/module.h>
48#include <linux/errno.h>
49#include <linux/init.h>
50#include <linux/slab.h>
51#include <linux/types.h>
52#include <linux/pci.h>
53#include <linux/delay.h>
54#include <linux/compat.h>
55#include <linux/poll.h>
56
57#include <linux/io.h>
58#include <linux/uaccess.h>
59
60#include "mpt3sas_base.h"
61#include "mpt3sas_ctl.h"
62
63
64static struct fasync_struct *async_queue;
65static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
66
67
68/**
69 * enum block_state - blocking state
70 * @NON_BLOCKING: non blocking
71 * @BLOCKING: blocking
72 *
73 * These states are for ioctls that need to wait for a response
74 * from firmware, so they probably require sleep.
75 */
76enum block_state {
77 NON_BLOCKING,
78 BLOCKING,
79};
80
f92363d1
SR
81/**
82 * _ctl_display_some_debug - debug routine
83 * @ioc: per adapter object
84 * @smid: system request message index
85 * @calling_function_name: string pass from calling function
86 * @mpi_reply: reply message frame
87 * Context: none.
88 *
89 * Function for displaying debug info helpful when debugging issues
90 * in this module.
91 */
92static void
93_ctl_display_some_debug(struct MPT3SAS_ADAPTER *ioc, u16 smid,
94 char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
95{
96 Mpi2ConfigRequest_t *mpi_request;
97 char *desc = NULL;
98
99 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
100 return;
101
102 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
103 switch (mpi_request->Function) {
104 case MPI2_FUNCTION_SCSI_IO_REQUEST:
105 {
106 Mpi2SCSIIORequest_t *scsi_request =
107 (Mpi2SCSIIORequest_t *)mpi_request;
108
109 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
110 "scsi_io, cmd(0x%02x), cdb_len(%d)",
111 scsi_request->CDB.CDB32[0],
112 le16_to_cpu(scsi_request->IoFlags) & 0xF);
113 desc = ioc->tmp_string;
114 break;
115 }
116 case MPI2_FUNCTION_SCSI_TASK_MGMT:
117 desc = "task_mgmt";
118 break;
119 case MPI2_FUNCTION_IOC_INIT:
120 desc = "ioc_init";
121 break;
122 case MPI2_FUNCTION_IOC_FACTS:
123 desc = "ioc_facts";
124 break;
125 case MPI2_FUNCTION_CONFIG:
126 {
127 Mpi2ConfigRequest_t *config_request =
128 (Mpi2ConfigRequest_t *)mpi_request;
129
130 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
131 "config, type(0x%02x), ext_type(0x%02x), number(%d)",
132 (config_request->Header.PageType &
133 MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
134 config_request->Header.PageNumber);
135 desc = ioc->tmp_string;
136 break;
137 }
138 case MPI2_FUNCTION_PORT_FACTS:
139 desc = "port_facts";
140 break;
141 case MPI2_FUNCTION_PORT_ENABLE:
142 desc = "port_enable";
143 break;
144 case MPI2_FUNCTION_EVENT_NOTIFICATION:
145 desc = "event_notification";
146 break;
147 case MPI2_FUNCTION_FW_DOWNLOAD:
148 desc = "fw_download";
149 break;
150 case MPI2_FUNCTION_FW_UPLOAD:
151 desc = "fw_upload";
152 break;
153 case MPI2_FUNCTION_RAID_ACTION:
154 desc = "raid_action";
155 break;
156 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
157 {
158 Mpi2SCSIIORequest_t *scsi_request =
159 (Mpi2SCSIIORequest_t *)mpi_request;
160
161 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
162 "raid_pass, cmd(0x%02x), cdb_len(%d)",
163 scsi_request->CDB.CDB32[0],
164 le16_to_cpu(scsi_request->IoFlags) & 0xF);
165 desc = ioc->tmp_string;
166 break;
167 }
168 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
169 desc = "sas_iounit_cntl";
170 break;
171 case MPI2_FUNCTION_SATA_PASSTHROUGH:
172 desc = "sata_pass";
173 break;
174 case MPI2_FUNCTION_DIAG_BUFFER_POST:
175 desc = "diag_buffer_post";
176 break;
177 case MPI2_FUNCTION_DIAG_RELEASE:
178 desc = "diag_release";
179 break;
180 case MPI2_FUNCTION_SMP_PASSTHROUGH:
181 desc = "smp_passthrough";
182 break;
5b061980
SR
183 case MPI2_FUNCTION_TOOLBOX:
184 desc = "toolbox";
185 break;
186 case MPI2_FUNCTION_NVME_ENCAPSULATED:
187 desc = "nvme_encapsulated";
188 break;
f92363d1
SR
189 }
190
191 if (!desc)
192 return;
193
919d8a3f 194 ioc_info(ioc, "%s: %s, smid(%d)\n", calling_function_name, desc, smid);
f92363d1
SR
195
196 if (!mpi_reply)
197 return;
198
199 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
919d8a3f
JP
200 ioc_info(ioc, "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
201 le16_to_cpu(mpi_reply->IOCStatus),
202 le32_to_cpu(mpi_reply->IOCLogInfo));
f92363d1
SR
203
204 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
205 mpi_request->Function ==
206 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
207 Mpi2SCSIIOReply_t *scsi_reply =
208 (Mpi2SCSIIOReply_t *)mpi_reply;
209 struct _sas_device *sas_device = NULL;
45aa6a1a 210 struct _pcie_device *pcie_device = NULL;
f92363d1 211
45aa6a1a 212 sas_device = mpt3sas_get_sdev_by_handle(ioc,
f92363d1
SR
213 le16_to_cpu(scsi_reply->DevHandle));
214 if (sas_device) {
919d8a3f
JP
215 ioc_warn(ioc, "\tsas_address(0x%016llx), phy(%d)\n",
216 (u64)sas_device->sas_address,
217 sas_device->phy);
218 ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
219 (u64)sas_device->enclosure_logical_id,
220 sas_device->slot);
45aa6a1a
SPS
221 sas_device_put(sas_device);
222 }
223 if (!sas_device) {
224 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
225 le16_to_cpu(scsi_reply->DevHandle));
226 if (pcie_device) {
919d8a3f
JP
227 ioc_warn(ioc, "\tWWID(0x%016llx), port(%d)\n",
228 (unsigned long long)pcie_device->wwid,
229 pcie_device->port_num);
45aa6a1a 230 if (pcie_device->enclosure_handle != 0)
919d8a3f
JP
231 ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
232 (u64)pcie_device->enclosure_logical_id,
233 pcie_device->slot);
45aa6a1a
SPS
234 pcie_device_put(pcie_device);
235 }
f92363d1 236 }
f92363d1 237 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
919d8a3f
JP
238 ioc_info(ioc, "\tscsi_state(0x%02x), scsi_status(0x%02x)\n",
239 scsi_reply->SCSIState,
240 scsi_reply->SCSIStatus);
f92363d1
SR
241 }
242}
243
f92363d1
SR
244/**
245 * mpt3sas_ctl_done - ctl module completion routine
246 * @ioc: per adapter object
247 * @smid: system request message index
248 * @msix_index: MSIX table index supplied by the OS
249 * @reply: reply message frame(lower 32bit addr)
250 * Context: none.
251 *
252 * The callback handler when using ioc->ctl_cb_idx.
253 *
4beb4867
BVA
254 * Return: 1 meaning mf should be freed from _base_interrupt
255 * 0 means the mf is freed from this function.
f92363d1
SR
256 */
257u8
258mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
259 u32 reply)
260{
261 MPI2DefaultReply_t *mpi_reply;
262 Mpi2SCSIIOReply_t *scsiio_reply;
aff39e61 263 Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
f92363d1
SR
264 const void *sense_data;
265 u32 sz;
266
267 if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
268 return 1;
269 if (ioc->ctl_cmds.smid != smid)
270 return 1;
271 ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
272 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
273 if (mpi_reply) {
274 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
275 ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
276 /* get sense data */
277 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
278 mpi_reply->Function ==
279 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
280 scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
281 if (scsiio_reply->SCSIState &
282 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
283 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
284 le32_to_cpu(scsiio_reply->SenseCount));
285 sense_data = mpt3sas_base_get_sense_buffer(ioc,
286 smid);
287 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
288 }
289 }
aff39e61
SPS
290 /*
291 * Get Error Response data for NVMe device. The ctl_cmds.sense
292 * buffer is used to store the Error Response data.
293 */
294 if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
295 nvme_error_reply =
296 (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
297 sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
cf6bf971 298 le16_to_cpu(nvme_error_reply->ErrorResponseCount));
aff39e61
SPS
299 sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
300 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
301 }
f92363d1 302 }
016d5c35 303
f92363d1 304 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
f92363d1
SR
305 ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
306 complete(&ioc->ctl_cmds.done);
307 return 1;
308}
309
310/**
311 * _ctl_check_event_type - determines when an event needs logging
312 * @ioc: per adapter object
313 * @event: firmware event
314 *
315 * The bitmask in ioc->event_type[] indicates which events should be
316 * be saved in the driver event_log. This bitmask is set by application.
317 *
4beb4867 318 * Return: 1 when event should be captured, or zero means no match.
f92363d1
SR
319 */
320static int
321_ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
322{
323 u16 i;
324 u32 desired_event;
325
326 if (event >= 128 || !event || !ioc->event_log)
327 return 0;
328
329 desired_event = (1 << (event % 32));
330 if (!desired_event)
331 desired_event = 1;
332 i = event / 32;
333 return desired_event & ioc->event_type[i];
334}
335
336/**
337 * mpt3sas_ctl_add_to_event_log - add event
338 * @ioc: per adapter object
339 * @mpi_reply: reply message frame
f92363d1
SR
340 */
341void
342mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
343 Mpi2EventNotificationReply_t *mpi_reply)
344{
345 struct MPT3_IOCTL_EVENTS *event_log;
346 u16 event;
347 int i;
348 u32 sz, event_data_sz;
349 u8 send_aen = 0;
350
351 if (!ioc->event_log)
352 return;
353
354 event = le16_to_cpu(mpi_reply->Event);
355
356 if (_ctl_check_event_type(ioc, event)) {
357
358 /* insert entry into circular event_log */
359 i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
360 event_log = ioc->event_log;
361 event_log[i].event = event;
362 event_log[i].context = ioc->event_context++;
363
364 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
365 sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
366 memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
367 memcpy(event_log[i].data, mpi_reply->EventData, sz);
368 send_aen = 1;
369 }
370
371 /* This aen_event_read_flag flag is set until the
372 * application has read the event log.
373 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
374 */
375 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
376 (send_aen && !ioc->aen_event_read_flag)) {
377 ioc->aen_event_read_flag = 1;
378 wake_up_interruptible(&ctl_poll_wait);
379 if (async_queue)
380 kill_fasync(&async_queue, SIGIO, POLL_IN);
381 }
382}
383
384/**
385 * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
386 * @ioc: per adapter object
387 * @msix_index: MSIX table index supplied by the OS
388 * @reply: reply message frame(lower 32bit addr)
389 * Context: interrupt.
390 *
391 * This function merely adds a new work task into ioc->firmware_event_thread.
392 * The tasks are worked from _firmware_event_work in user context.
393 *
4beb4867
BVA
394 * Return: 1 meaning mf should be freed from _base_interrupt
395 * 0 means the mf is freed from this function.
f92363d1
SR
396 */
397u8
398mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
399 u32 reply)
400{
401 Mpi2EventNotificationReply_t *mpi_reply;
402
403 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
869817f9
SS
404 if (mpi_reply)
405 mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
f92363d1
SR
406 return 1;
407}
408
409/**
410 * _ctl_verify_adapter - validates ioc_number passed from application
4beb4867 411 * @ioc_number: ?
f92363d1 412 * @iocpp: The ioc pointer is returned in this.
c84b06a4 413 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
b130b0d5 414 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
f92363d1 415 *
4beb4867 416 * Return: (-1) means error, else ioc_number.
f92363d1
SR
417 */
418static int
c84b06a4
SR
419_ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
420 int mpi_version)
f92363d1
SR
421{
422 struct MPT3SAS_ADAPTER *ioc;
b130b0d5 423 int version = 0;
08c4d550
SR
424 /* global ioc lock to protect controller on list operations */
425 spin_lock(&gioc_lock);
f92363d1
SR
426 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
427 if (ioc->id != ioc_number)
428 continue;
c84b06a4
SR
429 /* Check whether this ioctl command is from right
430 * ioctl device or not, if not continue the search.
431 */
b130b0d5
SS
432 version = ioc->hba_mpi_version_belonged;
433 /* MPI25_VERSION and MPI26_VERSION uses same ioctl
434 * device.
435 */
436 if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
437 if ((version == MPI25_VERSION) ||
438 (version == MPI26_VERSION))
439 goto out;
440 else
441 continue;
442 } else {
443 if (version != mpi_version)
444 continue;
445 }
446out:
08c4d550 447 spin_unlock(&gioc_lock);
f92363d1
SR
448 *iocpp = ioc;
449 return ioc_number;
450 }
08c4d550 451 spin_unlock(&gioc_lock);
f92363d1
SR
452 *iocpp = NULL;
453 return -1;
454}
455
456/**
457 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
458 * @ioc: per adapter object
f92363d1
SR
459 *
460 * The handler for doing any required cleanup or initialization.
f92363d1 461 */
c7a35705 462void mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
f92363d1
SR
463{
464 int i;
465 u8 issue_reset;
466
919d8a3f 467 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
c7a35705
BVA
468 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
469 if (!(ioc->diag_buffer_status[i] &
470 MPT3_DIAG_BUFFER_IS_REGISTERED))
471 continue;
472 if ((ioc->diag_buffer_status[i] &
473 MPT3_DIAG_BUFFER_IS_RELEASED))
474 continue;
4bc50dc1
SR
475
476 /*
477 * add a log message to indicate the release
478 */
479 ioc_info(ioc,
480 "%s: Releasing the trace buffer due to adapter reset.",
481 __func__);
c7a35705
BVA
482 mpt3sas_send_diag_release(ioc, i, &issue_reset);
483 }
484}
485
486/**
36c6c7f7 487 * mpt3sas_ctl_reset_handler - clears outstanding ioctl cmd.
c7a35705
BVA
488 * @ioc: per adapter object
489 *
490 * The handler for doing any required cleanup or initialization.
491 */
36c6c7f7 492void mpt3sas_ctl_clear_outstanding_ioctls(struct MPT3SAS_ADAPTER *ioc)
c7a35705 493{
36c6c7f7
SR
494 dtmprintk(ioc,
495 ioc_info(ioc, "%s: clear outstanding ioctl cmd\n", __func__));
c7a35705
BVA
496 if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
497 ioc->ctl_cmds.status |= MPT3_CMD_RESET;
498 mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
499 complete(&ioc->ctl_cmds.done);
500 }
501}
502
503/**
504 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
505 * @ioc: per adapter object
506 *
507 * The handler for doing any required cleanup or initialization.
508 */
509void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
510{
511 int i;
512
919d8a3f 513 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
f92363d1 514
c7a35705
BVA
515 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
516 if (!(ioc->diag_buffer_status[i] &
517 MPT3_DIAG_BUFFER_IS_REGISTERED))
518 continue;
519 if ((ioc->diag_buffer_status[i] &
520 MPT3_DIAG_BUFFER_IS_RELEASED))
521 continue;
522 ioc->diag_buffer_status[i] |=
523 MPT3_DIAG_BUFFER_IS_DIAG_RESET;
f92363d1
SR
524 }
525}
526
527/**
c84b06a4 528 * _ctl_fasync -
4beb4867
BVA
529 * @fd: ?
530 * @filep: ?
531 * @mode: ?
f92363d1
SR
532 *
533 * Called when application request fasyn callback handler.
534 */
8bbb1cf6 535static int
c84b06a4 536_ctl_fasync(int fd, struct file *filep, int mode)
f92363d1
SR
537{
538 return fasync_helper(fd, filep, mode, &async_queue);
539}
540
f92363d1 541/**
c84b06a4 542 * _ctl_poll -
4beb4867
BVA
543 * @filep: ?
544 * @wait: ?
f92363d1
SR
545 *
546 */
afc9a42b 547static __poll_t
c84b06a4 548_ctl_poll(struct file *filep, poll_table *wait)
f92363d1
SR
549{
550 struct MPT3SAS_ADAPTER *ioc;
551
552 poll_wait(filep, &ctl_poll_wait, wait);
553
08c4d550
SR
554 /* global ioc lock to protect controller on list operations */
555 spin_lock(&gioc_lock);
f92363d1 556 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
08c4d550
SR
557 if (ioc->aen_event_read_flag) {
558 spin_unlock(&gioc_lock);
a9a08845 559 return EPOLLIN | EPOLLRDNORM;
08c4d550 560 }
f92363d1 561 }
08c4d550 562 spin_unlock(&gioc_lock);
f92363d1
SR
563 return 0;
564}
565
566/**
567 * _ctl_set_task_mid - assign an active smid to tm request
568 * @ioc: per adapter object
4beb4867
BVA
569 * @karg: (struct mpt3_ioctl_command)
570 * @tm_request: pointer to mf from user space
f92363d1 571 *
4beb4867 572 * Return: 0 when an smid if found, else fail.
f92363d1
SR
573 * during failure, the reply frame is filled.
574 */
575static int
576_ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
577 Mpi2SCSITaskManagementRequest_t *tm_request)
578{
579 u8 found = 0;
dbec4c90 580 u16 smid;
f92363d1
SR
581 u16 handle;
582 struct scsi_cmnd *scmd;
583 struct MPT3SAS_DEVICE *priv_data;
f92363d1
SR
584 Mpi2SCSITaskManagementReply_t *tm_reply;
585 u32 sz;
586 u32 lun;
587 char *desc = NULL;
588
589 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
590 desc = "abort_task";
591 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
592 desc = "query_task";
593 else
594 return 0;
595
596 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
597
598 handle = le16_to_cpu(tm_request->DevHandle);
dbec4c90
SPS
599 for (smid = ioc->scsiio_depth; smid && !found; smid--) {
600 struct scsiio_tracker *st;
601
602 scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
603 if (!scmd)
f92363d1
SR
604 continue;
605 if (lun != scmd->device->lun)
606 continue;
607 priv_data = scmd->device->hostdata;
608 if (priv_data->sas_target == NULL)
609 continue;
610 if (priv_data->sas_target->handle != handle)
611 continue;
dbec4c90 612 st = scsi_cmd_priv(scmd);
8f55c307
MI
613
614 /*
615 * If the given TaskMID from the user space is zero, then the
616 * first outstanding smid will be picked up. Otherwise,
617 * targeted smid will be the one.
618 */
619 if (!tm_request->TaskMID || tm_request->TaskMID == st->smid) {
620 tm_request->TaskMID = cpu_to_le16(st->smid);
621 found = 1;
622 }
f92363d1 623 }
f92363d1
SR
624
625 if (!found) {
919d8a3f
JP
626 dctlprintk(ioc,
627 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), no active mid!!\n",
628 desc, le16_to_cpu(tm_request->DevHandle),
629 lun));
f92363d1
SR
630 tm_reply = ioc->ctl_cmds.reply;
631 tm_reply->DevHandle = tm_request->DevHandle;
632 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
633 tm_reply->TaskType = tm_request->TaskType;
634 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
635 tm_reply->VP_ID = tm_request->VP_ID;
636 tm_reply->VF_ID = tm_request->VF_ID;
637 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
638 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
639 sz))
640 pr_err("failure at %s:%d/%s()!\n", __FILE__,
641 __LINE__, __func__);
642 return 1;
643 }
644
919d8a3f
JP
645 dctlprintk(ioc,
646 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), task_mid(%d)\n",
647 desc, le16_to_cpu(tm_request->DevHandle), lun,
648 le16_to_cpu(tm_request->TaskMID)));
f92363d1
SR
649 return 0;
650}
651
652/**
653 * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
654 * @ioc: per adapter object
4beb4867
BVA
655 * @karg: (struct mpt3_ioctl_command)
656 * @mf: pointer to mf in user space
f92363d1
SR
657 */
658static long
659_ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
660 void __user *mf)
661{
662 MPI2RequestHeader_t *mpi_request = NULL, *request;
663 MPI2DefaultReply_t *mpi_reply;
aff39e61 664 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
c1a6c5ac 665 struct _pcie_device *pcie_device = NULL;
f92363d1 666 u16 smid;
c1a6c5ac 667 u8 timeout;
f92363d1 668 u8 issue_reset;
aff39e61 669 u32 sz, sz_arg;
f92363d1
SR
670 void *psge;
671 void *data_out = NULL;
672 dma_addr_t data_out_dma = 0;
673 size_t data_out_sz = 0;
674 void *data_in = NULL;
675 dma_addr_t data_in_dma = 0;
676 size_t data_in_sz = 0;
677 long ret;
c696f7b8 678 u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
f92363d1
SR
679
680 issue_reset = 0;
681
682 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
919d8a3f 683 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
f92363d1
SR
684 ret = -EAGAIN;
685 goto out;
686 }
687
f4305749
SP
688 ret = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
689 if (ret)
690 goto out;
f92363d1
SR
691
692 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
693 if (!mpi_request) {
919d8a3f
JP
694 ioc_err(ioc, "%s: failed obtaining a memory for mpi_request\n",
695 __func__);
f92363d1
SR
696 ret = -ENOMEM;
697 goto out;
698 }
699
700 /* Check for overflow and wraparound */
701 if (karg.data_sge_offset * 4 > ioc->request_sz ||
702 karg.data_sge_offset > (UINT_MAX / 4)) {
703 ret = -EINVAL;
704 goto out;
705 }
706
707 /* copy in request message frame from user */
708 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
709 pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
710 __func__);
711 ret = -EFAULT;
712 goto out;
713 }
714
715 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
716 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
717 if (!smid) {
919d8a3f 718 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
f92363d1
SR
719 ret = -EAGAIN;
720 goto out;
721 }
722 } else {
b0cd285e
HR
723 /* Use first reserved smid for passthrough ioctls */
724 smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
f92363d1
SR
725 }
726
727 ret = 0;
728 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
729 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
730 request = mpt3sas_base_get_msg_frame(ioc, smid);
e224e03b 731 memset(request, 0, ioc->request_sz);
f92363d1
SR
732 memcpy(request, mpi_request, karg.data_sge_offset*4);
733 ioc->ctl_cmds.smid = smid;
734 data_out_sz = karg.data_out_size;
735 data_in_sz = karg.data_in_size;
736
737 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
c696f7b8
SPS
738 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
739 mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
aff39e61
SPS
740 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
741 mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
c696f7b8
SPS
742
743 device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
744 if (!device_handle || (device_handle >
745 ioc->facts.MaxDevHandle)) {
f92363d1
SR
746 ret = -EINVAL;
747 mpt3sas_base_free_smid(ioc, smid);
748 goto out;
749 }
750 }
751
752 /* obtain dma-able memory for data transfer */
753 if (data_out_sz) /* WRITE */ {
1c2048bd
CH
754 data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz,
755 &data_out_dma, GFP_KERNEL);
f92363d1
SR
756 if (!data_out) {
757 pr_err("failure at %s:%d/%s()!\n", __FILE__,
758 __LINE__, __func__);
759 ret = -ENOMEM;
760 mpt3sas_base_free_smid(ioc, smid);
761 goto out;
762 }
763 if (copy_from_user(data_out, karg.data_out_buf_ptr,
764 data_out_sz)) {
765 pr_err("failure at %s:%d/%s()!\n", __FILE__,
766 __LINE__, __func__);
767 ret = -EFAULT;
768 mpt3sas_base_free_smid(ioc, smid);
769 goto out;
770 }
771 }
772
773 if (data_in_sz) /* READ */ {
1c2048bd
CH
774 data_in = dma_alloc_coherent(&ioc->pdev->dev, data_in_sz,
775 &data_in_dma, GFP_KERNEL);
f92363d1
SR
776 if (!data_in) {
777 pr_err("failure at %s:%d/%s()!\n", __FILE__,
778 __LINE__, __func__);
779 ret = -ENOMEM;
780 mpt3sas_base_free_smid(ioc, smid);
781 goto out;
782 }
783 }
784
785 psge = (void *)request + (karg.data_sge_offset*4);
786
787 /* send command to firmware */
f92363d1 788 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
f92363d1
SR
789
790 init_completion(&ioc->ctl_cmds.done);
791 switch (mpi_request->Function) {
aff39e61
SPS
792 case MPI2_FUNCTION_NVME_ENCAPSULATED:
793 {
794 nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
77fd4f2c
SR
795 if (!ioc->pcie_sg_lookup) {
796 dtmprintk(ioc, ioc_info(ioc,
797 "HBA doesn't support NVMe. Rejecting NVMe Encapsulated request.\n"
798 ));
799
800 if (ioc->logging_level & MPT_DEBUG_TM)
801 _debug_dump_mf(nvme_encap_request,
802 ioc->request_sz/4);
803 mpt3sas_base_free_smid(ioc, smid);
804 ret = -EINVAL;
805 goto out;
806 }
aff39e61
SPS
807 /*
808 * Get the Physical Address of the sense buffer.
809 * Use Error Response buffer address field to hold the sense
810 * buffer address.
811 * Clear the internal sense buffer, which will potentially hold
812 * the Completion Queue Entry on return, or 0 if no Entry.
813 * Build the PRPs and set direction bits.
814 * Send the request.
815 */
cf6bf971
C
816 nvme_encap_request->ErrorResponseBaseAddress =
817 cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
aff39e61 818 nvme_encap_request->ErrorResponseBaseAddress |=
cf6bf971
C
819 cpu_to_le64(le32_to_cpu(
820 mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
aff39e61 821 nvme_encap_request->ErrorResponseAllocationLength =
cf6bf971 822 cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
aff39e61
SPS
823 memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
824 ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
825 data_out_dma, data_out_sz, data_in_dma, data_in_sz);
826 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
919d8a3f
JP
827 dtmprintk(ioc,
828 ioc_info(ioc, "handle(0x%04x): ioctl failed due to device removal in progress\n",
829 device_handle));
aff39e61
SPS
830 mpt3sas_base_free_smid(ioc, smid);
831 ret = -EINVAL;
832 goto out;
833 }
40114bde 834 mpt3sas_base_put_smid_nvme_encap(ioc, smid);
aff39e61
SPS
835 break;
836 }
f92363d1
SR
837 case MPI2_FUNCTION_SCSI_IO_REQUEST:
838 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
839 {
840 Mpi2SCSIIORequest_t *scsiio_request =
841 (Mpi2SCSIIORequest_t *)request;
842 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
843 scsiio_request->SenseBufferLowAddress =
844 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
845 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
c696f7b8 846 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
919d8a3f
JP
847 dtmprintk(ioc,
848 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
849 device_handle));
c696f7b8
SPS
850 mpt3sas_base_free_smid(ioc, smid);
851 ret = -EINVAL;
852 goto out;
853 }
f92363d1
SR
854 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
855 data_in_dma, data_in_sz);
f92363d1 856 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
81c16f83 857 ioc->put_smid_scsi_io(ioc, smid, device_handle);
f92363d1 858 else
078a4cc1 859 ioc->put_smid_default(ioc, smid);
f92363d1
SR
860 break;
861 }
862 case MPI2_FUNCTION_SCSI_TASK_MGMT:
863 {
864 Mpi2SCSITaskManagementRequest_t *tm_request =
865 (Mpi2SCSITaskManagementRequest_t *)request;
866
919d8a3f
JP
867 dtmprintk(ioc,
868 ioc_info(ioc, "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
869 le16_to_cpu(tm_request->DevHandle),
870 tm_request->TaskType));
459325c4 871 ioc->got_task_abort_from_ioctl = 1;
f92363d1
SR
872 if (tm_request->TaskType ==
873 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
874 tm_request->TaskType ==
875 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
876 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
877 mpt3sas_base_free_smid(ioc, smid);
459325c4 878 ioc->got_task_abort_from_ioctl = 0;
f92363d1
SR
879 goto out;
880 }
881 }
459325c4 882 ioc->got_task_abort_from_ioctl = 0;
f92363d1 883
c696f7b8 884 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
919d8a3f
JP
885 dtmprintk(ioc,
886 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
887 device_handle));
c696f7b8
SPS
888 mpt3sas_base_free_smid(ioc, smid);
889 ret = -EINVAL;
890 goto out;
891 }
f92363d1
SR
892 mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
893 tm_request->DevHandle));
894 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
895 data_in_dma, data_in_sz);
078a4cc1 896 ioc->put_smid_hi_priority(ioc, smid, 0);
f92363d1
SR
897 break;
898 }
899 case MPI2_FUNCTION_SMP_PASSTHROUGH:
900 {
901 Mpi2SmpPassthroughRequest_t *smp_request =
902 (Mpi2SmpPassthroughRequest_t *)mpi_request;
903 u8 *data;
904
905 /* ioc determines which port to use */
906 smp_request->PhysicalPort = 0xFF;
907 if (smp_request->PassthroughFlags &
908 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
909 data = (u8 *)&smp_request->SGL;
910 else {
911 if (unlikely(data_out == NULL)) {
912 pr_err("failure at %s:%d/%s()!\n",
913 __FILE__, __LINE__, __func__);
914 mpt3sas_base_free_smid(ioc, smid);
915 ret = -EINVAL;
916 goto out;
917 }
918 data = data_out;
919 }
920
921 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
922 ioc->ioc_link_reset_in_progress = 1;
923 ioc->ignore_loginfos = 1;
924 }
925 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
926 data_in_sz);
078a4cc1 927 ioc->put_smid_default(ioc, smid);
f92363d1
SR
928 break;
929 }
930 case MPI2_FUNCTION_SATA_PASSTHROUGH:
c696f7b8
SPS
931 {
932 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
919d8a3f
JP
933 dtmprintk(ioc,
934 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
935 device_handle));
c696f7b8
SPS
936 mpt3sas_base_free_smid(ioc, smid);
937 ret = -EINVAL;
938 goto out;
939 }
940 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
941 data_in_sz);
078a4cc1 942 ioc->put_smid_default(ioc, smid);
c696f7b8
SPS
943 break;
944 }
f92363d1
SR
945 case MPI2_FUNCTION_FW_DOWNLOAD:
946 case MPI2_FUNCTION_FW_UPLOAD:
947 {
948 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
949 data_in_sz);
078a4cc1 950 ioc->put_smid_default(ioc, smid);
f92363d1
SR
951 break;
952 }
953 case MPI2_FUNCTION_TOOLBOX:
954 {
955 Mpi2ToolboxCleanRequest_t *toolbox_request =
956 (Mpi2ToolboxCleanRequest_t *)mpi_request;
957
f23ca2cb
SP
958 if ((toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
959 || (toolbox_request->Tool ==
960 MPI26_TOOLBOX_BACKEND_PCIE_LANE_MARGIN))
f92363d1
SR
961 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
962 data_in_dma, data_in_sz);
ba630ea0
SP
963 else if (toolbox_request->Tool ==
964 MPI2_TOOLBOX_MEMORY_MOVE_TOOL) {
965 Mpi2ToolboxMemMoveRequest_t *mem_move_request =
966 (Mpi2ToolboxMemMoveRequest_t *)request;
967 Mpi2SGESimple64_t tmp, *src = NULL, *dst = NULL;
968
969 ioc->build_sg_mpi(ioc, psge, data_out_dma,
970 data_out_sz, data_in_dma, data_in_sz);
971 if (data_out_sz && !data_in_sz) {
972 dst =
973 (Mpi2SGESimple64_t *)&mem_move_request->SGL;
974 src = (void *)dst + ioc->sge_size;
975
976 memcpy(&tmp, src, ioc->sge_size);
977 memcpy(src, dst, ioc->sge_size);
978 memcpy(dst, &tmp, ioc->sge_size);
979 }
980 if (ioc->logging_level & MPT_DEBUG_TM) {
981 ioc_info(ioc,
982 "Mpi2ToolboxMemMoveRequest_t request msg\n");
983 _debug_dump_mf(mem_move_request,
984 ioc->request_sz/4);
985 }
986 } else
f92363d1 987 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
ba630ea0 988 data_in_dma, data_in_sz);
078a4cc1 989 ioc->put_smid_default(ioc, smid);
f92363d1
SR
990 break;
991 }
992 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
993 {
994 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
995 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
996
997 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
998 || sasiounit_request->Operation ==
999 MPI2_SAS_OP_PHY_LINK_RESET) {
1000 ioc->ioc_link_reset_in_progress = 1;
1001 ioc->ignore_loginfos = 1;
1002 }
1003 /* drop to default case for posting the request */
1004 }
df561f66 1005 fallthrough;
f92363d1
SR
1006 default:
1007 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
1008 data_in_dma, data_in_sz);
078a4cc1 1009 ioc->put_smid_default(ioc, smid);
f92363d1
SR
1010 break;
1011 }
1012
1013 if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
1014 timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
1015 else
1016 timeout = karg.timeout;
8bbb1cf6 1017 wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
f92363d1
SR
1018 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
1019 Mpi2SCSITaskManagementRequest_t *tm_request =
1020 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
1021 mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
1022 tm_request->DevHandle));
1023 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
1024 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
1025 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
1026 ioc->ioc_link_reset_in_progress) {
1027 ioc->ioc_link_reset_in_progress = 0;
1028 ioc->ignore_loginfos = 0;
1029 }
1030 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
c6bdb6a1
SR
1031 mpt3sas_check_cmd_timeout(ioc,
1032 ioc->ctl_cmds.status, mpi_request,
1033 karg.data_sge_offset, issue_reset);
f92363d1
SR
1034 goto issue_host_reset;
1035 }
1036
1037 mpi_reply = ioc->ctl_cmds.reply;
f92363d1 1038
f92363d1
SR
1039 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
1040 (ioc->logging_level & MPT_DEBUG_TM)) {
1041 Mpi2SCSITaskManagementReply_t *tm_reply =
1042 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
1043
919d8a3f
JP
1044 ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
1045 le16_to_cpu(tm_reply->IOCStatus),
1046 le32_to_cpu(tm_reply->IOCLogInfo),
1047 le32_to_cpu(tm_reply->TerminationCount));
f92363d1 1048 }
af009411 1049
f92363d1
SR
1050 /* copy out xdata to user */
1051 if (data_in_sz) {
1052 if (copy_to_user(karg.data_in_buf_ptr, data_in,
1053 data_in_sz)) {
1054 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1055 __LINE__, __func__);
1056 ret = -ENODATA;
1057 goto out;
1058 }
1059 }
1060
1061 /* copy out reply message frame to user */
1062 if (karg.max_reply_bytes) {
1063 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1064 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1065 sz)) {
1066 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1067 __LINE__, __func__);
1068 ret = -ENODATA;
1069 goto out;
1070 }
1071 }
1072
aff39e61 1073 /* copy out sense/NVMe Error Response to user */
f92363d1
SR
1074 if (karg.max_sense_bytes && (mpi_request->Function ==
1075 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
aff39e61
SPS
1076 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1077 MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1078 if (karg.sense_data_ptr == NULL) {
919d8a3f 1079 ioc_info(ioc, "Response buffer provided by application is NULL; Response data will not be returned\n");
aff39e61
SPS
1080 goto out;
1081 }
1082 sz_arg = (mpi_request->Function ==
1083 MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1084 SCSI_SENSE_BUFFERSIZE;
1085 sz = min_t(u32, karg.max_sense_bytes, sz_arg);
f92363d1
SR
1086 if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1087 sz)) {
1088 pr_err("failure at %s:%d/%s()!\n", __FILE__,
aff39e61 1089 __LINE__, __func__);
f92363d1
SR
1090 ret = -ENODATA;
1091 goto out;
1092 }
1093 }
1094
1095 issue_host_reset:
1096 if (issue_reset) {
1097 ret = -ENODATA;
1098 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1099 mpi_request->Function ==
1100 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1101 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
919d8a3f
JP
1102 ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
1103 le16_to_cpu(mpi_request->FunctionDependent1));
f92363d1 1104 mpt3sas_halt_firmware(ioc);
c1a6c5ac
C
1105 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1106 le16_to_cpu(mpi_request->FunctionDependent1));
5bb309db
SP
1107 if (pcie_device && (!ioc->tm_custom_handling) &&
1108 (!(mpt3sas_scsih_is_pcie_scsi_device(
1109 pcie_device->device_info))))
c1a6c5ac
C
1110 mpt3sas_scsih_issue_locked_tm(ioc,
1111 le16_to_cpu(mpi_request->FunctionDependent1),
1112 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1113 0, pcie_device->reset_timeout,
5bb309db 1114 MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE);
c1a6c5ac
C
1115 else
1116 mpt3sas_scsih_issue_locked_tm(ioc,
1117 le16_to_cpu(mpi_request->FunctionDependent1),
1118 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1119 0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
f92363d1 1120 } else
98c56ad3 1121 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1
SR
1122 }
1123
1124 out:
c1a6c5ac
C
1125 if (pcie_device)
1126 pcie_device_put(pcie_device);
f92363d1
SR
1127
1128 /* free memory associated with sg buffers */
1129 if (data_in)
1c2048bd 1130 dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
f92363d1
SR
1131 data_in_dma);
1132
1133 if (data_out)
1c2048bd 1134 dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
f92363d1
SR
1135 data_out_dma);
1136
1137 kfree(mpi_request);
1138 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1139 return ret;
1140}
1141
1142/**
1143 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1144 * @ioc: per adapter object
4beb4867 1145 * @arg: user space buffer containing ioctl content
f92363d1
SR
1146 */
1147static long
1148_ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1149{
1150 struct mpt3_ioctl_iocinfo karg;
1151
919d8a3f
JP
1152 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1153 __func__));
f92363d1
SR
1154
1155 memset(&karg, 0 , sizeof(karg));
f92363d1
SR
1156 if (ioc->pfacts)
1157 karg.port_number = ioc->pfacts[0].PortNumber;
1158 karg.hw_rev = ioc->pdev->revision;
1159 karg.pci_id = ioc->pdev->device;
1160 karg.subsystem_device = ioc->pdev->subsystem_device;
1161 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1162 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1163 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1164 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1165 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1166 karg.firmware_version = ioc->facts.FWVersion.Word;
c84b06a4 1167 strcpy(karg.driver_version, ioc->driver_name);
f92363d1 1168 strcat(karg.driver_version, "-");
d357e84d
SR
1169 switch (ioc->hba_mpi_version_belonged) {
1170 case MPI2_VERSION:
7786ab6a
SR
1171 if (ioc->is_warpdrive)
1172 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1173 else
1174 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
d357e84d
SR
1175 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1176 break;
1177 case MPI25_VERSION:
b130b0d5 1178 case MPI26_VERSION:
998f26ae
SPS
1179 if (ioc->is_gen35_ioc)
1180 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1181 else
1182 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
d357e84d
SR
1183 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1184 break;
1185 }
f92363d1
SR
1186 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1187
1188 if (copy_to_user(arg, &karg, sizeof(karg))) {
1189 pr_err("failure at %s:%d/%s()!\n",
1190 __FILE__, __LINE__, __func__);
1191 return -EFAULT;
1192 }
1193 return 0;
1194}
1195
1196/**
1197 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1198 * @ioc: per adapter object
4beb4867 1199 * @arg: user space buffer containing ioctl content
f92363d1
SR
1200 */
1201static long
1202_ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1203{
1204 struct mpt3_ioctl_eventquery karg;
1205
1206 if (copy_from_user(&karg, arg, sizeof(karg))) {
1207 pr_err("failure at %s:%d/%s()!\n",
1208 __FILE__, __LINE__, __func__);
1209 return -EFAULT;
1210 }
1211
919d8a3f
JP
1212 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1213 __func__));
f92363d1
SR
1214
1215 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1216 memcpy(karg.event_types, ioc->event_type,
1217 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1218
1219 if (copy_to_user(arg, &karg, sizeof(karg))) {
1220 pr_err("failure at %s:%d/%s()!\n",
1221 __FILE__, __LINE__, __func__);
1222 return -EFAULT;
1223 }
1224 return 0;
1225}
1226
1227/**
1228 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1229 * @ioc: per adapter object
4beb4867 1230 * @arg: user space buffer containing ioctl content
f92363d1
SR
1231 */
1232static long
1233_ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1234{
1235 struct mpt3_ioctl_eventenable karg;
1236
1237 if (copy_from_user(&karg, arg, sizeof(karg))) {
1238 pr_err("failure at %s:%d/%s()!\n",
1239 __FILE__, __LINE__, __func__);
1240 return -EFAULT;
1241 }
1242
919d8a3f
JP
1243 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1244 __func__));
f92363d1
SR
1245
1246 memcpy(ioc->event_type, karg.event_types,
1247 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1248 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1249
1250 if (ioc->event_log)
1251 return 0;
1252 /* initialize event_log */
1253 ioc->event_context = 0;
1254 ioc->aen_event_read_flag = 0;
1255 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1256 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1257 if (!ioc->event_log) {
1258 pr_err("failure at %s:%d/%s()!\n",
1259 __FILE__, __LINE__, __func__);
1260 return -ENOMEM;
1261 }
1262 return 0;
1263}
1264
1265/**
1266 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1267 * @ioc: per adapter object
4beb4867 1268 * @arg: user space buffer containing ioctl content
f92363d1
SR
1269 */
1270static long
1271_ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1272{
1273 struct mpt3_ioctl_eventreport karg;
1274 u32 number_bytes, max_events, max;
1275 struct mpt3_ioctl_eventreport __user *uarg = arg;
1276
1277 if (copy_from_user(&karg, arg, sizeof(karg))) {
1278 pr_err("failure at %s:%d/%s()!\n",
1279 __FILE__, __LINE__, __func__);
1280 return -EFAULT;
1281 }
1282
919d8a3f
JP
1283 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1284 __func__));
f92363d1
SR
1285
1286 number_bytes = karg.hdr.max_data_size -
1287 sizeof(struct mpt3_ioctl_header);
1288 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1289 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1290
1291 /* If fewer than 1 event is requested, there must have
1292 * been some type of error.
1293 */
1294 if (!max || !ioc->event_log)
1295 return -ENODATA;
1296
1297 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1298 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1299 pr_err("failure at %s:%d/%s()!\n",
1300 __FILE__, __LINE__, __func__);
1301 return -EFAULT;
1302 }
1303
1304 /* reset flag so SIGIO can restart */
1305 ioc->aen_event_read_flag = 0;
1306 return 0;
1307}
1308
1309/**
1310 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1311 * @ioc: per adapter object
4beb4867 1312 * @arg: user space buffer containing ioctl content
f92363d1
SR
1313 */
1314static long
1315_ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1316{
1317 struct mpt3_ioctl_diag_reset karg;
1318 int retval;
1319
1320 if (copy_from_user(&karg, arg, sizeof(karg))) {
1321 pr_err("failure at %s:%d/%s()!\n",
1322 __FILE__, __LINE__, __func__);
1323 return -EFAULT;
1324 }
1325
1326 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1327 ioc->is_driver_loading)
1328 return -EAGAIN;
1329
919d8a3f
JP
1330 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1331 __func__));
f92363d1 1332
98c56ad3 1333 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
5b061980
SR
1334 ioc_info(ioc,
1335 "Ioctl: host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
f92363d1
SR
1336 return 0;
1337}
1338
1339/**
1340 * _ctl_btdh_search_sas_device - searching for sas device
1341 * @ioc: per adapter object
1342 * @btdh: btdh ioctl payload
1343 */
1344static int
1345_ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1346 struct mpt3_ioctl_btdh_mapping *btdh)
1347{
1348 struct _sas_device *sas_device;
1349 unsigned long flags;
1350 int rc = 0;
1351
1352 if (list_empty(&ioc->sas_device_list))
1353 return rc;
1354
1355 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1356 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1357 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1358 btdh->handle == sas_device->handle) {
1359 btdh->bus = sas_device->channel;
1360 btdh->id = sas_device->id;
1361 rc = 1;
1362 goto out;
1363 } else if (btdh->bus == sas_device->channel && btdh->id ==
1364 sas_device->id && btdh->handle == 0xFFFF) {
1365 btdh->handle = sas_device->handle;
1366 rc = 1;
1367 goto out;
1368 }
1369 }
1370 out:
1371 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1372 return rc;
1373}
1374
45aa6a1a
SPS
1375/**
1376 * _ctl_btdh_search_pcie_device - searching for pcie device
1377 * @ioc: per adapter object
1378 * @btdh: btdh ioctl payload
1379 */
1380static int
1381_ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
1382 struct mpt3_ioctl_btdh_mapping *btdh)
1383{
1384 struct _pcie_device *pcie_device;
1385 unsigned long flags;
1386 int rc = 0;
1387
1388 if (list_empty(&ioc->pcie_device_list))
1389 return rc;
1390
1391 spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1392 list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1393 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1394 btdh->handle == pcie_device->handle) {
1395 btdh->bus = pcie_device->channel;
1396 btdh->id = pcie_device->id;
1397 rc = 1;
1398 goto out;
1399 } else if (btdh->bus == pcie_device->channel && btdh->id ==
1400 pcie_device->id && btdh->handle == 0xFFFF) {
1401 btdh->handle = pcie_device->handle;
1402 rc = 1;
1403 goto out;
1404 }
1405 }
1406 out:
1407 spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1408 return rc;
1409}
1410
f92363d1
SR
1411/**
1412 * _ctl_btdh_search_raid_device - searching for raid device
1413 * @ioc: per adapter object
1414 * @btdh: btdh ioctl payload
1415 */
1416static int
1417_ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1418 struct mpt3_ioctl_btdh_mapping *btdh)
1419{
1420 struct _raid_device *raid_device;
1421 unsigned long flags;
1422 int rc = 0;
1423
1424 if (list_empty(&ioc->raid_device_list))
1425 return rc;
1426
1427 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1428 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1429 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1430 btdh->handle == raid_device->handle) {
1431 btdh->bus = raid_device->channel;
1432 btdh->id = raid_device->id;
1433 rc = 1;
1434 goto out;
1435 } else if (btdh->bus == raid_device->channel && btdh->id ==
1436 raid_device->id && btdh->handle == 0xFFFF) {
1437 btdh->handle = raid_device->handle;
1438 rc = 1;
1439 goto out;
1440 }
1441 }
1442 out:
1443 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1444 return rc;
1445}
1446
1447/**
1448 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1449 * @ioc: per adapter object
4beb4867 1450 * @arg: user space buffer containing ioctl content
f92363d1
SR
1451 */
1452static long
1453_ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1454{
1455 struct mpt3_ioctl_btdh_mapping karg;
1456 int rc;
1457
1458 if (copy_from_user(&karg, arg, sizeof(karg))) {
1459 pr_err("failure at %s:%d/%s()!\n",
1460 __FILE__, __LINE__, __func__);
1461 return -EFAULT;
1462 }
1463
919d8a3f
JP
1464 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1465 __func__));
f92363d1
SR
1466
1467 rc = _ctl_btdh_search_sas_device(ioc, &karg);
45aa6a1a
SPS
1468 if (!rc)
1469 rc = _ctl_btdh_search_pcie_device(ioc, &karg);
f92363d1
SR
1470 if (!rc)
1471 _ctl_btdh_search_raid_device(ioc, &karg);
1472
1473 if (copy_to_user(arg, &karg, sizeof(karg))) {
1474 pr_err("failure at %s:%d/%s()!\n",
1475 __FILE__, __LINE__, __func__);
1476 return -EFAULT;
1477 }
1478 return 0;
1479}
1480
1481/**
1482 * _ctl_diag_capability - return diag buffer capability
1483 * @ioc: per adapter object
1484 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1485 *
1486 * returns 1 when diag buffer support is enabled in firmware
1487 */
1488static u8
1489_ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1490{
1491 u8 rc = 0;
1492
1493 switch (buffer_type) {
1494 case MPI2_DIAG_BUF_TYPE_TRACE:
1495 if (ioc->facts.IOCCapabilities &
1496 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1497 rc = 1;
1498 break;
1499 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1500 if (ioc->facts.IOCCapabilities &
1501 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1502 rc = 1;
1503 break;
1504 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1505 if (ioc->facts.IOCCapabilities &
1506 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1507 rc = 1;
1508 }
1509
1510 return rc;
1511}
1512
08e7378e
SR
1513/**
1514 * _ctl_diag_get_bufftype - return diag buffer type
1515 * either TRACE, SNAPSHOT, or EXTENDED
1516 * @ioc: per adapter object
1517 * @unique_id: specifies the unique_id for the buffer
1518 *
1519 * returns MPT3_DIAG_UID_NOT_FOUND if the id not found
1520 */
1521static u8
1522_ctl_diag_get_bufftype(struct MPT3SAS_ADAPTER *ioc, u32 unique_id)
1523{
1524 u8 index;
1525
1526 for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
1527 if (ioc->unique_id[index] == unique_id)
1528 return index;
1529 }
1530
1531 return MPT3_DIAG_UID_NOT_FOUND;
1532}
f92363d1
SR
1533
1534/**
1535 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1536 * @ioc: per adapter object
1537 * @diag_register: the diag_register struct passed in from user space
1538 *
1539 */
1540static long
1541_ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1542 struct mpt3_diag_register *diag_register)
1543{
1544 int rc, i;
1545 void *request_data = NULL;
1546 dma_addr_t request_data_dma;
1547 u32 request_data_sz = 0;
1548 Mpi2DiagBufferPostRequest_t *mpi_request;
1549 Mpi2DiagBufferPostReply_t *mpi_reply;
1550 u8 buffer_type;
f92363d1
SR
1551 u16 smid;
1552 u16 ioc_status;
1553 u32 ioc_state;
1554 u8 issue_reset = 0;
1555
919d8a3f
JP
1556 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1557 __func__));
f92363d1
SR
1558
1559 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1560 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
919d8a3f
JP
1561 ioc_err(ioc, "%s: failed due to ioc not operational\n",
1562 __func__);
f92363d1
SR
1563 rc = -EAGAIN;
1564 goto out;
1565 }
1566
1567 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
919d8a3f 1568 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
f92363d1
SR
1569 rc = -EAGAIN;
1570 goto out;
1571 }
1572
1573 buffer_type = diag_register->buffer_type;
1574 if (!_ctl_diag_capability(ioc, buffer_type)) {
919d8a3f
JP
1575 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1576 __func__, buffer_type);
f92363d1
SR
1577 return -EPERM;
1578 }
1579
08e7378e
SR
1580 if (diag_register->unique_id == 0) {
1581 ioc_err(ioc,
1582 "%s: Invalid UID(0x%08x), buffer_type(0x%02x)\n", __func__,
1583 diag_register->unique_id, buffer_type);
1584 return -EINVAL;
1585 }
1586
a8a6cbcd
SR
1587 if ((ioc->diag_buffer_status[buffer_type] &
1588 MPT3_DIAG_BUFFER_IS_APP_OWNED) &&
1589 !(ioc->diag_buffer_status[buffer_type] &
1590 MPT3_DIAG_BUFFER_IS_RELEASED)) {
1591 ioc_err(ioc,
1592 "%s: buffer_type(0x%02x) is already registered by application with UID(0x%08x)\n",
1593 __func__, buffer_type, ioc->unique_id[buffer_type]);
1594 return -EINVAL;
1595 }
1596
f92363d1
SR
1597 if (ioc->diag_buffer_status[buffer_type] &
1598 MPT3_DIAG_BUFFER_IS_REGISTERED) {
08e7378e
SR
1599 /*
1600 * If driver posts buffer initially, then an application wants
1601 * to Register that buffer (own it) without Releasing first,
1602 * the application Register command MUST have the same buffer
1603 * type and size in the Register command (obtained from the
1604 * Query command). Otherwise that Register command will be
1605 * failed. If the application has released the buffer but wants
1606 * to re-register it, it should be allowed as long as the
1607 * Unique-Id/Size match.
1608 */
1609
1610 if (ioc->unique_id[buffer_type] == MPT3DIAGBUFFUNIQUEID &&
1611 ioc->diag_buffer_sz[buffer_type] ==
1612 diag_register->requested_buffer_size) {
1613
1614 if (!(ioc->diag_buffer_status[buffer_type] &
1615 MPT3_DIAG_BUFFER_IS_RELEASED)) {
1616 dctlprintk(ioc, ioc_info(ioc,
1617 "%s: diag_buffer (%d) ownership changed. old-ID(0x%08x), new-ID(0x%08x)\n",
1618 __func__, buffer_type,
1619 ioc->unique_id[buffer_type],
1620 diag_register->unique_id));
1621
1622 /*
1623 * Application wants to own the buffer with
1624 * the same size.
1625 */
1626 ioc->unique_id[buffer_type] =
1627 diag_register->unique_id;
1628 rc = 0; /* success */
1629 goto out;
1630 }
1631 } else if (ioc->unique_id[buffer_type] !=
1632 MPT3DIAGBUFFUNIQUEID) {
1633 if (ioc->unique_id[buffer_type] !=
1634 diag_register->unique_id ||
1635 ioc->diag_buffer_sz[buffer_type] !=
1636 diag_register->requested_buffer_size ||
1637 !(ioc->diag_buffer_status[buffer_type] &
1638 MPT3_DIAG_BUFFER_IS_RELEASED)) {
1639 ioc_err(ioc,
1640 "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1641 __func__, buffer_type);
1642 return -EINVAL;
1643 }
1644 } else {
1645 ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1646 __func__, buffer_type);
1647 return -EINVAL;
1648 }
a066f4c3
SR
1649 } else if (ioc->diag_buffer_status[buffer_type] &
1650 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
1651
1652 if (ioc->unique_id[buffer_type] != MPT3DIAGBUFFUNIQUEID ||
1653 ioc->diag_buffer_sz[buffer_type] !=
1654 diag_register->requested_buffer_size) {
1655
1656 ioc_err(ioc,
1657 "%s: already a buffer is allocated for buffer_type(0x%02x) of size %d bytes, so please try registering again with same size\n",
1658 __func__, buffer_type,
1659 ioc->diag_buffer_sz[buffer_type]);
1660 return -EINVAL;
1661 }
f92363d1
SR
1662 }
1663
1664 if (diag_register->requested_buffer_size % 4) {
919d8a3f
JP
1665 ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
1666 __func__);
f92363d1
SR
1667 return -EINVAL;
1668 }
1669
1670 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1671 if (!smid) {
919d8a3f 1672 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
f92363d1
SR
1673 rc = -EAGAIN;
1674 goto out;
1675 }
1676
1677 rc = 0;
1678 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1679 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1680 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1681 ioc->ctl_cmds.smid = smid;
1682
1683 request_data = ioc->diag_buffer[buffer_type];
1684 request_data_sz = diag_register->requested_buffer_size;
1685 ioc->unique_id[buffer_type] = diag_register->unique_id;
a066f4c3
SR
1686 ioc->diag_buffer_status[buffer_type] &=
1687 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
f92363d1
SR
1688 memcpy(ioc->product_specific[buffer_type],
1689 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1690 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1691
1692 if (request_data) {
1693 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1694 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1c2048bd
CH
1695 dma_free_coherent(&ioc->pdev->dev,
1696 ioc->diag_buffer_sz[buffer_type],
1697 request_data, request_data_dma);
f92363d1
SR
1698 request_data = NULL;
1699 }
1700 }
1701
1702 if (request_data == NULL) {
1703 ioc->diag_buffer_sz[buffer_type] = 0;
1704 ioc->diag_buffer_dma[buffer_type] = 0;
1c2048bd
CH
1705 request_data = dma_alloc_coherent(&ioc->pdev->dev,
1706 request_data_sz, &request_data_dma, GFP_KERNEL);
f92363d1 1707 if (request_data == NULL) {
919d8a3f
JP
1708 ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
1709 __func__, request_data_sz);
f92363d1 1710 mpt3sas_base_free_smid(ioc, smid);
782b2818
SR
1711 rc = -ENOMEM;
1712 goto out;
f92363d1
SR
1713 }
1714 ioc->diag_buffer[buffer_type] = request_data;
1715 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1716 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1717 }
1718
1719 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1720 mpi_request->BufferType = diag_register->buffer_type;
1721 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1722 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1723 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1724 mpi_request->VF_ID = 0; /* TODO */
1725 mpi_request->VP_ID = 0;
1726
919d8a3f
JP
1727 dctlprintk(ioc,
1728 ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1729 __func__, request_data,
1730 (unsigned long long)request_data_dma,
1731 le32_to_cpu(mpi_request->BufferLength)));
f92363d1
SR
1732
1733 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1734 mpi_request->ProductSpecific[i] =
1735 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1736
1737 init_completion(&ioc->ctl_cmds.done);
078a4cc1 1738 ioc->put_smid_default(ioc, smid);
8bbb1cf6 1739 wait_for_completion_timeout(&ioc->ctl_cmds.done,
f92363d1
SR
1740 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1741
1742 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
c6bdb6a1
SR
1743 mpt3sas_check_cmd_timeout(ioc,
1744 ioc->ctl_cmds.status, mpi_request,
1745 sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
f92363d1
SR
1746 goto issue_host_reset;
1747 }
1748
1749 /* process the completed Reply Message Frame */
1750 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
919d8a3f 1751 ioc_err(ioc, "%s: no reply message\n", __func__);
f92363d1
SR
1752 rc = -EFAULT;
1753 goto out;
1754 }
1755
1756 mpi_reply = ioc->ctl_cmds.reply;
1757 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1758
1759 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1760 ioc->diag_buffer_status[buffer_type] |=
1761 MPT3_DIAG_BUFFER_IS_REGISTERED;
919d8a3f 1762 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
f92363d1 1763 } else {
919d8a3f
JP
1764 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1765 __func__,
1766 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
f92363d1
SR
1767 rc = -EFAULT;
1768 }
1769
1770 issue_host_reset:
1771 if (issue_reset)
98c56ad3 1772 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1
SR
1773
1774 out:
1775
a066f4c3 1776 if (rc && request_data) {
1c2048bd 1777 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
f92363d1 1778 request_data, request_data_dma);
a066f4c3
SR
1779 ioc->diag_buffer_status[buffer_type] &=
1780 ~MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1781 }
f92363d1
SR
1782
1783 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1784 return rc;
1785}
1786
1787/**
1788 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1789 * @ioc: per adapter object
1790 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1791 *
1792 * This is called when command line option diag_buffer_enable is enabled
1793 * at driver load time.
1794 */
1795void
1796mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1797{
1798 struct mpt3_diag_register diag_register;
d04a6edf
SR
1799 u32 ret_val;
1800 u32 trace_buff_size = ioc->manu_pg11.HostTraceBufferMaxSizeKB<<10;
1801 u32 min_trace_buff_size = 0;
1802 u32 decr_trace_buff_size = 0;
f92363d1
SR
1803
1804 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1805
1806 if (bits_to_register & 1) {
919d8a3f 1807 ioc_info(ioc, "registering trace buffer support\n");
f92363d1
SR
1808 ioc->diag_trigger_master.MasterData =
1809 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1810 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
08e7378e
SR
1811 diag_register.unique_id =
1812 (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
1813 (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
d04a6edf
SR
1814
1815 if (trace_buff_size != 0) {
1816 diag_register.requested_buffer_size = trace_buff_size;
1817 min_trace_buff_size =
1818 ioc->manu_pg11.HostTraceBufferMinSizeKB<<10;
1819 decr_trace_buff_size =
1820 ioc->manu_pg11.HostTraceBufferDecrementSizeKB<<10;
1821
1822 if (min_trace_buff_size > trace_buff_size) {
1823 /* The buff size is not set correctly */
1824 ioc_err(ioc,
1825 "Min Trace Buff size (%d KB) greater than Max Trace Buff size (%d KB)\n",
1826 min_trace_buff_size>>10,
1827 trace_buff_size>>10);
1828 ioc_err(ioc,
1829 "Using zero Min Trace Buff Size\n");
3524a38e 1830 min_trace_buff_size = 0;
d04a6edf
SR
1831 }
1832
1833 if (decr_trace_buff_size == 0) {
1834 /*
1835 * retry the min size if decrement
1836 * is not available.
1837 */
1838 decr_trace_buff_size =
1839 trace_buff_size - min_trace_buff_size;
1840 }
1841 } else {
1842 /* register for 2MB buffers */
1843 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1844 }
1845
1846 do {
1847 ret_val = _ctl_diag_register_2(ioc, &diag_register);
1848
1849 if (ret_val == -ENOMEM && min_trace_buff_size &&
1850 (trace_buff_size - decr_trace_buff_size) >=
1851 min_trace_buff_size) {
1852 /* adjust the buffer size */
1853 trace_buff_size -= decr_trace_buff_size;
1854 diag_register.requested_buffer_size =
1855 trace_buff_size;
1856 } else
1857 break;
1858 } while (true);
1859
1860 if (ret_val == -ENOMEM)
1861 ioc_err(ioc,
1862 "Cannot allocate trace buffer memory. Last memory tried = %d KB\n",
1863 diag_register.requested_buffer_size>>10);
1864 else if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE]
a066f4c3 1865 & MPT3_DIAG_BUFFER_IS_REGISTERED) {
d04a6edf
SR
1866 ioc_err(ioc, "Trace buffer memory %d KB allocated\n",
1867 diag_register.requested_buffer_size>>10);
a066f4c3
SR
1868 if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
1869 ioc->diag_buffer_status[
1870 MPI2_DIAG_BUF_TYPE_TRACE] |=
1871 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1872 }
f92363d1
SR
1873 }
1874
1875 if (bits_to_register & 2) {
919d8a3f 1876 ioc_info(ioc, "registering snapshot buffer support\n");
f92363d1
SR
1877 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1878 /* register for 2MB buffers */
1879 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1880 diag_register.unique_id = 0x7075901;
1881 _ctl_diag_register_2(ioc, &diag_register);
1882 }
1883
1884 if (bits_to_register & 4) {
919d8a3f 1885 ioc_info(ioc, "registering extended buffer support\n");
f92363d1
SR
1886 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1887 /* register for 2MB buffers */
1888 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1889 diag_register.unique_id = 0x7075901;
1890 _ctl_diag_register_2(ioc, &diag_register);
1891 }
1892}
1893
1894/**
1895 * _ctl_diag_register - application register with driver
1896 * @ioc: per adapter object
4beb4867 1897 * @arg: user space buffer containing ioctl content
f92363d1
SR
1898 *
1899 * This will allow the driver to setup any required buffers that will be
1900 * needed by firmware to communicate with the driver.
1901 */
1902static long
1903_ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1904{
1905 struct mpt3_diag_register karg;
1906 long rc;
1907
1908 if (copy_from_user(&karg, arg, sizeof(karg))) {
1909 pr_err("failure at %s:%d/%s()!\n",
1910 __FILE__, __LINE__, __func__);
1911 return -EFAULT;
1912 }
1913
1914 rc = _ctl_diag_register_2(ioc, &karg);
a8a6cbcd
SR
1915
1916 if (!rc && (ioc->diag_buffer_status[karg.buffer_type] &
1917 MPT3_DIAG_BUFFER_IS_REGISTERED))
1918 ioc->diag_buffer_status[karg.buffer_type] |=
1919 MPT3_DIAG_BUFFER_IS_APP_OWNED;
1920
f92363d1
SR
1921 return rc;
1922}
1923
1924/**
1925 * _ctl_diag_unregister - application unregister with driver
1926 * @ioc: per adapter object
4beb4867 1927 * @arg: user space buffer containing ioctl content
f92363d1
SR
1928 *
1929 * This will allow the driver to cleanup any memory allocated for diag
1930 * messages and to free up any resources.
1931 */
1932static long
1933_ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1934{
1935 struct mpt3_diag_unregister karg;
1936 void *request_data;
1937 dma_addr_t request_data_dma;
1938 u32 request_data_sz;
1939 u8 buffer_type;
1940
1941 if (copy_from_user(&karg, arg, sizeof(karg))) {
1942 pr_err("failure at %s:%d/%s()!\n",
1943 __FILE__, __LINE__, __func__);
1944 return -EFAULT;
1945 }
1946
919d8a3f
JP
1947 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1948 __func__));
f92363d1 1949
08e7378e
SR
1950 buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
1951 if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
1952 ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
1953 __func__, karg.unique_id);
1954 return -EINVAL;
1955 }
1956
f92363d1 1957 if (!_ctl_diag_capability(ioc, buffer_type)) {
919d8a3f
JP
1958 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1959 __func__, buffer_type);
f92363d1
SR
1960 return -EPERM;
1961 }
1962
1963 if ((ioc->diag_buffer_status[buffer_type] &
1964 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
919d8a3f
JP
1965 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1966 __func__, buffer_type);
f92363d1
SR
1967 return -EINVAL;
1968 }
1969 if ((ioc->diag_buffer_status[buffer_type] &
1970 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
919d8a3f
JP
1971 ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
1972 __func__, buffer_type);
f92363d1
SR
1973 return -EINVAL;
1974 }
1975
1976 if (karg.unique_id != ioc->unique_id[buffer_type]) {
919d8a3f
JP
1977 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1978 __func__, karg.unique_id);
f92363d1
SR
1979 return -EINVAL;
1980 }
1981
1982 request_data = ioc->diag_buffer[buffer_type];
1983 if (!request_data) {
919d8a3f
JP
1984 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1985 __func__, buffer_type);
f92363d1
SR
1986 return -ENOMEM;
1987 }
1988
a066f4c3
SR
1989 if (ioc->diag_buffer_status[buffer_type] &
1990 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
1991 ioc->unique_id[buffer_type] = MPT3DIAGBUFFUNIQUEID;
a8a6cbcd
SR
1992 ioc->diag_buffer_status[buffer_type] &=
1993 ~MPT3_DIAG_BUFFER_IS_APP_OWNED;
a066f4c3
SR
1994 ioc->diag_buffer_status[buffer_type] &=
1995 ~MPT3_DIAG_BUFFER_IS_REGISTERED;
1996 } else {
1997 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1998 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1999 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
2000 request_data, request_data_dma);
2001 ioc->diag_buffer[buffer_type] = NULL;
2002 ioc->diag_buffer_status[buffer_type] = 0;
2003 }
f92363d1
SR
2004 return 0;
2005}
2006
2007/**
2008 * _ctl_diag_query - query relevant info associated with diag buffers
2009 * @ioc: per adapter object
4beb4867 2010 * @arg: user space buffer containing ioctl content
f92363d1
SR
2011 *
2012 * The application will send only buffer_type and unique_id. Driver will
2013 * inspect unique_id first, if valid, fill in all the info. If unique_id is
2014 * 0x00, the driver will return info specified by Buffer Type.
2015 */
2016static long
2017_ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2018{
2019 struct mpt3_diag_query karg;
2020 void *request_data;
2021 int i;
2022 u8 buffer_type;
2023
2024 if (copy_from_user(&karg, arg, sizeof(karg))) {
2025 pr_err("failure at %s:%d/%s()!\n",
2026 __FILE__, __LINE__, __func__);
2027 return -EFAULT;
2028 }
2029
919d8a3f
JP
2030 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2031 __func__));
f92363d1
SR
2032
2033 karg.application_flags = 0;
2034 buffer_type = karg.buffer_type;
2035
2036 if (!_ctl_diag_capability(ioc, buffer_type)) {
919d8a3f
JP
2037 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2038 __func__, buffer_type);
f92363d1
SR
2039 return -EPERM;
2040 }
2041
a066f4c3
SR
2042 if (!(ioc->diag_buffer_status[buffer_type] &
2043 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED)) {
2044 if ((ioc->diag_buffer_status[buffer_type] &
2045 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2046 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2047 __func__, buffer_type);
2048 return -EINVAL;
2049 }
f92363d1
SR
2050 }
2051
08e7378e 2052 if (karg.unique_id) {
f92363d1 2053 if (karg.unique_id != ioc->unique_id[buffer_type]) {
919d8a3f
JP
2054 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2055 __func__, karg.unique_id);
f92363d1
SR
2056 return -EINVAL;
2057 }
2058 }
2059
2060 request_data = ioc->diag_buffer[buffer_type];
2061 if (!request_data) {
919d8a3f
JP
2062 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2063 __func__, buffer_type);
f92363d1
SR
2064 return -ENOMEM;
2065 }
2066
a066f4c3
SR
2067 if ((ioc->diag_buffer_status[buffer_type] &
2068 MPT3_DIAG_BUFFER_IS_REGISTERED))
2069 karg.application_flags |= MPT3_APP_FLAGS_BUFFER_VALID;
2070
2071 if (!(ioc->diag_buffer_status[buffer_type] &
2072 MPT3_DIAG_BUFFER_IS_RELEASED))
2073 karg.application_flags |= MPT3_APP_FLAGS_FW_BUFFER_ACCESS;
2074
2075 if (!(ioc->diag_buffer_status[buffer_type] &
2076 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED))
2077 karg.application_flags |= MPT3_APP_FLAGS_DYNAMIC_BUFFER_ALLOC;
f92363d1 2078
a8a6cbcd
SR
2079 if ((ioc->diag_buffer_status[buffer_type] &
2080 MPT3_DIAG_BUFFER_IS_APP_OWNED))
2081 karg.application_flags |= MPT3_APP_FLAGS_APP_OWNED;
2082
f92363d1
SR
2083 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2084 karg.product_specific[i] =
2085 ioc->product_specific[buffer_type][i];
2086
2087 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
2088 karg.driver_added_buffer_size = 0;
2089 karg.unique_id = ioc->unique_id[buffer_type];
2090 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
2091
2092 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
919d8a3f
JP
2093 ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
2094 __func__, arg);
f92363d1
SR
2095 return -EFAULT;
2096 }
2097 return 0;
2098}
2099
2100/**
2101 * mpt3sas_send_diag_release - Diag Release Message
2102 * @ioc: per adapter object
4beb4867
BVA
2103 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
2104 * @issue_reset: specifies whether host reset is required.
f92363d1
SR
2105 *
2106 */
2107int
2108mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
2109 u8 *issue_reset)
2110{
2111 Mpi2DiagReleaseRequest_t *mpi_request;
2112 Mpi2DiagReleaseReply_t *mpi_reply;
2113 u16 smid;
2114 u16 ioc_status;
2115 u32 ioc_state;
2116 int rc;
c6bdb6a1 2117 u8 reset_needed = 0;
f92363d1 2118
919d8a3f
JP
2119 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2120 __func__));
f92363d1
SR
2121
2122 rc = 0;
2123 *issue_reset = 0;
2124
c6bdb6a1 2125
f92363d1
SR
2126 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2127 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2128 if (ioc->diag_buffer_status[buffer_type] &
2129 MPT3_DIAG_BUFFER_IS_REGISTERED)
2130 ioc->diag_buffer_status[buffer_type] |=
2131 MPT3_DIAG_BUFFER_IS_RELEASED;
919d8a3f
JP
2132 dctlprintk(ioc,
2133 ioc_info(ioc, "%s: skipping due to FAULT state\n",
2134 __func__));
f92363d1
SR
2135 rc = -EAGAIN;
2136 goto out;
2137 }
2138
2139 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
919d8a3f 2140 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
f92363d1
SR
2141 rc = -EAGAIN;
2142 goto out;
2143 }
2144
2145 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2146 if (!smid) {
919d8a3f 2147 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
f92363d1
SR
2148 rc = -EAGAIN;
2149 goto out;
2150 }
2151
2152 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2153 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2154 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2155 ioc->ctl_cmds.smid = smid;
2156
2157 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
2158 mpi_request->BufferType = buffer_type;
2159 mpi_request->VF_ID = 0; /* TODO */
2160 mpi_request->VP_ID = 0;
2161
2162 init_completion(&ioc->ctl_cmds.done);
078a4cc1 2163 ioc->put_smid_default(ioc, smid);
8bbb1cf6 2164 wait_for_completion_timeout(&ioc->ctl_cmds.done,
f92363d1
SR
2165 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2166
2167 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
c6bdb6a1
SR
2168 mpt3sas_check_cmd_timeout(ioc,
2169 ioc->ctl_cmds.status, mpi_request,
2170 sizeof(Mpi2DiagReleaseRequest_t)/4, reset_needed);
2171 *issue_reset = reset_needed;
f92363d1
SR
2172 rc = -EFAULT;
2173 goto out;
2174 }
2175
2176 /* process the completed Reply Message Frame */
2177 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
919d8a3f 2178 ioc_err(ioc, "%s: no reply message\n", __func__);
f92363d1
SR
2179 rc = -EFAULT;
2180 goto out;
2181 }
2182
2183 mpi_reply = ioc->ctl_cmds.reply;
2184 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2185
2186 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2187 ioc->diag_buffer_status[buffer_type] |=
2188 MPT3_DIAG_BUFFER_IS_RELEASED;
919d8a3f 2189 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
f92363d1 2190 } else {
919d8a3f
JP
2191 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2192 __func__,
2193 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
f92363d1
SR
2194 rc = -EFAULT;
2195 }
2196
2197 out:
2198 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2199 return rc;
2200}
2201
2202/**
2203 * _ctl_diag_release - request to send Diag Release Message to firmware
4beb4867
BVA
2204 * @ioc: ?
2205 * @arg: user space buffer containing ioctl content
f92363d1
SR
2206 *
2207 * This allows ownership of the specified buffer to returned to the driver,
2208 * allowing an application to read the buffer without fear that firmware is
9a284e5c 2209 * overwriting information in the buffer.
f92363d1
SR
2210 */
2211static long
2212_ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2213{
2214 struct mpt3_diag_release karg;
2215 void *request_data;
2216 int rc;
2217 u8 buffer_type;
2218 u8 issue_reset = 0;
2219
2220 if (copy_from_user(&karg, arg, sizeof(karg))) {
2221 pr_err("failure at %s:%d/%s()!\n",
2222 __FILE__, __LINE__, __func__);
2223 return -EFAULT;
2224 }
2225
919d8a3f
JP
2226 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2227 __func__));
f92363d1 2228
08e7378e
SR
2229 buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2230 if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2231 ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2232 __func__, karg.unique_id);
2233 return -EINVAL;
2234 }
2235
f92363d1 2236 if (!_ctl_diag_capability(ioc, buffer_type)) {
919d8a3f
JP
2237 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2238 __func__, buffer_type);
f92363d1
SR
2239 return -EPERM;
2240 }
2241
2242 if ((ioc->diag_buffer_status[buffer_type] &
2243 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
919d8a3f
JP
2244 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2245 __func__, buffer_type);
f92363d1
SR
2246 return -EINVAL;
2247 }
2248
2249 if (karg.unique_id != ioc->unique_id[buffer_type]) {
919d8a3f
JP
2250 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2251 __func__, karg.unique_id);
f92363d1
SR
2252 return -EINVAL;
2253 }
2254
2255 if (ioc->diag_buffer_status[buffer_type] &
2256 MPT3_DIAG_BUFFER_IS_RELEASED) {
919d8a3f
JP
2257 ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
2258 __func__, buffer_type);
29f571f8 2259 return -EINVAL;
f92363d1
SR
2260 }
2261
2262 request_data = ioc->diag_buffer[buffer_type];
2263
2264 if (!request_data) {
919d8a3f
JP
2265 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2266 __func__, buffer_type);
f92363d1
SR
2267 return -ENOMEM;
2268 }
2269
2270 /* buffers were released by due to host reset */
2271 if ((ioc->diag_buffer_status[buffer_type] &
2272 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2273 ioc->diag_buffer_status[buffer_type] |=
2274 MPT3_DIAG_BUFFER_IS_RELEASED;
2275 ioc->diag_buffer_status[buffer_type] &=
2276 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
919d8a3f
JP
2277 ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
2278 __func__, buffer_type);
f92363d1
SR
2279 return 0;
2280 }
2281
2282 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2283
2284 if (issue_reset)
98c56ad3 2285 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1
SR
2286
2287 return rc;
2288}
2289
2290/**
2291 * _ctl_diag_read_buffer - request for copy of the diag buffer
2292 * @ioc: per adapter object
4beb4867 2293 * @arg: user space buffer containing ioctl content
f92363d1
SR
2294 */
2295static long
2296_ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2297{
2298 struct mpt3_diag_read_buffer karg;
2299 struct mpt3_diag_read_buffer __user *uarg = arg;
2300 void *request_data, *diag_data;
2301 Mpi2DiagBufferPostRequest_t *mpi_request;
2302 Mpi2DiagBufferPostReply_t *mpi_reply;
2303 int rc, i;
2304 u8 buffer_type;
8bbb1cf6 2305 unsigned long request_size, copy_size;
f92363d1
SR
2306 u16 smid;
2307 u16 ioc_status;
2308 u8 issue_reset = 0;
2309
2310 if (copy_from_user(&karg, arg, sizeof(karg))) {
2311 pr_err("failure at %s:%d/%s()!\n",
2312 __FILE__, __LINE__, __func__);
2313 return -EFAULT;
2314 }
2315
919d8a3f
JP
2316 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2317 __func__));
f92363d1 2318
08e7378e
SR
2319 buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2320 if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2321 ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2322 __func__, karg.unique_id);
2323 return -EINVAL;
2324 }
2325
f92363d1 2326 if (!_ctl_diag_capability(ioc, buffer_type)) {
919d8a3f
JP
2327 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2328 __func__, buffer_type);
f92363d1
SR
2329 return -EPERM;
2330 }
2331
2332 if (karg.unique_id != ioc->unique_id[buffer_type]) {
919d8a3f
JP
2333 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2334 __func__, karg.unique_id);
f92363d1
SR
2335 return -EINVAL;
2336 }
2337
2338 request_data = ioc->diag_buffer[buffer_type];
2339 if (!request_data) {
919d8a3f
JP
2340 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2341 __func__, buffer_type);
f92363d1
SR
2342 return -ENOMEM;
2343 }
2344
2345 request_size = ioc->diag_buffer_sz[buffer_type];
2346
2347 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
919d8a3f
JP
2348 ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
2349 __func__);
f92363d1
SR
2350 return -EINVAL;
2351 }
2352
2353 if (karg.starting_offset > request_size)
2354 return -EINVAL;
2355
2356 diag_data = (void *)(request_data + karg.starting_offset);
919d8a3f
JP
2357 dctlprintk(ioc,
2358 ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2359 __func__, diag_data, karg.starting_offset,
2360 karg.bytes_to_read));
f92363d1
SR
2361
2362 /* Truncate data on requests that are too large */
2363 if ((diag_data + karg.bytes_to_read < diag_data) ||
2364 (diag_data + karg.bytes_to_read > request_data + request_size))
2365 copy_size = request_size - karg.starting_offset;
2366 else
2367 copy_size = karg.bytes_to_read;
2368
2369 if (copy_to_user((void __user *)uarg->diagnostic_data,
2370 diag_data, copy_size)) {
919d8a3f
JP
2371 ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2372 __func__, diag_data);
f92363d1
SR
2373 return -EFAULT;
2374 }
2375
2376 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2377 return 0;
2378
919d8a3f
JP
2379 dctlprintk(ioc,
2380 ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
2381 __func__, buffer_type));
f92363d1
SR
2382 if ((ioc->diag_buffer_status[buffer_type] &
2383 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
919d8a3f
JP
2384 dctlprintk(ioc,
2385 ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
2386 __func__, buffer_type));
f92363d1
SR
2387 return 0;
2388 }
2389 /* Get a free request frame and save the message context.
2390 */
2391
2392 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
919d8a3f 2393 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
f92363d1
SR
2394 rc = -EAGAIN;
2395 goto out;
2396 }
2397
2398 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2399 if (!smid) {
919d8a3f 2400 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
f92363d1
SR
2401 rc = -EAGAIN;
2402 goto out;
2403 }
2404
2405 rc = 0;
2406 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2407 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2408 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2409 ioc->ctl_cmds.smid = smid;
2410
2411 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2412 mpi_request->BufferType = buffer_type;
2413 mpi_request->BufferLength =
2414 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2415 mpi_request->BufferAddress =
2416 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2417 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2418 mpi_request->ProductSpecific[i] =
2419 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2420 mpi_request->VF_ID = 0; /* TODO */
2421 mpi_request->VP_ID = 0;
2422
2423 init_completion(&ioc->ctl_cmds.done);
078a4cc1 2424 ioc->put_smid_default(ioc, smid);
8bbb1cf6 2425 wait_for_completion_timeout(&ioc->ctl_cmds.done,
f92363d1
SR
2426 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2427
2428 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
c6bdb6a1
SR
2429 mpt3sas_check_cmd_timeout(ioc,
2430 ioc->ctl_cmds.status, mpi_request,
2431 sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
f92363d1
SR
2432 goto issue_host_reset;
2433 }
2434
2435 /* process the completed Reply Message Frame */
2436 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
919d8a3f 2437 ioc_err(ioc, "%s: no reply message\n", __func__);
f92363d1
SR
2438 rc = -EFAULT;
2439 goto out;
2440 }
2441
2442 mpi_reply = ioc->ctl_cmds.reply;
2443 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2444
2445 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2446 ioc->diag_buffer_status[buffer_type] |=
2447 MPT3_DIAG_BUFFER_IS_REGISTERED;
dd180e4e
SR
2448 ioc->diag_buffer_status[buffer_type] &=
2449 ~MPT3_DIAG_BUFFER_IS_RELEASED;
919d8a3f 2450 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
f92363d1 2451 } else {
919d8a3f
JP
2452 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2453 __func__, ioc_status,
2454 le32_to_cpu(mpi_reply->IOCLogInfo));
f92363d1
SR
2455 rc = -EFAULT;
2456 }
2457
2458 issue_host_reset:
2459 if (issue_reset)
98c56ad3 2460 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1
SR
2461
2462 out:
2463
2464 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2465 return rc;
2466}
2467
2468
2469
2470#ifdef CONFIG_COMPAT
2471/**
2472 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2473 * @ioc: per adapter object
4beb4867
BVA
2474 * @cmd: ioctl opcode
2475 * @arg: (struct mpt3_ioctl_command32)
f92363d1
SR
2476 *
2477 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2478 */
2479static long
2480_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2481 void __user *arg)
2482{
2483 struct mpt3_ioctl_command32 karg32;
2484 struct mpt3_ioctl_command32 __user *uarg;
2485 struct mpt3_ioctl_command karg;
2486
2487 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2488 return -EINVAL;
2489
2490 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2491
2492 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2493 pr_err("failure at %s:%d/%s()!\n",
2494 __FILE__, __LINE__, __func__);
2495 return -EFAULT;
2496 }
2497
2498 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2499 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2500 karg.hdr.port_number = karg32.hdr.port_number;
2501 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2502 karg.timeout = karg32.timeout;
2503 karg.max_reply_bytes = karg32.max_reply_bytes;
2504 karg.data_in_size = karg32.data_in_size;
2505 karg.data_out_size = karg32.data_out_size;
2506 karg.max_sense_bytes = karg32.max_sense_bytes;
2507 karg.data_sge_offset = karg32.data_sge_offset;
2508 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2509 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2510 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2511 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2512 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2513}
2514#endif
2515
2516/**
2517 * _ctl_ioctl_main - main ioctl entry point
4beb4867
BVA
2518 * @file: (struct file)
2519 * @cmd: ioctl opcode
2520 * @arg: user space data buffer
2521 * @compat: handles 32 bit applications in 64bit os
c84b06a4 2522 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
b130b0d5 2523 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
f92363d1
SR
2524 */
2525static long
2526_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
c84b06a4 2527 u8 compat, u16 mpi_version)
f92363d1
SR
2528{
2529 struct MPT3SAS_ADAPTER *ioc;
2530 struct mpt3_ioctl_header ioctl_header;
2531 enum block_state state;
2532 long ret = -EINVAL;
2533
2534 /* get IOCTL header */
2535 if (copy_from_user(&ioctl_header, (char __user *)arg,
2536 sizeof(struct mpt3_ioctl_header))) {
2537 pr_err("failure at %s:%d/%s()!\n",
2538 __FILE__, __LINE__, __func__);
2539 return -EFAULT;
2540 }
2541
c84b06a4
SR
2542 if (_ctl_verify_adapter(ioctl_header.ioc_number,
2543 &ioc, mpi_version) == -1 || !ioc)
f92363d1
SR
2544 return -ENODEV;
2545
08c4d550
SR
2546 /* pci_access_mutex lock acquired by ioctl path */
2547 mutex_lock(&ioc->pci_access_mutex);
2548
f92363d1 2549 if (ioc->shost_recovery || ioc->pci_error_recovery ||
08c4d550
SR
2550 ioc->is_driver_loading || ioc->remove_host) {
2551 ret = -EAGAIN;
2552 goto out_unlock_pciaccess;
2553 }
f92363d1
SR
2554
2555 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2556 if (state == NON_BLOCKING) {
08c4d550
SR
2557 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2558 ret = -EAGAIN;
2559 goto out_unlock_pciaccess;
2560 }
2561 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2562 ret = -ERESTARTSYS;
2563 goto out_unlock_pciaccess;
2564 }
f92363d1
SR
2565
2566
2567 switch (cmd) {
2568 case MPT3IOCINFO:
2569 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2570 ret = _ctl_getiocinfo(ioc, arg);
2571 break;
2572#ifdef CONFIG_COMPAT
2573 case MPT3COMMAND32:
2574#endif
2575 case MPT3COMMAND:
2576 {
2577 struct mpt3_ioctl_command __user *uarg;
2578 struct mpt3_ioctl_command karg;
2579
2580#ifdef CONFIG_COMPAT
2581 if (compat) {
2582 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2583 break;
2584 }
2585#endif
2586 if (copy_from_user(&karg, arg, sizeof(karg))) {
2587 pr_err("failure at %s:%d/%s()!\n",
2588 __FILE__, __LINE__, __func__);
2589 ret = -EFAULT;
2590 break;
2591 }
2592
f9e3ebee
GZ
2593 if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2594 ret = -EINVAL;
2595 break;
2596 }
f92363d1
SR
2597 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2598 uarg = arg;
2599 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2600 }
2601 break;
2602 }
2603 case MPT3EVENTQUERY:
2604 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2605 ret = _ctl_eventquery(ioc, arg);
2606 break;
2607 case MPT3EVENTENABLE:
2608 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2609 ret = _ctl_eventenable(ioc, arg);
2610 break;
2611 case MPT3EVENTREPORT:
2612 ret = _ctl_eventreport(ioc, arg);
2613 break;
2614 case MPT3HARDRESET:
2615 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2616 ret = _ctl_do_reset(ioc, arg);
2617 break;
2618 case MPT3BTDHMAPPING:
2619 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2620 ret = _ctl_btdh_mapping(ioc, arg);
2621 break;
2622 case MPT3DIAGREGISTER:
2623 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2624 ret = _ctl_diag_register(ioc, arg);
2625 break;
2626 case MPT3DIAGUNREGISTER:
2627 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2628 ret = _ctl_diag_unregister(ioc, arg);
2629 break;
2630 case MPT3DIAGQUERY:
2631 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2632 ret = _ctl_diag_query(ioc, arg);
2633 break;
2634 case MPT3DIAGRELEASE:
2635 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2636 ret = _ctl_diag_release(ioc, arg);
2637 break;
2638 case MPT3DIAGREADBUFFER:
2639 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2640 ret = _ctl_diag_read_buffer(ioc, arg);
2641 break;
2642 default:
919d8a3f
JP
2643 dctlprintk(ioc,
2644 ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2645 cmd));
f92363d1
SR
2646 break;
2647 }
2648
2649 mutex_unlock(&ioc->ctl_cmds.mutex);
08c4d550
SR
2650out_unlock_pciaccess:
2651 mutex_unlock(&ioc->pci_access_mutex);
f92363d1
SR
2652 return ret;
2653}
2654
2655/**
c84b06a4 2656 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
4beb4867
BVA
2657 * @file: (struct file)
2658 * @cmd: ioctl opcode
2659 * @arg: ?
f92363d1 2660 */
8bbb1cf6 2661static long
c84b06a4 2662_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
f92363d1
SR
2663{
2664 long ret;
2665
b130b0d5
SS
2666 /* pass MPI25_VERSION | MPI26_VERSION value,
2667 * to indicate that this ioctl cmd
c84b06a4
SR
2668 * came from mpt3ctl ioctl device.
2669 */
b130b0d5
SS
2670 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2671 MPI25_VERSION | MPI26_VERSION);
f92363d1
SR
2672 return ret;
2673}
2674
c84b06a4
SR
2675/**
2676 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
4beb4867
BVA
2677 * @file: (struct file)
2678 * @cmd: ioctl opcode
2679 * @arg: ?
c84b06a4 2680 */
8bbb1cf6 2681static long
c84b06a4
SR
2682_ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2683{
2684 long ret;
2685
2686 /* pass MPI2_VERSION value, to indicate that this ioctl cmd
2687 * came from mpt2ctl ioctl device.
2688 */
2689 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2690 return ret;
2691}
f92363d1
SR
2692#ifdef CONFIG_COMPAT
2693/**
c84b06a4 2694 *_ ctl_ioctl_compat - main ioctl entry point (compat)
4beb4867
BVA
2695 * @file: ?
2696 * @cmd: ?
2697 * @arg: ?
f92363d1
SR
2698 *
2699 * This routine handles 32 bit applications in 64bit os.
2700 */
8bbb1cf6 2701static long
c84b06a4 2702_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
f92363d1
SR
2703{
2704 long ret;
2705
b130b0d5
SS
2706 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2707 MPI25_VERSION | MPI26_VERSION);
c84b06a4
SR
2708 return ret;
2709}
2710
2711/**
2712 *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
4beb4867
BVA
2713 * @file: ?
2714 * @cmd: ?
2715 * @arg: ?
c84b06a4
SR
2716 *
2717 * This routine handles 32 bit applications in 64bit os.
2718 */
8bbb1cf6 2719static long
c84b06a4
SR
2720_ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2721{
2722 long ret;
2723
2724 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
f92363d1
SR
2725 return ret;
2726}
2727#endif
2728
2729/* scsi host attributes */
2730/**
c9df1442 2731 * version_fw_show - firmware version
4beb4867
BVA
2732 * @cdev: pointer to embedded class device
2733 * @attr: ?
2734 * @buf: the buffer returned
f92363d1
SR
2735 *
2736 * A sysfs 'read-only' shost attribute.
2737 */
2738static ssize_t
c9df1442 2739version_fw_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
2740 char *buf)
2741{
2742 struct Scsi_Host *shost = class_to_shost(cdev);
2743 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2744
2745 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2746 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2747 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2748 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2749 ioc->facts.FWVersion.Word & 0x000000FF);
2750}
c9df1442 2751static DEVICE_ATTR_RO(version_fw);
f92363d1
SR
2752
2753/**
c9df1442 2754 * version_bios_show - bios version
4beb4867
BVA
2755 * @cdev: pointer to embedded class device
2756 * @attr: ?
2757 * @buf: the buffer returned
f92363d1
SR
2758 *
2759 * A sysfs 'read-only' shost attribute.
2760 */
2761static ssize_t
c9df1442 2762version_bios_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
2763 char *buf)
2764{
2765 struct Scsi_Host *shost = class_to_shost(cdev);
2766 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2767
2768 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2769
2770 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2771 (version & 0xFF000000) >> 24,
2772 (version & 0x00FF0000) >> 16,
2773 (version & 0x0000FF00) >> 8,
2774 version & 0x000000FF);
2775}
c9df1442 2776static DEVICE_ATTR_RO(version_bios);
f92363d1
SR
2777
2778/**
c9df1442 2779 * version_mpi_show - MPI (message passing interface) version
4beb4867
BVA
2780 * @cdev: pointer to embedded class device
2781 * @attr: ?
2782 * @buf: the buffer returned
f92363d1
SR
2783 *
2784 * A sysfs 'read-only' shost attribute.
2785 */
2786static ssize_t
c9df1442 2787version_mpi_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
2788 char *buf)
2789{
2790 struct Scsi_Host *shost = class_to_shost(cdev);
2791 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2792
2793 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2794 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2795}
c9df1442 2796static DEVICE_ATTR_RO(version_mpi);
f92363d1
SR
2797
2798/**
c9df1442 2799 * version_product_show - product name
4beb4867
BVA
2800 * @cdev: pointer to embedded class device
2801 * @attr: ?
2802 * @buf: the buffer returned
f92363d1
SR
2803 *
2804 * A sysfs 'read-only' shost attribute.
2805 */
2806static ssize_t
c9df1442 2807version_product_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
2808 char *buf)
2809{
2810 struct Scsi_Host *shost = class_to_shost(cdev);
2811 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2812
2813 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2814}
c9df1442 2815static DEVICE_ATTR_RO(version_product);
f92363d1
SR
2816
2817/**
c9df1442 2818 * version_nvdata_persistent_show - ndvata persistent version
4beb4867
BVA
2819 * @cdev: pointer to embedded class device
2820 * @attr: ?
2821 * @buf: the buffer returned
f92363d1
SR
2822 *
2823 * A sysfs 'read-only' shost attribute.
2824 */
2825static ssize_t
c9df1442 2826version_nvdata_persistent_show(struct device *cdev,
f92363d1
SR
2827 struct device_attribute *attr, char *buf)
2828{
2829 struct Scsi_Host *shost = class_to_shost(cdev);
2830 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2831
2832 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2833 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2834}
c9df1442 2835static DEVICE_ATTR_RO(version_nvdata_persistent);
f92363d1
SR
2836
2837/**
c9df1442 2838 * version_nvdata_default_show - nvdata default version
4beb4867
BVA
2839 * @cdev: pointer to embedded class device
2840 * @attr: ?
2841 * @buf: the buffer returned
f92363d1
SR
2842 *
2843 * A sysfs 'read-only' shost attribute.
2844 */
2845static ssize_t
c9df1442 2846version_nvdata_default_show(struct device *cdev, struct device_attribute
f92363d1
SR
2847 *attr, char *buf)
2848{
2849 struct Scsi_Host *shost = class_to_shost(cdev);
2850 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2851
2852 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2853 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2854}
c9df1442 2855static DEVICE_ATTR_RO(version_nvdata_default);
f92363d1
SR
2856
2857/**
c9df1442 2858 * board_name_show - board name
4beb4867
BVA
2859 * @cdev: pointer to embedded class device
2860 * @attr: ?
2861 * @buf: the buffer returned
f92363d1
SR
2862 *
2863 * A sysfs 'read-only' shost attribute.
2864 */
2865static ssize_t
c9df1442 2866board_name_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
2867 char *buf)
2868{
2869 struct Scsi_Host *shost = class_to_shost(cdev);
2870 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2871
2872 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2873}
c9df1442 2874static DEVICE_ATTR_RO(board_name);
f92363d1
SR
2875
2876/**
c9df1442 2877 * board_assembly_show - board assembly name
4beb4867
BVA
2878 * @cdev: pointer to embedded class device
2879 * @attr: ?
2880 * @buf: the buffer returned
f92363d1
SR
2881 *
2882 * A sysfs 'read-only' shost attribute.
2883 */
2884static ssize_t
c9df1442 2885board_assembly_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
2886 char *buf)
2887{
2888 struct Scsi_Host *shost = class_to_shost(cdev);
2889 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2890
2891 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2892}
c9df1442 2893static DEVICE_ATTR_RO(board_assembly);
f92363d1
SR
2894
2895/**
c9df1442 2896 * board_tracer_show - board tracer number
4beb4867
BVA
2897 * @cdev: pointer to embedded class device
2898 * @attr: ?
2899 * @buf: the buffer returned
f92363d1
SR
2900 *
2901 * A sysfs 'read-only' shost attribute.
2902 */
2903static ssize_t
c9df1442 2904board_tracer_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
2905 char *buf)
2906{
2907 struct Scsi_Host *shost = class_to_shost(cdev);
2908 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2909
2910 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2911}
c9df1442 2912static DEVICE_ATTR_RO(board_tracer);
f92363d1
SR
2913
2914/**
c9df1442 2915 * io_delay_show - io missing delay
4beb4867
BVA
2916 * @cdev: pointer to embedded class device
2917 * @attr: ?
2918 * @buf: the buffer returned
f92363d1
SR
2919 *
2920 * This is for firmware implemention for deboucing device
2921 * removal events.
2922 *
2923 * A sysfs 'read-only' shost attribute.
2924 */
2925static ssize_t
c9df1442 2926io_delay_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
2927 char *buf)
2928{
2929 struct Scsi_Host *shost = class_to_shost(cdev);
2930 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2931
2932 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2933}
c9df1442 2934static DEVICE_ATTR_RO(io_delay);
f92363d1
SR
2935
2936/**
c9df1442 2937 * device_delay_show - device missing delay
4beb4867
BVA
2938 * @cdev: pointer to embedded class device
2939 * @attr: ?
2940 * @buf: the buffer returned
f92363d1
SR
2941 *
2942 * This is for firmware implemention for deboucing device
2943 * removal events.
2944 *
2945 * A sysfs 'read-only' shost attribute.
2946 */
2947static ssize_t
c9df1442 2948device_delay_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
2949 char *buf)
2950{
2951 struct Scsi_Host *shost = class_to_shost(cdev);
2952 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2953
2954 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2955}
c9df1442 2956static DEVICE_ATTR_RO(device_delay);
f92363d1
SR
2957
2958/**
c9df1442 2959 * fw_queue_depth_show - global credits
4beb4867
BVA
2960 * @cdev: pointer to embedded class device
2961 * @attr: ?
2962 * @buf: the buffer returned
f92363d1
SR
2963 *
2964 * This is firmware queue depth limit
2965 *
2966 * A sysfs 'read-only' shost attribute.
2967 */
2968static ssize_t
c9df1442 2969fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
2970 char *buf)
2971{
2972 struct Scsi_Host *shost = class_to_shost(cdev);
2973 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2974
2975 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2976}
c9df1442 2977static DEVICE_ATTR_RO(fw_queue_depth);
f92363d1
SR
2978
2979/**
c9df1442 2980 * sas_address_show - sas address
4beb4867
BVA
2981 * @cdev: pointer to embedded class device
2982 * @attr: ?
2983 * @buf: the buffer returned
f92363d1
SR
2984 *
2985 * This is the controller sas address
2986 *
2987 * A sysfs 'read-only' shost attribute.
2988 */
2989static ssize_t
c9df1442 2990host_sas_address_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
2991 char *buf)
2992
2993{
2994 struct Scsi_Host *shost = class_to_shost(cdev);
2995 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2996
2997 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2998 (unsigned long long)ioc->sas_hba.sas_address);
2999}
c9df1442 3000static DEVICE_ATTR_RO(host_sas_address);
f92363d1
SR
3001
3002/**
c9df1442 3003 * logging_level_show - logging level
4beb4867
BVA
3004 * @cdev: pointer to embedded class device
3005 * @attr: ?
3006 * @buf: the buffer returned
f92363d1
SR
3007 *
3008 * A sysfs 'read/write' shost attribute.
3009 */
3010static ssize_t
c9df1442 3011logging_level_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
3012 char *buf)
3013{
3014 struct Scsi_Host *shost = class_to_shost(cdev);
3015 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3016
3017 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
3018}
3019static ssize_t
c9df1442 3020logging_level_store(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
3021 const char *buf, size_t count)
3022{
3023 struct Scsi_Host *shost = class_to_shost(cdev);
3024 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3025 int val = 0;
3026
3027 if (sscanf(buf, "%x", &val) != 1)
3028 return -EINVAL;
3029
3030 ioc->logging_level = val;
919d8a3f
JP
3031 ioc_info(ioc, "logging_level=%08xh\n",
3032 ioc->logging_level);
f92363d1
SR
3033 return strlen(buf);
3034}
c9df1442 3035static DEVICE_ATTR_RW(logging_level);
f92363d1
SR
3036
3037/**
c9df1442 3038 * fwfault_debug_show - show/store fwfault_debug
4beb4867
BVA
3039 * @cdev: pointer to embedded class device
3040 * @attr: ?
3041 * @buf: the buffer returned
f92363d1
SR
3042 *
3043 * mpt3sas_fwfault_debug is command line option
3044 * A sysfs 'read/write' shost attribute.
3045 */
3046static ssize_t
c9df1442 3047fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
3048 char *buf)
3049{
3050 struct Scsi_Host *shost = class_to_shost(cdev);
3051 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3052
3053 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
3054}
3055static ssize_t
c9df1442 3056fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
3057 const char *buf, size_t count)
3058{
3059 struct Scsi_Host *shost = class_to_shost(cdev);
3060 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3061 int val = 0;
3062
3063 if (sscanf(buf, "%d", &val) != 1)
3064 return -EINVAL;
3065
3066 ioc->fwfault_debug = val;
919d8a3f
JP
3067 ioc_info(ioc, "fwfault_debug=%d\n",
3068 ioc->fwfault_debug);
f92363d1
SR
3069 return strlen(buf);
3070}
c9df1442 3071static DEVICE_ATTR_RW(fwfault_debug);
f92363d1
SR
3072
3073/**
c9df1442 3074 * ioc_reset_count_show - ioc reset count
4beb4867
BVA
3075 * @cdev: pointer to embedded class device
3076 * @attr: ?
3077 * @buf: the buffer returned
f92363d1
SR
3078 *
3079 * This is firmware queue depth limit
3080 *
3081 * A sysfs 'read-only' shost attribute.
3082 */
3083static ssize_t
c9df1442 3084ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
3085 char *buf)
3086{
3087 struct Scsi_Host *shost = class_to_shost(cdev);
3088 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3089
3090 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
3091}
c9df1442 3092static DEVICE_ATTR_RO(ioc_reset_count);
f92363d1
SR
3093
3094/**
c9df1442 3095 * reply_queue_count_show - number of reply queues
4beb4867
BVA
3096 * @cdev: pointer to embedded class device
3097 * @attr: ?
3098 * @buf: the buffer returned
f92363d1
SR
3099 *
3100 * This is number of reply queues
3101 *
3102 * A sysfs 'read-only' shost attribute.
3103 */
3104static ssize_t
c9df1442 3105reply_queue_count_show(struct device *cdev,
f92363d1
SR
3106 struct device_attribute *attr, char *buf)
3107{
3108 u8 reply_queue_count;
3109 struct Scsi_Host *shost = class_to_shost(cdev);
3110 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3111
3112 if ((ioc->facts.IOCCapabilities &
3113 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
3114 reply_queue_count = ioc->reply_queue_count;
3115 else
3116 reply_queue_count = 1;
3117
3118 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
3119}
c9df1442 3120static DEVICE_ATTR_RO(reply_queue_count);
f92363d1 3121
42263095 3122/**
c9df1442 3123 * BRM_status_show - Backup Rail Monitor Status
4beb4867
BVA
3124 * @cdev: pointer to embedded class device
3125 * @attr: ?
3126 * @buf: the buffer returned
42263095
SR
3127 *
3128 * This is number of reply queues
3129 *
3130 * A sysfs 'read-only' shost attribute.
3131 */
3132static ssize_t
c9df1442 3133BRM_status_show(struct device *cdev, struct device_attribute *attr,
42263095
SR
3134 char *buf)
3135{
3136 struct Scsi_Host *shost = class_to_shost(cdev);
3137 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3138 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
3139 Mpi2ConfigReply_t mpi_reply;
3140 u16 backup_rail_monitor_status = 0;
3141 u16 ioc_status;
3142 int sz;
3143 ssize_t rc = 0;
3144
3145 if (!ioc->is_warpdrive) {
919d8a3f
JP
3146 ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
3147 __func__);
cb551b8d 3148 return 0;
42263095 3149 }
08c4d550
SR
3150 /* pci_access_mutex lock acquired by sysfs show path */
3151 mutex_lock(&ioc->pci_access_mutex);
0fd18145
JT
3152 if (ioc->pci_error_recovery || ioc->remove_host)
3153 goto out;
42263095
SR
3154
3155 /* allocate upto GPIOVal 36 entries */
3156 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
3157 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
3158 if (!io_unit_pg3) {
0fd18145 3159 rc = -ENOMEM;
919d8a3f
JP
3160 ioc_err(ioc, "%s: failed allocating memory for iounit_pg3: (%d) bytes\n",
3161 __func__, sz);
42263095
SR
3162 goto out;
3163 }
3164
3165 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
3166 0) {
919d8a3f
JP
3167 ioc_err(ioc, "%s: failed reading iounit_pg3\n",
3168 __func__);
0fd18145 3169 rc = -EINVAL;
42263095
SR
3170 goto out;
3171 }
3172
3173 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3174 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
919d8a3f
JP
3175 ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
3176 __func__, ioc_status);
0fd18145 3177 rc = -EINVAL;
42263095
SR
3178 goto out;
3179 }
3180
3181 if (io_unit_pg3->GPIOCount < 25) {
919d8a3f
JP
3182 ioc_err(ioc, "%s: iounit_pg3->GPIOCount less than 25 entries, detected (%d) entries\n",
3183 __func__, io_unit_pg3->GPIOCount);
0fd18145 3184 rc = -EINVAL;
42263095
SR
3185 goto out;
3186 }
3187
3188 /* BRM status is in bit zero of GPIOVal[24] */
3189 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
3190 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
3191
3192 out:
3193 kfree(io_unit_pg3);
08c4d550 3194 mutex_unlock(&ioc->pci_access_mutex);
42263095
SR
3195 return rc;
3196}
c9df1442 3197static DEVICE_ATTR_RO(BRM_status);
42263095 3198
f92363d1
SR
3199struct DIAG_BUFFER_START {
3200 __le32 Size;
3201 __le32 DiagVersion;
3202 u8 BufferType;
3203 u8 Reserved[3];
3204 __le32 Reserved1;
3205 __le32 Reserved2;
3206 __le32 Reserved3;
3207};
3208
3209/**
c9df1442 3210 * host_trace_buffer_size_show - host buffer size (trace only)
4beb4867
BVA
3211 * @cdev: pointer to embedded class device
3212 * @attr: ?
3213 * @buf: the buffer returned
f92363d1
SR
3214 *
3215 * A sysfs 'read-only' shost attribute.
3216 */
3217static ssize_t
c9df1442 3218host_trace_buffer_size_show(struct device *cdev,
f92363d1
SR
3219 struct device_attribute *attr, char *buf)
3220{
3221 struct Scsi_Host *shost = class_to_shost(cdev);
3222 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3223 u32 size = 0;
3224 struct DIAG_BUFFER_START *request_data;
3225
3226 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
919d8a3f
JP
3227 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3228 __func__);
f92363d1
SR
3229 return 0;
3230 }
3231
3232 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3233 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
919d8a3f
JP
3234 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3235 __func__);
f92363d1
SR
3236 return 0;
3237 }
3238
3239 request_data = (struct DIAG_BUFFER_START *)
3240 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
3241 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
3242 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
3243 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
3244 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
3245 size = le32_to_cpu(request_data->Size);
3246
3247 ioc->ring_buffer_sz = size;
3248 return snprintf(buf, PAGE_SIZE, "%d\n", size);
3249}
c9df1442 3250static DEVICE_ATTR_RO(host_trace_buffer_size);
f92363d1
SR
3251
3252/**
c9df1442 3253 * host_trace_buffer_show - firmware ring buffer (trace only)
4beb4867
BVA
3254 * @cdev: pointer to embedded class device
3255 * @attr: ?
3256 * @buf: the buffer returned
f92363d1
SR
3257 *
3258 * A sysfs 'read/write' shost attribute.
3259 *
3260 * You will only be able to read 4k bytes of ring buffer at a time.
3261 * In order to read beyond 4k bytes, you will have to write out the
3262 * offset to the same attribute, it will move the pointer.
3263 */
3264static ssize_t
c9df1442 3265host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
3266 char *buf)
3267{
3268 struct Scsi_Host *shost = class_to_shost(cdev);
3269 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3270 void *request_data;
3271 u32 size;
3272
3273 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
919d8a3f
JP
3274 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3275 __func__);
f92363d1
SR
3276 return 0;
3277 }
3278
3279 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3280 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
919d8a3f
JP
3281 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3282 __func__);
f92363d1
SR
3283 return 0;
3284 }
3285
3286 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3287 return 0;
3288
3289 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3290 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3291 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3292 memcpy(buf, request_data, size);
3293 return size;
3294}
3295
3296static ssize_t
c9df1442 3297host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
f92363d1
SR
3298 const char *buf, size_t count)
3299{
3300 struct Scsi_Host *shost = class_to_shost(cdev);
3301 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3302 int val = 0;
3303
3304 if (sscanf(buf, "%d", &val) != 1)
3305 return -EINVAL;
3306
3307 ioc->ring_buffer_offset = val;
3308 return strlen(buf);
3309}
c9df1442 3310static DEVICE_ATTR_RW(host_trace_buffer);
f92363d1
SR
3311
3312
3313/*****************************************/
3314
3315/**
c9df1442 3316 * host_trace_buffer_enable_show - firmware ring buffer (trace only)
4beb4867
BVA
3317 * @cdev: pointer to embedded class device
3318 * @attr: ?
3319 * @buf: the buffer returned
f92363d1
SR
3320 *
3321 * A sysfs 'read/write' shost attribute.
3322 *
3323 * This is a mechnism to post/release host_trace_buffers
3324 */
3325static ssize_t
c9df1442 3326host_trace_buffer_enable_show(struct device *cdev,
f92363d1
SR
3327 struct device_attribute *attr, char *buf)
3328{
3329 struct Scsi_Host *shost = class_to_shost(cdev);
3330 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3331
3332 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3333 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3334 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3335 return snprintf(buf, PAGE_SIZE, "off\n");
3336 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3337 MPT3_DIAG_BUFFER_IS_RELEASED))
3338 return snprintf(buf, PAGE_SIZE, "release\n");
3339 else
3340 return snprintf(buf, PAGE_SIZE, "post\n");
3341}
3342
3343static ssize_t
c9df1442 3344host_trace_buffer_enable_store(struct device *cdev,
f92363d1
SR
3345 struct device_attribute *attr, const char *buf, size_t count)
3346{
3347 struct Scsi_Host *shost = class_to_shost(cdev);
3348 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3349 char str[10] = "";
3350 struct mpt3_diag_register diag_register;
3351 u8 issue_reset = 0;
3352
3353 /* don't allow post/release occurr while recovery is active */
3354 if (ioc->shost_recovery || ioc->remove_host ||
3355 ioc->pci_error_recovery || ioc->is_driver_loading)
3356 return -EBUSY;
3357
3358 if (sscanf(buf, "%9s", str) != 1)
3359 return -EINVAL;
3360
3361 if (!strcmp(str, "post")) {
3362 /* exit out if host buffers are already posted */
3363 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3364 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3365 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3366 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3367 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3368 goto out;
3369 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
919d8a3f 3370 ioc_info(ioc, "posting host trace buffers\n");
f92363d1 3371 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
d04a6edf
SR
3372
3373 if (ioc->manu_pg11.HostTraceBufferMaxSizeKB != 0 &&
3374 ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0) {
3375 /* post the same buffer allocated previously */
3376 diag_register.requested_buffer_size =
3377 ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE];
a8a6cbcd
SR
3378 } else {
3379 /*
3380 * Free the diag buffer memory which was previously
3381 * allocated by an application.
3382 */
3383 if ((ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0)
3384 &&
3385 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3386 MPT3_DIAG_BUFFER_IS_APP_OWNED)) {
3387 pci_free_consistent(ioc->pdev,
3388 ioc->diag_buffer_sz[
3389 MPI2_DIAG_BUF_TYPE_TRACE],
3390 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE],
3391 ioc->diag_buffer_dma[
3392 MPI2_DIAG_BUF_TYPE_TRACE]);
3393 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE] =
3394 NULL;
3395 }
3396
d04a6edf 3397 diag_register.requested_buffer_size = (1024 * 1024);
a8a6cbcd 3398 }
d04a6edf 3399
08e7378e
SR
3400 diag_register.unique_id =
3401 (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
3402 (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
f92363d1
SR
3403 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3404 _ctl_diag_register_2(ioc, &diag_register);
a066f4c3
SR
3405 if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3406 MPT3_DIAG_BUFFER_IS_REGISTERED) {
3407 ioc_info(ioc,
3408 "Trace buffer %d KB allocated through sysfs\n",
3409 diag_register.requested_buffer_size>>10);
3410 if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
3411 ioc->diag_buffer_status[
3412 MPI2_DIAG_BUF_TYPE_TRACE] |=
3413 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
3414 }
f92363d1
SR
3415 } else if (!strcmp(str, "release")) {
3416 /* exit out if host buffers are already released */
3417 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3418 goto out;
3419 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3420 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3421 goto out;
3422 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3423 MPT3_DIAG_BUFFER_IS_RELEASED))
3424 goto out;
919d8a3f 3425 ioc_info(ioc, "releasing host trace buffer\n");
f92363d1
SR
3426 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3427 &issue_reset);
3428 }
3429
3430 out:
3431 return strlen(buf);
3432}
c9df1442 3433static DEVICE_ATTR_RW(host_trace_buffer_enable);
f92363d1
SR
3434
3435/*********** diagnostic trigger suppport *********************************/
3436
3437/**
c9df1442 3438 * diag_trigger_master_show - show the diag_trigger_master attribute
4beb4867
BVA
3439 * @cdev: pointer to embedded class device
3440 * @attr: ?
3441 * @buf: the buffer returned
f92363d1
SR
3442 *
3443 * A sysfs 'read/write' shost attribute.
3444 */
3445static ssize_t
c9df1442 3446diag_trigger_master_show(struct device *cdev,
f92363d1
SR
3447 struct device_attribute *attr, char *buf)
3448
3449{
3450 struct Scsi_Host *shost = class_to_shost(cdev);
3451 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3452 unsigned long flags;
3453 ssize_t rc;
3454
3455 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3456 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3457 memcpy(buf, &ioc->diag_trigger_master, rc);
3458 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3459 return rc;
3460}
3461
3462/**
c9df1442 3463 * diag_trigger_master_store - store the diag_trigger_master attribute
4beb4867
BVA
3464 * @cdev: pointer to embedded class device
3465 * @attr: ?
3466 * @buf: the buffer returned
3467 * @count: ?
f92363d1
SR
3468 *
3469 * A sysfs 'read/write' shost attribute.
3470 */
3471static ssize_t
c9df1442 3472diag_trigger_master_store(struct device *cdev,
f92363d1
SR
3473 struct device_attribute *attr, const char *buf, size_t count)
3474
3475{
3476 struct Scsi_Host *shost = class_to_shost(cdev);
3477 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3478 unsigned long flags;
3479 ssize_t rc;
3480
3481 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3482 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3483 memset(&ioc->diag_trigger_master, 0,
3484 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3485 memcpy(&ioc->diag_trigger_master, buf, rc);
3486 ioc->diag_trigger_master.MasterData |=
3487 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3488 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3489 return rc;
3490}
c9df1442 3491static DEVICE_ATTR_RW(diag_trigger_master);
f92363d1
SR
3492
3493
3494/**
c9df1442 3495 * diag_trigger_event_show - show the diag_trigger_event attribute
4beb4867
BVA
3496 * @cdev: pointer to embedded class device
3497 * @attr: ?
3498 * @buf: the buffer returned
f92363d1
SR
3499 *
3500 * A sysfs 'read/write' shost attribute.
3501 */
3502static ssize_t
c9df1442 3503diag_trigger_event_show(struct device *cdev,
f92363d1
SR
3504 struct device_attribute *attr, char *buf)
3505{
3506 struct Scsi_Host *shost = class_to_shost(cdev);
3507 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3508 unsigned long flags;
3509 ssize_t rc;
3510
3511 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3512 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3513 memcpy(buf, &ioc->diag_trigger_event, rc);
3514 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3515 return rc;
3516}
3517
3518/**
c9df1442 3519 * diag_trigger_event_store - store the diag_trigger_event attribute
4beb4867
BVA
3520 * @cdev: pointer to embedded class device
3521 * @attr: ?
3522 * @buf: the buffer returned
3523 * @count: ?
f92363d1
SR
3524 *
3525 * A sysfs 'read/write' shost attribute.
3526 */
3527static ssize_t
c9df1442 3528diag_trigger_event_store(struct device *cdev,
f92363d1
SR
3529 struct device_attribute *attr, const char *buf, size_t count)
3530
3531{
3532 struct Scsi_Host *shost = class_to_shost(cdev);
3533 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3534 unsigned long flags;
3535 ssize_t sz;
3536
3537 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3538 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3539 memset(&ioc->diag_trigger_event, 0,
3540 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3541 memcpy(&ioc->diag_trigger_event, buf, sz);
3542 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3543 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3544 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3545 return sz;
3546}
c9df1442 3547static DEVICE_ATTR_RW(diag_trigger_event);
f92363d1
SR
3548
3549
3550/**
c9df1442 3551 * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
4beb4867
BVA
3552 * @cdev: pointer to embedded class device
3553 * @attr: ?
3554 * @buf: the buffer returned
f92363d1
SR
3555 *
3556 * A sysfs 'read/write' shost attribute.
3557 */
3558static ssize_t
c9df1442 3559diag_trigger_scsi_show(struct device *cdev,
f92363d1
SR
3560 struct device_attribute *attr, char *buf)
3561{
3562 struct Scsi_Host *shost = class_to_shost(cdev);
3563 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3564 unsigned long flags;
3565 ssize_t rc;
3566
3567 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3568 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3569 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3570 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3571 return rc;
3572}
3573
3574/**
c9df1442 3575 * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
4beb4867
BVA
3576 * @cdev: pointer to embedded class device
3577 * @attr: ?
3578 * @buf: the buffer returned
3579 * @count: ?
f92363d1
SR
3580 *
3581 * A sysfs 'read/write' shost attribute.
3582 */
3583static ssize_t
c9df1442 3584diag_trigger_scsi_store(struct device *cdev,
f92363d1
SR
3585 struct device_attribute *attr, const char *buf, size_t count)
3586{
3587 struct Scsi_Host *shost = class_to_shost(cdev);
3588 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3589 unsigned long flags;
3590 ssize_t sz;
3591
3592 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
1de540a9
DC
3593 sz = min(sizeof(ioc->diag_trigger_scsi), count);
3594 memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
f92363d1
SR
3595 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3596 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3597 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3598 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3599 return sz;
3600}
c9df1442 3601static DEVICE_ATTR_RW(diag_trigger_scsi);
f92363d1
SR
3602
3603
3604/**
c9df1442 3605 * diag_trigger_scsi_show - show the diag_trigger_mpi attribute
4beb4867
BVA
3606 * @cdev: pointer to embedded class device
3607 * @attr: ?
3608 * @buf: the buffer returned
f92363d1
SR
3609 *
3610 * A sysfs 'read/write' shost attribute.
3611 */
3612static ssize_t
c9df1442 3613diag_trigger_mpi_show(struct device *cdev,
f92363d1
SR
3614 struct device_attribute *attr, char *buf)
3615{
3616 struct Scsi_Host *shost = class_to_shost(cdev);
3617 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3618 unsigned long flags;
3619 ssize_t rc;
3620
3621 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3622 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3623 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3624 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3625 return rc;
3626}
3627
3628/**
c9df1442 3629 * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
4beb4867
BVA
3630 * @cdev: pointer to embedded class device
3631 * @attr: ?
3632 * @buf: the buffer returned
3633 * @count: ?
f92363d1
SR
3634 *
3635 * A sysfs 'read/write' shost attribute.
3636 */
3637static ssize_t
c9df1442 3638diag_trigger_mpi_store(struct device *cdev,
f92363d1
SR
3639 struct device_attribute *attr, const char *buf, size_t count)
3640{
3641 struct Scsi_Host *shost = class_to_shost(cdev);
3642 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3643 unsigned long flags;
3644 ssize_t sz;
3645
3646 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3647 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3648 memset(&ioc->diag_trigger_mpi, 0,
66331e8c 3649 sizeof(ioc->diag_trigger_mpi));
f92363d1
SR
3650 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3651 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3652 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3653 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3654 return sz;
3655}
3656
c9df1442 3657static DEVICE_ATTR_RW(diag_trigger_mpi);
f92363d1
SR
3658
3659/*********** diagnostic trigger suppport *** END ****************************/
3660
f92363d1
SR
3661/*****************************************/
3662
3ac8e47b
SP
3663/**
3664 * drv_support_bitmap_show - driver supported feature bitmap
9133d27e
DLM
3665 * @cdev: pointer to embedded class device
3666 * @attr: unused
3667 * @buf: the buffer returned
3ac8e47b
SP
3668 *
3669 * A sysfs 'read-only' shost attribute.
3670 */
3671static ssize_t
3672drv_support_bitmap_show(struct device *cdev,
3673 struct device_attribute *attr, char *buf)
3674{
3675 struct Scsi_Host *shost = class_to_shost(cdev);
3676 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3677
3678 return snprintf(buf, PAGE_SIZE, "0x%08x\n", ioc->drv_support_bitmap);
3679}
3680static DEVICE_ATTR_RO(drv_support_bitmap);
3681
8dc8d29a
SR
3682/**
3683 * enable_sdev_max_qd_show - display whether sdev max qd is enabled/disabled
9133d27e
DLM
3684 * @cdev: pointer to embedded class device
3685 * @attr: unused
3686 * @buf: the buffer returned
8dc8d29a
SR
3687 *
3688 * A sysfs read/write shost attribute. This attribute is used to set the
3689 * targets queue depth to HBA IO queue depth if this attribute is enabled.
3690 */
3691static ssize_t
3692enable_sdev_max_qd_show(struct device *cdev,
3693 struct device_attribute *attr, char *buf)
3694{
3695 struct Scsi_Host *shost = class_to_shost(cdev);
3696 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3697
3698 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->enable_sdev_max_qd);
3699}
3700
3701/**
3702 * enable_sdev_max_qd_store - Enable/disable sdev max qd
9133d27e
DLM
3703 * @cdev: pointer to embedded class device
3704 * @attr: unused
3705 * @buf: the buffer returned
3706 * @count: unused
8dc8d29a
SR
3707 *
3708 * A sysfs read/write shost attribute. This attribute is used to set the
3709 * targets queue depth to HBA IO queue depth if this attribute is enabled.
3710 * If this attribute is disabled then targets will have corresponding default
3711 * queue depth.
3712 */
3713static ssize_t
3714enable_sdev_max_qd_store(struct device *cdev,
3715 struct device_attribute *attr, const char *buf, size_t count)
3716{
3717 struct Scsi_Host *shost = class_to_shost(cdev);
3718 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3719 struct MPT3SAS_DEVICE *sas_device_priv_data;
3720 struct MPT3SAS_TARGET *sas_target_priv_data;
3721 int val = 0;
3722 struct scsi_device *sdev;
3723 struct _raid_device *raid_device;
3724 int qdepth;
3725
3726 if (kstrtoint(buf, 0, &val) != 0)
3727 return -EINVAL;
3728
3729 switch (val) {
3730 case 0:
3731 ioc->enable_sdev_max_qd = 0;
3732 shost_for_each_device(sdev, ioc->shost) {
3733 sas_device_priv_data = sdev->hostdata;
3734 if (!sas_device_priv_data)
3735 continue;
3736 sas_target_priv_data = sas_device_priv_data->sas_target;
3737 if (!sas_target_priv_data)
3738 continue;
3739
3740 if (sas_target_priv_data->flags &
3741 MPT_TARGET_FLAGS_VOLUME) {
3742 raid_device =
3743 mpt3sas_raid_device_find_by_handle(ioc,
3744 sas_target_priv_data->handle);
3745
3746 switch (raid_device->volume_type) {
3747 case MPI2_RAID_VOL_TYPE_RAID0:
3748 if (raid_device->device_info &
3749 MPI2_SAS_DEVICE_INFO_SSP_TARGET)
3750 qdepth =
3751 MPT3SAS_SAS_QUEUE_DEPTH;
3752 else
3753 qdepth =
3754 MPT3SAS_SATA_QUEUE_DEPTH;
3755 break;
3756 case MPI2_RAID_VOL_TYPE_RAID1E:
3757 case MPI2_RAID_VOL_TYPE_RAID1:
3758 case MPI2_RAID_VOL_TYPE_RAID10:
3759 case MPI2_RAID_VOL_TYPE_UNKNOWN:
3760 default:
3761 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
3762 }
3763 } else if (sas_target_priv_data->flags &
3764 MPT_TARGET_FLAGS_PCIE_DEVICE)
3765 qdepth = MPT3SAS_NVME_QUEUE_DEPTH;
3766 else
3767 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
3768
3769 mpt3sas_scsih_change_queue_depth(sdev, qdepth);
3770 }
3771 break;
3772 case 1:
3773 ioc->enable_sdev_max_qd = 1;
3774 shost_for_each_device(sdev, ioc->shost)
3775 mpt3sas_scsih_change_queue_depth(sdev,
3776 shost->can_queue);
3777 break;
3778 default:
3779 return -EINVAL;
3780 }
3781
3782 return strlen(buf);
3783}
3784static DEVICE_ATTR_RW(enable_sdev_max_qd);
3785
f92363d1
SR
3786struct device_attribute *mpt3sas_host_attrs[] = {
3787 &dev_attr_version_fw,
3788 &dev_attr_version_bios,
3789 &dev_attr_version_mpi,
3790 &dev_attr_version_product,
3791 &dev_attr_version_nvdata_persistent,
3792 &dev_attr_version_nvdata_default,
3793 &dev_attr_board_name,
3794 &dev_attr_board_assembly,
3795 &dev_attr_board_tracer,
3796 &dev_attr_io_delay,
3797 &dev_attr_device_delay,
3798 &dev_attr_logging_level,
3799 &dev_attr_fwfault_debug,
3800 &dev_attr_fw_queue_depth,
3801 &dev_attr_host_sas_address,
3802 &dev_attr_ioc_reset_count,
3803 &dev_attr_host_trace_buffer_size,
3804 &dev_attr_host_trace_buffer,
3805 &dev_attr_host_trace_buffer_enable,
3806 &dev_attr_reply_queue_count,
3807 &dev_attr_diag_trigger_master,
3808 &dev_attr_diag_trigger_event,
3809 &dev_attr_diag_trigger_scsi,
3810 &dev_attr_diag_trigger_mpi,
3ac8e47b 3811 &dev_attr_drv_support_bitmap,
42263095 3812 &dev_attr_BRM_status,
8dc8d29a 3813 &dev_attr_enable_sdev_max_qd,
f92363d1
SR
3814 NULL,
3815};
3816
3817/* device attributes */
3818
3819/**
c9df1442 3820 * sas_address_show - sas address
4beb4867
BVA
3821 * @dev: pointer to embedded class device
3822 * @attr: ?
3823 * @buf: the buffer returned
f92363d1
SR
3824 *
3825 * This is the sas address for the target
3826 *
3827 * A sysfs 'read-only' shost attribute.
3828 */
3829static ssize_t
c9df1442 3830sas_address_show(struct device *dev, struct device_attribute *attr,
f92363d1
SR
3831 char *buf)
3832{
3833 struct scsi_device *sdev = to_scsi_device(dev);
3834 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3835
3836 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3837 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3838}
c9df1442 3839static DEVICE_ATTR_RO(sas_address);
f92363d1
SR
3840
3841/**
c9df1442 3842 * sas_device_handle_show - device handle
4beb4867
BVA
3843 * @dev: pointer to embedded class device
3844 * @attr: ?
3845 * @buf: the buffer returned
f92363d1
SR
3846 *
3847 * This is the firmware assigned device handle
3848 *
3849 * A sysfs 'read-only' shost attribute.
3850 */
3851static ssize_t
c9df1442 3852sas_device_handle_show(struct device *dev, struct device_attribute *attr,
f92363d1
SR
3853 char *buf)
3854{
3855 struct scsi_device *sdev = to_scsi_device(dev);
3856 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3857
3858 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3859 sas_device_priv_data->sas_target->handle);
3860}
c9df1442 3861static DEVICE_ATTR_RO(sas_device_handle);
f92363d1 3862
307d9075 3863/**
c9df1442 3864 * sas_ncq_io_prio_show - send prioritized io commands to device
4beb4867
BVA
3865 * @dev: pointer to embedded device
3866 * @attr: ?
3867 * @buf: the buffer returned
307d9075
AM
3868 *
3869 * A sysfs 'read/write' sdev attribute, only works with SATA
3870 */
3871static ssize_t
c9df1442 3872sas_ncq_prio_enable_show(struct device *dev,
307d9075
AM
3873 struct device_attribute *attr, char *buf)
3874{
3875 struct scsi_device *sdev = to_scsi_device(dev);
3876 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3877
3878 return snprintf(buf, PAGE_SIZE, "%d\n",
3879 sas_device_priv_data->ncq_prio_enable);
3880}
3881
3882static ssize_t
c9df1442 3883sas_ncq_prio_enable_store(struct device *dev,
307d9075
AM
3884 struct device_attribute *attr,
3885 const char *buf, size_t count)
3886{
3887 struct scsi_device *sdev = to_scsi_device(dev);
3888 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3889 bool ncq_prio_enable = 0;
3890
3891 if (kstrtobool(buf, &ncq_prio_enable))
3892 return -EINVAL;
3893
3894 if (!scsih_ncq_prio_supp(sdev))
3895 return -EINVAL;
3896
3897 sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
3898 return strlen(buf);
3899}
c9df1442 3900static DEVICE_ATTR_RW(sas_ncq_prio_enable);
307d9075 3901
f92363d1
SR
3902struct device_attribute *mpt3sas_dev_attrs[] = {
3903 &dev_attr_sas_address,
3904 &dev_attr_sas_device_handle,
307d9075 3905 &dev_attr_sas_ncq_prio_enable,
f92363d1
SR
3906 NULL,
3907};
3908
c84b06a4
SR
3909/* file operations table for mpt3ctl device */
3910static const struct file_operations ctl_fops = {
3911 .owner = THIS_MODULE,
3912 .unlocked_ioctl = _ctl_ioctl,
3913 .poll = _ctl_poll,
3914 .fasync = _ctl_fasync,
3915#ifdef CONFIG_COMPAT
3916 .compat_ioctl = _ctl_ioctl_compat,
3917#endif
3918};
3919
3920/* file operations table for mpt2ctl device */
3921static const struct file_operations ctl_gen2_fops = {
3922 .owner = THIS_MODULE,
3923 .unlocked_ioctl = _ctl_mpt2_ioctl,
3924 .poll = _ctl_poll,
3925 .fasync = _ctl_fasync,
3926#ifdef CONFIG_COMPAT
3927 .compat_ioctl = _ctl_mpt2_ioctl_compat,
3928#endif
3929};
3930
3931static struct miscdevice ctl_dev = {
3932 .minor = MPT3SAS_MINOR,
3933 .name = MPT3SAS_DEV_NAME,
3934 .fops = &ctl_fops,
3935};
3936
3937static struct miscdevice gen2_ctl_dev = {
3938 .minor = MPT2SAS_MINOR,
3939 .name = MPT2SAS_DEV_NAME,
3940 .fops = &ctl_gen2_fops,
3941};
3942
f92363d1 3943/**
c84b06a4 3944 * mpt3sas_ctl_init - main entry point for ctl.
4beb4867 3945 * @hbas_to_enumerate: ?
f92363d1
SR
3946 */
3947void
c84b06a4 3948mpt3sas_ctl_init(ushort hbas_to_enumerate)
f92363d1
SR
3949{
3950 async_queue = NULL;
c84b06a4
SR
3951
3952 /* Don't register mpt3ctl ioctl device if
3953 * hbas_to_enumarate is one.
3954 */
3955 if (hbas_to_enumerate != 1)
3956 if (misc_register(&ctl_dev) < 0)
3957 pr_err("%s can't register misc device [minor=%d]\n",
3958 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3959
3960 /* Don't register mpt3ctl ioctl device if
3961 * hbas_to_enumarate is two.
3962 */
3963 if (hbas_to_enumerate != 2)
3964 if (misc_register(&gen2_ctl_dev) < 0)
3965 pr_err("%s can't register misc device [minor=%d]\n",
3966 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3967
f92363d1
SR
3968 init_waitqueue_head(&ctl_poll_wait);
3969}
3970
3971/**
c84b06a4 3972 * mpt3sas_ctl_exit - exit point for ctl
4beb4867 3973 * @hbas_to_enumerate: ?
f92363d1
SR
3974 */
3975void
c84b06a4 3976mpt3sas_ctl_exit(ushort hbas_to_enumerate)
f92363d1
SR
3977{
3978 struct MPT3SAS_ADAPTER *ioc;
3979 int i;
3980
3981 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3982
3983 /* free memory associated to diag buffers */
3984 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3985 if (!ioc->diag_buffer[i])
3986 continue;
1c2048bd
CH
3987 dma_free_coherent(&ioc->pdev->dev,
3988 ioc->diag_buffer_sz[i],
3989 ioc->diag_buffer[i],
3990 ioc->diag_buffer_dma[i]);
f92363d1
SR
3991 ioc->diag_buffer[i] = NULL;
3992 ioc->diag_buffer_status[i] = 0;
3993 }
3994
3995 kfree(ioc->event_log);
3996 }
c84b06a4
SR
3997 if (hbas_to_enumerate != 1)
3998 misc_deregister(&ctl_dev);
3999 if (hbas_to_enumerate != 2)
4000 misc_deregister(&gen2_ctl_dev);
f92363d1 4001}