blob: 41c54d4c945198475fc0c29b69351a9aef5ea0c4 [file] [log] [blame]
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301/*
2 * Management Module Support for MPT (Message Passing Technology) based
3 * controllers
4 *
5 * This code is based on drivers/scsi/mpt3sas/mpt3sas_ctl.c
Sreekanth Reddya4ffce02014-09-12 15:35:29 +05306 * Copyright (C) 2012-2014 LSI Corporation
Sreekanth Reddya03bd152015-01-12 11:39:02 +05307 * Copyright (C) 2013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * NO WARRANTY
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
30
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
43 * USA.
44 */
45
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053046#include <linux/kernel.h>
47#include <linux/module.h>
48#include <linux/errno.h>
49#include <linux/init.h>
50#include <linux/slab.h>
51#include <linux/types.h>
52#include <linux/pci.h>
53#include <linux/delay.h>
54#include <linux/compat.h>
55#include <linux/poll.h>
56
57#include <linux/io.h>
58#include <linux/uaccess.h>
59
60#include "mpt3sas_base.h"
61#include "mpt3sas_ctl.h"
62
63
64static struct fasync_struct *async_queue;
65static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
66
67
68/**
69 * enum block_state - blocking state
70 * @NON_BLOCKING: non blocking
71 * @BLOCKING: blocking
72 *
73 * These states are for ioctls that need to wait for a response
74 * from firmware, so they probably require sleep.
75 */
76enum block_state {
77 NON_BLOCKING,
78 BLOCKING,
79};
80
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053081/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053082 * _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;
183 }
184
185 if (!desc)
186 return;
187
Joe Perches919d8a32018-09-17 08:01:09 -0700188 ioc_info(ioc, "%s: %s, smid(%d)\n", calling_function_name, desc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530189
190 if (!mpi_reply)
191 return;
192
193 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
Joe Perches919d8a32018-09-17 08:01:09 -0700194 ioc_info(ioc, "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
195 le16_to_cpu(mpi_reply->IOCStatus),
196 le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530197
198 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
199 mpi_request->Function ==
200 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
201 Mpi2SCSIIOReply_t *scsi_reply =
202 (Mpi2SCSIIOReply_t *)mpi_reply;
203 struct _sas_device *sas_device = NULL;
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530204 struct _pcie_device *pcie_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530205
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530206 sas_device = mpt3sas_get_sdev_by_handle(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530207 le16_to_cpu(scsi_reply->DevHandle));
208 if (sas_device) {
Joe Perches919d8a32018-09-17 08:01:09 -0700209 ioc_warn(ioc, "\tsas_address(0x%016llx), phy(%d)\n",
210 (u64)sas_device->sas_address,
211 sas_device->phy);
212 ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
213 (u64)sas_device->enclosure_logical_id,
214 sas_device->slot);
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530215 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530216 }
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530217 if (!sas_device) {
218 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
219 le16_to_cpu(scsi_reply->DevHandle));
220 if (pcie_device) {
Joe Perches919d8a32018-09-17 08:01:09 -0700221 ioc_warn(ioc, "\tWWID(0x%016llx), port(%d)\n",
222 (unsigned long long)pcie_device->wwid,
223 pcie_device->port_num);
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530224 if (pcie_device->enclosure_handle != 0)
Joe Perches919d8a32018-09-17 08:01:09 -0700225 ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
226 (u64)pcie_device->enclosure_logical_id,
227 pcie_device->slot);
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530228 pcie_device_put(pcie_device);
229 }
230 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530231 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
Joe Perches919d8a32018-09-17 08:01:09 -0700232 ioc_info(ioc, "\tscsi_state(0x%02x), scsi_status(0x%02x)\n",
233 scsi_reply->SCSIState,
234 scsi_reply->SCSIStatus);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530235 }
236}
237
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530238/**
239 * mpt3sas_ctl_done - ctl module completion routine
240 * @ioc: per adapter object
241 * @smid: system request message index
242 * @msix_index: MSIX table index supplied by the OS
243 * @reply: reply message frame(lower 32bit addr)
244 * Context: none.
245 *
246 * The callback handler when using ioc->ctl_cb_idx.
247 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700248 * Return: 1 meaning mf should be freed from _base_interrupt
249 * 0 means the mf is freed from this function.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530250 */
251u8
252mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
253 u32 reply)
254{
255 MPI2DefaultReply_t *mpi_reply;
256 Mpi2SCSIIOReply_t *scsiio_reply;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530257 Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530258 const void *sense_data;
259 u32 sz;
260
261 if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
262 return 1;
263 if (ioc->ctl_cmds.smid != smid)
264 return 1;
265 ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
266 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
267 if (mpi_reply) {
268 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
269 ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
270 /* get sense data */
271 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
272 mpi_reply->Function ==
273 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
274 scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
275 if (scsiio_reply->SCSIState &
276 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
277 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
278 le32_to_cpu(scsiio_reply->SenseCount));
279 sense_data = mpt3sas_base_get_sense_buffer(ioc,
280 smid);
281 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
282 }
283 }
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530284 /*
285 * Get Error Response data for NVMe device. The ctl_cmds.sense
286 * buffer is used to store the Error Response data.
287 */
288 if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
289 nvme_error_reply =
290 (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
291 sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400292 le16_to_cpu(nvme_error_reply->ErrorResponseCount));
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530293 sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
294 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
295 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530296 }
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +0530297
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530298 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530299 ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
300 complete(&ioc->ctl_cmds.done);
301 return 1;
302}
303
304/**
305 * _ctl_check_event_type - determines when an event needs logging
306 * @ioc: per adapter object
307 * @event: firmware event
308 *
309 * The bitmask in ioc->event_type[] indicates which events should be
310 * be saved in the driver event_log. This bitmask is set by application.
311 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700312 * Return: 1 when event should be captured, or zero means no match.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530313 */
314static int
315_ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
316{
317 u16 i;
318 u32 desired_event;
319
320 if (event >= 128 || !event || !ioc->event_log)
321 return 0;
322
323 desired_event = (1 << (event % 32));
324 if (!desired_event)
325 desired_event = 1;
326 i = event / 32;
327 return desired_event & ioc->event_type[i];
328}
329
330/**
331 * mpt3sas_ctl_add_to_event_log - add event
332 * @ioc: per adapter object
333 * @mpi_reply: reply message frame
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530334 */
335void
336mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
337 Mpi2EventNotificationReply_t *mpi_reply)
338{
339 struct MPT3_IOCTL_EVENTS *event_log;
340 u16 event;
341 int i;
342 u32 sz, event_data_sz;
343 u8 send_aen = 0;
344
345 if (!ioc->event_log)
346 return;
347
348 event = le16_to_cpu(mpi_reply->Event);
349
350 if (_ctl_check_event_type(ioc, event)) {
351
352 /* insert entry into circular event_log */
353 i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
354 event_log = ioc->event_log;
355 event_log[i].event = event;
356 event_log[i].context = ioc->event_context++;
357
358 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
359 sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
360 memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
361 memcpy(event_log[i].data, mpi_reply->EventData, sz);
362 send_aen = 1;
363 }
364
365 /* This aen_event_read_flag flag is set until the
366 * application has read the event log.
367 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
368 */
369 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
370 (send_aen && !ioc->aen_event_read_flag)) {
371 ioc->aen_event_read_flag = 1;
372 wake_up_interruptible(&ctl_poll_wait);
373 if (async_queue)
374 kill_fasync(&async_queue, SIGIO, POLL_IN);
375 }
376}
377
378/**
379 * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
380 * @ioc: per adapter object
381 * @msix_index: MSIX table index supplied by the OS
382 * @reply: reply message frame(lower 32bit addr)
383 * Context: interrupt.
384 *
385 * This function merely adds a new work task into ioc->firmware_event_thread.
386 * The tasks are worked from _firmware_event_work in user context.
387 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700388 * Return: 1 meaning mf should be freed from _base_interrupt
389 * 0 means the mf is freed from this function.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530390 */
391u8
392mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
393 u32 reply)
394{
395 Mpi2EventNotificationReply_t *mpi_reply;
396
397 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
Suganath prabu Subramani869817f2016-01-28 12:07:00 +0530398 if (mpi_reply)
399 mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530400 return 1;
401}
402
403/**
404 * _ctl_verify_adapter - validates ioc_number passed from application
Bart Van Assche4beb4862018-06-15 14:42:01 -0700405 * @ioc_number: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530406 * @iocpp: The ioc pointer is returned in this.
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530407 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530408 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530409 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700410 * Return: (-1) means error, else ioc_number.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530411 */
412static int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530413_ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
414 int mpi_version)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530415{
416 struct MPT3SAS_ADAPTER *ioc;
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530417 int version = 0;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530418 /* global ioc lock to protect controller on list operations */
419 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530420 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
421 if (ioc->id != ioc_number)
422 continue;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530423 /* Check whether this ioctl command is from right
424 * ioctl device or not, if not continue the search.
425 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530426 version = ioc->hba_mpi_version_belonged;
427 /* MPI25_VERSION and MPI26_VERSION uses same ioctl
428 * device.
429 */
430 if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
431 if ((version == MPI25_VERSION) ||
432 (version == MPI26_VERSION))
433 goto out;
434 else
435 continue;
436 } else {
437 if (version != mpi_version)
438 continue;
439 }
440out:
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530441 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530442 *iocpp = ioc;
443 return ioc_number;
444 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530445 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530446 *iocpp = NULL;
447 return -1;
448}
449
450/**
451 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
452 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530453 *
454 * The handler for doing any required cleanup or initialization.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530455 */
Bart Van Asschec7a35702018-06-15 14:42:00 -0700456void mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530457{
458 int i;
459 u8 issue_reset;
460
Joe Perches919d8a32018-09-17 08:01:09 -0700461 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
Bart Van Asschec7a35702018-06-15 14:42:00 -0700462 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
463 if (!(ioc->diag_buffer_status[i] &
464 MPT3_DIAG_BUFFER_IS_REGISTERED))
465 continue;
466 if ((ioc->diag_buffer_status[i] &
467 MPT3_DIAG_BUFFER_IS_RELEASED))
468 continue;
469 mpt3sas_send_diag_release(ioc, i, &issue_reset);
470 }
471}
472
473/**
474 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
475 * @ioc: per adapter object
476 *
477 * The handler for doing any required cleanup or initialization.
478 */
479void mpt3sas_ctl_after_reset_handler(struct MPT3SAS_ADAPTER *ioc)
480{
Joe Perches919d8a32018-09-17 08:01:09 -0700481 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_AFTER_RESET\n", __func__));
Bart Van Asschec7a35702018-06-15 14:42:00 -0700482 if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
483 ioc->ctl_cmds.status |= MPT3_CMD_RESET;
484 mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
485 complete(&ioc->ctl_cmds.done);
486 }
487}
488
489/**
490 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
491 * @ioc: per adapter object
492 *
493 * The handler for doing any required cleanup or initialization.
494 */
495void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
496{
497 int i;
498
Joe Perches919d8a32018-09-17 08:01:09 -0700499 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530500
Bart Van Asschec7a35702018-06-15 14:42:00 -0700501 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
502 if (!(ioc->diag_buffer_status[i] &
503 MPT3_DIAG_BUFFER_IS_REGISTERED))
504 continue;
505 if ((ioc->diag_buffer_status[i] &
506 MPT3_DIAG_BUFFER_IS_RELEASED))
507 continue;
508 ioc->diag_buffer_status[i] |=
509 MPT3_DIAG_BUFFER_IS_DIAG_RESET;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530510 }
511}
512
513/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530514 * _ctl_fasync -
Bart Van Assche4beb4862018-06-15 14:42:01 -0700515 * @fd: ?
516 * @filep: ?
517 * @mode: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530518 *
519 * Called when application request fasyn callback handler.
520 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -0700521static int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530522_ctl_fasync(int fd, struct file *filep, int mode)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530523{
524 return fasync_helper(fd, filep, mode, &async_queue);
525}
526
527/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530528 * _ctl_poll -
Bart Van Assche4beb4862018-06-15 14:42:01 -0700529 * @filep: ?
530 * @wait: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530531 *
532 */
Al Viroafc9a422017-07-03 06:39:46 -0400533static __poll_t
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530534_ctl_poll(struct file *filep, poll_table *wait)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530535{
536 struct MPT3SAS_ADAPTER *ioc;
537
538 poll_wait(filep, &ctl_poll_wait, wait);
539
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530540 /* global ioc lock to protect controller on list operations */
541 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530542 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530543 if (ioc->aen_event_read_flag) {
544 spin_unlock(&gioc_lock);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800545 return EPOLLIN | EPOLLRDNORM;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530546 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530547 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530548 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530549 return 0;
550}
551
552/**
553 * _ctl_set_task_mid - assign an active smid to tm request
554 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -0700555 * @karg: (struct mpt3_ioctl_command)
556 * @tm_request: pointer to mf from user space
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530557 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700558 * Return: 0 when an smid if found, else fail.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530559 * during failure, the reply frame is filled.
560 */
561static int
562_ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
563 Mpi2SCSITaskManagementRequest_t *tm_request)
564{
565 u8 found = 0;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800566 u16 smid;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530567 u16 handle;
568 struct scsi_cmnd *scmd;
569 struct MPT3SAS_DEVICE *priv_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530570 Mpi2SCSITaskManagementReply_t *tm_reply;
571 u32 sz;
572 u32 lun;
573 char *desc = NULL;
574
575 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
576 desc = "abort_task";
577 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
578 desc = "query_task";
579 else
580 return 0;
581
582 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
583
584 handle = le16_to_cpu(tm_request->DevHandle);
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800585 for (smid = ioc->scsiio_depth; smid && !found; smid--) {
586 struct scsiio_tracker *st;
587
588 scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
589 if (!scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530590 continue;
591 if (lun != scmd->device->lun)
592 continue;
593 priv_data = scmd->device->hostdata;
594 if (priv_data->sas_target == NULL)
595 continue;
596 if (priv_data->sas_target->handle != handle)
597 continue;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800598 st = scsi_cmd_priv(scmd);
599 tm_request->TaskMID = cpu_to_le16(st->smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530600 found = 1;
601 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530602
603 if (!found) {
Joe Perches919d8a32018-09-17 08:01:09 -0700604 dctlprintk(ioc,
605 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), no active mid!!\n",
606 desc, le16_to_cpu(tm_request->DevHandle),
607 lun));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530608 tm_reply = ioc->ctl_cmds.reply;
609 tm_reply->DevHandle = tm_request->DevHandle;
610 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
611 tm_reply->TaskType = tm_request->TaskType;
612 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
613 tm_reply->VP_ID = tm_request->VP_ID;
614 tm_reply->VF_ID = tm_request->VF_ID;
615 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
616 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
617 sz))
618 pr_err("failure at %s:%d/%s()!\n", __FILE__,
619 __LINE__, __func__);
620 return 1;
621 }
622
Joe Perches919d8a32018-09-17 08:01:09 -0700623 dctlprintk(ioc,
624 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), task_mid(%d)\n",
625 desc, le16_to_cpu(tm_request->DevHandle), lun,
626 le16_to_cpu(tm_request->TaskMID)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530627 return 0;
628}
629
630/**
631 * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
632 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -0700633 * @karg: (struct mpt3_ioctl_command)
634 * @mf: pointer to mf in user space
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530635 */
636static long
637_ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
638 void __user *mf)
639{
640 MPI2RequestHeader_t *mpi_request = NULL, *request;
641 MPI2DefaultReply_t *mpi_reply;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530642 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -0400643 struct _pcie_device *pcie_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530644 u16 smid;
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -0400645 u8 timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530646 u8 issue_reset;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530647 u32 sz, sz_arg;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530648 void *psge;
649 void *data_out = NULL;
650 dma_addr_t data_out_dma = 0;
651 size_t data_out_sz = 0;
652 void *data_in = NULL;
653 dma_addr_t data_in_dma = 0;
654 size_t data_in_sz = 0;
655 long ret;
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530656 u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -0400657 u8 tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530658
659 issue_reset = 0;
660
661 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -0700662 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530663 ret = -EAGAIN;
664 goto out;
665 }
666
Suganath Prabuf4305742018-10-31 18:53:33 +0530667 ret = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
668 if (ret)
669 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530670
671 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
672 if (!mpi_request) {
Joe Perches919d8a32018-09-17 08:01:09 -0700673 ioc_err(ioc, "%s: failed obtaining a memory for mpi_request\n",
674 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530675 ret = -ENOMEM;
676 goto out;
677 }
678
679 /* Check for overflow and wraparound */
680 if (karg.data_sge_offset * 4 > ioc->request_sz ||
681 karg.data_sge_offset > (UINT_MAX / 4)) {
682 ret = -EINVAL;
683 goto out;
684 }
685
686 /* copy in request message frame from user */
687 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
688 pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
689 __func__);
690 ret = -EFAULT;
691 goto out;
692 }
693
694 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
695 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
696 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -0700697 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530698 ret = -EAGAIN;
699 goto out;
700 }
701 } else {
Hannes Reineckeb0cd285e2018-01-04 04:57:07 -0800702 /* Use first reserved smid for passthrough ioctls */
703 smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530704 }
705
706 ret = 0;
707 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
708 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
709 request = mpt3sas_base_get_msg_frame(ioc, smid);
710 memcpy(request, mpi_request, karg.data_sge_offset*4);
711 ioc->ctl_cmds.smid = smid;
712 data_out_sz = karg.data_out_size;
713 data_in_sz = karg.data_in_size;
714
715 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530716 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
717 mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530718 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
719 mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530720
721 device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
722 if (!device_handle || (device_handle >
723 ioc->facts.MaxDevHandle)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530724 ret = -EINVAL;
725 mpt3sas_base_free_smid(ioc, smid);
726 goto out;
727 }
728 }
729
730 /* obtain dma-able memory for data transfer */
731 if (data_out_sz) /* WRITE */ {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +0200732 data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz,
733 &data_out_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530734 if (!data_out) {
735 pr_err("failure at %s:%d/%s()!\n", __FILE__,
736 __LINE__, __func__);
737 ret = -ENOMEM;
738 mpt3sas_base_free_smid(ioc, smid);
739 goto out;
740 }
741 if (copy_from_user(data_out, karg.data_out_buf_ptr,
742 data_out_sz)) {
743 pr_err("failure at %s:%d/%s()!\n", __FILE__,
744 __LINE__, __func__);
745 ret = -EFAULT;
746 mpt3sas_base_free_smid(ioc, smid);
747 goto out;
748 }
749 }
750
751 if (data_in_sz) /* READ */ {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +0200752 data_in = dma_alloc_coherent(&ioc->pdev->dev, data_in_sz,
753 &data_in_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530754 if (!data_in) {
755 pr_err("failure at %s:%d/%s()!\n", __FILE__,
756 __LINE__, __func__);
757 ret = -ENOMEM;
758 mpt3sas_base_free_smid(ioc, smid);
759 goto out;
760 }
761 }
762
763 psge = (void *)request + (karg.data_sge_offset*4);
764
765 /* send command to firmware */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530766 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530767
768 init_completion(&ioc->ctl_cmds.done);
769 switch (mpi_request->Function) {
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530770 case MPI2_FUNCTION_NVME_ENCAPSULATED:
771 {
772 nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
773 /*
774 * Get the Physical Address of the sense buffer.
775 * Use Error Response buffer address field to hold the sense
776 * buffer address.
777 * Clear the internal sense buffer, which will potentially hold
778 * the Completion Queue Entry on return, or 0 if no Entry.
779 * Build the PRPs and set direction bits.
780 * Send the request.
781 */
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400782 nvme_encap_request->ErrorResponseBaseAddress =
783 cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530784 nvme_encap_request->ErrorResponseBaseAddress |=
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400785 cpu_to_le64(le32_to_cpu(
786 mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530787 nvme_encap_request->ErrorResponseAllocationLength =
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400788 cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530789 memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
790 ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
791 data_out_dma, data_out_sz, data_in_dma, data_in_sz);
792 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700793 dtmprintk(ioc,
794 ioc_info(ioc, "handle(0x%04x): ioctl failed due to device removal in progress\n",
795 device_handle));
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530796 mpt3sas_base_free_smid(ioc, smid);
797 ret = -EINVAL;
798 goto out;
799 }
Suganath Prabu S40114bd2018-02-14 02:16:37 -0800800 mpt3sas_base_put_smid_nvme_encap(ioc, smid);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530801 break;
802 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530803 case MPI2_FUNCTION_SCSI_IO_REQUEST:
804 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
805 {
806 Mpi2SCSIIORequest_t *scsiio_request =
807 (Mpi2SCSIIORequest_t *)request;
808 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
809 scsiio_request->SenseBufferLowAddress =
810 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
811 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530812 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700813 dtmprintk(ioc,
814 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
815 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530816 mpt3sas_base_free_smid(ioc, smid);
817 ret = -EINVAL;
818 goto out;
819 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530820 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
821 data_in_dma, data_in_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530822 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +0530823 ioc->put_smid_scsi_io(ioc, smid, device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530824 else
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400825 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530826 break;
827 }
828 case MPI2_FUNCTION_SCSI_TASK_MGMT:
829 {
830 Mpi2SCSITaskManagementRequest_t *tm_request =
831 (Mpi2SCSITaskManagementRequest_t *)request;
832
Joe Perches919d8a32018-09-17 08:01:09 -0700833 dtmprintk(ioc,
834 ioc_info(ioc, "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
835 le16_to_cpu(tm_request->DevHandle),
836 tm_request->TaskType));
Chaitra P B459325c2017-01-23 15:26:08 +0530837 ioc->got_task_abort_from_ioctl = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530838 if (tm_request->TaskType ==
839 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
840 tm_request->TaskType ==
841 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
842 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
843 mpt3sas_base_free_smid(ioc, smid);
Chaitra P B459325c2017-01-23 15:26:08 +0530844 ioc->got_task_abort_from_ioctl = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530845 goto out;
846 }
847 }
Chaitra P B459325c2017-01-23 15:26:08 +0530848 ioc->got_task_abort_from_ioctl = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530849
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530850 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700851 dtmprintk(ioc,
852 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
853 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530854 mpt3sas_base_free_smid(ioc, smid);
855 ret = -EINVAL;
856 goto out;
857 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530858 mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
859 tm_request->DevHandle));
860 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
861 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400862 ioc->put_smid_hi_priority(ioc, smid, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530863 break;
864 }
865 case MPI2_FUNCTION_SMP_PASSTHROUGH:
866 {
867 Mpi2SmpPassthroughRequest_t *smp_request =
868 (Mpi2SmpPassthroughRequest_t *)mpi_request;
869 u8 *data;
870
871 /* ioc determines which port to use */
872 smp_request->PhysicalPort = 0xFF;
873 if (smp_request->PassthroughFlags &
874 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
875 data = (u8 *)&smp_request->SGL;
876 else {
877 if (unlikely(data_out == NULL)) {
878 pr_err("failure at %s:%d/%s()!\n",
879 __FILE__, __LINE__, __func__);
880 mpt3sas_base_free_smid(ioc, smid);
881 ret = -EINVAL;
882 goto out;
883 }
884 data = data_out;
885 }
886
887 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
888 ioc->ioc_link_reset_in_progress = 1;
889 ioc->ignore_loginfos = 1;
890 }
891 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
892 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400893 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530894 break;
895 }
896 case MPI2_FUNCTION_SATA_PASSTHROUGH:
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530897 {
898 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700899 dtmprintk(ioc,
900 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
901 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530902 mpt3sas_base_free_smid(ioc, smid);
903 ret = -EINVAL;
904 goto out;
905 }
906 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
907 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400908 ioc->put_smid_default(ioc, smid);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530909 break;
910 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530911 case MPI2_FUNCTION_FW_DOWNLOAD:
912 case MPI2_FUNCTION_FW_UPLOAD:
913 {
914 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
915 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400916 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530917 break;
918 }
919 case MPI2_FUNCTION_TOOLBOX:
920 {
921 Mpi2ToolboxCleanRequest_t *toolbox_request =
922 (Mpi2ToolboxCleanRequest_t *)mpi_request;
923
924 if (toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL) {
925 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
926 data_in_dma, data_in_sz);
927 } else {
928 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
929 data_in_dma, data_in_sz);
930 }
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400931 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530932 break;
933 }
934 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
935 {
936 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
937 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
938
939 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
940 || sasiounit_request->Operation ==
941 MPI2_SAS_OP_PHY_LINK_RESET) {
942 ioc->ioc_link_reset_in_progress = 1;
943 ioc->ignore_loginfos = 1;
944 }
945 /* drop to default case for posting the request */
946 }
Bart Van Asscheeb0c7af2018-06-15 14:41:56 -0700947 /* fall through */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530948 default:
949 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
950 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400951 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530952 break;
953 }
954
955 if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
956 timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
957 else
958 timeout = karg.timeout;
Calvin Owens8bbb1cf2016-07-28 21:38:22 -0700959 wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530960 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
961 Mpi2SCSITaskManagementRequest_t *tm_request =
962 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
963 mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
964 tm_request->DevHandle));
965 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
966 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
967 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
968 ioc->ioc_link_reset_in_progress) {
969 ioc->ioc_link_reset_in_progress = 0;
970 ioc->ignore_loginfos = 0;
971 }
972 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -0400973 issue_reset =
974 mpt3sas_base_check_cmd_timeout(ioc,
975 ioc->ctl_cmds.status, mpi_request,
976 karg.data_sge_offset);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530977 goto issue_host_reset;
978 }
979
980 mpi_reply = ioc->ctl_cmds.reply;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530981
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530982 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
983 (ioc->logging_level & MPT_DEBUG_TM)) {
984 Mpi2SCSITaskManagementReply_t *tm_reply =
985 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
986
Joe Perches919d8a32018-09-17 08:01:09 -0700987 ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
988 le16_to_cpu(tm_reply->IOCStatus),
989 le32_to_cpu(tm_reply->IOCLogInfo),
990 le32_to_cpu(tm_reply->TerminationCount));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530991 }
Sreekanth Reddyaf009412015-11-11 17:30:23 +0530992
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530993 /* copy out xdata to user */
994 if (data_in_sz) {
995 if (copy_to_user(karg.data_in_buf_ptr, data_in,
996 data_in_sz)) {
997 pr_err("failure at %s:%d/%s()!\n", __FILE__,
998 __LINE__, __func__);
999 ret = -ENODATA;
1000 goto out;
1001 }
1002 }
1003
1004 /* copy out reply message frame to user */
1005 if (karg.max_reply_bytes) {
1006 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1007 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1008 sz)) {
1009 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1010 __LINE__, __func__);
1011 ret = -ENODATA;
1012 goto out;
1013 }
1014 }
1015
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301016 /* copy out sense/NVMe Error Response to user */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301017 if (karg.max_sense_bytes && (mpi_request->Function ==
1018 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301019 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1020 MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1021 if (karg.sense_data_ptr == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001022 ioc_info(ioc, "Response buffer provided by application is NULL; Response data will not be returned\n");
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301023 goto out;
1024 }
1025 sz_arg = (mpi_request->Function ==
1026 MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1027 SCSI_SENSE_BUFFERSIZE;
1028 sz = min_t(u32, karg.max_sense_bytes, sz_arg);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301029 if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1030 sz)) {
1031 pr_err("failure at %s:%d/%s()!\n", __FILE__,
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301032 __LINE__, __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301033 ret = -ENODATA;
1034 goto out;
1035 }
1036 }
1037
1038 issue_host_reset:
1039 if (issue_reset) {
1040 ret = -ENODATA;
1041 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1042 mpi_request->Function ==
1043 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1044 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001045 ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
1046 le16_to_cpu(mpi_request->FunctionDependent1));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301047 mpt3sas_halt_firmware(ioc);
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001048 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1049 le16_to_cpu(mpi_request->FunctionDependent1));
1050 if (pcie_device && (!ioc->tm_custom_handling))
1051 mpt3sas_scsih_issue_locked_tm(ioc,
1052 le16_to_cpu(mpi_request->FunctionDependent1),
1053 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1054 0, pcie_device->reset_timeout,
1055 tr_method);
1056 else
1057 mpt3sas_scsih_issue_locked_tm(ioc,
1058 le16_to_cpu(mpi_request->FunctionDependent1),
1059 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1060 0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301061 } else
Calvin Owens98c56ad2016-07-28 21:38:21 -07001062 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301063 }
1064
1065 out:
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001066 if (pcie_device)
1067 pcie_device_put(pcie_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301068
1069 /* free memory associated with sg buffers */
1070 if (data_in)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001071 dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301072 data_in_dma);
1073
1074 if (data_out)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001075 dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301076 data_out_dma);
1077
1078 kfree(mpi_request);
1079 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1080 return ret;
1081}
1082
1083/**
1084 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1085 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001086 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301087 */
1088static long
1089_ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1090{
1091 struct mpt3_ioctl_iocinfo karg;
1092
Joe Perches919d8a32018-09-17 08:01:09 -07001093 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1094 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301095
1096 memset(&karg, 0 , sizeof(karg));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301097 if (ioc->pfacts)
1098 karg.port_number = ioc->pfacts[0].PortNumber;
1099 karg.hw_rev = ioc->pdev->revision;
1100 karg.pci_id = ioc->pdev->device;
1101 karg.subsystem_device = ioc->pdev->subsystem_device;
1102 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1103 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1104 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1105 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1106 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1107 karg.firmware_version = ioc->facts.FWVersion.Word;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301108 strcpy(karg.driver_version, ioc->driver_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301109 strcat(karg.driver_version, "-");
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301110 switch (ioc->hba_mpi_version_belonged) {
1111 case MPI2_VERSION:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301112 if (ioc->is_warpdrive)
1113 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1114 else
1115 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301116 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1117 break;
1118 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301119 case MPI26_VERSION:
Suganath Prabu Subramani998f26a2016-10-26 13:34:37 +05301120 if (ioc->is_gen35_ioc)
1121 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1122 else
1123 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301124 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1125 break;
1126 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301127 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1128
1129 if (copy_to_user(arg, &karg, sizeof(karg))) {
1130 pr_err("failure at %s:%d/%s()!\n",
1131 __FILE__, __LINE__, __func__);
1132 return -EFAULT;
1133 }
1134 return 0;
1135}
1136
1137/**
1138 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1139 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001140 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301141 */
1142static long
1143_ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1144{
1145 struct mpt3_ioctl_eventquery karg;
1146
1147 if (copy_from_user(&karg, arg, sizeof(karg))) {
1148 pr_err("failure at %s:%d/%s()!\n",
1149 __FILE__, __LINE__, __func__);
1150 return -EFAULT;
1151 }
1152
Joe Perches919d8a32018-09-17 08:01:09 -07001153 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1154 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301155
1156 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1157 memcpy(karg.event_types, ioc->event_type,
1158 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1159
1160 if (copy_to_user(arg, &karg, sizeof(karg))) {
1161 pr_err("failure at %s:%d/%s()!\n",
1162 __FILE__, __LINE__, __func__);
1163 return -EFAULT;
1164 }
1165 return 0;
1166}
1167
1168/**
1169 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1170 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001171 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301172 */
1173static long
1174_ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1175{
1176 struct mpt3_ioctl_eventenable karg;
1177
1178 if (copy_from_user(&karg, arg, sizeof(karg))) {
1179 pr_err("failure at %s:%d/%s()!\n",
1180 __FILE__, __LINE__, __func__);
1181 return -EFAULT;
1182 }
1183
Joe Perches919d8a32018-09-17 08:01:09 -07001184 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1185 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301186
1187 memcpy(ioc->event_type, karg.event_types,
1188 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1189 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1190
1191 if (ioc->event_log)
1192 return 0;
1193 /* initialize event_log */
1194 ioc->event_context = 0;
1195 ioc->aen_event_read_flag = 0;
1196 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1197 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1198 if (!ioc->event_log) {
1199 pr_err("failure at %s:%d/%s()!\n",
1200 __FILE__, __LINE__, __func__);
1201 return -ENOMEM;
1202 }
1203 return 0;
1204}
1205
1206/**
1207 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1208 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001209 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301210 */
1211static long
1212_ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1213{
1214 struct mpt3_ioctl_eventreport karg;
1215 u32 number_bytes, max_events, max;
1216 struct mpt3_ioctl_eventreport __user *uarg = arg;
1217
1218 if (copy_from_user(&karg, arg, sizeof(karg))) {
1219 pr_err("failure at %s:%d/%s()!\n",
1220 __FILE__, __LINE__, __func__);
1221 return -EFAULT;
1222 }
1223
Joe Perches919d8a32018-09-17 08:01:09 -07001224 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1225 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301226
1227 number_bytes = karg.hdr.max_data_size -
1228 sizeof(struct mpt3_ioctl_header);
1229 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1230 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1231
1232 /* If fewer than 1 event is requested, there must have
1233 * been some type of error.
1234 */
1235 if (!max || !ioc->event_log)
1236 return -ENODATA;
1237
1238 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1239 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1240 pr_err("failure at %s:%d/%s()!\n",
1241 __FILE__, __LINE__, __func__);
1242 return -EFAULT;
1243 }
1244
1245 /* reset flag so SIGIO can restart */
1246 ioc->aen_event_read_flag = 0;
1247 return 0;
1248}
1249
1250/**
1251 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1252 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001253 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301254 */
1255static long
1256_ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1257{
1258 struct mpt3_ioctl_diag_reset karg;
1259 int retval;
1260
1261 if (copy_from_user(&karg, arg, sizeof(karg))) {
1262 pr_err("failure at %s:%d/%s()!\n",
1263 __FILE__, __LINE__, __func__);
1264 return -EFAULT;
1265 }
1266
1267 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1268 ioc->is_driver_loading)
1269 return -EAGAIN;
1270
Joe Perches919d8a32018-09-17 08:01:09 -07001271 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1272 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301273
Calvin Owens98c56ad2016-07-28 21:38:21 -07001274 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Joe Perches919d8a32018-09-17 08:01:09 -07001275 ioc_info(ioc, "host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301276 return 0;
1277}
1278
1279/**
1280 * _ctl_btdh_search_sas_device - searching for sas device
1281 * @ioc: per adapter object
1282 * @btdh: btdh ioctl payload
1283 */
1284static int
1285_ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1286 struct mpt3_ioctl_btdh_mapping *btdh)
1287{
1288 struct _sas_device *sas_device;
1289 unsigned long flags;
1290 int rc = 0;
1291
1292 if (list_empty(&ioc->sas_device_list))
1293 return rc;
1294
1295 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1296 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1297 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1298 btdh->handle == sas_device->handle) {
1299 btdh->bus = sas_device->channel;
1300 btdh->id = sas_device->id;
1301 rc = 1;
1302 goto out;
1303 } else if (btdh->bus == sas_device->channel && btdh->id ==
1304 sas_device->id && btdh->handle == 0xFFFF) {
1305 btdh->handle = sas_device->handle;
1306 rc = 1;
1307 goto out;
1308 }
1309 }
1310 out:
1311 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1312 return rc;
1313}
1314
1315/**
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +05301316 * _ctl_btdh_search_pcie_device - searching for pcie device
1317 * @ioc: per adapter object
1318 * @btdh: btdh ioctl payload
1319 */
1320static int
1321_ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
1322 struct mpt3_ioctl_btdh_mapping *btdh)
1323{
1324 struct _pcie_device *pcie_device;
1325 unsigned long flags;
1326 int rc = 0;
1327
1328 if (list_empty(&ioc->pcie_device_list))
1329 return rc;
1330
1331 spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1332 list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1333 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1334 btdh->handle == pcie_device->handle) {
1335 btdh->bus = pcie_device->channel;
1336 btdh->id = pcie_device->id;
1337 rc = 1;
1338 goto out;
1339 } else if (btdh->bus == pcie_device->channel && btdh->id ==
1340 pcie_device->id && btdh->handle == 0xFFFF) {
1341 btdh->handle = pcie_device->handle;
1342 rc = 1;
1343 goto out;
1344 }
1345 }
1346 out:
1347 spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1348 return rc;
1349}
1350
1351/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301352 * _ctl_btdh_search_raid_device - searching for raid device
1353 * @ioc: per adapter object
1354 * @btdh: btdh ioctl payload
1355 */
1356static int
1357_ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1358 struct mpt3_ioctl_btdh_mapping *btdh)
1359{
1360 struct _raid_device *raid_device;
1361 unsigned long flags;
1362 int rc = 0;
1363
1364 if (list_empty(&ioc->raid_device_list))
1365 return rc;
1366
1367 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1368 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1369 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1370 btdh->handle == raid_device->handle) {
1371 btdh->bus = raid_device->channel;
1372 btdh->id = raid_device->id;
1373 rc = 1;
1374 goto out;
1375 } else if (btdh->bus == raid_device->channel && btdh->id ==
1376 raid_device->id && btdh->handle == 0xFFFF) {
1377 btdh->handle = raid_device->handle;
1378 rc = 1;
1379 goto out;
1380 }
1381 }
1382 out:
1383 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1384 return rc;
1385}
1386
1387/**
1388 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1389 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001390 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301391 */
1392static long
1393_ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1394{
1395 struct mpt3_ioctl_btdh_mapping karg;
1396 int rc;
1397
1398 if (copy_from_user(&karg, arg, sizeof(karg))) {
1399 pr_err("failure at %s:%d/%s()!\n",
1400 __FILE__, __LINE__, __func__);
1401 return -EFAULT;
1402 }
1403
Joe Perches919d8a32018-09-17 08:01:09 -07001404 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1405 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301406
1407 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1408 if (!rc)
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +05301409 rc = _ctl_btdh_search_pcie_device(ioc, &karg);
1410 if (!rc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301411 _ctl_btdh_search_raid_device(ioc, &karg);
1412
1413 if (copy_to_user(arg, &karg, sizeof(karg))) {
1414 pr_err("failure at %s:%d/%s()!\n",
1415 __FILE__, __LINE__, __func__);
1416 return -EFAULT;
1417 }
1418 return 0;
1419}
1420
1421/**
1422 * _ctl_diag_capability - return diag buffer capability
1423 * @ioc: per adapter object
1424 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1425 *
1426 * returns 1 when diag buffer support is enabled in firmware
1427 */
1428static u8
1429_ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1430{
1431 u8 rc = 0;
1432
1433 switch (buffer_type) {
1434 case MPI2_DIAG_BUF_TYPE_TRACE:
1435 if (ioc->facts.IOCCapabilities &
1436 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1437 rc = 1;
1438 break;
1439 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1440 if (ioc->facts.IOCCapabilities &
1441 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1442 rc = 1;
1443 break;
1444 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1445 if (ioc->facts.IOCCapabilities &
1446 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1447 rc = 1;
1448 }
1449
1450 return rc;
1451}
1452
1453
1454/**
1455 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1456 * @ioc: per adapter object
1457 * @diag_register: the diag_register struct passed in from user space
1458 *
1459 */
1460static long
1461_ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1462 struct mpt3_diag_register *diag_register)
1463{
1464 int rc, i;
1465 void *request_data = NULL;
1466 dma_addr_t request_data_dma;
1467 u32 request_data_sz = 0;
1468 Mpi2DiagBufferPostRequest_t *mpi_request;
1469 Mpi2DiagBufferPostReply_t *mpi_reply;
1470 u8 buffer_type;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301471 u16 smid;
1472 u16 ioc_status;
1473 u32 ioc_state;
1474 u8 issue_reset = 0;
1475
Joe Perches919d8a32018-09-17 08:01:09 -07001476 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1477 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301478
1479 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1480 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001481 ioc_err(ioc, "%s: failed due to ioc not operational\n",
1482 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301483 rc = -EAGAIN;
1484 goto out;
1485 }
1486
1487 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001488 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301489 rc = -EAGAIN;
1490 goto out;
1491 }
1492
1493 buffer_type = diag_register->buffer_type;
1494 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001495 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1496 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301497 return -EPERM;
1498 }
1499
1500 if (ioc->diag_buffer_status[buffer_type] &
1501 MPT3_DIAG_BUFFER_IS_REGISTERED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001502 ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1503 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301504 return -EINVAL;
1505 }
1506
1507 if (diag_register->requested_buffer_size % 4) {
Joe Perches919d8a32018-09-17 08:01:09 -07001508 ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
1509 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301510 return -EINVAL;
1511 }
1512
1513 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1514 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07001515 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301516 rc = -EAGAIN;
1517 goto out;
1518 }
1519
1520 rc = 0;
1521 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1522 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1523 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1524 ioc->ctl_cmds.smid = smid;
1525
1526 request_data = ioc->diag_buffer[buffer_type];
1527 request_data_sz = diag_register->requested_buffer_size;
1528 ioc->unique_id[buffer_type] = diag_register->unique_id;
1529 ioc->diag_buffer_status[buffer_type] = 0;
1530 memcpy(ioc->product_specific[buffer_type],
1531 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1532 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1533
1534 if (request_data) {
1535 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1536 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001537 dma_free_coherent(&ioc->pdev->dev,
1538 ioc->diag_buffer_sz[buffer_type],
1539 request_data, request_data_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301540 request_data = NULL;
1541 }
1542 }
1543
1544 if (request_data == NULL) {
1545 ioc->diag_buffer_sz[buffer_type] = 0;
1546 ioc->diag_buffer_dma[buffer_type] = 0;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001547 request_data = dma_alloc_coherent(&ioc->pdev->dev,
1548 request_data_sz, &request_data_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301549 if (request_data == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001550 ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
1551 __func__, request_data_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301552 mpt3sas_base_free_smid(ioc, smid);
1553 return -ENOMEM;
1554 }
1555 ioc->diag_buffer[buffer_type] = request_data;
1556 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1557 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1558 }
1559
1560 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1561 mpi_request->BufferType = diag_register->buffer_type;
1562 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1563 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1564 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1565 mpi_request->VF_ID = 0; /* TODO */
1566 mpi_request->VP_ID = 0;
1567
Joe Perches919d8a32018-09-17 08:01:09 -07001568 dctlprintk(ioc,
1569 ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1570 __func__, request_data,
1571 (unsigned long long)request_data_dma,
1572 le32_to_cpu(mpi_request->BufferLength)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301573
1574 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1575 mpi_request->ProductSpecific[i] =
1576 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1577
1578 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001579 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07001580 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301581 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1582
1583 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -04001584 issue_reset =
1585 mpt3sas_base_check_cmd_timeout(ioc,
1586 ioc->ctl_cmds.status, mpi_request,
1587 sizeof(Mpi2DiagBufferPostRequest_t)/4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301588 goto issue_host_reset;
1589 }
1590
1591 /* process the completed Reply Message Frame */
1592 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001593 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301594 rc = -EFAULT;
1595 goto out;
1596 }
1597
1598 mpi_reply = ioc->ctl_cmds.reply;
1599 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1600
1601 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1602 ioc->diag_buffer_status[buffer_type] |=
1603 MPT3_DIAG_BUFFER_IS_REGISTERED;
Joe Perches919d8a32018-09-17 08:01:09 -07001604 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301605 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07001606 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1607 __func__,
1608 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301609 rc = -EFAULT;
1610 }
1611
1612 issue_host_reset:
1613 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07001614 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301615
1616 out:
1617
1618 if (rc && request_data)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001619 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301620 request_data, request_data_dma);
1621
1622 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1623 return rc;
1624}
1625
1626/**
1627 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1628 * @ioc: per adapter object
1629 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1630 *
1631 * This is called when command line option diag_buffer_enable is enabled
1632 * at driver load time.
1633 */
1634void
1635mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1636{
1637 struct mpt3_diag_register diag_register;
1638
1639 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1640
1641 if (bits_to_register & 1) {
Joe Perches919d8a32018-09-17 08:01:09 -07001642 ioc_info(ioc, "registering trace buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301643 ioc->diag_trigger_master.MasterData =
1644 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1645 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1646 /* register for 2MB buffers */
1647 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1648 diag_register.unique_id = 0x7075900;
1649 _ctl_diag_register_2(ioc, &diag_register);
1650 }
1651
1652 if (bits_to_register & 2) {
Joe Perches919d8a32018-09-17 08:01:09 -07001653 ioc_info(ioc, "registering snapshot buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301654 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1655 /* register for 2MB buffers */
1656 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1657 diag_register.unique_id = 0x7075901;
1658 _ctl_diag_register_2(ioc, &diag_register);
1659 }
1660
1661 if (bits_to_register & 4) {
Joe Perches919d8a32018-09-17 08:01:09 -07001662 ioc_info(ioc, "registering extended buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301663 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1664 /* register for 2MB buffers */
1665 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1666 diag_register.unique_id = 0x7075901;
1667 _ctl_diag_register_2(ioc, &diag_register);
1668 }
1669}
1670
1671/**
1672 * _ctl_diag_register - application register with driver
1673 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001674 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301675 *
1676 * This will allow the driver to setup any required buffers that will be
1677 * needed by firmware to communicate with the driver.
1678 */
1679static long
1680_ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1681{
1682 struct mpt3_diag_register karg;
1683 long rc;
1684
1685 if (copy_from_user(&karg, arg, sizeof(karg))) {
1686 pr_err("failure at %s:%d/%s()!\n",
1687 __FILE__, __LINE__, __func__);
1688 return -EFAULT;
1689 }
1690
1691 rc = _ctl_diag_register_2(ioc, &karg);
1692 return rc;
1693}
1694
1695/**
1696 * _ctl_diag_unregister - application unregister with driver
1697 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001698 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301699 *
1700 * This will allow the driver to cleanup any memory allocated for diag
1701 * messages and to free up any resources.
1702 */
1703static long
1704_ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1705{
1706 struct mpt3_diag_unregister karg;
1707 void *request_data;
1708 dma_addr_t request_data_dma;
1709 u32 request_data_sz;
1710 u8 buffer_type;
1711
1712 if (copy_from_user(&karg, arg, sizeof(karg))) {
1713 pr_err("failure at %s:%d/%s()!\n",
1714 __FILE__, __LINE__, __func__);
1715 return -EFAULT;
1716 }
1717
Joe Perches919d8a32018-09-17 08:01:09 -07001718 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1719 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301720
1721 buffer_type = karg.unique_id & 0x000000ff;
1722 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001723 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1724 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301725 return -EPERM;
1726 }
1727
1728 if ((ioc->diag_buffer_status[buffer_type] &
1729 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001730 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1731 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301732 return -EINVAL;
1733 }
1734 if ((ioc->diag_buffer_status[buffer_type] &
1735 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001736 ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
1737 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301738 return -EINVAL;
1739 }
1740
1741 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07001742 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1743 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301744 return -EINVAL;
1745 }
1746
1747 request_data = ioc->diag_buffer[buffer_type];
1748 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07001749 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1750 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301751 return -ENOMEM;
1752 }
1753
1754 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1755 request_data_dma = ioc->diag_buffer_dma[buffer_type];
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001756 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
1757 request_data, request_data_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301758 ioc->diag_buffer[buffer_type] = NULL;
1759 ioc->diag_buffer_status[buffer_type] = 0;
1760 return 0;
1761}
1762
1763/**
1764 * _ctl_diag_query - query relevant info associated with diag buffers
1765 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001766 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301767 *
1768 * The application will send only buffer_type and unique_id. Driver will
1769 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1770 * 0x00, the driver will return info specified by Buffer Type.
1771 */
1772static long
1773_ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1774{
1775 struct mpt3_diag_query karg;
1776 void *request_data;
1777 int i;
1778 u8 buffer_type;
1779
1780 if (copy_from_user(&karg, arg, sizeof(karg))) {
1781 pr_err("failure at %s:%d/%s()!\n",
1782 __FILE__, __LINE__, __func__);
1783 return -EFAULT;
1784 }
1785
Joe Perches919d8a32018-09-17 08:01:09 -07001786 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1787 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301788
1789 karg.application_flags = 0;
1790 buffer_type = karg.buffer_type;
1791
1792 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001793 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1794 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301795 return -EPERM;
1796 }
1797
1798 if ((ioc->diag_buffer_status[buffer_type] &
1799 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001800 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1801 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301802 return -EINVAL;
1803 }
1804
1805 if (karg.unique_id & 0xffffff00) {
1806 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07001807 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1808 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301809 return -EINVAL;
1810 }
1811 }
1812
1813 request_data = ioc->diag_buffer[buffer_type];
1814 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07001815 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
1816 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301817 return -ENOMEM;
1818 }
1819
1820 if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
1821 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1822 MPT3_APP_FLAGS_BUFFER_VALID);
1823 else
1824 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1825 MPT3_APP_FLAGS_BUFFER_VALID |
1826 MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
1827
1828 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1829 karg.product_specific[i] =
1830 ioc->product_specific[buffer_type][i];
1831
1832 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1833 karg.driver_added_buffer_size = 0;
1834 karg.unique_id = ioc->unique_id[buffer_type];
1835 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1836
1837 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
Joe Perches919d8a32018-09-17 08:01:09 -07001838 ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
1839 __func__, arg);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301840 return -EFAULT;
1841 }
1842 return 0;
1843}
1844
1845/**
1846 * mpt3sas_send_diag_release - Diag Release Message
1847 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001848 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1849 * @issue_reset: specifies whether host reset is required.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301850 *
1851 */
1852int
1853mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
1854 u8 *issue_reset)
1855{
1856 Mpi2DiagReleaseRequest_t *mpi_request;
1857 Mpi2DiagReleaseReply_t *mpi_reply;
1858 u16 smid;
1859 u16 ioc_status;
1860 u32 ioc_state;
1861 int rc;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301862
Joe Perches919d8a32018-09-17 08:01:09 -07001863 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1864 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301865
1866 rc = 0;
1867 *issue_reset = 0;
1868
1869 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1870 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1871 if (ioc->diag_buffer_status[buffer_type] &
1872 MPT3_DIAG_BUFFER_IS_REGISTERED)
1873 ioc->diag_buffer_status[buffer_type] |=
1874 MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07001875 dctlprintk(ioc,
1876 ioc_info(ioc, "%s: skipping due to FAULT state\n",
1877 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301878 rc = -EAGAIN;
1879 goto out;
1880 }
1881
1882 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001883 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301884 rc = -EAGAIN;
1885 goto out;
1886 }
1887
1888 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1889 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07001890 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301891 rc = -EAGAIN;
1892 goto out;
1893 }
1894
1895 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1896 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1897 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1898 ioc->ctl_cmds.smid = smid;
1899
1900 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1901 mpi_request->BufferType = buffer_type;
1902 mpi_request->VF_ID = 0; /* TODO */
1903 mpi_request->VP_ID = 0;
1904
1905 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001906 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07001907 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301908 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1909
1910 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -04001911 *issue_reset = mpt3sas_base_check_cmd_timeout(ioc,
1912 ioc->ctl_cmds.status, mpi_request,
1913 sizeof(Mpi2DiagReleaseRequest_t)/4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301914 rc = -EFAULT;
1915 goto out;
1916 }
1917
1918 /* process the completed Reply Message Frame */
1919 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001920 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301921 rc = -EFAULT;
1922 goto out;
1923 }
1924
1925 mpi_reply = ioc->ctl_cmds.reply;
1926 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1927
1928 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1929 ioc->diag_buffer_status[buffer_type] |=
1930 MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07001931 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301932 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07001933 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1934 __func__,
1935 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301936 rc = -EFAULT;
1937 }
1938
1939 out:
1940 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1941 return rc;
1942}
1943
1944/**
1945 * _ctl_diag_release - request to send Diag Release Message to firmware
Bart Van Assche4beb4862018-06-15 14:42:01 -07001946 * @ioc: ?
1947 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301948 *
1949 * This allows ownership of the specified buffer to returned to the driver,
1950 * allowing an application to read the buffer without fear that firmware is
Masahiro Yamada9a284e52017-02-27 14:29:48 -08001951 * overwriting information in the buffer.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301952 */
1953static long
1954_ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1955{
1956 struct mpt3_diag_release karg;
1957 void *request_data;
1958 int rc;
1959 u8 buffer_type;
1960 u8 issue_reset = 0;
1961
1962 if (copy_from_user(&karg, arg, sizeof(karg))) {
1963 pr_err("failure at %s:%d/%s()!\n",
1964 __FILE__, __LINE__, __func__);
1965 return -EFAULT;
1966 }
1967
Joe Perches919d8a32018-09-17 08:01:09 -07001968 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1969 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301970
1971 buffer_type = karg.unique_id & 0x000000ff;
1972 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001973 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1974 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301975 return -EPERM;
1976 }
1977
1978 if ((ioc->diag_buffer_status[buffer_type] &
1979 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001980 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1981 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301982 return -EINVAL;
1983 }
1984
1985 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07001986 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1987 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301988 return -EINVAL;
1989 }
1990
1991 if (ioc->diag_buffer_status[buffer_type] &
1992 MPT3_DIAG_BUFFER_IS_RELEASED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001993 ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
1994 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301995 return 0;
1996 }
1997
1998 request_data = ioc->diag_buffer[buffer_type];
1999
2000 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002001 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2002 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302003 return -ENOMEM;
2004 }
2005
2006 /* buffers were released by due to host reset */
2007 if ((ioc->diag_buffer_status[buffer_type] &
2008 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2009 ioc->diag_buffer_status[buffer_type] |=
2010 MPT3_DIAG_BUFFER_IS_RELEASED;
2011 ioc->diag_buffer_status[buffer_type] &=
2012 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
Joe Perches919d8a32018-09-17 08:01:09 -07002013 ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
2014 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302015 return 0;
2016 }
2017
2018 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2019
2020 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07002021 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302022
2023 return rc;
2024}
2025
2026/**
2027 * _ctl_diag_read_buffer - request for copy of the diag buffer
2028 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002029 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302030 */
2031static long
2032_ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2033{
2034 struct mpt3_diag_read_buffer karg;
2035 struct mpt3_diag_read_buffer __user *uarg = arg;
2036 void *request_data, *diag_data;
2037 Mpi2DiagBufferPostRequest_t *mpi_request;
2038 Mpi2DiagBufferPostReply_t *mpi_reply;
2039 int rc, i;
2040 u8 buffer_type;
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002041 unsigned long request_size, copy_size;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302042 u16 smid;
2043 u16 ioc_status;
2044 u8 issue_reset = 0;
2045
2046 if (copy_from_user(&karg, arg, sizeof(karg))) {
2047 pr_err("failure at %s:%d/%s()!\n",
2048 __FILE__, __LINE__, __func__);
2049 return -EFAULT;
2050 }
2051
Joe Perches919d8a32018-09-17 08:01:09 -07002052 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2053 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302054
2055 buffer_type = karg.unique_id & 0x000000ff;
2056 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002057 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2058 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302059 return -EPERM;
2060 }
2061
2062 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002063 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2064 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302065 return -EINVAL;
2066 }
2067
2068 request_data = ioc->diag_buffer[buffer_type];
2069 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002070 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2071 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302072 return -ENOMEM;
2073 }
2074
2075 request_size = ioc->diag_buffer_sz[buffer_type];
2076
2077 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002078 ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
2079 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302080 return -EINVAL;
2081 }
2082
2083 if (karg.starting_offset > request_size)
2084 return -EINVAL;
2085
2086 diag_data = (void *)(request_data + karg.starting_offset);
Joe Perches919d8a32018-09-17 08:01:09 -07002087 dctlprintk(ioc,
2088 ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2089 __func__, diag_data, karg.starting_offset,
2090 karg.bytes_to_read));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302091
2092 /* Truncate data on requests that are too large */
2093 if ((diag_data + karg.bytes_to_read < diag_data) ||
2094 (diag_data + karg.bytes_to_read > request_data + request_size))
2095 copy_size = request_size - karg.starting_offset;
2096 else
2097 copy_size = karg.bytes_to_read;
2098
2099 if (copy_to_user((void __user *)uarg->diagnostic_data,
2100 diag_data, copy_size)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002101 ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2102 __func__, diag_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302103 return -EFAULT;
2104 }
2105
2106 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2107 return 0;
2108
Joe Perches919d8a32018-09-17 08:01:09 -07002109 dctlprintk(ioc,
2110 ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
2111 __func__, buffer_type));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302112 if ((ioc->diag_buffer_status[buffer_type] &
2113 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002114 dctlprintk(ioc,
2115 ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
2116 __func__, buffer_type));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302117 return 0;
2118 }
2119 /* Get a free request frame and save the message context.
2120 */
2121
2122 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07002123 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302124 rc = -EAGAIN;
2125 goto out;
2126 }
2127
2128 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2129 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07002130 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302131 rc = -EAGAIN;
2132 goto out;
2133 }
2134
2135 rc = 0;
2136 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2137 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2138 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2139 ioc->ctl_cmds.smid = smid;
2140
2141 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2142 mpi_request->BufferType = buffer_type;
2143 mpi_request->BufferLength =
2144 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2145 mpi_request->BufferAddress =
2146 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2147 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2148 mpi_request->ProductSpecific[i] =
2149 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2150 mpi_request->VF_ID = 0; /* TODO */
2151 mpi_request->VP_ID = 0;
2152
2153 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04002154 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002155 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302156 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2157
2158 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -04002159 issue_reset =
2160 mpt3sas_base_check_cmd_timeout(ioc,
2161 ioc->ctl_cmds.status, mpi_request,
2162 sizeof(Mpi2DiagBufferPostRequest_t)/4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302163 goto issue_host_reset;
2164 }
2165
2166 /* process the completed Reply Message Frame */
2167 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002168 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302169 rc = -EFAULT;
2170 goto out;
2171 }
2172
2173 mpi_reply = ioc->ctl_cmds.reply;
2174 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2175
2176 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2177 ioc->diag_buffer_status[buffer_type] |=
2178 MPT3_DIAG_BUFFER_IS_REGISTERED;
Joe Perches919d8a32018-09-17 08:01:09 -07002179 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302180 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07002181 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2182 __func__, ioc_status,
2183 le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302184 rc = -EFAULT;
2185 }
2186
2187 issue_host_reset:
2188 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07002189 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302190
2191 out:
2192
2193 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2194 return rc;
2195}
2196
2197
2198
2199#ifdef CONFIG_COMPAT
2200/**
2201 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2202 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002203 * @cmd: ioctl opcode
2204 * @arg: (struct mpt3_ioctl_command32)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302205 *
2206 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2207 */
2208static long
2209_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2210 void __user *arg)
2211{
2212 struct mpt3_ioctl_command32 karg32;
2213 struct mpt3_ioctl_command32 __user *uarg;
2214 struct mpt3_ioctl_command karg;
2215
2216 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2217 return -EINVAL;
2218
2219 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2220
2221 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2222 pr_err("failure at %s:%d/%s()!\n",
2223 __FILE__, __LINE__, __func__);
2224 return -EFAULT;
2225 }
2226
2227 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2228 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2229 karg.hdr.port_number = karg32.hdr.port_number;
2230 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2231 karg.timeout = karg32.timeout;
2232 karg.max_reply_bytes = karg32.max_reply_bytes;
2233 karg.data_in_size = karg32.data_in_size;
2234 karg.data_out_size = karg32.data_out_size;
2235 karg.max_sense_bytes = karg32.max_sense_bytes;
2236 karg.data_sge_offset = karg32.data_sge_offset;
2237 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2238 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2239 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2240 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2241 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2242}
2243#endif
2244
2245/**
2246 * _ctl_ioctl_main - main ioctl entry point
Bart Van Assche4beb4862018-06-15 14:42:01 -07002247 * @file: (struct file)
2248 * @cmd: ioctl opcode
2249 * @arg: user space data buffer
2250 * @compat: handles 32 bit applications in 64bit os
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302251 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302252 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302253 */
2254static long
2255_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302256 u8 compat, u16 mpi_version)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302257{
2258 struct MPT3SAS_ADAPTER *ioc;
2259 struct mpt3_ioctl_header ioctl_header;
2260 enum block_state state;
2261 long ret = -EINVAL;
2262
2263 /* get IOCTL header */
2264 if (copy_from_user(&ioctl_header, (char __user *)arg,
2265 sizeof(struct mpt3_ioctl_header))) {
2266 pr_err("failure at %s:%d/%s()!\n",
2267 __FILE__, __LINE__, __func__);
2268 return -EFAULT;
2269 }
2270
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302271 if (_ctl_verify_adapter(ioctl_header.ioc_number,
2272 &ioc, mpi_version) == -1 || !ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302273 return -ENODEV;
2274
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302275 /* pci_access_mutex lock acquired by ioctl path */
2276 mutex_lock(&ioc->pci_access_mutex);
2277
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302278 if (ioc->shost_recovery || ioc->pci_error_recovery ||
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302279 ioc->is_driver_loading || ioc->remove_host) {
2280 ret = -EAGAIN;
2281 goto out_unlock_pciaccess;
2282 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302283
2284 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2285 if (state == NON_BLOCKING) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302286 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2287 ret = -EAGAIN;
2288 goto out_unlock_pciaccess;
2289 }
2290 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2291 ret = -ERESTARTSYS;
2292 goto out_unlock_pciaccess;
2293 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302294
2295
2296 switch (cmd) {
2297 case MPT3IOCINFO:
2298 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2299 ret = _ctl_getiocinfo(ioc, arg);
2300 break;
2301#ifdef CONFIG_COMPAT
2302 case MPT3COMMAND32:
2303#endif
2304 case MPT3COMMAND:
2305 {
2306 struct mpt3_ioctl_command __user *uarg;
2307 struct mpt3_ioctl_command karg;
2308
2309#ifdef CONFIG_COMPAT
2310 if (compat) {
2311 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2312 break;
2313 }
2314#endif
2315 if (copy_from_user(&karg, arg, sizeof(karg))) {
2316 pr_err("failure at %s:%d/%s()!\n",
2317 __FILE__, __LINE__, __func__);
2318 ret = -EFAULT;
2319 break;
2320 }
2321
Gen Zhangf9e3ebe2019-05-30 09:10:30 +08002322 if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2323 ret = -EINVAL;
2324 break;
2325 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302326 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2327 uarg = arg;
2328 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2329 }
2330 break;
2331 }
2332 case MPT3EVENTQUERY:
2333 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2334 ret = _ctl_eventquery(ioc, arg);
2335 break;
2336 case MPT3EVENTENABLE:
2337 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2338 ret = _ctl_eventenable(ioc, arg);
2339 break;
2340 case MPT3EVENTREPORT:
2341 ret = _ctl_eventreport(ioc, arg);
2342 break;
2343 case MPT3HARDRESET:
2344 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2345 ret = _ctl_do_reset(ioc, arg);
2346 break;
2347 case MPT3BTDHMAPPING:
2348 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2349 ret = _ctl_btdh_mapping(ioc, arg);
2350 break;
2351 case MPT3DIAGREGISTER:
2352 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2353 ret = _ctl_diag_register(ioc, arg);
2354 break;
2355 case MPT3DIAGUNREGISTER:
2356 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2357 ret = _ctl_diag_unregister(ioc, arg);
2358 break;
2359 case MPT3DIAGQUERY:
2360 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2361 ret = _ctl_diag_query(ioc, arg);
2362 break;
2363 case MPT3DIAGRELEASE:
2364 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2365 ret = _ctl_diag_release(ioc, arg);
2366 break;
2367 case MPT3DIAGREADBUFFER:
2368 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2369 ret = _ctl_diag_read_buffer(ioc, arg);
2370 break;
2371 default:
Joe Perches919d8a32018-09-17 08:01:09 -07002372 dctlprintk(ioc,
2373 ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2374 cmd));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302375 break;
2376 }
2377
2378 mutex_unlock(&ioc->ctl_cmds.mutex);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302379out_unlock_pciaccess:
2380 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302381 return ret;
2382}
2383
2384/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302385 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002386 * @file: (struct file)
2387 * @cmd: ioctl opcode
2388 * @arg: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302389 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002390static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302391_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302392{
2393 long ret;
2394
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302395 /* pass MPI25_VERSION | MPI26_VERSION value,
2396 * to indicate that this ioctl cmd
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302397 * came from mpt3ctl ioctl device.
2398 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302399 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2400 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302401 return ret;
2402}
2403
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302404/**
2405 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002406 * @file: (struct file)
2407 * @cmd: ioctl opcode
2408 * @arg: ?
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302409 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002410static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302411_ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2412{
2413 long ret;
2414
2415 /* pass MPI2_VERSION value, to indicate that this ioctl cmd
2416 * came from mpt2ctl ioctl device.
2417 */
2418 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2419 return ret;
2420}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302421#ifdef CONFIG_COMPAT
2422/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302423 *_ ctl_ioctl_compat - main ioctl entry point (compat)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002424 * @file: ?
2425 * @cmd: ?
2426 * @arg: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302427 *
2428 * This routine handles 32 bit applications in 64bit os.
2429 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002430static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302431_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302432{
2433 long ret;
2434
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302435 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2436 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302437 return ret;
2438}
2439
2440/**
2441 *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002442 * @file: ?
2443 * @cmd: ?
2444 * @arg: ?
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302445 *
2446 * This routine handles 32 bit applications in 64bit os.
2447 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002448static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302449_ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2450{
2451 long ret;
2452
2453 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302454 return ret;
2455}
2456#endif
2457
2458/* scsi host attributes */
2459/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002460 * version_fw_show - firmware version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002461 * @cdev: pointer to embedded class device
2462 * @attr: ?
2463 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302464 *
2465 * A sysfs 'read-only' shost attribute.
2466 */
2467static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002468version_fw_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302469 char *buf)
2470{
2471 struct Scsi_Host *shost = class_to_shost(cdev);
2472 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2473
2474 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2475 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2476 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2477 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2478 ioc->facts.FWVersion.Word & 0x000000FF);
2479}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002480static DEVICE_ATTR_RO(version_fw);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302481
2482/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002483 * version_bios_show - bios version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002484 * @cdev: pointer to embedded class device
2485 * @attr: ?
2486 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302487 *
2488 * A sysfs 'read-only' shost attribute.
2489 */
2490static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002491version_bios_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302492 char *buf)
2493{
2494 struct Scsi_Host *shost = class_to_shost(cdev);
2495 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2496
2497 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2498
2499 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2500 (version & 0xFF000000) >> 24,
2501 (version & 0x00FF0000) >> 16,
2502 (version & 0x0000FF00) >> 8,
2503 version & 0x000000FF);
2504}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002505static DEVICE_ATTR_RO(version_bios);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302506
2507/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002508 * version_mpi_show - MPI (message passing interface) version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002509 * @cdev: pointer to embedded class device
2510 * @attr: ?
2511 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302512 *
2513 * A sysfs 'read-only' shost attribute.
2514 */
2515static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002516version_mpi_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302517 char *buf)
2518{
2519 struct Scsi_Host *shost = class_to_shost(cdev);
2520 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2521
2522 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2523 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2524}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002525static DEVICE_ATTR_RO(version_mpi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302526
2527/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002528 * version_product_show - product name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002529 * @cdev: pointer to embedded class device
2530 * @attr: ?
2531 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302532 *
2533 * A sysfs 'read-only' shost attribute.
2534 */
2535static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002536version_product_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302537 char *buf)
2538{
2539 struct Scsi_Host *shost = class_to_shost(cdev);
2540 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2541
2542 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2543}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002544static DEVICE_ATTR_RO(version_product);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302545
2546/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002547 * version_nvdata_persistent_show - ndvata persistent version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002548 * @cdev: pointer to embedded class device
2549 * @attr: ?
2550 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302551 *
2552 * A sysfs 'read-only' shost attribute.
2553 */
2554static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002555version_nvdata_persistent_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302556 struct device_attribute *attr, char *buf)
2557{
2558 struct Scsi_Host *shost = class_to_shost(cdev);
2559 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2560
2561 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2562 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2563}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002564static DEVICE_ATTR_RO(version_nvdata_persistent);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302565
2566/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002567 * version_nvdata_default_show - nvdata default version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002568 * @cdev: pointer to embedded class device
2569 * @attr: ?
2570 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302571 *
2572 * A sysfs 'read-only' shost attribute.
2573 */
2574static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002575version_nvdata_default_show(struct device *cdev, struct device_attribute
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302576 *attr, char *buf)
2577{
2578 struct Scsi_Host *shost = class_to_shost(cdev);
2579 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2580
2581 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2582 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2583}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002584static DEVICE_ATTR_RO(version_nvdata_default);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302585
2586/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002587 * board_name_show - board name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002588 * @cdev: pointer to embedded class device
2589 * @attr: ?
2590 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302591 *
2592 * A sysfs 'read-only' shost attribute.
2593 */
2594static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002595board_name_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302596 char *buf)
2597{
2598 struct Scsi_Host *shost = class_to_shost(cdev);
2599 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2600
2601 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2602}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002603static DEVICE_ATTR_RO(board_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302604
2605/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002606 * board_assembly_show - board assembly name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002607 * @cdev: pointer to embedded class device
2608 * @attr: ?
2609 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302610 *
2611 * A sysfs 'read-only' shost attribute.
2612 */
2613static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002614board_assembly_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302615 char *buf)
2616{
2617 struct Scsi_Host *shost = class_to_shost(cdev);
2618 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2619
2620 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2621}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002622static DEVICE_ATTR_RO(board_assembly);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302623
2624/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002625 * board_tracer_show - board tracer number
Bart Van Assche4beb4862018-06-15 14:42:01 -07002626 * @cdev: pointer to embedded class device
2627 * @attr: ?
2628 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302629 *
2630 * A sysfs 'read-only' shost attribute.
2631 */
2632static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002633board_tracer_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302634 char *buf)
2635{
2636 struct Scsi_Host *shost = class_to_shost(cdev);
2637 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2638
2639 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2640}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002641static DEVICE_ATTR_RO(board_tracer);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302642
2643/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002644 * io_delay_show - io missing delay
Bart Van Assche4beb4862018-06-15 14:42:01 -07002645 * @cdev: pointer to embedded class device
2646 * @attr: ?
2647 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302648 *
2649 * This is for firmware implemention for deboucing device
2650 * removal events.
2651 *
2652 * A sysfs 'read-only' shost attribute.
2653 */
2654static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002655io_delay_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302656 char *buf)
2657{
2658 struct Scsi_Host *shost = class_to_shost(cdev);
2659 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2660
2661 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2662}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002663static DEVICE_ATTR_RO(io_delay);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302664
2665/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002666 * device_delay_show - device missing delay
Bart Van Assche4beb4862018-06-15 14:42:01 -07002667 * @cdev: pointer to embedded class device
2668 * @attr: ?
2669 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302670 *
2671 * This is for firmware implemention for deboucing device
2672 * removal events.
2673 *
2674 * A sysfs 'read-only' shost attribute.
2675 */
2676static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002677device_delay_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302678 char *buf)
2679{
2680 struct Scsi_Host *shost = class_to_shost(cdev);
2681 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2682
2683 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2684}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002685static DEVICE_ATTR_RO(device_delay);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302686
2687/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002688 * fw_queue_depth_show - global credits
Bart Van Assche4beb4862018-06-15 14:42:01 -07002689 * @cdev: pointer to embedded class device
2690 * @attr: ?
2691 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302692 *
2693 * This is firmware queue depth limit
2694 *
2695 * A sysfs 'read-only' shost attribute.
2696 */
2697static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002698fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302699 char *buf)
2700{
2701 struct Scsi_Host *shost = class_to_shost(cdev);
2702 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2703
2704 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2705}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002706static DEVICE_ATTR_RO(fw_queue_depth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302707
2708/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002709 * sas_address_show - sas address
Bart Van Assche4beb4862018-06-15 14:42:01 -07002710 * @cdev: pointer to embedded class device
2711 * @attr: ?
2712 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302713 *
2714 * This is the controller sas address
2715 *
2716 * A sysfs 'read-only' shost attribute.
2717 */
2718static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002719host_sas_address_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302720 char *buf)
2721
2722{
2723 struct Scsi_Host *shost = class_to_shost(cdev);
2724 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2725
2726 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2727 (unsigned long long)ioc->sas_hba.sas_address);
2728}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002729static DEVICE_ATTR_RO(host_sas_address);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302730
2731/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002732 * logging_level_show - logging level
Bart Van Assche4beb4862018-06-15 14:42:01 -07002733 * @cdev: pointer to embedded class device
2734 * @attr: ?
2735 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302736 *
2737 * A sysfs 'read/write' shost attribute.
2738 */
2739static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002740logging_level_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302741 char *buf)
2742{
2743 struct Scsi_Host *shost = class_to_shost(cdev);
2744 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2745
2746 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2747}
2748static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002749logging_level_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302750 const char *buf, size_t count)
2751{
2752 struct Scsi_Host *shost = class_to_shost(cdev);
2753 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2754 int val = 0;
2755
2756 if (sscanf(buf, "%x", &val) != 1)
2757 return -EINVAL;
2758
2759 ioc->logging_level = val;
Joe Perches919d8a32018-09-17 08:01:09 -07002760 ioc_info(ioc, "logging_level=%08xh\n",
2761 ioc->logging_level);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302762 return strlen(buf);
2763}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002764static DEVICE_ATTR_RW(logging_level);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302765
2766/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002767 * fwfault_debug_show - show/store fwfault_debug
Bart Van Assche4beb4862018-06-15 14:42:01 -07002768 * @cdev: pointer to embedded class device
2769 * @attr: ?
2770 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302771 *
2772 * mpt3sas_fwfault_debug is command line option
2773 * A sysfs 'read/write' shost attribute.
2774 */
2775static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002776fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302777 char *buf)
2778{
2779 struct Scsi_Host *shost = class_to_shost(cdev);
2780 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2781
2782 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2783}
2784static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002785fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302786 const char *buf, size_t count)
2787{
2788 struct Scsi_Host *shost = class_to_shost(cdev);
2789 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2790 int val = 0;
2791
2792 if (sscanf(buf, "%d", &val) != 1)
2793 return -EINVAL;
2794
2795 ioc->fwfault_debug = val;
Joe Perches919d8a32018-09-17 08:01:09 -07002796 ioc_info(ioc, "fwfault_debug=%d\n",
2797 ioc->fwfault_debug);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302798 return strlen(buf);
2799}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002800static DEVICE_ATTR_RW(fwfault_debug);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302801
2802/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002803 * ioc_reset_count_show - ioc reset count
Bart Van Assche4beb4862018-06-15 14:42:01 -07002804 * @cdev: pointer to embedded class device
2805 * @attr: ?
2806 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302807 *
2808 * This is firmware queue depth limit
2809 *
2810 * A sysfs 'read-only' shost attribute.
2811 */
2812static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002813ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302814 char *buf)
2815{
2816 struct Scsi_Host *shost = class_to_shost(cdev);
2817 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2818
2819 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
2820}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002821static DEVICE_ATTR_RO(ioc_reset_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302822
2823/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002824 * reply_queue_count_show - number of reply queues
Bart Van Assche4beb4862018-06-15 14:42:01 -07002825 * @cdev: pointer to embedded class device
2826 * @attr: ?
2827 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302828 *
2829 * This is number of reply queues
2830 *
2831 * A sysfs 'read-only' shost attribute.
2832 */
2833static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002834reply_queue_count_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302835 struct device_attribute *attr, char *buf)
2836{
2837 u8 reply_queue_count;
2838 struct Scsi_Host *shost = class_to_shost(cdev);
2839 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2840
2841 if ((ioc->facts.IOCCapabilities &
2842 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2843 reply_queue_count = ioc->reply_queue_count;
2844 else
2845 reply_queue_count = 1;
2846
2847 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2848}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002849static DEVICE_ATTR_RO(reply_queue_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302850
Sreekanth Reddy42263092015-11-11 17:30:29 +05302851/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002852 * BRM_status_show - Backup Rail Monitor Status
Bart Van Assche4beb4862018-06-15 14:42:01 -07002853 * @cdev: pointer to embedded class device
2854 * @attr: ?
2855 * @buf: the buffer returned
Sreekanth Reddy42263092015-11-11 17:30:29 +05302856 *
2857 * This is number of reply queues
2858 *
2859 * A sysfs 'read-only' shost attribute.
2860 */
2861static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002862BRM_status_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddy42263092015-11-11 17:30:29 +05302863 char *buf)
2864{
2865 struct Scsi_Host *shost = class_to_shost(cdev);
2866 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2867 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2868 Mpi2ConfigReply_t mpi_reply;
2869 u16 backup_rail_monitor_status = 0;
2870 u16 ioc_status;
2871 int sz;
2872 ssize_t rc = 0;
2873
2874 if (!ioc->is_warpdrive) {
Joe Perches919d8a32018-09-17 08:01:09 -07002875 ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
2876 __func__);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302877 goto out;
2878 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302879 /* pci_access_mutex lock acquired by sysfs show path */
2880 mutex_lock(&ioc->pci_access_mutex);
2881 if (ioc->pci_error_recovery || ioc->remove_host) {
2882 mutex_unlock(&ioc->pci_access_mutex);
2883 return 0;
2884 }
Sreekanth Reddy42263092015-11-11 17:30:29 +05302885
2886 /* allocate upto GPIOVal 36 entries */
2887 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2888 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2889 if (!io_unit_pg3) {
Joe Perches919d8a32018-09-17 08:01:09 -07002890 ioc_err(ioc, "%s: failed allocating memory for iounit_pg3: (%d) bytes\n",
2891 __func__, sz);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302892 goto out;
2893 }
2894
2895 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2896 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002897 ioc_err(ioc, "%s: failed reading iounit_pg3\n",
2898 __func__);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302899 goto out;
2900 }
2901
2902 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2903 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
Joe Perches919d8a32018-09-17 08:01:09 -07002904 ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
2905 __func__, ioc_status);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302906 goto out;
2907 }
2908
2909 if (io_unit_pg3->GPIOCount < 25) {
Joe Perches919d8a32018-09-17 08:01:09 -07002910 ioc_err(ioc, "%s: iounit_pg3->GPIOCount less than 25 entries, detected (%d) entries\n",
2911 __func__, io_unit_pg3->GPIOCount);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302912 goto out;
2913 }
2914
2915 /* BRM status is in bit zero of GPIOVal[24] */
2916 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2917 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2918
2919 out:
2920 kfree(io_unit_pg3);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302921 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302922 return rc;
2923}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002924static DEVICE_ATTR_RO(BRM_status);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302925
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302926struct DIAG_BUFFER_START {
2927 __le32 Size;
2928 __le32 DiagVersion;
2929 u8 BufferType;
2930 u8 Reserved[3];
2931 __le32 Reserved1;
2932 __le32 Reserved2;
2933 __le32 Reserved3;
2934};
2935
2936/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002937 * host_trace_buffer_size_show - host buffer size (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002938 * @cdev: pointer to embedded class device
2939 * @attr: ?
2940 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302941 *
2942 * A sysfs 'read-only' shost attribute.
2943 */
2944static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002945host_trace_buffer_size_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302946 struct device_attribute *attr, char *buf)
2947{
2948 struct Scsi_Host *shost = class_to_shost(cdev);
2949 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2950 u32 size = 0;
2951 struct DIAG_BUFFER_START *request_data;
2952
2953 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002954 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
2955 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302956 return 0;
2957 }
2958
2959 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2960 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002961 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
2962 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302963 return 0;
2964 }
2965
2966 request_data = (struct DIAG_BUFFER_START *)
2967 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2968 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2969 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
2970 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
2971 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2972 size = le32_to_cpu(request_data->Size);
2973
2974 ioc->ring_buffer_sz = size;
2975 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2976}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002977static DEVICE_ATTR_RO(host_trace_buffer_size);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302978
2979/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002980 * host_trace_buffer_show - firmware ring buffer (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002981 * @cdev: pointer to embedded class device
2982 * @attr: ?
2983 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302984 *
2985 * A sysfs 'read/write' shost attribute.
2986 *
2987 * You will only be able to read 4k bytes of ring buffer at a time.
2988 * In order to read beyond 4k bytes, you will have to write out the
2989 * offset to the same attribute, it will move the pointer.
2990 */
2991static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002992host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302993 char *buf)
2994{
2995 struct Scsi_Host *shost = class_to_shost(cdev);
2996 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2997 void *request_data;
2998 u32 size;
2999
3000 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
Joe Perches919d8a32018-09-17 08:01:09 -07003001 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3002 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303003 return 0;
3004 }
3005
3006 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3007 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003008 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3009 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303010 return 0;
3011 }
3012
3013 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3014 return 0;
3015
3016 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3017 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3018 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3019 memcpy(buf, request_data, size);
3020 return size;
3021}
3022
3023static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003024host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303025 const char *buf, size_t count)
3026{
3027 struct Scsi_Host *shost = class_to_shost(cdev);
3028 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3029 int val = 0;
3030
3031 if (sscanf(buf, "%d", &val) != 1)
3032 return -EINVAL;
3033
3034 ioc->ring_buffer_offset = val;
3035 return strlen(buf);
3036}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003037static DEVICE_ATTR_RW(host_trace_buffer);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303038
3039
3040/*****************************************/
3041
3042/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003043 * host_trace_buffer_enable_show - firmware ring buffer (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07003044 * @cdev: pointer to embedded class device
3045 * @attr: ?
3046 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303047 *
3048 * A sysfs 'read/write' shost attribute.
3049 *
3050 * This is a mechnism to post/release host_trace_buffers
3051 */
3052static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003053host_trace_buffer_enable_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303054 struct device_attribute *attr, char *buf)
3055{
3056 struct Scsi_Host *shost = class_to_shost(cdev);
3057 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3058
3059 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3060 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3061 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3062 return snprintf(buf, PAGE_SIZE, "off\n");
3063 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3064 MPT3_DIAG_BUFFER_IS_RELEASED))
3065 return snprintf(buf, PAGE_SIZE, "release\n");
3066 else
3067 return snprintf(buf, PAGE_SIZE, "post\n");
3068}
3069
3070static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003071host_trace_buffer_enable_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303072 struct device_attribute *attr, const char *buf, size_t count)
3073{
3074 struct Scsi_Host *shost = class_to_shost(cdev);
3075 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3076 char str[10] = "";
3077 struct mpt3_diag_register diag_register;
3078 u8 issue_reset = 0;
3079
3080 /* don't allow post/release occurr while recovery is active */
3081 if (ioc->shost_recovery || ioc->remove_host ||
3082 ioc->pci_error_recovery || ioc->is_driver_loading)
3083 return -EBUSY;
3084
3085 if (sscanf(buf, "%9s", str) != 1)
3086 return -EINVAL;
3087
3088 if (!strcmp(str, "post")) {
3089 /* exit out if host buffers are already posted */
3090 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3091 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3092 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3093 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3094 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3095 goto out;
3096 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
Joe Perches919d8a32018-09-17 08:01:09 -07003097 ioc_info(ioc, "posting host trace buffers\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303098 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3099 diag_register.requested_buffer_size = (1024 * 1024);
3100 diag_register.unique_id = 0x7075900;
3101 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3102 _ctl_diag_register_2(ioc, &diag_register);
3103 } else if (!strcmp(str, "release")) {
3104 /* exit out if host buffers are already released */
3105 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3106 goto out;
3107 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3108 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3109 goto out;
3110 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3111 MPT3_DIAG_BUFFER_IS_RELEASED))
3112 goto out;
Joe Perches919d8a32018-09-17 08:01:09 -07003113 ioc_info(ioc, "releasing host trace buffer\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303114 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3115 &issue_reset);
3116 }
3117
3118 out:
3119 return strlen(buf);
3120}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003121static DEVICE_ATTR_RW(host_trace_buffer_enable);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303122
3123/*********** diagnostic trigger suppport *********************************/
3124
3125/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003126 * diag_trigger_master_show - show the diag_trigger_master attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003127 * @cdev: pointer to embedded class device
3128 * @attr: ?
3129 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303130 *
3131 * A sysfs 'read/write' shost attribute.
3132 */
3133static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003134diag_trigger_master_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303135 struct device_attribute *attr, char *buf)
3136
3137{
3138 struct Scsi_Host *shost = class_to_shost(cdev);
3139 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3140 unsigned long flags;
3141 ssize_t rc;
3142
3143 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3144 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3145 memcpy(buf, &ioc->diag_trigger_master, rc);
3146 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3147 return rc;
3148}
3149
3150/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003151 * diag_trigger_master_store - store the diag_trigger_master attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003152 * @cdev: pointer to embedded class device
3153 * @attr: ?
3154 * @buf: the buffer returned
3155 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303156 *
3157 * A sysfs 'read/write' shost attribute.
3158 */
3159static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003160diag_trigger_master_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303161 struct device_attribute *attr, const char *buf, size_t count)
3162
3163{
3164 struct Scsi_Host *shost = class_to_shost(cdev);
3165 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3166 unsigned long flags;
3167 ssize_t rc;
3168
3169 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3170 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3171 memset(&ioc->diag_trigger_master, 0,
3172 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3173 memcpy(&ioc->diag_trigger_master, buf, rc);
3174 ioc->diag_trigger_master.MasterData |=
3175 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3176 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3177 return rc;
3178}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003179static DEVICE_ATTR_RW(diag_trigger_master);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303180
3181
3182/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003183 * diag_trigger_event_show - show the diag_trigger_event attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003184 * @cdev: pointer to embedded class device
3185 * @attr: ?
3186 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303187 *
3188 * A sysfs 'read/write' shost attribute.
3189 */
3190static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003191diag_trigger_event_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303192 struct device_attribute *attr, char *buf)
3193{
3194 struct Scsi_Host *shost = class_to_shost(cdev);
3195 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3196 unsigned long flags;
3197 ssize_t rc;
3198
3199 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3200 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3201 memcpy(buf, &ioc->diag_trigger_event, rc);
3202 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3203 return rc;
3204}
3205
3206/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003207 * diag_trigger_event_store - store the diag_trigger_event attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003208 * @cdev: pointer to embedded class device
3209 * @attr: ?
3210 * @buf: the buffer returned
3211 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303212 *
3213 * A sysfs 'read/write' shost attribute.
3214 */
3215static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003216diag_trigger_event_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303217 struct device_attribute *attr, const char *buf, size_t count)
3218
3219{
3220 struct Scsi_Host *shost = class_to_shost(cdev);
3221 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3222 unsigned long flags;
3223 ssize_t sz;
3224
3225 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3226 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3227 memset(&ioc->diag_trigger_event, 0,
3228 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3229 memcpy(&ioc->diag_trigger_event, buf, sz);
3230 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3231 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3232 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3233 return sz;
3234}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003235static DEVICE_ATTR_RW(diag_trigger_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303236
3237
3238/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003239 * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003240 * @cdev: pointer to embedded class device
3241 * @attr: ?
3242 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303243 *
3244 * A sysfs 'read/write' shost attribute.
3245 */
3246static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003247diag_trigger_scsi_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303248 struct device_attribute *attr, char *buf)
3249{
3250 struct Scsi_Host *shost = class_to_shost(cdev);
3251 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3252 unsigned long flags;
3253 ssize_t rc;
3254
3255 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3256 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3257 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3258 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3259 return rc;
3260}
3261
3262/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003263 * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003264 * @cdev: pointer to embedded class device
3265 * @attr: ?
3266 * @buf: the buffer returned
3267 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303268 *
3269 * A sysfs 'read/write' shost attribute.
3270 */
3271static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003272diag_trigger_scsi_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303273 struct device_attribute *attr, const char *buf, size_t count)
3274{
3275 struct Scsi_Host *shost = class_to_shost(cdev);
3276 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3277 unsigned long flags;
3278 ssize_t sz;
3279
3280 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
Dan Carpenter1de540a2019-07-26 09:52:05 +03003281 sz = min(sizeof(ioc->diag_trigger_scsi), count);
3282 memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303283 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3284 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3285 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3286 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3287 return sz;
3288}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003289static DEVICE_ATTR_RW(diag_trigger_scsi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303290
3291
3292/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003293 * diag_trigger_scsi_show - show the diag_trigger_mpi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003294 * @cdev: pointer to embedded class device
3295 * @attr: ?
3296 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303297 *
3298 * A sysfs 'read/write' shost attribute.
3299 */
3300static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003301diag_trigger_mpi_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303302 struct device_attribute *attr, char *buf)
3303{
3304 struct Scsi_Host *shost = class_to_shost(cdev);
3305 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3306 unsigned long flags;
3307 ssize_t rc;
3308
3309 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3310 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3311 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3312 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3313 return rc;
3314}
3315
3316/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003317 * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003318 * @cdev: pointer to embedded class device
3319 * @attr: ?
3320 * @buf: the buffer returned
3321 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303322 *
3323 * A sysfs 'read/write' shost attribute.
3324 */
3325static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003326diag_trigger_mpi_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303327 struct device_attribute *attr, const char *buf, size_t count)
3328{
3329 struct Scsi_Host *shost = class_to_shost(cdev);
3330 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3331 unsigned long flags;
3332 ssize_t sz;
3333
3334 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3335 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3336 memset(&ioc->diag_trigger_mpi, 0,
Dan Carpenter66331e82012-12-07 13:56:22 +03003337 sizeof(ioc->diag_trigger_mpi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303338 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3339 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3340 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3341 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3342 return sz;
3343}
3344
Tomas Henzlc9df1442019-06-14 16:41:44 +02003345static DEVICE_ATTR_RW(diag_trigger_mpi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303346
3347/*********** diagnostic trigger suppport *** END ****************************/
3348
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303349/*****************************************/
3350
3351struct device_attribute *mpt3sas_host_attrs[] = {
3352 &dev_attr_version_fw,
3353 &dev_attr_version_bios,
3354 &dev_attr_version_mpi,
3355 &dev_attr_version_product,
3356 &dev_attr_version_nvdata_persistent,
3357 &dev_attr_version_nvdata_default,
3358 &dev_attr_board_name,
3359 &dev_attr_board_assembly,
3360 &dev_attr_board_tracer,
3361 &dev_attr_io_delay,
3362 &dev_attr_device_delay,
3363 &dev_attr_logging_level,
3364 &dev_attr_fwfault_debug,
3365 &dev_attr_fw_queue_depth,
3366 &dev_attr_host_sas_address,
3367 &dev_attr_ioc_reset_count,
3368 &dev_attr_host_trace_buffer_size,
3369 &dev_attr_host_trace_buffer,
3370 &dev_attr_host_trace_buffer_enable,
3371 &dev_attr_reply_queue_count,
3372 &dev_attr_diag_trigger_master,
3373 &dev_attr_diag_trigger_event,
3374 &dev_attr_diag_trigger_scsi,
3375 &dev_attr_diag_trigger_mpi,
Sreekanth Reddy42263092015-11-11 17:30:29 +05303376 &dev_attr_BRM_status,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303377 NULL,
3378};
3379
3380/* device attributes */
3381
3382/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003383 * sas_address_show - sas address
Bart Van Assche4beb4862018-06-15 14:42:01 -07003384 * @dev: pointer to embedded class device
3385 * @attr: ?
3386 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303387 *
3388 * This is the sas address for the target
3389 *
3390 * A sysfs 'read-only' shost attribute.
3391 */
3392static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003393sas_address_show(struct device *dev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303394 char *buf)
3395{
3396 struct scsi_device *sdev = to_scsi_device(dev);
3397 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3398
3399 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3400 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3401}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003402static DEVICE_ATTR_RO(sas_address);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303403
3404/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003405 * sas_device_handle_show - device handle
Bart Van Assche4beb4862018-06-15 14:42:01 -07003406 * @dev: pointer to embedded class device
3407 * @attr: ?
3408 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303409 *
3410 * This is the firmware assigned device handle
3411 *
3412 * A sysfs 'read-only' shost attribute.
3413 */
3414static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003415sas_device_handle_show(struct device *dev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303416 char *buf)
3417{
3418 struct scsi_device *sdev = to_scsi_device(dev);
3419 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3420
3421 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3422 sas_device_priv_data->sas_target->handle);
3423}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003424static DEVICE_ATTR_RO(sas_device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303425
Adam Manzanares307d9072016-12-12 16:31:40 -08003426/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003427 * sas_ncq_io_prio_show - send prioritized io commands to device
Bart Van Assche4beb4862018-06-15 14:42:01 -07003428 * @dev: pointer to embedded device
3429 * @attr: ?
3430 * @buf: the buffer returned
Adam Manzanares307d9072016-12-12 16:31:40 -08003431 *
3432 * A sysfs 'read/write' sdev attribute, only works with SATA
3433 */
3434static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003435sas_ncq_prio_enable_show(struct device *dev,
Adam Manzanares307d9072016-12-12 16:31:40 -08003436 struct device_attribute *attr, char *buf)
3437{
3438 struct scsi_device *sdev = to_scsi_device(dev);
3439 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3440
3441 return snprintf(buf, PAGE_SIZE, "%d\n",
3442 sas_device_priv_data->ncq_prio_enable);
3443}
3444
3445static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003446sas_ncq_prio_enable_store(struct device *dev,
Adam Manzanares307d9072016-12-12 16:31:40 -08003447 struct device_attribute *attr,
3448 const char *buf, size_t count)
3449{
3450 struct scsi_device *sdev = to_scsi_device(dev);
3451 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3452 bool ncq_prio_enable = 0;
3453
3454 if (kstrtobool(buf, &ncq_prio_enable))
3455 return -EINVAL;
3456
3457 if (!scsih_ncq_prio_supp(sdev))
3458 return -EINVAL;
3459
3460 sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
3461 return strlen(buf);
3462}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003463static DEVICE_ATTR_RW(sas_ncq_prio_enable);
Adam Manzanares307d9072016-12-12 16:31:40 -08003464
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303465struct device_attribute *mpt3sas_dev_attrs[] = {
3466 &dev_attr_sas_address,
3467 &dev_attr_sas_device_handle,
Adam Manzanares307d9072016-12-12 16:31:40 -08003468 &dev_attr_sas_ncq_prio_enable,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303469 NULL,
3470};
3471
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303472/* file operations table for mpt3ctl device */
3473static const struct file_operations ctl_fops = {
3474 .owner = THIS_MODULE,
3475 .unlocked_ioctl = _ctl_ioctl,
3476 .poll = _ctl_poll,
3477 .fasync = _ctl_fasync,
3478#ifdef CONFIG_COMPAT
3479 .compat_ioctl = _ctl_ioctl_compat,
3480#endif
3481};
3482
3483/* file operations table for mpt2ctl device */
3484static const struct file_operations ctl_gen2_fops = {
3485 .owner = THIS_MODULE,
3486 .unlocked_ioctl = _ctl_mpt2_ioctl,
3487 .poll = _ctl_poll,
3488 .fasync = _ctl_fasync,
3489#ifdef CONFIG_COMPAT
3490 .compat_ioctl = _ctl_mpt2_ioctl_compat,
3491#endif
3492};
3493
3494static struct miscdevice ctl_dev = {
3495 .minor = MPT3SAS_MINOR,
3496 .name = MPT3SAS_DEV_NAME,
3497 .fops = &ctl_fops,
3498};
3499
3500static struct miscdevice gen2_ctl_dev = {
3501 .minor = MPT2SAS_MINOR,
3502 .name = MPT2SAS_DEV_NAME,
3503 .fops = &ctl_gen2_fops,
3504};
3505
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303506/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303507 * mpt3sas_ctl_init - main entry point for ctl.
Bart Van Assche4beb4862018-06-15 14:42:01 -07003508 * @hbas_to_enumerate: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303509 */
3510void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303511mpt3sas_ctl_init(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303512{
3513 async_queue = NULL;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303514
3515 /* Don't register mpt3ctl ioctl device if
3516 * hbas_to_enumarate is one.
3517 */
3518 if (hbas_to_enumerate != 1)
3519 if (misc_register(&ctl_dev) < 0)
3520 pr_err("%s can't register misc device [minor=%d]\n",
3521 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3522
3523 /* Don't register mpt3ctl ioctl device if
3524 * hbas_to_enumarate is two.
3525 */
3526 if (hbas_to_enumerate != 2)
3527 if (misc_register(&gen2_ctl_dev) < 0)
3528 pr_err("%s can't register misc device [minor=%d]\n",
3529 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3530
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303531 init_waitqueue_head(&ctl_poll_wait);
3532}
3533
3534/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303535 * mpt3sas_ctl_exit - exit point for ctl
Bart Van Assche4beb4862018-06-15 14:42:01 -07003536 * @hbas_to_enumerate: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303537 */
3538void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303539mpt3sas_ctl_exit(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303540{
3541 struct MPT3SAS_ADAPTER *ioc;
3542 int i;
3543
3544 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3545
3546 /* free memory associated to diag buffers */
3547 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3548 if (!ioc->diag_buffer[i])
3549 continue;
3550 if (!(ioc->diag_buffer_status[i] &
3551 MPT3_DIAG_BUFFER_IS_REGISTERED))
3552 continue;
3553 if ((ioc->diag_buffer_status[i] &
3554 MPT3_DIAG_BUFFER_IS_RELEASED))
3555 continue;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02003556 dma_free_coherent(&ioc->pdev->dev,
3557 ioc->diag_buffer_sz[i],
3558 ioc->diag_buffer[i],
3559 ioc->diag_buffer_dma[i]);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303560 ioc->diag_buffer[i] = NULL;
3561 ioc->diag_buffer_status[i] = 0;
3562 }
3563
3564 kfree(ioc->event_log);
3565 }
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303566 if (hbas_to_enumerate != 1)
3567 misc_deregister(&ctl_dev);
3568 if (hbas_to_enumerate != 2)
3569 misc_deregister(&gen2_ctl_dev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303570}