]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/scsi/mpt2sas/mpt2sas_ctl.c
Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / mpt2sas / mpt2sas_ctl.c
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-2008 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/version.h>
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/slab.h>
51 #include <linux/types.h>
52 #include <linux/pci.h>
53 #include <linux/delay.h>
54 #include <linux/smp_lock.h>
55 #include <linux/compat.h>
56 #include <linux/poll.h>
57
58 #include <linux/io.h>
59 #include <linux/uaccess.h>
60
61 #include "mpt2sas_base.h"
62 #include "mpt2sas_ctl.h"
63
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_display_some_debug - debug routine
86 * @ioc: per adapter object
87 * @smid: system request message index
88 * @calling_function_name: string pass from calling function
89 * @mpi_reply: reply message frame
90 * Context: none.
91 *
92 * Function for displaying debug info helpfull when debugging issues
93 * in this module.
94 */
95 static void
96 _ctl_display_some_debug(struct MPT2SAS_ADAPTER *ioc, u16 smid,
97 char *calling_function_name, MPI2DefaultReply_t *mpi_reply)
98 {
99 Mpi2ConfigRequest_t *mpi_request;
100 char *desc = NULL;
101
102 if (!(ioc->logging_level & MPT_DEBUG_IOCTL))
103 return;
104
105 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
106 switch (mpi_request->Function) {
107 case MPI2_FUNCTION_SCSI_IO_REQUEST:
108 {
109 Mpi2SCSIIORequest_t *scsi_request =
110 (Mpi2SCSIIORequest_t *)mpi_request;
111
112 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
113 "scsi_io, cmd(0x%02x), cdb_len(%d)",
114 scsi_request->CDB.CDB32[0],
115 le16_to_cpu(scsi_request->IoFlags) & 0xF);
116 desc = ioc->tmp_string;
117 break;
118 }
119 case MPI2_FUNCTION_SCSI_TASK_MGMT:
120 desc = "task_mgmt";
121 break;
122 case MPI2_FUNCTION_IOC_INIT:
123 desc = "ioc_init";
124 break;
125 case MPI2_FUNCTION_IOC_FACTS:
126 desc = "ioc_facts";
127 break;
128 case MPI2_FUNCTION_CONFIG:
129 {
130 Mpi2ConfigRequest_t *config_request =
131 (Mpi2ConfigRequest_t *)mpi_request;
132
133 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
134 "config, type(0x%02x), ext_type(0x%02x), number(%d)",
135 (config_request->Header.PageType &
136 MPI2_CONFIG_PAGETYPE_MASK), config_request->ExtPageType,
137 config_request->Header.PageNumber);
138 desc = ioc->tmp_string;
139 break;
140 }
141 case MPI2_FUNCTION_PORT_FACTS:
142 desc = "port_facts";
143 break;
144 case MPI2_FUNCTION_PORT_ENABLE:
145 desc = "port_enable";
146 break;
147 case MPI2_FUNCTION_EVENT_NOTIFICATION:
148 desc = "event_notification";
149 break;
150 case MPI2_FUNCTION_FW_DOWNLOAD:
151 desc = "fw_download";
152 break;
153 case MPI2_FUNCTION_FW_UPLOAD:
154 desc = "fw_upload";
155 break;
156 case MPI2_FUNCTION_RAID_ACTION:
157 desc = "raid_action";
158 break;
159 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
160 {
161 Mpi2SCSIIORequest_t *scsi_request =
162 (Mpi2SCSIIORequest_t *)mpi_request;
163
164 snprintf(ioc->tmp_string, MPT_STRING_LENGTH,
165 "raid_pass, cmd(0x%02x), cdb_len(%d)",
166 scsi_request->CDB.CDB32[0],
167 le16_to_cpu(scsi_request->IoFlags) & 0xF);
168 desc = ioc->tmp_string;
169 break;
170 }
171 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
172 desc = "sas_iounit_cntl";
173 break;
174 case MPI2_FUNCTION_SATA_PASSTHROUGH:
175 desc = "sata_pass";
176 break;
177 case MPI2_FUNCTION_DIAG_BUFFER_POST:
178 desc = "diag_buffer_post";
179 break;
180 case MPI2_FUNCTION_DIAG_RELEASE:
181 desc = "diag_release";
182 break;
183 case MPI2_FUNCTION_SMP_PASSTHROUGH:
184 desc = "smp_passthrough";
185 break;
186 }
187
188 if (!desc)
189 return;
190
191 printk(MPT2SAS_DEBUG_FMT "%s: %s, smid(%d)\n",
192 ioc->name, calling_function_name, desc, smid);
193
194 if (!mpi_reply)
195 return;
196
197 if (mpi_reply->IOCStatus || mpi_reply->IOCLogInfo)
198 printk(MPT2SAS_DEBUG_FMT
199 "\tiocstatus(0x%04x), loginfo(0x%08x)\n",
200 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
201 le32_to_cpu(mpi_reply->IOCLogInfo));
202
203 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
204 mpi_request->Function ==
205 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
206 Mpi2SCSIIOReply_t *scsi_reply =
207 (Mpi2SCSIIOReply_t *)mpi_reply;
208 if (scsi_reply->SCSIState || scsi_reply->SCSIStatus)
209 printk(MPT2SAS_DEBUG_FMT
210 "\tscsi_state(0x%02x), scsi_status"
211 "(0x%02x)\n", ioc->name,
212 scsi_reply->SCSIState,
213 scsi_reply->SCSIStatus);
214 }
215 }
216 #endif
217
218 /**
219 * mpt2sas_ctl_done - ctl module completion routine
220 * @ioc: per adapter object
221 * @smid: system request message index
222 * @VF_ID: virtual function id
223 * @reply: reply message frame(lower 32bit addr)
224 * Context: none.
225 *
226 * The callback handler when using ioc->ctl_cb_idx.
227 *
228 * Return nothing.
229 */
230 void
231 mpt2sas_ctl_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
232 {
233 MPI2DefaultReply_t *mpi_reply;
234
235 if (ioc->ctl_cmds.status == MPT2_CMD_NOT_USED)
236 return;
237 if (ioc->ctl_cmds.smid != smid)
238 return;
239 ioc->ctl_cmds.status |= MPT2_CMD_COMPLETE;
240 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
241 if (mpi_reply) {
242 memcpy(ioc->ctl_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
243 ioc->ctl_cmds.status |= MPT2_CMD_REPLY_VALID;
244 }
245 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
246 _ctl_display_some_debug(ioc, smid, "ctl_done", mpi_reply);
247 #endif
248 ioc->ctl_cmds.status &= ~MPT2_CMD_PENDING;
249 complete(&ioc->ctl_cmds.done);
250 }
251
252 /**
253 * _ctl_check_event_type - determines when an event needs logging
254 * @ioc: per adapter object
255 * @event: firmware event
256 *
257 * The bitmask in ioc->event_type[] indicates which events should be
258 * be saved in the driver event_log. This bitmask is set by application.
259 *
260 * Returns 1 when event should be captured, or zero means no match.
261 */
262 static int
263 _ctl_check_event_type(struct MPT2SAS_ADAPTER *ioc, u16 event)
264 {
265 u16 i;
266 u32 desired_event;
267
268 if (event >= 128 || !event || !ioc->event_log)
269 return 0;
270
271 desired_event = (1 << (event % 32));
272 if (!desired_event)
273 desired_event = 1;
274 i = event / 32;
275 return desired_event & ioc->event_type[i];
276 }
277
278 /**
279 * mpt2sas_ctl_add_to_event_log - add event
280 * @ioc: per adapter object
281 * @mpi_reply: reply message frame
282 *
283 * Return nothing.
284 */
285 void
286 mpt2sas_ctl_add_to_event_log(struct MPT2SAS_ADAPTER *ioc,
287 Mpi2EventNotificationReply_t *mpi_reply)
288 {
289 struct MPT2_IOCTL_EVENTS *event_log;
290 u16 event;
291 int i;
292 u32 sz, event_data_sz;
293 u8 send_aen = 0;
294
295 if (!ioc->event_log)
296 return;
297
298 event = le16_to_cpu(mpi_reply->Event);
299
300 if (_ctl_check_event_type(ioc, event)) {
301
302 /* insert entry into circular event_log */
303 i = ioc->event_context % MPT2SAS_CTL_EVENT_LOG_SIZE;
304 event_log = ioc->event_log;
305 event_log[i].event = event;
306 event_log[i].context = ioc->event_context++;
307
308 event_data_sz = le16_to_cpu(mpi_reply->EventDataLength)*4;
309 sz = min_t(u32, event_data_sz, MPT2_EVENT_DATA_SIZE);
310 memset(event_log[i].data, 0, MPT2_EVENT_DATA_SIZE);
311 memcpy(event_log[i].data, mpi_reply->EventData, sz);
312 send_aen = 1;
313 }
314
315 /* This aen_event_read_flag flag is set until the
316 * application has read the event log.
317 * For MPI2_EVENT_LOG_ENTRY_ADDED, we always notify.
318 */
319 if (event == MPI2_EVENT_LOG_ENTRY_ADDED ||
320 (send_aen && !ioc->aen_event_read_flag)) {
321 ioc->aen_event_read_flag = 1;
322 wake_up_interruptible(&ctl_poll_wait);
323 if (async_queue)
324 kill_fasync(&async_queue, SIGIO, POLL_IN);
325 }
326 }
327
328 /**
329 * mpt2sas_ctl_event_callback - firmware event handler (called at ISR time)
330 * @ioc: per adapter object
331 * @VF_ID: virtual function id
332 * @reply: reply message frame(lower 32bit addr)
333 * Context: interrupt.
334 *
335 * This function merely adds a new work task into ioc->firmware_event_thread.
336 * The tasks are worked from _firmware_event_work in user context.
337 *
338 * Return nothing.
339 */
340 void
341 mpt2sas_ctl_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
342 {
343 Mpi2EventNotificationReply_t *mpi_reply;
344
345 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
346 mpt2sas_ctl_add_to_event_log(ioc, mpi_reply);
347 }
348
349 /**
350 * _ctl_verify_adapter - validates ioc_number passed from application
351 * @ioc: per adapter object
352 * @iocpp: The ioc pointer is returned in this.
353 *
354 * Return (-1) means error, else ioc_number.
355 */
356 static int
357 _ctl_verify_adapter(int ioc_number, struct MPT2SAS_ADAPTER **iocpp)
358 {
359 struct MPT2SAS_ADAPTER *ioc;
360
361 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
362 if (ioc->id != ioc_number)
363 continue;
364 *iocpp = ioc;
365 return ioc_number;
366 }
367 *iocpp = NULL;
368 return -1;
369 }
370
371 /**
372 * mpt2sas_ctl_reset_handler - reset callback handler (for ctl)
373 * @ioc: per adapter object
374 * @reset_phase: phase
375 *
376 * The handler for doing any required cleanup or initialization.
377 *
378 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
379 * MPT2_IOC_DONE_RESET
380 */
381 void
382 mpt2sas_ctl_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
383 {
384 int i;
385 u8 issue_reset;
386
387 switch (reset_phase) {
388 case MPT2_IOC_PRE_RESET:
389 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
390 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
391 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
392 if (!(ioc->diag_buffer_status[i] &
393 MPT2_DIAG_BUFFER_IS_REGISTERED))
394 continue;
395 if ((ioc->diag_buffer_status[i] &
396 MPT2_DIAG_BUFFER_IS_RELEASED))
397 continue;
398 _ctl_send_release(ioc, i, &issue_reset);
399 }
400 break;
401 case MPT2_IOC_AFTER_RESET:
402 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
403 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
404 if (ioc->ctl_cmds.status & MPT2_CMD_PENDING) {
405 ioc->ctl_cmds.status |= MPT2_CMD_RESET;
406 mpt2sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
407 complete(&ioc->ctl_cmds.done);
408 }
409 break;
410 case MPT2_IOC_DONE_RESET:
411 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
412 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
413
414 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
415 if (!(ioc->diag_buffer_status[i] &
416 MPT2_DIAG_BUFFER_IS_REGISTERED))
417 continue;
418 if ((ioc->diag_buffer_status[i] &
419 MPT2_DIAG_BUFFER_IS_RELEASED))
420 continue;
421 ioc->diag_buffer_status[i] |=
422 MPT2_DIAG_BUFFER_IS_DIAG_RESET;
423 }
424 break;
425 }
426 }
427
428 /**
429 * _ctl_fasync -
430 * @fd -
431 * @filep -
432 * @mode -
433 *
434 * Called when application request fasyn callback handler.
435 */
436 static int
437 _ctl_fasync(int fd, struct file *filep, int mode)
438 {
439 return fasync_helper(fd, filep, mode, &async_queue);
440 }
441
442 /**
443 * _ctl_release -
444 * @inode -
445 * @filep -
446 *
447 * Called when application releases the fasyn callback handler.
448 */
449 static int
450 _ctl_release(struct inode *inode, struct file *filep)
451 {
452 return fasync_helper(-1, filep, 0, &async_queue);
453 }
454
455 /**
456 * _ctl_poll -
457 * @file -
458 * @wait -
459 *
460 */
461 static unsigned int
462 _ctl_poll(struct file *filep, poll_table *wait)
463 {
464 struct MPT2SAS_ADAPTER *ioc;
465
466 poll_wait(filep, &ctl_poll_wait, wait);
467
468 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
469 if (ioc->aen_event_read_flag)
470 return POLLIN | POLLRDNORM;
471 }
472 return 0;
473 }
474
475 /**
476 * _ctl_do_task_abort - assign an active smid to the abort_task
477 * @ioc: per adapter object
478 * @karg - (struct mpt2_ioctl_command)
479 * @tm_request - pointer to mf from user space
480 *
481 * Returns 0 when an smid if found, else fail.
482 * during failure, the reply frame is filled.
483 */
484 static int
485 _ctl_do_task_abort(struct MPT2SAS_ADAPTER *ioc, struct mpt2_ioctl_command *karg,
486 Mpi2SCSITaskManagementRequest_t *tm_request)
487 {
488 u8 found = 0;
489 u16 i;
490 u16 handle;
491 struct scsi_cmnd *scmd;
492 struct MPT2SAS_DEVICE *priv_data;
493 unsigned long flags;
494 Mpi2SCSITaskManagementReply_t *tm_reply;
495 u32 sz;
496 u32 lun;
497
498 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
499
500 handle = le16_to_cpu(tm_request->DevHandle);
501 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
502 for (i = ioc->request_depth; i && !found; i--) {
503 scmd = ioc->scsi_lookup[i - 1].scmd;
504 if (scmd == NULL || scmd->device == NULL ||
505 scmd->device->hostdata == NULL)
506 continue;
507 if (lun != scmd->device->lun)
508 continue;
509 priv_data = scmd->device->hostdata;
510 if (priv_data->sas_target == NULL)
511 continue;
512 if (priv_data->sas_target->handle != handle)
513 continue;
514 tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
515 found = 1;
516 }
517 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
518
519 if (!found) {
520 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ABORT_TASK: "
521 "DevHandle(0x%04x), lun(%d), no active mid!!\n", ioc->name,
522 tm_request->DevHandle, lun));
523 tm_reply = ioc->ctl_cmds.reply;
524 tm_reply->DevHandle = tm_request->DevHandle;
525 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
526 tm_reply->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK;
527 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
528 tm_reply->VP_ID = tm_request->VP_ID;
529 tm_reply->VF_ID = tm_request->VF_ID;
530 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
531 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
532 sz))
533 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
534 __LINE__, __func__);
535 return 1;
536 }
537
538 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "ABORT_TASK: "
539 "DevHandle(0x%04x), lun(%d), smid(%d)\n", ioc->name,
540 tm_request->DevHandle, lun, tm_request->TaskMID));
541 return 0;
542 }
543
544 /**
545 * _ctl_do_mpt_command - main handler for MPT2COMMAND opcode
546 * @ioc: per adapter object
547 * @karg - (struct mpt2_ioctl_command)
548 * @mf - pointer to mf in user space
549 * @state - NON_BLOCKING or BLOCKING
550 */
551 static long
552 _ctl_do_mpt_command(struct MPT2SAS_ADAPTER *ioc,
553 struct mpt2_ioctl_command karg, void __user *mf, enum block_state state)
554 {
555 MPI2RequestHeader_t *mpi_request;
556 MPI2DefaultReply_t *mpi_reply;
557 u32 ioc_state;
558 u16 ioc_status;
559 u16 smid;
560 unsigned long timeout, timeleft;
561 u8 issue_reset;
562 u32 sz;
563 void *psge;
564 void *priv_sense = NULL;
565 void *data_out = NULL;
566 dma_addr_t data_out_dma;
567 size_t data_out_sz = 0;
568 void *data_in = NULL;
569 dma_addr_t data_in_dma;
570 size_t data_in_sz = 0;
571 u32 sgl_flags;
572 long ret;
573 u16 wait_state_count;
574
575 issue_reset = 0;
576
577 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
578 return -EAGAIN;
579 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
580 return -ERESTARTSYS;
581
582 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
583 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
584 ioc->name, __func__);
585 ret = -EAGAIN;
586 goto out;
587 }
588
589 wait_state_count = 0;
590 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
591 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
592 if (wait_state_count++ == 10) {
593 printk(MPT2SAS_ERR_FMT
594 "%s: failed due to ioc not operational\n",
595 ioc->name, __func__);
596 ret = -EFAULT;
597 goto out;
598 }
599 ssleep(1);
600 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
601 printk(MPT2SAS_INFO_FMT "%s: waiting for "
602 "operational state(count=%d)\n", ioc->name,
603 __func__, wait_state_count);
604 }
605 if (wait_state_count)
606 printk(MPT2SAS_INFO_FMT "%s: ioc is operational\n",
607 ioc->name, __func__);
608
609 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
610 if (!smid) {
611 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
612 ioc->name, __func__);
613 ret = -EAGAIN;
614 goto out;
615 }
616
617 ret = 0;
618 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
619 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
620 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
621 ioc->ctl_cmds.smid = smid;
622 data_out_sz = karg.data_out_size;
623 data_in_sz = karg.data_in_size;
624
625 /* copy in request message frame from user */
626 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
627 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__, __LINE__,
628 __func__);
629 ret = -EFAULT;
630 mpt2sas_base_free_smid(ioc, smid);
631 goto out;
632 }
633
634 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
635 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
636 if (!mpi_request->FunctionDependent1 ||
637 mpi_request->FunctionDependent1 >
638 cpu_to_le16(ioc->facts.MaxDevHandle)) {
639 ret = -EINVAL;
640 mpt2sas_base_free_smid(ioc, smid);
641 goto out;
642 }
643 }
644
645 /* obtain dma-able memory for data transfer */
646 if (data_out_sz) /* WRITE */ {
647 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
648 &data_out_dma);
649 if (!data_out) {
650 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
651 __LINE__, __func__);
652 ret = -ENOMEM;
653 mpt2sas_base_free_smid(ioc, smid);
654 goto out;
655 }
656 if (copy_from_user(data_out, karg.data_out_buf_ptr,
657 data_out_sz)) {
658 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
659 __LINE__, __func__);
660 ret = -EFAULT;
661 mpt2sas_base_free_smid(ioc, smid);
662 goto out;
663 }
664 }
665
666 if (data_in_sz) /* READ */ {
667 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
668 &data_in_dma);
669 if (!data_in) {
670 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
671 __LINE__, __func__);
672 ret = -ENOMEM;
673 mpt2sas_base_free_smid(ioc, smid);
674 goto out;
675 }
676 }
677
678 /* add scatter gather elements */
679 psge = (void *)mpi_request + (karg.data_sge_offset*4);
680
681 if (!data_out_sz && !data_in_sz) {
682 mpt2sas_base_build_zero_len_sge(ioc, psge);
683 } else if (data_out_sz && data_in_sz) {
684 /* WRITE sgel first */
685 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
686 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
687 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
688 ioc->base_add_sg_single(psge, sgl_flags |
689 data_out_sz, data_out_dma);
690
691 /* incr sgel */
692 psge += ioc->sge_size;
693
694 /* READ sgel last */
695 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
696 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
697 MPI2_SGE_FLAGS_END_OF_LIST);
698 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
699 ioc->base_add_sg_single(psge, sgl_flags |
700 data_in_sz, data_in_dma);
701 } else if (data_out_sz) /* WRITE */ {
702 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
703 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
704 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
705 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
706 ioc->base_add_sg_single(psge, sgl_flags |
707 data_out_sz, data_out_dma);
708 } else if (data_in_sz) /* READ */ {
709 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
710 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
711 MPI2_SGE_FLAGS_END_OF_LIST);
712 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
713 ioc->base_add_sg_single(psge, sgl_flags |
714 data_in_sz, data_in_dma);
715 }
716
717 /* send command to firmware */
718 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
719 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
720 #endif
721
722 switch (mpi_request->Function) {
723 case MPI2_FUNCTION_SCSI_IO_REQUEST:
724 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
725 {
726 Mpi2SCSIIORequest_t *scsiio_request =
727 (Mpi2SCSIIORequest_t *)mpi_request;
728 scsiio_request->SenseBufferLowAddress =
729 (u32)mpt2sas_base_get_sense_buffer_dma(ioc, smid);
730 priv_sense = mpt2sas_base_get_sense_buffer(ioc, smid);
731 memset(priv_sense, 0, SCSI_SENSE_BUFFERSIZE);
732 mpt2sas_base_put_smid_scsi_io(ioc, smid, 0,
733 le16_to_cpu(mpi_request->FunctionDependent1));
734 break;
735 }
736 case MPI2_FUNCTION_SCSI_TASK_MGMT:
737 {
738 Mpi2SCSITaskManagementRequest_t *tm_request =
739 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
740
741 if (tm_request->TaskType ==
742 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK) {
743 if (_ctl_do_task_abort(ioc, &karg, tm_request)) {
744 mpt2sas_base_free_smid(ioc, smid);
745 goto out;
746 }
747 }
748
749 mutex_lock(&ioc->tm_cmds.mutex);
750 mpt2sas_scsih_set_tm_flag(ioc, le16_to_cpu(
751 tm_request->DevHandle));
752 mpt2sas_base_put_smid_hi_priority(ioc, smid,
753 mpi_request->VF_ID);
754 break;
755 }
756 case MPI2_FUNCTION_SMP_PASSTHROUGH:
757 {
758 Mpi2SmpPassthroughRequest_t *smp_request =
759 (Mpi2SmpPassthroughRequest_t *)mpi_request;
760 u8 *data;
761
762 /* ioc determines which port to use */
763 smp_request->PhysicalPort = 0xFF;
764 if (smp_request->PassthroughFlags &
765 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
766 data = (u8 *)&smp_request->SGL;
767 else
768 data = data_out;
769
770 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
771 ioc->ioc_link_reset_in_progress = 1;
772 ioc->ignore_loginfos = 1;
773 }
774 mpt2sas_base_put_smid_default(ioc, smid, mpi_request->VF_ID);
775 break;
776 }
777 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
778 {
779 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
780 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
781
782 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
783 || sasiounit_request->Operation ==
784 MPI2_SAS_OP_PHY_LINK_RESET) {
785 ioc->ioc_link_reset_in_progress = 1;
786 ioc->ignore_loginfos = 1;
787 }
788 mpt2sas_base_put_smid_default(ioc, smid, mpi_request->VF_ID);
789 break;
790 }
791 default:
792 mpt2sas_base_put_smid_default(ioc, smid, mpi_request->VF_ID);
793 break;
794 }
795
796 if (karg.timeout < MPT2_IOCTL_DEFAULT_TIMEOUT)
797 timeout = MPT2_IOCTL_DEFAULT_TIMEOUT;
798 else
799 timeout = karg.timeout;
800 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
801 timeout*HZ);
802 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
803 Mpi2SCSITaskManagementRequest_t *tm_request =
804 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
805 mutex_unlock(&ioc->tm_cmds.mutex);
806 mpt2sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
807 tm_request->DevHandle));
808 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
809 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
810 ioc->ioc_link_reset_in_progress) {
811 ioc->ioc_link_reset_in_progress = 0;
812 ioc->ignore_loginfos = 0;
813 }
814 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
815 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
816 __func__);
817 _debug_dump_mf(mpi_request, karg.data_sge_offset);
818 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
819 issue_reset = 1;
820 goto issue_host_reset;
821 }
822
823 mpi_reply = ioc->ctl_cmds.reply;
824 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
825
826 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
827 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
828 (ioc->logging_level & MPT_DEBUG_TM)) {
829 Mpi2SCSITaskManagementReply_t *tm_reply =
830 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
831
832 printk(MPT2SAS_DEBUG_FMT "TASK_MGMT: "
833 "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
834 "TerminationCount(0x%08x)\n", ioc->name,
835 tm_reply->IOCStatus, tm_reply->IOCLogInfo,
836 tm_reply->TerminationCount);
837 }
838 #endif
839 /* copy out xdata to user */
840 if (data_in_sz) {
841 if (copy_to_user(karg.data_in_buf_ptr, data_in,
842 data_in_sz)) {
843 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
844 __LINE__, __func__);
845 ret = -ENODATA;
846 goto out;
847 }
848 }
849
850 /* copy out reply message frame to user */
851 if (karg.max_reply_bytes) {
852 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
853 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
854 sz)) {
855 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
856 __LINE__, __func__);
857 ret = -ENODATA;
858 goto out;
859 }
860 }
861
862 /* copy out sense to user */
863 if (karg.max_sense_bytes && (mpi_request->Function ==
864 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
865 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
866 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
867 if (copy_to_user(karg.sense_data_ptr, priv_sense, sz)) {
868 printk(KERN_ERR "failure at %s:%d/%s()!\n", __FILE__,
869 __LINE__, __func__);
870 ret = -ENODATA;
871 goto out;
872 }
873 }
874
875 issue_host_reset:
876 if (issue_reset) {
877 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
878 mpi_request->Function ==
879 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
880 printk(MPT2SAS_INFO_FMT "issue target reset: handle "
881 "= (0x%04x)\n", ioc->name,
882 mpi_request->FunctionDependent1);
883 mutex_lock(&ioc->tm_cmds.mutex);
884 mpt2sas_scsih_issue_tm(ioc,
885 mpi_request->FunctionDependent1, 0,
886 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10);
887 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
888 mutex_unlock(&ioc->tm_cmds.mutex);
889 } else
890 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
891 FORCE_BIG_HAMMER);
892 }
893
894 out:
895
896 /* free memory associated with sg buffers */
897 if (data_in)
898 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
899 data_in_dma);
900
901 if (data_out)
902 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
903 data_out_dma);
904
905 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
906 mutex_unlock(&ioc->ctl_cmds.mutex);
907 return ret;
908 }
909
910 /**
911 * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
912 * @arg - user space buffer containing ioctl content
913 */
914 static long
915 _ctl_getiocinfo(void __user *arg)
916 {
917 struct mpt2_ioctl_iocinfo karg;
918 struct MPT2SAS_ADAPTER *ioc;
919 u8 revision;
920
921 if (copy_from_user(&karg, arg, sizeof(karg))) {
922 printk(KERN_ERR "failure at %s:%d/%s()!\n",
923 __FILE__, __LINE__, __func__);
924 return -EFAULT;
925 }
926 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
927 return -ENODEV;
928
929 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
930 __func__));
931
932 memset(&karg, 0 , sizeof(karg));
933 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
934 if (ioc->pfacts)
935 karg.port_number = ioc->pfacts[0].PortNumber;
936 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
937 karg.hw_rev = revision;
938 karg.pci_id = ioc->pdev->device;
939 karg.subsystem_device = ioc->pdev->subsystem_device;
940 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
941 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
942 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
943 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
944 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
945 karg.firmware_version = ioc->facts.FWVersion.Word;
946 strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
947 strcat(karg.driver_version, "-");
948 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
949 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
950
951 if (copy_to_user(arg, &karg, sizeof(karg))) {
952 printk(KERN_ERR "failure at %s:%d/%s()!\n",
953 __FILE__, __LINE__, __func__);
954 return -EFAULT;
955 }
956 return 0;
957 }
958
959 /**
960 * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
961 * @arg - user space buffer containing ioctl content
962 */
963 static long
964 _ctl_eventquery(void __user *arg)
965 {
966 struct mpt2_ioctl_eventquery karg;
967 struct MPT2SAS_ADAPTER *ioc;
968
969 if (copy_from_user(&karg, arg, sizeof(karg))) {
970 printk(KERN_ERR "failure at %s:%d/%s()!\n",
971 __FILE__, __LINE__, __func__);
972 return -EFAULT;
973 }
974 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
975 return -ENODEV;
976
977 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
978 __func__));
979
980 karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
981 memcpy(karg.event_types, ioc->event_type,
982 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
983
984 if (copy_to_user(arg, &karg, sizeof(karg))) {
985 printk(KERN_ERR "failure at %s:%d/%s()!\n",
986 __FILE__, __LINE__, __func__);
987 return -EFAULT;
988 }
989 return 0;
990 }
991
992 /**
993 * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
994 * @arg - user space buffer containing ioctl content
995 */
996 static long
997 _ctl_eventenable(void __user *arg)
998 {
999 struct mpt2_ioctl_eventenable karg;
1000 struct MPT2SAS_ADAPTER *ioc;
1001
1002 if (copy_from_user(&karg, arg, sizeof(karg))) {
1003 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1004 __FILE__, __LINE__, __func__);
1005 return -EFAULT;
1006 }
1007 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1008 return -ENODEV;
1009
1010 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1011 __func__));
1012
1013 if (ioc->event_log)
1014 return 0;
1015 memcpy(ioc->event_type, karg.event_types,
1016 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1017 mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1018
1019 /* initialize event_log */
1020 ioc->event_context = 0;
1021 ioc->aen_event_read_flag = 0;
1022 ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1023 sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1024 if (!ioc->event_log) {
1025 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1026 __FILE__, __LINE__, __func__);
1027 return -ENOMEM;
1028 }
1029 return 0;
1030 }
1031
1032 /**
1033 * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
1034 * @arg - user space buffer containing ioctl content
1035 */
1036 static long
1037 _ctl_eventreport(void __user *arg)
1038 {
1039 struct mpt2_ioctl_eventreport karg;
1040 struct MPT2SAS_ADAPTER *ioc;
1041 u32 number_bytes, max_events, max;
1042 struct mpt2_ioctl_eventreport __user *uarg = arg;
1043
1044 if (copy_from_user(&karg, arg, sizeof(karg))) {
1045 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1046 __FILE__, __LINE__, __func__);
1047 return -EFAULT;
1048 }
1049 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1050 return -ENODEV;
1051
1052 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1053 __func__));
1054
1055 number_bytes = karg.hdr.max_data_size -
1056 sizeof(struct mpt2_ioctl_header);
1057 max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1058 max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1059
1060 /* If fewer than 1 event is requested, there must have
1061 * been some type of error.
1062 */
1063 if (!max || !ioc->event_log)
1064 return -ENODATA;
1065
1066 number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1067 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1068 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1069 __FILE__, __LINE__, __func__);
1070 return -EFAULT;
1071 }
1072
1073 /* reset flag so SIGIO can restart */
1074 ioc->aen_event_read_flag = 0;
1075 return 0;
1076 }
1077
1078 /**
1079 * _ctl_do_reset - main handler for MPT2HARDRESET opcode
1080 * @arg - user space buffer containing ioctl content
1081 */
1082 static long
1083 _ctl_do_reset(void __user *arg)
1084 {
1085 struct mpt2_ioctl_diag_reset karg;
1086 struct MPT2SAS_ADAPTER *ioc;
1087 int retval;
1088
1089 if (copy_from_user(&karg, arg, sizeof(karg))) {
1090 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1091 __FILE__, __LINE__, __func__);
1092 return -EFAULT;
1093 }
1094 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1095 return -ENODEV;
1096
1097 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
1098 __func__));
1099
1100 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1101 FORCE_BIG_HAMMER);
1102 printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1103 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1104 return 0;
1105 }
1106
1107 /**
1108 * _ctl_btdh_search_sas_device - searching for sas device
1109 * @ioc: per adapter object
1110 * @btdh: btdh ioctl payload
1111 */
1112 static int
1113 _ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1114 struct mpt2_ioctl_btdh_mapping *btdh)
1115 {
1116 struct _sas_device *sas_device;
1117 unsigned long flags;
1118 int rc = 0;
1119
1120 if (list_empty(&ioc->sas_device_list))
1121 return rc;
1122
1123 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1124 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1125 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1126 btdh->handle == sas_device->handle) {
1127 btdh->bus = sas_device->channel;
1128 btdh->id = sas_device->id;
1129 rc = 1;
1130 goto out;
1131 } else if (btdh->bus == sas_device->channel && btdh->id ==
1132 sas_device->id && btdh->handle == 0xFFFF) {
1133 btdh->handle = sas_device->handle;
1134 rc = 1;
1135 goto out;
1136 }
1137 }
1138 out:
1139 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1140 return rc;
1141 }
1142
1143 /**
1144 * _ctl_btdh_search_raid_device - searching for raid device
1145 * @ioc: per adapter object
1146 * @btdh: btdh ioctl payload
1147 */
1148 static int
1149 _ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1150 struct mpt2_ioctl_btdh_mapping *btdh)
1151 {
1152 struct _raid_device *raid_device;
1153 unsigned long flags;
1154 int rc = 0;
1155
1156 if (list_empty(&ioc->raid_device_list))
1157 return rc;
1158
1159 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1160 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1161 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1162 btdh->handle == raid_device->handle) {
1163 btdh->bus = raid_device->channel;
1164 btdh->id = raid_device->id;
1165 rc = 1;
1166 goto out;
1167 } else if (btdh->bus == raid_device->channel && btdh->id ==
1168 raid_device->id && btdh->handle == 0xFFFF) {
1169 btdh->handle = raid_device->handle;
1170 rc = 1;
1171 goto out;
1172 }
1173 }
1174 out:
1175 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1176 return rc;
1177 }
1178
1179 /**
1180 * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
1181 * @arg - user space buffer containing ioctl content
1182 */
1183 static long
1184 _ctl_btdh_mapping(void __user *arg)
1185 {
1186 struct mpt2_ioctl_btdh_mapping karg;
1187 struct MPT2SAS_ADAPTER *ioc;
1188 int rc;
1189
1190 if (copy_from_user(&karg, arg, sizeof(karg))) {
1191 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1192 __FILE__, __LINE__, __func__);
1193 return -EFAULT;
1194 }
1195 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1196 return -ENODEV;
1197
1198 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1199 __func__));
1200
1201 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1202 if (!rc)
1203 _ctl_btdh_search_raid_device(ioc, &karg);
1204
1205 if (copy_to_user(arg, &karg, sizeof(karg))) {
1206 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1207 __FILE__, __LINE__, __func__);
1208 return -EFAULT;
1209 }
1210 return 0;
1211 }
1212
1213 /**
1214 * _ctl_diag_capability - return diag buffer capability
1215 * @ioc: per adapter object
1216 * @buffer_type: specifies either TRACE or SNAPSHOT
1217 *
1218 * returns 1 when diag buffer support is enabled in firmware
1219 */
1220 static u8
1221 _ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1222 {
1223 u8 rc = 0;
1224
1225 switch (buffer_type) {
1226 case MPI2_DIAG_BUF_TYPE_TRACE:
1227 if (ioc->facts.IOCCapabilities &
1228 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1229 rc = 1;
1230 break;
1231 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1232 if (ioc->facts.IOCCapabilities &
1233 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1234 rc = 1;
1235 break;
1236 }
1237
1238 return rc;
1239 }
1240
1241 /**
1242 * _ctl_diag_register - application register with driver
1243 * @arg - user space buffer containing ioctl content
1244 * @state - NON_BLOCKING or BLOCKING
1245 *
1246 * This will allow the driver to setup any required buffers that will be
1247 * needed by firmware to communicate with the driver.
1248 */
1249 static long
1250 _ctl_diag_register(void __user *arg, enum block_state state)
1251 {
1252 struct mpt2_diag_register karg;
1253 struct MPT2SAS_ADAPTER *ioc;
1254 int rc, i;
1255 void *request_data = NULL;
1256 dma_addr_t request_data_dma;
1257 u32 request_data_sz = 0;
1258 Mpi2DiagBufferPostRequest_t *mpi_request;
1259 Mpi2DiagBufferPostReply_t *mpi_reply;
1260 u8 buffer_type;
1261 unsigned long timeleft;
1262 u16 smid;
1263 u16 ioc_status;
1264 u8 issue_reset = 0;
1265
1266 if (copy_from_user(&karg, arg, sizeof(karg))) {
1267 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1268 __FILE__, __LINE__, __func__);
1269 return -EFAULT;
1270 }
1271 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1272 return -ENODEV;
1273
1274 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1275 __func__));
1276
1277 buffer_type = karg.buffer_type;
1278 if (!_ctl_diag_capability(ioc, buffer_type)) {
1279 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1280 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1281 return -EPERM;
1282 }
1283
1284 if (ioc->diag_buffer_status[buffer_type] &
1285 MPT2_DIAG_BUFFER_IS_REGISTERED) {
1286 printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1287 "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1288 buffer_type);
1289 return -EINVAL;
1290 }
1291
1292 if (karg.requested_buffer_size % 4) {
1293 printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1294 "is not 4 byte aligned\n", ioc->name, __func__);
1295 return -EINVAL;
1296 }
1297
1298 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1299 return -EAGAIN;
1300 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1301 return -ERESTARTSYS;
1302
1303 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1304 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1305 ioc->name, __func__);
1306 rc = -EAGAIN;
1307 goto out;
1308 }
1309
1310 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1311 if (!smid) {
1312 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1313 ioc->name, __func__);
1314 rc = -EAGAIN;
1315 goto out;
1316 }
1317
1318 rc = 0;
1319 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1320 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1321 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1322 ioc->ctl_cmds.smid = smid;
1323
1324 request_data = ioc->diag_buffer[buffer_type];
1325 request_data_sz = karg.requested_buffer_size;
1326 ioc->unique_id[buffer_type] = karg.unique_id;
1327 ioc->diag_buffer_status[buffer_type] = 0;
1328 memcpy(ioc->product_specific[buffer_type], karg.product_specific,
1329 MPT2_PRODUCT_SPECIFIC_DWORDS);
1330 ioc->diagnostic_flags[buffer_type] = karg.diagnostic_flags;
1331
1332 if (request_data) {
1333 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1334 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1335 pci_free_consistent(ioc->pdev,
1336 ioc->diag_buffer_sz[buffer_type],
1337 request_data, request_data_dma);
1338 request_data = NULL;
1339 }
1340 }
1341
1342 if (request_data == NULL) {
1343 ioc->diag_buffer_sz[buffer_type] = 0;
1344 ioc->diag_buffer_dma[buffer_type] = 0;
1345 request_data = pci_alloc_consistent(
1346 ioc->pdev, request_data_sz, &request_data_dma);
1347 if (request_data == NULL) {
1348 printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1349 " for diag buffers, requested size(%d)\n",
1350 ioc->name, __func__, request_data_sz);
1351 mpt2sas_base_free_smid(ioc, smid);
1352 return -ENOMEM;
1353 }
1354 ioc->diag_buffer[buffer_type] = request_data;
1355 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1356 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1357 }
1358
1359 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1360 mpi_request->BufferType = karg.buffer_type;
1361 mpi_request->Flags = cpu_to_le32(karg.diagnostic_flags);
1362 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1363 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1364
1365 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: diag_buffer(0x%p), "
1366 "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
1367 (unsigned long long)request_data_dma, mpi_request->BufferLength));
1368
1369 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1370 mpi_request->ProductSpecific[i] =
1371 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1372
1373 mpt2sas_base_put_smid_default(ioc, smid, mpi_request->VF_ID);
1374 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1375 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1376
1377 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1378 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1379 __func__);
1380 _debug_dump_mf(mpi_request,
1381 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1382 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1383 issue_reset = 1;
1384 goto issue_host_reset;
1385 }
1386
1387 /* process the completed Reply Message Frame */
1388 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1389 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1390 ioc->name, __func__);
1391 rc = -EFAULT;
1392 goto out;
1393 }
1394
1395 mpi_reply = ioc->ctl_cmds.reply;
1396 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1397
1398 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1399 ioc->diag_buffer_status[buffer_type] |=
1400 MPT2_DIAG_BUFFER_IS_REGISTERED;
1401 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1402 ioc->name, __func__));
1403 } else {
1404 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1405 "log_info(0x%08x)\n", ioc->name, __func__,
1406 ioc_status, mpi_reply->IOCLogInfo);
1407 rc = -EFAULT;
1408 }
1409
1410 issue_host_reset:
1411 if (issue_reset)
1412 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1413 FORCE_BIG_HAMMER);
1414
1415 out:
1416
1417 if (rc && request_data)
1418 pci_free_consistent(ioc->pdev, request_data_sz,
1419 request_data, request_data_dma);
1420
1421 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1422 mutex_unlock(&ioc->ctl_cmds.mutex);
1423 return rc;
1424 }
1425
1426 /**
1427 * _ctl_diag_unregister - application unregister with driver
1428 * @arg - user space buffer containing ioctl content
1429 *
1430 * This will allow the driver to cleanup any memory allocated for diag
1431 * messages and to free up any resources.
1432 */
1433 static long
1434 _ctl_diag_unregister(void __user *arg)
1435 {
1436 struct mpt2_diag_unregister karg;
1437 struct MPT2SAS_ADAPTER *ioc;
1438 void *request_data;
1439 dma_addr_t request_data_dma;
1440 u32 request_data_sz;
1441 u8 buffer_type;
1442
1443 if (copy_from_user(&karg, arg, sizeof(karg))) {
1444 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1445 __FILE__, __LINE__, __func__);
1446 return -EFAULT;
1447 }
1448 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1449 return -ENODEV;
1450
1451 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1452 __func__));
1453
1454 buffer_type = karg.unique_id & 0x000000ff;
1455 if (!_ctl_diag_capability(ioc, buffer_type)) {
1456 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1457 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1458 return -EPERM;
1459 }
1460
1461 if ((ioc->diag_buffer_status[buffer_type] &
1462 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1463 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1464 "registered\n", ioc->name, __func__, buffer_type);
1465 return -EINVAL;
1466 }
1467 if ((ioc->diag_buffer_status[buffer_type] &
1468 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1469 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1470 "released\n", ioc->name, __func__, buffer_type);
1471 return -EINVAL;
1472 }
1473
1474 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1475 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1476 "registered\n", ioc->name, __func__, karg.unique_id);
1477 return -EINVAL;
1478 }
1479
1480 request_data = ioc->diag_buffer[buffer_type];
1481 if (!request_data) {
1482 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1483 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1484 return -ENOMEM;
1485 }
1486
1487 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1488 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1489 pci_free_consistent(ioc->pdev, request_data_sz,
1490 request_data, request_data_dma);
1491 ioc->diag_buffer[buffer_type] = NULL;
1492 ioc->diag_buffer_status[buffer_type] = 0;
1493 return 0;
1494 }
1495
1496 /**
1497 * _ctl_diag_query - query relevant info associated with diag buffers
1498 * @arg - user space buffer containing ioctl content
1499 *
1500 * The application will send only buffer_type and unique_id. Driver will
1501 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1502 * 0x00, the driver will return info specified by Buffer Type.
1503 */
1504 static long
1505 _ctl_diag_query(void __user *arg)
1506 {
1507 struct mpt2_diag_query karg;
1508 struct MPT2SAS_ADAPTER *ioc;
1509 void *request_data;
1510 int i;
1511 u8 buffer_type;
1512
1513 if (copy_from_user(&karg, arg, sizeof(karg))) {
1514 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1515 __FILE__, __LINE__, __func__);
1516 return -EFAULT;
1517 }
1518 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1519 return -ENODEV;
1520
1521 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1522 __func__));
1523
1524 karg.application_flags = 0;
1525 buffer_type = karg.buffer_type;
1526
1527 if (!_ctl_diag_capability(ioc, buffer_type)) {
1528 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1529 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1530 return -EPERM;
1531 }
1532
1533 if ((ioc->diag_buffer_status[buffer_type] &
1534 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1535 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1536 "registered\n", ioc->name, __func__, buffer_type);
1537 return -EINVAL;
1538 }
1539
1540 if (karg.unique_id & 0xffffff00) {
1541 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1542 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1543 "registered\n", ioc->name, __func__,
1544 karg.unique_id);
1545 return -EINVAL;
1546 }
1547 }
1548
1549 request_data = ioc->diag_buffer[buffer_type];
1550 if (!request_data) {
1551 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1552 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1553 return -ENOMEM;
1554 }
1555
1556 if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1557 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1558 MPT2_APP_FLAGS_BUFFER_VALID);
1559 else
1560 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1561 MPT2_APP_FLAGS_BUFFER_VALID |
1562 MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1563
1564 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1565 karg.product_specific[i] =
1566 ioc->product_specific[buffer_type][i];
1567
1568 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1569 karg.driver_added_buffer_size = 0;
1570 karg.unique_id = ioc->unique_id[buffer_type];
1571 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1572
1573 if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1574 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1575 "data @ %p\n", ioc->name, __func__, arg);
1576 return -EFAULT;
1577 }
1578 return 0;
1579 }
1580
1581 /**
1582 * _ctl_send_release - Diag Release Message
1583 * @ioc: per adapter object
1584 * @buffer_type - specifies either TRACE or SNAPSHOT
1585 * @issue_reset - specifies whether host reset is required.
1586 *
1587 */
1588 static int
1589 _ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1590 {
1591 Mpi2DiagReleaseRequest_t *mpi_request;
1592 Mpi2DiagReleaseReply_t *mpi_reply;
1593 u16 smid;
1594 u16 ioc_status;
1595 u32 ioc_state;
1596 int rc;
1597 unsigned long timeleft;
1598
1599 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1600 __func__));
1601
1602 rc = 0;
1603 *issue_reset = 0;
1604
1605 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1606 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1607 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
1608 "skipping due to FAULT state\n", ioc->name,
1609 __func__));
1610 rc = -EAGAIN;
1611 goto out;
1612 }
1613
1614 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1615 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1616 ioc->name, __func__);
1617 rc = -EAGAIN;
1618 goto out;
1619 }
1620
1621 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1622 if (!smid) {
1623 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1624 ioc->name, __func__);
1625 rc = -EAGAIN;
1626 goto out;
1627 }
1628
1629 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1630 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1631 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1632 ioc->ctl_cmds.smid = smid;
1633
1634 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1635 mpi_request->BufferType = buffer_type;
1636
1637 mpt2sas_base_put_smid_default(ioc, smid, mpi_request->VF_ID);
1638 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1639 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1640
1641 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1642 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1643 __func__);
1644 _debug_dump_mf(mpi_request,
1645 sizeof(Mpi2DiagReleaseRequest_t)/4);
1646 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1647 *issue_reset = 1;
1648 rc = -EFAULT;
1649 goto out;
1650 }
1651
1652 /* process the completed Reply Message Frame */
1653 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1654 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1655 ioc->name, __func__);
1656 rc = -EFAULT;
1657 goto out;
1658 }
1659
1660 mpi_reply = ioc->ctl_cmds.reply;
1661 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1662
1663 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1664 ioc->diag_buffer_status[buffer_type] |=
1665 MPT2_DIAG_BUFFER_IS_RELEASED;
1666 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1667 ioc->name, __func__));
1668 } else {
1669 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1670 "log_info(0x%08x)\n", ioc->name, __func__,
1671 ioc_status, mpi_reply->IOCLogInfo);
1672 rc = -EFAULT;
1673 }
1674
1675 out:
1676 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1677 return rc;
1678 }
1679
1680 /**
1681 * _ctl_diag_release - request to send Diag Release Message to firmware
1682 * @arg - user space buffer containing ioctl content
1683 * @state - NON_BLOCKING or BLOCKING
1684 *
1685 * This allows ownership of the specified buffer to returned to the driver,
1686 * allowing an application to read the buffer without fear that firmware is
1687 * overwritting information in the buffer.
1688 */
1689 static long
1690 _ctl_diag_release(void __user *arg, enum block_state state)
1691 {
1692 struct mpt2_diag_release karg;
1693 struct MPT2SAS_ADAPTER *ioc;
1694 void *request_data;
1695 int rc;
1696 u8 buffer_type;
1697 u8 issue_reset = 0;
1698
1699 if (copy_from_user(&karg, arg, sizeof(karg))) {
1700 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1701 __FILE__, __LINE__, __func__);
1702 return -EFAULT;
1703 }
1704 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1705 return -ENODEV;
1706
1707 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1708 __func__));
1709
1710 buffer_type = karg.unique_id & 0x000000ff;
1711 if (!_ctl_diag_capability(ioc, buffer_type)) {
1712 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1713 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1714 return -EPERM;
1715 }
1716
1717 if ((ioc->diag_buffer_status[buffer_type] &
1718 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1719 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1720 "registered\n", ioc->name, __func__, buffer_type);
1721 return -EINVAL;
1722 }
1723
1724 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1725 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1726 "registered\n", ioc->name, __func__, karg.unique_id);
1727 return -EINVAL;
1728 }
1729
1730 if (ioc->diag_buffer_status[buffer_type] &
1731 MPT2_DIAG_BUFFER_IS_RELEASED) {
1732 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1733 "is already released\n", ioc->name, __func__,
1734 buffer_type);
1735 return 0;
1736 }
1737
1738 request_data = ioc->diag_buffer[buffer_type];
1739
1740 if (!request_data) {
1741 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1742 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1743 return -ENOMEM;
1744 }
1745
1746 /* buffers were released by due to host reset */
1747 if ((ioc->diag_buffer_status[buffer_type] &
1748 MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1749 ioc->diag_buffer_status[buffer_type] |=
1750 MPT2_DIAG_BUFFER_IS_RELEASED;
1751 ioc->diag_buffer_status[buffer_type] &=
1752 ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1753 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1754 "was released due to host reset\n", ioc->name, __func__,
1755 buffer_type);
1756 return 0;
1757 }
1758
1759 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1760 return -EAGAIN;
1761 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1762 return -ERESTARTSYS;
1763
1764 rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
1765
1766 if (issue_reset)
1767 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1768 FORCE_BIG_HAMMER);
1769
1770 mutex_unlock(&ioc->ctl_cmds.mutex);
1771 return rc;
1772 }
1773
1774 /**
1775 * _ctl_diag_read_buffer - request for copy of the diag buffer
1776 * @arg - user space buffer containing ioctl content
1777 * @state - NON_BLOCKING or BLOCKING
1778 */
1779 static long
1780 _ctl_diag_read_buffer(void __user *arg, enum block_state state)
1781 {
1782 struct mpt2_diag_read_buffer karg;
1783 struct mpt2_diag_read_buffer __user *uarg = arg;
1784 struct MPT2SAS_ADAPTER *ioc;
1785 void *request_data, *diag_data;
1786 Mpi2DiagBufferPostRequest_t *mpi_request;
1787 Mpi2DiagBufferPostReply_t *mpi_reply;
1788 int rc, i;
1789 u8 buffer_type;
1790 unsigned long timeleft;
1791 u16 smid;
1792 u16 ioc_status;
1793 u8 issue_reset = 0;
1794
1795 if (copy_from_user(&karg, arg, sizeof(karg))) {
1796 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1797 __FILE__, __LINE__, __func__);
1798 return -EFAULT;
1799 }
1800 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 || !ioc)
1801 return -ENODEV;
1802
1803 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1804 __func__));
1805
1806 buffer_type = karg.unique_id & 0x000000ff;
1807 if (!_ctl_diag_capability(ioc, buffer_type)) {
1808 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1809 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1810 return -EPERM;
1811 }
1812
1813 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1814 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1815 "registered\n", ioc->name, __func__, karg.unique_id);
1816 return -EINVAL;
1817 }
1818
1819 request_data = ioc->diag_buffer[buffer_type];
1820 if (!request_data) {
1821 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1822 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1823 return -ENOMEM;
1824 }
1825
1826 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1827 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1828 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1829 __func__);
1830 return -EINVAL;
1831 }
1832
1833 diag_data = (void *)(request_data + karg.starting_offset);
1834 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: diag_buffer(%p), "
1835 "offset(%d), sz(%d)\n", ioc->name, __func__,
1836 diag_data, karg.starting_offset, karg.bytes_to_read));
1837
1838 if (copy_to_user((void __user *)uarg->diagnostic_data,
1839 diag_data, karg.bytes_to_read)) {
1840 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
1841 "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
1842 __func__, diag_data);
1843 return -EFAULT;
1844 }
1845
1846 if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
1847 return 0;
1848
1849 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: Reregister "
1850 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
1851 if ((ioc->diag_buffer_status[buffer_type] &
1852 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1853 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
1854 "buffer_type(0x%02x) is still registered\n", ioc->name,
1855 __func__, buffer_type));
1856 return 0;
1857 }
1858 /* Get a free request frame and save the message context.
1859 */
1860 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
1861 return -EAGAIN;
1862 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
1863 return -ERESTARTSYS;
1864
1865 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1866 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1867 ioc->name, __func__);
1868 rc = -EAGAIN;
1869 goto out;
1870 }
1871
1872 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1873 if (!smid) {
1874 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1875 ioc->name, __func__);
1876 rc = -EAGAIN;
1877 goto out;
1878 }
1879
1880 rc = 0;
1881 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1882 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1883 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1884 ioc->ctl_cmds.smid = smid;
1885
1886 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1887 mpi_request->BufferType = buffer_type;
1888 mpi_request->BufferLength =
1889 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
1890 mpi_request->BufferAddress =
1891 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
1892 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1893 mpi_request->ProductSpecific[i] =
1894 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1895
1896 mpt2sas_base_put_smid_default(ioc, smid, mpi_request->VF_ID);
1897 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1898 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1899
1900 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1901 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1902 __func__);
1903 _debug_dump_mf(mpi_request,
1904 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1905 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1906 issue_reset = 1;
1907 goto issue_host_reset;
1908 }
1909
1910 /* process the completed Reply Message Frame */
1911 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1912 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1913 ioc->name, __func__);
1914 rc = -EFAULT;
1915 goto out;
1916 }
1917
1918 mpi_reply = ioc->ctl_cmds.reply;
1919 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1920
1921 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1922 ioc->diag_buffer_status[buffer_type] |=
1923 MPT2_DIAG_BUFFER_IS_REGISTERED;
1924 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: success\n",
1925 ioc->name, __func__));
1926 } else {
1927 printk(MPT2SAS_DEBUG_FMT "%s: ioc_status(0x%04x) "
1928 "log_info(0x%08x)\n", ioc->name, __func__,
1929 ioc_status, mpi_reply->IOCLogInfo);
1930 rc = -EFAULT;
1931 }
1932
1933 issue_host_reset:
1934 if (issue_reset)
1935 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1936 FORCE_BIG_HAMMER);
1937
1938 out:
1939
1940 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1941 mutex_unlock(&ioc->ctl_cmds.mutex);
1942 return rc;
1943 }
1944
1945 /**
1946 * _ctl_ioctl_main - main ioctl entry point
1947 * @file - (struct file)
1948 * @cmd - ioctl opcode
1949 * @arg -
1950 */
1951 static long
1952 _ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg)
1953 {
1954 enum block_state state;
1955 long ret = -EINVAL;
1956 unsigned long flags;
1957
1958 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING :
1959 BLOCKING;
1960
1961 switch (cmd) {
1962 case MPT2IOCINFO:
1963 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
1964 ret = _ctl_getiocinfo(arg);
1965 break;
1966 case MPT2COMMAND:
1967 {
1968 struct mpt2_ioctl_command karg;
1969 struct mpt2_ioctl_command __user *uarg;
1970 struct MPT2SAS_ADAPTER *ioc;
1971
1972 if (copy_from_user(&karg, arg, sizeof(karg))) {
1973 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1974 __FILE__, __LINE__, __func__);
1975 return -EFAULT;
1976 }
1977
1978 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
1979 !ioc)
1980 return -ENODEV;
1981
1982 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
1983 if (ioc->shost_recovery) {
1984 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
1985 flags);
1986 return -EAGAIN;
1987 }
1988 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
1989
1990 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
1991 uarg = arg;
1992 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
1993 }
1994 break;
1995 }
1996 case MPT2EVENTQUERY:
1997 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
1998 ret = _ctl_eventquery(arg);
1999 break;
2000 case MPT2EVENTENABLE:
2001 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2002 ret = _ctl_eventenable(arg);
2003 break;
2004 case MPT2EVENTREPORT:
2005 ret = _ctl_eventreport(arg);
2006 break;
2007 case MPT2HARDRESET:
2008 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2009 ret = _ctl_do_reset(arg);
2010 break;
2011 case MPT2BTDHMAPPING:
2012 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2013 ret = _ctl_btdh_mapping(arg);
2014 break;
2015 case MPT2DIAGREGISTER:
2016 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2017 ret = _ctl_diag_register(arg, state);
2018 break;
2019 case MPT2DIAGUNREGISTER:
2020 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2021 ret = _ctl_diag_unregister(arg);
2022 break;
2023 case MPT2DIAGQUERY:
2024 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2025 ret = _ctl_diag_query(arg);
2026 break;
2027 case MPT2DIAGRELEASE:
2028 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2029 ret = _ctl_diag_release(arg, state);
2030 break;
2031 case MPT2DIAGREADBUFFER:
2032 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2033 ret = _ctl_diag_read_buffer(arg, state);
2034 break;
2035 default:
2036 {
2037 struct mpt2_ioctl_command karg;
2038 struct MPT2SAS_ADAPTER *ioc;
2039
2040 if (copy_from_user(&karg, arg, sizeof(karg))) {
2041 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2042 __FILE__, __LINE__, __func__);
2043 return -EFAULT;
2044 }
2045
2046 if (_ctl_verify_adapter(karg.hdr.ioc_number, &ioc) == -1 ||
2047 !ioc)
2048 return -ENODEV;
2049
2050 dctlprintk(ioc, printk(MPT2SAS_DEBUG_FMT
2051 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2052 break;
2053 }
2054 }
2055 return ret;
2056 }
2057
2058 /**
2059 * _ctl_ioctl - main ioctl entry point (unlocked)
2060 * @file - (struct file)
2061 * @cmd - ioctl opcode
2062 * @arg -
2063 */
2064 static long
2065 _ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2066 {
2067 long ret;
2068 lock_kernel();
2069 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2070 unlock_kernel();
2071 return ret;
2072 }
2073
2074 #ifdef CONFIG_COMPAT
2075 /**
2076 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2077 * @file - (struct file)
2078 * @cmd - ioctl opcode
2079 * @arg - (struct mpt2_ioctl_command32)
2080 *
2081 * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2082 */
2083 static long
2084 _ctl_compat_mpt_command(struct file *file, unsigned cmd, unsigned long arg)
2085 {
2086 struct mpt2_ioctl_command32 karg32;
2087 struct mpt2_ioctl_command32 __user *uarg;
2088 struct mpt2_ioctl_command karg;
2089 struct MPT2SAS_ADAPTER *ioc;
2090 enum block_state state;
2091 unsigned long flags;
2092
2093 if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2094 return -EINVAL;
2095
2096 uarg = (struct mpt2_ioctl_command32 __user *) arg;
2097
2098 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2099 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2100 __FILE__, __LINE__, __func__);
2101 return -EFAULT;
2102 }
2103 if (_ctl_verify_adapter(karg32.hdr.ioc_number, &ioc) == -1 || !ioc)
2104 return -ENODEV;
2105
2106 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
2107 if (ioc->shost_recovery) {
2108 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
2109 flags);
2110 return -EAGAIN;
2111 }
2112 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
2113
2114 memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2115 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2116 karg.hdr.port_number = karg32.hdr.port_number;
2117 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2118 karg.timeout = karg32.timeout;
2119 karg.max_reply_bytes = karg32.max_reply_bytes;
2120 karg.data_in_size = karg32.data_in_size;
2121 karg.data_out_size = karg32.data_out_size;
2122 karg.max_sense_bytes = karg32.max_sense_bytes;
2123 karg.data_sge_offset = karg32.data_sge_offset;
2124 memcpy(&karg.reply_frame_buf_ptr, &karg32.reply_frame_buf_ptr,
2125 sizeof(uint32_t));
2126 memcpy(&karg.data_in_buf_ptr, &karg32.data_in_buf_ptr,
2127 sizeof(uint32_t));
2128 memcpy(&karg.data_out_buf_ptr, &karg32.data_out_buf_ptr,
2129 sizeof(uint32_t));
2130 memcpy(&karg.sense_data_ptr, &karg32.sense_data_ptr,
2131 sizeof(uint32_t));
2132 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2133 return _ctl_do_mpt_command(ioc, karg, &uarg->mf, state);
2134 }
2135
2136 /**
2137 * _ctl_ioctl_compat - main ioctl entry point (compat)
2138 * @file -
2139 * @cmd -
2140 * @arg -
2141 *
2142 * This routine handles 32 bit applications in 64bit os.
2143 */
2144 static long
2145 _ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2146 {
2147 long ret;
2148 lock_kernel();
2149 if (cmd == MPT2COMMAND32)
2150 ret = _ctl_compat_mpt_command(file, cmd, arg);
2151 else
2152 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg);
2153 unlock_kernel();
2154 return ret;
2155 }
2156 #endif
2157
2158 /* scsi host attributes */
2159
2160 /**
2161 * _ctl_version_fw_show - firmware version
2162 * @cdev - pointer to embedded class device
2163 * @buf - the buffer returned
2164 *
2165 * A sysfs 'read-only' shost attribute.
2166 */
2167 static ssize_t
2168 _ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2169 char *buf)
2170 {
2171 struct Scsi_Host *shost = class_to_shost(cdev);
2172 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2173
2174 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2175 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2176 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2177 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2178 ioc->facts.FWVersion.Word & 0x000000FF);
2179 }
2180 static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2181
2182 /**
2183 * _ctl_version_bios_show - bios version
2184 * @cdev - pointer to embedded class device
2185 * @buf - the buffer returned
2186 *
2187 * A sysfs 'read-only' shost attribute.
2188 */
2189 static ssize_t
2190 _ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2191 char *buf)
2192 {
2193 struct Scsi_Host *shost = class_to_shost(cdev);
2194 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2195
2196 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2197
2198 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2199 (version & 0xFF000000) >> 24,
2200 (version & 0x00FF0000) >> 16,
2201 (version & 0x0000FF00) >> 8,
2202 version & 0x000000FF);
2203 }
2204 static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2205
2206 /**
2207 * _ctl_version_mpi_show - MPI (message passing interface) version
2208 * @cdev - pointer to embedded class device
2209 * @buf - the buffer returned
2210 *
2211 * A sysfs 'read-only' shost attribute.
2212 */
2213 static ssize_t
2214 _ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2215 char *buf)
2216 {
2217 struct Scsi_Host *shost = class_to_shost(cdev);
2218 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2219
2220 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2221 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2222 }
2223 static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2224
2225 /**
2226 * _ctl_version_product_show - product name
2227 * @cdev - pointer to embedded class device
2228 * @buf - the buffer returned
2229 *
2230 * A sysfs 'read-only' shost attribute.
2231 */
2232 static ssize_t
2233 _ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2234 char *buf)
2235 {
2236 struct Scsi_Host *shost = class_to_shost(cdev);
2237 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2238
2239 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2240 }
2241 static DEVICE_ATTR(version_product, S_IRUGO,
2242 _ctl_version_product_show, NULL);
2243
2244 /**
2245 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2246 * @cdev - pointer to embedded class device
2247 * @buf - the buffer returned
2248 *
2249 * A sysfs 'read-only' shost attribute.
2250 */
2251 static ssize_t
2252 _ctl_version_nvdata_persistent_show(struct device *cdev,
2253 struct device_attribute *attr, char *buf)
2254 {
2255 struct Scsi_Host *shost = class_to_shost(cdev);
2256 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2257
2258 return snprintf(buf, PAGE_SIZE, "%02xh\n",
2259 le16_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2260 }
2261 static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2262 _ctl_version_nvdata_persistent_show, NULL);
2263
2264 /**
2265 * _ctl_version_nvdata_default_show - nvdata default version
2266 * @cdev - pointer to embedded class device
2267 * @buf - the buffer returned
2268 *
2269 * A sysfs 'read-only' shost attribute.
2270 */
2271 static ssize_t
2272 _ctl_version_nvdata_default_show(struct device *cdev,
2273 struct device_attribute *attr, char *buf)
2274 {
2275 struct Scsi_Host *shost = class_to_shost(cdev);
2276 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2277
2278 return snprintf(buf, PAGE_SIZE, "%02xh\n",
2279 le16_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2280 }
2281 static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2282 _ctl_version_nvdata_default_show, NULL);
2283
2284 /**
2285 * _ctl_board_name_show - board name
2286 * @cdev - pointer to embedded class device
2287 * @buf - the buffer returned
2288 *
2289 * A sysfs 'read-only' shost attribute.
2290 */
2291 static ssize_t
2292 _ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2293 char *buf)
2294 {
2295 struct Scsi_Host *shost = class_to_shost(cdev);
2296 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2297
2298 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2299 }
2300 static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2301
2302 /**
2303 * _ctl_board_assembly_show - board assembly name
2304 * @cdev - pointer to embedded class device
2305 * @buf - the buffer returned
2306 *
2307 * A sysfs 'read-only' shost attribute.
2308 */
2309 static ssize_t
2310 _ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2311 char *buf)
2312 {
2313 struct Scsi_Host *shost = class_to_shost(cdev);
2314 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2315
2316 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2317 }
2318 static DEVICE_ATTR(board_assembly, S_IRUGO,
2319 _ctl_board_assembly_show, NULL);
2320
2321 /**
2322 * _ctl_board_tracer_show - board tracer number
2323 * @cdev - pointer to embedded class device
2324 * @buf - the buffer returned
2325 *
2326 * A sysfs 'read-only' shost attribute.
2327 */
2328 static ssize_t
2329 _ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2330 char *buf)
2331 {
2332 struct Scsi_Host *shost = class_to_shost(cdev);
2333 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2334
2335 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2336 }
2337 static DEVICE_ATTR(board_tracer, S_IRUGO,
2338 _ctl_board_tracer_show, NULL);
2339
2340 /**
2341 * _ctl_io_delay_show - io missing delay
2342 * @cdev - pointer to embedded class device
2343 * @buf - the buffer returned
2344 *
2345 * This is for firmware implemention for deboucing device
2346 * removal events.
2347 *
2348 * A sysfs 'read-only' shost attribute.
2349 */
2350 static ssize_t
2351 _ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2352 char *buf)
2353 {
2354 struct Scsi_Host *shost = class_to_shost(cdev);
2355 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2356
2357 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2358 }
2359 static DEVICE_ATTR(io_delay, S_IRUGO,
2360 _ctl_io_delay_show, NULL);
2361
2362 /**
2363 * _ctl_device_delay_show - device missing delay
2364 * @cdev - pointer to embedded class device
2365 * @buf - the buffer returned
2366 *
2367 * This is for firmware implemention for deboucing device
2368 * removal events.
2369 *
2370 * A sysfs 'read-only' shost attribute.
2371 */
2372 static ssize_t
2373 _ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2374 char *buf)
2375 {
2376 struct Scsi_Host *shost = class_to_shost(cdev);
2377 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2378
2379 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2380 }
2381 static DEVICE_ATTR(device_delay, S_IRUGO,
2382 _ctl_device_delay_show, NULL);
2383
2384 /**
2385 * _ctl_fw_queue_depth_show - global credits
2386 * @cdev - pointer to embedded class device
2387 * @buf - the buffer returned
2388 *
2389 * This is firmware queue depth limit
2390 *
2391 * A sysfs 'read-only' shost attribute.
2392 */
2393 static ssize_t
2394 _ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2395 char *buf)
2396 {
2397 struct Scsi_Host *shost = class_to_shost(cdev);
2398 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2399
2400 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2401 }
2402 static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2403 _ctl_fw_queue_depth_show, NULL);
2404
2405 /**
2406 * _ctl_sas_address_show - sas address
2407 * @cdev - pointer to embedded class device
2408 * @buf - the buffer returned
2409 *
2410 * This is the controller sas address
2411 *
2412 * A sysfs 'read-only' shost attribute.
2413 */
2414 static ssize_t
2415 _ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2416 char *buf)
2417 {
2418 struct Scsi_Host *shost = class_to_shost(cdev);
2419 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2420
2421 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2422 (unsigned long long)ioc->sas_hba.sas_address);
2423 }
2424 static DEVICE_ATTR(host_sas_address, S_IRUGO,
2425 _ctl_host_sas_address_show, NULL);
2426
2427 /**
2428 * _ctl_logging_level_show - logging level
2429 * @cdev - pointer to embedded class device
2430 * @buf - the buffer returned
2431 *
2432 * A sysfs 'read/write' shost attribute.
2433 */
2434 static ssize_t
2435 _ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2436 char *buf)
2437 {
2438 struct Scsi_Host *shost = class_to_shost(cdev);
2439 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2440
2441 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2442 }
2443 static ssize_t
2444 _ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2445 const char *buf, size_t count)
2446 {
2447 struct Scsi_Host *shost = class_to_shost(cdev);
2448 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2449 int val = 0;
2450
2451 if (sscanf(buf, "%x", &val) != 1)
2452 return -EINVAL;
2453
2454 ioc->logging_level = val;
2455 printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2456 ioc->logging_level);
2457 return strlen(buf);
2458 }
2459 static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2460 _ctl_logging_level_show, _ctl_logging_level_store);
2461
2462 struct device_attribute *mpt2sas_host_attrs[] = {
2463 &dev_attr_version_fw,
2464 &dev_attr_version_bios,
2465 &dev_attr_version_mpi,
2466 &dev_attr_version_product,
2467 &dev_attr_version_nvdata_persistent,
2468 &dev_attr_version_nvdata_default,
2469 &dev_attr_board_name,
2470 &dev_attr_board_assembly,
2471 &dev_attr_board_tracer,
2472 &dev_attr_io_delay,
2473 &dev_attr_device_delay,
2474 &dev_attr_logging_level,
2475 &dev_attr_fw_queue_depth,
2476 &dev_attr_host_sas_address,
2477 NULL,
2478 };
2479
2480 /* device attributes */
2481
2482 /**
2483 * _ctl_device_sas_address_show - sas address
2484 * @cdev - pointer to embedded class device
2485 * @buf - the buffer returned
2486 *
2487 * This is the sas address for the target
2488 *
2489 * A sysfs 'read-only' shost attribute.
2490 */
2491 static ssize_t
2492 _ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2493 char *buf)
2494 {
2495 struct scsi_device *sdev = to_scsi_device(dev);
2496 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2497
2498 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2499 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2500 }
2501 static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2502
2503 /**
2504 * _ctl_device_handle_show - device handle
2505 * @cdev - pointer to embedded class device
2506 * @buf - the buffer returned
2507 *
2508 * This is the firmware assigned device handle
2509 *
2510 * A sysfs 'read-only' shost attribute.
2511 */
2512 static ssize_t
2513 _ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
2514 char *buf)
2515 {
2516 struct scsi_device *sdev = to_scsi_device(dev);
2517 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2518
2519 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
2520 sas_device_priv_data->sas_target->handle);
2521 }
2522 static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
2523
2524 struct device_attribute *mpt2sas_dev_attrs[] = {
2525 &dev_attr_sas_address,
2526 &dev_attr_sas_device_handle,
2527 NULL,
2528 };
2529
2530 static const struct file_operations ctl_fops = {
2531 .owner = THIS_MODULE,
2532 .unlocked_ioctl = _ctl_ioctl,
2533 .release = _ctl_release,
2534 .poll = _ctl_poll,
2535 .fasync = _ctl_fasync,
2536 #ifdef CONFIG_COMPAT
2537 .compat_ioctl = _ctl_ioctl_compat,
2538 #endif
2539 };
2540
2541 static struct miscdevice ctl_dev = {
2542 .minor = MPT2SAS_MINOR,
2543 .name = MPT2SAS_DEV_NAME,
2544 .fops = &ctl_fops,
2545 };
2546
2547 /**
2548 * mpt2sas_ctl_init - main entry point for ctl.
2549 *
2550 */
2551 void
2552 mpt2sas_ctl_init(void)
2553 {
2554 async_queue = NULL;
2555 if (misc_register(&ctl_dev) < 0)
2556 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
2557 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
2558
2559 init_waitqueue_head(&ctl_poll_wait);
2560 }
2561
2562 /**
2563 * mpt2sas_ctl_exit - exit point for ctl
2564 *
2565 */
2566 void
2567 mpt2sas_ctl_exit(void)
2568 {
2569 struct MPT2SAS_ADAPTER *ioc;
2570 int i;
2571
2572 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
2573
2574 /* free memory associated to diag buffers */
2575 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
2576 if (!ioc->diag_buffer[i])
2577 continue;
2578 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
2579 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
2580 ioc->diag_buffer[i] = NULL;
2581 ioc->diag_buffer_status[i] = 0;
2582 }
2583
2584 kfree(ioc->event_log);
2585 }
2586 misc_deregister(&ctl_dev);
2587 }
2588