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