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