• 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/mpt2sas/mpt2_ctl.c
6  * Copyright (C) 2007-2012  LSI Corporation
7  *  (mailto:DL-MPTFusionLinux@lsi.com)
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * NO WARRANTY
20  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24  * solely responsible for determining the appropriateness of using and
25  * distributing the Program and assumes all risks associated with its
26  * exercise of rights under this Agreement, including but not limited to
27  * the risks and costs of program errors, damage to or loss of data,
28  * programs or equipment, and unavailability or interruption of operations.
29 
30  * DISCLAIMER OF LIABILITY
31  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38 
39  * You should have received a copy of the GNU General Public License
40  * along with this program; if not, write to the Free Software
41  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
42  * USA.
43  */
44 
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/errno.h>
48 #include <linux/init.h>
49 #include <linux/slab.h>
50 #include <linux/types.h>
51 #include <linux/pci.h>
52 #include <linux/delay.h>
53 #include <linux/mutex.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 "mpt2sas_base.h"
61 #include "mpt2sas_ctl.h"
62 
63 static DEFINE_MUTEX(_ctl_mutex);
64 static struct fasync_struct *async_queue;
65 static DECLARE_WAIT_QUEUE_HEAD(ctl_poll_wait);
66 
67 static int _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type,
68     u8 *issue_reset);
69 
70 /**
71  * enum block_state - blocking state
72  * @NON_BLOCKING: non blocking
73  * @BLOCKING: blocking
74  *
75  * These states are for ioctls that need to wait for a response
76  * from firmware, so they probably require sleep.
77  */
78 enum block_state {
79 	NON_BLOCKING,
80 	BLOCKING,
81 };
82 
83 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
84 /**
85  * _ctl_sas_device_find_by_handle - sas device search
86  * @ioc: per adapter object
87  * @handle: sas device handle (assigned by firmware)
88  * Context: Calling function should acquire ioc->sas_device_lock
89  *
90  * This searches for sas_device based on sas_address, then return sas_device
91  * object.
92  */
93 static struct _sas_device *
_ctl_sas_device_find_by_handle(struct MPT2SAS_ADAPTER * ioc,u16 handle)94 _ctl_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
95 {
96 	struct _sas_device *sas_device, *r;
97 
98 	r = NULL;
99 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
100 		if (sas_device->handle != handle)
101 			continue;
102 		r = sas_device;
103 		goto out;
104 	}
105 
106  out:
107 	return r;
108 }
109 
110 /**
111  * _ctl_display_some_debug - debug routine
112  * @ioc: per adapter object
113  * @smid: system request message index
114  * @calling_function_name: string pass from calling function
115  * @mpi_reply: reply message frame
116  * Context: none.
117  *
118  * Function for displaying debug info helpful when debugging issues
119  * in this module.
120  */
121 static void
_ctl_display_some_debug(struct MPT2SAS_ADAPTER * ioc,u16 smid,char * calling_function_name,MPI2DefaultReply_t * mpi_reply)122 _ctl_display_some_debug(struct MPT2SAS_ADAPTER *ioc, u16 smid,
123     char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
124 {
125 	Mpi2ConfigRequest_t *mpi_request;
126 	char *desc = NULL;
127 
128 	if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
129 		return;
130 
131 	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
132 	switch (mpi_request->Function) {
133 	case MPI2_FUNCTION_SCSI_IO_REQUEST:
134 	{
135 		Mpi2SCSIIORequest_t *scsi_request =
136 		    (Mpi2SCSIIORequest_t *)mpi_request;
137 
138 		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
139 		    "scsi_io, cmd(0x%02x), cdb_len(%d)",
140 		    scsi_request->CDB.CDB32[0],
141 		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
142 		desc = ioc->tmp_string;
143 		break;
144 	}
145 	case MPI2_FUNCTION_SCSI_TASK_MGMT:
146 		desc = "task_mgmt";
147 		break;
148 	case MPI2_FUNCTION_IOC_INIT:
149 		desc = "ioc_init";
150 		break;
151 	case MPI2_FUNCTION_IOC_FACTS:
152 		desc = "ioc_facts";
153 		break;
154 	case MPI2_FUNCTION_CONFIG:
155 	{
156 		Mpi2ConfigRequest_t *config_request =
157 		    (Mpi2ConfigRequest_t *)mpi_request;
158 
159 		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
160 		    "config, type(0x%02x), ext_type(0x%02x), number(%d)",
161 		    (config_request->Header.PageType &
162 		     MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
163 		    config_request->Header.PageNumber);
164 		desc = ioc->tmp_string;
165 		break;
166 	}
167 	case MPI2_FUNCTION_PORT_FACTS:
168 		desc = "port_facts";
169 		break;
170 	case MPI2_FUNCTION_PORT_ENABLE:
171 		desc = "port_enable";
172 		break;
173 	case MPI2_FUNCTION_EVENT_NOTIFICATION:
174 		desc = "event_notification";
175 		break;
176 	case MPI2_FUNCTION_FW_DOWNLOAD:
177 		desc = "fw_download";
178 		break;
179 	case MPI2_FUNCTION_FW_UPLOAD:
180 		desc = "fw_upload";
181 		break;
182 	case MPI2_FUNCTION_RAID_ACTION:
183 		desc = "raid_action";
184 		break;
185 	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
186 	{
187 		Mpi2SCSIIORequest_t *scsi_request =
188 		    (Mpi2SCSIIORequest_t *)mpi_request;
189 
190 		snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
191 		    "raid_pass, cmd(0x%02x), cdb_len(%d)",
192 		    scsi_request->CDB.CDB32[0],
193 		    le16_to_cpu(scsi_request->IoFlags) & 0xF);
194 		desc = ioc->tmp_string;
195 		break;
196 	}
197 	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
198 		desc = "sas_iounit_cntl";
199 		break;
200 	case MPI2_FUNCTION_SATA_PASSTHROUGH:
201 		desc = "sata_pass";
202 		break;
203 	case MPI2_FUNCTION_DIAG_BUFFER_POST:
204 		desc = "diag_buffer_post";
205 		break;
206 	case MPI2_FUNCTION_DIAG_RELEASE:
207 		desc = "diag_release";
208 		break;
209 	case MPI2_FUNCTION_SMP_PASSTHROUGH:
210 		desc = "smp_passthrough";
211 		break;
212 	}
213 
214 	if (!desc)
215 		return;
216 
217 	printk(MPT2SAS_INFO_FMT "%s: %s, smid(%d)\n",
218 	    ioc->name, calling_function_name, desc, smid);
219 
220 	if (!mpi_reply)
221 		return;
222 
223 	if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
224 		printk(MPT2SAS_INFO_FMT
225 		    "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
226 		    ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
227 		    le32_to_cpu(mpi_reply->IOCLogInfo));
228 
229 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
230 	    mpi_request->Function ==
231 	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
232 		Mpi2SCSIIOReply_t *scsi_reply =
233 		    (Mpi2SCSIIOReply_t *)mpi_reply;
234 		struct _sas_device *sas_device = NULL;
235 		unsigned long flags;
236 
237 		spin_lock_irqsave(&ioc->sas_device_lock, flags);
238 		sas_device = _ctl_sas_device_find_by_handle(ioc,
239 		    le16_to_cpu(scsi_reply->DevHandle));
240 		if (sas_device) {
241 			printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
242 			    "phy(%d)\n", ioc->name, (unsigned long long)
243 			    sas_device->sas_address, sas_device->phy);
244 			printk(MPT2SAS_WARN_FMT
245 			    "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
246 			    ioc->name, sas_device->enclosure_logical_id,
247 			    sas_device->slot);
248 		}
249 		spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
250 		if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
251 			printk(MPT2SAS_INFO_FMT
252 			    "\tscsi_state(0x%02x), scsi_status"
253 			    "(0x%02x)\n", ioc->name,
254 			    scsi_reply->SCSIState,
255 			    scsi_reply->SCSIStatus);
256 	}
257 }
258 #endif
259 
260 /**
261  * mpt2sas_ctl_done - ctl module completion routine
262  * @ioc: per adapter object
263  * @smid: system request message index
264  * @msix_index: MSIX table index supplied by the OS
265  * @reply: reply message frame(lower 32bit addr)
266  * Context: none.
267  *
268  * The callback handler when using ioc->ctl_cb_idx.
269  *
270  * Return 1 meaning mf should be freed from _base_interrupt
271  *        0 means the mf is freed from this function.
272  */
273 u8
mpt2sas_ctl_done(struct MPT2SAS_ADAPTER * ioc,u16 smid,u8 msix_index,u32 reply)274 mpt2sas_ctl_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
275 	u32 reply)
276 {
277 	MPI2DefaultReply_t *mpi_reply;
278 	Mpi2SCSIIOReply_t *scsiio_reply;
279 	const void *sense_data;
280 	u32 sz;
281 
282 	if (ioc->ctl_cmds.status == MPT2_CMD_NOT_USED)
283 		return 1;
284 	if (ioc->ctl_cmds.smid != smid)
285 		return 1;
286 	ioc->ctl_cmds.status |= MPT2_CMD_COMPLETE;
287 	mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
288 	if (mpi_reply) {
289 		memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
290 		ioc->ctl_cmds.status |= MPT2_CMD_REPLY_VALID;
291 		/* get sense data */
292 		if (mpi_reply->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
293 		    mpi_reply->Function ==
294 		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
295 			scsiio_reply = (Mpi2SCSIIOReply_t *)mpi_reply;
296 			if (scsiio_reply->SCSIState &
297 			    MPI2_SCSI_STATE_AUTOSENSE_VALID) {
298 				sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
299 				    le32_to_cpu(scsiio_reply->SenseCount));
300 				sense_data = mpt2sas_base_get_sense_buffer(ioc,
301 				    smid);
302 				memcpy(ioc->ctl_cmds.sense, sense_data, sz);
303 			}
304 		}
305 	}
306 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
307 	_ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
308 #endif
309 	ioc->ctl_cmds.status &= ~MPT2_CMD_PENDING;
310 	complete(&ioc->ctl_cmds.done);
311 	return 1;
312 }
313 
314 /**
315  * _ctl_check_event_type - determines when an event needs logging
316  * @ioc: per adapter object
317  * @event: firmware event
318  *
319  * The bitmask in ioc->event_type[] indicates which events should be
320  * be saved in the driver event_log.  This bitmask is set by application.
321  *
322  * Returns 1 when event should be captured, or zero means no match.
323  */
324 static int
_ctl_check_event_type(struct MPT2SAS_ADAPTER * ioc,u16 event)325 _ctl_check_event_type(struct MPT2SAS_ADAPTER *ioc, u16 event)
326 {
327 	u16 i;
328 	u32 desired_event;
329 
330 	if (event >= 128 || !event || !ioc->event_log)
331 		return 0;
332 
333 	desired_event = (1 << (event % 32));
334 	if (!desired_event)
335 		desired_event = 1;
336 	i = event / 32;
337 	return desired_event & ioc->event_type[i];
338 }
339 
340 /**
341  * mpt2sas_ctl_add_to_event_log - add event
342  * @ioc: per adapter object
343  * @mpi_reply: reply message frame
344  *
345  * Return nothing.
346  */
347 void
mpt2sas_ctl_add_to_event_log(struct MPT2SAS_ADAPTER * ioc,Mpi2EventNotificationReply_t * mpi_reply)348 mpt2sas_ctl_add_to_event_log(struct MPT2SAS_ADAPTER *ioc,
349     Mpi2EventNotificationReply_t *mpi_reply)
350 {
351 	struct MPT2_IOCTL_EVENTS *event_log;
352 	u16 event;
353 	int i;
354 	u32 sz, event_data_sz;
355 	u8 send_aen = 0;
356 
357 	if (!ioc->event_log)
358 		return;
359 
360 	event = le16_to_cpu(mpi_reply->Event);
361 
362 	if (_ctl_check_event_type(ioc, event)) {
363 
364 		/* insert entry into circular event_log */
365 		i = ioc->event_context % MPT2SAS_CTL_EVENT_LOG_SIZE;
366 		event_log = ioc->event_log;
367 		event_log[i].event = event;
368 		event_log[i].context = ioc->event_context++;
369 
370 		event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
371 		sz = min_t(u32, event_data_sz, MPT2_EVENT_DATA_SIZE);
372 		memset(event_log[i].data, 0, MPT2_EVENT_DATA_SIZE);
373 		memcpy(event_log[i].data, mpi_reply->EventData, sz);
374 		send_aen = 1;
375 	}
376 
377 	/* This aen_event_read_flag flag is set until the
378 	 * application has read the event log.
379 	 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
380 	 */
381 	if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
382 	    (send_aen && !ioc->aen_event_read_flag)) {
383 		ioc->aen_event_read_flag = 1;
384 		wake_up_interruptible(&ctl_poll_wait);
385 		if (async_queue)
386 			kill_fasync(&async_queue, SIGIO, POLL_IN);
387 	}
388 }
389 
390 /**
391  * mpt2sas_ctl_event_callback - firmware event handler (called at ISR time)
392  * @ioc: per adapter object
393  * @msix_index: MSIX table index supplied by the OS
394  * @reply: reply message frame(lower 32bit addr)
395  * Context: interrupt.
396  *
397  * This function merely adds a new work task into ioc->firmware_event_thread.
398  * The tasks are worked from _firmware_event_work in user context.
399  *
400  * Return 1 meaning mf should be freed from _base_interrupt
401  *        0 means the mf is freed from this function.
402  */
403 u8
mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER * ioc,u8 msix_index,u32 reply)404 mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
405 	u32 reply)
406 {
407 	Mpi2EventNotificationReply_t *mpi_reply;
408 
409 	mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
410 	mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
411 	return 1;
412 }
413 
414 /**
415  * _ctl_verify_adapter - validates ioc_number passed from application
416  * @ioc: per adapter object
417  * @iocpp: The ioc pointer is returned in this.
418  *
419  * Return (-1) means error, else ioc_number.
420  */
421 static int
_ctl_verify_adapter(int ioc_number,struct MPT2SAS_ADAPTER ** iocpp)422 _ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
423 {
424 	struct MPT2SAS_ADAPTER *ioc;
425 
426 	list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
427 		if (ioc->id != ioc_number)
428 			continue;
429 		*iocpp = ioc;
430 		return ioc_number;
431 	}
432 	*iocpp = NULL;
433 	return -1;
434 }
435 
436 /**
437  * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
438  * @ioc: per adapter object
439  * @reset_phase: phase
440  *
441  * The handler for doing any required cleanup or initialization.
442  *
443  * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
444  * MPT2_IOC_DONE_RESET
445  */
446 void
mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER * ioc,int reset_phase)447 mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
448 {
449 	int i;
450 	u8 issue_reset;
451 
452 	switch (reset_phase) {
453 	case MPT2_IOC_PRE_RESET:
454 		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
455 		    "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
456 		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
457 			if (!(ioc->diag_buffer_status[i] &
458 			    MPT2_DIAG_BUFFER_IS_REGISTERED))
459 				continue;
460 			if ((ioc->diag_buffer_status[i] &
461 			    MPT2_DIAG_BUFFER_IS_RELEASED))
462 				continue;
463 			_ctl_send_release(ioc, i, &issue_reset);
464 		}
465 		break;
466 	case MPT2_IOC_AFTER_RESET:
467 		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
468 		    "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
469 		if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
470 			ioc->ctl_cmds.status |= MPT2_CMD_RESET;
471 			mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
472 			complete(&ioc->ctl_cmds.done);
473 		}
474 		break;
475 	case MPT2_IOC_DONE_RESET:
476 		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
477 		    "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
478 
479 		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
480 			if (!(ioc->diag_buffer_status[i] &
481 			    MPT2_DIAG_BUFFER_IS_REGISTERED))
482 				continue;
483 			if ((ioc->diag_buffer_status[i] &
484 			    MPT2_DIAG_BUFFER_IS_RELEASED))
485 				continue;
486 			ioc->diag_buffer_status[i] |=
487 			    MPT2_DIAG_BUFFER_IS_DIAG_RESET;
488 		}
489 		break;
490 	}
491 }
492 
493 /**
494  * _ctl_fasync -
495  * @fd -
496  * @filep -
497  * @mode -
498  *
499  * Called when application request fasyn callback handler.
500  */
501 static int
_ctl_fasync(int fd,struct file * filep,int mode)502 _ctl_fasync(int fd, struct file *filep, int mode)
503 {
504 	return fasync_helper(fd, filep, mode, &async_queue);
505 }
506 
507 /**
508  * _ctl_poll -
509  * @file -
510  * @wait -
511  *
512  */
513 static unsigned int
_ctl_poll(struct file * filep,poll_table * wait)514 _ctl_poll(struct file *filep, poll_table *wait)
515 {
516 	struct MPT2SAS_ADAPTER *ioc;
517 
518 	poll_wait(filep, &ctl_poll_wait, wait);
519 
520 	list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
521 		if (ioc->aen_event_read_flag)
522 			return POLLIN | POLLRDNORM;
523 	}
524 	return 0;
525 }
526 
527 /**
528  * _ctl_set_task_mid - assign an active smid to tm request
529  * @ioc: per adapter object
530  * @karg - (struct mpt2_ioctl_command)
531  * @tm_request - pointer to mf from user space
532  *
533  * Returns 0 when an smid if found, else fail.
534  * during failure, the reply frame is filled.
535  */
536 static int
_ctl_set_task_mid(struct MPT2SAS_ADAPTER * ioc,struct mpt2_ioctl_command * karg,Mpi2SCSITaskManagementRequest_t * tm_request)537 _ctl_set_task_mid(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
538     Mpi2SCSITaskManagementRequest_t *tm_request)
539 {
540 	u8 found = 0;
541 	u16 i;
542 	u16 handle;
543 	struct scsi_cmnd *scmd;
544 	struct MPT2SAS_DEVICE *priv_data;
545 	unsigned long flags;
546 	Mpi2SCSITaskManagementReply_t *tm_reply;
547 	u32 sz;
548 	u32 lun;
549 	char *desc = NULL;
550 
551 	if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
552 		desc = "abort_task";
553 	else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
554 		desc = "query_task";
555 	else
556 		return 0;
557 
558 	lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
559 
560 	handle = le16_to_cpu(tm_request->DevHandle);
561 	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
562 	for (i = ioc->scsiio_depth; i && !found; i--) {
563 		scmd = ioc->scsi_lookup[i - 1].scmd;
564 		if (scmd == NULL || scmd->device == NULL ||
565 		    scmd->device->hostdata == NULL)
566 			continue;
567 		if (lun != scmd->device->lun)
568 			continue;
569 		priv_data = scmd->device->hostdata;
570 		if (priv_data->sas_target == NULL)
571 			continue;
572 		if (priv_data->sas_target->handle != handle)
573 			continue;
574 		tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
575 		found = 1;
576 	}
577 	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
578 
579 	if (!found) {
580 		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
581 		    "handle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
582 		    desc, le16_to_cpu(tm_request->DevHandle), lun));
583 		tm_reply = ioc->ctl_cmds.reply;
584 		tm_reply->DevHandle = tm_request->DevHandle;
585 		tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
586 		tm_reply->TaskType = tm_request->TaskType;
587 		tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
588 		tm_reply->VP_ID = tm_request->VP_ID;
589 		tm_reply->VF_ID = tm_request->VF_ID;
590 		sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
591 		if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
592 		    sz))
593 			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
594 			    __LINE__, __func__);
595 		return 1;
596 	}
597 
598 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
599 	    "handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
600 	    desc, le16_to_cpu(tm_request->DevHandle), lun,
601 	     le16_to_cpu(tm_request->TaskMID)));
602 	return 0;
603 }
604 
605 /**
606  * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
607  * @ioc: per adapter object
608  * @karg - (struct mpt2_ioctl_command)
609  * @mf - pointer to mf in user space
610  */
611 static long
_ctl_do_mpt_command(struct MPT2SAS_ADAPTER * ioc,struct mpt2_ioctl_command karg,void __user * mf)612 _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command karg,
613 	void __user *mf)
614 {
615 	MPI2RequestHeader_t *mpi_request = NULL, *request;
616 	MPI2DefaultReply_t *mpi_reply;
617 	u32 ioc_state;
618 	u16 ioc_status;
619 	u16 smid;
620 	unsigned long timeout, timeleft;
621 	u8 issue_reset;
622 	u32 sz;
623 	void *psge;
624 	void *data_out = NULL;
625 	dma_addr_t data_out_dma;
626 	size_t data_out_sz = 0;
627 	void *data_in = NULL;
628 	dma_addr_t data_in_dma;
629 	size_t data_in_sz = 0;
630 	u32 sgl_flags;
631 	long ret;
632 	u16 wait_state_count;
633 
634 	issue_reset = 0;
635 
636 	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
637 		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
638 		    ioc->name, __func__);
639 		ret = -EAGAIN;
640 		goto out;
641 	}
642 
643 	wait_state_count = 0;
644 	ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
645 	while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
646 		if (wait_state_count++ == 10) {
647 			printk(MPT2SAS_ERR_FMT
648 			    "%s: failed due to ioc not operational\n",
649 			    ioc->name, __func__);
650 			ret = -EFAULT;
651 			goto out;
652 		}
653 		ssleep(1);
654 		ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
655 		printk(MPT2SAS_INFO_FMT "%s: waiting for "
656 		    "operational state(count=%d)\n", ioc->name,
657 		    __func__, wait_state_count);
658 	}
659 	if (wait_state_count)
660 		printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
661 		    ioc->name, __func__);
662 
663 	mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
664 	if (!mpi_request) {
665 		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a memory for "
666 		    "mpi_request\n", ioc->name, __func__);
667 		ret = -ENOMEM;
668 		goto out;
669 	}
670 
671 	/* Check for overflow and wraparound */
672 	if (karg.data_sge_offset * 4 > ioc->request_sz ||
673 	    karg.data_sge_offset > (UINT_MAX / 4)) {
674 		ret = -EINVAL;
675 		goto out;
676 	}
677 
678 	/* copy in request message frame from user */
679 	if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
680 		printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
681 		    __func__);
682 		ret = -EFAULT;
683 		goto out;
684 	}
685 
686 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
687 		smid = mpt2sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
688 		if (!smid) {
689 			printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
690 			    ioc->name, __func__);
691 			ret = -EAGAIN;
692 			goto out;
693 		}
694 	} else {
695 
696 		smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
697 		if (!smid) {
698 			printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
699 			    ioc->name, __func__);
700 			ret = -EAGAIN;
701 			goto out;
702 		}
703 	}
704 
705 	ret = 0;
706 	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
707 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
708 	request = mpt2sas_base_get_msg_frame(ioc, smid);
709 	memcpy(request, mpi_request, karg.data_sge_offset*4);
710 	ioc->ctl_cmds.smid = smid;
711 	data_out_sz = karg.data_out_size;
712 	data_in_sz = karg.data_in_size;
713 
714 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
715 	    mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
716 		if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
717 		    le16_to_cpu(mpi_request->FunctionDependent1) >
718 		    ioc->facts.MaxDevHandle) {
719 			ret = -EINVAL;
720 			mpt2sas_base_free_smid(ioc, smid);
721 			goto out;
722 		}
723 	}
724 
725 	/* obtain dma-able memory for data transfer */
726 	if (data_out_sz) /* WRITE */ {
727 		data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
728 		    &data_out_dma);
729 		if (!data_out) {
730 			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
731 			    __LINE__, __func__);
732 			ret = -ENOMEM;
733 			mpt2sas_base_free_smid(ioc, smid);
734 			goto out;
735 		}
736 		if (copy_from_user(data_out, karg.data_out_buf_ptr,
737 			data_out_sz)) {
738 			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
739 			    __LINE__, __func__);
740 			ret =  -EFAULT;
741 			mpt2sas_base_free_smid(ioc, smid);
742 			goto out;
743 		}
744 	}
745 
746 	if (data_in_sz) /* READ */ {
747 		data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
748 		    &data_in_dma);
749 		if (!data_in) {
750 			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
751 			    __LINE__, __func__);
752 			ret = -ENOMEM;
753 			mpt2sas_base_free_smid(ioc, smid);
754 			goto out;
755 		}
756 	}
757 
758 	/* add scatter gather elements */
759 	psge = (void *)request + (karg.data_sge_offset*4);
760 
761 	if (!data_out_sz && !data_in_sz) {
762 		mpt2sas_base_build_zero_len_sge(ioc, psge);
763 	} else if (data_out_sz && data_in_sz) {
764 		/* WRITE sgel first */
765 		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
766 		    MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
767 		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
768 		ioc->base_add_sg_single(psge, sgl_flags |
769 		    data_out_sz, data_out_dma);
770 
771 		/* incr sgel */
772 		psge += ioc->sge_size;
773 
774 		/* READ sgel last */
775 		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
776 		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
777 		    MPI2_SGE_FLAGS_END_OF_LIST);
778 		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
779 		ioc->base_add_sg_single(psge, sgl_flags |
780 		    data_in_sz, data_in_dma);
781 	} else if (data_out_sz) /* WRITE */ {
782 		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
783 		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
784 		    MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
785 		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
786 		ioc->base_add_sg_single(psge, sgl_flags |
787 		    data_out_sz, data_out_dma);
788 	} else if (data_in_sz) /* READ */ {
789 		sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
790 		    MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
791 		    MPI2_SGE_FLAGS_END_OF_LIST);
792 		sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
793 		ioc->base_add_sg_single(psge, sgl_flags |
794 		    data_in_sz, data_in_dma);
795 	}
796 
797 	/* send command to firmware */
798 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
799 	_ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
800 #endif
801 
802 	init_completion(&ioc->ctl_cmds.done);
803 	switch (mpi_request->Function) {
804 	case MPI2_FUNCTION_SCSI_IO_REQUEST:
805 	case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
806 	{
807 		Mpi2SCSIIORequest_t *scsiio_request =
808 		    (Mpi2SCSIIORequest_t *)request;
809 		scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
810 		scsiio_request->SenseBufferLowAddress =
811 		    mpt2sas_base_get_sense_buffer_dma(ioc, smid);
812 		memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
813 		if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
814 			mpt2sas_base_put_smid_scsi_io(ioc, smid,
815 			    le16_to_cpu(mpi_request->FunctionDependent1));
816 		else
817 			mpt2sas_base_put_smid_default(ioc, smid);
818 		break;
819 	}
820 	case MPI2_FUNCTION_SCSI_TASK_MGMT:
821 	{
822 		Mpi2SCSITaskManagementRequest_t *tm_request =
823 		    (Mpi2SCSITaskManagementRequest_t *)request;
824 
825 		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
826 		    "handle(0x%04x), task_type(0x%02x)\n", ioc->name,
827 		    le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
828 
829 		if (tm_request->TaskType ==
830 		    MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
831 		    tm_request->TaskType ==
832 		    MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
833 			if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
834 				mpt2sas_base_free_smid(ioc, smid);
835 				goto out;
836 			}
837 		}
838 
839 		mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
840 		    tm_request->DevHandle));
841 		mpt2sas_base_put_smid_hi_priority(ioc, smid);
842 		break;
843 	}
844 	case MPI2_FUNCTION_SMP_PASSTHROUGH:
845 	{
846 		Mpi2SmpPassthroughRequest_t *smp_request =
847 		    (Mpi2SmpPassthroughRequest_t *)mpi_request;
848 		u8 *data;
849 
850 		/* ioc determines which port to use */
851 		smp_request->PhysicalPort = 0xFF;
852 		if (smp_request->PassthroughFlags &
853 		    MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
854 			data = (u8 *)&smp_request->SGL;
855 		else {
856 			if (unlikely(data_out == NULL)) {
857 				printk(KERN_ERR "failure at %s:%d/%s()!\n",
858 				    __FILE__, __LINE__, __func__);
859 				mpt2sas_base_free_smid(ioc, smid);
860 				ret = -EINVAL;
861 				goto out;
862 			}
863 			data = data_out;
864 		}
865 
866 		if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
867 			ioc->ioc_link_reset_in_progress = 1;
868 			ioc->ignore_loginfos = 1;
869 		}
870 		mpt2sas_base_put_smid_default(ioc, smid);
871 		break;
872 	}
873 	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
874 	{
875 		Mpi2SasIoUnitControlRequest_t *sasiounit_request =
876 		    (Mpi2SasIoUnitControlRequest_t *)mpi_request;
877 
878 		if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
879 		    || sasiounit_request->Operation ==
880 		    MPI2_SAS_OP_PHY_LINK_RESET) {
881 			ioc->ioc_link_reset_in_progress = 1;
882 			ioc->ignore_loginfos = 1;
883 		}
884 		mpt2sas_base_put_smid_default(ioc, smid);
885 		break;
886 	}
887 	default:
888 		mpt2sas_base_put_smid_default(ioc, smid);
889 		break;
890 	}
891 
892 	if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
893 		timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
894 	else
895 		timeout = karg.timeout;
896 	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
897 	    timeout*HZ);
898 	if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
899 		Mpi2SCSITaskManagementRequest_t *tm_request =
900 		    (Mpi2SCSITaskManagementRequest_t *)mpi_request;
901 		mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
902 		    tm_request->DevHandle));
903 	} else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
904 	    mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
905 		ioc->ioc_link_reset_in_progress) {
906 		ioc->ioc_link_reset_in_progress = 0;
907 		ioc->ignore_loginfos = 0;
908 	}
909 	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
910 		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
911 		    __func__);
912 		_debug_dump_mf(mpi_request, karg.data_sge_offset);
913 		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
914 			issue_reset = 1;
915 		goto issue_host_reset;
916 	}
917 
918 	mpi_reply = ioc->ctl_cmds.reply;
919 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
920 
921 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
922 	if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
923 	    (ioc->logging_level & MPT_DEBUG_TM)) {
924 		Mpi2SCSITaskManagementReply_t *tm_reply =
925 		    (Mpi2SCSITaskManagementReply_t *)mpi_reply;
926 
927 		printk(MPT2SAS_INFO_FMT "TASK_MGMT: "
928 		    "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
929 		    "TerminationCount(0x%08x)\n", ioc->name,
930 		    le16_to_cpu(tm_reply->IOCStatus),
931 		    le32_to_cpu(tm_reply->IOCLogInfo),
932 		    le32_to_cpu(tm_reply->TerminationCount));
933 	}
934 #endif
935 	/* copy out xdata to user */
936 	if (data_in_sz) {
937 		if (copy_to_user(karg.data_in_buf_ptr, data_in,
938 		    data_in_sz)) {
939 			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
940 			    __LINE__, __func__);
941 			ret = -ENODATA;
942 			goto out;
943 		}
944 	}
945 
946 	/* copy out reply message frame to user */
947 	if (karg.max_reply_bytes) {
948 		sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
949 		if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
950 		    sz)) {
951 			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
952 			    __LINE__, __func__);
953 			ret = -ENODATA;
954 			goto out;
955 		}
956 	}
957 
958 	/* copy out sense to user */
959 	if (karg.max_sense_bytes && (mpi_request->Function ==
960 	    MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
961 	    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
962 		sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
963 		if (copy_to_user(karg.sense_data_ptr,
964 			ioc->ctl_cmds.sense, sz)) {
965 			printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
966 			    __LINE__, __func__);
967 			ret = -ENODATA;
968 			goto out;
969 		}
970 	}
971 
972  issue_host_reset:
973 	if (issue_reset) {
974 		ret = -ENODATA;
975 		if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
976 		    mpi_request->Function ==
977 		    MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
978 		    mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
979 			printk(MPT2SAS_INFO_FMT "issue target reset: handle "
980 			    "= (0x%04x)\n", ioc->name,
981 			    le16_to_cpu(mpi_request->FunctionDependent1));
982 			mpt2sas_halt_firmware(ioc);
983 			mpt2sas_scsih_issue_tm(ioc,
984 			    le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
985 			    0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
986 			    0, TM_MUTEX_ON);
987 			ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
988 		} else
989 			mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
990 			    FORCE_BIG_HAMMER);
991 	}
992 
993  out:
994 
995 	/* free memory associated with sg buffers */
996 	if (data_in)
997 		pci_free_consistent(ioc->pdev, data_in_sz, data_in,
998 		    data_in_dma);
999 
1000 	if (data_out)
1001 		pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1002 		    data_out_dma);
1003 
1004 	kfree(mpi_request);
1005 	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1006 	return ret;
1007 }
1008 
1009 /**
1010  * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
1011  * @ioc: per adapter object
1012  * @arg - user space buffer containing ioctl content
1013  */
1014 static long
_ctl_getiocinfo(struct MPT2SAS_ADAPTER * ioc,void __user * arg)1015 _ctl_getiocinfo(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1016 {
1017 	struct mpt2_ioctl_iocinfo karg;
1018 
1019 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1020 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1021 		    __FILE__, __LINE__, __func__);
1022 		return -EFAULT;
1023 	}
1024 
1025 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1026 	    __func__));
1027 
1028 	memset(&karg, 0 , sizeof(karg));
1029 	if (ioc->is_warpdrive)
1030 		karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1031 	else
1032 		karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
1033 	if (ioc->pfacts)
1034 		karg.port_number = ioc->pfacts[0].PortNumber;
1035 	karg.hw_rev = ioc->pdev->revision;
1036 	karg.pci_id = ioc->pdev->device;
1037 	karg.subsystem_device = ioc->pdev->subsystem_device;
1038 	karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1039 	karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1040 	karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1041 	karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1042 	karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1043 	karg.firmware_version = ioc->facts.FWVersion.Word;
1044 	strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
1045 	strcat(karg.driver_version, "-");
1046 	strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1047 	karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1048 
1049 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1050 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1051 		    __FILE__, __LINE__, __func__);
1052 		return -EFAULT;
1053 	}
1054 	return 0;
1055 }
1056 
1057 /**
1058  * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
1059  * @ioc: per adapter object
1060  * @arg - user space buffer containing ioctl content
1061  */
1062 static long
_ctl_eventquery(struct MPT2SAS_ADAPTER * ioc,void __user * arg)1063 _ctl_eventquery(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1064 {
1065 	struct mpt2_ioctl_eventquery karg;
1066 
1067 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1068 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1069 		    __FILE__, __LINE__, __func__);
1070 		return -EFAULT;
1071 	}
1072 
1073 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1074 	    __func__));
1075 
1076 	karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
1077 	memcpy(karg.event_types, ioc->event_type,
1078 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1079 
1080 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1081 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1082 		    __FILE__, __LINE__, __func__);
1083 		return -EFAULT;
1084 	}
1085 	return 0;
1086 }
1087 
1088 /**
1089  * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
1090  * @ioc: per adapter object
1091  * @arg - user space buffer containing ioctl content
1092  */
1093 static long
_ctl_eventenable(struct MPT2SAS_ADAPTER * ioc,void __user * arg)1094 _ctl_eventenable(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1095 {
1096 	struct mpt2_ioctl_eventenable karg;
1097 
1098 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1099 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1100 		    __FILE__, __LINE__, __func__);
1101 		return -EFAULT;
1102 	}
1103 
1104 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1105 	    __func__));
1106 
1107 	if (ioc->event_log)
1108 		return 0;
1109 	memcpy(ioc->event_type, karg.event_types,
1110 	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1111 	mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1112 
1113 	/* initialize event_log */
1114 	ioc->event_context = 0;
1115 	ioc->aen_event_read_flag = 0;
1116 	ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1117 	    sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1118 	if (!ioc->event_log) {
1119 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1120 		    __FILE__, __LINE__, __func__);
1121 		return -ENOMEM;
1122 	}
1123 	return 0;
1124 }
1125 
1126 /**
1127  * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1128  * @ioc: per adapter object
1129  * @arg - user space buffer containing ioctl content
1130  */
1131 static long
_ctl_eventreport(struct MPT2SAS_ADAPTER * ioc,void __user * arg)1132 _ctl_eventreport(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1133 {
1134 	struct mpt2_ioctl_eventreport karg;
1135 	u32 number_bytes, max_events, max;
1136 	struct mpt2_ioctl_eventreport __user *uarg = arg;
1137 
1138 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1139 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1140 		    __FILE__, __LINE__, __func__);
1141 		return -EFAULT;
1142 	}
1143 
1144 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1145 	    __func__));
1146 
1147 	number_bytes = karg.hdr.max_data_size -
1148 	    sizeof(struct mpt2_ioctl_header);
1149 	max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1150 	max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1151 
1152 	/* If fewer than 1 event is requested, there must have
1153 	 * been some type of error.
1154 	 */
1155 	if (!max || !ioc->event_log)
1156 		return -ENODATA;
1157 
1158 	number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1159 	if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1160 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1161 		    __FILE__, __LINE__, __func__);
1162 		return -EFAULT;
1163 	}
1164 
1165 	/* reset flag so SIGIO can restart */
1166 	ioc->aen_event_read_flag = 0;
1167 	return 0;
1168 }
1169 
1170 /**
1171  * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1172  * @ioc: per adapter object
1173  * @arg - user space buffer containing ioctl content
1174  */
1175 static long
_ctl_do_reset(struct MPT2SAS_ADAPTER * ioc,void __user * arg)1176 _ctl_do_reset(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1177 {
1178 	struct mpt2_ioctl_diag_reset karg;
1179 	int retval;
1180 
1181 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1182 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1183 		    __FILE__, __LINE__, __func__);
1184 		return -EFAULT;
1185 	}
1186 
1187 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
1188 		ioc->is_driver_loading)
1189 		return -EAGAIN;
1190 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
1191 	    __func__));
1192 
1193 	retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1194 	    FORCE_BIG_HAMMER);
1195 	printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1196 	    ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1197 	return 0;
1198 }
1199 
1200 /**
1201  * _ctl_btdh_search_sas_device - searching for sas device
1202  * @ioc: per adapter object
1203  * @btdh: btdh ioctl payload
1204  */
1205 static int
_ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER * ioc,struct mpt2_ioctl_btdh_mapping * btdh)1206 _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1207     struct mpt2_ioctl_btdh_mapping *btdh)
1208 {
1209 	struct _sas_device *sas_device;
1210 	unsigned long flags;
1211 	int rc = 0;
1212 
1213 	if (list_empty(&ioc->sas_device_list))
1214 		return rc;
1215 
1216 	spin_lock_irqsave(&ioc->sas_device_lock, flags);
1217 	list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1218 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1219 		    btdh->handle == sas_device->handle) {
1220 			btdh->bus = sas_device->channel;
1221 			btdh->id = sas_device->id;
1222 			rc = 1;
1223 			goto out;
1224 		} else if (btdh->bus == sas_device->channel && btdh->id ==
1225 		    sas_device->id && btdh->handle == 0xFFFF) {
1226 			btdh->handle = sas_device->handle;
1227 			rc = 1;
1228 			goto out;
1229 		}
1230 	}
1231  out:
1232 	spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1233 	return rc;
1234 }
1235 
1236 /**
1237  * _ctl_btdh_search_raid_device - searching for raid device
1238  * @ioc: per adapter object
1239  * @btdh: btdh ioctl payload
1240  */
1241 static int
_ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER * ioc,struct mpt2_ioctl_btdh_mapping * btdh)1242 _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1243     struct mpt2_ioctl_btdh_mapping *btdh)
1244 {
1245 	struct _raid_device *raid_device;
1246 	unsigned long flags;
1247 	int rc = 0;
1248 
1249 	if (list_empty(&ioc->raid_device_list))
1250 		return rc;
1251 
1252 	spin_lock_irqsave(&ioc->raid_device_lock, flags);
1253 	list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1254 		if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1255 		    btdh->handle == raid_device->handle) {
1256 			btdh->bus = raid_device->channel;
1257 			btdh->id = raid_device->id;
1258 			rc = 1;
1259 			goto out;
1260 		} else if (btdh->bus == raid_device->channel && btdh->id ==
1261 		    raid_device->id && btdh->handle == 0xFFFF) {
1262 			btdh->handle = raid_device->handle;
1263 			rc = 1;
1264 			goto out;
1265 		}
1266 	}
1267  out:
1268 	spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1269 	return rc;
1270 }
1271 
1272 /**
1273  * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1274  * @ioc: per adapter object
1275  * @arg - user space buffer containing ioctl content
1276  */
1277 static long
_ctl_btdh_mapping(struct MPT2SAS_ADAPTER * ioc,void __user * arg)1278 _ctl_btdh_mapping(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1279 {
1280 	struct mpt2_ioctl_btdh_mapping karg;
1281 	int rc;
1282 
1283 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1284 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1285 		    __FILE__, __LINE__, __func__);
1286 		return -EFAULT;
1287 	}
1288 
1289 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1290 	    __func__));
1291 
1292 	rc = _ctl_btdh_search_sas_device(ioc, &karg);
1293 	if (!rc)
1294 		_ctl_btdh_search_raid_device(ioc, &karg);
1295 
1296 	if (copy_to_user(arg, &karg, sizeof(karg))) {
1297 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1298 		    __FILE__, __LINE__, __func__);
1299 		return -EFAULT;
1300 	}
1301 	return 0;
1302 }
1303 
1304 /**
1305  * _ctl_diag_capability - return diag buffer capability
1306  * @ioc: per adapter object
1307  * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1308  *
1309  * returns 1 when diag buffer support is enabled in firmware
1310  */
1311 static u8
_ctl_diag_capability(struct MPT2SAS_ADAPTER * ioc,u8 buffer_type)1312 _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1313 {
1314 	u8 rc = 0;
1315 
1316 	switch (buffer_type) {
1317 	case MPI2_DIAG_BUF_TYPE_TRACE:
1318 		if (ioc->facts.IOCCapabilities &
1319 		    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1320 			rc = 1;
1321 		break;
1322 	case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1323 		if (ioc->facts.IOCCapabilities &
1324 		    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1325 			rc = 1;
1326 		break;
1327 	case MPI2_DIAG_BUF_TYPE_EXTENDED:
1328 		if (ioc->facts.IOCCapabilities &
1329 		    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1330 			rc = 1;
1331 	}
1332 
1333 	return rc;
1334 }
1335 
1336 /**
1337  * _ctl_diag_register_2 - wrapper for registering diag buffer support
1338  * @ioc: per adapter object
1339  * @diag_register: the diag_register struct passed in from user space
1340  *
1341  */
1342 static long
_ctl_diag_register_2(struct MPT2SAS_ADAPTER * ioc,struct mpt2_diag_register * diag_register)1343 _ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
1344     struct mpt2_diag_register *diag_register)
1345 {
1346 	int rc, i;
1347 	void *request_data = NULL;
1348 	dma_addr_t request_data_dma;
1349 	u32 request_data_sz = 0;
1350 	Mpi2DiagBufferPostRequest_t *mpi_request;
1351 	Mpi2DiagBufferPostReply_t *mpi_reply;
1352 	u8 buffer_type;
1353 	unsigned long timeleft;
1354 	u16 smid;
1355 	u16 ioc_status;
1356 	u8 issue_reset = 0;
1357 
1358 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1359 	    __func__));
1360 
1361 	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1362 		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1363 		    ioc->name, __func__);
1364 		rc = -EAGAIN;
1365 		goto out;
1366 	}
1367 
1368 	buffer_type = diag_register->buffer_type;
1369 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1370 		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1371 		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1372 		return -EPERM;
1373 	}
1374 
1375 	if (ioc->diag_buffer_status[buffer_type] &
1376 	    MPT2_DIAG_BUFFER_IS_REGISTERED) {
1377 		printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1378 		    "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1379 		    buffer_type);
1380 		return -EINVAL;
1381 	}
1382 
1383 	if (diag_register->requested_buffer_size % 4)  {
1384 		printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1385 		    "is not 4 byte aligned\n", ioc->name, __func__);
1386 		return -EINVAL;
1387 	}
1388 
1389 	smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1390 	if (!smid) {
1391 		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1392 		    ioc->name, __func__);
1393 		rc = -EAGAIN;
1394 		goto out;
1395 	}
1396 
1397 	rc = 0;
1398 	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1399 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1400 	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1401 	ioc->ctl_cmds.smid = smid;
1402 
1403 	request_data = ioc->diag_buffer[buffer_type];
1404 	request_data_sz = diag_register->requested_buffer_size;
1405 	ioc->unique_id[buffer_type] = diag_register->unique_id;
1406 	ioc->diag_buffer_status[buffer_type] = 0;
1407 	memcpy(ioc->product_specific[buffer_type],
1408 	    diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
1409 	ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1410 
1411 	if (request_data) {
1412 		request_data_dma = ioc->diag_buffer_dma[buffer_type];
1413 		if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1414 			pci_free_consistent(ioc->pdev,
1415 			    ioc->diag_buffer_sz[buffer_type],
1416 			    request_data, request_data_dma);
1417 			request_data = NULL;
1418 		}
1419 	}
1420 
1421 	if (request_data == NULL) {
1422 		ioc->diag_buffer_sz[buffer_type] = 0;
1423 		ioc->diag_buffer_dma[buffer_type] = 0;
1424 		request_data = pci_alloc_consistent(
1425 			ioc->pdev, request_data_sz, &request_data_dma);
1426 		if (request_data == NULL) {
1427 			printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1428 			    " for diag buffers, requested size(%d)\n",
1429 			    ioc->name, __func__, request_data_sz);
1430 			mpt2sas_base_free_smid(ioc, smid);
1431 			return -ENOMEM;
1432 		}
1433 		ioc->diag_buffer[buffer_type] = request_data;
1434 		ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1435 		ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1436 	}
1437 
1438 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1439 	mpi_request->BufferType = diag_register->buffer_type;
1440 	mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1441 	mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1442 	mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1443 	mpi_request->VF_ID = 0; /* TODO */
1444 	mpi_request->VP_ID = 0;
1445 
1446 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(0x%p), "
1447 	    "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1448 	    (unsigned long long)request_data_dma,
1449 	    le32_to_cpu(mpi_request->BufferLength)));
1450 
1451 	for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1452 		mpi_request->ProductSpecific[i] =
1453 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
1454 
1455 	init_completion(&ioc->ctl_cmds.done);
1456 	mpt2sas_base_put_smid_default(ioc, smid);
1457 	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1458 	    MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1459 
1460 	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1461 		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1462 		    __func__);
1463 		_debug_dump_mf(mpi_request,
1464 		    sizeof(Mpi2DiagBufferPostRequest_t)/4);
1465 		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1466 			issue_reset = 1;
1467 		goto issue_host_reset;
1468 	}
1469 
1470 	/* process the completed Reply Message Frame */
1471 	if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1472 		printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1473 		    ioc->name, __func__);
1474 		rc = -EFAULT;
1475 		goto out;
1476 	}
1477 
1478 	mpi_reply = ioc->ctl_cmds.reply;
1479 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1480 
1481 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1482 		ioc->diag_buffer_status[buffer_type] |=
1483 			MPT2_DIAG_BUFFER_IS_REGISTERED;
1484 		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1485 		    ioc->name, __func__));
1486 	} else {
1487 		printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1488 		    "log_info(0x%08x)\n", ioc->name, __func__,
1489 		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1490 		rc = -EFAULT;
1491 	}
1492 
1493  issue_host_reset:
1494 	if (issue_reset)
1495 		mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1496 		    FORCE_BIG_HAMMER);
1497 
1498  out:
1499 
1500 	if (rc && request_data)
1501 		pci_free_consistent(ioc->pdev, request_data_sz,
1502 		    request_data, request_data_dma);
1503 
1504 	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1505 	return rc;
1506 }
1507 
1508 /**
1509  * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
1510  * @ioc: per adapter object
1511  * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1512  *
1513  * This is called when command line option diag_buffer_enable is enabled
1514  * at driver load time.
1515  */
1516 void
mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER * ioc,u8 bits_to_register)1517 mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
1518 {
1519 	struct mpt2_diag_register diag_register;
1520 
1521 	memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
1522 
1523 	if (bits_to_register & 1) {
1524 		printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
1525 		    ioc->name);
1526 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1527 		/* register for 1MB buffers  */
1528 		diag_register.requested_buffer_size = (1024 * 1024);
1529 		diag_register.unique_id = 0x7075900;
1530 		_ctl_diag_register_2(ioc,  &diag_register);
1531 	}
1532 
1533 	if (bits_to_register & 2) {
1534 		printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
1535 		    ioc->name);
1536 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1537 		/* register for 2MB buffers  */
1538 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1539 		diag_register.unique_id = 0x7075901;
1540 		_ctl_diag_register_2(ioc,  &diag_register);
1541 	}
1542 
1543 	if (bits_to_register & 4) {
1544 		printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
1545 		    ioc->name);
1546 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1547 		/* register for 2MB buffers  */
1548 		diag_register.requested_buffer_size = 2 * (1024 * 1024);
1549 		diag_register.unique_id = 0x7075901;
1550 		_ctl_diag_register_2(ioc,  &diag_register);
1551 	}
1552 }
1553 
1554 /**
1555  * _ctl_diag_register - application register with driver
1556  * @ioc: per adapter object
1557  * @arg - user space buffer containing ioctl content
1558  *
1559  * This will allow the driver to setup any required buffers that will be
1560  * needed by firmware to communicate with the driver.
1561  */
1562 static long
_ctl_diag_register(struct MPT2SAS_ADAPTER * ioc,void __user * arg)1563 _ctl_diag_register(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1564 {
1565 	struct mpt2_diag_register karg;
1566 	long rc;
1567 
1568 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1569 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1570 		    __FILE__, __LINE__, __func__);
1571 		return -EFAULT;
1572 	}
1573 
1574 	rc = _ctl_diag_register_2(ioc, &karg);
1575 	return rc;
1576 }
1577 
1578 /**
1579  * _ctl_diag_unregister - application unregister with driver
1580  * @ioc: per adapter object
1581  * @arg - user space buffer containing ioctl content
1582  *
1583  * This will allow the driver to cleanup any memory allocated for diag
1584  * messages and to free up any resources.
1585  */
1586 static long
_ctl_diag_unregister(struct MPT2SAS_ADAPTER * ioc,void __user * arg)1587 _ctl_diag_unregister(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1588 {
1589 	struct mpt2_diag_unregister karg;
1590 	void *request_data;
1591 	dma_addr_t request_data_dma;
1592 	u32 request_data_sz;
1593 	u8 buffer_type;
1594 
1595 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1596 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1597 		    __FILE__, __LINE__, __func__);
1598 		return -EFAULT;
1599 	}
1600 
1601 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1602 	    __func__));
1603 
1604 	buffer_type = karg.unique_id & 0x000000ff;
1605 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1606 		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1607 		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1608 		return -EPERM;
1609 	}
1610 
1611 	if ((ioc->diag_buffer_status[buffer_type] &
1612 	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1613 		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1614 		    "registered\n", ioc->name, __func__, buffer_type);
1615 		return -EINVAL;
1616 	}
1617 	if ((ioc->diag_buffer_status[buffer_type] &
1618 	    MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1619 		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1620 		    "released\n", ioc->name, __func__, buffer_type);
1621 		return -EINVAL;
1622 	}
1623 
1624 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1625 		printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1626 		    "registered\n", ioc->name, __func__, karg.unique_id);
1627 		return -EINVAL;
1628 	}
1629 
1630 	request_data = ioc->diag_buffer[buffer_type];
1631 	if (!request_data) {
1632 		printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1633 		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1634 		return -ENOMEM;
1635 	}
1636 
1637 	request_data_sz = ioc->diag_buffer_sz[buffer_type];
1638 	request_data_dma = ioc->diag_buffer_dma[buffer_type];
1639 	pci_free_consistent(ioc->pdev, request_data_sz,
1640 	    request_data, request_data_dma);
1641 	ioc->diag_buffer[buffer_type] = NULL;
1642 	ioc->diag_buffer_status[buffer_type] = 0;
1643 	return 0;
1644 }
1645 
1646 /**
1647  * _ctl_diag_query - query relevant info associated with diag buffers
1648  * @ioc: per adapter object
1649  * @arg - user space buffer containing ioctl content
1650  *
1651  * The application will send only buffer_type and unique_id.  Driver will
1652  * inspect unique_id first, if valid, fill in all the info.  If unique_id is
1653  * 0x00, the driver will return info specified by Buffer Type.
1654  */
1655 static long
_ctl_diag_query(struct MPT2SAS_ADAPTER * ioc,void __user * arg)1656 _ctl_diag_query(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1657 {
1658 	struct mpt2_diag_query karg;
1659 	void *request_data;
1660 	int i;
1661 	u8 buffer_type;
1662 
1663 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1664 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1665 		    __FILE__, __LINE__, __func__);
1666 		return -EFAULT;
1667 	}
1668 
1669 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1670 	    __func__));
1671 
1672 	karg.application_flags = 0;
1673 	buffer_type = karg.buffer_type;
1674 
1675 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1676 		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1677 		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1678 		return -EPERM;
1679 	}
1680 
1681 	if ((ioc->diag_buffer_status[buffer_type] &
1682 	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1683 		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1684 		    "registered\n", ioc->name, __func__, buffer_type);
1685 		return -EINVAL;
1686 	}
1687 
1688 	if (karg.unique_id & 0xffffff00) {
1689 		if (karg.unique_id != ioc->unique_id[buffer_type]) {
1690 			printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1691 			    "registered\n", ioc->name, __func__,
1692 			    karg.unique_id);
1693 			return -EINVAL;
1694 		}
1695 	}
1696 
1697 	request_data = ioc->diag_buffer[buffer_type];
1698 	if (!request_data) {
1699 		printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1700 		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1701 		return -ENOMEM;
1702 	}
1703 
1704 	if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1705 		karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1706 		    MPT2_APP_FLAGS_BUFFER_VALID);
1707 	else
1708 		karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1709 		    MPT2_APP_FLAGS_BUFFER_VALID |
1710 		    MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1711 
1712 	for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1713 		karg.product_specific[i] =
1714 		    ioc->product_specific[buffer_type][i];
1715 
1716 	karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1717 	karg.driver_added_buffer_size = 0;
1718 	karg.unique_id = ioc->unique_id[buffer_type];
1719 	karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1720 
1721 	if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1722 		printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1723 		    "data @ %p\n", ioc->name, __func__, arg);
1724 		return -EFAULT;
1725 	}
1726 	return 0;
1727 }
1728 
1729 /**
1730  * _ctl_send_release - Diag Release Message
1731  * @ioc: per adapter object
1732  * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1733  * @issue_reset - specifies whether host reset is required.
1734  *
1735  */
1736 static int
_ctl_send_release(struct MPT2SAS_ADAPTER * ioc,u8 buffer_type,u8 * issue_reset)1737 _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1738 {
1739 	Mpi2DiagReleaseRequest_t *mpi_request;
1740 	Mpi2DiagReleaseReply_t *mpi_reply;
1741 	u16 smid;
1742 	u16 ioc_status;
1743 	u32 ioc_state;
1744 	int rc;
1745 	unsigned long timeleft;
1746 
1747 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1748 	    __func__));
1749 
1750 	rc = 0;
1751 	*issue_reset = 0;
1752 
1753 	ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1754 	if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1755 		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
1756 		    "skipping due to FAULT state\n", ioc->name,
1757 		    __func__));
1758 		rc = -EAGAIN;
1759 		goto out;
1760 	}
1761 
1762 	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1763 		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1764 		    ioc->name, __func__);
1765 		rc = -EAGAIN;
1766 		goto out;
1767 	}
1768 
1769 	smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1770 	if (!smid) {
1771 		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1772 		    ioc->name, __func__);
1773 		rc = -EAGAIN;
1774 		goto out;
1775 	}
1776 
1777 	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1778 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1779 	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1780 	ioc->ctl_cmds.smid = smid;
1781 
1782 	mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1783 	mpi_request->BufferType = buffer_type;
1784 	mpi_request->VF_ID = 0; /* TODO */
1785 	mpi_request->VP_ID = 0;
1786 
1787 	init_completion(&ioc->ctl_cmds.done);
1788 	mpt2sas_base_put_smid_default(ioc, smid);
1789 	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1790 	    MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1791 
1792 	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1793 		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1794 		    __func__);
1795 		_debug_dump_mf(mpi_request,
1796 		    sizeof(Mpi2DiagReleaseRequest_t)/4);
1797 		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1798 			*issue_reset = 1;
1799 		rc = -EFAULT;
1800 		goto out;
1801 	}
1802 
1803 	/* process the completed Reply Message Frame */
1804 	if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1805 		printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1806 		    ioc->name, __func__);
1807 		rc = -EFAULT;
1808 		goto out;
1809 	}
1810 
1811 	mpi_reply = ioc->ctl_cmds.reply;
1812 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1813 
1814 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1815 		ioc->diag_buffer_status[buffer_type] |=
1816 		    MPT2_DIAG_BUFFER_IS_RELEASED;
1817 		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
1818 		    ioc->name, __func__));
1819 	} else {
1820 		printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
1821 		    "log_info(0x%08x)\n", ioc->name, __func__,
1822 		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1823 		rc = -EFAULT;
1824 	}
1825 
1826  out:
1827 	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1828 	return rc;
1829 }
1830 
1831 /**
1832  * _ctl_diag_release - request to send Diag Release Message to firmware
1833  * @arg - user space buffer containing ioctl content
1834  *
1835  * This allows ownership of the specified buffer to returned to the driver,
1836  * allowing an application to read the buffer without fear that firmware is
1837  * overwritting information in the buffer.
1838  */
1839 static long
_ctl_diag_release(struct MPT2SAS_ADAPTER * ioc,void __user * arg)1840 _ctl_diag_release(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1841 {
1842 	struct mpt2_diag_release karg;
1843 	void *request_data;
1844 	int rc;
1845 	u8 buffer_type;
1846 	u8 issue_reset = 0;
1847 
1848 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1849 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1850 		    __FILE__, __LINE__, __func__);
1851 		return -EFAULT;
1852 	}
1853 
1854 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1855 	    __func__));
1856 
1857 	buffer_type = karg.unique_id & 0x000000ff;
1858 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1859 		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1860 		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1861 		return -EPERM;
1862 	}
1863 
1864 	if ((ioc->diag_buffer_status[buffer_type] &
1865 	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1866 		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1867 		    "registered\n", ioc->name, __func__, buffer_type);
1868 		return -EINVAL;
1869 	}
1870 
1871 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1872 		printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1873 		    "registered\n", ioc->name, __func__, karg.unique_id);
1874 		return -EINVAL;
1875 	}
1876 
1877 	if (ioc->diag_buffer_status[buffer_type] &
1878 	    MPT2_DIAG_BUFFER_IS_RELEASED) {
1879 		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1880 		    "is already released\n", ioc->name, __func__,
1881 		    buffer_type);
1882 		return 0;
1883 	}
1884 
1885 	request_data = ioc->diag_buffer[buffer_type];
1886 
1887 	if (!request_data) {
1888 		printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1889 		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1890 		return -ENOMEM;
1891 	}
1892 
1893 	/* buffers were released by due to host reset */
1894 	if ((ioc->diag_buffer_status[buffer_type] &
1895 	    MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1896 		ioc->diag_buffer_status[buffer_type] |=
1897 		    MPT2_DIAG_BUFFER_IS_RELEASED;
1898 		ioc->diag_buffer_status[buffer_type] &=
1899 		    ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1900 		printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1901 		    "was released due to host reset\n", ioc->name, __func__,
1902 		    buffer_type);
1903 		return 0;
1904 	}
1905 
1906 	rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1907 
1908 	if (issue_reset)
1909 		mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1910 		    FORCE_BIG_HAMMER);
1911 
1912 	return rc;
1913 }
1914 
1915 /**
1916  * _ctl_diag_read_buffer - request for copy of the diag buffer
1917  * @ioc: per adapter object
1918  * @arg - user space buffer containing ioctl content
1919  */
1920 static long
_ctl_diag_read_buffer(struct MPT2SAS_ADAPTER * ioc,void __user * arg)1921 _ctl_diag_read_buffer(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
1922 {
1923 	struct mpt2_diag_read_buffer karg;
1924 	struct mpt2_diag_read_buffer __user *uarg = arg;
1925 	void *request_data, *diag_data;
1926 	Mpi2DiagBufferPostRequest_t *mpi_request;
1927 	Mpi2DiagBufferPostReply_t *mpi_reply;
1928 	int rc, i;
1929 	u8 buffer_type;
1930 	unsigned long timeleft, request_size, copy_size;
1931 	u16 smid;
1932 	u16 ioc_status;
1933 	u8 issue_reset = 0;
1934 
1935 	if (copy_from_user(&karg, arg, sizeof(karg))) {
1936 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
1937 		    __FILE__, __LINE__, __func__);
1938 		return -EFAULT;
1939 	}
1940 
1941 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1942 	    __func__));
1943 
1944 	buffer_type = karg.unique_id & 0x000000ff;
1945 	if (!_ctl_diag_capability(ioc, buffer_type)) {
1946 		printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1947 		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1948 		return -EPERM;
1949 	}
1950 
1951 	if (karg.unique_id != ioc->unique_id[buffer_type]) {
1952 		printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1953 		    "registered\n", ioc->name, __func__, karg.unique_id);
1954 		return -EINVAL;
1955 	}
1956 
1957 	request_data = ioc->diag_buffer[buffer_type];
1958 	if (!request_data) {
1959 		printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1960 		    "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1961 		return -ENOMEM;
1962 	}
1963 
1964 	request_size = ioc->diag_buffer_sz[buffer_type];
1965 
1966 	if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1967 		printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1968 		    "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1969 		    __func__);
1970 		return -EINVAL;
1971 	}
1972 
1973 	if (karg.starting_offset > request_size)
1974 		return -EINVAL;
1975 
1976 	diag_data = (void *)(request_data + karg.starting_offset);
1977 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
1978 	    "offset(%d), sz(%d)\n", ioc->name, __func__,
1979 	    diag_data, karg.starting_offset, karg.bytes_to_read));
1980 
1981 	/* Truncate data on requests that are too large */
1982 	if ((diag_data + karg.bytes_to_read < diag_data) ||
1983 	    (diag_data + karg.bytes_to_read > request_data + request_size))
1984 		copy_size = request_size - karg.starting_offset;
1985 	else
1986 		copy_size = karg.bytes_to_read;
1987 
1988 	if (copy_to_user((void __user *)uarg->diagnostic_data,
1989 	    diag_data, copy_size)) {
1990 		printk(MPT2SAS_ERR_FMT "%s: Unable to write "
1991 		    "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
1992 		    __func__, diag_data);
1993 		return -EFAULT;
1994 	}
1995 
1996 	if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
1997 		return 0;
1998 
1999 	dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
2000 		"buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
2001 	if ((ioc->diag_buffer_status[buffer_type] &
2002 	    MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
2003 		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2004 		    "buffer_type(0x%02x) is still registered\n", ioc->name,
2005 		     __func__, buffer_type));
2006 		return 0;
2007 	}
2008 	/* Get a free request frame and save the message context.
2009 	*/
2010 
2011 	if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
2012 		printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
2013 		    ioc->name, __func__);
2014 		rc = -EAGAIN;
2015 		goto out;
2016 	}
2017 
2018 	smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2019 	if (!smid) {
2020 		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2021 		    ioc->name, __func__);
2022 		rc = -EAGAIN;
2023 		goto out;
2024 	}
2025 
2026 	rc = 0;
2027 	ioc->ctl_cmds.status = MPT2_CMD_PENDING;
2028 	memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2029 	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2030 	ioc->ctl_cmds.smid = smid;
2031 
2032 	mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2033 	mpi_request->BufferType = buffer_type;
2034 	mpi_request->BufferLength =
2035 	    cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2036 	mpi_request->BufferAddress =
2037 	    cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2038 	for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
2039 		mpi_request->ProductSpecific[i] =
2040 			cpu_to_le32(ioc->product_specific[buffer_type][i]);
2041 	mpi_request->VF_ID = 0; /* TODO */
2042 	mpi_request->VP_ID = 0;
2043 
2044 	init_completion(&ioc->ctl_cmds.done);
2045 	mpt2sas_base_put_smid_default(ioc, smid);
2046 	timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2047 	    MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
2048 
2049 	if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
2050 		printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
2051 		    __func__);
2052 		_debug_dump_mf(mpi_request,
2053 		    sizeof(Mpi2DiagBufferPostRequest_t)/4);
2054 		if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
2055 			issue_reset = 1;
2056 		goto issue_host_reset;
2057 	}
2058 
2059 	/* process the completed Reply Message Frame */
2060 	if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
2061 		printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
2062 		    ioc->name, __func__);
2063 		rc = -EFAULT;
2064 		goto out;
2065 	}
2066 
2067 	mpi_reply = ioc->ctl_cmds.reply;
2068 	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2069 
2070 	if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2071 		ioc->diag_buffer_status[buffer_type] |=
2072 		    MPT2_DIAG_BUFFER_IS_REGISTERED;
2073 		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
2074 		    ioc->name, __func__));
2075 	} else {
2076 		printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
2077 		    "log_info(0x%08x)\n", ioc->name, __func__,
2078 		    ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2079 		rc = -EFAULT;
2080 	}
2081 
2082  issue_host_reset:
2083 	if (issue_reset)
2084 		mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2085 		    FORCE_BIG_HAMMER);
2086 
2087  out:
2088 
2089 	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
2090 	return rc;
2091 }
2092 
2093 
2094 #ifdef CONFIG_COMPAT
2095 /**
2096  * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2097  * @ioc: per adapter object
2098  * @cmd - ioctl opcode
2099  * @arg - (struct mpt2_ioctl_command32)
2100  *
2101  * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2102  */
2103 static long
_ctl_compat_mpt_command(struct MPT2SAS_ADAPTER * ioc,unsigned cmd,void __user * arg)2104 _ctl_compat_mpt_command(struct MPT2SAS_ADAPTER *ioc, unsigned cmd,
2105 	void __user *arg)
2106 {
2107 	struct mpt2_ioctl_command32 karg32;
2108 	struct mpt2_ioctl_command32 __user *uarg;
2109 	struct mpt2_ioctl_command karg;
2110 
2111 	if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2112 		return -EINVAL;
2113 
2114 	uarg = (struct mpt2_ioctl_command32 __user *) arg;
2115 
2116 	if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2117 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
2118 		    __FILE__, __LINE__, __func__);
2119 		return -EFAULT;
2120 	}
2121 
2122 	memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2123 	karg.hdr.ioc_number = karg32.hdr.ioc_number;
2124 	karg.hdr.port_number = karg32.hdr.port_number;
2125 	karg.hdr.max_data_size = karg32.hdr.max_data_size;
2126 	karg.timeout = karg32.timeout;
2127 	karg.max_reply_bytes = karg32.max_reply_bytes;
2128 	karg.data_in_size = karg32.data_in_size;
2129 	karg.data_out_size = karg32.data_out_size;
2130 	karg.max_sense_bytes = karg32.max_sense_bytes;
2131 	karg.data_sge_offset = karg32.data_sge_offset;
2132 	karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2133 	karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2134 	karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2135 	karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2136 	return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2137 }
2138 #endif
2139 
2140 /**
2141  * _ctl_ioctl_main - main ioctl entry point
2142  * @file - (struct file)
2143  * @cmd - ioctl opcode
2144  * @arg -
2145  * compat - handles 32 bit applications in 64bit os
2146  */
2147 static long
_ctl_ioctl_main(struct file * file,unsigned int cmd,void __user * arg,u8 compat)2148 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2149 	u8 compat)
2150 {
2151 	struct MPT2SAS_ADAPTER *ioc;
2152 	struct mpt2_ioctl_header ioctl_header;
2153 	enum block_state state;
2154 	long ret = -EINVAL;
2155 
2156 	/* get IOCTL header */
2157 	if (copy_from_user(&ioctl_header, (char __user *)arg,
2158 	    sizeof(struct mpt2_ioctl_header))) {
2159 		printk(KERN_ERR "failure at %s:%d/%s()!\n",
2160 		    __FILE__, __LINE__, __func__);
2161 		return -EFAULT;
2162 	}
2163 
2164 	if (_ctl_verify_adapter(ioctl_header.ioc_number, &ioc) == -1 || !ioc)
2165 		return -ENODEV;
2166 	if (ioc->shost_recovery || ioc->pci_error_recovery ||
2167 	    ioc->is_driver_loading)
2168 		return -EAGAIN;
2169 
2170 	state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2171 	if (state == NON_BLOCKING) {
2172 		if (!mutex_trylock(&ioc->ctl_cmds.mutex))
2173 			return -EAGAIN;
2174 	} else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2175 		return -ERESTARTSYS;
2176 	}
2177 
2178 	switch (cmd) {
2179 	case MPT2IOCINFO:
2180 		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
2181 			ret = _ctl_getiocinfo(ioc, arg);
2182 		break;
2183 #ifdef CONFIG_COMPAT
2184 	case MPT2COMMAND32:
2185 #endif
2186 	case MPT2COMMAND:
2187 	{
2188 		struct mpt2_ioctl_command __user *uarg;
2189 		struct mpt2_ioctl_command karg;
2190 #ifdef CONFIG_COMPAT
2191 		if (compat) {
2192 			ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2193 			break;
2194 		}
2195 #endif
2196 		if (copy_from_user(&karg, arg, sizeof(karg))) {
2197 			printk(KERN_ERR "failure at %s:%d/%s()!\n",
2198 			    __FILE__, __LINE__, __func__);
2199 			ret = -EFAULT;
2200 			break;
2201 		}
2202 
2203 		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2204 			uarg = arg;
2205 			ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2206 		}
2207 		break;
2208 	}
2209 	case MPT2EVENTQUERY:
2210 		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2211 			ret = _ctl_eventquery(ioc, arg);
2212 		break;
2213 	case MPT2EVENTENABLE:
2214 		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2215 			ret = _ctl_eventenable(ioc, arg);
2216 		break;
2217 	case MPT2EVENTREPORT:
2218 		ret = _ctl_eventreport(ioc, arg);
2219 		break;
2220 	case MPT2HARDRESET:
2221 		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2222 			ret = _ctl_do_reset(ioc, arg);
2223 		break;
2224 	case MPT2BTDHMAPPING:
2225 		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2226 			ret = _ctl_btdh_mapping(ioc, arg);
2227 		break;
2228 	case MPT2DIAGREGISTER:
2229 		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2230 			ret = _ctl_diag_register(ioc, arg);
2231 		break;
2232 	case MPT2DIAGUNREGISTER:
2233 		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2234 			ret = _ctl_diag_unregister(ioc, arg);
2235 		break;
2236 	case MPT2DIAGQUERY:
2237 		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2238 			ret = _ctl_diag_query(ioc, arg);
2239 		break;
2240 	case MPT2DIAGRELEASE:
2241 		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2242 			ret = _ctl_diag_release(ioc, arg);
2243 		break;
2244 	case MPT2DIAGREADBUFFER:
2245 		if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2246 			ret = _ctl_diag_read_buffer(ioc, arg);
2247 		break;
2248 	default:
2249 
2250 		dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
2251 		    "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2252 		break;
2253 	}
2254 
2255 	mutex_unlock(&ioc->ctl_cmds.mutex);
2256 	return ret;
2257 }
2258 
2259 /**
2260  * _ctl_ioctl - main ioctl entry point (unlocked)
2261  * @file - (struct file)
2262  * @cmd - ioctl opcode
2263  * @arg -
2264  */
2265 static long
_ctl_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2266 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2267 {
2268 	long ret;
2269 
2270 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0);
2271 	return ret;
2272 }
2273 #ifdef CONFIG_COMPAT
2274 /**
2275  * _ctl_ioctl_compat - main ioctl entry point (compat)
2276  * @file -
2277  * @cmd -
2278  * @arg -
2279  *
2280  * This routine handles 32 bit applications in 64bit os.
2281  */
2282 static long
_ctl_ioctl_compat(struct file * file,unsigned cmd,unsigned long arg)2283 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2284 {
2285 	long ret;
2286 
2287 	ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1);
2288 	return ret;
2289 }
2290 #endif
2291 
2292 /* scsi host attributes */
2293 
2294 /**
2295  * _ctl_version_fw_show - firmware version
2296  * @cdev - pointer to embedded class device
2297  * @buf - the buffer returned
2298  *
2299  * A sysfs 'read-only' shost attribute.
2300  */
2301 static ssize_t
_ctl_version_fw_show(struct device * cdev,struct device_attribute * attr,char * buf)2302 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2303     char *buf)
2304 {
2305 	struct Scsi_Host *shost = class_to_shost(cdev);
2306 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2307 
2308 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2309 	    (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2310 	    (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2311 	    (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2312 	    ioc->facts.FWVersion.Word & 0x000000FF);
2313 }
2314 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2315 
2316 /**
2317  * _ctl_version_bios_show - bios version
2318  * @cdev - pointer to embedded class device
2319  * @buf - the buffer returned
2320  *
2321  * A sysfs 'read-only' shost attribute.
2322  */
2323 static ssize_t
_ctl_version_bios_show(struct device * cdev,struct device_attribute * attr,char * buf)2324 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2325     char *buf)
2326 {
2327 	struct Scsi_Host *shost = class_to_shost(cdev);
2328 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2329 
2330 	u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2331 
2332 	return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2333 	    (version & 0xFF000000) >> 24,
2334 	    (version & 0x00FF0000) >> 16,
2335 	    (version & 0x0000FF00) >> 8,
2336 	    version & 0x000000FF);
2337 }
2338 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2339 
2340 /**
2341  * _ctl_version_mpi_show - MPI (message passing interface) version
2342  * @cdev - pointer to embedded class device
2343  * @buf - the buffer returned
2344  *
2345  * A sysfs 'read-only' shost attribute.
2346  */
2347 static ssize_t
_ctl_version_mpi_show(struct device * cdev,struct device_attribute * attr,char * buf)2348 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2349     char *buf)
2350 {
2351 	struct Scsi_Host *shost = class_to_shost(cdev);
2352 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2353 
2354 	return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2355 	    ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2356 }
2357 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2358 
2359 /**
2360  * _ctl_version_product_show - product name
2361  * @cdev - pointer to embedded class device
2362  * @buf - the buffer returned
2363  *
2364  * A sysfs 'read-only' shost attribute.
2365  */
2366 static ssize_t
_ctl_version_product_show(struct device * cdev,struct device_attribute * attr,char * buf)2367 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2368     char *buf)
2369 {
2370 	struct Scsi_Host *shost = class_to_shost(cdev);
2371 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2372 
2373 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2374 }
2375 static DEVICE_ATTR(version_product, S_IRUGO,
2376    _ctl_version_product_show, NULL);
2377 
2378 /**
2379  * _ctl_version_nvdata_persistent_show - ndvata persistent version
2380  * @cdev - pointer to embedded class device
2381  * @buf - the buffer returned
2382  *
2383  * A sysfs 'read-only' shost attribute.
2384  */
2385 static ssize_t
_ctl_version_nvdata_persistent_show(struct device * cdev,struct device_attribute * attr,char * buf)2386 _ctl_version_nvdata_persistent_show(struct device *cdev,
2387     struct device_attribute *attr, char *buf)
2388 {
2389 	struct Scsi_Host *shost = class_to_shost(cdev);
2390 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2391 
2392 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2393 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2394 }
2395 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2396     _ctl_version_nvdata_persistent_show, NULL);
2397 
2398 /**
2399  * _ctl_version_nvdata_default_show - nvdata default version
2400  * @cdev - pointer to embedded class device
2401  * @buf - the buffer returned
2402  *
2403  * A sysfs 'read-only' shost attribute.
2404  */
2405 static ssize_t
_ctl_version_nvdata_default_show(struct device * cdev,struct device_attribute * attr,char * buf)2406 _ctl_version_nvdata_default_show(struct device *cdev,
2407     struct device_attribute *attr, char *buf)
2408 {
2409 	struct Scsi_Host *shost = class_to_shost(cdev);
2410 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2411 
2412 	return snprintf(buf, PAGE_SIZE, "%08xh\n",
2413 	    le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2414 }
2415 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2416     _ctl_version_nvdata_default_show, NULL);
2417 
2418 /**
2419  * _ctl_board_name_show - board name
2420  * @cdev - pointer to embedded class device
2421  * @buf - the buffer returned
2422  *
2423  * A sysfs 'read-only' shost attribute.
2424  */
2425 static ssize_t
_ctl_board_name_show(struct device * cdev,struct device_attribute * attr,char * buf)2426 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2427     char *buf)
2428 {
2429 	struct Scsi_Host *shost = class_to_shost(cdev);
2430 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2431 
2432 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2433 }
2434 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2435 
2436 /**
2437  * _ctl_board_assembly_show - board assembly name
2438  * @cdev - pointer to embedded class device
2439  * @buf - the buffer returned
2440  *
2441  * A sysfs 'read-only' shost attribute.
2442  */
2443 static ssize_t
_ctl_board_assembly_show(struct device * cdev,struct device_attribute * attr,char * buf)2444 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2445     char *buf)
2446 {
2447 	struct Scsi_Host *shost = class_to_shost(cdev);
2448 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2449 
2450 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2451 }
2452 static DEVICE_ATTR(board_assembly, S_IRUGO,
2453     _ctl_board_assembly_show, NULL);
2454 
2455 /**
2456  * _ctl_board_tracer_show - board tracer number
2457  * @cdev - pointer to embedded class device
2458  * @buf - the buffer returned
2459  *
2460  * A sysfs 'read-only' shost attribute.
2461  */
2462 static ssize_t
_ctl_board_tracer_show(struct device * cdev,struct device_attribute * attr,char * buf)2463 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2464     char *buf)
2465 {
2466 	struct Scsi_Host *shost = class_to_shost(cdev);
2467 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2468 
2469 	return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2470 }
2471 static DEVICE_ATTR(board_tracer, S_IRUGO,
2472     _ctl_board_tracer_show, NULL);
2473 
2474 /**
2475  * _ctl_io_delay_show - io missing delay
2476  * @cdev - pointer to embedded class device
2477  * @buf - the buffer returned
2478  *
2479  * This is for firmware implemention for deboucing device
2480  * removal events.
2481  *
2482  * A sysfs 'read-only' shost attribute.
2483  */
2484 static ssize_t
_ctl_io_delay_show(struct device * cdev,struct device_attribute * attr,char * buf)2485 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2486     char *buf)
2487 {
2488 	struct Scsi_Host *shost = class_to_shost(cdev);
2489 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2490 
2491 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2492 }
2493 static DEVICE_ATTR(io_delay, S_IRUGO,
2494     _ctl_io_delay_show, NULL);
2495 
2496 /**
2497  * _ctl_device_delay_show - device missing delay
2498  * @cdev - pointer to embedded class device
2499  * @buf - the buffer returned
2500  *
2501  * This is for firmware implemention for deboucing device
2502  * removal events.
2503  *
2504  * A sysfs 'read-only' shost attribute.
2505  */
2506 static ssize_t
_ctl_device_delay_show(struct device * cdev,struct device_attribute * attr,char * buf)2507 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2508     char *buf)
2509 {
2510 	struct Scsi_Host *shost = class_to_shost(cdev);
2511 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2512 
2513 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2514 }
2515 static DEVICE_ATTR(device_delay, S_IRUGO,
2516     _ctl_device_delay_show, NULL);
2517 
2518 /**
2519  * _ctl_fw_queue_depth_show - global credits
2520  * @cdev - pointer to embedded class device
2521  * @buf - the buffer returned
2522  *
2523  * This is firmware queue depth limit
2524  *
2525  * A sysfs 'read-only' shost attribute.
2526  */
2527 static ssize_t
_ctl_fw_queue_depth_show(struct device * cdev,struct device_attribute * attr,char * buf)2528 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2529     char *buf)
2530 {
2531 	struct Scsi_Host *shost = class_to_shost(cdev);
2532 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2533 
2534 	return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2535 }
2536 static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2537     _ctl_fw_queue_depth_show, NULL);
2538 
2539 /**
2540  * _ctl_sas_address_show - sas address
2541  * @cdev - pointer to embedded class device
2542  * @buf - the buffer returned
2543  *
2544  * This is the controller sas address
2545  *
2546  * A sysfs 'read-only' shost attribute.
2547  */
2548 static ssize_t
_ctl_host_sas_address_show(struct device * cdev,struct device_attribute * attr,char * buf)2549 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2550     char *buf)
2551 {
2552 	struct Scsi_Host *shost = class_to_shost(cdev);
2553 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2554 
2555 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2556 	    (unsigned long long)ioc->sas_hba.sas_address);
2557 }
2558 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2559     _ctl_host_sas_address_show, NULL);
2560 
2561 /**
2562  * _ctl_logging_level_show - logging level
2563  * @cdev - pointer to embedded class device
2564  * @buf - the buffer returned
2565  *
2566  * A sysfs 'read/write' shost attribute.
2567  */
2568 static ssize_t
_ctl_logging_level_show(struct device * cdev,struct device_attribute * attr,char * buf)2569 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2570     char *buf)
2571 {
2572 	struct Scsi_Host *shost = class_to_shost(cdev);
2573 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2574 
2575 	return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2576 }
2577 static ssize_t
_ctl_logging_level_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)2578 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2579     const char *buf, size_t count)
2580 {
2581 	struct Scsi_Host *shost = class_to_shost(cdev);
2582 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2583 	int val = 0;
2584 
2585 	if (sscanf(buf, "%x", &val) != 1)
2586 		return -EINVAL;
2587 
2588 	ioc->logging_level = val;
2589 	printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2590 	    ioc->logging_level);
2591 	return strlen(buf);
2592 }
2593 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2594     _ctl_logging_level_show, _ctl_logging_level_store);
2595 
2596 /* device attributes */
2597 /*
2598  * _ctl_fwfault_debug_show - show/store fwfault_debug
2599  * @cdev - pointer to embedded class device
2600  * @buf - the buffer returned
2601  *
2602  * mpt2sas_fwfault_debug is command line option
2603  * A sysfs 'read/write' shost attribute.
2604  */
2605 static ssize_t
_ctl_fwfault_debug_show(struct device * cdev,struct device_attribute * attr,char * buf)2606 _ctl_fwfault_debug_show(struct device *cdev,
2607     struct device_attribute *attr, char *buf)
2608 {
2609 	struct Scsi_Host *shost = class_to_shost(cdev);
2610 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2611 
2612 	return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2613 }
2614 static ssize_t
_ctl_fwfault_debug_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)2615 _ctl_fwfault_debug_store(struct device *cdev,
2616     struct device_attribute *attr, const char *buf, size_t count)
2617 {
2618 	struct Scsi_Host *shost = class_to_shost(cdev);
2619 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2620 	int val = 0;
2621 
2622 	if (sscanf(buf, "%d", &val) != 1)
2623 		return -EINVAL;
2624 
2625 	ioc->fwfault_debug = val;
2626 	printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
2627 	    ioc->fwfault_debug);
2628 	return strlen(buf);
2629 }
2630 static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2631     _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2632 
2633 
2634 /**
2635  * _ctl_ioc_reset_count_show - ioc reset count
2636  * @cdev - pointer to embedded class device
2637  * @buf - the buffer returned
2638  *
2639  * This is firmware queue depth limit
2640  *
2641  * A sysfs 'read-only' shost attribute.
2642  */
2643 static ssize_t
_ctl_ioc_reset_count_show(struct device * cdev,struct device_attribute * attr,char * buf)2644 _ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2645     char *buf)
2646 {
2647 	struct Scsi_Host *shost = class_to_shost(cdev);
2648 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2649 
2650 	return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
2651 }
2652 static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
2653     _ctl_ioc_reset_count_show, NULL);
2654 
2655 /**
2656  * _ctl_ioc_reply_queue_count_show - number of reply queues
2657  * @cdev - pointer to embedded class device
2658  * @buf - the buffer returned
2659  *
2660  * This is number of reply queues
2661  *
2662  * A sysfs 'read-only' shost attribute.
2663  */
2664 static ssize_t
_ctl_ioc_reply_queue_count_show(struct device * cdev,struct device_attribute * attr,char * buf)2665 _ctl_ioc_reply_queue_count_show(struct device *cdev,
2666 	 struct device_attribute *attr, char *buf)
2667 {
2668 	u8 reply_queue_count;
2669 	struct Scsi_Host *shost = class_to_shost(cdev);
2670 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2671 
2672 	if ((ioc->facts.IOCCapabilities &
2673 	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2674 		reply_queue_count = ioc->reply_queue_count;
2675 	else
2676 		reply_queue_count = 1;
2677 	return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2678 }
2679 static DEVICE_ATTR(reply_queue_count, S_IRUGO,
2680 	 _ctl_ioc_reply_queue_count_show, NULL);
2681 
2682 /**
2683  * _ctl_BRM_status_show - Backup Rail Monitor Status
2684  * @cdev - pointer to embedded class device
2685  * @buf - the buffer returned
2686  *
2687  * This is number of reply queues
2688  *
2689  * A sysfs 'read-only' shost attribute.
2690  */
2691 static ssize_t
_ctl_BRM_status_show(struct device * cdev,struct device_attribute * attr,char * buf)2692 _ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
2693 	char *buf)
2694 {
2695 	struct Scsi_Host *shost = class_to_shost(cdev);
2696 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2697 	Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2698 	Mpi2ConfigReply_t mpi_reply;
2699 	u16 backup_rail_monitor_status = 0;
2700 	u16 ioc_status;
2701 	int sz;
2702 	ssize_t rc = 0;
2703 
2704 	if (!ioc->is_warpdrive) {
2705 		printk(MPT2SAS_ERR_FMT "%s: BRM attribute is only for"\
2706 		    "warpdrive\n", ioc->name, __func__);
2707 		goto out;
2708 	}
2709 
2710 	/* allocate upto GPIOVal 36 entries */
2711 	sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2712 	io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2713 	if (!io_unit_pg3) {
2714 		printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"\
2715 		    "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
2716 		goto out;
2717 	}
2718 
2719 	if (mpt2sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2720 	    0) {
2721 		printk(MPT2SAS_ERR_FMT
2722 		    "%s: failed reading iounit_pg3\n", ioc->name,
2723 		    __func__);
2724 		goto out;
2725 	}
2726 
2727 	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2728 	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2729 		printk(MPT2SAS_ERR_FMT "%s: iounit_pg3 failed with"\
2730 		    "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
2731 		goto out;
2732 	}
2733 
2734 	if (io_unit_pg3->GPIOCount < 25) {
2735 		printk(MPT2SAS_ERR_FMT "%s: iounit_pg3->GPIOCount less than"\
2736 		     "25 entries, detected (%d) entries\n", ioc->name, __func__,
2737 		    io_unit_pg3->GPIOCount);
2738 		goto out;
2739 	}
2740 
2741 	/* BRM status is in bit zero of GPIOVal[24] */
2742 	backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2743 	rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2744 
2745  out:
2746 	kfree(io_unit_pg3);
2747 	return rc;
2748 }
2749 static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
2750 
2751 struct DIAG_BUFFER_START {
2752 	__le32 Size;
2753 	__le32 DiagVersion;
2754 	u8 BufferType;
2755 	u8 Reserved[3];
2756 	__le32 Reserved1;
2757 	__le32 Reserved2;
2758 	__le32 Reserved3;
2759 };
2760 /**
2761  * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2762  * @cdev - pointer to embedded class device
2763  * @buf - the buffer returned
2764  *
2765  * A sysfs 'read-only' shost attribute.
2766  */
2767 static ssize_t
_ctl_host_trace_buffer_size_show(struct device * cdev,struct device_attribute * attr,char * buf)2768 _ctl_host_trace_buffer_size_show(struct device *cdev,
2769     struct device_attribute *attr, char *buf)
2770 {
2771 	struct Scsi_Host *shost = class_to_shost(cdev);
2772 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2773 	u32 size = 0;
2774 	struct DIAG_BUFFER_START *request_data;
2775 
2776 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2777 		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2778 		    "registered\n", ioc->name, __func__);
2779 		return 0;
2780 	}
2781 
2782 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2783 	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2784 		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2785 		    "registered\n", ioc->name, __func__);
2786 		return 0;
2787 	}
2788 
2789 	request_data = (struct DIAG_BUFFER_START *)
2790 	    ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2791 	if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2792 	    le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
2793 	    le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2794 		size = le32_to_cpu(request_data->Size);
2795 
2796 	ioc->ring_buffer_sz = size;
2797 	return snprintf(buf, PAGE_SIZE, "%d\n", size);
2798 }
2799 static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2800 	 _ctl_host_trace_buffer_size_show, NULL);
2801 
2802 /**
2803  * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2804  * @cdev - pointer to embedded class device
2805  * @buf - the buffer returned
2806  *
2807  * A sysfs 'read/write' shost attribute.
2808  *
2809  * You will only be able to read 4k bytes of ring buffer at a time.
2810  * In order to read beyond 4k bytes, you will have to write out the
2811  * offset to the same attribute, it will move the pointer.
2812  */
2813 static ssize_t
_ctl_host_trace_buffer_show(struct device * cdev,struct device_attribute * attr,char * buf)2814 _ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2815      char *buf)
2816 {
2817 	struct Scsi_Host *shost = class_to_shost(cdev);
2818 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2819 	void *request_data;
2820 	u32 size;
2821 
2822 	if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2823 		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2824 		    "registered\n", ioc->name, __func__);
2825 		return 0;
2826 	}
2827 
2828 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2829 	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2830 		printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2831 		    "registered\n", ioc->name, __func__);
2832 		return 0;
2833 	}
2834 
2835 	if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2836 		return 0;
2837 
2838 	size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2839 	size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
2840 	request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2841 	memcpy(buf, request_data, size);
2842 	return size;
2843 }
2844 
2845 static ssize_t
_ctl_host_trace_buffer_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)2846 _ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2847     const char *buf, size_t count)
2848 {
2849 	struct Scsi_Host *shost = class_to_shost(cdev);
2850 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2851 	int val = 0;
2852 
2853 	if (sscanf(buf, "%d", &val) != 1)
2854 		return -EINVAL;
2855 
2856 	ioc->ring_buffer_offset = val;
2857 	return strlen(buf);
2858 }
2859 static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2860     _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2861 
2862 /*****************************************/
2863 
2864 /**
2865  * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2866  * @cdev - pointer to embedded class device
2867  * @buf - the buffer returned
2868  *
2869  * A sysfs 'read/write' shost attribute.
2870  *
2871  * This is a mechnism to post/release host_trace_buffers
2872  */
2873 static ssize_t
_ctl_host_trace_buffer_enable_show(struct device * cdev,struct device_attribute * attr,char * buf)2874 _ctl_host_trace_buffer_enable_show(struct device *cdev,
2875     struct device_attribute *attr, char *buf)
2876 {
2877 	struct Scsi_Host *shost = class_to_shost(cdev);
2878 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2879 
2880 	if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
2881 	   ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2882 	    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
2883 		return snprintf(buf, PAGE_SIZE, "off\n");
2884 	else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2885 	    MPT2_DIAG_BUFFER_IS_RELEASED))
2886 		return snprintf(buf, PAGE_SIZE, "release\n");
2887 	else
2888 		return snprintf(buf, PAGE_SIZE, "post\n");
2889 }
2890 
2891 static ssize_t
_ctl_host_trace_buffer_enable_store(struct device * cdev,struct device_attribute * attr,const char * buf,size_t count)2892 _ctl_host_trace_buffer_enable_store(struct device *cdev,
2893     struct device_attribute *attr, const char *buf, size_t count)
2894 {
2895 	struct Scsi_Host *shost = class_to_shost(cdev);
2896 	struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2897 	char str[10] = "";
2898 	struct mpt2_diag_register diag_register;
2899 	u8 issue_reset = 0;
2900 
2901 	if (sscanf(buf, "%9s", str) != 1)
2902 		return -EINVAL;
2903 
2904 	if (!strcmp(str, "post")) {
2905 		/* exit out if host buffers are already posted */
2906 		if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
2907 		    (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2908 		    MPT2_DIAG_BUFFER_IS_REGISTERED) &&
2909 		    ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2910 		    MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
2911 			goto out;
2912 		memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
2913 		printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
2914 		    ioc->name);
2915 		diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
2916 		diag_register.requested_buffer_size = (1024 * 1024);
2917 		diag_register.unique_id = 0x7075900;
2918 		ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
2919 		_ctl_diag_register_2(ioc,  &diag_register);
2920 	} else if (!strcmp(str, "release")) {
2921 		/* exit out if host buffers are already released */
2922 		if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
2923 			goto out;
2924 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2925 		    MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
2926 			goto out;
2927 		if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2928 		    MPT2_DIAG_BUFFER_IS_RELEASED))
2929 			goto out;
2930 		printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
2931 		    ioc->name);
2932 		_ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
2933 	}
2934 
2935  out:
2936 	return strlen(buf);
2937 }
2938 static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
2939     _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
2940 
2941 struct device_attribute *mpt2sas_host_attrs[] = {
2942 	&dev_attr_version_fw,
2943 	&dev_attr_version_bios,
2944 	&dev_attr_version_mpi,
2945 	&dev_attr_version_product,
2946 	&dev_attr_version_nvdata_persistent,
2947 	&dev_attr_version_nvdata_default,
2948 	&dev_attr_board_name,
2949 	&dev_attr_board_assembly,
2950 	&dev_attr_board_tracer,
2951 	&dev_attr_io_delay,
2952 	&dev_attr_device_delay,
2953 	&dev_attr_logging_level,
2954 	&dev_attr_fwfault_debug,
2955 	&dev_attr_fw_queue_depth,
2956 	&dev_attr_host_sas_address,
2957 	&dev_attr_ioc_reset_count,
2958 	&dev_attr_host_trace_buffer_size,
2959 	&dev_attr_host_trace_buffer,
2960 	&dev_attr_host_trace_buffer_enable,
2961 	&dev_attr_reply_queue_count,
2962 	&dev_attr_BRM_status,
2963 	NULL,
2964 };
2965 
2966 /**
2967  * _ctl_device_sas_address_show - sas address
2968  * @cdev - pointer to embedded class device
2969  * @buf - the buffer returned
2970  *
2971  * This is the sas address for the target
2972  *
2973  * A sysfs 'read-only' shost attribute.
2974  */
2975 static ssize_t
_ctl_device_sas_address_show(struct device * dev,struct device_attribute * attr,char * buf)2976 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2977     char *buf)
2978 {
2979 	struct scsi_device *sdev = to_scsi_device(dev);
2980 	struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2981 
2982 	return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2983 	    (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2984 }
2985 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2986 
2987 /**
2988  * _ctl_device_handle_show - device handle
2989  * @cdev - pointer to embedded class device
2990  * @buf - the buffer returned
2991  *
2992  * This is the firmware assigned device handle
2993  *
2994  * A sysfs 'read-only' shost attribute.
2995  */
2996 static ssize_t
_ctl_device_handle_show(struct device * dev,struct device_attribute * attr,char * buf)2997 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
2998     char *buf)
2999 {
3000 	struct scsi_device *sdev = to_scsi_device(dev);
3001 	struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3002 
3003 	return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3004 	    sas_device_priv_data->sas_target->handle);
3005 }
3006 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
3007 
3008 struct device_attribute *mpt2sas_dev_attrs[] = {
3009 	&dev_attr_sas_address,
3010 	&dev_attr_sas_device_handle,
3011 	NULL,
3012 };
3013 
3014 static const struct file_operations ctl_fops = {
3015 	.owner = THIS_MODULE,
3016 	.unlocked_ioctl = _ctl_ioctl,
3017 	.poll = _ctl_poll,
3018 	.fasync = _ctl_fasync,
3019 #ifdef CONFIG_COMPAT
3020 	.compat_ioctl = _ctl_ioctl_compat,
3021 #endif
3022 	.llseek = noop_llseek,
3023 };
3024 
3025 static struct miscdevice ctl_dev = {
3026 	.minor  = MPT2SAS_MINOR,
3027 	.name   = MPT2SAS_DEV_NAME,
3028 	.fops   = &ctl_fops,
3029 };
3030 
3031 /**
3032  * mpt2sas_ctl_init - main entry point for ctl.
3033  *
3034  */
3035 void
mpt2sas_ctl_init(void)3036 mpt2sas_ctl_init(void)
3037 {
3038 	async_queue = NULL;
3039 	if (misc_register(&ctl_dev) < 0)
3040 		printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
3041 		    MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3042 
3043 	init_waitqueue_head(&ctl_poll_wait);
3044 }
3045 
3046 /**
3047  * mpt2sas_ctl_exit - exit point for ctl
3048  *
3049  */
3050 void
mpt2sas_ctl_exit(void)3051 mpt2sas_ctl_exit(void)
3052 {
3053 	struct MPT2SAS_ADAPTER *ioc;
3054 	int i;
3055 
3056 	list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
3057 
3058 		/* free memory associated to diag buffers */
3059 		for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3060 			if (!ioc->diag_buffer[i])
3061 				continue;
3062 			pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3063 			    ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3064 			ioc->diag_buffer[i] = NULL;
3065 			ioc->diag_buffer_status[i] = 0;
3066 		}
3067 
3068 		kfree(ioc->event_log);
3069 	}
3070 	misc_deregister(&ctl_dev);
3071 }
3072 
3073