blob: 359586027c266f5cb16806b25d593898b0ef23f9 [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
932 if (toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL) {
933 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
934 data_in_dma, data_in_sz);
935 } else {
936 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
937 data_in_dma, data_in_sz);
938 }
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400939 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530940 break;
941 }
942 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
943 {
944 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
945 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
946
947 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
948 || sasiounit_request->Operation ==
949 MPI2_SAS_OP_PHY_LINK_RESET) {
950 ioc->ioc_link_reset_in_progress = 1;
951 ioc->ignore_loginfos = 1;
952 }
953 /* drop to default case for posting the request */
954 }
Bart Van Asscheeb0c7af2018-06-15 14:41:56 -0700955 /* fall through */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530956 default:
957 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
958 data_in_dma, data_in_sz);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -0400959 ioc->put_smid_default(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530960 break;
961 }
962
963 if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
964 timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
965 else
966 timeout = karg.timeout;
Calvin Owens8bbb1cf2016-07-28 21:38:22 -0700967 wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530968 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
969 Mpi2SCSITaskManagementRequest_t *tm_request =
970 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
971 mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
972 tm_request->DevHandle));
973 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
974 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
975 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
976 ioc->ioc_link_reset_in_progress) {
977 ioc->ioc_link_reset_in_progress = 0;
978 ioc->ignore_loginfos = 0;
979 }
980 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -0400981 issue_reset =
982 mpt3sas_base_check_cmd_timeout(ioc,
983 ioc->ctl_cmds.status, mpi_request,
984 karg.data_sge_offset);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530985 goto issue_host_reset;
986 }
987
988 mpi_reply = ioc->ctl_cmds.reply;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530989
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530990 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
991 (ioc->logging_level & MPT_DEBUG_TM)) {
992 Mpi2SCSITaskManagementReply_t *tm_reply =
993 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
994
Joe Perches919d8a32018-09-17 08:01:09 -0700995 ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
996 le16_to_cpu(tm_reply->IOCStatus),
997 le32_to_cpu(tm_reply->IOCLogInfo),
998 le32_to_cpu(tm_reply->TerminationCount));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530999 }
Sreekanth Reddyaf009412015-11-11 17:30:23 +05301000
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301001 /* copy out xdata to user */
1002 if (data_in_sz) {
1003 if (copy_to_user(karg.data_in_buf_ptr, data_in,
1004 data_in_sz)) {
1005 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1006 __LINE__, __func__);
1007 ret = -ENODATA;
1008 goto out;
1009 }
1010 }
1011
1012 /* copy out reply message frame to user */
1013 if (karg.max_reply_bytes) {
1014 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1015 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1016 sz)) {
1017 pr_err("failure at %s:%d/%s()!\n", __FILE__,
1018 __LINE__, __func__);
1019 ret = -ENODATA;
1020 goto out;
1021 }
1022 }
1023
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301024 /* copy out sense/NVMe Error Response to user */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301025 if (karg.max_sense_bytes && (mpi_request->Function ==
1026 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301027 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1028 MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1029 if (karg.sense_data_ptr == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001030 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 +05301031 goto out;
1032 }
1033 sz_arg = (mpi_request->Function ==
1034 MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1035 SCSI_SENSE_BUFFERSIZE;
1036 sz = min_t(u32, karg.max_sense_bytes, sz_arg);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301037 if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1038 sz)) {
1039 pr_err("failure at %s:%d/%s()!\n", __FILE__,
Suganath Prabu Subramaniaff39e62017-10-31 18:02:29 +05301040 __LINE__, __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301041 ret = -ENODATA;
1042 goto out;
1043 }
1044 }
1045
1046 issue_host_reset:
1047 if (issue_reset) {
1048 ret = -ENODATA;
1049 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1050 mpi_request->Function ==
1051 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1052 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001053 ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
1054 le16_to_cpu(mpi_request->FunctionDependent1));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301055 mpt3sas_halt_firmware(ioc);
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001056 pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1057 le16_to_cpu(mpi_request->FunctionDependent1));
1058 if (pcie_device && (!ioc->tm_custom_handling))
1059 mpt3sas_scsih_issue_locked_tm(ioc,
1060 le16_to_cpu(mpi_request->FunctionDependent1),
1061 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1062 0, pcie_device->reset_timeout,
1063 tr_method);
1064 else
1065 mpt3sas_scsih_issue_locked_tm(ioc,
1066 le16_to_cpu(mpi_request->FunctionDependent1),
1067 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1068 0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301069 } else
Calvin Owens98c56ad2016-07-28 21:38:21 -07001070 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301071 }
1072
1073 out:
Chaitra P Bc1a6c5a2018-04-24 05:28:41 -04001074 if (pcie_device)
1075 pcie_device_put(pcie_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301076
1077 /* free memory associated with sg buffers */
1078 if (data_in)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001079 dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301080 data_in_dma);
1081
1082 if (data_out)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001083 dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301084 data_out_dma);
1085
1086 kfree(mpi_request);
1087 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1088 return ret;
1089}
1090
1091/**
1092 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1093 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001094 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301095 */
1096static long
1097_ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1098{
1099 struct mpt3_ioctl_iocinfo karg;
1100
Joe Perches919d8a32018-09-17 08:01:09 -07001101 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1102 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301103
1104 memset(&karg, 0 , sizeof(karg));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301105 if (ioc->pfacts)
1106 karg.port_number = ioc->pfacts[0].PortNumber;
1107 karg.hw_rev = ioc->pdev->revision;
1108 karg.pci_id = ioc->pdev->device;
1109 karg.subsystem_device = ioc->pdev->subsystem_device;
1110 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1111 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1112 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1113 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1114 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1115 karg.firmware_version = ioc->facts.FWVersion.Word;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301116 strcpy(karg.driver_version, ioc->driver_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301117 strcat(karg.driver_version, "-");
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301118 switch (ioc->hba_mpi_version_belonged) {
1119 case MPI2_VERSION:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301120 if (ioc->is_warpdrive)
1121 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1122 else
1123 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301124 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1125 break;
1126 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301127 case MPI26_VERSION:
Suganath Prabu Subramani998f26a2016-10-26 13:34:37 +05301128 if (ioc->is_gen35_ioc)
1129 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1130 else
1131 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301132 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1133 break;
1134 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301135 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1136
1137 if (copy_to_user(arg, &karg, sizeof(karg))) {
1138 pr_err("failure at %s:%d/%s()!\n",
1139 __FILE__, __LINE__, __func__);
1140 return -EFAULT;
1141 }
1142 return 0;
1143}
1144
1145/**
1146 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1147 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001148 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301149 */
1150static long
1151_ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1152{
1153 struct mpt3_ioctl_eventquery karg;
1154
1155 if (copy_from_user(&karg, arg, sizeof(karg))) {
1156 pr_err("failure at %s:%d/%s()!\n",
1157 __FILE__, __LINE__, __func__);
1158 return -EFAULT;
1159 }
1160
Joe Perches919d8a32018-09-17 08:01:09 -07001161 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1162 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301163
1164 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1165 memcpy(karg.event_types, ioc->event_type,
1166 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1167
1168 if (copy_to_user(arg, &karg, sizeof(karg))) {
1169 pr_err("failure at %s:%d/%s()!\n",
1170 __FILE__, __LINE__, __func__);
1171 return -EFAULT;
1172 }
1173 return 0;
1174}
1175
1176/**
1177 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1178 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001179 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301180 */
1181static long
1182_ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1183{
1184 struct mpt3_ioctl_eventenable karg;
1185
1186 if (copy_from_user(&karg, arg, sizeof(karg))) {
1187 pr_err("failure at %s:%d/%s()!\n",
1188 __FILE__, __LINE__, __func__);
1189 return -EFAULT;
1190 }
1191
Joe Perches919d8a32018-09-17 08:01:09 -07001192 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1193 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301194
1195 memcpy(ioc->event_type, karg.event_types,
1196 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1197 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1198
1199 if (ioc->event_log)
1200 return 0;
1201 /* initialize event_log */
1202 ioc->event_context = 0;
1203 ioc->aen_event_read_flag = 0;
1204 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1205 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1206 if (!ioc->event_log) {
1207 pr_err("failure at %s:%d/%s()!\n",
1208 __FILE__, __LINE__, __func__);
1209 return -ENOMEM;
1210 }
1211 return 0;
1212}
1213
1214/**
1215 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1216 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001217 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301218 */
1219static long
1220_ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1221{
1222 struct mpt3_ioctl_eventreport karg;
1223 u32 number_bytes, max_events, max;
1224 struct mpt3_ioctl_eventreport __user *uarg = arg;
1225
1226 if (copy_from_user(&karg, arg, sizeof(karg))) {
1227 pr_err("failure at %s:%d/%s()!\n",
1228 __FILE__, __LINE__, __func__);
1229 return -EFAULT;
1230 }
1231
Joe Perches919d8a32018-09-17 08:01:09 -07001232 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1233 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301234
1235 number_bytes = karg.hdr.max_data_size -
1236 sizeof(struct mpt3_ioctl_header);
1237 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1238 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1239
1240 /* If fewer than 1 event is requested, there must have
1241 * been some type of error.
1242 */
1243 if (!max || !ioc->event_log)
1244 return -ENODATA;
1245
1246 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1247 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1248 pr_err("failure at %s:%d/%s()!\n",
1249 __FILE__, __LINE__, __func__);
1250 return -EFAULT;
1251 }
1252
1253 /* reset flag so SIGIO can restart */
1254 ioc->aen_event_read_flag = 0;
1255 return 0;
1256}
1257
1258/**
1259 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1260 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001261 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301262 */
1263static long
1264_ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1265{
1266 struct mpt3_ioctl_diag_reset karg;
1267 int retval;
1268
1269 if (copy_from_user(&karg, arg, sizeof(karg))) {
1270 pr_err("failure at %s:%d/%s()!\n",
1271 __FILE__, __LINE__, __func__);
1272 return -EFAULT;
1273 }
1274
1275 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1276 ioc->is_driver_loading)
1277 return -EAGAIN;
1278
Joe Perches919d8a32018-09-17 08:01:09 -07001279 dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1280 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301281
Calvin Owens98c56ad2016-07-28 21:38:21 -07001282 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Joe Perches919d8a32018-09-17 08:01:09 -07001283 ioc_info(ioc, "host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301284 return 0;
1285}
1286
1287/**
1288 * _ctl_btdh_search_sas_device - searching for sas device
1289 * @ioc: per adapter object
1290 * @btdh: btdh ioctl payload
1291 */
1292static int
1293_ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1294 struct mpt3_ioctl_btdh_mapping *btdh)
1295{
1296 struct _sas_device *sas_device;
1297 unsigned long flags;
1298 int rc = 0;
1299
1300 if (list_empty(&ioc->sas_device_list))
1301 return rc;
1302
1303 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1304 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1305 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1306 btdh->handle == sas_device->handle) {
1307 btdh->bus = sas_device->channel;
1308 btdh->id = sas_device->id;
1309 rc = 1;
1310 goto out;
1311 } else if (btdh->bus == sas_device->channel && btdh->id ==
1312 sas_device->id && btdh->handle == 0xFFFF) {
1313 btdh->handle = sas_device->handle;
1314 rc = 1;
1315 goto out;
1316 }
1317 }
1318 out:
1319 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1320 return rc;
1321}
1322
1323/**
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +05301324 * _ctl_btdh_search_pcie_device - searching for pcie device
1325 * @ioc: per adapter object
1326 * @btdh: btdh ioctl payload
1327 */
1328static int
1329_ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
1330 struct mpt3_ioctl_btdh_mapping *btdh)
1331{
1332 struct _pcie_device *pcie_device;
1333 unsigned long flags;
1334 int rc = 0;
1335
1336 if (list_empty(&ioc->pcie_device_list))
1337 return rc;
1338
1339 spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1340 list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1341 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1342 btdh->handle == pcie_device->handle) {
1343 btdh->bus = pcie_device->channel;
1344 btdh->id = pcie_device->id;
1345 rc = 1;
1346 goto out;
1347 } else if (btdh->bus == pcie_device->channel && btdh->id ==
1348 pcie_device->id && btdh->handle == 0xFFFF) {
1349 btdh->handle = pcie_device->handle;
1350 rc = 1;
1351 goto out;
1352 }
1353 }
1354 out:
1355 spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1356 return rc;
1357}
1358
1359/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301360 * _ctl_btdh_search_raid_device - searching for raid device
1361 * @ioc: per adapter object
1362 * @btdh: btdh ioctl payload
1363 */
1364static int
1365_ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1366 struct mpt3_ioctl_btdh_mapping *btdh)
1367{
1368 struct _raid_device *raid_device;
1369 unsigned long flags;
1370 int rc = 0;
1371
1372 if (list_empty(&ioc->raid_device_list))
1373 return rc;
1374
1375 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1376 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1377 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1378 btdh->handle == raid_device->handle) {
1379 btdh->bus = raid_device->channel;
1380 btdh->id = raid_device->id;
1381 rc = 1;
1382 goto out;
1383 } else if (btdh->bus == raid_device->channel && btdh->id ==
1384 raid_device->id && btdh->handle == 0xFFFF) {
1385 btdh->handle = raid_device->handle;
1386 rc = 1;
1387 goto out;
1388 }
1389 }
1390 out:
1391 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1392 return rc;
1393}
1394
1395/**
1396 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1397 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001398 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301399 */
1400static long
1401_ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1402{
1403 struct mpt3_ioctl_btdh_mapping karg;
1404 int rc;
1405
1406 if (copy_from_user(&karg, arg, sizeof(karg))) {
1407 pr_err("failure at %s:%d/%s()!\n",
1408 __FILE__, __LINE__, __func__);
1409 return -EFAULT;
1410 }
1411
Joe Perches919d8a32018-09-17 08:01:09 -07001412 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1413 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301414
1415 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1416 if (!rc)
Suganath Prabu Subramani45aa6a12017-10-31 18:02:36 +05301417 rc = _ctl_btdh_search_pcie_device(ioc, &karg);
1418 if (!rc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301419 _ctl_btdh_search_raid_device(ioc, &karg);
1420
1421 if (copy_to_user(arg, &karg, sizeof(karg))) {
1422 pr_err("failure at %s:%d/%s()!\n",
1423 __FILE__, __LINE__, __func__);
1424 return -EFAULT;
1425 }
1426 return 0;
1427}
1428
1429/**
1430 * _ctl_diag_capability - return diag buffer capability
1431 * @ioc: per adapter object
1432 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1433 *
1434 * returns 1 when diag buffer support is enabled in firmware
1435 */
1436static u8
1437_ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1438{
1439 u8 rc = 0;
1440
1441 switch (buffer_type) {
1442 case MPI2_DIAG_BUF_TYPE_TRACE:
1443 if (ioc->facts.IOCCapabilities &
1444 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1445 rc = 1;
1446 break;
1447 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1448 if (ioc->facts.IOCCapabilities &
1449 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1450 rc = 1;
1451 break;
1452 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1453 if (ioc->facts.IOCCapabilities &
1454 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1455 rc = 1;
1456 }
1457
1458 return rc;
1459}
1460
1461
1462/**
1463 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1464 * @ioc: per adapter object
1465 * @diag_register: the diag_register struct passed in from user space
1466 *
1467 */
1468static long
1469_ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1470 struct mpt3_diag_register *diag_register)
1471{
1472 int rc, i;
1473 void *request_data = NULL;
1474 dma_addr_t request_data_dma;
1475 u32 request_data_sz = 0;
1476 Mpi2DiagBufferPostRequest_t *mpi_request;
1477 Mpi2DiagBufferPostReply_t *mpi_reply;
1478 u8 buffer_type;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301479 u16 smid;
1480 u16 ioc_status;
1481 u32 ioc_state;
1482 u8 issue_reset = 0;
1483
Joe Perches919d8a32018-09-17 08:01:09 -07001484 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1485 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301486
1487 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1488 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001489 ioc_err(ioc, "%s: failed due to ioc not operational\n",
1490 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301491 rc = -EAGAIN;
1492 goto out;
1493 }
1494
1495 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001496 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301497 rc = -EAGAIN;
1498 goto out;
1499 }
1500
1501 buffer_type = diag_register->buffer_type;
1502 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001503 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1504 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301505 return -EPERM;
1506 }
1507
1508 if (ioc->diag_buffer_status[buffer_type] &
1509 MPT3_DIAG_BUFFER_IS_REGISTERED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001510 ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1511 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301512 return -EINVAL;
1513 }
1514
1515 if (diag_register->requested_buffer_size % 4) {
Joe Perches919d8a32018-09-17 08:01:09 -07001516 ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
1517 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301518 return -EINVAL;
1519 }
1520
1521 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1522 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07001523 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301524 rc = -EAGAIN;
1525 goto out;
1526 }
1527
1528 rc = 0;
1529 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1530 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1531 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1532 ioc->ctl_cmds.smid = smid;
1533
1534 request_data = ioc->diag_buffer[buffer_type];
1535 request_data_sz = diag_register->requested_buffer_size;
1536 ioc->unique_id[buffer_type] = diag_register->unique_id;
1537 ioc->diag_buffer_status[buffer_type] = 0;
1538 memcpy(ioc->product_specific[buffer_type],
1539 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1540 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1541
1542 if (request_data) {
1543 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1544 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001545 dma_free_coherent(&ioc->pdev->dev,
1546 ioc->diag_buffer_sz[buffer_type],
1547 request_data, request_data_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301548 request_data = NULL;
1549 }
1550 }
1551
1552 if (request_data == NULL) {
1553 ioc->diag_buffer_sz[buffer_type] = 0;
1554 ioc->diag_buffer_dma[buffer_type] = 0;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001555 request_data = dma_alloc_coherent(&ioc->pdev->dev,
1556 request_data_sz, &request_data_dma, GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301557 if (request_data == NULL) {
Joe Perches919d8a32018-09-17 08:01:09 -07001558 ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
1559 __func__, request_data_sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301560 mpt3sas_base_free_smid(ioc, smid);
1561 return -ENOMEM;
1562 }
1563 ioc->diag_buffer[buffer_type] = request_data;
1564 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1565 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1566 }
1567
1568 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1569 mpi_request->BufferType = diag_register->buffer_type;
1570 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1571 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1572 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1573 mpi_request->VF_ID = 0; /* TODO */
1574 mpi_request->VP_ID = 0;
1575
Joe Perches919d8a32018-09-17 08:01:09 -07001576 dctlprintk(ioc,
1577 ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1578 __func__, request_data,
1579 (unsigned long long)request_data_dma,
1580 le32_to_cpu(mpi_request->BufferLength)));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301581
1582 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1583 mpi_request->ProductSpecific[i] =
1584 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1585
1586 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001587 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07001588 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301589 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1590
1591 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -04001592 issue_reset =
1593 mpt3sas_base_check_cmd_timeout(ioc,
1594 ioc->ctl_cmds.status, mpi_request,
1595 sizeof(Mpi2DiagBufferPostRequest_t)/4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301596 goto issue_host_reset;
1597 }
1598
1599 /* process the completed Reply Message Frame */
1600 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001601 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301602 rc = -EFAULT;
1603 goto out;
1604 }
1605
1606 mpi_reply = ioc->ctl_cmds.reply;
1607 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1608
1609 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1610 ioc->diag_buffer_status[buffer_type] |=
1611 MPT3_DIAG_BUFFER_IS_REGISTERED;
Joe Perches919d8a32018-09-17 08:01:09 -07001612 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301613 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07001614 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1615 __func__,
1616 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301617 rc = -EFAULT;
1618 }
1619
1620 issue_host_reset:
1621 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07001622 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301623
1624 out:
1625
1626 if (rc && request_data)
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001627 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301628 request_data, request_data_dma);
1629
1630 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1631 return rc;
1632}
1633
1634/**
1635 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1636 * @ioc: per adapter object
1637 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1638 *
1639 * This is called when command line option diag_buffer_enable is enabled
1640 * at driver load time.
1641 */
1642void
1643mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1644{
1645 struct mpt3_diag_register diag_register;
1646
1647 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1648
1649 if (bits_to_register & 1) {
Joe Perches919d8a32018-09-17 08:01:09 -07001650 ioc_info(ioc, "registering trace buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301651 ioc->diag_trigger_master.MasterData =
1652 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1653 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1654 /* register for 2MB buffers */
1655 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1656 diag_register.unique_id = 0x7075900;
1657 _ctl_diag_register_2(ioc, &diag_register);
1658 }
1659
1660 if (bits_to_register & 2) {
Joe Perches919d8a32018-09-17 08:01:09 -07001661 ioc_info(ioc, "registering snapshot buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301662 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1663 /* register for 2MB buffers */
1664 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1665 diag_register.unique_id = 0x7075901;
1666 _ctl_diag_register_2(ioc, &diag_register);
1667 }
1668
1669 if (bits_to_register & 4) {
Joe Perches919d8a32018-09-17 08:01:09 -07001670 ioc_info(ioc, "registering extended buffer support\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301671 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1672 /* register for 2MB buffers */
1673 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1674 diag_register.unique_id = 0x7075901;
1675 _ctl_diag_register_2(ioc, &diag_register);
1676 }
1677}
1678
1679/**
1680 * _ctl_diag_register - application register with driver
1681 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001682 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301683 *
1684 * This will allow the driver to setup any required buffers that will be
1685 * needed by firmware to communicate with the driver.
1686 */
1687static long
1688_ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1689{
1690 struct mpt3_diag_register karg;
1691 long rc;
1692
1693 if (copy_from_user(&karg, arg, sizeof(karg))) {
1694 pr_err("failure at %s:%d/%s()!\n",
1695 __FILE__, __LINE__, __func__);
1696 return -EFAULT;
1697 }
1698
1699 rc = _ctl_diag_register_2(ioc, &karg);
1700 return rc;
1701}
1702
1703/**
1704 * _ctl_diag_unregister - application unregister with driver
1705 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001706 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301707 *
1708 * This will allow the driver to cleanup any memory allocated for diag
1709 * messages and to free up any resources.
1710 */
1711static long
1712_ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1713{
1714 struct mpt3_diag_unregister karg;
1715 void *request_data;
1716 dma_addr_t request_data_dma;
1717 u32 request_data_sz;
1718 u8 buffer_type;
1719
1720 if (copy_from_user(&karg, arg, sizeof(karg))) {
1721 pr_err("failure at %s:%d/%s()!\n",
1722 __FILE__, __LINE__, __func__);
1723 return -EFAULT;
1724 }
1725
Joe Perches919d8a32018-09-17 08:01:09 -07001726 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1727 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301728
1729 buffer_type = karg.unique_id & 0x000000ff;
1730 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001731 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1732 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301733 return -EPERM;
1734 }
1735
1736 if ((ioc->diag_buffer_status[buffer_type] &
1737 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001738 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1739 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301740 return -EINVAL;
1741 }
1742 if ((ioc->diag_buffer_status[buffer_type] &
1743 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001744 ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
1745 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301746 return -EINVAL;
1747 }
1748
1749 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07001750 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1751 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301752 return -EINVAL;
1753 }
1754
1755 request_data = ioc->diag_buffer[buffer_type];
1756 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07001757 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1758 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301759 return -ENOMEM;
1760 }
1761
1762 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1763 request_data_dma = ioc->diag_buffer_dma[buffer_type];
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02001764 dma_free_coherent(&ioc->pdev->dev, request_data_sz,
1765 request_data, request_data_dma);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301766 ioc->diag_buffer[buffer_type] = NULL;
1767 ioc->diag_buffer_status[buffer_type] = 0;
1768 return 0;
1769}
1770
1771/**
1772 * _ctl_diag_query - query relevant info associated with diag buffers
1773 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001774 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301775 *
1776 * The application will send only buffer_type and unique_id. Driver will
1777 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1778 * 0x00, the driver will return info specified by Buffer Type.
1779 */
1780static long
1781_ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1782{
1783 struct mpt3_diag_query karg;
1784 void *request_data;
1785 int i;
1786 u8 buffer_type;
1787
1788 if (copy_from_user(&karg, arg, sizeof(karg))) {
1789 pr_err("failure at %s:%d/%s()!\n",
1790 __FILE__, __LINE__, __func__);
1791 return -EFAULT;
1792 }
1793
Joe Perches919d8a32018-09-17 08:01:09 -07001794 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1795 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301796
1797 karg.application_flags = 0;
1798 buffer_type = karg.buffer_type;
1799
1800 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001801 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1802 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301803 return -EPERM;
1804 }
1805
1806 if ((ioc->diag_buffer_status[buffer_type] &
1807 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001808 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1809 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301810 return -EINVAL;
1811 }
1812
1813 if (karg.unique_id & 0xffffff00) {
1814 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07001815 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1816 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301817 return -EINVAL;
1818 }
1819 }
1820
1821 request_data = ioc->diag_buffer[buffer_type];
1822 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07001823 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
1824 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301825 return -ENOMEM;
1826 }
1827
1828 if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
1829 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1830 MPT3_APP_FLAGS_BUFFER_VALID);
1831 else
1832 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1833 MPT3_APP_FLAGS_BUFFER_VALID |
1834 MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
1835
1836 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1837 karg.product_specific[i] =
1838 ioc->product_specific[buffer_type][i];
1839
1840 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1841 karg.driver_added_buffer_size = 0;
1842 karg.unique_id = ioc->unique_id[buffer_type];
1843 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1844
1845 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
Joe Perches919d8a32018-09-17 08:01:09 -07001846 ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
1847 __func__, arg);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301848 return -EFAULT;
1849 }
1850 return 0;
1851}
1852
1853/**
1854 * mpt3sas_send_diag_release - Diag Release Message
1855 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07001856 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1857 * @issue_reset: specifies whether host reset is required.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301858 *
1859 */
1860int
1861mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
1862 u8 *issue_reset)
1863{
1864 Mpi2DiagReleaseRequest_t *mpi_request;
1865 Mpi2DiagReleaseReply_t *mpi_reply;
1866 u16 smid;
1867 u16 ioc_status;
1868 u32 ioc_state;
1869 int rc;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301870
Joe Perches919d8a32018-09-17 08:01:09 -07001871 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1872 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301873
1874 rc = 0;
1875 *issue_reset = 0;
1876
1877 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1878 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1879 if (ioc->diag_buffer_status[buffer_type] &
1880 MPT3_DIAG_BUFFER_IS_REGISTERED)
1881 ioc->diag_buffer_status[buffer_type] |=
1882 MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07001883 dctlprintk(ioc,
1884 ioc_info(ioc, "%s: skipping due to FAULT state\n",
1885 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301886 rc = -EAGAIN;
1887 goto out;
1888 }
1889
1890 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07001891 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301892 rc = -EAGAIN;
1893 goto out;
1894 }
1895
1896 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1897 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07001898 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301899 rc = -EAGAIN;
1900 goto out;
1901 }
1902
1903 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1904 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1905 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1906 ioc->ctl_cmds.smid = smid;
1907
1908 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1909 mpi_request->BufferType = buffer_type;
1910 mpi_request->VF_ID = 0; /* TODO */
1911 mpi_request->VP_ID = 0;
1912
1913 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04001914 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07001915 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301916 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1917
1918 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -04001919 *issue_reset = mpt3sas_base_check_cmd_timeout(ioc,
1920 ioc->ctl_cmds.status, mpi_request,
1921 sizeof(Mpi2DiagReleaseRequest_t)/4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301922 rc = -EFAULT;
1923 goto out;
1924 }
1925
1926 /* process the completed Reply Message Frame */
1927 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001928 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301929 rc = -EFAULT;
1930 goto out;
1931 }
1932
1933 mpi_reply = ioc->ctl_cmds.reply;
1934 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1935
1936 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1937 ioc->diag_buffer_status[buffer_type] |=
1938 MPT3_DIAG_BUFFER_IS_RELEASED;
Joe Perches919d8a32018-09-17 08:01:09 -07001939 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301940 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07001941 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1942 __func__,
1943 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301944 rc = -EFAULT;
1945 }
1946
1947 out:
1948 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1949 return rc;
1950}
1951
1952/**
1953 * _ctl_diag_release - request to send Diag Release Message to firmware
Bart Van Assche4beb4862018-06-15 14:42:01 -07001954 * @ioc: ?
1955 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301956 *
1957 * This allows ownership of the specified buffer to returned to the driver,
1958 * allowing an application to read the buffer without fear that firmware is
Masahiro Yamada9a284e52017-02-27 14:29:48 -08001959 * overwriting information in the buffer.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301960 */
1961static long
1962_ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1963{
1964 struct mpt3_diag_release karg;
1965 void *request_data;
1966 int rc;
1967 u8 buffer_type;
1968 u8 issue_reset = 0;
1969
1970 if (copy_from_user(&karg, arg, sizeof(karg))) {
1971 pr_err("failure at %s:%d/%s()!\n",
1972 __FILE__, __LINE__, __func__);
1973 return -EFAULT;
1974 }
1975
Joe Perches919d8a32018-09-17 08:01:09 -07001976 dctlprintk(ioc, ioc_info(ioc, "%s\n",
1977 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301978
1979 buffer_type = karg.unique_id & 0x000000ff;
1980 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07001981 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1982 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301983 return -EPERM;
1984 }
1985
1986 if ((ioc->diag_buffer_status[buffer_type] &
1987 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07001988 ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1989 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301990 return -EINVAL;
1991 }
1992
1993 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07001994 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
1995 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301996 return -EINVAL;
1997 }
1998
1999 if (ioc->diag_buffer_status[buffer_type] &
2000 MPT3_DIAG_BUFFER_IS_RELEASED) {
Joe Perches919d8a32018-09-17 08:01:09 -07002001 ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
2002 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302003 return 0;
2004 }
2005
2006 request_data = ioc->diag_buffer[buffer_type];
2007
2008 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002009 ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2010 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302011 return -ENOMEM;
2012 }
2013
2014 /* buffers were released by due to host reset */
2015 if ((ioc->diag_buffer_status[buffer_type] &
2016 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2017 ioc->diag_buffer_status[buffer_type] |=
2018 MPT3_DIAG_BUFFER_IS_RELEASED;
2019 ioc->diag_buffer_status[buffer_type] &=
2020 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
Joe Perches919d8a32018-09-17 08:01:09 -07002021 ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
2022 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302023 return 0;
2024 }
2025
2026 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2027
2028 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07002029 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302030
2031 return rc;
2032}
2033
2034/**
2035 * _ctl_diag_read_buffer - request for copy of the diag buffer
2036 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002037 * @arg: user space buffer containing ioctl content
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302038 */
2039static long
2040_ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2041{
2042 struct mpt3_diag_read_buffer karg;
2043 struct mpt3_diag_read_buffer __user *uarg = arg;
2044 void *request_data, *diag_data;
2045 Mpi2DiagBufferPostRequest_t *mpi_request;
2046 Mpi2DiagBufferPostReply_t *mpi_reply;
2047 int rc, i;
2048 u8 buffer_type;
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002049 unsigned long request_size, copy_size;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302050 u16 smid;
2051 u16 ioc_status;
2052 u8 issue_reset = 0;
2053
2054 if (copy_from_user(&karg, arg, sizeof(karg))) {
2055 pr_err("failure at %s:%d/%s()!\n",
2056 __FILE__, __LINE__, __func__);
2057 return -EFAULT;
2058 }
2059
Joe Perches919d8a32018-09-17 08:01:09 -07002060 dctlprintk(ioc, ioc_info(ioc, "%s\n",
2061 __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302062
2063 buffer_type = karg.unique_id & 0x000000ff;
2064 if (!_ctl_diag_capability(ioc, buffer_type)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002065 ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2066 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302067 return -EPERM;
2068 }
2069
2070 if (karg.unique_id != ioc->unique_id[buffer_type]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002071 ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2072 __func__, karg.unique_id);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302073 return -EINVAL;
2074 }
2075
2076 request_data = ioc->diag_buffer[buffer_type];
2077 if (!request_data) {
Joe Perches919d8a32018-09-17 08:01:09 -07002078 ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2079 __func__, buffer_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302080 return -ENOMEM;
2081 }
2082
2083 request_size = ioc->diag_buffer_sz[buffer_type];
2084
2085 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002086 ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
2087 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302088 return -EINVAL;
2089 }
2090
2091 if (karg.starting_offset > request_size)
2092 return -EINVAL;
2093
2094 diag_data = (void *)(request_data + karg.starting_offset);
Joe Perches919d8a32018-09-17 08:01:09 -07002095 dctlprintk(ioc,
2096 ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2097 __func__, diag_data, karg.starting_offset,
2098 karg.bytes_to_read));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302099
2100 /* Truncate data on requests that are too large */
2101 if ((diag_data + karg.bytes_to_read < diag_data) ||
2102 (diag_data + karg.bytes_to_read > request_data + request_size))
2103 copy_size = request_size - karg.starting_offset;
2104 else
2105 copy_size = karg.bytes_to_read;
2106
2107 if (copy_to_user((void __user *)uarg->diagnostic_data,
2108 diag_data, copy_size)) {
Joe Perches919d8a32018-09-17 08:01:09 -07002109 ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2110 __func__, diag_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302111 return -EFAULT;
2112 }
2113
2114 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2115 return 0;
2116
Joe Perches919d8a32018-09-17 08:01:09 -07002117 dctlprintk(ioc,
2118 ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
2119 __func__, buffer_type));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302120 if ((ioc->diag_buffer_status[buffer_type] &
2121 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002122 dctlprintk(ioc,
2123 ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
2124 __func__, buffer_type));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302125 return 0;
2126 }
2127 /* Get a free request frame and save the message context.
2128 */
2129
2130 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
Joe Perches919d8a32018-09-17 08:01:09 -07002131 ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302132 rc = -EAGAIN;
2133 goto out;
2134 }
2135
2136 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2137 if (!smid) {
Joe Perches919d8a32018-09-17 08:01:09 -07002138 ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302139 rc = -EAGAIN;
2140 goto out;
2141 }
2142
2143 rc = 0;
2144 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2145 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2146 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2147 ioc->ctl_cmds.smid = smid;
2148
2149 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2150 mpi_request->BufferType = buffer_type;
2151 mpi_request->BufferLength =
2152 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2153 mpi_request->BufferAddress =
2154 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2155 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2156 mpi_request->ProductSpecific[i] =
2157 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2158 mpi_request->VF_ID = 0; /* TODO */
2159 mpi_request->VP_ID = 0;
2160
2161 init_completion(&ioc->ctl_cmds.done);
Suganath Prabu S078a4cc2019-05-31 08:14:34 -04002162 ioc->put_smid_default(ioc, smid);
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002163 wait_for_completion_timeout(&ioc->ctl_cmds.done,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302164 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2165
2166 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
Chaitra P Bd37306c2018-05-31 06:34:50 -04002167 issue_reset =
2168 mpt3sas_base_check_cmd_timeout(ioc,
2169 ioc->ctl_cmds.status, mpi_request,
2170 sizeof(Mpi2DiagBufferPostRequest_t)/4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302171 goto issue_host_reset;
2172 }
2173
2174 /* process the completed Reply Message Frame */
2175 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002176 ioc_err(ioc, "%s: no reply message\n", __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302177 rc = -EFAULT;
2178 goto out;
2179 }
2180
2181 mpi_reply = ioc->ctl_cmds.reply;
2182 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2183
2184 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2185 ioc->diag_buffer_status[buffer_type] |=
2186 MPT3_DIAG_BUFFER_IS_REGISTERED;
Joe Perches919d8a32018-09-17 08:01:09 -07002187 dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302188 } else {
Joe Perches919d8a32018-09-17 08:01:09 -07002189 ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2190 __func__, ioc_status,
2191 le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302192 rc = -EFAULT;
2193 }
2194
2195 issue_host_reset:
2196 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07002197 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302198
2199 out:
2200
2201 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2202 return rc;
2203}
2204
2205
2206
2207#ifdef CONFIG_COMPAT
2208/**
2209 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2210 * @ioc: per adapter object
Bart Van Assche4beb4862018-06-15 14:42:01 -07002211 * @cmd: ioctl opcode
2212 * @arg: (struct mpt3_ioctl_command32)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302213 *
2214 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2215 */
2216static long
2217_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2218 void __user *arg)
2219{
2220 struct mpt3_ioctl_command32 karg32;
2221 struct mpt3_ioctl_command32 __user *uarg;
2222 struct mpt3_ioctl_command karg;
2223
2224 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2225 return -EINVAL;
2226
2227 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2228
2229 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2230 pr_err("failure at %s:%d/%s()!\n",
2231 __FILE__, __LINE__, __func__);
2232 return -EFAULT;
2233 }
2234
2235 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2236 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2237 karg.hdr.port_number = karg32.hdr.port_number;
2238 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2239 karg.timeout = karg32.timeout;
2240 karg.max_reply_bytes = karg32.max_reply_bytes;
2241 karg.data_in_size = karg32.data_in_size;
2242 karg.data_out_size = karg32.data_out_size;
2243 karg.max_sense_bytes = karg32.max_sense_bytes;
2244 karg.data_sge_offset = karg32.data_sge_offset;
2245 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2246 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2247 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2248 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2249 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2250}
2251#endif
2252
2253/**
2254 * _ctl_ioctl_main - main ioctl entry point
Bart Van Assche4beb4862018-06-15 14:42:01 -07002255 * @file: (struct file)
2256 * @cmd: ioctl opcode
2257 * @arg: user space data buffer
2258 * @compat: handles 32 bit applications in 64bit os
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302259 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302260 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302261 */
2262static long
2263_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302264 u8 compat, u16 mpi_version)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302265{
2266 struct MPT3SAS_ADAPTER *ioc;
2267 struct mpt3_ioctl_header ioctl_header;
2268 enum block_state state;
2269 long ret = -EINVAL;
2270
2271 /* get IOCTL header */
2272 if (copy_from_user(&ioctl_header, (char __user *)arg,
2273 sizeof(struct mpt3_ioctl_header))) {
2274 pr_err("failure at %s:%d/%s()!\n",
2275 __FILE__, __LINE__, __func__);
2276 return -EFAULT;
2277 }
2278
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302279 if (_ctl_verify_adapter(ioctl_header.ioc_number,
2280 &ioc, mpi_version) == -1 || !ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302281 return -ENODEV;
2282
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302283 /* pci_access_mutex lock acquired by ioctl path */
2284 mutex_lock(&ioc->pci_access_mutex);
2285
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302286 if (ioc->shost_recovery || ioc->pci_error_recovery ||
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302287 ioc->is_driver_loading || ioc->remove_host) {
2288 ret = -EAGAIN;
2289 goto out_unlock_pciaccess;
2290 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302291
2292 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2293 if (state == NON_BLOCKING) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302294 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2295 ret = -EAGAIN;
2296 goto out_unlock_pciaccess;
2297 }
2298 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2299 ret = -ERESTARTSYS;
2300 goto out_unlock_pciaccess;
2301 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302302
2303
2304 switch (cmd) {
2305 case MPT3IOCINFO:
2306 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2307 ret = _ctl_getiocinfo(ioc, arg);
2308 break;
2309#ifdef CONFIG_COMPAT
2310 case MPT3COMMAND32:
2311#endif
2312 case MPT3COMMAND:
2313 {
2314 struct mpt3_ioctl_command __user *uarg;
2315 struct mpt3_ioctl_command karg;
2316
2317#ifdef CONFIG_COMPAT
2318 if (compat) {
2319 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2320 break;
2321 }
2322#endif
2323 if (copy_from_user(&karg, arg, sizeof(karg))) {
2324 pr_err("failure at %s:%d/%s()!\n",
2325 __FILE__, __LINE__, __func__);
2326 ret = -EFAULT;
2327 break;
2328 }
2329
Gen Zhangf9e3ebe2019-05-30 09:10:30 +08002330 if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2331 ret = -EINVAL;
2332 break;
2333 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302334 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2335 uarg = arg;
2336 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2337 }
2338 break;
2339 }
2340 case MPT3EVENTQUERY:
2341 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2342 ret = _ctl_eventquery(ioc, arg);
2343 break;
2344 case MPT3EVENTENABLE:
2345 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2346 ret = _ctl_eventenable(ioc, arg);
2347 break;
2348 case MPT3EVENTREPORT:
2349 ret = _ctl_eventreport(ioc, arg);
2350 break;
2351 case MPT3HARDRESET:
2352 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2353 ret = _ctl_do_reset(ioc, arg);
2354 break;
2355 case MPT3BTDHMAPPING:
2356 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2357 ret = _ctl_btdh_mapping(ioc, arg);
2358 break;
2359 case MPT3DIAGREGISTER:
2360 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2361 ret = _ctl_diag_register(ioc, arg);
2362 break;
2363 case MPT3DIAGUNREGISTER:
2364 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2365 ret = _ctl_diag_unregister(ioc, arg);
2366 break;
2367 case MPT3DIAGQUERY:
2368 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2369 ret = _ctl_diag_query(ioc, arg);
2370 break;
2371 case MPT3DIAGRELEASE:
2372 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2373 ret = _ctl_diag_release(ioc, arg);
2374 break;
2375 case MPT3DIAGREADBUFFER:
2376 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2377 ret = _ctl_diag_read_buffer(ioc, arg);
2378 break;
2379 default:
Joe Perches919d8a32018-09-17 08:01:09 -07002380 dctlprintk(ioc,
2381 ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2382 cmd));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302383 break;
2384 }
2385
2386 mutex_unlock(&ioc->ctl_cmds.mutex);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302387out_unlock_pciaccess:
2388 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302389 return ret;
2390}
2391
2392/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302393 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002394 * @file: (struct file)
2395 * @cmd: ioctl opcode
2396 * @arg: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302397 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002398static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302399_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302400{
2401 long ret;
2402
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302403 /* pass MPI25_VERSION | MPI26_VERSION value,
2404 * to indicate that this ioctl cmd
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302405 * came from mpt3ctl ioctl device.
2406 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302407 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2408 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302409 return ret;
2410}
2411
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302412/**
2413 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002414 * @file: (struct file)
2415 * @cmd: ioctl opcode
2416 * @arg: ?
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302417 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002418static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302419_ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2420{
2421 long ret;
2422
2423 /* pass MPI2_VERSION value, to indicate that this ioctl cmd
2424 * came from mpt2ctl ioctl device.
2425 */
2426 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2427 return ret;
2428}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302429#ifdef CONFIG_COMPAT
2430/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302431 *_ ctl_ioctl_compat - main ioctl entry point (compat)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002432 * @file: ?
2433 * @cmd: ?
2434 * @arg: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302435 *
2436 * This routine handles 32 bit applications in 64bit os.
2437 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002438static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302439_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302440{
2441 long ret;
2442
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302443 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2444 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302445 return ret;
2446}
2447
2448/**
2449 *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002450 * @file: ?
2451 * @cmd: ?
2452 * @arg: ?
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302453 *
2454 * This routine handles 32 bit applications in 64bit os.
2455 */
Calvin Owens8bbb1cf2016-07-28 21:38:22 -07002456static long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302457_ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2458{
2459 long ret;
2460
2461 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302462 return ret;
2463}
2464#endif
2465
2466/* scsi host attributes */
2467/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002468 * version_fw_show - firmware version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002469 * @cdev: pointer to embedded class device
2470 * @attr: ?
2471 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302472 *
2473 * A sysfs 'read-only' shost attribute.
2474 */
2475static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002476version_fw_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302477 char *buf)
2478{
2479 struct Scsi_Host *shost = class_to_shost(cdev);
2480 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2481
2482 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2483 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2484 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2485 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2486 ioc->facts.FWVersion.Word & 0x000000FF);
2487}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002488static DEVICE_ATTR_RO(version_fw);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302489
2490/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002491 * version_bios_show - bios version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002492 * @cdev: pointer to embedded class device
2493 * @attr: ?
2494 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302495 *
2496 * A sysfs 'read-only' shost attribute.
2497 */
2498static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002499version_bios_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302500 char *buf)
2501{
2502 struct Scsi_Host *shost = class_to_shost(cdev);
2503 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2504
2505 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2506
2507 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2508 (version & 0xFF000000) >> 24,
2509 (version & 0x00FF0000) >> 16,
2510 (version & 0x0000FF00) >> 8,
2511 version & 0x000000FF);
2512}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002513static DEVICE_ATTR_RO(version_bios);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302514
2515/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002516 * version_mpi_show - MPI (message passing interface) version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002517 * @cdev: pointer to embedded class device
2518 * @attr: ?
2519 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302520 *
2521 * A sysfs 'read-only' shost attribute.
2522 */
2523static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002524version_mpi_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302525 char *buf)
2526{
2527 struct Scsi_Host *shost = class_to_shost(cdev);
2528 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2529
2530 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2531 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2532}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002533static DEVICE_ATTR_RO(version_mpi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302534
2535/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002536 * version_product_show - product name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002537 * @cdev: pointer to embedded class device
2538 * @attr: ?
2539 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302540 *
2541 * A sysfs 'read-only' shost attribute.
2542 */
2543static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002544version_product_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302545 char *buf)
2546{
2547 struct Scsi_Host *shost = class_to_shost(cdev);
2548 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2549
2550 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2551}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002552static DEVICE_ATTR_RO(version_product);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302553
2554/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002555 * version_nvdata_persistent_show - ndvata persistent version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002556 * @cdev: pointer to embedded class device
2557 * @attr: ?
2558 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302559 *
2560 * A sysfs 'read-only' shost attribute.
2561 */
2562static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002563version_nvdata_persistent_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302564 struct device_attribute *attr, char *buf)
2565{
2566 struct Scsi_Host *shost = class_to_shost(cdev);
2567 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2568
2569 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2570 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2571}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002572static DEVICE_ATTR_RO(version_nvdata_persistent);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302573
2574/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002575 * version_nvdata_default_show - nvdata default version
Bart Van Assche4beb4862018-06-15 14:42:01 -07002576 * @cdev: pointer to embedded class device
2577 * @attr: ?
2578 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302579 *
2580 * A sysfs 'read-only' shost attribute.
2581 */
2582static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002583version_nvdata_default_show(struct device *cdev, struct device_attribute
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302584 *attr, char *buf)
2585{
2586 struct Scsi_Host *shost = class_to_shost(cdev);
2587 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2588
2589 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2590 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2591}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002592static DEVICE_ATTR_RO(version_nvdata_default);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302593
2594/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002595 * board_name_show - board name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002596 * @cdev: pointer to embedded class device
2597 * @attr: ?
2598 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302599 *
2600 * A sysfs 'read-only' shost attribute.
2601 */
2602static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002603board_name_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302604 char *buf)
2605{
2606 struct Scsi_Host *shost = class_to_shost(cdev);
2607 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2608
2609 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2610}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002611static DEVICE_ATTR_RO(board_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302612
2613/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002614 * board_assembly_show - board assembly name
Bart Van Assche4beb4862018-06-15 14:42:01 -07002615 * @cdev: pointer to embedded class device
2616 * @attr: ?
2617 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302618 *
2619 * A sysfs 'read-only' shost attribute.
2620 */
2621static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002622board_assembly_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302623 char *buf)
2624{
2625 struct Scsi_Host *shost = class_to_shost(cdev);
2626 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2627
2628 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2629}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002630static DEVICE_ATTR_RO(board_assembly);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302631
2632/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002633 * board_tracer_show - board tracer number
Bart Van Assche4beb4862018-06-15 14:42:01 -07002634 * @cdev: pointer to embedded class device
2635 * @attr: ?
2636 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302637 *
2638 * A sysfs 'read-only' shost attribute.
2639 */
2640static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002641board_tracer_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302642 char *buf)
2643{
2644 struct Scsi_Host *shost = class_to_shost(cdev);
2645 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2646
2647 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2648}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002649static DEVICE_ATTR_RO(board_tracer);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302650
2651/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002652 * io_delay_show - io missing delay
Bart Van Assche4beb4862018-06-15 14:42:01 -07002653 * @cdev: pointer to embedded class device
2654 * @attr: ?
2655 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302656 *
2657 * This is for firmware implemention for deboucing device
2658 * removal events.
2659 *
2660 * A sysfs 'read-only' shost attribute.
2661 */
2662static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002663io_delay_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302664 char *buf)
2665{
2666 struct Scsi_Host *shost = class_to_shost(cdev);
2667 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2668
2669 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2670}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002671static DEVICE_ATTR_RO(io_delay);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302672
2673/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002674 * device_delay_show - device missing delay
Bart Van Assche4beb4862018-06-15 14:42:01 -07002675 * @cdev: pointer to embedded class device
2676 * @attr: ?
2677 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302678 *
2679 * This is for firmware implemention for deboucing device
2680 * removal events.
2681 *
2682 * A sysfs 'read-only' shost attribute.
2683 */
2684static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002685device_delay_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302686 char *buf)
2687{
2688 struct Scsi_Host *shost = class_to_shost(cdev);
2689 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2690
2691 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2692}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002693static DEVICE_ATTR_RO(device_delay);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302694
2695/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002696 * fw_queue_depth_show - global credits
Bart Van Assche4beb4862018-06-15 14:42:01 -07002697 * @cdev: pointer to embedded class device
2698 * @attr: ?
2699 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302700 *
2701 * This is firmware queue depth limit
2702 *
2703 * A sysfs 'read-only' shost attribute.
2704 */
2705static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002706fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302707 char *buf)
2708{
2709 struct Scsi_Host *shost = class_to_shost(cdev);
2710 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2711
2712 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2713}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002714static DEVICE_ATTR_RO(fw_queue_depth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302715
2716/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002717 * sas_address_show - sas address
Bart Van Assche4beb4862018-06-15 14:42:01 -07002718 * @cdev: pointer to embedded class device
2719 * @attr: ?
2720 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302721 *
2722 * This is the controller sas address
2723 *
2724 * A sysfs 'read-only' shost attribute.
2725 */
2726static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002727host_sas_address_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302728 char *buf)
2729
2730{
2731 struct Scsi_Host *shost = class_to_shost(cdev);
2732 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2733
2734 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2735 (unsigned long long)ioc->sas_hba.sas_address);
2736}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002737static DEVICE_ATTR_RO(host_sas_address);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302738
2739/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002740 * logging_level_show - logging level
Bart Van Assche4beb4862018-06-15 14:42:01 -07002741 * @cdev: pointer to embedded class device
2742 * @attr: ?
2743 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302744 *
2745 * A sysfs 'read/write' shost attribute.
2746 */
2747static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002748logging_level_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302749 char *buf)
2750{
2751 struct Scsi_Host *shost = class_to_shost(cdev);
2752 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2753
2754 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2755}
2756static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002757logging_level_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302758 const char *buf, size_t count)
2759{
2760 struct Scsi_Host *shost = class_to_shost(cdev);
2761 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2762 int val = 0;
2763
2764 if (sscanf(buf, "%x", &val) != 1)
2765 return -EINVAL;
2766
2767 ioc->logging_level = val;
Joe Perches919d8a32018-09-17 08:01:09 -07002768 ioc_info(ioc, "logging_level=%08xh\n",
2769 ioc->logging_level);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302770 return strlen(buf);
2771}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002772static DEVICE_ATTR_RW(logging_level);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302773
2774/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002775 * fwfault_debug_show - show/store fwfault_debug
Bart Van Assche4beb4862018-06-15 14:42:01 -07002776 * @cdev: pointer to embedded class device
2777 * @attr: ?
2778 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302779 *
2780 * mpt3sas_fwfault_debug is command line option
2781 * A sysfs 'read/write' shost attribute.
2782 */
2783static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002784fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302785 char *buf)
2786{
2787 struct Scsi_Host *shost = class_to_shost(cdev);
2788 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2789
2790 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2791}
2792static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002793fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302794 const char *buf, size_t count)
2795{
2796 struct Scsi_Host *shost = class_to_shost(cdev);
2797 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2798 int val = 0;
2799
2800 if (sscanf(buf, "%d", &val) != 1)
2801 return -EINVAL;
2802
2803 ioc->fwfault_debug = val;
Joe Perches919d8a32018-09-17 08:01:09 -07002804 ioc_info(ioc, "fwfault_debug=%d\n",
2805 ioc->fwfault_debug);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302806 return strlen(buf);
2807}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002808static DEVICE_ATTR_RW(fwfault_debug);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302809
2810/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002811 * ioc_reset_count_show - ioc reset count
Bart Van Assche4beb4862018-06-15 14:42:01 -07002812 * @cdev: pointer to embedded class device
2813 * @attr: ?
2814 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302815 *
2816 * This is firmware queue depth limit
2817 *
2818 * A sysfs 'read-only' shost attribute.
2819 */
2820static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002821ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302822 char *buf)
2823{
2824 struct Scsi_Host *shost = class_to_shost(cdev);
2825 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2826
2827 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
2828}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002829static DEVICE_ATTR_RO(ioc_reset_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302830
2831/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002832 * reply_queue_count_show - number of reply queues
Bart Van Assche4beb4862018-06-15 14:42:01 -07002833 * @cdev: pointer to embedded class device
2834 * @attr: ?
2835 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302836 *
2837 * This is number of reply queues
2838 *
2839 * A sysfs 'read-only' shost attribute.
2840 */
2841static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002842reply_queue_count_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302843 struct device_attribute *attr, char *buf)
2844{
2845 u8 reply_queue_count;
2846 struct Scsi_Host *shost = class_to_shost(cdev);
2847 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2848
2849 if ((ioc->facts.IOCCapabilities &
2850 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2851 reply_queue_count = ioc->reply_queue_count;
2852 else
2853 reply_queue_count = 1;
2854
2855 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2856}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002857static DEVICE_ATTR_RO(reply_queue_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302858
Sreekanth Reddy42263092015-11-11 17:30:29 +05302859/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002860 * BRM_status_show - Backup Rail Monitor Status
Bart Van Assche4beb4862018-06-15 14:42:01 -07002861 * @cdev: pointer to embedded class device
2862 * @attr: ?
2863 * @buf: the buffer returned
Sreekanth Reddy42263092015-11-11 17:30:29 +05302864 *
2865 * This is number of reply queues
2866 *
2867 * A sysfs 'read-only' shost attribute.
2868 */
2869static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002870BRM_status_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddy42263092015-11-11 17:30:29 +05302871 char *buf)
2872{
2873 struct Scsi_Host *shost = class_to_shost(cdev);
2874 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2875 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2876 Mpi2ConfigReply_t mpi_reply;
2877 u16 backup_rail_monitor_status = 0;
2878 u16 ioc_status;
2879 int sz;
2880 ssize_t rc = 0;
2881
2882 if (!ioc->is_warpdrive) {
Joe Perches919d8a32018-09-17 08:01:09 -07002883 ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
2884 __func__);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302885 goto out;
2886 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302887 /* pci_access_mutex lock acquired by sysfs show path */
2888 mutex_lock(&ioc->pci_access_mutex);
2889 if (ioc->pci_error_recovery || ioc->remove_host) {
2890 mutex_unlock(&ioc->pci_access_mutex);
2891 return 0;
2892 }
Sreekanth Reddy42263092015-11-11 17:30:29 +05302893
2894 /* allocate upto GPIOVal 36 entries */
2895 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2896 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2897 if (!io_unit_pg3) {
Joe Perches919d8a32018-09-17 08:01:09 -07002898 ioc_err(ioc, "%s: failed allocating memory for iounit_pg3: (%d) bytes\n",
2899 __func__, sz);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302900 goto out;
2901 }
2902
2903 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2904 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002905 ioc_err(ioc, "%s: failed reading iounit_pg3\n",
2906 __func__);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302907 goto out;
2908 }
2909
2910 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2911 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
Joe Perches919d8a32018-09-17 08:01:09 -07002912 ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
2913 __func__, ioc_status);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302914 goto out;
2915 }
2916
2917 if (io_unit_pg3->GPIOCount < 25) {
Joe Perches919d8a32018-09-17 08:01:09 -07002918 ioc_err(ioc, "%s: iounit_pg3->GPIOCount less than 25 entries, detected (%d) entries\n",
2919 __func__, io_unit_pg3->GPIOCount);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302920 goto out;
2921 }
2922
2923 /* BRM status is in bit zero of GPIOVal[24] */
2924 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2925 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2926
2927 out:
2928 kfree(io_unit_pg3);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302929 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302930 return rc;
2931}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002932static DEVICE_ATTR_RO(BRM_status);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302933
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302934struct DIAG_BUFFER_START {
2935 __le32 Size;
2936 __le32 DiagVersion;
2937 u8 BufferType;
2938 u8 Reserved[3];
2939 __le32 Reserved1;
2940 __le32 Reserved2;
2941 __le32 Reserved3;
2942};
2943
2944/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002945 * host_trace_buffer_size_show - host buffer size (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002946 * @cdev: pointer to embedded class device
2947 * @attr: ?
2948 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302949 *
2950 * A sysfs 'read-only' shost attribute.
2951 */
2952static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02002953host_trace_buffer_size_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302954 struct device_attribute *attr, char *buf)
2955{
2956 struct Scsi_Host *shost = class_to_shost(cdev);
2957 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2958 u32 size = 0;
2959 struct DIAG_BUFFER_START *request_data;
2960
2961 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
Joe Perches919d8a32018-09-17 08:01:09 -07002962 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
2963 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302964 return 0;
2965 }
2966
2967 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2968 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07002969 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
2970 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302971 return 0;
2972 }
2973
2974 request_data = (struct DIAG_BUFFER_START *)
2975 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2976 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2977 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
2978 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
2979 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2980 size = le32_to_cpu(request_data->Size);
2981
2982 ioc->ring_buffer_sz = size;
2983 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2984}
Tomas Henzlc9df1442019-06-14 16:41:44 +02002985static DEVICE_ATTR_RO(host_trace_buffer_size);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302986
2987/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02002988 * host_trace_buffer_show - firmware ring buffer (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07002989 * @cdev: pointer to embedded class device
2990 * @attr: ?
2991 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302992 *
2993 * A sysfs 'read/write' shost attribute.
2994 *
2995 * You will only be able to read 4k bytes of ring buffer at a time.
2996 * In order to read beyond 4k bytes, you will have to write out the
2997 * offset to the same attribute, it will move the pointer.
2998 */
2999static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003000host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303001 char *buf)
3002{
3003 struct Scsi_Host *shost = class_to_shost(cdev);
3004 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3005 void *request_data;
3006 u32 size;
3007
3008 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
Joe Perches919d8a32018-09-17 08:01:09 -07003009 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3010 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303011 return 0;
3012 }
3013
3014 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3015 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
Joe Perches919d8a32018-09-17 08:01:09 -07003016 ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3017 __func__);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303018 return 0;
3019 }
3020
3021 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3022 return 0;
3023
3024 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3025 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3026 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3027 memcpy(buf, request_data, size);
3028 return size;
3029}
3030
3031static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003032host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303033 const char *buf, size_t count)
3034{
3035 struct Scsi_Host *shost = class_to_shost(cdev);
3036 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3037 int val = 0;
3038
3039 if (sscanf(buf, "%d", &val) != 1)
3040 return -EINVAL;
3041
3042 ioc->ring_buffer_offset = val;
3043 return strlen(buf);
3044}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003045static DEVICE_ATTR_RW(host_trace_buffer);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303046
3047
3048/*****************************************/
3049
3050/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003051 * host_trace_buffer_enable_show - firmware ring buffer (trace only)
Bart Van Assche4beb4862018-06-15 14:42:01 -07003052 * @cdev: pointer to embedded class device
3053 * @attr: ?
3054 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303055 *
3056 * A sysfs 'read/write' shost attribute.
3057 *
3058 * This is a mechnism to post/release host_trace_buffers
3059 */
3060static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003061host_trace_buffer_enable_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303062 struct device_attribute *attr, char *buf)
3063{
3064 struct Scsi_Host *shost = class_to_shost(cdev);
3065 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3066
3067 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3068 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3069 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3070 return snprintf(buf, PAGE_SIZE, "off\n");
3071 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3072 MPT3_DIAG_BUFFER_IS_RELEASED))
3073 return snprintf(buf, PAGE_SIZE, "release\n");
3074 else
3075 return snprintf(buf, PAGE_SIZE, "post\n");
3076}
3077
3078static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003079host_trace_buffer_enable_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303080 struct device_attribute *attr, const char *buf, size_t count)
3081{
3082 struct Scsi_Host *shost = class_to_shost(cdev);
3083 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3084 char str[10] = "";
3085 struct mpt3_diag_register diag_register;
3086 u8 issue_reset = 0;
3087
3088 /* don't allow post/release occurr while recovery is active */
3089 if (ioc->shost_recovery || ioc->remove_host ||
3090 ioc->pci_error_recovery || ioc->is_driver_loading)
3091 return -EBUSY;
3092
3093 if (sscanf(buf, "%9s", str) != 1)
3094 return -EINVAL;
3095
3096 if (!strcmp(str, "post")) {
3097 /* exit out if host buffers are already posted */
3098 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3099 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3100 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3101 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3102 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3103 goto out;
3104 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
Joe Perches919d8a32018-09-17 08:01:09 -07003105 ioc_info(ioc, "posting host trace buffers\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303106 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3107 diag_register.requested_buffer_size = (1024 * 1024);
3108 diag_register.unique_id = 0x7075900;
3109 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3110 _ctl_diag_register_2(ioc, &diag_register);
3111 } else if (!strcmp(str, "release")) {
3112 /* exit out if host buffers are already released */
3113 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3114 goto out;
3115 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3116 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3117 goto out;
3118 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3119 MPT3_DIAG_BUFFER_IS_RELEASED))
3120 goto out;
Joe Perches919d8a32018-09-17 08:01:09 -07003121 ioc_info(ioc, "releasing host trace buffer\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303122 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3123 &issue_reset);
3124 }
3125
3126 out:
3127 return strlen(buf);
3128}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003129static DEVICE_ATTR_RW(host_trace_buffer_enable);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303130
3131/*********** diagnostic trigger suppport *********************************/
3132
3133/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003134 * diag_trigger_master_show - show the diag_trigger_master attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003135 * @cdev: pointer to embedded class device
3136 * @attr: ?
3137 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303138 *
3139 * A sysfs 'read/write' shost attribute.
3140 */
3141static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003142diag_trigger_master_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303143 struct device_attribute *attr, char *buf)
3144
3145{
3146 struct Scsi_Host *shost = class_to_shost(cdev);
3147 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3148 unsigned long flags;
3149 ssize_t rc;
3150
3151 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3152 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3153 memcpy(buf, &ioc->diag_trigger_master, rc);
3154 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3155 return rc;
3156}
3157
3158/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003159 * diag_trigger_master_store - store the diag_trigger_master attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003160 * @cdev: pointer to embedded class device
3161 * @attr: ?
3162 * @buf: the buffer returned
3163 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303164 *
3165 * A sysfs 'read/write' shost attribute.
3166 */
3167static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003168diag_trigger_master_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303169 struct device_attribute *attr, const char *buf, size_t count)
3170
3171{
3172 struct Scsi_Host *shost = class_to_shost(cdev);
3173 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3174 unsigned long flags;
3175 ssize_t rc;
3176
3177 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3178 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3179 memset(&ioc->diag_trigger_master, 0,
3180 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3181 memcpy(&ioc->diag_trigger_master, buf, rc);
3182 ioc->diag_trigger_master.MasterData |=
3183 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3184 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3185 return rc;
3186}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003187static DEVICE_ATTR_RW(diag_trigger_master);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303188
3189
3190/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003191 * diag_trigger_event_show - show the diag_trigger_event attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003192 * @cdev: pointer to embedded class device
3193 * @attr: ?
3194 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303195 *
3196 * A sysfs 'read/write' shost attribute.
3197 */
3198static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003199diag_trigger_event_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303200 struct device_attribute *attr, char *buf)
3201{
3202 struct Scsi_Host *shost = class_to_shost(cdev);
3203 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3204 unsigned long flags;
3205 ssize_t rc;
3206
3207 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3208 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3209 memcpy(buf, &ioc->diag_trigger_event, rc);
3210 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3211 return rc;
3212}
3213
3214/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003215 * diag_trigger_event_store - store the diag_trigger_event attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003216 * @cdev: pointer to embedded class device
3217 * @attr: ?
3218 * @buf: the buffer returned
3219 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303220 *
3221 * A sysfs 'read/write' shost attribute.
3222 */
3223static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003224diag_trigger_event_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303225 struct device_attribute *attr, const char *buf, size_t count)
3226
3227{
3228 struct Scsi_Host *shost = class_to_shost(cdev);
3229 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3230 unsigned long flags;
3231 ssize_t sz;
3232
3233 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3234 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3235 memset(&ioc->diag_trigger_event, 0,
3236 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3237 memcpy(&ioc->diag_trigger_event, buf, sz);
3238 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3239 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3240 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3241 return sz;
3242}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003243static DEVICE_ATTR_RW(diag_trigger_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303244
3245
3246/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003247 * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003248 * @cdev: pointer to embedded class device
3249 * @attr: ?
3250 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303251 *
3252 * A sysfs 'read/write' shost attribute.
3253 */
3254static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003255diag_trigger_scsi_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303256 struct device_attribute *attr, char *buf)
3257{
3258 struct Scsi_Host *shost = class_to_shost(cdev);
3259 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3260 unsigned long flags;
3261 ssize_t rc;
3262
3263 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3264 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3265 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3266 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3267 return rc;
3268}
3269
3270/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003271 * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003272 * @cdev: pointer to embedded class device
3273 * @attr: ?
3274 * @buf: the buffer returned
3275 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303276 *
3277 * A sysfs 'read/write' shost attribute.
3278 */
3279static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003280diag_trigger_scsi_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303281 struct device_attribute *attr, const char *buf, size_t count)
3282{
3283 struct Scsi_Host *shost = class_to_shost(cdev);
3284 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3285 unsigned long flags;
3286 ssize_t sz;
3287
3288 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
Dan Carpenter1de540a2019-07-26 09:52:05 +03003289 sz = min(sizeof(ioc->diag_trigger_scsi), count);
3290 memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303291 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3292 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3293 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3294 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3295 return sz;
3296}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003297static DEVICE_ATTR_RW(diag_trigger_scsi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303298
3299
3300/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003301 * diag_trigger_scsi_show - show the diag_trigger_mpi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003302 * @cdev: pointer to embedded class device
3303 * @attr: ?
3304 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303305 *
3306 * A sysfs 'read/write' shost attribute.
3307 */
3308static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003309diag_trigger_mpi_show(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303310 struct device_attribute *attr, char *buf)
3311{
3312 struct Scsi_Host *shost = class_to_shost(cdev);
3313 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3314 unsigned long flags;
3315 ssize_t rc;
3316
3317 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3318 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3319 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3320 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3321 return rc;
3322}
3323
3324/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003325 * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
Bart Van Assche4beb4862018-06-15 14:42:01 -07003326 * @cdev: pointer to embedded class device
3327 * @attr: ?
3328 * @buf: the buffer returned
3329 * @count: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303330 *
3331 * A sysfs 'read/write' shost attribute.
3332 */
3333static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003334diag_trigger_mpi_store(struct device *cdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303335 struct device_attribute *attr, const char *buf, size_t count)
3336{
3337 struct Scsi_Host *shost = class_to_shost(cdev);
3338 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3339 unsigned long flags;
3340 ssize_t sz;
3341
3342 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3343 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3344 memset(&ioc->diag_trigger_mpi, 0,
Dan Carpenter66331e82012-12-07 13:56:22 +03003345 sizeof(ioc->diag_trigger_mpi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303346 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3347 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3348 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3349 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3350 return sz;
3351}
3352
Tomas Henzlc9df1442019-06-14 16:41:44 +02003353static DEVICE_ATTR_RW(diag_trigger_mpi);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303354
3355/*********** diagnostic trigger suppport *** END ****************************/
3356
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303357/*****************************************/
3358
3359struct device_attribute *mpt3sas_host_attrs[] = {
3360 &dev_attr_version_fw,
3361 &dev_attr_version_bios,
3362 &dev_attr_version_mpi,
3363 &dev_attr_version_product,
3364 &dev_attr_version_nvdata_persistent,
3365 &dev_attr_version_nvdata_default,
3366 &dev_attr_board_name,
3367 &dev_attr_board_assembly,
3368 &dev_attr_board_tracer,
3369 &dev_attr_io_delay,
3370 &dev_attr_device_delay,
3371 &dev_attr_logging_level,
3372 &dev_attr_fwfault_debug,
3373 &dev_attr_fw_queue_depth,
3374 &dev_attr_host_sas_address,
3375 &dev_attr_ioc_reset_count,
3376 &dev_attr_host_trace_buffer_size,
3377 &dev_attr_host_trace_buffer,
3378 &dev_attr_host_trace_buffer_enable,
3379 &dev_attr_reply_queue_count,
3380 &dev_attr_diag_trigger_master,
3381 &dev_attr_diag_trigger_event,
3382 &dev_attr_diag_trigger_scsi,
3383 &dev_attr_diag_trigger_mpi,
Sreekanth Reddy42263092015-11-11 17:30:29 +05303384 &dev_attr_BRM_status,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303385 NULL,
3386};
3387
3388/* device attributes */
3389
3390/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003391 * sas_address_show - sas address
Bart Van Assche4beb4862018-06-15 14:42:01 -07003392 * @dev: pointer to embedded class device
3393 * @attr: ?
3394 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303395 *
3396 * This is the sas address for the target
3397 *
3398 * A sysfs 'read-only' shost attribute.
3399 */
3400static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003401sas_address_show(struct device *dev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303402 char *buf)
3403{
3404 struct scsi_device *sdev = to_scsi_device(dev);
3405 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3406
3407 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3408 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3409}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003410static DEVICE_ATTR_RO(sas_address);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303411
3412/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003413 * sas_device_handle_show - device handle
Bart Van Assche4beb4862018-06-15 14:42:01 -07003414 * @dev: pointer to embedded class device
3415 * @attr: ?
3416 * @buf: the buffer returned
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303417 *
3418 * This is the firmware assigned device handle
3419 *
3420 * A sysfs 'read-only' shost attribute.
3421 */
3422static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003423sas_device_handle_show(struct device *dev, struct device_attribute *attr,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303424 char *buf)
3425{
3426 struct scsi_device *sdev = to_scsi_device(dev);
3427 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3428
3429 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3430 sas_device_priv_data->sas_target->handle);
3431}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003432static DEVICE_ATTR_RO(sas_device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303433
Adam Manzanares307d9072016-12-12 16:31:40 -08003434/**
Tomas Henzlc9df1442019-06-14 16:41:44 +02003435 * sas_ncq_io_prio_show - send prioritized io commands to device
Bart Van Assche4beb4862018-06-15 14:42:01 -07003436 * @dev: pointer to embedded device
3437 * @attr: ?
3438 * @buf: the buffer returned
Adam Manzanares307d9072016-12-12 16:31:40 -08003439 *
3440 * A sysfs 'read/write' sdev attribute, only works with SATA
3441 */
3442static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003443sas_ncq_prio_enable_show(struct device *dev,
Adam Manzanares307d9072016-12-12 16:31:40 -08003444 struct device_attribute *attr, char *buf)
3445{
3446 struct scsi_device *sdev = to_scsi_device(dev);
3447 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3448
3449 return snprintf(buf, PAGE_SIZE, "%d\n",
3450 sas_device_priv_data->ncq_prio_enable);
3451}
3452
3453static ssize_t
Tomas Henzlc9df1442019-06-14 16:41:44 +02003454sas_ncq_prio_enable_store(struct device *dev,
Adam Manzanares307d9072016-12-12 16:31:40 -08003455 struct device_attribute *attr,
3456 const char *buf, size_t count)
3457{
3458 struct scsi_device *sdev = to_scsi_device(dev);
3459 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3460 bool ncq_prio_enable = 0;
3461
3462 if (kstrtobool(buf, &ncq_prio_enable))
3463 return -EINVAL;
3464
3465 if (!scsih_ncq_prio_supp(sdev))
3466 return -EINVAL;
3467
3468 sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
3469 return strlen(buf);
3470}
Tomas Henzlc9df1442019-06-14 16:41:44 +02003471static DEVICE_ATTR_RW(sas_ncq_prio_enable);
Adam Manzanares307d9072016-12-12 16:31:40 -08003472
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303473struct device_attribute *mpt3sas_dev_attrs[] = {
3474 &dev_attr_sas_address,
3475 &dev_attr_sas_device_handle,
Adam Manzanares307d9072016-12-12 16:31:40 -08003476 &dev_attr_sas_ncq_prio_enable,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303477 NULL,
3478};
3479
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303480/* file operations table for mpt3ctl device */
3481static const struct file_operations ctl_fops = {
3482 .owner = THIS_MODULE,
3483 .unlocked_ioctl = _ctl_ioctl,
3484 .poll = _ctl_poll,
3485 .fasync = _ctl_fasync,
3486#ifdef CONFIG_COMPAT
3487 .compat_ioctl = _ctl_ioctl_compat,
3488#endif
3489};
3490
3491/* file operations table for mpt2ctl device */
3492static const struct file_operations ctl_gen2_fops = {
3493 .owner = THIS_MODULE,
3494 .unlocked_ioctl = _ctl_mpt2_ioctl,
3495 .poll = _ctl_poll,
3496 .fasync = _ctl_fasync,
3497#ifdef CONFIG_COMPAT
3498 .compat_ioctl = _ctl_mpt2_ioctl_compat,
3499#endif
3500};
3501
3502static struct miscdevice ctl_dev = {
3503 .minor = MPT3SAS_MINOR,
3504 .name = MPT3SAS_DEV_NAME,
3505 .fops = &ctl_fops,
3506};
3507
3508static struct miscdevice gen2_ctl_dev = {
3509 .minor = MPT2SAS_MINOR,
3510 .name = MPT2SAS_DEV_NAME,
3511 .fops = &ctl_gen2_fops,
3512};
3513
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303514/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303515 * mpt3sas_ctl_init - main entry point for ctl.
Bart Van Assche4beb4862018-06-15 14:42:01 -07003516 * @hbas_to_enumerate: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303517 */
3518void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303519mpt3sas_ctl_init(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303520{
3521 async_queue = NULL;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303522
3523 /* Don't register mpt3ctl ioctl device if
3524 * hbas_to_enumarate is one.
3525 */
3526 if (hbas_to_enumerate != 1)
3527 if (misc_register(&ctl_dev) < 0)
3528 pr_err("%s can't register misc device [minor=%d]\n",
3529 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3530
3531 /* Don't register mpt3ctl ioctl device if
3532 * hbas_to_enumarate is two.
3533 */
3534 if (hbas_to_enumerate != 2)
3535 if (misc_register(&gen2_ctl_dev) < 0)
3536 pr_err("%s can't register misc device [minor=%d]\n",
3537 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3538
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303539 init_waitqueue_head(&ctl_poll_wait);
3540}
3541
3542/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303543 * mpt3sas_ctl_exit - exit point for ctl
Bart Van Assche4beb4862018-06-15 14:42:01 -07003544 * @hbas_to_enumerate: ?
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303545 */
3546void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303547mpt3sas_ctl_exit(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303548{
3549 struct MPT3SAS_ADAPTER *ioc;
3550 int i;
3551
3552 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3553
3554 /* free memory associated to diag buffers */
3555 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3556 if (!ioc->diag_buffer[i])
3557 continue;
3558 if (!(ioc->diag_buffer_status[i] &
3559 MPT3_DIAG_BUFFER_IS_REGISTERED))
3560 continue;
3561 if ((ioc->diag_buffer_status[i] &
3562 MPT3_DIAG_BUFFER_IS_RELEASED))
3563 continue;
Christoph Hellwig1c2048b2018-10-11 09:35:25 +02003564 dma_free_coherent(&ioc->pdev->dev,
3565 ioc->diag_buffer_sz[i],
3566 ioc->diag_buffer[i],
3567 ioc->diag_buffer_dma[i]);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303568 ioc->diag_buffer[i] = NULL;
3569 ioc->diag_buffer_status[i] = 0;
3570 }
3571
3572 kfree(ioc->event_log);
3573 }
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303574 if (hbas_to_enumerate != 1)
3575 misc_deregister(&ctl_dev);
3576 if (hbas_to_enumerate != 2)
3577 misc_deregister(&gen2_ctl_dev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303578}