blob: 8eb85e1a0c374457aeec3118b22b22b94ca18ea8 [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 ==
982 MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH)) {
983 printk(MPT2SAS_INFO_FMT "issue target reset: handle "
984 "= (0x%04x)\n", ioc->name,
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530985 le16_to_cpu(mpi_request->FunctionDependent1));
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530986 mpt2sas_halt_firmware(ioc);
Eric Moore635374e2009-03-09 01:21:12 -0600987 mpt2sas_scsih_issue_tm(ioc,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +0530988 le16_to_cpu(mpi_request->FunctionDependent1), 0, 0,
989 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0, 10,
Kashyap, Desaif93213d2011-06-14 10:56:43 +0530990 0, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -0600991 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -0600992 } else
993 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
994 FORCE_BIG_HAMMER);
995 }
996
997 out:
998
999 /* free memory associated with sg buffers */
1000 if (data_in)
1001 pci_free_consistent(ioc->pdev, data_in_sz, data_in,
1002 data_in_dma);
1003
1004 if (data_out)
1005 pci_free_consistent(ioc->pdev, data_out_sz, data_out,
1006 data_out_dma);
1007
Kashyap, Desai1bbfa372010-06-17 13:50:11 +05301008 kfree(mpi_request);
Eric Moore635374e2009-03-09 01:21:12 -06001009 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -06001010 return ret;
1011}
1012
1013/**
1014 * _ctl_getiocinfo - main handler for MPT2IOCINFO opcode
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301015 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06001016 * @arg - user space buffer containing ioctl content
1017 */
1018static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301019_ctl_getiocinfo(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
Eric Moore635374e2009-03-09 01:21:12 -06001020{
1021 struct mpt2_ioctl_iocinfo karg;
Eric Moore635374e2009-03-09 01:21:12 -06001022
1023 if (copy_from_user(&karg, arg, sizeof(karg))) {
1024 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1025 __FILE__, __LINE__, __func__);
1026 return -EFAULT;
1027 }
Eric Moore635374e2009-03-09 01:21:12 -06001028
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301029 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001030 __func__));
1031
1032 memset(&karg, 0 , sizeof(karg));
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301033 if (ioc->is_warpdrive)
1034 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2_SSS6200;
1035 else
1036 karg.adapter_type = MPT2_IOCTL_INTERFACE_SAS2;
Eric Moore635374e2009-03-09 01:21:12 -06001037 if (ioc->pfacts)
1038 karg.port_number = ioc->pfacts[0].PortNumber;
Sergei Shtylyov7d7311c2012-03-14 22:04:30 +03001039 karg.hw_rev = ioc->pdev->revision;
Eric Moore635374e2009-03-09 01:21:12 -06001040 karg.pci_id = ioc->pdev->device;
1041 karg.subsystem_device = ioc->pdev->subsystem_device;
1042 karg.subsystem_vendor = ioc->pdev->subsystem_vendor;
1043 karg.pci_information.u.bits.bus = ioc->pdev->bus->number;
1044 karg.pci_information.u.bits.device = PCI_SLOT(ioc->pdev->devfn);
1045 karg.pci_information.u.bits.function = PCI_FUNC(ioc->pdev->devfn);
1046 karg.pci_information.segment_id = pci_domain_nr(ioc->pdev->bus);
1047 karg.firmware_version = ioc->facts.FWVersion.Word;
Eric Mooree5f9bb12009-04-21 15:40:01 -06001048 strcpy(karg.driver_version, MPT2SAS_DRIVER_NAME);
1049 strcat(karg.driver_version, "-");
1050 strcat(karg.driver_version, MPT2SAS_DRIVER_VERSION);
Eric Moore635374e2009-03-09 01:21:12 -06001051 karg.bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
1052
1053 if (copy_to_user(arg, &karg, sizeof(karg))) {
1054 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1055 __FILE__, __LINE__, __func__);
1056 return -EFAULT;
1057 }
1058 return 0;
1059}
1060
1061/**
1062 * _ctl_eventquery - main handler for MPT2EVENTQUERY opcode
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301063 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06001064 * @arg - user space buffer containing ioctl content
1065 */
1066static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301067_ctl_eventquery(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
Eric Moore635374e2009-03-09 01:21:12 -06001068{
1069 struct mpt2_ioctl_eventquery karg;
Eric Moore635374e2009-03-09 01:21:12 -06001070
1071 if (copy_from_user(&karg, arg, sizeof(karg))) {
1072 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1073 __FILE__, __LINE__, __func__);
1074 return -EFAULT;
1075 }
Eric Moore635374e2009-03-09 01:21:12 -06001076
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301077 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001078 __func__));
1079
1080 karg.event_entries = MPT2SAS_CTL_EVENT_LOG_SIZE;
1081 memcpy(karg.event_types, ioc->event_type,
1082 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1083
1084 if (copy_to_user(arg, &karg, sizeof(karg))) {
1085 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1086 __FILE__, __LINE__, __func__);
1087 return -EFAULT;
1088 }
1089 return 0;
1090}
1091
1092/**
1093 * _ctl_eventenable - main handler for MPT2EVENTENABLE opcode
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301094 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06001095 * @arg - user space buffer containing ioctl content
1096 */
1097static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301098_ctl_eventenable(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
Eric Moore635374e2009-03-09 01:21:12 -06001099{
1100 struct mpt2_ioctl_eventenable karg;
Eric Moore635374e2009-03-09 01:21:12 -06001101
1102 if (copy_from_user(&karg, arg, sizeof(karg))) {
1103 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1104 __FILE__, __LINE__, __func__);
1105 return -EFAULT;
1106 }
Eric Moore635374e2009-03-09 01:21:12 -06001107
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301108 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001109 __func__));
1110
1111 if (ioc->event_log)
1112 return 0;
1113 memcpy(ioc->event_type, karg.event_types,
1114 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS * sizeof(u32));
1115 mpt2sas_base_validate_event_type(ioc, ioc->event_type);
1116
1117 /* initialize event_log */
1118 ioc->event_context = 0;
1119 ioc->aen_event_read_flag = 0;
1120 ioc->event_log = kcalloc(MPT2SAS_CTL_EVENT_LOG_SIZE,
1121 sizeof(struct MPT2_IOCTL_EVENTS), GFP_KERNEL);
1122 if (!ioc->event_log) {
1123 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1124 __FILE__, __LINE__, __func__);
1125 return -ENOMEM;
1126 }
1127 return 0;
1128}
1129
1130/**
1131 * _ctl_eventreport - main handler for MPT2EVENTREPORT opcode
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301132 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06001133 * @arg - user space buffer containing ioctl content
1134 */
1135static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301136_ctl_eventreport(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
Eric Moore635374e2009-03-09 01:21:12 -06001137{
1138 struct mpt2_ioctl_eventreport karg;
Eric Moore635374e2009-03-09 01:21:12 -06001139 u32 number_bytes, max_events, max;
1140 struct mpt2_ioctl_eventreport __user *uarg = arg;
1141
1142 if (copy_from_user(&karg, arg, sizeof(karg))) {
1143 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1144 __FILE__, __LINE__, __func__);
1145 return -EFAULT;
1146 }
Eric Moore635374e2009-03-09 01:21:12 -06001147
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301148 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001149 __func__));
1150
1151 number_bytes = karg.hdr.max_data_size -
1152 sizeof(struct mpt2_ioctl_header);
1153 max_events = number_bytes/sizeof(struct MPT2_IOCTL_EVENTS);
1154 max = min_t(u32, MPT2SAS_CTL_EVENT_LOG_SIZE, max_events);
1155
1156 /* If fewer than 1 event is requested, there must have
1157 * been some type of error.
1158 */
1159 if (!max || !ioc->event_log)
1160 return -ENODATA;
1161
1162 number_bytes = max * sizeof(struct MPT2_IOCTL_EVENTS);
1163 if (copy_to_user(uarg->event_data, ioc->event_log, number_bytes)) {
1164 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1165 __FILE__, __LINE__, __func__);
1166 return -EFAULT;
1167 }
1168
1169 /* reset flag so SIGIO can restart */
1170 ioc->aen_event_read_flag = 0;
1171 return 0;
1172}
1173
1174/**
1175 * _ctl_do_reset - main handler for MPT2HARDRESET opcode
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301176 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06001177 * @arg - user space buffer containing ioctl content
1178 */
1179static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301180_ctl_do_reset(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
Eric Moore635374e2009-03-09 01:21:12 -06001181{
1182 struct mpt2_ioctl_diag_reset karg;
Eric Moore635374e2009-03-09 01:21:12 -06001183 int retval;
1184
1185 if (copy_from_user(&karg, arg, sizeof(karg))) {
1186 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1187 __FILE__, __LINE__, __func__);
1188 return -EFAULT;
1189 }
Eric Moore635374e2009-03-09 01:21:12 -06001190
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05301191 if (ioc->shost_recovery || ioc->pci_error_recovery ||
1192 ioc->is_driver_loading)
1193 return -EAGAIN;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301194 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001195 __func__));
1196
1197 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1198 FORCE_BIG_HAMMER);
1199 printk(MPT2SAS_INFO_FMT "host reset: %s\n",
1200 ioc->name, ((!retval) ? "SUCCESS" : "FAILED"));
1201 return 0;
1202}
1203
1204/**
1205 * _ctl_btdh_search_sas_device - searching for sas device
1206 * @ioc: per adapter object
1207 * @btdh: btdh ioctl payload
1208 */
1209static int
1210_ctl_btdh_search_sas_device(struct MPT2SAS_ADAPTER *ioc,
1211 struct mpt2_ioctl_btdh_mapping *btdh)
1212{
1213 struct _sas_device *sas_device;
1214 unsigned long flags;
1215 int rc = 0;
1216
1217 if (list_empty(&ioc->sas_device_list))
1218 return rc;
1219
1220 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1221 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
1222 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1223 btdh->handle == sas_device->handle) {
1224 btdh->bus = sas_device->channel;
1225 btdh->id = sas_device->id;
1226 rc = 1;
1227 goto out;
1228 } else if (btdh->bus == sas_device->channel && btdh->id ==
1229 sas_device->id && btdh->handle == 0xFFFF) {
1230 btdh->handle = sas_device->handle;
1231 rc = 1;
1232 goto out;
1233 }
1234 }
1235 out:
1236 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1237 return rc;
1238}
1239
1240/**
1241 * _ctl_btdh_search_raid_device - searching for raid device
1242 * @ioc: per adapter object
1243 * @btdh: btdh ioctl payload
1244 */
1245static int
1246_ctl_btdh_search_raid_device(struct MPT2SAS_ADAPTER *ioc,
1247 struct mpt2_ioctl_btdh_mapping *btdh)
1248{
1249 struct _raid_device *raid_device;
1250 unsigned long flags;
1251 int rc = 0;
1252
1253 if (list_empty(&ioc->raid_device_list))
1254 return rc;
1255
1256 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1257 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
1258 if (btdh->bus == 0xFFFFFFFF && btdh->id == 0xFFFFFFFF &&
1259 btdh->handle == raid_device->handle) {
1260 btdh->bus = raid_device->channel;
1261 btdh->id = raid_device->id;
1262 rc = 1;
1263 goto out;
1264 } else if (btdh->bus == raid_device->channel && btdh->id ==
1265 raid_device->id && btdh->handle == 0xFFFF) {
1266 btdh->handle = raid_device->handle;
1267 rc = 1;
1268 goto out;
1269 }
1270 }
1271 out:
1272 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1273 return rc;
1274}
1275
1276/**
1277 * _ctl_btdh_mapping - main handler for MPT2BTDHMAPPING opcode
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301278 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06001279 * @arg - user space buffer containing ioctl content
1280 */
1281static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301282_ctl_btdh_mapping(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
Eric Moore635374e2009-03-09 01:21:12 -06001283{
1284 struct mpt2_ioctl_btdh_mapping karg;
Eric Moore635374e2009-03-09 01:21:12 -06001285 int rc;
1286
1287 if (copy_from_user(&karg, arg, sizeof(karg))) {
1288 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1289 __FILE__, __LINE__, __func__);
1290 return -EFAULT;
1291 }
Eric Moore635374e2009-03-09 01:21:12 -06001292
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301293 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001294 __func__));
1295
1296 rc = _ctl_btdh_search_sas_device(ioc, &karg);
1297 if (!rc)
1298 _ctl_btdh_search_raid_device(ioc, &karg);
1299
1300 if (copy_to_user(arg, &karg, sizeof(karg))) {
1301 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1302 __FILE__, __LINE__, __func__);
1303 return -EFAULT;
1304 }
1305 return 0;
1306}
1307
1308/**
1309 * _ctl_diag_capability - return diag buffer capability
1310 * @ioc: per adapter object
Kashyap, Desai1b01fe32009-09-23 17:28:59 +05301311 * @buffer_type: specifies either TRACE, SNAPSHOT, or EXTENDED
Eric Moore635374e2009-03-09 01:21:12 -06001312 *
1313 * returns 1 when diag buffer support is enabled in firmware
1314 */
1315static u8
1316_ctl_diag_capability(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type)
1317{
1318 u8 rc = 0;
1319
1320 switch (buffer_type) {
1321 case MPI2_DIAG_BUF_TYPE_TRACE:
1322 if (ioc->facts.IOCCapabilities &
1323 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER)
1324 rc = 1;
1325 break;
1326 case MPI2_DIAG_BUF_TYPE_SNAPSHOT:
1327 if (ioc->facts.IOCCapabilities &
1328 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER)
1329 rc = 1;
1330 break;
Kashyap, Desai1b01fe32009-09-23 17:28:59 +05301331 case MPI2_DIAG_BUF_TYPE_EXTENDED:
1332 if (ioc->facts.IOCCapabilities &
1333 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER)
1334 rc = 1;
Eric Moore635374e2009-03-09 01:21:12 -06001335 }
1336
1337 return rc;
1338}
1339
1340/**
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301341 * _ctl_diag_register_2 - wrapper for registering diag buffer support
1342 * @ioc: per adapter object
1343 * @diag_register: the diag_register struct passed in from user space
Eric Moore635374e2009-03-09 01:21:12 -06001344 *
Eric Moore635374e2009-03-09 01:21:12 -06001345 */
1346static long
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301347_ctl_diag_register_2(struct MPT2SAS_ADAPTER *ioc,
1348 struct mpt2_diag_register *diag_register)
Eric Moore635374e2009-03-09 01:21:12 -06001349{
Eric Moore635374e2009-03-09 01:21:12 -06001350 int rc, i;
1351 void *request_data = NULL;
1352 dma_addr_t request_data_dma;
1353 u32 request_data_sz = 0;
1354 Mpi2DiagBufferPostRequest_t *mpi_request;
1355 Mpi2DiagBufferPostReply_t *mpi_reply;
1356 u8 buffer_type;
1357 unsigned long timeleft;
1358 u16 smid;
1359 u16 ioc_status;
1360 u8 issue_reset = 0;
1361
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301362 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001363 __func__));
1364
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301365 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1366 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1367 ioc->name, __func__);
1368 rc = -EAGAIN;
1369 goto out;
1370 }
1371
1372 buffer_type = diag_register->buffer_type;
Eric Moore635374e2009-03-09 01:21:12 -06001373 if (!_ctl_diag_capability(ioc, buffer_type)) {
1374 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1375 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1376 return -EPERM;
1377 }
1378
1379 if (ioc->diag_buffer_status[buffer_type] &
1380 MPT2_DIAG_BUFFER_IS_REGISTERED) {
1381 printk(MPT2SAS_ERR_FMT "%s: already has a registered "
1382 "buffer for buffer_type(0x%02x)\n", ioc->name, __func__,
1383 buffer_type);
1384 return -EINVAL;
1385 }
1386
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301387 if (diag_register->requested_buffer_size % 4) {
Eric Moore635374e2009-03-09 01:21:12 -06001388 printk(MPT2SAS_ERR_FMT "%s: the requested_buffer_size "
1389 "is not 4 byte aligned\n", ioc->name, __func__);
1390 return -EINVAL;
1391 }
1392
Eric Moore635374e2009-03-09 01:21:12 -06001393 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1394 if (!smid) {
1395 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1396 ioc->name, __func__);
1397 rc = -EAGAIN;
1398 goto out;
1399 }
1400
1401 rc = 0;
1402 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1403 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1404 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1405 ioc->ctl_cmds.smid = smid;
1406
1407 request_data = ioc->diag_buffer[buffer_type];
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301408 request_data_sz = diag_register->requested_buffer_size;
1409 ioc->unique_id[buffer_type] = diag_register->unique_id;
Eric Moore635374e2009-03-09 01:21:12 -06001410 ioc->diag_buffer_status[buffer_type] = 0;
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301411 memcpy(ioc->product_specific[buffer_type],
1412 diag_register->product_specific, MPT2_PRODUCT_SPECIFIC_DWORDS);
1413 ioc->diagnostic_flags[buffer_type] = diag_register->diagnostic_flags;
Eric Moore635374e2009-03-09 01:21:12 -06001414
1415 if (request_data) {
1416 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1417 if (request_data_sz != ioc->diag_buffer_sz[buffer_type]) {
1418 pci_free_consistent(ioc->pdev,
1419 ioc->diag_buffer_sz[buffer_type],
1420 request_data, request_data_dma);
1421 request_data = NULL;
1422 }
1423 }
1424
1425 if (request_data == NULL) {
1426 ioc->diag_buffer_sz[buffer_type] = 0;
1427 ioc->diag_buffer_dma[buffer_type] = 0;
1428 request_data = pci_alloc_consistent(
1429 ioc->pdev, request_data_sz, &request_data_dma);
1430 if (request_data == NULL) {
1431 printk(MPT2SAS_ERR_FMT "%s: failed allocating memory"
1432 " for diag buffers, requested size(%d)\n",
1433 ioc->name, __func__, request_data_sz);
1434 mpt2sas_base_free_smid(ioc, smid);
1435 return -ENOMEM;
1436 }
1437 ioc->diag_buffer[buffer_type] = request_data;
1438 ioc->diag_buffer_sz[buffer_type] = request_data_sz;
1439 ioc->diag_buffer_dma[buffer_type] = request_data_dma;
1440 }
1441
1442 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301443 mpi_request->BufferType = diag_register->buffer_type;
1444 mpi_request->Flags = cpu_to_le32(diag_register->diagnostic_flags);
Eric Moore635374e2009-03-09 01:21:12 -06001445 mpi_request->BufferAddress = cpu_to_le64(request_data_dma);
1446 mpi_request->BufferLength = cpu_to_le32(request_data_sz);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301447 mpi_request->VF_ID = 0; /* TODO */
1448 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001449
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301450 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(0x%p), "
Eric Moore635374e2009-03-09 01:21:12 -06001451 "dma(0x%llx), sz(%d)\n", ioc->name, __func__, request_data,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301452 (unsigned long long)request_data_dma,
1453 le32_to_cpu(mpi_request->BufferLength)));
Eric Moore635374e2009-03-09 01:21:12 -06001454
1455 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1456 mpi_request->ProductSpecific[i] =
1457 cpu_to_le32(ioc->product_specific[buffer_type][i]);
1458
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05301459 init_completion(&ioc->ctl_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05301460 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06001461 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1462 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1463
1464 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1465 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1466 __func__);
1467 _debug_dump_mf(mpi_request,
1468 sizeof(Mpi2DiagBufferPostRequest_t)/4);
1469 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1470 issue_reset = 1;
1471 goto issue_host_reset;
1472 }
1473
1474 /* process the completed Reply Message Frame */
1475 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1476 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1477 ioc->name, __func__);
1478 rc = -EFAULT;
1479 goto out;
1480 }
1481
1482 mpi_reply = ioc->ctl_cmds.reply;
1483 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1484
1485 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1486 ioc->diag_buffer_status[buffer_type] |=
1487 MPT2_DIAG_BUFFER_IS_REGISTERED;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301488 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
Eric Moore635374e2009-03-09 01:21:12 -06001489 ioc->name, __func__));
1490 } else {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301491 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06001492 "log_info(0x%08x)\n", ioc->name, __func__,
Kashyap, Desai463217b2009-10-05 15:53:06 +05301493 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Eric Moore635374e2009-03-09 01:21:12 -06001494 rc = -EFAULT;
1495 }
1496
1497 issue_host_reset:
1498 if (issue_reset)
1499 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1500 FORCE_BIG_HAMMER);
1501
1502 out:
1503
1504 if (rc && request_data)
1505 pci_free_consistent(ioc->pdev, request_data_sz,
1506 request_data, request_data_dma);
1507
1508 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301509 return rc;
1510}
1511
1512/**
1513 * mpt2sas_enable_diag_buffer - enabling diag_buffers support driver load time
1514 * @ioc: per adapter object
1515 * @bits_to_register: bitwise field where trace is bit 0, and snapshot is bit 1
1516 *
1517 * This is called when command line option diag_buffer_enable is enabled
1518 * at driver load time.
1519 */
1520void
1521mpt2sas_enable_diag_buffer(struct MPT2SAS_ADAPTER *ioc, u8 bits_to_register)
1522{
1523 struct mpt2_diag_register diag_register;
1524
1525 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
1526
1527 if (bits_to_register & 1) {
1528 printk(MPT2SAS_INFO_FMT "registering trace buffer support\n",
1529 ioc->name);
1530 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
1531 /* register for 1MB buffers */
1532 diag_register.requested_buffer_size = (1024 * 1024);
1533 diag_register.unique_id = 0x7075900;
1534 _ctl_diag_register_2(ioc, &diag_register);
1535 }
1536
1537 if (bits_to_register & 2) {
1538 printk(MPT2SAS_INFO_FMT "registering snapshot buffer support\n",
1539 ioc->name);
1540 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_SNAPSHOT;
1541 /* register for 2MB buffers */
1542 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1543 diag_register.unique_id = 0x7075901;
1544 _ctl_diag_register_2(ioc, &diag_register);
1545 }
Kashyap, Desai1b01fe32009-09-23 17:28:59 +05301546
1547 if (bits_to_register & 4) {
1548 printk(MPT2SAS_INFO_FMT "registering extended buffer support\n",
1549 ioc->name);
1550 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_EXTENDED;
1551 /* register for 2MB buffers */
1552 diag_register.requested_buffer_size = 2 * (1024 * 1024);
1553 diag_register.unique_id = 0x7075901;
1554 _ctl_diag_register_2(ioc, &diag_register);
1555 }
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301556}
1557
1558/**
1559 * _ctl_diag_register - application register with driver
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301560 * @ioc: per adapter object
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301561 * @arg - user space buffer containing ioctl content
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301562 *
1563 * This will allow the driver to setup any required buffers that will be
1564 * needed by firmware to communicate with the driver.
1565 */
1566static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301567_ctl_diag_register(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301568{
1569 struct mpt2_diag_register karg;
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301570 long rc;
1571
1572 if (copy_from_user(&karg, arg, sizeof(karg))) {
1573 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1574 __FILE__, __LINE__, __func__);
1575 return -EFAULT;
1576 }
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301577
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05301578 rc = _ctl_diag_register_2(ioc, &karg);
Eric Moore635374e2009-03-09 01:21:12 -06001579 return rc;
1580}
1581
1582/**
1583 * _ctl_diag_unregister - application unregister with driver
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301584 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06001585 * @arg - user space buffer containing ioctl content
1586 *
1587 * This will allow the driver to cleanup any memory allocated for diag
1588 * messages and to free up any resources.
1589 */
1590static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301591_ctl_diag_unregister(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
Eric Moore635374e2009-03-09 01:21:12 -06001592{
1593 struct mpt2_diag_unregister karg;
Eric Moore635374e2009-03-09 01:21:12 -06001594 void *request_data;
1595 dma_addr_t request_data_dma;
1596 u32 request_data_sz;
1597 u8 buffer_type;
1598
1599 if (copy_from_user(&karg, arg, sizeof(karg))) {
1600 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1601 __FILE__, __LINE__, __func__);
1602 return -EFAULT;
1603 }
Eric Moore635374e2009-03-09 01:21:12 -06001604
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301605 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001606 __func__));
1607
1608 buffer_type = karg.unique_id & 0x000000ff;
1609 if (!_ctl_diag_capability(ioc, buffer_type)) {
1610 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1611 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1612 return -EPERM;
1613 }
1614
1615 if ((ioc->diag_buffer_status[buffer_type] &
1616 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1617 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1618 "registered\n", ioc->name, __func__, buffer_type);
1619 return -EINVAL;
1620 }
1621 if ((ioc->diag_buffer_status[buffer_type] &
1622 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
1623 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) has not been "
1624 "released\n", ioc->name, __func__, buffer_type);
1625 return -EINVAL;
1626 }
1627
1628 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1629 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1630 "registered\n", ioc->name, __func__, karg.unique_id);
1631 return -EINVAL;
1632 }
1633
1634 request_data = ioc->diag_buffer[buffer_type];
1635 if (!request_data) {
1636 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1637 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1638 return -ENOMEM;
1639 }
1640
1641 request_data_sz = ioc->diag_buffer_sz[buffer_type];
1642 request_data_dma = ioc->diag_buffer_dma[buffer_type];
1643 pci_free_consistent(ioc->pdev, request_data_sz,
1644 request_data, request_data_dma);
1645 ioc->diag_buffer[buffer_type] = NULL;
1646 ioc->diag_buffer_status[buffer_type] = 0;
1647 return 0;
1648}
1649
1650/**
1651 * _ctl_diag_query - query relevant info associated with diag buffers
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301652 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06001653 * @arg - user space buffer containing ioctl content
1654 *
1655 * The application will send only buffer_type and unique_id. Driver will
1656 * inspect unique_id first, if valid, fill in all the info. If unique_id is
1657 * 0x00, the driver will return info specified by Buffer Type.
1658 */
1659static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301660_ctl_diag_query(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
Eric Moore635374e2009-03-09 01:21:12 -06001661{
1662 struct mpt2_diag_query karg;
Eric Moore635374e2009-03-09 01:21:12 -06001663 void *request_data;
1664 int i;
1665 u8 buffer_type;
1666
1667 if (copy_from_user(&karg, arg, sizeof(karg))) {
1668 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1669 __FILE__, __LINE__, __func__);
1670 return -EFAULT;
1671 }
Eric Moore635374e2009-03-09 01:21:12 -06001672
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301673 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001674 __func__));
1675
1676 karg.application_flags = 0;
1677 buffer_type = karg.buffer_type;
1678
1679 if (!_ctl_diag_capability(ioc, buffer_type)) {
1680 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1681 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1682 return -EPERM;
1683 }
1684
1685 if ((ioc->diag_buffer_status[buffer_type] &
1686 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1687 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1688 "registered\n", ioc->name, __func__, buffer_type);
1689 return -EINVAL;
1690 }
1691
1692 if (karg.unique_id & 0xffffff00) {
1693 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1694 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1695 "registered\n", ioc->name, __func__,
1696 karg.unique_id);
1697 return -EINVAL;
1698 }
1699 }
1700
1701 request_data = ioc->diag_buffer[buffer_type];
1702 if (!request_data) {
1703 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1704 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1705 return -ENOMEM;
1706 }
1707
1708 if (ioc->diag_buffer_status[buffer_type] & MPT2_DIAG_BUFFER_IS_RELEASED)
1709 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1710 MPT2_APP_FLAGS_BUFFER_VALID);
1711 else
1712 karg.application_flags = (MPT2_APP_FLAGS_APP_OWNED |
1713 MPT2_APP_FLAGS_BUFFER_VALID |
1714 MPT2_APP_FLAGS_FW_BUFFER_ACCESS);
1715
1716 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
1717 karg.product_specific[i] =
1718 ioc->product_specific[buffer_type][i];
1719
1720 karg.total_buffer_size = ioc->diag_buffer_sz[buffer_type];
1721 karg.driver_added_buffer_size = 0;
1722 karg.unique_id = ioc->unique_id[buffer_type];
1723 karg.diagnostic_flags = ioc->diagnostic_flags[buffer_type];
1724
1725 if (copy_to_user(arg, &karg, sizeof(struct mpt2_diag_query))) {
1726 printk(MPT2SAS_ERR_FMT "%s: unable to write mpt2_diag_query "
1727 "data @ %p\n", ioc->name, __func__, arg);
1728 return -EFAULT;
1729 }
1730 return 0;
1731}
1732
1733/**
Eric Moore99bb2142009-04-21 15:42:13 -06001734 * _ctl_send_release - Diag Release Message
1735 * @ioc: per adapter object
Kashyap, Desai1b01fe32009-09-23 17:28:59 +05301736 * @buffer_type - specifies either TRACE, SNAPSHOT, or EXTENDED
Eric Moore99bb2142009-04-21 15:42:13 -06001737 * @issue_reset - specifies whether host reset is required.
1738 *
1739 */
1740static int
1741_ctl_send_release(struct MPT2SAS_ADAPTER *ioc, u8 buffer_type, u8 *issue_reset)
1742{
1743 Mpi2DiagReleaseRequest_t *mpi_request;
1744 Mpi2DiagReleaseReply_t *mpi_reply;
1745 u16 smid;
1746 u16 ioc_status;
1747 u32 ioc_state;
1748 int rc;
1749 unsigned long timeleft;
1750
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301751 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore99bb2142009-04-21 15:42:13 -06001752 __func__));
1753
1754 rc = 0;
1755 *issue_reset = 0;
1756
1757 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
1758 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301759 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore99bb2142009-04-21 15:42:13 -06001760 "skipping due to FAULT state\n", ioc->name,
1761 __func__));
1762 rc = -EAGAIN;
1763 goto out;
1764 }
1765
1766 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
1767 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
1768 ioc->name, __func__);
1769 rc = -EAGAIN;
1770 goto out;
1771 }
1772
1773 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
1774 if (!smid) {
1775 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
1776 ioc->name, __func__);
1777 rc = -EAGAIN;
1778 goto out;
1779 }
1780
1781 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
1782 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
1783 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1784 ioc->ctl_cmds.smid = smid;
1785
1786 mpi_request->Function = MPI2_FUNCTION_DIAG_RELEASE;
1787 mpi_request->BufferType = buffer_type;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301788 mpi_request->VF_ID = 0; /* TODO */
1789 mpi_request->VP_ID = 0;
Eric Moore99bb2142009-04-21 15:42:13 -06001790
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05301791 init_completion(&ioc->ctl_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05301792 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore99bb2142009-04-21 15:42:13 -06001793 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
1794 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
1795
1796 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
1797 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
1798 __func__);
1799 _debug_dump_mf(mpi_request,
1800 sizeof(Mpi2DiagReleaseRequest_t)/4);
1801 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
1802 *issue_reset = 1;
1803 rc = -EFAULT;
1804 goto out;
1805 }
1806
1807 /* process the completed Reply Message Frame */
1808 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
1809 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
1810 ioc->name, __func__);
1811 rc = -EFAULT;
1812 goto out;
1813 }
1814
1815 mpi_reply = ioc->ctl_cmds.reply;
1816 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
1817
1818 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
1819 ioc->diag_buffer_status[buffer_type] |=
1820 MPT2_DIAG_BUFFER_IS_RELEASED;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301821 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
Eric Moore99bb2142009-04-21 15:42:13 -06001822 ioc->name, __func__));
1823 } else {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301824 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
Eric Moore99bb2142009-04-21 15:42:13 -06001825 "log_info(0x%08x)\n", ioc->name, __func__,
Kashyap, Desai463217b2009-10-05 15:53:06 +05301826 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Eric Moore99bb2142009-04-21 15:42:13 -06001827 rc = -EFAULT;
1828 }
1829
1830 out:
1831 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
1832 return rc;
1833}
1834
1835/**
Eric Moore635374e2009-03-09 01:21:12 -06001836 * _ctl_diag_release - request to send Diag Release Message to firmware
1837 * @arg - user space buffer containing ioctl content
Eric Moore635374e2009-03-09 01:21:12 -06001838 *
1839 * This allows ownership of the specified buffer to returned to the driver,
1840 * allowing an application to read the buffer without fear that firmware is
1841 * overwritting information in the buffer.
1842 */
1843static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301844_ctl_diag_release(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
Eric Moore635374e2009-03-09 01:21:12 -06001845{
1846 struct mpt2_diag_release karg;
Eric Moore635374e2009-03-09 01:21:12 -06001847 void *request_data;
1848 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06001849 u8 buffer_type;
Eric Moore635374e2009-03-09 01:21:12 -06001850 u8 issue_reset = 0;
1851
1852 if (copy_from_user(&karg, arg, sizeof(karg))) {
1853 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1854 __FILE__, __LINE__, __func__);
1855 return -EFAULT;
1856 }
Eric Moore635374e2009-03-09 01:21:12 -06001857
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301858 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001859 __func__));
1860
1861 buffer_type = karg.unique_id & 0x000000ff;
1862 if (!_ctl_diag_capability(ioc, buffer_type)) {
1863 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1864 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1865 return -EPERM;
1866 }
1867
1868 if ((ioc->diag_buffer_status[buffer_type] &
1869 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
1870 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) is not "
1871 "registered\n", ioc->name, __func__, buffer_type);
1872 return -EINVAL;
1873 }
1874
1875 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1876 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1877 "registered\n", ioc->name, __func__, karg.unique_id);
1878 return -EINVAL;
1879 }
1880
1881 if (ioc->diag_buffer_status[buffer_type] &
1882 MPT2_DIAG_BUFFER_IS_RELEASED) {
1883 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1884 "is already released\n", ioc->name, __func__,
1885 buffer_type);
1886 return 0;
1887 }
1888
1889 request_data = ioc->diag_buffer[buffer_type];
1890
1891 if (!request_data) {
1892 printk(MPT2SAS_ERR_FMT "%s: doesn't have memory allocated for "
1893 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1894 return -ENOMEM;
1895 }
1896
Eric Moore99bb2142009-04-21 15:42:13 -06001897 /* buffers were released by due to host reset */
1898 if ((ioc->diag_buffer_status[buffer_type] &
1899 MPT2_DIAG_BUFFER_IS_DIAG_RESET)) {
1900 ioc->diag_buffer_status[buffer_type] |=
1901 MPT2_DIAG_BUFFER_IS_RELEASED;
1902 ioc->diag_buffer_status[buffer_type] &=
1903 ~MPT2_DIAG_BUFFER_IS_DIAG_RESET;
1904 printk(MPT2SAS_ERR_FMT "%s: buffer_type(0x%02x) "
1905 "was released due to host reset\n", ioc->name, __func__,
1906 buffer_type);
1907 return 0;
1908 }
1909
Eric Moore99bb2142009-04-21 15:42:13 -06001910 rc = _ctl_send_release(ioc, buffer_type, &issue_reset);
Eric Moore635374e2009-03-09 01:21:12 -06001911
Eric Moore635374e2009-03-09 01:21:12 -06001912 if (issue_reset)
1913 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
1914 FORCE_BIG_HAMMER);
1915
Eric Moore635374e2009-03-09 01:21:12 -06001916 return rc;
1917}
1918
1919/**
1920 * _ctl_diag_read_buffer - request for copy of the diag buffer
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301921 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06001922 * @arg - user space buffer containing ioctl content
Eric Moore635374e2009-03-09 01:21:12 -06001923 */
1924static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05301925_ctl_diag_read_buffer(struct MPT2SAS_ADAPTER *ioc, void __user *arg)
Eric Moore635374e2009-03-09 01:21:12 -06001926{
1927 struct mpt2_diag_read_buffer karg;
1928 struct mpt2_diag_read_buffer __user *uarg = arg;
Eric Moore635374e2009-03-09 01:21:12 -06001929 void *request_data, *diag_data;
1930 Mpi2DiagBufferPostRequest_t *mpi_request;
1931 Mpi2DiagBufferPostReply_t *mpi_reply;
1932 int rc, i;
1933 u8 buffer_type;
Dan Rosenberga1f74ae2011-04-05 12:45:59 -04001934 unsigned long timeleft, request_size, copy_size;
Eric Moore635374e2009-03-09 01:21:12 -06001935 u16 smid;
1936 u16 ioc_status;
1937 u8 issue_reset = 0;
1938
1939 if (copy_from_user(&karg, arg, sizeof(karg))) {
1940 printk(KERN_ERR "failure at %s:%d/%s()!\n",
1941 __FILE__, __LINE__, __func__);
1942 return -EFAULT;
1943 }
Eric Moore635374e2009-03-09 01:21:12 -06001944
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301945 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001946 __func__));
1947
1948 buffer_type = karg.unique_id & 0x000000ff;
1949 if (!_ctl_diag_capability(ioc, buffer_type)) {
1950 printk(MPT2SAS_ERR_FMT "%s: doesn't have capability for "
1951 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1952 return -EPERM;
1953 }
1954
1955 if (karg.unique_id != ioc->unique_id[buffer_type]) {
1956 printk(MPT2SAS_ERR_FMT "%s: unique_id(0x%08x) is not "
1957 "registered\n", ioc->name, __func__, karg.unique_id);
1958 return -EINVAL;
1959 }
1960
1961 request_data = ioc->diag_buffer[buffer_type];
1962 if (!request_data) {
1963 printk(MPT2SAS_ERR_FMT "%s: doesn't have buffer for "
1964 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type);
1965 return -ENOMEM;
1966 }
1967
Dan Rosenberga1f74ae2011-04-05 12:45:59 -04001968 request_size = ioc->diag_buffer_sz[buffer_type];
1969
Eric Moore635374e2009-03-09 01:21:12 -06001970 if ((karg.starting_offset % 4) || (karg.bytes_to_read % 4)) {
1971 printk(MPT2SAS_ERR_FMT "%s: either the starting_offset "
1972 "or bytes_to_read are not 4 byte aligned\n", ioc->name,
1973 __func__);
1974 return -EINVAL;
1975 }
1976
Dan Rosenberga1f74ae2011-04-05 12:45:59 -04001977 if (karg.starting_offset > request_size)
1978 return -EINVAL;
1979
Eric Moore635374e2009-03-09 01:21:12 -06001980 diag_data = (void *)(request_data + karg.starting_offset);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301981 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: diag_buffer(%p), "
Eric Moore635374e2009-03-09 01:21:12 -06001982 "offset(%d), sz(%d)\n", ioc->name, __func__,
1983 diag_data, karg.starting_offset, karg.bytes_to_read));
1984
Dan Rosenberga1f74ae2011-04-05 12:45:59 -04001985 /* Truncate data on requests that are too large */
1986 if ((diag_data + karg.bytes_to_read < diag_data) ||
1987 (diag_data + karg.bytes_to_read > request_data + request_size))
1988 copy_size = request_size - karg.starting_offset;
1989 else
1990 copy_size = karg.bytes_to_read;
1991
Eric Moore635374e2009-03-09 01:21:12 -06001992 if (copy_to_user((void __user *)uarg->diagnostic_data,
Dan Rosenberga1f74ae2011-04-05 12:45:59 -04001993 diag_data, copy_size)) {
Eric Moore635374e2009-03-09 01:21:12 -06001994 printk(MPT2SAS_ERR_FMT "%s: Unable to write "
1995 "mpt_diag_read_buffer_t data @ %p\n", ioc->name,
1996 __func__, diag_data);
1997 return -EFAULT;
1998 }
1999
2000 if ((karg.flags & MPT2_FLAGS_REREGISTER) == 0)
2001 return 0;
2002
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302003 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: Reregister "
Eric Moore635374e2009-03-09 01:21:12 -06002004 "buffer_type(0x%02x)\n", ioc->name, __func__, buffer_type));
2005 if ((ioc->diag_buffer_status[buffer_type] &
2006 MPT2_DIAG_BUFFER_IS_RELEASED) == 0) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302007 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06002008 "buffer_type(0x%02x) is still registered\n", ioc->name,
2009 __func__, buffer_type));
2010 return 0;
2011 }
2012 /* Get a free request frame and save the message context.
2013 */
Eric Moore635374e2009-03-09 01:21:12 -06002014
2015 if (ioc->ctl_cmds.status != MPT2_CMD_NOT_USED) {
2016 printk(MPT2SAS_ERR_FMT "%s: ctl_cmd in use\n",
2017 ioc->name, __func__);
2018 rc = -EAGAIN;
2019 goto out;
2020 }
2021
2022 smid = mpt2sas_base_get_smid(ioc, ioc->ctl_cb_idx);
2023 if (!smid) {
2024 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2025 ioc->name, __func__);
2026 rc = -EAGAIN;
2027 goto out;
2028 }
2029
2030 rc = 0;
2031 ioc->ctl_cmds.status = MPT2_CMD_PENDING;
2032 memset(ioc->ctl_cmds.reply, 0, ioc->reply_sz);
2033 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2034 ioc->ctl_cmds.smid = smid;
2035
2036 mpi_request->Function = MPI2_FUNCTION_DIAG_BUFFER_POST;
2037 mpi_request->BufferType = buffer_type;
2038 mpi_request->BufferLength =
2039 cpu_to_le32(ioc->diag_buffer_sz[buffer_type]);
2040 mpi_request->BufferAddress =
2041 cpu_to_le64(ioc->diag_buffer_dma[buffer_type]);
2042 for (i = 0; i < MPT2_PRODUCT_SPECIFIC_DWORDS; i++)
2043 mpi_request->ProductSpecific[i] =
2044 cpu_to_le32(ioc->product_specific[buffer_type][i]);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302045 mpi_request->VF_ID = 0; /* TODO */
2046 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06002047
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05302048 init_completion(&ioc->ctl_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05302049 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002050 timeleft = wait_for_completion_timeout(&ioc->ctl_cmds.done,
2051 MPT2_IOCTL_DEFAULT_TIMEOUT*HZ);
2052
2053 if (!(ioc->ctl_cmds.status & MPT2_CMD_COMPLETE)) {
2054 printk(MPT2SAS_ERR_FMT "%s: timeout\n", ioc->name,
2055 __func__);
2056 _debug_dump_mf(mpi_request,
2057 sizeof(Mpi2DiagBufferPostRequest_t)/4);
2058 if (!(ioc->ctl_cmds.status & MPT2_CMD_RESET))
2059 issue_reset = 1;
2060 goto issue_host_reset;
2061 }
2062
2063 /* process the completed Reply Message Frame */
2064 if ((ioc->ctl_cmds.status & MPT2_CMD_REPLY_VALID) == 0) {
2065 printk(MPT2SAS_ERR_FMT "%s: no reply message\n",
2066 ioc->name, __func__);
2067 rc = -EFAULT;
2068 goto out;
2069 }
2070
2071 mpi_reply = ioc->ctl_cmds.reply;
2072 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
2073
2074 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
2075 ioc->diag_buffer_status[buffer_type] |=
2076 MPT2_DIAG_BUFFER_IS_REGISTERED;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302077 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: success\n",
Eric Moore635374e2009-03-09 01:21:12 -06002078 ioc->name, __func__));
2079 } else {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302080 printk(MPT2SAS_INFO_FMT "%s: ioc_status(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06002081 "log_info(0x%08x)\n", ioc->name, __func__,
Kashyap, Desai463217b2009-10-05 15:53:06 +05302082 ioc_status, le32_to_cpu(mpi_reply->IOCLogInfo));
Eric Moore635374e2009-03-09 01:21:12 -06002083 rc = -EFAULT;
2084 }
2085
2086 issue_host_reset:
2087 if (issue_reset)
2088 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2089 FORCE_BIG_HAMMER);
2090
2091 out:
2092
2093 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
Eric Moore635374e2009-03-09 01:21:12 -06002094 return rc;
2095}
2096
Eric Moore635374e2009-03-09 01:21:12 -06002097
2098#ifdef CONFIG_COMPAT
2099/**
2100 * _ctl_compat_mpt_command - convert 32bit pointers to 64bit.
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05302101 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002102 * @cmd - ioctl opcode
2103 * @arg - (struct mpt2_ioctl_command32)
2104 *
2105 * MPT2COMMAND32 - Handle 32bit applications running on 64bit os.
2106 */
2107static long
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05302108_ctl_compat_mpt_command(struct MPT2SAS_ADAPTER *ioc, unsigned cmd,
2109 void __user *arg)
Eric Moore635374e2009-03-09 01:21:12 -06002110{
2111 struct mpt2_ioctl_command32 karg32;
2112 struct mpt2_ioctl_command32 __user *uarg;
2113 struct mpt2_ioctl_command karg;
Eric Moore635374e2009-03-09 01:21:12 -06002114
2115 if (_IOC_SIZE(cmd) != sizeof(struct mpt2_ioctl_command32))
2116 return -EINVAL;
2117
2118 uarg = (struct mpt2_ioctl_command32 __user *) arg;
2119
2120 if (copy_from_user(&karg32, (char __user *)arg, sizeof(karg32))) {
2121 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2122 __FILE__, __LINE__, __func__);
2123 return -EFAULT;
2124 }
Eric Moore635374e2009-03-09 01:21:12 -06002125
2126 memset(&karg, 0, sizeof(struct mpt2_ioctl_command));
2127 karg.hdr.ioc_number = karg32.hdr.ioc_number;
2128 karg.hdr.port_number = karg32.hdr.port_number;
2129 karg.hdr.max_data_size = karg32.hdr.max_data_size;
2130 karg.timeout = karg32.timeout;
2131 karg.max_reply_bytes = karg32.max_reply_bytes;
2132 karg.data_in_size = karg32.data_in_size;
2133 karg.data_out_size = karg32.data_out_size;
2134 karg.max_sense_bytes = karg32.max_sense_bytes;
2135 karg.data_sge_offset = karg32.data_sge_offset;
Kashyap, Desai22c88422009-12-16 18:53:04 +05302136 karg.reply_frame_buf_ptr = compat_ptr(karg32.reply_frame_buf_ptr);
2137 karg.data_in_buf_ptr = compat_ptr(karg32.data_in_buf_ptr);
2138 karg.data_out_buf_ptr = compat_ptr(karg32.data_out_buf_ptr);
2139 karg.sense_data_ptr = compat_ptr(karg32.sense_data_ptr);
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05302140 return _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2141}
2142#endif
2143
2144/**
2145 * _ctl_ioctl_main - main ioctl entry point
2146 * @file - (struct file)
2147 * @cmd - ioctl opcode
2148 * @arg -
2149 * compat - handles 32 bit applications in 64bit os
2150 */
2151static long
2152_ctl_ioctl_main(struct file *file, unsigned int cmd, void __user *arg,
2153 u8 compat)
2154{
2155 struct MPT2SAS_ADAPTER *ioc;
2156 struct mpt2_ioctl_header ioctl_header;
2157 enum block_state state;
2158 long ret = -EINVAL;
2159
2160 /* get IOCTL header */
2161 if (copy_from_user(&ioctl_header, (char __user *)arg,
2162 sizeof(struct mpt2_ioctl_header))) {
2163 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2164 __FILE__, __LINE__, __func__);
2165 return -EFAULT;
2166 }
2167
2168 if (_ctl_verify_adapter(ioctl_header.ioc_number, &ioc) == -1 || !ioc)
2169 return -ENODEV;
2170 if (ioc->shost_recovery || ioc->pci_error_recovery ||
2171 ioc->is_driver_loading)
2172 return -EAGAIN;
2173
Eric Moore635374e2009-03-09 01:21:12 -06002174 state = (file->f_flags & O_NONBLOCK) ? NON_BLOCKING : BLOCKING;
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05302175 if (state == NON_BLOCKING && !mutex_trylock(&ioc->ctl_cmds.mutex))
2176 return -EAGAIN;
2177 else if (mutex_lock_interruptible(&ioc->ctl_cmds.mutex))
2178 return -ERESTARTSYS;
2179
2180 switch (cmd) {
2181 case MPT2IOCINFO:
2182 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_iocinfo))
2183 ret = _ctl_getiocinfo(ioc, arg);
2184 break;
2185#ifdef CONFIG_COMPAT
2186 case MPT2COMMAND32:
2187#endif
2188 case MPT2COMMAND:
2189 {
2190 struct mpt2_ioctl_command __user *uarg;
2191 struct mpt2_ioctl_command karg;
2192#ifdef CONFIG_COMPAT
2193 if (compat) {
2194 ret = _ctl_compat_mpt_command(ioc, cmd, arg);
2195 break;
2196 }
2197#endif
2198 if (copy_from_user(&karg, arg, sizeof(karg))) {
2199 printk(KERN_ERR "failure at %s:%d/%s()!\n",
2200 __FILE__, __LINE__, __func__);
2201 ret = -EFAULT;
2202 break;
2203 }
2204
2205 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_command)) {
2206 uarg = arg;
2207 ret = _ctl_do_mpt_command(ioc, karg, &uarg->mf);
2208 }
2209 break;
2210 }
2211 case MPT2EVENTQUERY:
2212 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventquery))
2213 ret = _ctl_eventquery(ioc, arg);
2214 break;
2215 case MPT2EVENTENABLE:
2216 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_eventenable))
2217 ret = _ctl_eventenable(ioc, arg);
2218 break;
2219 case MPT2EVENTREPORT:
2220 ret = _ctl_eventreport(ioc, arg);
2221 break;
2222 case MPT2HARDRESET:
2223 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_diag_reset))
2224 ret = _ctl_do_reset(ioc, arg);
2225 break;
2226 case MPT2BTDHMAPPING:
2227 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_ioctl_btdh_mapping))
2228 ret = _ctl_btdh_mapping(ioc, arg);
2229 break;
2230 case MPT2DIAGREGISTER:
2231 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_register))
2232 ret = _ctl_diag_register(ioc, arg);
2233 break;
2234 case MPT2DIAGUNREGISTER:
2235 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_unregister))
2236 ret = _ctl_diag_unregister(ioc, arg);
2237 break;
2238 case MPT2DIAGQUERY:
2239 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_query))
2240 ret = _ctl_diag_query(ioc, arg);
2241 break;
2242 case MPT2DIAGRELEASE:
2243 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_release))
2244 ret = _ctl_diag_release(ioc, arg);
2245 break;
2246 case MPT2DIAGREADBUFFER:
2247 if (_IOC_SIZE(cmd) == sizeof(struct mpt2_diag_read_buffer))
2248 ret = _ctl_diag_read_buffer(ioc, arg);
2249 break;
2250 default:
2251
2252 dctlprintk(ioc, printk(MPT2SAS_INFO_FMT
2253 "unsupported ioctl opcode(0x%08x)\n", ioc->name, cmd));
2254 break;
2255 }
2256
2257 mutex_unlock(&ioc->ctl_cmds.mutex);
2258 return ret;
Eric Moore635374e2009-03-09 01:21:12 -06002259}
2260
2261/**
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05302262 * _ctl_ioctl - main ioctl entry point (unlocked)
2263 * @file - (struct file)
2264 * @cmd - ioctl opcode
2265 * @arg -
2266 */
2267static long
2268_ctl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2269{
2270 long ret;
2271
2272 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 0);
2273 return ret;
2274}
2275#ifdef CONFIG_COMPAT
2276/**
Eric Moore635374e2009-03-09 01:21:12 -06002277 * _ctl_ioctl_compat - main ioctl entry point (compat)
2278 * @file -
2279 * @cmd -
2280 * @arg -
2281 *
2282 * This routine handles 32 bit applications in 64bit os.
2283 */
2284static long
2285_ctl_ioctl_compat(struct file *file, unsigned cmd, unsigned long arg)
2286{
2287 long ret;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302288
nagalakshmi.nandigama@lsi.com913809f2012-03-20 12:04:11 +05302289 ret = _ctl_ioctl_main(file, cmd, (void __user *)arg, 1);
Eric Moore635374e2009-03-09 01:21:12 -06002290 return ret;
2291}
2292#endif
2293
2294/* scsi host attributes */
2295
2296/**
2297 * _ctl_version_fw_show - firmware version
2298 * @cdev - pointer to embedded class device
2299 * @buf - the buffer returned
2300 *
2301 * A sysfs 'read-only' shost attribute.
2302 */
2303static ssize_t
2304_ctl_version_fw_show(struct device *cdev, struct device_attribute *attr,
2305 char *buf)
2306{
2307 struct Scsi_Host *shost = class_to_shost(cdev);
2308 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2309
2310 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2311 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2312 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2313 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2314 ioc->facts.FWVersion.Word & 0x000000FF);
2315}
2316static DEVICE_ATTR(version_fw, S_IRUGO, _ctl_version_fw_show, NULL);
2317
2318/**
2319 * _ctl_version_bios_show - bios version
2320 * @cdev - pointer to embedded class device
2321 * @buf - the buffer returned
2322 *
2323 * A sysfs 'read-only' shost attribute.
2324 */
2325static ssize_t
2326_ctl_version_bios_show(struct device *cdev, struct device_attribute *attr,
2327 char *buf)
2328{
2329 struct Scsi_Host *shost = class_to_shost(cdev);
2330 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2331
2332 u32 version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2333
2334 return snprintf(buf, PAGE_SIZE, "%02d.%02d.%02d.%02d\n",
2335 (version & 0xFF000000) >> 24,
2336 (version & 0x00FF0000) >> 16,
2337 (version & 0x0000FF00) >> 8,
2338 version & 0x000000FF);
2339}
2340static DEVICE_ATTR(version_bios, S_IRUGO, _ctl_version_bios_show, NULL);
2341
2342/**
2343 * _ctl_version_mpi_show - MPI (message passing interface) version
2344 * @cdev - pointer to embedded class device
2345 * @buf - the buffer returned
2346 *
2347 * A sysfs 'read-only' shost attribute.
2348 */
2349static ssize_t
2350_ctl_version_mpi_show(struct device *cdev, struct device_attribute *attr,
2351 char *buf)
2352{
2353 struct Scsi_Host *shost = class_to_shost(cdev);
2354 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2355
2356 return snprintf(buf, PAGE_SIZE, "%03x.%02x\n",
2357 ioc->facts.MsgVersion, ioc->facts.HeaderVersion >> 8);
2358}
2359static DEVICE_ATTR(version_mpi, S_IRUGO, _ctl_version_mpi_show, NULL);
2360
2361/**
2362 * _ctl_version_product_show - product name
2363 * @cdev - pointer to embedded class device
2364 * @buf - the buffer returned
2365 *
2366 * A sysfs 'read-only' shost attribute.
2367 */
2368static ssize_t
2369_ctl_version_product_show(struct device *cdev, struct device_attribute *attr,
2370 char *buf)
2371{
2372 struct Scsi_Host *shost = class_to_shost(cdev);
2373 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2374
2375 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.ChipName);
2376}
2377static DEVICE_ATTR(version_product, S_IRUGO,
2378 _ctl_version_product_show, NULL);
2379
2380/**
2381 * _ctl_version_nvdata_persistent_show - ndvata persistent version
2382 * @cdev - pointer to embedded class device
2383 * @buf - the buffer returned
2384 *
2385 * A sysfs 'read-only' shost attribute.
2386 */
2387static ssize_t
2388_ctl_version_nvdata_persistent_show(struct device *cdev,
2389 struct device_attribute *attr, char *buf)
2390{
2391 struct Scsi_Host *shost = class_to_shost(cdev);
2392 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2393
Kashyap, Desaie94f6742010-03-17 16:24:52 +05302394 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2395 le32_to_cpu(ioc->iounit_pg0.NvdataVersionPersistent.Word));
Eric Moore635374e2009-03-09 01:21:12 -06002396}
2397static DEVICE_ATTR(version_nvdata_persistent, S_IRUGO,
2398 _ctl_version_nvdata_persistent_show, NULL);
2399
2400/**
2401 * _ctl_version_nvdata_default_show - nvdata default version
2402 * @cdev - pointer to embedded class device
2403 * @buf - the buffer returned
2404 *
2405 * A sysfs 'read-only' shost attribute.
2406 */
2407static ssize_t
2408_ctl_version_nvdata_default_show(struct device *cdev,
2409 struct device_attribute *attr, char *buf)
2410{
2411 struct Scsi_Host *shost = class_to_shost(cdev);
2412 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2413
Kashyap, Desaie94f6742010-03-17 16:24:52 +05302414 return snprintf(buf, PAGE_SIZE, "%08xh\n",
2415 le32_to_cpu(ioc->iounit_pg0.NvdataVersionDefault.Word));
Eric Moore635374e2009-03-09 01:21:12 -06002416}
2417static DEVICE_ATTR(version_nvdata_default, S_IRUGO,
2418 _ctl_version_nvdata_default_show, NULL);
2419
2420/**
2421 * _ctl_board_name_show - board name
2422 * @cdev - pointer to embedded class device
2423 * @buf - the buffer returned
2424 *
2425 * A sysfs 'read-only' shost attribute.
2426 */
2427static ssize_t
2428_ctl_board_name_show(struct device *cdev, struct device_attribute *attr,
2429 char *buf)
2430{
2431 struct Scsi_Host *shost = class_to_shost(cdev);
2432 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2433
2434 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardName);
2435}
2436static DEVICE_ATTR(board_name, S_IRUGO, _ctl_board_name_show, NULL);
2437
2438/**
2439 * _ctl_board_assembly_show - board assembly name
2440 * @cdev - pointer to embedded class device
2441 * @buf - the buffer returned
2442 *
2443 * A sysfs 'read-only' shost attribute.
2444 */
2445static ssize_t
2446_ctl_board_assembly_show(struct device *cdev, struct device_attribute *attr,
2447 char *buf)
2448{
2449 struct Scsi_Host *shost = class_to_shost(cdev);
2450 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2451
2452 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardAssembly);
2453}
2454static DEVICE_ATTR(board_assembly, S_IRUGO,
2455 _ctl_board_assembly_show, NULL);
2456
2457/**
2458 * _ctl_board_tracer_show - board tracer number
2459 * @cdev - pointer to embedded class device
2460 * @buf - the buffer returned
2461 *
2462 * A sysfs 'read-only' shost attribute.
2463 */
2464static ssize_t
2465_ctl_board_tracer_show(struct device *cdev, struct device_attribute *attr,
2466 char *buf)
2467{
2468 struct Scsi_Host *shost = class_to_shost(cdev);
2469 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2470
2471 return snprintf(buf, 16, "%s\n", ioc->manu_pg0.BoardTracerNumber);
2472}
2473static DEVICE_ATTR(board_tracer, S_IRUGO,
2474 _ctl_board_tracer_show, NULL);
2475
2476/**
2477 * _ctl_io_delay_show - io missing delay
2478 * @cdev - pointer to embedded class device
2479 * @buf - the buffer returned
2480 *
2481 * This is for firmware implemention for deboucing device
2482 * removal events.
2483 *
2484 * A sysfs 'read-only' shost attribute.
2485 */
2486static ssize_t
2487_ctl_io_delay_show(struct device *cdev, struct device_attribute *attr,
2488 char *buf)
2489{
2490 struct Scsi_Host *shost = class_to_shost(cdev);
2491 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2492
2493 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->io_missing_delay);
2494}
2495static DEVICE_ATTR(io_delay, S_IRUGO,
2496 _ctl_io_delay_show, NULL);
2497
2498/**
2499 * _ctl_device_delay_show - device missing delay
2500 * @cdev - pointer to embedded class device
2501 * @buf - the buffer returned
2502 *
2503 * This is for firmware implemention for deboucing device
2504 * removal events.
2505 *
2506 * A sysfs 'read-only' shost attribute.
2507 */
2508static ssize_t
2509_ctl_device_delay_show(struct device *cdev, struct device_attribute *attr,
2510 char *buf)
2511{
2512 struct Scsi_Host *shost = class_to_shost(cdev);
2513 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2514
2515 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->device_missing_delay);
2516}
2517static DEVICE_ATTR(device_delay, S_IRUGO,
2518 _ctl_device_delay_show, NULL);
2519
2520/**
2521 * _ctl_fw_queue_depth_show - global credits
2522 * @cdev - pointer to embedded class device
2523 * @buf - the buffer returned
2524 *
2525 * This is firmware queue depth limit
2526 *
2527 * A sysfs 'read-only' shost attribute.
2528 */
2529static ssize_t
2530_ctl_fw_queue_depth_show(struct device *cdev, struct device_attribute *attr,
2531 char *buf)
2532{
2533 struct Scsi_Host *shost = class_to_shost(cdev);
2534 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2535
2536 return snprintf(buf, PAGE_SIZE, "%02d\n", ioc->facts.RequestCredit);
2537}
2538static DEVICE_ATTR(fw_queue_depth, S_IRUGO,
2539 _ctl_fw_queue_depth_show, NULL);
2540
2541/**
2542 * _ctl_sas_address_show - sas address
2543 * @cdev - pointer to embedded class device
2544 * @buf - the buffer returned
2545 *
2546 * This is the controller sas address
2547 *
2548 * A sysfs 'read-only' shost attribute.
2549 */
2550static ssize_t
2551_ctl_host_sas_address_show(struct device *cdev, struct device_attribute *attr,
2552 char *buf)
2553{
2554 struct Scsi_Host *shost = class_to_shost(cdev);
2555 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2556
2557 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2558 (unsigned long long)ioc->sas_hba.sas_address);
2559}
2560static DEVICE_ATTR(host_sas_address, S_IRUGO,
2561 _ctl_host_sas_address_show, NULL);
2562
2563/**
2564 * _ctl_logging_level_show - logging level
2565 * @cdev - pointer to embedded class device
2566 * @buf - the buffer returned
2567 *
2568 * A sysfs 'read/write' shost attribute.
2569 */
2570static ssize_t
2571_ctl_logging_level_show(struct device *cdev, struct device_attribute *attr,
2572 char *buf)
2573{
2574 struct Scsi_Host *shost = class_to_shost(cdev);
2575 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2576
2577 return snprintf(buf, PAGE_SIZE, "%08xh\n", ioc->logging_level);
2578}
2579static ssize_t
2580_ctl_logging_level_store(struct device *cdev, struct device_attribute *attr,
2581 const char *buf, size_t count)
2582{
2583 struct Scsi_Host *shost = class_to_shost(cdev);
2584 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2585 int val = 0;
2586
2587 if (sscanf(buf, "%x", &val) != 1)
2588 return -EINVAL;
2589
2590 ioc->logging_level = val;
2591 printk(MPT2SAS_INFO_FMT "logging_level=%08xh\n", ioc->name,
2592 ioc->logging_level);
2593 return strlen(buf);
2594}
2595static DEVICE_ATTR(logging_level, S_IRUGO | S_IWUSR,
2596 _ctl_logging_level_show, _ctl_logging_level_store);
2597
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302598/* device attributes */
2599/*
2600 * _ctl_fwfault_debug_show - show/store fwfault_debug
2601 * @cdev - pointer to embedded class device
2602 * @buf - the buffer returned
2603 *
2604 * mpt2sas_fwfault_debug is command line option
2605 * A sysfs 'read/write' shost attribute.
2606 */
2607static ssize_t
2608_ctl_fwfault_debug_show(struct device *cdev,
2609 struct device_attribute *attr, char *buf)
2610{
2611 struct Scsi_Host *shost = class_to_shost(cdev);
2612 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2613
2614 return snprintf(buf, PAGE_SIZE, "%d\n", ioc->fwfault_debug);
2615}
2616static ssize_t
2617_ctl_fwfault_debug_store(struct device *cdev,
2618 struct device_attribute *attr, const char *buf, size_t count)
2619{
2620 struct Scsi_Host *shost = class_to_shost(cdev);
2621 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2622 int val = 0;
2623
2624 if (sscanf(buf, "%d", &val) != 1)
2625 return -EINVAL;
2626
2627 ioc->fwfault_debug = val;
2628 printk(MPT2SAS_INFO_FMT "fwfault_debug=%d\n", ioc->name,
2629 ioc->fwfault_debug);
2630 return strlen(buf);
2631}
2632static DEVICE_ATTR(fwfault_debug, S_IRUGO | S_IWUSR,
2633 _ctl_fwfault_debug_show, _ctl_fwfault_debug_store);
2634
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05302635
2636/**
2637 * _ctl_ioc_reset_count_show - ioc reset count
2638 * @cdev - pointer to embedded class device
2639 * @buf - the buffer returned
2640 *
2641 * This is firmware queue depth limit
2642 *
2643 * A sysfs 'read-only' shost attribute.
2644 */
2645static ssize_t
2646_ctl_ioc_reset_count_show(struct device *cdev, struct device_attribute *attr,
2647 char *buf)
2648{
2649 struct Scsi_Host *shost = class_to_shost(cdev);
2650 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2651
2652 return snprintf(buf, PAGE_SIZE, "%08d\n", ioc->ioc_reset_count);
2653}
2654static DEVICE_ATTR(ioc_reset_count, S_IRUGO,
2655 _ctl_ioc_reset_count_show, NULL);
2656
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302657/**
2658 * _ctl_ioc_reply_queue_count_show - number of reply queues
2659 * @cdev - pointer to embedded class device
2660 * @buf - the buffer returned
2661 *
2662 * This is number of reply queues
2663 *
2664 * A sysfs 'read-only' shost attribute.
2665 */
2666static ssize_t
2667_ctl_ioc_reply_queue_count_show(struct device *cdev,
2668 struct device_attribute *attr, char *buf)
2669{
2670 u8 reply_queue_count;
2671 struct Scsi_Host *shost = class_to_shost(cdev);
2672 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2673
2674 if ((ioc->facts.IOCCapabilities &
2675 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable)
2676 reply_queue_count = ioc->reply_queue_count;
2677 else
2678 reply_queue_count = 1;
2679 return snprintf(buf, PAGE_SIZE, "%d\n", reply_queue_count);
2680}
2681static DEVICE_ATTR(reply_queue_count, S_IRUGO,
2682 _ctl_ioc_reply_queue_count_show, NULL);
2683
Kashyap, Desai570c67a2010-06-17 13:43:17 +05302684struct DIAG_BUFFER_START {
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302685 __le32 Size;
2686 __le32 DiagVersion;
Kashyap, Desai570c67a2010-06-17 13:43:17 +05302687 u8 BufferType;
2688 u8 Reserved[3];
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302689 __le32 Reserved1;
2690 __le32 Reserved2;
2691 __le32 Reserved3;
Kashyap, Desai570c67a2010-06-17 13:43:17 +05302692};
2693/**
2694 * _ctl_host_trace_buffer_size_show - host buffer size (trace only)
2695 * @cdev - pointer to embedded class device
2696 * @buf - the buffer returned
2697 *
2698 * A sysfs 'read-only' shost attribute.
2699 */
2700static ssize_t
2701_ctl_host_trace_buffer_size_show(struct device *cdev,
2702 struct device_attribute *attr, char *buf)
2703{
2704 struct Scsi_Host *shost = class_to_shost(cdev);
2705 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2706 u32 size = 0;
2707 struct DIAG_BUFFER_START *request_data;
2708
2709 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2710 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2711 "registered\n", ioc->name, __func__);
2712 return 0;
2713 }
2714
2715 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2716 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2717 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2718 "registered\n", ioc->name, __func__);
2719 return 0;
2720 }
2721
2722 request_data = (struct DIAG_BUFFER_START *)
2723 ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE];
2724 if ((le32_to_cpu(request_data->DiagVersion) == 0x00000000 ||
2725 le32_to_cpu(request_data->DiagVersion) == 0x01000000) &&
2726 le32_to_cpu(request_data->Reserved3) == 0x4742444c)
2727 size = le32_to_cpu(request_data->Size);
2728
2729 ioc->ring_buffer_sz = size;
2730 return snprintf(buf, PAGE_SIZE, "%d\n", size);
2731}
2732static DEVICE_ATTR(host_trace_buffer_size, S_IRUGO,
2733 _ctl_host_trace_buffer_size_show, NULL);
2734
2735/**
2736 * _ctl_host_trace_buffer_show - firmware ring buffer (trace only)
2737 * @cdev - pointer to embedded class device
2738 * @buf - the buffer returned
2739 *
2740 * A sysfs 'read/write' shost attribute.
2741 *
2742 * You will only be able to read 4k bytes of ring buffer at a time.
2743 * In order to read beyond 4k bytes, you will have to write out the
2744 * offset to the same attribute, it will move the pointer.
2745 */
2746static ssize_t
2747_ctl_host_trace_buffer_show(struct device *cdev, struct device_attribute *attr,
2748 char *buf)
2749{
2750 struct Scsi_Host *shost = class_to_shost(cdev);
2751 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2752 void *request_data;
2753 u32 size;
2754
2755 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) {
2756 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2757 "registered\n", ioc->name, __func__);
2758 return 0;
2759 }
2760
2761 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2762 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0) {
2763 printk(MPT2SAS_ERR_FMT "%s: host_trace_buffer is not "
2764 "registered\n", ioc->name, __func__);
2765 return 0;
2766 }
2767
2768 if (ioc->ring_buffer_offset > ioc->ring_buffer_sz)
2769 return 0;
2770
2771 size = ioc->ring_buffer_sz - ioc->ring_buffer_offset;
2772 size = (size > PAGE_SIZE) ? PAGE_SIZE : size;
2773 request_data = ioc->diag_buffer[0] + ioc->ring_buffer_offset;
2774 memcpy(buf, request_data, size);
2775 return size;
2776}
2777
2778static ssize_t
2779_ctl_host_trace_buffer_store(struct device *cdev, struct device_attribute *attr,
2780 const char *buf, size_t count)
2781{
2782 struct Scsi_Host *shost = class_to_shost(cdev);
2783 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2784 int val = 0;
2785
2786 if (sscanf(buf, "%d", &val) != 1)
2787 return -EINVAL;
2788
2789 ioc->ring_buffer_offset = val;
2790 return strlen(buf);
2791}
2792static DEVICE_ATTR(host_trace_buffer, S_IRUGO | S_IWUSR,
2793 _ctl_host_trace_buffer_show, _ctl_host_trace_buffer_store);
2794
2795/*****************************************/
2796
2797/**
2798 * _ctl_host_trace_buffer_enable_show - firmware ring buffer (trace only)
2799 * @cdev - pointer to embedded class device
2800 * @buf - the buffer returned
2801 *
2802 * A sysfs 'read/write' shost attribute.
2803 *
2804 * This is a mechnism to post/release host_trace_buffers
2805 */
2806static ssize_t
2807_ctl_host_trace_buffer_enable_show(struct device *cdev,
2808 struct device_attribute *attr, char *buf)
2809{
2810 struct Scsi_Host *shost = class_to_shost(cdev);
2811 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2812
2813 if ((!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) ||
2814 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2815 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0))
2816 return snprintf(buf, PAGE_SIZE, "off\n");
2817 else if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2818 MPT2_DIAG_BUFFER_IS_RELEASED))
2819 return snprintf(buf, PAGE_SIZE, "release\n");
2820 else
2821 return snprintf(buf, PAGE_SIZE, "post\n");
2822}
2823
2824static ssize_t
2825_ctl_host_trace_buffer_enable_store(struct device *cdev,
2826 struct device_attribute *attr, const char *buf, size_t count)
2827{
2828 struct Scsi_Host *shost = class_to_shost(cdev);
2829 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2830 char str[10] = "";
2831 struct mpt2_diag_register diag_register;
2832 u8 issue_reset = 0;
2833
2834 if (sscanf(buf, "%s", str) != 1)
2835 return -EINVAL;
2836
2837 if (!strcmp(str, "post")) {
2838 /* exit out if host buffers are already posted */
2839 if ((ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE]) &&
2840 (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2841 MPT2_DIAG_BUFFER_IS_REGISTERED) &&
2842 ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2843 MPT2_DIAG_BUFFER_IS_RELEASED) == 0))
2844 goto out;
2845 memset(&diag_register, 0, sizeof(struct mpt2_diag_register));
2846 printk(MPT2SAS_INFO_FMT "posting host trace buffers\n",
2847 ioc->name);
2848 diag_register.buffer_type = MPI2_DIAG_BUF_TYPE_TRACE;
2849 diag_register.requested_buffer_size = (1024 * 1024);
2850 diag_register.unique_id = 0x7075900;
2851 ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] = 0;
2852 _ctl_diag_register_2(ioc, &diag_register);
2853 } else if (!strcmp(str, "release")) {
2854 /* exit out if host buffers are already released */
2855 if (!ioc->diag_buffer[MPI2_DIAG_BUF_TYPE_TRACE])
2856 goto out;
2857 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2858 MPT2_DIAG_BUFFER_IS_REGISTERED) == 0)
2859 goto out;
2860 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
2861 MPT2_DIAG_BUFFER_IS_RELEASED))
2862 goto out;
2863 printk(MPT2SAS_INFO_FMT "releasing host trace buffer\n",
2864 ioc->name);
2865 _ctl_send_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE, &issue_reset);
2866 }
2867
2868 out:
2869 return strlen(buf);
2870}
2871static DEVICE_ATTR(host_trace_buffer_enable, S_IRUGO | S_IWUSR,
2872 _ctl_host_trace_buffer_enable_show, _ctl_host_trace_buffer_enable_store);
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05302873
Eric Moore635374e2009-03-09 01:21:12 -06002874struct device_attribute *mpt2sas_host_attrs[] = {
2875 &dev_attr_version_fw,
2876 &dev_attr_version_bios,
2877 &dev_attr_version_mpi,
2878 &dev_attr_version_product,
2879 &dev_attr_version_nvdata_persistent,
2880 &dev_attr_version_nvdata_default,
2881 &dev_attr_board_name,
2882 &dev_attr_board_assembly,
2883 &dev_attr_board_tracer,
2884 &dev_attr_io_delay,
2885 &dev_attr_device_delay,
2886 &dev_attr_logging_level,
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302887 &dev_attr_fwfault_debug,
Eric Moore635374e2009-03-09 01:21:12 -06002888 &dev_attr_fw_queue_depth,
2889 &dev_attr_host_sas_address,
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05302890 &dev_attr_ioc_reset_count,
Kashyap, Desai570c67a2010-06-17 13:43:17 +05302891 &dev_attr_host_trace_buffer_size,
2892 &dev_attr_host_trace_buffer,
2893 &dev_attr_host_trace_buffer_enable,
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302894 &dev_attr_reply_queue_count,
Eric Moore635374e2009-03-09 01:21:12 -06002895 NULL,
2896};
2897
Eric Moore635374e2009-03-09 01:21:12 -06002898/**
2899 * _ctl_device_sas_address_show - sas address
2900 * @cdev - pointer to embedded class device
2901 * @buf - the buffer returned
2902 *
2903 * This is the sas address for the target
2904 *
2905 * A sysfs 'read-only' shost attribute.
2906 */
2907static ssize_t
2908_ctl_device_sas_address_show(struct device *dev, struct device_attribute *attr,
2909 char *buf)
2910{
2911 struct scsi_device *sdev = to_scsi_device(dev);
2912 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2913
2914 return snprintf(buf, PAGE_SIZE, "0x%016llx\n",
2915 (unsigned long long)sas_device_priv_data->sas_target->sas_address);
2916}
2917static DEVICE_ATTR(sas_address, S_IRUGO, _ctl_device_sas_address_show, NULL);
2918
2919/**
2920 * _ctl_device_handle_show - device handle
2921 * @cdev - pointer to embedded class device
2922 * @buf - the buffer returned
2923 *
2924 * This is the firmware assigned device handle
2925 *
2926 * A sysfs 'read-only' shost attribute.
2927 */
2928static ssize_t
2929_ctl_device_handle_show(struct device *dev, struct device_attribute *attr,
2930 char *buf)
2931{
2932 struct scsi_device *sdev = to_scsi_device(dev);
2933 struct MPT2SAS_DEVICE *sas_device_priv_data = sdev->hostdata;
2934
2935 return snprintf(buf, PAGE_SIZE, "0x%04x\n",
2936 sas_device_priv_data->sas_target->handle);
2937}
2938static DEVICE_ATTR(sas_device_handle, S_IRUGO, _ctl_device_handle_show, NULL);
2939
2940struct device_attribute *mpt2sas_dev_attrs[] = {
2941 &dev_attr_sas_address,
2942 &dev_attr_sas_device_handle,
2943 NULL,
2944};
2945
2946static const struct file_operations ctl_fops = {
2947 .owner = THIS_MODULE,
2948 .unlocked_ioctl = _ctl_ioctl,
2949 .release = _ctl_release,
2950 .poll = _ctl_poll,
2951 .fasync = _ctl_fasync,
2952#ifdef CONFIG_COMPAT
2953 .compat_ioctl = _ctl_ioctl_compat,
2954#endif
Arnd Bergmann6038f372010-08-15 18:52:59 +02002955 .llseek = noop_llseek,
Eric Moore635374e2009-03-09 01:21:12 -06002956};
2957
2958static struct miscdevice ctl_dev = {
2959 .minor = MPT2SAS_MINOR,
2960 .name = MPT2SAS_DEV_NAME,
2961 .fops = &ctl_fops,
2962};
2963
2964/**
2965 * mpt2sas_ctl_init - main entry point for ctl.
2966 *
2967 */
2968void
2969mpt2sas_ctl_init(void)
2970{
2971 async_queue = NULL;
2972 if (misc_register(&ctl_dev) < 0)
2973 printk(KERN_ERR "%s can't register misc device [minor=%d]\n",
2974 MPT2SAS_DRIVER_NAME, MPT2SAS_MINOR);
2975
2976 init_waitqueue_head(&ctl_poll_wait);
2977}
2978
2979/**
2980 * mpt2sas_ctl_exit - exit point for ctl
2981 *
2982 */
2983void
2984mpt2sas_ctl_exit(void)
2985{
2986 struct MPT2SAS_ADAPTER *ioc;
2987 int i;
2988
Eric Mooreba33fad2009-03-15 21:37:18 -06002989 list_for_each_entry(ioc, &mpt2sas_ioc_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06002990
2991 /* free memory associated to diag buffers */
2992 for (i = 0; i < MPI2_DIAG_BUF_TYPE_COUNT; i++) {
2993 if (!ioc->diag_buffer[i])
2994 continue;
2995 pci_free_consistent(ioc->pdev, ioc->diag_buffer_sz[i],
2996 ioc->diag_buffer[i], ioc->diag_buffer_dma[i]);
2997 ioc->diag_buffer[i] = NULL;
2998 ioc->diag_buffer_status[i] = 0;
2999 }
3000
3001 kfree(ioc->event_log);
3002 }
3003 misc_deregister(&ctl_dev);
3004}
3005