blob: b6483b7dd3f70799d8cd0ce7eda284f5aa180527 [file] [log] [blame]
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301/*
2 * Management Module Support for MPT (Message Passing Technology) based
3 * controllers
4 *
5 * This code is based on drivers/scsi/mpt3sas/mpt3sas_ctl.c
Sreekanth Reddya4ffce02014-09-12 15:35:29 +05306 * Copyright (C) 2012-2014 LSI Corporation
Sreekanth Reddya03bd152015-01-12 11:39:02 +05307 * Copyright (C) 2013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * NO WARRANTY
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
30
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
43 * USA.
44 */
45
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053046#include <linux/kernel.h>
47#include <linux/module.h>
48#include <linux/errno.h>
49#include <linux/init.h>
50#include <linux/slab.h>
51#include <linux/types.h>
52#include <linux/pci.h>
53#include <linux/delay.h>
54#include <linux/compat.h>
55#include <linux/poll.h>
56
57#include <linux/io.h>
58#include <linux/uaccess.h>
59
60#include "mpt3sas_base.h"
61#include "mpt3sas_ctl.h"
62
63
64static struct fasync_struct *async_queue;
65static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
66
67
68/**
69 * enum block_state - blocking state
70 * @NON_BLOCKING: non blocking
71 * @BLOCKING: blocking
72 *
73 * These states are for ioctls that need to wait for a response
74 * from firmware, so they probably require sleep.
75 */
76enum block_state {
77 NON_BLOCKING,
78 BLOCKING,
79};
80
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053081/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053082 * _ctl_display_some_debug - debug routine
83 * @ioc: per adapter object
84 * @smid: system request message index
85 * @calling_function_name: string pass from calling function
86 * @mpi_reply: reply message frame
87 * Context: none.
88 *
89 * Function for displaying debug info helpful when debugging issues
90 * in this module.
91 */
92static void
93_ctl_display_some_debug(struct MPT3SAS_ADAPTER *ioc, u16 smid,
94 char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
95{
96 Mpi2ConfigRequest_t *mpi_request;
97 char *desc = NULL;
98
99 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
100 return;
101
102 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
103 switch (mpi_request->Function) {
104 case MPI2_FUNCTION_SCSI_IO_REQUEST:
105 {
106 Mpi2SCSIIORequest_t *scsi_request =
107 (Mpi2SCSIIORequest_t *)mpi_request;
108
109 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
110 "scsi_io, cmd(0x%02x), cdb_len(%d)",
111 scsi_request->CDB.CDB32[0],
112 le16_to_cpu(scsi_request->IoFlags) & 0xF);
113 desc = ioc->tmp_string;
114 break;
115 }
116 case MPI2_FUNCTION_SCSI_TASK_MGMT:
117 desc = "task_mgmt";
118 break;
119 case MPI2_FUNCTION_IOC_INIT:
120 desc = "ioc_init";
121 break;
122 case MPI2_FUNCTION_IOC_FACTS:
123 desc = "ioc_facts";
124 break;
125 case MPI2_FUNCTION_CONFIG:
126 {
127 Mpi2ConfigRequest_t *config_request =
128 (Mpi2ConfigRequest_t *)mpi_request;
129
130 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
131 "config, type(0x%02x), ext_type(0x%02x), number(%d)",
132 (config_request->Header.PageType &
133 MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
134 config_request->Header.PageNumber);
135 desc = ioc->tmp_string;
136 break;
137 }
138 case MPI2_FUNCTION_PORT_FACTS:
139 desc = "port_facts";
140 break;
141 case MPI2_FUNCTION_PORT_ENABLE:
142 desc = "port_enable";
143 break;
144 case MPI2_FUNCTION_EVENT_NOTIFICATION:
145 desc = "event_notification";
146 break;
147 case MPI2_FUNCTION_FW_DOWNLOAD:
148 desc = "fw_download";
149 break;
150 case MPI2_FUNCTION_FW_UPLOAD:
151 desc = "fw_upload";
152 break;
153 case MPI2_FUNCTION_RAID_ACTION:
154 desc = "raid_action";
155 break;
156 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
157 {
158 Mpi2SCSIIORequest_t *scsi_request =
159 (Mpi2SCSIIORequest_t *)mpi_request;
160
161 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
162 "raid_pass, cmd(0x%02x), cdb_len(%d)",
163 scsi_request->CDB.CDB32[0],
164 le16_to_cpu(scsi_request->IoFlags) & 0xF);
165 desc = ioc->tmp_string;
166 break;
167 }
168 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
169 desc = "sas_iounit_cntl";
170 break;
171 case MPI2_FUNCTION_SATA_PASSTHROUGH:
172 desc = "sata_pass";
173 break;
174 case MPI2_FUNCTION_DIAG_BUFFER_POST:
175 desc = "diag_buffer_post";
176 break;
177 case MPI2_FUNCTION_DIAG_RELEASE:
178 desc = "diag_release";
179 break;
180 case MPI2_FUNCTION_SMP_PASSTHROUGH:
181 desc = "smp_passthrough";
182 break;
183 }
184
185 if (!desc)
186 return;
187
Joe Perches919d8a32018-09-17 08:01:09 -0700188 ioc_info(ioc, "%s: %s, smid(%d)\n", calling_function_name, desc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530189
190 if (!mpi_reply)
191 return;
192
193 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
Joe Perches919d8a32018-09-17 08:01:09 -0700194 ioc_info(ioc, "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
195 le16_to_cpu(mpi_reply->IOCStatus),
196 le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530197
198 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
199 mpi_request->Function ==
200 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
201 Mpi2SCSIIOReply_t *scsi_reply =
202 (Mpi2SCSIIOReply_t *)mpi_reply;
203 struct _sas_device *sas_device = NULL;
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530204 struct _pcie_device *pcie_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530205
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530206 sas_device = mpt3sas_get_sdev_by_handle(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530207 le16_to_cpu(scsi_reply->DevHandle));
208 if (sas_device) {
Joe Perches919d8a32018-09-17 08:01:09 -0700209 ioc_warn(ioc, "\tsas_address(0x%016llx), phy(%d)\n",
210 (u64)sas_device->sas_address,
211 sas_device->phy);
212 ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
213 (u64)sas_device->enclosure_logical_id,
214 sas_device->slot);
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530215 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530216 }
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530217 if (!sas_device) {
218 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
219 le16_to_cpu(scsi_reply->DevHandle));
220 if (pcie_device) {
Joe Perches919d8a32018-09-17 08:01:09 -0700221 ioc_warn(ioc, "\tWWID(0x%016llx), port(%d)\n",
222 (unsigned long long)pcie_device->wwid,
223 pcie_device->port_num);
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530224 if (pcie_device->enclosure_handle != 0)
Joe Perches919d8a32018-09-17 08:01:09 -0700225 ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
226 (u64)pcie_device->enclosure_logical_id,
227 pcie_device->slot);
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +0530228 pcie_device_put(pcie_device);
229 }
230 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530231 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
Joe Perches919d8a32018-09-17 08:01:09 -0700232 ioc_info(ioc, "\tscsi_state(0x%02x), scsi_status(0x%02x)\n",
233 scsi_reply->SCSIState,
234 scsi_reply->SCSIStatus);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530235 }
236}
237
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530238/**
239 * mpt3sas_ctl_done - ctl module completion routine
240 * @ioc: per adapter object
241 * @smid: system request message index
242 * @msix_index: MSIX table index supplied by the OS
243 * @reply: reply message frame(lower 32bit addr)
244 * Context: none.
245 *
246 * The callback handler when using ioc->ctl_cb_idx.
247 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700248 * Return: 1 meaning mf should be freed from _base_interrupt
249 * 0 means the mf is freed from this function.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530250 */
251u8
252mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
253 u32 reply)
254{
255 MPI2DefaultReply_t *mpi_reply;
256 Mpi2SCSIIOReply_t *scsiio_reply;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530257 Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530258 const void *sense_data;
259 u32 sz;
260
261 if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
262 return 1;
263 if (ioc->ctl_cmds.smid != smid)
264 return 1;
265 ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
266 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
267 if (mpi_reply) {
268 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
269 ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
270 /* get sense data */
271 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
272 mpi_reply->Function ==
273 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
274 scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
275 if (scsiio_reply->SCSIState &
276 MPI2_SCSI_STATE_AUTOSENSE_VALID) {
277 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
278 le32_to_cpu(scsiio_reply->SenseCount));
279 sense_data = mpt3sas_base_get_sense_buffer(ioc,
280 smid);
281 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
282 }
283 }
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530284 /*
285 * Get Error Response data for NVMe device. The ctl_cmds.sense
286 * buffer is used to store the Error Response data.
287 */
288 if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
289 nvme_error_reply =
290 (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
291 sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400292 le16_to_cpu(nvme_error_reply->ErrorResponseCount));
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530293 sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
294 memcpy(ioc->ctl_cmds.sense, sense_data, sz);
295 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530296 }
Suganath Prabu Subramani016d5c32017-10-31 18:02:28 +0530297
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530298 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530299 ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
300 complete(&ioc->ctl_cmds.done);
301 return 1;
302}
303
304/**
305 * _ctl_check_event_type - determines when an event needs logging
306 * @ioc: per adapter object
307 * @event: firmware event
308 *
309 * The bitmask in ioc->event_type[] indicates which events should be
310 * be saved in the driver event_log. This bitmask is set by application.
311 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700312 * Return: 1 when event should be captured, or zero means no match.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530313 */
314static int
315_ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
316{
317 u16 i;
318 u32 desired_event;
319
320 if (event >= 128 || !event || !ioc->event_log)
321 return 0;
322
323 desired_event = (1 << (event % 32));
324 if (!desired_event)
325 desired_event = 1;
326 i = event / 32;
327 return desired_event & ioc->event_type[i];
328}
329
330/**
331 * mpt3sas_ctl_add_to_event_log - add event
332 * @ioc: per adapter object
333 * @mpi_reply: reply message frame
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530334 */
335void
336mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
337 Mpi2EventNotificationReply_t *mpi_reply)
338{
339 struct MPT3_IOCTL_EVENTS *event_log;
340 u16 event;
341 int i;
342 u32 sz, event_data_sz;
343 u8 send_aen = 0;
344
345 if (!ioc->event_log)
346 return;
347
348 event = le16_to_cpu(mpi_reply->Event);
349
350 if (_ctl_check_event_type(ioc, event)) {
351
352 /* insert entry into circular event_log */
353 i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
354 event_log = ioc->event_log;
355 event_log[i].event = event;
356 event_log[i].context = ioc->event_context++;
357
358 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
359 sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
360 memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
361 memcpy(event_log[i].data, mpi_reply->EventData, sz);
362 send_aen = 1;
363 }
364
365 /* This aen_event_read_flag flag is set until the
366 * application has read the event log.
367 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
368 */
369 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
370 (send_aen && !ioc->aen_event_read_flag)) {
371 ioc->aen_event_read_flag = 1;
372 wake_up_interruptible(&ctl_poll_wait);
373 if (async_queue)
374 kill_fasync(&async_queue, SIGIO, POLL_IN);
375 }
376}
377
378/**
379 * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
380 * @ioc: per adapter object
381 * @msix_index: MSIX table index supplied by the OS
382 * @reply: reply message frame(lower 32bit addr)
383 * Context: interrupt.
384 *
385 * This function merely adds a new work task into ioc->firmware_event_thread.
386 * The tasks are worked from _firmware_event_work in user context.
387 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700388 * Return: 1 meaning mf should be freed from _base_interrupt
389 * 0 means the mf is freed from this function.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530390 */
391u8
392mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
393 u32 reply)
394{
395 Mpi2EventNotificationReply_t *mpi_reply;
396
397 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
Suganath prabu Subramani869817f2016-01-28 12:07:00 +0530398 if (mpi_reply)
399 mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530400 return 1;
401}
402
403/**
404 * _ctl_verify_adapter - validates ioc_number passed from application
Bart Van Assche4beb4862018-06-15 14:42:01 -0700405 * @ioc_number: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530406 * @iocpp: The ioc pointer is returned in this.
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530407 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530408 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530409 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700410 * Return: (-1) means error, else ioc_number.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530411 */
412static int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530413_ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
414 int mpi_version)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530415{
416 struct MPT3SAS_ADAPTER *ioc;
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530417 int version = 0;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530418 /* global ioc lock to protect controller on list operations */
419 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530420 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
421 if (ioc->id != ioc_number)
422 continue;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530423 /* Check whether this ioctl command is from right
424 * ioctl device or not, if not continue the search.
425 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530426 version = ioc->hba_mpi_version_belonged;
427 /* MPI25_VERSION and MPI26_VERSION uses same ioctl
428 * device.
429 */
430 if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
431 if ((version == MPI25_VERSION) ||
432 (version == MPI26_VERSION))
433 goto out;
434 else
435 continue;
436 } else {
437 if (version != mpi_version)
438 continue;
439 }
440out:
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530441 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530442 *iocpp = ioc;
443 return ioc_number;
444 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530445 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530446 *iocpp = NULL;
447 return -1;
448}
449
450/**
451 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
452 * @ioc: per adapter object
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530453 *
454 * The handler for doing any required cleanup or initialization.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530455 */
Bart Van Asschec7a35702018-06-15 14:42:00 -0700456void mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530457{
458 int i;
459 u8 issue_reset;
460
Joe Perches919d8a32018-09-17 08:01:09 -0700461 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
Bart Van Asschec7a35702018-06-15 14:42:00 -0700462 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
463 if (!(ioc->diag_buffer_status[i] &
464 MPT3_DIAG_BUFFER_IS_REGISTERED))
465 continue;
466 if ((ioc->diag_buffer_status[i] &
467 MPT3_DIAG_BUFFER_IS_RELEASED))
468 continue;
469 mpt3sas_send_diag_release(ioc, i, &issue_reset);
470 }
471}
472
473/**
474 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
475 * @ioc: per adapter object
476 *
477 * The handler for doing any required cleanup or initialization.
478 */
479void mpt3sas_ctl_after_reset_handler(struct MPT3SAS_ADAPTER *ioc)
480{
Joe Perches919d8a32018-09-17 08:01:09 -0700481 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_AFTER_RESET\n", __func__));
Bart Van Asschec7a35702018-06-15 14:42:00 -0700482 if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
483 ioc->ctl_cmds.status |= MPT3_CMD_RESET;
484 mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
485 complete(&ioc->ctl_cmds.done);
486 }
487}
488
489/**
490 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
491 * @ioc: per adapter object
492 *
493 * The handler for doing any required cleanup or initialization.
494 */
495void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
496{
497 int i;
498
Joe Perches919d8a32018-09-17 08:01:09 -0700499 dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530500
Bart Van Asschec7a35702018-06-15 14:42:00 -0700501 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
502 if (!(ioc->diag_buffer_status[i] &
503 MPT3_DIAG_BUFFER_IS_REGISTERED))
504 continue;
505 if ((ioc->diag_buffer_status[i] &
506 MPT3_DIAG_BUFFER_IS_RELEASED))
507 continue;
508 ioc->diag_buffer_status[i] |=
509 MPT3_DIAG_BUFFER_IS_DIAG_RESET;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530510 }
511}
512
513/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530514 * _ctl_fasync -
Bart Van Assche4beb4862018-06-15 14:42:01 -0700515 * @fd: ?
516 * @filep: ?
517 * @mode: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530518 *
519 * Called when application request fasyn callback handler.
520 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -0700521static int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530522_ctl_fasync(int fd, struct file *filep, int mode)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530523{
524 return fasync_helper(fd, filep, mode, &async_queue);
525}
526
527/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530528 * _ctl_poll -
Bart Van Assche4beb4862018-06-15 14:42:01 -0700529 * @filep: ?
530 * @wait: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530531 *
532 */
Al Viroafc9a422017-07-03 06:39:46 -0400533static __poll_t
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530534_ctl_poll(struct file *filep, poll_table *wait)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530535{
536 struct MPT3SAS_ADAPTER *ioc;
537
538 poll_wait(filep, &ctl_poll_wait, wait);
539
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530540 /* global ioc lock to protect controller on list operations */
541 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530542 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530543 if (ioc->aen_event_read_flag) {
544 spin_unlock(&gioc_lock);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800545 return EPOLLIN | EPOLLRDNORM;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530546 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530547 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530548 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530549 return 0;
550}
551
552/**
553 * _ctl_set_task_mid - assign an active smid to tm request
554 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -0700555 * @karg: (struct mpt3_ioctl_command)
556 * @tm_request: pointer to mf from user space
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530557 *
Bart Van Assche4beb4862018-06-15 14:42:01 -0700558 * Return: 0 when an smid if found, else fail.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530559 * during failure, the reply frame is filled.
560 */
561static int
562_ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
563 Mpi2SCSITaskManagementRequest_t *tm_request)
564{
565 u8 found = 0;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800566 u16 smid;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530567 u16 handle;
568 struct scsi_cmnd *scmd;
569 struct MPT3SAS_DEVICE *priv_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530570 Mpi2SCSITaskManagementReply_t *tm_reply;
571 u32 sz;
572 u32 lun;
573 char *desc = NULL;
574
575 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
576 desc = "abort_task";
577 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
578 desc = "query_task";
579 else
580 return 0;
581
582 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
583
584 handle = le16_to_cpu(tm_request->DevHandle);
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800585 for (smid = ioc->scsiio_depth; smid && !found; smid--) {
586 struct scsiio_tracker *st;
587
588 scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
589 if (!scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530590 continue;
591 if (lun != scmd->device->lun)
592 continue;
593 priv_data = scmd->device->hostdata;
594 if (priv_data->sas_target == NULL)
595 continue;
596 if (priv_data->sas_target->handle != handle)
597 continue;
Suganath Prabu Subramanidbec4c902018-01-04 04:57:11 -0800598 st = scsi_cmd_priv(scmd);
Minwoo Im8f55c302019-07-28 03:53:37 +0900599
600 /*
601 * If the given TaskMID from the user space is zero, then the
602 * first outstanding smid will be picked up. Otherwise,
603 * targeted smid will be the one.
604 */
605 if (!tm_request->TaskMID || tm_request->TaskMID == st->smid) {
606 tm_request->TaskMID = cpu_to_le16(st->smid);
607 found = 1;
608 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530609 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530610
611 if (!found) {
Joe Perches919d8a32018-09-17 08:01:09 -0700612 dctlprintk(ioc,
613 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), no active mid!!\n",
614 desc, le16_to_cpu(tm_request->DevHandle),
615 lun));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530616 tm_reply = ioc->ctl_cmds.reply;
617 tm_reply->DevHandle = tm_request->DevHandle;
618 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
619 tm_reply->TaskType = tm_request->TaskType;
620 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
621 tm_reply->VP_ID = tm_request->VP_ID;
622 tm_reply->VF_ID = tm_request->VF_ID;
623 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
624 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
625 sz))
626 pr_err("failure at %s:%d/%s()!\n", __FILE__,
627 __LINE__, __func__);
628 return 1;
629 }
630
Joe Perches919d8a32018-09-17 08:01:09 -0700631 dctlprintk(ioc,
632 ioc_info(ioc, "%s: handle(0x%04x), lun(%d), task_mid(%d)\n",
633 desc, le16_to_cpu(tm_request->DevHandle), lun,
634 le16_to_cpu(tm_request->TaskMID)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530635 return 0;
636}
637
638/**
639 * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
640 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -0700641 * @karg: (struct mpt3_ioctl_command)
642 * @mf: pointer to mf in user space
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530643 */
644static long
645_ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
646 void __user *mf)
647{
648 MPI2RequestHeader_t *mpi_request = NULL, *request;
649 MPI2DefaultReply_t *mpi_reply;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530650 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -0400651 struct _pcie_device *pcie_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530652 u16 smid;
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -0400653 u8 timeout;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530654 u8 issue_reset;
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530655 u32 sz, sz_arg;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530656 void *psge;
657 void *data_out = NULL;
658 dma_addr_t data_out_dma = 0;
659 size_t data_out_sz = 0;
660 void *data_in = NULL;
661 dma_addr_t data_in_dma = 0;
662 size_t data_in_sz = 0;
663 long ret;
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530664 u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -0400665 u8 tr_method = MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530666
667 issue_reset = 0;
668
669 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -0700670 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530671 ret = -EAGAIN;
672 goto out;
673 }
674
Suganath Prabuf4305742018-10-31 18:53:33 +0530675 ret = mpt3sas_wait_for_ioc(ioc, IOC_OPERATIONAL_WAIT_COUNT);
676 if (ret)
677 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530678
679 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
680 if (!mpi_request) {
Joe Perches919d8a32018-09-17 08:01:09 -0700681 ioc_err(ioc, "%s: failed obtaining a memory for mpi_request\n",
682 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530683 ret = -ENOMEM;
684 goto out;
685 }
686
687 /* Check for overflow and wraparound */
688 if (karg.data_sge_offset * 4 > ioc->request_sz ||
689 karg.data_sge_offset > (UINT_MAX / 4)) {
690 ret = -EINVAL;
691 goto out;
692 }
693
694 /* copy in request message frame from user */
695 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
696 pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
697 __func__);
698 ret = -EFAULT;
699 goto out;
700 }
701
702 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
703 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
704 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -0700705 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530706 ret = -EAGAIN;
707 goto out;
708 }
709 } else {
Hannes Reineckeb0cd285e2018-01-04 04:57:07 -0800710 /* Use first reserved smid for passthrough ioctls */
711 smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530712 }
713
714 ret = 0;
715 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
716 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
717 request = mpt3sas_base_get_msg_frame(ioc, smid);
718 memcpy(request, mpi_request, karg.data_sge_offset*4);
719 ioc->ctl_cmds.smid = smid;
720 data_out_sz = karg.data_out_size;
721 data_in_sz = karg.data_in_size;
722
723 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530724 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
725 mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530726 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
727 mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530728
729 device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
730 if (!device_handle || (device_handle >
731 ioc->facts.MaxDevHandle)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530732 ret = -EINVAL;
733 mpt3sas_base_free_smid(ioc, smid);
734 goto out;
735 }
736 }
737
738 /* obtain dma-able memory for data transfer */
739 if (data_out_sz) /* WRITE */ {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +0200740 data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz,
741 &data_out_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530742 if (!data_out) {
743 pr_err("failure at %s:%d/%s()!\n", __FILE__,
744 __LINE__, __func__);
745 ret = -ENOMEM;
746 mpt3sas_base_free_smid(ioc, smid);
747 goto out;
748 }
749 if (copy_from_user(data_out, karg.data_out_buf_ptr,
750 data_out_sz)) {
751 pr_err("failure at %s:%d/%s()!\n", __FILE__,
752 __LINE__, __func__);
753 ret = -EFAULT;
754 mpt3sas_base_free_smid(ioc, smid);
755 goto out;
756 }
757 }
758
759 if (data_in_sz) /* READ */ {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +0200760 data_in = dma_alloc_coherent(&ioc->pdev->dev, data_in_sz,
761 &data_in_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530762 if (!data_in) {
763 pr_err("failure at %s:%d/%s()!\n", __FILE__,
764 __LINE__, __func__);
765 ret = -ENOMEM;
766 mpt3sas_base_free_smid(ioc, smid);
767 goto out;
768 }
769 }
770
771 psge = (void *)request + (karg.data_sge_offset*4);
772
773 /* send command to firmware */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530774 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530775
776 init_completion(&ioc->ctl_cmds.done);
777 switch (mpi_request->Function) {
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530778 case MPI2_FUNCTION_NVME_ENCAPSULATED:
779 {
780 nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
781 /*
782 * Get the Physical Address of the sense buffer.
783 * Use Error Response buffer address field to hold the sense
784 * buffer address.
785 * Clear the internal sense buffer, which will potentially hold
786 * the Completion Queue Entry on return, or 0 if no Entry.
787 * Build the PRPs and set direction bits.
788 * Send the request.
789 */
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400790 nvme_encap_request->ErrorResponseBaseAddress =
791 cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530792 nvme_encap_request->ErrorResponseBaseAddress |=
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400793 cpu_to_le64(le32_to_cpu(
794 mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530795 nvme_encap_request->ErrorResponseAllocationLength =
Chaitra P Bcf6bf972018-04-24 05:28:30 -0400796 cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530797 memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
798 ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
799 data_out_dma, data_out_sz, data_in_dma, data_in_sz);
800 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700801 dtmprintk(ioc,
802 ioc_info(ioc, "handle(0x%04x): ioctl failed due to device removal in progress\n",
803 device_handle));
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530804 mpt3sas_base_free_smid(ioc, smid);
805 ret = -EINVAL;
806 goto out;
807 }
Suganath Prabu S40114bd2018-02-14 02:16:37 -0800808 mpt3sas_base_put_smid_nvme_encap(ioc, smid);
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +0530809 break;
810 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530811 case MPI2_FUNCTION_SCSI_IO_REQUEST:
812 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
813 {
814 Mpi2SCSIIORequest_t *scsiio_request =
815 (Mpi2SCSIIORequest_t *)request;
816 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
817 scsiio_request->SenseBufferLowAddress =
818 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
819 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530820 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700821 dtmprintk(ioc,
822 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
823 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530824 mpt3sas_base_free_smid(ioc, smid);
825 ret = -EINVAL;
826 goto out;
827 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530828 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
829 data_in_dma, data_in_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530830 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
Suganath Prabu Subramani81c16f82016-10-26 13:34:40 +0530831 ioc->put_smid_scsi_io(ioc, smid, device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530832 else
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400833 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530834 break;
835 }
836 case MPI2_FUNCTION_SCSI_TASK_MGMT:
837 {
838 Mpi2SCSITaskManagementRequest_t *tm_request =
839 (Mpi2SCSITaskManagementRequest_t *)request;
840
Joe Perches919d8a32018-09-17 08:01:09 -0700841 dtmprintk(ioc,
842 ioc_info(ioc, "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
843 le16_to_cpu(tm_request->DevHandle),
844 tm_request->TaskType));
Chaitra P B459325c2017-01-23 15:26:08 +0530845 ioc->got_task_abort_from_ioctl = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530846 if (tm_request->TaskType ==
847 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
848 tm_request->TaskType ==
849 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
850 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
851 mpt3sas_base_free_smid(ioc, smid);
Chaitra P B459325c2017-01-23 15:26:08 +0530852 ioc->got_task_abort_from_ioctl = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530853 goto out;
854 }
855 }
Chaitra P B459325c2017-01-23 15:26:08 +0530856 ioc->got_task_abort_from_ioctl = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530857
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530858 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700859 dtmprintk(ioc,
860 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
861 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530862 mpt3sas_base_free_smid(ioc, smid);
863 ret = -EINVAL;
864 goto out;
865 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530866 mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
867 tm_request->DevHandle));
868 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
869 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400870 ioc->put_smid_hi_priority(ioc, smid, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530871 break;
872 }
873 case MPI2_FUNCTION_SMP_PASSTHROUGH:
874 {
875 Mpi2SmpPassthroughRequest_t *smp_request =
876 (Mpi2SmpPassthroughRequest_t *)mpi_request;
877 u8 *data;
878
879 /* ioc determines which port to use */
880 smp_request->PhysicalPort = 0xFF;
881 if (smp_request->PassthroughFlags &
882 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
883 data = (u8 *)&smp_request->SGL;
884 else {
885 if (unlikely(data_out == NULL)) {
886 pr_err("failure at %s:%d/%s()!\n",
887 __FILE__, __LINE__, __func__);
888 mpt3sas_base_free_smid(ioc, smid);
889 ret = -EINVAL;
890 goto out;
891 }
892 data = data_out;
893 }
894
895 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
896 ioc->ioc_link_reset_in_progress = 1;
897 ioc->ignore_loginfos = 1;
898 }
899 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
900 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400901 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530902 break;
903 }
904 case MPI2_FUNCTION_SATA_PASSTHROUGH:
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530905 {
906 if (test_bit(device_handle, ioc->device_remove_in_progress)) {
Joe Perches919d8a32018-09-17 08:01:09 -0700907 dtmprintk(ioc,
908 ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
909 device_handle));
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530910 mpt3sas_base_free_smid(ioc, smid);
911 ret = -EINVAL;
912 goto out;
913 }
914 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
915 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400916 ioc->put_smid_default(ioc, smid);
Suganath Prabu Subramanic696f7b2016-10-26 13:34:34 +0530917 break;
918 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530919 case MPI2_FUNCTION_FW_DOWNLOAD:
920 case MPI2_FUNCTION_FW_UPLOAD:
921 {
922 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
923 data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400924 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530925 break;
926 }
927 case MPI2_FUNCTION_TOOLBOX:
928 {
929 Mpi2ToolboxCleanRequest_t *toolbox_request =
930 (Mpi2ToolboxCleanRequest_t *)mpi_request;
931
Suganath Prabuf23ca2c2019-08-03 09:59:46 -0400932 if ((toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
933 || (toolbox_request->Tool ==
934 MPI26_TOOLBOX_BACKEND_PCIE_LANE_MARGIN))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530935 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
936 data_in_dma, data_in_sz);
Suganath Prabuf23ca2c2019-08-03 09:59:46 -0400937 else
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530938 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
939 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400940 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530941 break;
942 }
943 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
944 {
945 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
946 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
947
948 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
949 || sasiounit_request->Operation ==
950 MPI2_SAS_OP_PHY_LINK_RESET) {
951 ioc->ioc_link_reset_in_progress = 1;
952 ioc->ignore_loginfos = 1;
953 }
954 /* drop to default case for posting the request */
955 }
Bart Van Asscheeb0c7af2018-06-15 14:41:56 -0700956 /* fall through */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530957 default:
958 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
959 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400960 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530961 break;
962 }
963
964 if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
965 timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
966 else
967 timeout = karg.timeout;
Calvin Owens8bbb1cf2016-07-28 21:38:22 -0700968 wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530969 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
970 Mpi2SCSITaskManagementRequest_t *tm_request =
971 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
972 mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
973 tm_request->DevHandle));
974 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
975 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
976 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
977 ioc->ioc_link_reset_in_progress) {
978 ioc->ioc_link_reset_in_progress = 0;
979 ioc->ignore_loginfos = 0;
980 }
981 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -0400982 issue_reset =
983 mpt3sas_base_check_cmd_timeout(ioc,
984 ioc->ctl_cmds.status, mpi_request,
985 karg.data_sge_offset);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530986 goto issue_host_reset;
987 }
988
989 mpi_reply = ioc->ctl_cmds.reply;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530990
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530991 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
992 (ioc->logging_level & MPT_DEBUG_TM)) {
993 Mpi2SCSITaskManagementReply_t *tm_reply =
994 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
995
Joe Perches919d8a32018-09-17 08:01:09 -0700996 ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
997 le16_to_cpu(tm_reply->IOCStatus),
998 le32_to_cpu(tm_reply->IOCLogInfo),
999 le32_to_cpu(tm_reply->TerminationCount));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301000 }
Sreekanth Reddyaf009412015-11-11 17:30:23 +05301001
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301002 /* copy out xdata to user */
1003 if (data_in_sz) {
1004 if (copy_to_user(karg.data_in_buf_ptr, data_in,
1005 data_in_sz)) {
1006 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1007 __LINE__, __func__);
1008 ret = -ENODATA;
1009 goto out;
1010 }
1011 }
1012
1013 /* copy out reply message frame to user */
1014 if (karg.max_reply_bytes) {
1015 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1016 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1017 sz)) {
1018 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1019 __LINE__, __func__);
1020 ret = -ENODATA;
1021 goto out;
1022 }
1023 }
1024
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301025 /* copy out sense/NVMe Error Response to user */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301026 if (karg.max_sense_bytes && (mpi_request->Function ==
1027 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301028 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1029 MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1030 if (karg.sense_data_ptr == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001031 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 +05301032 goto out;
1033 }
1034 sz_arg = (mpi_request->Function ==
1035 MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1036 SCSI_SENSE_BUFFERSIZE;
1037 sz = min_t(u32, karg.max_sense_bytes, sz_arg);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301038 if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1039 sz)) {
1040 pr_err("failure at %s:%d/%s()!\n", __FILE__,
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301041 __LINE__, __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301042 ret = -ENODATA;
1043 goto out;
1044 }
1045 }
1046
1047 issue_host_reset:
1048 if (issue_reset) {
1049 ret = -ENODATA;
1050 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1051 mpi_request->Function ==
1052 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1053 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001054 ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
1055 le16_to_cpu(mpi_request->FunctionDependent1));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301056 mpt3sas_halt_firmware(ioc);
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001057 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1058 le16_to_cpu(mpi_request->FunctionDependent1));
1059 if (pcie_device && (!ioc->tm_custom_handling))
1060 mpt3sas_scsih_issue_locked_tm(ioc,
1061 le16_to_cpu(mpi_request->FunctionDependent1),
1062 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1063 0, pcie_device->reset_timeout,
1064 tr_method);
1065 else
1066 mpt3sas_scsih_issue_locked_tm(ioc,
1067 le16_to_cpu(mpi_request->FunctionDependent1),
1068 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1069 0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301070 } else
Calvin Owens98c56ad2016-07-28 21:38:21 -07001071 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301072 }
1073
1074 out:
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001075 if (pcie_device)
1076 pcie_device_put(pcie_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301077
1078 /* free memory associated with sg buffers */
1079 if (data_in)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001080 dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301081 data_in_dma);
1082
1083 if (data_out)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001084 dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301085 data_out_dma);
1086
1087 kfree(mpi_request);
1088 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1089 return ret;
1090}
1091
1092/**
1093 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1094 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001095 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301096 */
1097static long
1098_ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1099{
1100 struct mpt3_ioctl_iocinfo karg;
1101
Joe Perches919d8a32018-09-17 08:01:09 -07001102 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1103 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301104
1105 memset(&karg, 0 , sizeof(karg));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301106 if (ioc->pfacts)
1107 karg.port_number = ioc->pfacts[0].PortNumber;
1108 karg.hw_rev = ioc->pdev->revision;
1109 karg.pci_id = ioc->pdev->device;
1110 karg.subsystem_device = ioc->pdev->subsystem_device;
1111 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1112 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1113 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1114 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1115 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1116 karg.firmware_version = ioc->facts.FWVersion.Word;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301117 strcpy(karg.driver_version, ioc->driver_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301118 strcat(karg.driver_version, "-");
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301119 switch (ioc->hba_mpi_version_belonged) {
1120 case MPI2_VERSION:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301121 if (ioc->is_warpdrive)
1122 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1123 else
1124 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301125 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1126 break;
1127 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301128 case MPI26_VERSION:
Suganath Prabu Subramani998f26a2016-10-26 13:34:37 +05301129 if (ioc->is_gen35_ioc)
1130 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1131 else
1132 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301133 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1134 break;
1135 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301136 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1137
1138 if (copy_to_user(arg, &karg, sizeof(karg))) {
1139 pr_err("failure at %s:%d/%s()!\n",
1140 __FILE__, __LINE__, __func__);
1141 return -EFAULT;
1142 }
1143 return 0;
1144}
1145
1146/**
1147 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1148 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001149 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301150 */
1151static long
1152_ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1153{
1154 struct mpt3_ioctl_eventquery karg;
1155
1156 if (copy_from_user(&karg, arg, sizeof(karg))) {
1157 pr_err("failure at %s:%d/%s()!\n",
1158 __FILE__, __LINE__, __func__);
1159 return -EFAULT;
1160 }
1161
Joe Perches919d8a32018-09-17 08:01:09 -07001162 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1163 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301164
1165 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1166 memcpy(karg.event_types, ioc->event_type,
1167 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1168
1169 if (copy_to_user(arg, &karg, sizeof(karg))) {
1170 pr_err("failure at %s:%d/%s()!\n",
1171 __FILE__, __LINE__, __func__);
1172 return -EFAULT;
1173 }
1174 return 0;
1175}
1176
1177/**
1178 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1179 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001180 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301181 */
1182static long
1183_ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1184{
1185 struct mpt3_ioctl_eventenable karg;
1186
1187 if (copy_from_user(&karg, arg, sizeof(karg))) {
1188 pr_err("failure at %s:%d/%s()!\n",
1189 __FILE__, __LINE__, __func__);
1190 return -EFAULT;
1191 }
1192
Joe Perches919d8a32018-09-17 08:01:09 -07001193 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1194 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301195
1196 memcpy(ioc->event_type, karg.event_types,
1197 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1198 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1199
1200 if (ioc->event_log)
1201 return 0;
1202 /* initialize event_log */
1203 ioc->event_context = 0;
1204 ioc->aen_event_read_flag = 0;
1205 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1206 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1207 if (!ioc->event_log) {
1208 pr_err("failure at %s:%d/%s()!\n",
1209 __FILE__, __LINE__, __func__);
1210 return -ENOMEM;
1211 }
1212 return 0;
1213}
1214
1215/**
1216 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1217 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001218 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301219 */
1220static long
1221_ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1222{
1223 struct mpt3_ioctl_eventreport karg;
1224 u32 number_bytes, max_events, max;
1225 struct mpt3_ioctl_eventreport __user *uarg = arg;
1226
1227 if (copy_from_user(&karg, arg, sizeof(karg))) {
1228 pr_err("failure at %s:%d/%s()!\n",
1229 __FILE__, __LINE__, __func__);
1230 return -EFAULT;
1231 }
1232
Joe Perches919d8a32018-09-17 08:01:09 -07001233 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1234 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301235
1236 number_bytes = karg.hdr.max_data_size -
1237 sizeof(struct mpt3_ioctl_header);
1238 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1239 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1240
1241 /* If fewer than 1 event is requested, there must have
1242 * been some type of error.
1243 */
1244 if (!max || !ioc->event_log)
1245 return -ENODATA;
1246
1247 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1248 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1249 pr_err("failure at %s:%d/%s()!\n",
1250 __FILE__, __LINE__, __func__);
1251 return -EFAULT;
1252 }
1253
1254 /* reset flag so SIGIO can restart */
1255 ioc->aen_event_read_flag = 0;
1256 return 0;
1257}
1258
1259/**
1260 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1261 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001262 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301263 */
1264static long
1265_ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1266{
1267 struct mpt3_ioctl_diag_reset karg;
1268 int retval;
1269
1270 if (copy_from_user(&karg, arg, sizeof(karg))) {
1271 pr_err("failure at %s:%d/%s()!\n",
1272 __FILE__, __LINE__, __func__);
1273 return -EFAULT;
1274 }
1275
1276 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1277 ioc->is_driver_loading)
1278 return -EAGAIN;
1279
Joe Perches919d8a32018-09-17 08:01:09 -07001280 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1281 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301282
Calvin Owens98c56ad2016-07-28 21:38:21 -07001283 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Joe Perches919d8a32018-09-17 08:01:09 -07001284 ioc_info(ioc, "host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301285 return 0;
1286}
1287
1288/**
1289 * _ctl_btdh_search_sas_device - searching for sas device
1290 * @ioc: per adapter object
1291 * @btdh: btdh ioctl payload
1292 */
1293static int
1294_ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1295 struct mpt3_ioctl_btdh_mapping *btdh)
1296{
1297 struct _sas_device *sas_device;
1298 unsigned long flags;
1299 int rc = 0;
1300
1301 if (list_empty(&ioc->sas_device_list))
1302 return rc;
1303
1304 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1305 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1306 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1307 btdh->handle == sas_device->handle) {
1308 btdh->bus = sas_device->channel;
1309 btdh->id = sas_device->id;
1310 rc = 1;
1311 goto out;
1312 } else if (btdh->bus == sas_device->channel && btdh->id ==
1313 sas_device->id && btdh->handle == 0xFFFF) {
1314 btdh->handle = sas_device->handle;
1315 rc = 1;
1316 goto out;
1317 }
1318 }
1319 out:
1320 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1321 return rc;
1322}
1323
1324/**
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +05301325 * _ctl_btdh_search_pcie_device - searching for pcie device
1326 * @ioc: per adapter object
1327 * @btdh: btdh ioctl payload
1328 */
1329static int
1330_ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
1331 struct mpt3_ioctl_btdh_mapping *btdh)
1332{
1333 struct _pcie_device *pcie_device;
1334 unsigned long flags;
1335 int rc = 0;
1336
1337 if (list_empty(&ioc->pcie_device_list))
1338 return rc;
1339
1340 spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1341 list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1342 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1343 btdh->handle == pcie_device->handle) {
1344 btdh->bus = pcie_device->channel;
1345 btdh->id = pcie_device->id;
1346 rc = 1;
1347 goto out;
1348 } else if (btdh->bus == pcie_device->channel && btdh->id ==
1349 pcie_device->id && btdh->handle == 0xFFFF) {
1350 btdh->handle = pcie_device->handle;
1351 rc = 1;
1352 goto out;
1353 }
1354 }
1355 out:
1356 spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1357 return rc;
1358}
1359
1360/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301361 * _ctl_btdh_search_raid_device - searching for raid device
1362 * @ioc: per adapter object
1363 * @btdh: btdh ioctl payload
1364 */
1365static int
1366_ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1367 struct mpt3_ioctl_btdh_mapping *btdh)
1368{
1369 struct _raid_device *raid_device;
1370 unsigned long flags;
1371 int rc = 0;
1372
1373 if (list_empty(&ioc->raid_device_list))
1374 return rc;
1375
1376 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1377 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1378 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1379 btdh->handle == raid_device->handle) {
1380 btdh->bus = raid_device->channel;
1381 btdh->id = raid_device->id;
1382 rc = 1;
1383 goto out;
1384 } else if (btdh->bus == raid_device->channel && btdh->id ==
1385 raid_device->id && btdh->handle == 0xFFFF) {
1386 btdh->handle = raid_device->handle;
1387 rc = 1;
1388 goto out;
1389 }
1390 }
1391 out:
1392 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1393 return rc;
1394}
1395
1396/**
1397 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1398 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001399 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301400 */
1401static long
1402_ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1403{
1404 struct mpt3_ioctl_btdh_mapping karg;
1405 int rc;
1406
1407 if (copy_from_user(&karg, arg, sizeof(karg))) {
1408 pr_err("failure at %s:%d/%s()!\n",
1409 __FILE__, __LINE__, __func__);
1410 return -EFAULT;
1411 }
1412
Joe Perches919d8a32018-09-17 08:01:09 -07001413 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1414 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301415
1416 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1417 if (!rc)
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +05301418 rc = _ctl_btdh_search_pcie_device(ioc, &karg);
1419 if (!rc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301420 _ctl_btdh_search_raid_device(ioc, &karg);
1421
1422 if (copy_to_user(arg, &karg, sizeof(karg))) {
1423 pr_err("failure at %s:%d/%s()!\n",
1424 __FILE__, __LINE__, __func__);
1425 return -EFAULT;
1426 }
1427 return 0;
1428}
1429
1430/**
1431 * _ctl_diag_capability - return diag buffer capability
1432 * @ioc: per adapter object
1433 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1434 *
1435 * returns 1 when diag buffer support is enabled in firmware
1436 */
1437static u8
1438_ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1439{
1440 u8 rc = 0;
1441
1442 switch (buffer_type) {
1443 case MPI2_DIAG_BUF_TYPE_TRACE:
1444 if (ioc->facts.IOCCapabilities &
1445 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1446 rc = 1;
1447 break;
1448 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1449 if (ioc->facts.IOCCapabilities &
1450 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1451 rc = 1;
1452 break;
1453 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1454 if (ioc->facts.IOCCapabilities &
1455 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1456 rc = 1;
1457 }
1458
1459 return rc;
1460}
1461
1462
1463/**
1464 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1465 * @ioc: per adapter object
1466 * @diag_register: the diag_register struct passed in from user space
1467 *
1468 */
1469static long
1470_ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1471 struct mpt3_diag_register *diag_register)
1472{
1473 int rc, i;
1474 void *request_data = NULL;
1475 dma_addr_t request_data_dma;
1476 u32 request_data_sz = 0;
1477 Mpi2DiagBufferPostRequest_t *mpi_request;
1478 Mpi2DiagBufferPostReply_t *mpi_reply;
1479 u8 buffer_type;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301480 u16 smid;
1481 u16 ioc_status;
1482 u32 ioc_state;
1483 u8 issue_reset = 0;
1484
Joe Perches919d8a32018-09-17 08:01:09 -07001485 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1486 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301487
1488 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1489 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001490 ioc_err(ioc, "%s: failed due to ioc not operational\n",
1491 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301492 rc = -EAGAIN;
1493 goto out;
1494 }
1495
1496 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001497 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301498 rc = -EAGAIN;
1499 goto out;
1500 }
1501
1502 buffer_type = diag_register->buffer_type;
1503 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001504 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1505 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301506 return -EPERM;
1507 }
1508
1509 if (ioc->diag_buffer_status[buffer_type] &
1510 MPT3_DIAG_BUFFER_IS_REGISTERED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001511 ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1512 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301513 return -EINVAL;
1514 }
1515
1516 if (diag_register->requested_buffer_size % 4) {
Joe Perches919d8a32018-09-17 08:01:09 -07001517 ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
1518 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301519 return -EINVAL;
1520 }
1521
1522 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1523 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07001524 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301525 rc = -EAGAIN;
1526 goto out;
1527 }
1528
1529 rc = 0;
1530 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1531 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1532 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1533 ioc->ctl_cmds.smid = smid;
1534
1535 request_data = ioc->diag_buffer[buffer_type];
1536 request_data_sz = diag_register->requested_buffer_size;
1537 ioc->unique_id[buffer_type] = diag_register->unique_id;
1538 ioc->diag_buffer_status[buffer_type] = 0;
1539 memcpy(ioc->product_specific[buffer_type],
1540 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1541 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1542
1543 if (request_data) {
1544 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1545 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001546 dma_free_coherent(&ioc->pdev->dev,
1547 ioc->diag_buffer_sz[buffer_type],
1548 request_data, request_data_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301549 request_data = NULL;
1550 }
1551 }
1552
1553 if (request_data == NULL) {
1554 ioc->diag_buffer_sz[buffer_type] = 0;
1555 ioc->diag_buffer_dma[buffer_type] = 0;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001556 request_data = dma_alloc_coherent(&ioc->pdev->dev,
1557 request_data_sz, &request_data_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301558 if (request_data == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001559 ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
1560 __func__, request_data_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301561 mpt3sas_base_free_smid(ioc, smid);
1562 return -ENOMEM;
1563 }
1564 ioc->diag_buffer[buffer_type] = request_data;
1565 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1566 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1567 }
1568
1569 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1570 mpi_request->BufferType = diag_register->buffer_type;
1571 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1572 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1573 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1574 mpi_request->VF_ID = 0; /* TODO */
1575 mpi_request->VP_ID = 0;
1576
Joe Perches919d8a32018-09-17 08:01:09 -07001577 dctlprintk(ioc,
1578 ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1579 __func__, request_data,
1580 (unsigned long long)request_data_dma,
1581 le32_to_cpu(mpi_request->BufferLength)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301582
1583 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1584 mpi_request->ProductSpecific[i] =
1585 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1586
1587 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001588 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07001589 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301590 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1591
1592 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -04001593 issue_reset =
1594 mpt3sas_base_check_cmd_timeout(ioc,
1595 ioc->ctl_cmds.status, mpi_request,
1596 sizeof(Mpi2DiagBufferPostRequest_t)/4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301597 goto issue_host_reset;
1598 }
1599
1600 /* process the completed Reply Message Frame */
1601 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001602 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301603 rc = -EFAULT;
1604 goto out;
1605 }
1606
1607 mpi_reply = ioc->ctl_cmds.reply;
1608 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1609
1610 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1611 ioc->diag_buffer_status[buffer_type] |=
1612 MPT3_DIAG_BUFFER_IS_REGISTERED;
Joe Perches919d8a32018-09-17 08:01:09 -07001613 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301614 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07001615 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1616 __func__,
1617 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301618 rc = -EFAULT;
1619 }
1620
1621 issue_host_reset:
1622 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07001623 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301624
1625 out:
1626
1627 if (rc && request_data)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001628 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301629 request_data, request_data_dma);
1630
1631 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1632 return rc;
1633}
1634
1635/**
1636 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1637 * @ioc: per adapter object
1638 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1639 *
1640 * This is called when command line option diag_buffer_enable is enabled
1641 * at driver load time.
1642 */
1643void
1644mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1645{
1646 struct mpt3_diag_register diag_register;
1647
1648 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1649
1650 if (bits_to_register & 1) {
Joe Perches919d8a32018-09-17 08:01:09 -07001651 ioc_info(ioc, "registering trace buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301652 ioc->diag_trigger_master.MasterData =
1653 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1654 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1655 /* register for 2MB buffers */
1656 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1657 diag_register.unique_id = 0x7075900;
1658 _ctl_diag_register_2(ioc, &diag_register);
1659 }
1660
1661 if (bits_to_register & 2) {
Joe Perches919d8a32018-09-17 08:01:09 -07001662 ioc_info(ioc, "registering snapshot buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301663 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1664 /* register for 2MB buffers */
1665 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1666 diag_register.unique_id = 0x7075901;
1667 _ctl_diag_register_2(ioc, &diag_register);
1668 }
1669
1670 if (bits_to_register & 4) {
Joe Perches919d8a32018-09-17 08:01:09 -07001671 ioc_info(ioc, "registering extended buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301672 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1673 /* register for 2MB buffers */
1674 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1675 diag_register.unique_id = 0x7075901;
1676 _ctl_diag_register_2(ioc, &diag_register);
1677 }
1678}
1679
1680/**
1681 * _ctl_diag_register - application register with driver
1682 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001683 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301684 *
1685 * This will allow the driver to setup any required buffers that will be
1686 * needed by firmware to communicate with the driver.
1687 */
1688static long
1689_ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1690{
1691 struct mpt3_diag_register karg;
1692 long rc;
1693
1694 if (copy_from_user(&karg, arg, sizeof(karg))) {
1695 pr_err("failure at %s:%d/%s()!\n",
1696 __FILE__, __LINE__, __func__);
1697 return -EFAULT;
1698 }
1699
1700 rc = _ctl_diag_register_2(ioc, &karg);
1701 return rc;
1702}
1703
1704/**
1705 * _ctl_diag_unregister - application unregister with driver
1706 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001707 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301708 *
1709 * This will allow the driver to cleanup any memory allocated for diag
1710 * messages and to free up any resources.
1711 */
1712static long
1713_ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1714{
1715 struct mpt3_diag_unregister karg;
1716 void *request_data;
1717 dma_addr_t request_data_dma;
1718 u32 request_data_sz;
1719 u8 buffer_type;
1720
1721 if (copy_from_user(&karg, arg, sizeof(karg))) {
1722 pr_err("failure at %s:%d/%s()!\n",
1723 __FILE__, __LINE__, __func__);
1724 return -EFAULT;
1725 }
1726
Joe Perches919d8a32018-09-17 08:01:09 -07001727 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1728 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301729
1730 buffer_type = karg.unique_id & 0x000000ff;
1731 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001732 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1733 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301734 return -EPERM;
1735 }
1736
1737 if ((ioc->diag_buffer_status[buffer_type] &
1738 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001739 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1740 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301741 return -EINVAL;
1742 }
1743 if ((ioc->diag_buffer_status[buffer_type] &
1744 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001745 ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
1746 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301747 return -EINVAL;
1748 }
1749
1750 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07001751 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1752 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301753 return -EINVAL;
1754 }
1755
1756 request_data = ioc->diag_buffer[buffer_type];
1757 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07001758 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1759 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301760 return -ENOMEM;
1761 }
1762
1763 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1764 request_data_dma = ioc->diag_buffer_dma[buffer_type];
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001765 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
1766 request_data, request_data_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301767 ioc->diag_buffer[buffer_type] = NULL;
1768 ioc->diag_buffer_status[buffer_type] = 0;
1769 return 0;
1770}
1771
1772/**
1773 * _ctl_diag_query - query relevant info associated with diag buffers
1774 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001775 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301776 *
1777 * The application will send only buffer_type and unique_id. Driver will
1778 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1779 * 0x00, the driver will return info specified by Buffer Type.
1780 */
1781static long
1782_ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1783{
1784 struct mpt3_diag_query karg;
1785 void *request_data;
1786 int i;
1787 u8 buffer_type;
1788
1789 if (copy_from_user(&karg, arg, sizeof(karg))) {
1790 pr_err("failure at %s:%d/%s()!\n",
1791 __FILE__, __LINE__, __func__);
1792 return -EFAULT;
1793 }
1794
Joe Perches919d8a32018-09-17 08:01:09 -07001795 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1796 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301797
1798 karg.application_flags = 0;
1799 buffer_type = karg.buffer_type;
1800
1801 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001802 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1803 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301804 return -EPERM;
1805 }
1806
1807 if ((ioc->diag_buffer_status[buffer_type] &
1808 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001809 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1810 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301811 return -EINVAL;
1812 }
1813
1814 if (karg.unique_id & 0xffffff00) {
1815 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07001816 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1817 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301818 return -EINVAL;
1819 }
1820 }
1821
1822 request_data = ioc->diag_buffer[buffer_type];
1823 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07001824 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
1825 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301826 return -ENOMEM;
1827 }
1828
1829 if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
1830 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1831 MPT3_APP_FLAGS_BUFFER_VALID);
1832 else
1833 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1834 MPT3_APP_FLAGS_BUFFER_VALID |
1835 MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
1836
1837 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1838 karg.product_specific[i] =
1839 ioc->product_specific[buffer_type][i];
1840
1841 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1842 karg.driver_added_buffer_size = 0;
1843 karg.unique_id = ioc->unique_id[buffer_type];
1844 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1845
1846 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
Joe Perches919d8a32018-09-17 08:01:09 -07001847 ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
1848 __func__, arg);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301849 return -EFAULT;
1850 }
1851 return 0;
1852}
1853
1854/**
1855 * mpt3sas_send_diag_release - Diag Release Message
1856 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001857 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1858 * @issue_reset: specifies whether host reset is required.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301859 *
1860 */
1861int
1862mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
1863 u8 *issue_reset)
1864{
1865 Mpi2DiagReleaseRequest_t *mpi_request;
1866 Mpi2DiagReleaseReply_t *mpi_reply;
1867 u16 smid;
1868 u16 ioc_status;
1869 u32 ioc_state;
1870 int rc;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301871
Joe Perches919d8a32018-09-17 08:01:09 -07001872 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1873 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301874
1875 rc = 0;
1876 *issue_reset = 0;
1877
1878 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1879 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1880 if (ioc->diag_buffer_status[buffer_type] &
1881 MPT3_DIAG_BUFFER_IS_REGISTERED)
1882 ioc->diag_buffer_status[buffer_type] |=
1883 MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07001884 dctlprintk(ioc,
1885 ioc_info(ioc, "%s: skipping due to FAULT state\n",
1886 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301887 rc = -EAGAIN;
1888 goto out;
1889 }
1890
1891 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001892 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301893 rc = -EAGAIN;
1894 goto out;
1895 }
1896
1897 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1898 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07001899 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301900 rc = -EAGAIN;
1901 goto out;
1902 }
1903
1904 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1905 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1906 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1907 ioc->ctl_cmds.smid = smid;
1908
1909 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1910 mpi_request->BufferType = buffer_type;
1911 mpi_request->VF_ID = 0; /* TODO */
1912 mpi_request->VP_ID = 0;
1913
1914 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001915 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07001916 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301917 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1918
1919 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -04001920 *issue_reset = mpt3sas_base_check_cmd_timeout(ioc,
1921 ioc->ctl_cmds.status, mpi_request,
1922 sizeof(Mpi2DiagReleaseRequest_t)/4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301923 rc = -EFAULT;
1924 goto out;
1925 }
1926
1927 /* process the completed Reply Message Frame */
1928 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001929 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301930 rc = -EFAULT;
1931 goto out;
1932 }
1933
1934 mpi_reply = ioc->ctl_cmds.reply;
1935 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1936
1937 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1938 ioc->diag_buffer_status[buffer_type] |=
1939 MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07001940 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301941 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07001942 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1943 __func__,
1944 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301945 rc = -EFAULT;
1946 }
1947
1948 out:
1949 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1950 return rc;
1951}
1952
1953/**
1954 * _ctl_diag_release - request to send Diag Release Message to firmware
Bart Van Assche4beb4862018-06-15 14:42:01 -07001955 * @ioc: ?
1956 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301957 *
1958 * This allows ownership of the specified buffer to returned to the driver,
1959 * allowing an application to read the buffer without fear that firmware is
Masahiro Yamada9a284e52017-02-27 14:29:48 -08001960 * overwriting information in the buffer.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301961 */
1962static long
1963_ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1964{
1965 struct mpt3_diag_release karg;
1966 void *request_data;
1967 int rc;
1968 u8 buffer_type;
1969 u8 issue_reset = 0;
1970
1971 if (copy_from_user(&karg, arg, sizeof(karg))) {
1972 pr_err("failure at %s:%d/%s()!\n",
1973 __FILE__, __LINE__, __func__);
1974 return -EFAULT;
1975 }
1976
Joe Perches919d8a32018-09-17 08:01:09 -07001977 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1978 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301979
1980 buffer_type = karg.unique_id & 0x000000ff;
1981 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001982 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1983 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301984 return -EPERM;
1985 }
1986
1987 if ((ioc->diag_buffer_status[buffer_type] &
1988 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001989 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1990 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301991 return -EINVAL;
1992 }
1993
1994 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07001995 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1996 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301997 return -EINVAL;
1998 }
1999
2000 if (ioc->diag_buffer_status[buffer_type] &
2001 MPT3_DIAG_BUFFER_IS_RELEASED) {
Joe Perches919d8a32018-09-17 08:01:09 -07002002 ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
2003 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302004 return 0;
2005 }
2006
2007 request_data = ioc->diag_buffer[buffer_type];
2008
2009 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002010 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2011 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302012 return -ENOMEM;
2013 }
2014
2015 /* buffers were released by due to host reset */
2016 if ((ioc->diag_buffer_status[buffer_type] &
2017 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2018 ioc->diag_buffer_status[buffer_type] |=
2019 MPT3_DIAG_BUFFER_IS_RELEASED;
2020 ioc->diag_buffer_status[buffer_type] &=
2021 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
Joe Perches919d8a32018-09-17 08:01:09 -07002022 ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
2023 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302024 return 0;
2025 }
2026
2027 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2028
2029 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07002030 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302031
2032 return rc;
2033}
2034
2035/**
2036 * _ctl_diag_read_buffer - request for copy of the diag buffer
2037 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002038 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302039 */
2040static long
2041_ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2042{
2043 struct mpt3_diag_read_buffer karg;
2044 struct mpt3_diag_read_buffer __user *uarg = arg;
2045 void *request_data, *diag_data;
2046 Mpi2DiagBufferPostRequest_t *mpi_request;
2047 Mpi2DiagBufferPostReply_t *mpi_reply;
2048 int rc, i;
2049 u8 buffer_type;
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002050 unsigned long request_size, copy_size;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302051 u16 smid;
2052 u16 ioc_status;
2053 u8 issue_reset = 0;
2054
2055 if (copy_from_user(&karg, arg, sizeof(karg))) {
2056 pr_err("failure at %s:%d/%s()!\n",
2057 __FILE__, __LINE__, __func__);
2058 return -EFAULT;
2059 }
2060
Joe Perches919d8a32018-09-17 08:01:09 -07002061 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2062 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302063
2064 buffer_type = karg.unique_id & 0x000000ff;
2065 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002066 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2067 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302068 return -EPERM;
2069 }
2070
2071 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002072 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2073 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302074 return -EINVAL;
2075 }
2076
2077 request_data = ioc->diag_buffer[buffer_type];
2078 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002079 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2080 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302081 return -ENOMEM;
2082 }
2083
2084 request_size = ioc->diag_buffer_sz[buffer_type];
2085
2086 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002087 ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
2088 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302089 return -EINVAL;
2090 }
2091
2092 if (karg.starting_offset > request_size)
2093 return -EINVAL;
2094
2095 diag_data = (void *)(request_data + karg.starting_offset);
Joe Perches919d8a32018-09-17 08:01:09 -07002096 dctlprintk(ioc,
2097 ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2098 __func__, diag_data, karg.starting_offset,
2099 karg.bytes_to_read));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302100
2101 /* Truncate data on requests that are too large */
2102 if ((diag_data + karg.bytes_to_read < diag_data) ||
2103 (diag_data + karg.bytes_to_read > request_data + request_size))
2104 copy_size = request_size - karg.starting_offset;
2105 else
2106 copy_size = karg.bytes_to_read;
2107
2108 if (copy_to_user((void __user *)uarg->diagnostic_data,
2109 diag_data, copy_size)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002110 ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2111 __func__, diag_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302112 return -EFAULT;
2113 }
2114
2115 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2116 return 0;
2117
Joe Perches919d8a32018-09-17 08:01:09 -07002118 dctlprintk(ioc,
2119 ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
2120 __func__, buffer_type));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302121 if ((ioc->diag_buffer_status[buffer_type] &
2122 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002123 dctlprintk(ioc,
2124 ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
2125 __func__, buffer_type));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302126 return 0;
2127 }
2128 /* Get a free request frame and save the message context.
2129 */
2130
2131 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07002132 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302133 rc = -EAGAIN;
2134 goto out;
2135 }
2136
2137 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2138 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07002139 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302140 rc = -EAGAIN;
2141 goto out;
2142 }
2143
2144 rc = 0;
2145 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2146 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2147 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2148 ioc->ctl_cmds.smid = smid;
2149
2150 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2151 mpi_request->BufferType = buffer_type;
2152 mpi_request->BufferLength =
2153 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2154 mpi_request->BufferAddress =
2155 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2156 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2157 mpi_request->ProductSpecific[i] =
2158 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2159 mpi_request->VF_ID = 0; /* TODO */
2160 mpi_request->VP_ID = 0;
2161
2162 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04002163 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002164 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302165 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2166
2167 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -04002168 issue_reset =
2169 mpt3sas_base_check_cmd_timeout(ioc,
2170 ioc->ctl_cmds.status, mpi_request,
2171 sizeof(Mpi2DiagBufferPostRequest_t)/4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302172 goto issue_host_reset;
2173 }
2174
2175 /* process the completed Reply Message Frame */
2176 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002177 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302178 rc = -EFAULT;
2179 goto out;
2180 }
2181
2182 mpi_reply = ioc->ctl_cmds.reply;
2183 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2184
2185 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2186 ioc->diag_buffer_status[buffer_type] |=
2187 MPT3_DIAG_BUFFER_IS_REGISTERED;
Joe Perches919d8a32018-09-17 08:01:09 -07002188 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302189 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07002190 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2191 __func__, ioc_status,
2192 le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302193 rc = -EFAULT;
2194 }
2195
2196 issue_host_reset:
2197 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07002198 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302199
2200 out:
2201
2202 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2203 return rc;
2204}
2205
2206
2207
2208#ifdef CONFIG_COMPAT
2209/**
2210 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2211 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002212 * @cmd: ioctl opcode
2213 * @arg: (struct mpt3_ioctl_command32)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302214 *
2215 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2216 */
2217static long
2218_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2219 void __user *arg)
2220{
2221 struct mpt3_ioctl_command32 karg32;
2222 struct mpt3_ioctl_command32 __user *uarg;
2223 struct mpt3_ioctl_command karg;
2224
2225 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2226 return -EINVAL;
2227
2228 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2229
2230 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2231 pr_err("failure at %s:%d/%s()!\n",
2232 __FILE__, __LINE__, __func__);
2233 return -EFAULT;
2234 }
2235
2236 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2237 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2238 karg.hdr.port_number = karg32.hdr.port_number;
2239 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2240 karg.timeout = karg32.timeout;
2241 karg.max_reply_bytes = karg32.max_reply_bytes;
2242 karg.data_in_size = karg32.data_in_size;
2243 karg.data_out_size = karg32.data_out_size;
2244 karg.max_sense_bytes = karg32.max_sense_bytes;
2245 karg.data_sge_offset = karg32.data_sge_offset;
2246 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2247 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2248 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2249 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2250 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2251}
2252#endif
2253
2254/**
2255 * _ctl_ioctl_main - main ioctl entry point
Bart Van Assche4beb4862018-06-15 14:42:01 -07002256 * @file: (struct file)
2257 * @cmd: ioctl opcode
2258 * @arg: user space data buffer
2259 * @compat: handles 32 bit applications in 64bit os
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302260 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302261 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302262 */
2263static long
2264_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302265 u8 compat, u16 mpi_version)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302266{
2267 struct MPT3SAS_ADAPTER *ioc;
2268 struct mpt3_ioctl_header ioctl_header;
2269 enum block_state state;
2270 long ret = -EINVAL;
2271
2272 /* get IOCTL header */
2273 if (copy_from_user(&ioctl_header, (char __user *)arg,
2274 sizeof(struct mpt3_ioctl_header))) {
2275 pr_err("failure at %s:%d/%s()!\n",
2276 __FILE__, __LINE__, __func__);
2277 return -EFAULT;
2278 }
2279
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302280 if (_ctl_verify_adapter(ioctl_header.ioc_number,
2281 &ioc, mpi_version) == -1 || !ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302282 return -ENODEV;
2283
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302284 /* pci_access_mutex lock acquired by ioctl path */
2285 mutex_lock(&ioc->pci_access_mutex);
2286
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302287 if (ioc->shost_recovery || ioc->pci_error_recovery ||
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302288 ioc->is_driver_loading || ioc->remove_host) {
2289 ret = -EAGAIN;
2290 goto out_unlock_pciaccess;
2291 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302292
2293 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2294 if (state == NON_BLOCKING) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302295 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2296 ret = -EAGAIN;
2297 goto out_unlock_pciaccess;
2298 }
2299 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2300 ret = -ERESTARTSYS;
2301 goto out_unlock_pciaccess;
2302 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302303
2304
2305 switch (cmd) {
2306 case MPT3IOCINFO:
2307 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2308 ret = _ctl_getiocinfo(ioc, arg);
2309 break;
2310#ifdef CONFIG_COMPAT
2311 case MPT3COMMAND32:
2312#endif
2313 case MPT3COMMAND:
2314 {
2315 struct mpt3_ioctl_command __user *uarg;
2316 struct mpt3_ioctl_command karg;
2317
2318#ifdef CONFIG_COMPAT
2319 if (compat) {
2320 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2321 break;
2322 }
2323#endif
2324 if (copy_from_user(&karg, arg, sizeof(karg))) {
2325 pr_err("failure at %s:%d/%s()!\n",
2326 __FILE__, __LINE__, __func__);
2327 ret = -EFAULT;
2328 break;
2329 }
2330
Gen Zhangf9e3ebe2019-05-30 09:10:30 +08002331 if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2332 ret = -EINVAL;
2333 break;
2334 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302335 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2336 uarg = arg;
2337 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2338 }
2339 break;
2340 }
2341 case MPT3EVENTQUERY:
2342 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2343 ret = _ctl_eventquery(ioc, arg);
2344 break;
2345 case MPT3EVENTENABLE:
2346 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2347 ret = _ctl_eventenable(ioc, arg);
2348 break;
2349 case MPT3EVENTREPORT:
2350 ret = _ctl_eventreport(ioc, arg);
2351 break;
2352 case MPT3HARDRESET:
2353 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2354 ret = _ctl_do_reset(ioc, arg);
2355 break;
2356 case MPT3BTDHMAPPING:
2357 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2358 ret = _ctl_btdh_mapping(ioc, arg);
2359 break;
2360 case MPT3DIAGREGISTER:
2361 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2362 ret = _ctl_diag_register(ioc, arg);
2363 break;
2364 case MPT3DIAGUNREGISTER:
2365 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2366 ret = _ctl_diag_unregister(ioc, arg);
2367 break;
2368 case MPT3DIAGQUERY:
2369 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2370 ret = _ctl_diag_query(ioc, arg);
2371 break;
2372 case MPT3DIAGRELEASE:
2373 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2374 ret = _ctl_diag_release(ioc, arg);
2375 break;
2376 case MPT3DIAGREADBUFFER:
2377 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2378 ret = _ctl_diag_read_buffer(ioc, arg);
2379 break;
2380 default:
Joe Perches919d8a32018-09-17 08:01:09 -07002381 dctlprintk(ioc,
2382 ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2383 cmd));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302384 break;
2385 }
2386
2387 mutex_unlock(&ioc->ctl_cmds.mutex);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302388out_unlock_pciaccess:
2389 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302390 return ret;
2391}
2392
2393/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302394 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002395 * @file: (struct file)
2396 * @cmd: ioctl opcode
2397 * @arg: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302398 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002399static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302400_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302401{
2402 long ret;
2403
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302404 /* pass MPI25_VERSION | MPI26_VERSION value,
2405 * to indicate that this ioctl cmd
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302406 * came from mpt3ctl ioctl device.
2407 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302408 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2409 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302410 return ret;
2411}
2412
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302413/**
2414 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002415 * @file: (struct file)
2416 * @cmd: ioctl opcode
2417 * @arg: ?
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302418 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002419static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302420_ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2421{
2422 long ret;
2423
2424 /* pass MPI2_VERSION value, to indicate that this ioctl cmd
2425 * came from mpt2ctl ioctl device.
2426 */
2427 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2428 return ret;
2429}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302430#ifdef CONFIG_COMPAT
2431/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302432 *_ ctl_ioctl_compat - main ioctl entry point (compat)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002433 * @file: ?
2434 * @cmd: ?
2435 * @arg: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302436 *
2437 * This routine handles 32 bit applications in 64bit os.
2438 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002439static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302440_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302441{
2442 long ret;
2443
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302444 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2445 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302446 return ret;
2447}
2448
2449/**
2450 *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002451 * @file: ?
2452 * @cmd: ?
2453 * @arg: ?
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302454 *
2455 * This routine handles 32 bit applications in 64bit os.
2456 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002457static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302458_ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2459{
2460 long ret;
2461
2462 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302463 return ret;
2464}
2465#endif
2466
2467/* scsi host attributes */
2468/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002469 * version_fw_show - firmware version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002470 * @cdev: pointer to embedded class device
2471 * @attr: ?
2472 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302473 *
2474 * A sysfs 'read-only' shost attribute.
2475 */
2476static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002477version_fw_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302478 char *buf)
2479{
2480 struct Scsi_Host *shost = class_to_shost(cdev);
2481 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2482
2483 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2484 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2485 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2486 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2487 ioc->facts.FWVersion.Word & 0x000000FF);
2488}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002489static DEVICE_ATTR_RO(version_fw);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302490
2491/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002492 * version_bios_show - bios version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002493 * @cdev: pointer to embedded class device
2494 * @attr: ?
2495 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302496 *
2497 * A sysfs 'read-only' shost attribute.
2498 */
2499static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002500version_bios_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302501 char *buf)
2502{
2503 struct Scsi_Host *shost = class_to_shost(cdev);
2504 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2505
2506 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2507
2508 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2509 (version & 0xFF000000) >> 24,
2510 (version & 0x00FF0000) >> 16,
2511 (version & 0x0000FF00) >> 8,
2512 version & 0x000000FF);
2513}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002514static DEVICE_ATTR_RO(version_bios);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302515
2516/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002517 * version_mpi_show - MPI (message passing interface) version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002518 * @cdev: pointer to embedded class device
2519 * @attr: ?
2520 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302521 *
2522 * A sysfs 'read-only' shost attribute.
2523 */
2524static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002525version_mpi_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302526 char *buf)
2527{
2528 struct Scsi_Host *shost = class_to_shost(cdev);
2529 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2530
2531 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2532 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2533}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002534static DEVICE_ATTR_RO(version_mpi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302535
2536/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002537 * version_product_show - product name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002538 * @cdev: pointer to embedded class device
2539 * @attr: ?
2540 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302541 *
2542 * A sysfs 'read-only' shost attribute.
2543 */
2544static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002545version_product_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302546 char *buf)
2547{
2548 struct Scsi_Host *shost = class_to_shost(cdev);
2549 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2550
2551 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2552}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002553static DEVICE_ATTR_RO(version_product);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302554
2555/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002556 * version_nvdata_persistent_show - ndvata persistent version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002557 * @cdev: pointer to embedded class device
2558 * @attr: ?
2559 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302560 *
2561 * A sysfs 'read-only' shost attribute.
2562 */
2563static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002564version_nvdata_persistent_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302565 struct device_attribute *attr, char *buf)
2566{
2567 struct Scsi_Host *shost = class_to_shost(cdev);
2568 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2569
2570 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2571 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2572}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002573static DEVICE_ATTR_RO(version_nvdata_persistent);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302574
2575/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002576 * version_nvdata_default_show - nvdata default version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002577 * @cdev: pointer to embedded class device
2578 * @attr: ?
2579 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302580 *
2581 * A sysfs 'read-only' shost attribute.
2582 */
2583static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002584version_nvdata_default_show(struct device *cdev, struct device_attribute
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302585 *attr, char *buf)
2586{
2587 struct Scsi_Host *shost = class_to_shost(cdev);
2588 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2589
2590 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2591 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2592}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002593static DEVICE_ATTR_RO(version_nvdata_default);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302594
2595/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002596 * board_name_show - board name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002597 * @cdev: pointer to embedded class device
2598 * @attr: ?
2599 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302600 *
2601 * A sysfs 'read-only' shost attribute.
2602 */
2603static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002604board_name_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302605 char *buf)
2606{
2607 struct Scsi_Host *shost = class_to_shost(cdev);
2608 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2609
2610 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2611}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002612static DEVICE_ATTR_RO(board_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302613
2614/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002615 * board_assembly_show - board assembly name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002616 * @cdev: pointer to embedded class device
2617 * @attr: ?
2618 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302619 *
2620 * A sysfs 'read-only' shost attribute.
2621 */
2622static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002623board_assembly_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302624 char *buf)
2625{
2626 struct Scsi_Host *shost = class_to_shost(cdev);
2627 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2628
2629 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2630}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002631static DEVICE_ATTR_RO(board_assembly);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302632
2633/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002634 * board_tracer_show - board tracer number
Bart Van Assche4beb4862018-06-15 14:42:01 -07002635 * @cdev: pointer to embedded class device
2636 * @attr: ?
2637 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302638 *
2639 * A sysfs 'read-only' shost attribute.
2640 */
2641static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002642board_tracer_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302643 char *buf)
2644{
2645 struct Scsi_Host *shost = class_to_shost(cdev);
2646 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2647
2648 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2649}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002650static DEVICE_ATTR_RO(board_tracer);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302651
2652/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002653 * io_delay_show - io missing delay
Bart Van Assche4beb4862018-06-15 14:42:01 -07002654 * @cdev: pointer to embedded class device
2655 * @attr: ?
2656 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302657 *
2658 * This is for firmware implemention for deboucing device
2659 * removal events.
2660 *
2661 * A sysfs 'read-only' shost attribute.
2662 */
2663static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002664io_delay_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302665 char *buf)
2666{
2667 struct Scsi_Host *shost = class_to_shost(cdev);
2668 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2669
2670 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2671}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002672static DEVICE_ATTR_RO(io_delay);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302673
2674/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002675 * device_delay_show - device missing delay
Bart Van Assche4beb4862018-06-15 14:42:01 -07002676 * @cdev: pointer to embedded class device
2677 * @attr: ?
2678 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302679 *
2680 * This is for firmware implemention for deboucing device
2681 * removal events.
2682 *
2683 * A sysfs 'read-only' shost attribute.
2684 */
2685static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002686device_delay_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302687 char *buf)
2688{
2689 struct Scsi_Host *shost = class_to_shost(cdev);
2690 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2691
2692 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2693}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002694static DEVICE_ATTR_RO(device_delay);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302695
2696/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002697 * fw_queue_depth_show - global credits
Bart Van Assche4beb4862018-06-15 14:42:01 -07002698 * @cdev: pointer to embedded class device
2699 * @attr: ?
2700 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302701 *
2702 * This is firmware queue depth limit
2703 *
2704 * A sysfs 'read-only' shost attribute.
2705 */
2706static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002707fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302708 char *buf)
2709{
2710 struct Scsi_Host *shost = class_to_shost(cdev);
2711 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2712
2713 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2714}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002715static DEVICE_ATTR_RO(fw_queue_depth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302716
2717/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002718 * sas_address_show - sas address
Bart Van Assche4beb4862018-06-15 14:42:01 -07002719 * @cdev: pointer to embedded class device
2720 * @attr: ?
2721 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302722 *
2723 * This is the controller sas address
2724 *
2725 * A sysfs 'read-only' shost attribute.
2726 */
2727static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002728host_sas_address_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302729 char *buf)
2730
2731{
2732 struct Scsi_Host *shost = class_to_shost(cdev);
2733 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2734
2735 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2736 (unsigned long long)ioc->sas_hba.sas_address);
2737}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002738static DEVICE_ATTR_RO(host_sas_address);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302739
2740/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002741 * logging_level_show - logging level
Bart Van Assche4beb4862018-06-15 14:42:01 -07002742 * @cdev: pointer to embedded class device
2743 * @attr: ?
2744 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302745 *
2746 * A sysfs 'read/write' shost attribute.
2747 */
2748static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002749logging_level_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302750 char *buf)
2751{
2752 struct Scsi_Host *shost = class_to_shost(cdev);
2753 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2754
2755 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2756}
2757static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002758logging_level_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302759 const char *buf, size_t count)
2760{
2761 struct Scsi_Host *shost = class_to_shost(cdev);
2762 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2763 int val = 0;
2764
2765 if (sscanf(buf, "%x", &val) != 1)
2766 return -EINVAL;
2767
2768 ioc->logging_level = val;
Joe Perches919d8a32018-09-17 08:01:09 -07002769 ioc_info(ioc, "logging_level=%08xh\n",
2770 ioc->logging_level);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302771 return strlen(buf);
2772}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002773static DEVICE_ATTR_RW(logging_level);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302774
2775/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002776 * fwfault_debug_show - show/store fwfault_debug
Bart Van Assche4beb4862018-06-15 14:42:01 -07002777 * @cdev: pointer to embedded class device
2778 * @attr: ?
2779 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302780 *
2781 * mpt3sas_fwfault_debug is command line option
2782 * A sysfs 'read/write' shost attribute.
2783 */
2784static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002785fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302786 char *buf)
2787{
2788 struct Scsi_Host *shost = class_to_shost(cdev);
2789 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2790
2791 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2792}
2793static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002794fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302795 const char *buf, size_t count)
2796{
2797 struct Scsi_Host *shost = class_to_shost(cdev);
2798 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2799 int val = 0;
2800
2801 if (sscanf(buf, "%d", &val) != 1)
2802 return -EINVAL;
2803
2804 ioc->fwfault_debug = val;
Joe Perches919d8a32018-09-17 08:01:09 -07002805 ioc_info(ioc, "fwfault_debug=%d\n",
2806 ioc->fwfault_debug);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302807 return strlen(buf);
2808}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002809static DEVICE_ATTR_RW(fwfault_debug);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302810
2811/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002812 * ioc_reset_count_show - ioc reset count
Bart Van Assche4beb4862018-06-15 14:42:01 -07002813 * @cdev: pointer to embedded class device
2814 * @attr: ?
2815 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302816 *
2817 * This is firmware queue depth limit
2818 *
2819 * A sysfs 'read-only' shost attribute.
2820 */
2821static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002822ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302823 char *buf)
2824{
2825 struct Scsi_Host *shost = class_to_shost(cdev);
2826 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2827
2828 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
2829}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002830static DEVICE_ATTR_RO(ioc_reset_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302831
2832/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002833 * reply_queue_count_show - number of reply queues
Bart Van Assche4beb4862018-06-15 14:42:01 -07002834 * @cdev: pointer to embedded class device
2835 * @attr: ?
2836 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302837 *
2838 * This is number of reply queues
2839 *
2840 * A sysfs 'read-only' shost attribute.
2841 */
2842static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002843reply_queue_count_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302844 struct device_attribute *attr, char *buf)
2845{
2846 u8 reply_queue_count;
2847 struct Scsi_Host *shost = class_to_shost(cdev);
2848 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2849
2850 if ((ioc->facts.IOCCapabilities &
2851 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2852 reply_queue_count = ioc->reply_queue_count;
2853 else
2854 reply_queue_count = 1;
2855
2856 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2857}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002858static DEVICE_ATTR_RO(reply_queue_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302859
Sreekanth Reddy42263092015-11-11 17:30:29 +05302860/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002861 * BRM_status_show - Backup Rail Monitor Status
Bart Van Assche4beb4862018-06-15 14:42:01 -07002862 * @cdev: pointer to embedded class device
2863 * @attr: ?
2864 * @buf: the buffer returned
Sreekanth Reddy42263092015-11-11 17:30:29 +05302865 *
2866 * This is number of reply queues
2867 *
2868 * A sysfs 'read-only' shost attribute.
2869 */
2870static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002871BRM_status_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddy42263092015-11-11 17:30:29 +05302872 char *buf)
2873{
2874 struct Scsi_Host *shost = class_to_shost(cdev);
2875 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2876 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2877 Mpi2ConfigReply_t mpi_reply;
2878 u16 backup_rail_monitor_status = 0;
2879 u16 ioc_status;
2880 int sz;
2881 ssize_t rc = 0;
2882
2883 if (!ioc->is_warpdrive) {
Joe Perches919d8a32018-09-17 08:01:09 -07002884 ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
2885 __func__);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302886 goto out;
2887 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302888 /* pci_access_mutex lock acquired by sysfs show path */
2889 mutex_lock(&ioc->pci_access_mutex);
2890 if (ioc->pci_error_recovery || ioc->remove_host) {
2891 mutex_unlock(&ioc->pci_access_mutex);
2892 return 0;
2893 }
Sreekanth Reddy42263092015-11-11 17:30:29 +05302894
2895 /* allocate upto GPIOVal 36 entries */
2896 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2897 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2898 if (!io_unit_pg3) {
Joe Perches919d8a32018-09-17 08:01:09 -07002899 ioc_err(ioc, "%s: failed allocating memory for iounit_pg3: (%d) bytes\n",
2900 __func__, sz);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302901 goto out;
2902 }
2903
2904 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2905 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002906 ioc_err(ioc, "%s: failed reading iounit_pg3\n",
2907 __func__);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302908 goto out;
2909 }
2910
2911 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2912 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
Joe Perches919d8a32018-09-17 08:01:09 -07002913 ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
2914 __func__, ioc_status);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302915 goto out;
2916 }
2917
2918 if (io_unit_pg3->GPIOCount < 25) {
Joe Perches919d8a32018-09-17 08:01:09 -07002919 ioc_err(ioc, "%s: iounit_pg3->GPIOCount less than 25 entries, detected (%d) entries\n",
2920 __func__, io_unit_pg3->GPIOCount);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302921 goto out;
2922 }
2923
2924 /* BRM status is in bit zero of GPIOVal[24] */
2925 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2926 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2927
2928 out:
2929 kfree(io_unit_pg3);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302930 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302931 return rc;
2932}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002933static DEVICE_ATTR_RO(BRM_status);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302934
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302935struct DIAG_BUFFER_START {
2936 __le32 Size;
2937 __le32 DiagVersion;
2938 u8 BufferType;
2939 u8 Reserved[3];
2940 __le32 Reserved1;
2941 __le32 Reserved2;
2942 __le32 Reserved3;
2943};
2944
2945/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002946 * host_trace_buffer_size_show - host buffer size (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002947 * @cdev: pointer to embedded class device
2948 * @attr: ?
2949 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302950 *
2951 * A sysfs 'read-only' shost attribute.
2952 */
2953static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002954host_trace_buffer_size_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302955 struct device_attribute *attr, char *buf)
2956{
2957 struct Scsi_Host *shost = class_to_shost(cdev);
2958 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2959 u32 size = 0;
2960 struct DIAG_BUFFER_START *request_data;
2961
2962 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002963 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
2964 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302965 return 0;
2966 }
2967
2968 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2969 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002970 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
2971 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302972 return 0;
2973 }
2974
2975 request_data = (struct DIAG_BUFFER_START *)
2976 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2977 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2978 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
2979 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
2980 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2981 size = le32_to_cpu(request_data->Size);
2982
2983 ioc->ring_buffer_sz = size;
2984 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2985}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002986static DEVICE_ATTR_RO(host_trace_buffer_size);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302987
2988/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002989 * host_trace_buffer_show - firmware ring buffer (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002990 * @cdev: pointer to embedded class device
2991 * @attr: ?
2992 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302993 *
2994 * A sysfs 'read/write' shost attribute.
2995 *
2996 * You will only be able to read 4k bytes of ring buffer at a time.
2997 * In order to read beyond 4k bytes, you will have to write out the
2998 * offset to the same attribute, it will move the pointer.
2999 */
3000static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003001host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303002 char *buf)
3003{
3004 struct Scsi_Host *shost = class_to_shost(cdev);
3005 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3006 void *request_data;
3007 u32 size;
3008
3009 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
Joe Perches919d8a32018-09-17 08:01:09 -07003010 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3011 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303012 return 0;
3013 }
3014
3015 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3016 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003017 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3018 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303019 return 0;
3020 }
3021
3022 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3023 return 0;
3024
3025 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3026 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3027 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3028 memcpy(buf, request_data, size);
3029 return size;
3030}
3031
3032static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003033host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303034 const char *buf, size_t count)
3035{
3036 struct Scsi_Host *shost = class_to_shost(cdev);
3037 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3038 int val = 0;
3039
3040 if (sscanf(buf, "%d", &val) != 1)
3041 return -EINVAL;
3042
3043 ioc->ring_buffer_offset = val;
3044 return strlen(buf);
3045}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003046static DEVICE_ATTR_RW(host_trace_buffer);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303047
3048
3049/*****************************************/
3050
3051/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003052 * host_trace_buffer_enable_show - firmware ring buffer (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07003053 * @cdev: pointer to embedded class device
3054 * @attr: ?
3055 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303056 *
3057 * A sysfs 'read/write' shost attribute.
3058 *
3059 * This is a mechnism to post/release host_trace_buffers
3060 */
3061static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003062host_trace_buffer_enable_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303063 struct device_attribute *attr, char *buf)
3064{
3065 struct Scsi_Host *shost = class_to_shost(cdev);
3066 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3067
3068 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3069 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3070 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3071 return snprintf(buf, PAGE_SIZE, "off\n");
3072 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3073 MPT3_DIAG_BUFFER_IS_RELEASED))
3074 return snprintf(buf, PAGE_SIZE, "release\n");
3075 else
3076 return snprintf(buf, PAGE_SIZE, "post\n");
3077}
3078
3079static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003080host_trace_buffer_enable_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303081 struct device_attribute *attr, const char *buf, size_t count)
3082{
3083 struct Scsi_Host *shost = class_to_shost(cdev);
3084 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3085 char str[10] = "";
3086 struct mpt3_diag_register diag_register;
3087 u8 issue_reset = 0;
3088
3089 /* don't allow post/release occurr while recovery is active */
3090 if (ioc->shost_recovery || ioc->remove_host ||
3091 ioc->pci_error_recovery || ioc->is_driver_loading)
3092 return -EBUSY;
3093
3094 if (sscanf(buf, "%9s", str) != 1)
3095 return -EINVAL;
3096
3097 if (!strcmp(str, "post")) {
3098 /* exit out if host buffers are already posted */
3099 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3100 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3101 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3102 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3103 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3104 goto out;
3105 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
Joe Perches919d8a32018-09-17 08:01:09 -07003106 ioc_info(ioc, "posting host trace buffers\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303107 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3108 diag_register.requested_buffer_size = (1024 * 1024);
3109 diag_register.unique_id = 0x7075900;
3110 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3111 _ctl_diag_register_2(ioc, &diag_register);
3112 } else if (!strcmp(str, "release")) {
3113 /* exit out if host buffers are already released */
3114 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3115 goto out;
3116 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3117 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3118 goto out;
3119 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3120 MPT3_DIAG_BUFFER_IS_RELEASED))
3121 goto out;
Joe Perches919d8a32018-09-17 08:01:09 -07003122 ioc_info(ioc, "releasing host trace buffer\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303123 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3124 &issue_reset);
3125 }
3126
3127 out:
3128 return strlen(buf);
3129}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003130static DEVICE_ATTR_RW(host_trace_buffer_enable);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303131
3132/*********** diagnostic trigger suppport *********************************/
3133
3134/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003135 * diag_trigger_master_show - show the diag_trigger_master attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003136 * @cdev: pointer to embedded class device
3137 * @attr: ?
3138 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303139 *
3140 * A sysfs 'read/write' shost attribute.
3141 */
3142static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003143diag_trigger_master_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303144 struct device_attribute *attr, char *buf)
3145
3146{
3147 struct Scsi_Host *shost = class_to_shost(cdev);
3148 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3149 unsigned long flags;
3150 ssize_t rc;
3151
3152 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3153 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3154 memcpy(buf, &ioc->diag_trigger_master, rc);
3155 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3156 return rc;
3157}
3158
3159/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003160 * diag_trigger_master_store - store the diag_trigger_master attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003161 * @cdev: pointer to embedded class device
3162 * @attr: ?
3163 * @buf: the buffer returned
3164 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303165 *
3166 * A sysfs 'read/write' shost attribute.
3167 */
3168static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003169diag_trigger_master_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303170 struct device_attribute *attr, const char *buf, size_t count)
3171
3172{
3173 struct Scsi_Host *shost = class_to_shost(cdev);
3174 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3175 unsigned long flags;
3176 ssize_t rc;
3177
3178 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3179 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3180 memset(&ioc->diag_trigger_master, 0,
3181 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3182 memcpy(&ioc->diag_trigger_master, buf, rc);
3183 ioc->diag_trigger_master.MasterData |=
3184 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3185 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3186 return rc;
3187}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003188static DEVICE_ATTR_RW(diag_trigger_master);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303189
3190
3191/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003192 * diag_trigger_event_show - show the diag_trigger_event attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003193 * @cdev: pointer to embedded class device
3194 * @attr: ?
3195 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303196 *
3197 * A sysfs 'read/write' shost attribute.
3198 */
3199static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003200diag_trigger_event_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303201 struct device_attribute *attr, char *buf)
3202{
3203 struct Scsi_Host *shost = class_to_shost(cdev);
3204 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3205 unsigned long flags;
3206 ssize_t rc;
3207
3208 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3209 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3210 memcpy(buf, &ioc->diag_trigger_event, rc);
3211 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3212 return rc;
3213}
3214
3215/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003216 * diag_trigger_event_store - store the diag_trigger_event attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003217 * @cdev: pointer to embedded class device
3218 * @attr: ?
3219 * @buf: the buffer returned
3220 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303221 *
3222 * A sysfs 'read/write' shost attribute.
3223 */
3224static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003225diag_trigger_event_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303226 struct device_attribute *attr, const char *buf, size_t count)
3227
3228{
3229 struct Scsi_Host *shost = class_to_shost(cdev);
3230 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3231 unsigned long flags;
3232 ssize_t sz;
3233
3234 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3235 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3236 memset(&ioc->diag_trigger_event, 0,
3237 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3238 memcpy(&ioc->diag_trigger_event, buf, sz);
3239 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3240 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3241 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3242 return sz;
3243}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003244static DEVICE_ATTR_RW(diag_trigger_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303245
3246
3247/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003248 * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003249 * @cdev: pointer to embedded class device
3250 * @attr: ?
3251 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303252 *
3253 * A sysfs 'read/write' shost attribute.
3254 */
3255static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003256diag_trigger_scsi_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303257 struct device_attribute *attr, char *buf)
3258{
3259 struct Scsi_Host *shost = class_to_shost(cdev);
3260 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3261 unsigned long flags;
3262 ssize_t rc;
3263
3264 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3265 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3266 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3267 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3268 return rc;
3269}
3270
3271/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003272 * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003273 * @cdev: pointer to embedded class device
3274 * @attr: ?
3275 * @buf: the buffer returned
3276 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303277 *
3278 * A sysfs 'read/write' shost attribute.
3279 */
3280static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003281diag_trigger_scsi_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303282 struct device_attribute *attr, const char *buf, size_t count)
3283{
3284 struct Scsi_Host *shost = class_to_shost(cdev);
3285 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3286 unsigned long flags;
3287 ssize_t sz;
3288
3289 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
Dan Carpenter1de540a2019-07-26 09:52:05 +03003290 sz = min(sizeof(ioc->diag_trigger_scsi), count);
3291 memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303292 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3293 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3294 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3295 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3296 return sz;
3297}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003298static DEVICE_ATTR_RW(diag_trigger_scsi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303299
3300
3301/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003302 * diag_trigger_scsi_show - show the diag_trigger_mpi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003303 * @cdev: pointer to embedded class device
3304 * @attr: ?
3305 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303306 *
3307 * A sysfs 'read/write' shost attribute.
3308 */
3309static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003310diag_trigger_mpi_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303311 struct device_attribute *attr, char *buf)
3312{
3313 struct Scsi_Host *shost = class_to_shost(cdev);
3314 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3315 unsigned long flags;
3316 ssize_t rc;
3317
3318 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3319 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3320 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3321 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3322 return rc;
3323}
3324
3325/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003326 * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003327 * @cdev: pointer to embedded class device
3328 * @attr: ?
3329 * @buf: the buffer returned
3330 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303331 *
3332 * A sysfs 'read/write' shost attribute.
3333 */
3334static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003335diag_trigger_mpi_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303336 struct device_attribute *attr, const char *buf, size_t count)
3337{
3338 struct Scsi_Host *shost = class_to_shost(cdev);
3339 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3340 unsigned long flags;
3341 ssize_t sz;
3342
3343 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3344 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3345 memset(&ioc->diag_trigger_mpi, 0,
Dan Carpenter66331e82012-12-07 13:56:22 +03003346 sizeof(ioc->diag_trigger_mpi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303347 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3348 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3349 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3350 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3351 return sz;
3352}
3353
Tomas Henzlc9df1442019-06-14 16:41:44 +02003354static DEVICE_ATTR_RW(diag_trigger_mpi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303355
3356/*********** diagnostic trigger suppport *** END ****************************/
3357
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303358/*****************************************/
3359
3360struct device_attribute *mpt3sas_host_attrs[] = {
3361 &dev_attr_version_fw,
3362 &dev_attr_version_bios,
3363 &dev_attr_version_mpi,
3364 &dev_attr_version_product,
3365 &dev_attr_version_nvdata_persistent,
3366 &dev_attr_version_nvdata_default,
3367 &dev_attr_board_name,
3368 &dev_attr_board_assembly,
3369 &dev_attr_board_tracer,
3370 &dev_attr_io_delay,
3371 &dev_attr_device_delay,
3372 &dev_attr_logging_level,
3373 &dev_attr_fwfault_debug,
3374 &dev_attr_fw_queue_depth,
3375 &dev_attr_host_sas_address,
3376 &dev_attr_ioc_reset_count,
3377 &dev_attr_host_trace_buffer_size,
3378 &dev_attr_host_trace_buffer,
3379 &dev_attr_host_trace_buffer_enable,
3380 &dev_attr_reply_queue_count,
3381 &dev_attr_diag_trigger_master,
3382 &dev_attr_diag_trigger_event,
3383 &dev_attr_diag_trigger_scsi,
3384 &dev_attr_diag_trigger_mpi,
Sreekanth Reddy42263092015-11-11 17:30:29 +05303385 &dev_attr_BRM_status,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303386 NULL,
3387};
3388
3389/* device attributes */
3390
3391/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003392 * sas_address_show - sas address
Bart Van Assche4beb4862018-06-15 14:42:01 -07003393 * @dev: pointer to embedded class device
3394 * @attr: ?
3395 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303396 *
3397 * This is the sas address for the target
3398 *
3399 * A sysfs 'read-only' shost attribute.
3400 */
3401static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003402sas_address_show(struct device *dev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303403 char *buf)
3404{
3405 struct scsi_device *sdev = to_scsi_device(dev);
3406 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3407
3408 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3409 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3410}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003411static DEVICE_ATTR_RO(sas_address);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303412
3413/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003414 * sas_device_handle_show - device handle
Bart Van Assche4beb4862018-06-15 14:42:01 -07003415 * @dev: pointer to embedded class device
3416 * @attr: ?
3417 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303418 *
3419 * This is the firmware assigned device handle
3420 *
3421 * A sysfs 'read-only' shost attribute.
3422 */
3423static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003424sas_device_handle_show(struct device *dev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303425 char *buf)
3426{
3427 struct scsi_device *sdev = to_scsi_device(dev);
3428 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3429
3430 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3431 sas_device_priv_data->sas_target->handle);
3432}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003433static DEVICE_ATTR_RO(sas_device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303434
Adam Manzanares307d9072016-12-12 16:31:40 -08003435/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003436 * sas_ncq_io_prio_show - send prioritized io commands to device
Bart Van Assche4beb4862018-06-15 14:42:01 -07003437 * @dev: pointer to embedded device
3438 * @attr: ?
3439 * @buf: the buffer returned
Adam Manzanares307d9072016-12-12 16:31:40 -08003440 *
3441 * A sysfs 'read/write' sdev attribute, only works with SATA
3442 */
3443static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003444sas_ncq_prio_enable_show(struct device *dev,
Adam Manzanares307d9072016-12-12 16:31:40 -08003445 struct device_attribute *attr, char *buf)
3446{
3447 struct scsi_device *sdev = to_scsi_device(dev);
3448 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3449
3450 return snprintf(buf, PAGE_SIZE, "%d\n",
3451 sas_device_priv_data->ncq_prio_enable);
3452}
3453
3454static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003455sas_ncq_prio_enable_store(struct device *dev,
Adam Manzanares307d9072016-12-12 16:31:40 -08003456 struct device_attribute *attr,
3457 const char *buf, size_t count)
3458{
3459 struct scsi_device *sdev = to_scsi_device(dev);
3460 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3461 bool ncq_prio_enable = 0;
3462
3463 if (kstrtobool(buf, &ncq_prio_enable))
3464 return -EINVAL;
3465
3466 if (!scsih_ncq_prio_supp(sdev))
3467 return -EINVAL;
3468
3469 sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
3470 return strlen(buf);
3471}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003472static DEVICE_ATTR_RW(sas_ncq_prio_enable);
Adam Manzanares307d9072016-12-12 16:31:40 -08003473
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303474struct device_attribute *mpt3sas_dev_attrs[] = {
3475 &dev_attr_sas_address,
3476 &dev_attr_sas_device_handle,
Adam Manzanares307d9072016-12-12 16:31:40 -08003477 &dev_attr_sas_ncq_prio_enable,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303478 NULL,
3479};
3480
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303481/* file operations table for mpt3ctl device */
3482static const struct file_operations ctl_fops = {
3483 .owner = THIS_MODULE,
3484 .unlocked_ioctl = _ctl_ioctl,
3485 .poll = _ctl_poll,
3486 .fasync = _ctl_fasync,
3487#ifdef CONFIG_COMPAT
3488 .compat_ioctl = _ctl_ioctl_compat,
3489#endif
3490};
3491
3492/* file operations table for mpt2ctl device */
3493static const struct file_operations ctl_gen2_fops = {
3494 .owner = THIS_MODULE,
3495 .unlocked_ioctl = _ctl_mpt2_ioctl,
3496 .poll = _ctl_poll,
3497 .fasync = _ctl_fasync,
3498#ifdef CONFIG_COMPAT
3499 .compat_ioctl = _ctl_mpt2_ioctl_compat,
3500#endif
3501};
3502
3503static struct miscdevice ctl_dev = {
3504 .minor = MPT3SAS_MINOR,
3505 .name = MPT3SAS_DEV_NAME,
3506 .fops = &ctl_fops,
3507};
3508
3509static struct miscdevice gen2_ctl_dev = {
3510 .minor = MPT2SAS_MINOR,
3511 .name = MPT2SAS_DEV_NAME,
3512 .fops = &ctl_gen2_fops,
3513};
3514
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303515/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303516 * mpt3sas_ctl_init - main entry point for ctl.
Bart Van Assche4beb4862018-06-15 14:42:01 -07003517 * @hbas_to_enumerate: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303518 */
3519void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303520mpt3sas_ctl_init(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303521{
3522 async_queue = NULL;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303523
3524 /* Don't register mpt3ctl ioctl device if
3525 * hbas_to_enumarate is one.
3526 */
3527 if (hbas_to_enumerate != 1)
3528 if (misc_register(&ctl_dev) < 0)
3529 pr_err("%s can't register misc device [minor=%d]\n",
3530 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3531
3532 /* Don't register mpt3ctl ioctl device if
3533 * hbas_to_enumarate is two.
3534 */
3535 if (hbas_to_enumerate != 2)
3536 if (misc_register(&gen2_ctl_dev) < 0)
3537 pr_err("%s can't register misc device [minor=%d]\n",
3538 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3539
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303540 init_waitqueue_head(&ctl_poll_wait);
3541}
3542
3543/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303544 * mpt3sas_ctl_exit - exit point for ctl
Bart Van Assche4beb4862018-06-15 14:42:01 -07003545 * @hbas_to_enumerate: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303546 */
3547void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303548mpt3sas_ctl_exit(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303549{
3550 struct MPT3SAS_ADAPTER *ioc;
3551 int i;
3552
3553 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3554
3555 /* free memory associated to diag buffers */
3556 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3557 if (!ioc->diag_buffer[i])
3558 continue;
3559 if (!(ioc->diag_buffer_status[i] &
3560 MPT3_DIAG_BUFFER_IS_REGISTERED))
3561 continue;
3562 if ((ioc->diag_buffer_status[i] &
3563 MPT3_DIAG_BUFFER_IS_RELEASED))
3564 continue;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02003565 dma_free_coherent(&ioc->pdev->dev,
3566 ioc->diag_buffer_sz[i],
3567 ioc->diag_buffer[i],
3568 ioc->diag_buffer_dma[i]);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303569 ioc->diag_buffer[i] = NULL;
3570 ioc->diag_buffer_status[i] = 0;
3571 }
3572
3573 kfree(ioc->event_log);
3574 }
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303575 if (hbas_to_enumerate != 1)
3576 misc_deregister(&ctl_dev);
3577 if (hbas_to_enumerate != 2)
3578 misc_deregister(&gen2_ctl_dev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303579}