blob: c924012d62d1a02f0ae845fad4713befc5bed752 [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
Calvin Owens98c56ad2016-07-28 21:38:21 -07001008 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301009 }
1010
1011 out:
1012
1013 /* free memory associated with sg buffers */
1014 if (data_in)
1015 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1016 data_in_dma);
1017
1018 if (data_out)
1019 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1020 data_out_dma);
1021
1022 kfree(mpi_request);
1023 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1024 return ret;
1025}
1026
1027/**
1028 * _ctl_getiocinfo - main handler for MPT3IOCINFO opcode
1029 * @ioc: per adapter object
1030 * @arg - user space buffer containing ioctl content
1031 */
1032static long
1033_ctl_getiocinfo(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1034{
1035 struct mpt3_ioctl_iocinfo karg;
1036
1037 if (copy_from_user(&karg, arg, sizeof(karg))) {
1038 pr_err("failure at %s:%d/%s()!\n",
1039 __FILE__, __LINE__, __func__);
1040 return -EFAULT;
1041 }
1042
1043 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1044 __func__));
1045
1046 memset(&karg, 0 , sizeof(karg));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301047 if (ioc->pfacts)
1048 karg.port_number = ioc->pfacts[0].PortNumber;
1049 karg.hw_rev = ioc->pdev->revision;
1050 karg.pci_id = ioc->pdev->device;
1051 karg.subsystem_device = ioc->pdev->subsystem_device;
1052 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1053 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1054 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1055 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1056 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1057 karg.firmware_version = ioc->facts.FWVersion.Word;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301058 strcpy(karg.driver_version, ioc->driver_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301059 strcat(karg.driver_version, "-");
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301060 switch (ioc->hba_mpi_version_belonged) {
1061 case MPI2_VERSION:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301062 if (ioc->is_warpdrive)
1063 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1064 else
1065 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301066 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
1067 break;
1068 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301069 case MPI26_VERSION:
Sreekanth Reddyd357e842015-11-11 17:30:22 +05301070 karg.adapter_type = MPT3_IOCTL_INTERFACE_SAS3;
1071 strcat(karg.driver_version, MPT3SAS_DRIVER_VERSION);
1072 break;
1073 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301074 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1075
1076 if (copy_to_user(arg, &karg, sizeof(karg))) {
1077 pr_err("failure at %s:%d/%s()!\n",
1078 __FILE__, __LINE__, __func__);
1079 return -EFAULT;
1080 }
1081 return 0;
1082}
1083
1084/**
1085 * _ctl_eventquery - main handler for MPT3EVENTQUERY opcode
1086 * @ioc: per adapter object
1087 * @arg - user space buffer containing ioctl content
1088 */
1089static long
1090_ctl_eventquery(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1091{
1092 struct mpt3_ioctl_eventquery karg;
1093
1094 if (copy_from_user(&karg, arg, sizeof(karg))) {
1095 pr_err("failure at %s:%d/%s()!\n",
1096 __FILE__, __LINE__, __func__);
1097 return -EFAULT;
1098 }
1099
1100 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1101 __func__));
1102
1103 karg.event_entries = MPT3SAS_CTL_EVENT_LOG_SIZE;
1104 memcpy(karg.event_types, ioc->event_type,
1105 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1106
1107 if (copy_to_user(arg, &karg, sizeof(karg))) {
1108 pr_err("failure at %s:%d/%s()!\n",
1109 __FILE__, __LINE__, __func__);
1110 return -EFAULT;
1111 }
1112 return 0;
1113}
1114
1115/**
1116 * _ctl_eventenable - main handler for MPT3EVENTENABLE opcode
1117 * @ioc: per adapter object
1118 * @arg - user space buffer containing ioctl content
1119 */
1120static long
1121_ctl_eventenable(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1122{
1123 struct mpt3_ioctl_eventenable karg;
1124
1125 if (copy_from_user(&karg, arg, sizeof(karg))) {
1126 pr_err("failure at %s:%d/%s()!\n",
1127 __FILE__, __LINE__, __func__);
1128 return -EFAULT;
1129 }
1130
1131 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1132 __func__));
1133
1134 memcpy(ioc->event_type, karg.event_types,
1135 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1136 mpt3sas_base_validate_event_type(ioc, ioc->event_type);
1137
1138 if (ioc->event_log)
1139 return 0;
1140 /* initialize event_log */
1141 ioc->event_context = 0;
1142 ioc->aen_event_read_flag = 0;
1143 ioc->event_log = kcalloc(MPT3SAS_CTL_EVENT_LOG_SIZE,
1144 sizeof(struct MPT3_IOCTL_EVENTS), GFP_KERNEL);
1145 if (!ioc->event_log) {
1146 pr_err("failure at %s:%d/%s()!\n",
1147 __FILE__, __LINE__, __func__);
1148 return -ENOMEM;
1149 }
1150 return 0;
1151}
1152
1153/**
1154 * _ctl_eventreport - main handler for MPT3EVENTREPORT opcode
1155 * @ioc: per adapter object
1156 * @arg - user space buffer containing ioctl content
1157 */
1158static long
1159_ctl_eventreport(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1160{
1161 struct mpt3_ioctl_eventreport karg;
1162 u32 number_bytes, max_events, max;
1163 struct mpt3_ioctl_eventreport __user *uarg = arg;
1164
1165 if (copy_from_user(&karg, arg, sizeof(karg))) {
1166 pr_err("failure at %s:%d/%s()!\n",
1167 __FILE__, __LINE__, __func__);
1168 return -EFAULT;
1169 }
1170
1171 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1172 __func__));
1173
1174 number_bytes = karg.hdr.max_data_size -
1175 sizeof(struct mpt3_ioctl_header);
1176 max_events = number_bytes/sizeof(struct MPT3_IOCTL_EVENTS);
1177 max = min_t(u32, MPT3SAS_CTL_EVENT_LOG_SIZE, max_events);
1178
1179 /* If fewer than 1 event is requested, there must have
1180 * been some type of error.
1181 */
1182 if (!max || !ioc->event_log)
1183 return -ENODATA;
1184
1185 number_bytes = max * sizeof(struct MPT3_IOCTL_EVENTS);
1186 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1187 pr_err("failure at %s:%d/%s()!\n",
1188 __FILE__, __LINE__, __func__);
1189 return -EFAULT;
1190 }
1191
1192 /* reset flag so SIGIO can restart */
1193 ioc->aen_event_read_flag = 0;
1194 return 0;
1195}
1196
1197/**
1198 * _ctl_do_reset - main handler for MPT3HARDRESET opcode
1199 * @ioc: per adapter object
1200 * @arg - user space buffer containing ioctl content
1201 */
1202static long
1203_ctl_do_reset(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1204{
1205 struct mpt3_ioctl_diag_reset karg;
1206 int retval;
1207
1208 if (copy_from_user(&karg, arg, sizeof(karg))) {
1209 pr_err("failure at %s:%d/%s()!\n",
1210 __FILE__, __LINE__, __func__);
1211 return -EFAULT;
1212 }
1213
1214 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1215 ioc->is_driver_loading)
1216 return -EAGAIN;
1217
1218 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
1219 __func__));
1220
Calvin Owens98c56ad2016-07-28 21:38:21 -07001221 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301222 pr_info(MPT3SAS_FMT "host reset: %s\n",
1223 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1224 return 0;
1225}
1226
1227/**
1228 * _ctl_btdh_search_sas_device - searching for sas device
1229 * @ioc: per adapter object
1230 * @btdh: btdh ioctl payload
1231 */
1232static int
1233_ctl_btdh_search_sas_device(struct MPT3SAS_ADAPTER *ioc,
1234 struct mpt3_ioctl_btdh_mapping *btdh)
1235{
1236 struct _sas_device *sas_device;
1237 unsigned long flags;
1238 int rc = 0;
1239
1240 if (list_empty(&ioc->sas_device_list))
1241 return rc;
1242
1243 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1244 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1245 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1246 btdh->handle == sas_device->handle) {
1247 btdh->bus = sas_device->channel;
1248 btdh->id = sas_device->id;
1249 rc = 1;
1250 goto out;
1251 } else if (btdh->bus == sas_device->channel && btdh->id ==
1252 sas_device->id && btdh->handle == 0xFFFF) {
1253 btdh->handle = sas_device->handle;
1254 rc = 1;
1255 goto out;
1256 }
1257 }
1258 out:
1259 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1260 return rc;
1261}
1262
1263/**
1264 * _ctl_btdh_search_raid_device - searching for raid device
1265 * @ioc: per adapter object
1266 * @btdh: btdh ioctl payload
1267 */
1268static int
1269_ctl_btdh_search_raid_device(struct MPT3SAS_ADAPTER *ioc,
1270 struct mpt3_ioctl_btdh_mapping *btdh)
1271{
1272 struct _raid_device *raid_device;
1273 unsigned long flags;
1274 int rc = 0;
1275
1276 if (list_empty(&ioc->raid_device_list))
1277 return rc;
1278
1279 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1280 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1281 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1282 btdh->handle == raid_device->handle) {
1283 btdh->bus = raid_device->channel;
1284 btdh->id = raid_device->id;
1285 rc = 1;
1286 goto out;
1287 } else if (btdh->bus == raid_device->channel && btdh->id ==
1288 raid_device->id && btdh->handle == 0xFFFF) {
1289 btdh->handle = raid_device->handle;
1290 rc = 1;
1291 goto out;
1292 }
1293 }
1294 out:
1295 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1296 return rc;
1297}
1298
1299/**
1300 * _ctl_btdh_mapping - main handler for MPT3BTDHMAPPING opcode
1301 * @ioc: per adapter object
1302 * @arg - user space buffer containing ioctl content
1303 */
1304static long
1305_ctl_btdh_mapping(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1306{
1307 struct mpt3_ioctl_btdh_mapping karg;
1308 int rc;
1309
1310 if (copy_from_user(&karg, arg, sizeof(karg))) {
1311 pr_err("failure at %s:%d/%s()!\n",
1312 __FILE__, __LINE__, __func__);
1313 return -EFAULT;
1314 }
1315
1316 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1317 __func__));
1318
1319 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1320 if (!rc)
1321 _ctl_btdh_search_raid_device(ioc, &karg);
1322
1323 if (copy_to_user(arg, &karg, sizeof(karg))) {
1324 pr_err("failure at %s:%d/%s()!\n",
1325 __FILE__, __LINE__, __func__);
1326 return -EFAULT;
1327 }
1328 return 0;
1329}
1330
1331/**
1332 * _ctl_diag_capability - return diag buffer capability
1333 * @ioc: per adapter object
1334 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
1335 *
1336 * returns 1 when diag buffer support is enabled in firmware
1337 */
1338static u8
1339_ctl_diag_capability(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type)
1340{
1341 u8 rc = 0;
1342
1343 switch (buffer_type) {
1344 case MPI2_DIAG_BUF_TYPE_TRACE:
1345 if (ioc->facts.IOCCapabilities &
1346 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1347 rc = 1;
1348 break;
1349 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1350 if (ioc->facts.IOCCapabilities &
1351 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1352 rc = 1;
1353 break;
1354 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1355 if (ioc->facts.IOCCapabilities &
1356 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1357 rc = 1;
1358 }
1359
1360 return rc;
1361}
1362
1363
1364/**
1365 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1366 * @ioc: per adapter object
1367 * @diag_register: the diag_register struct passed in from user space
1368 *
1369 */
1370static long
1371_ctl_diag_register_2(struct MPT3SAS_ADAPTER *ioc,
1372 struct mpt3_diag_register *diag_register)
1373{
1374 int rc, i;
1375 void *request_data = NULL;
1376 dma_addr_t request_data_dma;
1377 u32 request_data_sz = 0;
1378 Mpi2DiagBufferPostRequest_t *mpi_request;
1379 Mpi2DiagBufferPostReply_t *mpi_reply;
1380 u8 buffer_type;
1381 unsigned long timeleft;
1382 u16 smid;
1383 u16 ioc_status;
1384 u32 ioc_state;
1385 u8 issue_reset = 0;
1386
1387 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1388 __func__));
1389
1390 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1391 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1392 pr_err(MPT3SAS_FMT
1393 "%s: failed due to ioc not operational\n",
1394 ioc->name, __func__);
1395 rc = -EAGAIN;
1396 goto out;
1397 }
1398
1399 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1400 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1401 ioc->name, __func__);
1402 rc = -EAGAIN;
1403 goto out;
1404 }
1405
1406 buffer_type = diag_register->buffer_type;
1407 if (!_ctl_diag_capability(ioc, buffer_type)) {
1408 pr_err(MPT3SAS_FMT
1409 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1410 ioc->name, __func__, buffer_type);
1411 return -EPERM;
1412 }
1413
1414 if (ioc->diag_buffer_status[buffer_type] &
1415 MPT3_DIAG_BUFFER_IS_REGISTERED) {
1416 pr_err(MPT3SAS_FMT
1417 "%s: already has a registered buffer for buffer_type(0x%02x)\n",
1418 ioc->name, __func__,
1419 buffer_type);
1420 return -EINVAL;
1421 }
1422
1423 if (diag_register->requested_buffer_size % 4) {
1424 pr_err(MPT3SAS_FMT
1425 "%s: the requested_buffer_size is not 4 byte aligned\n",
1426 ioc->name, __func__);
1427 return -EINVAL;
1428 }
1429
1430 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1431 if (!smid) {
1432 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1433 ioc->name, __func__);
1434 rc = -EAGAIN;
1435 goto out;
1436 }
1437
1438 rc = 0;
1439 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1440 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1441 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1442 ioc->ctl_cmds.smid = smid;
1443
1444 request_data = ioc->diag_buffer[buffer_type];
1445 request_data_sz = diag_register->requested_buffer_size;
1446 ioc->unique_id[buffer_type] = diag_register->unique_id;
1447 ioc->diag_buffer_status[buffer_type] = 0;
1448 memcpy(ioc->product_specific[buffer_type],
1449 diag_register->product_specific, MPT3_PRODUCT_SPECIFIC_DWORDS);
1450 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
1451
1452 if (request_data) {
1453 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1454 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1455 pci_free_consistent(ioc->pdev,
1456 ioc->diag_buffer_sz[buffer_type],
1457 request_data, request_data_dma);
1458 request_data = NULL;
1459 }
1460 }
1461
1462 if (request_data == NULL) {
1463 ioc->diag_buffer_sz[buffer_type] = 0;
1464 ioc->diag_buffer_dma[buffer_type] = 0;
1465 request_data = pci_alloc_consistent(
1466 ioc->pdev, request_data_sz, &request_data_dma);
1467 if (request_data == NULL) {
1468 pr_err(MPT3SAS_FMT "%s: failed allocating memory" \
1469 " for diag buffers, requested size(%d)\n",
1470 ioc->name, __func__, request_data_sz);
1471 mpt3sas_base_free_smid(ioc, smid);
1472 return -ENOMEM;
1473 }
1474 ioc->diag_buffer[buffer_type] = request_data;
1475 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1476 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1477 }
1478
1479 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
1480 mpi_request->BufferType = diag_register->buffer_type;
1481 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
1482 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1483 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
1484 mpi_request->VF_ID = 0; /* TODO */
1485 mpi_request->VP_ID = 0;
1486
1487 dctlprintk(ioc, pr_info(MPT3SAS_FMT
1488 "%s: diag_buffer(0x%p), dma(0x%llx), sz(%d)\n",
1489 ioc->name, __func__, request_data,
1490 (unsigned long long)request_data_dma,
1491 le32_to_cpu(mpi_request->BufferLength)));
1492
1493 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1494 mpi_request->ProductSpecific[i] =
1495 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1496
1497 init_completion(&ioc->ctl_cmds.done);
1498 mpt3sas_base_put_smid_default(ioc, smid);
1499 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1500 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1501
1502 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1503 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
1504 __func__);
1505 _debug_dump_mf(mpi_request,
1506 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1507 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
1508 issue_reset = 1;
1509 goto issue_host_reset;
1510 }
1511
1512 /* process the completed Reply Message Frame */
1513 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1514 pr_err(MPT3SAS_FMT "%s: no reply message\n",
1515 ioc->name, __func__);
1516 rc = -EFAULT;
1517 goto out;
1518 }
1519
1520 mpi_reply = ioc->ctl_cmds.reply;
1521 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1522
1523 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1524 ioc->diag_buffer_status[buffer_type] |=
1525 MPT3_DIAG_BUFFER_IS_REGISTERED;
1526 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
1527 ioc->name, __func__));
1528 } else {
1529 pr_info(MPT3SAS_FMT
1530 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1531 ioc->name, __func__,
1532 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1533 rc = -EFAULT;
1534 }
1535
1536 issue_host_reset:
1537 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07001538 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301539
1540 out:
1541
1542 if (rc && request_data)
1543 pci_free_consistent(ioc->pdev, request_data_sz,
1544 request_data, request_data_dma);
1545
1546 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1547 return rc;
1548}
1549
1550/**
1551 * mpt3sas_enable_diag_buffer - enabling diag_buffers support driver load time
1552 * @ioc: per adapter object
1553 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1554 *
1555 * This is called when command line option diag_buffer_enable is enabled
1556 * at driver load time.
1557 */
1558void
1559mpt3sas_enable_diag_buffer(struct MPT3SAS_ADAPTER *ioc, u8 bits_to_register)
1560{
1561 struct mpt3_diag_register diag_register;
1562
1563 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
1564
1565 if (bits_to_register & 1) {
1566 pr_info(MPT3SAS_FMT "registering trace buffer support\n",
1567 ioc->name);
1568 ioc->diag_trigger_master.MasterData =
1569 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
1570 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1571 /* register for 2MB buffers */
1572 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1573 diag_register.unique_id = 0x7075900;
1574 _ctl_diag_register_2(ioc, &diag_register);
1575 }
1576
1577 if (bits_to_register & 2) {
1578 pr_info(MPT3SAS_FMT "registering snapshot buffer support\n",
1579 ioc->name);
1580 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1581 /* register for 2MB buffers */
1582 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1583 diag_register.unique_id = 0x7075901;
1584 _ctl_diag_register_2(ioc, &diag_register);
1585 }
1586
1587 if (bits_to_register & 4) {
1588 pr_info(MPT3SAS_FMT "registering extended buffer support\n",
1589 ioc->name);
1590 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1591 /* register for 2MB buffers */
1592 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1593 diag_register.unique_id = 0x7075901;
1594 _ctl_diag_register_2(ioc, &diag_register);
1595 }
1596}
1597
1598/**
1599 * _ctl_diag_register - application register with driver
1600 * @ioc: per adapter object
1601 * @arg - user space buffer containing ioctl content
1602 *
1603 * This will allow the driver to setup any required buffers that will be
1604 * needed by firmware to communicate with the driver.
1605 */
1606static long
1607_ctl_diag_register(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1608{
1609 struct mpt3_diag_register karg;
1610 long rc;
1611
1612 if (copy_from_user(&karg, arg, sizeof(karg))) {
1613 pr_err("failure at %s:%d/%s()!\n",
1614 __FILE__, __LINE__, __func__);
1615 return -EFAULT;
1616 }
1617
1618 rc = _ctl_diag_register_2(ioc, &karg);
1619 return rc;
1620}
1621
1622/**
1623 * _ctl_diag_unregister - application unregister with driver
1624 * @ioc: per adapter object
1625 * @arg - user space buffer containing ioctl content
1626 *
1627 * This will allow the driver to cleanup any memory allocated for diag
1628 * messages and to free up any resources.
1629 */
1630static long
1631_ctl_diag_unregister(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1632{
1633 struct mpt3_diag_unregister karg;
1634 void *request_data;
1635 dma_addr_t request_data_dma;
1636 u32 request_data_sz;
1637 u8 buffer_type;
1638
1639 if (copy_from_user(&karg, arg, sizeof(karg))) {
1640 pr_err("failure at %s:%d/%s()!\n",
1641 __FILE__, __LINE__, __func__);
1642 return -EFAULT;
1643 }
1644
1645 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1646 __func__));
1647
1648 buffer_type = karg.unique_id & 0x000000ff;
1649 if (!_ctl_diag_capability(ioc, buffer_type)) {
1650 pr_err(MPT3SAS_FMT
1651 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1652 ioc->name, __func__, buffer_type);
1653 return -EPERM;
1654 }
1655
1656 if ((ioc->diag_buffer_status[buffer_type] &
1657 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1658 pr_err(MPT3SAS_FMT
1659 "%s: buffer_type(0x%02x) is not registered\n",
1660 ioc->name, __func__, buffer_type);
1661 return -EINVAL;
1662 }
1663 if ((ioc->diag_buffer_status[buffer_type] &
1664 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
1665 pr_err(MPT3SAS_FMT
1666 "%s: buffer_type(0x%02x) has not been released\n",
1667 ioc->name, __func__, buffer_type);
1668 return -EINVAL;
1669 }
1670
1671 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1672 pr_err(MPT3SAS_FMT
1673 "%s: unique_id(0x%08x) is not registered\n",
1674 ioc->name, __func__, karg.unique_id);
1675 return -EINVAL;
1676 }
1677
1678 request_data = ioc->diag_buffer[buffer_type];
1679 if (!request_data) {
1680 pr_err(MPT3SAS_FMT
1681 "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1682 ioc->name, __func__, buffer_type);
1683 return -ENOMEM;
1684 }
1685
1686 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1687 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1688 pci_free_consistent(ioc->pdev, request_data_sz,
1689 request_data, request_data_dma);
1690 ioc->diag_buffer[buffer_type] = NULL;
1691 ioc->diag_buffer_status[buffer_type] = 0;
1692 return 0;
1693}
1694
1695/**
1696 * _ctl_diag_query - query relevant info associated with diag buffers
1697 * @ioc: per adapter object
1698 * @arg - user space buffer containing ioctl content
1699 *
1700 * The application will send only buffer_type and unique_id. Driver will
1701 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1702 * 0x00, the driver will return info specified by Buffer Type.
1703 */
1704static long
1705_ctl_diag_query(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1706{
1707 struct mpt3_diag_query karg;
1708 void *request_data;
1709 int i;
1710 u8 buffer_type;
1711
1712 if (copy_from_user(&karg, arg, sizeof(karg))) {
1713 pr_err("failure at %s:%d/%s()!\n",
1714 __FILE__, __LINE__, __func__);
1715 return -EFAULT;
1716 }
1717
1718 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1719 __func__));
1720
1721 karg.application_flags = 0;
1722 buffer_type = karg.buffer_type;
1723
1724 if (!_ctl_diag_capability(ioc, buffer_type)) {
1725 pr_err(MPT3SAS_FMT
1726 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1727 ioc->name, __func__, buffer_type);
1728 return -EPERM;
1729 }
1730
1731 if ((ioc->diag_buffer_status[buffer_type] &
1732 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1733 pr_err(MPT3SAS_FMT
1734 "%s: buffer_type(0x%02x) is not registered\n",
1735 ioc->name, __func__, buffer_type);
1736 return -EINVAL;
1737 }
1738
1739 if (karg.unique_id & 0xffffff00) {
1740 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1741 pr_err(MPT3SAS_FMT
1742 "%s: unique_id(0x%08x) is not registered\n",
1743 ioc->name, __func__, karg.unique_id);
1744 return -EINVAL;
1745 }
1746 }
1747
1748 request_data = ioc->diag_buffer[buffer_type];
1749 if (!request_data) {
1750 pr_err(MPT3SAS_FMT
1751 "%s: doesn't have buffer for buffer_type(0x%02x)\n",
1752 ioc->name, __func__, buffer_type);
1753 return -ENOMEM;
1754 }
1755
1756 if (ioc->diag_buffer_status[buffer_type] & MPT3_DIAG_BUFFER_IS_RELEASED)
1757 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1758 MPT3_APP_FLAGS_BUFFER_VALID);
1759 else
1760 karg.application_flags = (MPT3_APP_FLAGS_APP_OWNED |
1761 MPT3_APP_FLAGS_BUFFER_VALID |
1762 MPT3_APP_FLAGS_FW_BUFFER_ACCESS);
1763
1764 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
1765 karg.product_specific[i] =
1766 ioc->product_specific[buffer_type][i];
1767
1768 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1769 karg.driver_added_buffer_size = 0;
1770 karg.unique_id = ioc->unique_id[buffer_type];
1771 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1772
1773 if (copy_to_user(arg, &karg, sizeof(struct mpt3_diag_query))) {
1774 pr_err(MPT3SAS_FMT
1775 "%s: unable to write mpt3_diag_query data @ %p\n",
1776 ioc->name, __func__, arg);
1777 return -EFAULT;
1778 }
1779 return 0;
1780}
1781
1782/**
1783 * mpt3sas_send_diag_release - Diag Release Message
1784 * @ioc: per adapter object
1785 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
1786 * @issue_reset - specifies whether host reset is required.
1787 *
1788 */
1789int
1790mpt3sas_send_diag_release(struct MPT3SAS_ADAPTER *ioc, u8 buffer_type,
1791 u8 *issue_reset)
1792{
1793 Mpi2DiagReleaseRequest_t *mpi_request;
1794 Mpi2DiagReleaseReply_t *mpi_reply;
1795 u16 smid;
1796 u16 ioc_status;
1797 u32 ioc_state;
1798 int rc;
1799 unsigned long timeleft;
1800
1801 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1802 __func__));
1803
1804 rc = 0;
1805 *issue_reset = 0;
1806
1807 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
1808 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
1809 if (ioc->diag_buffer_status[buffer_type] &
1810 MPT3_DIAG_BUFFER_IS_REGISTERED)
1811 ioc->diag_buffer_status[buffer_type] |=
1812 MPT3_DIAG_BUFFER_IS_RELEASED;
1813 dctlprintk(ioc, pr_info(MPT3SAS_FMT
1814 "%s: skipping due to FAULT state\n", ioc->name,
1815 __func__));
1816 rc = -EAGAIN;
1817 goto out;
1818 }
1819
1820 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
1821 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
1822 ioc->name, __func__);
1823 rc = -EAGAIN;
1824 goto out;
1825 }
1826
1827 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1828 if (!smid) {
1829 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
1830 ioc->name, __func__);
1831 rc = -EAGAIN;
1832 goto out;
1833 }
1834
1835 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
1836 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1837 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1838 ioc->ctl_cmds.smid = smid;
1839
1840 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1841 mpi_request->BufferType = buffer_type;
1842 mpi_request->VF_ID = 0; /* TODO */
1843 mpi_request->VP_ID = 0;
1844
1845 init_completion(&ioc->ctl_cmds.done);
1846 mpt3sas_base_put_smid_default(ioc, smid);
1847 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1848 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
1849
1850 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
1851 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
1852 __func__);
1853 _debug_dump_mf(mpi_request,
1854 sizeof(Mpi2DiagReleaseRequest_t)/4);
1855 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
1856 *issue_reset = 1;
1857 rc = -EFAULT;
1858 goto out;
1859 }
1860
1861 /* process the completed Reply Message Frame */
1862 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
1863 pr_err(MPT3SAS_FMT "%s: no reply message\n",
1864 ioc->name, __func__);
1865 rc = -EFAULT;
1866 goto out;
1867 }
1868
1869 mpi_reply = ioc->ctl_cmds.reply;
1870 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1871
1872 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1873 ioc->diag_buffer_status[buffer_type] |=
1874 MPT3_DIAG_BUFFER_IS_RELEASED;
1875 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
1876 ioc->name, __func__));
1877 } else {
1878 pr_info(MPT3SAS_FMT
1879 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
1880 ioc->name, __func__,
1881 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
1882 rc = -EFAULT;
1883 }
1884
1885 out:
1886 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
1887 return rc;
1888}
1889
1890/**
1891 * _ctl_diag_release - request to send Diag Release Message to firmware
1892 * @arg - user space buffer containing ioctl content
1893 *
1894 * This allows ownership of the specified buffer to returned to the driver,
1895 * allowing an application to read the buffer without fear that firmware is
1896 * overwritting information in the buffer.
1897 */
1898static long
1899_ctl_diag_release(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1900{
1901 struct mpt3_diag_release karg;
1902 void *request_data;
1903 int rc;
1904 u8 buffer_type;
1905 u8 issue_reset = 0;
1906
1907 if (copy_from_user(&karg, arg, sizeof(karg))) {
1908 pr_err("failure at %s:%d/%s()!\n",
1909 __FILE__, __LINE__, __func__);
1910 return -EFAULT;
1911 }
1912
1913 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
1914 __func__));
1915
1916 buffer_type = karg.unique_id & 0x000000ff;
1917 if (!_ctl_diag_capability(ioc, buffer_type)) {
1918 pr_err(MPT3SAS_FMT
1919 "%s: doesn't have capability for buffer_type(0x%02x)\n",
1920 ioc->name, __func__, buffer_type);
1921 return -EPERM;
1922 }
1923
1924 if ((ioc->diag_buffer_status[buffer_type] &
1925 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
1926 pr_err(MPT3SAS_FMT
1927 "%s: buffer_type(0x%02x) is not registered\n",
1928 ioc->name, __func__, buffer_type);
1929 return -EINVAL;
1930 }
1931
1932 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1933 pr_err(MPT3SAS_FMT
1934 "%s: unique_id(0x%08x) is not registered\n",
1935 ioc->name, __func__, karg.unique_id);
1936 return -EINVAL;
1937 }
1938
1939 if (ioc->diag_buffer_status[buffer_type] &
1940 MPT3_DIAG_BUFFER_IS_RELEASED) {
1941 pr_err(MPT3SAS_FMT
1942 "%s: buffer_type(0x%02x) is already released\n",
1943 ioc->name, __func__,
1944 buffer_type);
1945 return 0;
1946 }
1947
1948 request_data = ioc->diag_buffer[buffer_type];
1949
1950 if (!request_data) {
1951 pr_err(MPT3SAS_FMT
1952 "%s: doesn't have memory allocated for buffer_type(0x%02x)\n",
1953 ioc->name, __func__, buffer_type);
1954 return -ENOMEM;
1955 }
1956
1957 /* buffers were released by due to host reset */
1958 if ((ioc->diag_buffer_status[buffer_type] &
1959 MPT3_DIAG_BUFFER_IS_DIAG_RESET)) {
1960 ioc->diag_buffer_status[buffer_type] |=
1961 MPT3_DIAG_BUFFER_IS_RELEASED;
1962 ioc->diag_buffer_status[buffer_type] &=
1963 ~MPT3_DIAG_BUFFER_IS_DIAG_RESET;
1964 pr_err(MPT3SAS_FMT
1965 "%s: buffer_type(0x%02x) was released due to host reset\n",
1966 ioc->name, __func__, buffer_type);
1967 return 0;
1968 }
1969
1970 rc = mpt3sas_send_diag_release(ioc, buffer_type, &issue_reset);
1971
1972 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07001973 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301974
1975 return rc;
1976}
1977
1978/**
1979 * _ctl_diag_read_buffer - request for copy of the diag buffer
1980 * @ioc: per adapter object
1981 * @arg - user space buffer containing ioctl content
1982 */
1983static long
1984_ctl_diag_read_buffer(struct MPT3SAS_ADAPTER *ioc, void __user *arg)
1985{
1986 struct mpt3_diag_read_buffer karg;
1987 struct mpt3_diag_read_buffer __user *uarg = arg;
1988 void *request_data, *diag_data;
1989 Mpi2DiagBufferPostRequest_t *mpi_request;
1990 Mpi2DiagBufferPostReply_t *mpi_reply;
1991 int rc, i;
1992 u8 buffer_type;
1993 unsigned long timeleft, request_size, copy_size;
1994 u16 smid;
1995 u16 ioc_status;
1996 u8 issue_reset = 0;
1997
1998 if (copy_from_user(&karg, arg, sizeof(karg))) {
1999 pr_err("failure at %s:%d/%s()!\n",
2000 __FILE__, __LINE__, __func__);
2001 return -EFAULT;
2002 }
2003
2004 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2005 __func__));
2006
2007 buffer_type = karg.unique_id & 0x000000ff;
2008 if (!_ctl_diag_capability(ioc, buffer_type)) {
2009 pr_err(MPT3SAS_FMT
2010 "%s: doesn't have capability for buffer_type(0x%02x)\n",
2011 ioc->name, __func__, buffer_type);
2012 return -EPERM;
2013 }
2014
2015 if (karg.unique_id != ioc->unique_id[buffer_type]) {
2016 pr_err(MPT3SAS_FMT
2017 "%s: unique_id(0x%08x) is not registered\n",
2018 ioc->name, __func__, karg.unique_id);
2019 return -EINVAL;
2020 }
2021
2022 request_data = ioc->diag_buffer[buffer_type];
2023 if (!request_data) {
2024 pr_err(MPT3SAS_FMT
2025 "%s: doesn't have buffer for buffer_type(0x%02x)\n",
2026 ioc->name, __func__, buffer_type);
2027 return -ENOMEM;
2028 }
2029
2030 request_size = ioc->diag_buffer_sz[buffer_type];
2031
2032 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
2033 pr_err(MPT3SAS_FMT "%s: either the starting_offset " \
2034 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
2035 __func__);
2036 return -EINVAL;
2037 }
2038
2039 if (karg.starting_offset > request_size)
2040 return -EINVAL;
2041
2042 diag_data = (void *)(request_data + karg.starting_offset);
2043 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2044 "%s: diag_buffer(%p), offset(%d), sz(%d)\n",
2045 ioc->name, __func__,
2046 diag_data, karg.starting_offset, karg.bytes_to_read));
2047
2048 /* Truncate data on requests that are too large */
2049 if ((diag_data + karg.bytes_to_read < diag_data) ||
2050 (diag_data + karg.bytes_to_read > request_data + request_size))
2051 copy_size = request_size - karg.starting_offset;
2052 else
2053 copy_size = karg.bytes_to_read;
2054
2055 if (copy_to_user((void __user *)uarg->diagnostic_data,
2056 diag_data, copy_size)) {
2057 pr_err(MPT3SAS_FMT
2058 "%s: Unable to write mpt_diag_read_buffer_t data @ %p\n",
2059 ioc->name, __func__, diag_data);
2060 return -EFAULT;
2061 }
2062
2063 if ((karg.flags & MPT3_FLAGS_REREGISTER) == 0)
2064 return 0;
2065
2066 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2067 "%s: Reregister buffer_type(0x%02x)\n",
2068 ioc->name, __func__, buffer_type));
2069 if ((ioc->diag_buffer_status[buffer_type] &
2070 MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
2071 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2072 "%s: buffer_type(0x%02x) is still registered\n",
2073 ioc->name, __func__, buffer_type));
2074 return 0;
2075 }
2076 /* Get a free request frame and save the message context.
2077 */
2078
2079 if (ioc->ctl_cmds.status != MPT3_CMD_NOT_USED) {
2080 pr_err(MPT3SAS_FMT "%s: ctl_cmd in use\n",
2081 ioc->name, __func__);
2082 rc = -EAGAIN;
2083 goto out;
2084 }
2085
2086 smid = mpt3sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2087 if (!smid) {
2088 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2089 ioc->name, __func__);
2090 rc = -EAGAIN;
2091 goto out;
2092 }
2093
2094 rc = 0;
2095 ioc->ctl_cmds.status = MPT3_CMD_PENDING;
2096 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2097 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2098 ioc->ctl_cmds.smid = smid;
2099
2100 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2101 mpi_request->BufferType = buffer_type;
2102 mpi_request->BufferLength =
2103 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2104 mpi_request->BufferAddress =
2105 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2106 for (i = 0; i < MPT3_PRODUCT_SPECIFIC_DWORDS; i++)
2107 mpi_request->ProductSpecific[i] =
2108 cpu_to_le32(ioc->product_specific[buffer_type][i]);
2109 mpi_request->VF_ID = 0; /* TODO */
2110 mpi_request->VP_ID = 0;
2111
2112 init_completion(&ioc->ctl_cmds.done);
2113 mpt3sas_base_put_smid_default(ioc, smid);
2114 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2115 MPT3_IOCTL_DEFAULT_TIMEOUT*HZ);
2116
2117 if (!(ioc->ctl_cmds.status & MPT3_CMD_COMPLETE)) {
2118 pr_err(MPT3SAS_FMT "%s: timeout\n", ioc->name,
2119 __func__);
2120 _debug_dump_mf(mpi_request,
2121 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2122 if (!(ioc->ctl_cmds.status & MPT3_CMD_RESET))
2123 issue_reset = 1;
2124 goto issue_host_reset;
2125 }
2126
2127 /* process the completed Reply Message Frame */
2128 if ((ioc->ctl_cmds.status & MPT3_CMD_REPLY_VALID) == 0) {
2129 pr_err(MPT3SAS_FMT "%s: no reply message\n",
2130 ioc->name, __func__);
2131 rc = -EFAULT;
2132 goto out;
2133 }
2134
2135 mpi_reply = ioc->ctl_cmds.reply;
2136 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2137
2138 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2139 ioc->diag_buffer_status[buffer_type] |=
2140 MPT3_DIAG_BUFFER_IS_REGISTERED;
2141 dctlprintk(ioc, pr_info(MPT3SAS_FMT "%s: success\n",
2142 ioc->name, __func__));
2143 } else {
2144 pr_info(MPT3SAS_FMT
2145 "%s: ioc_status(0x%04x) log_info(0x%08x)\n",
2146 ioc->name, __func__,
2147 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
2148 rc = -EFAULT;
2149 }
2150
2151 issue_host_reset:
2152 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07002153 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302154
2155 out:
2156
2157 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
2158 return rc;
2159}
2160
2161
2162
2163#ifdef CONFIG_COMPAT
2164/**
2165 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
2166 * @ioc: per adapter object
2167 * @cmd - ioctl opcode
2168 * @arg - (struct mpt3_ioctl_command32)
2169 *
2170 * MPT3COMMAND32 - Handle 32bit applications running on 64bit os.
2171 */
2172static long
2173_ctl_compat_mpt_command(struct MPT3SAS_ADAPTER *ioc, unsigned cmd,
2174 void __user *arg)
2175{
2176 struct mpt3_ioctl_command32 karg32;
2177 struct mpt3_ioctl_command32 __user *uarg;
2178 struct mpt3_ioctl_command karg;
2179
2180 if (_IOC_SIZE(cmd) != sizeof(struct mpt3_ioctl_command32))
2181 return -EINVAL;
2182
2183 uarg = (struct mpt3_ioctl_command32 __user *) arg;
2184
2185 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2186 pr_err("failure at %s:%d/%s()!\n",
2187 __FILE__, __LINE__, __func__);
2188 return -EFAULT;
2189 }
2190
2191 memset(&karg, 0, sizeof(struct mpt3_ioctl_command));
2192 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2193 karg.hdr.port_number = karg32.hdr.port_number;
2194 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2195 karg.timeout = karg32.timeout;
2196 karg.max_reply_bytes = karg32.max_reply_bytes;
2197 karg.data_in_size = karg32.data_in_size;
2198 karg.data_out_size = karg32.data_out_size;
2199 karg.max_sense_bytes = karg32.max_sense_bytes;
2200 karg.data_sge_offset = karg32.data_sge_offset;
2201 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2202 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2203 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2204 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
2205 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2206}
2207#endif
2208
2209/**
2210 * _ctl_ioctl_main - main ioctl entry point
2211 * @file - (struct file)
2212 * @cmd - ioctl opcode
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302213 * @arg - user space data buffer
2214 * @compat - handles 32 bit applications in 64bit os
2215 * @mpi_version: will be MPI2_VERSION for mpt2ctl ioctl device &
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302216 * MPI25_VERSION | MPI26_VERSION for mpt3ctl ioctl device.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302217 */
2218static long
2219_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302220 u8 compat, u16 mpi_version)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302221{
2222 struct MPT3SAS_ADAPTER *ioc;
2223 struct mpt3_ioctl_header ioctl_header;
2224 enum block_state state;
2225 long ret = -EINVAL;
2226
2227 /* get IOCTL header */
2228 if (copy_from_user(&ioctl_header, (char __user *)arg,
2229 sizeof(struct mpt3_ioctl_header))) {
2230 pr_err("failure at %s:%d/%s()!\n",
2231 __FILE__, __LINE__, __func__);
2232 return -EFAULT;
2233 }
2234
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302235 if (_ctl_verify_adapter(ioctl_header.ioc_number,
2236 &ioc, mpi_version) == -1 || !ioc)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302237 return -ENODEV;
2238
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302239 /* pci_access_mutex lock acquired by ioctl path */
2240 mutex_lock(&ioc->pci_access_mutex);
2241
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302242 if (ioc->shost_recovery || ioc->pci_error_recovery ||
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302243 ioc->is_driver_loading || ioc->remove_host) {
2244 ret = -EAGAIN;
2245 goto out_unlock_pciaccess;
2246 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302247
2248 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
2249 if (state == NON_BLOCKING) {
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302250 if (!mutex_trylock(&ioc->ctl_cmds.mutex)) {
2251 ret = -EAGAIN;
2252 goto out_unlock_pciaccess;
2253 }
2254 } else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex)) {
2255 ret = -ERESTARTSYS;
2256 goto out_unlock_pciaccess;
2257 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302258
2259
2260 switch (cmd) {
2261 case MPT3IOCINFO:
2262 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_iocinfo))
2263 ret = _ctl_getiocinfo(ioc, arg);
2264 break;
2265#ifdef CONFIG_COMPAT
2266 case MPT3COMMAND32:
2267#endif
2268 case MPT3COMMAND:
2269 {
2270 struct mpt3_ioctl_command __user *uarg;
2271 struct mpt3_ioctl_command karg;
2272
2273#ifdef CONFIG_COMPAT
2274 if (compat) {
2275 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2276 break;
2277 }
2278#endif
2279 if (copy_from_user(&karg, arg, sizeof(karg))) {
2280 pr_err("failure at %s:%d/%s()!\n",
2281 __FILE__, __LINE__, __func__);
2282 ret = -EFAULT;
2283 break;
2284 }
2285
2286 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_command)) {
2287 uarg = arg;
2288 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2289 }
2290 break;
2291 }
2292 case MPT3EVENTQUERY:
2293 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventquery))
2294 ret = _ctl_eventquery(ioc, arg);
2295 break;
2296 case MPT3EVENTENABLE:
2297 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_eventenable))
2298 ret = _ctl_eventenable(ioc, arg);
2299 break;
2300 case MPT3EVENTREPORT:
2301 ret = _ctl_eventreport(ioc, arg);
2302 break;
2303 case MPT3HARDRESET:
2304 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_diag_reset))
2305 ret = _ctl_do_reset(ioc, arg);
2306 break;
2307 case MPT3BTDHMAPPING:
2308 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_ioctl_btdh_mapping))
2309 ret = _ctl_btdh_mapping(ioc, arg);
2310 break;
2311 case MPT3DIAGREGISTER:
2312 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_register))
2313 ret = _ctl_diag_register(ioc, arg);
2314 break;
2315 case MPT3DIAGUNREGISTER:
2316 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_unregister))
2317 ret = _ctl_diag_unregister(ioc, arg);
2318 break;
2319 case MPT3DIAGQUERY:
2320 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_query))
2321 ret = _ctl_diag_query(ioc, arg);
2322 break;
2323 case MPT3DIAGRELEASE:
2324 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_release))
2325 ret = _ctl_diag_release(ioc, arg);
2326 break;
2327 case MPT3DIAGREADBUFFER:
2328 if (_IOC_SIZE(cmd) == sizeof(struct mpt3_diag_read_buffer))
2329 ret = _ctl_diag_read_buffer(ioc, arg);
2330 break;
2331 default:
2332 dctlprintk(ioc, pr_info(MPT3SAS_FMT
2333 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2334 break;
2335 }
2336
2337 mutex_unlock(&ioc->ctl_cmds.mutex);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302338out_unlock_pciaccess:
2339 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302340 return ret;
2341}
2342
2343/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302344 * _ctl_ioctl - mpt3ctl main ioctl entry point (unlocked)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302345 * @file - (struct file)
2346 * @cmd - ioctl opcode
2347 * @arg -
2348 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302349long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302350_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302351{
2352 long ret;
2353
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302354 /* pass MPI25_VERSION | MPI26_VERSION value,
2355 * to indicate that this ioctl cmd
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302356 * came from mpt3ctl ioctl device.
2357 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302358 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0,
2359 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302360 return ret;
2361}
2362
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302363/**
2364 * _ctl_mpt2_ioctl - mpt2ctl main ioctl entry point (unlocked)
2365 * @file - (struct file)
2366 * @cmd - ioctl opcode
2367 * @arg -
2368 */
2369long
2370_ctl_mpt2_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2371{
2372 long ret;
2373
2374 /* pass MPI2_VERSION value, to indicate that this ioctl cmd
2375 * came from mpt2ctl ioctl device.
2376 */
2377 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0, MPI2_VERSION);
2378 return ret;
2379}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302380#ifdef CONFIG_COMPAT
2381/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302382 *_ ctl_ioctl_compat - main ioctl entry point (compat)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302383 * @file -
2384 * @cmd -
2385 * @arg -
2386 *
2387 * This routine handles 32 bit applications in 64bit os.
2388 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302389long
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302390_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302391{
2392 long ret;
2393
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05302394 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1,
2395 MPI25_VERSION | MPI26_VERSION);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05302396 return ret;
2397}
2398
2399/**
2400 *_ ctl_mpt2_ioctl_compat - main ioctl entry point (compat)
2401 * @file -
2402 * @cmd -
2403 * @arg -
2404 *
2405 * This routine handles 32 bit applications in 64bit os.
2406 */
2407long
2408_ctl_mpt2_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2409{
2410 long ret;
2411
2412 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1, MPI2_VERSION);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302413 return ret;
2414}
2415#endif
2416
2417/* scsi host attributes */
2418/**
2419 * _ctl_version_fw_show - firmware version
2420 * @cdev - pointer to embedded class device
2421 * @buf - the buffer returned
2422 *
2423 * A sysfs 'read-only' shost attribute.
2424 */
2425static ssize_t
2426_ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2427 char *buf)
2428{
2429 struct Scsi_Host *shost = class_to_shost(cdev);
2430 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2431
2432 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2433 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2434 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2435 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2436 ioc->facts.FWVersion.Word & 0x000000FF);
2437}
2438static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2439
2440/**
2441 * _ctl_version_bios_show - bios version
2442 * @cdev - pointer to embedded class device
2443 * @buf - the buffer returned
2444 *
2445 * A sysfs 'read-only' shost attribute.
2446 */
2447static ssize_t
2448_ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2449 char *buf)
2450{
2451 struct Scsi_Host *shost = class_to_shost(cdev);
2452 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2453
2454 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2455
2456 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2457 (version & 0xFF000000) >> 24,
2458 (version & 0x00FF0000) >> 16,
2459 (version & 0x0000FF00) >> 8,
2460 version & 0x000000FF);
2461}
2462static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2463
2464/**
2465 * _ctl_version_mpi_show - MPI (message passing interface) version
2466 * @cdev - pointer to embedded class device
2467 * @buf - the buffer returned
2468 *
2469 * A sysfs 'read-only' shost attribute.
2470 */
2471static ssize_t
2472_ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2473 char *buf)
2474{
2475 struct Scsi_Host *shost = class_to_shost(cdev);
2476 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2477
2478 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2479 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2480}
2481static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2482
2483/**
2484 * _ctl_version_product_show - product name
2485 * @cdev - pointer to embedded class device
2486 * @buf - the buffer returned
2487 *
2488 * A sysfs 'read-only' shost attribute.
2489 */
2490static ssize_t
2491_ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2492 char *buf)
2493{
2494 struct Scsi_Host *shost = class_to_shost(cdev);
2495 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2496
2497 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2498}
2499static DEVICE_ATTR(version_product, S_IRUGO, _ctl_version_product_show, NULL);
2500
2501/**
2502 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2503 * @cdev - pointer to embedded class device
2504 * @buf - the buffer returned
2505 *
2506 * A sysfs 'read-only' shost attribute.
2507 */
2508static ssize_t
2509_ctl_version_nvdata_persistent_show(struct device *cdev,
2510 struct device_attribute *attr, char *buf)
2511{
2512 struct Scsi_Host *shost = class_to_shost(cdev);
2513 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2514
2515 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2516 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
2517}
2518static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2519 _ctl_version_nvdata_persistent_show, NULL);
2520
2521/**
2522 * _ctl_version_nvdata_default_show - nvdata default version
2523 * @cdev - pointer to embedded class device
2524 * @buf - the buffer returned
2525 *
2526 * A sysfs 'read-only' shost attribute.
2527 */
2528static ssize_t
2529_ctl_version_nvdata_default_show(struct device *cdev, struct device_attribute
2530 *attr, char *buf)
2531{
2532 struct Scsi_Host *shost = class_to_shost(cdev);
2533 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2534
2535 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2536 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
2537}
2538static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2539 _ctl_version_nvdata_default_show, NULL);
2540
2541/**
2542 * _ctl_board_name_show - board name
2543 * @cdev - pointer to embedded class device
2544 * @buf - the buffer returned
2545 *
2546 * A sysfs 'read-only' shost attribute.
2547 */
2548static ssize_t
2549_ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2550 char *buf)
2551{
2552 struct Scsi_Host *shost = class_to_shost(cdev);
2553 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2554
2555 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2556}
2557static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2558
2559/**
2560 * _ctl_board_assembly_show - board assembly name
2561 * @cdev - pointer to embedded class device
2562 * @buf - the buffer returned
2563 *
2564 * A sysfs 'read-only' shost attribute.
2565 */
2566static ssize_t
2567_ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2568 char *buf)
2569{
2570 struct Scsi_Host *shost = class_to_shost(cdev);
2571 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2572
2573 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2574}
2575static DEVICE_ATTR(board_assembly, S_IRUGO, _ctl_board_assembly_show, NULL);
2576
2577/**
2578 * _ctl_board_tracer_show - board tracer number
2579 * @cdev - pointer to embedded class device
2580 * @buf - the buffer returned
2581 *
2582 * A sysfs 'read-only' shost attribute.
2583 */
2584static ssize_t
2585_ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2586 char *buf)
2587{
2588 struct Scsi_Host *shost = class_to_shost(cdev);
2589 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2590
2591 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2592}
2593static DEVICE_ATTR(board_tracer, S_IRUGO, _ctl_board_tracer_show, NULL);
2594
2595/**
2596 * _ctl_io_delay_show - io missing delay
2597 * @cdev - pointer to embedded class device
2598 * @buf - the buffer returned
2599 *
2600 * This is for firmware implemention for deboucing device
2601 * removal events.
2602 *
2603 * A sysfs 'read-only' shost attribute.
2604 */
2605static ssize_t
2606_ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2607 char *buf)
2608{
2609 struct Scsi_Host *shost = class_to_shost(cdev);
2610 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2611
2612 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2613}
2614static DEVICE_ATTR(io_delay, S_IRUGO, _ctl_io_delay_show, NULL);
2615
2616/**
2617 * _ctl_device_delay_show - device missing delay
2618 * @cdev - pointer to embedded class device
2619 * @buf - the buffer returned
2620 *
2621 * This is for firmware implemention for deboucing device
2622 * removal events.
2623 *
2624 * A sysfs 'read-only' shost attribute.
2625 */
2626static ssize_t
2627_ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2628 char *buf)
2629{
2630 struct Scsi_Host *shost = class_to_shost(cdev);
2631 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2632
2633 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2634}
2635static DEVICE_ATTR(device_delay, S_IRUGO, _ctl_device_delay_show, NULL);
2636
2637/**
2638 * _ctl_fw_queue_depth_show - global credits
2639 * @cdev - pointer to embedded class device
2640 * @buf - the buffer returned
2641 *
2642 * This is firmware queue depth limit
2643 *
2644 * A sysfs 'read-only' shost attribute.
2645 */
2646static ssize_t
2647_ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2648 char *buf)
2649{
2650 struct Scsi_Host *shost = class_to_shost(cdev);
2651 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2652
2653 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2654}
2655static DEVICE_ATTR(fw_queue_depth, S_IRUGO, _ctl_fw_queue_depth_show, NULL);
2656
2657/**
2658 * _ctl_sas_address_show - sas address
2659 * @cdev - pointer to embedded class device
2660 * @buf - the buffer returned
2661 *
2662 * This is the controller sas address
2663 *
2664 * A sysfs 'read-only' shost attribute.
2665 */
2666static ssize_t
2667_ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2668 char *buf)
2669
2670{
2671 struct Scsi_Host *shost = class_to_shost(cdev);
2672 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2673
2674 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2675 (unsigned long long)ioc->sas_hba.sas_address);
2676}
2677static DEVICE_ATTR(host_sas_address, S_IRUGO,
2678 _ctl_host_sas_address_show, NULL);
2679
2680/**
2681 * _ctl_logging_level_show - logging level
2682 * @cdev - pointer to embedded class device
2683 * @buf - the buffer returned
2684 *
2685 * A sysfs 'read/write' shost attribute.
2686 */
2687static ssize_t
2688_ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2689 char *buf)
2690{
2691 struct Scsi_Host *shost = class_to_shost(cdev);
2692 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2693
2694 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2695}
2696static ssize_t
2697_ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2698 const char *buf, size_t count)
2699{
2700 struct Scsi_Host *shost = class_to_shost(cdev);
2701 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2702 int val = 0;
2703
2704 if (sscanf(buf, "%x", &val) != 1)
2705 return -EINVAL;
2706
2707 ioc->logging_level = val;
2708 pr_info(MPT3SAS_FMT "logging_level=%08xh\n", ioc->name,
2709 ioc->logging_level);
2710 return strlen(buf);
2711}
2712static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR, _ctl_logging_level_show,
2713 _ctl_logging_level_store);
2714
2715/**
2716 * _ctl_fwfault_debug_show - show/store fwfault_debug
2717 * @cdev - pointer to embedded class device
2718 * @buf - the buffer returned
2719 *
2720 * mpt3sas_fwfault_debug is command line option
2721 * A sysfs 'read/write' shost attribute.
2722 */
2723static ssize_t
2724_ctl_fwfault_debug_show(struct device *cdev, struct device_attribute *attr,
2725 char *buf)
2726{
2727 struct Scsi_Host *shost = class_to_shost(cdev);
2728 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2729
2730 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2731}
2732static ssize_t
2733_ctl_fwfault_debug_store(struct device *cdev, struct device_attribute *attr,
2734 const char *buf, size_t count)
2735{
2736 struct Scsi_Host *shost = class_to_shost(cdev);
2737 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2738 int val = 0;
2739
2740 if (sscanf(buf, "%d", &val) != 1)
2741 return -EINVAL;
2742
2743 ioc->fwfault_debug = val;
2744 pr_info(MPT3SAS_FMT "fwfault_debug=%d\n", ioc->name,
2745 ioc->fwfault_debug);
2746 return strlen(buf);
2747}
2748static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2749 _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2750
2751/**
2752 * _ctl_ioc_reset_count_show - ioc reset count
2753 * @cdev - pointer to embedded class device
2754 * @buf - the buffer returned
2755 *
2756 * This is firmware queue depth limit
2757 *
2758 * A sysfs 'read-only' shost attribute.
2759 */
2760static ssize_t
2761_ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2762 char *buf)
2763{
2764 struct Scsi_Host *shost = class_to_shost(cdev);
2765 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2766
2767 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->ioc_reset_count);
2768}
2769static DEVICE_ATTR(ioc_reset_count, S_IRUGO, _ctl_ioc_reset_count_show, NULL);
2770
2771/**
2772 * _ctl_ioc_reply_queue_count_show - number of reply queues
2773 * @cdev - pointer to embedded class device
2774 * @buf - the buffer returned
2775 *
2776 * This is number of reply queues
2777 *
2778 * A sysfs 'read-only' shost attribute.
2779 */
2780static ssize_t
2781_ctl_ioc_reply_queue_count_show(struct device *cdev,
2782 struct device_attribute *attr, char *buf)
2783{
2784 u8 reply_queue_count;
2785 struct Scsi_Host *shost = class_to_shost(cdev);
2786 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2787
2788 if ((ioc->facts.IOCCapabilities &
2789 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2790 reply_queue_count = ioc->reply_queue_count;
2791 else
2792 reply_queue_count = 1;
2793
2794 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2795}
2796static DEVICE_ATTR(reply_queue_count, S_IRUGO, _ctl_ioc_reply_queue_count_show,
2797 NULL);
2798
Sreekanth Reddy42263092015-11-11 17:30:29 +05302799/**
2800 * _ctl_BRM_status_show - Backup Rail Monitor Status
2801 * @cdev - pointer to embedded class device
2802 * @buf - the buffer returned
2803 *
2804 * This is number of reply queues
2805 *
2806 * A sysfs 'read-only' shost attribute.
2807 */
2808static ssize_t
2809_ctl_BRM_status_show(struct device *cdev, struct device_attribute *attr,
2810 char *buf)
2811{
2812 struct Scsi_Host *shost = class_to_shost(cdev);
2813 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2814 Mpi2IOUnitPage3_t *io_unit_pg3 = NULL;
2815 Mpi2ConfigReply_t mpi_reply;
2816 u16 backup_rail_monitor_status = 0;
2817 u16 ioc_status;
2818 int sz;
2819 ssize_t rc = 0;
2820
2821 if (!ioc->is_warpdrive) {
2822 pr_err(MPT3SAS_FMT "%s: BRM attribute is only for"
2823 " warpdrive\n", ioc->name, __func__);
2824 goto out;
2825 }
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302826 /* pci_access_mutex lock acquired by sysfs show path */
2827 mutex_lock(&ioc->pci_access_mutex);
2828 if (ioc->pci_error_recovery || ioc->remove_host) {
2829 mutex_unlock(&ioc->pci_access_mutex);
2830 return 0;
2831 }
Sreekanth Reddy42263092015-11-11 17:30:29 +05302832
2833 /* allocate upto GPIOVal 36 entries */
2834 sz = offsetof(Mpi2IOUnitPage3_t, GPIOVal) + (sizeof(u16) * 36);
2835 io_unit_pg3 = kzalloc(sz, GFP_KERNEL);
2836 if (!io_unit_pg3) {
2837 pr_err(MPT3SAS_FMT "%s: failed allocating memory "
2838 "for iounit_pg3: (%d) bytes\n", ioc->name, __func__, sz);
2839 goto out;
2840 }
2841
2842 if (mpt3sas_config_get_iounit_pg3(ioc, &mpi_reply, io_unit_pg3, sz) !=
2843 0) {
2844 pr_err(MPT3SAS_FMT
2845 "%s: failed reading iounit_pg3\n", ioc->name,
2846 __func__);
2847 goto out;
2848 }
2849
2850 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
2851 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2852 pr_err(MPT3SAS_FMT "%s: iounit_pg3 failed with "
2853 "ioc_status(0x%04x)\n", ioc->name, __func__, ioc_status);
2854 goto out;
2855 }
2856
2857 if (io_unit_pg3->GPIOCount < 25) {
2858 pr_err(MPT3SAS_FMT "%s: iounit_pg3->GPIOCount less than "
2859 "25 entries, detected (%d) entries\n", ioc->name, __func__,
2860 io_unit_pg3->GPIOCount);
2861 goto out;
2862 }
2863
2864 /* BRM status is in bit zero of GPIOVal[24] */
2865 backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
2866 rc = snprintf(buf, PAGE_SIZE, "%d\n", (backup_rail_monitor_status & 1));
2867
2868 out:
2869 kfree(io_unit_pg3);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05302870 mutex_unlock(&ioc->pci_access_mutex);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302871 return rc;
2872}
2873static DEVICE_ATTR(BRM_status, S_IRUGO, _ctl_BRM_status_show, NULL);
Sreekanth Reddy42263092015-11-11 17:30:29 +05302874
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302875struct DIAG_BUFFER_START {
2876 __le32 Size;
2877 __le32 DiagVersion;
2878 u8 BufferType;
2879 u8 Reserved[3];
2880 __le32 Reserved1;
2881 __le32 Reserved2;
2882 __le32 Reserved3;
2883};
2884
2885/**
2886 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2887 * @cdev - pointer to embedded class device
2888 * @buf - the buffer returned
2889 *
2890 * A sysfs 'read-only' shost attribute.
2891 */
2892static ssize_t
2893_ctl_host_trace_buffer_size_show(struct device *cdev,
2894 struct device_attribute *attr, char *buf)
2895{
2896 struct Scsi_Host *shost = class_to_shost(cdev);
2897 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2898 u32 size = 0;
2899 struct DIAG_BUFFER_START *request_data;
2900
2901 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2902 pr_err(MPT3SAS_FMT
2903 "%s: host_trace_buffer is not registered\n",
2904 ioc->name, __func__);
2905 return 0;
2906 }
2907
2908 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2909 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2910 pr_err(MPT3SAS_FMT
2911 "%s: host_trace_buffer is not registered\n",
2912 ioc->name, __func__);
2913 return 0;
2914 }
2915
2916 request_data = (struct DIAG_BUFFER_START *)
2917 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2918 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2919 le32_to_cpu(request_data->DiagVersion) == 0x01000000 ||
2920 le32_to_cpu(request_data->DiagVersion) == 0x01010000) &&
2921 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2922 size = le32_to_cpu(request_data->Size);
2923
2924 ioc->ring_buffer_sz = size;
2925 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2926}
2927static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2928 _ctl_host_trace_buffer_size_show, NULL);
2929
2930/**
2931 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2932 * @cdev - pointer to embedded class device
2933 * @buf - the buffer returned
2934 *
2935 * A sysfs 'read/write' shost attribute.
2936 *
2937 * You will only be able to read 4k bytes of ring buffer at a time.
2938 * In order to read beyond 4k bytes, you will have to write out the
2939 * offset to the same attribute, it will move the pointer.
2940 */
2941static ssize_t
2942_ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2943 char *buf)
2944{
2945 struct Scsi_Host *shost = class_to_shost(cdev);
2946 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2947 void *request_data;
2948 u32 size;
2949
2950 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2951 pr_err(MPT3SAS_FMT
2952 "%s: host_trace_buffer is not registered\n",
2953 ioc->name, __func__);
2954 return 0;
2955 }
2956
2957 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2958 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
2959 pr_err(MPT3SAS_FMT
2960 "%s: host_trace_buffer is not registered\n",
2961 ioc->name, __func__);
2962 return 0;
2963 }
2964
2965 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2966 return 0;
2967
2968 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2969 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2970 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2971 memcpy(buf, request_data, size);
2972 return size;
2973}
2974
2975static ssize_t
2976_ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2977 const char *buf, size_t count)
2978{
2979 struct Scsi_Host *shost = class_to_shost(cdev);
2980 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
2981 int val = 0;
2982
2983 if (sscanf(buf, "%d", &val) != 1)
2984 return -EINVAL;
2985
2986 ioc->ring_buffer_offset = val;
2987 return strlen(buf);
2988}
2989static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2990 _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2991
2992
2993/*****************************************/
2994
2995/**
2996 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2997 * @cdev - pointer to embedded class device
2998 * @buf - the buffer returned
2999 *
3000 * A sysfs 'read/write' shost attribute.
3001 *
3002 * This is a mechnism to post/release host_trace_buffers
3003 */
3004static ssize_t
3005_ctl_host_trace_buffer_enable_show(struct device *cdev,
3006 struct device_attribute *attr, char *buf)
3007{
3008 struct Scsi_Host *shost = class_to_shost(cdev);
3009 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3010
3011 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
3012 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3013 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0))
3014 return snprintf(buf, PAGE_SIZE, "off\n");
3015 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3016 MPT3_DIAG_BUFFER_IS_RELEASED))
3017 return snprintf(buf, PAGE_SIZE, "release\n");
3018 else
3019 return snprintf(buf, PAGE_SIZE, "post\n");
3020}
3021
3022static ssize_t
3023_ctl_host_trace_buffer_enable_store(struct device *cdev,
3024 struct device_attribute *attr, const char *buf, size_t count)
3025{
3026 struct Scsi_Host *shost = class_to_shost(cdev);
3027 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3028 char str[10] = "";
3029 struct mpt3_diag_register diag_register;
3030 u8 issue_reset = 0;
3031
3032 /* don't allow post/release occurr while recovery is active */
3033 if (ioc->shost_recovery || ioc->remove_host ||
3034 ioc->pci_error_recovery || ioc->is_driver_loading)
3035 return -EBUSY;
3036
3037 if (sscanf(buf, "%9s", str) != 1)
3038 return -EINVAL;
3039
3040 if (!strcmp(str, "post")) {
3041 /* exit out if host buffers are already posted */
3042 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
3043 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3044 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
3045 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3046 MPT3_DIAG_BUFFER_IS_RELEASED) == 0))
3047 goto out;
3048 memset(&diag_register, 0, sizeof(struct mpt3_diag_register));
3049 pr_info(MPT3SAS_FMT "posting host trace buffers\n",
3050 ioc->name);
3051 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
3052 diag_register.requested_buffer_size = (1024 * 1024);
3053 diag_register.unique_id = 0x7075900;
3054 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
3055 _ctl_diag_register_2(ioc, &diag_register);
3056 } else if (!strcmp(str, "release")) {
3057 /* exit out if host buffers are already released */
3058 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
3059 goto out;
3060 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3061 MPT3_DIAG_BUFFER_IS_REGISTERED) == 0)
3062 goto out;
3063 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
3064 MPT3_DIAG_BUFFER_IS_RELEASED))
3065 goto out;
3066 pr_info(MPT3SAS_FMT "releasing host trace buffer\n",
3067 ioc->name);
3068 mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
3069 &issue_reset);
3070 }
3071
3072 out:
3073 return strlen(buf);
3074}
3075static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
3076 _ctl_host_trace_buffer_enable_show,
3077 _ctl_host_trace_buffer_enable_store);
3078
3079/*********** diagnostic trigger suppport *********************************/
3080
3081/**
3082 * _ctl_diag_trigger_master_show - show the diag_trigger_master attribute
3083 * @cdev - pointer to embedded class device
3084 * @buf - the buffer returned
3085 *
3086 * A sysfs 'read/write' shost attribute.
3087 */
3088static ssize_t
3089_ctl_diag_trigger_master_show(struct device *cdev,
3090 struct device_attribute *attr, char *buf)
3091
3092{
3093 struct Scsi_Host *shost = class_to_shost(cdev);
3094 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3095 unsigned long flags;
3096 ssize_t rc;
3097
3098 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3099 rc = sizeof(struct SL_WH_MASTER_TRIGGER_T);
3100 memcpy(buf, &ioc->diag_trigger_master, rc);
3101 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3102 return rc;
3103}
3104
3105/**
3106 * _ctl_diag_trigger_master_store - store the diag_trigger_master attribute
3107 * @cdev - pointer to embedded class device
3108 * @buf - the buffer returned
3109 *
3110 * A sysfs 'read/write' shost attribute.
3111 */
3112static ssize_t
3113_ctl_diag_trigger_master_store(struct device *cdev,
3114 struct device_attribute *attr, const char *buf, size_t count)
3115
3116{
3117 struct Scsi_Host *shost = class_to_shost(cdev);
3118 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3119 unsigned long flags;
3120 ssize_t rc;
3121
3122 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3123 rc = min(sizeof(struct SL_WH_MASTER_TRIGGER_T), count);
3124 memset(&ioc->diag_trigger_master, 0,
3125 sizeof(struct SL_WH_MASTER_TRIGGER_T));
3126 memcpy(&ioc->diag_trigger_master, buf, rc);
3127 ioc->diag_trigger_master.MasterData |=
3128 (MASTER_TRIGGER_FW_FAULT + MASTER_TRIGGER_ADAPTER_RESET);
3129 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3130 return rc;
3131}
3132static DEVICE_ATTR(diag_trigger_master, S_IRUGO | S_IWUSR,
3133 _ctl_diag_trigger_master_show, _ctl_diag_trigger_master_store);
3134
3135
3136/**
3137 * _ctl_diag_trigger_event_show - show the diag_trigger_event attribute
3138 * @cdev - pointer to embedded class device
3139 * @buf - the buffer returned
3140 *
3141 * A sysfs 'read/write' shost attribute.
3142 */
3143static ssize_t
3144_ctl_diag_trigger_event_show(struct device *cdev,
3145 struct device_attribute *attr, char *buf)
3146{
3147 struct Scsi_Host *shost = class_to_shost(cdev);
3148 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3149 unsigned long flags;
3150 ssize_t rc;
3151
3152 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3153 rc = sizeof(struct SL_WH_EVENT_TRIGGERS_T);
3154 memcpy(buf, &ioc->diag_trigger_event, rc);
3155 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3156 return rc;
3157}
3158
3159/**
3160 * _ctl_diag_trigger_event_store - store the diag_trigger_event attribute
3161 * @cdev - pointer to embedded class device
3162 * @buf - the buffer returned
3163 *
3164 * A sysfs 'read/write' shost attribute.
3165 */
3166static ssize_t
3167_ctl_diag_trigger_event_store(struct device *cdev,
3168 struct device_attribute *attr, const char *buf, size_t count)
3169
3170{
3171 struct Scsi_Host *shost = class_to_shost(cdev);
3172 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3173 unsigned long flags;
3174 ssize_t sz;
3175
3176 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3177 sz = min(sizeof(struct SL_WH_EVENT_TRIGGERS_T), count);
3178 memset(&ioc->diag_trigger_event, 0,
3179 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3180 memcpy(&ioc->diag_trigger_event, buf, sz);
3181 if (ioc->diag_trigger_event.ValidEntries > NUM_VALID_ENTRIES)
3182 ioc->diag_trigger_event.ValidEntries = NUM_VALID_ENTRIES;
3183 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3184 return sz;
3185}
3186static DEVICE_ATTR(diag_trigger_event, S_IRUGO | S_IWUSR,
3187 _ctl_diag_trigger_event_show, _ctl_diag_trigger_event_store);
3188
3189
3190/**
3191 * _ctl_diag_trigger_scsi_show - show the diag_trigger_scsi attribute
3192 * @cdev - pointer to embedded class device
3193 * @buf - the buffer returned
3194 *
3195 * A sysfs 'read/write' shost attribute.
3196 */
3197static ssize_t
3198_ctl_diag_trigger_scsi_show(struct device *cdev,
3199 struct device_attribute *attr, char *buf)
3200{
3201 struct Scsi_Host *shost = class_to_shost(cdev);
3202 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3203 unsigned long flags;
3204 ssize_t rc;
3205
3206 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3207 rc = sizeof(struct SL_WH_SCSI_TRIGGERS_T);
3208 memcpy(buf, &ioc->diag_trigger_scsi, rc);
3209 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3210 return rc;
3211}
3212
3213/**
3214 * _ctl_diag_trigger_scsi_store - store the diag_trigger_scsi attribute
3215 * @cdev - pointer to embedded class device
3216 * @buf - the buffer returned
3217 *
3218 * A sysfs 'read/write' shost attribute.
3219 */
3220static ssize_t
3221_ctl_diag_trigger_scsi_store(struct device *cdev,
3222 struct device_attribute *attr, const char *buf, size_t count)
3223{
3224 struct Scsi_Host *shost = class_to_shost(cdev);
3225 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3226 unsigned long flags;
3227 ssize_t sz;
3228
3229 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3230 sz = min(sizeof(struct SL_WH_SCSI_TRIGGERS_T), count);
3231 memset(&ioc->diag_trigger_scsi, 0,
3232 sizeof(struct SL_WH_EVENT_TRIGGERS_T));
3233 memcpy(&ioc->diag_trigger_scsi, buf, sz);
3234 if (ioc->diag_trigger_scsi.ValidEntries > NUM_VALID_ENTRIES)
3235 ioc->diag_trigger_scsi.ValidEntries = NUM_VALID_ENTRIES;
3236 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3237 return sz;
3238}
3239static DEVICE_ATTR(diag_trigger_scsi, S_IRUGO | S_IWUSR,
3240 _ctl_diag_trigger_scsi_show, _ctl_diag_trigger_scsi_store);
3241
3242
3243/**
3244 * _ctl_diag_trigger_scsi_show - show the diag_trigger_mpi attribute
3245 * @cdev - pointer to embedded class device
3246 * @buf - the buffer returned
3247 *
3248 * A sysfs 'read/write' shost attribute.
3249 */
3250static ssize_t
3251_ctl_diag_trigger_mpi_show(struct device *cdev,
3252 struct device_attribute *attr, char *buf)
3253{
3254 struct Scsi_Host *shost = class_to_shost(cdev);
3255 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3256 unsigned long flags;
3257 ssize_t rc;
3258
3259 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3260 rc = sizeof(struct SL_WH_MPI_TRIGGERS_T);
3261 memcpy(buf, &ioc->diag_trigger_mpi, rc);
3262 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3263 return rc;
3264}
3265
3266/**
3267 * _ctl_diag_trigger_mpi_store - store the diag_trigger_mpi attribute
3268 * @cdev - pointer to embedded class device
3269 * @buf - the buffer returned
3270 *
3271 * A sysfs 'read/write' shost attribute.
3272 */
3273static ssize_t
3274_ctl_diag_trigger_mpi_store(struct device *cdev,
3275 struct device_attribute *attr, const char *buf, size_t count)
3276{
3277 struct Scsi_Host *shost = class_to_shost(cdev);
3278 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
3279 unsigned long flags;
3280 ssize_t sz;
3281
3282 spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
3283 sz = min(sizeof(struct SL_WH_MPI_TRIGGERS_T), count);
3284 memset(&ioc->diag_trigger_mpi, 0,
Dan Carpenter66331e82012-12-07 13:56:22 +03003285 sizeof(ioc->diag_trigger_mpi));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303286 memcpy(&ioc->diag_trigger_mpi, buf, sz);
3287 if (ioc->diag_trigger_mpi.ValidEntries > NUM_VALID_ENTRIES)
3288 ioc->diag_trigger_mpi.ValidEntries = NUM_VALID_ENTRIES;
3289 spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
3290 return sz;
3291}
3292
3293static DEVICE_ATTR(diag_trigger_mpi, S_IRUGO | S_IWUSR,
3294 _ctl_diag_trigger_mpi_show, _ctl_diag_trigger_mpi_store);
3295
3296/*********** diagnostic trigger suppport *** END ****************************/
3297
3298
3299
3300/*****************************************/
3301
3302struct device_attribute *mpt3sas_host_attrs[] = {
3303 &dev_attr_version_fw,
3304 &dev_attr_version_bios,
3305 &dev_attr_version_mpi,
3306 &dev_attr_version_product,
3307 &dev_attr_version_nvdata_persistent,
3308 &dev_attr_version_nvdata_default,
3309 &dev_attr_board_name,
3310 &dev_attr_board_assembly,
3311 &dev_attr_board_tracer,
3312 &dev_attr_io_delay,
3313 &dev_attr_device_delay,
3314 &dev_attr_logging_level,
3315 &dev_attr_fwfault_debug,
3316 &dev_attr_fw_queue_depth,
3317 &dev_attr_host_sas_address,
3318 &dev_attr_ioc_reset_count,
3319 &dev_attr_host_trace_buffer_size,
3320 &dev_attr_host_trace_buffer,
3321 &dev_attr_host_trace_buffer_enable,
3322 &dev_attr_reply_queue_count,
3323 &dev_attr_diag_trigger_master,
3324 &dev_attr_diag_trigger_event,
3325 &dev_attr_diag_trigger_scsi,
3326 &dev_attr_diag_trigger_mpi,
Sreekanth Reddy42263092015-11-11 17:30:29 +05303327 &dev_attr_BRM_status,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303328 NULL,
3329};
3330
3331/* device attributes */
3332
3333/**
3334 * _ctl_device_sas_address_show - sas address
3335 * @cdev - pointer to embedded class device
3336 * @buf - the buffer returned
3337 *
3338 * This is the sas address for the target
3339 *
3340 * A sysfs 'read-only' shost attribute.
3341 */
3342static ssize_t
3343_ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
3344 char *buf)
3345{
3346 struct scsi_device *sdev = to_scsi_device(dev);
3347 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3348
3349 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
3350 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
3351}
3352static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
3353
3354/**
3355 * _ctl_device_handle_show - device handle
3356 * @cdev - pointer to embedded class device
3357 * @buf - the buffer returned
3358 *
3359 * This is the firmware assigned device handle
3360 *
3361 * A sysfs 'read-only' shost attribute.
3362 */
3363static ssize_t
3364_ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
3365 char *buf)
3366{
3367 struct scsi_device *sdev = to_scsi_device(dev);
3368 struct MPT3SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
3369
3370 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
3371 sas_device_priv_data->sas_target->handle);
3372}
3373static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
3374
3375struct device_attribute *mpt3sas_dev_attrs[] = {
3376 &dev_attr_sas_address,
3377 &dev_attr_sas_device_handle,
3378 NULL,
3379};
3380
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303381/* file operations table for mpt3ctl device */
3382static const struct file_operations ctl_fops = {
3383 .owner = THIS_MODULE,
3384 .unlocked_ioctl = _ctl_ioctl,
3385 .poll = _ctl_poll,
3386 .fasync = _ctl_fasync,
3387#ifdef CONFIG_COMPAT
3388 .compat_ioctl = _ctl_ioctl_compat,
3389#endif
3390};
3391
3392/* file operations table for mpt2ctl device */
3393static const struct file_operations ctl_gen2_fops = {
3394 .owner = THIS_MODULE,
3395 .unlocked_ioctl = _ctl_mpt2_ioctl,
3396 .poll = _ctl_poll,
3397 .fasync = _ctl_fasync,
3398#ifdef CONFIG_COMPAT
3399 .compat_ioctl = _ctl_mpt2_ioctl_compat,
3400#endif
3401};
3402
3403static struct miscdevice ctl_dev = {
3404 .minor = MPT3SAS_MINOR,
3405 .name = MPT3SAS_DEV_NAME,
3406 .fops = &ctl_fops,
3407};
3408
3409static struct miscdevice gen2_ctl_dev = {
3410 .minor = MPT2SAS_MINOR,
3411 .name = MPT2SAS_DEV_NAME,
3412 .fops = &ctl_gen2_fops,
3413};
3414
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303415/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303416 * mpt3sas_ctl_init - main entry point for ctl.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303417 *
3418 */
3419void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303420mpt3sas_ctl_init(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303421{
3422 async_queue = NULL;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303423
3424 /* Don't register mpt3ctl ioctl device if
3425 * hbas_to_enumarate is one.
3426 */
3427 if (hbas_to_enumerate != 1)
3428 if (misc_register(&ctl_dev) < 0)
3429 pr_err("%s can't register misc device [minor=%d]\n",
3430 MPT3SAS_DRIVER_NAME, MPT3SAS_MINOR);
3431
3432 /* Don't register mpt3ctl ioctl device if
3433 * hbas_to_enumarate is two.
3434 */
3435 if (hbas_to_enumerate != 2)
3436 if (misc_register(&gen2_ctl_dev) < 0)
3437 pr_err("%s can't register misc device [minor=%d]\n",
3438 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
3439
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303440 init_waitqueue_head(&ctl_poll_wait);
3441}
3442
3443/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303444 * mpt3sas_ctl_exit - exit point for ctl
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303445 *
3446 */
3447void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303448mpt3sas_ctl_exit(ushort hbas_to_enumerate)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303449{
3450 struct MPT3SAS_ADAPTER *ioc;
3451 int i;
3452
3453 list_for_each_entry(ioc, &mpt3sas_ioc_list, list) {
3454
3455 /* free memory associated to diag buffers */
3456 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
3457 if (!ioc->diag_buffer[i])
3458 continue;
3459 if (!(ioc->diag_buffer_status[i] &
3460 MPT3_DIAG_BUFFER_IS_REGISTERED))
3461 continue;
3462 if ((ioc->diag_buffer_status[i] &
3463 MPT3_DIAG_BUFFER_IS_RELEASED))
3464 continue;
3465 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
3466 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
3467 ioc->diag_buffer[i] = NULL;
3468 ioc->diag_buffer_status[i] = 0;
3469 }
3470
3471 kfree(ioc->event_log);
3472 }
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303473 if (hbas_to_enumerate != 1)
3474 misc_deregister(&ctl_dev);
3475 if (hbas_to_enumerate != 2)
3476 misc_deregister(&gen2_ctl_dev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303477}