2 * This is the Fusion MPT base driver providing common API layer interface
3 * for access to MPT (Message Passing Technology) firmware.
5 * This code is based on drivers/scsi/mpt2sas/mpt2_base.c
6 * Copyright (C) 2007-2010 LSI Corporation
7 * (mailto:DL-MPTFusionLinux@lsi.com)
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.
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.
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.
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
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,
45 #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>
59 #include <linux/time.h>
60 #include <linux/aer.h>
62 #include "mpt2sas_base.h"
64 static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
66 #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
68 #define MAX_HBA_QUEUE_DEPTH 30000
69 #define MAX_CHAIN_DEPTH 100000
70 static int max_queue_depth = -1;
71 module_param(max_queue_depth, int, 0);
72 MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
74 static int max_sgl_entries = -1;
75 module_param(max_sgl_entries, int, 0);
76 MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
78 static int msix_disable = -1;
79 module_param(msix_disable, int, 0);
80 MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
82 static int missing_delay[2] = {-1, -1};
83 module_param_array(missing_delay, int, NULL, 0);
84 MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
86 static int mpt2sas_fwfault_debug;
87 MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
88 "and halt firmware - (default=0)");
90 static int disable_discovery = -1;
91 module_param(disable_discovery, int, 0);
92 MODULE_PARM_DESC(disable_discovery, " disable discovery ");
95 /* diag_buffer_enable is bitwise
97 * bit 1 set = SNAPSHOT
98 * bit 2 set = EXTENDED
100 * Either bit can be set, or both
102 static int diag_buffer_enable;
103 module_param(diag_buffer_enable, int, 0);
104 MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
105 "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
108 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
112 _scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
114 int ret = param_set_int(val, kp);
115 struct MPT2SAS_ADAPTER *ioc;
120 printk(KERN_INFO "setting fwfault_debug(%d)\n", mpt2sas_fwfault_debug);
121 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
122 ioc->fwfault_debug = mpt2sas_fwfault_debug;
125 module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
126 param_get_int, &mpt2sas_fwfault_debug, 0644);
129 * _base_fault_reset_work - workq handling ioc fault conditions
130 * @work: input argument, used to derive ioc
136 _base_fault_reset_work(struct work_struct *work)
138 struct MPT2SAS_ADAPTER *ioc =
139 container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
144 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
145 if (ioc->shost_recovery)
147 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
149 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
150 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
151 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
153 printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
154 __func__, (rc == 0) ? "success" : "failed");
155 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
156 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
157 mpt2sas_base_fault_info(ioc, doorbell &
158 MPI2_DOORBELL_DATA_MASK);
161 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
163 if (ioc->fault_reset_work_q)
164 queue_delayed_work(ioc->fault_reset_work_q,
165 &ioc->fault_reset_work,
166 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
167 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
171 * mpt2sas_base_start_watchdog - start the fault_reset_work_q
172 * @ioc: per adapter object
178 mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
182 if (ioc->fault_reset_work_q)
185 /* initialize fault polling */
186 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
187 snprintf(ioc->fault_reset_work_q_name,
188 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
189 ioc->fault_reset_work_q =
190 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
191 if (!ioc->fault_reset_work_q) {
192 printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
193 ioc->name, __func__, __LINE__);
196 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
197 if (ioc->fault_reset_work_q)
198 queue_delayed_work(ioc->fault_reset_work_q,
199 &ioc->fault_reset_work,
200 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
201 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
205 * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
206 * @ioc: per adapter object
212 mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
215 struct workqueue_struct *wq;
217 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
218 wq = ioc->fault_reset_work_q;
219 ioc->fault_reset_work_q = NULL;
220 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
222 if (!cancel_delayed_work(&ioc->fault_reset_work))
224 destroy_workqueue(wq);
229 * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
230 * @ioc: per adapter object
231 * @fault_code: fault code
236 mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
238 printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
239 ioc->name, fault_code);
243 * mpt2sas_halt_firmware - halt's mpt controller firmware
244 * @ioc: per adapter object
246 * For debugging timeout related issues. Writing 0xCOFFEE00
247 * to the doorbell register will halt controller firmware. With
248 * the purpose to stop both driver and firmware, the enduser can
249 * obtain a ring buffer from controller UART.
252 mpt2sas_halt_firmware(struct MPT2SAS_ADAPTER *ioc)
256 if (!ioc->fwfault_debug)
261 doorbell = readl(&ioc->chip->Doorbell);
262 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
263 mpt2sas_base_fault_info(ioc , doorbell);
265 writel(0xC0FFEE00, &ioc->chip->Doorbell);
266 printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
267 "timeout\n", ioc->name);
270 panic("panic in %s\n", __func__);
273 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
275 * _base_sas_ioc_info - verbose translation of the ioc status
276 * @ioc: per adapter object
277 * @mpi_reply: reply mf payload returned from firmware
278 * @request_hdr: request mf
283 _base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
284 MPI2RequestHeader_t *request_hdr)
286 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
290 char *func_str = NULL;
292 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
293 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
294 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
295 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
298 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
301 switch (ioc_status) {
303 /****************************************************************************
304 * Common IOCStatus values for all replies
305 ****************************************************************************/
307 case MPI2_IOCSTATUS_INVALID_FUNCTION:
308 desc = "invalid function";
310 case MPI2_IOCSTATUS_BUSY:
313 case MPI2_IOCSTATUS_INVALID_SGL:
314 desc = "invalid sgl";
316 case MPI2_IOCSTATUS_INTERNAL_ERROR:
317 desc = "internal error";
319 case MPI2_IOCSTATUS_INVALID_VPID:
320 desc = "invalid vpid";
322 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
323 desc = "insufficient resources";
325 case MPI2_IOCSTATUS_INVALID_FIELD:
326 desc = "invalid field";
328 case MPI2_IOCSTATUS_INVALID_STATE:
329 desc = "invalid state";
331 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
332 desc = "op state not supported";
335 /****************************************************************************
336 * Config IOCStatus values
337 ****************************************************************************/
339 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
340 desc = "config invalid action";
342 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
343 desc = "config invalid type";
345 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
346 desc = "config invalid page";
348 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
349 desc = "config invalid data";
351 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
352 desc = "config no defaults";
354 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
355 desc = "config cant commit";
358 /****************************************************************************
360 ****************************************************************************/
362 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
363 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
364 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
365 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
366 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
367 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
368 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
369 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
370 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
371 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
372 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
373 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
376 /****************************************************************************
377 * For use by SCSI Initiator and SCSI Target end-to-end data protection
378 ****************************************************************************/
380 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
381 desc = "eedp guard error";
383 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
384 desc = "eedp ref tag error";
386 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
387 desc = "eedp app tag error";
390 /****************************************************************************
392 ****************************************************************************/
394 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
395 desc = "target invalid io index";
397 case MPI2_IOCSTATUS_TARGET_ABORTED:
398 desc = "target aborted";
400 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
401 desc = "target no conn retryable";
403 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
404 desc = "target no connection";
406 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
407 desc = "target xfer count mismatch";
409 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
410 desc = "target data offset error";
412 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
413 desc = "target too much write data";
415 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
416 desc = "target iu too short";
418 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
419 desc = "target ack nak timeout";
421 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
422 desc = "target nak received";
425 /****************************************************************************
426 * Serial Attached SCSI values
427 ****************************************************************************/
429 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
430 desc = "smp request failed";
432 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
433 desc = "smp data overrun";
436 /****************************************************************************
437 * Diagnostic Buffer Post / Diagnostic Release values
438 ****************************************************************************/
440 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
441 desc = "diagnostic released";
450 switch (request_hdr->Function) {
451 case MPI2_FUNCTION_CONFIG:
452 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
453 func_str = "config_page";
455 case MPI2_FUNCTION_SCSI_TASK_MGMT:
456 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
457 func_str = "task_mgmt";
459 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
460 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
461 func_str = "sas_iounit_ctl";
463 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
464 frame_sz = sizeof(Mpi2SepRequest_t);
465 func_str = "enclosure";
467 case MPI2_FUNCTION_IOC_INIT:
468 frame_sz = sizeof(Mpi2IOCInitRequest_t);
469 func_str = "ioc_init";
471 case MPI2_FUNCTION_PORT_ENABLE:
472 frame_sz = sizeof(Mpi2PortEnableRequest_t);
473 func_str = "port_enable";
475 case MPI2_FUNCTION_SMP_PASSTHROUGH:
476 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
477 func_str = "smp_passthru";
481 func_str = "unknown";
485 printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
486 " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
488 _debug_dump_mf(request_hdr, frame_sz/4);
492 * _base_display_event_data - verbose translation of firmware asyn events
493 * @ioc: per adapter object
494 * @mpi_reply: reply mf payload returned from firmware
499 _base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
500 Mpi2EventNotificationReply_t *mpi_reply)
505 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
508 event = le16_to_cpu(mpi_reply->Event);
511 case MPI2_EVENT_LOG_DATA:
514 case MPI2_EVENT_STATE_CHANGE:
515 desc = "Status Change";
517 case MPI2_EVENT_HARD_RESET_RECEIVED:
518 desc = "Hard Reset Received";
520 case MPI2_EVENT_EVENT_CHANGE:
521 desc = "Event Change";
523 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
524 desc = "Device Status Change";
526 case MPI2_EVENT_IR_OPERATION_STATUS:
527 if (!ioc->hide_ir_msg)
528 desc = "IR Operation Status";
530 case MPI2_EVENT_SAS_DISCOVERY:
532 Mpi2EventDataSasDiscovery_t *event_data =
533 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
534 printk(MPT2SAS_INFO_FMT "Discovery: (%s)", ioc->name,
535 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
537 if (event_data->DiscoveryStatus)
538 printk("discovery_status(0x%08x)",
539 le32_to_cpu(event_data->DiscoveryStatus));
543 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
544 desc = "SAS Broadcast Primitive";
546 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
547 desc = "SAS Init Device Status Change";
549 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
550 desc = "SAS Init Table Overflow";
552 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
553 desc = "SAS Topology Change List";
555 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
556 desc = "SAS Enclosure Device Status Change";
558 case MPI2_EVENT_IR_VOLUME:
559 if (!ioc->hide_ir_msg)
562 case MPI2_EVENT_IR_PHYSICAL_DISK:
563 if (!ioc->hide_ir_msg)
564 desc = "IR Physical Disk";
566 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
567 if (!ioc->hide_ir_msg)
568 desc = "IR Configuration Change List";
570 case MPI2_EVENT_LOG_ENTRY_ADDED:
571 if (!ioc->hide_ir_msg)
572 desc = "Log Entry Added";
579 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
584 * _base_sas_log_info - verbose translation of firmware log info
585 * @ioc: per adapter object
586 * @log_info: log info
591 _base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
602 union loginfo_type sas_loginfo;
603 char *originator_str = NULL;
605 sas_loginfo.loginfo = log_info;
606 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
609 /* each nexus loss loginfo */
610 if (log_info == 0x31170000)
613 /* eat the loginfos associated with task aborts */
614 if (ioc->ignore_loginfos && (log_info == 30050000 || log_info ==
615 0x31140000 || log_info == 0x31130000))
618 switch (sas_loginfo.dw.originator) {
620 originator_str = "IOP";
623 originator_str = "PL";
626 if (!ioc->hide_ir_msg)
627 originator_str = "IR";
629 originator_str = "WarpDrive";
633 printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
634 "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
635 originator_str, sas_loginfo.dw.code,
636 sas_loginfo.dw.subcode);
640 * _base_display_reply_info -
641 * @ioc: per adapter object
642 * @smid: system request message index
643 * @msix_index: MSIX table index supplied by the OS
644 * @reply: reply message frame(lower 32bit addr)
649 _base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
652 MPI2DefaultReply_t *mpi_reply;
655 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
656 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
657 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
658 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
659 (ioc->logging_level & MPT_DEBUG_REPLY)) {
660 _base_sas_ioc_info(ioc , mpi_reply,
661 mpt2sas_base_get_msg_frame(ioc, smid));
664 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
665 _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
669 * mpt2sas_base_done - base internal command completion routine
670 * @ioc: per adapter object
671 * @smid: system request message index
672 * @msix_index: MSIX table index supplied by the OS
673 * @reply: reply message frame(lower 32bit addr)
675 * Return 1 meaning mf should be freed from _base_interrupt
676 * 0 means the mf is freed from this function.
679 mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
682 MPI2DefaultReply_t *mpi_reply;
684 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
685 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
688 if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
691 ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
693 ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
694 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
696 ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
698 complete(&ioc->base_cmds.done);
703 * _base_async_event - main callback handler for firmware asyn events
704 * @ioc: per adapter object
705 * @msix_index: MSIX table index supplied by the OS
706 * @reply: reply message frame(lower 32bit addr)
708 * Return 1 meaning mf should be freed from _base_interrupt
709 * 0 means the mf is freed from this function.
712 _base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
714 Mpi2EventNotificationReply_t *mpi_reply;
715 Mpi2EventAckRequest_t *ack_request;
718 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
721 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
723 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
724 _base_display_event_data(ioc, mpi_reply);
726 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
728 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
730 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
731 ioc->name, __func__);
735 ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
736 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
737 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
738 ack_request->Event = mpi_reply->Event;
739 ack_request->EventContext = mpi_reply->EventContext;
740 ack_request->VF_ID = 0; /* TODO */
741 ack_request->VP_ID = 0;
742 mpt2sas_base_put_smid_default(ioc, smid);
746 /* scsih callback handler */
747 mpt2sas_scsih_event_callback(ioc, msix_index, reply);
749 /* ctl callback handler */
750 mpt2sas_ctl_event_callback(ioc, msix_index, reply);
756 * _base_get_cb_idx - obtain the callback index
757 * @ioc: per adapter object
758 * @smid: system request message index
760 * Return callback index.
763 _base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
768 if (smid < ioc->hi_priority_smid) {
770 cb_idx = ioc->scsi_lookup[i].cb_idx;
771 } else if (smid < ioc->internal_smid) {
772 i = smid - ioc->hi_priority_smid;
773 cb_idx = ioc->hpr_lookup[i].cb_idx;
774 } else if (smid <= ioc->hba_queue_depth) {
775 i = smid - ioc->internal_smid;
776 cb_idx = ioc->internal_lookup[i].cb_idx;
783 * _base_mask_interrupts - disable interrupts
784 * @ioc: per adapter object
786 * Disabling ResetIRQ, Reply and Doorbell Interrupts
791 _base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
795 ioc->mask_interrupts = 1;
796 him_register = readl(&ioc->chip->HostInterruptMask);
797 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
798 writel(him_register, &ioc->chip->HostInterruptMask);
799 readl(&ioc->chip->HostInterruptMask);
803 * _base_unmask_interrupts - enable interrupts
804 * @ioc: per adapter object
806 * Enabling only Reply Interrupts
811 _base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
815 him_register = readl(&ioc->chip->HostInterruptMask);
816 him_register &= ~MPI2_HIM_RIM;
817 writel(him_register, &ioc->chip->HostInterruptMask);
818 ioc->mask_interrupts = 0;
821 union reply_descriptor {
830 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
831 * @irq: irq number (not used)
832 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
833 * @r: pt_regs pointer (not used)
835 * Return IRQ_HANDLE if processed, else IRQ_NONE.
838 _base_interrupt(int irq, void *bus_id)
840 struct adapter_reply_queue *reply_q = bus_id;
841 union reply_descriptor rd;
843 u8 request_desript_type;
847 u8 msix_index = reply_q->msix_index;
848 struct MPT2SAS_ADAPTER *ioc = reply_q->ioc;
849 Mpi2ReplyDescriptorsUnion_t *rpf;
852 if (ioc->mask_interrupts)
855 if (!atomic_add_unless(&reply_q->busy, 1, 1))
858 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
859 request_desript_type = rpf->Default.ReplyFlags
860 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
861 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
862 atomic_dec(&reply_q->busy);
869 rd.word = le64_to_cpu(rpf->Words);
870 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
873 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
874 if (request_desript_type ==
875 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
877 (rpf->AddressReply.ReplyFrameAddress);
878 if (reply > ioc->reply_dma_max_address ||
879 reply < ioc->reply_dma_min_address)
881 } else if (request_desript_type ==
882 MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
884 else if (request_desript_type ==
885 MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
888 cb_idx = _base_get_cb_idx(ioc, smid);
889 if (smid && cb_idx != 0xFF) {
890 rc = mpt_callbacks[cb_idx](ioc, smid, msix_index,
893 _base_display_reply_info(ioc, smid, msix_index,
896 mpt2sas_base_free_smid(ioc, smid);
899 _base_async_event(ioc, msix_index, reply);
901 /* reply free queue handling */
903 ioc->reply_free_host_index =
904 (ioc->reply_free_host_index ==
905 (ioc->reply_free_queue_depth - 1)) ?
906 0 : ioc->reply_free_host_index + 1;
907 ioc->reply_free[ioc->reply_free_host_index] =
910 writel(ioc->reply_free_host_index,
911 &ioc->chip->ReplyFreeHostIndex);
916 rpf->Words = cpu_to_le64(ULLONG_MAX);
917 reply_q->reply_post_host_index =
918 (reply_q->reply_post_host_index ==
919 (ioc->reply_post_queue_depth - 1)) ? 0 :
920 reply_q->reply_post_host_index + 1;
921 request_desript_type =
922 reply_q->reply_post_free[reply_q->reply_post_host_index].
923 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
925 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
927 if (!reply_q->reply_post_host_index)
928 rpf = reply_q->reply_post_free;
935 if (!completed_cmds) {
936 atomic_dec(&reply_q->busy);
940 if (ioc->is_warpdrive) {
941 writel(reply_q->reply_post_host_index,
942 ioc->reply_post_host_index[msix_index]);
943 atomic_dec(&reply_q->busy);
946 writel(reply_q->reply_post_host_index | (msix_index <<
947 MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
948 atomic_dec(&reply_q->busy);
953 * _base_is_controller_msix_enabled - is controller support muli-reply queues
954 * @ioc: per adapter object
958 _base_is_controller_msix_enabled(struct MPT2SAS_ADAPTER *ioc)
960 return (ioc->facts.IOCCapabilities &
961 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
965 * mpt2sas_base_flush_reply_queues - flushing the MSIX reply queues
966 * @ioc: per adapter object
967 * Context: ISR conext
969 * Called when a Task Management request has completed. We want
970 * to flush the other reply queues so all the outstanding IO has been
971 * completed back to OS before we process the TM completetion.
976 mpt2sas_base_flush_reply_queues(struct MPT2SAS_ADAPTER *ioc)
978 struct adapter_reply_queue *reply_q;
980 /* If MSIX capability is turned off
981 * then multi-queues are not enabled
983 if (!_base_is_controller_msix_enabled(ioc))
986 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
987 if (ioc->shost_recovery)
989 /* TMs are on msix_index == 0 */
990 if (reply_q->msix_index == 0)
992 _base_interrupt(reply_q->vector, (void *)reply_q);
997 * mpt2sas_base_release_callback_handler - clear interrupt callback handler
998 * @cb_idx: callback index
1003 mpt2sas_base_release_callback_handler(u8 cb_idx)
1005 mpt_callbacks[cb_idx] = NULL;
1009 * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
1010 * @cb_func: callback function
1015 mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1019 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1020 if (mpt_callbacks[cb_idx] == NULL)
1023 mpt_callbacks[cb_idx] = cb_func;
1028 * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
1033 mpt2sas_base_initialize_callback_handler(void)
1037 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1038 mpt2sas_base_release_callback_handler(cb_idx);
1042 * mpt2sas_base_build_zero_len_sge - build zero length sg entry
1043 * @ioc: per adapter object
1044 * @paddr: virtual address for SGE
1046 * Create a zero length scatter gather entry to insure the IOCs hardware has
1047 * something to use if the target device goes brain dead and tries
1048 * to send data even when none is asked for.
1053 mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
1055 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1056 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1057 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1058 MPI2_SGE_FLAGS_SHIFT);
1059 ioc->base_add_sg_single(paddr, flags_length, -1);
1063 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1064 * @paddr: virtual address for SGE
1065 * @flags_length: SGE flags and data transfer length
1066 * @dma_addr: Physical address
1071 _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1073 Mpi2SGESimple32_t *sgel = paddr;
1075 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1076 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1077 sgel->FlagsLength = cpu_to_le32(flags_length);
1078 sgel->Address = cpu_to_le32(dma_addr);
1083 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1084 * @paddr: virtual address for SGE
1085 * @flags_length: SGE flags and data transfer length
1086 * @dma_addr: Physical address
1091 _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1093 Mpi2SGESimple64_t *sgel = paddr;
1095 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1096 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1097 sgel->FlagsLength = cpu_to_le32(flags_length);
1098 sgel->Address = cpu_to_le64(dma_addr);
1101 #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1104 * _base_config_dma_addressing - set dma addressing
1105 * @ioc: per adapter object
1106 * @pdev: PCI device struct
1108 * Returns 0 for success, non-zero for failure.
1111 _base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
1116 if (sizeof(dma_addr_t) > 4) {
1117 const uint64_t required_mask =
1118 dma_get_required_mask(&pdev->dev);
1119 if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
1120 DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
1121 DMA_BIT_MASK(64))) {
1122 ioc->base_add_sg_single = &_base_add_sg_single_64;
1123 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
1129 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1130 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1131 ioc->base_add_sg_single = &_base_add_sg_single_32;
1132 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
1139 printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
1140 "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
1146 * _base_check_enable_msix - checks MSIX capabable.
1147 * @ioc: per adapter object
1149 * Check to see if card is capable of MSIX, and set number
1150 * of available msix vectors
1153 _base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1156 u16 message_control;
1159 /* Check whether controller SAS2008 B0 controller,
1160 if it is SAS2008 B0 controller use IO-APIC instead of MSIX */
1161 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
1162 ioc->pdev->revision == 0x01) {
1166 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1168 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
1169 "supported\n", ioc->name));
1173 /* get msix vector count */
1174 /* NUMA_IO not supported for older controllers */
1175 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
1176 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
1177 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
1178 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
1179 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
1180 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
1181 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
1182 ioc->msix_vector_count = 1;
1184 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1185 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1187 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
1188 "vector_count(%d)\n", ioc->name, ioc->msix_vector_count));
1194 * _base_free_irq - free irq
1195 * @ioc: per adapter object
1197 * Freeing respective reply_queue from the list.
1200 _base_free_irq(struct MPT2SAS_ADAPTER *ioc)
1202 struct adapter_reply_queue *reply_q, *next;
1204 if (list_empty(&ioc->reply_queue_list))
1207 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1208 list_del(&reply_q->list);
1209 synchronize_irq(reply_q->vector);
1210 free_irq(reply_q->vector, reply_q);
1216 * _base_request_irq - request irq
1217 * @ioc: per adapter object
1218 * @index: msix index into vector table
1219 * @vector: irq vector
1221 * Inserting respective reply_queue into the list.
1224 _base_request_irq(struct MPT2SAS_ADAPTER *ioc, u8 index, u32 vector)
1226 struct adapter_reply_queue *reply_q;
1229 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1231 printk(MPT2SAS_ERR_FMT "unable to allocate memory %d!\n",
1232 ioc->name, (int)sizeof(struct adapter_reply_queue));
1236 reply_q->msix_index = index;
1237 reply_q->vector = vector;
1238 atomic_set(&reply_q->busy, 0);
1239 if (ioc->msix_enable)
1240 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
1241 MPT2SAS_DRIVER_NAME, ioc->id, index);
1243 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
1244 MPT2SAS_DRIVER_NAME, ioc->id);
1245 r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1248 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1249 reply_q->name, vector);
1254 INIT_LIST_HEAD(&reply_q->list);
1255 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1260 * _base_assign_reply_queues - assigning msix index for each cpu
1261 * @ioc: per adapter object
1263 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1265 * It would nice if we could call irq_set_affinity, however it is not
1266 * an exported symbol
1269 _base_assign_reply_queues(struct MPT2SAS_ADAPTER *ioc)
1271 struct adapter_reply_queue *reply_q;
1273 int cpu_grouping, loop, grouping, grouping_mod;
1275 if (!_base_is_controller_msix_enabled(ioc))
1278 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1279 /* when there are more cpus than available msix vectors,
1280 * then group cpus togeather on same irq
1282 if (ioc->cpu_count > ioc->msix_vector_count) {
1283 grouping = ioc->cpu_count / ioc->msix_vector_count;
1284 grouping_mod = ioc->cpu_count % ioc->msix_vector_count;
1285 if (grouping < 2 || (grouping == 2 && !grouping_mod))
1287 else if (grouping < 4 || (grouping == 4 && !grouping_mod))
1289 else if (grouping < 8 || (grouping == 8 && !grouping_mod))
1297 reply_q = list_entry(ioc->reply_queue_list.next,
1298 struct adapter_reply_queue, list);
1299 for_each_online_cpu(cpu_id) {
1300 if (!cpu_grouping) {
1301 ioc->cpu_msix_table[cpu_id] = reply_q->msix_index;
1302 reply_q = list_entry(reply_q->list.next,
1303 struct adapter_reply_queue, list);
1305 if (loop < cpu_grouping) {
1306 ioc->cpu_msix_table[cpu_id] =
1307 reply_q->msix_index;
1310 reply_q = list_entry(reply_q->list.next,
1311 struct adapter_reply_queue, list);
1312 ioc->cpu_msix_table[cpu_id] =
1313 reply_q->msix_index;
1321 * _base_disable_msix - disables msix
1322 * @ioc: per adapter object
1326 _base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
1328 if (ioc->msix_enable) {
1329 pci_disable_msix(ioc->pdev);
1330 ioc->msix_enable = 0;
1335 * _base_enable_msix - enables msix, failback to io_apic
1336 * @ioc: per adapter object
1340 _base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1342 struct msix_entry *entries, *a;
1347 INIT_LIST_HEAD(&ioc->reply_queue_list);
1349 if (msix_disable == -1 || msix_disable == 0)
1355 if (_base_check_enable_msix(ioc) != 0)
1358 ioc->reply_queue_count = min_t(u8, ioc->cpu_count,
1359 ioc->msix_vector_count);
1361 entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1364 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "kcalloc "
1365 "failed @ at %s:%d/%s() !!!\n", ioc->name, __FILE__,
1366 __LINE__, __func__));
1370 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
1373 r = pci_enable_msix(ioc->pdev, entries, ioc->reply_queue_count);
1375 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
1376 "failed (r=%d) !!!\n", ioc->name, r));
1381 ioc->msix_enable = 1;
1382 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
1383 r = _base_request_irq(ioc, i, a->vector);
1385 _base_free_irq(ioc);
1386 _base_disable_msix(ioc);
1395 /* failback to io_apic interrupt routing */
1398 r = _base_request_irq(ioc, 0, ioc->pdev->irq);
1404 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1405 * @ioc: per adapter object
1407 * Returns 0 for success, non-zero for failure.
1410 mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1412 struct pci_dev *pdev = ioc->pdev;
1418 struct adapter_reply_queue *reply_q;
1420 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
1421 ioc->name, __func__));
1423 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1424 if (pci_enable_device_mem(pdev)) {
1425 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1426 "failed\n", ioc->name);
1431 if (pci_request_selected_regions(pdev, ioc->bars,
1432 MPT2SAS_DRIVER_NAME)) {
1433 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1434 "failed\n", ioc->name);
1439 /* AER (Advanced Error Reporting) hooks */
1440 pci_enable_pcie_error_reporting(pdev);
1442 pci_set_master(pdev);
1444 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1445 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1446 ioc->name, pci_name(pdev));
1451 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
1452 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
1455 pio_chip = (u64)pci_resource_start(pdev, i);
1456 pio_sz = pci_resource_len(pdev, i);
1460 /* verify memory resource is valid before using */
1461 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1462 ioc->chip_phys = pci_resource_start(pdev, i);
1463 chip_phys = (u64)ioc->chip_phys;
1464 memap_sz = pci_resource_len(pdev, i);
1465 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1466 if (ioc->chip == NULL) {
1467 printk(MPT2SAS_ERR_FMT "unable to map "
1468 "adapter memory!\n", ioc->name);
1476 _base_mask_interrupts(ioc);
1477 r = _base_enable_msix(ioc);
1481 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
1482 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1483 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1484 "IO-APIC enabled"), reply_q->vector);
1486 printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1487 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1488 printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
1489 ioc->name, (unsigned long long)pio_chip, pio_sz);
1491 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1492 pci_save_state(pdev);
1500 pci_release_selected_regions(ioc->pdev, ioc->bars);
1501 pci_disable_pcie_error_reporting(pdev);
1502 pci_disable_device(pdev);
1507 * mpt2sas_base_get_msg_frame - obtain request mf pointer
1508 * @ioc: per adapter object
1509 * @smid: system request message index(smid zero is invalid)
1511 * Returns virt pointer to message frame.
1514 mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1516 return (void *)(ioc->request + (smid * ioc->request_sz));
1520 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1521 * @ioc: per adapter object
1522 * @smid: system request message index
1524 * Returns virt pointer to sense buffer.
1527 mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1529 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1533 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1534 * @ioc: per adapter object
1535 * @smid: system request message index
1537 * Returns phys pointer to the low 32bit address of the sense buffer.
1540 mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1542 return cpu_to_le32(ioc->sense_dma +
1543 ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1547 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1548 * @ioc: per adapter object
1549 * @phys_addr: lower 32 physical addr of the reply
1551 * Converts 32bit lower physical addr into a virt address.
1554 mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1558 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1562 * mpt2sas_base_get_smid - obtain a free smid from internal queue
1563 * @ioc: per adapter object
1564 * @cb_idx: callback index
1566 * Returns smid (zero is invalid)
1569 mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1571 unsigned long flags;
1572 struct request_tracker *request;
1575 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1576 if (list_empty(&ioc->internal_free_list)) {
1577 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1578 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1579 ioc->name, __func__);
1583 request = list_entry(ioc->internal_free_list.next,
1584 struct request_tracker, tracker_list);
1585 request->cb_idx = cb_idx;
1586 smid = request->smid;
1587 list_del(&request->tracker_list);
1588 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1593 * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1594 * @ioc: per adapter object
1595 * @cb_idx: callback index
1596 * @scmd: pointer to scsi command object
1598 * Returns smid (zero is invalid)
1601 mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
1602 struct scsi_cmnd *scmd)
1604 unsigned long flags;
1605 struct scsiio_tracker *request;
1608 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1609 if (list_empty(&ioc->free_list)) {
1610 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1611 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1612 ioc->name, __func__);
1616 request = list_entry(ioc->free_list.next,
1617 struct scsiio_tracker, tracker_list);
1618 request->scmd = scmd;
1619 request->cb_idx = cb_idx;
1620 smid = request->smid;
1621 list_del(&request->tracker_list);
1622 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1627 * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1628 * @ioc: per adapter object
1629 * @cb_idx: callback index
1631 * Returns smid (zero is invalid)
1634 mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1636 unsigned long flags;
1637 struct request_tracker *request;
1640 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1641 if (list_empty(&ioc->hpr_free_list)) {
1642 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1646 request = list_entry(ioc->hpr_free_list.next,
1647 struct request_tracker, tracker_list);
1648 request->cb_idx = cb_idx;
1649 smid = request->smid;
1650 list_del(&request->tracker_list);
1651 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1657 * mpt2sas_base_free_smid - put smid back on free_list
1658 * @ioc: per adapter object
1659 * @smid: system request message index
1664 mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1666 unsigned long flags;
1668 struct chain_tracker *chain_req, *next;
1670 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1671 if (smid < ioc->hi_priority_smid) {
1674 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
1675 list_for_each_entry_safe(chain_req, next,
1676 &ioc->scsi_lookup[i].chain_list, tracker_list) {
1677 list_del_init(&chain_req->tracker_list);
1678 list_add_tail(&chain_req->tracker_list,
1679 &ioc->free_chain_list);
1682 ioc->scsi_lookup[i].cb_idx = 0xFF;
1683 ioc->scsi_lookup[i].scmd = NULL;
1684 ioc->scsi_lookup[i].direct_io = 0;
1685 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
1687 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1690 * See _wait_for_commands_to_complete() call with regards
1693 if (ioc->shost_recovery && ioc->pending_io_count) {
1694 if (ioc->pending_io_count == 1)
1695 wake_up(&ioc->reset_wq);
1696 ioc->pending_io_count--;
1699 } else if (smid < ioc->internal_smid) {
1701 i = smid - ioc->hi_priority_smid;
1702 ioc->hpr_lookup[i].cb_idx = 0xFF;
1703 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
1704 &ioc->hpr_free_list);
1705 } else if (smid <= ioc->hba_queue_depth) {
1706 /* internal queue */
1707 i = smid - ioc->internal_smid;
1708 ioc->internal_lookup[i].cb_idx = 0xFF;
1709 list_add_tail(&ioc->internal_lookup[i].tracker_list,
1710 &ioc->internal_free_list);
1712 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1716 * _base_writeq - 64 bit write to MMIO
1717 * @ioc: per adapter object
1719 * @addr: address in MMIO space
1720 * @writeq_lock: spin lock
1722 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1723 * care of 32 bit environment where its not quarenteed to send the entire word
1727 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1728 spinlock_t *writeq_lock)
1730 unsigned long flags;
1731 __u64 data_out = cpu_to_le64(b);
1733 spin_lock_irqsave(writeq_lock, flags);
1734 writel((u32)(data_out), addr);
1735 writel((u32)(data_out >> 32), (addr + 4));
1736 spin_unlock_irqrestore(writeq_lock, flags);
1739 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1740 spinlock_t *writeq_lock)
1742 writeq(cpu_to_le64(b), addr);
1747 _base_get_msix_index(struct MPT2SAS_ADAPTER *ioc)
1749 return ioc->cpu_msix_table[raw_smp_processor_id()];
1753 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1754 * @ioc: per adapter object
1755 * @smid: system request message index
1756 * @handle: device handle
1761 mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
1763 Mpi2RequestDescriptorUnion_t descriptor;
1764 u64 *request = (u64 *)&descriptor;
1767 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1768 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
1769 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1770 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1771 descriptor.SCSIIO.LMID = 0;
1772 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1773 &ioc->scsi_lookup_lock);
1778 * mpt2sas_base_put_smid_hi_priority - send Task Management request to firmware
1779 * @ioc: per adapter object
1780 * @smid: system request message index
1785 mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1787 Mpi2RequestDescriptorUnion_t descriptor;
1788 u64 *request = (u64 *)&descriptor;
1790 descriptor.HighPriority.RequestFlags =
1791 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1792 descriptor.HighPriority.MSIxIndex = 0;
1793 descriptor.HighPriority.SMID = cpu_to_le16(smid);
1794 descriptor.HighPriority.LMID = 0;
1795 descriptor.HighPriority.Reserved1 = 0;
1796 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1797 &ioc->scsi_lookup_lock);
1801 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1802 * @ioc: per adapter object
1803 * @smid: system request message index
1808 mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1810 Mpi2RequestDescriptorUnion_t descriptor;
1811 u64 *request = (u64 *)&descriptor;
1813 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1814 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
1815 descriptor.Default.SMID = cpu_to_le16(smid);
1816 descriptor.Default.LMID = 0;
1817 descriptor.Default.DescriptorTypeDependent = 0;
1818 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1819 &ioc->scsi_lookup_lock);
1823 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1824 * @ioc: per adapter object
1825 * @smid: system request message index
1826 * @io_index: value used to track the IO
1831 mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
1834 Mpi2RequestDescriptorUnion_t descriptor;
1835 u64 *request = (u64 *)&descriptor;
1837 descriptor.SCSITarget.RequestFlags =
1838 MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
1839 descriptor.SCSITarget.MSIxIndex = _base_get_msix_index(ioc);
1840 descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1841 descriptor.SCSITarget.LMID = 0;
1842 descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1843 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1844 &ioc->scsi_lookup_lock);
1848 * _base_display_dell_branding - Disply branding string
1849 * @ioc: per adapter object
1854 _base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1856 char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1858 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1861 memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1862 switch (ioc->pdev->subsystem_device) {
1863 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1864 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1865 MPT2SAS_DELL_BRANDING_SIZE - 1);
1867 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1868 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1869 MPT2SAS_DELL_BRANDING_SIZE - 1);
1871 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1872 strncpy(dell_branding,
1873 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1874 MPT2SAS_DELL_BRANDING_SIZE - 1);
1876 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1877 strncpy(dell_branding,
1878 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
1879 MPT2SAS_DELL_BRANDING_SIZE - 1);
1881 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
1882 strncpy(dell_branding,
1883 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
1884 MPT2SAS_DELL_BRANDING_SIZE - 1);
1886 case MPT2SAS_DELL_PERC_H200_SSDID:
1887 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
1888 MPT2SAS_DELL_BRANDING_SIZE - 1);
1890 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
1891 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
1892 MPT2SAS_DELL_BRANDING_SIZE - 1);
1895 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
1899 printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
1900 " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
1901 ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
1902 ioc->pdev->subsystem_device);
1906 * _base_display_intel_branding - Display branding string
1907 * @ioc: per adapter object
1912 _base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
1914 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1917 switch (ioc->pdev->device) {
1918 case MPI2_MFGPAGE_DEVID_SAS2008:
1919 switch (ioc->pdev->subsystem_device) {
1920 case MPT2SAS_INTEL_RMS2LL080_SSDID:
1921 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1922 MPT2SAS_INTEL_RMS2LL080_BRANDING);
1924 case MPT2SAS_INTEL_RMS2LL040_SSDID:
1925 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1926 MPT2SAS_INTEL_RMS2LL040_BRANDING);
1931 case MPI2_MFGPAGE_DEVID_SAS2308_2:
1932 switch (ioc->pdev->subsystem_device) {
1933 case MPT2SAS_INTEL_RS25GB008_SSDID:
1934 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1935 MPT2SAS_INTEL_RS25GB008_BRANDING);
1946 * _base_display_hp_branding - Display branding string
1947 * @ioc: per adapter object
1952 _base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
1954 if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
1957 switch (ioc->pdev->device) {
1958 case MPI2_MFGPAGE_DEVID_SAS2004:
1959 switch (ioc->pdev->subsystem_device) {
1960 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
1961 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1962 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
1967 case MPI2_MFGPAGE_DEVID_SAS2308_2:
1968 switch (ioc->pdev->subsystem_device) {
1969 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
1970 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1971 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
1973 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
1974 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1975 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
1977 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
1978 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1979 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
1981 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
1982 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1983 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
1994 * _base_display_ioc_capabilities - Disply IOC's capabilities.
1995 * @ioc: per adapter object
2000 _base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
2005 u32 iounit_pg1_flags;
2008 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2009 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
2010 strncpy(desc, ioc->manu_pg0.ChipName, 16);
2011 printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
2012 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2014 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2015 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2016 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2017 ioc->facts.FWVersion.Word & 0x000000FF,
2019 (bios_version & 0xFF000000) >> 24,
2020 (bios_version & 0x00FF0000) >> 16,
2021 (bios_version & 0x0000FF00) >> 8,
2022 bios_version & 0x000000FF);
2024 _base_display_dell_branding(ioc);
2025 _base_display_intel_branding(ioc);
2026 _base_display_hp_branding(ioc);
2028 printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
2030 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2031 printk("Initiator");
2035 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2036 printk("%sTarget", i ? "," : "");
2042 printk("Capabilities=(");
2044 if (!ioc->hide_ir_msg) {
2045 if (ioc->facts.IOCCapabilities &
2046 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2052 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2053 printk("%sTLR", i ? "," : "");
2057 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2058 printk("%sMulticast", i ? "," : "");
2062 if (ioc->facts.IOCCapabilities &
2063 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2064 printk("%sBIDI Target", i ? "," : "");
2068 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2069 printk("%sEEDP", i ? "," : "");
2073 if (ioc->facts.IOCCapabilities &
2074 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2075 printk("%sSnapshot Buffer", i ? "," : "");
2079 if (ioc->facts.IOCCapabilities &
2080 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2081 printk("%sDiag Trace Buffer", i ? "," : "");
2085 if (ioc->facts.IOCCapabilities &
2086 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2087 printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
2091 if (ioc->facts.IOCCapabilities &
2092 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2093 printk("%sTask Set Full", i ? "," : "");
2097 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2098 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2099 printk("%sNCQ", i ? "," : "");
2107 * _base_update_missing_delay - change the missing delay timers
2108 * @ioc: per adapter object
2109 * @device_missing_delay: amount of time till device is reported missing
2110 * @io_missing_delay: interval IO is returned when there is a missing device
2114 * Passed on the command line, this function will modify the device missing
2115 * delay, as well as the io missing delay. This should be called at driver
2119 _base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
2120 u16 device_missing_delay, u8 io_missing_delay)
2122 u16 dmd, dmd_new, dmd_orignal;
2123 u8 io_missing_delay_original;
2125 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2126 Mpi2ConfigReply_t mpi_reply;
2130 mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
2134 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2135 sizeof(Mpi2SasIOUnit1PhyData_t));
2136 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2137 if (!sas_iounit_pg1) {
2138 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2139 ioc->name, __FILE__, __LINE__, __func__);
2142 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2143 sas_iounit_pg1, sz))) {
2144 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2145 ioc->name, __FILE__, __LINE__, __func__);
2148 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2149 MPI2_IOCSTATUS_MASK;
2150 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2151 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2152 ioc->name, __FILE__, __LINE__, __func__);
2156 /* device missing delay */
2157 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2158 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2159 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2161 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2163 if (device_missing_delay > 0x7F) {
2164 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2165 device_missing_delay;
2167 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2169 dmd = device_missing_delay;
2170 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2172 /* io missing delay */
2173 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2174 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2176 if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2178 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2180 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2183 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2184 printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
2185 "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
2186 printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
2187 "new(%d)\n", ioc->name, io_missing_delay_original,
2189 ioc->device_missing_delay = dmd_new;
2190 ioc->io_missing_delay = io_missing_delay;
2194 kfree(sas_iounit_pg1);
2198 * _base_static_config_pages - static start of day config pages
2199 * @ioc: per adapter object
2204 _base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
2206 Mpi2ConfigReply_t mpi_reply;
2207 u32 iounit_pg1_flags;
2209 mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
2210 if (ioc->ir_firmware)
2211 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2213 mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
2214 mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
2215 mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
2216 mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
2217 mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2218 _base_display_ioc_capabilities(ioc);
2221 * Enable task_set_full handling in iounit_pg1 when the
2222 * facts capabilities indicate that its supported.
2224 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2225 if ((ioc->facts.IOCCapabilities &
2226 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
2228 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2231 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2232 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
2233 mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2238 * _base_release_memory_pools - release memory
2239 * @ioc: per adapter object
2241 * Free memory allocated from _base_allocate_memory_pools.
2246 _base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
2250 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2254 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2255 ioc->request, ioc->request_dma);
2256 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
2257 ": free\n", ioc->name, ioc->request));
2258 ioc->request = NULL;
2262 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2263 if (ioc->sense_dma_pool)
2264 pci_pool_destroy(ioc->sense_dma_pool);
2265 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
2266 ": free\n", ioc->name, ioc->sense));
2271 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2272 if (ioc->reply_dma_pool)
2273 pci_pool_destroy(ioc->reply_dma_pool);
2274 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
2275 ": free\n", ioc->name, ioc->reply));
2279 if (ioc->reply_free) {
2280 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2281 ioc->reply_free_dma);
2282 if (ioc->reply_free_dma_pool)
2283 pci_pool_destroy(ioc->reply_free_dma_pool);
2284 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
2285 "(0x%p): free\n", ioc->name, ioc->reply_free));
2286 ioc->reply_free = NULL;
2289 if (ioc->reply_post_free) {
2290 pci_pool_free(ioc->reply_post_free_dma_pool,
2291 ioc->reply_post_free, ioc->reply_post_free_dma);
2292 if (ioc->reply_post_free_dma_pool)
2293 pci_pool_destroy(ioc->reply_post_free_dma_pool);
2294 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2295 "reply_post_free_pool(0x%p): free\n", ioc->name,
2296 ioc->reply_post_free));
2297 ioc->reply_post_free = NULL;
2300 if (ioc->config_page) {
2301 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2302 "config_page(0x%p): free\n", ioc->name,
2304 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2305 ioc->config_page, ioc->config_page_dma);
2308 if (ioc->scsi_lookup) {
2309 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2310 ioc->scsi_lookup = NULL;
2312 kfree(ioc->hpr_lookup);
2313 kfree(ioc->internal_lookup);
2314 if (ioc->chain_lookup) {
2315 for (i = 0; i < ioc->chain_depth; i++) {
2316 if (ioc->chain_lookup[i].chain_buffer)
2317 pci_pool_free(ioc->chain_dma_pool,
2318 ioc->chain_lookup[i].chain_buffer,
2319 ioc->chain_lookup[i].chain_buffer_dma);
2321 if (ioc->chain_dma_pool)
2322 pci_pool_destroy(ioc->chain_dma_pool);
2323 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2324 ioc->chain_lookup = NULL;
2330 * _base_allocate_memory_pools - allocate start of day memory pools
2331 * @ioc: per adapter object
2332 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2334 * Returns 0 success, anything else error
2337 _base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2339 struct mpt2sas_facts *facts;
2340 u16 max_sge_elements;
2341 u16 chains_needed_per_io;
2342 u32 sz, total_sz, reply_post_free_sz;
2344 u16 max_request_credit;
2347 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2351 facts = &ioc->facts;
2353 /* command line tunables for max sgl entries */
2354 if (max_sgl_entries != -1) {
2355 ioc->shost->sg_tablesize = (max_sgl_entries <
2356 MPT2SAS_SG_DEPTH) ? max_sgl_entries :
2359 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
2362 /* command line tunables for max controller queue depth */
2363 if (max_queue_depth != -1 && max_queue_depth != 0) {
2364 max_request_credit = min_t(u16, max_queue_depth +
2365 ioc->hi_priority_depth + ioc->internal_depth,
2366 facts->RequestCredit);
2367 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
2368 max_request_credit = MAX_HBA_QUEUE_DEPTH;
2370 max_request_credit = min_t(u16, facts->RequestCredit,
2371 MAX_HBA_QUEUE_DEPTH);
2373 ioc->hba_queue_depth = max_request_credit;
2374 ioc->hi_priority_depth = facts->HighPriorityCredit;
2375 ioc->internal_depth = ioc->hi_priority_depth + 5;
2377 /* request frame size */
2378 ioc->request_sz = facts->IOCRequestFrameSize * 4;
2380 /* reply frame size */
2381 ioc->reply_sz = facts->ReplyFrameSize * 4;
2385 /* calculate number of sg elements left over in the 1st frame */
2386 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2387 sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
2388 ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
2390 /* now do the same for a chain buffer */
2391 max_sge_elements = ioc->request_sz - ioc->sge_size;
2392 ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
2394 ioc->chain_offset_value_for_main_message =
2395 ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
2396 (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
2399 * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2401 chains_needed_per_io = ((ioc->shost->sg_tablesize -
2402 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2404 if (chains_needed_per_io > facts->MaxChainDepth) {
2405 chains_needed_per_io = facts->MaxChainDepth;
2406 ioc->shost->sg_tablesize = min_t(u16,
2407 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2408 * chains_needed_per_io), ioc->shost->sg_tablesize);
2410 ioc->chains_needed_per_io = chains_needed_per_io;
2412 /* reply free queue sizing - taking into account for 64 FW events */
2413 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2415 /* align the reply post queue on the next 16 count boundary */
2416 if (!ioc->reply_free_queue_depth % 16)
2417 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth + 16;
2419 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth +
2420 32 - (ioc->reply_free_queue_depth % 16);
2421 if (ioc->reply_post_queue_depth >
2422 facts->MaxReplyDescriptorPostQueueDepth) {
2423 ioc->reply_post_queue_depth = min_t(u16,
2424 (facts->MaxReplyDescriptorPostQueueDepth -
2425 (facts->MaxReplyDescriptorPostQueueDepth % 16)),
2426 (ioc->hba_queue_depth - (ioc->hba_queue_depth % 16)));
2427 ioc->reply_free_queue_depth = ioc->reply_post_queue_depth - 16;
2428 ioc->hba_queue_depth = ioc->reply_free_queue_depth - 64;
2432 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
2433 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2434 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2435 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2436 ioc->chains_needed_per_io));
2438 ioc->scsiio_depth = ioc->hba_queue_depth -
2439 ioc->hi_priority_depth - ioc->internal_depth;
2441 /* set the scsi host can_queue depth
2442 * with some internal commands that could be outstanding
2444 ioc->shost->can_queue = ioc->scsiio_depth;
2445 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
2446 "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
2448 /* contiguous pool for request and chains, 16 byte align, one extra "
2451 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
2452 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
2454 /* hi-priority queue */
2455 sz += (ioc->hi_priority_depth * ioc->request_sz);
2457 /* internal queue */
2458 sz += (ioc->internal_depth * ioc->request_sz);
2460 ioc->request_dma_sz = sz;
2461 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2462 if (!ioc->request) {
2463 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2464 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2465 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
2466 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2467 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
2470 ioc->hba_queue_depth = max_request_credit - retry_sz;
2471 goto retry_allocation;
2475 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2476 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2477 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
2478 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2481 /* hi-priority queue */
2482 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
2484 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
2487 /* internal queue */
2488 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2490 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2494 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2495 "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2496 ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2497 (ioc->hba_queue_depth * ioc->request_sz)/1024));
2498 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2499 ioc->name, (unsigned long long) ioc->request_dma));
2502 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
2503 ioc->scsi_lookup_pages = get_order(sz);
2504 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
2505 GFP_KERNEL, ioc->scsi_lookup_pages);
2506 if (!ioc->scsi_lookup) {
2507 printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
2508 "sz(%d)\n", ioc->name, (int)sz);
2512 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2513 "depth(%d)\n", ioc->name, ioc->request,
2514 ioc->scsiio_depth));
2516 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
2517 sz = ioc->chain_depth * sizeof(struct chain_tracker);
2518 ioc->chain_pages = get_order(sz);
2520 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
2521 GFP_KERNEL, ioc->chain_pages);
2522 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
2523 ioc->request_sz, 16, 0);
2524 if (!ioc->chain_dma_pool) {
2525 printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
2526 "failed\n", ioc->name);
2529 for (i = 0; i < ioc->chain_depth; i++) {
2530 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
2531 ioc->chain_dma_pool , GFP_KERNEL,
2532 &ioc->chain_lookup[i].chain_buffer_dma);
2533 if (!ioc->chain_lookup[i].chain_buffer) {
2534 ioc->chain_depth = i;
2537 total_sz += ioc->request_sz;
2540 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
2541 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2542 ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2543 ioc->request_sz))/1024));
2545 /* initialize hi-priority queue smid's */
2546 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2547 sizeof(struct request_tracker), GFP_KERNEL);
2548 if (!ioc->hpr_lookup) {
2549 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2553 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2554 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2555 "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2556 ioc->hi_priority_depth, ioc->hi_priority_smid));
2558 /* initialize internal queue smid's */
2559 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2560 sizeof(struct request_tracker), GFP_KERNEL);
2561 if (!ioc->internal_lookup) {
2562 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2566 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2567 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2568 "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2569 ioc->internal_depth, ioc->internal_smid));
2571 /* sense buffers, 4 byte align */
2572 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
2573 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2575 if (!ioc->sense_dma_pool) {
2576 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2580 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2583 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2587 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2588 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
2589 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
2590 SCSI_SENSE_BUFFERSIZE, sz/1024));
2591 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2592 ioc->name, (unsigned long long)ioc->sense_dma));
2595 /* reply pool, 4 byte align */
2596 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2597 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2599 if (!ioc->reply_dma_pool) {
2600 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2604 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2607 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2611 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2612 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
2613 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2614 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2615 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2616 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2617 ioc->name, (unsigned long long)ioc->reply_dma));
2620 /* reply free queue, 16 byte align */
2621 sz = ioc->reply_free_queue_depth * 4;
2622 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2623 ioc->pdev, sz, 16, 0);
2624 if (!ioc->reply_free_dma_pool) {
2625 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2626 "failed\n", ioc->name);
2629 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2630 &ioc->reply_free_dma);
2631 if (!ioc->reply_free) {
2632 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2633 "failed\n", ioc->name);
2636 memset(ioc->reply_free, 0, sz);
2637 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2638 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2639 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2640 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2641 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2644 /* reply post queue, 16 byte align */
2645 reply_post_free_sz = ioc->reply_post_queue_depth *
2646 sizeof(Mpi2DefaultReplyDescriptor_t);
2647 if (_base_is_controller_msix_enabled(ioc))
2648 sz = reply_post_free_sz * ioc->reply_queue_count;
2650 sz = reply_post_free_sz;
2651 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2652 ioc->pdev, sz, 16, 0);
2653 if (!ioc->reply_post_free_dma_pool) {
2654 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
2655 "failed\n", ioc->name);
2658 ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2659 GFP_KERNEL, &ioc->reply_post_free_dma);
2660 if (!ioc->reply_post_free) {
2661 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
2662 "failed\n", ioc->name);
2665 memset(ioc->reply_post_free, 0, sz);
2666 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
2667 "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2668 ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2670 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
2671 "(0x%llx)\n", ioc->name, (unsigned long long)
2672 ioc->reply_post_free_dma));
2675 ioc->config_page_sz = 512;
2676 ioc->config_page = pci_alloc_consistent(ioc->pdev,
2677 ioc->config_page_sz, &ioc->config_page_dma);
2678 if (!ioc->config_page) {
2679 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2680 "failed\n", ioc->name);
2683 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2684 "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2685 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2686 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2687 total_sz += ioc->config_page_sz;
2689 printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2690 ioc->name, total_sz/1024);
2691 printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2692 "Max Controller Queue Depth(%d)\n",
2693 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2694 printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2695 ioc->name, ioc->shost->sg_tablesize);
2704 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2705 * @ioc: Pointer to MPT_ADAPTER structure
2706 * @cooked: Request raw or cooked IOC state
2708 * Returns all IOC Doorbell register bits if cooked==0, else just the
2709 * Doorbell bits in MPI_IOC_STATE_MASK.
2712 mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2716 s = readl(&ioc->chip->Doorbell);
2717 sc = s & MPI2_IOC_STATE_MASK;
2718 return cooked ? sc : s;
2722 * _base_wait_on_iocstate - waiting on a particular ioc state
2723 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2724 * @timeout: timeout in second
2725 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2727 * Returns 0 for success, non-zero for failure.
2730 _base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2737 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2739 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2740 if (current_state == ioc_state)
2742 if (count && current_state == MPI2_IOC_STATE_FAULT)
2744 if (sleep_flag == CAN_SLEEP)
2751 return current_state;
2755 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2756 * a write to the doorbell)
2757 * @ioc: per adapter object
2758 * @timeout: timeout in second
2759 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2761 * Returns 0 for success, non-zero for failure.
2763 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2766 _base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2773 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2775 int_status = readl(&ioc->chip->HostInterruptStatus);
2776 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2777 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2778 "successful count(%d), timeout(%d)\n", ioc->name,
2779 __func__, count, timeout));
2782 if (sleep_flag == CAN_SLEEP)
2789 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2790 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2795 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2796 * @ioc: per adapter object
2797 * @timeout: timeout in second
2798 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2800 * Returns 0 for success, non-zero for failure.
2802 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2806 _base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2814 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2816 int_status = readl(&ioc->chip->HostInterruptStatus);
2817 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
2818 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2819 "successful count(%d), timeout(%d)\n", ioc->name,
2820 __func__, count, timeout));
2822 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2823 doorbell = readl(&ioc->chip->Doorbell);
2824 if ((doorbell & MPI2_IOC_STATE_MASK) ==
2825 MPI2_IOC_STATE_FAULT) {
2826 mpt2sas_base_fault_info(ioc , doorbell);
2829 } else if (int_status == 0xFFFFFFFF)
2832 if (sleep_flag == CAN_SLEEP)
2840 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2841 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2846 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2847 * @ioc: per adapter object
2848 * @timeout: timeout in second
2849 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2851 * Returns 0 for success, non-zero for failure.
2855 _base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2862 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2864 doorbell_reg = readl(&ioc->chip->Doorbell);
2865 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
2866 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2867 "successful count(%d), timeout(%d)\n", ioc->name,
2868 __func__, count, timeout));
2871 if (sleep_flag == CAN_SLEEP)
2878 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2879 "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2884 * _base_send_ioc_reset - send doorbell reset
2885 * @ioc: per adapter object
2886 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2887 * @timeout: timeout in second
2888 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2890 * Returns 0 for success, non-zero for failure.
2893 _base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2899 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2900 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2901 ioc->name, __func__);
2905 if (!(ioc->facts.IOCCapabilities &
2906 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2909 printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
2911 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
2912 &ioc->chip->Doorbell);
2913 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
2917 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
2918 timeout, sleep_flag);
2920 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
2921 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
2926 printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
2927 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
2932 * _base_handshake_req_reply_wait - send request thru doorbell interface
2933 * @ioc: per adapter object
2934 * @request_bytes: request length
2935 * @request: pointer having request payload
2936 * @reply_bytes: reply length
2937 * @reply: pointer to reply payload
2938 * @timeout: timeout in second
2939 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2941 * Returns 0 for success, non-zero for failure.
2944 _base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
2945 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
2947 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
2953 /* make sure doorbell is not in use */
2954 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
2955 printk(MPT2SAS_ERR_FMT "doorbell is in use "
2956 " (line=%d)\n", ioc->name, __LINE__);
2960 /* clear pending doorbell interrupts from previous state changes */
2961 if (readl(&ioc->chip->HostInterruptStatus) &
2962 MPI2_HIS_IOC2SYS_DB_STATUS)
2963 writel(0, &ioc->chip->HostInterruptStatus);
2965 /* send message to ioc */
2966 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
2967 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
2968 &ioc->chip->Doorbell);
2970 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
2971 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2972 "int failed (line=%d)\n", ioc->name, __LINE__);
2975 writel(0, &ioc->chip->HostInterruptStatus);
2977 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
2978 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2979 "ack failed (line=%d)\n", ioc->name, __LINE__);
2983 /* send message 32-bits at a time */
2984 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
2985 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
2986 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
2991 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2992 "sending request failed (line=%d)\n", ioc->name, __LINE__);
2996 /* now wait for the reply */
2997 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
2998 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2999 "int failed (line=%d)\n", ioc->name, __LINE__);
3003 /* read the first two 16-bits, it gives the total length of the reply */
3004 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3005 & MPI2_DOORBELL_DATA_MASK);
3006 writel(0, &ioc->chip->HostInterruptStatus);
3007 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3008 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3009 "int failed (line=%d)\n", ioc->name, __LINE__);
3012 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3013 & MPI2_DOORBELL_DATA_MASK);
3014 writel(0, &ioc->chip->HostInterruptStatus);
3016 for (i = 2; i < default_reply->MsgLength * 2; i++) {
3017 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3018 printk(MPT2SAS_ERR_FMT "doorbell "
3019 "handshake int failed (line=%d)\n", ioc->name,
3023 if (i >= reply_bytes/2) /* overflow case */
3024 dummy = readl(&ioc->chip->Doorbell);
3026 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3027 & MPI2_DOORBELL_DATA_MASK);
3028 writel(0, &ioc->chip->HostInterruptStatus);
3031 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3032 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3033 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
3034 " (line=%d)\n", ioc->name, __LINE__));
3036 writel(0, &ioc->chip->HostInterruptStatus);
3038 if (ioc->logging_level & MPT_DEBUG_INIT) {
3039 mfp = (__le32 *)reply;
3040 printk(KERN_INFO "\toffset:data\n");
3041 for (i = 0; i < reply_bytes/4; i++)
3042 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
3043 le32_to_cpu(mfp[i]));
3049 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
3050 * @ioc: per adapter object
3051 * @mpi_reply: the reply payload from FW
3052 * @mpi_request: the request payload sent to FW
3054 * The SAS IO Unit Control Request message allows the host to perform low-level
3055 * operations, such as resets on the PHYs of the IO Unit, also allows the host
3056 * to obtain the IOC assigned device handles for a device if it has other
3057 * identifying information about the device, in addition allows the host to
3058 * remove IOC resources associated with the device.
3060 * Returns 0 for success, non-zero for failure.
3063 mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
3064 Mpi2SasIoUnitControlReply_t *mpi_reply,
3065 Mpi2SasIoUnitControlRequest_t *mpi_request)
3069 unsigned long timeleft;
3073 u16 wait_state_count;
3075 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3078 mutex_lock(&ioc->base_cmds.mutex);
3080 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3081 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3082 ioc->name, __func__);
3087 wait_state_count = 0;
3088 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3089 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3090 if (wait_state_count++ == 10) {
3091 printk(MPT2SAS_ERR_FMT
3092 "%s: failed due to ioc not operational\n",
3093 ioc->name, __func__);
3098 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3099 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3100 "operational state(count=%d)\n", ioc->name,
3101 __func__, wait_state_count);
3104 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3106 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3107 ioc->name, __func__);
3113 ioc->base_cmds.status = MPT2_CMD_PENDING;
3114 request = mpt2sas_base_get_msg_frame(ioc, smid);
3115 ioc->base_cmds.smid = smid;
3116 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3117 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3118 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3119 ioc->ioc_link_reset_in_progress = 1;
3120 mpt2sas_base_put_smid_default(ioc, smid);
3121 init_completion(&ioc->base_cmds.done);
3122 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3123 msecs_to_jiffies(10000));
3124 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3125 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
3126 ioc->ioc_link_reset_in_progress)
3127 ioc->ioc_link_reset_in_progress = 0;
3128 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3129 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3130 ioc->name, __func__);
3131 _debug_dump_mf(mpi_request,
3132 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
3133 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3135 goto issue_host_reset;
3137 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3138 memcpy(mpi_reply, ioc->base_cmds.reply,
3139 sizeof(Mpi2SasIoUnitControlReply_t));
3141 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
3142 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3147 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3149 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3152 mutex_unlock(&ioc->base_cmds.mutex);
3158 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
3159 * @ioc: per adapter object
3160 * @mpi_reply: the reply payload from FW
3161 * @mpi_request: the request payload sent to FW
3163 * The SCSI Enclosure Processor request message causes the IOC to
3164 * communicate with SES devices to control LED status signals.
3166 * Returns 0 for success, non-zero for failure.
3169 mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
3170 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
3174 unsigned long timeleft;
3178 u16 wait_state_count;
3180 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3183 mutex_lock(&ioc->base_cmds.mutex);
3185 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3186 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3187 ioc->name, __func__);
3192 wait_state_count = 0;
3193 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3194 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3195 if (wait_state_count++ == 10) {
3196 printk(MPT2SAS_ERR_FMT
3197 "%s: failed due to ioc not operational\n",
3198 ioc->name, __func__);
3203 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3204 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3205 "operational state(count=%d)\n", ioc->name,
3206 __func__, wait_state_count);
3209 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3211 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3212 ioc->name, __func__);
3218 ioc->base_cmds.status = MPT2_CMD_PENDING;
3219 request = mpt2sas_base_get_msg_frame(ioc, smid);
3220 ioc->base_cmds.smid = smid;
3221 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
3222 mpt2sas_base_put_smid_default(ioc, smid);
3223 init_completion(&ioc->base_cmds.done);
3224 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3225 msecs_to_jiffies(10000));
3226 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3227 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3228 ioc->name, __func__);
3229 _debug_dump_mf(mpi_request,
3230 sizeof(Mpi2SepRequest_t)/4);
3231 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3233 goto issue_host_reset;
3235 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3236 memcpy(mpi_reply, ioc->base_cmds.reply,
3237 sizeof(Mpi2SepReply_t));
3239 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3240 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3245 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3247 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3250 mutex_unlock(&ioc->base_cmds.mutex);
3255 * _base_get_port_facts - obtain port facts reply and save in ioc
3256 * @ioc: per adapter object
3257 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3259 * Returns 0 for success, non-zero for failure.
3262 _base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
3264 Mpi2PortFactsRequest_t mpi_request;
3265 Mpi2PortFactsReply_t mpi_reply;
3266 struct mpt2sas_port_facts *pfacts;
3267 int mpi_reply_sz, mpi_request_sz, r;
3269 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3272 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3273 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3274 memset(&mpi_request, 0, mpi_request_sz);
3275 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3276 mpi_request.PortNumber = port;
3277 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3278 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3281 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3282 ioc->name, __func__, r);
3286 pfacts = &ioc->pfacts[port];
3287 memset(pfacts, 0, sizeof(struct mpt2sas_port_facts));
3288 pfacts->PortNumber = mpi_reply.PortNumber;
3289 pfacts->VP_ID = mpi_reply.VP_ID;
3290 pfacts->VF_ID = mpi_reply.VF_ID;
3291 pfacts->MaxPostedCmdBuffers =
3292 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3298 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3299 * @ioc: per adapter object
3300 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3302 * Returns 0 for success, non-zero for failure.
3305 _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3307 Mpi2IOCFactsRequest_t mpi_request;
3308 Mpi2IOCFactsReply_t mpi_reply;
3309 struct mpt2sas_facts *facts;
3310 int mpi_reply_sz, mpi_request_sz, r;
3312 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3315 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3316 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3317 memset(&mpi_request, 0, mpi_request_sz);
3318 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3319 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3320 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3323 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3324 ioc->name, __func__, r);
3328 facts = &ioc->facts;
3329 memset(facts, 0, sizeof(struct mpt2sas_facts));
3330 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3331 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3332 facts->VP_ID = mpi_reply.VP_ID;
3333 facts->VF_ID = mpi_reply.VF_ID;
3334 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3335 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3336 facts->WhoInit = mpi_reply.WhoInit;
3337 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
3338 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
3339 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3340 facts->MaxReplyDescriptorPostQueueDepth =
3341 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3342 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3343 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3344 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3345 ioc->ir_firmware = 1;
3346 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3347 facts->IOCRequestFrameSize =
3348 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3349 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3350 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3351 ioc->shost->max_id = -1;
3352 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3353 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3354 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3355 facts->HighPriorityCredit =
3356 le16_to_cpu(mpi_reply.HighPriorityCredit);
3357 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3358 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3360 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
3361 "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
3362 facts->MaxChainDepth));
3363 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
3364 "reply frame size(%d)\n", ioc->name,
3365 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3370 * _base_send_ioc_init - send ioc_init to firmware
3371 * @ioc: per adapter object
3372 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3374 * Returns 0 for success, non-zero for failure.
3377 _base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3379 Mpi2IOCInitRequest_t mpi_request;
3380 Mpi2IOCInitReply_t mpi_reply;
3382 struct timeval current_time;
3385 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3388 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3389 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3390 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
3391 mpi_request.VF_ID = 0; /* TODO */
3392 mpi_request.VP_ID = 0;
3393 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3394 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3396 if (_base_is_controller_msix_enabled(ioc))
3397 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
3398 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3399 mpi_request.ReplyDescriptorPostQueueDepth =
3400 cpu_to_le16(ioc->reply_post_queue_depth);
3401 mpi_request.ReplyFreeQueueDepth =
3402 cpu_to_le16(ioc->reply_free_queue_depth);
3404 mpi_request.SenseBufferAddressHigh =
3405 cpu_to_le32((u64)ioc->sense_dma >> 32);
3406 mpi_request.SystemReplyAddressHigh =
3407 cpu_to_le32((u64)ioc->reply_dma >> 32);
3408 mpi_request.SystemRequestFrameBaseAddress =
3409 cpu_to_le64((u64)ioc->request_dma);
3410 mpi_request.ReplyFreeQueueAddress =
3411 cpu_to_le64((u64)ioc->reply_free_dma);
3412 mpi_request.ReplyDescriptorPostQueueAddress =
3413 cpu_to_le64((u64)ioc->reply_post_free_dma);
3416 /* This time stamp specifies number of milliseconds
3417 * since epoch ~ midnight January 1, 1970.
3419 do_gettimeofday(¤t_time);
3420 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3421 (current_time.tv_usec / 1000));
3423 if (ioc->logging_level & MPT_DEBUG_INIT) {
3427 mfp = (__le32 *)&mpi_request;
3428 printk(KERN_INFO "\toffset:data\n");
3429 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
3430 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
3431 le32_to_cpu(mfp[i]));
3434 r = _base_handshake_req_reply_wait(ioc,
3435 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3436 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3440 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3441 ioc->name, __func__, r);
3445 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3446 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
3447 mpi_reply.IOCLogInfo) {
3448 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
3456 * mpt2sas_port_enable_done - command completion routine for port enable
3457 * @ioc: per adapter object
3458 * @smid: system request message index
3459 * @msix_index: MSIX table index supplied by the OS
3460 * @reply: reply message frame(lower 32bit addr)
3462 * Return 1 meaning mf should be freed from _base_interrupt
3463 * 0 means the mf is freed from this function.
3466 mpt2sas_port_enable_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3469 MPI2DefaultReply_t *mpi_reply;
3472 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3473 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
3476 if (ioc->port_enable_cmds.status == MPT2_CMD_NOT_USED)
3479 ioc->port_enable_cmds.status |= MPT2_CMD_COMPLETE;
3481 ioc->port_enable_cmds.status |= MPT2_CMD_REPLY_VALID;
3482 memcpy(ioc->port_enable_cmds.reply, mpi_reply,
3483 mpi_reply->MsgLength*4);
3485 ioc->port_enable_cmds.status &= ~MPT2_CMD_PENDING;
3487 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3489 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3490 ioc->port_enable_failed = 1;
3492 if (ioc->is_driver_loading) {
3493 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
3494 mpt2sas_port_enable_complete(ioc);
3497 ioc->start_scan_failed = ioc_status;
3498 ioc->start_scan = 0;
3502 complete(&ioc->port_enable_cmds.done);
3508 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3509 * @ioc: per adapter object
3510 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3512 * Returns 0 for success, non-zero for failure.
3515 _base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3517 Mpi2PortEnableRequest_t *mpi_request;
3518 Mpi2PortEnableReply_t *mpi_reply;
3519 unsigned long timeleft;
3524 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3526 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3527 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3528 ioc->name, __func__);
3532 smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3534 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3535 ioc->name, __func__);
3539 ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3540 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3541 ioc->port_enable_cmds.smid = smid;
3542 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3543 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3545 init_completion(&ioc->port_enable_cmds.done);
3546 mpt2sas_base_put_smid_default(ioc, smid);
3547 timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
3549 if (!(ioc->port_enable_cmds.status & MPT2_CMD_COMPLETE)) {
3550 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3551 ioc->name, __func__);
3552 _debug_dump_mf(mpi_request,
3553 sizeof(Mpi2PortEnableRequest_t)/4);
3554 if (ioc->port_enable_cmds.status & MPT2_CMD_RESET)
3560 mpi_reply = ioc->port_enable_cmds.reply;
3562 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3563 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3564 printk(MPT2SAS_ERR_FMT "%s: failed with (ioc_status=0x%08x)\n",
3565 ioc->name, __func__, ioc_status);
3570 ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
3571 printk(MPT2SAS_INFO_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
3572 "SUCCESS" : "FAILED"));
3577 * mpt2sas_port_enable - initiate firmware discovery (don't wait for reply)
3578 * @ioc: per adapter object
3580 * Returns 0 for success, non-zero for failure.
3583 mpt2sas_port_enable(struct MPT2SAS_ADAPTER *ioc)
3585 Mpi2PortEnableRequest_t *mpi_request;
3588 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3590 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3591 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3592 ioc->name, __func__);
3596 smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3598 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3599 ioc->name, __func__);
3603 ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3604 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3605 ioc->port_enable_cmds.smid = smid;
3606 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3607 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3609 mpt2sas_base_put_smid_default(ioc, smid);
3614 * _base_determine_wait_on_discovery - desposition
3615 * @ioc: per adapter object
3617 * Decide whether to wait on discovery to complete. Used to either
3618 * locate boot device, or report volumes ahead of physical devices.
3620 * Returns 1 for wait, 0 for don't wait
3623 _base_determine_wait_on_discovery(struct MPT2SAS_ADAPTER *ioc)
3625 /* We wait for discovery to complete if IR firmware is loaded.
3626 * The sas topology events arrive before PD events, so we need time to
3627 * turn on the bit in ioc->pd_handles to indicate PD
3628 * Also, it maybe required to report Volumes ahead of physical
3629 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
3631 if (ioc->ir_firmware)
3634 /* if no Bios, then we don't need to wait */
3635 if (!ioc->bios_pg3.BiosVersion)
3638 /* Bios is present, then we drop down here.
3640 * If there any entries in the Bios Page 2, then we wait
3641 * for discovery to complete.
3644 /* Current Boot Device */
3645 if ((ioc->bios_pg2.CurrentBootDeviceForm &
3646 MPI2_BIOSPAGE2_FORM_MASK) ==
3647 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3648 /* Request Boot Device */
3649 (ioc->bios_pg2.ReqBootDeviceForm &
3650 MPI2_BIOSPAGE2_FORM_MASK) ==
3651 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3652 /* Alternate Request Boot Device */
3653 (ioc->bios_pg2.ReqAltBootDeviceForm &
3654 MPI2_BIOSPAGE2_FORM_MASK) ==
3655 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
3663 * _base_unmask_events - turn on notification for this event
3664 * @ioc: per adapter object
3665 * @event: firmware event
3667 * The mask is stored in ioc->event_masks.
3670 _base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3677 desired_event = (1 << (event % 32));
3680 ioc->event_masks[0] &= ~desired_event;
3681 else if (event < 64)
3682 ioc->event_masks[1] &= ~desired_event;
3683 else if (event < 96)
3684 ioc->event_masks[2] &= ~desired_event;
3685 else if (event < 128)
3686 ioc->event_masks[3] &= ~desired_event;
3690 * _base_event_notification - send event notification
3691 * @ioc: per adapter object
3692 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3694 * Returns 0 for success, non-zero for failure.
3697 _base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3699 Mpi2EventNotificationRequest_t *mpi_request;
3700 unsigned long timeleft;
3705 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3708 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3709 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3710 ioc->name, __func__);
3714 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3716 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3717 ioc->name, __func__);
3720 ioc->base_cmds.status = MPT2_CMD_PENDING;
3721 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3722 ioc->base_cmds.smid = smid;
3723 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
3724 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3725 mpi_request->VF_ID = 0; /* TODO */
3726 mpi_request->VP_ID = 0;
3727 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3728 mpi_request->EventMasks[i] =
3729 cpu_to_le32(ioc->event_masks[i]);
3730 mpt2sas_base_put_smid_default(ioc, smid);
3731 init_completion(&ioc->base_cmds.done);
3732 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
3733 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3734 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3735 ioc->name, __func__);
3736 _debug_dump_mf(mpi_request,
3737 sizeof(Mpi2EventNotificationRequest_t)/4);
3738 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3743 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
3744 ioc->name, __func__));
3745 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3750 * mpt2sas_base_validate_event_type - validating event types
3751 * @ioc: per adapter object
3752 * @event: firmware event
3754 * This will turn on firmware event notification when application
3755 * ask for that event. We don't mask events that are already enabled.
3758 mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
3761 u32 event_mask, desired_event;
3762 u8 send_update_to_fw;
3764 for (i = 0, send_update_to_fw = 0; i <
3765 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
3766 event_mask = ~event_type[i];
3768 for (j = 0; j < 32; j++) {
3769 if (!(event_mask & desired_event) &&
3770 (ioc->event_masks[i] & desired_event)) {
3771 ioc->event_masks[i] &= ~desired_event;
3772 send_update_to_fw = 1;
3774 desired_event = (desired_event << 1);
3778 if (!send_update_to_fw)
3781 mutex_lock(&ioc->base_cmds.mutex);
3782 _base_event_notification(ioc, CAN_SLEEP);
3783 mutex_unlock(&ioc->base_cmds.mutex);
3787 * _base_diag_reset - the "big hammer" start of day reset
3788 * @ioc: per adapter object
3789 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3791 * Returns 0 for success, non-zero for failure.
3794 _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3796 u32 host_diagnostic;
3801 printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
3802 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
3807 /* Write magic sequence to WriteSequence register
3808 * Loop until in diagnostic mode
3810 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
3811 "sequence\n", ioc->name));
3812 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3813 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
3814 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
3815 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
3816 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
3817 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
3818 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
3821 if (sleep_flag == CAN_SLEEP)
3829 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3830 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
3831 "sequence: count(%d), host_diagnostic(0x%08x)\n",
3832 ioc->name, count, host_diagnostic));
3834 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
3836 hcb_size = readl(&ioc->chip->HCBSize);
3838 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
3840 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
3841 &ioc->chip->HostDiagnostic);
3843 /* don't access any registers for 50 milliseconds */
3846 /* 300 second max wait */
3847 for (count = 0; count < 3000000 ; count++) {
3849 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3851 if (host_diagnostic == 0xFFFFFFFF)
3853 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3857 if (sleep_flag == CAN_SLEEP)
3863 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3865 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
3866 "assuming the HCB Address points to good F/W\n",
3868 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3869 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3870 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3872 drsprintk(ioc, printk(MPT2SAS_INFO_FMT
3873 "re-enable the HCDW\n", ioc->name));
3874 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3875 &ioc->chip->HCBSize);
3878 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
3880 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3881 &ioc->chip->HostDiagnostic);
3883 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
3884 "diagnostic register\n", ioc->name));
3885 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3887 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
3888 "READY state\n", ioc->name));
3889 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3892 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3893 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3897 printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3901 printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3906 * _base_make_ioc_ready - put controller in READY state
3907 * @ioc: per adapter object
3908 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3909 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3911 * Returns 0 for success, non-zero for failure.
3914 _base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3915 enum reset_type type)
3920 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3923 if (ioc->pci_error_recovery)
3926 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
3927 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
3928 ioc->name, __func__, ioc_state));
3930 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
3933 if (ioc_state & MPI2_DOORBELL_USED) {
3934 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
3935 "active!\n", ioc->name));
3936 goto issue_diag_reset;
3939 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3940 mpt2sas_base_fault_info(ioc, ioc_state &
3941 MPI2_DOORBELL_DATA_MASK);
3942 goto issue_diag_reset;
3945 if (type == FORCE_BIG_HAMMER)
3946 goto issue_diag_reset;
3948 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
3949 if (!(_base_send_ioc_reset(ioc,
3950 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
3951 ioc->ioc_reset_count++;
3956 rc = _base_diag_reset(ioc, CAN_SLEEP);
3957 ioc->ioc_reset_count++;
3962 * _base_make_ioc_operational - put controller in OPERATIONAL state
3963 * @ioc: per adapter object
3964 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3966 * Returns 0 for success, non-zero for failure.
3969 _base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3972 unsigned long flags;
3975 struct _tr_list *delayed_tr, *delayed_tr_next;
3977 struct adapter_reply_queue *reply_q;
3978 long reply_post_free;
3979 u32 reply_post_free_sz;
3981 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3984 /* clean the delayed target reset list */
3985 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3986 &ioc->delayed_tr_list, list) {
3987 list_del(&delayed_tr->list);
3991 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3992 &ioc->delayed_tr_volume_list, list) {
3993 list_del(&delayed_tr->list);
3997 /* initialize the scsi lookup free list */
3998 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3999 INIT_LIST_HEAD(&ioc->free_list);
4001 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
4002 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
4003 ioc->scsi_lookup[i].cb_idx = 0xFF;
4004 ioc->scsi_lookup[i].smid = smid;
4005 ioc->scsi_lookup[i].scmd = NULL;
4006 ioc->scsi_lookup[i].direct_io = 0;
4007 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
4011 /* hi-priority queue */
4012 INIT_LIST_HEAD(&ioc->hpr_free_list);
4013 smid = ioc->hi_priority_smid;
4014 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
4015 ioc->hpr_lookup[i].cb_idx = 0xFF;
4016 ioc->hpr_lookup[i].smid = smid;
4017 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
4018 &ioc->hpr_free_list);
4021 /* internal queue */
4022 INIT_LIST_HEAD(&ioc->internal_free_list);
4023 smid = ioc->internal_smid;
4024 for (i = 0; i < ioc->internal_depth; i++, smid++) {
4025 ioc->internal_lookup[i].cb_idx = 0xFF;
4026 ioc->internal_lookup[i].smid = smid;
4027 list_add_tail(&ioc->internal_lookup[i].tracker_list,
4028 &ioc->internal_free_list);
4032 INIT_LIST_HEAD(&ioc->free_chain_list);
4033 for (i = 0; i < ioc->chain_depth; i++)
4034 list_add_tail(&ioc->chain_lookup[i].tracker_list,
4035 &ioc->free_chain_list);
4037 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4039 /* initialize Reply Free Queue */
4040 for (i = 0, reply_address = (u32)ioc->reply_dma ;
4041 i < ioc->reply_free_queue_depth ; i++, reply_address +=
4043 ioc->reply_free[i] = cpu_to_le32(reply_address);
4045 /* initialize reply queues */
4046 if (ioc->is_driver_loading)
4047 _base_assign_reply_queues(ioc);
4049 /* initialize Reply Post Free Queue */
4050 reply_post_free = (long)ioc->reply_post_free;
4051 reply_post_free_sz = ioc->reply_post_queue_depth *
4052 sizeof(Mpi2DefaultReplyDescriptor_t);
4053 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4054 reply_q->reply_post_host_index = 0;
4055 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
4057 for (i = 0; i < ioc->reply_post_queue_depth; i++)
4058 reply_q->reply_post_free[i].Words =
4059 cpu_to_le64(ULLONG_MAX);
4060 if (!_base_is_controller_msix_enabled(ioc))
4061 goto skip_init_reply_post_free_queue;
4062 reply_post_free += reply_post_free_sz;
4064 skip_init_reply_post_free_queue:
4066 r = _base_send_ioc_init(ioc, sleep_flag);
4070 /* initialize reply free host index */
4071 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
4072 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
4074 /* initialize reply post host index */
4075 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4076 writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
4077 &ioc->chip->ReplyPostHostIndex);
4078 if (!_base_is_controller_msix_enabled(ioc))
4079 goto skip_init_reply_post_host_index;
4082 skip_init_reply_post_host_index:
4084 _base_unmask_interrupts(ioc);
4086 r = _base_event_notification(ioc, sleep_flag);
4090 if (sleep_flag == CAN_SLEEP)
4091 _base_static_config_pages(ioc);
4094 if (ioc->is_driver_loading) {
4095 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
4097 hide_flag = (u8) (ioc->manu_pg10.OEMSpecificFlags0 &
4098 MFG_PAGE10_HIDE_SSDS_MASK);
4099 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
4100 ioc->mfg_pg10_hide_flag = hide_flag;
4102 ioc->wait_for_discovery_to_complete =
4103 _base_determine_wait_on_discovery(ioc);
4104 return r; /* scan_start and scan_finished support */
4106 r = _base_send_port_enable(ioc, sleep_flag);
4114 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
4115 * @ioc: per adapter object
4120 mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
4122 struct pci_dev *pdev = ioc->pdev;
4124 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4127 _base_mask_interrupts(ioc);
4128 ioc->shost_recovery = 1;
4129 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4130 ioc->shost_recovery = 0;
4131 _base_free_irq(ioc);
4132 _base_disable_msix(ioc);
4136 pci_release_selected_regions(ioc->pdev, ioc->bars);
4137 pci_disable_pcie_error_reporting(pdev);
4138 pci_disable_device(pdev);
4143 * mpt2sas_base_attach - attach controller instance
4144 * @ioc: per adapter object
4146 * Returns 0 for success, non-zero for failure.
4149 mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
4152 int cpu_id, last_cpu_id = 0;
4154 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4157 /* setup cpu_msix_table */
4158 ioc->cpu_count = num_online_cpus();
4159 for_each_online_cpu(cpu_id)
4160 last_cpu_id = cpu_id;
4161 ioc->cpu_msix_table_sz = last_cpu_id + 1;
4162 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
4163 ioc->reply_queue_count = 1;
4164 if (!ioc->cpu_msix_table) {
4165 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
4166 "cpu_msix_table failed!!!\n", ioc->name));
4168 goto out_free_resources;
4171 if (ioc->is_warpdrive) {
4172 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
4173 sizeof(resource_size_t *), GFP_KERNEL);
4174 if (!ioc->reply_post_host_index) {
4175 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation "
4176 "for cpu_msix_table failed!!!\n", ioc->name));
4178 goto out_free_resources;
4182 r = mpt2sas_base_map_resources(ioc);
4186 if (ioc->is_warpdrive) {
4187 ioc->reply_post_host_index[0] =
4188 (resource_size_t *)&ioc->chip->ReplyPostHostIndex;
4190 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
4191 ioc->reply_post_host_index[i] = (resource_size_t *)
4192 ((u8 *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
4196 pci_set_drvdata(ioc->pdev, ioc->shost);
4197 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
4199 goto out_free_resources;
4201 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4203 goto out_free_resources;
4205 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
4206 sizeof(struct mpt2sas_port_facts), GFP_KERNEL);
4209 goto out_free_resources;
4212 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
4213 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
4215 goto out_free_resources;
4218 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
4220 goto out_free_resources;
4222 init_waitqueue_head(&ioc->reset_wq);
4224 /* allocate memory pd handle bitmask list */
4225 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
4226 if (ioc->facts.MaxDevHandle % 8)
4227 ioc->pd_handles_sz++;
4228 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
4230 if (!ioc->pd_handles) {
4232 goto out_free_resources;
4235 ioc->fwfault_debug = mpt2sas_fwfault_debug;
4237 /* base internal command bits */
4238 mutex_init(&ioc->base_cmds.mutex);
4239 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4240 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
4242 /* port_enable command bits */
4243 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4244 ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
4246 /* transport internal command bits */
4247 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4248 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
4249 mutex_init(&ioc->transport_cmds.mutex);
4251 /* scsih internal command bits */
4252 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4253 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
4254 mutex_init(&ioc->scsih_cmds.mutex);
4256 /* task management internal command bits */
4257 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4258 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4259 mutex_init(&ioc->tm_cmds.mutex);
4261 /* config page internal command bits */
4262 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4263 ioc->config_cmds.status = MPT2_CMD_NOT_USED;
4264 mutex_init(&ioc->config_cmds.mutex);
4266 /* ctl module internal command bits */
4267 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4268 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4269 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
4270 mutex_init(&ioc->ctl_cmds.mutex);
4272 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4273 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4274 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
4275 !ioc->ctl_cmds.sense) {
4277 goto out_free_resources;
4280 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4281 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4282 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
4284 goto out_free_resources;
4287 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4288 ioc->event_masks[i] = -1;
4290 /* here we enable the events we care about */
4291 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
4292 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
4293 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
4294 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4295 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
4296 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
4297 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
4298 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
4299 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
4300 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
4301 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
4303 goto out_free_resources;
4305 if (missing_delay[0] != -1 && missing_delay[1] != -1)
4306 _base_update_missing_delay(ioc, missing_delay[0],
4313 ioc->remove_host = 1;
4314 mpt2sas_base_free_resources(ioc);
4315 _base_release_memory_pools(ioc);
4316 pci_set_drvdata(ioc->pdev, NULL);
4317 kfree(ioc->cpu_msix_table);
4318 if (ioc->is_warpdrive)
4319 kfree(ioc->reply_post_host_index);
4320 kfree(ioc->pd_handles);
4321 kfree(ioc->tm_cmds.reply);
4322 kfree(ioc->transport_cmds.reply);
4323 kfree(ioc->scsih_cmds.reply);
4324 kfree(ioc->config_cmds.reply);
4325 kfree(ioc->base_cmds.reply);
4326 kfree(ioc->port_enable_cmds.reply);
4327 kfree(ioc->ctl_cmds.reply);
4328 kfree(ioc->ctl_cmds.sense);
4330 ioc->ctl_cmds.reply = NULL;
4331 ioc->base_cmds.reply = NULL;
4332 ioc->tm_cmds.reply = NULL;
4333 ioc->scsih_cmds.reply = NULL;
4334 ioc->transport_cmds.reply = NULL;
4335 ioc->config_cmds.reply = NULL;
4342 * mpt2sas_base_detach - remove controller instance
4343 * @ioc: per adapter object
4348 mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
4351 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4354 mpt2sas_base_stop_watchdog(ioc);
4355 mpt2sas_base_free_resources(ioc);
4356 _base_release_memory_pools(ioc);
4357 pci_set_drvdata(ioc->pdev, NULL);
4358 kfree(ioc->cpu_msix_table);
4359 if (ioc->is_warpdrive)
4360 kfree(ioc->reply_post_host_index);
4361 kfree(ioc->pd_handles);
4363 kfree(ioc->ctl_cmds.reply);
4364 kfree(ioc->ctl_cmds.sense);
4365 kfree(ioc->base_cmds.reply);
4366 kfree(ioc->port_enable_cmds.reply);
4367 kfree(ioc->tm_cmds.reply);
4368 kfree(ioc->transport_cmds.reply);
4369 kfree(ioc->scsih_cmds.reply);
4370 kfree(ioc->config_cmds.reply);
4374 * _base_reset_handler - reset callback handler (for base)
4375 * @ioc: per adapter object
4376 * @reset_phase: phase
4378 * The handler for doing any required cleanup or initialization.
4380 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
4381 * MPT2_IOC_DONE_RESET
4386 _base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
4388 mpt2sas_scsih_reset_handler(ioc, reset_phase);
4389 mpt2sas_ctl_reset_handler(ioc, reset_phase);
4390 switch (reset_phase) {
4391 case MPT2_IOC_PRE_RESET:
4392 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4393 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
4395 case MPT2_IOC_AFTER_RESET:
4396 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4397 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
4398 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
4399 ioc->transport_cmds.status |= MPT2_CMD_RESET;
4400 mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
4401 complete(&ioc->transport_cmds.done);
4403 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
4404 ioc->base_cmds.status |= MPT2_CMD_RESET;
4405 mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
4406 complete(&ioc->base_cmds.done);
4408 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
4409 ioc->port_enable_failed = 1;
4410 ioc->port_enable_cmds.status |= MPT2_CMD_RESET;
4411 mpt2sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
4412 if (ioc->is_driver_loading) {
4413 ioc->start_scan_failed =
4414 MPI2_IOCSTATUS_INTERNAL_ERROR;
4415 ioc->start_scan = 0;
4416 ioc->port_enable_cmds.status =
4419 complete(&ioc->port_enable_cmds.done);
4422 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
4423 ioc->config_cmds.status |= MPT2_CMD_RESET;
4424 mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
4425 ioc->config_cmds.smid = USHRT_MAX;
4426 complete(&ioc->config_cmds.done);
4429 case MPT2_IOC_DONE_RESET:
4430 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4431 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
4437 * _wait_for_commands_to_complete - reset controller
4438 * @ioc: Pointer to MPT_ADAPTER structure
4439 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4441 * This function waiting(3s) for all pending commands to complete
4442 * prior to putting controller in reset.
4445 _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4448 unsigned long flags;
4451 ioc->pending_io_count = 0;
4452 if (sleep_flag != CAN_SLEEP)
4455 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4456 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
4459 /* pending command count */
4460 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4461 for (i = 0; i < ioc->scsiio_depth; i++)
4462 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
4463 ioc->pending_io_count++;
4464 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4466 if (!ioc->pending_io_count)
4469 /* wait for pending commands to complete */
4470 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
4474 * mpt2sas_base_hard_reset_handler - reset controller
4475 * @ioc: Pointer to MPT_ADAPTER structure
4476 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4477 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4479 * Returns 0 for success, non-zero for failure.
4482 mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4483 enum reset_type type)
4486 unsigned long flags;
4488 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
4491 if (ioc->pci_error_recovery) {
4492 printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
4493 ioc->name, __func__);
4498 if (mpt2sas_fwfault_debug)
4499 mpt2sas_halt_firmware(ioc);
4501 /* TODO - What we really should be doing is pulling
4502 * out all the code associated with NO_SLEEP; its never used.
4503 * That is legacy code from mpt fusion driver, ported over.
4504 * I will leave this BUG_ON here for now till its been resolved.
4506 BUG_ON(sleep_flag == NO_SLEEP);
4508 /* wait for an active reset in progress to complete */
4509 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4512 } while (ioc->shost_recovery == 1);
4513 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
4515 return ioc->ioc_reset_in_progress_status;
4518 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4519 ioc->shost_recovery = 1;
4520 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4522 _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
4523 _wait_for_commands_to_complete(ioc, sleep_flag);
4524 _base_mask_interrupts(ioc);
4525 r = _base_make_ioc_ready(ioc, sleep_flag, type);
4528 _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
4530 /* If this hard reset is called while port enable is active, then
4531 * there is no reason to call make_ioc_operational
4533 if (ioc->is_driver_loading && ioc->port_enable_failed) {
4534 ioc->remove_host = 1;
4538 r = _base_make_ioc_operational(ioc, sleep_flag);
4540 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
4542 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
4543 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4545 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4546 ioc->ioc_reset_in_progress_status = r;
4547 ioc->shost_recovery = 0;
4548 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4549 mutex_unlock(&ioc->reset_in_progress_mutex);
4551 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,