blob: c4cef569540b6c7536a508656496e6e60bc8e7cc [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * This is the Fusion MPT base driver providing common API layer interface
3 * for access to MPT (Message Passing Technology) firmware.
4 *
5 * This code is based on drivers/scsi/mpt2sas/mpt2_base.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/kdev_t.h>
53#include <linux/blkdev.h>
54#include <linux/delay.h>
55#include <linux/interrupt.h>
56#include <linux/dma-mapping.h>
57#include <linux/sort.h>
58#include <linux/io.h>
Kashyap, Desaia8ebd762009-09-23 17:29:29 +053059#include <linux/time.h>
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +053060#include <linux/kthread.h>
Kashyap, Desaief7c80c2010-04-05 14:20:07 +053061#include <linux/aer.h>
Eric Moore635374e2009-03-09 01:21:12 -060062
63#include "mpt2sas_base.h"
64
65static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
66
67#define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
Eric Moore635374e2009-03-09 01:21:12 -060068
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +053069#define MAX_HBA_QUEUE_DEPTH 30000
70#define MAX_CHAIN_DEPTH 100000
Eric Moore635374e2009-03-09 01:21:12 -060071static int max_queue_depth = -1;
72module_param(max_queue_depth, int, 0);
73MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
74
75static int max_sgl_entries = -1;
76module_param(max_sgl_entries, int, 0);
77MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
78
79static int msix_disable = -1;
80module_param(msix_disable, int, 0);
81MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
82
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +053083static int missing_delay[2] = {-1, -1};
84module_param_array(missing_delay, int, NULL, 0);
85MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
86
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +053087static int mpt2sas_fwfault_debug;
88MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
89 "and halt firmware - (default=0)");
90
91static int disable_discovery = -1;
92module_param(disable_discovery, int, 0);
93MODULE_PARM_DESC(disable_discovery, " disable discovery ");
94
Kashyap, Desaifa7f3162009-09-23 17:26:58 +053095/**
96 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
97 *
98 */
99static int
100_scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
101{
102 int ret = param_set_int(val, kp);
103 struct MPT2SAS_ADAPTER *ioc;
104
105 if (ret)
106 return ret;
107
Kashyap, Desaie75b9b62009-12-16 18:52:39 +0530108 printk(KERN_INFO "setting fwfault_debug(%d)\n", mpt2sas_fwfault_debug);
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530109 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
110 ioc->fwfault_debug = mpt2sas_fwfault_debug;
111 return 0;
112}
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +0530113
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530114module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
115 param_get_int, &mpt2sas_fwfault_debug, 0644);
116
Eric Moore635374e2009-03-09 01:21:12 -0600117/**
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +0530118 * mpt2sas_remove_dead_ioc_func - kthread context to remove dead ioc
119 * @arg: input argument, used to derive ioc
120 *
121 * Return 0 if controller is removed from pci subsystem.
122 * Return -1 for other case.
123 */
124static int mpt2sas_remove_dead_ioc_func(void *arg)
125{
126 struct MPT2SAS_ADAPTER *ioc = (struct MPT2SAS_ADAPTER *)arg;
127 struct pci_dev *pdev;
128
129 if ((ioc == NULL))
130 return -1;
131
132 pdev = ioc->pdev;
133 if ((pdev == NULL))
134 return -1;
Yinghai Lu210647a2012-02-25 13:54:20 -0800135 pci_stop_and_remove_bus_device(pdev);
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +0530136 return 0;
137}
138
139
140/**
Eric Moore635374e2009-03-09 01:21:12 -0600141 * _base_fault_reset_work - workq handling ioc fault conditions
142 * @work: input argument, used to derive ioc
143 * Context: sleep.
144 *
145 * Return nothing.
146 */
147static void
148_base_fault_reset_work(struct work_struct *work)
149{
150 struct MPT2SAS_ADAPTER *ioc =
151 container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
152 unsigned long flags;
153 u32 doorbell;
154 int rc;
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +0530155 struct task_struct *p;
Eric Moore635374e2009-03-09 01:21:12 -0600156
157 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530158 if (ioc->shost_recovery)
Eric Moore635374e2009-03-09 01:21:12 -0600159 goto rearm_timer;
160 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
161
162 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +0530163 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
164 printk(MPT2SAS_INFO_FMT "%s : SAS host is non-operational !!!!\n",
165 ioc->name, __func__);
166
167 /*
168 * Call _scsih_flush_pending_cmds callback so that we flush all
169 * pending commands back to OS. This call is required to aovid
170 * deadlock at block layer. Dead IOC will fail to do diag reset,
171 * and this call is safe since dead ioc will never return any
172 * command back from HW.
173 */
174 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
175 /*
176 * Set remove_host flag early since kernel thread will
177 * take some time to execute.
178 */
179 ioc->remove_host = 1;
180 /*Remove the Dead Host */
181 p = kthread_run(mpt2sas_remove_dead_ioc_func, ioc,
182 "mpt2sas_dead_ioc_%d", ioc->id);
183 if (IS_ERR(p)) {
184 printk(MPT2SAS_ERR_FMT
185 "%s: Running mpt2sas_dead_ioc thread failed !!!!\n",
186 ioc->name, __func__);
187 } else {
188 printk(MPT2SAS_ERR_FMT
189 "%s: Running mpt2sas_dead_ioc thread success !!!!\n",
190 ioc->name, __func__);
191 }
192
193 return; /* don't rearm timer */
194 }
195
Eric Moore635374e2009-03-09 01:21:12 -0600196 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
197 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
198 FORCE_BIG_HAMMER);
199 printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
200 __func__, (rc == 0) ? "success" : "failed");
201 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
202 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
203 mpt2sas_base_fault_info(ioc, doorbell &
204 MPI2_DOORBELL_DATA_MASK);
205 }
206
207 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
208 rearm_timer:
209 if (ioc->fault_reset_work_q)
210 queue_delayed_work(ioc->fault_reset_work_q,
211 &ioc->fault_reset_work,
212 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
213 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
214}
215
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530216/**
217 * mpt2sas_base_start_watchdog - start the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530218 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530219 * Context: sleep.
220 *
221 * Return nothing.
222 */
223void
224mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
225{
226 unsigned long flags;
227
228 if (ioc->fault_reset_work_q)
229 return;
230
231 /* initialize fault polling */
232 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
233 snprintf(ioc->fault_reset_work_q_name,
234 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
235 ioc->fault_reset_work_q =
236 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
237 if (!ioc->fault_reset_work_q) {
238 printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
239 ioc->name, __func__, __LINE__);
240 return;
241 }
242 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
243 if (ioc->fault_reset_work_q)
244 queue_delayed_work(ioc->fault_reset_work_q,
245 &ioc->fault_reset_work,
246 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
247 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
248}
249
250/**
251 * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530252 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530253 * Context: sleep.
254 *
255 * Return nothing.
256 */
257void
258mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
259{
260 unsigned long flags;
261 struct workqueue_struct *wq;
262
263 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
264 wq = ioc->fault_reset_work_q;
265 ioc->fault_reset_work_q = NULL;
266 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
267 if (wq) {
268 if (!cancel_delayed_work(&ioc->fault_reset_work))
269 flush_workqueue(wq);
270 destroy_workqueue(wq);
271 }
272}
273
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530274/**
275 * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
276 * @ioc: per adapter object
277 * @fault_code: fault code
278 *
279 * Return nothing.
280 */
281void
282mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
283{
284 printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
285 ioc->name, fault_code);
286}
287
288/**
289 * mpt2sas_halt_firmware - halt's mpt controller firmware
290 * @ioc: per adapter object
291 *
292 * For debugging timeout related issues. Writing 0xCOFFEE00
293 * to the doorbell register will halt controller firmware. With
294 * the purpose to stop both driver and firmware, the enduser can
295 * obtain a ring buffer from controller UART.
296 */
297void
298mpt2sas_halt_firmware(struct MPT2SAS_ADAPTER *ioc)
299{
300 u32 doorbell;
301
302 if (!ioc->fwfault_debug)
303 return;
304
305 dump_stack();
306
307 doorbell = readl(&ioc->chip->Doorbell);
308 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
309 mpt2sas_base_fault_info(ioc , doorbell);
310 else {
311 writel(0xC0FFEE00, &ioc->chip->Doorbell);
312 printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
313 "timeout\n", ioc->name);
314 }
315
316 panic("panic in %s\n", __func__);
317}
318
Eric Moore635374e2009-03-09 01:21:12 -0600319#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
320/**
321 * _base_sas_ioc_info - verbose translation of the ioc status
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530322 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600323 * @mpi_reply: reply mf payload returned from firmware
324 * @request_hdr: request mf
325 *
326 * Return nothing.
327 */
328static void
329_base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
330 MPI2RequestHeader_t *request_hdr)
331{
332 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
333 MPI2_IOCSTATUS_MASK;
334 char *desc = NULL;
335 u16 frame_sz;
336 char *func_str = NULL;
337
338 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
339 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
340 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
341 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
342 return;
343
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530344 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
345 return;
346
Eric Moore635374e2009-03-09 01:21:12 -0600347 switch (ioc_status) {
348
349/****************************************************************************
350* Common IOCStatus values for all replies
351****************************************************************************/
352
353 case MPI2_IOCSTATUS_INVALID_FUNCTION:
354 desc = "invalid function";
355 break;
356 case MPI2_IOCSTATUS_BUSY:
357 desc = "busy";
358 break;
359 case MPI2_IOCSTATUS_INVALID_SGL:
360 desc = "invalid sgl";
361 break;
362 case MPI2_IOCSTATUS_INTERNAL_ERROR:
363 desc = "internal error";
364 break;
365 case MPI2_IOCSTATUS_INVALID_VPID:
366 desc = "invalid vpid";
367 break;
368 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
369 desc = "insufficient resources";
370 break;
371 case MPI2_IOCSTATUS_INVALID_FIELD:
372 desc = "invalid field";
373 break;
374 case MPI2_IOCSTATUS_INVALID_STATE:
375 desc = "invalid state";
376 break;
377 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
378 desc = "op state not supported";
379 break;
380
381/****************************************************************************
382* Config IOCStatus values
383****************************************************************************/
384
385 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
386 desc = "config invalid action";
387 break;
388 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
389 desc = "config invalid type";
390 break;
391 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
392 desc = "config invalid page";
393 break;
394 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
395 desc = "config invalid data";
396 break;
397 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
398 desc = "config no defaults";
399 break;
400 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
401 desc = "config cant commit";
402 break;
403
404/****************************************************************************
405* SCSI IO Reply
406****************************************************************************/
407
408 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
409 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
410 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
411 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
412 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
413 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
414 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
415 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
416 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
417 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
418 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
419 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
420 break;
421
422/****************************************************************************
423* For use by SCSI Initiator and SCSI Target end-to-end data protection
424****************************************************************************/
425
426 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
427 desc = "eedp guard error";
428 break;
429 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
430 desc = "eedp ref tag error";
431 break;
432 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
433 desc = "eedp app tag error";
434 break;
435
436/****************************************************************************
437* SCSI Target values
438****************************************************************************/
439
440 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
441 desc = "target invalid io index";
442 break;
443 case MPI2_IOCSTATUS_TARGET_ABORTED:
444 desc = "target aborted";
445 break;
446 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
447 desc = "target no conn retryable";
448 break;
449 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
450 desc = "target no connection";
451 break;
452 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
453 desc = "target xfer count mismatch";
454 break;
455 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
456 desc = "target data offset error";
457 break;
458 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
459 desc = "target too much write data";
460 break;
461 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
462 desc = "target iu too short";
463 break;
464 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
465 desc = "target ack nak timeout";
466 break;
467 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
468 desc = "target nak received";
469 break;
470
471/****************************************************************************
472* Serial Attached SCSI values
473****************************************************************************/
474
475 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
476 desc = "smp request failed";
477 break;
478 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
479 desc = "smp data overrun";
480 break;
481
482/****************************************************************************
483* Diagnostic Buffer Post / Diagnostic Release values
484****************************************************************************/
485
486 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
487 desc = "diagnostic released";
488 break;
489 default:
490 break;
491 }
492
493 if (!desc)
494 return;
495
496 switch (request_hdr->Function) {
497 case MPI2_FUNCTION_CONFIG:
498 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
499 func_str = "config_page";
500 break;
501 case MPI2_FUNCTION_SCSI_TASK_MGMT:
502 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
503 func_str = "task_mgmt";
504 break;
505 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
506 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
507 func_str = "sas_iounit_ctl";
508 break;
509 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
510 frame_sz = sizeof(Mpi2SepRequest_t);
511 func_str = "enclosure";
512 break;
513 case MPI2_FUNCTION_IOC_INIT:
514 frame_sz = sizeof(Mpi2IOCInitRequest_t);
515 func_str = "ioc_init";
516 break;
517 case MPI2_FUNCTION_PORT_ENABLE:
518 frame_sz = sizeof(Mpi2PortEnableRequest_t);
519 func_str = "port_enable";
520 break;
521 case MPI2_FUNCTION_SMP_PASSTHROUGH:
522 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
523 func_str = "smp_passthru";
524 break;
525 default:
526 frame_sz = 32;
527 func_str = "unknown";
528 break;
529 }
530
531 printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
532 " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
533
534 _debug_dump_mf(request_hdr, frame_sz/4);
535}
536
537/**
538 * _base_display_event_data - verbose translation of firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530539 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600540 * @mpi_reply: reply mf payload returned from firmware
541 *
542 * Return nothing.
543 */
544static void
545_base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
546 Mpi2EventNotificationReply_t *mpi_reply)
547{
548 char *desc = NULL;
549 u16 event;
550
551 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
552 return;
553
554 event = le16_to_cpu(mpi_reply->Event);
555
556 switch (event) {
557 case MPI2_EVENT_LOG_DATA:
558 desc = "Log Data";
559 break;
560 case MPI2_EVENT_STATE_CHANGE:
561 desc = "Status Change";
562 break;
563 case MPI2_EVENT_HARD_RESET_RECEIVED:
564 desc = "Hard Reset Received";
565 break;
566 case MPI2_EVENT_EVENT_CHANGE:
567 desc = "Event Change";
568 break;
Eric Moore635374e2009-03-09 01:21:12 -0600569 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
570 desc = "Device Status Change";
571 break;
572 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530573 if (!ioc->hide_ir_msg)
574 desc = "IR Operation Status";
Eric Moore635374e2009-03-09 01:21:12 -0600575 break;
576 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530577 {
578 Mpi2EventDataSasDiscovery_t *event_data =
579 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
580 printk(MPT2SAS_INFO_FMT "Discovery: (%s)", ioc->name,
581 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
582 "start" : "stop");
583 if (event_data->DiscoveryStatus)
584 printk("discovery_status(0x%08x)",
585 le32_to_cpu(event_data->DiscoveryStatus));
Julia Lawall7968f192010-08-05 22:19:36 +0200586 printk("\n");
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530587 return;
588 }
Eric Moore635374e2009-03-09 01:21:12 -0600589 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
590 desc = "SAS Broadcast Primitive";
591 break;
592 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
593 desc = "SAS Init Device Status Change";
594 break;
595 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
596 desc = "SAS Init Table Overflow";
597 break;
598 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
599 desc = "SAS Topology Change List";
600 break;
601 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
602 desc = "SAS Enclosure Device Status Change";
603 break;
604 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530605 if (!ioc->hide_ir_msg)
606 desc = "IR Volume";
Eric Moore635374e2009-03-09 01:21:12 -0600607 break;
608 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530609 if (!ioc->hide_ir_msg)
610 desc = "IR Physical Disk";
Eric Moore635374e2009-03-09 01:21:12 -0600611 break;
612 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530613 if (!ioc->hide_ir_msg)
614 desc = "IR Configuration Change List";
Eric Moore635374e2009-03-09 01:21:12 -0600615 break;
616 case MPI2_EVENT_LOG_ENTRY_ADDED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530617 if (!ioc->hide_ir_msg)
618 desc = "Log Entry Added";
Eric Moore635374e2009-03-09 01:21:12 -0600619 break;
620 }
621
622 if (!desc)
623 return;
624
625 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
626}
627#endif
628
629/**
630 * _base_sas_log_info - verbose translation of firmware log info
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530631 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600632 * @log_info: log info
633 *
634 * Return nothing.
635 */
636static void
637_base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
638{
639 union loginfo_type {
640 u32 loginfo;
641 struct {
642 u32 subcode:16;
643 u32 code:8;
644 u32 originator:4;
645 u32 bus_type:4;
646 } dw;
647 };
648 union loginfo_type sas_loginfo;
649 char *originator_str = NULL;
650
651 sas_loginfo.loginfo = log_info;
652 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
653 return;
654
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +0530655 /* each nexus loss loginfo */
656 if (log_info == 0x31170000)
657 return;
658
Eric Moore635374e2009-03-09 01:21:12 -0600659 /* eat the loginfos associated with task aborts */
Sathisha Nanjappa714be352012-03-13 11:59:28 -0700660 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
Eric Moore635374e2009-03-09 01:21:12 -0600661 0x31140000 || log_info == 0x31130000))
662 return;
663
664 switch (sas_loginfo.dw.originator) {
665 case 0:
666 originator_str = "IOP";
667 break;
668 case 1:
669 originator_str = "PL";
670 break;
671 case 2:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530672 if (!ioc->hide_ir_msg)
673 originator_str = "IR";
674 else
675 originator_str = "WarpDrive";
Eric Moore635374e2009-03-09 01:21:12 -0600676 break;
677 }
678
679 printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
680 "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
681 originator_str, sas_loginfo.dw.code,
682 sas_loginfo.dw.subcode);
683}
684
685/**
Eric Moore635374e2009-03-09 01:21:12 -0600686 * _base_display_reply_info -
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530687 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600688 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530689 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600690 * @reply: reply message frame(lower 32bit addr)
691 *
692 * Return nothing.
693 */
694static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530695_base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600696 u32 reply)
697{
698 MPI2DefaultReply_t *mpi_reply;
699 u16 ioc_status;
700
701 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
702 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
703#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
704 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
705 (ioc->logging_level & MPT_DEBUG_REPLY)) {
706 _base_sas_ioc_info(ioc , mpi_reply,
707 mpt2sas_base_get_msg_frame(ioc, smid));
708 }
709#endif
710 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
711 _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
712}
713
714/**
715 * mpt2sas_base_done - base internal command completion routine
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530716 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600717 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530718 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600719 * @reply: reply message frame(lower 32bit addr)
720 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530721 * Return 1 meaning mf should be freed from _base_interrupt
722 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600723 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530724u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530725mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
726 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600727{
728 MPI2DefaultReply_t *mpi_reply;
729
730 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
731 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530732 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600733
734 if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530735 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600736
737 ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
738 if (mpi_reply) {
739 ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
740 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
741 }
742 ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530743
Eric Moore635374e2009-03-09 01:21:12 -0600744 complete(&ioc->base_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530745 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600746}
747
748/**
749 * _base_async_event - main callback handler for firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530750 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530751 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600752 * @reply: reply message frame(lower 32bit addr)
753 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530754 * Return 1 meaning mf should be freed from _base_interrupt
755 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600756 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530757static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530758_base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600759{
760 Mpi2EventNotificationReply_t *mpi_reply;
761 Mpi2EventAckRequest_t *ack_request;
762 u16 smid;
763
764 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
765 if (!mpi_reply)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530766 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600767 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530768 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600769#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
770 _base_display_event_data(ioc, mpi_reply);
771#endif
772 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
773 goto out;
774 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
775 if (!smid) {
776 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
777 ioc->name, __func__);
778 goto out;
779 }
780
781 ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
782 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
783 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
784 ack_request->Event = mpi_reply->Event;
785 ack_request->EventContext = mpi_reply->EventContext;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530786 ack_request->VF_ID = 0; /* TODO */
787 ack_request->VP_ID = 0;
788 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600789
790 out:
791
792 /* scsih callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530793 mpt2sas_scsih_event_callback(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600794
795 /* ctl callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530796 mpt2sas_ctl_event_callback(ioc, msix_index, reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530797
798 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600799}
800
801/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530802 * _base_get_cb_idx - obtain the callback index
803 * @ioc: per adapter object
804 * @smid: system request message index
805 *
806 * Return callback index.
807 */
808static u8
809_base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
810{
811 int i;
Kashyap, Desaid5bd3492011-01-04 11:39:20 +0530812 u8 cb_idx;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530813
Kashyap, Desaid5bd3492011-01-04 11:39:20 +0530814 if (smid < ioc->hi_priority_smid) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530815 i = smid - 1;
816 cb_idx = ioc->scsi_lookup[i].cb_idx;
Kashyap, Desaid5bd3492011-01-04 11:39:20 +0530817 } else if (smid < ioc->internal_smid) {
818 i = smid - ioc->hi_priority_smid;
819 cb_idx = ioc->hpr_lookup[i].cb_idx;
820 } else if (smid <= ioc->hba_queue_depth) {
821 i = smid - ioc->internal_smid;
822 cb_idx = ioc->internal_lookup[i].cb_idx;
823 } else
824 cb_idx = 0xFF;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530825 return cb_idx;
826}
827
828/**
Eric Moore635374e2009-03-09 01:21:12 -0600829 * _base_mask_interrupts - disable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530830 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600831 *
832 * Disabling ResetIRQ, Reply and Doorbell Interrupts
833 *
834 * Return nothing.
835 */
836static void
837_base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
838{
839 u32 him_register;
840
841 ioc->mask_interrupts = 1;
842 him_register = readl(&ioc->chip->HostInterruptMask);
843 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
844 writel(him_register, &ioc->chip->HostInterruptMask);
845 readl(&ioc->chip->HostInterruptMask);
846}
847
848/**
849 * _base_unmask_interrupts - enable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530850 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600851 *
852 * Enabling only Reply Interrupts
853 *
854 * Return nothing.
855 */
856static void
857_base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
858{
859 u32 him_register;
860
Eric Moore635374e2009-03-09 01:21:12 -0600861 him_register = readl(&ioc->chip->HostInterruptMask);
862 him_register &= ~MPI2_HIM_RIM;
863 writel(him_register, &ioc->chip->HostInterruptMask);
864 ioc->mask_interrupts = 0;
865}
866
Kashyap, Desai5b768582009-08-20 13:24:31 +0530867union reply_descriptor {
868 u64 word;
869 struct {
870 u32 low;
871 u32 high;
872 } u;
873};
874
Eric Moore635374e2009-03-09 01:21:12 -0600875/**
876 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
877 * @irq: irq number (not used)
878 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
879 * @r: pt_regs pointer (not used)
880 *
881 * Return IRQ_HANDLE if processed, else IRQ_NONE.
882 */
883static irqreturn_t
884_base_interrupt(int irq, void *bus_id)
885{
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530886 struct adapter_reply_queue *reply_q = bus_id;
Eric Moore03ea1112009-04-21 15:37:57 -0600887 union reply_descriptor rd;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530888 u32 completed_cmds;
Eric Moore635374e2009-03-09 01:21:12 -0600889 u8 request_desript_type;
890 u16 smid;
891 u8 cb_idx;
892 u32 reply;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530893 u8 msix_index = reply_q->msix_index;
894 struct MPT2SAS_ADAPTER *ioc = reply_q->ioc;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530895 Mpi2ReplyDescriptorsUnion_t *rpf;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530896 u8 rc;
Eric Moore635374e2009-03-09 01:21:12 -0600897
898 if (ioc->mask_interrupts)
899 return IRQ_NONE;
900
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530901 if (!atomic_add_unless(&reply_q->busy, 1, 1))
902 return IRQ_NONE;
903
904 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
Kashyap, Desai5b768582009-08-20 13:24:31 +0530905 request_desript_type = rpf->Default.ReplyFlags
906 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530907 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
908 atomic_dec(&reply_q->busy);
Eric Moore635374e2009-03-09 01:21:12 -0600909 return IRQ_NONE;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530910 }
Eric Moore635374e2009-03-09 01:21:12 -0600911
912 completed_cmds = 0;
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530913 cb_idx = 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -0600914 do {
Kashyap, Desaic97951e2011-06-14 10:54:56 +0530915 rd.word = le64_to_cpu(rpf->Words);
Eric Moore03ea1112009-04-21 15:37:57 -0600916 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
Eric Moore635374e2009-03-09 01:21:12 -0600917 goto out;
918 reply = 0;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530919 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
Eric Moore635374e2009-03-09 01:21:12 -0600920 if (request_desript_type ==
921 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
Kashyap, Desai5b768582009-08-20 13:24:31 +0530922 reply = le32_to_cpu
923 (rpf->AddressReply.ReplyFrameAddress);
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530924 if (reply > ioc->reply_dma_max_address ||
925 reply < ioc->reply_dma_min_address)
926 reply = 0;
Eric Moore635374e2009-03-09 01:21:12 -0600927 } else if (request_desript_type ==
928 MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
929 goto next;
930 else if (request_desript_type ==
931 MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
932 goto next;
933 if (smid)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530934 cb_idx = _base_get_cb_idx(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600935 if (smid && cb_idx != 0xFF) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530936 rc = mpt_callbacks[cb_idx](ioc, smid, msix_index,
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530937 reply);
Eric Moore635374e2009-03-09 01:21:12 -0600938 if (reply)
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530939 _base_display_reply_info(ioc, smid, msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600940 reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530941 if (rc)
942 mpt2sas_base_free_smid(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600943 }
944 if (!smid)
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530945 _base_async_event(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600946
947 /* reply free queue handling */
948 if (reply) {
949 ioc->reply_free_host_index =
950 (ioc->reply_free_host_index ==
951 (ioc->reply_free_queue_depth - 1)) ?
952 0 : ioc->reply_free_host_index + 1;
953 ioc->reply_free[ioc->reply_free_host_index] =
954 cpu_to_le32(reply);
Kashyap, Desai5b768582009-08-20 13:24:31 +0530955 wmb();
Eric Moore635374e2009-03-09 01:21:12 -0600956 writel(ioc->reply_free_host_index,
957 &ioc->chip->ReplyFreeHostIndex);
Eric Moore635374e2009-03-09 01:21:12 -0600958 }
959
960 next:
Kashyap, Desai5b768582009-08-20 13:24:31 +0530961
Kashyap, Desaic97951e2011-06-14 10:54:56 +0530962 rpf->Words = cpu_to_le64(ULLONG_MAX);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530963 reply_q->reply_post_host_index =
964 (reply_q->reply_post_host_index ==
Kashyap, Desai5b768582009-08-20 13:24:31 +0530965 (ioc->reply_post_queue_depth - 1)) ? 0 :
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530966 reply_q->reply_post_host_index + 1;
Eric Moore635374e2009-03-09 01:21:12 -0600967 request_desript_type =
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530968 reply_q->reply_post_free[reply_q->reply_post_host_index].
969 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
Eric Moore635374e2009-03-09 01:21:12 -0600970 completed_cmds++;
971 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
972 goto out;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530973 if (!reply_q->reply_post_host_index)
974 rpf = reply_q->reply_post_free;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530975 else
976 rpf++;
Eric Moore635374e2009-03-09 01:21:12 -0600977 } while (1);
978
979 out:
980
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530981 if (!completed_cmds) {
982 atomic_dec(&reply_q->busy);
Eric Moore635374e2009-03-09 01:21:12 -0600983 return IRQ_NONE;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530984 }
Eric Moore635374e2009-03-09 01:21:12 -0600985 wmb();
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530986 if (ioc->is_warpdrive) {
987 writel(reply_q->reply_post_host_index,
988 ioc->reply_post_host_index[msix_index]);
989 atomic_dec(&reply_q->busy);
990 return IRQ_HANDLED;
991 }
992 writel(reply_q->reply_post_host_index | (msix_index <<
993 MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
994 atomic_dec(&reply_q->busy);
Eric Moore635374e2009-03-09 01:21:12 -0600995 return IRQ_HANDLED;
996}
997
998/**
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530999 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1000 * @ioc: per adapter object
1001 *
1002 */
1003static inline int
1004_base_is_controller_msix_enabled(struct MPT2SAS_ADAPTER *ioc)
1005{
1006 return (ioc->facts.IOCCapabilities &
1007 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1008}
1009
1010/**
1011 * mpt2sas_base_flush_reply_queues - flushing the MSIX reply queues
1012 * @ioc: per adapter object
1013 * Context: ISR conext
1014 *
1015 * Called when a Task Management request has completed. We want
1016 * to flush the other reply queues so all the outstanding IO has been
1017 * completed back to OS before we process the TM completetion.
1018 *
1019 * Return nothing.
1020 */
1021void
1022mpt2sas_base_flush_reply_queues(struct MPT2SAS_ADAPTER *ioc)
1023{
1024 struct adapter_reply_queue *reply_q;
1025
1026 /* If MSIX capability is turned off
1027 * then multi-queues are not enabled
1028 */
1029 if (!_base_is_controller_msix_enabled(ioc))
1030 return;
1031
1032 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1033 if (ioc->shost_recovery)
1034 return;
1035 /* TMs are on msix_index == 0 */
1036 if (reply_q->msix_index == 0)
1037 continue;
1038 _base_interrupt(reply_q->vector, (void *)reply_q);
1039 }
1040}
1041
1042/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001043 * mpt2sas_base_release_callback_handler - clear interrupt callback handler
Eric Moore635374e2009-03-09 01:21:12 -06001044 * @cb_idx: callback index
1045 *
1046 * Return nothing.
1047 */
1048void
1049mpt2sas_base_release_callback_handler(u8 cb_idx)
1050{
1051 mpt_callbacks[cb_idx] = NULL;
1052}
1053
1054/**
1055 * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
1056 * @cb_func: callback function
1057 *
1058 * Returns cb_func.
1059 */
1060u8
1061mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1062{
1063 u8 cb_idx;
1064
1065 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1066 if (mpt_callbacks[cb_idx] == NULL)
1067 break;
1068
1069 mpt_callbacks[cb_idx] = cb_func;
1070 return cb_idx;
1071}
1072
1073/**
1074 * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
1075 *
1076 * Return nothing.
1077 */
1078void
1079mpt2sas_base_initialize_callback_handler(void)
1080{
1081 u8 cb_idx;
1082
1083 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1084 mpt2sas_base_release_callback_handler(cb_idx);
1085}
1086
1087/**
1088 * mpt2sas_base_build_zero_len_sge - build zero length sg entry
1089 * @ioc: per adapter object
1090 * @paddr: virtual address for SGE
1091 *
1092 * Create a zero length scatter gather entry to insure the IOCs hardware has
1093 * something to use if the target device goes brain dead and tries
1094 * to send data even when none is asked for.
1095 *
1096 * Return nothing.
1097 */
1098void
1099mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
1100{
1101 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1102 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1103 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1104 MPI2_SGE_FLAGS_SHIFT);
1105 ioc->base_add_sg_single(paddr, flags_length, -1);
1106}
1107
1108/**
1109 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1110 * @paddr: virtual address for SGE
1111 * @flags_length: SGE flags and data transfer length
1112 * @dma_addr: Physical address
1113 *
1114 * Return nothing.
1115 */
1116static void
1117_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1118{
1119 Mpi2SGESimple32_t *sgel = paddr;
1120
1121 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1122 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1123 sgel->FlagsLength = cpu_to_le32(flags_length);
1124 sgel->Address = cpu_to_le32(dma_addr);
1125}
1126
1127
1128/**
1129 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1130 * @paddr: virtual address for SGE
1131 * @flags_length: SGE flags and data transfer length
1132 * @dma_addr: Physical address
1133 *
1134 * Return nothing.
1135 */
1136static void
1137_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1138{
1139 Mpi2SGESimple64_t *sgel = paddr;
1140
1141 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1142 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1143 sgel->FlagsLength = cpu_to_le32(flags_length);
1144 sgel->Address = cpu_to_le64(dma_addr);
1145}
1146
1147#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1148
1149/**
1150 * _base_config_dma_addressing - set dma addressing
1151 * @ioc: per adapter object
1152 * @pdev: PCI device struct
1153 *
1154 * Returns 0 for success, non-zero for failure.
1155 */
1156static int
1157_base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
1158{
1159 struct sysinfo s;
1160 char *desc = NULL;
1161
1162 if (sizeof(dma_addr_t) > 4) {
1163 const uint64_t required_mask =
1164 dma_get_required_mask(&pdev->dev);
Yang Hongyange9304382009-04-13 14:40:14 -07001165 if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
1166 DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
1167 DMA_BIT_MASK(64))) {
Eric Moore635374e2009-03-09 01:21:12 -06001168 ioc->base_add_sg_single = &_base_add_sg_single_64;
1169 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
1170 desc = "64";
1171 goto out;
1172 }
1173 }
1174
Yang Hongyange9304382009-04-13 14:40:14 -07001175 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1176 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
Eric Moore635374e2009-03-09 01:21:12 -06001177 ioc->base_add_sg_single = &_base_add_sg_single_32;
1178 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
1179 desc = "32";
1180 } else
1181 return -ENODEV;
1182
1183 out:
1184 si_meminfo(&s);
1185 printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
1186 "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
1187
1188 return 0;
1189}
1190
1191/**
Eric Moore635374e2009-03-09 01:21:12 -06001192 * _base_check_enable_msix - checks MSIX capabable.
1193 * @ioc: per adapter object
1194 *
1195 * Check to see if card is capable of MSIX, and set number
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001196 * of available msix vectors
Eric Moore635374e2009-03-09 01:21:12 -06001197 */
1198static int
1199_base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1200{
1201 int base;
1202 u16 message_control;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301203
Eric Moore635374e2009-03-09 01:21:12 -06001204
1205 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1206 if (!base) {
1207 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
1208 "supported\n", ioc->name));
1209 return -EINVAL;
1210 }
1211
1212 /* get msix vector count */
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301213 /* NUMA_IO not supported for older controllers */
1214 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
1215 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
1216 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
1217 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
1218 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
1219 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
1220 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
1221 ioc->msix_vector_count = 1;
1222 else {
1223 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1224 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1225 }
Eric Moore635374e2009-03-09 01:21:12 -06001226 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301227 "vector_count(%d)\n", ioc->name, ioc->msix_vector_count));
1228
Eric Moore635374e2009-03-09 01:21:12 -06001229 return 0;
1230}
1231
1232/**
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301233 * _base_free_irq - free irq
1234 * @ioc: per adapter object
1235 *
1236 * Freeing respective reply_queue from the list.
1237 */
1238static void
1239_base_free_irq(struct MPT2SAS_ADAPTER *ioc)
1240{
1241 struct adapter_reply_queue *reply_q, *next;
1242
1243 if (list_empty(&ioc->reply_queue_list))
1244 return;
1245
1246 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1247 list_del(&reply_q->list);
1248 synchronize_irq(reply_q->vector);
1249 free_irq(reply_q->vector, reply_q);
1250 kfree(reply_q);
1251 }
1252}
1253
1254/**
1255 * _base_request_irq - request irq
1256 * @ioc: per adapter object
1257 * @index: msix index into vector table
1258 * @vector: irq vector
1259 *
1260 * Inserting respective reply_queue into the list.
1261 */
1262static int
1263_base_request_irq(struct MPT2SAS_ADAPTER *ioc, u8 index, u32 vector)
1264{
1265 struct adapter_reply_queue *reply_q;
1266 int r;
1267
1268 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1269 if (!reply_q) {
1270 printk(MPT2SAS_ERR_FMT "unable to allocate memory %d!\n",
1271 ioc->name, (int)sizeof(struct adapter_reply_queue));
1272 return -ENOMEM;
1273 }
1274 reply_q->ioc = ioc;
1275 reply_q->msix_index = index;
1276 reply_q->vector = vector;
1277 atomic_set(&reply_q->busy, 0);
1278 if (ioc->msix_enable)
1279 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
1280 MPT2SAS_DRIVER_NAME, ioc->id, index);
1281 else
1282 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
1283 MPT2SAS_DRIVER_NAME, ioc->id);
1284 r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1285 reply_q);
1286 if (r) {
1287 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1288 reply_q->name, vector);
1289 kfree(reply_q);
1290 return -EBUSY;
1291 }
1292
1293 INIT_LIST_HEAD(&reply_q->list);
1294 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1295 return 0;
1296}
1297
1298/**
1299 * _base_assign_reply_queues - assigning msix index for each cpu
1300 * @ioc: per adapter object
1301 *
1302 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1303 *
1304 * It would nice if we could call irq_set_affinity, however it is not
1305 * an exported symbol
1306 */
1307static void
1308_base_assign_reply_queues(struct MPT2SAS_ADAPTER *ioc)
1309{
1310 struct adapter_reply_queue *reply_q;
1311 int cpu_id;
1312 int cpu_grouping, loop, grouping, grouping_mod;
1313
1314 if (!_base_is_controller_msix_enabled(ioc))
1315 return;
1316
1317 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1318 /* when there are more cpus than available msix vectors,
1319 * then group cpus togeather on same irq
1320 */
1321 if (ioc->cpu_count > ioc->msix_vector_count) {
1322 grouping = ioc->cpu_count / ioc->msix_vector_count;
1323 grouping_mod = ioc->cpu_count % ioc->msix_vector_count;
1324 if (grouping < 2 || (grouping == 2 && !grouping_mod))
1325 cpu_grouping = 2;
1326 else if (grouping < 4 || (grouping == 4 && !grouping_mod))
1327 cpu_grouping = 4;
1328 else if (grouping < 8 || (grouping == 8 && !grouping_mod))
1329 cpu_grouping = 8;
1330 else
1331 cpu_grouping = 16;
1332 } else
1333 cpu_grouping = 0;
1334
1335 loop = 0;
1336 reply_q = list_entry(ioc->reply_queue_list.next,
1337 struct adapter_reply_queue, list);
1338 for_each_online_cpu(cpu_id) {
1339 if (!cpu_grouping) {
1340 ioc->cpu_msix_table[cpu_id] = reply_q->msix_index;
1341 reply_q = list_entry(reply_q->list.next,
1342 struct adapter_reply_queue, list);
1343 } else {
1344 if (loop < cpu_grouping) {
1345 ioc->cpu_msix_table[cpu_id] =
1346 reply_q->msix_index;
1347 loop++;
1348 } else {
1349 reply_q = list_entry(reply_q->list.next,
1350 struct adapter_reply_queue, list);
1351 ioc->cpu_msix_table[cpu_id] =
1352 reply_q->msix_index;
1353 loop = 1;
1354 }
1355 }
1356 }
1357}
1358
1359/**
Eric Moore635374e2009-03-09 01:21:12 -06001360 * _base_disable_msix - disables msix
1361 * @ioc: per adapter object
1362 *
1363 */
1364static void
1365_base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
1366{
1367 if (ioc->msix_enable) {
1368 pci_disable_msix(ioc->pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001369 ioc->msix_enable = 0;
1370 }
1371}
1372
1373/**
1374 * _base_enable_msix - enables msix, failback to io_apic
1375 * @ioc: per adapter object
1376 *
1377 */
1378static int
1379_base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1380{
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301381 struct msix_entry *entries, *a;
Eric Moore635374e2009-03-09 01:21:12 -06001382 int r;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301383 int i;
Eric Moore635374e2009-03-09 01:21:12 -06001384 u8 try_msix = 0;
1385
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301386 INIT_LIST_HEAD(&ioc->reply_queue_list);
1387
Eric Moore635374e2009-03-09 01:21:12 -06001388 if (msix_disable == -1 || msix_disable == 0)
1389 try_msix = 1;
1390
1391 if (!try_msix)
1392 goto try_ioapic;
1393
1394 if (_base_check_enable_msix(ioc) != 0)
1395 goto try_ioapic;
1396
Roland Dreier2f73b9a2011-11-30 16:30:33 -08001397 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301398 ioc->msix_vector_count);
1399
1400 entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1401 GFP_KERNEL);
1402 if (!entries) {
1403 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "kcalloc "
1404 "failed @ at %s:%d/%s() !!!\n", ioc->name, __FILE__,
1405 __LINE__, __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001406 goto try_ioapic;
1407 }
1408
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301409 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
1410 a->entry = i;
1411
1412 r = pci_enable_msix(ioc->pdev, entries, ioc->reply_queue_count);
Eric Moore635374e2009-03-09 01:21:12 -06001413 if (r) {
1414 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
1415 "failed (r=%d) !!!\n", ioc->name, r));
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301416 kfree(entries);
Eric Moore635374e2009-03-09 01:21:12 -06001417 goto try_ioapic;
1418 }
1419
Eric Moore635374e2009-03-09 01:21:12 -06001420 ioc->msix_enable = 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301421 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
1422 r = _base_request_irq(ioc, i, a->vector);
1423 if (r) {
1424 _base_free_irq(ioc);
1425 _base_disable_msix(ioc);
1426 kfree(entries);
1427 goto try_ioapic;
1428 }
1429 }
1430
1431 kfree(entries);
Eric Moore635374e2009-03-09 01:21:12 -06001432 return 0;
1433
1434/* failback to io_apic interrupt routing */
1435 try_ioapic:
1436
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301437 r = _base_request_irq(ioc, 0, ioc->pdev->irq);
Eric Moore635374e2009-03-09 01:21:12 -06001438
Eric Moore635374e2009-03-09 01:21:12 -06001439 return r;
1440}
1441
1442/**
1443 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1444 * @ioc: per adapter object
1445 *
1446 * Returns 0 for success, non-zero for failure.
1447 */
1448int
1449mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1450{
1451 struct pci_dev *pdev = ioc->pdev;
1452 u32 memap_sz;
1453 u32 pio_sz;
1454 int i, r = 0;
Kashyap, Desai6846e752009-12-16 18:51:45 +05301455 u64 pio_chip = 0;
1456 u64 chip_phys = 0;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301457 struct adapter_reply_queue *reply_q;
Eric Moore635374e2009-03-09 01:21:12 -06001458
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301459 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
Eric Moore635374e2009-03-09 01:21:12 -06001460 ioc->name, __func__));
1461
1462 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1463 if (pci_enable_device_mem(pdev)) {
1464 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1465 "failed\n", ioc->name);
1466 return -ENODEV;
1467 }
1468
1469
1470 if (pci_request_selected_regions(pdev, ioc->bars,
1471 MPT2SAS_DRIVER_NAME)) {
1472 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1473 "failed\n", ioc->name);
1474 r = -ENODEV;
1475 goto out_fail;
1476 }
1477
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05301478 /* AER (Advanced Error Reporting) hooks */
1479 pci_enable_pcie_error_reporting(pdev);
1480
Eric Moore635374e2009-03-09 01:21:12 -06001481 pci_set_master(pdev);
1482
1483 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1484 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1485 ioc->name, pci_name(pdev));
1486 r = -ENODEV;
1487 goto out_fail;
1488 }
1489
1490 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
Richard A Laryfc193172010-03-12 15:27:06 -08001491 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
Eric Moore635374e2009-03-09 01:21:12 -06001492 if (pio_sz)
1493 continue;
Kashyap, Desai6846e752009-12-16 18:51:45 +05301494 pio_chip = (u64)pci_resource_start(pdev, i);
Eric Moore635374e2009-03-09 01:21:12 -06001495 pio_sz = pci_resource_len(pdev, i);
1496 } else {
1497 if (memap_sz)
1498 continue;
Richard A Laryfc193172010-03-12 15:27:06 -08001499 /* verify memory resource is valid before using */
1500 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1501 ioc->chip_phys = pci_resource_start(pdev, i);
1502 chip_phys = (u64)ioc->chip_phys;
1503 memap_sz = pci_resource_len(pdev, i);
1504 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1505 if (ioc->chip == NULL) {
1506 printk(MPT2SAS_ERR_FMT "unable to map "
1507 "adapter memory!\n", ioc->name);
1508 r = -EINVAL;
1509 goto out_fail;
1510 }
Eric Moore635374e2009-03-09 01:21:12 -06001511 }
1512 }
1513 }
1514
Eric Moore635374e2009-03-09 01:21:12 -06001515 _base_mask_interrupts(ioc);
1516 r = _base_enable_msix(ioc);
1517 if (r)
1518 goto out_fail;
1519
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301520 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
1521 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1522 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1523 "IO-APIC enabled"), reply_q->vector);
1524
Kashyap, Desai6846e752009-12-16 18:51:45 +05301525 printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1526 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1527 printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
1528 ioc->name, (unsigned long long)pio_chip, pio_sz);
Eric Moore635374e2009-03-09 01:21:12 -06001529
Eric Moore3cb54692010-07-08 14:44:34 -06001530 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1531 pci_save_state(pdev);
1532
Eric Moore635374e2009-03-09 01:21:12 -06001533 return 0;
1534
1535 out_fail:
1536 if (ioc->chip_phys)
1537 iounmap(ioc->chip);
1538 ioc->chip_phys = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001539 pci_release_selected_regions(ioc->pdev, ioc->bars);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05301540 pci_disable_pcie_error_reporting(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001541 pci_disable_device(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001542 return r;
1543}
1544
1545/**
Eric Moore635374e2009-03-09 01:21:12 -06001546 * mpt2sas_base_get_msg_frame - obtain request mf pointer
1547 * @ioc: per adapter object
1548 * @smid: system request message index(smid zero is invalid)
1549 *
1550 * Returns virt pointer to message frame.
1551 */
1552void *
1553mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1554{
1555 return (void *)(ioc->request + (smid * ioc->request_sz));
1556}
1557
1558/**
1559 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1560 * @ioc: per adapter object
1561 * @smid: system request message index
1562 *
1563 * Returns virt pointer to sense buffer.
1564 */
1565void *
1566mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1567{
1568 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1569}
1570
1571/**
1572 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1573 * @ioc: per adapter object
1574 * @smid: system request message index
1575 *
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301576 * Returns phys pointer to the low 32bit address of the sense buffer.
Eric Moore635374e2009-03-09 01:21:12 -06001577 */
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301578__le32
Eric Moore635374e2009-03-09 01:21:12 -06001579mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1580{
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301581 return cpu_to_le32(ioc->sense_dma +
1582 ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
Eric Moore635374e2009-03-09 01:21:12 -06001583}
1584
1585/**
1586 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1587 * @ioc: per adapter object
1588 * @phys_addr: lower 32 physical addr of the reply
1589 *
1590 * Converts 32bit lower physical addr into a virt address.
1591 */
1592void *
1593mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1594{
1595 if (!phys_addr)
1596 return NULL;
1597 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1598}
1599
1600/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301601 * mpt2sas_base_get_smid - obtain a free smid from internal queue
Eric Moore635374e2009-03-09 01:21:12 -06001602 * @ioc: per adapter object
1603 * @cb_idx: callback index
1604 *
1605 * Returns smid (zero is invalid)
1606 */
1607u16
1608mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1609{
1610 unsigned long flags;
1611 struct request_tracker *request;
1612 u16 smid;
1613
1614 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301615 if (list_empty(&ioc->internal_free_list)) {
1616 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1617 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1618 ioc->name, __func__);
1619 return 0;
1620 }
1621
1622 request = list_entry(ioc->internal_free_list.next,
1623 struct request_tracker, tracker_list);
1624 request->cb_idx = cb_idx;
1625 smid = request->smid;
1626 list_del(&request->tracker_list);
1627 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1628 return smid;
1629}
1630
1631/**
1632 * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1633 * @ioc: per adapter object
1634 * @cb_idx: callback index
1635 * @scmd: pointer to scsi command object
1636 *
1637 * Returns smid (zero is invalid)
1638 */
1639u16
1640mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
1641 struct scsi_cmnd *scmd)
1642{
1643 unsigned long flags;
Kashyap, Desaid5bd3492011-01-04 11:39:20 +05301644 struct scsiio_tracker *request;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301645 u16 smid;
1646
1647 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001648 if (list_empty(&ioc->free_list)) {
1649 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1650 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1651 ioc->name, __func__);
1652 return 0;
1653 }
1654
1655 request = list_entry(ioc->free_list.next,
Kashyap, Desaid5bd3492011-01-04 11:39:20 +05301656 struct scsiio_tracker, tracker_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301657 request->scmd = scmd;
1658 request->cb_idx = cb_idx;
1659 smid = request->smid;
1660 list_del(&request->tracker_list);
1661 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1662 return smid;
1663}
1664
1665/**
1666 * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1667 * @ioc: per adapter object
1668 * @cb_idx: callback index
1669 *
1670 * Returns smid (zero is invalid)
1671 */
1672u16
1673mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1674{
1675 unsigned long flags;
1676 struct request_tracker *request;
1677 u16 smid;
1678
1679 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1680 if (list_empty(&ioc->hpr_free_list)) {
1681 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1682 return 0;
1683 }
1684
1685 request = list_entry(ioc->hpr_free_list.next,
1686 struct request_tracker, tracker_list);
Eric Moore635374e2009-03-09 01:21:12 -06001687 request->cb_idx = cb_idx;
1688 smid = request->smid;
1689 list_del(&request->tracker_list);
1690 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1691 return smid;
1692}
1693
1694
1695/**
1696 * mpt2sas_base_free_smid - put smid back on free_list
1697 * @ioc: per adapter object
1698 * @smid: system request message index
1699 *
1700 * Return nothing.
1701 */
1702void
1703mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1704{
1705 unsigned long flags;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301706 int i;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301707 struct chain_tracker *chain_req, *next;
Eric Moore635374e2009-03-09 01:21:12 -06001708
1709 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaid5bd3492011-01-04 11:39:20 +05301710 if (smid < ioc->hi_priority_smid) {
1711 /* scsiio queue */
1712 i = smid - 1;
1713 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
1714 list_for_each_entry_safe(chain_req, next,
1715 &ioc->scsi_lookup[i].chain_list, tracker_list) {
1716 list_del_init(&chain_req->tracker_list);
1717 list_add_tail(&chain_req->tracker_list,
1718 &ioc->free_chain_list);
1719 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301720 }
Kashyap, Desaid5bd3492011-01-04 11:39:20 +05301721 ioc->scsi_lookup[i].cb_idx = 0xFF;
1722 ioc->scsi_lookup[i].scmd = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301723 ioc->scsi_lookup[i].direct_io = 0;
Kashyap, Desaid5bd3492011-01-04 11:39:20 +05301724 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
1725 &ioc->free_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301726 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301727
Kashyap, Desaid5bd3492011-01-04 11:39:20 +05301728 /*
1729 * See _wait_for_commands_to_complete() call with regards
1730 * to this code.
1731 */
1732 if (ioc->shost_recovery && ioc->pending_io_count) {
1733 if (ioc->pending_io_count == 1)
1734 wake_up(&ioc->reset_wq);
1735 ioc->pending_io_count--;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301736 }
Kashyap, Desaid5bd3492011-01-04 11:39:20 +05301737 return;
1738 } else if (smid < ioc->internal_smid) {
1739 /* hi-priority */
1740 i = smid - ioc->hi_priority_smid;
1741 ioc->hpr_lookup[i].cb_idx = 0xFF;
1742 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
1743 &ioc->hpr_free_list);
1744 } else if (smid <= ioc->hba_queue_depth) {
1745 /* internal queue */
1746 i = smid - ioc->internal_smid;
1747 ioc->internal_lookup[i].cb_idx = 0xFF;
1748 list_add_tail(&ioc->internal_lookup[i].tracker_list,
1749 &ioc->internal_free_list);
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301750 }
Eric Moore635374e2009-03-09 01:21:12 -06001751 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001752}
1753
1754/**
1755 * _base_writeq - 64 bit write to MMIO
1756 * @ioc: per adapter object
1757 * @b: data payload
1758 * @addr: address in MMIO space
1759 * @writeq_lock: spin lock
1760 *
1761 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1762 * care of 32 bit environment where its not quarenteed to send the entire word
1763 * in one transfer.
1764 */
1765#ifndef writeq
1766static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1767 spinlock_t *writeq_lock)
1768{
1769 unsigned long flags;
1770 __u64 data_out = cpu_to_le64(b);
1771
1772 spin_lock_irqsave(writeq_lock, flags);
1773 writel((u32)(data_out), addr);
1774 writel((u32)(data_out >> 32), (addr + 4));
1775 spin_unlock_irqrestore(writeq_lock, flags);
1776}
1777#else
1778static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1779 spinlock_t *writeq_lock)
1780{
1781 writeq(cpu_to_le64(b), addr);
1782}
1783#endif
1784
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301785static inline u8
1786_base_get_msix_index(struct MPT2SAS_ADAPTER *ioc)
1787{
nagalakshmi.nandigama@lsi.com696615b2012-04-17 11:25:04 +05301788 return ioc->cpu_msix_table[raw_smp_processor_id()];
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301789}
1790
Eric Moore635374e2009-03-09 01:21:12 -06001791/**
1792 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1793 * @ioc: per adapter object
1794 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001795 * @handle: device handle
1796 *
1797 * Return nothing.
1798 */
1799void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301800mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06001801{
1802 Mpi2RequestDescriptorUnion_t descriptor;
1803 u64 *request = (u64 *)&descriptor;
1804
1805
1806 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301807 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06001808 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1809 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1810 descriptor.SCSIIO.LMID = 0;
1811 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1812 &ioc->scsi_lookup_lock);
1813}
1814
1815
1816/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001817 * mpt2sas_base_put_smid_hi_priority - send Task Management request to firmware
Eric Moore635374e2009-03-09 01:21:12 -06001818 * @ioc: per adapter object
1819 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001820 *
1821 * Return nothing.
1822 */
1823void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301824mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001825{
1826 Mpi2RequestDescriptorUnion_t descriptor;
1827 u64 *request = (u64 *)&descriptor;
1828
1829 descriptor.HighPriority.RequestFlags =
1830 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301831 descriptor.HighPriority.MSIxIndex = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001832 descriptor.HighPriority.SMID = cpu_to_le16(smid);
1833 descriptor.HighPriority.LMID = 0;
1834 descriptor.HighPriority.Reserved1 = 0;
1835 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1836 &ioc->scsi_lookup_lock);
1837}
1838
1839/**
1840 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1841 * @ioc: per adapter object
1842 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001843 *
1844 * Return nothing.
1845 */
1846void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301847mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001848{
1849 Mpi2RequestDescriptorUnion_t descriptor;
1850 u64 *request = (u64 *)&descriptor;
1851
1852 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301853 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06001854 descriptor.Default.SMID = cpu_to_le16(smid);
1855 descriptor.Default.LMID = 0;
1856 descriptor.Default.DescriptorTypeDependent = 0;
1857 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1858 &ioc->scsi_lookup_lock);
1859}
1860
1861/**
1862 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1863 * @ioc: per adapter object
1864 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001865 * @io_index: value used to track the IO
1866 *
1867 * Return nothing.
1868 */
1869void
1870mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301871 u16 io_index)
Eric Moore635374e2009-03-09 01:21:12 -06001872{
1873 Mpi2RequestDescriptorUnion_t descriptor;
1874 u64 *request = (u64 *)&descriptor;
1875
1876 descriptor.SCSITarget.RequestFlags =
1877 MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301878 descriptor.SCSITarget.MSIxIndex = _base_get_msix_index(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06001879 descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1880 descriptor.SCSITarget.LMID = 0;
1881 descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1882 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1883 &ioc->scsi_lookup_lock);
1884}
1885
1886/**
Eric Mooref0f9cc12009-04-21 15:40:48 -06001887 * _base_display_dell_branding - Disply branding string
1888 * @ioc: per adapter object
1889 *
1890 * Return nothing.
1891 */
1892static void
1893_base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1894{
1895 char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1896
1897 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1898 return;
1899
1900 memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1901 switch (ioc->pdev->subsystem_device) {
1902 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1903 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1904 MPT2SAS_DELL_BRANDING_SIZE - 1);
1905 break;
1906 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1907 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1908 MPT2SAS_DELL_BRANDING_SIZE - 1);
1909 break;
1910 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1911 strncpy(dell_branding,
1912 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1913 MPT2SAS_DELL_BRANDING_SIZE - 1);
1914 break;
1915 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1916 strncpy(dell_branding,
1917 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
1918 MPT2SAS_DELL_BRANDING_SIZE - 1);
1919 break;
1920 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
1921 strncpy(dell_branding,
1922 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
1923 MPT2SAS_DELL_BRANDING_SIZE - 1);
1924 break;
1925 case MPT2SAS_DELL_PERC_H200_SSDID:
1926 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
1927 MPT2SAS_DELL_BRANDING_SIZE - 1);
1928 break;
1929 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
1930 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
1931 MPT2SAS_DELL_BRANDING_SIZE - 1);
1932 break;
1933 default:
1934 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
1935 break;
1936 }
1937
1938 printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
1939 " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
1940 ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
1941 ioc->pdev->subsystem_device);
1942}
1943
1944/**
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301945 * _base_display_intel_branding - Display branding string
1946 * @ioc: per adapter object
1947 *
1948 * Return nothing.
1949 */
1950static void
1951_base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
1952{
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301953 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1954 return;
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301955
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301956 switch (ioc->pdev->device) {
1957 case MPI2_MFGPAGE_DEVID_SAS2008:
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301958 switch (ioc->pdev->subsystem_device) {
1959 case MPT2SAS_INTEL_RMS2LL080_SSDID:
1960 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1961 MPT2SAS_INTEL_RMS2LL080_BRANDING);
1962 break;
1963 case MPT2SAS_INTEL_RMS2LL040_SSDID:
1964 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1965 MPT2SAS_INTEL_RMS2LL040_BRANDING);
1966 break;
nagalakshmi.nandigama@lsi.com70e73f92011-12-01 07:43:00 +05301967 case MPT2SAS_INTEL_RAMSDALE_SSDID:
1968 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1969 MPT2SAS_INTEL_RAMSDALE_BRANDING);
1970 break;
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301971 default:
1972 break;
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301973 }
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301974 case MPI2_MFGPAGE_DEVID_SAS2308_2:
1975 switch (ioc->pdev->subsystem_device) {
1976 case MPT2SAS_INTEL_RS25GB008_SSDID:
1977 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1978 MPT2SAS_INTEL_RS25GB008_BRANDING);
1979 break;
nagalakshmi.nandigama@lsi.com8bad3052011-12-01 07:52:42 +05301980 case MPT2SAS_INTEL_RMS25JB080_SSDID:
1981 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1982 MPT2SAS_INTEL_RMS25JB080_BRANDING);
1983 break;
1984 case MPT2SAS_INTEL_RMS25JB040_SSDID:
1985 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1986 MPT2SAS_INTEL_RMS25JB040_BRANDING);
1987 break;
1988 case MPT2SAS_INTEL_RMS25KB080_SSDID:
1989 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1990 MPT2SAS_INTEL_RMS25KB080_BRANDING);
1991 break;
1992 case MPT2SAS_INTEL_RMS25KB040_SSDID:
1993 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1994 MPT2SAS_INTEL_RMS25KB040_BRANDING);
1995 break;
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301996 default:
1997 break;
1998 }
1999 default:
2000 break;
Kashyap, Desaifb396be2011-01-04 11:36:37 +05302001 }
2002}
2003
2004/**
Kashyap, Desai0a2385c2011-03-15 20:04:26 +05302005 * _base_display_hp_branding - Display branding string
2006 * @ioc: per adapter object
2007 *
2008 * Return nothing.
2009 */
2010static void
2011_base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
2012{
2013 if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
2014 return;
2015
2016 switch (ioc->pdev->device) {
2017 case MPI2_MFGPAGE_DEVID_SAS2004:
2018 switch (ioc->pdev->subsystem_device) {
2019 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
2020 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2021 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
2022 break;
2023 default:
2024 break;
2025 }
2026 case MPI2_MFGPAGE_DEVID_SAS2308_2:
2027 switch (ioc->pdev->subsystem_device) {
2028 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
2029 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2030 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
2031 break;
2032 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
2033 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2034 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
2035 break;
2036 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
2037 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2038 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
2039 break;
2040 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
2041 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2042 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
2043 break;
2044 default:
2045 break;
2046 }
2047 default:
2048 break;
2049 }
2050}
2051
2052/**
Eric Moore635374e2009-03-09 01:21:12 -06002053 * _base_display_ioc_capabilities - Disply IOC's capabilities.
2054 * @ioc: per adapter object
2055 *
2056 * Return nothing.
2057 */
2058static void
2059_base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
2060{
2061 int i = 0;
2062 char desc[16];
Eric Moore635374e2009-03-09 01:21:12 -06002063 u32 iounit_pg1_flags;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302064 u32 bios_version;
Eric Moore635374e2009-03-09 01:21:12 -06002065
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302066 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
Eric Moore635374e2009-03-09 01:21:12 -06002067 strncpy(desc, ioc->manu_pg0.ChipName, 16);
2068 printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
2069 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2070 ioc->name, desc,
2071 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2072 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2073 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2074 ioc->facts.FWVersion.Word & 0x000000FF,
Sergei Shtylyov7d7311c2012-03-14 22:04:30 +03002075 ioc->pdev->revision,
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302076 (bios_version & 0xFF000000) >> 24,
2077 (bios_version & 0x00FF0000) >> 16,
2078 (bios_version & 0x0000FF00) >> 8,
2079 bios_version & 0x000000FF);
Eric Moore635374e2009-03-09 01:21:12 -06002080
Kashyap, Desaied79f122009-08-20 13:23:49 +05302081 _base_display_dell_branding(ioc);
Kashyap, Desaifb396be2011-01-04 11:36:37 +05302082 _base_display_intel_branding(ioc);
Kashyap, Desai0a2385c2011-03-15 20:04:26 +05302083 _base_display_hp_branding(ioc);
Kashyap, Desaied79f122009-08-20 13:23:49 +05302084
Eric Moore635374e2009-03-09 01:21:12 -06002085 printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
2086
2087 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2088 printk("Initiator");
2089 i++;
2090 }
2091
2092 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2093 printk("%sTarget", i ? "," : "");
2094 i++;
2095 }
2096
2097 i = 0;
2098 printk("), ");
2099 printk("Capabilities=(");
2100
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302101 if (!ioc->hide_ir_msg) {
2102 if (ioc->facts.IOCCapabilities &
2103 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2104 printk("Raid");
2105 i++;
2106 }
Eric Moore635374e2009-03-09 01:21:12 -06002107 }
2108
2109 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2110 printk("%sTLR", i ? "," : "");
2111 i++;
2112 }
2113
2114 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2115 printk("%sMulticast", i ? "," : "");
2116 i++;
2117 }
2118
2119 if (ioc->facts.IOCCapabilities &
2120 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2121 printk("%sBIDI Target", i ? "," : "");
2122 i++;
2123 }
2124
2125 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2126 printk("%sEEDP", i ? "," : "");
2127 i++;
2128 }
2129
2130 if (ioc->facts.IOCCapabilities &
2131 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2132 printk("%sSnapshot Buffer", i ? "," : "");
2133 i++;
2134 }
2135
2136 if (ioc->facts.IOCCapabilities &
2137 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2138 printk("%sDiag Trace Buffer", i ? "," : "");
2139 i++;
2140 }
2141
2142 if (ioc->facts.IOCCapabilities &
Kashyap, Desai1b01fe32009-09-23 17:28:59 +05302143 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2144 printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
2145 i++;
2146 }
2147
2148 if (ioc->facts.IOCCapabilities &
Eric Moore635374e2009-03-09 01:21:12 -06002149 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2150 printk("%sTask Set Full", i ? "," : "");
2151 i++;
2152 }
2153
2154 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2155 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2156 printk("%sNCQ", i ? "," : "");
2157 i++;
2158 }
2159
2160 printk(")\n");
2161}
2162
2163/**
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05302164 * _base_update_missing_delay - change the missing delay timers
2165 * @ioc: per adapter object
2166 * @device_missing_delay: amount of time till device is reported missing
2167 * @io_missing_delay: interval IO is returned when there is a missing device
2168 *
2169 * Return nothing.
2170 *
2171 * Passed on the command line, this function will modify the device missing
2172 * delay, as well as the io missing delay. This should be called at driver
2173 * load time.
2174 */
2175static void
2176_base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
2177 u16 device_missing_delay, u8 io_missing_delay)
2178{
2179 u16 dmd, dmd_new, dmd_orignal;
2180 u8 io_missing_delay_original;
2181 u16 sz;
2182 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2183 Mpi2ConfigReply_t mpi_reply;
2184 u8 num_phys = 0;
2185 u16 ioc_status;
2186
2187 mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
2188 if (!num_phys)
2189 return;
2190
2191 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2192 sizeof(Mpi2SasIOUnit1PhyData_t));
2193 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2194 if (!sas_iounit_pg1) {
2195 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2196 ioc->name, __FILE__, __LINE__, __func__);
2197 goto out;
2198 }
2199 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2200 sas_iounit_pg1, sz))) {
2201 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2202 ioc->name, __FILE__, __LINE__, __func__);
2203 goto out;
2204 }
2205 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2206 MPI2_IOCSTATUS_MASK;
2207 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2208 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2209 ioc->name, __FILE__, __LINE__, __func__);
2210 goto out;
2211 }
2212
2213 /* device missing delay */
2214 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2215 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2216 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2217 else
2218 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2219 dmd_orignal = dmd;
2220 if (device_missing_delay > 0x7F) {
2221 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2222 device_missing_delay;
2223 dmd = dmd / 16;
2224 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2225 } else
2226 dmd = device_missing_delay;
2227 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2228
2229 /* io missing delay */
2230 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2231 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2232
2233 if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2234 sz)) {
2235 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2236 dmd_new = (dmd &
2237 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2238 else
2239 dmd_new =
2240 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2241 printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
2242 "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
2243 printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
2244 "new(%d)\n", ioc->name, io_missing_delay_original,
2245 io_missing_delay);
2246 ioc->device_missing_delay = dmd_new;
2247 ioc->io_missing_delay = io_missing_delay;
2248 }
2249
2250out:
2251 kfree(sas_iounit_pg1);
2252}
2253
2254/**
Eric Moore635374e2009-03-09 01:21:12 -06002255 * _base_static_config_pages - static start of day config pages
2256 * @ioc: per adapter object
2257 *
2258 * Return nothing.
2259 */
2260static void
2261_base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
2262{
2263 Mpi2ConfigReply_t mpi_reply;
2264 u32 iounit_pg1_flags;
2265
2266 mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
Kashyap, Desaied79f122009-08-20 13:23:49 +05302267 if (ioc->ir_firmware)
2268 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2269 &ioc->manu_pg10);
Eric Moore635374e2009-03-09 01:21:12 -06002270 mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
2271 mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
2272 mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
2273 mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
2274 mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2275 _base_display_ioc_capabilities(ioc);
2276
2277 /*
2278 * Enable task_set_full handling in iounit_pg1 when the
2279 * facts capabilities indicate that its supported.
2280 */
2281 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2282 if ((ioc->facts.IOCCapabilities &
2283 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
2284 iounit_pg1_flags &=
2285 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2286 else
2287 iounit_pg1_flags |=
2288 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2289 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302290 mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05302291
Eric Moore635374e2009-03-09 01:21:12 -06002292}
2293
2294/**
2295 * _base_release_memory_pools - release memory
2296 * @ioc: per adapter object
2297 *
2298 * Free memory allocated from _base_allocate_memory_pools.
2299 *
2300 * Return nothing.
2301 */
2302static void
2303_base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
2304{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302305 int i;
2306
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302307 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002308 __func__));
2309
2310 if (ioc->request) {
2311 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2312 ioc->request, ioc->request_dma);
2313 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
2314 ": free\n", ioc->name, ioc->request));
2315 ioc->request = NULL;
2316 }
2317
2318 if (ioc->sense) {
2319 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2320 if (ioc->sense_dma_pool)
2321 pci_pool_destroy(ioc->sense_dma_pool);
2322 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
2323 ": free\n", ioc->name, ioc->sense));
2324 ioc->sense = NULL;
2325 }
2326
2327 if (ioc->reply) {
2328 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2329 if (ioc->reply_dma_pool)
2330 pci_pool_destroy(ioc->reply_dma_pool);
2331 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
2332 ": free\n", ioc->name, ioc->reply));
2333 ioc->reply = NULL;
2334 }
2335
2336 if (ioc->reply_free) {
2337 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2338 ioc->reply_free_dma);
2339 if (ioc->reply_free_dma_pool)
2340 pci_pool_destroy(ioc->reply_free_dma_pool);
2341 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
2342 "(0x%p): free\n", ioc->name, ioc->reply_free));
2343 ioc->reply_free = NULL;
2344 }
2345
2346 if (ioc->reply_post_free) {
2347 pci_pool_free(ioc->reply_post_free_dma_pool,
2348 ioc->reply_post_free, ioc->reply_post_free_dma);
2349 if (ioc->reply_post_free_dma_pool)
2350 pci_pool_destroy(ioc->reply_post_free_dma_pool);
2351 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2352 "reply_post_free_pool(0x%p): free\n", ioc->name,
2353 ioc->reply_post_free));
2354 ioc->reply_post_free = NULL;
2355 }
2356
2357 if (ioc->config_page) {
2358 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2359 "config_page(0x%p): free\n", ioc->name,
2360 ioc->config_page));
2361 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2362 ioc->config_page, ioc->config_page_dma);
2363 }
2364
Kashyap, Desai66a67932010-04-05 14:21:07 +05302365 if (ioc->scsi_lookup) {
2366 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2367 ioc->scsi_lookup = NULL;
2368 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302369 kfree(ioc->hpr_lookup);
2370 kfree(ioc->internal_lookup);
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302371 if (ioc->chain_lookup) {
2372 for (i = 0; i < ioc->chain_depth; i++) {
2373 if (ioc->chain_lookup[i].chain_buffer)
2374 pci_pool_free(ioc->chain_dma_pool,
2375 ioc->chain_lookup[i].chain_buffer,
2376 ioc->chain_lookup[i].chain_buffer_dma);
2377 }
2378 if (ioc->chain_dma_pool)
2379 pci_pool_destroy(ioc->chain_dma_pool);
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302380 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2381 ioc->chain_lookup = NULL;
2382 }
Eric Moore635374e2009-03-09 01:21:12 -06002383}
2384
2385
2386/**
2387 * _base_allocate_memory_pools - allocate start of day memory pools
2388 * @ioc: per adapter object
2389 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2390 *
2391 * Returns 0 success, anything else error
2392 */
2393static int
2394_base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2395{
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302396 struct mpt2sas_facts *facts;
Eric Moore635374e2009-03-09 01:21:12 -06002397 u16 max_sge_elements;
Eric Moore635374e2009-03-09 01:21:12 -06002398 u16 chains_needed_per_io;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302399 u32 sz, total_sz, reply_post_free_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002400 u32 retry_sz;
2401 u16 max_request_credit;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302402 int i;
Eric Moore635374e2009-03-09 01:21:12 -06002403
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302404 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002405 __func__));
2406
2407 retry_sz = 0;
2408 facts = &ioc->facts;
2409
2410 /* command line tunables for max sgl entries */
2411 if (max_sgl_entries != -1) {
2412 ioc->shost->sg_tablesize = (max_sgl_entries <
2413 MPT2SAS_SG_DEPTH) ? max_sgl_entries :
2414 MPT2SAS_SG_DEPTH;
2415 } else {
2416 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
2417 }
2418
2419 /* command line tunables for max controller queue depth */
sreekanth.reddy@lsi.com7a4601c2012-07-17 15:57:05 +05302420 if (max_queue_depth != -1 && max_queue_depth != 0) {
2421 max_request_credit = min_t(u16, max_queue_depth +
2422 ioc->hi_priority_depth + ioc->internal_depth,
2423 facts->RequestCredit);
2424 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
2425 max_request_credit = MAX_HBA_QUEUE_DEPTH;
2426 } else
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05302427 max_request_credit = min_t(u16, facts->RequestCredit,
2428 MAX_HBA_QUEUE_DEPTH);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302429
2430 ioc->hba_queue_depth = max_request_credit;
2431 ioc->hi_priority_depth = facts->HighPriorityCredit;
2432 ioc->internal_depth = ioc->hi_priority_depth + 5;
Eric Moore635374e2009-03-09 01:21:12 -06002433
2434 /* request frame size */
2435 ioc->request_sz = facts->IOCRequestFrameSize * 4;
2436
2437 /* reply frame size */
2438 ioc->reply_sz = facts->ReplyFrameSize * 4;
2439
2440 retry_allocation:
2441 total_sz = 0;
2442 /* calculate number of sg elements left over in the 1st frame */
2443 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2444 sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
2445 ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
2446
2447 /* now do the same for a chain buffer */
2448 max_sge_elements = ioc->request_sz - ioc->sge_size;
2449 ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
2450
2451 ioc->chain_offset_value_for_main_message =
2452 ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
2453 (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
2454
2455 /*
2456 * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2457 */
2458 chains_needed_per_io = ((ioc->shost->sg_tablesize -
2459 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2460 + 1;
2461 if (chains_needed_per_io > facts->MaxChainDepth) {
2462 chains_needed_per_io = facts->MaxChainDepth;
2463 ioc->shost->sg_tablesize = min_t(u16,
2464 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2465 * chains_needed_per_io), ioc->shost->sg_tablesize);
2466 }
2467 ioc->chains_needed_per_io = chains_needed_per_io;
2468
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05302469 /* reply free queue sizing - taking into account for 64 FW events */
2470 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
Eric Moore635374e2009-03-09 01:21:12 -06002471
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05302472 /* align the reply post queue on the next 16 count boundary */
2473 if (!ioc->reply_free_queue_depth % 16)
2474 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth + 16;
2475 else
2476 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth +
2477 32 - (ioc->reply_free_queue_depth % 16);
2478 if (ioc->reply_post_queue_depth >
2479 facts->MaxReplyDescriptorPostQueueDepth) {
2480 ioc->reply_post_queue_depth = min_t(u16,
2481 (facts->MaxReplyDescriptorPostQueueDepth -
2482 (facts->MaxReplyDescriptorPostQueueDepth % 16)),
2483 (ioc->hba_queue_depth - (ioc->hba_queue_depth % 16)));
2484 ioc->reply_free_queue_depth = ioc->reply_post_queue_depth - 16;
2485 ioc->hba_queue_depth = ioc->reply_free_queue_depth - 64;
Eric Moore635374e2009-03-09 01:21:12 -06002486 }
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05302487
Eric Moore635374e2009-03-09 01:21:12 -06002488
Eric Moore635374e2009-03-09 01:21:12 -06002489 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
2490 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2491 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2492 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2493 ioc->chains_needed_per_io));
2494
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302495 ioc->scsiio_depth = ioc->hba_queue_depth -
2496 ioc->hi_priority_depth - ioc->internal_depth;
2497
2498 /* set the scsi host can_queue depth
2499 * with some internal commands that could be outstanding
2500 */
sreekanth.reddy@lsi.com7a4601c2012-07-17 15:57:05 +05302501 ioc->shost->can_queue = ioc->scsiio_depth;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302502 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
2503 "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
2504
Eric Moore635374e2009-03-09 01:21:12 -06002505 /* contiguous pool for request and chains, 16 byte align, one extra "
2506 * "frame for smid=0
2507 */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302508 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302509 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302510
2511 /* hi-priority queue */
2512 sz += (ioc->hi_priority_depth * ioc->request_sz);
2513
2514 /* internal queue */
2515 sz += (ioc->internal_depth * ioc->request_sz);
Eric Moore635374e2009-03-09 01:21:12 -06002516
2517 ioc->request_dma_sz = sz;
2518 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2519 if (!ioc->request) {
2520 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302521 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2522 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002523 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302524 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
Eric Moore635374e2009-03-09 01:21:12 -06002525 goto out;
2526 retry_sz += 64;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302527 ioc->hba_queue_depth = max_request_credit - retry_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002528 goto retry_allocation;
2529 }
2530
2531 if (retry_sz)
2532 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302533 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2534 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002535 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2536
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302537
2538 /* hi-priority queue */
2539 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002540 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302541 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002542 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302543
2544 /* internal queue */
2545 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2546 ioc->request_sz);
2547 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2548 ioc->request_sz);
2549
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302550
Eric Moore635374e2009-03-09 01:21:12 -06002551 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2552 "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302553 ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2554 (ioc->hba_queue_depth * ioc->request_sz)/1024));
Eric Moore635374e2009-03-09 01:21:12 -06002555 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2556 ioc->name, (unsigned long long) ioc->request_dma));
2557 total_sz += sz;
2558
Kashyap, Desaid5bd3492011-01-04 11:39:20 +05302559 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302560 ioc->scsi_lookup_pages = get_order(sz);
Kashyap, Desaid5bd3492011-01-04 11:39:20 +05302561 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302562 GFP_KERNEL, ioc->scsi_lookup_pages);
Eric Moore635374e2009-03-09 01:21:12 -06002563 if (!ioc->scsi_lookup) {
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302564 printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
2565 "sz(%d)\n", ioc->name, (int)sz);
Eric Moore635374e2009-03-09 01:21:12 -06002566 goto out;
2567 }
2568
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302569 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2570 "depth(%d)\n", ioc->name, ioc->request,
2571 ioc->scsiio_depth));
2572
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05302573 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
2574 sz = ioc->chain_depth * sizeof(struct chain_tracker);
2575 ioc->chain_pages = get_order(sz);
2576
2577 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
2578 GFP_KERNEL, ioc->chain_pages);
Tomas Henzlc834b1c2012-02-13 18:29:58 +01002579 if (!ioc->chain_lookup) {
2580 printk(MPT2SAS_ERR_FMT "chain_lookup: get_free_pages failed, "
2581 "sz(%d)\n", ioc->name, (int)sz);
2582 goto out;
2583 }
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302584 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
2585 ioc->request_sz, 16, 0);
2586 if (!ioc->chain_dma_pool) {
2587 printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
2588 "failed\n", ioc->name);
2589 goto out;
2590 }
2591 for (i = 0; i < ioc->chain_depth; i++) {
2592 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
2593 ioc->chain_dma_pool , GFP_KERNEL,
2594 &ioc->chain_lookup[i].chain_buffer_dma);
2595 if (!ioc->chain_lookup[i].chain_buffer) {
2596 ioc->chain_depth = i;
2597 goto chain_done;
2598 }
2599 total_sz += ioc->request_sz;
2600 }
2601chain_done:
2602 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
2603 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2604 ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2605 ioc->request_sz))/1024));
2606
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302607 /* initialize hi-priority queue smid's */
2608 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2609 sizeof(struct request_tracker), GFP_KERNEL);
2610 if (!ioc->hpr_lookup) {
2611 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2612 ioc->name);
2613 goto out;
2614 }
2615 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2616 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2617 "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2618 ioc->hi_priority_depth, ioc->hi_priority_smid));
2619
2620 /* initialize internal queue smid's */
2621 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2622 sizeof(struct request_tracker), GFP_KERNEL);
2623 if (!ioc->internal_lookup) {
2624 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2625 ioc->name);
2626 goto out;
2627 }
2628 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2629 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2630 "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2631 ioc->internal_depth, ioc->internal_smid));
Eric Moore635374e2009-03-09 01:21:12 -06002632
2633 /* sense buffers, 4 byte align */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302634 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
Eric Moore635374e2009-03-09 01:21:12 -06002635 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2636 0);
2637 if (!ioc->sense_dma_pool) {
2638 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2639 ioc->name);
2640 goto out;
2641 }
2642 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2643 &ioc->sense_dma);
2644 if (!ioc->sense) {
2645 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2646 ioc->name);
2647 goto out;
2648 }
2649 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2650 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302651 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002652 SCSI_SENSE_BUFFERSIZE, sz/1024));
2653 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2654 ioc->name, (unsigned long long)ioc->sense_dma));
2655 total_sz += sz;
2656
2657 /* reply pool, 4 byte align */
2658 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2659 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2660 0);
2661 if (!ioc->reply_dma_pool) {
2662 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2663 ioc->name);
2664 goto out;
2665 }
2666 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2667 &ioc->reply_dma);
2668 if (!ioc->reply) {
2669 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2670 ioc->name);
2671 goto out;
2672 }
Kashyap, Desaidd3741d2010-11-13 04:31:14 +05302673 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2674 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
Eric Moore635374e2009-03-09 01:21:12 -06002675 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2676 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2677 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2678 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2679 ioc->name, (unsigned long long)ioc->reply_dma));
2680 total_sz += sz;
2681
2682 /* reply free queue, 16 byte align */
2683 sz = ioc->reply_free_queue_depth * 4;
2684 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2685 ioc->pdev, sz, 16, 0);
2686 if (!ioc->reply_free_dma_pool) {
2687 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2688 "failed\n", ioc->name);
2689 goto out;
2690 }
2691 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2692 &ioc->reply_free_dma);
2693 if (!ioc->reply_free) {
2694 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2695 "failed\n", ioc->name);
2696 goto out;
2697 }
2698 memset(ioc->reply_free, 0, sz);
2699 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2700 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2701 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2702 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2703 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2704 total_sz += sz;
2705
2706 /* reply post queue, 16 byte align */
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302707 reply_post_free_sz = ioc->reply_post_queue_depth *
2708 sizeof(Mpi2DefaultReplyDescriptor_t);
2709 if (_base_is_controller_msix_enabled(ioc))
2710 sz = reply_post_free_sz * ioc->reply_queue_count;
2711 else
2712 sz = reply_post_free_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002713 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2714 ioc->pdev, sz, 16, 0);
2715 if (!ioc->reply_post_free_dma_pool) {
2716 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
2717 "failed\n", ioc->name);
2718 goto out;
2719 }
2720 ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2721 GFP_KERNEL, &ioc->reply_post_free_dma);
2722 if (!ioc->reply_post_free) {
2723 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
2724 "failed\n", ioc->name);
2725 goto out;
2726 }
2727 memset(ioc->reply_post_free, 0, sz);
2728 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
2729 "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2730 ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2731 sz/1024));
2732 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
2733 "(0x%llx)\n", ioc->name, (unsigned long long)
2734 ioc->reply_post_free_dma));
2735 total_sz += sz;
2736
2737 ioc->config_page_sz = 512;
2738 ioc->config_page = pci_alloc_consistent(ioc->pdev,
2739 ioc->config_page_sz, &ioc->config_page_dma);
2740 if (!ioc->config_page) {
2741 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2742 "failed\n", ioc->name);
2743 goto out;
2744 }
2745 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2746 "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2747 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2748 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2749 total_sz += ioc->config_page_sz;
2750
2751 printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2752 ioc->name, total_sz/1024);
2753 printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2754 "Max Controller Queue Depth(%d)\n",
2755 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2756 printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2757 ioc->name, ioc->shost->sg_tablesize);
2758 return 0;
2759
2760 out:
Eric Moore635374e2009-03-09 01:21:12 -06002761 return -ENOMEM;
2762}
2763
2764
2765/**
2766 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2767 * @ioc: Pointer to MPT_ADAPTER structure
2768 * @cooked: Request raw or cooked IOC state
2769 *
2770 * Returns all IOC Doorbell register bits if cooked==0, else just the
2771 * Doorbell bits in MPI_IOC_STATE_MASK.
2772 */
2773u32
2774mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2775{
2776 u32 s, sc;
2777
2778 s = readl(&ioc->chip->Doorbell);
2779 sc = s & MPI2_IOC_STATE_MASK;
2780 return cooked ? sc : s;
2781}
2782
2783/**
2784 * _base_wait_on_iocstate - waiting on a particular ioc state
2785 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2786 * @timeout: timeout in second
2787 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2788 *
2789 * Returns 0 for success, non-zero for failure.
2790 */
2791static int
2792_base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2793 int sleep_flag)
2794{
2795 u32 count, cntdn;
2796 u32 current_state;
2797
2798 count = 0;
2799 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2800 do {
2801 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2802 if (current_state == ioc_state)
2803 return 0;
2804 if (count && current_state == MPI2_IOC_STATE_FAULT)
2805 break;
2806 if (sleep_flag == CAN_SLEEP)
2807 msleep(1);
2808 else
2809 udelay(500);
2810 count++;
2811 } while (--cntdn);
2812
2813 return current_state;
2814}
2815
2816/**
2817 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2818 * a write to the doorbell)
2819 * @ioc: per adapter object
2820 * @timeout: timeout in second
2821 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2822 *
2823 * Returns 0 for success, non-zero for failure.
2824 *
2825 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2826 */
2827static int
2828_base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2829 int sleep_flag)
2830{
2831 u32 cntdn, count;
2832 u32 int_status;
2833
2834 count = 0;
2835 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2836 do {
2837 int_status = readl(&ioc->chip->HostInterruptStatus);
2838 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302839 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002840 "successful count(%d), timeout(%d)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002841 __func__, count, timeout));
2842 return 0;
2843 }
2844 if (sleep_flag == CAN_SLEEP)
2845 msleep(1);
2846 else
2847 udelay(500);
2848 count++;
2849 } while (--cntdn);
2850
2851 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2852 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2853 return -EFAULT;
2854}
2855
2856/**
2857 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2858 * @ioc: per adapter object
2859 * @timeout: timeout in second
2860 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2861 *
2862 * Returns 0 for success, non-zero for failure.
2863 *
2864 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2865 * doorbell.
2866 */
2867static int
2868_base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2869 int sleep_flag)
2870{
2871 u32 cntdn, count;
2872 u32 int_status;
2873 u32 doorbell;
2874
2875 count = 0;
2876 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2877 do {
2878 int_status = readl(&ioc->chip->HostInterruptStatus);
2879 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302880 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002881 "successful count(%d), timeout(%d)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002882 __func__, count, timeout));
2883 return 0;
2884 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2885 doorbell = readl(&ioc->chip->Doorbell);
2886 if ((doorbell & MPI2_IOC_STATE_MASK) ==
2887 MPI2_IOC_STATE_FAULT) {
2888 mpt2sas_base_fault_info(ioc , doorbell);
2889 return -EFAULT;
2890 }
2891 } else if (int_status == 0xFFFFFFFF)
2892 goto out;
2893
2894 if (sleep_flag == CAN_SLEEP)
2895 msleep(1);
2896 else
2897 udelay(500);
2898 count++;
2899 } while (--cntdn);
2900
2901 out:
2902 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2903 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2904 return -EFAULT;
2905}
2906
2907/**
2908 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2909 * @ioc: per adapter object
2910 * @timeout: timeout in second
2911 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2912 *
2913 * Returns 0 for success, non-zero for failure.
2914 *
2915 */
2916static int
2917_base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2918 int sleep_flag)
2919{
2920 u32 cntdn, count;
2921 u32 doorbell_reg;
2922
2923 count = 0;
2924 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2925 do {
2926 doorbell_reg = readl(&ioc->chip->Doorbell);
2927 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302928 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002929 "successful count(%d), timeout(%d)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002930 __func__, count, timeout));
2931 return 0;
2932 }
2933 if (sleep_flag == CAN_SLEEP)
2934 msleep(1);
2935 else
2936 udelay(500);
2937 count++;
2938 } while (--cntdn);
2939
2940 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2941 "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2942 return -EFAULT;
2943}
2944
2945/**
2946 * _base_send_ioc_reset - send doorbell reset
2947 * @ioc: per adapter object
2948 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2949 * @timeout: timeout in second
2950 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2951 *
2952 * Returns 0 for success, non-zero for failure.
2953 */
2954static int
2955_base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2956 int sleep_flag)
2957{
2958 u32 ioc_state;
2959 int r = 0;
2960
2961 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2962 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2963 ioc->name, __func__);
2964 return -EFAULT;
2965 }
2966
2967 if (!(ioc->facts.IOCCapabilities &
2968 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2969 return -EFAULT;
2970
2971 printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
2972
2973 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
2974 &ioc->chip->Doorbell);
2975 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
2976 r = -EFAULT;
2977 goto out;
2978 }
2979 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
2980 timeout, sleep_flag);
2981 if (ioc_state) {
2982 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
2983 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
2984 r = -EFAULT;
2985 goto out;
2986 }
2987 out:
2988 printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
2989 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
2990 return r;
2991}
2992
2993/**
2994 * _base_handshake_req_reply_wait - send request thru doorbell interface
2995 * @ioc: per adapter object
2996 * @request_bytes: request length
2997 * @request: pointer having request payload
2998 * @reply_bytes: reply length
2999 * @reply: pointer to reply payload
3000 * @timeout: timeout in second
3001 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3002 *
3003 * Returns 0 for success, non-zero for failure.
3004 */
3005static int
3006_base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
3007 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
3008{
3009 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
3010 int i;
3011 u8 failed;
3012 u16 dummy;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303013 __le32 *mfp;
Eric Moore635374e2009-03-09 01:21:12 -06003014
3015 /* make sure doorbell is not in use */
3016 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
3017 printk(MPT2SAS_ERR_FMT "doorbell is in use "
3018 " (line=%d)\n", ioc->name, __LINE__);
3019 return -EFAULT;
3020 }
3021
3022 /* clear pending doorbell interrupts from previous state changes */
3023 if (readl(&ioc->chip->HostInterruptStatus) &
3024 MPI2_HIS_IOC2SYS_DB_STATUS)
3025 writel(0, &ioc->chip->HostInterruptStatus);
3026
3027 /* send message to ioc */
3028 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
3029 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
3030 &ioc->chip->Doorbell);
3031
Kashyap, Desai29786e12009-09-14 11:06:21 +05303032 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
Eric Moore635374e2009-03-09 01:21:12 -06003033 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3034 "int failed (line=%d)\n", ioc->name, __LINE__);
3035 return -EFAULT;
3036 }
3037 writel(0, &ioc->chip->HostInterruptStatus);
3038
3039 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
3040 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3041 "ack failed (line=%d)\n", ioc->name, __LINE__);
3042 return -EFAULT;
3043 }
3044
3045 /* send message 32-bits at a time */
3046 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
3047 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
3048 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
3049 failed = 1;
3050 }
3051
3052 if (failed) {
3053 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3054 "sending request failed (line=%d)\n", ioc->name, __LINE__);
3055 return -EFAULT;
3056 }
3057
3058 /* now wait for the reply */
3059 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
3060 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3061 "int failed (line=%d)\n", ioc->name, __LINE__);
3062 return -EFAULT;
3063 }
3064
3065 /* read the first two 16-bits, it gives the total length of the reply */
3066 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3067 & MPI2_DOORBELL_DATA_MASK);
3068 writel(0, &ioc->chip->HostInterruptStatus);
3069 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3070 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3071 "int failed (line=%d)\n", ioc->name, __LINE__);
3072 return -EFAULT;
3073 }
3074 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3075 & MPI2_DOORBELL_DATA_MASK);
3076 writel(0, &ioc->chip->HostInterruptStatus);
3077
3078 for (i = 2; i < default_reply->MsgLength * 2; i++) {
3079 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3080 printk(MPT2SAS_ERR_FMT "doorbell "
3081 "handshake int failed (line=%d)\n", ioc->name,
3082 __LINE__);
3083 return -EFAULT;
3084 }
3085 if (i >= reply_bytes/2) /* overflow case */
3086 dummy = readl(&ioc->chip->Doorbell);
3087 else
3088 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3089 & MPI2_DOORBELL_DATA_MASK);
3090 writel(0, &ioc->chip->HostInterruptStatus);
3091 }
3092
3093 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3094 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3095 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
3096 " (line=%d)\n", ioc->name, __LINE__));
3097 }
3098 writel(0, &ioc->chip->HostInterruptStatus);
3099
3100 if (ioc->logging_level & MPT_DEBUG_INIT) {
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303101 mfp = (__le32 *)reply;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303102 printk(KERN_INFO "\toffset:data\n");
Eric Moore635374e2009-03-09 01:21:12 -06003103 for (i = 0; i < reply_bytes/4; i++)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303104 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
Eric Moore635374e2009-03-09 01:21:12 -06003105 le32_to_cpu(mfp[i]));
3106 }
3107 return 0;
3108}
3109
3110/**
3111 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
3112 * @ioc: per adapter object
3113 * @mpi_reply: the reply payload from FW
3114 * @mpi_request: the request payload sent to FW
3115 *
3116 * The SAS IO Unit Control Request message allows the host to perform low-level
3117 * operations, such as resets on the PHYs of the IO Unit, also allows the host
3118 * to obtain the IOC assigned device handles for a device if it has other
3119 * identifying information about the device, in addition allows the host to
3120 * remove IOC resources associated with the device.
3121 *
3122 * Returns 0 for success, non-zero for failure.
3123 */
3124int
3125mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
3126 Mpi2SasIoUnitControlReply_t *mpi_reply,
3127 Mpi2SasIoUnitControlRequest_t *mpi_request)
3128{
3129 u16 smid;
3130 u32 ioc_state;
3131 unsigned long timeleft;
3132 u8 issue_reset;
3133 int rc;
3134 void *request;
3135 u16 wait_state_count;
3136
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303137 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003138 __func__));
3139
3140 mutex_lock(&ioc->base_cmds.mutex);
3141
3142 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3143 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3144 ioc->name, __func__);
3145 rc = -EAGAIN;
3146 goto out;
3147 }
3148
3149 wait_state_count = 0;
3150 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3151 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3152 if (wait_state_count++ == 10) {
3153 printk(MPT2SAS_ERR_FMT
3154 "%s: failed due to ioc not operational\n",
3155 ioc->name, __func__);
3156 rc = -EFAULT;
3157 goto out;
3158 }
3159 ssleep(1);
3160 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3161 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3162 "operational state(count=%d)\n", ioc->name,
3163 __func__, wait_state_count);
3164 }
3165
3166 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3167 if (!smid) {
3168 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3169 ioc->name, __func__);
3170 rc = -EAGAIN;
3171 goto out;
3172 }
3173
3174 rc = 0;
3175 ioc->base_cmds.status = MPT2_CMD_PENDING;
3176 request = mpt2sas_base_get_msg_frame(ioc, smid);
3177 ioc->base_cmds.smid = smid;
3178 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3179 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3180 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3181 ioc->ioc_link_reset_in_progress = 1;
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303182 init_completion(&ioc->base_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05303183 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003184 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3185 msecs_to_jiffies(10000));
3186 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3187 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
3188 ioc->ioc_link_reset_in_progress)
3189 ioc->ioc_link_reset_in_progress = 0;
3190 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3191 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3192 ioc->name, __func__);
3193 _debug_dump_mf(mpi_request,
3194 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
3195 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3196 issue_reset = 1;
3197 goto issue_host_reset;
3198 }
3199 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3200 memcpy(mpi_reply, ioc->base_cmds.reply,
3201 sizeof(Mpi2SasIoUnitControlReply_t));
3202 else
3203 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
3204 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3205 goto out;
3206
3207 issue_host_reset:
3208 if (issue_reset)
3209 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3210 FORCE_BIG_HAMMER);
3211 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3212 rc = -EFAULT;
3213 out:
3214 mutex_unlock(&ioc->base_cmds.mutex);
3215 return rc;
3216}
3217
3218
3219/**
3220 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
3221 * @ioc: per adapter object
3222 * @mpi_reply: the reply payload from FW
3223 * @mpi_request: the request payload sent to FW
3224 *
3225 * The SCSI Enclosure Processor request message causes the IOC to
3226 * communicate with SES devices to control LED status signals.
3227 *
3228 * Returns 0 for success, non-zero for failure.
3229 */
3230int
3231mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
3232 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
3233{
3234 u16 smid;
3235 u32 ioc_state;
3236 unsigned long timeleft;
3237 u8 issue_reset;
3238 int rc;
3239 void *request;
3240 u16 wait_state_count;
3241
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303242 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003243 __func__));
3244
3245 mutex_lock(&ioc->base_cmds.mutex);
3246
3247 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3248 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3249 ioc->name, __func__);
3250 rc = -EAGAIN;
3251 goto out;
3252 }
3253
3254 wait_state_count = 0;
3255 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3256 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3257 if (wait_state_count++ == 10) {
3258 printk(MPT2SAS_ERR_FMT
3259 "%s: failed due to ioc not operational\n",
3260 ioc->name, __func__);
3261 rc = -EFAULT;
3262 goto out;
3263 }
3264 ssleep(1);
3265 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3266 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3267 "operational state(count=%d)\n", ioc->name,
3268 __func__, wait_state_count);
3269 }
3270
3271 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3272 if (!smid) {
3273 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3274 ioc->name, __func__);
3275 rc = -EAGAIN;
3276 goto out;
3277 }
3278
3279 rc = 0;
3280 ioc->base_cmds.status = MPT2_CMD_PENDING;
3281 request = mpt2sas_base_get_msg_frame(ioc, smid);
3282 ioc->base_cmds.smid = smid;
3283 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303284 init_completion(&ioc->base_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05303285 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003286 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3287 msecs_to_jiffies(10000));
3288 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3289 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3290 ioc->name, __func__);
3291 _debug_dump_mf(mpi_request,
3292 sizeof(Mpi2SepRequest_t)/4);
3293 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3294 issue_reset = 1;
3295 goto issue_host_reset;
3296 }
3297 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3298 memcpy(mpi_reply, ioc->base_cmds.reply,
3299 sizeof(Mpi2SepReply_t));
3300 else
3301 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3302 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3303 goto out;
3304
3305 issue_host_reset:
3306 if (issue_reset)
3307 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3308 FORCE_BIG_HAMMER);
3309 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3310 rc = -EFAULT;
3311 out:
3312 mutex_unlock(&ioc->base_cmds.mutex);
3313 return rc;
3314}
3315
3316/**
3317 * _base_get_port_facts - obtain port facts reply and save in ioc
3318 * @ioc: per adapter object
3319 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3320 *
3321 * Returns 0 for success, non-zero for failure.
3322 */
3323static int
3324_base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
3325{
3326 Mpi2PortFactsRequest_t mpi_request;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303327 Mpi2PortFactsReply_t mpi_reply;
3328 struct mpt2sas_port_facts *pfacts;
Eric Moore635374e2009-03-09 01:21:12 -06003329 int mpi_reply_sz, mpi_request_sz, r;
3330
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303331 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003332 __func__));
3333
3334 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3335 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3336 memset(&mpi_request, 0, mpi_request_sz);
3337 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3338 mpi_request.PortNumber = port;
3339 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3340 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3341
3342 if (r != 0) {
3343 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3344 ioc->name, __func__, r);
3345 return r;
3346 }
3347
3348 pfacts = &ioc->pfacts[port];
nagalakshmi.nandigama@lsi.com3102e702012-03-20 12:10:01 +05303349 memset(pfacts, 0, sizeof(struct mpt2sas_port_facts));
Eric Moore635374e2009-03-09 01:21:12 -06003350 pfacts->PortNumber = mpi_reply.PortNumber;
3351 pfacts->VP_ID = mpi_reply.VP_ID;
3352 pfacts->VF_ID = mpi_reply.VF_ID;
3353 pfacts->MaxPostedCmdBuffers =
3354 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3355
3356 return 0;
3357}
3358
3359/**
3360 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3361 * @ioc: per adapter object
3362 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3363 *
3364 * Returns 0 for success, non-zero for failure.
3365 */
3366static int
3367_base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3368{
3369 Mpi2IOCFactsRequest_t mpi_request;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303370 Mpi2IOCFactsReply_t mpi_reply;
3371 struct mpt2sas_facts *facts;
Eric Moore635374e2009-03-09 01:21:12 -06003372 int mpi_reply_sz, mpi_request_sz, r;
3373
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303374 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003375 __func__));
3376
3377 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3378 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3379 memset(&mpi_request, 0, mpi_request_sz);
3380 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3381 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3382 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3383
3384 if (r != 0) {
3385 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3386 ioc->name, __func__, r);
3387 return r;
3388 }
3389
3390 facts = &ioc->facts;
nagalakshmi.nandigama@lsi.com3102e702012-03-20 12:10:01 +05303391 memset(facts, 0, sizeof(struct mpt2sas_facts));
Eric Moore635374e2009-03-09 01:21:12 -06003392 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3393 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3394 facts->VP_ID = mpi_reply.VP_ID;
3395 facts->VF_ID = mpi_reply.VF_ID;
3396 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3397 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3398 facts->WhoInit = mpi_reply.WhoInit;
3399 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05303400 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
Eric Moore635374e2009-03-09 01:21:12 -06003401 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3402 facts->MaxReplyDescriptorPostQueueDepth =
3403 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3404 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3405 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3406 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3407 ioc->ir_firmware = 1;
3408 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3409 facts->IOCRequestFrameSize =
3410 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3411 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3412 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3413 ioc->shost->max_id = -1;
3414 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3415 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3416 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3417 facts->HighPriorityCredit =
3418 le16_to_cpu(mpi_reply.HighPriorityCredit);
3419 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3420 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3421
3422 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
3423 "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
3424 facts->MaxChainDepth));
3425 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
3426 "reply frame size(%d)\n", ioc->name,
3427 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3428 return 0;
3429}
3430
3431/**
3432 * _base_send_ioc_init - send ioc_init to firmware
3433 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003434 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3435 *
3436 * Returns 0 for success, non-zero for failure.
3437 */
3438static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303439_base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003440{
3441 Mpi2IOCInitRequest_t mpi_request;
3442 Mpi2IOCInitReply_t mpi_reply;
3443 int r;
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303444 struct timeval current_time;
Kashyap, Desai463217b2009-10-05 15:53:06 +05303445 u16 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06003446
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303447 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003448 __func__));
3449
3450 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3451 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3452 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303453 mpi_request.VF_ID = 0; /* TODO */
3454 mpi_request.VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003455 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3456 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3457
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05303458 if (_base_is_controller_msix_enabled(ioc))
3459 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
Eric Moore635374e2009-03-09 01:21:12 -06003460 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3461 mpi_request.ReplyDescriptorPostQueueDepth =
3462 cpu_to_le16(ioc->reply_post_queue_depth);
3463 mpi_request.ReplyFreeQueueDepth =
3464 cpu_to_le16(ioc->reply_free_queue_depth);
3465
Eric Moore635374e2009-03-09 01:21:12 -06003466 mpi_request.SenseBufferAddressHigh =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303467 cpu_to_le32((u64)ioc->sense_dma >> 32);
Eric Moore635374e2009-03-09 01:21:12 -06003468 mpi_request.SystemReplyAddressHigh =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303469 cpu_to_le32((u64)ioc->reply_dma >> 32);
Eric Moore635374e2009-03-09 01:21:12 -06003470 mpi_request.SystemRequestFrameBaseAddress =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303471 cpu_to_le64((u64)ioc->request_dma);
Eric Moore635374e2009-03-09 01:21:12 -06003472 mpi_request.ReplyFreeQueueAddress =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303473 cpu_to_le64((u64)ioc->reply_free_dma);
Eric Moore635374e2009-03-09 01:21:12 -06003474 mpi_request.ReplyDescriptorPostQueueAddress =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303475 cpu_to_le64((u64)ioc->reply_post_free_dma);
3476
Eric Moore635374e2009-03-09 01:21:12 -06003477
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303478 /* This time stamp specifies number of milliseconds
3479 * since epoch ~ midnight January 1, 1970.
3480 */
3481 do_gettimeofday(&current_time);
Kashyap, Desai7921b352010-03-17 16:21:33 +05303482 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3483 (current_time.tv_usec / 1000));
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303484
Eric Moore635374e2009-03-09 01:21:12 -06003485 if (ioc->logging_level & MPT_DEBUG_INIT) {
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303486 __le32 *mfp;
Eric Moore635374e2009-03-09 01:21:12 -06003487 int i;
3488
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303489 mfp = (__le32 *)&mpi_request;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303490 printk(KERN_INFO "\toffset:data\n");
Eric Moore635374e2009-03-09 01:21:12 -06003491 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303492 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
Eric Moore635374e2009-03-09 01:21:12 -06003493 le32_to_cpu(mfp[i]));
3494 }
3495
3496 r = _base_handshake_req_reply_wait(ioc,
3497 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3498 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3499 sleep_flag);
3500
3501 if (r != 0) {
3502 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3503 ioc->name, __func__, r);
3504 return r;
3505 }
3506
Kashyap, Desai463217b2009-10-05 15:53:06 +05303507 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3508 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
Eric Moore635374e2009-03-09 01:21:12 -06003509 mpi_reply.IOCLogInfo) {
3510 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
3511 r = -EIO;
3512 }
3513
3514 return 0;
3515}
3516
3517/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303518 * mpt2sas_port_enable_done - command completion routine for port enable
3519 * @ioc: per adapter object
3520 * @smid: system request message index
3521 * @msix_index: MSIX table index supplied by the OS
3522 * @reply: reply message frame(lower 32bit addr)
3523 *
3524 * Return 1 meaning mf should be freed from _base_interrupt
3525 * 0 means the mf is freed from this function.
3526 */
3527u8
3528mpt2sas_port_enable_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3529 u32 reply)
3530{
3531 MPI2DefaultReply_t *mpi_reply;
3532 u16 ioc_status;
3533
3534 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3535 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
3536 return 1;
3537
3538 if (ioc->port_enable_cmds.status == MPT2_CMD_NOT_USED)
3539 return 1;
3540
3541 ioc->port_enable_cmds.status |= MPT2_CMD_COMPLETE;
3542 if (mpi_reply) {
3543 ioc->port_enable_cmds.status |= MPT2_CMD_REPLY_VALID;
3544 memcpy(ioc->port_enable_cmds.reply, mpi_reply,
3545 mpi_reply->MsgLength*4);
3546 }
3547 ioc->port_enable_cmds.status &= ~MPT2_CMD_PENDING;
3548
3549 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3550
3551 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3552 ioc->port_enable_failed = 1;
3553
3554 if (ioc->is_driver_loading) {
3555 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
3556 mpt2sas_port_enable_complete(ioc);
3557 return 1;
3558 } else {
3559 ioc->start_scan_failed = ioc_status;
3560 ioc->start_scan = 0;
3561 return 1;
3562 }
3563 }
3564 complete(&ioc->port_enable_cmds.done);
3565 return 1;
3566}
3567
3568
3569/**
Eric Moore635374e2009-03-09 01:21:12 -06003570 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3571 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003572 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3573 *
3574 * Returns 0 for success, non-zero for failure.
3575 */
3576static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303577_base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003578{
3579 Mpi2PortEnableRequest_t *mpi_request;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303580 Mpi2PortEnableReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06003581 unsigned long timeleft;
3582 int r = 0;
3583 u16 smid;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303584 u16 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06003585
3586 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3587
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303588 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
Eric Moore635374e2009-03-09 01:21:12 -06003589 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3590 ioc->name, __func__);
3591 return -EAGAIN;
3592 }
3593
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303594 smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06003595 if (!smid) {
3596 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3597 ioc->name, __func__);
3598 return -EAGAIN;
3599 }
3600
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303601 ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
Eric Moore635374e2009-03-09 01:21:12 -06003602 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303603 ioc->port_enable_cmds.smid = smid;
Eric Moore635374e2009-03-09 01:21:12 -06003604 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3605 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
Eric Moore635374e2009-03-09 01:21:12 -06003606
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303607 init_completion(&ioc->port_enable_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303608 mpt2sas_base_put_smid_default(ioc, smid);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303609 timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
Eric Moore635374e2009-03-09 01:21:12 -06003610 300*HZ);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303611 if (!(ioc->port_enable_cmds.status & MPT2_CMD_COMPLETE)) {
Eric Moore635374e2009-03-09 01:21:12 -06003612 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3613 ioc->name, __func__);
3614 _debug_dump_mf(mpi_request,
3615 sizeof(Mpi2PortEnableRequest_t)/4);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303616 if (ioc->port_enable_cmds.status & MPT2_CMD_RESET)
Eric Moore635374e2009-03-09 01:21:12 -06003617 r = -EFAULT;
3618 else
3619 r = -ETIME;
3620 goto out;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303621 }
3622 mpi_reply = ioc->port_enable_cmds.reply;
Eric Moore635374e2009-03-09 01:21:12 -06003623
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303624 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3625 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3626 printk(MPT2SAS_ERR_FMT "%s: failed with (ioc_status=0x%08x)\n",
3627 ioc->name, __func__, ioc_status);
Eric Moore635374e2009-03-09 01:21:12 -06003628 r = -EFAULT;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303629 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003630 }
3631 out:
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303632 ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
3633 printk(MPT2SAS_INFO_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
3634 "SUCCESS" : "FAILED"));
Eric Moore635374e2009-03-09 01:21:12 -06003635 return r;
3636}
3637
3638/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303639 * mpt2sas_port_enable - initiate firmware discovery (don't wait for reply)
3640 * @ioc: per adapter object
3641 *
3642 * Returns 0 for success, non-zero for failure.
3643 */
3644int
3645mpt2sas_port_enable(struct MPT2SAS_ADAPTER *ioc)
3646{
3647 Mpi2PortEnableRequest_t *mpi_request;
3648 u16 smid;
3649
3650 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3651
3652 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3653 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3654 ioc->name, __func__);
3655 return -EAGAIN;
3656 }
3657
3658 smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3659 if (!smid) {
3660 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3661 ioc->name, __func__);
3662 return -EAGAIN;
3663 }
3664
3665 ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3666 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3667 ioc->port_enable_cmds.smid = smid;
3668 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3669 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3670
3671 mpt2sas_base_put_smid_default(ioc, smid);
3672 return 0;
3673}
3674
3675/**
3676 * _base_determine_wait_on_discovery - desposition
3677 * @ioc: per adapter object
3678 *
3679 * Decide whether to wait on discovery to complete. Used to either
3680 * locate boot device, or report volumes ahead of physical devices.
3681 *
3682 * Returns 1 for wait, 0 for don't wait
3683 */
3684static int
3685_base_determine_wait_on_discovery(struct MPT2SAS_ADAPTER *ioc)
3686{
3687 /* We wait for discovery to complete if IR firmware is loaded.
3688 * The sas topology events arrive before PD events, so we need time to
3689 * turn on the bit in ioc->pd_handles to indicate PD
3690 * Also, it maybe required to report Volumes ahead of physical
3691 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
3692 */
3693 if (ioc->ir_firmware)
3694 return 1;
3695
3696 /* if no Bios, then we don't need to wait */
3697 if (!ioc->bios_pg3.BiosVersion)
3698 return 0;
3699
3700 /* Bios is present, then we drop down here.
3701 *
3702 * If there any entries in the Bios Page 2, then we wait
3703 * for discovery to complete.
3704 */
3705
3706 /* Current Boot Device */
3707 if ((ioc->bios_pg2.CurrentBootDeviceForm &
3708 MPI2_BIOSPAGE2_FORM_MASK) ==
3709 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3710 /* Request Boot Device */
3711 (ioc->bios_pg2.ReqBootDeviceForm &
3712 MPI2_BIOSPAGE2_FORM_MASK) ==
3713 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3714 /* Alternate Request Boot Device */
3715 (ioc->bios_pg2.ReqAltBootDeviceForm &
3716 MPI2_BIOSPAGE2_FORM_MASK) ==
3717 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
3718 return 0;
3719
3720 return 1;
3721}
3722
3723
3724/**
Eric Moore635374e2009-03-09 01:21:12 -06003725 * _base_unmask_events - turn on notification for this event
3726 * @ioc: per adapter object
3727 * @event: firmware event
3728 *
3729 * The mask is stored in ioc->event_masks.
3730 */
3731static void
3732_base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3733{
3734 u32 desired_event;
3735
3736 if (event >= 128)
3737 return;
3738
3739 desired_event = (1 << (event % 32));
3740
3741 if (event < 32)
3742 ioc->event_masks[0] &= ~desired_event;
3743 else if (event < 64)
3744 ioc->event_masks[1] &= ~desired_event;
3745 else if (event < 96)
3746 ioc->event_masks[2] &= ~desired_event;
3747 else if (event < 128)
3748 ioc->event_masks[3] &= ~desired_event;
3749}
3750
3751/**
3752 * _base_event_notification - send event notification
3753 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003754 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3755 *
3756 * Returns 0 for success, non-zero for failure.
3757 */
3758static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303759_base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003760{
3761 Mpi2EventNotificationRequest_t *mpi_request;
3762 unsigned long timeleft;
3763 u16 smid;
3764 int r = 0;
3765 int i;
3766
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303767 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003768 __func__));
3769
3770 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3771 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3772 ioc->name, __func__);
3773 return -EAGAIN;
3774 }
3775
3776 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3777 if (!smid) {
3778 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3779 ioc->name, __func__);
3780 return -EAGAIN;
3781 }
3782 ioc->base_cmds.status = MPT2_CMD_PENDING;
3783 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3784 ioc->base_cmds.smid = smid;
3785 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
3786 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303787 mpi_request->VF_ID = 0; /* TODO */
3788 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003789 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3790 mpi_request->EventMasks[i] =
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303791 cpu_to_le32(ioc->event_masks[i]);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303792 init_completion(&ioc->base_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05303793 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003794 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
3795 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3796 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3797 ioc->name, __func__);
3798 _debug_dump_mf(mpi_request,
3799 sizeof(Mpi2EventNotificationRequest_t)/4);
3800 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3801 r = -EFAULT;
3802 else
3803 r = -ETIME;
3804 } else
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303805 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
Eric Moore635374e2009-03-09 01:21:12 -06003806 ioc->name, __func__));
3807 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3808 return r;
3809}
3810
3811/**
3812 * mpt2sas_base_validate_event_type - validating event types
3813 * @ioc: per adapter object
3814 * @event: firmware event
3815 *
3816 * This will turn on firmware event notification when application
3817 * ask for that event. We don't mask events that are already enabled.
3818 */
3819void
3820mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
3821{
3822 int i, j;
3823 u32 event_mask, desired_event;
3824 u8 send_update_to_fw;
3825
3826 for (i = 0, send_update_to_fw = 0; i <
3827 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
3828 event_mask = ~event_type[i];
3829 desired_event = 1;
3830 for (j = 0; j < 32; j++) {
3831 if (!(event_mask & desired_event) &&
3832 (ioc->event_masks[i] & desired_event)) {
3833 ioc->event_masks[i] &= ~desired_event;
3834 send_update_to_fw = 1;
3835 }
3836 desired_event = (desired_event << 1);
3837 }
3838 }
3839
3840 if (!send_update_to_fw)
3841 return;
3842
3843 mutex_lock(&ioc->base_cmds.mutex);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303844 _base_event_notification(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06003845 mutex_unlock(&ioc->base_cmds.mutex);
3846}
3847
3848/**
3849 * _base_diag_reset - the "big hammer" start of day reset
3850 * @ioc: per adapter object
3851 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3852 *
3853 * Returns 0 for success, non-zero for failure.
3854 */
3855static int
3856_base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3857{
3858 u32 host_diagnostic;
3859 u32 ioc_state;
3860 u32 count;
3861 u32 hcb_size;
3862
3863 printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303864 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
Eric Moore635374e2009-03-09 01:21:12 -06003865 ioc->name));
Eric Moore635374e2009-03-09 01:21:12 -06003866
3867 count = 0;
3868 do {
3869 /* Write magic sequence to WriteSequence register
3870 * Loop until in diagnostic mode
3871 */
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303872 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
Eric Moore635374e2009-03-09 01:21:12 -06003873 "sequence\n", ioc->name));
3874 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3875 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
3876 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
3877 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
3878 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
3879 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
3880 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
3881
3882 /* wait 100 msec */
3883 if (sleep_flag == CAN_SLEEP)
3884 msleep(100);
3885 else
3886 mdelay(100);
3887
3888 if (count++ > 20)
3889 goto out;
3890
3891 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303892 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
Eric Moore635374e2009-03-09 01:21:12 -06003893 "sequence: count(%d), host_diagnostic(0x%08x)\n",
3894 ioc->name, count, host_diagnostic));
3895
3896 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
3897
3898 hcb_size = readl(&ioc->chip->HCBSize);
3899
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303900 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
Eric Moore635374e2009-03-09 01:21:12 -06003901 ioc->name));
3902 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
3903 &ioc->chip->HostDiagnostic);
3904
3905 /* don't access any registers for 50 milliseconds */
3906 msleep(50);
3907
3908 /* 300 second max wait */
3909 for (count = 0; count < 3000000 ; count++) {
3910
3911 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3912
3913 if (host_diagnostic == 0xFFFFFFFF)
3914 goto out;
3915 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3916 break;
3917
3918 /* wait 100 msec */
3919 if (sleep_flag == CAN_SLEEP)
3920 msleep(1);
3921 else
3922 mdelay(1);
3923 }
3924
3925 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3926
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303927 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
Eric Moore635374e2009-03-09 01:21:12 -06003928 "assuming the HCB Address points to good F/W\n",
3929 ioc->name));
3930 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3931 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3932 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3933
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303934 drsprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003935 "re-enable the HCDW\n", ioc->name));
3936 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3937 &ioc->chip->HCBSize);
3938 }
3939
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303940 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
Eric Moore635374e2009-03-09 01:21:12 -06003941 ioc->name));
3942 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3943 &ioc->chip->HostDiagnostic);
3944
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303945 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
Eric Moore635374e2009-03-09 01:21:12 -06003946 "diagnostic register\n", ioc->name));
3947 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3948
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303949 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
Eric Moore635374e2009-03-09 01:21:12 -06003950 "READY state\n", ioc->name));
3951 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3952 sleep_flag);
3953 if (ioc_state) {
3954 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3955 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3956 goto out;
3957 }
3958
Eric Moore635374e2009-03-09 01:21:12 -06003959 printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3960 return 0;
3961
3962 out:
3963 printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3964 return -EFAULT;
3965}
3966
3967/**
3968 * _base_make_ioc_ready - put controller in READY state
3969 * @ioc: per adapter object
3970 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3971 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3972 *
3973 * Returns 0 for success, non-zero for failure.
3974 */
3975static int
3976_base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3977 enum reset_type type)
3978{
3979 u32 ioc_state;
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303980 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06003981
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303982 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003983 __func__));
3984
Eric Moore3cb54692010-07-08 14:44:34 -06003985 if (ioc->pci_error_recovery)
3986 return 0;
3987
Eric Moore635374e2009-03-09 01:21:12 -06003988 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303989 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
Eric Moore635374e2009-03-09 01:21:12 -06003990 ioc->name, __func__, ioc_state));
3991
3992 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
3993 return 0;
3994
3995 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303996 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06003997 "active!\n", ioc->name));
3998 goto issue_diag_reset;
3999 }
4000
4001 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4002 mpt2sas_base_fault_info(ioc, ioc_state &
4003 MPI2_DOORBELL_DATA_MASK);
4004 goto issue_diag_reset;
4005 }
4006
4007 if (type == FORCE_BIG_HAMMER)
4008 goto issue_diag_reset;
4009
4010 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4011 if (!(_base_send_ioc_reset(ioc,
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05304012 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
4013 ioc->ioc_reset_count++;
Eric Moore635374e2009-03-09 01:21:12 -06004014 return 0;
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05304015 }
Eric Moore635374e2009-03-09 01:21:12 -06004016
4017 issue_diag_reset:
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05304018 rc = _base_diag_reset(ioc, CAN_SLEEP);
4019 ioc->ioc_reset_count++;
4020 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06004021}
4022
4023/**
4024 * _base_make_ioc_operational - put controller in OPERATIONAL state
4025 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004026 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4027 *
4028 * Returns 0 for success, non-zero for failure.
4029 */
4030static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304031_base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06004032{
4033 int r, i;
4034 unsigned long flags;
4035 u32 reply_address;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304036 u16 smid;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304037 struct _tr_list *delayed_tr, *delayed_tr_next;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304038 u8 hide_flag;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304039 struct adapter_reply_queue *reply_q;
4040 long reply_post_free;
4041 u32 reply_post_free_sz;
Eric Moore635374e2009-03-09 01:21:12 -06004042
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304043 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004044 __func__));
4045
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304046 /* clean the delayed target reset list */
4047 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4048 &ioc->delayed_tr_list, list) {
4049 list_del(&delayed_tr->list);
4050 kfree(delayed_tr);
4051 }
4052
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304053 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4054 &ioc->delayed_tr_volume_list, list) {
4055 list_del(&delayed_tr->list);
4056 kfree(delayed_tr);
4057 }
4058
Eric Moore635374e2009-03-09 01:21:12 -06004059 /* initialize the scsi lookup free list */
4060 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4061 INIT_LIST_HEAD(&ioc->free_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304062 smid = 1;
4063 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
Kashyap, Desai35f805b2010-11-13 04:34:06 +05304064 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
Eric Moore635374e2009-03-09 01:21:12 -06004065 ioc->scsi_lookup[i].cb_idx = 0xFF;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304066 ioc->scsi_lookup[i].smid = smid;
4067 ioc->scsi_lookup[i].scmd = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304068 ioc->scsi_lookup[i].direct_io = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004069 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
4070 &ioc->free_list);
4071 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304072
4073 /* hi-priority queue */
4074 INIT_LIST_HEAD(&ioc->hpr_free_list);
4075 smid = ioc->hi_priority_smid;
4076 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
4077 ioc->hpr_lookup[i].cb_idx = 0xFF;
4078 ioc->hpr_lookup[i].smid = smid;
4079 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
4080 &ioc->hpr_free_list);
4081 }
4082
4083 /* internal queue */
4084 INIT_LIST_HEAD(&ioc->internal_free_list);
4085 smid = ioc->internal_smid;
4086 for (i = 0; i < ioc->internal_depth; i++, smid++) {
4087 ioc->internal_lookup[i].cb_idx = 0xFF;
4088 ioc->internal_lookup[i].smid = smid;
4089 list_add_tail(&ioc->internal_lookup[i].tracker_list,
4090 &ioc->internal_free_list);
4091 }
Kashyap, Desai35f805b2010-11-13 04:34:06 +05304092
4093 /* chain pool */
4094 INIT_LIST_HEAD(&ioc->free_chain_list);
4095 for (i = 0; i < ioc->chain_depth; i++)
4096 list_add_tail(&ioc->chain_lookup[i].tracker_list,
4097 &ioc->free_chain_list);
4098
Eric Moore635374e2009-03-09 01:21:12 -06004099 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4100
4101 /* initialize Reply Free Queue */
4102 for (i = 0, reply_address = (u32)ioc->reply_dma ;
4103 i < ioc->reply_free_queue_depth ; i++, reply_address +=
4104 ioc->reply_sz)
4105 ioc->reply_free[i] = cpu_to_le32(reply_address);
4106
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304107 /* initialize reply queues */
nagalakshmi.nandigama@lsi.com2cb6fc82011-12-13 09:29:15 +05304108 if (ioc->is_driver_loading)
4109 _base_assign_reply_queues(ioc);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304110
Eric Moore635374e2009-03-09 01:21:12 -06004111 /* initialize Reply Post Free Queue */
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304112 reply_post_free = (long)ioc->reply_post_free;
4113 reply_post_free_sz = ioc->reply_post_queue_depth *
4114 sizeof(Mpi2DefaultReplyDescriptor_t);
4115 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4116 reply_q->reply_post_host_index = 0;
4117 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
4118 reply_post_free;
4119 for (i = 0; i < ioc->reply_post_queue_depth; i++)
4120 reply_q->reply_post_free[i].Words =
4121 cpu_to_le64(ULLONG_MAX);
4122 if (!_base_is_controller_msix_enabled(ioc))
4123 goto skip_init_reply_post_free_queue;
4124 reply_post_free += reply_post_free_sz;
4125 }
4126 skip_init_reply_post_free_queue:
Eric Moore635374e2009-03-09 01:21:12 -06004127
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304128 r = _base_send_ioc_init(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06004129 if (r)
4130 return r;
4131
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304132 /* initialize reply free host index */
Eric Moore635374e2009-03-09 01:21:12 -06004133 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
Eric Moore635374e2009-03-09 01:21:12 -06004134 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304135
4136 /* initialize reply post host index */
4137 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4138 writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
4139 &ioc->chip->ReplyPostHostIndex);
4140 if (!_base_is_controller_msix_enabled(ioc))
4141 goto skip_init_reply_post_host_index;
4142 }
4143
4144 skip_init_reply_post_host_index:
Eric Moore635374e2009-03-09 01:21:12 -06004145
4146 _base_unmask_interrupts(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304147
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304148 r = _base_event_notification(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06004149 if (r)
4150 return r;
4151
4152 if (sleep_flag == CAN_SLEEP)
4153 _base_static_config_pages(ioc);
4154
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304155
4156 if (ioc->is_driver_loading) {
nagalakshmi.nandigama@lsi.com2cb6fc82011-12-13 09:29:15 +05304157 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
4158 == 0x80) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304159 hide_flag = (u8) (ioc->manu_pg10.OEMSpecificFlags0 &
4160 MFG_PAGE10_HIDE_SSDS_MASK);
4161 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
4162 ioc->mfg_pg10_hide_flag = hide_flag;
4163 }
nagalakshmi.nandigama@lsi.com2cb6fc82011-12-13 09:29:15 +05304164 ioc->wait_for_discovery_to_complete =
4165 _base_determine_wait_on_discovery(ioc);
4166 return r; /* scan_start and scan_finished support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304167 }
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304168 r = _base_send_port_enable(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06004169 if (r)
4170 return r;
4171
4172 return r;
4173}
4174
4175/**
4176 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
4177 * @ioc: per adapter object
4178 *
4179 * Return nothing.
4180 */
4181void
4182mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
4183{
4184 struct pci_dev *pdev = ioc->pdev;
4185
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304186 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004187 __func__));
4188
4189 _base_mask_interrupts(ioc);
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05304190 ioc->shost_recovery = 1;
Eric Moore635374e2009-03-09 01:21:12 -06004191 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05304192 ioc->shost_recovery = 0;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304193 _base_free_irq(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004194 _base_disable_msix(ioc);
4195 if (ioc->chip_phys)
4196 iounmap(ioc->chip);
Eric Moore635374e2009-03-09 01:21:12 -06004197 ioc->chip_phys = 0;
4198 pci_release_selected_regions(ioc->pdev, ioc->bars);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05304199 pci_disable_pcie_error_reporting(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06004200 pci_disable_device(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06004201 return;
4202}
4203
4204/**
4205 * mpt2sas_base_attach - attach controller instance
4206 * @ioc: per adapter object
4207 *
4208 * Returns 0 for success, non-zero for failure.
4209 */
4210int
4211mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
4212{
4213 int r, i;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304214 int cpu_id, last_cpu_id = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004215
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304216 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004217 __func__));
4218
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304219 /* setup cpu_msix_table */
4220 ioc->cpu_count = num_online_cpus();
4221 for_each_online_cpu(cpu_id)
4222 last_cpu_id = cpu_id;
4223 ioc->cpu_msix_table_sz = last_cpu_id + 1;
4224 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
4225 ioc->reply_queue_count = 1;
4226 if (!ioc->cpu_msix_table) {
4227 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
4228 "cpu_msix_table failed!!!\n", ioc->name));
4229 r = -ENOMEM;
4230 goto out_free_resources;
4231 }
4232
4233 if (ioc->is_warpdrive) {
4234 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
4235 sizeof(resource_size_t *), GFP_KERNEL);
4236 if (!ioc->reply_post_host_index) {
4237 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation "
4238 "for cpu_msix_table failed!!!\n", ioc->name));
4239 r = -ENOMEM;
4240 goto out_free_resources;
4241 }
4242 }
4243
Eric Moore635374e2009-03-09 01:21:12 -06004244 r = mpt2sas_base_map_resources(ioc);
4245 if (r)
Roland Dreierc24a1712011-11-30 17:14:22 -08004246 goto out_free_resources;
Eric Moore635374e2009-03-09 01:21:12 -06004247
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304248 if (ioc->is_warpdrive) {
4249 ioc->reply_post_host_index[0] =
4250 (resource_size_t *)&ioc->chip->ReplyPostHostIndex;
4251
4252 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
4253 ioc->reply_post_host_index[i] = (resource_size_t *)
4254 ((u8 *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
4255 * 4)));
4256 }
4257
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05304258 pci_set_drvdata(ioc->pdev, ioc->shost);
Kashyap, Desai96b681c2009-09-23 17:32:06 +05304259 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06004260 if (r)
4261 goto out_free_resources;
4262
Kashyap, Desai96b681c2009-09-23 17:32:06 +05304263 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
Eric Moore635374e2009-03-09 01:21:12 -06004264 if (r)
4265 goto out_free_resources;
4266
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304267 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
nagalakshmi.nandigama@lsi.com3102e702012-03-20 12:10:01 +05304268 sizeof(struct mpt2sas_port_facts), GFP_KERNEL);
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304269 if (!ioc->pfacts) {
4270 r = -ENOMEM;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304271 goto out_free_resources;
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304272 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304273
4274 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
4275 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
4276 if (r)
4277 goto out_free_resources;
4278 }
4279
Eric Moore635374e2009-03-09 01:21:12 -06004280 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
4281 if (r)
4282 goto out_free_resources;
4283
4284 init_waitqueue_head(&ioc->reset_wq);
4285
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304286 /* allocate memory pd handle bitmask list */
4287 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
4288 if (ioc->facts.MaxDevHandle % 8)
4289 ioc->pd_handles_sz++;
4290 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
4291 GFP_KERNEL);
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05304292 if (!ioc->pd_handles) {
4293 r = -ENOMEM;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304294 goto out_free_resources;
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05304295 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304296
Kashyap, Desaie75b9b62009-12-16 18:52:39 +05304297 ioc->fwfault_debug = mpt2sas_fwfault_debug;
4298
Eric Moore635374e2009-03-09 01:21:12 -06004299 /* base internal command bits */
4300 mutex_init(&ioc->base_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004301 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4302 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
4303
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304304 /* port_enable command bits */
4305 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4306 ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
4307
Eric Moore635374e2009-03-09 01:21:12 -06004308 /* transport internal command bits */
4309 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4310 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
4311 mutex_init(&ioc->transport_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004312
Kashyap, Desaid685c262009-11-17 13:16:37 +05304313 /* scsih internal command bits */
4314 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4315 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
4316 mutex_init(&ioc->scsih_cmds.mutex);
4317
Eric Moore635374e2009-03-09 01:21:12 -06004318 /* task management internal command bits */
4319 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4320 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4321 mutex_init(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004322
4323 /* config page internal command bits */
4324 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4325 ioc->config_cmds.status = MPT2_CMD_NOT_USED;
4326 mutex_init(&ioc->config_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004327
4328 /* ctl module internal command bits */
4329 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
Kashyap, Desai769578f2010-06-17 13:49:28 +05304330 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
Eric Moore635374e2009-03-09 01:21:12 -06004331 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
4332 mutex_init(&ioc->ctl_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004333
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304334 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4335 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
Kashyap, Desai769578f2010-06-17 13:49:28 +05304336 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
4337 !ioc->ctl_cmds.sense) {
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304338 r = -ENOMEM;
4339 goto out_free_resources;
4340 }
4341
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05304342 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4343 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4344 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
4345 r = -ENOMEM;
4346 goto out_free_resources;
4347 }
4348
Eric Moore635374e2009-03-09 01:21:12 -06004349 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4350 ioc->event_masks[i] = -1;
4351
4352 /* here we enable the events we care about */
4353 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
4354 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
4355 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
4356 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4357 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
4358 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
4359 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
4360 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
4361 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
Eric Moore635374e2009-03-09 01:21:12 -06004362 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304363 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06004364 if (r)
4365 goto out_free_resources;
4366
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05304367 if (missing_delay[0] != -1 && missing_delay[1] != -1)
4368 _base_update_missing_delay(ioc, missing_delay[0],
4369 missing_delay[1]);
4370
Eric Moore635374e2009-03-09 01:21:12 -06004371 return 0;
4372
4373 out_free_resources:
4374
4375 ioc->remove_host = 1;
4376 mpt2sas_base_free_resources(ioc);
4377 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05304378 pci_set_drvdata(ioc->pdev, NULL);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304379 kfree(ioc->cpu_msix_table);
4380 if (ioc->is_warpdrive)
4381 kfree(ioc->reply_post_host_index);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304382 kfree(ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06004383 kfree(ioc->tm_cmds.reply);
4384 kfree(ioc->transport_cmds.reply);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304385 kfree(ioc->scsih_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06004386 kfree(ioc->config_cmds.reply);
4387 kfree(ioc->base_cmds.reply);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304388 kfree(ioc->port_enable_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06004389 kfree(ioc->ctl_cmds.reply);
Kashyap, Desai769578f2010-06-17 13:49:28 +05304390 kfree(ioc->ctl_cmds.sense);
Eric Moore635374e2009-03-09 01:21:12 -06004391 kfree(ioc->pfacts);
4392 ioc->ctl_cmds.reply = NULL;
4393 ioc->base_cmds.reply = NULL;
4394 ioc->tm_cmds.reply = NULL;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304395 ioc->scsih_cmds.reply = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004396 ioc->transport_cmds.reply = NULL;
4397 ioc->config_cmds.reply = NULL;
4398 ioc->pfacts = NULL;
4399 return r;
4400}
4401
4402
4403/**
4404 * mpt2sas_base_detach - remove controller instance
4405 * @ioc: per adapter object
4406 *
4407 * Return nothing.
4408 */
4409void
4410mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
4411{
Eric Moore635374e2009-03-09 01:21:12 -06004412
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304413 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004414 __func__));
4415
Kashyap, Desaie4750c92009-08-07 19:37:59 +05304416 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004417 mpt2sas_base_free_resources(ioc);
4418 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05304419 pci_set_drvdata(ioc->pdev, NULL);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304420 kfree(ioc->cpu_msix_table);
4421 if (ioc->is_warpdrive)
4422 kfree(ioc->reply_post_host_index);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304423 kfree(ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06004424 kfree(ioc->pfacts);
4425 kfree(ioc->ctl_cmds.reply);
Kashyap, Desai769578f2010-06-17 13:49:28 +05304426 kfree(ioc->ctl_cmds.sense);
Eric Moore635374e2009-03-09 01:21:12 -06004427 kfree(ioc->base_cmds.reply);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304428 kfree(ioc->port_enable_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06004429 kfree(ioc->tm_cmds.reply);
4430 kfree(ioc->transport_cmds.reply);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304431 kfree(ioc->scsih_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06004432 kfree(ioc->config_cmds.reply);
4433}
4434
4435/**
4436 * _base_reset_handler - reset callback handler (for base)
4437 * @ioc: per adapter object
4438 * @reset_phase: phase
4439 *
4440 * The handler for doing any required cleanup or initialization.
4441 *
4442 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
4443 * MPT2_IOC_DONE_RESET
4444 *
4445 * Return nothing.
4446 */
4447static void
4448_base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
4449{
Kashyap, Desai42244892011-01-04 11:38:39 +05304450 mpt2sas_scsih_reset_handler(ioc, reset_phase);
4451 mpt2sas_ctl_reset_handler(ioc, reset_phase);
Eric Moore635374e2009-03-09 01:21:12 -06004452 switch (reset_phase) {
4453 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304454 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06004455 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
4456 break;
4457 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304458 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06004459 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
4460 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
4461 ioc->transport_cmds.status |= MPT2_CMD_RESET;
4462 mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
4463 complete(&ioc->transport_cmds.done);
4464 }
4465 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
4466 ioc->base_cmds.status |= MPT2_CMD_RESET;
4467 mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
4468 complete(&ioc->base_cmds.done);
4469 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304470 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
4471 ioc->port_enable_failed = 1;
4472 ioc->port_enable_cmds.status |= MPT2_CMD_RESET;
4473 mpt2sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
4474 if (ioc->is_driver_loading) {
4475 ioc->start_scan_failed =
4476 MPI2_IOCSTATUS_INTERNAL_ERROR;
4477 ioc->start_scan = 0;
4478 ioc->port_enable_cmds.status =
4479 MPT2_CMD_NOT_USED;
4480 } else
4481 complete(&ioc->port_enable_cmds.done);
4482
4483 }
Eric Moore635374e2009-03-09 01:21:12 -06004484 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
4485 ioc->config_cmds.status |= MPT2_CMD_RESET;
4486 mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07004487 ioc->config_cmds.smid = USHRT_MAX;
Eric Moore635374e2009-03-09 01:21:12 -06004488 complete(&ioc->config_cmds.done);
4489 }
4490 break;
4491 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304492 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06004493 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
4494 break;
4495 }
Eric Moore635374e2009-03-09 01:21:12 -06004496}
4497
4498/**
4499 * _wait_for_commands_to_complete - reset controller
4500 * @ioc: Pointer to MPT_ADAPTER structure
4501 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4502 *
4503 * This function waiting(3s) for all pending commands to complete
4504 * prior to putting controller in reset.
4505 */
4506static void
4507_wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4508{
4509 u32 ioc_state;
4510 unsigned long flags;
4511 u16 i;
4512
4513 ioc->pending_io_count = 0;
4514 if (sleep_flag != CAN_SLEEP)
4515 return;
4516
4517 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4518 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
4519 return;
4520
4521 /* pending command count */
4522 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304523 for (i = 0; i < ioc->scsiio_depth; i++)
Eric Moore635374e2009-03-09 01:21:12 -06004524 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
4525 ioc->pending_io_count++;
4526 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4527
4528 if (!ioc->pending_io_count)
4529 return;
4530
4531 /* wait for pending commands to complete */
Kashyap, Desaid2742132010-06-17 13:28:55 +05304532 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
Eric Moore635374e2009-03-09 01:21:12 -06004533}
4534
4535/**
4536 * mpt2sas_base_hard_reset_handler - reset controller
4537 * @ioc: Pointer to MPT_ADAPTER structure
4538 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4539 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4540 *
4541 * Returns 0 for success, non-zero for failure.
4542 */
4543int
4544mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4545 enum reset_type type)
4546{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304547 int r;
Eric Moore635374e2009-03-09 01:21:12 -06004548 unsigned long flags;
4549
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304550 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004551 __func__));
4552
Eric Moore3cb54692010-07-08 14:44:34 -06004553 if (ioc->pci_error_recovery) {
4554 printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
4555 ioc->name, __func__);
4556 r = 0;
Alexey Khoroshilov7fbd7642011-08-26 00:36:23 +04004557 goto out_unlocked;
Eric Moore3cb54692010-07-08 14:44:34 -06004558 }
4559
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05304560 if (mpt2sas_fwfault_debug)
4561 mpt2sas_halt_firmware(ioc);
4562
Kashyap, Desaid2742132010-06-17 13:28:55 +05304563 /* TODO - What we really should be doing is pulling
4564 * out all the code associated with NO_SLEEP; its never used.
4565 * That is legacy code from mpt fusion driver, ported over.
4566 * I will leave this BUG_ON here for now till its been resolved.
4567 */
4568 BUG_ON(sleep_flag == NO_SLEEP);
4569
4570 /* wait for an active reset in progress to complete */
4571 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4572 do {
4573 ssleep(1);
4574 } while (ioc->shost_recovery == 1);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304575 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
Kashyap, Desaid2742132010-06-17 13:28:55 +05304576 __func__));
4577 return ioc->ioc_reset_in_progress_status;
Eric Moore635374e2009-03-09 01:21:12 -06004578 }
Kashyap, Desaid2742132010-06-17 13:28:55 +05304579
4580 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06004581 ioc->shost_recovery = 1;
Eric Moore635374e2009-03-09 01:21:12 -06004582 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4583
4584 _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
4585 _wait_for_commands_to_complete(ioc, sleep_flag);
4586 _base_mask_interrupts(ioc);
4587 r = _base_make_ioc_ready(ioc, sleep_flag, type);
4588 if (r)
4589 goto out;
4590 _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
Kashyap, Desai42244892011-01-04 11:38:39 +05304591
4592 /* If this hard reset is called while port enable is active, then
4593 * there is no reason to call make_ioc_operational
4594 */
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304595 if (ioc->is_driver_loading && ioc->port_enable_failed) {
4596 ioc->remove_host = 1;
Kashyap, Desai42244892011-01-04 11:38:39 +05304597 r = -EFAULT;
4598 goto out;
4599 }
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304600 r = _base_make_ioc_operational(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06004601 if (!r)
4602 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
4603 out:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304604 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
Eric Moore635374e2009-03-09 01:21:12 -06004605 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4606
4607 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desaid2742132010-06-17 13:28:55 +05304608 ioc->ioc_reset_in_progress_status = r;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304609 ioc->shost_recovery = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004610 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desaid2742132010-06-17 13:28:55 +05304611 mutex_unlock(&ioc->reset_in_progress_mutex);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05304612
Alexey Khoroshilov7fbd7642011-08-26 00:36:23 +04004613 out_unlocked:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304614 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
Kashyap, Desaid2742132010-06-17 13:28:55 +05304615 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06004616 return r;
4617}