blob: 0f2b681449e64ab2bd5a8c8a4b7f35012458ba77 [file] [log] [blame]
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301/*
2 * Management Module Support for MPT (Message Passing Technology) based
3 * controllers
4 *
5 * This code is based on drivers/scsi/mpt3sas/mpt3sas_ctl.c
Sreekanth Reddya4ffce02014-09-12 15:35:29 +05306 * Copyright (C) 2012-2014 LSI Corporation
Sreekanth Reddya03bd152015-01-12 11:39:02 +05307 * Copyright (C) 2013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * NO WARRANTY
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
30
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
43 * USA.
44 */
45
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053046#include <linux/kernel.h>
47#include <linux/module.h>
48#include <linux/errno.h>
49#include <linux/init.h>
50#include <linux/slab.h>
51#include <linux/types.h>
52#include <linux/pci.h>
53#include <linux/delay.h>
54#include <linux/compat.h>
55#include <linux/poll.h>
56
57#include <linux/io.h>
58#include <linux/uaccess.h>
59
60#include "mpt3sas_base.h"
61#include "mpt3sas_ctl.h"
62
63
64static struct fasync_struct *async_queue;
65static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
66
67
68/**
69 * enum block_state - blocking state
70 * @NON_BLOCKING: non blocking
71 * @BLOCKING: blocking
72 *
73 * These states are for ioctls that need to wait for a response
74 * from firmware, so they probably require sleep.
75 */
76enum block_state {
77 NON_BLOCKING,
78 BLOCKING,
79};
80
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053081/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053082 * _ctl_display_some_debug - debug routine
83 * @ioc: per adapter object
84 * @smid: system request message index
85 * @calling_function_name: string pass from calling function
86 * @mpi_reply: reply message frame
87 * Context: none.
88 *
89 * Function for displaying debug info helpful when debugging issues
90 * in this module.
91 */
92static void
93_ctl_display_some_debug(struct MPT3SAS_ADAPTER *ioc, u16 smid,
94 char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
95{
96 Mpi2ConfigRequest_t *mpi_request;
97 char *desc = NULL;
98
99 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
100 return;
101
102 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
103 switch (mpi_request->Function) {
104 case MPI2_FUNCTION_SCSI_IO_REQUEST:
105 {
106 Mpi2SCSIIORequest_t *scsi_request =
107 (Mpi2SCSIIORequest_t *)mpi_request;
108
109 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
110 "scsi_io, cmd(0x%02x), cdb_len(%d)",
111 scsi_request->CDB.CDB32[0],
112 le16_to_cpu(scsi_request->IoFlags) & 0xF);
113 desc = ioc->tmp_string;
114 break;
115 }
116 case MPI2_FUNCTION_SCSI_TASK_MGMT:
117 desc = "task_mgmt";
118 break;
119 case MPI2_FUNCTION_IOC_INIT:
120 desc = "ioc_init";
121 break;
122 case MPI2_FUNCTION_IOC_FACTS:
123 desc = "ioc_facts";
124 break;
125 case MPI2_FUNCTION_CONFIG:
126 {
127 Mpi2ConfigRequest_t *config_request =
128 (Mpi2ConfigRequest_t *)mpi_request;
129
130 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
131 "config, type(0x%02x), ext_type(0x%02x), number(%d)",
132 (config_request->Header.PageType &
133 MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
134 config_request->Header.PageNumber);
135 desc = ioc->tmp_string;
136 break;
137 }
138 case MPI2_FUNCTION_PORT_FACTS:
139 desc = "port_facts";
140 break;
141 case MPI2_FUNCTION_PORT_ENABLE:
142 desc = "port_enable";
143 break;
144 case MPI2_FUNCTION_EVENT_NOTIFICATION:
145 desc = "event_notification";
146 break;
147 case MPI2_FUNCTION_FW_DOWNLOAD:
148 desc = "fw_download";
149 break;
150 case MPI2_FUNCTION_FW_UPLOAD:
151 desc = "fw_upload";
152 break;
153 case MPI2_FUNCTION_RAID_ACTION:
154 desc = "raid_action";
155 break;
156 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
157 {
158 Mpi2SCSIIORequest_t *scsi_request =
159 (Mpi2SCSIIORequest_t *)mpi_request;
160
161 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
162 "raid_pass, cmd(0x%02x), cdb_len(%d)",
163 scsi_request->CDB.CDB32[0],
164 le16_to_cpu(scsi_request->IoFlags) & 0xF);
165 desc = ioc->tmp_string;
166 break;
167 }
168 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
169 desc = "sas_iounit_cntl";
170 break;
171 case MPI2_FUNCTION_SATA_PASSTHROUGH:
172 desc = "sata_pass";
173 break;
174 case MPI2_FUNCTION_DIAG_BUFFER_POST:
175 desc = "diag_buffer_post";
176 break;
177 case MPI2_FUNCTION_DIAG_RELEASE:
178 desc = "diag_release";
179 break;
180 case MPI2_FUNCTION_SMP_PASSTHROUGH:
181 desc = "smp_passthrough";
182 break;
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/**
457 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
458 * @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 Reddy4bc50dc2019-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__);
Bart Van Asschec7a35702018-06-15 14:42:00 -0700482 mpt3sas_send_diag_release(ioc, i, &issue_reset);
483 }
484}
485
486/**
Sreekanth Reddy36c6c7f2019-12-26 06:13:26 -0500487 * mpt3sas_ctl_reset_handler - clears outstanding ioctl cmd.
Bart Van Asschec7a35702018-06-15 14:42:00 -0700488 * @ioc: per adapter object
489 *
490 * The handler for doing any required cleanup or initialization.
491 */
Sreekanth Reddy36c6c7f2019-12-26 06:13:26 -0500492void mpt3sas_ctl_clear_outstanding_ioctls(struct MPT3SAS_ADAPTER *ioc)
Bart Van Asschec7a35702018-06-15 14:42:00 -0700493{
Sreekanth Reddy36c6c7f2019-12-26 06:13:26 -0500494 dtmprintk(ioc,
495 ioc_info(ioc, "%s: clear outstanding ioctl cmd\n", __func__));
Bart Van Asschec7a35702018-06-15 14:42:00 -0700496 if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
497 ioc->ctl_cmds.status |= MPT3_CMD_RESET;
498 mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
499 complete(&ioc->ctl_cmds.done);
500 }
501}
502
503/**
504 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
505 * @ioc: per adapter object
506 *
507 * The handler for doing any required cleanup or initialization.
508 */
509void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
510{
511 int i;
512
Joe Perches919d8a32018-09-17 08:01:09 -0700513 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530514
Bart Van Asschec7a35702018-06-15 14:42:00 -0700515 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
516 if (!(ioc->diag_buffer_status[i] &
517 MPT3_DIAG_BUFFER_IS_REGISTERED))
518 continue;
519 if ((ioc->diag_buffer_status[i] &
520 MPT3_DIAG_BUFFER_IS_RELEASED))
521 continue;
522 ioc->diag_buffer_status[i] |=
523 MPT3_DIAG_BUFFER_IS_DIAG_RESET;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530524 }
525}
526
527/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530528 * _ctl_fasync -
Bart Van Assche4beb4862018-06-15 14:42:01 -0700529 * @fd: ?
530 * @filep: ?
531 * @mode: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530532 *
533 * Called when application request fasyn callback handler.
534 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -0700535static int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530536_ctl_fasync(int fd, struct file *filep, int mode)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530537{
538 return fasync_helper(fd, filep, mode, &async_queue);
539}
540
541/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530542 * _ctl_poll -
Bart Van Assche4beb4862018-06-15 14:42:01 -0700543 * @filep: ?
544 * @wait: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530545 *
546 */
Al Viroafc9a422017-07-03 06:39:46 -0400547static __poll_t
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530548_ctl_poll(struct file *filep, poll_table *wait)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530549{
550 struct MPT3SAS_ADAPTER *ioc;
551
552 poll_wait(filep, &ctl_poll_wait, wait);
553
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530554 /* global ioc lock to protect controller on list operations */
555 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530556 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530557 if (ioc->aen_event_read_flag) {
558 spin_unlock(&gioc_lock);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800559 return EPOLLIN | EPOLLRDNORM;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530560 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530561 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530562 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530563 return 0;
564}
565
566/**
567 * _ctl_set_task_mid - assign an active smid to tm request
568 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -0700569 * @karg: (struct mpt3_ioctl_command)
570 * @tm_request: pointer to mf from user space
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530571 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700572 * Return: 0 when an smid if found, else fail.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530573 * during failure, the reply frame is filled.
574 */
575static int
576_ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
577 Mpi2SCSITaskManagementRequest_t *tm_request)
578{
579 u8 found = 0;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800580 u16 smid;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530581 u16 handle;
582 struct scsi_cmnd *scmd;
583 struct MPT3SAS_DEVICE *priv_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530584 Mpi2SCSITaskManagementReply_t *tm_reply;
585 u32 sz;
586 u32 lun;
587 char *desc = NULL;
588
589 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
590 desc = "abort_task";
591 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
592 desc = "query_task";
593 else
594 return 0;
595
596 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
597
598 handle = le16_to_cpu(tm_request->DevHandle);
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800599 for (smid = ioc->scsiio_depth; smid && !found; smid--) {
600 struct scsiio_tracker *st;
601
602 scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
603 if (!scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530604 continue;
605 if (lun != scmd->device->lun)
606 continue;
607 priv_data = scmd->device->hostdata;
608 if (priv_data->sas_target == NULL)
609 continue;
610 if (priv_data->sas_target->handle != handle)
611 continue;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800612 st = scsi_cmd_priv(scmd);
Minwoo Im8f55c302019-07-28 03:53:37 +0900613
614 /*
615 * If the given TaskMID from the user space is zero, then the
616 * first outstanding smid will be picked up. Otherwise,
617 * targeted smid will be the one.
618 */
619 if (!tm_request->TaskMID || tm_request->TaskMID == st->smid) {
620 tm_request->TaskMID = cpu_to_le16(st->smid);
621 found = 1;
622 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530623 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530624
625 if (!found) {
Joe Perches919d8a32018-09-17 08:01:09 -0700626 dctlprintk(ioc,
627 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), no active mid!!\n",
628 desc, le16_to_cpu(tm_request->DevHandle),
629 lun));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530630 tm_reply = ioc->ctl_cmds.reply;
631 tm_reply->DevHandle = tm_request->DevHandle;
632 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
633 tm_reply->TaskType = tm_request->TaskType;
634 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
635 tm_reply->VP_ID = tm_request->VP_ID;
636 tm_reply->VF_ID = tm_request->VF_ID;
637 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
638 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
639 sz))
640 pr_err("failure at %s:%d/%s()!\n", __FILE__,
641 __LINE__, __func__);
642 return 1;
643 }
644
Joe Perches919d8a32018-09-17 08:01:09 -0700645 dctlprintk(ioc,
646 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), task_mid(%d)\n",
647 desc, le16_to_cpu(tm_request->DevHandle), lun,
648 le16_to_cpu(tm_request->TaskMID)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530649 return 0;
650}
651
652/**
653 * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
654 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -0700655 * @karg: (struct mpt3_ioctl_command)
656 * @mf: pointer to mf in user space
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530657 */
658static long
659_ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
660 void __user *mf)
661{
662 MPI2RequestHeader_t *mpi_request = NULL, *request;
663 MPI2DefaultReply_t *mpi_reply;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530664 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -0400665 struct _pcie_device *pcie_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530666 u16 smid;
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -0400667 u8 timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530668 u8 issue_reset;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530669 u32 sz, sz_arg;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530670 void *psge;
671 void *data_out = NULL;
672 dma_addr_t data_out_dma = 0;
673 size_t data_out_sz = 0;
674 void *data_in = NULL;
675 dma_addr_t data_in_dma = 0;
676 size_t data_in_sz = 0;
677 long ret;
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530678 u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530679
680 issue_reset = 0;
681
682 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -0700683 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530684 ret = -EAGAIN;
685 goto out;
686 }
687
Suganath Prabuf4305742018-10-31 18:53:33 +0530688 ret = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
689 if (ret)
690 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530691
692 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
693 if (!mpi_request) {
Joe Perches919d8a32018-09-17 08:01:09 -0700694 ioc_err(ioc, "%s: failed obtaining a memory for mpi_request\n",
695 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530696 ret = -ENOMEM;
697 goto out;
698 }
699
700 /* Check for overflow and wraparound */
701 if (karg.data_sge_offset * 4 > ioc->request_sz ||
702 karg.data_sge_offset > (UINT_MAX / 4)) {
703 ret = -EINVAL;
704 goto out;
705 }
706
707 /* copy in request message frame from user */
708 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
709 pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
710 __func__);
711 ret = -EFAULT;
712 goto out;
713 }
714
715 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
716 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
717 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -0700718 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530719 ret = -EAGAIN;
720 goto out;
721 }
722 } else {
Hannes Reineckeb0cd285e2018-01-04 04:57:07 -0800723 /* Use first reserved smid for passthrough ioctls */
724 smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530725 }
726
727 ret = 0;
728 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
729 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
730 request = mpt3sas_base_get_msg_frame(ioc, smid);
Suganath Prabue224e032019-08-03 09:59:47 -0400731 memset(request, 0, ioc->request_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530732 memcpy(request, mpi_request, karg.data_sge_offset*4);
733 ioc->ctl_cmds.smid = smid;
734 data_out_sz = karg.data_out_size;
735 data_in_sz = karg.data_in_size;
736
737 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530738 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
739 mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530740 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
741 mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530742
743 device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
744 if (!device_handle || (device_handle >
745 ioc->facts.MaxDevHandle)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530746 ret = -EINVAL;
747 mpt3sas_base_free_smid(ioc, smid);
748 goto out;
749 }
750 }
751
752 /* obtain dma-able memory for data transfer */
753 if (data_out_sz) /* WRITE */ {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +0200754 data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz,
755 &data_out_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530756 if (!data_out) {
757 pr_err("failure at %s:%d/%s()!\n", __FILE__,
758 __LINE__, __func__);
759 ret = -ENOMEM;
760 mpt3sas_base_free_smid(ioc, smid);
761 goto out;
762 }
763 if (copy_from_user(data_out, karg.data_out_buf_ptr,
764 data_out_sz)) {
765 pr_err("failure at %s:%d/%s()!\n", __FILE__,
766 __LINE__, __func__);
767 ret = -EFAULT;
768 mpt3sas_base_free_smid(ioc, smid);
769 goto out;
770 }
771 }
772
773 if (data_in_sz) /* READ */ {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +0200774 data_in = dma_alloc_coherent(&ioc->pdev->dev, data_in_sz,
775 &data_in_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530776 if (!data_in) {
777 pr_err("failure at %s:%d/%s()!\n", __FILE__,
778 __LINE__, __func__);
779 ret = -ENOMEM;
780 mpt3sas_base_free_smid(ioc, smid);
781 goto out;
782 }
783 }
784
785 psge = (void *)request + (karg.data_sge_offset*4);
786
787 /* send command to firmware */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530788 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530789
790 init_completion(&ioc->ctl_cmds.done);
791 switch (mpi_request->Function) {
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530792 case MPI2_FUNCTION_NVME_ENCAPSULATED:
793 {
794 nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
Sreekanth Reddy77fd4f22019-09-13 09:04:48 -0400795 if (!ioc->pcie_sg_lookup) {
796 dtmprintk(ioc, ioc_info(ioc,
797 "HBA doesn't support NVMe. Rejecting NVMe Encapsulated request.\n"
798 ));
799
800 if (ioc->logging_level & MPT_DEBUG_TM)
801 _debug_dump_mf(nvme_encap_request,
802 ioc->request_sz/4);
803 mpt3sas_base_free_smid(ioc, smid);
804 ret = -EINVAL;
805 goto out;
806 }
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530807 /*
808 * Get the Physical Address of the sense buffer.
809 * Use Error Response buffer address field to hold the sense
810 * buffer address.
811 * Clear the internal sense buffer, which will potentially hold
812 * the Completion Queue Entry on return, or 0 if no Entry.
813 * Build the PRPs and set direction bits.
814 * Send the request.
815 */
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400816 nvme_encap_request->ErrorResponseBaseAddress =
817 cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530818 nvme_encap_request->ErrorResponseBaseAddress |=
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400819 cpu_to_le64(le32_to_cpu(
820 mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530821 nvme_encap_request->ErrorResponseAllocationLength =
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400822 cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530823 memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
824 ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
825 data_out_dma, data_out_sz, data_in_dma, data_in_sz);
826 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700827 dtmprintk(ioc,
828 ioc_info(ioc, "handle(0x%04x): ioctl failed due to device removal in progress\n",
829 device_handle));
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530830 mpt3sas_base_free_smid(ioc, smid);
831 ret = -EINVAL;
832 goto out;
833 }
Suganath Prabu S40114bd2018-02-14 02:16:37 -0800834 mpt3sas_base_put_smid_nvme_encap(ioc, smid);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530835 break;
836 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530837 case MPI2_FUNCTION_SCSI_IO_REQUEST:
838 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
839 {
840 Mpi2SCSIIORequest_t *scsiio_request =
841 (Mpi2SCSIIORequest_t *)request;
842 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
843 scsiio_request->SenseBufferLowAddress =
844 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
845 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530846 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700847 dtmprintk(ioc,
848 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
849 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530850 mpt3sas_base_free_smid(ioc, smid);
851 ret = -EINVAL;
852 goto out;
853 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530854 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
855 data_in_dma, data_in_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530856 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +0530857 ioc->put_smid_scsi_io(ioc, smid, device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530858 else
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400859 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530860 break;
861 }
862 case MPI2_FUNCTION_SCSI_TASK_MGMT:
863 {
864 Mpi2SCSITaskManagementRequest_t *tm_request =
865 (Mpi2SCSITaskManagementRequest_t *)request;
866
Joe Perches919d8a32018-09-17 08:01:09 -0700867 dtmprintk(ioc,
868 ioc_info(ioc, "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
869 le16_to_cpu(tm_request->DevHandle),
870 tm_request->TaskType));
Chaitra P B459325c2017-01-23 15:26:08 +0530871 ioc->got_task_abort_from_ioctl = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530872 if (tm_request->TaskType ==
873 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
874 tm_request->TaskType ==
875 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
876 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
877 mpt3sas_base_free_smid(ioc, smid);
Chaitra P B459325c2017-01-23 15:26:08 +0530878 ioc->got_task_abort_from_ioctl = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530879 goto out;
880 }
881 }
Chaitra P B459325c2017-01-23 15:26:08 +0530882 ioc->got_task_abort_from_ioctl = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530883
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530884 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700885 dtmprintk(ioc,
886 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
887 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530888 mpt3sas_base_free_smid(ioc, smid);
889 ret = -EINVAL;
890 goto out;
891 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530892 mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
893 tm_request->DevHandle));
894 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
895 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400896 ioc->put_smid_hi_priority(ioc, smid, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530897 break;
898 }
899 case MPI2_FUNCTION_SMP_PASSTHROUGH:
900 {
901 Mpi2SmpPassthroughRequest_t *smp_request =
902 (Mpi2SmpPassthroughRequest_t *)mpi_request;
903 u8 *data;
904
905 /* ioc determines which port to use */
906 smp_request->PhysicalPort = 0xFF;
907 if (smp_request->PassthroughFlags &
908 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
909 data = (u8 *)&smp_request->SGL;
910 else {
911 if (unlikely(data_out == NULL)) {
912 pr_err("failure at %s:%d/%s()!\n",
913 __FILE__, __LINE__, __func__);
914 mpt3sas_base_free_smid(ioc, smid);
915 ret = -EINVAL;
916 goto out;
917 }
918 data = data_out;
919 }
920
921 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
922 ioc->ioc_link_reset_in_progress = 1;
923 ioc->ignore_loginfos = 1;
924 }
925 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
926 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400927 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530928 break;
929 }
930 case MPI2_FUNCTION_SATA_PASSTHROUGH:
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530931 {
932 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700933 dtmprintk(ioc,
934 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
935 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530936 mpt3sas_base_free_smid(ioc, smid);
937 ret = -EINVAL;
938 goto out;
939 }
940 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
941 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400942 ioc->put_smid_default(ioc, smid);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530943 break;
944 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530945 case MPI2_FUNCTION_FW_DOWNLOAD:
946 case MPI2_FUNCTION_FW_UPLOAD:
947 {
948 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
949 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400950 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530951 break;
952 }
953 case MPI2_FUNCTION_TOOLBOX:
954 {
955 Mpi2ToolboxCleanRequest_t *toolbox_request =
956 (Mpi2ToolboxCleanRequest_t *)mpi_request;
957
Suganath Prabuf23ca2c2019-08-03 09:59:46 -0400958 if ((toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
959 || (toolbox_request->Tool ==
960 MPI26_TOOLBOX_BACKEND_PCIE_LANE_MARGIN))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530961 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
962 data_in_dma, data_in_sz);
Suganath Prabuba630ea2019-08-03 09:59:52 -0400963 else if (toolbox_request->Tool ==
964 MPI2_TOOLBOX_MEMORY_MOVE_TOOL) {
965 Mpi2ToolboxMemMoveRequest_t *mem_move_request =
966 (Mpi2ToolboxMemMoveRequest_t *)request;
967 Mpi2SGESimple64_t tmp, *src = NULL, *dst = NULL;
968
969 ioc->build_sg_mpi(ioc, psge, data_out_dma,
970 data_out_sz, data_in_dma, data_in_sz);
971 if (data_out_sz && !data_in_sz) {
972 dst =
973 (Mpi2SGESimple64_t *)&mem_move_request->SGL;
974 src = (void *)dst + ioc->sge_size;
975
976 memcpy(&tmp, src, ioc->sge_size);
977 memcpy(src, dst, ioc->sge_size);
978 memcpy(dst, &tmp, ioc->sge_size);
979 }
980 if (ioc->logging_level & MPT_DEBUG_TM) {
981 ioc_info(ioc,
982 "Mpi2ToolboxMemMoveRequest_t request msg\n");
983 _debug_dump_mf(mem_move_request,
984 ioc->request_sz/4);
985 }
986 } else
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530987 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
Suganath Prabuba630ea2019-08-03 09:59:52 -0400988 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400989 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530990 break;
991 }
992 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
993 {
994 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
995 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
996
997 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
998 || sasiounit_request->Operation ==
999 MPI2_SAS_OP_PHY_LINK_RESET) {
1000 ioc->ioc_link_reset_in_progress = 1;
1001 ioc->ignore_loginfos = 1;
1002 }
1003 /* drop to default case for posting the request */
1004 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001005 fallthrough;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301006 default:
1007 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
1008 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001009 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301010 break;
1011 }
1012
1013 if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
1014 timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
1015 else
1016 timeout = karg.timeout;
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07001017 wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301018 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
1019 Mpi2SCSITaskManagementRequest_t *tm_request =
1020 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
1021 mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
1022 tm_request->DevHandle));
1023 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
1024 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
1025 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
1026 ioc->ioc_link_reset_in_progress) {
1027 ioc->ioc_link_reset_in_progress = 0;
1028 ioc->ignore_loginfos = 0;
1029 }
1030 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05001031 mpt3sas_check_cmd_timeout(ioc,
1032 ioc->ctl_cmds.status, mpi_request,
1033 karg.data_sge_offset, issue_reset);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301034 goto issue_host_reset;
1035 }
1036
1037 mpi_reply = ioc->ctl_cmds.reply;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301038
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301039 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
1040 (ioc->logging_level & MPT_DEBUG_TM)) {
1041 Mpi2SCSITaskManagementReply_t *tm_reply =
1042 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
1043
Joe Perches919d8a32018-09-17 08:01:09 -07001044 ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
1045 le16_to_cpu(tm_reply->IOCStatus),
1046 le32_to_cpu(tm_reply->IOCLogInfo),
1047 le32_to_cpu(tm_reply->TerminationCount));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301048 }
Sreekanth Reddyaf009412015-11-11 17:30:23 +05301049
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301050 /* copy out xdata to user */
1051 if (data_in_sz) {
1052 if (copy_to_user(karg.data_in_buf_ptr, data_in,
1053 data_in_sz)) {
1054 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1055 __LINE__, __func__);
1056 ret = -ENODATA;
1057 goto out;
1058 }
1059 }
1060
1061 /* copy out reply message frame to user */
1062 if (karg.max_reply_bytes) {
1063 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1064 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1065 sz)) {
1066 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1067 __LINE__, __func__);
1068 ret = -ENODATA;
1069 goto out;
1070 }
1071 }
1072
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301073 /* copy out sense/NVMe Error Response to user */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301074 if (karg.max_sense_bytes && (mpi_request->Function ==
1075 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301076 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1077 MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1078 if (karg.sense_data_ptr == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001079 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 +05301080 goto out;
1081 }
1082 sz_arg = (mpi_request->Function ==
1083 MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1084 SCSI_SENSE_BUFFERSIZE;
1085 sz = min_t(u32, karg.max_sense_bytes, sz_arg);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301086 if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1087 sz)) {
1088 pr_err("failure at %s:%d/%s()!\n", __FILE__,
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301089 __LINE__, __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301090 ret = -ENODATA;
1091 goto out;
1092 }
1093 }
1094
1095 issue_host_reset:
1096 if (issue_reset) {
1097 ret = -ENODATA;
1098 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1099 mpi_request->Function ==
1100 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1101 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001102 ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
1103 le16_to_cpu(mpi_request->FunctionDependent1));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301104 mpt3sas_halt_firmware(ioc);
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001105 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1106 le16_to_cpu(mpi_request->FunctionDependent1));
Suganath Prabu5bb309d2019-08-03 09:59:50 -04001107 if (pcie_device && (!ioc->tm_custom_handling) &&
1108 (!(mpt3sas_scsih_is_pcie_scsi_device(
1109 pcie_device->device_info))))
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001110 mpt3sas_scsih_issue_locked_tm(ioc,
1111 le16_to_cpu(mpi_request->FunctionDependent1),
Suganath Prabu S521e9c02020-07-30 13:33:47 +05301112 0, 0, 0,
1113 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001114 0, pcie_device->reset_timeout,
Suganath Prabu5bb309d2019-08-03 09:59:50 -04001115 MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE);
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001116 else
1117 mpt3sas_scsih_issue_locked_tm(ioc,
1118 le16_to_cpu(mpi_request->FunctionDependent1),
Suganath Prabu S521e9c02020-07-30 13:33:47 +05301119 0, 0, 0,
1120 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001121 0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301122 } else
Calvin Owens98c56ad2016-07-28 21:38:21 -07001123 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301124 }
1125
1126 out:
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001127 if (pcie_device)
1128 pcie_device_put(pcie_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301129
1130 /* free memory associated with sg buffers */
1131 if (data_in)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001132 dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301133 data_in_dma);
1134
1135 if (data_out)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001136 dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301137 data_out_dma);
1138
1139 kfree(mpi_request);
1140 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1141 return ret;
1142}
1143
1144/**
1145 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1146 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001147 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301148 */
1149static long
1150_ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1151{
1152 struct mpt3_ioctl_iocinfo karg;
1153
Joe Perches919d8a32018-09-17 08:01:09 -07001154 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1155 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301156
1157 memset(&karg, 0 , sizeof(karg));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301158 if (ioc->pfacts)
1159 karg.port_number = ioc->pfacts[0].PortNumber;
1160 karg.hw_rev = ioc->pdev->revision;
1161 karg.pci_id = ioc->pdev->device;
1162 karg.subsystem_device = ioc->pdev->subsystem_device;
1163 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1164 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1165 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1166 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1167 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1168 karg.firmware_version = ioc->facts.FWVersion.Word;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301169 strcpy(karg.driver_version, ioc->driver_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301170 strcat(karg.driver_version, "-");
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301171 switch (ioc->hba_mpi_version_belonged) {
1172 case MPI2_VERSION:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301173 if (ioc->is_warpdrive)
1174 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1175 else
1176 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301177 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1178 break;
1179 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301180 case MPI26_VERSION:
Suganath Prabu Subramani998f26a2016-10-26 13:34:37 +05301181 if (ioc->is_gen35_ioc)
1182 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1183 else
1184 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301185 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1186 break;
1187 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301188 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1189
1190 if (copy_to_user(arg, &karg, sizeof(karg))) {
1191 pr_err("failure at %s:%d/%s()!\n",
1192 __FILE__, __LINE__, __func__);
1193 return -EFAULT;
1194 }
1195 return 0;
1196}
1197
1198/**
1199 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1200 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001201 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301202 */
1203static long
1204_ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1205{
1206 struct mpt3_ioctl_eventquery karg;
1207
1208 if (copy_from_user(&karg, arg, sizeof(karg))) {
1209 pr_err("failure at %s:%d/%s()!\n",
1210 __FILE__, __LINE__, __func__);
1211 return -EFAULT;
1212 }
1213
Joe Perches919d8a32018-09-17 08:01:09 -07001214 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1215 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301216
1217 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1218 memcpy(karg.event_types, ioc->event_type,
1219 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1220
1221 if (copy_to_user(arg, &karg, sizeof(karg))) {
1222 pr_err("failure at %s:%d/%s()!\n",
1223 __FILE__, __LINE__, __func__);
1224 return -EFAULT;
1225 }
1226 return 0;
1227}
1228
1229/**
1230 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1231 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001232 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301233 */
1234static long
1235_ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1236{
1237 struct mpt3_ioctl_eventenable karg;
1238
1239 if (copy_from_user(&karg, arg, sizeof(karg))) {
1240 pr_err("failure at %s:%d/%s()!\n",
1241 __FILE__, __LINE__, __func__);
1242 return -EFAULT;
1243 }
1244
Joe Perches919d8a32018-09-17 08:01:09 -07001245 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1246 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301247
1248 memcpy(ioc->event_type, karg.event_types,
1249 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1250 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1251
1252 if (ioc->event_log)
1253 return 0;
1254 /* initialize event_log */
1255 ioc->event_context = 0;
1256 ioc->aen_event_read_flag = 0;
1257 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1258 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1259 if (!ioc->event_log) {
1260 pr_err("failure at %s:%d/%s()!\n",
1261 __FILE__, __LINE__, __func__);
1262 return -ENOMEM;
1263 }
1264 return 0;
1265}
1266
1267/**
1268 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1269 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001270 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301271 */
1272static long
1273_ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1274{
1275 struct mpt3_ioctl_eventreport karg;
1276 u32 number_bytes, max_events, max;
1277 struct mpt3_ioctl_eventreport __user *uarg = arg;
1278
1279 if (copy_from_user(&karg, arg, sizeof(karg))) {
1280 pr_err("failure at %s:%d/%s()!\n",
1281 __FILE__, __LINE__, __func__);
1282 return -EFAULT;
1283 }
1284
Joe Perches919d8a32018-09-17 08:01:09 -07001285 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1286 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301287
1288 number_bytes = karg.hdr.max_data_size -
1289 sizeof(struct mpt3_ioctl_header);
1290 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1291 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1292
1293 /* If fewer than 1 event is requested, there must have
1294 * been some type of error.
1295 */
1296 if (!max || !ioc->event_log)
1297 return -ENODATA;
1298
1299 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1300 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1301 pr_err("failure at %s:%d/%s()!\n",
1302 __FILE__, __LINE__, __func__);
1303 return -EFAULT;
1304 }
1305
1306 /* reset flag so SIGIO can restart */
1307 ioc->aen_event_read_flag = 0;
1308 return 0;
1309}
1310
1311/**
1312 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1313 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001314 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301315 */
1316static long
1317_ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1318{
1319 struct mpt3_ioctl_diag_reset karg;
1320 int retval;
1321
1322 if (copy_from_user(&karg, arg, sizeof(karg))) {
1323 pr_err("failure at %s:%d/%s()!\n",
1324 __FILE__, __LINE__, __func__);
1325 return -EFAULT;
1326 }
1327
1328 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1329 ioc->is_driver_loading)
1330 return -EAGAIN;
1331
Joe Perches919d8a32018-09-17 08:01:09 -07001332 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1333 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301334
Calvin Owens98c56ad2016-07-28 21:38:21 -07001335 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddy5b061982019-12-26 06:13:30 -05001336 ioc_info(ioc,
1337 "Ioctl: host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301338 return 0;
1339}
1340
1341/**
1342 * _ctl_btdh_search_sas_device - searching for sas device
1343 * @ioc: per adapter object
1344 * @btdh: btdh ioctl payload
1345 */
1346static int
1347_ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1348 struct mpt3_ioctl_btdh_mapping *btdh)
1349{
1350 struct _sas_device *sas_device;
1351 unsigned long flags;
1352 int rc = 0;
1353
1354 if (list_empty(&ioc->sas_device_list))
1355 return rc;
1356
1357 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1358 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1359 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1360 btdh->handle == sas_device->handle) {
1361 btdh->bus = sas_device->channel;
1362 btdh->id = sas_device->id;
1363 rc = 1;
1364 goto out;
1365 } else if (btdh->bus == sas_device->channel && btdh->id ==
1366 sas_device->id && btdh->handle == 0xFFFF) {
1367 btdh->handle = sas_device->handle;
1368 rc = 1;
1369 goto out;
1370 }
1371 }
1372 out:
1373 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1374 return rc;
1375}
1376
1377/**
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +05301378 * _ctl_btdh_search_pcie_device - searching for pcie device
1379 * @ioc: per adapter object
1380 * @btdh: btdh ioctl payload
1381 */
1382static int
1383_ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
1384 struct mpt3_ioctl_btdh_mapping *btdh)
1385{
1386 struct _pcie_device *pcie_device;
1387 unsigned long flags;
1388 int rc = 0;
1389
1390 if (list_empty(&ioc->pcie_device_list))
1391 return rc;
1392
1393 spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1394 list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1395 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1396 btdh->handle == pcie_device->handle) {
1397 btdh->bus = pcie_device->channel;
1398 btdh->id = pcie_device->id;
1399 rc = 1;
1400 goto out;
1401 } else if (btdh->bus == pcie_device->channel && btdh->id ==
1402 pcie_device->id && btdh->handle == 0xFFFF) {
1403 btdh->handle = pcie_device->handle;
1404 rc = 1;
1405 goto out;
1406 }
1407 }
1408 out:
1409 spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1410 return rc;
1411}
1412
1413/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301414 * _ctl_btdh_search_raid_device - searching for raid device
1415 * @ioc: per adapter object
1416 * @btdh: btdh ioctl payload
1417 */
1418static int
1419_ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1420 struct mpt3_ioctl_btdh_mapping *btdh)
1421{
1422 struct _raid_device *raid_device;
1423 unsigned long flags;
1424 int rc = 0;
1425
1426 if (list_empty(&ioc->raid_device_list))
1427 return rc;
1428
1429 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1430 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1431 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1432 btdh->handle == raid_device->handle) {
1433 btdh->bus = raid_device->channel;
1434 btdh->id = raid_device->id;
1435 rc = 1;
1436 goto out;
1437 } else if (btdh->bus == raid_device->channel && btdh->id ==
1438 raid_device->id && btdh->handle == 0xFFFF) {
1439 btdh->handle = raid_device->handle;
1440 rc = 1;
1441 goto out;
1442 }
1443 }
1444 out:
1445 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1446 return rc;
1447}
1448
1449/**
1450 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1451 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001452 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301453 */
1454static long
1455_ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1456{
1457 struct mpt3_ioctl_btdh_mapping karg;
1458 int rc;
1459
1460 if (copy_from_user(&karg, arg, sizeof(karg))) {
1461 pr_err("failure at %s:%d/%s()!\n",
1462 __FILE__, __LINE__, __func__);
1463 return -EFAULT;
1464 }
1465
Joe Perches919d8a32018-09-17 08:01:09 -07001466 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1467 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301468
1469 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1470 if (!rc)
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +05301471 rc = _ctl_btdh_search_pcie_device(ioc, &karg);
1472 if (!rc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301473 _ctl_btdh_search_raid_device(ioc, &karg);
1474
1475 if (copy_to_user(arg, &karg, sizeof(karg))) {
1476 pr_err("failure at %s:%d/%s()!\n",
1477 __FILE__, __LINE__, __func__);
1478 return -EFAULT;
1479 }
1480 return 0;
1481}
1482
1483/**
1484 * _ctl_diag_capability - return diag buffer capability
1485 * @ioc: per adapter object
1486 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1487 *
1488 * returns 1 when diag buffer support is enabled in firmware
1489 */
1490static u8
1491_ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1492{
1493 u8 rc = 0;
1494
1495 switch (buffer_type) {
1496 case MPI2_DIAG_BUF_TYPE_TRACE:
1497 if (ioc->facts.IOCCapabilities &
1498 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1499 rc = 1;
1500 break;
1501 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1502 if (ioc->facts.IOCCapabilities &
1503 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1504 rc = 1;
1505 break;
1506 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1507 if (ioc->facts.IOCCapabilities &
1508 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1509 rc = 1;
1510 }
1511
1512 return rc;
1513}
1514
Sreekanth Reddy08e73782019-09-13 09:04:42 -04001515/**
1516 * _ctl_diag_get_bufftype - return diag buffer type
1517 * either TRACE, SNAPSHOT, or EXTENDED
1518 * @ioc: per adapter object
1519 * @unique_id: specifies the unique_id for the buffer
1520 *
1521 * returns MPT3_DIAG_UID_NOT_FOUND if the id not found
1522 */
1523static u8
1524_ctl_diag_get_bufftype(struct MPT3SAS_ADAPTER *ioc, u32 unique_id)
1525{
1526 u8 index;
1527
1528 for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
1529 if (ioc->unique_id[index] == unique_id)
1530 return index;
1531 }
1532
1533 return MPT3_DIAG_UID_NOT_FOUND;
1534}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301535
1536/**
1537 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1538 * @ioc: per adapter object
1539 * @diag_register: the diag_register struct passed in from user space
1540 *
1541 */
1542static long
1543_ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1544 struct mpt3_diag_register *diag_register)
1545{
1546 int rc, i;
1547 void *request_data = NULL;
1548 dma_addr_t request_data_dma;
1549 u32 request_data_sz = 0;
1550 Mpi2DiagBufferPostRequest_t *mpi_request;
1551 Mpi2DiagBufferPostReply_t *mpi_reply;
1552 u8 buffer_type;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301553 u16 smid;
1554 u16 ioc_status;
1555 u32 ioc_state;
1556 u8 issue_reset = 0;
1557
Joe Perches919d8a32018-09-17 08:01:09 -07001558 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1559 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301560
1561 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1562 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001563 ioc_err(ioc, "%s: failed due to ioc not operational\n",
1564 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301565 rc = -EAGAIN;
1566 goto out;
1567 }
1568
1569 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001570 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301571 rc = -EAGAIN;
1572 goto out;
1573 }
1574
1575 buffer_type = diag_register->buffer_type;
1576 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001577 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1578 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301579 return -EPERM;
1580 }
1581
Sreekanth Reddy08e73782019-09-13 09:04:42 -04001582 if (diag_register->unique_id == 0) {
1583 ioc_err(ioc,
1584 "%s: Invalid UID(0x%08x), buffer_type(0x%02x)\n", __func__,
1585 diag_register->unique_id, buffer_type);
1586 return -EINVAL;
1587 }
1588
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04001589 if ((ioc->diag_buffer_status[buffer_type] &
1590 MPT3_DIAG_BUFFER_IS_APP_OWNED) &&
1591 !(ioc->diag_buffer_status[buffer_type] &
1592 MPT3_DIAG_BUFFER_IS_RELEASED)) {
1593 ioc_err(ioc,
1594 "%s: buffer_type(0x%02x) is already registered by application with UID(0x%08x)\n",
1595 __func__, buffer_type, ioc->unique_id[buffer_type]);
1596 return -EINVAL;
1597 }
1598
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301599 if (ioc->diag_buffer_status[buffer_type] &
1600 MPT3_DIAG_BUFFER_IS_REGISTERED) {
Sreekanth Reddy08e73782019-09-13 09:04:42 -04001601 /*
1602 * If driver posts buffer initially, then an application wants
1603 * to Register that buffer (own it) without Releasing first,
1604 * the application Register command MUST have the same buffer
1605 * type and size in the Register command (obtained from the
1606 * Query command). Otherwise that Register command will be
1607 * failed. If the application has released the buffer but wants
1608 * to re-register it, it should be allowed as long as the
1609 * Unique-Id/Size match.
1610 */
1611
1612 if (ioc->unique_id[buffer_type] == MPT3DIAGBUFFUNIQUEID &&
1613 ioc->diag_buffer_sz[buffer_type] ==
1614 diag_register->requested_buffer_size) {
1615
1616 if (!(ioc->diag_buffer_status[buffer_type] &
1617 MPT3_DIAG_BUFFER_IS_RELEASED)) {
1618 dctlprintk(ioc, ioc_info(ioc,
1619 "%s: diag_buffer (%d) ownership changed. old-ID(0x%08x), new-ID(0x%08x)\n",
1620 __func__, buffer_type,
1621 ioc->unique_id[buffer_type],
1622 diag_register->unique_id));
1623
1624 /*
1625 * Application wants to own the buffer with
1626 * the same size.
1627 */
1628 ioc->unique_id[buffer_type] =
1629 diag_register->unique_id;
1630 rc = 0; /* success */
1631 goto out;
1632 }
1633 } else if (ioc->unique_id[buffer_type] !=
1634 MPT3DIAGBUFFUNIQUEID) {
1635 if (ioc->unique_id[buffer_type] !=
1636 diag_register->unique_id ||
1637 ioc->diag_buffer_sz[buffer_type] !=
1638 diag_register->requested_buffer_size ||
1639 !(ioc->diag_buffer_status[buffer_type] &
1640 MPT3_DIAG_BUFFER_IS_RELEASED)) {
1641 ioc_err(ioc,
1642 "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1643 __func__, buffer_type);
1644 return -EINVAL;
1645 }
1646 } else {
1647 ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1648 __func__, buffer_type);
1649 return -EINVAL;
1650 }
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001651 } else if (ioc->diag_buffer_status[buffer_type] &
1652 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
1653
1654 if (ioc->unique_id[buffer_type] != MPT3DIAGBUFFUNIQUEID ||
1655 ioc->diag_buffer_sz[buffer_type] !=
1656 diag_register->requested_buffer_size) {
1657
1658 ioc_err(ioc,
1659 "%s: already a buffer is allocated for buffer_type(0x%02x) of size %d bytes, so please try registering again with same size\n",
1660 __func__, buffer_type,
1661 ioc->diag_buffer_sz[buffer_type]);
1662 return -EINVAL;
1663 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301664 }
1665
1666 if (diag_register->requested_buffer_size % 4) {
Joe Perches919d8a32018-09-17 08:01:09 -07001667 ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
1668 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301669 return -EINVAL;
1670 }
1671
1672 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1673 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07001674 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301675 rc = -EAGAIN;
1676 goto out;
1677 }
1678
1679 rc = 0;
1680 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1681 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1682 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1683 ioc->ctl_cmds.smid = smid;
1684
1685 request_data = ioc->diag_buffer[buffer_type];
1686 request_data_sz = diag_register->requested_buffer_size;
1687 ioc->unique_id[buffer_type] = diag_register->unique_id;
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001688 ioc->diag_buffer_status[buffer_type] &=
1689 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301690 memcpy(ioc->product_specific[buffer_type],
1691 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1692 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1693
1694 if (request_data) {
1695 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1696 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001697 dma_free_coherent(&ioc->pdev->dev,
1698 ioc->diag_buffer_sz[buffer_type],
1699 request_data, request_data_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301700 request_data = NULL;
1701 }
1702 }
1703
1704 if (request_data == NULL) {
1705 ioc->diag_buffer_sz[buffer_type] = 0;
1706 ioc->diag_buffer_dma[buffer_type] = 0;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001707 request_data = dma_alloc_coherent(&ioc->pdev->dev,
1708 request_data_sz, &request_data_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301709 if (request_data == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001710 ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
1711 __func__, request_data_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301712 mpt3sas_base_free_smid(ioc, smid);
Sreekanth Reddy782b2812019-09-13 09:04:40 -04001713 rc = -ENOMEM;
1714 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301715 }
1716 ioc->diag_buffer[buffer_type] = request_data;
1717 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1718 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1719 }
1720
1721 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1722 mpi_request->BufferType = diag_register->buffer_type;
1723 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1724 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1725 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1726 mpi_request->VF_ID = 0; /* TODO */
1727 mpi_request->VP_ID = 0;
1728
Joe Perches919d8a32018-09-17 08:01:09 -07001729 dctlprintk(ioc,
1730 ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1731 __func__, request_data,
1732 (unsigned long long)request_data_dma,
1733 le32_to_cpu(mpi_request->BufferLength)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301734
1735 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1736 mpi_request->ProductSpecific[i] =
1737 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1738
1739 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001740 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07001741 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301742 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1743
1744 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05001745 mpt3sas_check_cmd_timeout(ioc,
1746 ioc->ctl_cmds.status, mpi_request,
1747 sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301748 goto issue_host_reset;
1749 }
1750
1751 /* process the completed Reply Message Frame */
1752 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001753 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301754 rc = -EFAULT;
1755 goto out;
1756 }
1757
1758 mpi_reply = ioc->ctl_cmds.reply;
1759 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1760
1761 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1762 ioc->diag_buffer_status[buffer_type] |=
1763 MPT3_DIAG_BUFFER_IS_REGISTERED;
Joe Perches919d8a32018-09-17 08:01:09 -07001764 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301765 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07001766 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1767 __func__,
1768 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301769 rc = -EFAULT;
1770 }
1771
1772 issue_host_reset:
1773 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07001774 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301775
1776 out:
1777
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001778 if (rc && request_data) {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001779 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301780 request_data, request_data_dma);
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001781 ioc->diag_buffer_status[buffer_type] &=
1782 ~MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1783 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301784
1785 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1786 return rc;
1787}
1788
1789/**
1790 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1791 * @ioc: per adapter object
1792 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1793 *
1794 * This is called when command line option diag_buffer_enable is enabled
1795 * at driver load time.
1796 */
1797void
1798mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1799{
1800 struct mpt3_diag_register diag_register;
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04001801 u32 ret_val;
1802 u32 trace_buff_size = ioc->manu_pg11.HostTraceBufferMaxSizeKB<<10;
1803 u32 min_trace_buff_size = 0;
1804 u32 decr_trace_buff_size = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301805
1806 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1807
1808 if (bits_to_register & 1) {
Joe Perches919d8a32018-09-17 08:01:09 -07001809 ioc_info(ioc, "registering trace buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301810 ioc->diag_trigger_master.MasterData =
1811 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1812 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
Sreekanth Reddy08e73782019-09-13 09:04:42 -04001813 diag_register.unique_id =
1814 (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
1815 (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04001816
1817 if (trace_buff_size != 0) {
1818 diag_register.requested_buffer_size = trace_buff_size;
1819 min_trace_buff_size =
1820 ioc->manu_pg11.HostTraceBufferMinSizeKB<<10;
1821 decr_trace_buff_size =
1822 ioc->manu_pg11.HostTraceBufferDecrementSizeKB<<10;
1823
1824 if (min_trace_buff_size > trace_buff_size) {
1825 /* The buff size is not set correctly */
1826 ioc_err(ioc,
1827 "Min Trace Buff size (%d KB) greater than Max Trace Buff size (%d KB)\n",
1828 min_trace_buff_size>>10,
1829 trace_buff_size>>10);
1830 ioc_err(ioc,
1831 "Using zero Min Trace Buff Size\n");
Dan Carpenter3524a382019-10-04 13:06:15 +03001832 min_trace_buff_size = 0;
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04001833 }
1834
1835 if (decr_trace_buff_size == 0) {
1836 /*
1837 * retry the min size if decrement
1838 * is not available.
1839 */
1840 decr_trace_buff_size =
1841 trace_buff_size - min_trace_buff_size;
1842 }
1843 } else {
1844 /* register for 2MB buffers */
1845 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1846 }
1847
1848 do {
1849 ret_val = _ctl_diag_register_2(ioc, &diag_register);
1850
1851 if (ret_val == -ENOMEM && min_trace_buff_size &&
1852 (trace_buff_size - decr_trace_buff_size) >=
1853 min_trace_buff_size) {
1854 /* adjust the buffer size */
1855 trace_buff_size -= decr_trace_buff_size;
1856 diag_register.requested_buffer_size =
1857 trace_buff_size;
1858 } else
1859 break;
1860 } while (true);
1861
1862 if (ret_val == -ENOMEM)
1863 ioc_err(ioc,
1864 "Cannot allocate trace buffer memory. Last memory tried = %d KB\n",
1865 diag_register.requested_buffer_size>>10);
1866 else if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE]
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001867 & MPT3_DIAG_BUFFER_IS_REGISTERED) {
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04001868 ioc_err(ioc, "Trace buffer memory %d KB allocated\n",
1869 diag_register.requested_buffer_size>>10);
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001870 if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
1871 ioc->diag_buffer_status[
1872 MPI2_DIAG_BUF_TYPE_TRACE] |=
1873 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1874 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301875 }
1876
1877 if (bits_to_register & 2) {
Joe Perches919d8a32018-09-17 08:01:09 -07001878 ioc_info(ioc, "registering snapshot buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301879 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1880 /* register for 2MB buffers */
1881 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1882 diag_register.unique_id = 0x7075901;
1883 _ctl_diag_register_2(ioc, &diag_register);
1884 }
1885
1886 if (bits_to_register & 4) {
Joe Perches919d8a32018-09-17 08:01:09 -07001887 ioc_info(ioc, "registering extended buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301888 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1889 /* register for 2MB buffers */
1890 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1891 diag_register.unique_id = 0x7075901;
1892 _ctl_diag_register_2(ioc, &diag_register);
1893 }
1894}
1895
1896/**
1897 * _ctl_diag_register - application register with driver
1898 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001899 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301900 *
1901 * This will allow the driver to setup any required buffers that will be
1902 * needed by firmware to communicate with the driver.
1903 */
1904static long
1905_ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1906{
1907 struct mpt3_diag_register karg;
1908 long rc;
1909
1910 if (copy_from_user(&karg, arg, sizeof(karg))) {
1911 pr_err("failure at %s:%d/%s()!\n",
1912 __FILE__, __LINE__, __func__);
1913 return -EFAULT;
1914 }
1915
1916 rc = _ctl_diag_register_2(ioc, &karg);
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04001917
1918 if (!rc && (ioc->diag_buffer_status[karg.buffer_type] &
1919 MPT3_DIAG_BUFFER_IS_REGISTERED))
1920 ioc->diag_buffer_status[karg.buffer_type] |=
1921 MPT3_DIAG_BUFFER_IS_APP_OWNED;
1922
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301923 return rc;
1924}
1925
1926/**
1927 * _ctl_diag_unregister - application unregister with driver
1928 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001929 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301930 *
1931 * This will allow the driver to cleanup any memory allocated for diag
1932 * messages and to free up any resources.
1933 */
1934static long
1935_ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1936{
1937 struct mpt3_diag_unregister karg;
1938 void *request_data;
1939 dma_addr_t request_data_dma;
1940 u32 request_data_sz;
1941 u8 buffer_type;
1942
1943 if (copy_from_user(&karg, arg, sizeof(karg))) {
1944 pr_err("failure at %s:%d/%s()!\n",
1945 __FILE__, __LINE__, __func__);
1946 return -EFAULT;
1947 }
1948
Joe Perches919d8a32018-09-17 08:01:09 -07001949 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1950 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301951
Sreekanth Reddy08e73782019-09-13 09:04:42 -04001952 buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
1953 if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
1954 ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
1955 __func__, karg.unique_id);
1956 return -EINVAL;
1957 }
1958
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301959 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001960 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1961 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301962 return -EPERM;
1963 }
1964
1965 if ((ioc->diag_buffer_status[buffer_type] &
1966 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001967 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1968 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301969 return -EINVAL;
1970 }
1971 if ((ioc->diag_buffer_status[buffer_type] &
1972 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001973 ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
1974 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301975 return -EINVAL;
1976 }
1977
1978 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07001979 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1980 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301981 return -EINVAL;
1982 }
1983
1984 request_data = ioc->diag_buffer[buffer_type];
1985 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07001986 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1987 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301988 return -ENOMEM;
1989 }
1990
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001991 if (ioc->diag_buffer_status[buffer_type] &
1992 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
1993 ioc->unique_id[buffer_type] = MPT3DIAGBUFFUNIQUEID;
1994 ioc->diag_buffer_status[buffer_type] &=
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04001995 ~MPT3_DIAG_BUFFER_IS_APP_OWNED;
1996 ioc->diag_buffer_status[buffer_type] &=
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04001997 ~MPT3_DIAG_BUFFER_IS_REGISTERED;
1998 } else {
1999 request_data_sz = ioc->diag_buffer_sz[buffer_type];
2000 request_data_dma = ioc->diag_buffer_dma[buffer_type];
2001 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
2002 request_data, request_data_dma);
2003 ioc->diag_buffer[buffer_type] = NULL;
2004 ioc->diag_buffer_status[buffer_type] = 0;
2005 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302006 return 0;
2007}
2008
2009/**
2010 * _ctl_diag_query - query relevant info associated with diag buffers
2011 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002012 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302013 *
2014 * The application will send only buffer_type and unique_id. Driver will
2015 * inspect unique_id first, if valid, fill in all the info. If unique_id is
2016 * 0x00, the driver will return info specified by Buffer Type.
2017 */
2018static long
2019_ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2020{
2021 struct mpt3_diag_query karg;
2022 void *request_data;
2023 int i;
2024 u8 buffer_type;
2025
2026 if (copy_from_user(&karg, arg, sizeof(karg))) {
2027 pr_err("failure at %s:%d/%s()!\n",
2028 __FILE__, __LINE__, __func__);
2029 return -EFAULT;
2030 }
2031
Joe Perches919d8a32018-09-17 08:01:09 -07002032 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2033 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302034
2035 karg.application_flags = 0;
2036 buffer_type = karg.buffer_type;
2037
2038 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002039 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2040 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302041 return -EPERM;
2042 }
2043
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04002044 if (!(ioc->diag_buffer_status[buffer_type] &
2045 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED)) {
2046 if ((ioc->diag_buffer_status[buffer_type] &
2047 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2048 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2049 __func__, buffer_type);
2050 return -EINVAL;
2051 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302052 }
2053
Sreekanth Reddy08e73782019-09-13 09:04:42 -04002054 if (karg.unique_id) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302055 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002056 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2057 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302058 return -EINVAL;
2059 }
2060 }
2061
2062 request_data = ioc->diag_buffer[buffer_type];
2063 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002064 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2065 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302066 return -ENOMEM;
2067 }
2068
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04002069 if ((ioc->diag_buffer_status[buffer_type] &
2070 MPT3_DIAG_BUFFER_IS_REGISTERED))
2071 karg.application_flags |= MPT3_APP_FLAGS_BUFFER_VALID;
2072
2073 if (!(ioc->diag_buffer_status[buffer_type] &
2074 MPT3_DIAG_BUFFER_IS_RELEASED))
2075 karg.application_flags |= MPT3_APP_FLAGS_FW_BUFFER_ACCESS;
2076
2077 if (!(ioc->diag_buffer_status[buffer_type] &
2078 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED))
2079 karg.application_flags |= MPT3_APP_FLAGS_DYNAMIC_BUFFER_ALLOC;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302080
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04002081 if ((ioc->diag_buffer_status[buffer_type] &
2082 MPT3_DIAG_BUFFER_IS_APP_OWNED))
2083 karg.application_flags |= MPT3_APP_FLAGS_APP_OWNED;
2084
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302085 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2086 karg.product_specific[i] =
2087 ioc->product_specific[buffer_type][i];
2088
2089 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
2090 karg.driver_added_buffer_size = 0;
2091 karg.unique_id = ioc->unique_id[buffer_type];
2092 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
2093
2094 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
Joe Perches919d8a32018-09-17 08:01:09 -07002095 ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
2096 __func__, arg);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302097 return -EFAULT;
2098 }
2099 return 0;
2100}
2101
2102/**
2103 * mpt3sas_send_diag_release - Diag Release Message
2104 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002105 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
2106 * @issue_reset: specifies whether host reset is required.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302107 *
2108 */
2109int
2110mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
2111 u8 *issue_reset)
2112{
2113 Mpi2DiagReleaseRequest_t *mpi_request;
2114 Mpi2DiagReleaseReply_t *mpi_reply;
2115 u16 smid;
2116 u16 ioc_status;
2117 u32 ioc_state;
2118 int rc;
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05002119 u8 reset_needed = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302120
Joe Perches919d8a32018-09-17 08:01:09 -07002121 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2122 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302123
2124 rc = 0;
2125 *issue_reset = 0;
2126
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05002127
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302128 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2129 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2130 if (ioc->diag_buffer_status[buffer_type] &
2131 MPT3_DIAG_BUFFER_IS_REGISTERED)
2132 ioc->diag_buffer_status[buffer_type] |=
2133 MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07002134 dctlprintk(ioc,
2135 ioc_info(ioc, "%s: skipping due to FAULT state\n",
2136 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302137 rc = -EAGAIN;
2138 goto out;
2139 }
2140
2141 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07002142 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302143 rc = -EAGAIN;
2144 goto out;
2145 }
2146
2147 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2148 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07002149 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302150 rc = -EAGAIN;
2151 goto out;
2152 }
2153
2154 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2155 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2156 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2157 ioc->ctl_cmds.smid = smid;
2158
2159 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
2160 mpi_request->BufferType = buffer_type;
2161 mpi_request->VF_ID = 0; /* TODO */
2162 mpi_request->VP_ID = 0;
2163
2164 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04002165 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002166 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302167 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2168
2169 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05002170 mpt3sas_check_cmd_timeout(ioc,
2171 ioc->ctl_cmds.status, mpi_request,
2172 sizeof(Mpi2DiagReleaseRequest_t)/4, reset_needed);
2173 *issue_reset = reset_needed;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302174 rc = -EFAULT;
2175 goto out;
2176 }
2177
2178 /* process the completed Reply Message Frame */
2179 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002180 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302181 rc = -EFAULT;
2182 goto out;
2183 }
2184
2185 mpi_reply = ioc->ctl_cmds.reply;
2186 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2187
2188 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2189 ioc->diag_buffer_status[buffer_type] |=
2190 MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07002191 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302192 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07002193 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2194 __func__,
2195 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302196 rc = -EFAULT;
2197 }
2198
2199 out:
2200 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2201 return rc;
2202}
2203
2204/**
2205 * _ctl_diag_release - request to send Diag Release Message to firmware
Bart Van Assche4beb4862018-06-15 14:42:01 -07002206 * @ioc: ?
2207 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302208 *
2209 * This allows ownership of the specified buffer to returned to the driver,
2210 * allowing an application to read the buffer without fear that firmware is
Masahiro Yamada9a284e52017-02-27 14:29:48 -08002211 * overwriting information in the buffer.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302212 */
2213static long
2214_ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2215{
2216 struct mpt3_diag_release karg;
2217 void *request_data;
2218 int rc;
2219 u8 buffer_type;
2220 u8 issue_reset = 0;
2221
2222 if (copy_from_user(&karg, arg, sizeof(karg))) {
2223 pr_err("failure at %s:%d/%s()!\n",
2224 __FILE__, __LINE__, __func__);
2225 return -EFAULT;
2226 }
2227
Joe Perches919d8a32018-09-17 08:01:09 -07002228 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2229 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302230
Sreekanth Reddy08e73782019-09-13 09:04:42 -04002231 buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2232 if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2233 ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2234 __func__, karg.unique_id);
2235 return -EINVAL;
2236 }
2237
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302238 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002239 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2240 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302241 return -EPERM;
2242 }
2243
2244 if ((ioc->diag_buffer_status[buffer_type] &
2245 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002246 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2247 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302248 return -EINVAL;
2249 }
2250
2251 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002252 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2253 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302254 return -EINVAL;
2255 }
2256
2257 if (ioc->diag_buffer_status[buffer_type] &
2258 MPT3_DIAG_BUFFER_IS_RELEASED) {
Joe Perches919d8a32018-09-17 08:01:09 -07002259 ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
2260 __func__, buffer_type);
Sreekanth Reddy29f571f2019-09-13 09:04:46 -04002261 return -EINVAL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302262 }
2263
2264 request_data = ioc->diag_buffer[buffer_type];
2265
2266 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002267 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2268 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302269 return -ENOMEM;
2270 }
2271
2272 /* buffers were released by due to host reset */
2273 if ((ioc->diag_buffer_status[buffer_type] &
2274 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2275 ioc->diag_buffer_status[buffer_type] |=
2276 MPT3_DIAG_BUFFER_IS_RELEASED;
2277 ioc->diag_buffer_status[buffer_type] &=
2278 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
Joe Perches919d8a32018-09-17 08:01:09 -07002279 ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
2280 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302281 return 0;
2282 }
2283
2284 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2285
2286 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07002287 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302288
2289 return rc;
2290}
2291
2292/**
2293 * _ctl_diag_read_buffer - request for copy of the diag buffer
2294 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002295 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302296 */
2297static long
2298_ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2299{
2300 struct mpt3_diag_read_buffer karg;
2301 struct mpt3_diag_read_buffer __user *uarg = arg;
2302 void *request_data, *diag_data;
2303 Mpi2DiagBufferPostRequest_t *mpi_request;
2304 Mpi2DiagBufferPostReply_t *mpi_reply;
2305 int rc, i;
2306 u8 buffer_type;
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002307 unsigned long request_size, copy_size;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302308 u16 smid;
2309 u16 ioc_status;
2310 u8 issue_reset = 0;
2311
2312 if (copy_from_user(&karg, arg, sizeof(karg))) {
2313 pr_err("failure at %s:%d/%s()!\n",
2314 __FILE__, __LINE__, __func__);
2315 return -EFAULT;
2316 }
2317
Joe Perches919d8a32018-09-17 08:01:09 -07002318 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2319 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302320
Sreekanth Reddy08e73782019-09-13 09:04:42 -04002321 buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2322 if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2323 ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2324 __func__, karg.unique_id);
2325 return -EINVAL;
2326 }
2327
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302328 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002329 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2330 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302331 return -EPERM;
2332 }
2333
2334 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002335 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2336 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302337 return -EINVAL;
2338 }
2339
2340 request_data = ioc->diag_buffer[buffer_type];
2341 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002342 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2343 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302344 return -ENOMEM;
2345 }
2346
2347 request_size = ioc->diag_buffer_sz[buffer_type];
2348
2349 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002350 ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
2351 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302352 return -EINVAL;
2353 }
2354
2355 if (karg.starting_offset > request_size)
2356 return -EINVAL;
2357
2358 diag_data = (void *)(request_data + karg.starting_offset);
Joe Perches919d8a32018-09-17 08:01:09 -07002359 dctlprintk(ioc,
2360 ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2361 __func__, diag_data, karg.starting_offset,
2362 karg.bytes_to_read));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302363
2364 /* Truncate data on requests that are too large */
2365 if ((diag_data + karg.bytes_to_read < diag_data) ||
2366 (diag_data + karg.bytes_to_read > request_data + request_size))
2367 copy_size = request_size - karg.starting_offset;
2368 else
2369 copy_size = karg.bytes_to_read;
2370
2371 if (copy_to_user((void __user *)uarg->diagnostic_data,
2372 diag_data, copy_size)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002373 ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2374 __func__, diag_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302375 return -EFAULT;
2376 }
2377
2378 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2379 return 0;
2380
Joe Perches919d8a32018-09-17 08:01:09 -07002381 dctlprintk(ioc,
2382 ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
2383 __func__, buffer_type));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302384 if ((ioc->diag_buffer_status[buffer_type] &
2385 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002386 dctlprintk(ioc,
2387 ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
2388 __func__, buffer_type));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302389 return 0;
2390 }
2391 /* Get a free request frame and save the message context.
2392 */
2393
2394 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07002395 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302396 rc = -EAGAIN;
2397 goto out;
2398 }
2399
2400 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2401 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07002402 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302403 rc = -EAGAIN;
2404 goto out;
2405 }
2406
2407 rc = 0;
2408 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2409 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2410 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2411 ioc->ctl_cmds.smid = smid;
2412
2413 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2414 mpi_request->BufferType = buffer_type;
2415 mpi_request->BufferLength =
2416 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2417 mpi_request->BufferAddress =
2418 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2419 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2420 mpi_request->ProductSpecific[i] =
2421 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2422 mpi_request->VF_ID = 0; /* TODO */
2423 mpi_request->VP_ID = 0;
2424
2425 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04002426 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002427 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302428 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2429
2430 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Sreekanth Reddyc6bdb6a2019-12-26 06:13:31 -05002431 mpt3sas_check_cmd_timeout(ioc,
2432 ioc->ctl_cmds.status, mpi_request,
2433 sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302434 goto issue_host_reset;
2435 }
2436
2437 /* process the completed Reply Message Frame */
2438 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002439 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302440 rc = -EFAULT;
2441 goto out;
2442 }
2443
2444 mpi_reply = ioc->ctl_cmds.reply;
2445 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2446
2447 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2448 ioc->diag_buffer_status[buffer_type] |=
2449 MPT3_DIAG_BUFFER_IS_REGISTERED;
Sreekanth Reddydd180e4e2019-09-13 09:04:43 -04002450 ioc->diag_buffer_status[buffer_type] &=
2451 ~MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07002452 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302453 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07002454 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2455 __func__, ioc_status,
2456 le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302457 rc = -EFAULT;
2458 }
2459
2460 issue_host_reset:
2461 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07002462 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302463
2464 out:
2465
2466 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2467 return rc;
2468}
2469
2470
2471
2472#ifdef CONFIG_COMPAT
2473/**
2474 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2475 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002476 * @cmd: ioctl opcode
2477 * @arg: (struct mpt3_ioctl_command32)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302478 *
2479 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2480 */
2481static long
2482_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2483 void __user *arg)
2484{
2485 struct mpt3_ioctl_command32 karg32;
2486 struct mpt3_ioctl_command32 __user *uarg;
2487 struct mpt3_ioctl_command karg;
2488
2489 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2490 return -EINVAL;
2491
2492 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2493
2494 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2495 pr_err("failure at %s:%d/%s()!\n",
2496 __FILE__, __LINE__, __func__);
2497 return -EFAULT;
2498 }
2499
2500 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2501 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2502 karg.hdr.port_number = karg32.hdr.port_number;
2503 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2504 karg.timeout = karg32.timeout;
2505 karg.max_reply_bytes = karg32.max_reply_bytes;
2506 karg.data_in_size = karg32.data_in_size;
2507 karg.data_out_size = karg32.data_out_size;
2508 karg.max_sense_bytes = karg32.max_sense_bytes;
2509 karg.data_sge_offset = karg32.data_sge_offset;
2510 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2511 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2512 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2513 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2514 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2515}
2516#endif
2517
2518/**
2519 * _ctl_ioctl_main - main ioctl entry point
Bart Van Assche4beb4862018-06-15 14:42:01 -07002520 * @file: (struct file)
2521 * @cmd: ioctl opcode
2522 * @arg: user space data buffer
2523 * @compat: handles 32 bit applications in 64bit os
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302524 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302525 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302526 */
2527static long
2528_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302529 u8 compat, u16 mpi_version)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302530{
2531 struct MPT3SAS_ADAPTER *ioc;
2532 struct mpt3_ioctl_header ioctl_header;
2533 enum block_state state;
2534 long ret = -EINVAL;
2535
2536 /* get IOCTL header */
2537 if (copy_from_user(&ioctl_header, (char __user *)arg,
2538 sizeof(struct mpt3_ioctl_header))) {
2539 pr_err("failure at %s:%d/%s()!\n",
2540 __FILE__, __LINE__, __func__);
2541 return -EFAULT;
2542 }
2543
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302544 if (_ctl_verify_adapter(ioctl_header.ioc_number,
2545 &ioc, mpi_version) == -1 || !ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302546 return -ENODEV;
2547
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302548 /* pci_access_mutex lock acquired by ioctl path */
2549 mutex_lock(&ioc->pci_access_mutex);
2550
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302551 if (ioc->shost_recovery || ioc->pci_error_recovery ||
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302552 ioc->is_driver_loading || ioc->remove_host) {
2553 ret = -EAGAIN;
2554 goto out_unlock_pciaccess;
2555 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302556
2557 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2558 if (state == NON_BLOCKING) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302559 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2560 ret = -EAGAIN;
2561 goto out_unlock_pciaccess;
2562 }
2563 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2564 ret = -ERESTARTSYS;
2565 goto out_unlock_pciaccess;
2566 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302567
2568
2569 switch (cmd) {
2570 case MPT3IOCINFO:
2571 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2572 ret = _ctl_getiocinfo(ioc, arg);
2573 break;
2574#ifdef CONFIG_COMPAT
2575 case MPT3COMMAND32:
2576#endif
2577 case MPT3COMMAND:
2578 {
2579 struct mpt3_ioctl_command __user *uarg;
2580 struct mpt3_ioctl_command karg;
2581
2582#ifdef CONFIG_COMPAT
2583 if (compat) {
2584 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2585 break;
2586 }
2587#endif
2588 if (copy_from_user(&karg, arg, sizeof(karg))) {
2589 pr_err("failure at %s:%d/%s()!\n",
2590 __FILE__, __LINE__, __func__);
2591 ret = -EFAULT;
2592 break;
2593 }
2594
Gen Zhangf9e3ebe2019-05-30 09:10:30 +08002595 if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2596 ret = -EINVAL;
2597 break;
2598 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302599 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2600 uarg = arg;
2601 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2602 }
2603 break;
2604 }
2605 case MPT3EVENTQUERY:
2606 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2607 ret = _ctl_eventquery(ioc, arg);
2608 break;
2609 case MPT3EVENTENABLE:
2610 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2611 ret = _ctl_eventenable(ioc, arg);
2612 break;
2613 case MPT3EVENTREPORT:
2614 ret = _ctl_eventreport(ioc, arg);
2615 break;
2616 case MPT3HARDRESET:
2617 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2618 ret = _ctl_do_reset(ioc, arg);
2619 break;
2620 case MPT3BTDHMAPPING:
2621 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2622 ret = _ctl_btdh_mapping(ioc, arg);
2623 break;
2624 case MPT3DIAGREGISTER:
2625 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2626 ret = _ctl_diag_register(ioc, arg);
2627 break;
2628 case MPT3DIAGUNREGISTER:
2629 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2630 ret = _ctl_diag_unregister(ioc, arg);
2631 break;
2632 case MPT3DIAGQUERY:
2633 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2634 ret = _ctl_diag_query(ioc, arg);
2635 break;
2636 case MPT3DIAGRELEASE:
2637 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2638 ret = _ctl_diag_release(ioc, arg);
2639 break;
2640 case MPT3DIAGREADBUFFER:
2641 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2642 ret = _ctl_diag_read_buffer(ioc, arg);
2643 break;
2644 default:
Joe Perches919d8a32018-09-17 08:01:09 -07002645 dctlprintk(ioc,
2646 ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2647 cmd));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302648 break;
2649 }
2650
2651 mutex_unlock(&ioc->ctl_cmds.mutex);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302652out_unlock_pciaccess:
2653 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302654 return ret;
2655}
2656
2657/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302658 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002659 * @file: (struct file)
2660 * @cmd: ioctl opcode
2661 * @arg: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302662 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002663static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302664_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302665{
2666 long ret;
2667
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302668 /* pass MPI25_VERSION | MPI26_VERSION value,
2669 * to indicate that this ioctl cmd
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302670 * came from mpt3ctl ioctl device.
2671 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302672 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2673 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302674 return ret;
2675}
2676
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302677/**
2678 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002679 * @file: (struct file)
2680 * @cmd: ioctl opcode
2681 * @arg: ?
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302682 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002683static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302684_ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2685{
2686 long ret;
2687
2688 /* pass MPI2_VERSION value, to indicate that this ioctl cmd
2689 * came from mpt2ctl ioctl device.
2690 */
2691 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2692 return ret;
2693}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302694#ifdef CONFIG_COMPAT
2695/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302696 *_ ctl_ioctl_compat - main ioctl entry point (compat)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002697 * @file: ?
2698 * @cmd: ?
2699 * @arg: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302700 *
2701 * This routine handles 32 bit applications in 64bit os.
2702 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002703static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302704_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302705{
2706 long ret;
2707
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302708 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2709 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302710 return ret;
2711}
2712
2713/**
2714 *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002715 * @file: ?
2716 * @cmd: ?
2717 * @arg: ?
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302718 *
2719 * This routine handles 32 bit applications in 64bit os.
2720 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002721static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302722_ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2723{
2724 long ret;
2725
2726 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302727 return ret;
2728}
2729#endif
2730
2731/* scsi host attributes */
2732/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002733 * version_fw_show - firmware version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002734 * @cdev: pointer to embedded class device
2735 * @attr: ?
2736 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302737 *
2738 * A sysfs 'read-only' shost attribute.
2739 */
2740static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002741version_fw_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302742 char *buf)
2743{
2744 struct Scsi_Host *shost = class_to_shost(cdev);
2745 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2746
2747 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2748 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2749 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2750 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2751 ioc->facts.FWVersion.Word & 0x000000FF);
2752}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002753static DEVICE_ATTR_RO(version_fw);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302754
2755/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002756 * version_bios_show - bios version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002757 * @cdev: pointer to embedded class device
2758 * @attr: ?
2759 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302760 *
2761 * A sysfs 'read-only' shost attribute.
2762 */
2763static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002764version_bios_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302765 char *buf)
2766{
2767 struct Scsi_Host *shost = class_to_shost(cdev);
2768 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2769
2770 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2771
2772 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2773 (version & 0xFF000000) >> 24,
2774 (version & 0x00FF0000) >> 16,
2775 (version & 0x0000FF00) >> 8,
2776 version & 0x000000FF);
2777}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002778static DEVICE_ATTR_RO(version_bios);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302779
2780/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002781 * version_mpi_show - MPI (message passing interface) version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002782 * @cdev: pointer to embedded class device
2783 * @attr: ?
2784 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302785 *
2786 * A sysfs 'read-only' shost attribute.
2787 */
2788static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002789version_mpi_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302790 char *buf)
2791{
2792 struct Scsi_Host *shost = class_to_shost(cdev);
2793 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2794
2795 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2796 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2797}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002798static DEVICE_ATTR_RO(version_mpi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302799
2800/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002801 * version_product_show - product name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002802 * @cdev: pointer to embedded class device
2803 * @attr: ?
2804 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302805 *
2806 * A sysfs 'read-only' shost attribute.
2807 */
2808static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002809version_product_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302810 char *buf)
2811{
2812 struct Scsi_Host *shost = class_to_shost(cdev);
2813 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2814
2815 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2816}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002817static DEVICE_ATTR_RO(version_product);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302818
2819/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002820 * version_nvdata_persistent_show - ndvata persistent version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002821 * @cdev: pointer to embedded class device
2822 * @attr: ?
2823 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302824 *
2825 * A sysfs 'read-only' shost attribute.
2826 */
2827static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002828version_nvdata_persistent_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302829 struct device_attribute *attr, char *buf)
2830{
2831 struct Scsi_Host *shost = class_to_shost(cdev);
2832 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2833
2834 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2835 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2836}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002837static DEVICE_ATTR_RO(version_nvdata_persistent);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302838
2839/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002840 * version_nvdata_default_show - nvdata default version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002841 * @cdev: pointer to embedded class device
2842 * @attr: ?
2843 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302844 *
2845 * A sysfs 'read-only' shost attribute.
2846 */
2847static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002848version_nvdata_default_show(struct device *cdev, struct device_attribute
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302849 *attr, char *buf)
2850{
2851 struct Scsi_Host *shost = class_to_shost(cdev);
2852 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2853
2854 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2855 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2856}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002857static DEVICE_ATTR_RO(version_nvdata_default);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302858
2859/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002860 * board_name_show - board name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002861 * @cdev: pointer to embedded class device
2862 * @attr: ?
2863 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302864 *
2865 * A sysfs 'read-only' shost attribute.
2866 */
2867static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002868board_name_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302869 char *buf)
2870{
2871 struct Scsi_Host *shost = class_to_shost(cdev);
2872 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2873
2874 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2875}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002876static DEVICE_ATTR_RO(board_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302877
2878/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002879 * board_assembly_show - board assembly 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 +02002887board_assembly_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.BoardAssembly);
2894}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002895static DEVICE_ATTR_RO(board_assembly);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302896
2897/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002898 * board_tracer_show - board tracer number
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 +02002906board_tracer_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302907 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, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2913}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002914static DEVICE_ATTR_RO(board_tracer);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302915
2916/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002917 * io_delay_show - io missing delay
Bart Van Assche4beb4862018-06-15 14:42:01 -07002918 * @cdev: pointer to embedded class device
2919 * @attr: ?
2920 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302921 *
2922 * This is for firmware implemention for deboucing device
2923 * removal events.
2924 *
2925 * A sysfs 'read-only' shost attribute.
2926 */
2927static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002928io_delay_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302929 char *buf)
2930{
2931 struct Scsi_Host *shost = class_to_shost(cdev);
2932 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2933
2934 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2935}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002936static DEVICE_ATTR_RO(io_delay);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302937
2938/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002939 * device_delay_show - device missing delay
Bart Van Assche4beb4862018-06-15 14:42:01 -07002940 * @cdev: pointer to embedded class device
2941 * @attr: ?
2942 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302943 *
2944 * This is for firmware implemention for deboucing device
2945 * removal events.
2946 *
2947 * A sysfs 'read-only' shost attribute.
2948 */
2949static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002950device_delay_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302951 char *buf)
2952{
2953 struct Scsi_Host *shost = class_to_shost(cdev);
2954 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2955
2956 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2957}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002958static DEVICE_ATTR_RO(device_delay);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302959
2960/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002961 * fw_queue_depth_show - global credits
Bart Van Assche4beb4862018-06-15 14:42:01 -07002962 * @cdev: pointer to embedded class device
2963 * @attr: ?
2964 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302965 *
2966 * This is firmware queue depth limit
2967 *
2968 * A sysfs 'read-only' shost attribute.
2969 */
2970static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002971fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302972 char *buf)
2973{
2974 struct Scsi_Host *shost = class_to_shost(cdev);
2975 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2976
2977 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2978}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002979static DEVICE_ATTR_RO(fw_queue_depth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302980
2981/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002982 * sas_address_show - sas address
Bart Van Assche4beb4862018-06-15 14:42:01 -07002983 * @cdev: pointer to embedded class device
2984 * @attr: ?
2985 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302986 *
2987 * This is the controller sas address
2988 *
2989 * A sysfs 'read-only' shost attribute.
2990 */
2991static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002992host_sas_address_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302993 char *buf)
2994
2995{
2996 struct Scsi_Host *shost = class_to_shost(cdev);
2997 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2998
2999 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3000 (unsigned long long)ioc->sas_hba.sas_address);
3001}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003002static DEVICE_ATTR_RO(host_sas_address);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303003
3004/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003005 * logging_level_show - logging level
Bart Van Assche4beb4862018-06-15 14:42:01 -07003006 * @cdev: pointer to embedded class device
3007 * @attr: ?
3008 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303009 *
3010 * A sysfs 'read/write' shost attribute.
3011 */
3012static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003013logging_level_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303014 char *buf)
3015{
3016 struct Scsi_Host *shost = class_to_shost(cdev);
3017 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3018
3019 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
3020}
3021static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003022logging_level_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303023 const char *buf, size_t count)
3024{
3025 struct Scsi_Host *shost = class_to_shost(cdev);
3026 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3027 int val = 0;
3028
3029 if (sscanf(buf, "%x", &val) != 1)
3030 return -EINVAL;
3031
3032 ioc->logging_level = val;
Joe Perches919d8a32018-09-17 08:01:09 -07003033 ioc_info(ioc, "logging_level=%08xh\n",
3034 ioc->logging_level);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303035 return strlen(buf);
3036}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003037static DEVICE_ATTR_RW(logging_level);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303038
3039/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003040 * fwfault_debug_show - show/store fwfault_debug
Bart Van Assche4beb4862018-06-15 14:42:01 -07003041 * @cdev: pointer to embedded class device
3042 * @attr: ?
3043 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303044 *
3045 * mpt3sas_fwfault_debug is command line option
3046 * A sysfs 'read/write' shost attribute.
3047 */
3048static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003049fwfault_debug_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, "%d\n", ioc->fwfault_debug);
3056}
3057static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003058fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303059 const char *buf, size_t count)
3060{
3061 struct Scsi_Host *shost = class_to_shost(cdev);
3062 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3063 int val = 0;
3064
3065 if (sscanf(buf, "%d", &val) != 1)
3066 return -EINVAL;
3067
3068 ioc->fwfault_debug = val;
Joe Perches919d8a32018-09-17 08:01:09 -07003069 ioc_info(ioc, "fwfault_debug=%d\n",
3070 ioc->fwfault_debug);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303071 return strlen(buf);
3072}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003073static DEVICE_ATTR_RW(fwfault_debug);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303074
3075/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003076 * ioc_reset_count_show - ioc reset count
Bart Van Assche4beb4862018-06-15 14:42:01 -07003077 * @cdev: pointer to embedded class device
3078 * @attr: ?
3079 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303080 *
3081 * This is firmware queue depth limit
3082 *
3083 * A sysfs 'read-only' shost attribute.
3084 */
3085static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003086ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303087 char *buf)
3088{
3089 struct Scsi_Host *shost = class_to_shost(cdev);
3090 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3091
3092 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
3093}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003094static DEVICE_ATTR_RO(ioc_reset_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303095
3096/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003097 * reply_queue_count_show - number of reply queues
Bart Van Assche4beb4862018-06-15 14:42:01 -07003098 * @cdev: pointer to embedded class device
3099 * @attr: ?
3100 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303101 *
3102 * This is number of reply queues
3103 *
3104 * A sysfs 'read-only' shost attribute.
3105 */
3106static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003107reply_queue_count_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303108 struct device_attribute *attr, char *buf)
3109{
3110 u8 reply_queue_count;
3111 struct Scsi_Host *shost = class_to_shost(cdev);
3112 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3113
3114 if ((ioc->facts.IOCCapabilities &
3115 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
3116 reply_queue_count = ioc->reply_queue_count;
3117 else
3118 reply_queue_count = 1;
3119
3120 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
3121}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003122static DEVICE_ATTR_RO(reply_queue_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303123
Sreekanth Reddy42263092015-11-11 17:30:29 +05303124/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003125 * BRM_status_show - Backup Rail Monitor Status
Bart Van Assche4beb4862018-06-15 14:42:01 -07003126 * @cdev: pointer to embedded class device
3127 * @attr: ?
3128 * @buf: the buffer returned
Sreekanth Reddy42263092015-11-11 17:30:29 +05303129 *
3130 * This is number of reply queues
3131 *
3132 * A sysfs 'read-only' shost attribute.
3133 */
3134static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003135BRM_status_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddy42263092015-11-11 17:30:29 +05303136 char *buf)
3137{
3138 struct Scsi_Host *shost = class_to_shost(cdev);
3139 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3140 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
3141 Mpi2ConfigReply_t mpi_reply;
3142 u16 backup_rail_monitor_status = 0;
3143 u16 ioc_status;
3144 int sz;
3145 ssize_t rc = 0;
3146
3147 if (!ioc->is_warpdrive) {
Joe Perches919d8a32018-09-17 08:01:09 -07003148 ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
3149 __func__);
Damien Le Moalcb551b82020-07-01 17:52:54 +09003150 return 0;
Sreekanth Reddy42263092015-11-11 17:30:29 +05303151 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05303152 /* pci_access_mutex lock acquired by sysfs show path */
3153 mutex_lock(&ioc->pci_access_mutex);
Johannes Thumshirn0fd18142020-07-01 22:14:54 +09003154 if (ioc->pci_error_recovery || ioc->remove_host)
3155 goto out;
Sreekanth Reddy42263092015-11-11 17:30:29 +05303156
3157 /* allocate upto GPIOVal 36 entries */
3158 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
3159 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
3160 if (!io_unit_pg3) {
Johannes Thumshirn0fd18142020-07-01 22:14:54 +09003161 rc = -ENOMEM;
Joe Perches919d8a32018-09-17 08:01:09 -07003162 ioc_err(ioc, "%s: failed allocating memory for iounit_pg3: (%d) bytes\n",
3163 __func__, sz);
Sreekanth Reddy42263092015-11-11 17:30:29 +05303164 goto out;
3165 }
3166
3167 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
3168 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003169 ioc_err(ioc, "%s: failed reading iounit_pg3\n",
3170 __func__);
Johannes Thumshirn0fd18142020-07-01 22:14:54 +09003171 rc = -EINVAL;
Sreekanth Reddy42263092015-11-11 17:30:29 +05303172 goto out;
3173 }
3174
3175 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3176 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
Joe Perches919d8a32018-09-17 08:01:09 -07003177 ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
3178 __func__, ioc_status);
Johannes Thumshirn0fd18142020-07-01 22:14:54 +09003179 rc = -EINVAL;
Sreekanth Reddy42263092015-11-11 17:30:29 +05303180 goto out;
3181 }
3182
3183 if (io_unit_pg3->GPIOCount < 25) {
Joe Perches919d8a32018-09-17 08:01:09 -07003184 ioc_err(ioc, "%s: iounit_pg3->GPIOCount less than 25 entries, detected (%d) entries\n",
3185 __func__, io_unit_pg3->GPIOCount);
Johannes Thumshirn0fd18142020-07-01 22:14:54 +09003186 rc = -EINVAL;
Sreekanth Reddy42263092015-11-11 17:30:29 +05303187 goto out;
3188 }
3189
3190 /* BRM status is in bit zero of GPIOVal[24] */
3191 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
3192 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
3193
3194 out:
3195 kfree(io_unit_pg3);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05303196 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddy42263092015-11-11 17:30:29 +05303197 return rc;
3198}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003199static DEVICE_ATTR_RO(BRM_status);
Sreekanth Reddy42263092015-11-11 17:30:29 +05303200
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303201struct DIAG_BUFFER_START {
3202 __le32 Size;
3203 __le32 DiagVersion;
3204 u8 BufferType;
3205 u8 Reserved[3];
3206 __le32 Reserved1;
3207 __le32 Reserved2;
3208 __le32 Reserved3;
3209};
3210
3211/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003212 * host_trace_buffer_size_show - host buffer size (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07003213 * @cdev: pointer to embedded class device
3214 * @attr: ?
3215 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303216 *
3217 * A sysfs 'read-only' shost attribute.
3218 */
3219static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003220host_trace_buffer_size_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303221 struct device_attribute *attr, char *buf)
3222{
3223 struct Scsi_Host *shost = class_to_shost(cdev);
3224 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3225 u32 size = 0;
3226 struct DIAG_BUFFER_START *request_data;
3227
3228 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
Joe Perches919d8a32018-09-17 08:01:09 -07003229 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3230 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303231 return 0;
3232 }
3233
3234 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3235 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003236 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3237 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303238 return 0;
3239 }
3240
3241 request_data = (struct DIAG_BUFFER_START *)
3242 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
3243 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
3244 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
3245 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
3246 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
3247 size = le32_to_cpu(request_data->Size);
3248
3249 ioc->ring_buffer_sz = size;
3250 return snprintf(buf, PAGE_SIZE, "%d\n", size);
3251}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003252static DEVICE_ATTR_RO(host_trace_buffer_size);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303253
3254/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003255 * host_trace_buffer_show - firmware ring buffer (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07003256 * @cdev: pointer to embedded class device
3257 * @attr: ?
3258 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303259 *
3260 * A sysfs 'read/write' shost attribute.
3261 *
3262 * You will only be able to read 4k bytes of ring buffer at a time.
3263 * In order to read beyond 4k bytes, you will have to write out the
3264 * offset to the same attribute, it will move the pointer.
3265 */
3266static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003267host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303268 char *buf)
3269{
3270 struct Scsi_Host *shost = class_to_shost(cdev);
3271 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3272 void *request_data;
3273 u32 size;
3274
3275 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
Joe Perches919d8a32018-09-17 08:01:09 -07003276 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3277 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303278 return 0;
3279 }
3280
3281 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3282 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003283 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3284 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303285 return 0;
3286 }
3287
3288 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3289 return 0;
3290
3291 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3292 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3293 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3294 memcpy(buf, request_data, size);
3295 return size;
3296}
3297
3298static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003299host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303300 const char *buf, size_t count)
3301{
3302 struct Scsi_Host *shost = class_to_shost(cdev);
3303 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3304 int val = 0;
3305
3306 if (sscanf(buf, "%d", &val) != 1)
3307 return -EINVAL;
3308
3309 ioc->ring_buffer_offset = val;
3310 return strlen(buf);
3311}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003312static DEVICE_ATTR_RW(host_trace_buffer);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303313
3314
3315/*****************************************/
3316
3317/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003318 * host_trace_buffer_enable_show - firmware ring buffer (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07003319 * @cdev: pointer to embedded class device
3320 * @attr: ?
3321 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303322 *
3323 * A sysfs 'read/write' shost attribute.
3324 *
3325 * This is a mechnism to post/release host_trace_buffers
3326 */
3327static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003328host_trace_buffer_enable_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303329 struct device_attribute *attr, char *buf)
3330{
3331 struct Scsi_Host *shost = class_to_shost(cdev);
3332 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3333
3334 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3335 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3336 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3337 return snprintf(buf, PAGE_SIZE, "off\n");
3338 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3339 MPT3_DIAG_BUFFER_IS_RELEASED))
3340 return snprintf(buf, PAGE_SIZE, "release\n");
3341 else
3342 return snprintf(buf, PAGE_SIZE, "post\n");
3343}
3344
3345static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003346host_trace_buffer_enable_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303347 struct device_attribute *attr, const char *buf, size_t count)
3348{
3349 struct Scsi_Host *shost = class_to_shost(cdev);
3350 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3351 char str[10] = "";
3352 struct mpt3_diag_register diag_register;
3353 u8 issue_reset = 0;
3354
3355 /* don't allow post/release occurr while recovery is active */
3356 if (ioc->shost_recovery || ioc->remove_host ||
3357 ioc->pci_error_recovery || ioc->is_driver_loading)
3358 return -EBUSY;
3359
3360 if (sscanf(buf, "%9s", str) != 1)
3361 return -EINVAL;
3362
3363 if (!strcmp(str, "post")) {
3364 /* exit out if host buffers are already posted */
3365 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3366 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3367 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3368 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3369 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3370 goto out;
3371 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
Joe Perches919d8a32018-09-17 08:01:09 -07003372 ioc_info(ioc, "posting host trace buffers\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303373 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04003374
3375 if (ioc->manu_pg11.HostTraceBufferMaxSizeKB != 0 &&
3376 ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0) {
3377 /* post the same buffer allocated previously */
3378 diag_register.requested_buffer_size =
3379 ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE];
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04003380 } else {
3381 /*
3382 * Free the diag buffer memory which was previously
3383 * allocated by an application.
3384 */
3385 if ((ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0)
3386 &&
3387 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3388 MPT3_DIAG_BUFFER_IS_APP_OWNED)) {
Suraj Upadhyaya5a20c42020-07-29 23:40:21 +05303389 dma_free_coherent(&ioc->pdev->dev,
3390 ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE],
3391 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE],
3392 ioc->diag_buffer_dma[MPI2_DIAG_BUF_TYPE_TRACE]);
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04003393 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE] =
3394 NULL;
3395 }
3396
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04003397 diag_register.requested_buffer_size = (1024 * 1024);
Sreekanth Reddya8a6cbc2019-09-13 09:04:45 -04003398 }
Sreekanth Reddyd04a6ed2019-09-13 09:04:38 -04003399
Sreekanth Reddy08e73782019-09-13 09:04:42 -04003400 diag_register.unique_id =
3401 (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
3402 (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303403 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3404 _ctl_diag_register_2(ioc, &diag_register);
Sreekanth Reddya066f4c2019-09-13 09:04:44 -04003405 if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3406 MPT3_DIAG_BUFFER_IS_REGISTERED) {
3407 ioc_info(ioc,
3408 "Trace buffer %d KB allocated through sysfs\n",
3409 diag_register.requested_buffer_size>>10);
3410 if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
3411 ioc->diag_buffer_status[
3412 MPI2_DIAG_BUF_TYPE_TRACE] |=
3413 MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
3414 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303415 } else if (!strcmp(str, "release")) {
3416 /* exit out if host buffers are already released */
3417 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3418 goto out;
3419 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3420 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3421 goto out;
3422 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3423 MPT3_DIAG_BUFFER_IS_RELEASED))
3424 goto out;
Joe Perches919d8a32018-09-17 08:01:09 -07003425 ioc_info(ioc, "releasing host trace buffer\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303426 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3427 &issue_reset);
3428 }
3429
3430 out:
3431 return strlen(buf);
3432}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003433static DEVICE_ATTR_RW(host_trace_buffer_enable);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303434
3435/*********** diagnostic trigger suppport *********************************/
3436
3437/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003438 * diag_trigger_master_show - show the diag_trigger_master attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003439 * @cdev: pointer to embedded class device
3440 * @attr: ?
3441 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303442 *
3443 * A sysfs 'read/write' shost attribute.
3444 */
3445static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003446diag_trigger_master_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303447 struct device_attribute *attr, char *buf)
3448
3449{
3450 struct Scsi_Host *shost = class_to_shost(cdev);
3451 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3452 unsigned long flags;
3453 ssize_t rc;
3454
3455 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3456 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3457 memcpy(buf, &ioc->diag_trigger_master, rc);
3458 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3459 return rc;
3460}
3461
3462/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003463 * diag_trigger_master_store - store the diag_trigger_master attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003464 * @cdev: pointer to embedded class device
3465 * @attr: ?
3466 * @buf: the buffer returned
3467 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303468 *
3469 * A sysfs 'read/write' shost attribute.
3470 */
3471static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003472diag_trigger_master_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303473 struct device_attribute *attr, const char *buf, size_t count)
3474
3475{
3476 struct Scsi_Host *shost = class_to_shost(cdev);
3477 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3478 unsigned long flags;
3479 ssize_t rc;
3480
3481 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3482 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3483 memset(&ioc->diag_trigger_master, 0,
3484 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3485 memcpy(&ioc->diag_trigger_master, buf, rc);
3486 ioc->diag_trigger_master.MasterData |=
3487 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3488 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3489 return rc;
3490}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003491static DEVICE_ATTR_RW(diag_trigger_master);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303492
3493
3494/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003495 * diag_trigger_event_show - show the diag_trigger_event attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003496 * @cdev: pointer to embedded class device
3497 * @attr: ?
3498 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303499 *
3500 * A sysfs 'read/write' shost attribute.
3501 */
3502static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003503diag_trigger_event_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303504 struct device_attribute *attr, char *buf)
3505{
3506 struct Scsi_Host *shost = class_to_shost(cdev);
3507 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3508 unsigned long flags;
3509 ssize_t rc;
3510
3511 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3512 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3513 memcpy(buf, &ioc->diag_trigger_event, rc);
3514 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3515 return rc;
3516}
3517
3518/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003519 * diag_trigger_event_store - store the diag_trigger_event attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003520 * @cdev: pointer to embedded class device
3521 * @attr: ?
3522 * @buf: the buffer returned
3523 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303524 *
3525 * A sysfs 'read/write' shost attribute.
3526 */
3527static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003528diag_trigger_event_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303529 struct device_attribute *attr, const char *buf, size_t count)
3530
3531{
3532 struct Scsi_Host *shost = class_to_shost(cdev);
3533 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3534 unsigned long flags;
3535 ssize_t sz;
3536
3537 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3538 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3539 memset(&ioc->diag_trigger_event, 0,
3540 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3541 memcpy(&ioc->diag_trigger_event, buf, sz);
3542 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3543 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3544 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3545 return sz;
3546}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003547static DEVICE_ATTR_RW(diag_trigger_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303548
3549
3550/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003551 * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003552 * @cdev: pointer to embedded class device
3553 * @attr: ?
3554 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303555 *
3556 * A sysfs 'read/write' shost attribute.
3557 */
3558static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003559diag_trigger_scsi_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303560 struct device_attribute *attr, char *buf)
3561{
3562 struct Scsi_Host *shost = class_to_shost(cdev);
3563 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3564 unsigned long flags;
3565 ssize_t rc;
3566
3567 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3568 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3569 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3570 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3571 return rc;
3572}
3573
3574/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003575 * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003576 * @cdev: pointer to embedded class device
3577 * @attr: ?
3578 * @buf: the buffer returned
3579 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303580 *
3581 * A sysfs 'read/write' shost attribute.
3582 */
3583static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003584diag_trigger_scsi_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303585 struct device_attribute *attr, const char *buf, size_t count)
3586{
3587 struct Scsi_Host *shost = class_to_shost(cdev);
3588 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3589 unsigned long flags;
3590 ssize_t sz;
3591
3592 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
Dan Carpenter1de540a2019-07-26 09:52:05 +03003593 sz = min(sizeof(ioc->diag_trigger_scsi), count);
3594 memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303595 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3596 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3597 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3598 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3599 return sz;
3600}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003601static DEVICE_ATTR_RW(diag_trigger_scsi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303602
3603
3604/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003605 * diag_trigger_scsi_show - show the diag_trigger_mpi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003606 * @cdev: pointer to embedded class device
3607 * @attr: ?
3608 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303609 *
3610 * A sysfs 'read/write' shost attribute.
3611 */
3612static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003613diag_trigger_mpi_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303614 struct device_attribute *attr, char *buf)
3615{
3616 struct Scsi_Host *shost = class_to_shost(cdev);
3617 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3618 unsigned long flags;
3619 ssize_t rc;
3620
3621 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3622 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3623 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3624 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3625 return rc;
3626}
3627
3628/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003629 * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003630 * @cdev: pointer to embedded class device
3631 * @attr: ?
3632 * @buf: the buffer returned
3633 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303634 *
3635 * A sysfs 'read/write' shost attribute.
3636 */
3637static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003638diag_trigger_mpi_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303639 struct device_attribute *attr, const char *buf, size_t count)
3640{
3641 struct Scsi_Host *shost = class_to_shost(cdev);
3642 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3643 unsigned long flags;
3644 ssize_t sz;
3645
3646 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3647 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3648 memset(&ioc->diag_trigger_mpi, 0,
Dan Carpenter66331e82012-12-07 13:56:22 +03003649 sizeof(ioc->diag_trigger_mpi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303650 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3651 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3652 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3653 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3654 return sz;
3655}
3656
Tomas Henzlc9df1442019-06-14 16:41:44 +02003657static DEVICE_ATTR_RW(diag_trigger_mpi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303658
3659/*********** diagnostic trigger suppport *** END ****************************/
3660
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303661/*****************************************/
3662
Suganath Prabu3ac8e472019-08-03 09:59:53 -04003663/**
3664 * drv_support_bitmap_show - driver supported feature bitmap
Damien Le Moal9133d272020-07-06 21:33:58 +09003665 * @cdev: pointer to embedded class device
3666 * @attr: unused
3667 * @buf: the buffer returned
Suganath Prabu3ac8e472019-08-03 09:59:53 -04003668 *
3669 * A sysfs 'read-only' shost attribute.
3670 */
3671static ssize_t
3672drv_support_bitmap_show(struct device *cdev,
3673 struct device_attribute *attr, char *buf)
3674{
3675 struct Scsi_Host *shost = class_to_shost(cdev);
3676 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3677
3678 return snprintf(buf, PAGE_SIZE, "0x%08x\n", ioc->drv_support_bitmap);
3679}
3680static DEVICE_ATTR_RO(drv_support_bitmap);
3681
Sreekanth Reddy8dc8d292019-08-22 02:19:01 -04003682/**
3683 * enable_sdev_max_qd_show - display whether sdev max qd is enabled/disabled
Damien Le Moal9133d272020-07-06 21:33:58 +09003684 * @cdev: pointer to embedded class device
3685 * @attr: unused
3686 * @buf: the buffer returned
Sreekanth Reddy8dc8d292019-08-22 02:19:01 -04003687 *
3688 * A sysfs read/write shost attribute. This attribute is used to set the
3689 * targets queue depth to HBA IO queue depth if this attribute is enabled.
3690 */
3691static ssize_t
3692enable_sdev_max_qd_show(struct device *cdev,
3693 struct device_attribute *attr, char *buf)
3694{
3695 struct Scsi_Host *shost = class_to_shost(cdev);
3696 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3697
3698 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->enable_sdev_max_qd);
3699}
3700
3701/**
3702 * enable_sdev_max_qd_store - Enable/disable sdev max qd
Damien Le Moal9133d272020-07-06 21:33:58 +09003703 * @cdev: pointer to embedded class device
3704 * @attr: unused
3705 * @buf: the buffer returned
3706 * @count: unused
Sreekanth Reddy8dc8d292019-08-22 02:19:01 -04003707 *
3708 * A sysfs read/write shost attribute. This attribute is used to set the
3709 * targets queue depth to HBA IO queue depth if this attribute is enabled.
3710 * If this attribute is disabled then targets will have corresponding default
3711 * queue depth.
3712 */
3713static ssize_t
3714enable_sdev_max_qd_store(struct device *cdev,
3715 struct device_attribute *attr, const char *buf, size_t count)
3716{
3717 struct Scsi_Host *shost = class_to_shost(cdev);
3718 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3719 struct MPT3SAS_DEVICE *sas_device_priv_data;
3720 struct MPT3SAS_TARGET *sas_target_priv_data;
3721 int val = 0;
3722 struct scsi_device *sdev;
3723 struct _raid_device *raid_device;
3724 int qdepth;
3725
3726 if (kstrtoint(buf, 0, &val) != 0)
3727 return -EINVAL;
3728
3729 switch (val) {
3730 case 0:
3731 ioc->enable_sdev_max_qd = 0;
3732 shost_for_each_device(sdev, ioc->shost) {
3733 sas_device_priv_data = sdev->hostdata;
3734 if (!sas_device_priv_data)
3735 continue;
3736 sas_target_priv_data = sas_device_priv_data->sas_target;
3737 if (!sas_target_priv_data)
3738 continue;
3739
3740 if (sas_target_priv_data->flags &
3741 MPT_TARGET_FLAGS_VOLUME) {
3742 raid_device =
3743 mpt3sas_raid_device_find_by_handle(ioc,
3744 sas_target_priv_data->handle);
3745
3746 switch (raid_device->volume_type) {
3747 case MPI2_RAID_VOL_TYPE_RAID0:
3748 if (raid_device->device_info &
3749 MPI2_SAS_DEVICE_INFO_SSP_TARGET)
3750 qdepth =
3751 MPT3SAS_SAS_QUEUE_DEPTH;
3752 else
3753 qdepth =
3754 MPT3SAS_SATA_QUEUE_DEPTH;
3755 break;
3756 case MPI2_RAID_VOL_TYPE_RAID1E:
3757 case MPI2_RAID_VOL_TYPE_RAID1:
3758 case MPI2_RAID_VOL_TYPE_RAID10:
3759 case MPI2_RAID_VOL_TYPE_UNKNOWN:
3760 default:
3761 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
3762 }
3763 } else if (sas_target_priv_data->flags &
3764 MPT_TARGET_FLAGS_PCIE_DEVICE)
3765 qdepth = MPT3SAS_NVME_QUEUE_DEPTH;
3766 else
3767 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
3768
3769 mpt3sas_scsih_change_queue_depth(sdev, qdepth);
3770 }
3771 break;
3772 case 1:
3773 ioc->enable_sdev_max_qd = 1;
3774 shost_for_each_device(sdev, ioc->shost)
3775 mpt3sas_scsih_change_queue_depth(sdev,
3776 shost->can_queue);
3777 break;
3778 default:
3779 return -EINVAL;
3780 }
3781
3782 return strlen(buf);
3783}
3784static DEVICE_ATTR_RW(enable_sdev_max_qd);
3785
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303786struct device_attribute *mpt3sas_host_attrs[] = {
3787 &dev_attr_version_fw,
3788 &dev_attr_version_bios,
3789 &dev_attr_version_mpi,
3790 &dev_attr_version_product,
3791 &dev_attr_version_nvdata_persistent,
3792 &dev_attr_version_nvdata_default,
3793 &dev_attr_board_name,
3794 &dev_attr_board_assembly,
3795 &dev_attr_board_tracer,
3796 &dev_attr_io_delay,
3797 &dev_attr_device_delay,
3798 &dev_attr_logging_level,
3799 &dev_attr_fwfault_debug,
3800 &dev_attr_fw_queue_depth,
3801 &dev_attr_host_sas_address,
3802 &dev_attr_ioc_reset_count,
3803 &dev_attr_host_trace_buffer_size,
3804 &dev_attr_host_trace_buffer,
3805 &dev_attr_host_trace_buffer_enable,
3806 &dev_attr_reply_queue_count,
3807 &dev_attr_diag_trigger_master,
3808 &dev_attr_diag_trigger_event,
3809 &dev_attr_diag_trigger_scsi,
3810 &dev_attr_diag_trigger_mpi,
Suganath Prabu3ac8e472019-08-03 09:59:53 -04003811 &dev_attr_drv_support_bitmap,
Sreekanth Reddy42263092015-11-11 17:30:29 +05303812 &dev_attr_BRM_status,
Sreekanth Reddy8dc8d292019-08-22 02:19:01 -04003813 &dev_attr_enable_sdev_max_qd,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303814 NULL,
3815};
3816
3817/* device attributes */
3818
3819/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003820 * sas_address_show - sas address
Bart Van Assche4beb4862018-06-15 14:42:01 -07003821 * @dev: pointer to embedded class device
3822 * @attr: ?
3823 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303824 *
3825 * This is the sas address for the target
3826 *
3827 * A sysfs 'read-only' shost attribute.
3828 */
3829static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003830sas_address_show(struct device *dev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303831 char *buf)
3832{
3833 struct scsi_device *sdev = to_scsi_device(dev);
3834 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3835
3836 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3837 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3838}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003839static DEVICE_ATTR_RO(sas_address);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303840
3841/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003842 * sas_device_handle_show - device handle
Bart Van Assche4beb4862018-06-15 14:42:01 -07003843 * @dev: pointer to embedded class device
3844 * @attr: ?
3845 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303846 *
3847 * This is the firmware assigned device handle
3848 *
3849 * A sysfs 'read-only' shost attribute.
3850 */
3851static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003852sas_device_handle_show(struct device *dev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303853 char *buf)
3854{
3855 struct scsi_device *sdev = to_scsi_device(dev);
3856 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3857
3858 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3859 sas_device_priv_data->sas_target->handle);
3860}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003861static DEVICE_ATTR_RO(sas_device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303862
Adam Manzanares307d9072016-12-12 16:31:40 -08003863/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003864 * sas_ncq_io_prio_show - send prioritized io commands to device
Bart Van Assche4beb4862018-06-15 14:42:01 -07003865 * @dev: pointer to embedded device
3866 * @attr: ?
3867 * @buf: the buffer returned
Adam Manzanares307d9072016-12-12 16:31:40 -08003868 *
3869 * A sysfs 'read/write' sdev attribute, only works with SATA
3870 */
3871static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003872sas_ncq_prio_enable_show(struct device *dev,
Adam Manzanares307d9072016-12-12 16:31:40 -08003873 struct device_attribute *attr, char *buf)
3874{
3875 struct scsi_device *sdev = to_scsi_device(dev);
3876 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3877
3878 return snprintf(buf, PAGE_SIZE, "%d\n",
3879 sas_device_priv_data->ncq_prio_enable);
3880}
3881
3882static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003883sas_ncq_prio_enable_store(struct device *dev,
Adam Manzanares307d9072016-12-12 16:31:40 -08003884 struct device_attribute *attr,
3885 const char *buf, size_t count)
3886{
3887 struct scsi_device *sdev = to_scsi_device(dev);
3888 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3889 bool ncq_prio_enable = 0;
3890
3891 if (kstrtobool(buf, &ncq_prio_enable))
3892 return -EINVAL;
3893
3894 if (!scsih_ncq_prio_supp(sdev))
3895 return -EINVAL;
3896
3897 sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
3898 return strlen(buf);
3899}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003900static DEVICE_ATTR_RW(sas_ncq_prio_enable);
Adam Manzanares307d9072016-12-12 16:31:40 -08003901
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303902struct device_attribute *mpt3sas_dev_attrs[] = {
3903 &dev_attr_sas_address,
3904 &dev_attr_sas_device_handle,
Adam Manzanares307d9072016-12-12 16:31:40 -08003905 &dev_attr_sas_ncq_prio_enable,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303906 NULL,
3907};
3908
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303909/* file operations table for mpt3ctl device */
3910static const struct file_operations ctl_fops = {
3911 .owner = THIS_MODULE,
3912 .unlocked_ioctl = _ctl_ioctl,
3913 .poll = _ctl_poll,
3914 .fasync = _ctl_fasync,
3915#ifdef CONFIG_COMPAT
3916 .compat_ioctl = _ctl_ioctl_compat,
3917#endif
3918};
3919
3920/* file operations table for mpt2ctl device */
3921static const struct file_operations ctl_gen2_fops = {
3922 .owner = THIS_MODULE,
3923 .unlocked_ioctl = _ctl_mpt2_ioctl,
3924 .poll = _ctl_poll,
3925 .fasync = _ctl_fasync,
3926#ifdef CONFIG_COMPAT
3927 .compat_ioctl = _ctl_mpt2_ioctl_compat,
3928#endif
3929};
3930
3931static struct miscdevice ctl_dev = {
3932 .minor = MPT3SAS_MINOR,
3933 .name = MPT3SAS_DEV_NAME,
3934 .fops = &ctl_fops,
3935};
3936
3937static struct miscdevice gen2_ctl_dev = {
3938 .minor = MPT2SAS_MINOR,
3939 .name = MPT2SAS_DEV_NAME,
3940 .fops = &ctl_gen2_fops,
3941};
3942
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303943/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303944 * mpt3sas_ctl_init - main entry point for ctl.
Bart Van Assche4beb4862018-06-15 14:42:01 -07003945 * @hbas_to_enumerate: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303946 */
3947void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303948mpt3sas_ctl_init(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303949{
3950 async_queue = NULL;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303951
3952 /* Don't register mpt3ctl ioctl device if
3953 * hbas_to_enumarate is one.
3954 */
3955 if (hbas_to_enumerate != 1)
3956 if (misc_register(&ctl_dev) < 0)
3957 pr_err("%s can't register misc device [minor=%d]\n",
3958 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3959
3960 /* Don't register mpt3ctl ioctl device if
3961 * hbas_to_enumarate is two.
3962 */
3963 if (hbas_to_enumerate != 2)
3964 if (misc_register(&gen2_ctl_dev) < 0)
3965 pr_err("%s can't register misc device [minor=%d]\n",
3966 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3967
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303968 init_waitqueue_head(&ctl_poll_wait);
3969}
3970
3971/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303972 * mpt3sas_ctl_exit - exit point for ctl
Bart Van Assche4beb4862018-06-15 14:42:01 -07003973 * @hbas_to_enumerate: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303974 */
3975void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303976mpt3sas_ctl_exit(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303977{
3978 struct MPT3SAS_ADAPTER *ioc;
3979 int i;
3980
3981 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3982
3983 /* free memory associated to diag buffers */
3984 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3985 if (!ioc->diag_buffer[i])
3986 continue;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02003987 dma_free_coherent(&ioc->pdev->dev,
3988 ioc->diag_buffer_sz[i],
3989 ioc->diag_buffer[i],
3990 ioc->diag_buffer_dma[i]);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303991 ioc->diag_buffer[i] = NULL;
3992 ioc->diag_buffer_status[i] = 0;
3993 }
3994
3995 kfree(ioc->event_log);
3996 }
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303997 if (hbas_to_enumerate != 1)
3998 misc_deregister(&ctl_dev);
3999 if (hbas_to_enumerate != 2)
4000 misc_deregister(&gen2_ctl_dev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304001}