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 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1161 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
1162 "supported\n", ioc->name));
1166 /* get msix vector count */
1167 /* NUMA_IO not supported for older controllers */
1168 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
1169 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
1170 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
1171 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
1172 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
1173 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
1174 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
1175 ioc->msix_vector_count = 1;
1177 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1178 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1180 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
1181 "vector_count(%d)\n", ioc->name, ioc->msix_vector_count));
1187 * _base_free_irq - free irq
1188 * @ioc: per adapter object
1190 * Freeing respective reply_queue from the list.
1193 _base_free_irq(struct MPT2SAS_ADAPTER *ioc)
1195 struct adapter_reply_queue *reply_q, *next;
1197 if (list_empty(&ioc->reply_queue_list))
1200 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1201 list_del(&reply_q->list);
1202 synchronize_irq(reply_q->vector);
1203 free_irq(reply_q->vector, reply_q);
1209 * _base_request_irq - request irq
1210 * @ioc: per adapter object
1211 * @index: msix index into vector table
1212 * @vector: irq vector
1214 * Inserting respective reply_queue into the list.
1217 _base_request_irq(struct MPT2SAS_ADAPTER *ioc, u8 index, u32 vector)
1219 struct adapter_reply_queue *reply_q;
1222 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1224 printk(MPT2SAS_ERR_FMT "unable to allocate memory %d!\n",
1225 ioc->name, (int)sizeof(struct adapter_reply_queue));
1229 reply_q->msix_index = index;
1230 reply_q->vector = vector;
1231 atomic_set(&reply_q->busy, 0);
1232 if (ioc->msix_enable)
1233 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
1234 MPT2SAS_DRIVER_NAME, ioc->id, index);
1236 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
1237 MPT2SAS_DRIVER_NAME, ioc->id);
1238 r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1241 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1242 reply_q->name, vector);
1247 INIT_LIST_HEAD(&reply_q->list);
1248 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1253 * _base_assign_reply_queues - assigning msix index for each cpu
1254 * @ioc: per adapter object
1256 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1258 * It would nice if we could call irq_set_affinity, however it is not
1259 * an exported symbol
1262 _base_assign_reply_queues(struct MPT2SAS_ADAPTER *ioc)
1264 struct adapter_reply_queue *reply_q;
1266 int cpu_grouping, loop, grouping, grouping_mod;
1268 if (!_base_is_controller_msix_enabled(ioc))
1271 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1272 /* when there are more cpus than available msix vectors,
1273 * then group cpus togeather on same irq
1275 if (ioc->cpu_count > ioc->msix_vector_count) {
1276 grouping = ioc->cpu_count / ioc->msix_vector_count;
1277 grouping_mod = ioc->cpu_count % ioc->msix_vector_count;
1278 if (grouping < 2 || (grouping == 2 && !grouping_mod))
1280 else if (grouping < 4 || (grouping == 4 && !grouping_mod))
1282 else if (grouping < 8 || (grouping == 8 && !grouping_mod))
1290 reply_q = list_entry(ioc->reply_queue_list.next,
1291 struct adapter_reply_queue, list);
1292 for_each_online_cpu(cpu_id) {
1293 if (!cpu_grouping) {
1294 ioc->cpu_msix_table[cpu_id] = reply_q->msix_index;
1295 reply_q = list_entry(reply_q->list.next,
1296 struct adapter_reply_queue, list);
1298 if (loop < cpu_grouping) {
1299 ioc->cpu_msix_table[cpu_id] =
1300 reply_q->msix_index;
1303 reply_q = list_entry(reply_q->list.next,
1304 struct adapter_reply_queue, list);
1305 ioc->cpu_msix_table[cpu_id] =
1306 reply_q->msix_index;
1314 * _base_disable_msix - disables msix
1315 * @ioc: per adapter object
1319 _base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
1321 if (ioc->msix_enable) {
1322 pci_disable_msix(ioc->pdev);
1323 ioc->msix_enable = 0;
1328 * _base_enable_msix - enables msix, failback to io_apic
1329 * @ioc: per adapter object
1333 _base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1335 struct msix_entry *entries, *a;
1340 INIT_LIST_HEAD(&ioc->reply_queue_list);
1342 if (msix_disable == -1 || msix_disable == 0)
1348 if (_base_check_enable_msix(ioc) != 0)
1351 ioc->reply_queue_count = min_t(u8, ioc->cpu_count,
1352 ioc->msix_vector_count);
1354 entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1357 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "kcalloc "
1358 "failed @ at %s:%d/%s() !!!\n", ioc->name, __FILE__,
1359 __LINE__, __func__));
1363 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
1366 r = pci_enable_msix(ioc->pdev, entries, ioc->reply_queue_count);
1368 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
1369 "failed (r=%d) !!!\n", ioc->name, r));
1374 ioc->msix_enable = 1;
1375 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
1376 r = _base_request_irq(ioc, i, a->vector);
1378 _base_free_irq(ioc);
1379 _base_disable_msix(ioc);
1388 /* failback to io_apic interrupt routing */
1391 r = _base_request_irq(ioc, 0, ioc->pdev->irq);
1397 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1398 * @ioc: per adapter object
1400 * Returns 0 for success, non-zero for failure.
1403 mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1405 struct pci_dev *pdev = ioc->pdev;
1411 struct adapter_reply_queue *reply_q;
1413 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
1414 ioc->name, __func__));
1416 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1417 if (pci_enable_device_mem(pdev)) {
1418 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1419 "failed\n", ioc->name);
1424 if (pci_request_selected_regions(pdev, ioc->bars,
1425 MPT2SAS_DRIVER_NAME)) {
1426 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1427 "failed\n", ioc->name);
1432 /* AER (Advanced Error Reporting) hooks */
1433 pci_enable_pcie_error_reporting(pdev);
1435 pci_set_master(pdev);
1437 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1438 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1439 ioc->name, pci_name(pdev));
1444 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
1445 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
1448 pio_chip = (u64)pci_resource_start(pdev, i);
1449 pio_sz = pci_resource_len(pdev, i);
1453 /* verify memory resource is valid before using */
1454 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1455 ioc->chip_phys = pci_resource_start(pdev, i);
1456 chip_phys = (u64)ioc->chip_phys;
1457 memap_sz = pci_resource_len(pdev, i);
1458 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1459 if (ioc->chip == NULL) {
1460 printk(MPT2SAS_ERR_FMT "unable to map "
1461 "adapter memory!\n", ioc->name);
1469 _base_mask_interrupts(ioc);
1470 r = _base_enable_msix(ioc);
1474 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
1475 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1476 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1477 "IO-APIC enabled"), reply_q->vector);
1479 printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1480 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1481 printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
1482 ioc->name, (unsigned long long)pio_chip, pio_sz);
1484 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1485 pci_save_state(pdev);
1493 pci_release_selected_regions(ioc->pdev, ioc->bars);
1494 pci_disable_pcie_error_reporting(pdev);
1495 pci_disable_device(pdev);
1500 * mpt2sas_base_get_msg_frame - obtain request mf pointer
1501 * @ioc: per adapter object
1502 * @smid: system request message index(smid zero is invalid)
1504 * Returns virt pointer to message frame.
1507 mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1509 return (void *)(ioc->request + (smid * ioc->request_sz));
1513 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1514 * @ioc: per adapter object
1515 * @smid: system request message index
1517 * Returns virt pointer to sense buffer.
1520 mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1522 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1526 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1527 * @ioc: per adapter object
1528 * @smid: system request message index
1530 * Returns phys pointer to the low 32bit address of the sense buffer.
1533 mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1535 return cpu_to_le32(ioc->sense_dma +
1536 ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1540 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1541 * @ioc: per adapter object
1542 * @phys_addr: lower 32 physical addr of the reply
1544 * Converts 32bit lower physical addr into a virt address.
1547 mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1551 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1555 * mpt2sas_base_get_smid - obtain a free smid from internal queue
1556 * @ioc: per adapter object
1557 * @cb_idx: callback index
1559 * Returns smid (zero is invalid)
1562 mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1564 unsigned long flags;
1565 struct request_tracker *request;
1568 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1569 if (list_empty(&ioc->internal_free_list)) {
1570 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1571 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1572 ioc->name, __func__);
1576 request = list_entry(ioc->internal_free_list.next,
1577 struct request_tracker, tracker_list);
1578 request->cb_idx = cb_idx;
1579 smid = request->smid;
1580 list_del(&request->tracker_list);
1581 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1586 * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1587 * @ioc: per adapter object
1588 * @cb_idx: callback index
1589 * @scmd: pointer to scsi command object
1591 * Returns smid (zero is invalid)
1594 mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
1595 struct scsi_cmnd *scmd)
1597 unsigned long flags;
1598 struct scsiio_tracker *request;
1601 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1602 if (list_empty(&ioc->free_list)) {
1603 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1604 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1605 ioc->name, __func__);
1609 request = list_entry(ioc->free_list.next,
1610 struct scsiio_tracker, tracker_list);
1611 request->scmd = scmd;
1612 request->cb_idx = cb_idx;
1613 smid = request->smid;
1614 list_del(&request->tracker_list);
1615 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1620 * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1621 * @ioc: per adapter object
1622 * @cb_idx: callback index
1624 * Returns smid (zero is invalid)
1627 mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1629 unsigned long flags;
1630 struct request_tracker *request;
1633 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1634 if (list_empty(&ioc->hpr_free_list)) {
1635 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1639 request = list_entry(ioc->hpr_free_list.next,
1640 struct request_tracker, tracker_list);
1641 request->cb_idx = cb_idx;
1642 smid = request->smid;
1643 list_del(&request->tracker_list);
1644 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1650 * mpt2sas_base_free_smid - put smid back on free_list
1651 * @ioc: per adapter object
1652 * @smid: system request message index
1657 mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1659 unsigned long flags;
1661 struct chain_tracker *chain_req, *next;
1663 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1664 if (smid < ioc->hi_priority_smid) {
1667 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
1668 list_for_each_entry_safe(chain_req, next,
1669 &ioc->scsi_lookup[i].chain_list, tracker_list) {
1670 list_del_init(&chain_req->tracker_list);
1671 list_add_tail(&chain_req->tracker_list,
1672 &ioc->free_chain_list);
1675 ioc->scsi_lookup[i].cb_idx = 0xFF;
1676 ioc->scsi_lookup[i].scmd = NULL;
1677 ioc->scsi_lookup[i].direct_io = 0;
1678 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
1680 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1683 * See _wait_for_commands_to_complete() call with regards
1686 if (ioc->shost_recovery && ioc->pending_io_count) {
1687 if (ioc->pending_io_count == 1)
1688 wake_up(&ioc->reset_wq);
1689 ioc->pending_io_count--;
1692 } else if (smid < ioc->internal_smid) {
1694 i = smid - ioc->hi_priority_smid;
1695 ioc->hpr_lookup[i].cb_idx = 0xFF;
1696 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
1697 &ioc->hpr_free_list);
1698 } else if (smid <= ioc->hba_queue_depth) {
1699 /* internal queue */
1700 i = smid - ioc->internal_smid;
1701 ioc->internal_lookup[i].cb_idx = 0xFF;
1702 list_add_tail(&ioc->internal_lookup[i].tracker_list,
1703 &ioc->internal_free_list);
1705 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1709 * _base_writeq - 64 bit write to MMIO
1710 * @ioc: per adapter object
1712 * @addr: address in MMIO space
1713 * @writeq_lock: spin lock
1715 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1716 * care of 32 bit environment where its not quarenteed to send the entire word
1720 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1721 spinlock_t *writeq_lock)
1723 unsigned long flags;
1724 __u64 data_out = cpu_to_le64(b);
1726 spin_lock_irqsave(writeq_lock, flags);
1727 writel((u32)(data_out), addr);
1728 writel((u32)(data_out >> 32), (addr + 4));
1729 spin_unlock_irqrestore(writeq_lock, flags);
1732 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1733 spinlock_t *writeq_lock)
1735 writeq(cpu_to_le64(b), addr);
1740 _base_get_msix_index(struct MPT2SAS_ADAPTER *ioc)
1742 return ioc->cpu_msix_table[smp_processor_id()];
1746 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1747 * @ioc: per adapter object
1748 * @smid: system request message index
1749 * @handle: device handle
1754 mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
1756 Mpi2RequestDescriptorUnion_t descriptor;
1757 u64 *request = (u64 *)&descriptor;
1760 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1761 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
1762 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1763 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1764 descriptor.SCSIIO.LMID = 0;
1765 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1766 &ioc->scsi_lookup_lock);
1771 * mpt2sas_base_put_smid_hi_priority - send Task Management request to firmware
1772 * @ioc: per adapter object
1773 * @smid: system request message index
1778 mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1780 Mpi2RequestDescriptorUnion_t descriptor;
1781 u64 *request = (u64 *)&descriptor;
1783 descriptor.HighPriority.RequestFlags =
1784 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1785 descriptor.HighPriority.MSIxIndex = 0;
1786 descriptor.HighPriority.SMID = cpu_to_le16(smid);
1787 descriptor.HighPriority.LMID = 0;
1788 descriptor.HighPriority.Reserved1 = 0;
1789 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1790 &ioc->scsi_lookup_lock);
1794 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1795 * @ioc: per adapter object
1796 * @smid: system request message index
1801 mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1803 Mpi2RequestDescriptorUnion_t descriptor;
1804 u64 *request = (u64 *)&descriptor;
1806 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1807 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
1808 descriptor.Default.SMID = cpu_to_le16(smid);
1809 descriptor.Default.LMID = 0;
1810 descriptor.Default.DescriptorTypeDependent = 0;
1811 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1812 &ioc->scsi_lookup_lock);
1816 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1817 * @ioc: per adapter object
1818 * @smid: system request message index
1819 * @io_index: value used to track the IO
1824 mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
1827 Mpi2RequestDescriptorUnion_t descriptor;
1828 u64 *request = (u64 *)&descriptor;
1830 descriptor.SCSITarget.RequestFlags =
1831 MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
1832 descriptor.SCSITarget.MSIxIndex = _base_get_msix_index(ioc);
1833 descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1834 descriptor.SCSITarget.LMID = 0;
1835 descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1836 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1837 &ioc->scsi_lookup_lock);
1841 * _base_display_dell_branding - Disply branding string
1842 * @ioc: per adapter object
1847 _base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1849 char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1851 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1854 memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1855 switch (ioc->pdev->subsystem_device) {
1856 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1857 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1858 MPT2SAS_DELL_BRANDING_SIZE - 1);
1860 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1861 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1862 MPT2SAS_DELL_BRANDING_SIZE - 1);
1864 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1865 strncpy(dell_branding,
1866 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1867 MPT2SAS_DELL_BRANDING_SIZE - 1);
1869 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1870 strncpy(dell_branding,
1871 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
1872 MPT2SAS_DELL_BRANDING_SIZE - 1);
1874 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
1875 strncpy(dell_branding,
1876 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
1877 MPT2SAS_DELL_BRANDING_SIZE - 1);
1879 case MPT2SAS_DELL_PERC_H200_SSDID:
1880 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
1881 MPT2SAS_DELL_BRANDING_SIZE - 1);
1883 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
1884 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
1885 MPT2SAS_DELL_BRANDING_SIZE - 1);
1888 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
1892 printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
1893 " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
1894 ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
1895 ioc->pdev->subsystem_device);
1899 * _base_display_intel_branding - Display branding string
1900 * @ioc: per adapter object
1905 _base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
1907 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1910 switch (ioc->pdev->device) {
1911 case MPI2_MFGPAGE_DEVID_SAS2008:
1912 switch (ioc->pdev->subsystem_device) {
1913 case MPT2SAS_INTEL_RMS2LL080_SSDID:
1914 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1915 MPT2SAS_INTEL_RMS2LL080_BRANDING);
1917 case MPT2SAS_INTEL_RMS2LL040_SSDID:
1918 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1919 MPT2SAS_INTEL_RMS2LL040_BRANDING);
1924 case MPI2_MFGPAGE_DEVID_SAS2308_2:
1925 switch (ioc->pdev->subsystem_device) {
1926 case MPT2SAS_INTEL_RS25GB008_SSDID:
1927 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1928 MPT2SAS_INTEL_RS25GB008_BRANDING);
1939 * _base_display_hp_branding - Display branding string
1940 * @ioc: per adapter object
1945 _base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
1947 if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
1950 switch (ioc->pdev->device) {
1951 case MPI2_MFGPAGE_DEVID_SAS2004:
1952 switch (ioc->pdev->subsystem_device) {
1953 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
1954 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1955 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
1960 case MPI2_MFGPAGE_DEVID_SAS2308_2:
1961 switch (ioc->pdev->subsystem_device) {
1962 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
1963 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1964 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
1966 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
1967 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1968 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
1970 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
1971 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1972 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
1974 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
1975 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1976 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
1987 * _base_display_ioc_capabilities - Disply IOC's capabilities.
1988 * @ioc: per adapter object
1993 _base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
1998 u32 iounit_pg1_flags;
2001 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2002 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
2003 strncpy(desc, ioc->manu_pg0.ChipName, 16);
2004 printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
2005 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2007 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2008 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2009 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2010 ioc->facts.FWVersion.Word & 0x000000FF,
2012 (bios_version & 0xFF000000) >> 24,
2013 (bios_version & 0x00FF0000) >> 16,
2014 (bios_version & 0x0000FF00) >> 8,
2015 bios_version & 0x000000FF);
2017 _base_display_dell_branding(ioc);
2018 _base_display_intel_branding(ioc);
2019 _base_display_hp_branding(ioc);
2021 printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
2023 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2024 printk("Initiator");
2028 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2029 printk("%sTarget", i ? "," : "");
2035 printk("Capabilities=(");
2037 if (!ioc->hide_ir_msg) {
2038 if (ioc->facts.IOCCapabilities &
2039 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2045 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2046 printk("%sTLR", i ? "," : "");
2050 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2051 printk("%sMulticast", i ? "," : "");
2055 if (ioc->facts.IOCCapabilities &
2056 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2057 printk("%sBIDI Target", i ? "," : "");
2061 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2062 printk("%sEEDP", i ? "," : "");
2066 if (ioc->facts.IOCCapabilities &
2067 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2068 printk("%sSnapshot Buffer", i ? "," : "");
2072 if (ioc->facts.IOCCapabilities &
2073 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2074 printk("%sDiag Trace Buffer", i ? "," : "");
2078 if (ioc->facts.IOCCapabilities &
2079 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2080 printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
2084 if (ioc->facts.IOCCapabilities &
2085 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2086 printk("%sTask Set Full", i ? "," : "");
2090 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2091 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2092 printk("%sNCQ", i ? "," : "");
2100 * _base_update_missing_delay - change the missing delay timers
2101 * @ioc: per adapter object
2102 * @device_missing_delay: amount of time till device is reported missing
2103 * @io_missing_delay: interval IO is returned when there is a missing device
2107 * Passed on the command line, this function will modify the device missing
2108 * delay, as well as the io missing delay. This should be called at driver
2112 _base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
2113 u16 device_missing_delay, u8 io_missing_delay)
2115 u16 dmd, dmd_new, dmd_orignal;
2116 u8 io_missing_delay_original;
2118 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2119 Mpi2ConfigReply_t mpi_reply;
2123 mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
2127 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2128 sizeof(Mpi2SasIOUnit1PhyData_t));
2129 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2130 if (!sas_iounit_pg1) {
2131 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2132 ioc->name, __FILE__, __LINE__, __func__);
2135 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2136 sas_iounit_pg1, sz))) {
2137 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2138 ioc->name, __FILE__, __LINE__, __func__);
2141 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2142 MPI2_IOCSTATUS_MASK;
2143 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2144 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2145 ioc->name, __FILE__, __LINE__, __func__);
2149 /* device missing delay */
2150 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2151 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2152 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2154 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2156 if (device_missing_delay > 0x7F) {
2157 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2158 device_missing_delay;
2160 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2162 dmd = device_missing_delay;
2163 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2165 /* io missing delay */
2166 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2167 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2169 if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2171 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2173 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2176 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2177 printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
2178 "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
2179 printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
2180 "new(%d)\n", ioc->name, io_missing_delay_original,
2182 ioc->device_missing_delay = dmd_new;
2183 ioc->io_missing_delay = io_missing_delay;
2187 kfree(sas_iounit_pg1);
2191 * _base_static_config_pages - static start of day config pages
2192 * @ioc: per adapter object
2197 _base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
2199 Mpi2ConfigReply_t mpi_reply;
2200 u32 iounit_pg1_flags;
2202 mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
2203 if (ioc->ir_firmware)
2204 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2206 mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
2207 mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
2208 mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
2209 mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
2210 mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2211 _base_display_ioc_capabilities(ioc);
2214 * Enable task_set_full handling in iounit_pg1 when the
2215 * facts capabilities indicate that its supported.
2217 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2218 if ((ioc->facts.IOCCapabilities &
2219 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
2221 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2224 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2225 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
2226 mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2231 * _base_release_memory_pools - release memory
2232 * @ioc: per adapter object
2234 * Free memory allocated from _base_allocate_memory_pools.
2239 _base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
2243 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2247 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2248 ioc->request, ioc->request_dma);
2249 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
2250 ": free\n", ioc->name, ioc->request));
2251 ioc->request = NULL;
2255 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2256 if (ioc->sense_dma_pool)
2257 pci_pool_destroy(ioc->sense_dma_pool);
2258 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
2259 ": free\n", ioc->name, ioc->sense));
2264 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2265 if (ioc->reply_dma_pool)
2266 pci_pool_destroy(ioc->reply_dma_pool);
2267 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
2268 ": free\n", ioc->name, ioc->reply));
2272 if (ioc->reply_free) {
2273 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2274 ioc->reply_free_dma);
2275 if (ioc->reply_free_dma_pool)
2276 pci_pool_destroy(ioc->reply_free_dma_pool);
2277 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
2278 "(0x%p): free\n", ioc->name, ioc->reply_free));
2279 ioc->reply_free = NULL;
2282 if (ioc->reply_post_free) {
2283 pci_pool_free(ioc->reply_post_free_dma_pool,
2284 ioc->reply_post_free, ioc->reply_post_free_dma);
2285 if (ioc->reply_post_free_dma_pool)
2286 pci_pool_destroy(ioc->reply_post_free_dma_pool);
2287 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2288 "reply_post_free_pool(0x%p): free\n", ioc->name,
2289 ioc->reply_post_free));
2290 ioc->reply_post_free = NULL;
2293 if (ioc->config_page) {
2294 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2295 "config_page(0x%p): free\n", ioc->name,
2297 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2298 ioc->config_page, ioc->config_page_dma);
2301 if (ioc->scsi_lookup) {
2302 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2303 ioc->scsi_lookup = NULL;
2305 kfree(ioc->hpr_lookup);
2306 kfree(ioc->internal_lookup);
2307 if (ioc->chain_lookup) {
2308 for (i = 0; i < ioc->chain_depth; i++) {
2309 if (ioc->chain_lookup[i].chain_buffer)
2310 pci_pool_free(ioc->chain_dma_pool,
2311 ioc->chain_lookup[i].chain_buffer,
2312 ioc->chain_lookup[i].chain_buffer_dma);
2314 if (ioc->chain_dma_pool)
2315 pci_pool_destroy(ioc->chain_dma_pool);
2316 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2317 ioc->chain_lookup = NULL;
2323 * _base_allocate_memory_pools - allocate start of day memory pools
2324 * @ioc: per adapter object
2325 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2327 * Returns 0 success, anything else error
2330 _base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2332 struct mpt2sas_facts *facts;
2333 u16 max_sge_elements;
2334 u16 chains_needed_per_io;
2335 u32 sz, total_sz, reply_post_free_sz;
2337 u16 max_request_credit;
2340 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2344 facts = &ioc->facts;
2346 /* command line tunables for max sgl entries */
2347 if (max_sgl_entries != -1) {
2348 ioc->shost->sg_tablesize = (max_sgl_entries <
2349 MPT2SAS_SG_DEPTH) ? max_sgl_entries :
2352 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
2355 /* command line tunables for max controller queue depth */
2356 if (max_queue_depth != -1)
2357 max_request_credit = (max_queue_depth < facts->RequestCredit)
2358 ? max_queue_depth : facts->RequestCredit;
2360 max_request_credit = min_t(u16, facts->RequestCredit,
2361 MAX_HBA_QUEUE_DEPTH);
2363 ioc->hba_queue_depth = max_request_credit;
2364 ioc->hi_priority_depth = facts->HighPriorityCredit;
2365 ioc->internal_depth = ioc->hi_priority_depth + 5;
2367 /* request frame size */
2368 ioc->request_sz = facts->IOCRequestFrameSize * 4;
2370 /* reply frame size */
2371 ioc->reply_sz = facts->ReplyFrameSize * 4;
2375 /* calculate number of sg elements left over in the 1st frame */
2376 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2377 sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
2378 ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
2380 /* now do the same for a chain buffer */
2381 max_sge_elements = ioc->request_sz - ioc->sge_size;
2382 ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
2384 ioc->chain_offset_value_for_main_message =
2385 ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
2386 (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
2389 * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2391 chains_needed_per_io = ((ioc->shost->sg_tablesize -
2392 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2394 if (chains_needed_per_io > facts->MaxChainDepth) {
2395 chains_needed_per_io = facts->MaxChainDepth;
2396 ioc->shost->sg_tablesize = min_t(u16,
2397 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2398 * chains_needed_per_io), ioc->shost->sg_tablesize);
2400 ioc->chains_needed_per_io = chains_needed_per_io;
2402 /* reply free queue sizing - taking into account for 64 FW events */
2403 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2405 /* align the reply post queue on the next 16 count boundary */
2406 if (!ioc->reply_free_queue_depth % 16)
2407 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth + 16;
2409 ioc->reply_post_queue_depth = ioc->reply_free_queue_depth +
2410 32 - (ioc->reply_free_queue_depth % 16);
2411 if (ioc->reply_post_queue_depth >
2412 facts->MaxReplyDescriptorPostQueueDepth) {
2413 ioc->reply_post_queue_depth = min_t(u16,
2414 (facts->MaxReplyDescriptorPostQueueDepth -
2415 (facts->MaxReplyDescriptorPostQueueDepth % 16)),
2416 (ioc->hba_queue_depth - (ioc->hba_queue_depth % 16)));
2417 ioc->reply_free_queue_depth = ioc->reply_post_queue_depth - 16;
2418 ioc->hba_queue_depth = ioc->reply_free_queue_depth - 64;
2422 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
2423 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2424 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2425 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2426 ioc->chains_needed_per_io));
2428 ioc->scsiio_depth = ioc->hba_queue_depth -
2429 ioc->hi_priority_depth - ioc->internal_depth;
2431 /* set the scsi host can_queue depth
2432 * with some internal commands that could be outstanding
2434 ioc->shost->can_queue = ioc->scsiio_depth - (2);
2435 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
2436 "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
2438 /* contiguous pool for request and chains, 16 byte align, one extra "
2441 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
2442 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
2444 /* hi-priority queue */
2445 sz += (ioc->hi_priority_depth * ioc->request_sz);
2447 /* internal queue */
2448 sz += (ioc->internal_depth * ioc->request_sz);
2450 ioc->request_dma_sz = sz;
2451 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2452 if (!ioc->request) {
2453 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2454 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2455 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
2456 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2457 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
2460 ioc->hba_queue_depth = max_request_credit - retry_sz;
2461 goto retry_allocation;
2465 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2466 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2467 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
2468 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2471 /* hi-priority queue */
2472 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
2474 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
2477 /* internal queue */
2478 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2480 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2484 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2485 "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2486 ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2487 (ioc->hba_queue_depth * ioc->request_sz)/1024));
2488 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2489 ioc->name, (unsigned long long) ioc->request_dma));
2492 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
2493 ioc->scsi_lookup_pages = get_order(sz);
2494 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
2495 GFP_KERNEL, ioc->scsi_lookup_pages);
2496 if (!ioc->scsi_lookup) {
2497 printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
2498 "sz(%d)\n", ioc->name, (int)sz);
2502 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2503 "depth(%d)\n", ioc->name, ioc->request,
2504 ioc->scsiio_depth));
2506 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
2507 sz = ioc->chain_depth * sizeof(struct chain_tracker);
2508 ioc->chain_pages = get_order(sz);
2510 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
2511 GFP_KERNEL, ioc->chain_pages);
2512 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
2513 ioc->request_sz, 16, 0);
2514 if (!ioc->chain_dma_pool) {
2515 printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
2516 "failed\n", ioc->name);
2519 for (i = 0; i < ioc->chain_depth; i++) {
2520 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
2521 ioc->chain_dma_pool , GFP_KERNEL,
2522 &ioc->chain_lookup[i].chain_buffer_dma);
2523 if (!ioc->chain_lookup[i].chain_buffer) {
2524 ioc->chain_depth = i;
2527 total_sz += ioc->request_sz;
2530 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
2531 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2532 ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2533 ioc->request_sz))/1024));
2535 /* initialize hi-priority queue smid's */
2536 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2537 sizeof(struct request_tracker), GFP_KERNEL);
2538 if (!ioc->hpr_lookup) {
2539 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2543 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2544 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2545 "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2546 ioc->hi_priority_depth, ioc->hi_priority_smid));
2548 /* initialize internal queue smid's */
2549 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2550 sizeof(struct request_tracker), GFP_KERNEL);
2551 if (!ioc->internal_lookup) {
2552 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2556 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2557 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2558 "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2559 ioc->internal_depth, ioc->internal_smid));
2561 /* sense buffers, 4 byte align */
2562 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
2563 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2565 if (!ioc->sense_dma_pool) {
2566 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2570 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2573 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2577 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2578 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
2579 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
2580 SCSI_SENSE_BUFFERSIZE, sz/1024));
2581 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2582 ioc->name, (unsigned long long)ioc->sense_dma));
2585 /* reply pool, 4 byte align */
2586 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2587 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2589 if (!ioc->reply_dma_pool) {
2590 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2594 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2597 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2601 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2602 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
2603 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2604 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2605 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2606 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2607 ioc->name, (unsigned long long)ioc->reply_dma));
2610 /* reply free queue, 16 byte align */
2611 sz = ioc->reply_free_queue_depth * 4;
2612 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2613 ioc->pdev, sz, 16, 0);
2614 if (!ioc->reply_free_dma_pool) {
2615 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2616 "failed\n", ioc->name);
2619 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2620 &ioc->reply_free_dma);
2621 if (!ioc->reply_free) {
2622 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2623 "failed\n", ioc->name);
2626 memset(ioc->reply_free, 0, sz);
2627 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2628 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2629 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2630 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2631 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2634 /* reply post queue, 16 byte align */
2635 reply_post_free_sz = ioc->reply_post_queue_depth *
2636 sizeof(Mpi2DefaultReplyDescriptor_t);
2637 if (_base_is_controller_msix_enabled(ioc))
2638 sz = reply_post_free_sz * ioc->reply_queue_count;
2640 sz = reply_post_free_sz;
2641 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2642 ioc->pdev, sz, 16, 0);
2643 if (!ioc->reply_post_free_dma_pool) {
2644 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
2645 "failed\n", ioc->name);
2648 ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2649 GFP_KERNEL, &ioc->reply_post_free_dma);
2650 if (!ioc->reply_post_free) {
2651 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
2652 "failed\n", ioc->name);
2655 memset(ioc->reply_post_free, 0, sz);
2656 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
2657 "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2658 ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2660 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
2661 "(0x%llx)\n", ioc->name, (unsigned long long)
2662 ioc->reply_post_free_dma));
2665 ioc->config_page_sz = 512;
2666 ioc->config_page = pci_alloc_consistent(ioc->pdev,
2667 ioc->config_page_sz, &ioc->config_page_dma);
2668 if (!ioc->config_page) {
2669 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2670 "failed\n", ioc->name);
2673 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2674 "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2675 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2676 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2677 total_sz += ioc->config_page_sz;
2679 printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2680 ioc->name, total_sz/1024);
2681 printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2682 "Max Controller Queue Depth(%d)\n",
2683 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2684 printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2685 ioc->name, ioc->shost->sg_tablesize);
2694 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2695 * @ioc: Pointer to MPT_ADAPTER structure
2696 * @cooked: Request raw or cooked IOC state
2698 * Returns all IOC Doorbell register bits if cooked==0, else just the
2699 * Doorbell bits in MPI_IOC_STATE_MASK.
2702 mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2706 s = readl(&ioc->chip->Doorbell);
2707 sc = s & MPI2_IOC_STATE_MASK;
2708 return cooked ? sc : s;
2712 * _base_wait_on_iocstate - waiting on a particular ioc state
2713 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2714 * @timeout: timeout in second
2715 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2717 * Returns 0 for success, non-zero for failure.
2720 _base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2727 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2729 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2730 if (current_state == ioc_state)
2732 if (count && current_state == MPI2_IOC_STATE_FAULT)
2734 if (sleep_flag == CAN_SLEEP)
2741 return current_state;
2745 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2746 * a write to the doorbell)
2747 * @ioc: per adapter object
2748 * @timeout: timeout in second
2749 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2751 * Returns 0 for success, non-zero for failure.
2753 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2756 _base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2763 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2765 int_status = readl(&ioc->chip->HostInterruptStatus);
2766 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2767 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2768 "successful count(%d), timeout(%d)\n", ioc->name,
2769 __func__, count, timeout));
2772 if (sleep_flag == CAN_SLEEP)
2779 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2780 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2785 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2786 * @ioc: per adapter object
2787 * @timeout: timeout in second
2788 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2790 * Returns 0 for success, non-zero for failure.
2792 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2796 _base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2804 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2806 int_status = readl(&ioc->chip->HostInterruptStatus);
2807 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
2808 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2809 "successful count(%d), timeout(%d)\n", ioc->name,
2810 __func__, count, timeout));
2812 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2813 doorbell = readl(&ioc->chip->Doorbell);
2814 if ((doorbell & MPI2_IOC_STATE_MASK) ==
2815 MPI2_IOC_STATE_FAULT) {
2816 mpt2sas_base_fault_info(ioc , doorbell);
2819 } else if (int_status == 0xFFFFFFFF)
2822 if (sleep_flag == CAN_SLEEP)
2830 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2831 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2836 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2837 * @ioc: per adapter object
2838 * @timeout: timeout in second
2839 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2841 * Returns 0 for success, non-zero for failure.
2845 _base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2852 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2854 doorbell_reg = readl(&ioc->chip->Doorbell);
2855 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
2856 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2857 "successful count(%d), timeout(%d)\n", ioc->name,
2858 __func__, count, timeout));
2861 if (sleep_flag == CAN_SLEEP)
2868 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2869 "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2874 * _base_send_ioc_reset - send doorbell reset
2875 * @ioc: per adapter object
2876 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2877 * @timeout: timeout in second
2878 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2880 * Returns 0 for success, non-zero for failure.
2883 _base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2889 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2890 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2891 ioc->name, __func__);
2895 if (!(ioc->facts.IOCCapabilities &
2896 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2899 printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
2901 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
2902 &ioc->chip->Doorbell);
2903 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
2907 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
2908 timeout, sleep_flag);
2910 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
2911 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
2916 printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
2917 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
2922 * _base_handshake_req_reply_wait - send request thru doorbell interface
2923 * @ioc: per adapter object
2924 * @request_bytes: request length
2925 * @request: pointer having request payload
2926 * @reply_bytes: reply length
2927 * @reply: pointer to reply payload
2928 * @timeout: timeout in second
2929 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2931 * Returns 0 for success, non-zero for failure.
2934 _base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
2935 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
2937 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
2943 /* make sure doorbell is not in use */
2944 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
2945 printk(MPT2SAS_ERR_FMT "doorbell is in use "
2946 " (line=%d)\n", ioc->name, __LINE__);
2950 /* clear pending doorbell interrupts from previous state changes */
2951 if (readl(&ioc->chip->HostInterruptStatus) &
2952 MPI2_HIS_IOC2SYS_DB_STATUS)
2953 writel(0, &ioc->chip->HostInterruptStatus);
2955 /* send message to ioc */
2956 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
2957 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
2958 &ioc->chip->Doorbell);
2960 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
2961 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2962 "int failed (line=%d)\n", ioc->name, __LINE__);
2965 writel(0, &ioc->chip->HostInterruptStatus);
2967 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
2968 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2969 "ack failed (line=%d)\n", ioc->name, __LINE__);
2973 /* send message 32-bits at a time */
2974 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
2975 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
2976 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
2981 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2982 "sending request failed (line=%d)\n", ioc->name, __LINE__);
2986 /* now wait for the reply */
2987 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
2988 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2989 "int failed (line=%d)\n", ioc->name, __LINE__);
2993 /* read the first two 16-bits, it gives the total length of the reply */
2994 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2995 & MPI2_DOORBELL_DATA_MASK);
2996 writel(0, &ioc->chip->HostInterruptStatus);
2997 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2998 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2999 "int failed (line=%d)\n", ioc->name, __LINE__);
3002 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3003 & MPI2_DOORBELL_DATA_MASK);
3004 writel(0, &ioc->chip->HostInterruptStatus);
3006 for (i = 2; i < default_reply->MsgLength * 2; i++) {
3007 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3008 printk(MPT2SAS_ERR_FMT "doorbell "
3009 "handshake int failed (line=%d)\n", ioc->name,
3013 if (i >= reply_bytes/2) /* overflow case */
3014 dummy = readl(&ioc->chip->Doorbell);
3016 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3017 & MPI2_DOORBELL_DATA_MASK);
3018 writel(0, &ioc->chip->HostInterruptStatus);
3021 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3022 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3023 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
3024 " (line=%d)\n", ioc->name, __LINE__));
3026 writel(0, &ioc->chip->HostInterruptStatus);
3028 if (ioc->logging_level & MPT_DEBUG_INIT) {
3029 mfp = (__le32 *)reply;
3030 printk(KERN_INFO "\toffset:data\n");
3031 for (i = 0; i < reply_bytes/4; i++)
3032 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
3033 le32_to_cpu(mfp[i]));
3039 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
3040 * @ioc: per adapter object
3041 * @mpi_reply: the reply payload from FW
3042 * @mpi_request: the request payload sent to FW
3044 * The SAS IO Unit Control Request message allows the host to perform low-level
3045 * operations, such as resets on the PHYs of the IO Unit, also allows the host
3046 * to obtain the IOC assigned device handles for a device if it has other
3047 * identifying information about the device, in addition allows the host to
3048 * remove IOC resources associated with the device.
3050 * Returns 0 for success, non-zero for failure.
3053 mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
3054 Mpi2SasIoUnitControlReply_t *mpi_reply,
3055 Mpi2SasIoUnitControlRequest_t *mpi_request)
3059 unsigned long timeleft;
3063 u16 wait_state_count;
3065 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3068 mutex_lock(&ioc->base_cmds.mutex);
3070 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3071 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3072 ioc->name, __func__);
3077 wait_state_count = 0;
3078 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3079 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3080 if (wait_state_count++ == 10) {
3081 printk(MPT2SAS_ERR_FMT
3082 "%s: failed due to ioc not operational\n",
3083 ioc->name, __func__);
3088 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3089 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3090 "operational state(count=%d)\n", ioc->name,
3091 __func__, wait_state_count);
3094 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3096 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3097 ioc->name, __func__);
3103 ioc->base_cmds.status = MPT2_CMD_PENDING;
3104 request = mpt2sas_base_get_msg_frame(ioc, smid);
3105 ioc->base_cmds.smid = smid;
3106 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3107 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3108 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3109 ioc->ioc_link_reset_in_progress = 1;
3110 mpt2sas_base_put_smid_default(ioc, smid);
3111 init_completion(&ioc->base_cmds.done);
3112 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3113 msecs_to_jiffies(10000));
3114 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3115 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
3116 ioc->ioc_link_reset_in_progress)
3117 ioc->ioc_link_reset_in_progress = 0;
3118 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3119 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3120 ioc->name, __func__);
3121 _debug_dump_mf(mpi_request,
3122 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
3123 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3125 goto issue_host_reset;
3127 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3128 memcpy(mpi_reply, ioc->base_cmds.reply,
3129 sizeof(Mpi2SasIoUnitControlReply_t));
3131 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
3132 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3137 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3139 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3142 mutex_unlock(&ioc->base_cmds.mutex);
3148 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
3149 * @ioc: per adapter object
3150 * @mpi_reply: the reply payload from FW
3151 * @mpi_request: the request payload sent to FW
3153 * The SCSI Enclosure Processor request message causes the IOC to
3154 * communicate with SES devices to control LED status signals.
3156 * Returns 0 for success, non-zero for failure.
3159 mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
3160 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
3164 unsigned long timeleft;
3168 u16 wait_state_count;
3170 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3173 mutex_lock(&ioc->base_cmds.mutex);
3175 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3176 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3177 ioc->name, __func__);
3182 wait_state_count = 0;
3183 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3184 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3185 if (wait_state_count++ == 10) {
3186 printk(MPT2SAS_ERR_FMT
3187 "%s: failed due to ioc not operational\n",
3188 ioc->name, __func__);
3193 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3194 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3195 "operational state(count=%d)\n", ioc->name,
3196 __func__, wait_state_count);
3199 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3201 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3202 ioc->name, __func__);
3208 ioc->base_cmds.status = MPT2_CMD_PENDING;
3209 request = mpt2sas_base_get_msg_frame(ioc, smid);
3210 ioc->base_cmds.smid = smid;
3211 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
3212 mpt2sas_base_put_smid_default(ioc, smid);
3213 init_completion(&ioc->base_cmds.done);
3214 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3215 msecs_to_jiffies(10000));
3216 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3217 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3218 ioc->name, __func__);
3219 _debug_dump_mf(mpi_request,
3220 sizeof(Mpi2SepRequest_t)/4);
3221 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3223 goto issue_host_reset;
3225 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3226 memcpy(mpi_reply, ioc->base_cmds.reply,
3227 sizeof(Mpi2SepReply_t));
3229 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3230 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3235 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3237 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3240 mutex_unlock(&ioc->base_cmds.mutex);
3245 * _base_get_port_facts - obtain port facts reply and save in ioc
3246 * @ioc: per adapter object
3247 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3249 * Returns 0 for success, non-zero for failure.
3252 _base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
3254 Mpi2PortFactsRequest_t mpi_request;
3255 Mpi2PortFactsReply_t mpi_reply;
3256 struct mpt2sas_port_facts *pfacts;
3257 int mpi_reply_sz, mpi_request_sz, r;
3259 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3262 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3263 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3264 memset(&mpi_request, 0, mpi_request_sz);
3265 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3266 mpi_request.PortNumber = port;
3267 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3268 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3271 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3272 ioc->name, __func__, r);
3276 pfacts = &ioc->pfacts[port];
3277 memset(pfacts, 0, sizeof(Mpi2PortFactsReply_t));
3278 pfacts->PortNumber = mpi_reply.PortNumber;
3279 pfacts->VP_ID = mpi_reply.VP_ID;
3280 pfacts->VF_ID = mpi_reply.VF_ID;
3281 pfacts->MaxPostedCmdBuffers =
3282 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3288 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3289 * @ioc: per adapter object
3290 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3292 * Returns 0 for success, non-zero for failure.
3295 _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3297 Mpi2IOCFactsRequest_t mpi_request;
3298 Mpi2IOCFactsReply_t mpi_reply;
3299 struct mpt2sas_facts *facts;
3300 int mpi_reply_sz, mpi_request_sz, r;
3302 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3305 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3306 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3307 memset(&mpi_request, 0, mpi_request_sz);
3308 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3309 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3310 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3313 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3314 ioc->name, __func__, r);
3318 facts = &ioc->facts;
3319 memset(facts, 0, sizeof(Mpi2IOCFactsReply_t));
3320 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3321 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3322 facts->VP_ID = mpi_reply.VP_ID;
3323 facts->VF_ID = mpi_reply.VF_ID;
3324 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3325 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3326 facts->WhoInit = mpi_reply.WhoInit;
3327 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
3328 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
3329 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3330 facts->MaxReplyDescriptorPostQueueDepth =
3331 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3332 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3333 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3334 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3335 ioc->ir_firmware = 1;
3336 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3337 facts->IOCRequestFrameSize =
3338 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3339 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3340 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3341 ioc->shost->max_id = -1;
3342 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3343 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3344 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3345 facts->HighPriorityCredit =
3346 le16_to_cpu(mpi_reply.HighPriorityCredit);
3347 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3348 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3350 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
3351 "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
3352 facts->MaxChainDepth));
3353 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
3354 "reply frame size(%d)\n", ioc->name,
3355 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3360 * _base_send_ioc_init - send ioc_init to firmware
3361 * @ioc: per adapter object
3362 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3364 * Returns 0 for success, non-zero for failure.
3367 _base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3369 Mpi2IOCInitRequest_t mpi_request;
3370 Mpi2IOCInitReply_t mpi_reply;
3372 struct timeval current_time;
3375 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3378 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3379 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3380 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
3381 mpi_request.VF_ID = 0; /* TODO */
3382 mpi_request.VP_ID = 0;
3383 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3384 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3386 if (_base_is_controller_msix_enabled(ioc))
3387 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
3388 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3389 mpi_request.ReplyDescriptorPostQueueDepth =
3390 cpu_to_le16(ioc->reply_post_queue_depth);
3391 mpi_request.ReplyFreeQueueDepth =
3392 cpu_to_le16(ioc->reply_free_queue_depth);
3394 mpi_request.SenseBufferAddressHigh =
3395 cpu_to_le32((u64)ioc->sense_dma >> 32);
3396 mpi_request.SystemReplyAddressHigh =
3397 cpu_to_le32((u64)ioc->reply_dma >> 32);
3398 mpi_request.SystemRequestFrameBaseAddress =
3399 cpu_to_le64((u64)ioc->request_dma);
3400 mpi_request.ReplyFreeQueueAddress =
3401 cpu_to_le64((u64)ioc->reply_free_dma);
3402 mpi_request.ReplyDescriptorPostQueueAddress =
3403 cpu_to_le64((u64)ioc->reply_post_free_dma);
3406 /* This time stamp specifies number of milliseconds
3407 * since epoch ~ midnight January 1, 1970.
3409 do_gettimeofday(¤t_time);
3410 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3411 (current_time.tv_usec / 1000));
3413 if (ioc->logging_level & MPT_DEBUG_INIT) {
3417 mfp = (__le32 *)&mpi_request;
3418 printk(KERN_INFO "\toffset:data\n");
3419 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
3420 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
3421 le32_to_cpu(mfp[i]));
3424 r = _base_handshake_req_reply_wait(ioc,
3425 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3426 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3430 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3431 ioc->name, __func__, r);
3435 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3436 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
3437 mpi_reply.IOCLogInfo) {
3438 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
3446 * mpt2sas_port_enable_done - command completion routine for port enable
3447 * @ioc: per adapter object
3448 * @smid: system request message index
3449 * @msix_index: MSIX table index supplied by the OS
3450 * @reply: reply message frame(lower 32bit addr)
3452 * Return 1 meaning mf should be freed from _base_interrupt
3453 * 0 means the mf is freed from this function.
3456 mpt2sas_port_enable_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3459 MPI2DefaultReply_t *mpi_reply;
3462 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3463 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
3466 if (ioc->port_enable_cmds.status == MPT2_CMD_NOT_USED)
3469 ioc->port_enable_cmds.status |= MPT2_CMD_COMPLETE;
3471 ioc->port_enable_cmds.status |= MPT2_CMD_REPLY_VALID;
3472 memcpy(ioc->port_enable_cmds.reply, mpi_reply,
3473 mpi_reply->MsgLength*4);
3475 ioc->port_enable_cmds.status &= ~MPT2_CMD_PENDING;
3477 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3479 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3480 ioc->port_enable_failed = 1;
3482 if (ioc->is_driver_loading) {
3483 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
3484 mpt2sas_port_enable_complete(ioc);
3487 ioc->start_scan_failed = ioc_status;
3488 ioc->start_scan = 0;
3492 complete(&ioc->port_enable_cmds.done);
3498 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3499 * @ioc: per adapter object
3500 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3502 * Returns 0 for success, non-zero for failure.
3505 _base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3507 Mpi2PortEnableRequest_t *mpi_request;
3508 Mpi2PortEnableReply_t *mpi_reply;
3509 unsigned long timeleft;
3514 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3516 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3517 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3518 ioc->name, __func__);
3522 smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3524 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3525 ioc->name, __func__);
3529 ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3530 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3531 ioc->port_enable_cmds.smid = smid;
3532 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3533 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3535 init_completion(&ioc->port_enable_cmds.done);
3536 mpt2sas_base_put_smid_default(ioc, smid);
3537 timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
3539 if (!(ioc->port_enable_cmds.status & MPT2_CMD_COMPLETE)) {
3540 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3541 ioc->name, __func__);
3542 _debug_dump_mf(mpi_request,
3543 sizeof(Mpi2PortEnableRequest_t)/4);
3544 if (ioc->port_enable_cmds.status & MPT2_CMD_RESET)
3550 mpi_reply = ioc->port_enable_cmds.reply;
3552 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3553 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3554 printk(MPT2SAS_ERR_FMT "%s: failed with (ioc_status=0x%08x)\n",
3555 ioc->name, __func__, ioc_status);
3560 ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
3561 printk(MPT2SAS_INFO_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
3562 "SUCCESS" : "FAILED"));
3567 * mpt2sas_port_enable - initiate firmware discovery (don't wait for reply)
3568 * @ioc: per adapter object
3570 * Returns 0 for success, non-zero for failure.
3573 mpt2sas_port_enable(struct MPT2SAS_ADAPTER *ioc)
3575 Mpi2PortEnableRequest_t *mpi_request;
3578 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3580 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3581 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3582 ioc->name, __func__);
3586 smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3588 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3589 ioc->name, __func__);
3593 ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3594 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3595 ioc->port_enable_cmds.smid = smid;
3596 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3597 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3599 mpt2sas_base_put_smid_default(ioc, smid);
3604 * _base_determine_wait_on_discovery - desposition
3605 * @ioc: per adapter object
3607 * Decide whether to wait on discovery to complete. Used to either
3608 * locate boot device, or report volumes ahead of physical devices.
3610 * Returns 1 for wait, 0 for don't wait
3613 _base_determine_wait_on_discovery(struct MPT2SAS_ADAPTER *ioc)
3615 /* We wait for discovery to complete if IR firmware is loaded.
3616 * The sas topology events arrive before PD events, so we need time to
3617 * turn on the bit in ioc->pd_handles to indicate PD
3618 * Also, it maybe required to report Volumes ahead of physical
3619 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
3621 if (ioc->ir_firmware)
3624 /* if no Bios, then we don't need to wait */
3625 if (!ioc->bios_pg3.BiosVersion)
3628 /* Bios is present, then we drop down here.
3630 * If there any entries in the Bios Page 2, then we wait
3631 * for discovery to complete.
3634 /* Current Boot Device */
3635 if ((ioc->bios_pg2.CurrentBootDeviceForm &
3636 MPI2_BIOSPAGE2_FORM_MASK) ==
3637 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3638 /* Request Boot Device */
3639 (ioc->bios_pg2.ReqBootDeviceForm &
3640 MPI2_BIOSPAGE2_FORM_MASK) ==
3641 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3642 /* Alternate Request Boot Device */
3643 (ioc->bios_pg2.ReqAltBootDeviceForm &
3644 MPI2_BIOSPAGE2_FORM_MASK) ==
3645 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
3653 * _base_unmask_events - turn on notification for this event
3654 * @ioc: per adapter object
3655 * @event: firmware event
3657 * The mask is stored in ioc->event_masks.
3660 _base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3667 desired_event = (1 << (event % 32));
3670 ioc->event_masks[0] &= ~desired_event;
3671 else if (event < 64)
3672 ioc->event_masks[1] &= ~desired_event;
3673 else if (event < 96)
3674 ioc->event_masks[2] &= ~desired_event;
3675 else if (event < 128)
3676 ioc->event_masks[3] &= ~desired_event;
3680 * _base_event_notification - send event notification
3681 * @ioc: per adapter object
3682 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3684 * Returns 0 for success, non-zero for failure.
3687 _base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3689 Mpi2EventNotificationRequest_t *mpi_request;
3690 unsigned long timeleft;
3695 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3698 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3699 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3700 ioc->name, __func__);
3704 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3706 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3707 ioc->name, __func__);
3710 ioc->base_cmds.status = MPT2_CMD_PENDING;
3711 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3712 ioc->base_cmds.smid = smid;
3713 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
3714 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3715 mpi_request->VF_ID = 0; /* TODO */
3716 mpi_request->VP_ID = 0;
3717 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3718 mpi_request->EventMasks[i] =
3719 cpu_to_le32(ioc->event_masks[i]);
3720 mpt2sas_base_put_smid_default(ioc, smid);
3721 init_completion(&ioc->base_cmds.done);
3722 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
3723 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3724 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3725 ioc->name, __func__);
3726 _debug_dump_mf(mpi_request,
3727 sizeof(Mpi2EventNotificationRequest_t)/4);
3728 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3733 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
3734 ioc->name, __func__));
3735 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3740 * mpt2sas_base_validate_event_type - validating event types
3741 * @ioc: per adapter object
3742 * @event: firmware event
3744 * This will turn on firmware event notification when application
3745 * ask for that event. We don't mask events that are already enabled.
3748 mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
3751 u32 event_mask, desired_event;
3752 u8 send_update_to_fw;
3754 for (i = 0, send_update_to_fw = 0; i <
3755 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
3756 event_mask = ~event_type[i];
3758 for (j = 0; j < 32; j++) {
3759 if (!(event_mask & desired_event) &&
3760 (ioc->event_masks[i] & desired_event)) {
3761 ioc->event_masks[i] &= ~desired_event;
3762 send_update_to_fw = 1;
3764 desired_event = (desired_event << 1);
3768 if (!send_update_to_fw)
3771 mutex_lock(&ioc->base_cmds.mutex);
3772 _base_event_notification(ioc, CAN_SLEEP);
3773 mutex_unlock(&ioc->base_cmds.mutex);
3777 * _base_diag_reset - the "big hammer" start of day reset
3778 * @ioc: per adapter object
3779 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3781 * Returns 0 for success, non-zero for failure.
3784 _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3786 u32 host_diagnostic;
3791 printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
3792 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
3797 /* Write magic sequence to WriteSequence register
3798 * Loop until in diagnostic mode
3800 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
3801 "sequence\n", ioc->name));
3802 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3803 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
3804 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
3805 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
3806 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
3807 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
3808 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
3811 if (sleep_flag == CAN_SLEEP)
3819 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3820 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
3821 "sequence: count(%d), host_diagnostic(0x%08x)\n",
3822 ioc->name, count, host_diagnostic));
3824 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
3826 hcb_size = readl(&ioc->chip->HCBSize);
3828 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
3830 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
3831 &ioc->chip->HostDiagnostic);
3833 /* don't access any registers for 50 milliseconds */
3836 /* 300 second max wait */
3837 for (count = 0; count < 3000000 ; count++) {
3839 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3841 if (host_diagnostic == 0xFFFFFFFF)
3843 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3847 if (sleep_flag == CAN_SLEEP)
3853 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3855 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
3856 "assuming the HCB Address points to good F/W\n",
3858 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3859 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3860 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3862 drsprintk(ioc, printk(MPT2SAS_INFO_FMT
3863 "re-enable the HCDW\n", ioc->name));
3864 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3865 &ioc->chip->HCBSize);
3868 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
3870 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3871 &ioc->chip->HostDiagnostic);
3873 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
3874 "diagnostic register\n", ioc->name));
3875 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3877 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
3878 "READY state\n", ioc->name));
3879 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3882 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3883 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3887 printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3891 printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3896 * _base_make_ioc_ready - put controller in READY state
3897 * @ioc: per adapter object
3898 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3899 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3901 * Returns 0 for success, non-zero for failure.
3904 _base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3905 enum reset_type type)
3910 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3913 if (ioc->pci_error_recovery)
3916 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
3917 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
3918 ioc->name, __func__, ioc_state));
3920 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
3923 if (ioc_state & MPI2_DOORBELL_USED) {
3924 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
3925 "active!\n", ioc->name));
3926 goto issue_diag_reset;
3929 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3930 mpt2sas_base_fault_info(ioc, ioc_state &
3931 MPI2_DOORBELL_DATA_MASK);
3932 goto issue_diag_reset;
3935 if (type == FORCE_BIG_HAMMER)
3936 goto issue_diag_reset;
3938 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
3939 if (!(_base_send_ioc_reset(ioc,
3940 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
3941 ioc->ioc_reset_count++;
3946 rc = _base_diag_reset(ioc, CAN_SLEEP);
3947 ioc->ioc_reset_count++;
3952 * _base_make_ioc_operational - put controller in OPERATIONAL state
3953 * @ioc: per adapter object
3954 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3956 * Returns 0 for success, non-zero for failure.
3959 _base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3962 unsigned long flags;
3965 struct _tr_list *delayed_tr, *delayed_tr_next;
3967 struct adapter_reply_queue *reply_q;
3968 long reply_post_free;
3969 u32 reply_post_free_sz;
3971 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3974 /* clean the delayed target reset list */
3975 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3976 &ioc->delayed_tr_list, list) {
3977 list_del(&delayed_tr->list);
3981 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3982 &ioc->delayed_tr_volume_list, list) {
3983 list_del(&delayed_tr->list);
3987 /* initialize the scsi lookup free list */
3988 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3989 INIT_LIST_HEAD(&ioc->free_list);
3991 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
3992 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
3993 ioc->scsi_lookup[i].cb_idx = 0xFF;
3994 ioc->scsi_lookup[i].smid = smid;
3995 ioc->scsi_lookup[i].scmd = NULL;
3996 ioc->scsi_lookup[i].direct_io = 0;
3997 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
4001 /* hi-priority queue */
4002 INIT_LIST_HEAD(&ioc->hpr_free_list);
4003 smid = ioc->hi_priority_smid;
4004 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
4005 ioc->hpr_lookup[i].cb_idx = 0xFF;
4006 ioc->hpr_lookup[i].smid = smid;
4007 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
4008 &ioc->hpr_free_list);
4011 /* internal queue */
4012 INIT_LIST_HEAD(&ioc->internal_free_list);
4013 smid = ioc->internal_smid;
4014 for (i = 0; i < ioc->internal_depth; i++, smid++) {
4015 ioc->internal_lookup[i].cb_idx = 0xFF;
4016 ioc->internal_lookup[i].smid = smid;
4017 list_add_tail(&ioc->internal_lookup[i].tracker_list,
4018 &ioc->internal_free_list);
4022 INIT_LIST_HEAD(&ioc->free_chain_list);
4023 for (i = 0; i < ioc->chain_depth; i++)
4024 list_add_tail(&ioc->chain_lookup[i].tracker_list,
4025 &ioc->free_chain_list);
4027 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4029 /* initialize Reply Free Queue */
4030 for (i = 0, reply_address = (u32)ioc->reply_dma ;
4031 i < ioc->reply_free_queue_depth ; i++, reply_address +=
4033 ioc->reply_free[i] = cpu_to_le32(reply_address);
4035 /* initialize reply queues */
4036 if (ioc->is_driver_loading)
4037 _base_assign_reply_queues(ioc);
4039 /* initialize Reply Post Free Queue */
4040 reply_post_free = (long)ioc->reply_post_free;
4041 reply_post_free_sz = ioc->reply_post_queue_depth *
4042 sizeof(Mpi2DefaultReplyDescriptor_t);
4043 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4044 reply_q->reply_post_host_index = 0;
4045 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
4047 for (i = 0; i < ioc->reply_post_queue_depth; i++)
4048 reply_q->reply_post_free[i].Words =
4049 cpu_to_le64(ULLONG_MAX);
4050 if (!_base_is_controller_msix_enabled(ioc))
4051 goto skip_init_reply_post_free_queue;
4052 reply_post_free += reply_post_free_sz;
4054 skip_init_reply_post_free_queue:
4056 r = _base_send_ioc_init(ioc, sleep_flag);
4060 /* initialize reply free host index */
4061 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
4062 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
4064 /* initialize reply post host index */
4065 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4066 writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
4067 &ioc->chip->ReplyPostHostIndex);
4068 if (!_base_is_controller_msix_enabled(ioc))
4069 goto skip_init_reply_post_host_index;
4072 skip_init_reply_post_host_index:
4074 _base_unmask_interrupts(ioc);
4076 r = _base_event_notification(ioc, sleep_flag);
4080 if (sleep_flag == CAN_SLEEP)
4081 _base_static_config_pages(ioc);
4084 if (ioc->is_driver_loading) {
4085 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
4087 hide_flag = (u8) (ioc->manu_pg10.OEMSpecificFlags0 &
4088 MFG_PAGE10_HIDE_SSDS_MASK);
4089 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
4090 ioc->mfg_pg10_hide_flag = hide_flag;
4092 ioc->wait_for_discovery_to_complete =
4093 _base_determine_wait_on_discovery(ioc);
4094 return r; /* scan_start and scan_finished support */
4096 r = _base_send_port_enable(ioc, sleep_flag);
4104 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
4105 * @ioc: per adapter object
4110 mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
4112 struct pci_dev *pdev = ioc->pdev;
4114 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4117 _base_mask_interrupts(ioc);
4118 ioc->shost_recovery = 1;
4119 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4120 ioc->shost_recovery = 0;
4121 _base_free_irq(ioc);
4122 _base_disable_msix(ioc);
4126 pci_release_selected_regions(ioc->pdev, ioc->bars);
4127 pci_disable_pcie_error_reporting(pdev);
4128 pci_disable_device(pdev);
4133 * mpt2sas_base_attach - attach controller instance
4134 * @ioc: per adapter object
4136 * Returns 0 for success, non-zero for failure.
4139 mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
4142 int cpu_id, last_cpu_id = 0;
4144 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4147 /* setup cpu_msix_table */
4148 ioc->cpu_count = num_online_cpus();
4149 for_each_online_cpu(cpu_id)
4150 last_cpu_id = cpu_id;
4151 ioc->cpu_msix_table_sz = last_cpu_id + 1;
4152 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
4153 ioc->reply_queue_count = 1;
4154 if (!ioc->cpu_msix_table) {
4155 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
4156 "cpu_msix_table failed!!!\n", ioc->name));
4158 goto out_free_resources;
4161 if (ioc->is_warpdrive) {
4162 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
4163 sizeof(resource_size_t *), GFP_KERNEL);
4164 if (!ioc->reply_post_host_index) {
4165 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation "
4166 "for cpu_msix_table failed!!!\n", ioc->name));
4168 goto out_free_resources;
4172 r = mpt2sas_base_map_resources(ioc);
4176 if (ioc->is_warpdrive) {
4177 ioc->reply_post_host_index[0] =
4178 (resource_size_t *)&ioc->chip->ReplyPostHostIndex;
4180 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
4181 ioc->reply_post_host_index[i] = (resource_size_t *)
4182 ((u8 *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
4186 pci_set_drvdata(ioc->pdev, ioc->shost);
4187 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
4189 goto out_free_resources;
4191 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4193 goto out_free_resources;
4195 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
4196 sizeof(Mpi2PortFactsReply_t), GFP_KERNEL);
4199 goto out_free_resources;
4202 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
4203 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
4205 goto out_free_resources;
4208 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
4210 goto out_free_resources;
4212 init_waitqueue_head(&ioc->reset_wq);
4214 /* allocate memory pd handle bitmask list */
4215 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
4216 if (ioc->facts.MaxDevHandle % 8)
4217 ioc->pd_handles_sz++;
4218 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
4220 if (!ioc->pd_handles) {
4222 goto out_free_resources;
4225 ioc->fwfault_debug = mpt2sas_fwfault_debug;
4227 /* base internal command bits */
4228 mutex_init(&ioc->base_cmds.mutex);
4229 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4230 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
4232 /* port_enable command bits */
4233 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4234 ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
4236 /* transport internal command bits */
4237 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4238 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
4239 mutex_init(&ioc->transport_cmds.mutex);
4241 /* scsih internal command bits */
4242 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4243 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
4244 mutex_init(&ioc->scsih_cmds.mutex);
4246 /* task management internal command bits */
4247 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4248 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4249 mutex_init(&ioc->tm_cmds.mutex);
4251 /* config page internal command bits */
4252 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4253 ioc->config_cmds.status = MPT2_CMD_NOT_USED;
4254 mutex_init(&ioc->config_cmds.mutex);
4256 /* ctl module internal command bits */
4257 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4258 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4259 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
4260 mutex_init(&ioc->ctl_cmds.mutex);
4262 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4263 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4264 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
4265 !ioc->ctl_cmds.sense) {
4267 goto out_free_resources;
4270 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4271 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4272 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
4274 goto out_free_resources;
4277 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4278 ioc->event_masks[i] = -1;
4280 /* here we enable the events we care about */
4281 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
4282 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
4283 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
4284 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4285 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
4286 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
4287 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
4288 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
4289 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
4290 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
4291 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
4293 goto out_free_resources;
4295 if (missing_delay[0] != -1 && missing_delay[1] != -1)
4296 _base_update_missing_delay(ioc, missing_delay[0],
4303 ioc->remove_host = 1;
4304 mpt2sas_base_free_resources(ioc);
4305 _base_release_memory_pools(ioc);
4306 pci_set_drvdata(ioc->pdev, NULL);
4307 kfree(ioc->cpu_msix_table);
4308 if (ioc->is_warpdrive)
4309 kfree(ioc->reply_post_host_index);
4310 kfree(ioc->pd_handles);
4311 kfree(ioc->tm_cmds.reply);
4312 kfree(ioc->transport_cmds.reply);
4313 kfree(ioc->scsih_cmds.reply);
4314 kfree(ioc->config_cmds.reply);
4315 kfree(ioc->base_cmds.reply);
4316 kfree(ioc->port_enable_cmds.reply);
4317 kfree(ioc->ctl_cmds.reply);
4318 kfree(ioc->ctl_cmds.sense);
4320 ioc->ctl_cmds.reply = NULL;
4321 ioc->base_cmds.reply = NULL;
4322 ioc->tm_cmds.reply = NULL;
4323 ioc->scsih_cmds.reply = NULL;
4324 ioc->transport_cmds.reply = NULL;
4325 ioc->config_cmds.reply = NULL;
4332 * mpt2sas_base_detach - remove controller instance
4333 * @ioc: per adapter object
4338 mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
4341 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4344 mpt2sas_base_stop_watchdog(ioc);
4345 mpt2sas_base_free_resources(ioc);
4346 _base_release_memory_pools(ioc);
4347 pci_set_drvdata(ioc->pdev, NULL);
4348 kfree(ioc->cpu_msix_table);
4349 if (ioc->is_warpdrive)
4350 kfree(ioc->reply_post_host_index);
4351 kfree(ioc->pd_handles);
4353 kfree(ioc->ctl_cmds.reply);
4354 kfree(ioc->ctl_cmds.sense);
4355 kfree(ioc->base_cmds.reply);
4356 kfree(ioc->port_enable_cmds.reply);
4357 kfree(ioc->tm_cmds.reply);
4358 kfree(ioc->transport_cmds.reply);
4359 kfree(ioc->scsih_cmds.reply);
4360 kfree(ioc->config_cmds.reply);
4364 * _base_reset_handler - reset callback handler (for base)
4365 * @ioc: per adapter object
4366 * @reset_phase: phase
4368 * The handler for doing any required cleanup or initialization.
4370 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
4371 * MPT2_IOC_DONE_RESET
4376 _base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
4378 mpt2sas_scsih_reset_handler(ioc, reset_phase);
4379 mpt2sas_ctl_reset_handler(ioc, reset_phase);
4380 switch (reset_phase) {
4381 case MPT2_IOC_PRE_RESET:
4382 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4383 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
4385 case MPT2_IOC_AFTER_RESET:
4386 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4387 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
4388 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
4389 ioc->transport_cmds.status |= MPT2_CMD_RESET;
4390 mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
4391 complete(&ioc->transport_cmds.done);
4393 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
4394 ioc->base_cmds.status |= MPT2_CMD_RESET;
4395 mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
4396 complete(&ioc->base_cmds.done);
4398 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
4399 ioc->port_enable_failed = 1;
4400 ioc->port_enable_cmds.status |= MPT2_CMD_RESET;
4401 mpt2sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
4402 if (ioc->is_driver_loading) {
4403 ioc->start_scan_failed =
4404 MPI2_IOCSTATUS_INTERNAL_ERROR;
4405 ioc->start_scan = 0;
4406 ioc->port_enable_cmds.status =
4409 complete(&ioc->port_enable_cmds.done);
4412 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
4413 ioc->config_cmds.status |= MPT2_CMD_RESET;
4414 mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
4415 ioc->config_cmds.smid = USHRT_MAX;
4416 complete(&ioc->config_cmds.done);
4419 case MPT2_IOC_DONE_RESET:
4420 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4421 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
4427 * _wait_for_commands_to_complete - reset controller
4428 * @ioc: Pointer to MPT_ADAPTER structure
4429 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4431 * This function waiting(3s) for all pending commands to complete
4432 * prior to putting controller in reset.
4435 _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4438 unsigned long flags;
4441 ioc->pending_io_count = 0;
4442 if (sleep_flag != CAN_SLEEP)
4445 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4446 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
4449 /* pending command count */
4450 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4451 for (i = 0; i < ioc->scsiio_depth; i++)
4452 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
4453 ioc->pending_io_count++;
4454 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4456 if (!ioc->pending_io_count)
4459 /* wait for pending commands to complete */
4460 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
4464 * mpt2sas_base_hard_reset_handler - reset controller
4465 * @ioc: Pointer to MPT_ADAPTER structure
4466 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4467 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4469 * Returns 0 for success, non-zero for failure.
4472 mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4473 enum reset_type type)
4476 unsigned long flags;
4478 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
4481 if (ioc->pci_error_recovery) {
4482 printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
4483 ioc->name, __func__);
4488 if (mpt2sas_fwfault_debug)
4489 mpt2sas_halt_firmware(ioc);
4491 /* TODO - What we really should be doing is pulling
4492 * out all the code associated with NO_SLEEP; its never used.
4493 * That is legacy code from mpt fusion driver, ported over.
4494 * I will leave this BUG_ON here for now till its been resolved.
4496 BUG_ON(sleep_flag == NO_SLEEP);
4498 /* wait for an active reset in progress to complete */
4499 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4502 } while (ioc->shost_recovery == 1);
4503 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
4505 return ioc->ioc_reset_in_progress_status;
4508 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4509 ioc->shost_recovery = 1;
4510 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4512 _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
4513 _wait_for_commands_to_complete(ioc, sleep_flag);
4514 _base_mask_interrupts(ioc);
4515 r = _base_make_ioc_ready(ioc, sleep_flag, type);
4518 _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
4520 /* If this hard reset is called while port enable is active, then
4521 * there is no reason to call make_ioc_operational
4523 if (ioc->is_driver_loading && ioc->port_enable_failed) {
4524 ioc->remove_host = 1;
4528 r = _base_make_ioc_operational(ioc, sleep_flag);
4530 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
4532 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
4533 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4535 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4536 ioc->ioc_reset_in_progress_status = r;
4537 ioc->shost_recovery = 0;
4538 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4539 mutex_unlock(&ioc->reset_in_progress_mutex);
4541 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,