blob: 75ae533ac8d9e4c9e6cd4814f7ec2db1f30d44a2 [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);
Suganath prabu Subramani869817f2016-01-28 12:07:00 +0530404 if (mpi_reply)
405 mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530406 return 1;
407}
408
409/**
410 * _ctl_verify_adapter - validates ioc_number passed from application
411 * @ioc: per adapter object
412 * @iocpp: The ioc pointer is returned in this.
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530413 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530414 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530415 *
416 * Return (-1) means error, else ioc_number.
417 */
418static int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530419_ctl_verify_adapter(int ioc_number, struct MPT3SAS_ADAPTER **iocpp,
420 int mpi_version)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530421{
422 struct MPT3SAS_ADAPTER *ioc;
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530423 int version = 0;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530424 /* global ioc lock to protect controller on list operations */
425 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530426 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
427 if (ioc->id != ioc_number)
428 continue;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530429 /* Check whether this ioctl command is from right
430 * ioctl device or not, if not continue the search.
431 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +0530432 version = ioc->hba_mpi_version_belonged;
433 /* MPI25_VERSION and MPI26_VERSION uses same ioctl
434 * device.
435 */
436 if (mpi_version == (MPI25_VERSION | MPI26_VERSION)) {
437 if ((version == MPI25_VERSION) ||
438 (version == MPI26_VERSION))
439 goto out;
440 else
441 continue;
442 } else {
443 if (version != mpi_version)
444 continue;
445 }
446out:
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530447 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530448 *iocpp = ioc;
449 return ioc_number;
450 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530451 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530452 *iocpp = NULL;
453 return -1;
454}
455
456/**
457 * mpt3sas_ctl_reset_handler - reset callback handler (for ctl)
458 * @ioc: per adapter object
459 * @reset_phase: phase
460 *
461 * The handler for doing any required cleanup or initialization.
462 *
463 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
464 * MPT3_IOC_DONE_RESET
465 */
466void
467mpt3sas_ctl_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
468{
469 int i;
470 u8 issue_reset;
471
472 switch (reset_phase) {
473 case MPT3_IOC_PRE_RESET:
474 dtmprintk(ioc, pr_info(MPT3SAS_FMT
475 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
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 mpt3sas_send_diag_release(ioc, i, &issue_reset);
484 }
485 break;
486 case MPT3_IOC_AFTER_RESET:
487 dtmprintk(ioc, pr_info(MPT3SAS_FMT
488 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
489 if (ioc->ctl_cmds.status & MPT3_CMD_PENDING) {
490 ioc->ctl_cmds.status |= MPT3_CMD_RESET;
491 mpt3sas_base_free_smid(ioc, ioc->ctl_cmds.smid);
492 complete(&ioc->ctl_cmds.done);
493 }
494 break;
495 case MPT3_IOC_DONE_RESET:
496 dtmprintk(ioc, pr_info(MPT3SAS_FMT
497 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
498
499 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
500 if (!(ioc->diag_buffer_status[i] &
501 MPT3_DIAG_BUFFER_IS_REGISTERED))
502 continue;
503 if ((ioc->diag_buffer_status[i] &
504 MPT3_DIAG_BUFFER_IS_RELEASED))
505 continue;
506 ioc->diag_buffer_status[i] |=
507 MPT3_DIAG_BUFFER_IS_DIAG_RESET;
508 }
509 break;
510 }
511}
512
513/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530514 * _ctl_fasync -
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530515 * @fd -
516 * @filep -
517 * @mode -
518 *
519 * Called when application request fasyn callback handler.
520 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +0530521int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530522_ctl_fasync(int fd, struct file *filep, int mode)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530523{
524 return fasync_helper(fd, filep, mode, &async_queue);
525}
526
527/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530528 * _ctl_poll -
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530529 * @file -
530 * @wait -
531 *
532 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +0530533unsigned int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530534_ctl_poll(struct file *filep, poll_table *wait)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530535{
536 struct MPT3SAS_ADAPTER *ioc;
537
538 poll_wait(filep, &ctl_poll_wait, wait);
539
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530540 /* global ioc lock to protect controller on list operations */
541 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530542 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530543 if (ioc->aen_event_read_flag) {
544 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530545 return POLLIN | POLLRDNORM;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530546 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530547 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530548 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530549 return 0;
550}
551
552/**
553 * _ctl_set_task_mid - assign an active smid to tm request
554 * @ioc: per adapter object
555 * @karg - (struct mpt3_ioctl_command)
556 * @tm_request - pointer to mf from user space
557 *
558 * Returns 0 when an smid if found, else fail.
559 * during failure, the reply frame is filled.
560 */
561static int
562_ctl_set_task_mid(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command *karg,
563 Mpi2SCSITaskManagementRequest_t *tm_request)
564{
565 u8 found = 0;
566 u16 i;
567 u16 handle;
568 struct scsi_cmnd *scmd;
569 struct MPT3SAS_DEVICE *priv_data;
570 unsigned long flags;
571 Mpi2SCSITaskManagementReply_t *tm_reply;
572 u32 sz;
573 u32 lun;
574 char *desc = NULL;
575
576 if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
577 desc = "abort_task";
578 else if (tm_request->TaskType == MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK)
579 desc = "query_task";
580 else
581 return 0;
582
583 lun = scsilun_to_int((struct scsi_lun *)tm_request->LUN);
584
585 handle = le16_to_cpu(tm_request->DevHandle);
586 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
587 for (i = ioc->scsiio_depth; i && !found; i--) {
588 scmd = ioc->scsi_lookup[i - 1].scmd;
589 if (scmd == NULL || scmd->device == NULL ||
590 scmd->device->hostdata == NULL)
591 continue;
592 if (lun != scmd->device->lun)
593 continue;
594 priv_data = scmd->device->hostdata;
595 if (priv_data->sas_target == NULL)
596 continue;
597 if (priv_data->sas_target->handle != handle)
598 continue;
599 tm_request->TaskMID = cpu_to_le16(ioc->scsi_lookup[i - 1].smid);
600 found = 1;
601 }
602 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
603
604 if (!found) {
605 dctlprintk(ioc, pr_info(MPT3SAS_FMT
606 "%s: handle(0x%04x), lun(%d), no active mid!!\n",
607 ioc->name,
608 desc, le16_to_cpu(tm_request->DevHandle), lun));
609 tm_reply = ioc->ctl_cmds.reply;
610 tm_reply->DevHandle = tm_request->DevHandle;
611 tm_reply->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
612 tm_reply->TaskType = tm_request->TaskType;
613 tm_reply->MsgLength = sizeof(Mpi2SCSITaskManagementReply_t)/4;
614 tm_reply->VP_ID = tm_request->VP_ID;
615 tm_reply->VF_ID = tm_request->VF_ID;
616 sz = min_t(u32, karg->max_reply_bytes, ioc->reply_sz);
617 if (copy_to_user(karg->reply_frame_buf_ptr, ioc->ctl_cmds.reply,
618 sz))
619 pr_err("failure at %s:%d/%s()!\n", __FILE__,
620 __LINE__, __func__);
621 return 1;
622 }
623
624 dctlprintk(ioc, pr_info(MPT3SAS_FMT
625 "%s: handle(0x%04x), lun(%d), task_mid(%d)\n", ioc->name,
626 desc, le16_to_cpu(tm_request->DevHandle), lun,
627 le16_to_cpu(tm_request->TaskMID)));
628 return 0;
629}
630
631/**
632 * _ctl_do_mpt_command - main handler for MPT3COMMAND opcode
633 * @ioc: per adapter object
634 * @karg - (struct mpt3_ioctl_command)
635 * @mf - pointer to mf in user space
636 */
637static long
638_ctl_do_mpt_command(struct MPT3SAS_ADAPTER *ioc, struct mpt3_ioctl_command karg,
639 void __user *mf)
640{
641 MPI2RequestHeader_t *mpi_request = NULL, *request;
642 MPI2DefaultReply_t *mpi_reply;
643 u32 ioc_state;
644 u16 ioc_status;
645 u16 smid;
646 unsigned long timeout, timeleft;
647 u8 issue_reset;
648 u32 sz;
649 void *psge;
650 void *data_out = NULL;
651 dma_addr_t data_out_dma = 0;
652 size_t data_out_sz = 0;
653 void *data_in = NULL;
654 dma_addr_t data_in_dma = 0;
655 size_t data_in_sz = 0;
656 long ret;
657 u16 wait_state_count;
658
659 issue_reset = 0;
660
661 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
662 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
663 ioc->name, __func__);
664 ret = -EAGAIN;
665 goto out;
666 }
667
668 wait_state_count = 0;
669 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
670 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
671 if (wait_state_count++ == 10) {
672 pr_err(MPT3SAS_FMT
673 "%s: failed due to ioc not operational\n",
674 ioc->name, __func__);
675 ret = -EFAULT;
676 goto out;
677 }
678 ssleep(1);
679 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
680 pr_info(MPT3SAS_FMT
681 "%s: waiting for operational state(count=%d)\n",
682 ioc->name,
683 __func__, wait_state_count);
684 }
685 if (wait_state_count)
686 pr_info(MPT3SAS_FMT "%s: ioc is operational\n",
687 ioc->name, __func__);
688
689 mpi_request = kzalloc(ioc->request_sz, GFP_KERNEL);
690 if (!mpi_request) {
691 pr_err(MPT3SAS_FMT
692 "%s: failed obtaining a memory for mpi_request\n",
693 ioc->name, __func__);
694 ret = -ENOMEM;
695 goto out;
696 }
697
698 /* Check for overflow and wraparound */
699 if (karg.data_sge_offset * 4 > ioc->request_sz ||
700 karg.data_sge_offset > (UINT_MAX / 4)) {
701 ret = -EINVAL;
702 goto out;
703 }
704
705 /* copy in request message frame from user */
706 if (copy_from_user(mpi_request, mf, karg.data_sge_offset*4)) {
707 pr_err("failure at %s:%d/%s()!\n", __FILE__, __LINE__,
708 __func__);
709 ret = -EFAULT;
710 goto out;
711 }
712
713 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
714 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->ctl_cb_idx);
715 if (!smid) {
716 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
717 ioc->name, __func__);
718 ret = -EAGAIN;
719 goto out;
720 }
721 } else {
722
723 smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->ctl_cb_idx, NULL);
724 if (!smid) {
725 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
726 ioc->name, __func__);
727 ret = -EAGAIN;
728 goto out;
729 }
730 }
731
732 ret = 0;
733 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
734 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
735 request = mpt3sas_base_get_msg_frame(ioc, smid);
736 memcpy(request, mpi_request, karg.data_sge_offset*4);
737 ioc->ctl_cmds.smid = smid;
738 data_out_sz = karg.data_out_size;
739 data_in_sz = karg.data_in_size;
740
741 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
742 mpi_request->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
743 if (!le16_to_cpu(mpi_request->FunctionDependent1) ||
744 le16_to_cpu(mpi_request->FunctionDependent1) >
745 ioc->facts.MaxDevHandle) {
746 ret = -EINVAL;
747 mpt3sas_base_free_smid(ioc, smid);
748 goto out;
749 }
750 }
751
752 /* obtain dma-able memory for data transfer */
753 if (data_out_sz) /* WRITE */ {
754 data_out = pci_alloc_consistent(ioc->pdev, data_out_sz,
755 &data_out_dma);
756 if (!data_out) {
757 pr_err("failure at %s:%d/%s()!\n", __FILE__,
758 __LINE__, __func__);
759 ret = -ENOMEM;
760 mpt3sas_base_free_smid(ioc, smid);
761 goto out;
762 }
763 if (copy_from_user(data_out, karg.data_out_buf_ptr,
764 data_out_sz)) {
765 pr_err("failure at %s:%d/%s()!\n", __FILE__,
766 __LINE__, __func__);
767 ret = -EFAULT;
768 mpt3sas_base_free_smid(ioc, smid);
769 goto out;
770 }
771 }
772
773 if (data_in_sz) /* READ */ {
774 data_in = pci_alloc_consistent(ioc->pdev, data_in_sz,
775 &data_in_dma);
776 if (!data_in) {
777 pr_err("failure at %s:%d/%s()!\n", __FILE__,
778 __LINE__, __func__);
779 ret = -ENOMEM;
780 mpt3sas_base_free_smid(ioc, smid);
781 goto out;
782 }
783 }
784
785 psge = (void *)request + (karg.data_sge_offset*4);
786
787 /* send command to firmware */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530788 _ctl_display_some_debug(ioc, smid, "ctl_request", NULL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530789
790 init_completion(&ioc->ctl_cmds.done);
791 switch (mpi_request->Function) {
792 case MPI2_FUNCTION_SCSI_IO_REQUEST:
793 case MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
794 {
795 Mpi2SCSIIORequest_t *scsiio_request =
796 (Mpi2SCSIIORequest_t *)request;
797 scsiio_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
798 scsiio_request->SenseBufferLowAddress =
799 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
800 memset(ioc->ctl_cmds.sense, 0, SCSI_SENSE_BUFFERSIZE);
801 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
802 data_in_dma, data_in_sz);
803
804 if (mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)
805 mpt3sas_base_put_smid_scsi_io(ioc, smid,
806 le16_to_cpu(mpi_request->FunctionDependent1));
807 else
808 mpt3sas_base_put_smid_default(ioc, smid);
809 break;
810 }
811 case MPI2_FUNCTION_SCSI_TASK_MGMT:
812 {
813 Mpi2SCSITaskManagementRequest_t *tm_request =
814 (Mpi2SCSITaskManagementRequest_t *)request;
815
816 dtmprintk(ioc, pr_info(MPT3SAS_FMT
817 "TASK_MGMT: handle(0x%04x), task_type(0x%02x)\n",
818 ioc->name,
819 le16_to_cpu(tm_request->DevHandle), tm_request->TaskType));
820
821 if (tm_request->TaskType ==
822 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK ||
823 tm_request->TaskType ==
824 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK) {
825 if (_ctl_set_task_mid(ioc, &karg, tm_request)) {
826 mpt3sas_base_free_smid(ioc, smid);
827 goto out;
828 }
829 }
830
831 mpt3sas_scsih_set_tm_flag(ioc, le16_to_cpu(
832 tm_request->DevHandle));
833 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
834 data_in_dma, data_in_sz);
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +0530835 mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530836 break;
837 }
838 case MPI2_FUNCTION_SMP_PASSTHROUGH:
839 {
840 Mpi2SmpPassthroughRequest_t *smp_request =
841 (Mpi2SmpPassthroughRequest_t *)mpi_request;
842 u8 *data;
843
844 /* ioc determines which port to use */
845 smp_request->PhysicalPort = 0xFF;
846 if (smp_request->PassthroughFlags &
847 MPI2_SMP_PT_REQ_PT_FLAGS_IMMEDIATE)
848 data = (u8 *)&smp_request->SGL;
849 else {
850 if (unlikely(data_out == NULL)) {
851 pr_err("failure at %s:%d/%s()!\n",
852 __FILE__, __LINE__, __func__);
853 mpt3sas_base_free_smid(ioc, smid);
854 ret = -EINVAL;
855 goto out;
856 }
857 data = data_out;
858 }
859
860 if (data[1] == 0x91 && (data[10] == 1 || data[10] == 2)) {
861 ioc->ioc_link_reset_in_progress = 1;
862 ioc->ignore_loginfos = 1;
863 }
864 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
865 data_in_sz);
866 mpt3sas_base_put_smid_default(ioc, smid);
867 break;
868 }
869 case MPI2_FUNCTION_SATA_PASSTHROUGH:
870 case MPI2_FUNCTION_FW_DOWNLOAD:
871 case MPI2_FUNCTION_FW_UPLOAD:
872 {
873 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz, data_in_dma,
874 data_in_sz);
875 mpt3sas_base_put_smid_default(ioc, smid);
876 break;
877 }
878 case MPI2_FUNCTION_TOOLBOX:
879 {
880 Mpi2ToolboxCleanRequest_t *toolbox_request =
881 (Mpi2ToolboxCleanRequest_t *)mpi_request;
882
883 if (toolbox_request->Tool == MPI2_TOOLBOX_DIAGNOSTIC_CLI_TOOL) {
884 ioc->build_sg(ioc, psge, data_out_dma, data_out_sz,
885 data_in_dma, data_in_sz);
886 } else {
887 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
888 data_in_dma, data_in_sz);
889 }
890 mpt3sas_base_put_smid_default(ioc, smid);
891 break;
892 }
893 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
894 {
895 Mpi2SasIoUnitControlRequest_t *sasiounit_request =
896 (Mpi2SasIoUnitControlRequest_t *)mpi_request;
897
898 if (sasiounit_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET
899 || sasiounit_request->Operation ==
900 MPI2_SAS_OP_PHY_LINK_RESET) {
901 ioc->ioc_link_reset_in_progress = 1;
902 ioc->ignore_loginfos = 1;
903 }
904 /* drop to default case for posting the request */
905 }
906 default:
907 ioc->build_sg_mpi(ioc, psge, data_out_dma, data_out_sz,
908 data_in_dma, data_in_sz);
909 mpt3sas_base_put_smid_default(ioc, smid);
910 break;
911 }
912
913 if (karg.timeout < MPT3_IOCTL_DEFAULT_TIMEOUT)
914 timeout = MPT3_IOCTL_DEFAULT_TIMEOUT;
915 else
916 timeout = karg.timeout;
917 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
918 timeout*HZ);
919 if (mpi_request->Function == MPI2_FUNCTION_SCSI_TASK_MGMT) {
920 Mpi2SCSITaskManagementRequest_t *tm_request =
921 (Mpi2SCSITaskManagementRequest_t *)mpi_request;
922 mpt3sas_scsih_clear_tm_flag(ioc, le16_to_cpu(
923 tm_request->DevHandle));
924 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
925 } else if ((mpi_request->Function == MPI2_FUNCTION_SMP_PASSTHROUGH ||
926 mpi_request->Function == MPI2_FUNCTION_SAS_IO_UNIT_CONTROL) &&
927 ioc->ioc_link_reset_in_progress) {
928 ioc->ioc_link_reset_in_progress = 0;
929 ioc->ignore_loginfos = 0;
930 }
931 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
932 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
933 __func__);
934 _debug_dump_mf(mpi_request, karg.data_sge_offset);
935 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
936 issue_reset = 1;
937 goto issue_host_reset;
938 }
939
940 mpi_reply = ioc->ctl_cmds.reply;
941 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
942
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530943 if (mpi_reply->Function == MPI2_FUNCTION_SCSI_TASK_MGMT &&
944 (ioc->logging_level & MPT_DEBUG_TM)) {
945 Mpi2SCSITaskManagementReply_t *tm_reply =
946 (Mpi2SCSITaskManagementReply_t *)mpi_reply;
947
948 pr_info(MPT3SAS_FMT "TASK_MGMT: " \
949 "IOCStatus(0x%04x), IOCLogInfo(0x%08x), "
950 "TerminationCount(0x%08x)\n", ioc->name,
951 le16_to_cpu(tm_reply->IOCStatus),
952 le32_to_cpu(tm_reply->IOCLogInfo),
953 le32_to_cpu(tm_reply->TerminationCount));
954 }
Sreekanth Reddyaf009412015-11-11 17:30:23 +0530955
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530956 /* copy out xdata to user */
957 if (data_in_sz) {
958 if (copy_to_user(karg.data_in_buf_ptr, data_in,
959 data_in_sz)) {
960 pr_err("failure at %s:%d/%s()!\n", __FILE__,
961 __LINE__, __func__);
962 ret = -ENODATA;
963 goto out;
964 }
965 }
966
967 /* copy out reply message frame to user */
968 if (karg.max_reply_bytes) {
969 sz = min_t(u32, karg.max_reply_bytes, ioc->reply_sz);
970 if (copy_to_user(karg.reply_frame_buf_ptr, ioc->ctl_cmds.reply,
971 sz)) {
972 pr_err("failure at %s:%d/%s()!\n", __FILE__,
973 __LINE__, __func__);
974 ret = -ENODATA;
975 goto out;
976 }
977 }
978
979 /* copy out sense to user */
980 if (karg.max_sense_bytes && (mpi_request->Function ==
981 MPI2_FUNCTION_SCSI_IO_REQUEST || mpi_request->Function ==
982 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
983 sz = min_t(u32, karg.max_sense_bytes, SCSI_SENSE_BUFFERSIZE);
984 if (copy_to_user(karg.sense_data_ptr, ioc->ctl_cmds.sense,
985 sz)) {
986 pr_err("failure at %s:%d/%s()!\n", __FILE__,
987 __LINE__, __func__);
988 ret = -ENODATA;
989 goto out;
990 }
991 }
992
993 issue_host_reset:
994 if (issue_reset) {
995 ret = -ENODATA;
996 if ((mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
997 mpi_request->Function ==
998 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
999 mpi_request->Function == MPI2_FUNCTION_SATA_PASSTHROUGH)) {
1000 pr_info(MPT3SAS_FMT "issue target reset: handle = (0x%04x)\n",
1001 ioc->name,
1002 le16_to_cpu(mpi_request->FunctionDependent1));
1003 mpt3sas_halt_firmware(ioc);
Calvin Owens96902832016-07-28 21:38:20 -07001004 mpt3sas_scsih_issue_locked_tm(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301005 le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
Calvin Owens96902832016-07-28 21:38:20 -07001006 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 30);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301007 } else
1008 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1009 FORCE_BIG_HAMMER);
1010 }
1011
1012 out:
1013
1014 /* free memory associated with sg buffers */
1015 if (data_in)
1016 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1017 data_in_dma);
1018
1019 if (data_out)
1020 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1021 data_out_dma);
1022
1023 kfree(mpi_request);
1024 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1025 return ret;
1026}
1027
1028/**
1029 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1030 * @ioc: per adapter object
1031 * @arg - user space buffer containing ioctl content
1032 */
1033static long
1034_ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1035{
1036 struct mpt3_ioctl_iocinfo karg;
1037
1038 if (copy_from_user(&karg, arg, sizeof(karg))) {
1039 pr_err("failure at %s:%d/%s()!\n",
1040 __FILE__, __LINE__, __func__);
1041 return -EFAULT;
1042 }
1043
1044 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1045 __func__));
1046
1047 memset(&karg, 0 , sizeof(karg));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301048 if (ioc->pfacts)
1049 karg.port_number = ioc->pfacts[0].PortNumber;
1050 karg.hw_rev = ioc->pdev->revision;
1051 karg.pci_id = ioc->pdev->device;
1052 karg.subsystem_device = ioc->pdev->subsystem_device;
1053 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1054 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1055 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1056 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1057 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1058 karg.firmware_version = ioc->facts.FWVersion.Word;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301059 strcpy(karg.driver_version, ioc->driver_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301060 strcat(karg.driver_version, "-");
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301061 switch (ioc->hba_mpi_version_belonged) {
1062 case MPI2_VERSION:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301063 if (ioc->is_warpdrive)
1064 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1065 else
1066 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301067 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1068 break;
1069 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301070 case MPI26_VERSION:
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301071 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1072 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1073 break;
1074 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301075 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1076
1077 if (copy_to_user(arg, &karg, sizeof(karg))) {
1078 pr_err("failure at %s:%d/%s()!\n",
1079 __FILE__, __LINE__, __func__);
1080 return -EFAULT;
1081 }
1082 return 0;
1083}
1084
1085/**
1086 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1087 * @ioc: per adapter object
1088 * @arg - user space buffer containing ioctl content
1089 */
1090static long
1091_ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1092{
1093 struct mpt3_ioctl_eventquery karg;
1094
1095 if (copy_from_user(&karg, arg, sizeof(karg))) {
1096 pr_err("failure at %s:%d/%s()!\n",
1097 __FILE__, __LINE__, __func__);
1098 return -EFAULT;
1099 }
1100
1101 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1102 __func__));
1103
1104 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1105 memcpy(karg.event_types, ioc->event_type,
1106 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1107
1108 if (copy_to_user(arg, &karg, sizeof(karg))) {
1109 pr_err("failure at %s:%d/%s()!\n",
1110 __FILE__, __LINE__, __func__);
1111 return -EFAULT;
1112 }
1113 return 0;
1114}
1115
1116/**
1117 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1118 * @ioc: per adapter object
1119 * @arg - user space buffer containing ioctl content
1120 */
1121static long
1122_ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1123{
1124 struct mpt3_ioctl_eventenable karg;
1125
1126 if (copy_from_user(&karg, arg, sizeof(karg))) {
1127 pr_err("failure at %s:%d/%s()!\n",
1128 __FILE__, __LINE__, __func__);
1129 return -EFAULT;
1130 }
1131
1132 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1133 __func__));
1134
1135 memcpy(ioc->event_type, karg.event_types,
1136 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1137 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1138
1139 if (ioc->event_log)
1140 return 0;
1141 /* initialize event_log */
1142 ioc->event_context = 0;
1143 ioc->aen_event_read_flag = 0;
1144 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1145 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1146 if (!ioc->event_log) {
1147 pr_err("failure at %s:%d/%s()!\n",
1148 __FILE__, __LINE__, __func__);
1149 return -ENOMEM;
1150 }
1151 return 0;
1152}
1153
1154/**
1155 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1156 * @ioc: per adapter object
1157 * @arg - user space buffer containing ioctl content
1158 */
1159static long
1160_ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1161{
1162 struct mpt3_ioctl_eventreport karg;
1163 u32 number_bytes, max_events, max;
1164 struct mpt3_ioctl_eventreport __user *uarg = arg;
1165
1166 if (copy_from_user(&karg, arg, sizeof(karg))) {
1167 pr_err("failure at %s:%d/%s()!\n",
1168 __FILE__, __LINE__, __func__);
1169 return -EFAULT;
1170 }
1171
1172 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1173 __func__));
1174
1175 number_bytes = karg.hdr.max_data_size -
1176 sizeof(struct mpt3_ioctl_header);
1177 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1178 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1179
1180 /* If fewer than 1 event is requested, there must have
1181 * been some type of error.
1182 */
1183 if (!max || !ioc->event_log)
1184 return -ENODATA;
1185
1186 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1187 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1188 pr_err("failure at %s:%d/%s()!\n",
1189 __FILE__, __LINE__, __func__);
1190 return -EFAULT;
1191 }
1192
1193 /* reset flag so SIGIO can restart */
1194 ioc->aen_event_read_flag = 0;
1195 return 0;
1196}
1197
1198/**
1199 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1200 * @ioc: per adapter object
1201 * @arg - user space buffer containing ioctl content
1202 */
1203static long
1204_ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1205{
1206 struct mpt3_ioctl_diag_reset karg;
1207 int retval;
1208
1209 if (copy_from_user(&karg, arg, sizeof(karg))) {
1210 pr_err("failure at %s:%d/%s()!\n",
1211 __FILE__, __LINE__, __func__);
1212 return -EFAULT;
1213 }
1214
1215 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1216 ioc->is_driver_loading)
1217 return -EAGAIN;
1218
1219 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1220 __func__));
1221
1222 retval = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1223 FORCE_BIG_HAMMER);
1224 pr_info(MPT3SAS_FMT "host reset: %s\n",
1225 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1226 return 0;
1227}
1228
1229/**
1230 * _ctl_btdh_search_sas_device - searching for sas device
1231 * @ioc: per adapter object
1232 * @btdh: btdh ioctl payload
1233 */
1234static int
1235_ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1236 struct mpt3_ioctl_btdh_mapping *btdh)
1237{
1238 struct _sas_device *sas_device;
1239 unsigned long flags;
1240 int rc = 0;
1241
1242 if (list_empty(&ioc->sas_device_list))
1243 return rc;
1244
1245 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1246 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1247 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1248 btdh->handle == sas_device->handle) {
1249 btdh->bus = sas_device->channel;
1250 btdh->id = sas_device->id;
1251 rc = 1;
1252 goto out;
1253 } else if (btdh->bus == sas_device->channel && btdh->id ==
1254 sas_device->id && btdh->handle == 0xFFFF) {
1255 btdh->handle = sas_device->handle;
1256 rc = 1;
1257 goto out;
1258 }
1259 }
1260 out:
1261 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1262 return rc;
1263}
1264
1265/**
1266 * _ctl_btdh_search_raid_device - searching for raid device
1267 * @ioc: per adapter object
1268 * @btdh: btdh ioctl payload
1269 */
1270static int
1271_ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1272 struct mpt3_ioctl_btdh_mapping *btdh)
1273{
1274 struct _raid_device *raid_device;
1275 unsigned long flags;
1276 int rc = 0;
1277
1278 if (list_empty(&ioc->raid_device_list))
1279 return rc;
1280
1281 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1282 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1283 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1284 btdh->handle == raid_device->handle) {
1285 btdh->bus = raid_device->channel;
1286 btdh->id = raid_device->id;
1287 rc = 1;
1288 goto out;
1289 } else if (btdh->bus == raid_device->channel && btdh->id ==
1290 raid_device->id && btdh->handle == 0xFFFF) {
1291 btdh->handle = raid_device->handle;
1292 rc = 1;
1293 goto out;
1294 }
1295 }
1296 out:
1297 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1298 return rc;
1299}
1300
1301/**
1302 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1303 * @ioc: per adapter object
1304 * @arg - user space buffer containing ioctl content
1305 */
1306static long
1307_ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1308{
1309 struct mpt3_ioctl_btdh_mapping karg;
1310 int rc;
1311
1312 if (copy_from_user(&karg, arg, sizeof(karg))) {
1313 pr_err("failure at %s:%d/%s()!\n",
1314 __FILE__, __LINE__, __func__);
1315 return -EFAULT;
1316 }
1317
1318 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1319 __func__));
1320
1321 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1322 if (!rc)
1323 _ctl_btdh_search_raid_device(ioc, &karg);
1324
1325 if (copy_to_user(arg, &karg, sizeof(karg))) {
1326 pr_err("failure at %s:%d/%s()!\n",
1327 __FILE__, __LINE__, __func__);
1328 return -EFAULT;
1329 }
1330 return 0;
1331}
1332
1333/**
1334 * _ctl_diag_capability - return diag buffer capability
1335 * @ioc: per adapter object
1336 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1337 *
1338 * returns 1 when diag buffer support is enabled in firmware
1339 */
1340static u8
1341_ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1342{
1343 u8 rc = 0;
1344
1345 switch (buffer_type) {
1346 case MPI2_DIAG_BUF_TYPE_TRACE:
1347 if (ioc->facts.IOCCapabilities &
1348 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1349 rc = 1;
1350 break;
1351 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1352 if (ioc->facts.IOCCapabilities &
1353 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1354 rc = 1;
1355 break;
1356 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1357 if (ioc->facts.IOCCapabilities &
1358 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1359 rc = 1;
1360 }
1361
1362 return rc;
1363}
1364
1365
1366/**
1367 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1368 * @ioc: per adapter object
1369 * @diag_register: the diag_register struct passed in from user space
1370 *
1371 */
1372static long
1373_ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1374 struct mpt3_diag_register *diag_register)
1375{
1376 int rc, i;
1377 void *request_data = NULL;
1378 dma_addr_t request_data_dma;
1379 u32 request_data_sz = 0;
1380 Mpi2DiagBufferPostRequest_t *mpi_request;
1381 Mpi2DiagBufferPostReply_t *mpi_reply;
1382 u8 buffer_type;
1383 unsigned long timeleft;
1384 u16 smid;
1385 u16 ioc_status;
1386 u32 ioc_state;
1387 u8 issue_reset = 0;
1388
1389 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1390 __func__));
1391
1392 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1393 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1394 pr_err(MPT3SAS_FMT
1395 "%s: failed due to ioc not operational\n",
1396 ioc->name, __func__);
1397 rc = -EAGAIN;
1398 goto out;
1399 }
1400
1401 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1402 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1403 ioc->name, __func__);
1404 rc = -EAGAIN;
1405 goto out;
1406 }
1407
1408 buffer_type = diag_register->buffer_type;
1409 if (!_ctl_diag_capability(ioc, buffer_type)) {
1410 pr_err(MPT3SAS_FMT
1411 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1412 ioc->name, __func__, buffer_type);
1413 return -EPERM;
1414 }
1415
1416 if (ioc->diag_buffer_status[buffer_type] &
1417 MPT3_DIAG_BUFFER_IS_REGISTERED) {
1418 pr_err(MPT3SAS_FMT
1419 "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1420 ioc->name, __func__,
1421 buffer_type);
1422 return -EINVAL;
1423 }
1424
1425 if (diag_register->requested_buffer_size % 4) {
1426 pr_err(MPT3SAS_FMT
1427 "%s: the requested_buffer_size is not 4 byte aligned\n",
1428 ioc->name, __func__);
1429 return -EINVAL;
1430 }
1431
1432 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1433 if (!smid) {
1434 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1435 ioc->name, __func__);
1436 rc = -EAGAIN;
1437 goto out;
1438 }
1439
1440 rc = 0;
1441 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1442 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1443 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1444 ioc->ctl_cmds.smid = smid;
1445
1446 request_data = ioc->diag_buffer[buffer_type];
1447 request_data_sz = diag_register->requested_buffer_size;
1448 ioc->unique_id[buffer_type] = diag_register->unique_id;
1449 ioc->diag_buffer_status[buffer_type] = 0;
1450 memcpy(ioc->product_specific[buffer_type],
1451 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1452 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1453
1454 if (request_data) {
1455 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1456 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1457 pci_free_consistent(ioc->pdev,
1458 ioc->diag_buffer_sz[buffer_type],
1459 request_data, request_data_dma);
1460 request_data = NULL;
1461 }
1462 }
1463
1464 if (request_data == NULL) {
1465 ioc->diag_buffer_sz[buffer_type] = 0;
1466 ioc->diag_buffer_dma[buffer_type] = 0;
1467 request_data = pci_alloc_consistent(
1468 ioc->pdev, request_data_sz, &request_data_dma);
1469 if (request_data == NULL) {
1470 pr_err(MPT3SAS_FMT "%s: failed allocating memory" \
1471 " for diag buffers, requested size(%d)\n",
1472 ioc->name, __func__, request_data_sz);
1473 mpt3sas_base_free_smid(ioc, smid);
1474 return -ENOMEM;
1475 }
1476 ioc->diag_buffer[buffer_type] = request_data;
1477 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1478 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1479 }
1480
1481 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1482 mpi_request->BufferType = diag_register->buffer_type;
1483 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1484 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1485 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1486 mpi_request->VF_ID = 0; /* TODO */
1487 mpi_request->VP_ID = 0;
1488
1489 dctlprintk(ioc, pr_info(MPT3SAS_FMT
1490 "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1491 ioc->name, __func__, request_data,
1492 (unsigned long long)request_data_dma,
1493 le32_to_cpu(mpi_request->BufferLength)));
1494
1495 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1496 mpi_request->ProductSpecific[i] =
1497 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1498
1499 init_completion(&ioc->ctl_cmds.done);
1500 mpt3sas_base_put_smid_default(ioc, smid);
1501 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1502 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1503
1504 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1505 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
1506 __func__);
1507 _debug_dump_mf(mpi_request,
1508 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1509 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
1510 issue_reset = 1;
1511 goto issue_host_reset;
1512 }
1513
1514 /* process the completed Reply Message Frame */
1515 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1516 pr_err(MPT3SAS_FMT "%s: no reply message\n",
1517 ioc->name, __func__);
1518 rc = -EFAULT;
1519 goto out;
1520 }
1521
1522 mpi_reply = ioc->ctl_cmds.reply;
1523 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1524
1525 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1526 ioc->diag_buffer_status[buffer_type] |=
1527 MPT3_DIAG_BUFFER_IS_REGISTERED;
1528 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
1529 ioc->name, __func__));
1530 } else {
1531 pr_info(MPT3SAS_FMT
1532 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1533 ioc->name, __func__,
1534 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1535 rc = -EFAULT;
1536 }
1537
1538 issue_host_reset:
1539 if (issue_reset)
1540 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1541 FORCE_BIG_HAMMER);
1542
1543 out:
1544
1545 if (rc && request_data)
1546 pci_free_consistent(ioc->pdev, request_data_sz,
1547 request_data, request_data_dma);
1548
1549 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1550 return rc;
1551}
1552
1553/**
1554 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1555 * @ioc: per adapter object
1556 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1557 *
1558 * This is called when command line option diag_buffer_enable is enabled
1559 * at driver load time.
1560 */
1561void
1562mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1563{
1564 struct mpt3_diag_register diag_register;
1565
1566 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1567
1568 if (bits_to_register & 1) {
1569 pr_info(MPT3SAS_FMT "registering trace buffer support\n",
1570 ioc->name);
1571 ioc->diag_trigger_master.MasterData =
1572 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1573 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1574 /* register for 2MB buffers */
1575 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1576 diag_register.unique_id = 0x7075900;
1577 _ctl_diag_register_2(ioc, &diag_register);
1578 }
1579
1580 if (bits_to_register & 2) {
1581 pr_info(MPT3SAS_FMT "registering snapshot buffer support\n",
1582 ioc->name);
1583 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1584 /* register for 2MB buffers */
1585 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1586 diag_register.unique_id = 0x7075901;
1587 _ctl_diag_register_2(ioc, &diag_register);
1588 }
1589
1590 if (bits_to_register & 4) {
1591 pr_info(MPT3SAS_FMT "registering extended buffer support\n",
1592 ioc->name);
1593 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1594 /* register for 2MB buffers */
1595 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1596 diag_register.unique_id = 0x7075901;
1597 _ctl_diag_register_2(ioc, &diag_register);
1598 }
1599}
1600
1601/**
1602 * _ctl_diag_register - application register with driver
1603 * @ioc: per adapter object
1604 * @arg - user space buffer containing ioctl content
1605 *
1606 * This will allow the driver to setup any required buffers that will be
1607 * needed by firmware to communicate with the driver.
1608 */
1609static long
1610_ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1611{
1612 struct mpt3_diag_register karg;
1613 long rc;
1614
1615 if (copy_from_user(&karg, arg, sizeof(karg))) {
1616 pr_err("failure at %s:%d/%s()!\n",
1617 __FILE__, __LINE__, __func__);
1618 return -EFAULT;
1619 }
1620
1621 rc = _ctl_diag_register_2(ioc, &karg);
1622 return rc;
1623}
1624
1625/**
1626 * _ctl_diag_unregister - application unregister with driver
1627 * @ioc: per adapter object
1628 * @arg - user space buffer containing ioctl content
1629 *
1630 * This will allow the driver to cleanup any memory allocated for diag
1631 * messages and to free up any resources.
1632 */
1633static long
1634_ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1635{
1636 struct mpt3_diag_unregister karg;
1637 void *request_data;
1638 dma_addr_t request_data_dma;
1639 u32 request_data_sz;
1640 u8 buffer_type;
1641
1642 if (copy_from_user(&karg, arg, sizeof(karg))) {
1643 pr_err("failure at %s:%d/%s()!\n",
1644 __FILE__, __LINE__, __func__);
1645 return -EFAULT;
1646 }
1647
1648 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1649 __func__));
1650
1651 buffer_type = karg.unique_id & 0x000000ff;
1652 if (!_ctl_diag_capability(ioc, buffer_type)) {
1653 pr_err(MPT3SAS_FMT
1654 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1655 ioc->name, __func__, buffer_type);
1656 return -EPERM;
1657 }
1658
1659 if ((ioc->diag_buffer_status[buffer_type] &
1660 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1661 pr_err(MPT3SAS_FMT
1662 "%s: buffer_type(0x%02x) is not registered\n",
1663 ioc->name, __func__, buffer_type);
1664 return -EINVAL;
1665 }
1666 if ((ioc->diag_buffer_status[buffer_type] &
1667 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
1668 pr_err(MPT3SAS_FMT
1669 "%s: buffer_type(0x%02x) has not been released\n",
1670 ioc->name, __func__, buffer_type);
1671 return -EINVAL;
1672 }
1673
1674 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1675 pr_err(MPT3SAS_FMT
1676 "%s: unique_id(0x%08x) is not registered\n",
1677 ioc->name, __func__, karg.unique_id);
1678 return -EINVAL;
1679 }
1680
1681 request_data = ioc->diag_buffer[buffer_type];
1682 if (!request_data) {
1683 pr_err(MPT3SAS_FMT
1684 "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1685 ioc->name, __func__, buffer_type);
1686 return -ENOMEM;
1687 }
1688
1689 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1690 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1691 pci_free_consistent(ioc->pdev, request_data_sz,
1692 request_data, request_data_dma);
1693 ioc->diag_buffer[buffer_type] = NULL;
1694 ioc->diag_buffer_status[buffer_type] = 0;
1695 return 0;
1696}
1697
1698/**
1699 * _ctl_diag_query - query relevant info associated with diag buffers
1700 * @ioc: per adapter object
1701 * @arg - user space buffer containing ioctl content
1702 *
1703 * The application will send only buffer_type and unique_id. Driver will
1704 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1705 * 0x00, the driver will return info specified by Buffer Type.
1706 */
1707static long
1708_ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1709{
1710 struct mpt3_diag_query karg;
1711 void *request_data;
1712 int i;
1713 u8 buffer_type;
1714
1715 if (copy_from_user(&karg, arg, sizeof(karg))) {
1716 pr_err("failure at %s:%d/%s()!\n",
1717 __FILE__, __LINE__, __func__);
1718 return -EFAULT;
1719 }
1720
1721 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1722 __func__));
1723
1724 karg.application_flags = 0;
1725 buffer_type = karg.buffer_type;
1726
1727 if (!_ctl_diag_capability(ioc, buffer_type)) {
1728 pr_err(MPT3SAS_FMT
1729 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1730 ioc->name, __func__, buffer_type);
1731 return -EPERM;
1732 }
1733
1734 if ((ioc->diag_buffer_status[buffer_type] &
1735 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1736 pr_err(MPT3SAS_FMT
1737 "%s: buffer_type(0x%02x) is not registered\n",
1738 ioc->name, __func__, buffer_type);
1739 return -EINVAL;
1740 }
1741
1742 if (karg.unique_id & 0xffffff00) {
1743 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1744 pr_err(MPT3SAS_FMT
1745 "%s: unique_id(0x%08x) is not registered\n",
1746 ioc->name, __func__, karg.unique_id);
1747 return -EINVAL;
1748 }
1749 }
1750
1751 request_data = ioc->diag_buffer[buffer_type];
1752 if (!request_data) {
1753 pr_err(MPT3SAS_FMT
1754 "%s: doesn't have buffer for buffer_type(0x%02x)\n",
1755 ioc->name, __func__, buffer_type);
1756 return -ENOMEM;
1757 }
1758
1759 if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
1760 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1761 MPT3_APP_FLAGS_BUFFER_VALID);
1762 else
1763 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1764 MPT3_APP_FLAGS_BUFFER_VALID |
1765 MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
1766
1767 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1768 karg.product_specific[i] =
1769 ioc->product_specific[buffer_type][i];
1770
1771 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1772 karg.driver_added_buffer_size = 0;
1773 karg.unique_id = ioc->unique_id[buffer_type];
1774 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1775
1776 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
1777 pr_err(MPT3SAS_FMT
1778 "%s: unable to write mpt3_diag_query data @ %p\n",
1779 ioc->name, __func__, arg);
1780 return -EFAULT;
1781 }
1782 return 0;
1783}
1784
1785/**
1786 * mpt3sas_send_diag_release - Diag Release Message
1787 * @ioc: per adapter object
1788 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1789 * @issue_reset - specifies whether host reset is required.
1790 *
1791 */
1792int
1793mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
1794 u8 *issue_reset)
1795{
1796 Mpi2DiagReleaseRequest_t *mpi_request;
1797 Mpi2DiagReleaseReply_t *mpi_reply;
1798 u16 smid;
1799 u16 ioc_status;
1800 u32 ioc_state;
1801 int rc;
1802 unsigned long timeleft;
1803
1804 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1805 __func__));
1806
1807 rc = 0;
1808 *issue_reset = 0;
1809
1810 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1811 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1812 if (ioc->diag_buffer_status[buffer_type] &
1813 MPT3_DIAG_BUFFER_IS_REGISTERED)
1814 ioc->diag_buffer_status[buffer_type] |=
1815 MPT3_DIAG_BUFFER_IS_RELEASED;
1816 dctlprintk(ioc, pr_info(MPT3SAS_FMT
1817 "%s: skipping due to FAULT state\n", ioc->name,
1818 __func__));
1819 rc = -EAGAIN;
1820 goto out;
1821 }
1822
1823 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1824 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1825 ioc->name, __func__);
1826 rc = -EAGAIN;
1827 goto out;
1828 }
1829
1830 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1831 if (!smid) {
1832 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1833 ioc->name, __func__);
1834 rc = -EAGAIN;
1835 goto out;
1836 }
1837
1838 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1839 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1840 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1841 ioc->ctl_cmds.smid = smid;
1842
1843 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1844 mpi_request->BufferType = buffer_type;
1845 mpi_request->VF_ID = 0; /* TODO */
1846 mpi_request->VP_ID = 0;
1847
1848 init_completion(&ioc->ctl_cmds.done);
1849 mpt3sas_base_put_smid_default(ioc, smid);
1850 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1851 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1852
1853 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1854 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
1855 __func__);
1856 _debug_dump_mf(mpi_request,
1857 sizeof(Mpi2DiagReleaseRequest_t)/4);
1858 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
1859 *issue_reset = 1;
1860 rc = -EFAULT;
1861 goto out;
1862 }
1863
1864 /* process the completed Reply Message Frame */
1865 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1866 pr_err(MPT3SAS_FMT "%s: no reply message\n",
1867 ioc->name, __func__);
1868 rc = -EFAULT;
1869 goto out;
1870 }
1871
1872 mpi_reply = ioc->ctl_cmds.reply;
1873 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1874
1875 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1876 ioc->diag_buffer_status[buffer_type] |=
1877 MPT3_DIAG_BUFFER_IS_RELEASED;
1878 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
1879 ioc->name, __func__));
1880 } else {
1881 pr_info(MPT3SAS_FMT
1882 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1883 ioc->name, __func__,
1884 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1885 rc = -EFAULT;
1886 }
1887
1888 out:
1889 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1890 return rc;
1891}
1892
1893/**
1894 * _ctl_diag_release - request to send Diag Release Message to firmware
1895 * @arg - user space buffer containing ioctl content
1896 *
1897 * This allows ownership of the specified buffer to returned to the driver,
1898 * allowing an application to read the buffer without fear that firmware is
1899 * overwritting information in the buffer.
1900 */
1901static long
1902_ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1903{
1904 struct mpt3_diag_release karg;
1905 void *request_data;
1906 int rc;
1907 u8 buffer_type;
1908 u8 issue_reset = 0;
1909
1910 if (copy_from_user(&karg, arg, sizeof(karg))) {
1911 pr_err("failure at %s:%d/%s()!\n",
1912 __FILE__, __LINE__, __func__);
1913 return -EFAULT;
1914 }
1915
1916 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1917 __func__));
1918
1919 buffer_type = karg.unique_id & 0x000000ff;
1920 if (!_ctl_diag_capability(ioc, buffer_type)) {
1921 pr_err(MPT3SAS_FMT
1922 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1923 ioc->name, __func__, buffer_type);
1924 return -EPERM;
1925 }
1926
1927 if ((ioc->diag_buffer_status[buffer_type] &
1928 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1929 pr_err(MPT3SAS_FMT
1930 "%s: buffer_type(0x%02x) is not registered\n",
1931 ioc->name, __func__, buffer_type);
1932 return -EINVAL;
1933 }
1934
1935 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1936 pr_err(MPT3SAS_FMT
1937 "%s: unique_id(0x%08x) is not registered\n",
1938 ioc->name, __func__, karg.unique_id);
1939 return -EINVAL;
1940 }
1941
1942 if (ioc->diag_buffer_status[buffer_type] &
1943 MPT3_DIAG_BUFFER_IS_RELEASED) {
1944 pr_err(MPT3SAS_FMT
1945 "%s: buffer_type(0x%02x) is already released\n",
1946 ioc->name, __func__,
1947 buffer_type);
1948 return 0;
1949 }
1950
1951 request_data = ioc->diag_buffer[buffer_type];
1952
1953 if (!request_data) {
1954 pr_err(MPT3SAS_FMT
1955 "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1956 ioc->name, __func__, buffer_type);
1957 return -ENOMEM;
1958 }
1959
1960 /* buffers were released by due to host reset */
1961 if ((ioc->diag_buffer_status[buffer_type] &
1962 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
1963 ioc->diag_buffer_status[buffer_type] |=
1964 MPT3_DIAG_BUFFER_IS_RELEASED;
1965 ioc->diag_buffer_status[buffer_type] &=
1966 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
1967 pr_err(MPT3SAS_FMT
1968 "%s: buffer_type(0x%02x) was released due to host reset\n",
1969 ioc->name, __func__, buffer_type);
1970 return 0;
1971 }
1972
1973 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
1974
1975 if (issue_reset)
1976 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1977 FORCE_BIG_HAMMER);
1978
1979 return rc;
1980}
1981
1982/**
1983 * _ctl_diag_read_buffer - request for copy of the diag buffer
1984 * @ioc: per adapter object
1985 * @arg - user space buffer containing ioctl content
1986 */
1987static long
1988_ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1989{
1990 struct mpt3_diag_read_buffer karg;
1991 struct mpt3_diag_read_buffer __user *uarg = arg;
1992 void *request_data, *diag_data;
1993 Mpi2DiagBufferPostRequest_t *mpi_request;
1994 Mpi2DiagBufferPostReply_t *mpi_reply;
1995 int rc, i;
1996 u8 buffer_type;
1997 unsigned long timeleft, request_size, copy_size;
1998 u16 smid;
1999 u16 ioc_status;
2000 u8 issue_reset = 0;
2001
2002 if (copy_from_user(&karg, arg, sizeof(karg))) {
2003 pr_err("failure at %s:%d/%s()!\n",
2004 __FILE__, __LINE__, __func__);
2005 return -EFAULT;
2006 }
2007
2008 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2009 __func__));
2010
2011 buffer_type = karg.unique_id & 0x000000ff;
2012 if (!_ctl_diag_capability(ioc, buffer_type)) {
2013 pr_err(MPT3SAS_FMT
2014 "%s: doesn't have capability for buffer_type(0x%02x)\n",
2015 ioc->name, __func__, buffer_type);
2016 return -EPERM;
2017 }
2018
2019 if (karg.unique_id != ioc->unique_id[buffer_type]) {
2020 pr_err(MPT3SAS_FMT
2021 "%s: unique_id(0x%08x) is not registered\n",
2022 ioc->name, __func__, karg.unique_id);
2023 return -EINVAL;
2024 }
2025
2026 request_data = ioc->diag_buffer[buffer_type];
2027 if (!request_data) {
2028 pr_err(MPT3SAS_FMT
2029 "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2030 ioc->name, __func__, buffer_type);
2031 return -ENOMEM;
2032 }
2033
2034 request_size = ioc->diag_buffer_sz[buffer_type];
2035
2036 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2037 pr_err(MPT3SAS_FMT "%s: either the starting_offset " \
2038 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
2039 __func__);
2040 return -EINVAL;
2041 }
2042
2043 if (karg.starting_offset > request_size)
2044 return -EINVAL;
2045
2046 diag_data = (void *)(request_data + karg.starting_offset);
2047 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2048 "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2049 ioc->name, __func__,
2050 diag_data, karg.starting_offset, karg.bytes_to_read));
2051
2052 /* Truncate data on requests that are too large */
2053 if ((diag_data + karg.bytes_to_read < diag_data) ||
2054 (diag_data + karg.bytes_to_read > request_data + request_size))
2055 copy_size = request_size - karg.starting_offset;
2056 else
2057 copy_size = karg.bytes_to_read;
2058
2059 if (copy_to_user((void __user *)uarg->diagnostic_data,
2060 diag_data, copy_size)) {
2061 pr_err(MPT3SAS_FMT
2062 "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2063 ioc->name, __func__, diag_data);
2064 return -EFAULT;
2065 }
2066
2067 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2068 return 0;
2069
2070 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2071 "%s: Reregister buffer_type(0x%02x)\n",
2072 ioc->name, __func__, buffer_type));
2073 if ((ioc->diag_buffer_status[buffer_type] &
2074 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2075 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2076 "%s: buffer_type(0x%02x) is still registered\n",
2077 ioc->name, __func__, buffer_type));
2078 return 0;
2079 }
2080 /* Get a free request frame and save the message context.
2081 */
2082
2083 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2084 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
2085 ioc->name, __func__);
2086 rc = -EAGAIN;
2087 goto out;
2088 }
2089
2090 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2091 if (!smid) {
2092 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2093 ioc->name, __func__);
2094 rc = -EAGAIN;
2095 goto out;
2096 }
2097
2098 rc = 0;
2099 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2100 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2101 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2102 ioc->ctl_cmds.smid = smid;
2103
2104 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2105 mpi_request->BufferType = buffer_type;
2106 mpi_request->BufferLength =
2107 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2108 mpi_request->BufferAddress =
2109 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2110 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2111 mpi_request->ProductSpecific[i] =
2112 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2113 mpi_request->VF_ID = 0; /* TODO */
2114 mpi_request->VP_ID = 0;
2115
2116 init_completion(&ioc->ctl_cmds.done);
2117 mpt3sas_base_put_smid_default(ioc, smid);
2118 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2119 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2120
2121 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2122 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
2123 __func__);
2124 _debug_dump_mf(mpi_request,
2125 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2126 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
2127 issue_reset = 1;
2128 goto issue_host_reset;
2129 }
2130
2131 /* process the completed Reply Message Frame */
2132 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2133 pr_err(MPT3SAS_FMT "%s: no reply message\n",
2134 ioc->name, __func__);
2135 rc = -EFAULT;
2136 goto out;
2137 }
2138
2139 mpi_reply = ioc->ctl_cmds.reply;
2140 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2141
2142 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2143 ioc->diag_buffer_status[buffer_type] |=
2144 MPT3_DIAG_BUFFER_IS_REGISTERED;
2145 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
2146 ioc->name, __func__));
2147 } else {
2148 pr_info(MPT3SAS_FMT
2149 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2150 ioc->name, __func__,
2151 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2152 rc = -EFAULT;
2153 }
2154
2155 issue_host_reset:
2156 if (issue_reset)
2157 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2158 FORCE_BIG_HAMMER);
2159
2160 out:
2161
2162 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2163 return rc;
2164}
2165
2166
2167
2168#ifdef CONFIG_COMPAT
2169/**
2170 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2171 * @ioc: per adapter object
2172 * @cmd - ioctl opcode
2173 * @arg - (struct mpt3_ioctl_command32)
2174 *
2175 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2176 */
2177static long
2178_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2179 void __user *arg)
2180{
2181 struct mpt3_ioctl_command32 karg32;
2182 struct mpt3_ioctl_command32 __user *uarg;
2183 struct mpt3_ioctl_command karg;
2184
2185 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2186 return -EINVAL;
2187
2188 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2189
2190 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2191 pr_err("failure at %s:%d/%s()!\n",
2192 __FILE__, __LINE__, __func__);
2193 return -EFAULT;
2194 }
2195
2196 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2197 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2198 karg.hdr.port_number = karg32.hdr.port_number;
2199 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2200 karg.timeout = karg32.timeout;
2201 karg.max_reply_bytes = karg32.max_reply_bytes;
2202 karg.data_in_size = karg32.data_in_size;
2203 karg.data_out_size = karg32.data_out_size;
2204 karg.max_sense_bytes = karg32.max_sense_bytes;
2205 karg.data_sge_offset = karg32.data_sge_offset;
2206 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2207 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2208 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2209 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2210 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2211}
2212#endif
2213
2214/**
2215 * _ctl_ioctl_main - main ioctl entry point
2216 * @file - (struct file)
2217 * @cmd - ioctl opcode
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302218 * @arg - user space data buffer
2219 * @compat - handles 32 bit applications in 64bit os
2220 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302221 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302222 */
2223static long
2224_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302225 u8 compat, u16 mpi_version)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302226{
2227 struct MPT3SAS_ADAPTER *ioc;
2228 struct mpt3_ioctl_header ioctl_header;
2229 enum block_state state;
2230 long ret = -EINVAL;
2231
2232 /* get IOCTL header */
2233 if (copy_from_user(&ioctl_header, (char __user *)arg,
2234 sizeof(struct mpt3_ioctl_header))) {
2235 pr_err("failure at %s:%d/%s()!\n",
2236 __FILE__, __LINE__, __func__);
2237 return -EFAULT;
2238 }
2239
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302240 if (_ctl_verify_adapter(ioctl_header.ioc_number,
2241 &ioc, mpi_version) == -1 || !ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302242 return -ENODEV;
2243
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302244 /* pci_access_mutex lock acquired by ioctl path */
2245 mutex_lock(&ioc->pci_access_mutex);
2246
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302247 if (ioc->shost_recovery || ioc->pci_error_recovery ||
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302248 ioc->is_driver_loading || ioc->remove_host) {
2249 ret = -EAGAIN;
2250 goto out_unlock_pciaccess;
2251 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302252
2253 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2254 if (state == NON_BLOCKING) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302255 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2256 ret = -EAGAIN;
2257 goto out_unlock_pciaccess;
2258 }
2259 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2260 ret = -ERESTARTSYS;
2261 goto out_unlock_pciaccess;
2262 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302263
2264
2265 switch (cmd) {
2266 case MPT3IOCINFO:
2267 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2268 ret = _ctl_getiocinfo(ioc, arg);
2269 break;
2270#ifdef CONFIG_COMPAT
2271 case MPT3COMMAND32:
2272#endif
2273 case MPT3COMMAND:
2274 {
2275 struct mpt3_ioctl_command __user *uarg;
2276 struct mpt3_ioctl_command karg;
2277
2278#ifdef CONFIG_COMPAT
2279 if (compat) {
2280 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2281 break;
2282 }
2283#endif
2284 if (copy_from_user(&karg, arg, sizeof(karg))) {
2285 pr_err("failure at %s:%d/%s()!\n",
2286 __FILE__, __LINE__, __func__);
2287 ret = -EFAULT;
2288 break;
2289 }
2290
2291 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2292 uarg = arg;
2293 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2294 }
2295 break;
2296 }
2297 case MPT3EVENTQUERY:
2298 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2299 ret = _ctl_eventquery(ioc, arg);
2300 break;
2301 case MPT3EVENTENABLE:
2302 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2303 ret = _ctl_eventenable(ioc, arg);
2304 break;
2305 case MPT3EVENTREPORT:
2306 ret = _ctl_eventreport(ioc, arg);
2307 break;
2308 case MPT3HARDRESET:
2309 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2310 ret = _ctl_do_reset(ioc, arg);
2311 break;
2312 case MPT3BTDHMAPPING:
2313 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2314 ret = _ctl_btdh_mapping(ioc, arg);
2315 break;
2316 case MPT3DIAGREGISTER:
2317 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2318 ret = _ctl_diag_register(ioc, arg);
2319 break;
2320 case MPT3DIAGUNREGISTER:
2321 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2322 ret = _ctl_diag_unregister(ioc, arg);
2323 break;
2324 case MPT3DIAGQUERY:
2325 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2326 ret = _ctl_diag_query(ioc, arg);
2327 break;
2328 case MPT3DIAGRELEASE:
2329 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2330 ret = _ctl_diag_release(ioc, arg);
2331 break;
2332 case MPT3DIAGREADBUFFER:
2333 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2334 ret = _ctl_diag_read_buffer(ioc, arg);
2335 break;
2336 default:
2337 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2338 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2339 break;
2340 }
2341
2342 mutex_unlock(&ioc->ctl_cmds.mutex);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302343out_unlock_pciaccess:
2344 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302345 return ret;
2346}
2347
2348/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302349 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302350 * @file - (struct file)
2351 * @cmd - ioctl opcode
2352 * @arg -
2353 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302354long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302355_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302356{
2357 long ret;
2358
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302359 /* pass MPI25_VERSION | MPI26_VERSION value,
2360 * to indicate that this ioctl cmd
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302361 * came from mpt3ctl ioctl device.
2362 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302363 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2364 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302365 return ret;
2366}
2367
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302368/**
2369 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
2370 * @file - (struct file)
2371 * @cmd - ioctl opcode
2372 * @arg -
2373 */
2374long
2375_ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2376{
2377 long ret;
2378
2379 /* pass MPI2_VERSION value, to indicate that this ioctl cmd
2380 * came from mpt2ctl ioctl device.
2381 */
2382 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2383 return ret;
2384}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302385#ifdef CONFIG_COMPAT
2386/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302387 *_ ctl_ioctl_compat - main ioctl entry point (compat)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302388 * @file -
2389 * @cmd -
2390 * @arg -
2391 *
2392 * This routine handles 32 bit applications in 64bit os.
2393 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302394long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302395_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302396{
2397 long ret;
2398
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302399 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2400 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302401 return ret;
2402}
2403
2404/**
2405 *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
2406 * @file -
2407 * @cmd -
2408 * @arg -
2409 *
2410 * This routine handles 32 bit applications in 64bit os.
2411 */
2412long
2413_ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2414{
2415 long ret;
2416
2417 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302418 return ret;
2419}
2420#endif
2421
2422/* scsi host attributes */
2423/**
2424 * _ctl_version_fw_show - firmware version
2425 * @cdev - pointer to embedded class device
2426 * @buf - the buffer returned
2427 *
2428 * A sysfs 'read-only' shost attribute.
2429 */
2430static ssize_t
2431_ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2432 char *buf)
2433{
2434 struct Scsi_Host *shost = class_to_shost(cdev);
2435 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2436
2437 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2438 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2439 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2440 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2441 ioc->facts.FWVersion.Word & 0x000000FF);
2442}
2443static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2444
2445/**
2446 * _ctl_version_bios_show - bios version
2447 * @cdev - pointer to embedded class device
2448 * @buf - the buffer returned
2449 *
2450 * A sysfs 'read-only' shost attribute.
2451 */
2452static ssize_t
2453_ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2454 char *buf)
2455{
2456 struct Scsi_Host *shost = class_to_shost(cdev);
2457 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2458
2459 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2460
2461 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2462 (version & 0xFF000000) >> 24,
2463 (version & 0x00FF0000) >> 16,
2464 (version & 0x0000FF00) >> 8,
2465 version & 0x000000FF);
2466}
2467static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2468
2469/**
2470 * _ctl_version_mpi_show - MPI (message passing interface) version
2471 * @cdev - pointer to embedded class device
2472 * @buf - the buffer returned
2473 *
2474 * A sysfs 'read-only' shost attribute.
2475 */
2476static ssize_t
2477_ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2478 char *buf)
2479{
2480 struct Scsi_Host *shost = class_to_shost(cdev);
2481 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2482
2483 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2484 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2485}
2486static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2487
2488/**
2489 * _ctl_version_product_show - product name
2490 * @cdev - pointer to embedded class device
2491 * @buf - the buffer returned
2492 *
2493 * A sysfs 'read-only' shost attribute.
2494 */
2495static ssize_t
2496_ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2497 char *buf)
2498{
2499 struct Scsi_Host *shost = class_to_shost(cdev);
2500 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2501
2502 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2503}
2504static DEVICE_ATTR(version_product, S_IRUGO, _ctl_version_product_show, NULL);
2505
2506/**
2507 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2508 * @cdev - pointer to embedded class device
2509 * @buf - the buffer returned
2510 *
2511 * A sysfs 'read-only' shost attribute.
2512 */
2513static ssize_t
2514_ctl_version_nvdata_persistent_show(struct device *cdev,
2515 struct device_attribute *attr, char *buf)
2516{
2517 struct Scsi_Host *shost = class_to_shost(cdev);
2518 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2519
2520 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2521 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2522}
2523static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2524 _ctl_version_nvdata_persistent_show, NULL);
2525
2526/**
2527 * _ctl_version_nvdata_default_show - nvdata default version
2528 * @cdev - pointer to embedded class device
2529 * @buf - the buffer returned
2530 *
2531 * A sysfs 'read-only' shost attribute.
2532 */
2533static ssize_t
2534_ctl_version_nvdata_default_show(struct device *cdev, struct device_attribute
2535 *attr, char *buf)
2536{
2537 struct Scsi_Host *shost = class_to_shost(cdev);
2538 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2539
2540 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2541 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2542}
2543static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2544 _ctl_version_nvdata_default_show, NULL);
2545
2546/**
2547 * _ctl_board_name_show - board name
2548 * @cdev - pointer to embedded class device
2549 * @buf - the buffer returned
2550 *
2551 * A sysfs 'read-only' shost attribute.
2552 */
2553static ssize_t
2554_ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2555 char *buf)
2556{
2557 struct Scsi_Host *shost = class_to_shost(cdev);
2558 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2559
2560 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2561}
2562static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2563
2564/**
2565 * _ctl_board_assembly_show - board assembly name
2566 * @cdev - pointer to embedded class device
2567 * @buf - the buffer returned
2568 *
2569 * A sysfs 'read-only' shost attribute.
2570 */
2571static ssize_t
2572_ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2573 char *buf)
2574{
2575 struct Scsi_Host *shost = class_to_shost(cdev);
2576 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2577
2578 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2579}
2580static DEVICE_ATTR(board_assembly, S_IRUGO, _ctl_board_assembly_show, NULL);
2581
2582/**
2583 * _ctl_board_tracer_show - board tracer number
2584 * @cdev - pointer to embedded class device
2585 * @buf - the buffer returned
2586 *
2587 * A sysfs 'read-only' shost attribute.
2588 */
2589static ssize_t
2590_ctl_board_tracer_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, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2597}
2598static DEVICE_ATTR(board_tracer, S_IRUGO, _ctl_board_tracer_show, NULL);
2599
2600/**
2601 * _ctl_io_delay_show - io missing delay
2602 * @cdev - pointer to embedded class device
2603 * @buf - the buffer returned
2604 *
2605 * This is for firmware implemention for deboucing device
2606 * removal events.
2607 *
2608 * A sysfs 'read-only' shost attribute.
2609 */
2610static ssize_t
2611_ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2612 char *buf)
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, "%02d\n", ioc->io_missing_delay);
2618}
2619static DEVICE_ATTR(io_delay, S_IRUGO, _ctl_io_delay_show, NULL);
2620
2621/**
2622 * _ctl_device_delay_show - device missing delay
2623 * @cdev - pointer to embedded class device
2624 * @buf - the buffer returned
2625 *
2626 * This is for firmware implemention for deboucing device
2627 * removal events.
2628 *
2629 * A sysfs 'read-only' shost attribute.
2630 */
2631static ssize_t
2632_ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2633 char *buf)
2634{
2635 struct Scsi_Host *shost = class_to_shost(cdev);
2636 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2637
2638 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2639}
2640static DEVICE_ATTR(device_delay, S_IRUGO, _ctl_device_delay_show, NULL);
2641
2642/**
2643 * _ctl_fw_queue_depth_show - global credits
2644 * @cdev - pointer to embedded class device
2645 * @buf - the buffer returned
2646 *
2647 * This is firmware queue depth limit
2648 *
2649 * A sysfs 'read-only' shost attribute.
2650 */
2651static ssize_t
2652_ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2653 char *buf)
2654{
2655 struct Scsi_Host *shost = class_to_shost(cdev);
2656 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2657
2658 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2659}
2660static DEVICE_ATTR(fw_queue_depth, S_IRUGO, _ctl_fw_queue_depth_show, NULL);
2661
2662/**
2663 * _ctl_sas_address_show - sas address
2664 * @cdev - pointer to embedded class device
2665 * @buf - the buffer returned
2666 *
2667 * This is the controller sas address
2668 *
2669 * A sysfs 'read-only' shost attribute.
2670 */
2671static ssize_t
2672_ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2673 char *buf)
2674
2675{
2676 struct Scsi_Host *shost = class_to_shost(cdev);
2677 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2678
2679 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2680 (unsigned long long)ioc->sas_hba.sas_address);
2681}
2682static DEVICE_ATTR(host_sas_address, S_IRUGO,
2683 _ctl_host_sas_address_show, NULL);
2684
2685/**
2686 * _ctl_logging_level_show - logging level
2687 * @cdev - pointer to embedded class device
2688 * @buf - the buffer returned
2689 *
2690 * A sysfs 'read/write' shost attribute.
2691 */
2692static ssize_t
2693_ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2694 char *buf)
2695{
2696 struct Scsi_Host *shost = class_to_shost(cdev);
2697 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2698
2699 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2700}
2701static ssize_t
2702_ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2703 const char *buf, size_t count)
2704{
2705 struct Scsi_Host *shost = class_to_shost(cdev);
2706 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2707 int val = 0;
2708
2709 if (sscanf(buf, "%x", &val) != 1)
2710 return -EINVAL;
2711
2712 ioc->logging_level = val;
2713 pr_info(MPT3SAS_FMT "logging_level=%08xh\n", ioc->name,
2714 ioc->logging_level);
2715 return strlen(buf);
2716}
2717static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR, _ctl_logging_level_show,
2718 _ctl_logging_level_store);
2719
2720/**
2721 * _ctl_fwfault_debug_show - show/store fwfault_debug
2722 * @cdev - pointer to embedded class device
2723 * @buf - the buffer returned
2724 *
2725 * mpt3sas_fwfault_debug is command line option
2726 * A sysfs 'read/write' shost attribute.
2727 */
2728static ssize_t
2729_ctl_fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
2730 char *buf)
2731{
2732 struct Scsi_Host *shost = class_to_shost(cdev);
2733 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2734
2735 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2736}
2737static ssize_t
2738_ctl_fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
2739 const char *buf, size_t count)
2740{
2741 struct Scsi_Host *shost = class_to_shost(cdev);
2742 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2743 int val = 0;
2744
2745 if (sscanf(buf, "%d", &val) != 1)
2746 return -EINVAL;
2747
2748 ioc->fwfault_debug = val;
2749 pr_info(MPT3SAS_FMT "fwfault_debug=%d\n", ioc->name,
2750 ioc->fwfault_debug);
2751 return strlen(buf);
2752}
2753static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2754 _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2755
2756/**
2757 * _ctl_ioc_reset_count_show - ioc reset count
2758 * @cdev - pointer to embedded class device
2759 * @buf - the buffer returned
2760 *
2761 * This is firmware queue depth limit
2762 *
2763 * A sysfs 'read-only' shost attribute.
2764 */
2765static ssize_t
2766_ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2767 char *buf)
2768{
2769 struct Scsi_Host *shost = class_to_shost(cdev);
2770 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2771
2772 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
2773}
2774static DEVICE_ATTR(ioc_reset_count, S_IRUGO, _ctl_ioc_reset_count_show, NULL);
2775
2776/**
2777 * _ctl_ioc_reply_queue_count_show - number of reply queues
2778 * @cdev - pointer to embedded class device
2779 * @buf - the buffer returned
2780 *
2781 * This is number of reply queues
2782 *
2783 * A sysfs 'read-only' shost attribute.
2784 */
2785static ssize_t
2786_ctl_ioc_reply_queue_count_show(struct device *cdev,
2787 struct device_attribute *attr, char *buf)
2788{
2789 u8 reply_queue_count;
2790 struct Scsi_Host *shost = class_to_shost(cdev);
2791 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2792
2793 if ((ioc->facts.IOCCapabilities &
2794 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2795 reply_queue_count = ioc->reply_queue_count;
2796 else
2797 reply_queue_count = 1;
2798
2799 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2800}
2801static DEVICE_ATTR(reply_queue_count, S_IRUGO, _ctl_ioc_reply_queue_count_show,
2802 NULL);
2803
Sreekanth Reddy42263092015-11-11 17:30:29 +05302804/**
2805 * _ctl_BRM_status_show - Backup Rail Monitor Status
2806 * @cdev - pointer to embedded class device
2807 * @buf - the buffer returned
2808 *
2809 * This is number of reply queues
2810 *
2811 * A sysfs 'read-only' shost attribute.
2812 */
2813static ssize_t
2814_ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
2815 char *buf)
2816{
2817 struct Scsi_Host *shost = class_to_shost(cdev);
2818 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2819 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2820 Mpi2ConfigReply_t mpi_reply;
2821 u16 backup_rail_monitor_status = 0;
2822 u16 ioc_status;
2823 int sz;
2824 ssize_t rc = 0;
2825
2826 if (!ioc->is_warpdrive) {
2827 pr_err(MPT3SAS_FMT "%s: BRM attribute is only for"
2828 " warpdrive\n", ioc->name, __func__);
2829 goto out;
2830 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302831 /* pci_access_mutex lock acquired by sysfs show path */
2832 mutex_lock(&ioc->pci_access_mutex);
2833 if (ioc->pci_error_recovery || ioc->remove_host) {
2834 mutex_unlock(&ioc->pci_access_mutex);
2835 return 0;
2836 }
Sreekanth Reddy42263092015-11-11 17:30:29 +05302837
2838 /* allocate upto GPIOVal 36 entries */
2839 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2840 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2841 if (!io_unit_pg3) {
2842 pr_err(MPT3SAS_FMT "%s: failed allocating memory "
2843 "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
2844 goto out;
2845 }
2846
2847 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2848 0) {
2849 pr_err(MPT3SAS_FMT
2850 "%s: failed reading iounit_pg3\n", ioc->name,
2851 __func__);
2852 goto out;
2853 }
2854
2855 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2856 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2857 pr_err(MPT3SAS_FMT "%s: iounit_pg3 failed with "
2858 "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
2859 goto out;
2860 }
2861
2862 if (io_unit_pg3->GPIOCount < 25) {
2863 pr_err(MPT3SAS_FMT "%s: iounit_pg3->GPIOCount less than "
2864 "25 entries, detected (%d) entries\n", ioc->name, __func__,
2865 io_unit_pg3->GPIOCount);
2866 goto out;
2867 }
2868
2869 /* BRM status is in bit zero of GPIOVal[24] */
2870 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2871 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2872
2873 out:
2874 kfree(io_unit_pg3);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302875 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302876 return rc;
2877}
2878static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302879
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302880struct DIAG_BUFFER_START {
2881 __le32 Size;
2882 __le32 DiagVersion;
2883 u8 BufferType;
2884 u8 Reserved[3];
2885 __le32 Reserved1;
2886 __le32 Reserved2;
2887 __le32 Reserved3;
2888};
2889
2890/**
2891 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2892 * @cdev - pointer to embedded class device
2893 * @buf - the buffer returned
2894 *
2895 * A sysfs 'read-only' shost attribute.
2896 */
2897static ssize_t
2898_ctl_host_trace_buffer_size_show(struct device *cdev,
2899 struct device_attribute *attr, char *buf)
2900{
2901 struct Scsi_Host *shost = class_to_shost(cdev);
2902 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2903 u32 size = 0;
2904 struct DIAG_BUFFER_START *request_data;
2905
2906 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2907 pr_err(MPT3SAS_FMT
2908 "%s: host_trace_buffer is not registered\n",
2909 ioc->name, __func__);
2910 return 0;
2911 }
2912
2913 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2914 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2915 pr_err(MPT3SAS_FMT
2916 "%s: host_trace_buffer is not registered\n",
2917 ioc->name, __func__);
2918 return 0;
2919 }
2920
2921 request_data = (struct DIAG_BUFFER_START *)
2922 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2923 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2924 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
2925 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
2926 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2927 size = le32_to_cpu(request_data->Size);
2928
2929 ioc->ring_buffer_sz = size;
2930 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2931}
2932static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2933 _ctl_host_trace_buffer_size_show, NULL);
2934
2935/**
2936 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2937 * @cdev - pointer to embedded class device
2938 * @buf - the buffer returned
2939 *
2940 * A sysfs 'read/write' shost attribute.
2941 *
2942 * You will only be able to read 4k bytes of ring buffer at a time.
2943 * In order to read beyond 4k bytes, you will have to write out the
2944 * offset to the same attribute, it will move the pointer.
2945 */
2946static ssize_t
2947_ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2948 char *buf)
2949{
2950 struct Scsi_Host *shost = class_to_shost(cdev);
2951 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2952 void *request_data;
2953 u32 size;
2954
2955 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2956 pr_err(MPT3SAS_FMT
2957 "%s: host_trace_buffer is not registered\n",
2958 ioc->name, __func__);
2959 return 0;
2960 }
2961
2962 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2963 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2964 pr_err(MPT3SAS_FMT
2965 "%s: host_trace_buffer is not registered\n",
2966 ioc->name, __func__);
2967 return 0;
2968 }
2969
2970 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2971 return 0;
2972
2973 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2974 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2975 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2976 memcpy(buf, request_data, size);
2977 return size;
2978}
2979
2980static ssize_t
2981_ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2982 const char *buf, size_t count)
2983{
2984 struct Scsi_Host *shost = class_to_shost(cdev);
2985 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2986 int val = 0;
2987
2988 if (sscanf(buf, "%d", &val) != 1)
2989 return -EINVAL;
2990
2991 ioc->ring_buffer_offset = val;
2992 return strlen(buf);
2993}
2994static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2995 _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2996
2997
2998/*****************************************/
2999
3000/**
3001 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
3002 * @cdev - pointer to embedded class device
3003 * @buf - the buffer returned
3004 *
3005 * A sysfs 'read/write' shost attribute.
3006 *
3007 * This is a mechnism to post/release host_trace_buffers
3008 */
3009static ssize_t
3010_ctl_host_trace_buffer_enable_show(struct device *cdev,
3011 struct device_attribute *attr, char *buf)
3012{
3013 struct Scsi_Host *shost = class_to_shost(cdev);
3014 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3015
3016 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3017 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3018 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3019 return snprintf(buf, PAGE_SIZE, "off\n");
3020 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3021 MPT3_DIAG_BUFFER_IS_RELEASED))
3022 return snprintf(buf, PAGE_SIZE, "release\n");
3023 else
3024 return snprintf(buf, PAGE_SIZE, "post\n");
3025}
3026
3027static ssize_t
3028_ctl_host_trace_buffer_enable_store(struct device *cdev,
3029 struct device_attribute *attr, const char *buf, size_t count)
3030{
3031 struct Scsi_Host *shost = class_to_shost(cdev);
3032 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3033 char str[10] = "";
3034 struct mpt3_diag_register diag_register;
3035 u8 issue_reset = 0;
3036
3037 /* don't allow post/release occurr while recovery is active */
3038 if (ioc->shost_recovery || ioc->remove_host ||
3039 ioc->pci_error_recovery || ioc->is_driver_loading)
3040 return -EBUSY;
3041
3042 if (sscanf(buf, "%9s", str) != 1)
3043 return -EINVAL;
3044
3045 if (!strcmp(str, "post")) {
3046 /* exit out if host buffers are already posted */
3047 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3048 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3049 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3050 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3051 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3052 goto out;
3053 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3054 pr_info(MPT3SAS_FMT "posting host trace buffers\n",
3055 ioc->name);
3056 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3057 diag_register.requested_buffer_size = (1024 * 1024);
3058 diag_register.unique_id = 0x7075900;
3059 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3060 _ctl_diag_register_2(ioc, &diag_register);
3061 } else if (!strcmp(str, "release")) {
3062 /* exit out if host buffers are already released */
3063 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3064 goto out;
3065 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3066 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3067 goto out;
3068 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3069 MPT3_DIAG_BUFFER_IS_RELEASED))
3070 goto out;
3071 pr_info(MPT3SAS_FMT "releasing host trace buffer\n",
3072 ioc->name);
3073 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3074 &issue_reset);
3075 }
3076
3077 out:
3078 return strlen(buf);
3079}
3080static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
3081 _ctl_host_trace_buffer_enable_show,
3082 _ctl_host_trace_buffer_enable_store);
3083
3084/*********** diagnostic trigger suppport *********************************/
3085
3086/**
3087 * _ctl_diag_trigger_master_show - show the diag_trigger_master attribute
3088 * @cdev - pointer to embedded class device
3089 * @buf - the buffer returned
3090 *
3091 * A sysfs 'read/write' shost attribute.
3092 */
3093static ssize_t
3094_ctl_diag_trigger_master_show(struct device *cdev,
3095 struct device_attribute *attr, char *buf)
3096
3097{
3098 struct Scsi_Host *shost = class_to_shost(cdev);
3099 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3100 unsigned long flags;
3101 ssize_t rc;
3102
3103 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3104 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3105 memcpy(buf, &ioc->diag_trigger_master, rc);
3106 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3107 return rc;
3108}
3109
3110/**
3111 * _ctl_diag_trigger_master_store - store the diag_trigger_master attribute
3112 * @cdev - pointer to embedded class device
3113 * @buf - the buffer returned
3114 *
3115 * A sysfs 'read/write' shost attribute.
3116 */
3117static ssize_t
3118_ctl_diag_trigger_master_store(struct device *cdev,
3119 struct device_attribute *attr, const char *buf, size_t count)
3120
3121{
3122 struct Scsi_Host *shost = class_to_shost(cdev);
3123 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3124 unsigned long flags;
3125 ssize_t rc;
3126
3127 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3128 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3129 memset(&ioc->diag_trigger_master, 0,
3130 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3131 memcpy(&ioc->diag_trigger_master, buf, rc);
3132 ioc->diag_trigger_master.MasterData |=
3133 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3134 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3135 return rc;
3136}
3137static DEVICE_ATTR(diag_trigger_master, S_IRUGO | S_IWUSR,
3138 _ctl_diag_trigger_master_show, _ctl_diag_trigger_master_store);
3139
3140
3141/**
3142 * _ctl_diag_trigger_event_show - show the diag_trigger_event attribute
3143 * @cdev - pointer to embedded class device
3144 * @buf - the buffer returned
3145 *
3146 * A sysfs 'read/write' shost attribute.
3147 */
3148static ssize_t
3149_ctl_diag_trigger_event_show(struct device *cdev,
3150 struct device_attribute *attr, char *buf)
3151{
3152 struct Scsi_Host *shost = class_to_shost(cdev);
3153 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3154 unsigned long flags;
3155 ssize_t rc;
3156
3157 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3158 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3159 memcpy(buf, &ioc->diag_trigger_event, rc);
3160 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3161 return rc;
3162}
3163
3164/**
3165 * _ctl_diag_trigger_event_store - store the diag_trigger_event attribute
3166 * @cdev - pointer to embedded class device
3167 * @buf - the buffer returned
3168 *
3169 * A sysfs 'read/write' shost attribute.
3170 */
3171static ssize_t
3172_ctl_diag_trigger_event_store(struct device *cdev,
3173 struct device_attribute *attr, const char *buf, size_t count)
3174
3175{
3176 struct Scsi_Host *shost = class_to_shost(cdev);
3177 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3178 unsigned long flags;
3179 ssize_t sz;
3180
3181 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3182 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3183 memset(&ioc->diag_trigger_event, 0,
3184 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3185 memcpy(&ioc->diag_trigger_event, buf, sz);
3186 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3187 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3188 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3189 return sz;
3190}
3191static DEVICE_ATTR(diag_trigger_event, S_IRUGO | S_IWUSR,
3192 _ctl_diag_trigger_event_show, _ctl_diag_trigger_event_store);
3193
3194
3195/**
3196 * _ctl_diag_trigger_scsi_show - show the diag_trigger_scsi attribute
3197 * @cdev - pointer to embedded class device
3198 * @buf - the buffer returned
3199 *
3200 * A sysfs 'read/write' shost attribute.
3201 */
3202static ssize_t
3203_ctl_diag_trigger_scsi_show(struct device *cdev,
3204 struct device_attribute *attr, char *buf)
3205{
3206 struct Scsi_Host *shost = class_to_shost(cdev);
3207 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3208 unsigned long flags;
3209 ssize_t rc;
3210
3211 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3212 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3213 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3214 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3215 return rc;
3216}
3217
3218/**
3219 * _ctl_diag_trigger_scsi_store - store the diag_trigger_scsi attribute
3220 * @cdev - pointer to embedded class device
3221 * @buf - the buffer returned
3222 *
3223 * A sysfs 'read/write' shost attribute.
3224 */
3225static ssize_t
3226_ctl_diag_trigger_scsi_store(struct device *cdev,
3227 struct device_attribute *attr, const char *buf, size_t count)
3228{
3229 struct Scsi_Host *shost = class_to_shost(cdev);
3230 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3231 unsigned long flags;
3232 ssize_t sz;
3233
3234 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3235 sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
3236 memset(&ioc->diag_trigger_scsi, 0,
3237 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3238 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3239 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3240 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3241 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3242 return sz;
3243}
3244static DEVICE_ATTR(diag_trigger_scsi, S_IRUGO | S_IWUSR,
3245 _ctl_diag_trigger_scsi_show, _ctl_diag_trigger_scsi_store);
3246
3247
3248/**
3249 * _ctl_diag_trigger_scsi_show - show the diag_trigger_mpi attribute
3250 * @cdev - pointer to embedded class device
3251 * @buf - the buffer returned
3252 *
3253 * A sysfs 'read/write' shost attribute.
3254 */
3255static ssize_t
3256_ctl_diag_trigger_mpi_show(struct device *cdev,
3257 struct device_attribute *attr, char *buf)
3258{
3259 struct Scsi_Host *shost = class_to_shost(cdev);
3260 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3261 unsigned long flags;
3262 ssize_t rc;
3263
3264 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3265 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3266 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3267 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3268 return rc;
3269}
3270
3271/**
3272 * _ctl_diag_trigger_mpi_store - store the diag_trigger_mpi attribute
3273 * @cdev - pointer to embedded class device
3274 * @buf - the buffer returned
3275 *
3276 * A sysfs 'read/write' shost attribute.
3277 */
3278static ssize_t
3279_ctl_diag_trigger_mpi_store(struct device *cdev,
3280 struct device_attribute *attr, const char *buf, size_t count)
3281{
3282 struct Scsi_Host *shost = class_to_shost(cdev);
3283 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3284 unsigned long flags;
3285 ssize_t sz;
3286
3287 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3288 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3289 memset(&ioc->diag_trigger_mpi, 0,
Dan Carpenter66331e82012-12-07 13:56:22 +03003290 sizeof(ioc->diag_trigger_mpi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303291 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3292 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3293 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3294 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3295 return sz;
3296}
3297
3298static DEVICE_ATTR(diag_trigger_mpi, S_IRUGO | S_IWUSR,
3299 _ctl_diag_trigger_mpi_show, _ctl_diag_trigger_mpi_store);
3300
3301/*********** diagnostic trigger suppport *** END ****************************/
3302
3303
3304
3305/*****************************************/
3306
3307struct device_attribute *mpt3sas_host_attrs[] = {
3308 &dev_attr_version_fw,
3309 &dev_attr_version_bios,
3310 &dev_attr_version_mpi,
3311 &dev_attr_version_product,
3312 &dev_attr_version_nvdata_persistent,
3313 &dev_attr_version_nvdata_default,
3314 &dev_attr_board_name,
3315 &dev_attr_board_assembly,
3316 &dev_attr_board_tracer,
3317 &dev_attr_io_delay,
3318 &dev_attr_device_delay,
3319 &dev_attr_logging_level,
3320 &dev_attr_fwfault_debug,
3321 &dev_attr_fw_queue_depth,
3322 &dev_attr_host_sas_address,
3323 &dev_attr_ioc_reset_count,
3324 &dev_attr_host_trace_buffer_size,
3325 &dev_attr_host_trace_buffer,
3326 &dev_attr_host_trace_buffer_enable,
3327 &dev_attr_reply_queue_count,
3328 &dev_attr_diag_trigger_master,
3329 &dev_attr_diag_trigger_event,
3330 &dev_attr_diag_trigger_scsi,
3331 &dev_attr_diag_trigger_mpi,
Sreekanth Reddy42263092015-11-11 17:30:29 +05303332 &dev_attr_BRM_status,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303333 NULL,
3334};
3335
3336/* device attributes */
3337
3338/**
3339 * _ctl_device_sas_address_show - sas address
3340 * @cdev - pointer to embedded class device
3341 * @buf - the buffer returned
3342 *
3343 * This is the sas address for the target
3344 *
3345 * A sysfs 'read-only' shost attribute.
3346 */
3347static ssize_t
3348_ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
3349 char *buf)
3350{
3351 struct scsi_device *sdev = to_scsi_device(dev);
3352 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3353
3354 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3355 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3356}
3357static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
3358
3359/**
3360 * _ctl_device_handle_show - device handle
3361 * @cdev - pointer to embedded class device
3362 * @buf - the buffer returned
3363 *
3364 * This is the firmware assigned device handle
3365 *
3366 * A sysfs 'read-only' shost attribute.
3367 */
3368static ssize_t
3369_ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
3370 char *buf)
3371{
3372 struct scsi_device *sdev = to_scsi_device(dev);
3373 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3374
3375 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3376 sas_device_priv_data->sas_target->handle);
3377}
3378static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
3379
3380struct device_attribute *mpt3sas_dev_attrs[] = {
3381 &dev_attr_sas_address,
3382 &dev_attr_sas_device_handle,
3383 NULL,
3384};
3385
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303386/* file operations table for mpt3ctl device */
3387static const struct file_operations ctl_fops = {
3388 .owner = THIS_MODULE,
3389 .unlocked_ioctl = _ctl_ioctl,
3390 .poll = _ctl_poll,
3391 .fasync = _ctl_fasync,
3392#ifdef CONFIG_COMPAT
3393 .compat_ioctl = _ctl_ioctl_compat,
3394#endif
3395};
3396
3397/* file operations table for mpt2ctl device */
3398static const struct file_operations ctl_gen2_fops = {
3399 .owner = THIS_MODULE,
3400 .unlocked_ioctl = _ctl_mpt2_ioctl,
3401 .poll = _ctl_poll,
3402 .fasync = _ctl_fasync,
3403#ifdef CONFIG_COMPAT
3404 .compat_ioctl = _ctl_mpt2_ioctl_compat,
3405#endif
3406};
3407
3408static struct miscdevice ctl_dev = {
3409 .minor = MPT3SAS_MINOR,
3410 .name = MPT3SAS_DEV_NAME,
3411 .fops = &ctl_fops,
3412};
3413
3414static struct miscdevice gen2_ctl_dev = {
3415 .minor = MPT2SAS_MINOR,
3416 .name = MPT2SAS_DEV_NAME,
3417 .fops = &ctl_gen2_fops,
3418};
3419
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303420/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303421 * mpt3sas_ctl_init - main entry point for ctl.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303422 *
3423 */
3424void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303425mpt3sas_ctl_init(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303426{
3427 async_queue = NULL;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303428
3429 /* Don't register mpt3ctl ioctl device if
3430 * hbas_to_enumarate is one.
3431 */
3432 if (hbas_to_enumerate != 1)
3433 if (misc_register(&ctl_dev) < 0)
3434 pr_err("%s can't register misc device [minor=%d]\n",
3435 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3436
3437 /* Don't register mpt3ctl ioctl device if
3438 * hbas_to_enumarate is two.
3439 */
3440 if (hbas_to_enumerate != 2)
3441 if (misc_register(&gen2_ctl_dev) < 0)
3442 pr_err("%s can't register misc device [minor=%d]\n",
3443 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3444
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303445 init_waitqueue_head(&ctl_poll_wait);
3446}
3447
3448/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303449 * mpt3sas_ctl_exit - exit point for ctl
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303450 *
3451 */
3452void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303453mpt3sas_ctl_exit(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303454{
3455 struct MPT3SAS_ADAPTER *ioc;
3456 int i;
3457
3458 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3459
3460 /* free memory associated to diag buffers */
3461 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3462 if (!ioc->diag_buffer[i])
3463 continue;
3464 if (!(ioc->diag_buffer_status[i] &
3465 MPT3_DIAG_BUFFER_IS_REGISTERED))
3466 continue;
3467 if ((ioc->diag_buffer_status[i] &
3468 MPT3_DIAG_BUFFER_IS_RELEASED))
3469 continue;
3470 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3471 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3472 ioc->diag_buffer[i] = NULL;
3473 ioc->diag_buffer_status[i] = 0;
3474 }
3475
3476 kfree(ioc->event_log);
3477 }
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303478 if (hbas_to_enumerate != 1)
3479 misc_deregister(&ctl_dev);
3480 if (hbas_to_enumerate != 2)
3481 misc_deregister(&gen2_ctl_dev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303482}