blob: e289f18fc7643723f7771f6736648f5b078cf1bc [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: [email protected])
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;
Sreekanth Reddy5b061982019-12-26 06:13:30 -0500183 case MPI2_FUNCTION_TOOLBOX:
184 desc = "toolbox";
185 break;
186 case MPI2_FUNCTION_NVME_ENCAPSULATED:
187 desc = "nvme_encapsulated";
188 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530189 }
190
191 if (!desc)
192 return;
193
Joe Perches919d8a32018-09-17 08:01:09 -0700194 ioc_info(ioc, "%s: %s, smid(%d)\n", calling_function_name, desc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530195
196 if (!mpi_reply)
197 return;
198
199 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
Joe Perches919d8a32018-09-17 08:01:09 -0700200 ioc_info(ioc, "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
201 le16_to_cpu(mpi_reply->IOCStatus),
202 le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530203
204 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
205 mpi_request->Function ==
206 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
207 Mpi2SCSIIOReply_t *scsi_reply =
208 (Mpi2SCSIIOReply_t *)mpi_reply;
209 struct _sas_device *sas_device = NULL;
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530210 struct _pcie_device *pcie_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530211
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530212 sas_device = mpt3sas_get_sdev_by_handle(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530213 le16_to_cpu(scsi_reply->DevHandle));
214 if (sas_device) {
Joe Perches919d8a32018-09-17 08:01:09 -0700215 ioc_warn(ioc, "\tsas_address(0x%016llx), phy(%d)\n",
216 (u64)sas_device->sas_address,
217 sas_device->phy);
218 ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
219 (u64)sas_device->enclosure_logical_id,
220 sas_device->slot);
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530221 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530222 }
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530223 if (!sas_device) {
224 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
225 le16_to_cpu(scsi_reply->DevHandle));
226 if (pcie_device) {
Joe Perches919d8a32018-09-17 08:01:09 -0700227 ioc_warn(ioc, "\tWWID(0x%016llx), port(%d)\n",
228 (unsigned long long)pcie_device->wwid,
229 pcie_device->port_num);
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530230 if (pcie_device->enclosure_handle != 0)
Joe Perches919d8a32018-09-17 08:01:09 -0700231 ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
232 (u64)pcie_device->enclosure_logical_id,
233 pcie_device->slot);
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530234 pcie_device_put(pcie_device);
235 }
236 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530237 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
Joe Perches919d8a32018-09-17 08:01:09 -0700238 ioc_info(ioc, "\tscsi_state(0x%02x), scsi_status(0x%02x)\n",
239 scsi_reply->SCSIState,
240 scsi_reply->SCSIStatus);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530241 }
242}
243
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530244/**
245 * mpt3sas_ctl_done - ctl module completion routine
246 * @ioc: per adapter object
247 * @smid: system request message index
248 * @msix_index: MSIX table index supplied by the OS
249 * @reply: reply message frame(lower 32bit addr)
250 * Context: none.
251 *
252 * The callback handler when using ioc->ctl_cb_idx.
253 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700254 * Return: 1 meaning mf should be freed from _base_interrupt
255 * 0 means the mf is freed from this function.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530256 */
257u8
258mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
259 u32 reply)
260{
261 MPI2DefaultReply_t *mpi_reply;
262 Mpi2SCSIIOReply_t *scsiio_reply;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530263 Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530264 const void *sense_data;
265 u32 sz;
266
267 if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
268 return 1;
269 if (ioc->ctl_cmds.smid != smid)
270 return 1;
271 ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
272 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
273 if (mpi_reply) {
274 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
275 ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
276 /* get sense data */
277 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
278 mpi_reply->Function ==
279 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
280 scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
281 if (scsiio_reply->SCSIState &
282 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
283 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
284 le32_to_cpu(scsiio_reply->SenseCount));
285 sense_data = mpt3sas_base_get_sense_buffer(ioc,
286 smid);
287 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
288 }
289 }
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530290 /*
291 * Get Error Response data for NVMe device. The ctl_cmds.sense
292 * buffer is used to store the Error Response data.
293 */
294 if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
295 nvme_error_reply =
296 (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
297 sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400298 le16_to_cpu(nvme_error_reply->ErrorResponseCount));
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530299 sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
300 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
301 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530302 }
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +0530303
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530304 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530305 ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
306 complete(&ioc->ctl_cmds.done);
307 return 1;
308}
309
310/**
311 * _ctl_check_event_type - determines when an event needs logging
312 * @ioc: per adapter object
313 * @event: firmware event
314 *
315 * The bitmask in ioc->event_type[] indicates which events should be
316 * be saved in the driver event_log. This bitmask is set by application.
317 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700318 * Return: 1 when event should be captured, or zero means no match.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530319 */
320static int
321_ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
322{
323 u16 i;
324 u32 desired_event;
325
326 if (event >= 128 || !event || !ioc->event_log)
327 return 0;
328
329 desired_event = (1 << (event % 32));
330 if (!desired_event)
331 desired_event = 1;
332 i = event / 32;
333 return desired_event & ioc->event_type[i];
334}
335
336/**
337 * mpt3sas_ctl_add_to_event_log - add event
338 * @ioc: per adapter object
339 * @mpi_reply: reply message frame
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530340 */
341void
342mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
343 Mpi2EventNotificationReply_t *mpi_reply)
344{
345 struct MPT3_IOCTL_EVENTS *event_log;
346 u16 event;
347 int i;
348 u32 sz, event_data_sz;
349 u8 send_aen = 0;
350
351 if (!ioc->event_log)
352 return;
353
354 event = le16_to_cpu(mpi_reply->Event);
355
356 if (_ctl_check_event_type(ioc, event)) {
357
358 /* insert entry into circular event_log */
359 i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
360 event_log = ioc->event_log;
361 event_log[i].event = event;
362 event_log[i].context = ioc->event_context++;
363
364 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
365 sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
366 memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
367 memcpy(event_log[i].data, mpi_reply->EventData, sz);
368 send_aen = 1;
369 }
370
371 /* This aen_event_read_flag flag is set until the
372 * application has read the event log.
373 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
374 */
375 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
376 (send_aen && !ioc->aen_event_read_flag)) {
377 ioc->aen_event_read_flag = 1;
378 wake_up_interruptible(&ctl_poll_wait);
379 if (async_queue)
380 kill_fasync(&async_queue, SIGIO, POLL_IN);
381 }
382}
383
384/**
385 * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
386 * @ioc: per adapter object
387 * @msix_index: MSIX table index supplied by the OS
388 * @reply: reply message frame(lower 32bit addr)
389 * Context: interrupt.
390 *
391 * This function merely adds a new work task into ioc->firmware_event_thread.
392 * The tasks are worked from _firmware_event_work in user context.
393 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700394 * Return: 1 meaning mf should be freed from _base_interrupt
395 * 0 means the mf is freed from this function.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530396 */
397u8
398mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
399 u32 reply)
400{
401 Mpi2EventNotificationReply_t *mpi_reply;
402
403 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
Suganath prabu Subramani869817f2016-01-28 12:07:00 +0530404 if (mpi_reply)
405 mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530406 return 1;
407}
408
409/**
410 * _ctl_verify_adapter - validates ioc_number passed from application
Bart Van Assche4beb4862018-06-15 14:42:01 -0700411 * @ioc_number: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530412 * @iocpp: The ioc pointer is returned in this.
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530413 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530414 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530415 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700416 * Return: (-1) means error, else ioc_number.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530417 */
418static int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530419_ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
420 int mpi_version)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530421{
422 struct MPT3SAS_ADAPTER *ioc;
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530423 int version = 0;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530424 /* global ioc lock to protect controller on list operations */
425 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530426 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
427 if (ioc->id != ioc_number)
428 continue;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530429 /* Check whether this ioctl command is from right
430 * ioctl device or not, if not continue the search.
431 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530432 version = ioc->hba_mpi_version_belonged;
433 /* MPI25_VERSION and MPI26_VERSION uses same ioctl
434 * device.
435 */
436 if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
437 if ((version == MPI25_VERSION) ||
438 (version == MPI26_VERSION))
439 goto out;
440 else
441 continue;
442 } else {
443 if (version != mpi_version)
444 continue;
445 }
446out:
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530447 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530448 *iocpp = ioc;
449 return ioc_number;
450 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530451 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530452 *iocpp = NULL;
453 return -1;
454}
455
456/**
Lee Jones782a1ab2021-03-12 09:47:17 +0000457 * mpt3sas_ctl_pre_reset_handler - reset callback handler (for ctl)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530458 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530459 *
460 * The handler for doing any required cleanup or initialization.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530461 */
Bart Van Asschec7a35702018-06-15 14:42:00 -0700462void mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530463{
464 int i;
465 u8 issue_reset;
466
Joe Perches919d8a32018-09-17 08:01:09 -0700467 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
Bart Van Asschec7a35702018-06-15 14:42:00 -0700468 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
469 if (!(ioc->diag_buffer_status[i] &
470 MPT3_DIAG_BUFFER_IS_REGISTERED))
471 continue;
472 if ((ioc->diag_buffer_status[i] &
473 MPT3_DIAG_BUFFER_IS_RELEASED))
474 continue;
Sreekanth Reddy4bc50dc12019-09-13 09:04:39 -0400475
476 /*
477 * add a log message to indicate the release
478 */
479 ioc_info(ioc,
480 "%s: Releasing the trace buffer due to adapter reset.",
481 __func__);
Suganath Prabu S688c1a02021-02-04 09:07:23 +0530482 ioc->htb_rel.buffer_rel_condition =
483 MPT3_DIAG_BUFFER_REL_TRIGGER;
Bart Van Asschec7a35702018-06-15 14:42:00 -0700484 mpt3sas_send_diag_release(ioc, i, &issue_reset);
485 }
486}
487
488/**
Lee Jones782a1ab2021-03-12 09:47:17 +0000489 * mpt3sas_ctl_clear_outstanding_ioctls - clears outstanding ioctl cmd.
Bart Van Asschec7a35702018-06-15 14:42:00 -0700490 * @ioc: per adapter object
491 *
492 * The handler for doing any required cleanup or initialization.
493 */
Sreekanth Reddy36c6c7f2019-12-26 06:13:26 -0500494void mpt3sas_ctl_clear_outstanding_ioctls(struct MPT3SAS_ADAPTER *ioc)
Bart Van Asschec7a35702018-06-15 14:42:00 -0700495{
Sreekanth Reddy36c6c7f2019-12-26 06:13:26 -0500496 dtmprintk(ioc,
497 ioc_info(ioc, "%s: clear outstanding ioctl cmd\n", __func__));
Bart Van Asschec7a35702018-06-15 14:42:00 -0700498 if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
499 ioc->ctl_cmds.status |= MPT3_CMD_RESET;
500 mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
501 complete(&ioc->ctl_cmds.done);
502 }
503}
504
505/**
Lee Jones782a1ab2021-03-12 09:47:17 +0000506 * mpt3sas_ctl_reset_done_handler - reset callback handler (for ctl)
Bart Van Asschec7a35702018-06-15 14:42:00 -0700507 * @ioc: per adapter object
508 *
509 * The handler for doing any required cleanup or initialization.
510 */
511void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
512{
513 int i;
514
Joe Perches919d8a32018-09-17 08:01:09 -0700515 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530516
Bart Van Asschec7a35702018-06-15 14:42:00 -0700517 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
518 if (!(ioc->diag_buffer_status[i] &
519 MPT3_DIAG_BUFFER_IS_REGISTERED))
520 continue;
521 if ((ioc->diag_buffer_status[i] &
522 MPT3_DIAG_BUFFER_IS_RELEASED))
523 continue;
524 ioc->diag_buffer_status[i] |=
525 MPT3_DIAG_BUFFER_IS_DIAG_RESET;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530526 }
527}
528
529/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530530 * _ctl_fasync -
Bart Van Assche4beb4862018-06-15 14:42:01 -0700531 * @fd: ?
532 * @filep: ?
533 * @mode: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530534 *
535 * Called when application request fasyn callback handler.
536 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -0700537static int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530538_ctl_fasync(int fd, struct file *filep, int mode)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530539{
540 return fasync_helper(fd, filep, mode, &async_queue);
541}
542
543/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530544 * _ctl_poll -
Bart Van Assche4beb4862018-06-15 14:42:01 -0700545 * @filep: ?
546 * @wait: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530547 *
548 */
Al Viroafc9a422017-07-03 06:39:46 -0400549static __poll_t
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530550_ctl_poll(struct file *filep, poll_table *wait)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530551{
552 struct MPT3SAS_ADAPTER *ioc;
553
554 poll_wait(filep, &ctl_poll_wait, wait);
555
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530556 /* global ioc lock to protect controller on list operations */
557 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530558 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530559 if (ioc->aen_event_read_flag) {
560 spin_unlock(&gioc_lock);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800561 return EPOLLIN | EPOLLRDNORM;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530562 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530563 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530564 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530565 return 0;
566}
567
568/**
569 * _ctl_set_task_mid - assign an active smid to tm request
570 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -0700571 * @karg: (struct mpt3_ioctl_command)
572 * @tm_request: pointer to mf from user space
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530573 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700574 * Return: 0 when an smid if found, else fail.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530575 * during failure, the reply frame is filled.
576 */
577static int
578_ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
579 Mpi2SCSITaskManagementRequest_t *tm_request)
580{
Damien Le Moaldceaef92022-03-08 08:48:50 +0900581 bool found = false;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800582 u16 smid;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530583 u16 handle;
584 struct scsi_cmnd *scmd;
585 struct MPT3SAS_DEVICE *priv_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530586 Mpi2SCSITaskManagementReply_t *tm_reply;
587 u32 sz;
588 u32 lun;
589 char *desc = NULL;
590
591 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
592 desc = "abort_task";
593 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
594 desc = "query_task";
595 else
596 return 0;
597
598 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
599
600 handle = le16_to_cpu(tm_request->DevHandle);
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800601 for (smid = ioc->scsiio_depth; smid && !found; smid--) {
602 struct scsiio_tracker *st;
Damien Le Moaldceaef92022-03-08 08:48:50 +0900603 __le16 task_mid;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800604
605 scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
606 if (!scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530607 continue;
608 if (lun != scmd->device->lun)
609 continue;
610 priv_data = scmd->device->hostdata;
611 if (priv_data->sas_target == NULL)
612 continue;
613 if (priv_data->sas_target->handle != handle)
614 continue;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800615 st = scsi_cmd_priv(scmd);
Minwoo Im8f55c302019-07-28 03:53:37 +0900616
617 /*
618 * If the given TaskMID from the user space is zero, then the
619 * first outstanding smid will be picked up. Otherwise,
620 * targeted smid will be the one.
621 */
Damien Le Moaldceaef92022-03-08 08:48:50 +0900622 task_mid = cpu_to_le16(st->smid);
623 if (!tm_request->TaskMID)
624 tm_request->TaskMID = task_mid;
625 found = tm_request->TaskMID == task_mid;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530626 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530627
628 if (!found) {
Joe Perches919d8a32018-09-17 08:01:09 -0700629 dctlprintk(ioc,
630 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), no active mid!!\n",
631 desc, le16_to_cpu(tm_request->DevHandle),
632 lun));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530633 tm_reply = ioc->ctl_cmds.reply;
634 tm_reply->DevHandle = tm_request->DevHandle;
635 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
636 tm_reply->TaskType = tm_request->TaskType;
637 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
638 tm_reply->VP_ID = tm_request->VP_ID;
639 tm_reply->VF_ID = tm_request->VF_ID;
640 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
641 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
642 sz))
643 pr_err("failure at %s:%d/%s()!\n", __FILE__,
644 __LINE__, __func__);
645 return 1;
646 }
647
Joe Perches919d8a32018-09-17 08:01:09 -0700648 dctlprintk(ioc,
649 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), task_mid(%d)\n",
650 desc, le16_to_cpu(tm_request->DevHandle), lun,
651 le16_to_cpu(tm_request->TaskMID)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530652 return 0;
653}
654
655/**
656 * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
657 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -0700658 * @karg: (struct mpt3_ioctl_command)
659 * @mf: pointer to mf in user space
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530660 */
661static long
662_ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
663 void __user *mf)
664{
665 MPI2RequestHeader_t *mpi_request = NULL, *request;
666 MPI2DefaultReply_t *mpi_reply;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530667 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -0400668 struct _pcie_device *pcie_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530669 u16 smid;
Suganath Prabu S42f68702020-11-25 15:18:38 +0530670 unsigned long timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530671 u8 issue_reset;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530672 u32 sz, sz_arg;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530673 void *psge;
674 void *data_out = NULL;
675 dma_addr_t data_out_dma = 0;
676 size_t data_out_sz = 0;
677 void *data_in = NULL;
678 dma_addr_t data_in_dma = 0;
679 size_t data_in_sz = 0;
680 long ret;
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530681 u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530682
683 issue_reset = 0;
684
685 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -0700686 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530687 ret = -EAGAIN;
688 goto out;
689 }
690
Suganath Prabuf4305742018-10-31 18:53:33 +0530691 ret = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
692 if (ret)
693 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530694
695 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
696 if (!mpi_request) {
Joe Perches919d8a32018-09-17 08:01:09 -0700697 ioc_err(ioc, "%s: failed obtaining a memory for mpi_request\n",
698 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530699 ret = -ENOMEM;
700 goto out;
701 }
702
703 /* Check for overflow and wraparound */
704 if (karg.data_sge_offset * 4 > ioc->request_sz ||
705 karg.data_sge_offset > (UINT_MAX / 4)) {
706 ret = -EINVAL;
707 goto out;
708 }
709
710 /* copy in request message frame from user */
711 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
712 pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
713 __func__);
714 ret = -EFAULT;
715 goto out;
716 }
717
718 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
719 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
720 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -0700721 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530722 ret = -EAGAIN;
723 goto out;
724 }
725 } else {
Hannes Reineckeb0cd285e2018-01-04 04:57:07 -0800726 /* Use first reserved smid for passthrough ioctls */
727 smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530728 }
729
730 ret = 0;
731 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
732 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
733 request = mpt3sas_base_get_msg_frame(ioc, smid);
Suganath Prabue224e032019-08-03 09:59:47 -0400734 memset(request, 0, ioc->request_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530735 memcpy(request, mpi_request, karg.data_sge_offset*4);
736 ioc->ctl_cmds.smid = smid;
737 data_out_sz = karg.data_out_size;
738 data_in_sz = karg.data_in_size;
739
740 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530741 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
742 mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530743 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
744 mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530745
746 device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
747 if (!device_handle || (device_handle >
748 ioc->facts.MaxDevHandle)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530749 ret = -EINVAL;
750 mpt3sas_base_free_smid(ioc, smid);
751 goto out;
752 }
753 }
754
755 /* obtain dma-able memory for data transfer */
756 if (data_out_sz) /* WRITE */ {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +0200757 data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz,
758 &data_out_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530759 if (!data_out) {
760 pr_err("failure at %s:%d/%s()!\n", __FILE__,
761 __LINE__, __func__);
762 ret = -ENOMEM;
763 mpt3sas_base_free_smid(ioc, smid);
764 goto out;
765 }
766 if (copy_from_user(data_out, karg.data_out_buf_ptr,
767 data_out_sz)) {
768 pr_err("failure at %s:%d/%s()!\n", __FILE__,
769 __LINE__, __func__);
770 ret = -EFAULT;
771 mpt3sas_base_free_smid(ioc, smid);
772 goto out;
773 }
774 }
775
776 if (data_in_sz) /* READ */ {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +0200777 data_in = dma_alloc_coherent(&ioc->pdev->dev, data_in_sz,
778 &data_in_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530779 if (!data_in) {
780 pr_err("failure at %s:%d/%s()!\n", __FILE__,
781 __LINE__, __func__);
782 ret = -ENOMEM;
783 mpt3sas_base_free_smid(ioc, smid);
784 goto out;
785 }
786 }
787
788 psge = (void *)request + (karg.data_sge_offset*4);
789
790 /* send command to firmware */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530791 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530792
793 init_completion(&ioc->ctl_cmds.done);
794 switch (mpi_request->Function) {
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530795 case MPI2_FUNCTION_NVME_ENCAPSULATED:
796 {
797 nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
Sreekanth Reddy77fd4f22019-09-13 09:04:48 -0400798 if (!ioc->pcie_sg_lookup) {
799 dtmprintk(ioc, ioc_info(ioc,
800 "HBA doesn't support NVMe. Rejecting NVMe Encapsulated request.\n"
801 ));
802
803 if (ioc->logging_level & MPT_DEBUG_TM)
804 _debug_dump_mf(nvme_encap_request,
805 ioc->request_sz/4);
806 mpt3sas_base_free_smid(ioc, smid);
807 ret = -EINVAL;
808 goto out;
809 }
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530810 /*
811 * Get the Physical Address of the sense buffer.
812 * Use Error Response buffer address field to hold the sense
813 * buffer address.
814 * Clear the internal sense buffer, which will potentially hold
815 * the Completion Queue Entry on return, or 0 if no Entry.
816 * Build the PRPs and set direction bits.
817 * Send the request.
818 */
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400819 nvme_encap_request->ErrorResponseBaseAddress =
820 cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530821 nvme_encap_request->ErrorResponseBaseAddress |=
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400822 cpu_to_le64(le32_to_cpu(
823 mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530824 nvme_encap_request->ErrorResponseAllocationLength =
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400825 cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530826 memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
827 ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
828 data_out_dma, data_out_sz, data_in_dma, data_in_sz);
829 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700830 dtmprintk(ioc,
831 ioc_info(ioc, "handle(0x%04x): ioctl failed due to device removal in progress\n",
832 device_handle));
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530833 mpt3sas_base_free_smid(ioc, smid);
834 ret = -EINVAL;
835 goto out;
836 }
Suganath Prabu S40114bd2018-02-14 02:16:37 -0800837 mpt3sas_base_put_smid_nvme_encap(ioc, smid);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530838 break;
839 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530840 case MPI2_FUNCTION_SCSI_IO_REQUEST:
841 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
842 {
843 Mpi2SCSIIORequest_t *scsiio_request =
844 (Mpi2SCSIIORequest_t *)request;
845 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
846 scsiio_request->SenseBufferLowAddress =
847 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
848 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530849 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700850 dtmprintk(ioc,
851 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
852 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530853 mpt3sas_base_free_smid(ioc, smid);
854 ret = -EINVAL;
855 goto out;
856 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530857 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
858 data_in_dma, data_in_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530859 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +0530860 ioc->put_smid_scsi_io(ioc, smid, device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530861 else
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400862 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530863 break;
864 }
865 case MPI2_FUNCTION_SCSI_TASK_MGMT:
866 {
867 Mpi2SCSITaskManagementRequest_t *tm_request =
868 (Mpi2SCSITaskManagementRequest_t *)request;
869
Joe Perches919d8a32018-09-17 08:01:09 -0700870 dtmprintk(ioc,
871 ioc_info(ioc, "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
872 le16_to_cpu(tm_request->DevHandle),
873 tm_request->TaskType));
Chaitra P B459325c2017-01-23 15:26:08 +0530874 ioc->got_task_abort_from_ioctl = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530875 if (tm_request->TaskType ==
876 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
877 tm_request->TaskType ==
878 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
879 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
880 mpt3sas_base_free_smid(ioc, smid);
Chaitra P B459325c2017-01-23 15:26:08 +0530881 ioc->got_task_abort_from_ioctl = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530882 goto out;
883 }
884 }
Chaitra P B459325c2017-01-23 15:26:08 +0530885 ioc->got_task_abort_from_ioctl = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530886
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530887 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700888 dtmprintk(ioc,
889 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
890 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530891 mpt3sas_base_free_smid(ioc, smid);
892 ret = -EINVAL;
893 goto out;
894 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530895 mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
896 tm_request->DevHandle));
897 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
898 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400899 ioc->put_smid_hi_priority(ioc, smid, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530900 break;
901 }
902 case MPI2_FUNCTION_SMP_PASSTHROUGH:
903 {
904 Mpi2SmpPassthroughRequest_t *smp_request =
905 (Mpi2SmpPassthroughRequest_t *)mpi_request;
906 u8 *data;
907
Sreekanth Reddy324c1222020-10-27 18:38:46 +0530908 if (!ioc->multipath_on_hba) {
909 /* ioc determines which port to use */
910 smp_request->PhysicalPort = 0xFF;
911 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530912 if (smp_request->PassthroughFlags &
913 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
914 data = (u8 *)&smp_request->SGL;
915 else {
916 if (unlikely(data_out == NULL)) {
917 pr_err("failure at %s:%d/%s()!\n",
918 __FILE__, __LINE__, __func__);
919 mpt3sas_base_free_smid(ioc, smid);
920 ret = -EINVAL;
921 goto out;
922 }
923 data = data_out;
924 }
925
926 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
927 ioc->ioc_link_reset_in_progress = 1;
928 ioc->ignore_loginfos = 1;
929 }
930 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
931 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400932 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530933 break;
934 }
935 case MPI2_FUNCTION_SATA_PASSTHROUGH:
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530936 {
937 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700938 dtmprintk(ioc,
939 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
940 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530941 mpt3sas_base_free_smid(ioc, smid);
942 ret = -EINVAL;
943 goto out;
944 }
945 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
946 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400947 ioc->put_smid_default(ioc, smid);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530948 break;
949 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530950 case MPI2_FUNCTION_FW_DOWNLOAD:
Bradley Grovef45fadd2022-08-05 13:46:09 -0400951 {
952 if (ioc->pdev->vendor == MPI2_MFGPAGE_VENDORID_ATTO) {
953 ioc_info(ioc, "Firmware download not supported for ATTO HBA.\n");
954 ret = -EPERM;
955 break;
956 }
957 fallthrough;
958 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530959 case MPI2_FUNCTION_FW_UPLOAD:
960 {
961 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
962 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400963 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530964 break;
965 }
966 case MPI2_FUNCTION_TOOLBOX:
967 {
968 Mpi2ToolboxCleanRequest_t *toolbox_request =
969 (Mpi2ToolboxCleanRequest_t *)mpi_request;
970
Suganath Prabuf23ca2c2019-08-03 09:59:46 -0400971 if ((toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
972 || (toolbox_request->Tool ==
973 MPI26_TOOLBOX_BACKEND_PCIE_LANE_MARGIN))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530974 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
975 data_in_dma, data_in_sz);
Suganath Prabuba630ea2019-08-03 09:59:52 -0400976 else if (toolbox_request->Tool ==
977 MPI2_TOOLBOX_MEMORY_MOVE_TOOL) {
978 Mpi2ToolboxMemMoveRequest_t *mem_move_request =
979 (Mpi2ToolboxMemMoveRequest_t *)request;
980 Mpi2SGESimple64_t tmp, *src = NULL, *dst = NULL;
981
982 ioc->build_sg_mpi(ioc, psge, data_out_dma,
983 data_out_sz, data_in_dma, data_in_sz);
984 if (data_out_sz && !data_in_sz) {
985 dst =
986 (Mpi2SGESimple64_t *)&mem_move_request->SGL;
987 src = (void *)dst + ioc->sge_size;
988
989 memcpy(&tmp, src, ioc->sge_size);
990 memcpy(src, dst, ioc->sge_size);
991 memcpy(dst, &tmp, ioc->sge_size);
992 }
993 if (ioc->logging_level & MPT_DEBUG_TM) {
994 ioc_info(ioc,
995 "Mpi2ToolboxMemMoveRequest_t request msg\n");
996 _debug_dump_mf(mem_move_request,
997 ioc->request_sz/4);
998 }
999 } else
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301000 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
Suganath Prabuba630ea2019-08-03 09:59:52 -04001001 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001002 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301003 break;
1004 }
1005 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
1006 {
1007 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
1008 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
1009
1010 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
1011 || sasiounit_request->Operation ==
1012 MPI2_SAS_OP_PHY_LINK_RESET) {
1013 ioc->ioc_link_reset_in_progress = 1;
1014 ioc->ignore_loginfos = 1;
1015 }
1016 /* drop to default case for posting the request */
1017 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001018 fallthrough;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301019 default:
1020 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
1021 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001022 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301023 break;
1024 }
1025
1026 if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
1027 timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
1028 else
1029 timeout = karg.timeout;
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07001030 wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301031 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
1032 Mpi2SCSITaskManagementRequest_t *tm_request =
1033 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
1034 mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
1035 tm_request->DevHandle));
1036 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
1037 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
1038 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
1039 ioc->ioc_link_reset_in_progress) {
1040 ioc->ioc_link_reset_in_progress = 0;
1041 ioc->ignore_loginfos = 0;
1042 }
1043 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05001044 mpt3sas_check_cmd_timeout(ioc,
1045 ioc->ctl_cmds.status, mpi_request,
1046 karg.data_sge_offset, issue_reset);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301047 goto issue_host_reset;
1048 }
1049
1050 mpi_reply = ioc->ctl_cmds.reply;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301051
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301052 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
1053 (ioc->logging_level & MPT_DEBUG_TM)) {
1054 Mpi2SCSITaskManagementReply_t *tm_reply =
1055 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
1056
Joe Perches919d8a32018-09-17 08:01:09 -07001057 ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
1058 le16_to_cpu(tm_reply->IOCStatus),
1059 le32_to_cpu(tm_reply->IOCLogInfo),
1060 le32_to_cpu(tm_reply->TerminationCount));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301061 }
Sreekanth Reddyaf009412015-11-11 17:30:23 +05301062
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301063 /* copy out xdata to user */
1064 if (data_in_sz) {
1065 if (copy_to_user(karg.data_in_buf_ptr, data_in,
1066 data_in_sz)) {
1067 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1068 __LINE__, __func__);
1069 ret = -ENODATA;
1070 goto out;
1071 }
1072 }
1073
1074 /* copy out reply message frame to user */
1075 if (karg.max_reply_bytes) {
1076 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1077 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1078 sz)) {
1079 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1080 __LINE__, __func__);
1081 ret = -ENODATA;
1082 goto out;
1083 }
1084 }
1085
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301086 /* copy out sense/NVMe Error Response to user */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301087 if (karg.max_sense_bytes && (mpi_request->Function ==
1088 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301089 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1090 MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1091 if (karg.sense_data_ptr == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001092 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 +05301093 goto out;
1094 }
1095 sz_arg = (mpi_request->Function ==
1096 MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1097 SCSI_SENSE_BUFFERSIZE;
1098 sz = min_t(u32, karg.max_sense_bytes, sz_arg);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301099 if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1100 sz)) {
1101 pr_err("failure at %s:%d/%s()!\n", __FILE__,
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301102 __LINE__, __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301103 ret = -ENODATA;
1104 goto out;
1105 }
1106 }
1107
1108 issue_host_reset:
1109 if (issue_reset) {
1110 ret = -ENODATA;
1111 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1112 mpi_request->Function ==
1113 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1114 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001115 ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
1116 le16_to_cpu(mpi_request->FunctionDependent1));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301117 mpt3sas_halt_firmware(ioc);
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001118 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1119 le16_to_cpu(mpi_request->FunctionDependent1));
Suganath Prabu5bb309d2019-08-03 09:59:50 -04001120 if (pcie_device && (!ioc->tm_custom_handling) &&
1121 (!(mpt3sas_scsih_is_pcie_scsi_device(
1122 pcie_device->device_info))))
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001123 mpt3sas_scsih_issue_locked_tm(ioc,
1124 le16_to_cpu(mpi_request->FunctionDependent1),
Suganath Prabu S521e9c02020-07-30 13:33:47 +05301125 0, 0, 0,
1126 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001127 0, pcie_device->reset_timeout,
Suganath Prabu5bb309d2019-08-03 09:59:50 -04001128 MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE);
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001129 else
1130 mpt3sas_scsih_issue_locked_tm(ioc,
1131 le16_to_cpu(mpi_request->FunctionDependent1),
Suganath Prabu S521e9c02020-07-30 13:33:47 +05301132 0, 0, 0,
1133 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001134 0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301135 } else
Calvin Owens98c56ad2016-07-28 21:38:21 -07001136 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301137 }
1138
1139 out:
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001140 if (pcie_device)
1141 pcie_device_put(pcie_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301142
1143 /* free memory associated with sg buffers */
1144 if (data_in)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001145 dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301146 data_in_dma);
1147
1148 if (data_out)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001149 dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301150 data_out_dma);
1151
1152 kfree(mpi_request);
1153 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1154 return ret;
1155}
1156
1157/**
1158 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1159 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001160 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301161 */
1162static long
1163_ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1164{
1165 struct mpt3_ioctl_iocinfo karg;
1166
Joe Perches919d8a32018-09-17 08:01:09 -07001167 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1168 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301169
1170 memset(&karg, 0 , sizeof(karg));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301171 if (ioc->pfacts)
1172 karg.port_number = ioc->pfacts[0].PortNumber;
1173 karg.hw_rev = ioc->pdev->revision;
1174 karg.pci_id = ioc->pdev->device;
1175 karg.subsystem_device = ioc->pdev->subsystem_device;
1176 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1177 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1178 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1179 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1180 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1181 karg.firmware_version = ioc->facts.FWVersion.Word;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301182 strcpy(karg.driver_version, ioc->driver_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301183 strcat(karg.driver_version, "-");
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301184 switch (ioc->hba_mpi_version_belonged) {
1185 case MPI2_VERSION:
Sreekanth Reddy7786ab6a2015-11-11 17:30:28 +05301186 if (ioc->is_warpdrive)
1187 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1188 else
1189 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301190 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1191 break;
1192 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301193 case MPI26_VERSION:
Suganath Prabu Subramani998f26a2016-10-26 13:34:37 +05301194 if (ioc->is_gen35_ioc)
1195 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1196 else
1197 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301198 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1199 break;
1200 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301201 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1202
1203 if (copy_to_user(arg, &karg, sizeof(karg))) {
1204 pr_err("failure at %s:%d/%s()!\n",
1205 __FILE__, __LINE__, __func__);
1206 return -EFAULT;
1207 }
1208 return 0;
1209}
1210
1211/**
1212 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1213 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001214 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301215 */
1216static long
1217_ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1218{
1219 struct mpt3_ioctl_eventquery karg;
1220
1221 if (copy_from_user(&karg, arg, sizeof(karg))) {
1222 pr_err("failure at %s:%d/%s()!\n",
1223 __FILE__, __LINE__, __func__);
1224 return -EFAULT;
1225 }
1226
Joe Perches919d8a32018-09-17 08:01:09 -07001227 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1228 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301229
1230 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1231 memcpy(karg.event_types, ioc->event_type,
1232 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1233
1234 if (copy_to_user(arg, &karg, sizeof(karg))) {
1235 pr_err("failure at %s:%d/%s()!\n",
1236 __FILE__, __LINE__, __func__);
1237 return -EFAULT;
1238 }
1239 return 0;
1240}
1241
1242/**
1243 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1244 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001245 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301246 */
1247static long
1248_ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1249{
1250 struct mpt3_ioctl_eventenable karg;
1251
1252 if (copy_from_user(&karg, arg, sizeof(karg))) {
1253 pr_err("failure at %s:%d/%s()!\n",
1254 __FILE__, __LINE__, __func__);
1255 return -EFAULT;
1256 }
1257
Joe Perches919d8a32018-09-17 08:01:09 -07001258 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1259 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301260
1261 memcpy(ioc->event_type, karg.event_types,
1262 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1263 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1264
1265 if (ioc->event_log)
1266 return 0;
1267 /* initialize event_log */
1268 ioc->event_context = 0;
1269 ioc->aen_event_read_flag = 0;
1270 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1271 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1272 if (!ioc->event_log) {
1273 pr_err("failure at %s:%d/%s()!\n",
1274 __FILE__, __LINE__, __func__);
1275 return -ENOMEM;
1276 }
1277 return 0;
1278}
1279
1280/**
1281 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1282 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001283 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301284 */
1285static long
1286_ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1287{
1288 struct mpt3_ioctl_eventreport karg;
1289 u32 number_bytes, max_events, max;
1290 struct mpt3_ioctl_eventreport __user *uarg = arg;
1291
1292 if (copy_from_user(&karg, arg, sizeof(karg))) {
1293 pr_err("failure at %s:%d/%s()!\n",
1294 __FILE__, __LINE__, __func__);
1295 return -EFAULT;
1296 }
1297
Joe Perches919d8a32018-09-17 08:01:09 -07001298 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1299 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301300
1301 number_bytes = karg.hdr.max_data_size -
1302 sizeof(struct mpt3_ioctl_header);
1303 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1304 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1305
1306 /* If fewer than 1 event is requested, there must have
1307 * been some type of error.
1308 */
1309 if (!max || !ioc->event_log)
1310 return -ENODATA;
1311
1312 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1313 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1314 pr_err("failure at %s:%d/%s()!\n",
1315 __FILE__, __LINE__, __func__);
1316 return -EFAULT;
1317 }
1318
1319 /* reset flag so SIGIO can restart */
1320 ioc->aen_event_read_flag = 0;
1321 return 0;
1322}
1323
1324/**
1325 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1326 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001327 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301328 */
1329static long
1330_ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1331{
1332 struct mpt3_ioctl_diag_reset karg;
1333 int retval;
1334
1335 if (copy_from_user(&karg, arg, sizeof(karg))) {
1336 pr_err("failure at %s:%d/%s()!\n",
1337 __FILE__, __LINE__, __func__);
1338 return -EFAULT;
1339 }
1340
1341 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1342 ioc->is_driver_loading)
1343 return -EAGAIN;
1344
Joe Perches919d8a32018-09-17 08:01:09 -07001345 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1346 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301347
Suganath Prabu S688c1a02021-02-04 09:07:23 +05301348 ioc->reset_from_user = 1;
Calvin Owens98c56ad2016-07-28 21:38:21 -07001349 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddy5b061982019-12-26 06:13:30 -05001350 ioc_info(ioc,
1351 "Ioctl: host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301352 return 0;
1353}
1354
1355/**
1356 * _ctl_btdh_search_sas_device - searching for sas device
1357 * @ioc: per adapter object
1358 * @btdh: btdh ioctl payload
1359 */
1360static int
1361_ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1362 struct mpt3_ioctl_btdh_mapping *btdh)
1363{
1364 struct _sas_device *sas_device;
1365 unsigned long flags;
1366 int rc = 0;
1367
1368 if (list_empty(&ioc->sas_device_list))
1369 return rc;
1370
1371 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1372 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1373 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1374 btdh->handle == sas_device->handle) {
1375 btdh->bus = sas_device->channel;
1376 btdh->id = sas_device->id;
1377 rc = 1;
1378 goto out;
1379 } else if (btdh->bus == sas_device->channel && btdh->id ==
1380 sas_device->id && btdh->handle == 0xFFFF) {
1381 btdh->handle = sas_device->handle;
1382 rc = 1;
1383 goto out;
1384 }
1385 }
1386 out:
1387 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1388 return rc;
1389}
1390
1391/**
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +05301392 * _ctl_btdh_search_pcie_device - searching for pcie device
1393 * @ioc: per adapter object
1394 * @btdh: btdh ioctl payload
1395 */
1396static int
1397_ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
1398 struct mpt3_ioctl_btdh_mapping *btdh)
1399{
1400 struct _pcie_device *pcie_device;
1401 unsigned long flags;
1402 int rc = 0;
1403
1404 if (list_empty(&ioc->pcie_device_list))
1405 return rc;
1406
1407 spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1408 list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1409 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1410 btdh->handle == pcie_device->handle) {
1411 btdh->bus = pcie_device->channel;
1412 btdh->id = pcie_device->id;
1413 rc = 1;
1414 goto out;
1415 } else if (btdh->bus == pcie_device->channel && btdh->id ==
1416 pcie_device->id && btdh->handle == 0xFFFF) {
1417 btdh->handle = pcie_device->handle;
1418 rc = 1;
1419 goto out;
1420 }
1421 }
1422 out:
1423 spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1424 return rc;
1425}
1426
1427/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301428 * _ctl_btdh_search_raid_device - searching for raid device
1429 * @ioc: per adapter object
1430 * @btdh: btdh ioctl payload
1431 */
1432static int
1433_ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1434 struct mpt3_ioctl_btdh_mapping *btdh)
1435{
1436 struct _raid_device *raid_device;
1437 unsigned long flags;
1438 int rc = 0;
1439
1440 if (list_empty(&ioc->raid_device_list))
1441 return rc;
1442
1443 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1444 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1445 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1446 btdh->handle == raid_device->handle) {
1447 btdh->bus = raid_device->channel;
1448 btdh->id = raid_device->id;
1449 rc = 1;
1450 goto out;
1451 } else if (btdh->bus == raid_device->channel && btdh->id ==
1452 raid_device->id && btdh->handle == 0xFFFF) {
1453 btdh->handle = raid_device->handle;
1454 rc = 1;
1455 goto out;
1456 }
1457 }
1458 out:
1459 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1460 return rc;
1461}
1462
1463/**
1464 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1465 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001466 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301467 */
1468static long
1469_ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1470{
1471 struct mpt3_ioctl_btdh_mapping karg;
1472 int rc;
1473
1474 if (copy_from_user(&karg, arg, sizeof(karg))) {
1475 pr_err("failure at %s:%d/%s()!\n",
1476 __FILE__, __LINE__, __func__);
1477 return -EFAULT;
1478 }
1479
Joe Perches919d8a32018-09-17 08:01:09 -07001480 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1481 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301482
1483 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1484 if (!rc)
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +05301485 rc = _ctl_btdh_search_pcie_device(ioc, &karg);
1486 if (!rc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301487 _ctl_btdh_search_raid_device(ioc, &karg);
1488
1489 if (copy_to_user(arg, &karg, sizeof(karg))) {
1490 pr_err("failure at %s:%d/%s()!\n",
1491 __FILE__, __LINE__, __func__);
1492 return -EFAULT;
1493 }
1494 return 0;
1495}
1496
1497/**
1498 * _ctl_diag_capability - return diag buffer capability
1499 * @ioc: per adapter object
1500 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1501 *
1502 * returns 1 when diag buffer support is enabled in firmware
1503 */
1504static u8
1505_ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1506{
1507 u8 rc = 0;
1508
1509 switch (buffer_type) {
1510 case MPI2_DIAG_BUF_TYPE_TRACE:
1511 if (ioc->facts.IOCCapabilities &
1512 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1513 rc = 1;
1514 break;
1515 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1516 if (ioc->facts.IOCCapabilities &
1517 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1518 rc = 1;
1519 break;
1520 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1521 if (ioc->facts.IOCCapabilities &
1522 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1523 rc = 1;
1524 }
1525
1526 return rc;
1527}
1528
Sreekanth Reddy08e73782019-09-13 09:04:42 -04001529/**
1530 * _ctl_diag_get_bufftype - return diag buffer type
1531 * either TRACE, SNAPSHOT, or EXTENDED
1532 * @ioc: per adapter object
1533 * @unique_id: specifies the unique_id for the buffer
1534 *
1535 * returns MPT3_DIAG_UID_NOT_FOUND if the id not found
1536 */
1537static u8
1538_ctl_diag_get_bufftype(struct MPT3SAS_ADAPTER *ioc, u32 unique_id)
1539{
1540 u8 index;
1541
1542 for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
1543 if (ioc->unique_id[index] == unique_id)
1544 return index;
1545 }
1546
1547 return MPT3_DIAG_UID_NOT_FOUND;
1548}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301549
1550/**
1551 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1552 * @ioc: per adapter object
1553 * @diag_register: the diag_register struct passed in from user space
1554 *
1555 */
1556static long
1557_ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1558 struct mpt3_diag_register *diag_register)
1559{
1560 int rc, i;
1561 void *request_data = NULL;
1562 dma_addr_t request_data_dma;
1563 u32 request_data_sz = 0;
1564 Mpi2DiagBufferPostRequest_t *mpi_request;
1565 Mpi2DiagBufferPostReply_t *mpi_reply;
1566 u8 buffer_type;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301567 u16 smid;
1568 u16 ioc_status;
1569 u32 ioc_state;
1570 u8 issue_reset = 0;
1571
Joe Perches919d8a32018-09-17 08:01:09 -07001572 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1573 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301574
1575 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1576 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001577 ioc_err(ioc, "%s: failed due to ioc not operational\n",
1578 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301579 rc = -EAGAIN;
1580 goto out;
1581 }
1582
1583 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001584 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301585 rc = -EAGAIN;
1586 goto out;
1587 }
1588
1589 buffer_type = diag_register->buffer_type;
1590 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001591 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1592 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301593 return -EPERM;
1594 }
1595
Sreekanth Reddy08e73782019-09-13 09:04:42 -04001596 if (diag_register->unique_id == 0) {
1597 ioc_err(ioc,
1598 "%s: Invalid UID(0x%08x), buffer_type(0x%02x)\n", __func__,
1599 diag_register->unique_id, buffer_type);
1600 return -EINVAL;
1601 }
1602
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04001603 if ((ioc->diag_buffer_status[buffer_type] &
1604 MPT3_DIAG_BUFFER_IS_APP_OWNED) &&
1605 !(ioc->diag_buffer_status[buffer_type] &
1606 MPT3_DIAG_BUFFER_IS_RELEASED)) {
1607 ioc_err(ioc,
1608 "%s: buffer_type(0x%02x) is already registered by application with UID(0x%08x)\n",
1609 __func__, buffer_type, ioc->unique_id[buffer_type]);
1610 return -EINVAL;
1611 }
1612
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301613 if (ioc->diag_buffer_status[buffer_type] &
1614 MPT3_DIAG_BUFFER_IS_REGISTERED) {
Sreekanth Reddy08e73782019-09-13 09:04:42 -04001615 /*
1616 * If driver posts buffer initially, then an application wants
1617 * to Register that buffer (own it) without Releasing first,
1618 * the application Register command MUST have the same buffer
1619 * type and size in the Register command (obtained from the
1620 * Query command). Otherwise that Register command will be
1621 * failed. If the application has released the buffer but wants
1622 * to re-register it, it should be allowed as long as the
1623 * Unique-Id/Size match.
1624 */
1625
1626 if (ioc->unique_id[buffer_type] == MPT3DIAGBUFFUNIQUEID &&
1627 ioc->diag_buffer_sz[buffer_type] ==
1628 diag_register->requested_buffer_size) {
1629
1630 if (!(ioc->diag_buffer_status[buffer_type] &
1631 MPT3_DIAG_BUFFER_IS_RELEASED)) {
1632 dctlprintk(ioc, ioc_info(ioc,
1633 "%s: diag_buffer (%d) ownership changed. old-ID(0x%08x), new-ID(0x%08x)\n",
1634 __func__, buffer_type,
1635 ioc->unique_id[buffer_type],
1636 diag_register->unique_id));
1637
1638 /*
1639 * Application wants to own the buffer with
1640 * the same size.
1641 */
1642 ioc->unique_id[buffer_type] =
1643 diag_register->unique_id;
1644 rc = 0; /* success */
1645 goto out;
1646 }
1647 } else if (ioc->unique_id[buffer_type] !=
1648 MPT3DIAGBUFFUNIQUEID) {
1649 if (ioc->unique_id[buffer_type] !=
1650 diag_register->unique_id ||
1651 ioc->diag_buffer_sz[buffer_type] !=
1652 diag_register->requested_buffer_size ||
1653 !(ioc->diag_buffer_status[buffer_type] &
1654 MPT3_DIAG_BUFFER_IS_RELEASED)) {
1655 ioc_err(ioc,
1656 "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1657 __func__, buffer_type);
1658 return -EINVAL;
1659 }
1660 } else {
1661 ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1662 __func__, buffer_type);
1663 return -EINVAL;
1664 }
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001665 } else if (ioc->diag_buffer_status[buffer_type] &
1666 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
1667
1668 if (ioc->unique_id[buffer_type] != MPT3DIAGBUFFUNIQUEID ||
1669 ioc->diag_buffer_sz[buffer_type] !=
1670 diag_register->requested_buffer_size) {
1671
1672 ioc_err(ioc,
1673 "%s: already a buffer is allocated for buffer_type(0x%02x) of size %d bytes, so please try registering again with same size\n",
1674 __func__, buffer_type,
1675 ioc->diag_buffer_sz[buffer_type]);
1676 return -EINVAL;
1677 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301678 }
1679
1680 if (diag_register->requested_buffer_size % 4) {
Joe Perches919d8a32018-09-17 08:01:09 -07001681 ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
1682 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301683 return -EINVAL;
1684 }
1685
1686 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1687 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07001688 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301689 rc = -EAGAIN;
1690 goto out;
1691 }
1692
1693 rc = 0;
1694 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1695 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1696 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
Sreekanth Reddy463e683b2022-08-25 13:24:55 +05301697 memset(mpi_request, 0, ioc->request_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301698 ioc->ctl_cmds.smid = smid;
1699
1700 request_data = ioc->diag_buffer[buffer_type];
1701 request_data_sz = diag_register->requested_buffer_size;
1702 ioc->unique_id[buffer_type] = diag_register->unique_id;
Suganath Prabu S688c1a02021-02-04 09:07:23 +05301703 /* Reset ioc variables used for additional query commands */
1704 ioc->reset_from_user = 0;
1705 memset(&ioc->htb_rel, 0, sizeof(struct htb_rel_query));
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001706 ioc->diag_buffer_status[buffer_type] &=
1707 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301708 memcpy(ioc->product_specific[buffer_type],
1709 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1710 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1711
1712 if (request_data) {
1713 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1714 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001715 dma_free_coherent(&ioc->pdev->dev,
1716 ioc->diag_buffer_sz[buffer_type],
1717 request_data, request_data_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301718 request_data = NULL;
1719 }
1720 }
1721
1722 if (request_data == NULL) {
1723 ioc->diag_buffer_sz[buffer_type] = 0;
1724 ioc->diag_buffer_dma[buffer_type] = 0;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001725 request_data = dma_alloc_coherent(&ioc->pdev->dev,
1726 request_data_sz, &request_data_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301727 if (request_data == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001728 ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
1729 __func__, request_data_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301730 mpt3sas_base_free_smid(ioc, smid);
Sreekanth Reddy782b2812019-09-13 09:04:40 -04001731 rc = -ENOMEM;
1732 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301733 }
1734 ioc->diag_buffer[buffer_type] = request_data;
1735 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1736 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1737 }
1738
1739 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1740 mpi_request->BufferType = diag_register->buffer_type;
1741 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1742 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1743 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1744 mpi_request->VF_ID = 0; /* TODO */
1745 mpi_request->VP_ID = 0;
1746
Joe Perches919d8a32018-09-17 08:01:09 -07001747 dctlprintk(ioc,
1748 ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1749 __func__, request_data,
1750 (unsigned long long)request_data_dma,
1751 le32_to_cpu(mpi_request->BufferLength)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301752
1753 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1754 mpi_request->ProductSpecific[i] =
1755 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1756
1757 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001758 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07001759 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301760 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1761
1762 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05001763 mpt3sas_check_cmd_timeout(ioc,
1764 ioc->ctl_cmds.status, mpi_request,
1765 sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301766 goto issue_host_reset;
1767 }
1768
1769 /* process the completed Reply Message Frame */
1770 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001771 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301772 rc = -EFAULT;
1773 goto out;
1774 }
1775
1776 mpi_reply = ioc->ctl_cmds.reply;
1777 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1778
1779 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1780 ioc->diag_buffer_status[buffer_type] |=
1781 MPT3_DIAG_BUFFER_IS_REGISTERED;
Joe Perches919d8a32018-09-17 08:01:09 -07001782 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301783 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07001784 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1785 __func__,
1786 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301787 rc = -EFAULT;
1788 }
1789
1790 issue_host_reset:
1791 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07001792 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301793
1794 out:
1795
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001796 if (rc && request_data) {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001797 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301798 request_data, request_data_dma);
Sreekanth Reddy463e683b2022-08-25 13:24:55 +05301799 ioc->diag_buffer[buffer_type] = NULL;
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001800 ioc->diag_buffer_status[buffer_type] &=
1801 ~MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1802 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301803
1804 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1805 return rc;
1806}
1807
1808/**
1809 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1810 * @ioc: per adapter object
1811 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1812 *
1813 * This is called when command line option diag_buffer_enable is enabled
1814 * at driver load time.
1815 */
1816void
1817mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1818{
1819 struct mpt3_diag_register diag_register;
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04001820 u32 ret_val;
1821 u32 trace_buff_size = ioc->manu_pg11.HostTraceBufferMaxSizeKB<<10;
1822 u32 min_trace_buff_size = 0;
1823 u32 decr_trace_buff_size = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301824
1825 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1826
1827 if (bits_to_register & 1) {
Joe Perches919d8a32018-09-17 08:01:09 -07001828 ioc_info(ioc, "registering trace buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301829 ioc->diag_trigger_master.MasterData =
1830 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1831 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
Sreekanth Reddy08e73782019-09-13 09:04:42 -04001832 diag_register.unique_id =
1833 (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
1834 (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04001835
1836 if (trace_buff_size != 0) {
1837 diag_register.requested_buffer_size = trace_buff_size;
1838 min_trace_buff_size =
1839 ioc->manu_pg11.HostTraceBufferMinSizeKB<<10;
1840 decr_trace_buff_size =
1841 ioc->manu_pg11.HostTraceBufferDecrementSizeKB<<10;
1842
1843 if (min_trace_buff_size > trace_buff_size) {
1844 /* The buff size is not set correctly */
1845 ioc_err(ioc,
1846 "Min Trace Buff size (%d KB) greater than Max Trace Buff size (%d KB)\n",
1847 min_trace_buff_size>>10,
1848 trace_buff_size>>10);
1849 ioc_err(ioc,
1850 "Using zero Min Trace Buff Size\n");
Dan Carpenter3524a382019-10-04 13:06:15 +03001851 min_trace_buff_size = 0;
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04001852 }
1853
1854 if (decr_trace_buff_size == 0) {
1855 /*
1856 * retry the min size if decrement
1857 * is not available.
1858 */
1859 decr_trace_buff_size =
1860 trace_buff_size - min_trace_buff_size;
1861 }
1862 } else {
1863 /* register for 2MB buffers */
1864 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1865 }
1866
1867 do {
1868 ret_val = _ctl_diag_register_2(ioc, &diag_register);
1869
1870 if (ret_val == -ENOMEM && min_trace_buff_size &&
1871 (trace_buff_size - decr_trace_buff_size) >=
1872 min_trace_buff_size) {
1873 /* adjust the buffer size */
1874 trace_buff_size -= decr_trace_buff_size;
1875 diag_register.requested_buffer_size =
1876 trace_buff_size;
1877 } else
1878 break;
1879 } while (true);
1880
1881 if (ret_val == -ENOMEM)
1882 ioc_err(ioc,
1883 "Cannot allocate trace buffer memory. Last memory tried = %d KB\n",
1884 diag_register.requested_buffer_size>>10);
1885 else if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE]
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001886 & MPT3_DIAG_BUFFER_IS_REGISTERED) {
Paul Menzel1eeedfa2023-01-03 16:04:37 +01001887 ioc_info(ioc, "Trace buffer memory %d KB allocated\n",
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04001888 diag_register.requested_buffer_size>>10);
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001889 if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
1890 ioc->diag_buffer_status[
1891 MPI2_DIAG_BUF_TYPE_TRACE] |=
1892 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1893 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301894 }
1895
1896 if (bits_to_register & 2) {
Joe Perches919d8a32018-09-17 08:01:09 -07001897 ioc_info(ioc, "registering snapshot buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301898 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1899 /* register for 2MB buffers */
1900 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1901 diag_register.unique_id = 0x7075901;
1902 _ctl_diag_register_2(ioc, &diag_register);
1903 }
1904
1905 if (bits_to_register & 4) {
Joe Perches919d8a32018-09-17 08:01:09 -07001906 ioc_info(ioc, "registering extended buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301907 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1908 /* register for 2MB buffers */
1909 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1910 diag_register.unique_id = 0x7075901;
1911 _ctl_diag_register_2(ioc, &diag_register);
1912 }
1913}
1914
1915/**
1916 * _ctl_diag_register - application register with driver
1917 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001918 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301919 *
1920 * This will allow the driver to setup any required buffers that will be
1921 * needed by firmware to communicate with the driver.
1922 */
1923static long
1924_ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1925{
1926 struct mpt3_diag_register karg;
1927 long rc;
1928
1929 if (copy_from_user(&karg, arg, sizeof(karg))) {
1930 pr_err("failure at %s:%d/%s()!\n",
1931 __FILE__, __LINE__, __func__);
1932 return -EFAULT;
1933 }
1934
1935 rc = _ctl_diag_register_2(ioc, &karg);
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04001936
1937 if (!rc && (ioc->diag_buffer_status[karg.buffer_type] &
1938 MPT3_DIAG_BUFFER_IS_REGISTERED))
1939 ioc->diag_buffer_status[karg.buffer_type] |=
1940 MPT3_DIAG_BUFFER_IS_APP_OWNED;
1941
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301942 return rc;
1943}
1944
1945/**
1946 * _ctl_diag_unregister - application unregister with driver
1947 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001948 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301949 *
1950 * This will allow the driver to cleanup any memory allocated for diag
1951 * messages and to free up any resources.
1952 */
1953static long
1954_ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1955{
1956 struct mpt3_diag_unregister karg;
1957 void *request_data;
1958 dma_addr_t request_data_dma;
1959 u32 request_data_sz;
1960 u8 buffer_type;
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
Sreekanth Reddy08e73782019-09-13 09:04:42 -04001971 buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
1972 if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
1973 ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
1974 __func__, karg.unique_id);
1975 return -EINVAL;
1976 }
1977
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301978 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001979 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1980 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301981 return -EPERM;
1982 }
1983
1984 if ((ioc->diag_buffer_status[buffer_type] &
1985 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001986 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1987 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301988 return -EINVAL;
1989 }
1990 if ((ioc->diag_buffer_status[buffer_type] &
1991 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001992 ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
1993 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301994 return -EINVAL;
1995 }
1996
1997 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07001998 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1999 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302000 return -EINVAL;
2001 }
2002
2003 request_data = ioc->diag_buffer[buffer_type];
2004 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002005 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2006 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302007 return -ENOMEM;
2008 }
2009
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04002010 if (ioc->diag_buffer_status[buffer_type] &
2011 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
2012 ioc->unique_id[buffer_type] = MPT3DIAGBUFFUNIQUEID;
2013 ioc->diag_buffer_status[buffer_type] &=
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04002014 ~MPT3_DIAG_BUFFER_IS_APP_OWNED;
2015 ioc->diag_buffer_status[buffer_type] &=
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04002016 ~MPT3_DIAG_BUFFER_IS_REGISTERED;
2017 } else {
2018 request_data_sz = ioc->diag_buffer_sz[buffer_type];
2019 request_data_dma = ioc->diag_buffer_dma[buffer_type];
2020 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
2021 request_data, request_data_dma);
2022 ioc->diag_buffer[buffer_type] = NULL;
2023 ioc->diag_buffer_status[buffer_type] = 0;
2024 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302025 return 0;
2026}
2027
2028/**
2029 * _ctl_diag_query - query relevant info associated with diag buffers
2030 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002031 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302032 *
2033 * The application will send only buffer_type and unique_id. Driver will
2034 * inspect unique_id first, if valid, fill in all the info. If unique_id is
2035 * 0x00, the driver will return info specified by Buffer Type.
2036 */
2037static long
2038_ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2039{
2040 struct mpt3_diag_query karg;
2041 void *request_data;
2042 int i;
2043 u8 buffer_type;
2044
2045 if (copy_from_user(&karg, arg, sizeof(karg))) {
2046 pr_err("failure at %s:%d/%s()!\n",
2047 __FILE__, __LINE__, __func__);
2048 return -EFAULT;
2049 }
2050
Joe Perches919d8a32018-09-17 08:01:09 -07002051 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2052 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302053
2054 karg.application_flags = 0;
2055 buffer_type = karg.buffer_type;
2056
2057 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002058 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2059 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302060 return -EPERM;
2061 }
2062
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04002063 if (!(ioc->diag_buffer_status[buffer_type] &
2064 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED)) {
2065 if ((ioc->diag_buffer_status[buffer_type] &
2066 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2067 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2068 __func__, buffer_type);
2069 return -EINVAL;
2070 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302071 }
2072
Sreekanth Reddy08e73782019-09-13 09:04:42 -04002073 if (karg.unique_id) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302074 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002075 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2076 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302077 return -EINVAL;
2078 }
2079 }
2080
2081 request_data = ioc->diag_buffer[buffer_type];
2082 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002083 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2084 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302085 return -ENOMEM;
2086 }
2087
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04002088 if ((ioc->diag_buffer_status[buffer_type] &
2089 MPT3_DIAG_BUFFER_IS_REGISTERED))
2090 karg.application_flags |= MPT3_APP_FLAGS_BUFFER_VALID;
2091
2092 if (!(ioc->diag_buffer_status[buffer_type] &
2093 MPT3_DIAG_BUFFER_IS_RELEASED))
2094 karg.application_flags |= MPT3_APP_FLAGS_FW_BUFFER_ACCESS;
2095
2096 if (!(ioc->diag_buffer_status[buffer_type] &
2097 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED))
2098 karg.application_flags |= MPT3_APP_FLAGS_DYNAMIC_BUFFER_ALLOC;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302099
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04002100 if ((ioc->diag_buffer_status[buffer_type] &
2101 MPT3_DIAG_BUFFER_IS_APP_OWNED))
2102 karg.application_flags |= MPT3_APP_FLAGS_APP_OWNED;
2103
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302104 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2105 karg.product_specific[i] =
2106 ioc->product_specific[buffer_type][i];
2107
2108 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
2109 karg.driver_added_buffer_size = 0;
2110 karg.unique_id = ioc->unique_id[buffer_type];
2111 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
2112
2113 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
Joe Perches919d8a32018-09-17 08:01:09 -07002114 ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
2115 __func__, arg);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302116 return -EFAULT;
2117 }
2118 return 0;
2119}
2120
2121/**
2122 * mpt3sas_send_diag_release - Diag Release Message
2123 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002124 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
2125 * @issue_reset: specifies whether host reset is required.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302126 *
2127 */
2128int
2129mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
2130 u8 *issue_reset)
2131{
2132 Mpi2DiagReleaseRequest_t *mpi_request;
2133 Mpi2DiagReleaseReply_t *mpi_reply;
2134 u16 smid;
2135 u16 ioc_status;
2136 u32 ioc_state;
2137 int rc;
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05002138 u8 reset_needed = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302139
Joe Perches919d8a32018-09-17 08:01:09 -07002140 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2141 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302142
2143 rc = 0;
2144 *issue_reset = 0;
2145
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05002146
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302147 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2148 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2149 if (ioc->diag_buffer_status[buffer_type] &
2150 MPT3_DIAG_BUFFER_IS_REGISTERED)
2151 ioc->diag_buffer_status[buffer_type] |=
2152 MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07002153 dctlprintk(ioc,
2154 ioc_info(ioc, "%s: skipping due to FAULT state\n",
2155 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302156 rc = -EAGAIN;
2157 goto out;
2158 }
2159
2160 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07002161 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302162 rc = -EAGAIN;
2163 goto out;
2164 }
2165
2166 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2167 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07002168 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302169 rc = -EAGAIN;
2170 goto out;
2171 }
2172
2173 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2174 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2175 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
Sreekanth Reddy463e683b2022-08-25 13:24:55 +05302176 memset(mpi_request, 0, ioc->request_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302177 ioc->ctl_cmds.smid = smid;
2178
2179 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
2180 mpi_request->BufferType = buffer_type;
2181 mpi_request->VF_ID = 0; /* TODO */
2182 mpi_request->VP_ID = 0;
2183
2184 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04002185 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002186 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302187 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2188
2189 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05002190 mpt3sas_check_cmd_timeout(ioc,
2191 ioc->ctl_cmds.status, mpi_request,
2192 sizeof(Mpi2DiagReleaseRequest_t)/4, reset_needed);
Colin Ian King04c260b2021-09-02 23:42:15 +01002193 *issue_reset = reset_needed;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302194 rc = -EFAULT;
2195 goto out;
2196 }
2197
2198 /* process the completed Reply Message Frame */
2199 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002200 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302201 rc = -EFAULT;
2202 goto out;
2203 }
2204
2205 mpi_reply = ioc->ctl_cmds.reply;
2206 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2207
2208 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2209 ioc->diag_buffer_status[buffer_type] |=
2210 MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07002211 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302212 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07002213 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2214 __func__,
2215 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302216 rc = -EFAULT;
2217 }
2218
2219 out:
2220 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2221 return rc;
2222}
2223
2224/**
2225 * _ctl_diag_release - request to send Diag Release Message to firmware
Bart Van Assche4beb4862018-06-15 14:42:01 -07002226 * @ioc: ?
2227 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302228 *
2229 * This allows ownership of the specified buffer to returned to the driver,
2230 * allowing an application to read the buffer without fear that firmware is
Masahiro Yamada9a284e52017-02-27 14:29:48 -08002231 * overwriting information in the buffer.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302232 */
2233static long
2234_ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2235{
2236 struct mpt3_diag_release karg;
2237 void *request_data;
2238 int rc;
2239 u8 buffer_type;
2240 u8 issue_reset = 0;
2241
2242 if (copy_from_user(&karg, arg, sizeof(karg))) {
2243 pr_err("failure at %s:%d/%s()!\n",
2244 __FILE__, __LINE__, __func__);
2245 return -EFAULT;
2246 }
2247
Joe Perches919d8a32018-09-17 08:01:09 -07002248 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2249 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302250
Sreekanth Reddy08e73782019-09-13 09:04:42 -04002251 buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2252 if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2253 ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2254 __func__, karg.unique_id);
2255 return -EINVAL;
2256 }
2257
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302258 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002259 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2260 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302261 return -EPERM;
2262 }
2263
2264 if ((ioc->diag_buffer_status[buffer_type] &
2265 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002266 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2267 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302268 return -EINVAL;
2269 }
2270
2271 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002272 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2273 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302274 return -EINVAL;
2275 }
2276
2277 if (ioc->diag_buffer_status[buffer_type] &
2278 MPT3_DIAG_BUFFER_IS_RELEASED) {
Joe Perches919d8a32018-09-17 08:01:09 -07002279 ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
2280 __func__, buffer_type);
Sreekanth Reddy29f571f2019-09-13 09:04:46 -04002281 return -EINVAL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302282 }
2283
2284 request_data = ioc->diag_buffer[buffer_type];
2285
2286 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002287 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2288 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302289 return -ENOMEM;
2290 }
2291
2292 /* buffers were released by due to host reset */
2293 if ((ioc->diag_buffer_status[buffer_type] &
2294 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2295 ioc->diag_buffer_status[buffer_type] |=
2296 MPT3_DIAG_BUFFER_IS_RELEASED;
2297 ioc->diag_buffer_status[buffer_type] &=
2298 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
Joe Perches919d8a32018-09-17 08:01:09 -07002299 ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
2300 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302301 return 0;
2302 }
2303
2304 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2305
2306 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07002307 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302308
2309 return rc;
2310}
2311
2312/**
2313 * _ctl_diag_read_buffer - request for copy of the diag buffer
2314 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002315 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302316 */
2317static long
2318_ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2319{
2320 struct mpt3_diag_read_buffer karg;
2321 struct mpt3_diag_read_buffer __user *uarg = arg;
2322 void *request_data, *diag_data;
2323 Mpi2DiagBufferPostRequest_t *mpi_request;
2324 Mpi2DiagBufferPostReply_t *mpi_reply;
2325 int rc, i;
2326 u8 buffer_type;
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002327 unsigned long request_size, copy_size;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302328 u16 smid;
2329 u16 ioc_status;
2330 u8 issue_reset = 0;
2331
2332 if (copy_from_user(&karg, arg, sizeof(karg))) {
2333 pr_err("failure at %s:%d/%s()!\n",
2334 __FILE__, __LINE__, __func__);
2335 return -EFAULT;
2336 }
2337
Joe Perches919d8a32018-09-17 08:01:09 -07002338 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2339 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302340
Sreekanth Reddy08e73782019-09-13 09:04:42 -04002341 buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2342 if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2343 ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2344 __func__, karg.unique_id);
2345 return -EINVAL;
2346 }
2347
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302348 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002349 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2350 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302351 return -EPERM;
2352 }
2353
2354 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002355 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2356 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302357 return -EINVAL;
2358 }
2359
2360 request_data = ioc->diag_buffer[buffer_type];
2361 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002362 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2363 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302364 return -ENOMEM;
2365 }
2366
2367 request_size = ioc->diag_buffer_sz[buffer_type];
2368
2369 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002370 ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
2371 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302372 return -EINVAL;
2373 }
2374
2375 if (karg.starting_offset > request_size)
2376 return -EINVAL;
2377
2378 diag_data = (void *)(request_data + karg.starting_offset);
Joe Perches919d8a32018-09-17 08:01:09 -07002379 dctlprintk(ioc,
2380 ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2381 __func__, diag_data, karg.starting_offset,
2382 karg.bytes_to_read));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302383
2384 /* Truncate data on requests that are too large */
2385 if ((diag_data + karg.bytes_to_read < diag_data) ||
2386 (diag_data + karg.bytes_to_read > request_data + request_size))
2387 copy_size = request_size - karg.starting_offset;
2388 else
2389 copy_size = karg.bytes_to_read;
2390
2391 if (copy_to_user((void __user *)uarg->diagnostic_data,
2392 diag_data, copy_size)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002393 ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2394 __func__, diag_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302395 return -EFAULT;
2396 }
2397
2398 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2399 return 0;
2400
Joe Perches919d8a32018-09-17 08:01:09 -07002401 dctlprintk(ioc,
2402 ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
2403 __func__, buffer_type));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302404 if ((ioc->diag_buffer_status[buffer_type] &
2405 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002406 dctlprintk(ioc,
2407 ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
2408 __func__, buffer_type));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302409 return 0;
2410 }
2411 /* Get a free request frame and save the message context.
2412 */
2413
2414 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07002415 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302416 rc = -EAGAIN;
2417 goto out;
2418 }
2419
2420 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2421 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07002422 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302423 rc = -EAGAIN;
2424 goto out;
2425 }
2426
2427 rc = 0;
2428 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2429 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2430 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
Sreekanth Reddy463e683b2022-08-25 13:24:55 +05302431 memset(mpi_request, 0, ioc->request_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302432 ioc->ctl_cmds.smid = smid;
2433
2434 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2435 mpi_request->BufferType = buffer_type;
2436 mpi_request->BufferLength =
2437 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2438 mpi_request->BufferAddress =
2439 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2440 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2441 mpi_request->ProductSpecific[i] =
2442 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2443 mpi_request->VF_ID = 0; /* TODO */
2444 mpi_request->VP_ID = 0;
2445
2446 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04002447 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002448 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302449 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2450
2451 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05002452 mpt3sas_check_cmd_timeout(ioc,
2453 ioc->ctl_cmds.status, mpi_request,
2454 sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302455 goto issue_host_reset;
2456 }
2457
2458 /* process the completed Reply Message Frame */
2459 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002460 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302461 rc = -EFAULT;
2462 goto out;
2463 }
2464
2465 mpi_reply = ioc->ctl_cmds.reply;
2466 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2467
2468 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2469 ioc->diag_buffer_status[buffer_type] |=
2470 MPT3_DIAG_BUFFER_IS_REGISTERED;
Sreekanth Reddydd180e4e2019-09-13 09:04:43 -04002471 ioc->diag_buffer_status[buffer_type] &=
2472 ~MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07002473 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302474 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07002475 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2476 __func__, ioc_status,
2477 le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302478 rc = -EFAULT;
2479 }
2480
2481 issue_host_reset:
2482 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07002483 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302484
2485 out:
2486
2487 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2488 return rc;
2489}
2490
Suganath Prabu S688c1a02021-02-04 09:07:23 +05302491/**
2492 * _ctl_addnl_diag_query - query relevant info associated with diag buffers
2493 * @ioc: per adapter object
2494 * @arg: user space buffer containing ioctl content
2495 *
2496 * The application will send only unique_id. Driver will
2497 * inspect unique_id first, if valid, fill the details related to cause
2498 * for diag buffer release.
2499 */
2500static long
2501_ctl_addnl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2502{
2503 struct mpt3_addnl_diag_query karg;
2504 u32 buffer_type = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302505
Suganath Prabu S688c1a02021-02-04 09:07:23 +05302506 if (copy_from_user(&karg, arg, sizeof(karg))) {
2507 pr_err("%s: failure at %s:%d/%s()!\n",
2508 ioc->name, __FILE__, __LINE__, __func__);
2509 return -EFAULT;
2510 }
2511 dctlprintk(ioc, ioc_info(ioc, "%s\n", __func__));
2512 if (karg.unique_id == 0) {
2513 ioc_err(ioc, "%s: unique_id is(0x%08x)\n",
2514 __func__, karg.unique_id);
2515 return -EPERM;
2516 }
2517 buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2518 if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2519 ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2520 __func__, karg.unique_id);
2521 return -EPERM;
2522 }
Gustavo A. R. Silva16660db2021-04-01 11:20:54 -05002523 memset(&karg.rel_query, 0, sizeof(karg.rel_query));
Suganath Prabu S688c1a02021-02-04 09:07:23 +05302524 if ((ioc->diag_buffer_status[buffer_type] &
2525 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2526 ioc_info(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2527 __func__, buffer_type);
2528 goto out;
2529 }
2530 if ((ioc->diag_buffer_status[buffer_type] &
2531 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2532 ioc_err(ioc, "%s: buffer_type(0x%02x) is not released\n",
2533 __func__, buffer_type);
2534 return -EPERM;
2535 }
Gustavo A. R. Silva16660db2021-04-01 11:20:54 -05002536 memcpy(&karg.rel_query, &ioc->htb_rel, sizeof(karg.rel_query));
Suganath Prabu S688c1a02021-02-04 09:07:23 +05302537out:
2538 if (copy_to_user(arg, &karg, sizeof(struct mpt3_addnl_diag_query))) {
2539 ioc_err(ioc, "%s: unable to write mpt3_addnl_diag_query data @ %p\n",
2540 __func__, arg);
2541 return -EFAULT;
2542 }
2543 return 0;
2544}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302545
2546#ifdef CONFIG_COMPAT
2547/**
2548 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2549 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002550 * @cmd: ioctl opcode
2551 * @arg: (struct mpt3_ioctl_command32)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302552 *
2553 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2554 */
2555static long
2556_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2557 void __user *arg)
2558{
2559 struct mpt3_ioctl_command32 karg32;
2560 struct mpt3_ioctl_command32 __user *uarg;
2561 struct mpt3_ioctl_command karg;
2562
2563 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2564 return -EINVAL;
2565
2566 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2567
2568 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2569 pr_err("failure at %s:%d/%s()!\n",
2570 __FILE__, __LINE__, __func__);
2571 return -EFAULT;
2572 }
2573
2574 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2575 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2576 karg.hdr.port_number = karg32.hdr.port_number;
2577 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2578 karg.timeout = karg32.timeout;
2579 karg.max_reply_bytes = karg32.max_reply_bytes;
2580 karg.data_in_size = karg32.data_in_size;
2581 karg.data_out_size = karg32.data_out_size;
2582 karg.max_sense_bytes = karg32.max_sense_bytes;
2583 karg.data_sge_offset = karg32.data_sge_offset;
2584 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2585 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2586 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2587 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2588 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2589}
2590#endif
2591
2592/**
2593 * _ctl_ioctl_main - main ioctl entry point
Bart Van Assche4beb4862018-06-15 14:42:01 -07002594 * @file: (struct file)
2595 * @cmd: ioctl opcode
2596 * @arg: user space data buffer
2597 * @compat: handles 32 bit applications in 64bit os
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302598 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302599 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302600 */
2601static long
2602_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302603 u8 compat, u16 mpi_version)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302604{
2605 struct MPT3SAS_ADAPTER *ioc;
2606 struct mpt3_ioctl_header ioctl_header;
2607 enum block_state state;
Suganath Prabu S688c1a02021-02-04 09:07:23 +05302608 long ret = -ENOIOCTLCMD;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302609
2610 /* get IOCTL header */
2611 if (copy_from_user(&ioctl_header, (char __user *)arg,
2612 sizeof(struct mpt3_ioctl_header))) {
2613 pr_err("failure at %s:%d/%s()!\n",
2614 __FILE__, __LINE__, __func__);
2615 return -EFAULT;
2616 }
2617
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302618 if (_ctl_verify_adapter(ioctl_header.ioc_number,
2619 &ioc, mpi_version) == -1 || !ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302620 return -ENODEV;
2621
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302622 /* pci_access_mutex lock acquired by ioctl path */
2623 mutex_lock(&ioc->pci_access_mutex);
2624
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302625 if (ioc->shost_recovery || ioc->pci_error_recovery ||
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302626 ioc->is_driver_loading || ioc->remove_host) {
2627 ret = -EAGAIN;
2628 goto out_unlock_pciaccess;
2629 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302630
2631 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2632 if (state == NON_BLOCKING) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302633 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2634 ret = -EAGAIN;
2635 goto out_unlock_pciaccess;
2636 }
2637 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2638 ret = -ERESTARTSYS;
2639 goto out_unlock_pciaccess;
2640 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302641
2642
2643 switch (cmd) {
2644 case MPT3IOCINFO:
2645 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2646 ret = _ctl_getiocinfo(ioc, arg);
2647 break;
2648#ifdef CONFIG_COMPAT
2649 case MPT3COMMAND32:
2650#endif
2651 case MPT3COMMAND:
2652 {
2653 struct mpt3_ioctl_command __user *uarg;
2654 struct mpt3_ioctl_command karg;
2655
2656#ifdef CONFIG_COMPAT
2657 if (compat) {
2658 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2659 break;
2660 }
2661#endif
2662 if (copy_from_user(&karg, arg, sizeof(karg))) {
2663 pr_err("failure at %s:%d/%s()!\n",
2664 __FILE__, __LINE__, __func__);
2665 ret = -EFAULT;
2666 break;
2667 }
2668
Gen Zhangf9e3ebe2019-05-30 09:10:30 +08002669 if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2670 ret = -EINVAL;
2671 break;
2672 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302673 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2674 uarg = arg;
2675 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2676 }
2677 break;
2678 }
2679 case MPT3EVENTQUERY:
2680 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2681 ret = _ctl_eventquery(ioc, arg);
2682 break;
2683 case MPT3EVENTENABLE:
2684 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2685 ret = _ctl_eventenable(ioc, arg);
2686 break;
2687 case MPT3EVENTREPORT:
2688 ret = _ctl_eventreport(ioc, arg);
2689 break;
2690 case MPT3HARDRESET:
2691 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2692 ret = _ctl_do_reset(ioc, arg);
2693 break;
2694 case MPT3BTDHMAPPING:
2695 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2696 ret = _ctl_btdh_mapping(ioc, arg);
2697 break;
2698 case MPT3DIAGREGISTER:
2699 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2700 ret = _ctl_diag_register(ioc, arg);
2701 break;
2702 case MPT3DIAGUNREGISTER:
2703 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2704 ret = _ctl_diag_unregister(ioc, arg);
2705 break;
2706 case MPT3DIAGQUERY:
2707 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2708 ret = _ctl_diag_query(ioc, arg);
2709 break;
2710 case MPT3DIAGRELEASE:
2711 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2712 ret = _ctl_diag_release(ioc, arg);
2713 break;
2714 case MPT3DIAGREADBUFFER:
2715 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2716 ret = _ctl_diag_read_buffer(ioc, arg);
2717 break;
Suganath Prabu S688c1a02021-02-04 09:07:23 +05302718 case MPT3ADDNLDIAGQUERY:
2719 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_addnl_diag_query))
2720 ret = _ctl_addnl_diag_query(ioc, arg);
2721 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302722 default:
Joe Perches919d8a32018-09-17 08:01:09 -07002723 dctlprintk(ioc,
2724 ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2725 cmd));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302726 break;
2727 }
2728
2729 mutex_unlock(&ioc->ctl_cmds.mutex);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302730out_unlock_pciaccess:
2731 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302732 return ret;
2733}
2734
2735/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302736 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002737 * @file: (struct file)
2738 * @cmd: ioctl opcode
2739 * @arg: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302740 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002741static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302742_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302743{
2744 long ret;
2745
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302746 /* pass MPI25_VERSION | MPI26_VERSION value,
2747 * to indicate that this ioctl cmd
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302748 * came from mpt3ctl ioctl device.
2749 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302750 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2751 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302752 return ret;
2753}
2754
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302755/**
2756 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002757 * @file: (struct file)
2758 * @cmd: ioctl opcode
2759 * @arg: ?
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302760 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002761static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302762_ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2763{
2764 long ret;
2765
2766 /* pass MPI2_VERSION value, to indicate that this ioctl cmd
2767 * came from mpt2ctl ioctl device.
2768 */
2769 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2770 return ret;
2771}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302772#ifdef CONFIG_COMPAT
2773/**
Lee Jones782a1ab2021-03-12 09:47:17 +00002774 * _ctl_ioctl_compat - main ioctl entry point (compat)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002775 * @file: ?
2776 * @cmd: ?
2777 * @arg: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302778 *
2779 * This routine handles 32 bit applications in 64bit os.
2780 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002781static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302782_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302783{
2784 long ret;
2785
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302786 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2787 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302788 return ret;
2789}
2790
2791/**
Lee Jones782a1ab2021-03-12 09:47:17 +00002792 * _ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002793 * @file: ?
2794 * @cmd: ?
2795 * @arg: ?
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302796 *
2797 * This routine handles 32 bit applications in 64bit os.
2798 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002799static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302800_ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2801{
2802 long ret;
2803
2804 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302805 return ret;
2806}
2807#endif
2808
2809/* scsi host attributes */
2810/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002811 * version_fw_show - firmware version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002812 * @cdev: pointer to embedded class device
2813 * @attr: ?
2814 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302815 *
2816 * A sysfs 'read-only' shost attribute.
2817 */
2818static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002819version_fw_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302820 char *buf)
2821{
2822 struct Scsi_Host *shost = class_to_shost(cdev);
2823 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2824
2825 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2826 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2827 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2828 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2829 ioc->facts.FWVersion.Word & 0x000000FF);
2830}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002831static DEVICE_ATTR_RO(version_fw);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302832
2833/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002834 * version_bios_show - bios version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002835 * @cdev: pointer to embedded class device
2836 * @attr: ?
2837 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302838 *
2839 * A sysfs 'read-only' shost attribute.
2840 */
2841static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002842version_bios_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302843 char *buf)
2844{
2845 struct Scsi_Host *shost = class_to_shost(cdev);
2846 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2847
2848 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2849
2850 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2851 (version & 0xFF000000) >> 24,
2852 (version & 0x00FF0000) >> 16,
2853 (version & 0x0000FF00) >> 8,
2854 version & 0x000000FF);
2855}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002856static DEVICE_ATTR_RO(version_bios);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302857
2858/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002859 * version_mpi_show - MPI (message passing interface) version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002860 * @cdev: pointer to embedded class device
2861 * @attr: ?
2862 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302863 *
2864 * A sysfs 'read-only' shost attribute.
2865 */
2866static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002867version_mpi_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302868 char *buf)
2869{
2870 struct Scsi_Host *shost = class_to_shost(cdev);
2871 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2872
2873 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2874 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2875}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002876static DEVICE_ATTR_RO(version_mpi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302877
2878/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002879 * version_product_show - product name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002880 * @cdev: pointer to embedded class device
2881 * @attr: ?
2882 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302883 *
2884 * A sysfs 'read-only' shost attribute.
2885 */
2886static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002887version_product_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302888 char *buf)
2889{
2890 struct Scsi_Host *shost = class_to_shost(cdev);
2891 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2892
2893 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2894}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002895static DEVICE_ATTR_RO(version_product);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302896
2897/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002898 * version_nvdata_persistent_show - ndvata persistent version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002899 * @cdev: pointer to embedded class device
2900 * @attr: ?
2901 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302902 *
2903 * A sysfs 'read-only' shost attribute.
2904 */
2905static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002906version_nvdata_persistent_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302907 struct device_attribute *attr, char *buf)
2908{
2909 struct Scsi_Host *shost = class_to_shost(cdev);
2910 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2911
2912 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2913 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2914}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002915static DEVICE_ATTR_RO(version_nvdata_persistent);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302916
2917/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002918 * version_nvdata_default_show - nvdata default version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002919 * @cdev: pointer to embedded class device
2920 * @attr: ?
2921 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302922 *
2923 * A sysfs 'read-only' shost attribute.
2924 */
2925static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002926version_nvdata_default_show(struct device *cdev, struct device_attribute
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302927 *attr, char *buf)
2928{
2929 struct Scsi_Host *shost = class_to_shost(cdev);
2930 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2931
2932 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2933 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2934}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002935static DEVICE_ATTR_RO(version_nvdata_default);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302936
2937/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002938 * board_name_show - board name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002939 * @cdev: pointer to embedded class device
2940 * @attr: ?
2941 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302942 *
2943 * A sysfs 'read-only' shost attribute.
2944 */
2945static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002946board_name_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302947 char *buf)
2948{
2949 struct Scsi_Host *shost = class_to_shost(cdev);
2950 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2951
2952 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2953}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002954static DEVICE_ATTR_RO(board_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302955
2956/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002957 * board_assembly_show - board assembly name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002958 * @cdev: pointer to embedded class device
2959 * @attr: ?
2960 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302961 *
2962 * A sysfs 'read-only' shost attribute.
2963 */
2964static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002965board_assembly_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302966 char *buf)
2967{
2968 struct Scsi_Host *shost = class_to_shost(cdev);
2969 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2970
2971 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2972}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002973static DEVICE_ATTR_RO(board_assembly);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302974
2975/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002976 * board_tracer_show - board tracer number
Bart Van Assche4beb4862018-06-15 14:42:01 -07002977 * @cdev: pointer to embedded class device
2978 * @attr: ?
2979 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302980 *
2981 * A sysfs 'read-only' shost attribute.
2982 */
2983static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002984board_tracer_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302985 char *buf)
2986{
2987 struct Scsi_Host *shost = class_to_shost(cdev);
2988 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2989
2990 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2991}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002992static DEVICE_ATTR_RO(board_tracer);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302993
2994/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002995 * io_delay_show - io missing delay
Bart Van Assche4beb4862018-06-15 14:42:01 -07002996 * @cdev: pointer to embedded class device
2997 * @attr: ?
2998 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302999 *
3000 * This is for firmware implemention for deboucing device
3001 * removal events.
3002 *
3003 * A sysfs 'read-only' shost attribute.
3004 */
3005static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003006io_delay_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303007 char *buf)
3008{
3009 struct Scsi_Host *shost = class_to_shost(cdev);
3010 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3011
3012 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
3013}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003014static DEVICE_ATTR_RO(io_delay);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303015
3016/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003017 * device_delay_show - device missing delay
Bart Van Assche4beb4862018-06-15 14:42:01 -07003018 * @cdev: pointer to embedded class device
3019 * @attr: ?
3020 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303021 *
3022 * This is for firmware implemention for deboucing device
3023 * removal events.
3024 *
3025 * A sysfs 'read-only' shost attribute.
3026 */
3027static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003028device_delay_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303029 char *buf)
3030{
3031 struct Scsi_Host *shost = class_to_shost(cdev);
3032 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3033
3034 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
3035}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003036static DEVICE_ATTR_RO(device_delay);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303037
3038/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003039 * fw_queue_depth_show - global credits
Bart Van Assche4beb4862018-06-15 14:42:01 -07003040 * @cdev: pointer to embedded class device
3041 * @attr: ?
3042 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303043 *
3044 * This is firmware queue depth limit
3045 *
3046 * A sysfs 'read-only' shost attribute.
3047 */
3048static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003049fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303050 char *buf)
3051{
3052 struct Scsi_Host *shost = class_to_shost(cdev);
3053 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3054
3055 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
3056}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003057static DEVICE_ATTR_RO(fw_queue_depth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303058
3059/**
Lee Jones782a1ab2021-03-12 09:47:17 +00003060 * host_sas_address_show - sas address
Bart Van Assche4beb4862018-06-15 14:42:01 -07003061 * @cdev: pointer to embedded class device
3062 * @attr: ?
3063 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303064 *
3065 * This is the controller sas address
3066 *
3067 * A sysfs 'read-only' shost attribute.
3068 */
3069static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003070host_sas_address_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303071 char *buf)
3072
3073{
3074 struct Scsi_Host *shost = class_to_shost(cdev);
3075 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3076
3077 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3078 (unsigned long long)ioc->sas_hba.sas_address);
3079}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003080static DEVICE_ATTR_RO(host_sas_address);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303081
3082/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003083 * logging_level_show - logging level
Bart Van Assche4beb4862018-06-15 14:42:01 -07003084 * @cdev: pointer to embedded class device
3085 * @attr: ?
3086 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303087 *
3088 * A sysfs 'read/write' shost attribute.
3089 */
3090static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003091logging_level_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303092 char *buf)
3093{
3094 struct Scsi_Host *shost = class_to_shost(cdev);
3095 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3096
3097 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
3098}
3099static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003100logging_level_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303101 const char *buf, size_t count)
3102{
3103 struct Scsi_Host *shost = class_to_shost(cdev);
3104 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3105 int val = 0;
3106
3107 if (sscanf(buf, "%x", &val) != 1)
3108 return -EINVAL;
3109
3110 ioc->logging_level = val;
Joe Perches919d8a32018-09-17 08:01:09 -07003111 ioc_info(ioc, "logging_level=%08xh\n",
3112 ioc->logging_level);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303113 return strlen(buf);
3114}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003115static DEVICE_ATTR_RW(logging_level);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303116
3117/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003118 * fwfault_debug_show - show/store fwfault_debug
Bart Van Assche4beb4862018-06-15 14:42:01 -07003119 * @cdev: pointer to embedded class device
3120 * @attr: ?
3121 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303122 *
3123 * mpt3sas_fwfault_debug is command line option
3124 * A sysfs 'read/write' shost attribute.
3125 */
3126static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003127fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303128 char *buf)
3129{
3130 struct Scsi_Host *shost = class_to_shost(cdev);
3131 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3132
3133 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
3134}
3135static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003136fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303137 const char *buf, size_t count)
3138{
3139 struct Scsi_Host *shost = class_to_shost(cdev);
3140 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3141 int val = 0;
3142
3143 if (sscanf(buf, "%d", &val) != 1)
3144 return -EINVAL;
3145
3146 ioc->fwfault_debug = val;
Joe Perches919d8a32018-09-17 08:01:09 -07003147 ioc_info(ioc, "fwfault_debug=%d\n",
3148 ioc->fwfault_debug);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303149 return strlen(buf);
3150}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003151static DEVICE_ATTR_RW(fwfault_debug);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303152
3153/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003154 * ioc_reset_count_show - ioc reset count
Bart Van Assche4beb4862018-06-15 14:42:01 -07003155 * @cdev: pointer to embedded class device
3156 * @attr: ?
3157 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303158 *
3159 * This is firmware queue depth limit
3160 *
3161 * A sysfs 'read-only' shost attribute.
3162 */
3163static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003164ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303165 char *buf)
3166{
3167 struct Scsi_Host *shost = class_to_shost(cdev);
3168 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3169
3170 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
3171}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003172static DEVICE_ATTR_RO(ioc_reset_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303173
3174/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003175 * reply_queue_count_show - number of reply queues
Bart Van Assche4beb4862018-06-15 14:42:01 -07003176 * @cdev: pointer to embedded class device
3177 * @attr: ?
3178 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303179 *
3180 * This is number of reply queues
3181 *
3182 * A sysfs 'read-only' shost attribute.
3183 */
3184static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003185reply_queue_count_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303186 struct device_attribute *attr, char *buf)
3187{
3188 u8 reply_queue_count;
3189 struct Scsi_Host *shost = class_to_shost(cdev);
3190 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3191
3192 if ((ioc->facts.IOCCapabilities &
3193 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
3194 reply_queue_count = ioc->reply_queue_count;
3195 else
3196 reply_queue_count = 1;
3197
3198 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
3199}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003200static DEVICE_ATTR_RO(reply_queue_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303201
Sreekanth Reddy42263092015-11-11 17:30:29 +05303202/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003203 * BRM_status_show - Backup Rail Monitor Status
Bart Van Assche4beb4862018-06-15 14:42:01 -07003204 * @cdev: pointer to embedded class device
3205 * @attr: ?
3206 * @buf: the buffer returned
Sreekanth Reddy42263092015-11-11 17:30:29 +05303207 *
3208 * This is number of reply queues
3209 *
3210 * A sysfs 'read-only' shost attribute.
3211 */
3212static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003213BRM_status_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddy42263092015-11-11 17:30:29 +05303214 char *buf)
3215{
3216 struct Scsi_Host *shost = class_to_shost(cdev);
3217 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
Gustavo A. R. Silvaa1c4d772021-03-10 17:59:51 -06003218 Mpi2IOUnitPage3_t io_unit_pg3;
Sreekanth Reddy42263092015-11-11 17:30:29 +05303219 Mpi2ConfigReply_t mpi_reply;
3220 u16 backup_rail_monitor_status = 0;
3221 u16 ioc_status;
3222 int sz;
3223 ssize_t rc = 0;
3224
3225 if (!ioc->is_warpdrive) {
Joe Perches919d8a32018-09-17 08:01:09 -07003226 ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
3227 __func__);
Damien Le Moalcb551b82020-07-01 17:52:54 +09003228 return 0;
Sreekanth Reddy42263092015-11-11 17:30:29 +05303229 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05303230 /* pci_access_mutex lock acquired by sysfs show path */
3231 mutex_lock(&ioc->pci_access_mutex);
Johannes Thumshirn0fd18142020-07-01 22:14:54 +09003232 if (ioc->pci_error_recovery || ioc->remove_host)
3233 goto out;
Sreekanth Reddy42263092015-11-11 17:30:29 +05303234
Gustavo A. R. Silvaa1c4d772021-03-10 17:59:51 -06003235 sz = sizeof(io_unit_pg3);
3236 memset(&io_unit_pg3, 0, sz);
Sreekanth Reddy42263092015-11-11 17:30:29 +05303237
Gustavo A. R. Silvaa1c4d772021-03-10 17:59:51 -06003238 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, &io_unit_pg3, sz) !=
Sreekanth Reddy42263092015-11-11 17:30:29 +05303239 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003240 ioc_err(ioc, "%s: failed reading iounit_pg3\n",
3241 __func__);
Johannes Thumshirn0fd18142020-07-01 22:14:54 +09003242 rc = -EINVAL;
Sreekanth Reddy42263092015-11-11 17:30:29 +05303243 goto out;
3244 }
3245
3246 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3247 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
Joe Perches919d8a32018-09-17 08:01:09 -07003248 ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
3249 __func__, ioc_status);
Johannes Thumshirn0fd18142020-07-01 22:14:54 +09003250 rc = -EINVAL;
Sreekanth Reddy42263092015-11-11 17:30:29 +05303251 goto out;
3252 }
3253
Gustavo A. R. Silvaa1c4d772021-03-10 17:59:51 -06003254 if (io_unit_pg3.GPIOCount < 25) {
3255 ioc_err(ioc, "%s: iounit_pg3.GPIOCount less than 25 entries, detected (%d) entries\n",
3256 __func__, io_unit_pg3.GPIOCount);
Johannes Thumshirn0fd18142020-07-01 22:14:54 +09003257 rc = -EINVAL;
Sreekanth Reddy42263092015-11-11 17:30:29 +05303258 goto out;
3259 }
3260
3261 /* BRM status is in bit zero of GPIOVal[24] */
Gustavo A. R. Silvaa1c4d772021-03-10 17:59:51 -06003262 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3.GPIOVal[24]);
Sreekanth Reddy42263092015-11-11 17:30:29 +05303263 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
3264
3265 out:
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05303266 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddy42263092015-11-11 17:30:29 +05303267 return rc;
3268}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003269static DEVICE_ATTR_RO(BRM_status);
Sreekanth Reddy42263092015-11-11 17:30:29 +05303270
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303271struct DIAG_BUFFER_START {
3272 __le32 Size;
3273 __le32 DiagVersion;
3274 u8 BufferType;
3275 u8 Reserved[3];
3276 __le32 Reserved1;
3277 __le32 Reserved2;
3278 __le32 Reserved3;
3279};
3280
3281/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003282 * host_trace_buffer_size_show - host buffer size (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07003283 * @cdev: pointer to embedded class device
3284 * @attr: ?
3285 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303286 *
3287 * A sysfs 'read-only' shost attribute.
3288 */
3289static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003290host_trace_buffer_size_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303291 struct device_attribute *attr, char *buf)
3292{
3293 struct Scsi_Host *shost = class_to_shost(cdev);
3294 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3295 u32 size = 0;
3296 struct DIAG_BUFFER_START *request_data;
3297
3298 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
Joe Perches919d8a32018-09-17 08:01:09 -07003299 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3300 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303301 return 0;
3302 }
3303
3304 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3305 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003306 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3307 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303308 return 0;
3309 }
3310
3311 request_data = (struct DIAG_BUFFER_START *)
3312 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
3313 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
3314 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
3315 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
3316 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
3317 size = le32_to_cpu(request_data->Size);
3318
3319 ioc->ring_buffer_sz = size;
3320 return snprintf(buf, PAGE_SIZE, "%d\n", size);
3321}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003322static DEVICE_ATTR_RO(host_trace_buffer_size);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303323
3324/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003325 * host_trace_buffer_show - firmware ring buffer (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07003326 * @cdev: pointer to embedded class device
3327 * @attr: ?
3328 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303329 *
3330 * A sysfs 'read/write' shost attribute.
3331 *
3332 * You will only be able to read 4k bytes of ring buffer at a time.
3333 * In order to read beyond 4k bytes, you will have to write out the
3334 * offset to the same attribute, it will move the pointer.
3335 */
3336static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003337host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303338 char *buf)
3339{
3340 struct Scsi_Host *shost = class_to_shost(cdev);
3341 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3342 void *request_data;
3343 u32 size;
3344
3345 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
Joe Perches919d8a32018-09-17 08:01:09 -07003346 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3347 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303348 return 0;
3349 }
3350
3351 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3352 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003353 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3354 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303355 return 0;
3356 }
3357
3358 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3359 return 0;
3360
3361 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3362 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3363 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3364 memcpy(buf, request_data, size);
3365 return size;
3366}
3367
3368static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003369host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303370 const char *buf, size_t count)
3371{
3372 struct Scsi_Host *shost = class_to_shost(cdev);
3373 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3374 int val = 0;
3375
3376 if (sscanf(buf, "%d", &val) != 1)
3377 return -EINVAL;
3378
3379 ioc->ring_buffer_offset = val;
3380 return strlen(buf);
3381}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003382static DEVICE_ATTR_RW(host_trace_buffer);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303383
3384
3385/*****************************************/
3386
3387/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003388 * host_trace_buffer_enable_show - firmware ring buffer (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07003389 * @cdev: pointer to embedded class device
3390 * @attr: ?
3391 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303392 *
3393 * A sysfs 'read/write' shost attribute.
3394 *
3395 * This is a mechnism to post/release host_trace_buffers
3396 */
3397static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003398host_trace_buffer_enable_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303399 struct device_attribute *attr, char *buf)
3400{
3401 struct Scsi_Host *shost = class_to_shost(cdev);
3402 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3403
3404 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3405 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3406 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3407 return snprintf(buf, PAGE_SIZE, "off\n");
3408 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3409 MPT3_DIAG_BUFFER_IS_RELEASED))
3410 return snprintf(buf, PAGE_SIZE, "release\n");
3411 else
3412 return snprintf(buf, PAGE_SIZE, "post\n");
3413}
3414
3415static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003416host_trace_buffer_enable_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303417 struct device_attribute *attr, const char *buf, size_t count)
3418{
3419 struct Scsi_Host *shost = class_to_shost(cdev);
3420 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3421 char str[10] = "";
3422 struct mpt3_diag_register diag_register;
3423 u8 issue_reset = 0;
3424
3425 /* don't allow post/release occurr while recovery is active */
3426 if (ioc->shost_recovery || ioc->remove_host ||
3427 ioc->pci_error_recovery || ioc->is_driver_loading)
3428 return -EBUSY;
3429
3430 if (sscanf(buf, "%9s", str) != 1)
3431 return -EINVAL;
3432
3433 if (!strcmp(str, "post")) {
3434 /* exit out if host buffers are already posted */
3435 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3436 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3437 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3438 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3439 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3440 goto out;
3441 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
Joe Perches919d8a32018-09-17 08:01:09 -07003442 ioc_info(ioc, "posting host trace buffers\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303443 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04003444
3445 if (ioc->manu_pg11.HostTraceBufferMaxSizeKB != 0 &&
3446 ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0) {
3447 /* post the same buffer allocated previously */
3448 diag_register.requested_buffer_size =
3449 ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE];
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04003450 } else {
3451 /*
3452 * Free the diag buffer memory which was previously
3453 * allocated by an application.
3454 */
3455 if ((ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0)
3456 &&
3457 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3458 MPT3_DIAG_BUFFER_IS_APP_OWNED)) {
Suraj Upadhyaya5a20c42020-07-29 23:40:21 +05303459 dma_free_coherent(&ioc->pdev->dev,
3460 ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE],
3461 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE],
3462 ioc->diag_buffer_dma[MPI2_DIAG_BUF_TYPE_TRACE]);
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04003463 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE] =
3464 NULL;
3465 }
3466
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04003467 diag_register.requested_buffer_size = (1024 * 1024);
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04003468 }
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04003469
Sreekanth Reddy08e73782019-09-13 09:04:42 -04003470 diag_register.unique_id =
3471 (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
3472 (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303473 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3474 _ctl_diag_register_2(ioc, &diag_register);
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04003475 if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3476 MPT3_DIAG_BUFFER_IS_REGISTERED) {
3477 ioc_info(ioc,
3478 "Trace buffer %d KB allocated through sysfs\n",
3479 diag_register.requested_buffer_size>>10);
3480 if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
3481 ioc->diag_buffer_status[
3482 MPI2_DIAG_BUF_TYPE_TRACE] |=
3483 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
3484 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303485 } else if (!strcmp(str, "release")) {
3486 /* exit out if host buffers are already released */
3487 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3488 goto out;
3489 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3490 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3491 goto out;
3492 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3493 MPT3_DIAG_BUFFER_IS_RELEASED))
3494 goto out;
Joe Perches919d8a32018-09-17 08:01:09 -07003495 ioc_info(ioc, "releasing host trace buffer\n");
Suganath Prabu S688c1a02021-02-04 09:07:23 +05303496 ioc->htb_rel.buffer_rel_condition = MPT3_DIAG_BUFFER_REL_SYSFS;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303497 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3498 &issue_reset);
3499 }
3500
3501 out:
3502 return strlen(buf);
3503}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003504static DEVICE_ATTR_RW(host_trace_buffer_enable);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303505
3506/*********** diagnostic trigger suppport *********************************/
3507
3508/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003509 * diag_trigger_master_show - show the diag_trigger_master attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003510 * @cdev: pointer to embedded class device
3511 * @attr: ?
3512 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303513 *
3514 * A sysfs 'read/write' shost attribute.
3515 */
3516static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003517diag_trigger_master_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303518 struct device_attribute *attr, char *buf)
3519
3520{
3521 struct Scsi_Host *shost = class_to_shost(cdev);
3522 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3523 unsigned long flags;
3524 ssize_t rc;
3525
3526 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3527 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3528 memcpy(buf, &ioc->diag_trigger_master, rc);
3529 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3530 return rc;
3531}
3532
3533/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003534 * diag_trigger_master_store - store the diag_trigger_master attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003535 * @cdev: pointer to embedded class device
3536 * @attr: ?
3537 * @buf: the buffer returned
3538 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303539 *
3540 * A sysfs 'read/write' shost attribute.
3541 */
3542static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003543diag_trigger_master_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303544 struct device_attribute *attr, const char *buf, size_t count)
3545
3546{
3547 struct Scsi_Host *shost = class_to_shost(cdev);
3548 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
Suganath Prabu S9211faa2021-12-27 11:00:55 +05303549 struct SL_WH_MASTER_TRIGGER_T *master_tg;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303550 unsigned long flags;
3551 ssize_t rc;
Suganath Prabu S9211faa2021-12-27 11:00:55 +05303552 bool set = 1;
3553
3554 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3555
3556 if (ioc->supports_trigger_pages) {
3557 master_tg = kzalloc(sizeof(struct SL_WH_MASTER_TRIGGER_T),
3558 GFP_KERNEL);
3559 if (!master_tg)
3560 return -ENOMEM;
3561
3562 memcpy(master_tg, buf, rc);
3563 if (!master_tg->MasterData)
3564 set = 0;
3565 if (mpt3sas_config_update_driver_trigger_pg1(ioc, master_tg,
3566 set)) {
3567 kfree(master_tg);
3568 return -EFAULT;
3569 }
3570 kfree(master_tg);
3571 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303572
3573 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303574 memset(&ioc->diag_trigger_master, 0,
3575 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3576 memcpy(&ioc->diag_trigger_master, buf, rc);
3577 ioc->diag_trigger_master.MasterData |=
3578 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3579 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3580 return rc;
3581}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003582static DEVICE_ATTR_RW(diag_trigger_master);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303583
3584
3585/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003586 * diag_trigger_event_show - show the diag_trigger_event attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003587 * @cdev: pointer to embedded class device
3588 * @attr: ?
3589 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303590 *
3591 * A sysfs 'read/write' shost attribute.
3592 */
3593static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003594diag_trigger_event_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303595 struct device_attribute *attr, char *buf)
3596{
3597 struct Scsi_Host *shost = class_to_shost(cdev);
3598 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3599 unsigned long flags;
3600 ssize_t rc;
3601
3602 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3603 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3604 memcpy(buf, &ioc->diag_trigger_event, rc);
3605 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3606 return rc;
3607}
3608
3609/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003610 * diag_trigger_event_store - store the diag_trigger_event attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003611 * @cdev: pointer to embedded class device
3612 * @attr: ?
3613 * @buf: the buffer returned
3614 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303615 *
3616 * A sysfs 'read/write' shost attribute.
3617 */
3618static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003619diag_trigger_event_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303620 struct device_attribute *attr, const char *buf, size_t count)
3621
3622{
3623 struct Scsi_Host *shost = class_to_shost(cdev);
3624 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
Suganath Prabu S9211faa2021-12-27 11:00:55 +05303625 struct SL_WH_EVENT_TRIGGERS_T *event_tg;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303626 unsigned long flags;
3627 ssize_t sz;
Suganath Prabu S9211faa2021-12-27 11:00:55 +05303628 bool set = 1;
3629
3630 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3631 if (ioc->supports_trigger_pages) {
3632 event_tg = kzalloc(sizeof(struct SL_WH_EVENT_TRIGGERS_T),
3633 GFP_KERNEL);
3634 if (!event_tg)
3635 return -ENOMEM;
3636
3637 memcpy(event_tg, buf, sz);
3638 if (!event_tg->ValidEntries)
3639 set = 0;
3640 if (mpt3sas_config_update_driver_trigger_pg2(ioc, event_tg,
3641 set)) {
3642 kfree(event_tg);
3643 return -EFAULT;
3644 }
3645 kfree(event_tg);
3646 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303647
3648 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
Suganath Prabu S9211faa2021-12-27 11:00:55 +05303649
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303650 memset(&ioc->diag_trigger_event, 0,
3651 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3652 memcpy(&ioc->diag_trigger_event, buf, sz);
3653 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3654 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3655 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3656 return sz;
3657}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003658static DEVICE_ATTR_RW(diag_trigger_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303659
3660
3661/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003662 * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003663 * @cdev: pointer to embedded class device
3664 * @attr: ?
3665 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303666 *
3667 * A sysfs 'read/write' shost attribute.
3668 */
3669static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003670diag_trigger_scsi_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303671 struct device_attribute *attr, char *buf)
3672{
3673 struct Scsi_Host *shost = class_to_shost(cdev);
3674 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3675 unsigned long flags;
3676 ssize_t rc;
3677
3678 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3679 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3680 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3681 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3682 return rc;
3683}
3684
3685/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003686 * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003687 * @cdev: pointer to embedded class device
3688 * @attr: ?
3689 * @buf: the buffer returned
3690 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303691 *
3692 * A sysfs 'read/write' shost attribute.
3693 */
3694static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003695diag_trigger_scsi_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303696 struct device_attribute *attr, const char *buf, size_t count)
3697{
3698 struct Scsi_Host *shost = class_to_shost(cdev);
3699 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
Suganath Prabu S9211faa2021-12-27 11:00:55 +05303700 struct SL_WH_SCSI_TRIGGERS_T *scsi_tg;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303701 unsigned long flags;
3702 ssize_t sz;
Suganath Prabu S9211faa2021-12-27 11:00:55 +05303703 bool set = 1;
3704
3705 sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
3706 if (ioc->supports_trigger_pages) {
3707 scsi_tg = kzalloc(sizeof(struct SL_WH_SCSI_TRIGGERS_T),
3708 GFP_KERNEL);
3709 if (!scsi_tg)
3710 return -ENOMEM;
3711
3712 memcpy(scsi_tg, buf, sz);
3713 if (!scsi_tg->ValidEntries)
3714 set = 0;
3715 if (mpt3sas_config_update_driver_trigger_pg3(ioc, scsi_tg,
3716 set)) {
3717 kfree(scsi_tg);
3718 return -EFAULT;
3719 }
3720 kfree(scsi_tg);
3721 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303722
3723 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
Suganath Prabu S9211faa2021-12-27 11:00:55 +05303724
Dan Carpenter1de540a2019-07-26 09:52:05 +03003725 memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303726 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3727 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3728 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3729 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3730 return sz;
3731}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003732static DEVICE_ATTR_RW(diag_trigger_scsi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303733
3734
3735/**
Lee Jones782a1ab2021-03-12 09:47:17 +00003736 * diag_trigger_mpi_show - show the diag_trigger_mpi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003737 * @cdev: pointer to embedded class device
3738 * @attr: ?
3739 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303740 *
3741 * A sysfs 'read/write' shost attribute.
3742 */
3743static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003744diag_trigger_mpi_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303745 struct device_attribute *attr, char *buf)
3746{
3747 struct Scsi_Host *shost = class_to_shost(cdev);
3748 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3749 unsigned long flags;
3750 ssize_t rc;
3751
3752 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3753 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3754 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3755 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3756 return rc;
3757}
3758
3759/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003760 * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003761 * @cdev: pointer to embedded class device
3762 * @attr: ?
3763 * @buf: the buffer returned
3764 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303765 *
3766 * A sysfs 'read/write' shost attribute.
3767 */
3768static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003769diag_trigger_mpi_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303770 struct device_attribute *attr, const char *buf, size_t count)
3771{
3772 struct Scsi_Host *shost = class_to_shost(cdev);
3773 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
Suganath Prabu S9211faa2021-12-27 11:00:55 +05303774 struct SL_WH_MPI_TRIGGERS_T *mpi_tg;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303775 unsigned long flags;
3776 ssize_t sz;
Suganath Prabu S9211faa2021-12-27 11:00:55 +05303777 bool set = 1;
3778
3779 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3780 if (ioc->supports_trigger_pages) {
3781 mpi_tg = kzalloc(sizeof(struct SL_WH_MPI_TRIGGERS_T),
3782 GFP_KERNEL);
3783 if (!mpi_tg)
3784 return -ENOMEM;
3785
3786 memcpy(mpi_tg, buf, sz);
3787 if (!mpi_tg->ValidEntries)
3788 set = 0;
3789 if (mpt3sas_config_update_driver_trigger_pg4(ioc, mpi_tg,
3790 set)) {
3791 kfree(mpi_tg);
3792 return -EFAULT;
3793 }
3794 kfree(mpi_tg);
3795 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303796
3797 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303798 memset(&ioc->diag_trigger_mpi, 0,
Dan Carpenter66331e82012-12-07 13:56:22 +03003799 sizeof(ioc->diag_trigger_mpi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303800 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3801 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3802 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3803 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3804 return sz;
3805}
3806
Tomas Henzlc9df1442019-06-14 16:41:44 +02003807static DEVICE_ATTR_RW(diag_trigger_mpi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303808
3809/*********** diagnostic trigger suppport *** END ****************************/
3810
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303811/*****************************************/
3812
Suganath Prabu3ac8e472019-08-03 09:59:53 -04003813/**
3814 * drv_support_bitmap_show - driver supported feature bitmap
Damien Le Moal9133d272020-07-06 21:33:58 +09003815 * @cdev: pointer to embedded class device
3816 * @attr: unused
3817 * @buf: the buffer returned
Suganath Prabu3ac8e472019-08-03 09:59:53 -04003818 *
3819 * A sysfs 'read-only' shost attribute.
3820 */
3821static ssize_t
3822drv_support_bitmap_show(struct device *cdev,
3823 struct device_attribute *attr, char *buf)
3824{
3825 struct Scsi_Host *shost = class_to_shost(cdev);
3826 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3827
3828 return snprintf(buf, PAGE_SIZE, "0x%08x\n", ioc->drv_support_bitmap);
3829}
3830static DEVICE_ATTR_RO(drv_support_bitmap);
3831
Sreekanth Reddy8dc8d292019-08-22 02:19:01 -04003832/**
3833 * enable_sdev_max_qd_show - display whether sdev max qd is enabled/disabled
Damien Le Moal9133d272020-07-06 21:33:58 +09003834 * @cdev: pointer to embedded class device
3835 * @attr: unused
3836 * @buf: the buffer returned
Sreekanth Reddy8dc8d292019-08-22 02:19:01 -04003837 *
3838 * A sysfs read/write shost attribute. This attribute is used to set the
3839 * targets queue depth to HBA IO queue depth if this attribute is enabled.
3840 */
3841static ssize_t
3842enable_sdev_max_qd_show(struct device *cdev,
3843 struct device_attribute *attr, char *buf)
3844{
3845 struct Scsi_Host *shost = class_to_shost(cdev);
3846 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3847
3848 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->enable_sdev_max_qd);
3849}
3850
3851/**
3852 * enable_sdev_max_qd_store - Enable/disable sdev max qd
Damien Le Moal9133d272020-07-06 21:33:58 +09003853 * @cdev: pointer to embedded class device
3854 * @attr: unused
3855 * @buf: the buffer returned
3856 * @count: unused
Sreekanth Reddy8dc8d292019-08-22 02:19:01 -04003857 *
3858 * A sysfs read/write shost attribute. This attribute is used to set the
3859 * targets queue depth to HBA IO queue depth if this attribute is enabled.
3860 * If this attribute is disabled then targets will have corresponding default
3861 * queue depth.
3862 */
3863static ssize_t
3864enable_sdev_max_qd_store(struct device *cdev,
3865 struct device_attribute *attr, const char *buf, size_t count)
3866{
3867 struct Scsi_Host *shost = class_to_shost(cdev);
3868 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3869 struct MPT3SAS_DEVICE *sas_device_priv_data;
3870 struct MPT3SAS_TARGET *sas_target_priv_data;
3871 int val = 0;
3872 struct scsi_device *sdev;
3873 struct _raid_device *raid_device;
3874 int qdepth;
3875
3876 if (kstrtoint(buf, 0, &val) != 0)
3877 return -EINVAL;
3878
3879 switch (val) {
3880 case 0:
3881 ioc->enable_sdev_max_qd = 0;
3882 shost_for_each_device(sdev, ioc->shost) {
3883 sas_device_priv_data = sdev->hostdata;
3884 if (!sas_device_priv_data)
3885 continue;
3886 sas_target_priv_data = sas_device_priv_data->sas_target;
3887 if (!sas_target_priv_data)
3888 continue;
3889
3890 if (sas_target_priv_data->flags &
3891 MPT_TARGET_FLAGS_VOLUME) {
3892 raid_device =
3893 mpt3sas_raid_device_find_by_handle(ioc,
3894 sas_target_priv_data->handle);
3895
3896 switch (raid_device->volume_type) {
3897 case MPI2_RAID_VOL_TYPE_RAID0:
3898 if (raid_device->device_info &
3899 MPI2_SAS_DEVICE_INFO_SSP_TARGET)
3900 qdepth =
3901 MPT3SAS_SAS_QUEUE_DEPTH;
3902 else
3903 qdepth =
3904 MPT3SAS_SATA_QUEUE_DEPTH;
3905 break;
3906 case MPI2_RAID_VOL_TYPE_RAID1E:
3907 case MPI2_RAID_VOL_TYPE_RAID1:
3908 case MPI2_RAID_VOL_TYPE_RAID10:
3909 case MPI2_RAID_VOL_TYPE_UNKNOWN:
3910 default:
3911 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
3912 }
3913 } else if (sas_target_priv_data->flags &
3914 MPT_TARGET_FLAGS_PCIE_DEVICE)
Suganath Prabu S787f2442021-08-09 12:56:38 +05303915 qdepth = ioc->max_nvme_qd;
Sreekanth Reddy8dc8d292019-08-22 02:19:01 -04003916 else
Suganath Prabu S787f2442021-08-09 12:56:38 +05303917 qdepth = (sas_target_priv_data->sas_dev->port_type > 1) ?
3918 ioc->max_wideport_qd : ioc->max_narrowport_qd;
Sreekanth Reddy8dc8d292019-08-22 02:19:01 -04003919
3920 mpt3sas_scsih_change_queue_depth(sdev, qdepth);
3921 }
3922 break;
3923 case 1:
3924 ioc->enable_sdev_max_qd = 1;
3925 shost_for_each_device(sdev, ioc->shost)
3926 mpt3sas_scsih_change_queue_depth(sdev,
3927 shost->can_queue);
3928 break;
3929 default:
3930 return -EINVAL;
3931 }
3932
3933 return strlen(buf);
3934}
3935static DEVICE_ATTR_RW(enable_sdev_max_qd);
3936
Bart Van Assche1bb3ca22021-10-12 16:35:41 -07003937static struct attribute *mpt3sas_host_attrs[] = {
3938 &dev_attr_version_fw.attr,
3939 &dev_attr_version_bios.attr,
3940 &dev_attr_version_mpi.attr,
3941 &dev_attr_version_product.attr,
3942 &dev_attr_version_nvdata_persistent.attr,
3943 &dev_attr_version_nvdata_default.attr,
3944 &dev_attr_board_name.attr,
3945 &dev_attr_board_assembly.attr,
3946 &dev_attr_board_tracer.attr,
3947 &dev_attr_io_delay.attr,
3948 &dev_attr_device_delay.attr,
3949 &dev_attr_logging_level.attr,
3950 &dev_attr_fwfault_debug.attr,
3951 &dev_attr_fw_queue_depth.attr,
3952 &dev_attr_host_sas_address.attr,
3953 &dev_attr_ioc_reset_count.attr,
3954 &dev_attr_host_trace_buffer_size.attr,
3955 &dev_attr_host_trace_buffer.attr,
3956 &dev_attr_host_trace_buffer_enable.attr,
3957 &dev_attr_reply_queue_count.attr,
3958 &dev_attr_diag_trigger_master.attr,
3959 &dev_attr_diag_trigger_event.attr,
3960 &dev_attr_diag_trigger_scsi.attr,
3961 &dev_attr_diag_trigger_mpi.attr,
3962 &dev_attr_drv_support_bitmap.attr,
3963 &dev_attr_BRM_status.attr,
3964 &dev_attr_enable_sdev_max_qd.attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303965 NULL,
3966};
3967
Bart Van Assche1bb3ca22021-10-12 16:35:41 -07003968static const struct attribute_group mpt3sas_host_attr_group = {
3969 .attrs = mpt3sas_host_attrs
3970};
3971
3972const struct attribute_group *mpt3sas_host_groups[] = {
3973 &mpt3sas_host_attr_group,
3974 NULL
3975};
3976
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303977/* device attributes */
3978
3979/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003980 * sas_address_show - sas address
Bart Van Assche4beb4862018-06-15 14:42:01 -07003981 * @dev: pointer to embedded class device
3982 * @attr: ?
3983 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303984 *
3985 * This is the sas address for the target
3986 *
3987 * A sysfs 'read-only' shost attribute.
3988 */
3989static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003990sas_address_show(struct device *dev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303991 char *buf)
3992{
3993 struct scsi_device *sdev = to_scsi_device(dev);
3994 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3995
3996 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3997 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3998}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003999static DEVICE_ATTR_RO(sas_address);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304000
4001/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02004002 * sas_device_handle_show - device handle
Bart Van Assche4beb4862018-06-15 14:42:01 -07004003 * @dev: pointer to embedded class device
4004 * @attr: ?
4005 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304006 *
4007 * This is the firmware assigned device handle
4008 *
4009 * A sysfs 'read-only' shost attribute.
4010 */
4011static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02004012sas_device_handle_show(struct device *dev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304013 char *buf)
4014{
4015 struct scsi_device *sdev = to_scsi_device(dev);
4016 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4017
4018 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
4019 sas_device_priv_data->sas_target->handle);
4020}
Tomas Henzlc9df1442019-06-14 16:41:44 +02004021static DEVICE_ATTR_RO(sas_device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304022
Adam Manzanares307d9072016-12-12 16:31:40 -08004023/**
Damien Le Moal4758fd92021-08-07 13:18:59 +09004024 * sas_ncq_prio_supported_show - Indicate if device supports NCQ priority
4025 * @dev: pointer to embedded device
4026 * @attr: sas_ncq_prio_supported attribute descriptor
4027 * @buf: the buffer returned
4028 *
4029 * A sysfs 'read-only' sdev attribute, only works with SATA
4030 */
4031static ssize_t
4032sas_ncq_prio_supported_show(struct device *dev,
4033 struct device_attribute *attr, char *buf)
4034{
4035 struct scsi_device *sdev = to_scsi_device(dev);
4036
Damien Le Moalda097dc2024-06-11 17:34:35 +09004037 return sysfs_emit(buf, "%d\n", sas_ata_ncq_prio_supported(sdev));
Damien Le Moal4758fd92021-08-07 13:18:59 +09004038}
4039static DEVICE_ATTR_RO(sas_ncq_prio_supported);
4040
4041/**
Lee Jones782a1ab2021-03-12 09:47:17 +00004042 * sas_ncq_prio_enable_show - send prioritized io commands to device
Bart Van Assche4beb4862018-06-15 14:42:01 -07004043 * @dev: pointer to embedded device
4044 * @attr: ?
4045 * @buf: the buffer returned
Adam Manzanares307d9072016-12-12 16:31:40 -08004046 *
4047 * A sysfs 'read/write' sdev attribute, only works with SATA
4048 */
4049static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02004050sas_ncq_prio_enable_show(struct device *dev,
Adam Manzanares307d9072016-12-12 16:31:40 -08004051 struct device_attribute *attr, char *buf)
4052{
4053 struct scsi_device *sdev = to_scsi_device(dev);
4054 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4055
4056 return snprintf(buf, PAGE_SIZE, "%d\n",
4057 sas_device_priv_data->ncq_prio_enable);
4058}
4059
4060static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02004061sas_ncq_prio_enable_store(struct device *dev,
Adam Manzanares307d9072016-12-12 16:31:40 -08004062 struct device_attribute *attr,
4063 const char *buf, size_t count)
4064{
4065 struct scsi_device *sdev = to_scsi_device(dev);
4066 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4067 bool ncq_prio_enable = 0;
4068
4069 if (kstrtobool(buf, &ncq_prio_enable))
4070 return -EINVAL;
4071
Damien Le Moalda097dc2024-06-11 17:34:35 +09004072 if (!sas_ata_ncq_prio_supported(sdev))
Adam Manzanares307d9072016-12-12 16:31:40 -08004073 return -EINVAL;
4074
4075 sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
4076 return strlen(buf);
4077}
Tomas Henzlc9df1442019-06-14 16:41:44 +02004078static DEVICE_ATTR_RW(sas_ncq_prio_enable);
Adam Manzanares307d9072016-12-12 16:31:40 -08004079
Jiapeng Chong0ae8f472021-10-19 18:27:19 +08004080static struct attribute *mpt3sas_dev_attrs[] = {
Bart Van Assche1bb3ca22021-10-12 16:35:41 -07004081 &dev_attr_sas_address.attr,
4082 &dev_attr_sas_device_handle.attr,
4083 &dev_attr_sas_ncq_prio_supported.attr,
4084 &dev_attr_sas_ncq_prio_enable.attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304085 NULL,
4086};
4087
Bart Van Assche1bb3ca22021-10-12 16:35:41 -07004088static const struct attribute_group mpt3sas_dev_attr_group = {
4089 .attrs = mpt3sas_dev_attrs
4090};
4091
4092const struct attribute_group *mpt3sas_dev_groups[] = {
4093 &mpt3sas_dev_attr_group,
4094 NULL
4095};
4096
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304097/* file operations table for mpt3ctl device */
4098static const struct file_operations ctl_fops = {
4099 .owner = THIS_MODULE,
4100 .unlocked_ioctl = _ctl_ioctl,
4101 .poll = _ctl_poll,
4102 .fasync = _ctl_fasync,
4103#ifdef CONFIG_COMPAT
4104 .compat_ioctl = _ctl_ioctl_compat,
4105#endif
4106};
4107
4108/* file operations table for mpt2ctl device */
4109static const struct file_operations ctl_gen2_fops = {
4110 .owner = THIS_MODULE,
4111 .unlocked_ioctl = _ctl_mpt2_ioctl,
4112 .poll = _ctl_poll,
4113 .fasync = _ctl_fasync,
4114#ifdef CONFIG_COMPAT
4115 .compat_ioctl = _ctl_mpt2_ioctl_compat,
4116#endif
4117};
4118
4119static struct miscdevice ctl_dev = {
4120 .minor = MPT3SAS_MINOR,
4121 .name = MPT3SAS_DEV_NAME,
4122 .fops = &ctl_fops,
4123};
4124
4125static struct miscdevice gen2_ctl_dev = {
4126 .minor = MPT2SAS_MINOR,
4127 .name = MPT2SAS_DEV_NAME,
4128 .fops = &ctl_gen2_fops,
4129};
4130
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304131/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304132 * mpt3sas_ctl_init - main entry point for ctl.
Bart Van Assche4beb4862018-06-15 14:42:01 -07004133 * @hbas_to_enumerate: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304134 */
4135void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304136mpt3sas_ctl_init(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304137{
4138 async_queue = NULL;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304139
4140 /* Don't register mpt3ctl ioctl device if
4141 * hbas_to_enumarate is one.
4142 */
4143 if (hbas_to_enumerate != 1)
4144 if (misc_register(&ctl_dev) < 0)
4145 pr_err("%s can't register misc device [minor=%d]\n",
4146 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
4147
4148 /* Don't register mpt3ctl ioctl device if
4149 * hbas_to_enumarate is two.
4150 */
4151 if (hbas_to_enumerate != 2)
4152 if (misc_register(&gen2_ctl_dev) < 0)
4153 pr_err("%s can't register misc device [minor=%d]\n",
4154 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
4155
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304156 init_waitqueue_head(&ctl_poll_wait);
4157}
4158
4159/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304160 * mpt3sas_ctl_exit - exit point for ctl
Bart Van Assche4beb4862018-06-15 14:42:01 -07004161 * @hbas_to_enumerate: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304162 */
4163void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304164mpt3sas_ctl_exit(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304165{
4166 struct MPT3SAS_ADAPTER *ioc;
4167 int i;
4168
4169 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
4170
4171 /* free memory associated to diag buffers */
4172 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
4173 if (!ioc->diag_buffer[i])
4174 continue;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02004175 dma_free_coherent(&ioc->pdev->dev,
4176 ioc->diag_buffer_sz[i],
4177 ioc->diag_buffer[i],
4178 ioc->diag_buffer_dma[i]);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304179 ioc->diag_buffer[i] = NULL;
4180 ioc->diag_buffer_status[i] = 0;
4181 }
4182
4183 kfree(ioc->event_log);
4184 }
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304185 if (hbas_to_enumerate != 1)
4186 misc_deregister(&ctl_dev);
4187 if (hbas_to_enumerate != 2)
4188 misc_deregister(&gen2_ctl_dev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304189}