• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
2555  * _ctl_enable_diag_sbr_reload - enable sbr reload bit
2556  * @ioc: per adapter object
2557  * @arg: user space buffer containing ioctl content
2558  *
2559  * Enable the SBR reload bit
2560  */
2561 static int
_ctl_enable_diag_sbr_reload(struct MPT3SAS_ADAPTER * ioc,void __user * arg)2562 _ctl_enable_diag_sbr_reload(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
2563 {
2564 	u32 ioc_state, host_diagnostic;
2565 
2566 	if (ioc->shost_recovery ||
2567 	    ioc->pci_error_recovery || ioc->is_driver_loading ||
2568 	    ioc->remove_host)
2569 		return -EAGAIN;
2570 
2571 	ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2572 
2573 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL)
2574 		return -EFAULT;
2575 
2576 	host_diagnostic = ioc->base_readl(&ioc->chip->HostDiagnostic);
2577 
2578 	if (host_diagnostic & MPI2_DIAG_SBR_RELOAD)
2579 		return 0;
2580 
2581 	if (mutex_trylock(&ioc->hostdiag_unlock_mutex)) {
2582 		if (mpt3sas_base_unlock_and_get_host_diagnostic(ioc, &host_diagnostic)) {
2583 			mutex_unlock(&ioc->hostdiag_unlock_mutex);
2584 				return -EFAULT;
2585 		}
2586 	} else
2587 		return -EAGAIN;
2588 
2589 	host_diagnostic |= MPI2_DIAG_SBR_RELOAD;
2590 	writel(host_diagnostic, &ioc->chip->HostDiagnostic);
2591 	host_diagnostic = ioc->base_readl(&ioc->chip->HostDiagnostic);
2592 	mpt3sas_base_lock_host_diagnostic(ioc);
2593 	mutex_unlock(&ioc->hostdiag_unlock_mutex);
2594 
2595 	if (!(host_diagnostic & MPI2_DIAG_SBR_RELOAD)) {
2596 		ioc_err(ioc, "%s: Failed to set Diag SBR Reload Bit\n", __func__);
2597 		return -EFAULT;
2598 	}
2599 
2600 	ioc_info(ioc, "%s: Successfully set the Diag SBR Reload Bit\n", __func__);
2601 	return 0;
2602 }
2603 
2604 #ifdef CONFIG_COMPAT
2605 /**
2606  * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2607  * @ioc: per adapter object
2608  * @cmd: ioctl opcode
2609  * @arg: (struct mpt3_ioctl_command32)
2610  *
2611  * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2612  */
2613 static long
_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER * ioc,unsigned cmd,void __user * arg)2614 _ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2615 	void __user *arg)
2616 {
2617 	struct mpt3_ioctl_command32 karg32;
2618 	struct mpt3_ioctl_command32 __user *uarg;
2619 	struct mpt3_ioctl_command karg;
2620 
2621 	if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2622 		return -EINVAL;
2623 
2624 	uarg = (struct mpt3_ioctl_command32 __user *) arg;
2625 
2626 	if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2627 		pr_err("failure at %s:%d/%s()!\n",
2628 		    __FILE__, __LINE__, __func__);
2629 		return -EFAULT;
2630 	}
2631 
2632 	memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2633 	karg.hdr.ioc_number = karg32.hdr.ioc_number;
2634 	karg.hdr.port_number = karg32.hdr.port_number;
2635 	karg.hdr.max_data_size = karg32.hdr.max_data_size;
2636 	karg.timeout = karg32.timeout;
2637 	karg.max_reply_bytes = karg32.max_reply_bytes;
2638 	karg.data_in_size = karg32.data_in_size;
2639 	karg.data_out_size = karg32.data_out_size;
2640 	karg.max_sense_bytes = karg32.max_sense_bytes;
2641 	karg.data_sge_offset = karg32.data_sge_offset;
2642 	karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2643 	karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2644 	karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2645 	karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2646 	return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2647 }
2648 #endif
2649 
2650 /**
2651  * _ctl_ioctl_main - main ioctl entry point
2652  * @file:  (struct file)
2653  * @cmd:  ioctl opcode
2654  * @arg:  user space data buffer
2655  * @compat:  handles 32 bit applications in 64bit os
2656  * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
2657  * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
2658  */
2659 static long
_ctl_ioctl_main(struct file * file,unsigned int cmd,void __user * arg,u8 compat,u16 mpi_version)2660 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2661 	u8 compat, u16 mpi_version)
2662 {
2663 	struct MPT3SAS_ADAPTER *ioc;
2664 	struct mpt3_ioctl_header ioctl_header;
2665 	enum block_state state;
2666 	long ret = -ENOIOCTLCMD;
2667 
2668 	/* get IOCTL header */
2669 	if (copy_from_user(&ioctl_header, (char __user *)arg,
2670 	    sizeof(struct mpt3_ioctl_header))) {
2671 		pr_err("failure at %s:%d/%s()!\n",
2672 		    __FILE__, __LINE__, __func__);
2673 		return -EFAULT;
2674 	}
2675 
2676 	if (_ctl_verify_adapter(ioctl_header.ioc_number,
2677 				&ioc, mpi_version) == -1 || !ioc)
2678 		return -ENODEV;
2679 
2680 	/* pci_access_mutex lock acquired by ioctl path */
2681 	mutex_lock(&ioc->pci_access_mutex);
2682 
2683 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
2684 	    ioc->is_driver_loading || ioc->remove_host) {
2685 		ret = -EAGAIN;
2686 		goto out_unlock_pciaccess;
2687 	}
2688 
2689 	state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2690 	if (state == NON_BLOCKING) {
2691 		if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2692 			ret = -EAGAIN;
2693 			goto out_unlock_pciaccess;
2694 		}
2695 	} else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2696 		ret = -ERESTARTSYS;
2697 		goto out_unlock_pciaccess;
2698 	}
2699 
2700 
2701 	switch (cmd) {
2702 	case MPT3IOCINFO:
2703 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2704 			ret = _ctl_getiocinfo(ioc, arg);
2705 		break;
2706 #ifdef CONFIG_COMPAT
2707 	case MPT3COMMAND32:
2708 #endif
2709 	case MPT3COMMAND:
2710 	{
2711 		struct mpt3_ioctl_command __user *uarg;
2712 		struct mpt3_ioctl_command karg;
2713 
2714 #ifdef CONFIG_COMPAT
2715 		if (compat) {
2716 			ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2717 			break;
2718 		}
2719 #endif
2720 		if (copy_from_user(&karg, arg, sizeof(karg))) {
2721 			pr_err("failure at %s:%d/%s()!\n",
2722 			    __FILE__, __LINE__, __func__);
2723 			ret = -EFAULT;
2724 			break;
2725 		}
2726 
2727 		if (karg.hdr.ioc_number != ioctl_header.ioc_number) {
2728 			ret = -EINVAL;
2729 			break;
2730 		}
2731 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2732 			uarg = arg;
2733 			ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2734 		}
2735 		break;
2736 	}
2737 	case MPT3EVENTQUERY:
2738 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2739 			ret = _ctl_eventquery(ioc, arg);
2740 		break;
2741 	case MPT3EVENTENABLE:
2742 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2743 			ret = _ctl_eventenable(ioc, arg);
2744 		break;
2745 	case MPT3EVENTREPORT:
2746 		ret = _ctl_eventreport(ioc, arg);
2747 		break;
2748 	case MPT3HARDRESET:
2749 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2750 			ret = _ctl_do_reset(ioc, arg);
2751 		break;
2752 	case MPT3BTDHMAPPING:
2753 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2754 			ret = _ctl_btdh_mapping(ioc, arg);
2755 		break;
2756 	case MPT3DIAGREGISTER:
2757 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2758 			ret = _ctl_diag_register(ioc, arg);
2759 		break;
2760 	case MPT3DIAGUNREGISTER:
2761 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2762 			ret = _ctl_diag_unregister(ioc, arg);
2763 		break;
2764 	case MPT3DIAGQUERY:
2765 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2766 			ret = _ctl_diag_query(ioc, arg);
2767 		break;
2768 	case MPT3DIAGRELEASE:
2769 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2770 			ret = _ctl_diag_release(ioc, arg);
2771 		break;
2772 	case MPT3DIAGREADBUFFER:
2773 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2774 			ret = _ctl_diag_read_buffer(ioc, arg);
2775 		break;
2776 	case MPT3ADDNLDIAGQUERY:
2777 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_addnl_diag_query))
2778 			ret = _ctl_addnl_diag_query(ioc, arg);
2779 		break;
2780 	case MPT3ENABLEDIAGSBRRELOAD:
2781 		if (_IOC_SIZE(cmd) == sizeof(struct mpt3_enable_diag_sbr_reload))
2782 			ret = _ctl_enable_diag_sbr_reload(ioc, arg);
2783 		break;
2784 	default:
2785 		dctlprintk(ioc,
2786 			   ioc_info(ioc, "unsupported ioctl opcode(0x%08x)\n",
2787 				    cmd));
2788 		break;
2789 	}
2790 
2791 	mutex_unlock(&ioc->ctl_cmds.mutex);
2792 out_unlock_pciaccess:
2793 	mutex_unlock(&ioc->pci_access_mutex);
2794 	return ret;
2795 }
2796 
2797 /**
2798  * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
2799  * @file: (struct file)
2800  * @cmd: ioctl opcode
2801  * @arg: ?
2802  */
2803 static long
_ctl_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2804 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2805 {
2806 	long ret;
2807 
2808 	/* pass MPI25_VERSION | MPI26_VERSION value,
2809 	 * to indicate that this ioctl cmd
2810 	 * came from mpt3ctl ioctl device.
2811 	 */
2812 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2813 		MPI25_VERSION | MPI26_VERSION);
2814 	return ret;
2815 }
2816 
2817 /**
2818  * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
2819  * @file: (struct file)
2820  * @cmd: ioctl opcode
2821  * @arg: ?
2822  */
2823 static long
_ctl_mpt2_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2824 _ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2825 {
2826 	long ret;
2827 
2828 	/* pass MPI2_VERSION value, to indicate that this ioctl cmd
2829 	 * came from mpt2ctl ioctl device.
2830 	 */
2831 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2832 	return ret;
2833 }
2834 #ifdef CONFIG_COMPAT
2835 /**
2836  * _ctl_ioctl_compat - main ioctl entry point (compat)
2837  * @file: ?
2838  * @cmd: ?
2839  * @arg: ?
2840  *
2841  * This routine handles 32 bit applications in 64bit os.
2842  */
2843 static long
_ctl_ioctl_compat(struct file * file,unsigned cmd,unsigned long arg)2844 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2845 {
2846 	long ret;
2847 
2848 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2849 		MPI25_VERSION | MPI26_VERSION);
2850 	return ret;
2851 }
2852 
2853 /**
2854  * _ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
2855  * @file: ?
2856  * @cmd: ?
2857  * @arg: ?
2858  *
2859  * This routine handles 32 bit applications in 64bit os.
2860  */
2861 static long
_ctl_mpt2_ioctl_compat(struct file * file,unsigned cmd,unsigned long arg)2862 _ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2863 {
2864 	long ret;
2865 
2866 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
2867 	return ret;
2868 }
2869 #endif
2870 
2871 /* scsi host attributes */
2872 /**
2873  * version_fw_show - firmware version
2874  * @cdev: pointer to embedded class device
2875  * @attr: ?
2876  * @buf: the buffer returned
2877  *
2878  * A sysfs 'read-only' shost attribute.
2879  */
2880 static ssize_t
version_fw_show(struct device * cdev,struct device_attribute * attr,char * buf)2881 version_fw_show(struct device *cdev, struct device_attribute *attr,
2882 	char *buf)
2883 {
2884 	struct Scsi_Host *shost = class_to_shost(cdev);
2885 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2886 
2887 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2888 	    (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2889 	    (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2890 	    (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2891 	    ioc->facts.FWVersion.Word & 0x000000FF);
2892 }
2893 static DEVICE_ATTR_RO(version_fw);
2894 
2895 /**
2896  * version_bios_show - bios version
2897  * @cdev: pointer to embedded class device
2898  * @attr: ?
2899  * @buf: the buffer returned
2900  *
2901  * A sysfs 'read-only' shost attribute.
2902  */
2903 static ssize_t
version_bios_show(struct device * cdev,struct device_attribute * attr,char * buf)2904 version_bios_show(struct device *cdev, struct device_attribute *attr,
2905 	char *buf)
2906 {
2907 	struct Scsi_Host *shost = class_to_shost(cdev);
2908 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2909 
2910 	u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2911 
2912 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2913 	    (version & 0xFF000000) >> 24,
2914 	    (version & 0x00FF0000) >> 16,
2915 	    (version & 0x0000FF00) >> 8,
2916 	    version & 0x000000FF);
2917 }
2918 static DEVICE_ATTR_RO(version_bios);
2919 
2920 /**
2921  * version_mpi_show - MPI (message passing interface) version
2922  * @cdev: pointer to embedded class device
2923  * @attr: ?
2924  * @buf: the buffer returned
2925  *
2926  * A sysfs 'read-only' shost attribute.
2927  */
2928 static ssize_t
version_mpi_show(struct device * cdev,struct device_attribute * attr,char * buf)2929 version_mpi_show(struct device *cdev, struct device_attribute *attr,
2930 	char *buf)
2931 {
2932 	struct Scsi_Host *shost = class_to_shost(cdev);
2933 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2934 
2935 	return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2936 	    ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2937 }
2938 static DEVICE_ATTR_RO(version_mpi);
2939 
2940 /**
2941  * version_product_show - product name
2942  * @cdev: pointer to embedded class device
2943  * @attr: ?
2944  * @buf: the buffer returned
2945  *
2946  * A sysfs 'read-only' shost attribute.
2947  */
2948 static ssize_t
version_product_show(struct device * cdev,struct device_attribute * attr,char * buf)2949 version_product_show(struct device *cdev, struct device_attribute *attr,
2950 	char *buf)
2951 {
2952 	struct Scsi_Host *shost = class_to_shost(cdev);
2953 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2954 
2955 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2956 }
2957 static DEVICE_ATTR_RO(version_product);
2958 
2959 /**
2960  * version_nvdata_persistent_show - ndvata persistent version
2961  * @cdev: pointer to embedded class device
2962  * @attr: ?
2963  * @buf: the buffer returned
2964  *
2965  * A sysfs 'read-only' shost attribute.
2966  */
2967 static ssize_t
version_nvdata_persistent_show(struct device * cdev,struct device_attribute * attr,char * buf)2968 version_nvdata_persistent_show(struct device *cdev,
2969 	struct device_attribute *attr, char *buf)
2970 {
2971 	struct Scsi_Host *shost = class_to_shost(cdev);
2972 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2973 
2974 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2975 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2976 }
2977 static DEVICE_ATTR_RO(version_nvdata_persistent);
2978 
2979 /**
2980  * version_nvdata_default_show - nvdata default version
2981  * @cdev: pointer to embedded class device
2982  * @attr: ?
2983  * @buf: the buffer returned
2984  *
2985  * A sysfs 'read-only' shost attribute.
2986  */
2987 static ssize_t
version_nvdata_default_show(struct device * cdev,struct device_attribute * attr,char * buf)2988 version_nvdata_default_show(struct device *cdev, struct device_attribute
2989 	*attr, char *buf)
2990 {
2991 	struct Scsi_Host *shost = class_to_shost(cdev);
2992 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2993 
2994 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2995 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2996 }
2997 static DEVICE_ATTR_RO(version_nvdata_default);
2998 
2999 /**
3000  * board_name_show - board name
3001  * @cdev: pointer to embedded class device
3002  * @attr: ?
3003  * @buf: the buffer returned
3004  *
3005  * A sysfs 'read-only' shost attribute.
3006  */
3007 static ssize_t
board_name_show(struct device * cdev,struct device_attribute * attr,char * buf)3008 board_name_show(struct device *cdev, struct device_attribute *attr,
3009 	char *buf)
3010 {
3011 	struct Scsi_Host *shost = class_to_shost(cdev);
3012 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3013 
3014 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
3015 }
3016 static DEVICE_ATTR_RO(board_name);
3017 
3018 /**
3019  * board_assembly_show - board assembly name
3020  * @cdev: pointer to embedded class device
3021  * @attr: ?
3022  * @buf: the buffer returned
3023  *
3024  * A sysfs 'read-only' shost attribute.
3025  */
3026 static ssize_t
board_assembly_show(struct device * cdev,struct device_attribute * attr,char * buf)3027 board_assembly_show(struct device *cdev, struct device_attribute *attr,
3028 	char *buf)
3029 {
3030 	struct Scsi_Host *shost = class_to_shost(cdev);
3031 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3032 
3033 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
3034 }
3035 static DEVICE_ATTR_RO(board_assembly);
3036 
3037 /**
3038  * board_tracer_show - board tracer number
3039  * @cdev: pointer to embedded class device
3040  * @attr: ?
3041  * @buf: the buffer returned
3042  *
3043  * A sysfs 'read-only' shost attribute.
3044  */
3045 static ssize_t
board_tracer_show(struct device * cdev,struct device_attribute * attr,char * buf)3046 board_tracer_show(struct device *cdev, struct device_attribute *attr,
3047 	char *buf)
3048 {
3049 	struct Scsi_Host *shost = class_to_shost(cdev);
3050 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3051 
3052 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
3053 }
3054 static DEVICE_ATTR_RO(board_tracer);
3055 
3056 /**
3057  * io_delay_show - io missing delay
3058  * @cdev: pointer to embedded class device
3059  * @attr: ?
3060  * @buf: the buffer returned
3061  *
3062  * This is for firmware implemention for deboucing device
3063  * removal events.
3064  *
3065  * A sysfs 'read-only' shost attribute.
3066  */
3067 static ssize_t
io_delay_show(struct device * cdev,struct device_attribute * attr,char * buf)3068 io_delay_show(struct device *cdev, struct device_attribute *attr,
3069 	char *buf)
3070 {
3071 	struct Scsi_Host *shost = class_to_shost(cdev);
3072 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3073 
3074 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
3075 }
3076 static DEVICE_ATTR_RO(io_delay);
3077 
3078 /**
3079  * device_delay_show - device missing delay
3080  * @cdev: pointer to embedded class device
3081  * @attr: ?
3082  * @buf: the buffer returned
3083  *
3084  * This is for firmware implemention for deboucing device
3085  * removal events.
3086  *
3087  * A sysfs 'read-only' shost attribute.
3088  */
3089 static ssize_t
device_delay_show(struct device * cdev,struct device_attribute * attr,char * buf)3090 device_delay_show(struct device *cdev, struct device_attribute *attr,
3091 	char *buf)
3092 {
3093 	struct Scsi_Host *shost = class_to_shost(cdev);
3094 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3095 
3096 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
3097 }
3098 static DEVICE_ATTR_RO(device_delay);
3099 
3100 /**
3101  * fw_queue_depth_show - global credits
3102  * @cdev: pointer to embedded class device
3103  * @attr: ?
3104  * @buf: the buffer returned
3105  *
3106  * This is firmware queue depth limit
3107  *
3108  * A sysfs 'read-only' shost attribute.
3109  */
3110 static ssize_t
fw_queue_depth_show(struct device * cdev,struct device_attribute * attr,char * buf)3111 fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
3112 	char *buf)
3113 {
3114 	struct Scsi_Host *shost = class_to_shost(cdev);
3115 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3116 
3117 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
3118 }
3119 static DEVICE_ATTR_RO(fw_queue_depth);
3120 
3121 /**
3122  * host_sas_address_show - sas address
3123  * @cdev: pointer to embedded class device
3124  * @attr: ?
3125  * @buf: the buffer returned
3126  *
3127  * This is the controller sas address
3128  *
3129  * A sysfs 'read-only' shost attribute.
3130  */
3131 static ssize_t
host_sas_address_show(struct device * cdev,struct device_attribute * attr,char * buf)3132 host_sas_address_show(struct device *cdev, struct device_attribute *attr,
3133 	char *buf)
3134 
3135 {
3136 	struct Scsi_Host *shost = class_to_shost(cdev);
3137 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3138 
3139 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3140 	    (unsigned long long)ioc->sas_hba.sas_address);
3141 }
3142 static DEVICE_ATTR_RO(host_sas_address);
3143 
3144 /**
3145  * logging_level_show - logging level
3146  * @cdev: pointer to embedded class device
3147  * @attr: ?
3148  * @buf: the buffer returned
3149  *
3150  * A sysfs 'read/write' shost attribute.
3151  */
3152 static ssize_t
logging_level_show(struct device * cdev,struct device_attribute * attr,char * buf)3153 logging_level_show(struct device *cdev, struct device_attribute *attr,
3154 	char *buf)
3155 {
3156 	struct Scsi_Host *shost = class_to_shost(cdev);
3157 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3158 
3159 	return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
3160 }
3161 static ssize_t
logging_level_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3162 logging_level_store(struct device *cdev, struct device_attribute *attr,
3163 	const char *buf, size_t count)
3164 {
3165 	struct Scsi_Host *shost = class_to_shost(cdev);
3166 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3167 	int val = 0;
3168 
3169 	if (sscanf(buf, "%x", &val) != 1)
3170 		return -EINVAL;
3171 
3172 	ioc->logging_level = val;
3173 	ioc_info(ioc, "logging_level=%08xh\n",
3174 		 ioc->logging_level);
3175 	return strlen(buf);
3176 }
3177 static DEVICE_ATTR_RW(logging_level);
3178 
3179 /**
3180  * fwfault_debug_show - show/store fwfault_debug
3181  * @cdev: pointer to embedded class device
3182  * @attr: ?
3183  * @buf: the buffer returned
3184  *
3185  * mpt3sas_fwfault_debug is command line option
3186  * A sysfs 'read/write' shost attribute.
3187  */
3188 static ssize_t
fwfault_debug_show(struct device * cdev,struct device_attribute * attr,char * buf)3189 fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
3190 	char *buf)
3191 {
3192 	struct Scsi_Host *shost = class_to_shost(cdev);
3193 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3194 
3195 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
3196 }
3197 static ssize_t
fwfault_debug_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3198 fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
3199 	const char *buf, size_t count)
3200 {
3201 	struct Scsi_Host *shost = class_to_shost(cdev);
3202 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3203 	int val = 0;
3204 
3205 	if (sscanf(buf, "%d", &val) != 1)
3206 		return -EINVAL;
3207 
3208 	ioc->fwfault_debug = val;
3209 	ioc_info(ioc, "fwfault_debug=%d\n",
3210 		 ioc->fwfault_debug);
3211 	return strlen(buf);
3212 }
3213 static DEVICE_ATTR_RW(fwfault_debug);
3214 
3215 /**
3216  * ioc_reset_count_show - ioc reset count
3217  * @cdev: pointer to embedded class device
3218  * @attr: ?
3219  * @buf: the buffer returned
3220  *
3221  * This is firmware queue depth limit
3222  *
3223  * A sysfs 'read-only' shost attribute.
3224  */
3225 static ssize_t
ioc_reset_count_show(struct device * cdev,struct device_attribute * attr,char * buf)3226 ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
3227 	char *buf)
3228 {
3229 	struct Scsi_Host *shost = class_to_shost(cdev);
3230 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3231 
3232 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
3233 }
3234 static DEVICE_ATTR_RO(ioc_reset_count);
3235 
3236 /**
3237  * reply_queue_count_show - number of reply queues
3238  * @cdev: pointer to embedded class device
3239  * @attr: ?
3240  * @buf: the buffer returned
3241  *
3242  * This is number of reply queues
3243  *
3244  * A sysfs 'read-only' shost attribute.
3245  */
3246 static ssize_t
reply_queue_count_show(struct device * cdev,struct device_attribute * attr,char * buf)3247 reply_queue_count_show(struct device *cdev,
3248 	struct device_attribute *attr, char *buf)
3249 {
3250 	u8 reply_queue_count;
3251 	struct Scsi_Host *shost = class_to_shost(cdev);
3252 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3253 
3254 	if ((ioc->facts.IOCCapabilities &
3255 	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
3256 		reply_queue_count = ioc->reply_queue_count;
3257 	else
3258 		reply_queue_count = 1;
3259 
3260 	return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
3261 }
3262 static DEVICE_ATTR_RO(reply_queue_count);
3263 
3264 /**
3265  * BRM_status_show - Backup Rail Monitor Status
3266  * @cdev: pointer to embedded class device
3267  * @attr: ?
3268  * @buf: the buffer returned
3269  *
3270  * This is number of reply queues
3271  *
3272  * A sysfs 'read-only' shost attribute.
3273  */
3274 static ssize_t
BRM_status_show(struct device * cdev,struct device_attribute * attr,char * buf)3275 BRM_status_show(struct device *cdev, struct device_attribute *attr,
3276 	char *buf)
3277 {
3278 	struct Scsi_Host *shost = class_to_shost(cdev);
3279 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3280 	Mpi2IOUnitPage3_t io_unit_pg3;
3281 	Mpi2ConfigReply_t mpi_reply;
3282 	u16 backup_rail_monitor_status = 0;
3283 	u16 ioc_status;
3284 	int sz;
3285 	ssize_t rc = 0;
3286 
3287 	if (!ioc->is_warpdrive) {
3288 		ioc_err(ioc, "%s: BRM attribute is only for warpdrive\n",
3289 			__func__);
3290 		return 0;
3291 	}
3292 	/* pci_access_mutex lock acquired by sysfs show path */
3293 	mutex_lock(&ioc->pci_access_mutex);
3294 	if (ioc->pci_error_recovery || ioc->remove_host)
3295 		goto out;
3296 
3297 	sz = sizeof(io_unit_pg3);
3298 	memset(&io_unit_pg3, 0, sz);
3299 
3300 	if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, &io_unit_pg3, sz) !=
3301 	    0) {
3302 		ioc_err(ioc, "%s: failed reading iounit_pg3\n",
3303 			__func__);
3304 		rc = -EINVAL;
3305 		goto out;
3306 	}
3307 
3308 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3309 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3310 		ioc_err(ioc, "%s: iounit_pg3 failed with ioc_status(0x%04x)\n",
3311 			__func__, ioc_status);
3312 		rc = -EINVAL;
3313 		goto out;
3314 	}
3315 
3316 	if (io_unit_pg3.GPIOCount < 25) {
3317 		ioc_err(ioc, "%s: iounit_pg3.GPIOCount less than 25 entries, detected (%d) entries\n",
3318 			__func__, io_unit_pg3.GPIOCount);
3319 		rc = -EINVAL;
3320 		goto out;
3321 	}
3322 
3323 	/* BRM status is in bit zero of GPIOVal[24] */
3324 	backup_rail_monitor_status = le16_to_cpu(io_unit_pg3.GPIOVal[24]);
3325 	rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
3326 
3327  out:
3328 	mutex_unlock(&ioc->pci_access_mutex);
3329 	return rc;
3330 }
3331 static DEVICE_ATTR_RO(BRM_status);
3332 
3333 struct DIAG_BUFFER_START {
3334 	__le32	Size;
3335 	__le32	DiagVersion;
3336 	u8	BufferType;
3337 	u8	Reserved[3];
3338 	__le32	Reserved1;
3339 	__le32	Reserved2;
3340 	__le32	Reserved3;
3341 };
3342 
3343 /**
3344  * host_trace_buffer_size_show - host buffer size (trace only)
3345  * @cdev: pointer to embedded class device
3346  * @attr: ?
3347  * @buf: the buffer returned
3348  *
3349  * A sysfs 'read-only' shost attribute.
3350  */
3351 static ssize_t
host_trace_buffer_size_show(struct device * cdev,struct device_attribute * attr,char * buf)3352 host_trace_buffer_size_show(struct device *cdev,
3353 	struct device_attribute *attr, char *buf)
3354 {
3355 	struct Scsi_Host *shost = class_to_shost(cdev);
3356 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3357 	u32 size = 0;
3358 	struct DIAG_BUFFER_START *request_data;
3359 
3360 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3361 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3362 			__func__);
3363 		return 0;
3364 	}
3365 
3366 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3367 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3368 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3369 			__func__);
3370 		return 0;
3371 	}
3372 
3373 	request_data = (struct DIAG_BUFFER_START *)
3374 	    ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
3375 	if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
3376 	    le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
3377 	    le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
3378 	    le32_to_cpu(request_data->Reserved3) == 0x4742444c)
3379 		size = le32_to_cpu(request_data->Size);
3380 
3381 	ioc->ring_buffer_sz = size;
3382 	return snprintf(buf, PAGE_SIZE, "%d\n", size);
3383 }
3384 static DEVICE_ATTR_RO(host_trace_buffer_size);
3385 
3386 /**
3387  * host_trace_buffer_show - firmware ring buffer (trace only)
3388  * @cdev: pointer to embedded class device
3389  * @attr: ?
3390  * @buf: the buffer returned
3391  *
3392  * A sysfs 'read/write' shost attribute.
3393  *
3394  * You will only be able to read 4k bytes of ring buffer at a time.
3395  * In order to read beyond 4k bytes, you will have to write out the
3396  * offset to the same attribute, it will move the pointer.
3397  */
3398 static ssize_t
host_trace_buffer_show(struct device * cdev,struct device_attribute * attr,char * buf)3399 host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
3400 	char *buf)
3401 {
3402 	struct Scsi_Host *shost = class_to_shost(cdev);
3403 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3404 	void *request_data;
3405 	u32 size;
3406 
3407 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
3408 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3409 			__func__);
3410 		return 0;
3411 	}
3412 
3413 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3414 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
3415 		ioc_err(ioc, "%s: host_trace_buffer is not registered\n",
3416 			__func__);
3417 		return 0;
3418 	}
3419 
3420 	if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
3421 		return 0;
3422 
3423 	size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
3424 	size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3425 	request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
3426 	memcpy(buf, request_data, size);
3427 	return size;
3428 }
3429 
3430 static ssize_t
host_trace_buffer_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3431 host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
3432 	const char *buf, size_t count)
3433 {
3434 	struct Scsi_Host *shost = class_to_shost(cdev);
3435 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3436 	int val = 0;
3437 
3438 	if (sscanf(buf, "%d", &val) != 1)
3439 		return -EINVAL;
3440 
3441 	ioc->ring_buffer_offset = val;
3442 	return strlen(buf);
3443 }
3444 static DEVICE_ATTR_RW(host_trace_buffer);
3445 
3446 
3447 /*****************************************/
3448 
3449 /**
3450  * host_trace_buffer_enable_show - firmware ring buffer (trace only)
3451  * @cdev: pointer to embedded class device
3452  * @attr: ?
3453  * @buf: the buffer returned
3454  *
3455  * A sysfs 'read/write' shost attribute.
3456  *
3457  * This is a mechnism to post/release host_trace_buffers
3458  */
3459 static ssize_t
host_trace_buffer_enable_show(struct device * cdev,struct device_attribute * attr,char * buf)3460 host_trace_buffer_enable_show(struct device *cdev,
3461 	struct device_attribute *attr, char *buf)
3462 {
3463 	struct Scsi_Host *shost = class_to_shost(cdev);
3464 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3465 
3466 	if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3467 	   ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3468 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3469 		return snprintf(buf, PAGE_SIZE, "off\n");
3470 	else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3471 	    MPT3_DIAG_BUFFER_IS_RELEASED))
3472 		return snprintf(buf, PAGE_SIZE, "release\n");
3473 	else
3474 		return snprintf(buf, PAGE_SIZE, "post\n");
3475 }
3476 
3477 static ssize_t
host_trace_buffer_enable_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3478 host_trace_buffer_enable_store(struct device *cdev,
3479 	struct device_attribute *attr, const char *buf, size_t count)
3480 {
3481 	struct Scsi_Host *shost = class_to_shost(cdev);
3482 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3483 	char str[10] = "";
3484 	struct mpt3_diag_register diag_register;
3485 	u8 issue_reset = 0;
3486 
3487 	/* don't allow post/release occurr while recovery is active */
3488 	if (ioc->shost_recovery || ioc->remove_host ||
3489 	    ioc->pci_error_recovery || ioc->is_driver_loading)
3490 		return -EBUSY;
3491 
3492 	if (sscanf(buf, "%9s", str) != 1)
3493 		return -EINVAL;
3494 
3495 	if (!strcmp(str, "post")) {
3496 		/* exit out if host buffers are already posted */
3497 		if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3498 		    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3499 		    MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3500 		    ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3501 		    MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3502 			goto out;
3503 		memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3504 		ioc_info(ioc, "posting host trace buffers\n");
3505 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3506 
3507 		if (ioc->manu_pg11.HostTraceBufferMaxSizeKB != 0 &&
3508 		    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0) {
3509 			/* post the same buffer allocated previously */
3510 			diag_register.requested_buffer_size =
3511 			    ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE];
3512 		} else {
3513 			/*
3514 			 * Free the diag buffer memory which was previously
3515 			 * allocated by an application.
3516 			 */
3517 			if ((ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE] != 0)
3518 			    &&
3519 			    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3520 			    MPT3_DIAG_BUFFER_IS_APP_OWNED)) {
3521 				dma_free_coherent(&ioc->pdev->dev,
3522 						  ioc->diag_buffer_sz[MPI2_DIAG_BUF_TYPE_TRACE],
3523 						  ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE],
3524 						  ioc->diag_buffer_dma[MPI2_DIAG_BUF_TYPE_TRACE]);
3525 				ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE] =
3526 				    NULL;
3527 			}
3528 
3529 			diag_register.requested_buffer_size = (1024 * 1024);
3530 		}
3531 
3532 		diag_register.unique_id =
3533 		    (ioc->hba_mpi_version_belonged == MPI2_VERSION) ?
3534 		    (MPT2DIAGBUFFUNIQUEID):(MPT3DIAGBUFFUNIQUEID);
3535 		ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3536 		_ctl_diag_register_2(ioc,  &diag_register);
3537 		if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3538 		    MPT3_DIAG_BUFFER_IS_REGISTERED) {
3539 			ioc_info(ioc,
3540 			    "Trace buffer %d KB allocated through sysfs\n",
3541 			    diag_register.requested_buffer_size>>10);
3542 			if (ioc->hba_mpi_version_belonged != MPI2_VERSION)
3543 				ioc->diag_buffer_status[
3544 				    MPI2_DIAG_BUF_TYPE_TRACE] |=
3545 				    MPT3_DIAG_BUFFER_IS_DRIVER_ALLOCATED;
3546 		}
3547 	} else if (!strcmp(str, "release")) {
3548 		/* exit out if host buffers are already released */
3549 		if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3550 			goto out;
3551 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3552 		    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3553 			goto out;
3554 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3555 		    MPT3_DIAG_BUFFER_IS_RELEASED))
3556 			goto out;
3557 		ioc_info(ioc, "releasing host trace buffer\n");
3558 		ioc->htb_rel.buffer_rel_condition = MPT3_DIAG_BUFFER_REL_SYSFS;
3559 		mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3560 		    &issue_reset);
3561 	}
3562 
3563  out:
3564 	return strlen(buf);
3565 }
3566 static DEVICE_ATTR_RW(host_trace_buffer_enable);
3567 
3568 /*********** diagnostic trigger suppport *********************************/
3569 
3570 /**
3571  * diag_trigger_master_show - show the diag_trigger_master attribute
3572  * @cdev: pointer to embedded class device
3573  * @attr: ?
3574  * @buf: the buffer returned
3575  *
3576  * A sysfs 'read/write' shost attribute.
3577  */
3578 static ssize_t
diag_trigger_master_show(struct device * cdev,struct device_attribute * attr,char * buf)3579 diag_trigger_master_show(struct device *cdev,
3580 	struct device_attribute *attr, char *buf)
3581 
3582 {
3583 	struct Scsi_Host *shost = class_to_shost(cdev);
3584 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3585 	unsigned long flags;
3586 	ssize_t rc;
3587 
3588 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3589 	rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3590 	memcpy(buf, &ioc->diag_trigger_master, rc);
3591 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3592 	return rc;
3593 }
3594 
3595 /**
3596  * diag_trigger_master_store - store the diag_trigger_master attribute
3597  * @cdev: pointer to embedded class device
3598  * @attr: ?
3599  * @buf: the buffer returned
3600  * @count: ?
3601  *
3602  * A sysfs 'read/write' shost attribute.
3603  */
3604 static ssize_t
diag_trigger_master_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3605 diag_trigger_master_store(struct device *cdev,
3606 	struct device_attribute *attr, const char *buf, size_t count)
3607 
3608 {
3609 	struct Scsi_Host *shost = class_to_shost(cdev);
3610 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3611 	struct SL_WH_MASTER_TRIGGER_T *master_tg;
3612 	unsigned long flags;
3613 	ssize_t rc;
3614 	bool set = 1;
3615 
3616 	rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3617 
3618 	if (ioc->supports_trigger_pages) {
3619 		master_tg = kzalloc(sizeof(struct SL_WH_MASTER_TRIGGER_T),
3620 		    GFP_KERNEL);
3621 		if (!master_tg)
3622 			return -ENOMEM;
3623 
3624 		memcpy(master_tg, buf, rc);
3625 		if (!master_tg->MasterData)
3626 			set = 0;
3627 		if (mpt3sas_config_update_driver_trigger_pg1(ioc, master_tg,
3628 		    set)) {
3629 			kfree(master_tg);
3630 			return -EFAULT;
3631 		}
3632 		kfree(master_tg);
3633 	}
3634 
3635 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3636 	memset(&ioc->diag_trigger_master, 0,
3637 	    sizeof(struct SL_WH_MASTER_TRIGGER_T));
3638 	memcpy(&ioc->diag_trigger_master, buf, rc);
3639 	ioc->diag_trigger_master.MasterData |=
3640 	    (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3641 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3642 	return rc;
3643 }
3644 static DEVICE_ATTR_RW(diag_trigger_master);
3645 
3646 
3647 /**
3648  * diag_trigger_event_show - show the diag_trigger_event attribute
3649  * @cdev: pointer to embedded class device
3650  * @attr: ?
3651  * @buf: the buffer returned
3652  *
3653  * A sysfs 'read/write' shost attribute.
3654  */
3655 static ssize_t
diag_trigger_event_show(struct device * cdev,struct device_attribute * attr,char * buf)3656 diag_trigger_event_show(struct device *cdev,
3657 	struct device_attribute *attr, char *buf)
3658 {
3659 	struct Scsi_Host *shost = class_to_shost(cdev);
3660 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3661 	unsigned long flags;
3662 	ssize_t rc;
3663 
3664 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3665 	rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3666 	memcpy(buf, &ioc->diag_trigger_event, rc);
3667 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3668 	return rc;
3669 }
3670 
3671 /**
3672  * diag_trigger_event_store - store the diag_trigger_event attribute
3673  * @cdev: pointer to embedded class device
3674  * @attr: ?
3675  * @buf: the buffer returned
3676  * @count: ?
3677  *
3678  * A sysfs 'read/write' shost attribute.
3679  */
3680 static ssize_t
diag_trigger_event_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3681 diag_trigger_event_store(struct device *cdev,
3682 	struct device_attribute *attr, const char *buf, size_t count)
3683 
3684 {
3685 	struct Scsi_Host *shost = class_to_shost(cdev);
3686 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3687 	struct SL_WH_EVENT_TRIGGERS_T *event_tg;
3688 	unsigned long flags;
3689 	ssize_t sz;
3690 	bool set = 1;
3691 
3692 	sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3693 	if (ioc->supports_trigger_pages) {
3694 		event_tg = kzalloc(sizeof(struct SL_WH_EVENT_TRIGGERS_T),
3695 		    GFP_KERNEL);
3696 		if (!event_tg)
3697 			return -ENOMEM;
3698 
3699 		memcpy(event_tg, buf, sz);
3700 		if (!event_tg->ValidEntries)
3701 			set = 0;
3702 		if (mpt3sas_config_update_driver_trigger_pg2(ioc, event_tg,
3703 		    set)) {
3704 			kfree(event_tg);
3705 			return -EFAULT;
3706 		}
3707 		kfree(event_tg);
3708 	}
3709 
3710 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3711 
3712 	memset(&ioc->diag_trigger_event, 0,
3713 	    sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3714 	memcpy(&ioc->diag_trigger_event, buf, sz);
3715 	if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3716 		ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3717 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3718 	return sz;
3719 }
3720 static DEVICE_ATTR_RW(diag_trigger_event);
3721 
3722 
3723 /**
3724  * diag_trigger_scsi_show - show the diag_trigger_scsi attribute
3725  * @cdev: pointer to embedded class device
3726  * @attr: ?
3727  * @buf: the buffer returned
3728  *
3729  * A sysfs 'read/write' shost attribute.
3730  */
3731 static ssize_t
diag_trigger_scsi_show(struct device * cdev,struct device_attribute * attr,char * buf)3732 diag_trigger_scsi_show(struct device *cdev,
3733 	struct device_attribute *attr, char *buf)
3734 {
3735 	struct Scsi_Host *shost = class_to_shost(cdev);
3736 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3737 	unsigned long flags;
3738 	ssize_t rc;
3739 
3740 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3741 	rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3742 	memcpy(buf, &ioc->diag_trigger_scsi, rc);
3743 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3744 	return rc;
3745 }
3746 
3747 /**
3748  * diag_trigger_scsi_store - store the diag_trigger_scsi attribute
3749  * @cdev: pointer to embedded class device
3750  * @attr: ?
3751  * @buf: the buffer returned
3752  * @count: ?
3753  *
3754  * A sysfs 'read/write' shost attribute.
3755  */
3756 static ssize_t
diag_trigger_scsi_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3757 diag_trigger_scsi_store(struct device *cdev,
3758 	struct device_attribute *attr, const char *buf, size_t count)
3759 {
3760 	struct Scsi_Host *shost = class_to_shost(cdev);
3761 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3762 	struct SL_WH_SCSI_TRIGGERS_T *scsi_tg;
3763 	unsigned long flags;
3764 	ssize_t sz;
3765 	bool set = 1;
3766 
3767 	sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
3768 	if (ioc->supports_trigger_pages) {
3769 		scsi_tg = kzalloc(sizeof(struct SL_WH_SCSI_TRIGGERS_T),
3770 		    GFP_KERNEL);
3771 		if (!scsi_tg)
3772 			return -ENOMEM;
3773 
3774 		memcpy(scsi_tg, buf, sz);
3775 		if (!scsi_tg->ValidEntries)
3776 			set = 0;
3777 		if (mpt3sas_config_update_driver_trigger_pg3(ioc, scsi_tg,
3778 		    set)) {
3779 			kfree(scsi_tg);
3780 			return -EFAULT;
3781 		}
3782 		kfree(scsi_tg);
3783 	}
3784 
3785 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3786 
3787 	memset(&ioc->diag_trigger_scsi, 0, sizeof(ioc->diag_trigger_scsi));
3788 	memcpy(&ioc->diag_trigger_scsi, buf, sz);
3789 	if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3790 		ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3791 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3792 	return sz;
3793 }
3794 static DEVICE_ATTR_RW(diag_trigger_scsi);
3795 
3796 
3797 /**
3798  * diag_trigger_mpi_show - show the diag_trigger_mpi attribute
3799  * @cdev: pointer to embedded class device
3800  * @attr: ?
3801  * @buf: the buffer returned
3802  *
3803  * A sysfs 'read/write' shost attribute.
3804  */
3805 static ssize_t
diag_trigger_mpi_show(struct device * cdev,struct device_attribute * attr,char * buf)3806 diag_trigger_mpi_show(struct device *cdev,
3807 	struct device_attribute *attr, char *buf)
3808 {
3809 	struct Scsi_Host *shost = class_to_shost(cdev);
3810 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3811 	unsigned long flags;
3812 	ssize_t rc;
3813 
3814 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3815 	rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3816 	memcpy(buf, &ioc->diag_trigger_mpi, rc);
3817 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3818 	return rc;
3819 }
3820 
3821 /**
3822  * diag_trigger_mpi_store - store the diag_trigger_mpi attribute
3823  * @cdev: pointer to embedded class device
3824  * @attr: ?
3825  * @buf: the buffer returned
3826  * @count: ?
3827  *
3828  * A sysfs 'read/write' shost attribute.
3829  */
3830 static ssize_t
diag_trigger_mpi_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3831 diag_trigger_mpi_store(struct device *cdev,
3832 	struct device_attribute *attr, const char *buf, size_t count)
3833 {
3834 	struct Scsi_Host *shost = class_to_shost(cdev);
3835 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3836 	struct SL_WH_MPI_TRIGGERS_T *mpi_tg;
3837 	unsigned long flags;
3838 	ssize_t sz;
3839 	bool set = 1;
3840 
3841 	sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3842 	if (ioc->supports_trigger_pages) {
3843 		mpi_tg = kzalloc(sizeof(struct SL_WH_MPI_TRIGGERS_T),
3844 		    GFP_KERNEL);
3845 		if (!mpi_tg)
3846 			return -ENOMEM;
3847 
3848 		memcpy(mpi_tg, buf, sz);
3849 		if (!mpi_tg->ValidEntries)
3850 			set = 0;
3851 		if (mpt3sas_config_update_driver_trigger_pg4(ioc, mpi_tg,
3852 		    set)) {
3853 			kfree(mpi_tg);
3854 			return -EFAULT;
3855 		}
3856 		kfree(mpi_tg);
3857 	}
3858 
3859 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3860 	memset(&ioc->diag_trigger_mpi, 0,
3861 	    sizeof(ioc->diag_trigger_mpi));
3862 	memcpy(&ioc->diag_trigger_mpi, buf, sz);
3863 	if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3864 		ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3865 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3866 	return sz;
3867 }
3868 
3869 static DEVICE_ATTR_RW(diag_trigger_mpi);
3870 
3871 /*********** diagnostic trigger suppport *** END ****************************/
3872 
3873 /*****************************************/
3874 
3875 /**
3876  * drv_support_bitmap_show - driver supported feature bitmap
3877  * @cdev: pointer to embedded class device
3878  * @attr: unused
3879  * @buf: the buffer returned
3880  *
3881  * A sysfs 'read-only' shost attribute.
3882  */
3883 static ssize_t
drv_support_bitmap_show(struct device * cdev,struct device_attribute * attr,char * buf)3884 drv_support_bitmap_show(struct device *cdev,
3885 	struct device_attribute *attr, char *buf)
3886 {
3887 	struct Scsi_Host *shost = class_to_shost(cdev);
3888 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3889 
3890 	return snprintf(buf, PAGE_SIZE, "0x%08x\n", ioc->drv_support_bitmap);
3891 }
3892 static DEVICE_ATTR_RO(drv_support_bitmap);
3893 
3894 /**
3895  * enable_sdev_max_qd_show - display whether sdev max qd is enabled/disabled
3896  * @cdev: pointer to embedded class device
3897  * @attr: unused
3898  * @buf: the buffer returned
3899  *
3900  * A sysfs read/write shost attribute. This attribute is used to set the
3901  * targets queue depth to HBA IO queue depth if this attribute is enabled.
3902  */
3903 static ssize_t
enable_sdev_max_qd_show(struct device * cdev,struct device_attribute * attr,char * buf)3904 enable_sdev_max_qd_show(struct device *cdev,
3905 	struct device_attribute *attr, char *buf)
3906 {
3907 	struct Scsi_Host *shost = class_to_shost(cdev);
3908 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3909 
3910 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->enable_sdev_max_qd);
3911 }
3912 
3913 /**
3914  * enable_sdev_max_qd_store - Enable/disable sdev max qd
3915  * @cdev: pointer to embedded class device
3916  * @attr: unused
3917  * @buf: the buffer returned
3918  * @count: unused
3919  *
3920  * A sysfs read/write shost attribute. This attribute is used to set the
3921  * targets queue depth to HBA IO queue depth if this attribute is enabled.
3922  * If this attribute is disabled then targets will have corresponding default
3923  * queue depth.
3924  */
3925 static ssize_t
enable_sdev_max_qd_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)3926 enable_sdev_max_qd_store(struct device *cdev,
3927 	struct device_attribute *attr, const char *buf, size_t count)
3928 {
3929 	struct Scsi_Host *shost = class_to_shost(cdev);
3930 	struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3931 	struct MPT3SAS_DEVICE *sas_device_priv_data;
3932 	struct MPT3SAS_TARGET *sas_target_priv_data;
3933 	int val = 0;
3934 	struct scsi_device *sdev;
3935 	struct _raid_device *raid_device;
3936 	int qdepth;
3937 
3938 	if (kstrtoint(buf, 0, &val) != 0)
3939 		return -EINVAL;
3940 
3941 	switch (val) {
3942 	case 0:
3943 		ioc->enable_sdev_max_qd = 0;
3944 		shost_for_each_device(sdev, ioc->shost) {
3945 			sas_device_priv_data = sdev->hostdata;
3946 			if (!sas_device_priv_data)
3947 				continue;
3948 			sas_target_priv_data = sas_device_priv_data->sas_target;
3949 			if (!sas_target_priv_data)
3950 				continue;
3951 
3952 			if (sas_target_priv_data->flags &
3953 			    MPT_TARGET_FLAGS_VOLUME) {
3954 				raid_device =
3955 				    mpt3sas_raid_device_find_by_handle(ioc,
3956 				    sas_target_priv_data->handle);
3957 
3958 				switch (raid_device->volume_type) {
3959 				case MPI2_RAID_VOL_TYPE_RAID0:
3960 					if (raid_device->device_info &
3961 					    MPI2_SAS_DEVICE_INFO_SSP_TARGET)
3962 						qdepth =
3963 						    MPT3SAS_SAS_QUEUE_DEPTH;
3964 					else
3965 						qdepth =
3966 						    MPT3SAS_SATA_QUEUE_DEPTH;
3967 					break;
3968 				case MPI2_RAID_VOL_TYPE_RAID1E:
3969 				case MPI2_RAID_VOL_TYPE_RAID1:
3970 				case MPI2_RAID_VOL_TYPE_RAID10:
3971 				case MPI2_RAID_VOL_TYPE_UNKNOWN:
3972 				default:
3973 					qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
3974 				}
3975 			} else if (sas_target_priv_data->flags &
3976 			    MPT_TARGET_FLAGS_PCIE_DEVICE)
3977 				qdepth = ioc->max_nvme_qd;
3978 			else
3979 				qdepth = (sas_target_priv_data->sas_dev->port_type > 1) ?
3980 				    ioc->max_wideport_qd : ioc->max_narrowport_qd;
3981 
3982 			mpt3sas_scsih_change_queue_depth(sdev, qdepth);
3983 		}
3984 		break;
3985 	case 1:
3986 		ioc->enable_sdev_max_qd = 1;
3987 		shost_for_each_device(sdev, ioc->shost)
3988 			mpt3sas_scsih_change_queue_depth(sdev,
3989 			    shost->can_queue);
3990 		break;
3991 	default:
3992 		return -EINVAL;
3993 	}
3994 
3995 	return strlen(buf);
3996 }
3997 static DEVICE_ATTR_RW(enable_sdev_max_qd);
3998 
3999 static struct attribute *mpt3sas_host_attrs[] = {
4000 	&dev_attr_version_fw.attr,
4001 	&dev_attr_version_bios.attr,
4002 	&dev_attr_version_mpi.attr,
4003 	&dev_attr_version_product.attr,
4004 	&dev_attr_version_nvdata_persistent.attr,
4005 	&dev_attr_version_nvdata_default.attr,
4006 	&dev_attr_board_name.attr,
4007 	&dev_attr_board_assembly.attr,
4008 	&dev_attr_board_tracer.attr,
4009 	&dev_attr_io_delay.attr,
4010 	&dev_attr_device_delay.attr,
4011 	&dev_attr_logging_level.attr,
4012 	&dev_attr_fwfault_debug.attr,
4013 	&dev_attr_fw_queue_depth.attr,
4014 	&dev_attr_host_sas_address.attr,
4015 	&dev_attr_ioc_reset_count.attr,
4016 	&dev_attr_host_trace_buffer_size.attr,
4017 	&dev_attr_host_trace_buffer.attr,
4018 	&dev_attr_host_trace_buffer_enable.attr,
4019 	&dev_attr_reply_queue_count.attr,
4020 	&dev_attr_diag_trigger_master.attr,
4021 	&dev_attr_diag_trigger_event.attr,
4022 	&dev_attr_diag_trigger_scsi.attr,
4023 	&dev_attr_diag_trigger_mpi.attr,
4024 	&dev_attr_drv_support_bitmap.attr,
4025 	&dev_attr_BRM_status.attr,
4026 	&dev_attr_enable_sdev_max_qd.attr,
4027 	NULL,
4028 };
4029 
4030 static const struct attribute_group mpt3sas_host_attr_group = {
4031 	.attrs = mpt3sas_host_attrs
4032 };
4033 
4034 const struct attribute_group *mpt3sas_host_groups[] = {
4035 	&mpt3sas_host_attr_group,
4036 	NULL
4037 };
4038 
4039 /* device attributes */
4040 
4041 /**
4042  * sas_address_show - sas address
4043  * @dev: pointer to embedded class device
4044  * @attr: ?
4045  * @buf: the buffer returned
4046  *
4047  * This is the sas address for the target
4048  *
4049  * A sysfs 'read-only' shost attribute.
4050  */
4051 static ssize_t
sas_address_show(struct device * dev,struct device_attribute * attr,char * buf)4052 sas_address_show(struct device *dev, struct device_attribute *attr,
4053 	char *buf)
4054 {
4055 	struct scsi_device *sdev = to_scsi_device(dev);
4056 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4057 
4058 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
4059 	    (unsigned long long)sas_device_priv_data->sas_target->sas_address);
4060 }
4061 static DEVICE_ATTR_RO(sas_address);
4062 
4063 /**
4064  * sas_device_handle_show - device handle
4065  * @dev: pointer to embedded class device
4066  * @attr: ?
4067  * @buf: the buffer returned
4068  *
4069  * This is the firmware assigned device handle
4070  *
4071  * A sysfs 'read-only' shost attribute.
4072  */
4073 static ssize_t
sas_device_handle_show(struct device * dev,struct device_attribute * attr,char * buf)4074 sas_device_handle_show(struct device *dev, struct device_attribute *attr,
4075 	char *buf)
4076 {
4077 	struct scsi_device *sdev = to_scsi_device(dev);
4078 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4079 
4080 	return snprintf(buf, PAGE_SIZE, "0x%04x\n",
4081 	    sas_device_priv_data->sas_target->handle);
4082 }
4083 static DEVICE_ATTR_RO(sas_device_handle);
4084 
4085 /**
4086  * sas_ncq_prio_supported_show - Indicate if device supports NCQ priority
4087  * @dev: pointer to embedded device
4088  * @attr: sas_ncq_prio_supported attribute descriptor
4089  * @buf: the buffer returned
4090  *
4091  * A sysfs 'read-only' sdev attribute, only works with SATA
4092  */
4093 static ssize_t
sas_ncq_prio_supported_show(struct device * dev,struct device_attribute * attr,char * buf)4094 sas_ncq_prio_supported_show(struct device *dev,
4095 			    struct device_attribute *attr, char *buf)
4096 {
4097 	struct scsi_device *sdev = to_scsi_device(dev);
4098 
4099 	return sysfs_emit(buf, "%d\n", sas_ata_ncq_prio_supported(sdev));
4100 }
4101 static DEVICE_ATTR_RO(sas_ncq_prio_supported);
4102 
4103 /**
4104  * sas_ncq_prio_enable_show - send prioritized io commands to device
4105  * @dev: pointer to embedded device
4106  * @attr: ?
4107  * @buf: the buffer returned
4108  *
4109  * A sysfs 'read/write' sdev attribute, only works with SATA
4110  */
4111 static ssize_t
sas_ncq_prio_enable_show(struct device * dev,struct device_attribute * attr,char * buf)4112 sas_ncq_prio_enable_show(struct device *dev,
4113 				 struct device_attribute *attr, char *buf)
4114 {
4115 	struct scsi_device *sdev = to_scsi_device(dev);
4116 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4117 
4118 	return snprintf(buf, PAGE_SIZE, "%d\n",
4119 			sas_device_priv_data->ncq_prio_enable);
4120 }
4121 
4122 static ssize_t
sas_ncq_prio_enable_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)4123 sas_ncq_prio_enable_store(struct device *dev,
4124 				  struct device_attribute *attr,
4125 				  const char *buf, size_t count)
4126 {
4127 	struct scsi_device *sdev = to_scsi_device(dev);
4128 	struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
4129 	bool ncq_prio_enable = 0;
4130 
4131 	if (kstrtobool(buf, &ncq_prio_enable))
4132 		return -EINVAL;
4133 
4134 	if (!sas_ata_ncq_prio_supported(sdev))
4135 		return -EINVAL;
4136 
4137 	sas_device_priv_data->ncq_prio_enable = ncq_prio_enable;
4138 	return strlen(buf);
4139 }
4140 static DEVICE_ATTR_RW(sas_ncq_prio_enable);
4141 
4142 static struct attribute *mpt3sas_dev_attrs[] = {
4143 	&dev_attr_sas_address.attr,
4144 	&dev_attr_sas_device_handle.attr,
4145 	&dev_attr_sas_ncq_prio_supported.attr,
4146 	&dev_attr_sas_ncq_prio_enable.attr,
4147 	NULL,
4148 };
4149 
4150 static const struct attribute_group mpt3sas_dev_attr_group = {
4151 	.attrs = mpt3sas_dev_attrs
4152 };
4153 
4154 const struct attribute_group *mpt3sas_dev_groups[] = {
4155 	&mpt3sas_dev_attr_group,
4156 	NULL
4157 };
4158 
4159 /* file operations table for mpt3ctl device */
4160 static const struct file_operations ctl_fops = {
4161 	.owner = THIS_MODULE,
4162 	.unlocked_ioctl = _ctl_ioctl,
4163 	.poll = _ctl_poll,
4164 	.fasync = _ctl_fasync,
4165 #ifdef CONFIG_COMPAT
4166 	.compat_ioctl = _ctl_ioctl_compat,
4167 #endif
4168 };
4169 
4170 /* file operations table for mpt2ctl device */
4171 static const struct file_operations ctl_gen2_fops = {
4172 	.owner = THIS_MODULE,
4173 	.unlocked_ioctl = _ctl_mpt2_ioctl,
4174 	.poll = _ctl_poll,
4175 	.fasync = _ctl_fasync,
4176 #ifdef CONFIG_COMPAT
4177 	.compat_ioctl = _ctl_mpt2_ioctl_compat,
4178 #endif
4179 };
4180 
4181 static struct miscdevice ctl_dev = {
4182 	.minor  = MPT3SAS_MINOR,
4183 	.name   = MPT3SAS_DEV_NAME,
4184 	.fops   = &ctl_fops,
4185 };
4186 
4187 static struct miscdevice gen2_ctl_dev = {
4188 	.minor  = MPT2SAS_MINOR,
4189 	.name   = MPT2SAS_DEV_NAME,
4190 	.fops   = &ctl_gen2_fops,
4191 };
4192 
4193 /**
4194  * mpt3sas_ctl_init - main entry point for ctl.
4195  * @hbas_to_enumerate: ?
4196  */
4197 void
mpt3sas_ctl_init(ushort hbas_to_enumerate)4198 mpt3sas_ctl_init(ushort hbas_to_enumerate)
4199 {
4200 	async_queue = NULL;
4201 
4202 	/* Don't register mpt3ctl ioctl device if
4203 	 * hbas_to_enumarate is one.
4204 	 */
4205 	if (hbas_to_enumerate != 1)
4206 		if (misc_register(&ctl_dev) < 0)
4207 			pr_err("%s can't register misc device [minor=%d]\n",
4208 			    MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
4209 
4210 	/* Don't register mpt3ctl ioctl device if
4211 	 * hbas_to_enumarate is two.
4212 	 */
4213 	if (hbas_to_enumerate != 2)
4214 		if (misc_register(&gen2_ctl_dev) < 0)
4215 			pr_err("%s can't register misc device [minor=%d]\n",
4216 			    MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
4217 
4218 	init_waitqueue_head(&ctl_poll_wait);
4219 }
4220 
4221 /**
4222  * mpt3sas_ctl_release - release dma for ctl
4223  * @ioc: per adapter object
4224  */
4225 void
mpt3sas_ctl_release(struct MPT3SAS_ADAPTER * ioc)4226 mpt3sas_ctl_release(struct MPT3SAS_ADAPTER *ioc)
4227 {
4228 	int i;
4229 
4230 	/* free memory associated to diag buffers */
4231 	for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
4232 		if (!ioc->diag_buffer[i])
4233 			continue;
4234 		dma_free_coherent(&ioc->pdev->dev,
4235 				  ioc->diag_buffer_sz[i],
4236 				  ioc->diag_buffer[i],
4237 				  ioc->diag_buffer_dma[i]);
4238 		ioc->diag_buffer[i] = NULL;
4239 		ioc->diag_buffer_status[i] = 0;
4240 	}
4241 
4242 	kfree(ioc->event_log);
4243 }
4244 
4245 /**
4246  * mpt3sas_ctl_exit - exit point for ctl
4247  * @hbas_to_enumerate: ?
4248  */
4249 void
mpt3sas_ctl_exit(ushort hbas_to_enumerate)4250 mpt3sas_ctl_exit(ushort hbas_to_enumerate)
4251 {
4252 
4253 	if (hbas_to_enumerate != 1)
4254 		misc_deregister(&ctl_dev);
4255 	if (hbas_to_enumerate != 2)
4256 		misc_deregister(&gen2_ctl_dev);
4257 }
4258