xref: /openbmc/linux/drivers/scsi/mpt3sas/mpt3sas_ctl.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
1 /*
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
6  * Copyright (C) 2012-2014  LSI Corporation
7  * Copyright (C) 2013-2014 Avago Technologies
8  *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
9  *
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 
46 #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 
64 static struct fasync_struct *async_queue;
65 static 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  */
76 enum block_state {
77 	NON_BLOCKING,
78 	BLOCKING,
79 };
80 
81 /**
82  * _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  */
92 static void
_ctl_display_some_debug(struct MPT3SAS_ADAPTER * ioc,u16 smid,char * calling_function_name,MPI2DefaultReply_t * mpi_reply)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 	case MPI2_FUNCTION_TOOLBOX:
184 		desc = "toolbox";
185 		break;
186 	case MPI2_FUNCTION_NVME_ENCAPSULATED:
187 		desc = "nvme_encapsulated";
188 		break;
189 	}
190 
191 	if (!desc)
192 		return;
193 
194 	ioc_info(ioc, "%s: %s, smid(%d)\n", calling_function_name, desc, smid);
195 
196 	if (!mpi_reply)
197 		return;
198 
199 	if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
200 		ioc_info(ioc, "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
201 			 le16_to_cpu(mpi_reply->IOCStatus),
202 			 le32_to_cpu(mpi_reply->IOCLogInfo));
203 
204 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
205 	    mpi_request->Function ==
206 	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
207 		Mpi2SCSIIOReply_t *scsi_reply =
208 		    (Mpi2SCSIIOReply_t *)mpi_reply;
209 		struct _sas_device *sas_device = NULL;
210 		struct _pcie_device *pcie_device = NULL;
211 
212 		sas_device = mpt3sas_get_sdev_by_handle(ioc,
213 		    le16_to_cpu(scsi_reply->DevHandle));
214 		if (sas_device) {
215 			ioc_warn(ioc, "\tsas_address(0x%016llx), phy(%d)\n",
216 				 (u64)sas_device->sas_address,
217 				 sas_device->phy);
218 			ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
219 				 (u64)sas_device->enclosure_logical_id,
220 				 sas_device->slot);
221 			sas_device_put(sas_device);
222 		}
223 		if (!sas_device) {
224 			pcie_device = mpt3sas_get_pdev_by_handle(ioc,
225 				le16_to_cpu(scsi_reply->DevHandle));
226 			if (pcie_device) {
227 				ioc_warn(ioc, "\tWWID(0x%016llx), port(%d)\n",
228 					 (unsigned long long)pcie_device->wwid,
229 					 pcie_device->port_num);
230 				if (pcie_device->enclosure_handle != 0)
231 					ioc_warn(ioc, "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
232 						 (u64)pcie_device->enclosure_logical_id,
233 						 pcie_device->slot);
234 				pcie_device_put(pcie_device);
235 			}
236 		}
237 		if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
238 			ioc_info(ioc, "\tscsi_state(0x%02x), scsi_status(0x%02x)\n",
239 				 scsi_reply->SCSIState,
240 				 scsi_reply->SCSIStatus);
241 	}
242 }
243 
244 /**
245  * mpt3sas_ctl_done - ctl module completion routine
246  * @ioc: per adapter object
247  * @smid: system request message index
248  * @msix_index: MSIX table index supplied by the OS
249  * @reply: reply message frame(lower 32bit addr)
250  * Context: none.
251  *
252  * The callback handler when using ioc->ctl_cb_idx.
253  *
254  * Return: 1 meaning mf should be freed from _base_interrupt
255  *         0 means the mf is freed from this function.
256  */
257 u8
mpt3sas_ctl_done(struct MPT3SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)258 mpt3sas_ctl_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
259 	u32 reply)
260 {
261 	MPI2DefaultReply_t *mpi_reply;
262 	Mpi2SCSIIOReply_t *scsiio_reply;
263 	Mpi26NVMeEncapsulatedErrorReply_t *nvme_error_reply;
264 	const void *sense_data;
265 	u32 sz;
266 
267 	if (ioc->ctl_cmds.status == MPT3_CMD_NOT_USED)
268 		return 1;
269 	if (ioc->ctl_cmds.smid != smid)
270 		return 1;
271 	ioc->ctl_cmds.status |= MPT3_CMD_COMPLETE;
272 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
273 	if (mpi_reply) {
274 		memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
275 		ioc->ctl_cmds.status |= MPT3_CMD_REPLY_VALID;
276 		/* get sense data */
277 		if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
278 		    mpi_reply->Function ==
279 		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
280 			scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
281 			if (scsiio_reply->SCSIState &
282 			    MPI2_SCSI_STATE_AUTOSENSE_VALID) {
283 				sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
284 				    le32_to_cpu(scsiio_reply->SenseCount));
285 				sense_data = mpt3sas_base_get_sense_buffer(ioc,
286 				    smid);
287 				memcpy(ioc->ctl_cmds.sense, sense_data, sz);
288 			}
289 		}
290 		/*
291 		 * Get Error Response data for NVMe device. The ctl_cmds.sense
292 		 * buffer is used to store the Error Response data.
293 		 */
294 		if (mpi_reply->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
295 			nvme_error_reply =
296 			    (Mpi26NVMeEncapsulatedErrorReply_t *)mpi_reply;
297 			sz = min_t(u32, NVME_ERROR_RESPONSE_SIZE,
298 			    le16_to_cpu(nvme_error_reply->ErrorResponseCount));
299 			sense_data = mpt3sas_base_get_sense_buffer(ioc, smid);
300 			memcpy(ioc->ctl_cmds.sense, sense_data, sz);
301 		}
302 	}
303 
304 	_ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
305 	ioc->ctl_cmds.status &= ~MPT3_CMD_PENDING;
306 	complete(&ioc->ctl_cmds.done);
307 	return 1;
308 }
309 
310 /**
311  * _ctl_check_event_type - determines when an event needs logging
312  * @ioc: per adapter object
313  * @event: firmware event
314  *
315  * The bitmask in ioc->event_type[] indicates which events should be
316  * be saved in the driver event_log.  This bitmask is set by application.
317  *
318  * Return: 1 when event should be captured, or zero means no match.
319  */
320 static int
_ctl_check_event_type(struct MPT3SAS_ADAPTER * ioc,u16 event)321 _ctl_check_event_type(struct MPT3SAS_ADAPTER *ioc, u16 event)
322 {
323 	u16 i;
324 	u32 desired_event;
325 
326 	if (event >= 128 || !event || !ioc->event_log)
327 		return 0;
328 
329 	desired_event = (1 << (event % 32));
330 	if (!desired_event)
331 		desired_event = 1;
332 	i = event / 32;
333 	return desired_event & ioc->event_type[i];
334 }
335 
336 /**
337  * mpt3sas_ctl_add_to_event_log - add event
338  * @ioc: per adapter object
339  * @mpi_reply: reply message frame
340  */
341 void
mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER * ioc,Mpi2EventNotificationReply_t * mpi_reply)342 mpt3sas_ctl_add_to_event_log(struct MPT3SAS_ADAPTER *ioc,
343 	Mpi2EventNotificationReply_t *mpi_reply)
344 {
345 	struct MPT3_IOCTL_EVENTS *event_log;
346 	u16 event;
347 	int i;
348 	u32 sz, event_data_sz;
349 	u8 send_aen = 0;
350 
351 	if (!ioc->event_log)
352 		return;
353 
354 	event = le16_to_cpu(mpi_reply->Event);
355 
356 	if (_ctl_check_event_type(ioc, event)) {
357 
358 		/* insert entry into circular event_log */
359 		i = ioc->event_context % MPT3SAS_CTL_EVENT_LOG_SIZE;
360 		event_log = ioc->event_log;
361 		event_log[i].event = event;
362 		event_log[i].context = ioc->event_context++;
363 
364 		event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
365 		sz = min_t(u32, event_data_sz, MPT3_EVENT_DATA_SIZE);
366 		memset(event_log[i].data, 0, MPT3_EVENT_DATA_SIZE);
367 		memcpy(event_log[i].data, mpi_reply->EventData, sz);
368 		send_aen = 1;
369 	}
370 
371 	/* This aen_event_read_flag flag is set until the
372 	 * application has read the event log.
373 	 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
374 	 */
375 	if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
376 	    (send_aen && !ioc->aen_event_read_flag)) {
377 		ioc->aen_event_read_flag = 1;
378 		wake_up_interruptible(&ctl_poll_wait);
379 		if (async_queue)
380 			kill_fasync(&async_queue, SIGIO, POLL_IN);
381 	}
382 }
383 
384 /**
385  * mpt3sas_ctl_event_callback - firmware event handler (called at ISR time)
386  * @ioc: per adapter object
387  * @msix_index: MSIX table index supplied by the OS
388  * @reply: reply message frame(lower 32bit addr)
389  * Context: interrupt.
390  *
391  * This function merely adds a new work task into ioc->firmware_event_thread.
392  * The tasks are worked from _firmware_event_work in user context.
393  *
394  * Return: 1 meaning mf should be freed from _base_interrupt
395  *         0 means the mf is freed from this function.
396  */
397 u8
mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER * ioc,u8 msix_index,u32 reply)398 mpt3sas_ctl_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
399 	u32 reply)
400 {
401 	Mpi2EventNotificationReply_t *mpi_reply;
402 
403 	mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
404 	if (mpi_reply)
405 		mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
406 	return 1;
407 }
408 
409 /**
410  * _ctl_verify_adapter - validates ioc_number passed from application
411  * @ioc_number: ?
412  * @iocpp: The ioc pointer is returned in this.
413  * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
414  * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
415  *
416  * Return: (-1) means error, else ioc_number.
417  */
418 static int
_ctl_verify_adapter(int ioc_number,struct MPT3SAS_ADAPTER ** iocpp,int mpi_version)419 _ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
420 							int mpi_version)
421 {
422 	struct MPT3SAS_ADAPTER *ioc;
423 	int version = 0;
424 	/* global ioc lock to protect controller on list operations */
425 	spin_lock(&gioc_lock);
426 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
427 		if (ioc->id != ioc_number)
428 			continue;
429 		/* Check whether this ioctl command is from right
430 		 * ioctl device or not, if not continue the search.
431 		 */
432 		version = ioc->hba_mpi_version_belonged;
433 		/* MPI25_VERSION and MPI26_VERSION uses same ioctl
434 		 * device.
435 		 */
436 		if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
437 			if ((version == MPI25_VERSION) ||
438 				(version == MPI26_VERSION))
439 				goto out;
440 			else
441 				continue;
442 		} else {
443 			if (version != mpi_version)
444 				continue;
445 		}
446 out:
447 		spin_unlock(&gioc_lock);
448 		*iocpp = ioc;
449 		return ioc_number;
450 	}
451 	spin_unlock(&gioc_lock);
452 	*iocpp = NULL;
453 	return -1;
454 }
455 
456 /**
457  * mpt3sas_ctl_pre_reset_handler - reset callback handler (for ctl)
458  * @ioc: per adapter object
459  *
460  * The handler for doing any required cleanup or initialization.
461  */
mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER * ioc)462 void mpt3sas_ctl_pre_reset_handler(struct MPT3SAS_ADAPTER *ioc)
463 {
464 	int i;
465 	u8 issue_reset;
466 
467 	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_PRE_RESET\n", __func__));
468 	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
469 		if (!(ioc->diag_buffer_status[i] &
470 		      MPT3_DIAG_BUFFER_IS_REGISTERED))
471 			continue;
472 		if ((ioc->diag_buffer_status[i] &
473 		     MPT3_DIAG_BUFFER_IS_RELEASED))
474 			continue;
475 
476 		/*
477 		 * add a log message to indicate the release
478 		 */
479 		ioc_info(ioc,
480 		    "%s: Releasing the trace buffer due to adapter reset.",
481 		    __func__);
482 		ioc->htb_rel.buffer_rel_condition =
483 		    MPT3_DIAG_BUFFER_REL_TRIGGER;
484 		mpt3sas_send_diag_release(ioc, i, &issue_reset);
485 	}
486 }
487 
488 /**
489  * mpt3sas_ctl_clear_outstanding_ioctls - clears outstanding ioctl cmd.
490  * @ioc: per adapter object
491  *
492  * The handler for doing any required cleanup or initialization.
493  */
mpt3sas_ctl_clear_outstanding_ioctls(struct MPT3SAS_ADAPTER * ioc)494 void mpt3sas_ctl_clear_outstanding_ioctls(struct MPT3SAS_ADAPTER *ioc)
495 {
496 	dtmprintk(ioc,
497 	    ioc_info(ioc, "%s: clear outstanding ioctl cmd\n", __func__));
498 	if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
499 		ioc->ctl_cmds.status |= MPT3_CMD_RESET;
500 		mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
501 		complete(&ioc->ctl_cmds.done);
502 	}
503 }
504 
505 /**
506  * mpt3sas_ctl_reset_done_handler - reset callback handler (for ctl)
507  * @ioc: per adapter object
508  *
509  * The handler for doing any required cleanup or initialization.
510  */
mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER * ioc)511 void mpt3sas_ctl_reset_done_handler(struct MPT3SAS_ADAPTER *ioc)
512 {
513 	int i;
514 
515 	dtmprintk(ioc, ioc_info(ioc, "%s: MPT3_IOC_DONE_RESET\n", __func__));
516 
517 	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
518 		if (!(ioc->diag_buffer_status[i] &
519 		      MPT3_DIAG_BUFFER_IS_REGISTERED))
520 			continue;
521 		if ((ioc->diag_buffer_status[i] &
522 		     MPT3_DIAG_BUFFER_IS_RELEASED))
523 			continue;
524 		ioc->diag_buffer_status[i] |=
525 			MPT3_DIAG_BUFFER_IS_DIAG_RESET;
526 	}
527 }
528 
529 /**
530  * _ctl_fasync -
531  * @fd: ?
532  * @filep: ?
533  * @mode: ?
534  *
535  * Called when application request fasyn callback handler.
536  */
537 static int
_ctl_fasync(int fd,struct file * filep,int mode)538 _ctl_fasync(int fd, struct file *filep, int mode)
539 {
540 	return fasync_helper(fd, filep, mode, &async_queue);
541 }
542 
543 /**
544  * _ctl_poll -
545  * @filep: ?
546  * @wait: ?
547  *
548  */
549 static __poll_t
_ctl_poll(struct file * filep,poll_table * wait)550 _ctl_poll(struct file *filep, poll_table *wait)
551 {
552 	struct MPT3SAS_ADAPTER *ioc;
553 
554 	poll_wait(filep, &ctl_poll_wait, wait);
555 
556 	/* global ioc lock to protect controller on list operations */
557 	spin_lock(&gioc_lock);
558 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
559 		if (ioc->aen_event_read_flag) {
560 			spin_unlock(&gioc_lock);
561 			return EPOLLIN | EPOLLRDNORM;
562 		}
563 	}
564 	spin_unlock(&gioc_lock);
565 	return 0;
566 }
567 
568 /**
569  * _ctl_set_task_mid - assign an active smid to tm request
570  * @ioc: per adapter object
571  * @karg: (struct mpt3_ioctl_command)
572  * @tm_request: pointer to mf from user space
573  *
574  * Return: 0 when an smid if found, else fail.
575  * during failure, the reply frame is filled.
576  */
577 static int
_ctl_set_task_mid(struct MPT3SAS_ADAPTER * ioc,struct mpt3_ioctl_command * karg,Mpi2SCSITaskManagementRequest_t * tm_request)578 _ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
579 	Mpi2SCSITaskManagementRequest_t *tm_request)
580 {
581 	bool found = false;
582 	u16 smid;
583 	u16 handle;
584 	struct scsi_cmnd *scmd;
585 	struct MPT3SAS_DEVICE *priv_data;
586 	Mpi2SCSITaskManagementReply_t *tm_reply;
587 	u32 sz;
588 	u32 lun;
589 	char *desc = NULL;
590 
591 	if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
592 		desc = "abort_task";
593 	else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
594 		desc = "query_task";
595 	else
596 		return 0;
597 
598 	lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
599 
600 	handle = le16_to_cpu(tm_request->DevHandle);
601 	for (smid = ioc->scsiio_depth; smid && !found; smid--) {
602 		struct scsiio_tracker *st;
603 		__le16 task_mid;
604 
605 		scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
606 		if (!scmd)
607 			continue;
608 		if (lun != scmd->device->lun)
609 			continue;
610 		priv_data = scmd->device->hostdata;
611 		if (priv_data->sas_target == NULL)
612 			continue;
613 		if (priv_data->sas_target->handle != handle)
614 			continue;
615 		st = scsi_cmd_priv(scmd);
616 
617 		/*
618 		 * If the given TaskMID from the user space is zero, then the
619 		 * first outstanding smid will be picked up.  Otherwise,
620 		 * targeted smid will be the one.
621 		 */
622 		task_mid = cpu_to_le16(st->smid);
623 		if (!tm_request->TaskMID)
624 			tm_request->TaskMID = task_mid;
625 		found = tm_request->TaskMID == task_mid;
626 	}
627 
628 	if (!found) {
629 		dctlprintk(ioc,
630 			   ioc_info(ioc, "%s: handle(0x%04x), lun(%d), no active mid!!\n",
631 				    desc, le16_to_cpu(tm_request->DevHandle),
632 				    lun));
633 		tm_reply = ioc->ctl_cmds.reply;
634 		tm_reply->DevHandle = tm_request->DevHandle;
635 		tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
636 		tm_reply->TaskType = tm_request->TaskType;
637 		tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
638 		tm_reply->VP_ID = tm_request->VP_ID;
639 		tm_reply->VF_ID = tm_request->VF_ID;
640 		sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
641 		if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
642 		    sz))
643 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
644 			    __LINE__, __func__);
645 		return 1;
646 	}
647 
648 	dctlprintk(ioc,
649 		   ioc_info(ioc, "%s: handle(0x%04x), lun(%d), task_mid(%d)\n",
650 			    desc, le16_to_cpu(tm_request->DevHandle), lun,
651 			    le16_to_cpu(tm_request->TaskMID)));
652 	return 0;
653 }
654 
655 /**
656  * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
657  * @ioc: per adapter object
658  * @karg: (struct mpt3_ioctl_command)
659  * @mf: pointer to mf in user space
660  */
661 static long
_ctl_do_mpt_command(struct MPT3SAS_ADAPTER * ioc,struct mpt3_ioctl_command karg,void __user * mf)662 _ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
663 	void __user *mf)
664 {
665 	MPI2RequestHeader_t *mpi_request = NULL, *request;
666 	MPI2DefaultReply_t *mpi_reply;
667 	Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request = NULL;
668 	struct _pcie_device *pcie_device = NULL;
669 	u16 smid;
670 	unsigned long timeout;
671 	u8 issue_reset;
672 	u32 sz, sz_arg;
673 	void *psge;
674 	void *data_out = NULL;
675 	dma_addr_t data_out_dma = 0;
676 	size_t data_out_sz = 0;
677 	void *data_in = NULL;
678 	dma_addr_t data_in_dma = 0;
679 	size_t data_in_sz = 0;
680 	long ret;
681 	u16 device_handle = MPT3SAS_INVALID_DEVICE_HANDLE;
682 	int tm_ret;
683 
684 	issue_reset = 0;
685 
686 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
687 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
688 		ret = -EAGAIN;
689 		goto out;
690 	}
691 
692 	ret = mpt3sas_wait_for_ioc(ioc,	IOC_OPERATIONAL_WAIT_COUNT);
693 	if (ret)
694 		goto out;
695 
696 	mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
697 	if (!mpi_request) {
698 		ioc_err(ioc, "%s: failed obtaining a memory for mpi_request\n",
699 			__func__);
700 		ret = -ENOMEM;
701 		goto out;
702 	}
703 
704 	/* Check for overflow and wraparound */
705 	if (karg.data_sge_offset * 4 > ioc->request_sz ||
706 	    karg.data_sge_offset > (UINT_MAX / 4)) {
707 		ret = -EINVAL;
708 		goto out;
709 	}
710 
711 	/* copy in request message frame from user */
712 	if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
713 		pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
714 		    __func__);
715 		ret = -EFAULT;
716 		goto out;
717 	}
718 
719 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
720 		smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
721 		if (!smid) {
722 			ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
723 			ret = -EAGAIN;
724 			goto out;
725 		}
726 	} else {
727 		/* Use first reserved smid for passthrough ioctls */
728 		smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
729 	}
730 
731 	ret = 0;
732 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
733 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
734 	request = mpt3sas_base_get_msg_frame(ioc, smid);
735 	memset(request, 0, ioc->request_sz);
736 	memcpy(request, mpi_request, karg.data_sge_offset*4);
737 	ioc->ctl_cmds.smid = smid;
738 	data_out_sz = karg.data_out_size;
739 	data_in_sz = karg.data_in_size;
740 
741 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
742 	    mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
743 	    mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT ||
744 	    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH ||
745 	    mpi_request->Function == MPI2_FUNCTION_NVME_ENCAPSULATED) {
746 
747 		device_handle = le16_to_cpu(mpi_request->FunctionDependent1);
748 		if (!device_handle || (device_handle >
749 		    ioc->facts.MaxDevHandle)) {
750 			ret = -EINVAL;
751 			mpt3sas_base_free_smid(ioc, smid);
752 			goto out;
753 		}
754 	}
755 
756 	/* obtain dma-able memory for data transfer */
757 	if (data_out_sz) /* WRITE */ {
758 		data_out = dma_alloc_coherent(&ioc->pdev->dev, data_out_sz,
759 				&data_out_dma, GFP_KERNEL);
760 		if (!data_out) {
761 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
762 			    __LINE__, __func__);
763 			ret = -ENOMEM;
764 			mpt3sas_base_free_smid(ioc, smid);
765 			goto out;
766 		}
767 		if (copy_from_user(data_out, karg.data_out_buf_ptr,
768 			data_out_sz)) {
769 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
770 			    __LINE__, __func__);
771 			ret =  -EFAULT;
772 			mpt3sas_base_free_smid(ioc, smid);
773 			goto out;
774 		}
775 	}
776 
777 	if (data_in_sz) /* READ */ {
778 		data_in = dma_alloc_coherent(&ioc->pdev->dev, data_in_sz,
779 				&data_in_dma, GFP_KERNEL);
780 		if (!data_in) {
781 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
782 			    __LINE__, __func__);
783 			ret = -ENOMEM;
784 			mpt3sas_base_free_smid(ioc, smid);
785 			goto out;
786 		}
787 	}
788 
789 	psge = (void *)request + (karg.data_sge_offset*4);
790 
791 	/* send command to firmware */
792 	_ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
793 
794 	init_completion(&ioc->ctl_cmds.done);
795 	switch (mpi_request->Function) {
796 	case MPI2_FUNCTION_NVME_ENCAPSULATED:
797 	{
798 		nvme_encap_request = (Mpi26NVMeEncapsulatedRequest_t *)request;
799 		if (!ioc->pcie_sg_lookup) {
800 			dtmprintk(ioc, ioc_info(ioc,
801 			    "HBA doesn't support NVMe. Rejecting NVMe Encapsulated request.\n"
802 			    ));
803 
804 			if (ioc->logging_level & MPT_DEBUG_TM)
805 				_debug_dump_mf(nvme_encap_request,
806 				    ioc->request_sz/4);
807 			mpt3sas_base_free_smid(ioc, smid);
808 			ret = -EINVAL;
809 			goto out;
810 		}
811 		/*
812 		 * Get the Physical Address of the sense buffer.
813 		 * Use Error Response buffer address field to hold the sense
814 		 * buffer address.
815 		 * Clear the internal sense buffer, which will potentially hold
816 		 * the Completion Queue Entry on return, or 0 if no Entry.
817 		 * Build the PRPs and set direction bits.
818 		 * Send the request.
819 		 */
820 		nvme_encap_request->ErrorResponseBaseAddress =
821 		    cpu_to_le64(ioc->sense_dma & 0xFFFFFFFF00000000UL);
822 		nvme_encap_request->ErrorResponseBaseAddress |=
823 		   cpu_to_le64(le32_to_cpu(
824 		   mpt3sas_base_get_sense_buffer_dma(ioc, smid)));
825 		nvme_encap_request->ErrorResponseAllocationLength =
826 					cpu_to_le16(NVME_ERROR_RESPONSE_SIZE);
827 		memset(ioc->ctl_cmds.sense, 0, NVME_ERROR_RESPONSE_SIZE);
828 		ioc->build_nvme_prp(ioc, smid, nvme_encap_request,
829 		    data_out_dma, data_out_sz, data_in_dma, data_in_sz);
830 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
831 			dtmprintk(ioc,
832 				  ioc_info(ioc, "handle(0x%04x): ioctl failed due to device removal in progress\n",
833 					   device_handle));
834 			mpt3sas_base_free_smid(ioc, smid);
835 			ret = -EINVAL;
836 			goto out;
837 		}
838 		mpt3sas_base_put_smid_nvme_encap(ioc, smid);
839 		break;
840 	}
841 	case MPI2_FUNCTION_SCSI_IO_REQUEST:
842 	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
843 	{
844 		Mpi2SCSIIORequest_t *scsiio_request =
845 		    (Mpi2SCSIIORequest_t *)request;
846 		scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
847 		scsiio_request->SenseBufferLowAddress =
848 		    mpt3sas_base_get_sense_buffer_dma(ioc, smid);
849 		memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
850 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
851 			dtmprintk(ioc,
852 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
853 					   device_handle));
854 			mpt3sas_base_free_smid(ioc, smid);
855 			ret = -EINVAL;
856 			goto out;
857 		}
858 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
859 		    data_in_dma, data_in_sz);
860 		if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
861 			ioc->put_smid_scsi_io(ioc, smid, device_handle);
862 		else
863 			ioc->put_smid_default(ioc, smid);
864 		break;
865 	}
866 	case MPI2_FUNCTION_SCSI_TASK_MGMT:
867 	{
868 		Mpi2SCSITaskManagementRequest_t *tm_request =
869 		    (Mpi2SCSITaskManagementRequest_t *)request;
870 
871 		dtmprintk(ioc,
872 			  ioc_info(ioc, "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
873 				   le16_to_cpu(tm_request->DevHandle),
874 				   tm_request->TaskType));
875 		ioc->got_task_abort_from_ioctl = 1;
876 		if (tm_request->TaskType ==
877 		    MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
878 		    tm_request->TaskType ==
879 		    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
880 			if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
881 				mpt3sas_base_free_smid(ioc, smid);
882 				ioc->got_task_abort_from_ioctl = 0;
883 				goto out;
884 			}
885 		}
886 		ioc->got_task_abort_from_ioctl = 0;
887 
888 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
889 			dtmprintk(ioc,
890 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
891 					   device_handle));
892 			mpt3sas_base_free_smid(ioc, smid);
893 			ret = -EINVAL;
894 			goto out;
895 		}
896 		mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
897 		    tm_request->DevHandle));
898 		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
899 		    data_in_dma, data_in_sz);
900 		ioc->put_smid_hi_priority(ioc, smid, 0);
901 		break;
902 	}
903 	case MPI2_FUNCTION_SMP_PASSTHROUGH:
904 	{
905 		Mpi2SmpPassthroughRequest_t *smp_request =
906 		    (Mpi2SmpPassthroughRequest_t *)mpi_request;
907 		u8 *data;
908 
909 		if (!ioc->multipath_on_hba) {
910 			/* ioc determines which port to use */
911 			smp_request->PhysicalPort = 0xFF;
912 		}
913 		if (smp_request->PassthroughFlags &
914 		    MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
915 			data = (u8 *)&smp_request->SGL;
916 		else {
917 			if (unlikely(data_out == NULL)) {
918 				pr_err("failure at %s:%d/%s()!\n",
919 				    __FILE__, __LINE__, __func__);
920 				mpt3sas_base_free_smid(ioc, smid);
921 				ret = -EINVAL;
922 				goto out;
923 			}
924 			data = data_out;
925 		}
926 
927 		if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
928 			ioc->ioc_link_reset_in_progress = 1;
929 			ioc->ignore_loginfos = 1;
930 		}
931 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
932 		    data_in_sz);
933 		ioc->put_smid_default(ioc, smid);
934 		break;
935 	}
936 	case MPI2_FUNCTION_SATA_PASSTHROUGH:
937 	{
938 		if (test_bit(device_handle, ioc->device_remove_in_progress)) {
939 			dtmprintk(ioc,
940 				  ioc_info(ioc, "handle(0x%04x) :ioctl failed due to device removal in progress\n",
941 					   device_handle));
942 			mpt3sas_base_free_smid(ioc, smid);
943 			ret = -EINVAL;
944 			goto out;
945 		}
946 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
947 		    data_in_sz);
948 		ioc->put_smid_default(ioc, smid);
949 		break;
950 	}
951 	case MPI2_FUNCTION_FW_DOWNLOAD:
952 	{
953 		if (ioc->pdev->vendor == MPI2_MFGPAGE_VENDORID_ATTO) {
954 			ioc_info(ioc, "Firmware download not supported for ATTO HBA.\n");
955 			ret = -EPERM;
956 			break;
957 		}
958 		fallthrough;
959 	}
960 	case MPI2_FUNCTION_FW_UPLOAD:
961 	{
962 		ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
963 		    data_in_sz);
964 		ioc->put_smid_default(ioc, smid);
965 		break;
966 	}
967 	case MPI2_FUNCTION_TOOLBOX:
968 	{
969 		Mpi2ToolboxCleanRequest_t *toolbox_request =
970 			(Mpi2ToolboxCleanRequest_t *)mpi_request;
971 
972 		if ((toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL)
973 		    || (toolbox_request->Tool ==
974 		    MPI26_TOOLBOX_BACKEND_PCIE_LANE_MARGIN))
975 			ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
976 				data_in_dma, data_in_sz);
977 		else if (toolbox_request->Tool ==
978 				MPI2_TOOLBOX_MEMORY_MOVE_TOOL) {
979 			Mpi2ToolboxMemMoveRequest_t *mem_move_request =
980 					(Mpi2ToolboxMemMoveRequest_t *)request;
981 			Mpi2SGESimple64_t tmp, *src = NULL, *dst = NULL;
982 
983 			ioc->build_sg_mpi(ioc, psge, data_out_dma,
984 					data_out_sz, data_in_dma, data_in_sz);
985 			if (data_out_sz && !data_in_sz) {
986 				dst =
987 				    (Mpi2SGESimple64_t *)&mem_move_request->SGL;
988 				src = (void *)dst + ioc->sge_size;
989 
990 				memcpy(&tmp, src, ioc->sge_size);
991 				memcpy(src, dst, ioc->sge_size);
992 				memcpy(dst, &tmp, ioc->sge_size);
993 			}
994 			if (ioc->logging_level & MPT_DEBUG_TM) {
995 				ioc_info(ioc,
996 				  "Mpi2ToolboxMemMoveRequest_t request msg\n");
997 				_debug_dump_mf(mem_move_request,
998 							ioc->request_sz/4);
999 			}
1000 		} else
1001 			ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
1002 			    data_in_dma, data_in_sz);
1003 		ioc->put_smid_default(ioc, smid);
1004 		break;
1005 	}
1006 	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
1007 	{
1008 		Mpi2SasIoUnitControlRequest_t *sasiounit_request =
1009 		    (Mpi2SasIoUnitControlRequest_t *)mpi_request;
1010 
1011 		if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
1012 		    || sasiounit_request->Operation ==
1013 		    MPI2_SAS_OP_PHY_LINK_RESET) {
1014 			ioc->ioc_link_reset_in_progress = 1;
1015 			ioc->ignore_loginfos = 1;
1016 		}
1017 		/* drop to default case for posting the request */
1018 	}
1019 		fallthrough;
1020 	default:
1021 		ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
1022 		    data_in_dma, data_in_sz);
1023 		ioc->put_smid_default(ioc, smid);
1024 		break;
1025 	}
1026 
1027 	if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
1028 		timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
1029 	else
1030 		timeout = karg.timeout;
1031 	wait_for_completion_timeout(&ioc->ctl_cmds.done, timeout*HZ);
1032 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
1033 		Mpi2SCSITaskManagementRequest_t *tm_request =
1034 		    (Mpi2SCSITaskManagementRequest_t *)mpi_request;
1035 		mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
1036 		    tm_request->DevHandle));
1037 		mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
1038 	} else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
1039 	    mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
1040 		ioc->ioc_link_reset_in_progress) {
1041 		ioc->ioc_link_reset_in_progress = 0;
1042 		ioc->ignore_loginfos = 0;
1043 	}
1044 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1045 		mpt3sas_check_cmd_timeout(ioc,
1046 		    ioc->ctl_cmds.status, mpi_request,
1047 		    karg.data_sge_offset, issue_reset);
1048 		goto issue_host_reset;
1049 	}
1050 
1051 	mpi_reply = ioc->ctl_cmds.reply;
1052 
1053 	if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
1054 	    (ioc->logging_level & MPT_DEBUG_TM)) {
1055 		Mpi2SCSITaskManagementReply_t *tm_reply =
1056 		    (Mpi2SCSITaskManagementReply_t *)mpi_reply;
1057 
1058 		ioc_info(ioc, "TASK_MGMT: IOCStatus(0x%04x), IOCLogInfo(0x%08x), TerminationCount(0x%08x)\n",
1059 			 le16_to_cpu(tm_reply->IOCStatus),
1060 			 le32_to_cpu(tm_reply->IOCLogInfo),
1061 			 le32_to_cpu(tm_reply->TerminationCount));
1062 	}
1063 
1064 	/* copy out xdata to user */
1065 	if (data_in_sz) {
1066 		if (copy_to_user(karg.data_in_buf_ptr, data_in,
1067 		    data_in_sz)) {
1068 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1069 			    __LINE__, __func__);
1070 			ret = -ENODATA;
1071 			goto out;
1072 		}
1073 	}
1074 
1075 	/* copy out reply message frame to user */
1076 	if (karg.max_reply_bytes) {
1077 		sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
1078 		if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
1079 		    sz)) {
1080 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1081 			    __LINE__, __func__);
1082 			ret = -ENODATA;
1083 			goto out;
1084 		}
1085 	}
1086 
1087 	/* copy out sense/NVMe Error Response to user */
1088 	if (karg.max_sense_bytes && (mpi_request->Function ==
1089 	    MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
1090 	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH || mpi_request->Function ==
1091 	    MPI2_FUNCTION_NVME_ENCAPSULATED)) {
1092 		if (karg.sense_data_ptr == NULL) {
1093 			ioc_info(ioc, "Response buffer provided by application is NULL; Response data will not be returned\n");
1094 			goto out;
1095 		}
1096 		sz_arg = (mpi_request->Function ==
1097 		MPI2_FUNCTION_NVME_ENCAPSULATED) ? NVME_ERROR_RESPONSE_SIZE :
1098 							SCSI_SENSE_BUFFERSIZE;
1099 		sz = min_t(u32, karg.max_sense_bytes, sz_arg);
1100 		if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
1101 		    sz)) {
1102 			pr_err("failure at %s:%d/%s()!\n", __FILE__,
1103 				__LINE__, __func__);
1104 			ret = -ENODATA;
1105 			goto out;
1106 		}
1107 	}
1108 
1109  issue_host_reset:
1110 	if (issue_reset) {
1111 		ret = -ENODATA;
1112 		if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
1113 		    mpi_request->Function ==
1114 		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
1115 		    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
1116 			ioc_info(ioc, "issue target reset: handle = (0x%04x)\n",
1117 				 le16_to_cpu(mpi_request->FunctionDependent1));
1118 			mpt3sas_halt_firmware(ioc);
1119 			pcie_device = mpt3sas_get_pdev_by_handle(ioc,
1120 				le16_to_cpu(mpi_request->FunctionDependent1));
1121 			if (pcie_device && (!ioc->tm_custom_handling) &&
1122 			    (!(mpt3sas_scsih_is_pcie_scsi_device(
1123 			    pcie_device->device_info))))
1124 				tm_ret = mpt3sas_scsih_issue_locked_tm(ioc,
1125 				  le16_to_cpu(mpi_request->FunctionDependent1),
1126 				  0, 0, 0,
1127 				  MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1128 				  0, pcie_device->reset_timeout,
1129 			MPI26_SCSITASKMGMT_MSGFLAGS_PROTOCOL_LVL_RST_PCIE);
1130 			else
1131 				tm_ret = mpt3sas_scsih_issue_locked_tm(ioc,
1132 				  le16_to_cpu(mpi_request->FunctionDependent1),
1133 				  0, 0, 0,
1134 				  MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
1135 				  0, 30, MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET);
1136 
1137 			if (tm_ret != SUCCESS) {
1138 				ioc_info(ioc,
1139 					 "target reset failed, issue hard reset: handle (0x%04x)\n",
1140 					 le16_to_cpu(mpi_request->FunctionDependent1));
1141 				mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1142 			}
1143 		} else
1144 			mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1145 	}
1146 
1147  out:
1148 	if (pcie_device)
1149 		pcie_device_put(pcie_device);
1150 
1151 	/* free memory associated with sg buffers */
1152 	if (data_in)
1153 		dma_free_coherent(&ioc->pdev->dev, data_in_sz, data_in,
1154 		    data_in_dma);
1155 
1156 	if (data_out)
1157 		dma_free_coherent(&ioc->pdev->dev, data_out_sz, data_out,
1158 		    data_out_dma);
1159 
1160 	kfree(mpi_request);
1161 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1162 	return ret;
1163 }
1164 
1165 /**
1166  * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1167  * @ioc: per adapter object
1168  * @arg: user space buffer containing ioctl content
1169  */
1170 static long
_ctl_getiocinfo(struct MPT3SAS_ADAPTER * ioc,void __user * arg)1171 _ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1172 {
1173 	struct mpt3_ioctl_iocinfo karg;
1174 
1175 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1176 				 __func__));
1177 
1178 	memset(&karg, 0 , sizeof(karg));
1179 	if (ioc->pfacts)
1180 		karg.port_number = ioc->pfacts[0].PortNumber;
1181 	karg.hw_rev = ioc->pdev->revision;
1182 	karg.pci_id = ioc->pdev->device;
1183 	karg.subsystem_device = ioc->pdev->subsystem_device;
1184 	karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1185 	karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1186 	karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1187 	karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1188 	karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1189 	karg.firmware_version = ioc->facts.FWVersion.Word;
1190 	strcpy(karg.driver_version, ioc->driver_name);
1191 	strcat(karg.driver_version, "-");
1192 	switch  (ioc->hba_mpi_version_belonged) {
1193 	case MPI2_VERSION:
1194 		if (ioc->is_warpdrive)
1195 			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1196 		else
1197 			karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1198 		strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1199 		break;
1200 	case MPI25_VERSION:
1201 	case MPI26_VERSION:
1202 		if (ioc->is_gen35_ioc)
1203 			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS35;
1204 		else
1205 			karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1206 		strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1207 		break;
1208 	}
1209 	karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1210 
1211 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1212 		pr_err("failure at %s:%d/%s()!\n",
1213 		    __FILE__, __LINE__, __func__);
1214 		return -EFAULT;
1215 	}
1216 	return 0;
1217 }
1218 
1219 /**
1220  * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1221  * @ioc: per adapter object
1222  * @arg: user space buffer containing ioctl content
1223  */
1224 static long
_ctl_eventquery(struct MPT3SAS_ADAPTER * ioc,void __user * arg)1225 _ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1226 {
1227 	struct mpt3_ioctl_eventquery karg;
1228 
1229 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1230 		pr_err("failure at %s:%d/%s()!\n",
1231 		    __FILE__, __LINE__, __func__);
1232 		return -EFAULT;
1233 	}
1234 
1235 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1236 				 __func__));
1237 
1238 	karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1239 	memcpy(karg.event_types, ioc->event_type,
1240 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1241 
1242 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1243 		pr_err("failure at %s:%d/%s()!\n",
1244 		    __FILE__, __LINE__, __func__);
1245 		return -EFAULT;
1246 	}
1247 	return 0;
1248 }
1249 
1250 /**
1251  * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1252  * @ioc: per adapter object
1253  * @arg: user space buffer containing ioctl content
1254  */
1255 static long
_ctl_eventenable(struct MPT3SAS_ADAPTER * ioc,void __user * arg)1256 _ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1257 {
1258 	struct mpt3_ioctl_eventenable karg;
1259 
1260 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1261 		pr_err("failure at %s:%d/%s()!\n",
1262 		    __FILE__, __LINE__, __func__);
1263 		return -EFAULT;
1264 	}
1265 
1266 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1267 				 __func__));
1268 
1269 	memcpy(ioc->event_type, karg.event_types,
1270 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1271 	mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1272 
1273 	if (ioc->event_log)
1274 		return 0;
1275 	/* initialize event_log */
1276 	ioc->event_context = 0;
1277 	ioc->aen_event_read_flag = 0;
1278 	ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1279 	    sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1280 	if (!ioc->event_log) {
1281 		pr_err("failure at %s:%d/%s()!\n",
1282 		    __FILE__, __LINE__, __func__);
1283 		return -ENOMEM;
1284 	}
1285 	return 0;
1286 }
1287 
1288 /**
1289  * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1290  * @ioc: per adapter object
1291  * @arg: user space buffer containing ioctl content
1292  */
1293 static long
_ctl_eventreport(struct MPT3SAS_ADAPTER * ioc,void __user * arg)1294 _ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1295 {
1296 	struct mpt3_ioctl_eventreport karg;
1297 	u32 number_bytes, max_events, max;
1298 	struct mpt3_ioctl_eventreport __user *uarg = arg;
1299 
1300 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1301 		pr_err("failure at %s:%d/%s()!\n",
1302 		    __FILE__, __LINE__, __func__);
1303 		return -EFAULT;
1304 	}
1305 
1306 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1307 				 __func__));
1308 
1309 	number_bytes = karg.hdr.max_data_size -
1310 	    sizeof(struct mpt3_ioctl_header);
1311 	max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1312 	max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1313 
1314 	/* If fewer than 1 event is requested, there must have
1315 	 * been some type of error.
1316 	 */
1317 	if (!max || !ioc->event_log)
1318 		return -ENODATA;
1319 
1320 	number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1321 	if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1322 		pr_err("failure at %s:%d/%s()!\n",
1323 		    __FILE__, __LINE__, __func__);
1324 		return -EFAULT;
1325 	}
1326 
1327 	/* reset flag so SIGIO can restart */
1328 	ioc->aen_event_read_flag = 0;
1329 	return 0;
1330 }
1331 
1332 /**
1333  * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1334  * @ioc: per adapter object
1335  * @arg: user space buffer containing ioctl content
1336  */
1337 static long
_ctl_do_reset(struct MPT3SAS_ADAPTER * ioc,void __user * arg)1338 _ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1339 {
1340 	struct mpt3_ioctl_diag_reset karg;
1341 	int retval;
1342 
1343 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1344 		pr_err("failure at %s:%d/%s()!\n",
1345 		    __FILE__, __LINE__, __func__);
1346 		return -EFAULT;
1347 	}
1348 
1349 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
1350 	    ioc->is_driver_loading)
1351 		return -EAGAIN;
1352 
1353 	dctlprintk(ioc, ioc_info(ioc, "%s: enter\n",
1354 				 __func__));
1355 
1356 	ioc->reset_from_user = 1;
1357 	retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1358 	ioc_info(ioc,
1359 	    "Ioctl: host reset: %s\n", ((!retval) ? "SUCCESS" : "FAILED"));
1360 	return 0;
1361 }
1362 
1363 /**
1364  * _ctl_btdh_search_sas_device - searching for sas device
1365  * @ioc: per adapter object
1366  * @btdh: btdh ioctl payload
1367  */
1368 static int
_ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER * ioc,struct mpt3_ioctl_btdh_mapping * btdh)1369 _ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1370 	struct mpt3_ioctl_btdh_mapping *btdh)
1371 {
1372 	struct _sas_device *sas_device;
1373 	unsigned long flags;
1374 	int rc = 0;
1375 
1376 	if (list_empty(&ioc->sas_device_list))
1377 		return rc;
1378 
1379 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1380 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1381 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1382 		    btdh->handle == sas_device->handle) {
1383 			btdh->bus = sas_device->channel;
1384 			btdh->id = sas_device->id;
1385 			rc = 1;
1386 			goto out;
1387 		} else if (btdh->bus == sas_device->channel && btdh->id ==
1388 		    sas_device->id && btdh->handle == 0xFFFF) {
1389 			btdh->handle = sas_device->handle;
1390 			rc = 1;
1391 			goto out;
1392 		}
1393 	}
1394  out:
1395 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1396 	return rc;
1397 }
1398 
1399 /**
1400  * _ctl_btdh_search_pcie_device - searching for pcie device
1401  * @ioc: per adapter object
1402  * @btdh: btdh ioctl payload
1403  */
1404 static int
_ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER * ioc,struct mpt3_ioctl_btdh_mapping * btdh)1405 _ctl_btdh_search_pcie_device(struct MPT3SAS_ADAPTER *ioc,
1406 	struct mpt3_ioctl_btdh_mapping *btdh)
1407 {
1408 	struct _pcie_device *pcie_device;
1409 	unsigned long flags;
1410 	int rc = 0;
1411 
1412 	if (list_empty(&ioc->pcie_device_list))
1413 		return rc;
1414 
1415 	spin_lock_irqsave(&ioc->pcie_device_lock, flags);
1416 	list_for_each_entry(pcie_device, &ioc->pcie_device_list, list) {
1417 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1418 			   btdh->handle == pcie_device->handle) {
1419 			btdh->bus = pcie_device->channel;
1420 			btdh->id = pcie_device->id;
1421 			rc = 1;
1422 			goto out;
1423 		} else if (btdh->bus == pcie_device->channel && btdh->id ==
1424 			   pcie_device->id && btdh->handle == 0xFFFF) {
1425 			btdh->handle = pcie_device->handle;
1426 			rc = 1;
1427 			goto out;
1428 		}
1429 	}
1430  out:
1431 	spin_unlock_irqrestore(&ioc->pcie_device_lock, flags);
1432 	return rc;
1433 }
1434 
1435 /**
1436  * _ctl_btdh_search_raid_device - searching for raid device
1437  * @ioc: per adapter object
1438  * @btdh: btdh ioctl payload
1439  */
1440 static int
_ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER * ioc,struct mpt3_ioctl_btdh_mapping * btdh)1441 _ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1442 	struct mpt3_ioctl_btdh_mapping *btdh)
1443 {
1444 	struct _raid_device *raid_device;
1445 	unsigned long flags;
1446 	int rc = 0;
1447 
1448 	if (list_empty(&ioc->raid_device_list))
1449 		return rc;
1450 
1451 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1452 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1453 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1454 		    btdh->handle == raid_device->handle) {
1455 			btdh->bus = raid_device->channel;
1456 			btdh->id = raid_device->id;
1457 			rc = 1;
1458 			goto out;
1459 		} else if (btdh->bus == raid_device->channel && btdh->id ==
1460 		    raid_device->id && btdh->handle == 0xFFFF) {
1461 			btdh->handle = raid_device->handle;
1462 			rc = 1;
1463 			goto out;
1464 		}
1465 	}
1466  out:
1467 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1468 	return rc;
1469 }
1470 
1471 /**
1472  * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1473  * @ioc: per adapter object
1474  * @arg: user space buffer containing ioctl content
1475  */
1476 static long
_ctl_btdh_mapping(struct MPT3SAS_ADAPTER * ioc,void __user * arg)1477 _ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1478 {
1479 	struct mpt3_ioctl_btdh_mapping karg;
1480 	int rc;
1481 
1482 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1483 		pr_err("failure at %s:%d/%s()!\n",
1484 		    __FILE__, __LINE__, __func__);
1485 		return -EFAULT;
1486 	}
1487 
1488 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1489 				 __func__));
1490 
1491 	rc = _ctl_btdh_search_sas_device(ioc, &karg);
1492 	if (!rc)
1493 		rc = _ctl_btdh_search_pcie_device(ioc, &karg);
1494 	if (!rc)
1495 		_ctl_btdh_search_raid_device(ioc, &karg);
1496 
1497 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1498 		pr_err("failure at %s:%d/%s()!\n",
1499 		    __FILE__, __LINE__, __func__);
1500 		return -EFAULT;
1501 	}
1502 	return 0;
1503 }
1504 
1505 /**
1506  * _ctl_diag_capability - return diag buffer capability
1507  * @ioc: per adapter object
1508  * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1509  *
1510  * returns 1 when diag buffer support is enabled in firmware
1511  */
1512 static u8
_ctl_diag_capability(struct MPT3SAS_ADAPTER * ioc,u8 buffer_type)1513 _ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1514 {
1515 	u8 rc = 0;
1516 
1517 	switch (buffer_type) {
1518 	case MPI2_DIAG_BUF_TYPE_TRACE:
1519 		if (ioc->facts.IOCCapabilities &
1520 		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1521 			rc = 1;
1522 		break;
1523 	case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1524 		if (ioc->facts.IOCCapabilities &
1525 		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1526 			rc = 1;
1527 		break;
1528 	case MPI2_DIAG_BUF_TYPE_EXTENDED:
1529 		if (ioc->facts.IOCCapabilities &
1530 		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1531 			rc = 1;
1532 	}
1533 
1534 	return rc;
1535 }
1536 
1537 /**
1538  * _ctl_diag_get_bufftype - return diag buffer type
1539  *              either TRACE, SNAPSHOT, or EXTENDED
1540  * @ioc: per adapter object
1541  * @unique_id: specifies the unique_id for the buffer
1542  *
1543  * returns MPT3_DIAG_UID_NOT_FOUND if the id not found
1544  */
1545 static u8
_ctl_diag_get_bufftype(struct MPT3SAS_ADAPTER * ioc,u32 unique_id)1546 _ctl_diag_get_bufftype(struct MPT3SAS_ADAPTER *ioc, u32 unique_id)
1547 {
1548 	u8  index;
1549 
1550 	for (index = 0; index < MPI2_DIAG_BUF_TYPE_COUNT; index++) {
1551 		if (ioc->unique_id[index] == unique_id)
1552 			return index;
1553 	}
1554 
1555 	return MPT3_DIAG_UID_NOT_FOUND;
1556 }
1557 
1558 /**
1559  * _ctl_diag_register_2 - wrapper for registering diag buffer support
1560  * @ioc: per adapter object
1561  * @diag_register: the diag_register struct passed in from user space
1562  *
1563  */
1564 static long
_ctl_diag_register_2(struct MPT3SAS_ADAPTER * ioc,struct mpt3_diag_register * diag_register)1565 _ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1566 	struct mpt3_diag_register *diag_register)
1567 {
1568 	int rc, i;
1569 	void *request_data = NULL;
1570 	dma_addr_t request_data_dma;
1571 	u32 request_data_sz = 0;
1572 	Mpi2DiagBufferPostRequest_t *mpi_request;
1573 	Mpi2DiagBufferPostReply_t *mpi_reply;
1574 	u8 buffer_type;
1575 	u16 smid;
1576 	u16 ioc_status;
1577 	u32 ioc_state;
1578 	u8 issue_reset = 0;
1579 
1580 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1581 				 __func__));
1582 
1583 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1584 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1585 		ioc_err(ioc, "%s: failed due to ioc not operational\n",
1586 			__func__);
1587 		rc = -EAGAIN;
1588 		goto out;
1589 	}
1590 
1591 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1592 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
1593 		rc = -EAGAIN;
1594 		goto out;
1595 	}
1596 
1597 	buffer_type = diag_register->buffer_type;
1598 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1599 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1600 			__func__, buffer_type);
1601 		return -EPERM;
1602 	}
1603 
1604 	if (diag_register->unique_id == 0) {
1605 		ioc_err(ioc,
1606 		    "%s: Invalid UID(0x%08x), buffer_type(0x%02x)\n", __func__,
1607 		    diag_register->unique_id, buffer_type);
1608 		return -EINVAL;
1609 	}
1610 
1611 	if ((ioc->diag_buffer_status[buffer_type] &
1612 	    MPT3_DIAG_BUFFER_IS_APP_OWNED) &&
1613 	    !(ioc->diag_buffer_status[buffer_type] &
1614 	    MPT3_DIAG_BUFFER_IS_RELEASED)) {
1615 		ioc_err(ioc,
1616 		    "%s: buffer_type(0x%02x) is already registered by application with UID(0x%08x)\n",
1617 		    __func__, buffer_type, ioc->unique_id[buffer_type]);
1618 		return -EINVAL;
1619 	}
1620 
1621 	if (ioc->diag_buffer_status[buffer_type] &
1622 	    MPT3_DIAG_BUFFER_IS_REGISTERED) {
1623 		/*
1624 		 * If driver posts buffer initially, then an application wants
1625 		 * to Register that buffer (own it) without Releasing first,
1626 		 * the application Register command MUST have the same buffer
1627 		 * type and size in the Register command (obtained from the
1628 		 * Query command). Otherwise that Register command will be
1629 		 * failed. If the application has released the buffer but wants
1630 		 * to re-register it, it should be allowed as long as the
1631 		 * Unique-Id/Size match.
1632 		 */
1633 
1634 		if (ioc->unique_id[buffer_type] == MPT3DIAGBUFFUNIQUEID &&
1635 		    ioc->diag_buffer_sz[buffer_type] ==
1636 		    diag_register->requested_buffer_size) {
1637 
1638 			if (!(ioc->diag_buffer_status[buffer_type] &
1639 			     MPT3_DIAG_BUFFER_IS_RELEASED)) {
1640 				dctlprintk(ioc, ioc_info(ioc,
1641 				    "%s: diag_buffer (%d) ownership changed. old-ID(0x%08x), new-ID(0x%08x)\n",
1642 				    __func__, buffer_type,
1643 				    ioc->unique_id[buffer_type],
1644 				    diag_register->unique_id));
1645 
1646 				/*
1647 				 * Application wants to own the buffer with
1648 				 * the same size.
1649 				 */
1650 				ioc->unique_id[buffer_type] =
1651 				    diag_register->unique_id;
1652 				rc = 0; /* success */
1653 				goto out;
1654 			}
1655 		} else if (ioc->unique_id[buffer_type] !=
1656 		    MPT3DIAGBUFFUNIQUEID) {
1657 			if (ioc->unique_id[buffer_type] !=
1658 			    diag_register->unique_id ||
1659 			    ioc->diag_buffer_sz[buffer_type] !=
1660 			    diag_register->requested_buffer_size ||
1661 			    !(ioc->diag_buffer_status[buffer_type] &
1662 			    MPT3_DIAG_BUFFER_IS_RELEASED)) {
1663 				ioc_err(ioc,
1664 				    "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1665 				    __func__, buffer_type);
1666 				return -EINVAL;
1667 			}
1668 		} else {
1669 			ioc_err(ioc, "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1670 			    __func__, buffer_type);
1671 			return -EINVAL;
1672 		}
1673 	} else if (ioc->diag_buffer_status[buffer_type] &
1674 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
1675 
1676 		if (ioc->unique_id[buffer_type] != MPT3DIAGBUFFUNIQUEID ||
1677 		    ioc->diag_buffer_sz[buffer_type] !=
1678 		    diag_register->requested_buffer_size) {
1679 
1680 			ioc_err(ioc,
1681 			    "%s: already a buffer is allocated for buffer_type(0x%02x) of size %d bytes, so please try registering again with same size\n",
1682 			     __func__, buffer_type,
1683 			    ioc->diag_buffer_sz[buffer_type]);
1684 			return -EINVAL;
1685 		}
1686 	}
1687 
1688 	if (diag_register->requested_buffer_size % 4)  {
1689 		ioc_err(ioc, "%s: the requested_buffer_size is not 4 byte aligned\n",
1690 			__func__);
1691 		return -EINVAL;
1692 	}
1693 
1694 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1695 	if (!smid) {
1696 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
1697 		rc = -EAGAIN;
1698 		goto out;
1699 	}
1700 
1701 	rc = 0;
1702 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1703 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1704 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1705 	memset(mpi_request, 0, ioc->request_sz);
1706 	ioc->ctl_cmds.smid = smid;
1707 
1708 	request_data = ioc->diag_buffer[buffer_type];
1709 	request_data_sz = diag_register->requested_buffer_size;
1710 	ioc->unique_id[buffer_type] = diag_register->unique_id;
1711 	/* Reset ioc variables used for additional query commands */
1712 	ioc->reset_from_user = 0;
1713 	memset(&ioc->htb_rel, 0, sizeof(struct htb_rel_query));
1714 	ioc->diag_buffer_status[buffer_type] &=
1715 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1716 	memcpy(ioc->product_specific[buffer_type],
1717 	    diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1718 	ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1719 
1720 	if (request_data) {
1721 		request_data_dma = ioc->diag_buffer_dma[buffer_type];
1722 		if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1723 			dma_free_coherent(&ioc->pdev->dev,
1724 					ioc->diag_buffer_sz[buffer_type],
1725 					request_data, request_data_dma);
1726 			request_data = NULL;
1727 		}
1728 	}
1729 
1730 	if (request_data == NULL) {
1731 		ioc->diag_buffer_sz[buffer_type] = 0;
1732 		ioc->diag_buffer_dma[buffer_type] = 0;
1733 		request_data = dma_alloc_coherent(&ioc->pdev->dev,
1734 				request_data_sz, &request_data_dma, GFP_KERNEL);
1735 		if (request_data == NULL) {
1736 			ioc_err(ioc, "%s: failed allocating memory for diag buffers, requested size(%d)\n",
1737 				__func__, request_data_sz);
1738 			mpt3sas_base_free_smid(ioc, smid);
1739 			rc = -ENOMEM;
1740 			goto out;
1741 		}
1742 		ioc->diag_buffer[buffer_type] = request_data;
1743 		ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1744 		ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1745 	}
1746 
1747 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1748 	mpi_request->BufferType = diag_register->buffer_type;
1749 	mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1750 	mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1751 	mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1752 	mpi_request->VF_ID = 0; /* TODO */
1753 	mpi_request->VP_ID = 0;
1754 
1755 	dctlprintk(ioc,
1756 		   ioc_info(ioc, "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1757 			    __func__, request_data,
1758 			    (unsigned long long)request_data_dma,
1759 			    le32_to_cpu(mpi_request->BufferLength)));
1760 
1761 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1762 		mpi_request->ProductSpecific[i] =
1763 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
1764 
1765 	init_completion(&ioc->ctl_cmds.done);
1766 	ioc->put_smid_default(ioc, smid);
1767 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
1768 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1769 
1770 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1771 		mpt3sas_check_cmd_timeout(ioc,
1772 		    ioc->ctl_cmds.status, mpi_request,
1773 		    sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
1774 		goto issue_host_reset;
1775 	}
1776 
1777 	/* process the completed Reply Message Frame */
1778 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1779 		ioc_err(ioc, "%s: no reply message\n", __func__);
1780 		rc = -EFAULT;
1781 		goto out;
1782 	}
1783 
1784 	mpi_reply = ioc->ctl_cmds.reply;
1785 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1786 
1787 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1788 		ioc->diag_buffer_status[buffer_type] |=
1789 			MPT3_DIAG_BUFFER_IS_REGISTERED;
1790 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
1791 	} else {
1792 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1793 			 __func__,
1794 			 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1795 		rc = -EFAULT;
1796 	}
1797 
1798  issue_host_reset:
1799 	if (issue_reset)
1800 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
1801 
1802  out:
1803 
1804 	if (rc && request_data) {
1805 		dma_free_coherent(&ioc->pdev->dev, request_data_sz,
1806 		    request_data, request_data_dma);
1807 		ioc->diag_buffer[buffer_type] = NULL;
1808 		ioc->diag_buffer_status[buffer_type] &=
1809 		    ~MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1810 	}
1811 
1812 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1813 	return rc;
1814 }
1815 
1816 /**
1817  * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1818  * @ioc: per adapter object
1819  * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1820  *
1821  * This is called when command line option diag_buffer_enable is enabled
1822  * at driver load time.
1823  */
1824 void
mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER * ioc,u8 bits_to_register)1825 mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1826 {
1827 	struct mpt3_diag_register diag_register;
1828 	u32 ret_val;
1829 	u32 trace_buff_size = ioc->manu_pg11.HostTraceBufferMaxSizeKB<<10;
1830 	u32 min_trace_buff_size = 0;
1831 	u32 decr_trace_buff_size = 0;
1832 
1833 	memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1834 
1835 	if (bits_to_register & 1) {
1836 		ioc_info(ioc, "registering trace buffer support\n");
1837 		ioc->diag_trigger_master.MasterData =
1838 		    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1839 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1840 		diag_register.unique_id =
1841 		    (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
1842 		    (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
1843 
1844 		if (trace_buff_size != 0) {
1845 			diag_register.requested_buffer_size = trace_buff_size;
1846 			min_trace_buff_size =
1847 			    ioc->manu_pg11.HostTraceBufferMinSizeKB<<10;
1848 			decr_trace_buff_size =
1849 			    ioc->manu_pg11.HostTraceBufferDecrementSizeKB<<10;
1850 
1851 			if (min_trace_buff_size > trace_buff_size) {
1852 				/* The buff size is not set correctly */
1853 				ioc_err(ioc,
1854 				    "Min Trace Buff size (%d KB) greater than Max Trace Buff size (%d KB)\n",
1855 				     min_trace_buff_size>>10,
1856 				     trace_buff_size>>10);
1857 				ioc_err(ioc,
1858 				    "Using zero Min Trace Buff Size\n");
1859 				min_trace_buff_size = 0;
1860 			}
1861 
1862 			if (decr_trace_buff_size == 0) {
1863 				/*
1864 				 * retry the min size if decrement
1865 				 * is not available.
1866 				 */
1867 				decr_trace_buff_size =
1868 				    trace_buff_size - min_trace_buff_size;
1869 			}
1870 		} else {
1871 			/* register for 2MB buffers  */
1872 			diag_register.requested_buffer_size = 2 * (1024 * 1024);
1873 		}
1874 
1875 		do {
1876 			ret_val = _ctl_diag_register_2(ioc,  &diag_register);
1877 
1878 			if (ret_val == -ENOMEM && min_trace_buff_size &&
1879 			    (trace_buff_size - decr_trace_buff_size) >=
1880 			    min_trace_buff_size) {
1881 				/* adjust the buffer size */
1882 				trace_buff_size -= decr_trace_buff_size;
1883 				diag_register.requested_buffer_size =
1884 				    trace_buff_size;
1885 			} else
1886 				break;
1887 		} while (true);
1888 
1889 		if (ret_val == -ENOMEM)
1890 			ioc_err(ioc,
1891 			    "Cannot allocate trace buffer memory. Last memory tried = %d KB\n",
1892 			    diag_register.requested_buffer_size>>10);
1893 		else if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE]
1894 		    & MPT3_DIAG_BUFFER_IS_REGISTERED) {
1895 			ioc_info(ioc, "Trace buffer memory %d KB allocated\n",
1896 			    diag_register.requested_buffer_size>>10);
1897 			if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
1898 				ioc->diag_buffer_status[
1899 				    MPI2_DIAG_BUF_TYPE_TRACE] |=
1900 				    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
1901 		}
1902 	}
1903 
1904 	if (bits_to_register & 2) {
1905 		ioc_info(ioc, "registering snapshot buffer support\n");
1906 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1907 		/* register for 2MB buffers  */
1908 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1909 		diag_register.unique_id = 0x7075901;
1910 		_ctl_diag_register_2(ioc,  &diag_register);
1911 	}
1912 
1913 	if (bits_to_register & 4) {
1914 		ioc_info(ioc, "registering extended buffer support\n");
1915 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1916 		/* register for 2MB buffers  */
1917 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1918 		diag_register.unique_id = 0x7075901;
1919 		_ctl_diag_register_2(ioc,  &diag_register);
1920 	}
1921 }
1922 
1923 /**
1924  * _ctl_diag_register - application register with driver
1925  * @ioc: per adapter object
1926  * @arg: user space buffer containing ioctl content
1927  *
1928  * This will allow the driver to setup any required buffers that will be
1929  * needed by firmware to communicate with the driver.
1930  */
1931 static long
_ctl_diag_register(struct MPT3SAS_ADAPTER * ioc,void __user * arg)1932 _ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1933 {
1934 	struct mpt3_diag_register karg;
1935 	long rc;
1936 
1937 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1938 		pr_err("failure at %s:%d/%s()!\n",
1939 		    __FILE__, __LINE__, __func__);
1940 		return -EFAULT;
1941 	}
1942 
1943 	rc = _ctl_diag_register_2(ioc, &karg);
1944 
1945 	if (!rc && (ioc->diag_buffer_status[karg.buffer_type] &
1946 	    MPT3_DIAG_BUFFER_IS_REGISTERED))
1947 		ioc->diag_buffer_status[karg.buffer_type] |=
1948 		    MPT3_DIAG_BUFFER_IS_APP_OWNED;
1949 
1950 	return rc;
1951 }
1952 
1953 /**
1954  * _ctl_diag_unregister - application unregister with driver
1955  * @ioc: per adapter object
1956  * @arg: user space buffer containing ioctl content
1957  *
1958  * This will allow the driver to cleanup any memory allocated for diag
1959  * messages and to free up any resources.
1960  */
1961 static long
_ctl_diag_unregister(struct MPT3SAS_ADAPTER * ioc,void __user * arg)1962 _ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1963 {
1964 	struct mpt3_diag_unregister karg;
1965 	void *request_data;
1966 	dma_addr_t request_data_dma;
1967 	u32 request_data_sz;
1968 	u8 buffer_type;
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 
1976 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
1977 				 __func__));
1978 
1979 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
1980 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
1981 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
1982 		    __func__, karg.unique_id);
1983 		return -EINVAL;
1984 	}
1985 
1986 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1987 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
1988 			__func__, buffer_type);
1989 		return -EPERM;
1990 	}
1991 
1992 	if ((ioc->diag_buffer_status[buffer_type] &
1993 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1994 		ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
1995 			__func__, buffer_type);
1996 		return -EINVAL;
1997 	}
1998 	if ((ioc->diag_buffer_status[buffer_type] &
1999 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2000 		ioc_err(ioc, "%s: buffer_type(0x%02x) has not been released\n",
2001 			__func__, buffer_type);
2002 		return -EINVAL;
2003 	}
2004 
2005 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2006 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2007 			__func__, karg.unique_id);
2008 		return -EINVAL;
2009 	}
2010 
2011 	request_data = ioc->diag_buffer[buffer_type];
2012 	if (!request_data) {
2013 		ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2014 			__func__, buffer_type);
2015 		return -ENOMEM;
2016 	}
2017 
2018 	if (ioc->diag_buffer_status[buffer_type] &
2019 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED) {
2020 		ioc->unique_id[buffer_type] = MPT3DIAGBUFFUNIQUEID;
2021 		ioc->diag_buffer_status[buffer_type] &=
2022 		    ~MPT3_DIAG_BUFFER_IS_APP_OWNED;
2023 		ioc->diag_buffer_status[buffer_type] &=
2024 		    ~MPT3_DIAG_BUFFER_IS_REGISTERED;
2025 	} else {
2026 		request_data_sz = ioc->diag_buffer_sz[buffer_type];
2027 		request_data_dma = ioc->diag_buffer_dma[buffer_type];
2028 		dma_free_coherent(&ioc->pdev->dev, request_data_sz,
2029 				request_data, request_data_dma);
2030 		ioc->diag_buffer[buffer_type] = NULL;
2031 		ioc->diag_buffer_status[buffer_type] = 0;
2032 	}
2033 	return 0;
2034 }
2035 
2036 /**
2037  * _ctl_diag_query - query relevant info associated with diag buffers
2038  * @ioc: per adapter object
2039  * @arg: user space buffer containing ioctl content
2040  *
2041  * The application will send only buffer_type and unique_id.  Driver will
2042  * inspect unique_id first, if valid, fill in all the info.  If unique_id is
2043  * 0x00, the driver will return info specified by Buffer Type.
2044  */
2045 static long
_ctl_diag_query(struct MPT3SAS_ADAPTER * ioc,void __user * arg)2046 _ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2047 {
2048 	struct mpt3_diag_query karg;
2049 	void *request_data;
2050 	int i;
2051 	u8 buffer_type;
2052 
2053 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2054 		pr_err("failure at %s:%d/%s()!\n",
2055 		    __FILE__, __LINE__, __func__);
2056 		return -EFAULT;
2057 	}
2058 
2059 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2060 				 __func__));
2061 
2062 	karg.application_flags = 0;
2063 	buffer_type = karg.buffer_type;
2064 
2065 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2066 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2067 			__func__, buffer_type);
2068 		return -EPERM;
2069 	}
2070 
2071 	if (!(ioc->diag_buffer_status[buffer_type] &
2072 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED)) {
2073 		if ((ioc->diag_buffer_status[buffer_type] &
2074 		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2075 			ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2076 				__func__, buffer_type);
2077 			return -EINVAL;
2078 		}
2079 	}
2080 
2081 	if (karg.unique_id) {
2082 		if (karg.unique_id != ioc->unique_id[buffer_type]) {
2083 			ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2084 				__func__, karg.unique_id);
2085 			return -EINVAL;
2086 		}
2087 	}
2088 
2089 	request_data = ioc->diag_buffer[buffer_type];
2090 	if (!request_data) {
2091 		ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2092 			__func__, buffer_type);
2093 		return -ENOMEM;
2094 	}
2095 
2096 	if ((ioc->diag_buffer_status[buffer_type] &
2097 	    MPT3_DIAG_BUFFER_IS_REGISTERED))
2098 		karg.application_flags |= MPT3_APP_FLAGS_BUFFER_VALID;
2099 
2100 	if (!(ioc->diag_buffer_status[buffer_type] &
2101 	     MPT3_DIAG_BUFFER_IS_RELEASED))
2102 		karg.application_flags |= MPT3_APP_FLAGS_FW_BUFFER_ACCESS;
2103 
2104 	if (!(ioc->diag_buffer_status[buffer_type] &
2105 	    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED))
2106 		karg.application_flags |= MPT3_APP_FLAGS_DYNAMIC_BUFFER_ALLOC;
2107 
2108 	if ((ioc->diag_buffer_status[buffer_type] &
2109 	    MPT3_DIAG_BUFFER_IS_APP_OWNED))
2110 		karg.application_flags |= MPT3_APP_FLAGS_APP_OWNED;
2111 
2112 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2113 		karg.product_specific[i] =
2114 		    ioc->product_specific[buffer_type][i];
2115 
2116 	karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
2117 	karg.driver_added_buffer_size = 0;
2118 	karg.unique_id = ioc->unique_id[buffer_type];
2119 	karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
2120 
2121 	if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
2122 		ioc_err(ioc, "%s: unable to write mpt3_diag_query data @ %p\n",
2123 			__func__, arg);
2124 		return -EFAULT;
2125 	}
2126 	return 0;
2127 }
2128 
2129 /**
2130  * mpt3sas_send_diag_release - Diag Release Message
2131  * @ioc: per adapter object
2132  * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
2133  * @issue_reset: specifies whether host reset is required.
2134  *
2135  */
2136 int
mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER * ioc,u8 buffer_type,u8 * issue_reset)2137 mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
2138 	u8 *issue_reset)
2139 {
2140 	Mpi2DiagReleaseRequest_t *mpi_request;
2141 	Mpi2DiagReleaseReply_t *mpi_reply;
2142 	u16 smid;
2143 	u16 ioc_status;
2144 	u32 ioc_state;
2145 	int rc;
2146 	u8 reset_needed = 0;
2147 
2148 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2149 				 __func__));
2150 
2151 	rc = 0;
2152 	*issue_reset = 0;
2153 
2154 
2155 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2156 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2157 		if (ioc->diag_buffer_status[buffer_type] &
2158 		    MPT3_DIAG_BUFFER_IS_REGISTERED)
2159 			ioc->diag_buffer_status[buffer_type] |=
2160 			    MPT3_DIAG_BUFFER_IS_RELEASED;
2161 		dctlprintk(ioc,
2162 			   ioc_info(ioc, "%s: skipping due to FAULT state\n",
2163 				    __func__));
2164 		rc = -EAGAIN;
2165 		goto out;
2166 	}
2167 
2168 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2169 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
2170 		rc = -EAGAIN;
2171 		goto out;
2172 	}
2173 
2174 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2175 	if (!smid) {
2176 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2177 		rc = -EAGAIN;
2178 		goto out;
2179 	}
2180 
2181 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2182 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2183 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2184 	memset(mpi_request, 0, ioc->request_sz);
2185 	ioc->ctl_cmds.smid = smid;
2186 
2187 	mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
2188 	mpi_request->BufferType = buffer_type;
2189 	mpi_request->VF_ID = 0; /* TODO */
2190 	mpi_request->VP_ID = 0;
2191 
2192 	init_completion(&ioc->ctl_cmds.done);
2193 	ioc->put_smid_default(ioc, smid);
2194 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
2195 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2196 
2197 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2198 		mpt3sas_check_cmd_timeout(ioc,
2199 		    ioc->ctl_cmds.status, mpi_request,
2200 		    sizeof(Mpi2DiagReleaseRequest_t)/4, reset_needed);
2201 		*issue_reset = reset_needed;
2202 		rc = -EFAULT;
2203 		goto out;
2204 	}
2205 
2206 	/* process the completed Reply Message Frame */
2207 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2208 		ioc_err(ioc, "%s: no reply message\n", __func__);
2209 		rc = -EFAULT;
2210 		goto out;
2211 	}
2212 
2213 	mpi_reply = ioc->ctl_cmds.reply;
2214 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2215 
2216 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2217 		ioc->diag_buffer_status[buffer_type] |=
2218 		    MPT3_DIAG_BUFFER_IS_RELEASED;
2219 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
2220 	} else {
2221 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2222 			 __func__,
2223 			 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2224 		rc = -EFAULT;
2225 	}
2226 
2227  out:
2228 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2229 	return rc;
2230 }
2231 
2232 /**
2233  * _ctl_diag_release - request to send Diag Release Message to firmware
2234  * @ioc: ?
2235  * @arg: user space buffer containing ioctl content
2236  *
2237  * This allows ownership of the specified buffer to returned to the driver,
2238  * allowing an application to read the buffer without fear that firmware is
2239  * overwriting information in the buffer.
2240  */
2241 static long
_ctl_diag_release(struct MPT3SAS_ADAPTER * ioc,void __user * arg)2242 _ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2243 {
2244 	struct mpt3_diag_release karg;
2245 	void *request_data;
2246 	int rc;
2247 	u8 buffer_type;
2248 	u8 issue_reset = 0;
2249 
2250 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2251 		pr_err("failure at %s:%d/%s()!\n",
2252 		    __FILE__, __LINE__, __func__);
2253 		return -EFAULT;
2254 	}
2255 
2256 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2257 				 __func__));
2258 
2259 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2260 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2261 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2262 		    __func__, karg.unique_id);
2263 		return -EINVAL;
2264 	}
2265 
2266 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2267 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2268 			__func__, buffer_type);
2269 		return -EPERM;
2270 	}
2271 
2272 	if ((ioc->diag_buffer_status[buffer_type] &
2273 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2274 		ioc_err(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2275 			__func__, buffer_type);
2276 		return -EINVAL;
2277 	}
2278 
2279 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2280 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2281 			__func__, karg.unique_id);
2282 		return -EINVAL;
2283 	}
2284 
2285 	if (ioc->diag_buffer_status[buffer_type] &
2286 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
2287 		ioc_err(ioc, "%s: buffer_type(0x%02x) is already released\n",
2288 			__func__, buffer_type);
2289 		return -EINVAL;
2290 	}
2291 
2292 	request_data = ioc->diag_buffer[buffer_type];
2293 
2294 	if (!request_data) {
2295 		ioc_err(ioc, "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
2296 			__func__, buffer_type);
2297 		return -ENOMEM;
2298 	}
2299 
2300 	/* buffers were released by due to host reset */
2301 	if ((ioc->diag_buffer_status[buffer_type] &
2302 	    MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
2303 		ioc->diag_buffer_status[buffer_type] |=
2304 		    MPT3_DIAG_BUFFER_IS_RELEASED;
2305 		ioc->diag_buffer_status[buffer_type] &=
2306 		    ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
2307 		ioc_err(ioc, "%s: buffer_type(0x%02x) was released due to host reset\n",
2308 			__func__, buffer_type);
2309 		return 0;
2310 	}
2311 
2312 	rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
2313 
2314 	if (issue_reset)
2315 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2316 
2317 	return rc;
2318 }
2319 
2320 /**
2321  * _ctl_diag_read_buffer - request for copy of the diag buffer
2322  * @ioc: per adapter object
2323  * @arg: user space buffer containing ioctl content
2324  */
2325 static long
_ctl_diag_read_buffer(struct MPT3SAS_ADAPTER * ioc,void __user * arg)2326 _ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2327 {
2328 	struct mpt3_diag_read_buffer karg;
2329 	struct mpt3_diag_read_buffer __user *uarg = arg;
2330 	void *request_data, *diag_data;
2331 	Mpi2DiagBufferPostRequest_t *mpi_request;
2332 	Mpi2DiagBufferPostReply_t *mpi_reply;
2333 	int rc, i;
2334 	u8 buffer_type;
2335 	unsigned long request_size, copy_size;
2336 	u16 smid;
2337 	u16 ioc_status;
2338 	u8 issue_reset = 0;
2339 
2340 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2341 		pr_err("failure at %s:%d/%s()!\n",
2342 		    __FILE__, __LINE__, __func__);
2343 		return -EFAULT;
2344 	}
2345 
2346 	dctlprintk(ioc, ioc_info(ioc, "%s\n",
2347 				 __func__));
2348 
2349 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2350 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2351 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2352 		    __func__, karg.unique_id);
2353 		return -EINVAL;
2354 	}
2355 
2356 	if (!_ctl_diag_capability(ioc, buffer_type)) {
2357 		ioc_err(ioc, "%s: doesn't have capability for buffer_type(0x%02x)\n",
2358 			__func__, buffer_type);
2359 		return -EPERM;
2360 	}
2361 
2362 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
2363 		ioc_err(ioc, "%s: unique_id(0x%08x) is not registered\n",
2364 			__func__, karg.unique_id);
2365 		return -EINVAL;
2366 	}
2367 
2368 	request_data = ioc->diag_buffer[buffer_type];
2369 	if (!request_data) {
2370 		ioc_err(ioc, "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2371 			__func__, buffer_type);
2372 		return -ENOMEM;
2373 	}
2374 
2375 	request_size = ioc->diag_buffer_sz[buffer_type];
2376 
2377 	if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2378 		ioc_err(ioc, "%s: either the starting_offset or bytes_to_read are not 4 byte aligned\n",
2379 			__func__);
2380 		return -EINVAL;
2381 	}
2382 
2383 	if (karg.starting_offset > request_size)
2384 		return -EINVAL;
2385 
2386 	diag_data = (void *)(request_data + karg.starting_offset);
2387 	dctlprintk(ioc,
2388 		   ioc_info(ioc, "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2389 			    __func__, diag_data, karg.starting_offset,
2390 			    karg.bytes_to_read));
2391 
2392 	/* Truncate data on requests that are too large */
2393 	if ((diag_data + karg.bytes_to_read < diag_data) ||
2394 	    (diag_data + karg.bytes_to_read > request_data + request_size))
2395 		copy_size = request_size - karg.starting_offset;
2396 	else
2397 		copy_size = karg.bytes_to_read;
2398 
2399 	if (copy_to_user((void __user *)uarg->diagnostic_data,
2400 	    diag_data, copy_size)) {
2401 		ioc_err(ioc, "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2402 			__func__, diag_data);
2403 		return -EFAULT;
2404 	}
2405 
2406 	if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2407 		return 0;
2408 
2409 	dctlprintk(ioc,
2410 		   ioc_info(ioc, "%s: Reregister buffer_type(0x%02x)\n",
2411 			    __func__, buffer_type));
2412 	if ((ioc->diag_buffer_status[buffer_type] &
2413 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2414 		dctlprintk(ioc,
2415 			   ioc_info(ioc, "%s: buffer_type(0x%02x) is still registered\n",
2416 				    __func__, buffer_type));
2417 		return 0;
2418 	}
2419 	/* Get a free request frame and save the message context.
2420 	*/
2421 
2422 	if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2423 		ioc_err(ioc, "%s: ctl_cmd in use\n", __func__);
2424 		rc = -EAGAIN;
2425 		goto out;
2426 	}
2427 
2428 	smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2429 	if (!smid) {
2430 		ioc_err(ioc, "%s: failed obtaining a smid\n", __func__);
2431 		rc = -EAGAIN;
2432 		goto out;
2433 	}
2434 
2435 	rc = 0;
2436 	ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2437 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2438 	mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2439 	memset(mpi_request, 0, ioc->request_sz);
2440 	ioc->ctl_cmds.smid = smid;
2441 
2442 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2443 	mpi_request->BufferType = buffer_type;
2444 	mpi_request->BufferLength =
2445 	    cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2446 	mpi_request->BufferAddress =
2447 	    cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2448 	for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2449 		mpi_request->ProductSpecific[i] =
2450 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
2451 	mpi_request->VF_ID = 0; /* TODO */
2452 	mpi_request->VP_ID = 0;
2453 
2454 	init_completion(&ioc->ctl_cmds.done);
2455 	ioc->put_smid_default(ioc, smid);
2456 	wait_for_completion_timeout(&ioc->ctl_cmds.done,
2457 	    MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2458 
2459 	if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2460 		mpt3sas_check_cmd_timeout(ioc,
2461 		    ioc->ctl_cmds.status, mpi_request,
2462 		    sizeof(Mpi2DiagBufferPostRequest_t)/4, issue_reset);
2463 		goto issue_host_reset;
2464 	}
2465 
2466 	/* process the completed Reply Message Frame */
2467 	if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2468 		ioc_err(ioc, "%s: no reply message\n", __func__);
2469 		rc = -EFAULT;
2470 		goto out;
2471 	}
2472 
2473 	mpi_reply = ioc->ctl_cmds.reply;
2474 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2475 
2476 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2477 		ioc->diag_buffer_status[buffer_type] |=
2478 		    MPT3_DIAG_BUFFER_IS_REGISTERED;
2479 		ioc->diag_buffer_status[buffer_type] &=
2480 		    ~MPT3_DIAG_BUFFER_IS_RELEASED;
2481 		dctlprintk(ioc, ioc_info(ioc, "%s: success\n", __func__));
2482 	} else {
2483 		ioc_info(ioc, "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2484 			 __func__, ioc_status,
2485 			 le32_to_cpu(mpi_reply->IOCLogInfo));
2486 		rc = -EFAULT;
2487 	}
2488 
2489  issue_host_reset:
2490 	if (issue_reset)
2491 		mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
2492 
2493  out:
2494 
2495 	ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2496 	return rc;
2497 }
2498 
2499 /**
2500  * _ctl_addnl_diag_query - query relevant info associated with diag buffers
2501  * @ioc: per adapter object
2502  * @arg: user space buffer containing ioctl content
2503  *
2504  * The application will send only unique_id.  Driver will
2505  * inspect unique_id first, if valid, fill the details related to cause
2506  * for diag buffer release.
2507  */
2508 static long
_ctl_addnl_diag_query(struct MPT3SAS_ADAPTER * ioc,void __user * arg)2509 _ctl_addnl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2510 {
2511 	struct mpt3_addnl_diag_query karg;
2512 	u32 buffer_type = 0;
2513 
2514 	if (copy_from_user(&karg, arg, sizeof(karg))) {
2515 		pr_err("%s: failure at %s:%d/%s()!\n",
2516 		    ioc->name, __FILE__, __LINE__, __func__);
2517 		return -EFAULT;
2518 	}
2519 	dctlprintk(ioc, ioc_info(ioc, "%s\n",  __func__));
2520 	if (karg.unique_id == 0) {
2521 		ioc_err(ioc, "%s: unique_id is(0x%08x)\n",
2522 		    __func__, karg.unique_id);
2523 		return -EPERM;
2524 	}
2525 	buffer_type = _ctl_diag_get_bufftype(ioc, karg.unique_id);
2526 	if (buffer_type == MPT3_DIAG_UID_NOT_FOUND) {
2527 		ioc_err(ioc, "%s: buffer with unique_id(0x%08x) not found\n",
2528 		    __func__, karg.unique_id);
2529 		return -EPERM;
2530 	}
2531 	memset(&karg.rel_query, 0, sizeof(karg.rel_query));
2532 	if ((ioc->diag_buffer_status[buffer_type] &
2533 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2534 		ioc_info(ioc, "%s: buffer_type(0x%02x) is not registered\n",
2535 		    __func__, buffer_type);
2536 		goto out;
2537 	}
2538 	if ((ioc->diag_buffer_status[buffer_type] &
2539 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2540 		ioc_err(ioc, "%s: buffer_type(0x%02x) is not released\n",
2541 		    __func__, buffer_type);
2542 		return -EPERM;
2543 	}
2544 	memcpy(&karg.rel_query, &ioc->htb_rel, sizeof(karg.rel_query));
2545 out:
2546 	if (copy_to_user(arg, &karg, sizeof(struct mpt3_addnl_diag_query))) {
2547 		ioc_err(ioc, "%s: unable to write mpt3_addnl_diag_query data @ %p\n",
2548 		    __func__, arg);
2549 		return -EFAULT;
2550 	}
2551 	return 0;
2552 }
2553 
2554 #ifdef CONFIG_COMPAT
2555 /**
2556  * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2557  * @ioc: per adapter object
2558  * @cmd: ioctl opcode
2559  * @arg: (struct mpt3_ioctl_command32)
2560  *
2561  * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2562  */
2563 static long
_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER * ioc,unsigned cmd,void __user * arg)2564 _ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2565 	void __user *arg)
2566 {
2567 	struct mpt3_ioctl_command32 karg32;
2568 	struct mpt3_ioctl_command32 __user *uarg;
2569 	struct mpt3_ioctl_command karg;
2570 
2571 	if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2572 		return -EINVAL;
2573 
2574 	uarg = (struct mpt3_ioctl_command32 __user *) arg;
2575 
2576 	if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2577 		pr_err("failure at %s:%d/%s()!\n",
2578 		    __FILE__, __LINE__, __func__);
2579 		return -EFAULT;
2580 	}
2581 
2582 	memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2583 	karg.hdr.ioc_number = karg32.hdr.ioc_number;
2584 	karg.hdr.port_number = karg32.hdr.port_number;
2585 	karg.hdr.max_data_size = karg32.hdr.max_data_size;
2586 	karg.timeout = karg32.timeout;
2587 	karg.max_reply_bytes = karg32.max_reply_bytes;
2588 	karg.data_in_size = karg32.data_in_size;
2589 	karg.data_out_size = karg32.data_out_size;
2590 	karg.max_sense_bytes = karg32.max_sense_bytes;
2591 	karg.data_sge_offset = karg32.data_sge_offset;
2592 	karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2593 	karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2594 	karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2595 	karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2596 	return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2597 }
2598 #endif
2599 
2600 /**
2601  * _ctl_ioctl_main - main ioctl entry point
2602  * @file:  (struct file)
2603  * @cmd:  ioctl opcode
2604  * @arg:  user space data buffer
2605  * @compat:  handles 32 bit applications in 64bit os
2606  * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
2607  * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
2608  */
2609 static long
_ctl_ioctl_main(struct file * file,unsigned int cmd,void __user * arg,u8 compat,u16 mpi_version)2610 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2611 	u8 compat, u16 mpi_version)
2612 {
2613 	struct MPT3SAS_ADAPTER *ioc;
2614 	struct mpt3_ioctl_header ioctl_header;
2615 	enum block_state state;
2616 	long ret = -ENOIOCTLCMD;
2617 
2618 	/* get IOCTL header */
2619 	if (copy_from_user(&ioctl_header, (char __user *)arg,
2620 	    sizeof(struct mpt3_ioctl_header))) {
2621 		pr_err("failure at %s:%d/%s()!\n",
2622 		    __FILE__, __LINE__, __func__);
2623 		return -EFAULT;
2624 	}
2625 
2626 	if (_ctl_verify_adapter(ioctl_header.ioc_number,
2627 				&ioc, mpi_version) == -1 || !ioc)
2628 		return -ENODEV;
2629 
2630 	/* pci_access_mutex lock acquired by ioctl path */
2631 	mutex_lock(&ioc->pci_access_mutex);
2632 
2633 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
2634 	    ioc->is_driver_loading || ioc->remove_host) {
2635 		ret = -EAGAIN;
2636 		goto out_unlock_pciaccess;
2637 	}
2638 
2639 	state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2640 	if (state == NON_BLOCKING) {
2641 		if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2642 			ret = -EAGAIN;
2643 			goto out_unlock_pciaccess;
2644 		}
2645 	} else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2646 		ret = -ERESTARTSYS;
2647 		goto out_unlock_pciaccess;
2648 	}
2649 
2650 
2651 	switch (cmd) {
2652 	case MPT3IOCINFO:
2653 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2654 			ret = _ctl_getiocinfo(ioc, arg);
2655 		break;
2656 #ifdef CONFIG_COMPAT
2657 	case MPT3COMMAND32:
2658 #endif
2659 	case MPT3COMMAND:
2660 	{
2661 		struct mpt3_ioctl_command __user *uarg;
2662 		struct mpt3_ioctl_command karg;
2663 
2664 #ifdef CONFIG_COMPAT
2665 		if (compat) {
2666 			ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2667 			break;
2668 		}
2669 #endif
2670 		if (copy_from_user(&karg, arg, sizeof(karg))) {
2671 			pr_err("failure at %s:%d/%s()!\n",
2672 			    __FILE__, __LINE__, __func__);
2673 			ret = -EFAULT;
2674 			break;
2675 		}
2676 
2677 		if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2678 			ret = -EINVAL;
2679 			break;
2680 		}
2681 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2682 			uarg = arg;
2683 			ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2684 		}
2685 		break;
2686 	}
2687 	case MPT3EVENTQUERY:
2688 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2689 			ret = _ctl_eventquery(ioc, arg);
2690 		break;
2691 	case MPT3EVENTENABLE:
2692 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2693 			ret = _ctl_eventenable(ioc, arg);
2694 		break;
2695 	case MPT3EVENTREPORT:
2696 		ret = _ctl_eventreport(ioc, arg);
2697 		break;
2698 	case MPT3HARDRESET:
2699 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2700 			ret = _ctl_do_reset(ioc, arg);
2701 		break;
2702 	case MPT3BTDHMAPPING:
2703 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2704 			ret = _ctl_btdh_mapping(ioc, arg);
2705 		break;
2706 	case MPT3DIAGREGISTER:
2707 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2708 			ret = _ctl_diag_register(ioc, arg);
2709 		break;
2710 	case MPT3DIAGUNREGISTER:
2711 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2712 			ret = _ctl_diag_unregister(ioc, arg);
2713 		break;
2714 	case MPT3DIAGQUERY:
2715 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2716 			ret = _ctl_diag_query(ioc, arg);
2717 		break;
2718 	case MPT3DIAGRELEASE:
2719 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2720 			ret = _ctl_diag_release(ioc, arg);
2721 		break;
2722 	case MPT3DIAGREADBUFFER:
2723 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2724 			ret = _ctl_diag_read_buffer(ioc, arg);
2725 		break;
2726 	case MPT3ADDNLDIAGQUERY:
2727 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_addnl_diag_query))
2728 			ret = _ctl_addnl_diag_query(ioc, arg);
2729 		break;
2730 	default:
2731 		dctlprintk(ioc,
2732 			   ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2733 				    cmd));
2734 		break;
2735 	}
2736 
2737 	mutex_unlock(&ioc->ctl_cmds.mutex);
2738 out_unlock_pciaccess:
2739 	mutex_unlock(&ioc->pci_access_mutex);
2740 	return ret;
2741 }
2742 
2743 /**
2744  * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
2745  * @file: (struct file)
2746  * @cmd: ioctl opcode
2747  * @arg: ?
2748  */
2749 static long
_ctl_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2750 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2751 {
2752 	long ret;
2753 
2754 	/* pass MPI25_VERSION | MPI26_VERSION value,
2755 	 * to indicate that this ioctl cmd
2756 	 * came from mpt3ctl ioctl device.
2757 	 */
2758 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2759 		MPI25_VERSION | MPI26_VERSION);
2760 	return ret;
2761 }
2762 
2763 /**
2764  * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
2765  * @file: (struct file)
2766  * @cmd: ioctl opcode
2767  * @arg: ?
2768  */
2769 static long
_ctl_mpt2_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2770 _ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2771 {
2772 	long ret;
2773 
2774 	/* pass MPI2_VERSION value, to indicate that this ioctl cmd
2775 	 * came from mpt2ctl ioctl device.
2776 	 */
2777 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2778 	return ret;
2779 }
2780 #ifdef CONFIG_COMPAT
2781 /**
2782  * _ctl_ioctl_compat - main ioctl entry point (compat)
2783  * @file: ?
2784  * @cmd: ?
2785  * @arg: ?
2786  *
2787  * This routine handles 32 bit applications in 64bit os.
2788  */
2789 static long
_ctl_ioctl_compat(struct file * file,unsigned cmd,unsigned long arg)2790 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2791 {
2792 	long ret;
2793 
2794 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2795 		MPI25_VERSION | MPI26_VERSION);
2796 	return ret;
2797 }
2798 
2799 /**
2800  * _ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
2801  * @file: ?
2802  * @cmd: ?
2803  * @arg: ?
2804  *
2805  * This routine handles 32 bit applications in 64bit os.
2806  */
2807 static long
_ctl_mpt2_ioctl_compat(struct file * file,unsigned cmd,unsigned long arg)2808 _ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2809 {
2810 	long ret;
2811 
2812 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
2813 	return ret;
2814 }
2815 #endif
2816 
2817 /* scsi host attributes */
2818 /**
2819  * version_fw_show - firmware version
2820  * @cdev: pointer to embedded class device
2821  * @attr: ?
2822  * @buf: the buffer returned
2823  *
2824  * A sysfs 'read-only' shost attribute.
2825  */
2826 static ssize_t
version_fw_show(struct device * cdev,struct device_attribute * attr,char * buf)2827 version_fw_show(struct device *cdev, struct device_attribute *attr,
2828 	char *buf)
2829 {
2830 	struct Scsi_Host *shost = class_to_shost(cdev);
2831 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2832 
2833 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2834 	    (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2835 	    (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2836 	    (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2837 	    ioc->facts.FWVersion.Word & 0x000000FF);
2838 }
2839 static DEVICE_ATTR_RO(version_fw);
2840 
2841 /**
2842  * version_bios_show - bios version
2843  * @cdev: pointer to embedded class device
2844  * @attr: ?
2845  * @buf: the buffer returned
2846  *
2847  * A sysfs 'read-only' shost attribute.
2848  */
2849 static ssize_t
version_bios_show(struct device * cdev,struct device_attribute * attr,char * buf)2850 version_bios_show(struct device *cdev, struct device_attribute *attr,
2851 	char *buf)
2852 {
2853 	struct Scsi_Host *shost = class_to_shost(cdev);
2854 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2855 
2856 	u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2857 
2858 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2859 	    (version & 0xFF000000) >> 24,
2860 	    (version & 0x00FF0000) >> 16,
2861 	    (version & 0x0000FF00) >> 8,
2862 	    version & 0x000000FF);
2863 }
2864 static DEVICE_ATTR_RO(version_bios);
2865 
2866 /**
2867  * version_mpi_show - MPI (message passing interface) version
2868  * @cdev: pointer to embedded class device
2869  * @attr: ?
2870  * @buf: the buffer returned
2871  *
2872  * A sysfs 'read-only' shost attribute.
2873  */
2874 static ssize_t
version_mpi_show(struct device * cdev,struct device_attribute * attr,char * buf)2875 version_mpi_show(struct device *cdev, struct device_attribute *attr,
2876 	char *buf)
2877 {
2878 	struct Scsi_Host *shost = class_to_shost(cdev);
2879 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2880 
2881 	return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2882 	    ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2883 }
2884 static DEVICE_ATTR_RO(version_mpi);
2885 
2886 /**
2887  * version_product_show - product name
2888  * @cdev: pointer to embedded class device
2889  * @attr: ?
2890  * @buf: the buffer returned
2891  *
2892  * A sysfs 'read-only' shost attribute.
2893  */
2894 static ssize_t
version_product_show(struct device * cdev,struct device_attribute * attr,char * buf)2895 version_product_show(struct device *cdev, struct device_attribute *attr,
2896 	char *buf)
2897 {
2898 	struct Scsi_Host *shost = class_to_shost(cdev);
2899 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2900 
2901 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2902 }
2903 static DEVICE_ATTR_RO(version_product);
2904 
2905 /**
2906  * version_nvdata_persistent_show - ndvata persistent version
2907  * @cdev: pointer to embedded class device
2908  * @attr: ?
2909  * @buf: the buffer returned
2910  *
2911  * A sysfs 'read-only' shost attribute.
2912  */
2913 static ssize_t
version_nvdata_persistent_show(struct device * cdev,struct device_attribute * attr,char * buf)2914 version_nvdata_persistent_show(struct device *cdev,
2915 	struct device_attribute *attr, char *buf)
2916 {
2917 	struct Scsi_Host *shost = class_to_shost(cdev);
2918 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2919 
2920 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2921 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2922 }
2923 static DEVICE_ATTR_RO(version_nvdata_persistent);
2924 
2925 /**
2926  * version_nvdata_default_show - nvdata default version
2927  * @cdev: pointer to embedded class device
2928  * @attr: ?
2929  * @buf: the buffer returned
2930  *
2931  * A sysfs 'read-only' shost attribute.
2932  */
2933 static ssize_t
version_nvdata_default_show(struct device * cdev,struct device_attribute * attr,char * buf)2934 version_nvdata_default_show(struct device *cdev, struct device_attribute
2935 	*attr, char *buf)
2936 {
2937 	struct Scsi_Host *shost = class_to_shost(cdev);
2938 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2939 
2940 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2941 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2942 }
2943 static DEVICE_ATTR_RO(version_nvdata_default);
2944 
2945 /**
2946  * board_name_show - board name
2947  * @cdev: pointer to embedded class device
2948  * @attr: ?
2949  * @buf: the buffer returned
2950  *
2951  * A sysfs 'read-only' shost attribute.
2952  */
2953 static ssize_t
board_name_show(struct device * cdev,struct device_attribute * attr,char * buf)2954 board_name_show(struct device *cdev, struct device_attribute *attr,
2955 	char *buf)
2956 {
2957 	struct Scsi_Host *shost = class_to_shost(cdev);
2958 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2959 
2960 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2961 }
2962 static DEVICE_ATTR_RO(board_name);
2963 
2964 /**
2965  * board_assembly_show - board assembly name
2966  * @cdev: pointer to embedded class device
2967  * @attr: ?
2968  * @buf: the buffer returned
2969  *
2970  * A sysfs 'read-only' shost attribute.
2971  */
2972 static ssize_t
board_assembly_show(struct device * cdev,struct device_attribute * attr,char * buf)2973 board_assembly_show(struct device *cdev, struct device_attribute *attr,
2974 	char *buf)
2975 {
2976 	struct Scsi_Host *shost = class_to_shost(cdev);
2977 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2978 
2979 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2980 }
2981 static DEVICE_ATTR_RO(board_assembly);
2982 
2983 /**
2984  * board_tracer_show - board tracer number
2985  * @cdev: pointer to embedded class device
2986  * @attr: ?
2987  * @buf: the buffer returned
2988  *
2989  * A sysfs 'read-only' shost attribute.
2990  */
2991 static ssize_t
board_tracer_show(struct device * cdev,struct device_attribute * attr,char * buf)2992 board_tracer_show(struct device *cdev, struct device_attribute *attr,
2993 	char *buf)
2994 {
2995 	struct Scsi_Host *shost = class_to_shost(cdev);
2996 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2997 
2998 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2999 }
3000 static DEVICE_ATTR_RO(board_tracer);
3001 
3002 /**
3003  * io_delay_show - io missing delay
3004  * @cdev: pointer to embedded class device
3005  * @attr: ?
3006  * @buf: the buffer returned
3007  *
3008  * This is for firmware implemention for deboucing device
3009  * removal events.
3010  *
3011  * A sysfs 'read-only' shost attribute.
3012  */
3013 static ssize_t
io_delay_show(struct device * cdev,struct device_attribute * attr,char * buf)3014 io_delay_show(struct device *cdev, struct device_attribute *attr,
3015 	char *buf)
3016 {
3017 	struct Scsi_Host *shost = class_to_shost(cdev);
3018 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3019 
3020 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
3021 }
3022 static DEVICE_ATTR_RO(io_delay);
3023 
3024 /**
3025  * device_delay_show - device missing delay
3026  * @cdev: pointer to embedded class device
3027  * @attr: ?
3028  * @buf: the buffer returned
3029  *
3030  * This is for firmware implemention for deboucing device
3031  * removal events.
3032  *
3033  * A sysfs 'read-only' shost attribute.
3034  */
3035 static ssize_t
device_delay_show(struct device * cdev,struct device_attribute * attr,char * buf)3036 device_delay_show(struct device *cdev, struct device_attribute *attr,
3037 	char *buf)
3038 {
3039 	struct Scsi_Host *shost = class_to_shost(cdev);
3040 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3041 
3042 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
3043 }
3044 static DEVICE_ATTR_RO(device_delay);
3045 
3046 /**
3047  * fw_queue_depth_show - global credits
3048  * @cdev: pointer to embedded class device
3049  * @attr: ?
3050  * @buf: the buffer returned
3051  *
3052  * This is firmware queue depth limit
3053  *
3054  * A sysfs 'read-only' shost attribute.
3055  */
3056 static ssize_t
fw_queue_depth_show(struct device * cdev,struct device_attribute * attr,char * buf)3057 fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
3058 	char *buf)
3059 {
3060 	struct Scsi_Host *shost = class_to_shost(cdev);
3061 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3062 
3063 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
3064 }
3065 static DEVICE_ATTR_RO(fw_queue_depth);
3066 
3067 /**
3068  * host_sas_address_show - sas address
3069  * @cdev: pointer to embedded class device
3070  * @attr: ?
3071  * @buf: the buffer returned
3072  *
3073  * This is the controller sas address
3074  *
3075  * A sysfs 'read-only' shost attribute.
3076  */
3077 static ssize_t
host_sas_address_show(struct device * cdev,struct device_attribute * attr,char * buf)3078 host_sas_address_show(struct device *cdev, struct device_attribute *attr,
3079 	char *buf)
3080 
3081 {
3082 	struct Scsi_Host *shost = class_to_shost(cdev);
3083 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3084 
3085 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3086 	    (unsigned long long)ioc->sas_hba.sas_address);
3087 }
3088 static DEVICE_ATTR_RO(host_sas_address);
3089 
3090 /**
3091  * logging_level_show - logging level
3092  * @cdev: pointer to embedded class device
3093  * @attr: ?
3094  * @buf: the buffer returned
3095  *
3096  * A sysfs 'read/write' shost attribute.
3097  */
3098 static ssize_t
logging_level_show(struct device * cdev,struct device_attribute * attr,char * buf)3099 logging_level_show(struct device *cdev, struct device_attribute *attr,
3100 	char *buf)
3101 {
3102 	struct Scsi_Host *shost = class_to_shost(cdev);
3103 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3104 
3105 	return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
3106 }
3107 static ssize_t
logging_level_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3108 logging_level_store(struct device *cdev, struct device_attribute *attr,
3109 	const char *buf, size_t count)
3110 {
3111 	struct Scsi_Host *shost = class_to_shost(cdev);
3112 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3113 	int val = 0;
3114 
3115 	if (sscanf(buf, "%x", &val) != 1)
3116 		return -EINVAL;
3117 
3118 	ioc->logging_level = val;
3119 	ioc_info(ioc, "logging_level=%08xh\n",
3120 		 ioc->logging_level);
3121 	return strlen(buf);
3122 }
3123 static DEVICE_ATTR_RW(logging_level);
3124 
3125 /**
3126  * fwfault_debug_show - show/store fwfault_debug
3127  * @cdev: pointer to embedded class device
3128  * @attr: ?
3129  * @buf: the buffer returned
3130  *
3131  * mpt3sas_fwfault_debug is command line option
3132  * A sysfs 'read/write' shost attribute.
3133  */
3134 static ssize_t
fwfault_debug_show(struct device * cdev,struct device_attribute * attr,char * buf)3135 fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
3136 	char *buf)
3137 {
3138 	struct Scsi_Host *shost = class_to_shost(cdev);
3139 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3140 
3141 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
3142 }
3143 static ssize_t
fwfault_debug_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3144 fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
3145 	const char *buf, size_t count)
3146 {
3147 	struct Scsi_Host *shost = class_to_shost(cdev);
3148 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3149 	int val = 0;
3150 
3151 	if (sscanf(buf, "%d", &val) != 1)
3152 		return -EINVAL;
3153 
3154 	ioc->fwfault_debug = val;
3155 	ioc_info(ioc, "fwfault_debug=%d\n",
3156 		 ioc->fwfault_debug);
3157 	return strlen(buf);
3158 }
3159 static DEVICE_ATTR_RW(fwfault_debug);
3160 
3161 /**
3162  * ioc_reset_count_show - ioc reset count
3163  * @cdev: pointer to embedded class device
3164  * @attr: ?
3165  * @buf: the buffer returned
3166  *
3167  * This is firmware queue depth limit
3168  *
3169  * A sysfs 'read-only' shost attribute.
3170  */
3171 static ssize_t
ioc_reset_count_show(struct device * cdev,struct device_attribute * attr,char * buf)3172 ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
3173 	char *buf)
3174 {
3175 	struct Scsi_Host *shost = class_to_shost(cdev);
3176 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3177 
3178 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
3179 }
3180 static DEVICE_ATTR_RO(ioc_reset_count);
3181 
3182 /**
3183  * reply_queue_count_show - number of reply queues
3184  * @cdev: pointer to embedded class device
3185  * @attr: ?
3186  * @buf: the buffer returned
3187  *
3188  * This is number of reply queues
3189  *
3190  * A sysfs 'read-only' shost attribute.
3191  */
3192 static ssize_t
reply_queue_count_show(struct device * cdev,struct device_attribute * attr,char * buf)3193 reply_queue_count_show(struct device *cdev,
3194 	struct device_attribute *attr, char *buf)
3195 {
3196 	u8 reply_queue_count;
3197 	struct Scsi_Host *shost = class_to_shost(cdev);
3198 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3199 
3200 	if ((ioc->facts.IOCCapabilities &
3201 	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
3202 		reply_queue_count = ioc->reply_queue_count;
3203 	else
3204 		reply_queue_count = 1;
3205 
3206 	return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
3207 }
3208 static DEVICE_ATTR_RO(reply_queue_count);
3209 
3210 /**
3211  * BRM_status_show - Backup Rail Monitor Status
3212  * @cdev: pointer to embedded class device
3213  * @attr: ?
3214  * @buf: the buffer returned
3215  *
3216  * This is number of reply queues
3217  *
3218  * A sysfs 'read-only' shost attribute.
3219  */
3220 static ssize_t
BRM_status_show(struct device * cdev,struct device_attribute * attr,char * buf)3221 BRM_status_show(struct device *cdev, struct device_attribute *attr,
3222 	char *buf)
3223 {
3224 	struct Scsi_Host *shost = class_to_shost(cdev);
3225 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3226 	Mpi2IOUnitPage3_t io_unit_pg3;
3227 	Mpi2ConfigReply_t mpi_reply;
3228 	u16 backup_rail_monitor_status = 0;
3229 	u16 ioc_status;
3230 	int sz;
3231 	ssize_t rc = 0;
3232 
3233 	if (!ioc->is_warpdrive) {
3234 		ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
3235 			__func__);
3236 		return 0;
3237 	}
3238 	/* pci_access_mutex lock acquired by sysfs show path */
3239 	mutex_lock(&ioc->pci_access_mutex);
3240 	if (ioc->pci_error_recovery || ioc->remove_host)
3241 		goto out;
3242 
3243 	sz = sizeof(io_unit_pg3);
3244 	memset(&io_unit_pg3, 0, sz);
3245 
3246 	if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, &io_unit_pg3, sz) !=
3247 	    0) {
3248 		ioc_err(ioc, "%s: failed reading iounit_pg3\n",
3249 			__func__);
3250 		rc = -EINVAL;
3251 		goto out;
3252 	}
3253 
3254 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3255 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3256 		ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
3257 			__func__, ioc_status);
3258 		rc = -EINVAL;
3259 		goto out;
3260 	}
3261 
3262 	if (io_unit_pg3.GPIOCount < 25) {
3263 		ioc_err(ioc, "%s: iounit_pg3.GPIOCount less than 25 entries, detected (%d) entries\n",
3264 			__func__, io_unit_pg3.GPIOCount);
3265 		rc = -EINVAL;
3266 		goto out;
3267 	}
3268 
3269 	/* BRM status is in bit zero of GPIOVal[24] */
3270 	backup_rail_monitor_status = le16_to_cpu(io_unit_pg3.GPIOVal[24]);
3271 	rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
3272 
3273  out:
3274 	mutex_unlock(&ioc->pci_access_mutex);
3275 	return rc;
3276 }
3277 static DEVICE_ATTR_RO(BRM_status);
3278 
3279 struct DIAG_BUFFER_START {
3280 	__le32	Size;
3281 	__le32	DiagVersion;
3282 	u8	BufferType;
3283 	u8	Reserved[3];
3284 	__le32	Reserved1;
3285 	__le32	Reserved2;
3286 	__le32	Reserved3;
3287 };
3288 
3289 /**
3290  * host_trace_buffer_size_show - host buffer size (trace only)
3291  * @cdev: pointer to embedded class device
3292  * @attr: ?
3293  * @buf: the buffer returned
3294  *
3295  * A sysfs 'read-only' shost attribute.
3296  */
3297 static ssize_t
host_trace_buffer_size_show(struct device * cdev,struct device_attribute * attr,char * buf)3298 host_trace_buffer_size_show(struct device *cdev,
3299 	struct device_attribute *attr, char *buf)
3300 {
3301 	struct Scsi_Host *shost = class_to_shost(cdev);
3302 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3303 	u32 size = 0;
3304 	struct DIAG_BUFFER_START *request_data;
3305 
3306 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3307 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3308 			__func__);
3309 		return 0;
3310 	}
3311 
3312 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3313 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3314 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3315 			__func__);
3316 		return 0;
3317 	}
3318 
3319 	request_data = (struct DIAG_BUFFER_START *)
3320 	    ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
3321 	if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
3322 	    le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
3323 	    le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
3324 	    le32_to_cpu(request_data->Reserved3) == 0x4742444c)
3325 		size = le32_to_cpu(request_data->Size);
3326 
3327 	ioc->ring_buffer_sz = size;
3328 	return snprintf(buf, PAGE_SIZE, "%d\n", size);
3329 }
3330 static DEVICE_ATTR_RO(host_trace_buffer_size);
3331 
3332 /**
3333  * host_trace_buffer_show - firmware ring buffer (trace only)
3334  * @cdev: pointer to embedded class device
3335  * @attr: ?
3336  * @buf: the buffer returned
3337  *
3338  * A sysfs 'read/write' shost attribute.
3339  *
3340  * You will only be able to read 4k bytes of ring buffer at a time.
3341  * In order to read beyond 4k bytes, you will have to write out the
3342  * offset to the same attribute, it will move the pointer.
3343  */
3344 static ssize_t
host_trace_buffer_show(struct device * cdev,struct device_attribute * attr,char * buf)3345 host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
3346 	char *buf)
3347 {
3348 	struct Scsi_Host *shost = class_to_shost(cdev);
3349 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3350 	void *request_data;
3351 	u32 size;
3352 
3353 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3354 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3355 			__func__);
3356 		return 0;
3357 	}
3358 
3359 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3360 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3361 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3362 			__func__);
3363 		return 0;
3364 	}
3365 
3366 	if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3367 		return 0;
3368 
3369 	size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3370 	size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3371 	request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3372 	memcpy(buf, request_data, size);
3373 	return size;
3374 }
3375 
3376 static ssize_t
host_trace_buffer_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3377 host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
3378 	const char *buf, size_t count)
3379 {
3380 	struct Scsi_Host *shost = class_to_shost(cdev);
3381 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3382 	int val = 0;
3383 
3384 	if (sscanf(buf, "%d", &val) != 1)
3385 		return -EINVAL;
3386 
3387 	ioc->ring_buffer_offset = val;
3388 	return strlen(buf);
3389 }
3390 static DEVICE_ATTR_RW(host_trace_buffer);
3391 
3392 
3393 /*****************************************/
3394 
3395 /**
3396  * host_trace_buffer_enable_show - firmware ring buffer (trace only)
3397  * @cdev: pointer to embedded class device
3398  * @attr: ?
3399  * @buf: the buffer returned
3400  *
3401  * A sysfs 'read/write' shost attribute.
3402  *
3403  * This is a mechnism to post/release host_trace_buffers
3404  */
3405 static ssize_t
host_trace_buffer_enable_show(struct device * cdev,struct device_attribute * attr,char * buf)3406 host_trace_buffer_enable_show(struct device *cdev,
3407 	struct device_attribute *attr, char *buf)
3408 {
3409 	struct Scsi_Host *shost = class_to_shost(cdev);
3410 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3411 
3412 	if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3413 	   ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3414 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3415 		return snprintf(buf, PAGE_SIZE, "off\n");
3416 	else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3417 	    MPT3_DIAG_BUFFER_IS_RELEASED))
3418 		return snprintf(buf, PAGE_SIZE, "release\n");
3419 	else
3420 		return snprintf(buf, PAGE_SIZE, "post\n");
3421 }
3422 
3423 static ssize_t
host_trace_buffer_enable_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3424 host_trace_buffer_enable_store(struct device *cdev,
3425 	struct device_attribute *attr, const char *buf, size_t count)
3426 {
3427 	struct Scsi_Host *shost = class_to_shost(cdev);
3428 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3429 	char str[10] = "";
3430 	struct mpt3_diag_register diag_register;
3431 	u8 issue_reset = 0;
3432 
3433 	/* don't allow post/release occurr while recovery is active */
3434 	if (ioc->shost_recovery || ioc->remove_host ||
3435 	    ioc->pci_error_recovery || ioc->is_driver_loading)
3436 		return -EBUSY;
3437 
3438 	if (sscanf(buf, "%9s", str) != 1)
3439 		return -EINVAL;
3440 
3441 	if (!strcmp(str, "post")) {
3442 		/* exit out if host buffers are already posted */
3443 		if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3444 		    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3445 		    MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3446 		    ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3447 		    MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3448 			goto out;
3449 		memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3450 		ioc_info(ioc, "posting host trace buffers\n");
3451 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3452 
3453 		if (ioc->manu_pg11.HostTraceBufferMaxSizeKB != 0 &&
3454 		    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0) {
3455 			/* post the same buffer allocated previously */
3456 			diag_register.requested_buffer_size =
3457 			    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE];
3458 		} else {
3459 			/*
3460 			 * Free the diag buffer memory which was previously
3461 			 * allocated by an application.
3462 			 */
3463 			if ((ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0)
3464 			    &&
3465 			    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3466 			    MPT3_DIAG_BUFFER_IS_APP_OWNED)) {
3467 				dma_free_coherent(&ioc->pdev->dev,
3468 						  ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE],
3469 						  ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE],
3470 						  ioc->diag_buffer_dma[MPI2_DIAG_BUF_TYPE_TRACE]);
3471 				ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE] =
3472 				    NULL;
3473 			}
3474 
3475 			diag_register.requested_buffer_size = (1024 * 1024);
3476 		}
3477 
3478 		diag_register.unique_id =
3479 		    (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
3480 		    (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
3481 		ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3482 		_ctl_diag_register_2(ioc,  &diag_register);
3483 		if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3484 		    MPT3_DIAG_BUFFER_IS_REGISTERED) {
3485 			ioc_info(ioc,
3486 			    "Trace buffer %d KB allocated through sysfs\n",
3487 			    diag_register.requested_buffer_size>>10);
3488 			if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
3489 				ioc->diag_buffer_status[
3490 				    MPI2_DIAG_BUF_TYPE_TRACE] |=
3491 				    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
3492 		}
3493 	} else if (!strcmp(str, "release")) {
3494 		/* exit out if host buffers are already released */
3495 		if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3496 			goto out;
3497 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3498 		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3499 			goto out;
3500 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3501 		    MPT3_DIAG_BUFFER_IS_RELEASED))
3502 			goto out;
3503 		ioc_info(ioc, "releasing host trace buffer\n");
3504 		ioc->htb_rel.buffer_rel_condition = MPT3_DIAG_BUFFER_REL_SYSFS;
3505 		mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3506 		    &issue_reset);
3507 	}
3508 
3509  out:
3510 	return strlen(buf);
3511 }
3512 static DEVICE_ATTR_RW(host_trace_buffer_enable);
3513 
3514 /*********** diagnostic trigger suppport *********************************/
3515 
3516 /**
3517  * diag_trigger_master_show - show the diag_trigger_master attribute
3518  * @cdev: pointer to embedded class device
3519  * @attr: ?
3520  * @buf: the buffer returned
3521  *
3522  * A sysfs 'read/write' shost attribute.
3523  */
3524 static ssize_t
diag_trigger_master_show(struct device * cdev,struct device_attribute * attr,char * buf)3525 diag_trigger_master_show(struct device *cdev,
3526 	struct device_attribute *attr, char *buf)
3527 
3528 {
3529 	struct Scsi_Host *shost = class_to_shost(cdev);
3530 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3531 	unsigned long flags;
3532 	ssize_t rc;
3533 
3534 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3535 	rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3536 	memcpy(buf, &ioc->diag_trigger_master, rc);
3537 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3538 	return rc;
3539 }
3540 
3541 /**
3542  * diag_trigger_master_store - store the diag_trigger_master attribute
3543  * @cdev: pointer to embedded class device
3544  * @attr: ?
3545  * @buf: the buffer returned
3546  * @count: ?
3547  *
3548  * A sysfs 'read/write' shost attribute.
3549  */
3550 static ssize_t
diag_trigger_master_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3551 diag_trigger_master_store(struct device *cdev,
3552 	struct device_attribute *attr, const char *buf, size_t count)
3553 
3554 {
3555 	struct Scsi_Host *shost = class_to_shost(cdev);
3556 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3557 	struct SL_WH_MASTER_TRIGGER_T *master_tg;
3558 	unsigned long flags;
3559 	ssize_t rc;
3560 	bool set = 1;
3561 
3562 	rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3563 
3564 	if (ioc->supports_trigger_pages) {
3565 		master_tg = kzalloc(sizeof(struct SL_WH_MASTER_TRIGGER_T),
3566 		    GFP_KERNEL);
3567 		if (!master_tg)
3568 			return -ENOMEM;
3569 
3570 		memcpy(master_tg, buf, rc);
3571 		if (!master_tg->MasterData)
3572 			set = 0;
3573 		if (mpt3sas_config_update_driver_trigger_pg1(ioc, master_tg,
3574 		    set)) {
3575 			kfree(master_tg);
3576 			return -EFAULT;
3577 		}
3578 		kfree(master_tg);
3579 	}
3580 
3581 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3582 	memset(&ioc->diag_trigger_master, 0,
3583 	    sizeof(struct SL_WH_MASTER_TRIGGER_T));
3584 	memcpy(&ioc->diag_trigger_master, buf, rc);
3585 	ioc->diag_trigger_master.MasterData |=
3586 	    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3587 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3588 	return rc;
3589 }
3590 static DEVICE_ATTR_RW(diag_trigger_master);
3591 
3592 
3593 /**
3594  * diag_trigger_event_show - show the diag_trigger_event attribute
3595  * @cdev: pointer to embedded class device
3596  * @attr: ?
3597  * @buf: the buffer returned
3598  *
3599  * A sysfs 'read/write' shost attribute.
3600  */
3601 static ssize_t
diag_trigger_event_show(struct device * cdev,struct device_attribute * attr,char * buf)3602 diag_trigger_event_show(struct device *cdev,
3603 	struct device_attribute *attr, char *buf)
3604 {
3605 	struct Scsi_Host *shost = class_to_shost(cdev);
3606 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3607 	unsigned long flags;
3608 	ssize_t rc;
3609 
3610 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3611 	rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3612 	memcpy(buf, &ioc->diag_trigger_event, rc);
3613 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3614 	return rc;
3615 }
3616 
3617 /**
3618  * diag_trigger_event_store - store the diag_trigger_event attribute
3619  * @cdev: pointer to embedded class device
3620  * @attr: ?
3621  * @buf: the buffer returned
3622  * @count: ?
3623  *
3624  * A sysfs 'read/write' shost attribute.
3625  */
3626 static ssize_t
diag_trigger_event_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3627 diag_trigger_event_store(struct device *cdev,
3628 	struct device_attribute *attr, const char *buf, size_t count)
3629 
3630 {
3631 	struct Scsi_Host *shost = class_to_shost(cdev);
3632 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3633 	struct SL_WH_EVENT_TRIGGERS_T *event_tg;
3634 	unsigned long flags;
3635 	ssize_t sz;
3636 	bool set = 1;
3637 
3638 	sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3639 	if (ioc->supports_trigger_pages) {
3640 		event_tg = kzalloc(sizeof(struct SL_WH_EVENT_TRIGGERS_T),
3641 		    GFP_KERNEL);
3642 		if (!event_tg)
3643 			return -ENOMEM;
3644 
3645 		memcpy(event_tg, buf, sz);
3646 		if (!event_tg->ValidEntries)
3647 			set = 0;
3648 		if (mpt3sas_config_update_driver_trigger_pg2(ioc, event_tg,
3649 		    set)) {
3650 			kfree(event_tg);
3651 			return -EFAULT;
3652 		}
3653 		kfree(event_tg);
3654 	}
3655 
3656 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3657 
3658 	memset(&ioc->diag_trigger_event, 0,
3659 	    sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3660 	memcpy(&ioc->diag_trigger_event, buf, sz);
3661 	if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3662 		ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3663 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3664 	return sz;
3665 }
3666 static DEVICE_ATTR_RW(diag_trigger_event);
3667 
3668 
3669 /**
3670  * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
3671  * @cdev: pointer to embedded class device
3672  * @attr: ?
3673  * @buf: the buffer returned
3674  *
3675  * A sysfs 'read/write' shost attribute.
3676  */
3677 static ssize_t
diag_trigger_scsi_show(struct device * cdev,struct device_attribute * attr,char * buf)3678 diag_trigger_scsi_show(struct device *cdev,
3679 	struct device_attribute *attr, char *buf)
3680 {
3681 	struct Scsi_Host *shost = class_to_shost(cdev);
3682 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3683 	unsigned long flags;
3684 	ssize_t rc;
3685 
3686 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3687 	rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3688 	memcpy(buf, &ioc->diag_trigger_scsi, rc);
3689 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3690 	return rc;
3691 }
3692 
3693 /**
3694  * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
3695  * @cdev: pointer to embedded class device
3696  * @attr: ?
3697  * @buf: the buffer returned
3698  * @count: ?
3699  *
3700  * A sysfs 'read/write' shost attribute.
3701  */
3702 static ssize_t
diag_trigger_scsi_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3703 diag_trigger_scsi_store(struct device *cdev,
3704 	struct device_attribute *attr, const char *buf, size_t count)
3705 {
3706 	struct Scsi_Host *shost = class_to_shost(cdev);
3707 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3708 	struct SL_WH_SCSI_TRIGGERS_T *scsi_tg;
3709 	unsigned long flags;
3710 	ssize_t sz;
3711 	bool set = 1;
3712 
3713 	sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
3714 	if (ioc->supports_trigger_pages) {
3715 		scsi_tg = kzalloc(sizeof(struct SL_WH_SCSI_TRIGGERS_T),
3716 		    GFP_KERNEL);
3717 		if (!scsi_tg)
3718 			return -ENOMEM;
3719 
3720 		memcpy(scsi_tg, buf, sz);
3721 		if (!scsi_tg->ValidEntries)
3722 			set = 0;
3723 		if (mpt3sas_config_update_driver_trigger_pg3(ioc, scsi_tg,
3724 		    set)) {
3725 			kfree(scsi_tg);
3726 			return -EFAULT;
3727 		}
3728 		kfree(scsi_tg);
3729 	}
3730 
3731 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3732 
3733 	memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
3734 	memcpy(&ioc->diag_trigger_scsi, buf, sz);
3735 	if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3736 		ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3737 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3738 	return sz;
3739 }
3740 static DEVICE_ATTR_RW(diag_trigger_scsi);
3741 
3742 
3743 /**
3744  * diag_trigger_mpi_show - show the diag_trigger_mpi attribute
3745  * @cdev: pointer to embedded class device
3746  * @attr: ?
3747  * @buf: the buffer returned
3748  *
3749  * A sysfs 'read/write' shost attribute.
3750  */
3751 static ssize_t
diag_trigger_mpi_show(struct device * cdev,struct device_attribute * attr,char * buf)3752 diag_trigger_mpi_show(struct device *cdev,
3753 	struct device_attribute *attr, char *buf)
3754 {
3755 	struct Scsi_Host *shost = class_to_shost(cdev);
3756 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3757 	unsigned long flags;
3758 	ssize_t rc;
3759 
3760 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3761 	rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3762 	memcpy(buf, &ioc->diag_trigger_mpi, rc);
3763 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3764 	return rc;
3765 }
3766 
3767 /**
3768  * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
3769  * @cdev: pointer to embedded class device
3770  * @attr: ?
3771  * @buf: the buffer returned
3772  * @count: ?
3773  *
3774  * A sysfs 'read/write' shost attribute.
3775  */
3776 static ssize_t
diag_trigger_mpi_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3777 diag_trigger_mpi_store(struct device *cdev,
3778 	struct device_attribute *attr, const char *buf, size_t count)
3779 {
3780 	struct Scsi_Host *shost = class_to_shost(cdev);
3781 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3782 	struct SL_WH_MPI_TRIGGERS_T *mpi_tg;
3783 	unsigned long flags;
3784 	ssize_t sz;
3785 	bool set = 1;
3786 
3787 	sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3788 	if (ioc->supports_trigger_pages) {
3789 		mpi_tg = kzalloc(sizeof(struct SL_WH_MPI_TRIGGERS_T),
3790 		    GFP_KERNEL);
3791 		if (!mpi_tg)
3792 			return -ENOMEM;
3793 
3794 		memcpy(mpi_tg, buf, sz);
3795 		if (!mpi_tg->ValidEntries)
3796 			set = 0;
3797 		if (mpt3sas_config_update_driver_trigger_pg4(ioc, mpi_tg,
3798 		    set)) {
3799 			kfree(mpi_tg);
3800 			return -EFAULT;
3801 		}
3802 		kfree(mpi_tg);
3803 	}
3804 
3805 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3806 	memset(&ioc->diag_trigger_mpi, 0,
3807 	    sizeof(ioc->diag_trigger_mpi));
3808 	memcpy(&ioc->diag_trigger_mpi, buf, sz);
3809 	if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3810 		ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3811 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3812 	return sz;
3813 }
3814 
3815 static DEVICE_ATTR_RW(diag_trigger_mpi);
3816 
3817 /*********** diagnostic trigger suppport *** END ****************************/
3818 
3819 /*****************************************/
3820 
3821 /**
3822  * drv_support_bitmap_show - driver supported feature bitmap
3823  * @cdev: pointer to embedded class device
3824  * @attr: unused
3825  * @buf: the buffer returned
3826  *
3827  * A sysfs 'read-only' shost attribute.
3828  */
3829 static ssize_t
drv_support_bitmap_show(struct device * cdev,struct device_attribute * attr,char * buf)3830 drv_support_bitmap_show(struct device *cdev,
3831 	struct device_attribute *attr, char *buf)
3832 {
3833 	struct Scsi_Host *shost = class_to_shost(cdev);
3834 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3835 
3836 	return snprintf(buf, PAGE_SIZE, "0x%08x\n", ioc->drv_support_bitmap);
3837 }
3838 static DEVICE_ATTR_RO(drv_support_bitmap);
3839 
3840 /**
3841  * enable_sdev_max_qd_show - display whether sdev max qd is enabled/disabled
3842  * @cdev: pointer to embedded class device
3843  * @attr: unused
3844  * @buf: the buffer returned
3845  *
3846  * A sysfs read/write shost attribute. This attribute is used to set the
3847  * targets queue depth to HBA IO queue depth if this attribute is enabled.
3848  */
3849 static ssize_t
enable_sdev_max_qd_show(struct device * cdev,struct device_attribute * attr,char * buf)3850 enable_sdev_max_qd_show(struct device *cdev,
3851 	struct device_attribute *attr, char *buf)
3852 {
3853 	struct Scsi_Host *shost = class_to_shost(cdev);
3854 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3855 
3856 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->enable_sdev_max_qd);
3857 }
3858 
3859 /**
3860  * enable_sdev_max_qd_store - Enable/disable sdev max qd
3861  * @cdev: pointer to embedded class device
3862  * @attr: unused
3863  * @buf: the buffer returned
3864  * @count: unused
3865  *
3866  * A sysfs read/write shost attribute. This attribute is used to set the
3867  * targets queue depth to HBA IO queue depth if this attribute is enabled.
3868  * If this attribute is disabled then targets will have corresponding default
3869  * queue depth.
3870  */
3871 static ssize_t
enable_sdev_max_qd_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3872 enable_sdev_max_qd_store(struct device *cdev,
3873 	struct device_attribute *attr, const char *buf, size_t count)
3874 {
3875 	struct Scsi_Host *shost = class_to_shost(cdev);
3876 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3877 	struct MPT3SAS_DEVICE *sas_device_priv_data;
3878 	struct MPT3SAS_TARGET *sas_target_priv_data;
3879 	int val = 0;
3880 	struct scsi_device *sdev;
3881 	struct _raid_device *raid_device;
3882 	int qdepth;
3883 
3884 	if (kstrtoint(buf, 0, &val) != 0)
3885 		return -EINVAL;
3886 
3887 	switch (val) {
3888 	case 0:
3889 		ioc->enable_sdev_max_qd = 0;
3890 		shost_for_each_device(sdev, ioc->shost) {
3891 			sas_device_priv_data = sdev->hostdata;
3892 			if (!sas_device_priv_data)
3893 				continue;
3894 			sas_target_priv_data = sas_device_priv_data->sas_target;
3895 			if (!sas_target_priv_data)
3896 				continue;
3897 
3898 			if (sas_target_priv_data->flags &
3899 			    MPT_TARGET_FLAGS_VOLUME) {
3900 				raid_device =
3901 				    mpt3sas_raid_device_find_by_handle(ioc,
3902 				    sas_target_priv_data->handle);
3903 
3904 				switch (raid_device->volume_type) {
3905 				case MPI2_RAID_VOL_TYPE_RAID0:
3906 					if (raid_device->device_info &
3907 					    MPI2_SAS_DEVICE_INFO_SSP_TARGET)
3908 						qdepth =
3909 						    MPT3SAS_SAS_QUEUE_DEPTH;
3910 					else
3911 						qdepth =
3912 						    MPT3SAS_SATA_QUEUE_DEPTH;
3913 					break;
3914 				case MPI2_RAID_VOL_TYPE_RAID1E:
3915 				case MPI2_RAID_VOL_TYPE_RAID1:
3916 				case MPI2_RAID_VOL_TYPE_RAID10:
3917 				case MPI2_RAID_VOL_TYPE_UNKNOWN:
3918 				default:
3919 					qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
3920 				}
3921 			} else if (sas_target_priv_data->flags &
3922 			    MPT_TARGET_FLAGS_PCIE_DEVICE)
3923 				qdepth = ioc->max_nvme_qd;
3924 			else
3925 				qdepth = (sas_target_priv_data->sas_dev->port_type > 1) ?
3926 				    ioc->max_wideport_qd : ioc->max_narrowport_qd;
3927 
3928 			mpt3sas_scsih_change_queue_depth(sdev, qdepth);
3929 		}
3930 		break;
3931 	case 1:
3932 		ioc->enable_sdev_max_qd = 1;
3933 		shost_for_each_device(sdev, ioc->shost)
3934 			mpt3sas_scsih_change_queue_depth(sdev,
3935 			    shost->can_queue);
3936 		break;
3937 	default:
3938 		return -EINVAL;
3939 	}
3940 
3941 	return strlen(buf);
3942 }
3943 static DEVICE_ATTR_RW(enable_sdev_max_qd);
3944 
3945 static struct attribute *mpt3sas_host_attrs[] = {
3946 	&dev_attr_version_fw.attr,
3947 	&dev_attr_version_bios.attr,
3948 	&dev_attr_version_mpi.attr,
3949 	&dev_attr_version_product.attr,
3950 	&dev_attr_version_nvdata_persistent.attr,
3951 	&dev_attr_version_nvdata_default.attr,
3952 	&dev_attr_board_name.attr,
3953 	&dev_attr_board_assembly.attr,
3954 	&dev_attr_board_tracer.attr,
3955 	&dev_attr_io_delay.attr,
3956 	&dev_attr_device_delay.attr,
3957 	&dev_attr_logging_level.attr,
3958 	&dev_attr_fwfault_debug.attr,
3959 	&dev_attr_fw_queue_depth.attr,
3960 	&dev_attr_host_sas_address.attr,
3961 	&dev_attr_ioc_reset_count.attr,
3962 	&dev_attr_host_trace_buffer_size.attr,
3963 	&dev_attr_host_trace_buffer.attr,
3964 	&dev_attr_host_trace_buffer_enable.attr,
3965 	&dev_attr_reply_queue_count.attr,
3966 	&dev_attr_diag_trigger_master.attr,
3967 	&dev_attr_diag_trigger_event.attr,
3968 	&dev_attr_diag_trigger_scsi.attr,
3969 	&dev_attr_diag_trigger_mpi.attr,
3970 	&dev_attr_drv_support_bitmap.attr,
3971 	&dev_attr_BRM_status.attr,
3972 	&dev_attr_enable_sdev_max_qd.attr,
3973 	NULL,
3974 };
3975 
3976 static const struct attribute_group mpt3sas_host_attr_group = {
3977 	.attrs = mpt3sas_host_attrs
3978 };
3979 
3980 const struct attribute_group *mpt3sas_host_groups[] = {
3981 	&mpt3sas_host_attr_group,
3982 	NULL
3983 };
3984 
3985 /* device attributes */
3986 
3987 /**
3988  * sas_address_show - sas address
3989  * @dev: pointer to embedded class device
3990  * @attr: ?
3991  * @buf: the buffer returned
3992  *
3993  * This is the sas address for the target
3994  *
3995  * A sysfs 'read-only' shost attribute.
3996  */
3997 static ssize_t
sas_address_show(struct device * dev,struct device_attribute * attr,char * buf)3998 sas_address_show(struct device *dev, struct device_attribute *attr,
3999 	char *buf)
4000 {
4001 	struct scsi_device *sdev = to_scsi_device(dev);
4002 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4003 
4004 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
4005 	    (unsigned long long)sas_device_priv_data->sas_target->sas_address);
4006 }
4007 static DEVICE_ATTR_RO(sas_address);
4008 
4009 /**
4010  * sas_device_handle_show - device handle
4011  * @dev: pointer to embedded class device
4012  * @attr: ?
4013  * @buf: the buffer returned
4014  *
4015  * This is the firmware assigned device handle
4016  *
4017  * A sysfs 'read-only' shost attribute.
4018  */
4019 static ssize_t
sas_device_handle_show(struct device * dev,struct device_attribute * attr,char * buf)4020 sas_device_handle_show(struct device *dev, struct device_attribute *attr,
4021 	char *buf)
4022 {
4023 	struct scsi_device *sdev = to_scsi_device(dev);
4024 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4025 
4026 	return snprintf(buf, PAGE_SIZE, "0x%04x\n",
4027 	    sas_device_priv_data->sas_target->handle);
4028 }
4029 static DEVICE_ATTR_RO(sas_device_handle);
4030 
4031 /**
4032  * sas_ncq_prio_supported_show - Indicate if device supports NCQ priority
4033  * @dev: pointer to embedded device
4034  * @attr: sas_ncq_prio_supported attribute descriptor
4035  * @buf: the buffer returned
4036  *
4037  * A sysfs 'read-only' sdev attribute, only works with SATA
4038  */
4039 static ssize_t
sas_ncq_prio_supported_show(struct device * dev,struct device_attribute * attr,char * buf)4040 sas_ncq_prio_supported_show(struct device *dev,
4041 			    struct device_attribute *attr, char *buf)
4042 {
4043 	struct scsi_device *sdev = to_scsi_device(dev);
4044 
4045 	return sysfs_emit(buf, "%d\n", sas_ata_ncq_prio_supported(sdev));
4046 }
4047 static DEVICE_ATTR_RO(sas_ncq_prio_supported);
4048 
4049 /**
4050  * sas_ncq_prio_enable_show - send prioritized io commands to device
4051  * @dev: pointer to embedded device
4052  * @attr: ?
4053  * @buf: the buffer returned
4054  *
4055  * A sysfs 'read/write' sdev attribute, only works with SATA
4056  */
4057 static ssize_t
sas_ncq_prio_enable_show(struct device * dev,struct device_attribute * attr,char * buf)4058 sas_ncq_prio_enable_show(struct device *dev,
4059 				 struct device_attribute *attr, char *buf)
4060 {
4061 	struct scsi_device *sdev = to_scsi_device(dev);
4062 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4063 
4064 	return snprintf(buf, PAGE_SIZE, "%d\n",
4065 			sas_device_priv_data->ncq_prio_enable);
4066 }
4067 
4068 static ssize_t
sas_ncq_prio_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4069 sas_ncq_prio_enable_store(struct device *dev,
4070 				  struct device_attribute *attr,
4071 				  const char *buf, size_t count)
4072 {
4073 	struct scsi_device *sdev = to_scsi_device(dev);
4074 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4075 	bool ncq_prio_enable = 0;
4076 
4077 	if (kstrtobool(buf, &ncq_prio_enable))
4078 		return -EINVAL;
4079 
4080 	if (!sas_ata_ncq_prio_supported(sdev))
4081 		return -EINVAL;
4082 
4083 	sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
4084 	return strlen(buf);
4085 }
4086 static DEVICE_ATTR_RW(sas_ncq_prio_enable);
4087 
4088 static struct attribute *mpt3sas_dev_attrs[] = {
4089 	&dev_attr_sas_address.attr,
4090 	&dev_attr_sas_device_handle.attr,
4091 	&dev_attr_sas_ncq_prio_supported.attr,
4092 	&dev_attr_sas_ncq_prio_enable.attr,
4093 	NULL,
4094 };
4095 
4096 static const struct attribute_group mpt3sas_dev_attr_group = {
4097 	.attrs = mpt3sas_dev_attrs
4098 };
4099 
4100 const struct attribute_group *mpt3sas_dev_groups[] = {
4101 	&mpt3sas_dev_attr_group,
4102 	NULL
4103 };
4104 
4105 /* file operations table for mpt3ctl device */
4106 static const struct file_operations ctl_fops = {
4107 	.owner = THIS_MODULE,
4108 	.unlocked_ioctl = _ctl_ioctl,
4109 	.poll = _ctl_poll,
4110 	.fasync = _ctl_fasync,
4111 #ifdef CONFIG_COMPAT
4112 	.compat_ioctl = _ctl_ioctl_compat,
4113 #endif
4114 };
4115 
4116 /* file operations table for mpt2ctl device */
4117 static const struct file_operations ctl_gen2_fops = {
4118 	.owner = THIS_MODULE,
4119 	.unlocked_ioctl = _ctl_mpt2_ioctl,
4120 	.poll = _ctl_poll,
4121 	.fasync = _ctl_fasync,
4122 #ifdef CONFIG_COMPAT
4123 	.compat_ioctl = _ctl_mpt2_ioctl_compat,
4124 #endif
4125 };
4126 
4127 static struct miscdevice ctl_dev = {
4128 	.minor  = MPT3SAS_MINOR,
4129 	.name   = MPT3SAS_DEV_NAME,
4130 	.fops   = &ctl_fops,
4131 };
4132 
4133 static struct miscdevice gen2_ctl_dev = {
4134 	.minor  = MPT2SAS_MINOR,
4135 	.name   = MPT2SAS_DEV_NAME,
4136 	.fops   = &ctl_gen2_fops,
4137 };
4138 
4139 /**
4140  * mpt3sas_ctl_init - main entry point for ctl.
4141  * @hbas_to_enumerate: ?
4142  */
4143 void
mpt3sas_ctl_init(ushort hbas_to_enumerate)4144 mpt3sas_ctl_init(ushort hbas_to_enumerate)
4145 {
4146 	async_queue = NULL;
4147 
4148 	/* Don't register mpt3ctl ioctl device if
4149 	 * hbas_to_enumarate is one.
4150 	 */
4151 	if (hbas_to_enumerate != 1)
4152 		if (misc_register(&ctl_dev) < 0)
4153 			pr_err("%s can't register misc device [minor=%d]\n",
4154 			    MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
4155 
4156 	/* Don't register mpt3ctl ioctl device if
4157 	 * hbas_to_enumarate is two.
4158 	 */
4159 	if (hbas_to_enumerate != 2)
4160 		if (misc_register(&gen2_ctl_dev) < 0)
4161 			pr_err("%s can't register misc device [minor=%d]\n",
4162 			    MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
4163 
4164 	init_waitqueue_head(&ctl_poll_wait);
4165 }
4166 
4167 /**
4168  * mpt3sas_ctl_exit - exit point for ctl
4169  * @hbas_to_enumerate: ?
4170  */
4171 void
mpt3sas_ctl_exit(ushort hbas_to_enumerate)4172 mpt3sas_ctl_exit(ushort hbas_to_enumerate)
4173 {
4174 	struct MPT3SAS_ADAPTER *ioc;
4175 	int i;
4176 
4177 	list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
4178 
4179 		/* free memory associated to diag buffers */
4180 		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
4181 			if (!ioc->diag_buffer[i])
4182 				continue;
4183 			dma_free_coherent(&ioc->pdev->dev,
4184 					  ioc->diag_buffer_sz[i],
4185 					  ioc->diag_buffer[i],
4186 					  ioc->diag_buffer_dma[i]);
4187 			ioc->diag_buffer[i] = NULL;
4188 			ioc->diag_buffer_status[i] = 0;
4189 		}
4190 
4191 		kfree(ioc->event_log);
4192 	}
4193 	if (hbas_to_enumerate != 1)
4194 		misc_deregister(&ctl_dev);
4195 	if (hbas_to_enumerate != 2)
4196 		misc_deregister(&gen2_ctl_dev);
4197 }
4198