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/mpt3sas/mpt3sas_base.c
6 * Copyright (C) 2012-2014 LSI Corporation
7 * Copyright (C) 2013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/slab.h>
51 #include <linux/types.h>
52 #include <linux/pci.h>
53 #include <linux/kdev_t.h>
54 #include <linux/blkdev.h>
55 #include <linux/delay.h>
56 #include <linux/interrupt.h>
57 #include <linux/dma-mapping.h>
59 #include <linux/time.h>
60 #include <linux/kthread.h>
61 #include <linux/aer.h>
64 #include "mpt3sas_base.h"
66 static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
69 #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
71 /* maximum controller queue depth */
72 #define MAX_HBA_QUEUE_DEPTH 30000
73 #define MAX_CHAIN_DEPTH 100000
74 static int max_queue_depth = -1;
75 module_param(max_queue_depth, int, 0);
76 MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
78 static int max_sgl_entries = -1;
79 module_param(max_sgl_entries, int, 0);
80 MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
82 static int msix_disable = -1;
83 module_param(msix_disable, int, 0);
84 MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
86 static int max_msix_vectors = -1;
87 module_param(max_msix_vectors, int, 0);
88 MODULE_PARM_DESC(max_msix_vectors,
91 static int mpt3sas_fwfault_debug;
92 MODULE_PARM_DESC(mpt3sas_fwfault_debug,
93 " enable detection of firmware fault and halt firmware - (default=0)");
96 _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc, int sleep_flag);
99 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
103 _scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
105 int ret = param_set_int(val, kp);
106 struct MPT3SAS_ADAPTER *ioc;
111 /* global ioc spinlock to protect controller list on list operations */
112 pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
113 spin_lock(&gioc_lock);
114 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
115 ioc->fwfault_debug = mpt3sas_fwfault_debug;
116 spin_unlock(&gioc_lock);
119 module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
120 param_get_int, &mpt3sas_fwfault_debug, 0644);
123 * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
124 * @arg: input argument, used to derive ioc
126 * Return 0 if controller is removed from pci subsystem.
127 * Return -1 for other case.
129 static int mpt3sas_remove_dead_ioc_func(void *arg)
131 struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
132 struct pci_dev *pdev;
140 pci_stop_and_remove_bus_device_locked(pdev);
145 * _base_fault_reset_work - workq handling ioc fault conditions
146 * @work: input argument, used to derive ioc
152 _base_fault_reset_work(struct work_struct *work)
154 struct MPT3SAS_ADAPTER *ioc =
155 container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
159 struct task_struct *p;
162 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
163 if (ioc->shost_recovery || ioc->pci_error_recovery)
165 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
167 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
168 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
169 pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
172 /* It may be possible that EEH recovery can resolve some of
173 * pci bus failure issues rather removing the dead ioc function
174 * by considering controller is in a non-operational state. So
175 * here priority is given to the EEH recovery. If it doesn't
176 * not resolve this issue, mpt3sas driver will consider this
177 * controller to non-operational state and remove the dead ioc
180 if (ioc->non_operational_loop++ < 5) {
181 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
187 * Call _scsih_flush_pending_cmds callback so that we flush all
188 * pending commands back to OS. This call is required to aovid
189 * deadlock at block layer. Dead IOC will fail to do diag reset,
190 * and this call is safe since dead ioc will never return any
191 * command back from HW.
193 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
195 * Set remove_host flag early since kernel thread will
196 * take some time to execute.
198 ioc->remove_host = 1;
199 /*Remove the Dead Host */
200 p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
201 "mpt3sas_dead_ioc_%d", ioc->id);
204 "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
205 ioc->name, __func__);
208 "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
209 ioc->name, __func__);
210 return; /* don't rearm timer */
213 ioc->non_operational_loop = 0;
215 if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
216 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
218 pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
219 __func__, (rc == 0) ? "success" : "failed");
220 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
221 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
222 mpt3sas_base_fault_info(ioc, doorbell &
223 MPI2_DOORBELL_DATA_MASK);
224 if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
225 MPI2_IOC_STATE_OPERATIONAL)
226 return; /* don't rearm timer */
229 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
231 if (ioc->fault_reset_work_q)
232 queue_delayed_work(ioc->fault_reset_work_q,
233 &ioc->fault_reset_work,
234 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
235 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
239 * mpt3sas_base_start_watchdog - start the fault_reset_work_q
240 * @ioc: per adapter object
246 mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
250 if (ioc->fault_reset_work_q)
253 /* initialize fault polling */
255 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
256 snprintf(ioc->fault_reset_work_q_name,
257 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
258 ioc->fault_reset_work_q =
259 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
260 if (!ioc->fault_reset_work_q) {
261 pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
262 ioc->name, __func__, __LINE__);
265 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
266 if (ioc->fault_reset_work_q)
267 queue_delayed_work(ioc->fault_reset_work_q,
268 &ioc->fault_reset_work,
269 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
270 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
274 * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
275 * @ioc: per adapter object
281 mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
284 struct workqueue_struct *wq;
286 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
287 wq = ioc->fault_reset_work_q;
288 ioc->fault_reset_work_q = NULL;
289 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
291 if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
293 destroy_workqueue(wq);
298 * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
299 * @ioc: per adapter object
300 * @fault_code: fault code
305 mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
307 pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
308 ioc->name, fault_code);
312 * mpt3sas_halt_firmware - halt's mpt controller firmware
313 * @ioc: per adapter object
315 * For debugging timeout related issues. Writing 0xCOFFEE00
316 * to the doorbell register will halt controller firmware. With
317 * the purpose to stop both driver and firmware, the enduser can
318 * obtain a ring buffer from controller UART.
321 mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
325 if (!ioc->fwfault_debug)
330 doorbell = readl(&ioc->chip->Doorbell);
331 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
332 mpt3sas_base_fault_info(ioc , doorbell);
334 writel(0xC0FFEE00, &ioc->chip->Doorbell);
335 pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
339 if (ioc->fwfault_debug == 2)
343 panic("panic in %s\n", __func__);
347 * _base_sas_ioc_info - verbose translation of the ioc status
348 * @ioc: per adapter object
349 * @mpi_reply: reply mf payload returned from firmware
350 * @request_hdr: request mf
355 _base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
356 MPI2RequestHeader_t *request_hdr)
358 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
362 char *func_str = NULL;
364 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
365 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
366 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
367 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
370 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
373 switch (ioc_status) {
375 /****************************************************************************
376 * Common IOCStatus values for all replies
377 ****************************************************************************/
379 case MPI2_IOCSTATUS_INVALID_FUNCTION:
380 desc = "invalid function";
382 case MPI2_IOCSTATUS_BUSY:
385 case MPI2_IOCSTATUS_INVALID_SGL:
386 desc = "invalid sgl";
388 case MPI2_IOCSTATUS_INTERNAL_ERROR:
389 desc = "internal error";
391 case MPI2_IOCSTATUS_INVALID_VPID:
392 desc = "invalid vpid";
394 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
395 desc = "insufficient resources";
397 case MPI2_IOCSTATUS_INVALID_FIELD:
398 desc = "invalid field";
400 case MPI2_IOCSTATUS_INVALID_STATE:
401 desc = "invalid state";
403 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
404 desc = "op state not supported";
407 /****************************************************************************
408 * Config IOCStatus values
409 ****************************************************************************/
411 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
412 desc = "config invalid action";
414 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
415 desc = "config invalid type";
417 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
418 desc = "config invalid page";
420 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
421 desc = "config invalid data";
423 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
424 desc = "config no defaults";
426 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
427 desc = "config cant commit";
430 /****************************************************************************
432 ****************************************************************************/
434 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
435 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
436 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
437 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
438 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
439 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
440 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
441 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
442 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
443 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
444 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
445 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
448 /****************************************************************************
449 * For use by SCSI Initiator and SCSI Target end-to-end data protection
450 ****************************************************************************/
452 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
453 desc = "eedp guard error";
455 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
456 desc = "eedp ref tag error";
458 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
459 desc = "eedp app tag error";
462 /****************************************************************************
464 ****************************************************************************/
466 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
467 desc = "target invalid io index";
469 case MPI2_IOCSTATUS_TARGET_ABORTED:
470 desc = "target aborted";
472 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
473 desc = "target no conn retryable";
475 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
476 desc = "target no connection";
478 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
479 desc = "target xfer count mismatch";
481 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
482 desc = "target data offset error";
484 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
485 desc = "target too much write data";
487 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
488 desc = "target iu too short";
490 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
491 desc = "target ack nak timeout";
493 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
494 desc = "target nak received";
497 /****************************************************************************
498 * Serial Attached SCSI values
499 ****************************************************************************/
501 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
502 desc = "smp request failed";
504 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
505 desc = "smp data overrun";
508 /****************************************************************************
509 * Diagnostic Buffer Post / Diagnostic Release values
510 ****************************************************************************/
512 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
513 desc = "diagnostic released";
522 switch (request_hdr->Function) {
523 case MPI2_FUNCTION_CONFIG:
524 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
525 func_str = "config_page";
527 case MPI2_FUNCTION_SCSI_TASK_MGMT:
528 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
529 func_str = "task_mgmt";
531 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
532 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
533 func_str = "sas_iounit_ctl";
535 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
536 frame_sz = sizeof(Mpi2SepRequest_t);
537 func_str = "enclosure";
539 case MPI2_FUNCTION_IOC_INIT:
540 frame_sz = sizeof(Mpi2IOCInitRequest_t);
541 func_str = "ioc_init";
543 case MPI2_FUNCTION_PORT_ENABLE:
544 frame_sz = sizeof(Mpi2PortEnableRequest_t);
545 func_str = "port_enable";
547 case MPI2_FUNCTION_SMP_PASSTHROUGH:
548 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
549 func_str = "smp_passthru";
553 func_str = "unknown";
557 pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
558 ioc->name, desc, ioc_status, request_hdr, func_str);
560 _debug_dump_mf(request_hdr, frame_sz/4);
564 * _base_display_event_data - verbose translation of firmware asyn events
565 * @ioc: per adapter object
566 * @mpi_reply: reply mf payload returned from firmware
571 _base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
572 Mpi2EventNotificationReply_t *mpi_reply)
577 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
580 event = le16_to_cpu(mpi_reply->Event);
583 case MPI2_EVENT_LOG_DATA:
586 case MPI2_EVENT_STATE_CHANGE:
587 desc = "Status Change";
589 case MPI2_EVENT_HARD_RESET_RECEIVED:
590 desc = "Hard Reset Received";
592 case MPI2_EVENT_EVENT_CHANGE:
593 desc = "Event Change";
595 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
596 desc = "Device Status Change";
598 case MPI2_EVENT_IR_OPERATION_STATUS:
599 if (!ioc->hide_ir_msg)
600 desc = "IR Operation Status";
602 case MPI2_EVENT_SAS_DISCOVERY:
604 Mpi2EventDataSasDiscovery_t *event_data =
605 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
606 pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
607 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
609 if (event_data->DiscoveryStatus)
610 pr_info("discovery_status(0x%08x)",
611 le32_to_cpu(event_data->DiscoveryStatus));
615 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
616 desc = "SAS Broadcast Primitive";
618 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
619 desc = "SAS Init Device Status Change";
621 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
622 desc = "SAS Init Table Overflow";
624 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
625 desc = "SAS Topology Change List";
627 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
628 desc = "SAS Enclosure Device Status Change";
630 case MPI2_EVENT_IR_VOLUME:
631 if (!ioc->hide_ir_msg)
634 case MPI2_EVENT_IR_PHYSICAL_DISK:
635 if (!ioc->hide_ir_msg)
636 desc = "IR Physical Disk";
638 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
639 if (!ioc->hide_ir_msg)
640 desc = "IR Configuration Change List";
642 case MPI2_EVENT_LOG_ENTRY_ADDED:
643 if (!ioc->hide_ir_msg)
644 desc = "Log Entry Added";
646 case MPI2_EVENT_TEMP_THRESHOLD:
647 desc = "Temperature Threshold";
654 pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
658 * _base_sas_log_info - verbose translation of firmware log info
659 * @ioc: per adapter object
660 * @log_info: log info
665 _base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
676 union loginfo_type sas_loginfo;
677 char *originator_str = NULL;
679 sas_loginfo.loginfo = log_info;
680 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
683 /* each nexus loss loginfo */
684 if (log_info == 0x31170000)
687 /* eat the loginfos associated with task aborts */
688 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
689 0x31140000 || log_info == 0x31130000))
692 switch (sas_loginfo.dw.originator) {
694 originator_str = "IOP";
697 originator_str = "PL";
700 if (!ioc->hide_ir_msg)
701 originator_str = "IR";
703 originator_str = "WarpDrive";
708 "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
710 originator_str, sas_loginfo.dw.code,
711 sas_loginfo.dw.subcode);
715 * _base_display_reply_info -
716 * @ioc: per adapter object
717 * @smid: system request message index
718 * @msix_index: MSIX table index supplied by the OS
719 * @reply: reply message frame(lower 32bit addr)
724 _base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
727 MPI2DefaultReply_t *mpi_reply;
731 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
732 if (unlikely(!mpi_reply)) {
733 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
734 ioc->name, __FILE__, __LINE__, __func__);
737 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
739 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
740 (ioc->logging_level & MPT_DEBUG_REPLY)) {
741 _base_sas_ioc_info(ioc , mpi_reply,
742 mpt3sas_base_get_msg_frame(ioc, smid));
745 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
746 loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
747 _base_sas_log_info(ioc, loginfo);
750 if (ioc_status || loginfo) {
751 ioc_status &= MPI2_IOCSTATUS_MASK;
752 mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
757 * mpt3sas_base_done - base internal command completion routine
758 * @ioc: per adapter object
759 * @smid: system request message index
760 * @msix_index: MSIX table index supplied by the OS
761 * @reply: reply message frame(lower 32bit addr)
763 * Return 1 meaning mf should be freed from _base_interrupt
764 * 0 means the mf is freed from this function.
767 mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
770 MPI2DefaultReply_t *mpi_reply;
772 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
773 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
776 if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
779 ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
781 ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
782 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
784 ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
786 complete(&ioc->base_cmds.done);
791 * _base_async_event - main callback handler for firmware asyn events
792 * @ioc: per adapter object
793 * @msix_index: MSIX table index supplied by the OS
794 * @reply: reply message frame(lower 32bit addr)
796 * Return 1 meaning mf should be freed from _base_interrupt
797 * 0 means the mf is freed from this function.
800 _base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
802 Mpi2EventNotificationReply_t *mpi_reply;
803 Mpi2EventAckRequest_t *ack_request;
806 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
809 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
812 _base_display_event_data(ioc, mpi_reply);
814 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
816 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
818 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
819 ioc->name, __func__);
823 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
824 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
825 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
826 ack_request->Event = mpi_reply->Event;
827 ack_request->EventContext = mpi_reply->EventContext;
828 ack_request->VF_ID = 0; /* TODO */
829 ack_request->VP_ID = 0;
830 mpt3sas_base_put_smid_default(ioc, smid);
834 /* scsih callback handler */
835 mpt3sas_scsih_event_callback(ioc, msix_index, reply);
837 /* ctl callback handler */
838 mpt3sas_ctl_event_callback(ioc, msix_index, reply);
844 * _base_get_cb_idx - obtain the callback index
845 * @ioc: per adapter object
846 * @smid: system request message index
848 * Return callback index.
851 _base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
856 if (smid < ioc->hi_priority_smid) {
858 cb_idx = ioc->scsi_lookup[i].cb_idx;
859 } else if (smid < ioc->internal_smid) {
860 i = smid - ioc->hi_priority_smid;
861 cb_idx = ioc->hpr_lookup[i].cb_idx;
862 } else if (smid <= ioc->hba_queue_depth) {
863 i = smid - ioc->internal_smid;
864 cb_idx = ioc->internal_lookup[i].cb_idx;
871 * _base_mask_interrupts - disable interrupts
872 * @ioc: per adapter object
874 * Disabling ResetIRQ, Reply and Doorbell Interrupts
879 _base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
883 ioc->mask_interrupts = 1;
884 him_register = readl(&ioc->chip->HostInterruptMask);
885 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
886 writel(him_register, &ioc->chip->HostInterruptMask);
887 readl(&ioc->chip->HostInterruptMask);
891 * _base_unmask_interrupts - enable interrupts
892 * @ioc: per adapter object
894 * Enabling only Reply Interrupts
899 _base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
903 him_register = readl(&ioc->chip->HostInterruptMask);
904 him_register &= ~MPI2_HIM_RIM;
905 writel(him_register, &ioc->chip->HostInterruptMask);
906 ioc->mask_interrupts = 0;
909 union reply_descriptor {
918 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
919 * @irq: irq number (not used)
920 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
921 * @r: pt_regs pointer (not used)
923 * Return IRQ_HANDLE if processed, else IRQ_NONE.
926 _base_interrupt(int irq, void *bus_id)
928 struct adapter_reply_queue *reply_q = bus_id;
929 union reply_descriptor rd;
931 u8 request_desript_type;
935 u8 msix_index = reply_q->msix_index;
936 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
937 Mpi2ReplyDescriptorsUnion_t *rpf;
940 if (ioc->mask_interrupts)
943 if (!atomic_add_unless(&reply_q->busy, 1, 1))
946 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
947 request_desript_type = rpf->Default.ReplyFlags
948 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
949 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
950 atomic_dec(&reply_q->busy);
957 rd.word = le64_to_cpu(rpf->Words);
958 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
961 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
962 if (request_desript_type ==
963 MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
964 request_desript_type ==
965 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
966 cb_idx = _base_get_cb_idx(ioc, smid);
967 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
968 (likely(mpt_callbacks[cb_idx] != NULL))) {
969 rc = mpt_callbacks[cb_idx](ioc, smid,
972 mpt3sas_base_free_smid(ioc, smid);
974 } else if (request_desript_type ==
975 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
977 rpf->AddressReply.ReplyFrameAddress);
978 if (reply > ioc->reply_dma_max_address ||
979 reply < ioc->reply_dma_min_address)
982 cb_idx = _base_get_cb_idx(ioc, smid);
983 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
984 (likely(mpt_callbacks[cb_idx] != NULL))) {
985 rc = mpt_callbacks[cb_idx](ioc, smid,
988 _base_display_reply_info(ioc,
989 smid, msix_index, reply);
991 mpt3sas_base_free_smid(ioc,
995 _base_async_event(ioc, msix_index, reply);
998 /* reply free queue handling */
1000 ioc->reply_free_host_index =
1001 (ioc->reply_free_host_index ==
1002 (ioc->reply_free_queue_depth - 1)) ?
1003 0 : ioc->reply_free_host_index + 1;
1004 ioc->reply_free[ioc->reply_free_host_index] =
1007 writel(ioc->reply_free_host_index,
1008 &ioc->chip->ReplyFreeHostIndex);
1012 rpf->Words = cpu_to_le64(ULLONG_MAX);
1013 reply_q->reply_post_host_index =
1014 (reply_q->reply_post_host_index ==
1015 (ioc->reply_post_queue_depth - 1)) ? 0 :
1016 reply_q->reply_post_host_index + 1;
1017 request_desript_type =
1018 reply_q->reply_post_free[reply_q->reply_post_host_index].
1019 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1021 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1023 if (!reply_q->reply_post_host_index)
1024 rpf = reply_q->reply_post_free;
1031 if (!completed_cmds) {
1032 atomic_dec(&reply_q->busy);
1037 if (ioc->is_warpdrive) {
1038 writel(reply_q->reply_post_host_index,
1039 ioc->reply_post_host_index[msix_index]);
1040 atomic_dec(&reply_q->busy);
1044 /* Update Reply Post Host Index.
1045 * For those HBA's which support combined reply queue feature
1046 * 1. Get the correct Supplemental Reply Post Host Index Register.
1047 * i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
1048 * Index Register address bank i.e replyPostRegisterIndex[],
1049 * 2. Then update this register with new reply host index value
1050 * in ReplyPostIndex field and the MSIxIndex field with
1051 * msix_index value reduced to a value between 0 and 7,
1052 * using a modulo 8 operation. Since each Supplemental Reply Post
1053 * Host Index Register supports 8 MSI-X vectors.
1055 * For other HBA's just update the Reply Post Host Index register with
1056 * new reply host index value in ReplyPostIndex Field and msix_index
1057 * value in MSIxIndex field.
1059 if (ioc->msix96_vector)
1060 writel(reply_q->reply_post_host_index | ((msix_index & 7) <<
1061 MPI2_RPHI_MSIX_INDEX_SHIFT),
1062 ioc->replyPostRegisterIndex[msix_index/8]);
1064 writel(reply_q->reply_post_host_index | (msix_index <<
1065 MPI2_RPHI_MSIX_INDEX_SHIFT),
1066 &ioc->chip->ReplyPostHostIndex);
1067 atomic_dec(&reply_q->busy);
1072 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1073 * @ioc: per adapter object
1077 _base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
1079 return (ioc->facts.IOCCapabilities &
1080 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1084 * mpt3sas_base_flush_reply_queues - flushing the MSIX reply queues
1085 * @ioc: per adapter object
1086 * Context: ISR conext
1088 * Called when a Task Management request has completed. We want
1089 * to flush the other reply queues so all the outstanding IO has been
1090 * completed back to OS before we process the TM completetion.
1095 mpt3sas_base_flush_reply_queues(struct MPT3SAS_ADAPTER *ioc)
1097 struct adapter_reply_queue *reply_q;
1099 /* If MSIX capability is turned off
1100 * then multi-queues are not enabled
1102 if (!_base_is_controller_msix_enabled(ioc))
1105 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1106 if (ioc->shost_recovery)
1108 /* TMs are on msix_index == 0 */
1109 if (reply_q->msix_index == 0)
1111 _base_interrupt(reply_q->vector, (void *)reply_q);
1116 * mpt3sas_base_release_callback_handler - clear interrupt callback handler
1117 * @cb_idx: callback index
1122 mpt3sas_base_release_callback_handler(u8 cb_idx)
1124 mpt_callbacks[cb_idx] = NULL;
1128 * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
1129 * @cb_func: callback function
1134 mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1138 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1139 if (mpt_callbacks[cb_idx] == NULL)
1142 mpt_callbacks[cb_idx] = cb_func;
1147 * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
1152 mpt3sas_base_initialize_callback_handler(void)
1156 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1157 mpt3sas_base_release_callback_handler(cb_idx);
1162 * _base_build_zero_len_sge - build zero length sg entry
1163 * @ioc: per adapter object
1164 * @paddr: virtual address for SGE
1166 * Create a zero length scatter gather entry to insure the IOCs hardware has
1167 * something to use if the target device goes brain dead and tries
1168 * to send data even when none is asked for.
1173 _base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1175 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1176 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1177 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1178 MPI2_SGE_FLAGS_SHIFT);
1179 ioc->base_add_sg_single(paddr, flags_length, -1);
1183 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1184 * @paddr: virtual address for SGE
1185 * @flags_length: SGE flags and data transfer length
1186 * @dma_addr: Physical address
1191 _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1193 Mpi2SGESimple32_t *sgel = paddr;
1195 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1196 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1197 sgel->FlagsLength = cpu_to_le32(flags_length);
1198 sgel->Address = cpu_to_le32(dma_addr);
1203 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1204 * @paddr: virtual address for SGE
1205 * @flags_length: SGE flags and data transfer length
1206 * @dma_addr: Physical address
1211 _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1213 Mpi2SGESimple64_t *sgel = paddr;
1215 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1216 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1217 sgel->FlagsLength = cpu_to_le32(flags_length);
1218 sgel->Address = cpu_to_le64(dma_addr);
1222 * _base_get_chain_buffer_tracker - obtain chain tracker
1223 * @ioc: per adapter object
1224 * @smid: smid associated to an IO request
1226 * Returns chain tracker(from ioc->free_chain_list)
1228 static struct chain_tracker *
1229 _base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1231 struct chain_tracker *chain_req;
1232 unsigned long flags;
1234 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1235 if (list_empty(&ioc->free_chain_list)) {
1236 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1237 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1238 "chain buffers not available\n", ioc->name));
1241 chain_req = list_entry(ioc->free_chain_list.next,
1242 struct chain_tracker, tracker_list);
1243 list_del_init(&chain_req->tracker_list);
1244 list_add_tail(&chain_req->tracker_list,
1245 &ioc->scsi_lookup[smid - 1].chain_list);
1246 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1252 * _base_build_sg - build generic sg
1253 * @ioc: per adapter object
1254 * @psge: virtual address for SGE
1255 * @data_out_dma: physical address for WRITES
1256 * @data_out_sz: data xfer size for WRITES
1257 * @data_in_dma: physical address for READS
1258 * @data_in_sz: data xfer size for READS
1263 _base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
1264 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1269 if (!data_out_sz && !data_in_sz) {
1270 _base_build_zero_len_sge(ioc, psge);
1274 if (data_out_sz && data_in_sz) {
1275 /* WRITE sgel first */
1276 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1277 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1278 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1279 ioc->base_add_sg_single(psge, sgl_flags |
1280 data_out_sz, data_out_dma);
1283 psge += ioc->sge_size;
1285 /* READ sgel last */
1286 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1287 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1288 MPI2_SGE_FLAGS_END_OF_LIST);
1289 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1290 ioc->base_add_sg_single(psge, sgl_flags |
1291 data_in_sz, data_in_dma);
1292 } else if (data_out_sz) /* WRITE */ {
1293 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1294 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1295 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
1296 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1297 ioc->base_add_sg_single(psge, sgl_flags |
1298 data_out_sz, data_out_dma);
1299 } else if (data_in_sz) /* READ */ {
1300 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1301 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1302 MPI2_SGE_FLAGS_END_OF_LIST);
1303 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1304 ioc->base_add_sg_single(psge, sgl_flags |
1305 data_in_sz, data_in_dma);
1309 /* IEEE format sgls */
1312 * _base_add_sg_single_ieee - add sg element for IEEE format
1313 * @paddr: virtual address for SGE
1315 * @chain_offset: number of 128 byte elements from start of segment
1316 * @length: data transfer length
1317 * @dma_addr: Physical address
1322 _base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
1323 dma_addr_t dma_addr)
1325 Mpi25IeeeSgeChain64_t *sgel = paddr;
1327 sgel->Flags = flags;
1328 sgel->NextChainOffset = chain_offset;
1329 sgel->Length = cpu_to_le32(length);
1330 sgel->Address = cpu_to_le64(dma_addr);
1334 * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
1335 * @ioc: per adapter object
1336 * @paddr: virtual address for SGE
1338 * Create a zero length scatter gather entry to insure the IOCs hardware has
1339 * something to use if the target device goes brain dead and tries
1340 * to send data even when none is asked for.
1345 _base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1347 u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1348 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
1349 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
1350 _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
1354 * _base_build_sg_scmd - main sg creation routine
1355 * @ioc: per adapter object
1356 * @scmd: scsi command
1357 * @smid: system request message index
1360 * The main routine that builds scatter gather table from a given
1361 * scsi request sent via the .queuecommand main handler.
1363 * Returns 0 success, anything else error
1366 _base_build_sg_scmd(struct MPT3SAS_ADAPTER *ioc,
1367 struct scsi_cmnd *scmd, u16 smid)
1369 Mpi2SCSIIORequest_t *mpi_request;
1370 dma_addr_t chain_dma;
1371 struct scatterlist *sg_scmd;
1372 void *sg_local, *chain;
1377 u32 sges_in_segment;
1379 u32 sgl_flags_last_element;
1380 u32 sgl_flags_end_buffer;
1381 struct chain_tracker *chain_req;
1383 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1385 /* init scatter gather flags */
1386 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1387 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1388 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1389 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1390 << MPI2_SGE_FLAGS_SHIFT;
1391 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1392 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1393 << MPI2_SGE_FLAGS_SHIFT;
1394 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1396 sg_scmd = scsi_sglist(scmd);
1397 sges_left = scsi_dma_map(scmd);
1398 if (sges_left < 0) {
1399 sdev_printk(KERN_ERR, scmd->device,
1400 "pci_map_sg failed: request for %d bytes!\n",
1401 scsi_bufflen(scmd));
1405 sg_local = &mpi_request->SGL;
1406 sges_in_segment = ioc->max_sges_in_main_message;
1407 if (sges_left <= sges_in_segment)
1408 goto fill_in_last_segment;
1410 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1411 (sges_in_segment * ioc->sge_size))/4;
1413 /* fill in main message segment when there is a chain following */
1414 while (sges_in_segment) {
1415 if (sges_in_segment == 1)
1416 ioc->base_add_sg_single(sg_local,
1417 sgl_flags_last_element | sg_dma_len(sg_scmd),
1418 sg_dma_address(sg_scmd));
1420 ioc->base_add_sg_single(sg_local, sgl_flags |
1421 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1422 sg_scmd = sg_next(sg_scmd);
1423 sg_local += ioc->sge_size;
1428 /* initializing the chain flags and pointers */
1429 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1430 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1433 chain = chain_req->chain_buffer;
1434 chain_dma = chain_req->chain_buffer_dma;
1436 sges_in_segment = (sges_left <=
1437 ioc->max_sges_in_chain_message) ? sges_left :
1438 ioc->max_sges_in_chain_message;
1439 chain_offset = (sges_left == sges_in_segment) ?
1440 0 : (sges_in_segment * ioc->sge_size)/4;
1441 chain_length = sges_in_segment * ioc->sge_size;
1443 chain_offset = chain_offset <<
1444 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1445 chain_length += ioc->sge_size;
1447 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1448 chain_length, chain_dma);
1451 goto fill_in_last_segment;
1453 /* fill in chain segments */
1454 while (sges_in_segment) {
1455 if (sges_in_segment == 1)
1456 ioc->base_add_sg_single(sg_local,
1457 sgl_flags_last_element |
1458 sg_dma_len(sg_scmd),
1459 sg_dma_address(sg_scmd));
1461 ioc->base_add_sg_single(sg_local, sgl_flags |
1462 sg_dma_len(sg_scmd),
1463 sg_dma_address(sg_scmd));
1464 sg_scmd = sg_next(sg_scmd);
1465 sg_local += ioc->sge_size;
1470 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1473 chain = chain_req->chain_buffer;
1474 chain_dma = chain_req->chain_buffer_dma;
1478 fill_in_last_segment:
1480 /* fill the last segment */
1483 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1484 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1486 ioc->base_add_sg_single(sg_local, sgl_flags |
1487 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1488 sg_scmd = sg_next(sg_scmd);
1489 sg_local += ioc->sge_size;
1497 * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
1498 * @ioc: per adapter object
1499 * @scmd: scsi command
1500 * @smid: system request message index
1503 * The main routine that builds scatter gather table from a given
1504 * scsi request sent via the .queuecommand main handler.
1506 * Returns 0 success, anything else error
1509 _base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
1510 struct scsi_cmnd *scmd, u16 smid)
1512 Mpi2SCSIIORequest_t *mpi_request;
1513 dma_addr_t chain_dma;
1514 struct scatterlist *sg_scmd;
1515 void *sg_local, *chain;
1519 u32 sges_in_segment;
1520 u8 simple_sgl_flags;
1521 u8 simple_sgl_flags_last;
1523 struct chain_tracker *chain_req;
1525 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1527 /* init scatter gather flags */
1528 simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1529 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1530 simple_sgl_flags_last = simple_sgl_flags |
1531 MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
1532 chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1533 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1535 sg_scmd = scsi_sglist(scmd);
1536 sges_left = scsi_dma_map(scmd);
1537 if (sges_left < 0) {
1538 sdev_printk(KERN_ERR, scmd->device,
1539 "pci_map_sg failed: request for %d bytes!\n",
1540 scsi_bufflen(scmd));
1544 sg_local = &mpi_request->SGL;
1545 sges_in_segment = (ioc->request_sz -
1546 offsetof(Mpi2SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
1547 if (sges_left <= sges_in_segment)
1548 goto fill_in_last_segment;
1550 mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
1551 (offsetof(Mpi2SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
1553 /* fill in main message segment when there is a chain following */
1554 while (sges_in_segment > 1) {
1555 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1556 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1557 sg_scmd = sg_next(sg_scmd);
1558 sg_local += ioc->sge_size_ieee;
1563 /* initializing the pointers */
1564 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1567 chain = chain_req->chain_buffer;
1568 chain_dma = chain_req->chain_buffer_dma;
1570 sges_in_segment = (sges_left <=
1571 ioc->max_sges_in_chain_message) ? sges_left :
1572 ioc->max_sges_in_chain_message;
1573 chain_offset = (sges_left == sges_in_segment) ?
1574 0 : sges_in_segment;
1575 chain_length = sges_in_segment * ioc->sge_size_ieee;
1577 chain_length += ioc->sge_size_ieee;
1578 _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
1579 chain_offset, chain_length, chain_dma);
1583 goto fill_in_last_segment;
1585 /* fill in chain segments */
1586 while (sges_in_segment) {
1587 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1588 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1589 sg_scmd = sg_next(sg_scmd);
1590 sg_local += ioc->sge_size_ieee;
1595 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1598 chain = chain_req->chain_buffer;
1599 chain_dma = chain_req->chain_buffer_dma;
1603 fill_in_last_segment:
1605 /* fill the last segment */
1606 while (sges_left > 0) {
1608 _base_add_sg_single_ieee(sg_local,
1609 simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
1610 sg_dma_address(sg_scmd));
1612 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1613 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1614 sg_scmd = sg_next(sg_scmd);
1615 sg_local += ioc->sge_size_ieee;
1623 * _base_build_sg_ieee - build generic sg for IEEE format
1624 * @ioc: per adapter object
1625 * @psge: virtual address for SGE
1626 * @data_out_dma: physical address for WRITES
1627 * @data_out_sz: data xfer size for WRITES
1628 * @data_in_dma: physical address for READS
1629 * @data_in_sz: data xfer size for READS
1634 _base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
1635 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1640 if (!data_out_sz && !data_in_sz) {
1641 _base_build_zero_len_sge_ieee(ioc, psge);
1645 if (data_out_sz && data_in_sz) {
1646 /* WRITE sgel first */
1647 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1648 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1649 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
1653 psge += ioc->sge_size_ieee;
1655 /* READ sgel last */
1656 sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
1657 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
1659 } else if (data_out_sz) /* WRITE */ {
1660 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1661 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
1662 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1663 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
1665 } else if (data_in_sz) /* READ */ {
1666 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1667 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
1668 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1669 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
1674 #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1677 * _base_config_dma_addressing - set dma addressing
1678 * @ioc: per adapter object
1679 * @pdev: PCI device struct
1681 * Returns 0 for success, non-zero for failure.
1684 _base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
1687 u64 consistent_dma_mask;
1690 consistent_dma_mask = DMA_BIT_MASK(64);
1692 consistent_dma_mask = DMA_BIT_MASK(32);
1694 if (sizeof(dma_addr_t) > 4) {
1695 const uint64_t required_mask =
1696 dma_get_required_mask(&pdev->dev);
1697 if ((required_mask > DMA_BIT_MASK(32)) &&
1698 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1699 !pci_set_consistent_dma_mask(pdev, consistent_dma_mask)) {
1700 ioc->base_add_sg_single = &_base_add_sg_single_64;
1701 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
1707 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1708 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1709 ioc->base_add_sg_single = &_base_add_sg_single_32;
1710 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
1718 "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
1719 ioc->name, ioc->dma_mask, convert_to_kb(s.totalram));
1725 _base_change_consistent_dma_mask(struct MPT3SAS_ADAPTER *ioc,
1726 struct pci_dev *pdev)
1728 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
1729 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1736 * _base_check_enable_msix - checks MSIX capabable.
1737 * @ioc: per adapter object
1739 * Check to see if card is capable of MSIX, and set number
1740 * of available msix vectors
1743 _base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
1746 u16 message_control;
1748 /* Check whether controller SAS2008 B0 controller,
1749 * if it is SAS2008 B0 controller use IO-APIC instead of MSIX
1751 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
1752 ioc->pdev->revision == SAS2_PCI_DEVICE_B0_REVISION) {
1756 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1758 dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
1763 /* get msix vector count */
1764 /* NUMA_IO not supported for older controllers */
1765 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
1766 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
1767 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
1768 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
1769 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
1770 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
1771 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
1772 ioc->msix_vector_count = 1;
1774 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1775 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1777 dinitprintk(ioc, pr_info(MPT3SAS_FMT
1778 "msix is supported, vector_count(%d)\n",
1779 ioc->name, ioc->msix_vector_count));
1784 * _base_free_irq - free irq
1785 * @ioc: per adapter object
1787 * Freeing respective reply_queue from the list.
1790 _base_free_irq(struct MPT3SAS_ADAPTER *ioc)
1792 struct adapter_reply_queue *reply_q, *next;
1794 if (list_empty(&ioc->reply_queue_list))
1797 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1798 list_del(&reply_q->list);
1799 irq_set_affinity_hint(reply_q->vector, NULL);
1800 free_cpumask_var(reply_q->affinity_hint);
1801 synchronize_irq(reply_q->vector);
1802 free_irq(reply_q->vector, reply_q);
1808 * _base_request_irq - request irq
1809 * @ioc: per adapter object
1810 * @index: msix index into vector table
1811 * @vector: irq vector
1813 * Inserting respective reply_queue into the list.
1816 _base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index, u32 vector)
1818 struct adapter_reply_queue *reply_q;
1821 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1823 pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
1824 ioc->name, (int)sizeof(struct adapter_reply_queue));
1828 reply_q->msix_index = index;
1829 reply_q->vector = vector;
1831 if (!alloc_cpumask_var(&reply_q->affinity_hint, GFP_KERNEL))
1833 cpumask_clear(reply_q->affinity_hint);
1835 atomic_set(&reply_q->busy, 0);
1836 if (ioc->msix_enable)
1837 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
1838 driver_name, ioc->id, index);
1840 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
1841 driver_name, ioc->id);
1842 r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1845 pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
1846 reply_q->name, vector);
1851 INIT_LIST_HEAD(&reply_q->list);
1852 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1857 * _base_assign_reply_queues - assigning msix index for each cpu
1858 * @ioc: per adapter object
1860 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1862 * It would nice if we could call irq_set_affinity, however it is not
1863 * an exported symbol
1866 _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
1868 unsigned int cpu, nr_cpus, nr_msix, index = 0;
1869 struct adapter_reply_queue *reply_q;
1871 if (!_base_is_controller_msix_enabled(ioc))
1874 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1876 nr_cpus = num_online_cpus();
1877 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
1878 ioc->facts.MaxMSIxVectors);
1882 cpu = cpumask_first(cpu_online_mask);
1884 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1886 unsigned int i, group = nr_cpus / nr_msix;
1891 if (index < nr_cpus % nr_msix)
1894 for (i = 0 ; i < group ; i++) {
1895 ioc->cpu_msix_table[cpu] = index;
1896 cpumask_or(reply_q->affinity_hint,
1897 reply_q->affinity_hint, get_cpu_mask(cpu));
1898 cpu = cpumask_next(cpu, cpu_online_mask);
1901 if (irq_set_affinity_hint(reply_q->vector,
1902 reply_q->affinity_hint))
1903 dinitprintk(ioc, pr_info(MPT3SAS_FMT
1904 "error setting affinity hint for irq vector %d\n",
1905 ioc->name, reply_q->vector));
1911 * _base_disable_msix - disables msix
1912 * @ioc: per adapter object
1916 _base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
1918 if (!ioc->msix_enable)
1920 pci_disable_msix(ioc->pdev);
1921 ioc->msix_enable = 0;
1925 * _base_enable_msix - enables msix, failback to io_apic
1926 * @ioc: per adapter object
1930 _base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
1932 struct msix_entry *entries, *a;
1937 if (msix_disable == -1 || msix_disable == 0)
1943 if (_base_check_enable_msix(ioc) != 0)
1946 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
1947 ioc->msix_vector_count);
1949 printk(MPT3SAS_FMT "MSI-X vectors supported: %d, no of cores"
1950 ": %d, max_msix_vectors: %d\n", ioc->name, ioc->msix_vector_count,
1951 ioc->cpu_count, max_msix_vectors);
1953 if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
1954 max_msix_vectors = 8;
1956 if (max_msix_vectors > 0) {
1957 ioc->reply_queue_count = min_t(int, max_msix_vectors,
1958 ioc->reply_queue_count);
1959 ioc->msix_vector_count = ioc->reply_queue_count;
1960 } else if (max_msix_vectors == 0)
1963 entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1966 dfailprintk(ioc, pr_info(MPT3SAS_FMT
1967 "kcalloc failed @ at %s:%d/%s() !!!\n",
1968 ioc->name, __FILE__, __LINE__, __func__));
1972 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
1975 r = pci_enable_msix_exact(ioc->pdev, entries, ioc->reply_queue_count);
1977 dfailprintk(ioc, pr_info(MPT3SAS_FMT
1978 "pci_enable_msix_exact failed (r=%d) !!!\n",
1984 ioc->msix_enable = 1;
1985 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
1986 r = _base_request_irq(ioc, i, a->vector);
1988 _base_free_irq(ioc);
1989 _base_disable_msix(ioc);
1998 /* failback to io_apic interrupt routing */
2001 ioc->reply_queue_count = 1;
2002 r = _base_request_irq(ioc, 0, ioc->pdev->irq);
2008 * mpt3sas_base_unmap_resources - free controller resources
2009 * @ioc: per adapter object
2012 mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
2014 struct pci_dev *pdev = ioc->pdev;
2016 dexitprintk(ioc, printk(MPT3SAS_FMT "%s\n",
2017 ioc->name, __func__));
2019 _base_free_irq(ioc);
2020 _base_disable_msix(ioc);
2022 if (ioc->msix96_vector)
2023 kfree(ioc->replyPostRegisterIndex);
2025 if (ioc->chip_phys) {
2030 if (pci_is_enabled(pdev)) {
2031 pci_release_selected_regions(ioc->pdev, ioc->bars);
2032 pci_disable_pcie_error_reporting(pdev);
2033 pci_disable_device(pdev);
2038 * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
2039 * @ioc: per adapter object
2041 * Returns 0 for success, non-zero for failure.
2044 mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
2046 struct pci_dev *pdev = ioc->pdev;
2052 struct adapter_reply_queue *reply_q;
2054 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
2055 ioc->name, __func__));
2057 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
2058 if (pci_enable_device_mem(pdev)) {
2059 pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
2066 if (pci_request_selected_regions(pdev, ioc->bars,
2068 pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
2075 /* AER (Advanced Error Reporting) hooks */
2076 pci_enable_pcie_error_reporting(pdev);
2078 pci_set_master(pdev);
2081 if (_base_config_dma_addressing(ioc, pdev) != 0) {
2082 pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
2083 ioc->name, pci_name(pdev));
2088 for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
2089 (!memap_sz || !pio_sz); i++) {
2090 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
2093 pio_chip = (u64)pci_resource_start(pdev, i);
2094 pio_sz = pci_resource_len(pdev, i);
2095 } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
2098 ioc->chip_phys = pci_resource_start(pdev, i);
2099 chip_phys = (u64)ioc->chip_phys;
2100 memap_sz = pci_resource_len(pdev, i);
2101 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
2105 if (ioc->chip == NULL) {
2106 pr_err(MPT3SAS_FMT "unable to map adapter memory! "
2107 " or resource not found\n", ioc->name);
2112 _base_mask_interrupts(ioc);
2114 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
2118 if (!ioc->rdpq_array_enable_assigned) {
2119 ioc->rdpq_array_enable = ioc->rdpq_array_capable;
2120 ioc->rdpq_array_enable_assigned = 1;
2123 r = _base_enable_msix(ioc);
2127 /* Use the Combined reply queue feature only for SAS3 C0 & higher
2128 * revision HBAs and also only when reply queue count is greater than 8
2130 if (ioc->msix96_vector && ioc->reply_queue_count > 8) {
2131 /* Determine the Supplemental Reply Post Host Index Registers
2132 * Addresse. Supplemental Reply Post Host Index Registers
2133 * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
2134 * each register is at offset bytes of
2135 * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
2137 ioc->replyPostRegisterIndex = kcalloc(
2138 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT,
2139 sizeof(resource_size_t *), GFP_KERNEL);
2140 if (!ioc->replyPostRegisterIndex) {
2141 dfailprintk(ioc, printk(MPT3SAS_FMT
2142 "allocation for reply Post Register Index failed!!!\n",
2148 for (i = 0; i < MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT; i++) {
2149 ioc->replyPostRegisterIndex[i] = (resource_size_t *)
2150 ((u8 *)&ioc->chip->Doorbell +
2151 MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
2152 (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
2155 ioc->msix96_vector = 0;
2157 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
2158 pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
2159 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
2160 "IO-APIC enabled"), reply_q->vector);
2162 pr_info(MPT3SAS_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
2163 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
2164 pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
2165 ioc->name, (unsigned long long)pio_chip, pio_sz);
2167 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
2168 pci_save_state(pdev);
2172 mpt3sas_base_unmap_resources(ioc);
2177 * mpt3sas_base_get_msg_frame - obtain request mf pointer
2178 * @ioc: per adapter object
2179 * @smid: system request message index(smid zero is invalid)
2181 * Returns virt pointer to message frame.
2184 mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2186 return (void *)(ioc->request + (smid * ioc->request_sz));
2190 * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
2191 * @ioc: per adapter object
2192 * @smid: system request message index
2194 * Returns virt pointer to sense buffer.
2197 mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2199 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
2203 * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
2204 * @ioc: per adapter object
2205 * @smid: system request message index
2207 * Returns phys pointer to the low 32bit address of the sense buffer.
2210 mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2212 return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
2213 SCSI_SENSE_BUFFERSIZE));
2217 * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
2218 * @ioc: per adapter object
2219 * @phys_addr: lower 32 physical addr of the reply
2221 * Converts 32bit lower physical addr into a virt address.
2224 mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
2228 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
2232 * mpt3sas_base_get_smid - obtain a free smid from internal queue
2233 * @ioc: per adapter object
2234 * @cb_idx: callback index
2236 * Returns smid (zero is invalid)
2239 mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2241 unsigned long flags;
2242 struct request_tracker *request;
2245 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2246 if (list_empty(&ioc->internal_free_list)) {
2247 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2248 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2249 ioc->name, __func__);
2253 request = list_entry(ioc->internal_free_list.next,
2254 struct request_tracker, tracker_list);
2255 request->cb_idx = cb_idx;
2256 smid = request->smid;
2257 list_del(&request->tracker_list);
2258 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2263 * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
2264 * @ioc: per adapter object
2265 * @cb_idx: callback index
2266 * @scmd: pointer to scsi command object
2268 * Returns smid (zero is invalid)
2271 mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
2272 struct scsi_cmnd *scmd)
2274 unsigned long flags;
2275 struct scsiio_tracker *request;
2278 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2279 if (list_empty(&ioc->free_list)) {
2280 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2281 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2282 ioc->name, __func__);
2286 request = list_entry(ioc->free_list.next,
2287 struct scsiio_tracker, tracker_list);
2288 request->scmd = scmd;
2289 request->cb_idx = cb_idx;
2290 smid = request->smid;
2291 list_del(&request->tracker_list);
2292 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2297 * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
2298 * @ioc: per adapter object
2299 * @cb_idx: callback index
2301 * Returns smid (zero is invalid)
2304 mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2306 unsigned long flags;
2307 struct request_tracker *request;
2310 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2311 if (list_empty(&ioc->hpr_free_list)) {
2312 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2316 request = list_entry(ioc->hpr_free_list.next,
2317 struct request_tracker, tracker_list);
2318 request->cb_idx = cb_idx;
2319 smid = request->smid;
2320 list_del(&request->tracker_list);
2321 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2326 * mpt3sas_base_free_smid - put smid back on free_list
2327 * @ioc: per adapter object
2328 * @smid: system request message index
2333 mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2335 unsigned long flags;
2337 struct chain_tracker *chain_req, *next;
2339 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2340 if (smid < ioc->hi_priority_smid) {
2343 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
2344 list_for_each_entry_safe(chain_req, next,
2345 &ioc->scsi_lookup[i].chain_list, tracker_list) {
2346 list_del_init(&chain_req->tracker_list);
2347 list_add(&chain_req->tracker_list,
2348 &ioc->free_chain_list);
2351 ioc->scsi_lookup[i].cb_idx = 0xFF;
2352 ioc->scsi_lookup[i].scmd = NULL;
2353 ioc->scsi_lookup[i].direct_io = 0;
2354 list_add(&ioc->scsi_lookup[i].tracker_list, &ioc->free_list);
2355 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2358 * See _wait_for_commands_to_complete() call with regards
2361 if (ioc->shost_recovery && ioc->pending_io_count) {
2362 if (ioc->pending_io_count == 1)
2363 wake_up(&ioc->reset_wq);
2364 ioc->pending_io_count--;
2367 } else if (smid < ioc->internal_smid) {
2369 i = smid - ioc->hi_priority_smid;
2370 ioc->hpr_lookup[i].cb_idx = 0xFF;
2371 list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
2372 } else if (smid <= ioc->hba_queue_depth) {
2373 /* internal queue */
2374 i = smid - ioc->internal_smid;
2375 ioc->internal_lookup[i].cb_idx = 0xFF;
2376 list_add(&ioc->internal_lookup[i].tracker_list,
2377 &ioc->internal_free_list);
2379 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2383 * _base_writeq - 64 bit write to MMIO
2384 * @ioc: per adapter object
2386 * @addr: address in MMIO space
2387 * @writeq_lock: spin lock
2389 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
2390 * care of 32 bit environment where its not quarenteed to send the entire word
2393 #if defined(writeq) && defined(CONFIG_64BIT)
2395 _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2397 writeq(cpu_to_le64(b), addr);
2401 _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2403 unsigned long flags;
2404 __u64 data_out = cpu_to_le64(b);
2406 spin_lock_irqsave(writeq_lock, flags);
2407 writel((u32)(data_out), addr);
2408 writel((u32)(data_out >> 32), (addr + 4));
2409 spin_unlock_irqrestore(writeq_lock, flags);
2414 _base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
2416 return ioc->cpu_msix_table[raw_smp_processor_id()];
2420 * mpt3sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
2421 * @ioc: per adapter object
2422 * @smid: system request message index
2423 * @handle: device handle
2428 mpt3sas_base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
2430 Mpi2RequestDescriptorUnion_t descriptor;
2431 u64 *request = (u64 *)&descriptor;
2434 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
2435 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2436 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2437 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2438 descriptor.SCSIIO.LMID = 0;
2439 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2440 &ioc->scsi_lookup_lock);
2444 * mpt3sas_base_put_smid_fast_path - send fast path request to firmware
2445 * @ioc: per adapter object
2446 * @smid: system request message index
2447 * @handle: device handle
2452 mpt3sas_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
2455 Mpi2RequestDescriptorUnion_t descriptor;
2456 u64 *request = (u64 *)&descriptor;
2458 descriptor.SCSIIO.RequestFlags =
2459 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
2460 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2461 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2462 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2463 descriptor.SCSIIO.LMID = 0;
2464 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2465 &ioc->scsi_lookup_lock);
2469 * mpt3sas_base_put_smid_hi_priority - send Task Managment request to firmware
2470 * @ioc: per adapter object
2471 * @smid: system request message index
2476 mpt3sas_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2478 Mpi2RequestDescriptorUnion_t descriptor;
2479 u64 *request = (u64 *)&descriptor;
2481 descriptor.HighPriority.RequestFlags =
2482 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
2483 descriptor.HighPriority.MSIxIndex = 0;
2484 descriptor.HighPriority.SMID = cpu_to_le16(smid);
2485 descriptor.HighPriority.LMID = 0;
2486 descriptor.HighPriority.Reserved1 = 0;
2487 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2488 &ioc->scsi_lookup_lock);
2492 * mpt3sas_base_put_smid_default - Default, primarily used for config pages
2493 * @ioc: per adapter object
2494 * @smid: system request message index
2499 mpt3sas_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2501 Mpi2RequestDescriptorUnion_t descriptor;
2502 u64 *request = (u64 *)&descriptor;
2504 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2505 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
2506 descriptor.Default.SMID = cpu_to_le16(smid);
2507 descriptor.Default.LMID = 0;
2508 descriptor.Default.DescriptorTypeDependent = 0;
2509 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2510 &ioc->scsi_lookup_lock);
2514 * _base_display_OEMs_branding - Display branding string
2515 * @ioc: per adapter object
2520 _base_display_OEMs_branding(struct MPT3SAS_ADAPTER *ioc)
2522 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
2525 switch (ioc->pdev->subsystem_vendor) {
2526 case PCI_VENDOR_ID_INTEL:
2527 switch (ioc->pdev->device) {
2528 case MPI2_MFGPAGE_DEVID_SAS2008:
2529 switch (ioc->pdev->subsystem_device) {
2530 case MPT2SAS_INTEL_RMS2LL080_SSDID:
2531 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2532 MPT2SAS_INTEL_RMS2LL080_BRANDING);
2534 case MPT2SAS_INTEL_RMS2LL040_SSDID:
2535 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2536 MPT2SAS_INTEL_RMS2LL040_BRANDING);
2538 case MPT2SAS_INTEL_SSD910_SSDID:
2539 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2540 MPT2SAS_INTEL_SSD910_BRANDING);
2544 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2545 ioc->name, ioc->pdev->subsystem_device);
2548 case MPI2_MFGPAGE_DEVID_SAS2308_2:
2549 switch (ioc->pdev->subsystem_device) {
2550 case MPT2SAS_INTEL_RS25GB008_SSDID:
2551 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2552 MPT2SAS_INTEL_RS25GB008_BRANDING);
2554 case MPT2SAS_INTEL_RMS25JB080_SSDID:
2555 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2556 MPT2SAS_INTEL_RMS25JB080_BRANDING);
2558 case MPT2SAS_INTEL_RMS25JB040_SSDID:
2559 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2560 MPT2SAS_INTEL_RMS25JB040_BRANDING);
2562 case MPT2SAS_INTEL_RMS25KB080_SSDID:
2563 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2564 MPT2SAS_INTEL_RMS25KB080_BRANDING);
2566 case MPT2SAS_INTEL_RMS25KB040_SSDID:
2567 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2568 MPT2SAS_INTEL_RMS25KB040_BRANDING);
2570 case MPT2SAS_INTEL_RMS25LB040_SSDID:
2571 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2572 MPT2SAS_INTEL_RMS25LB040_BRANDING);
2574 case MPT2SAS_INTEL_RMS25LB080_SSDID:
2575 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2576 MPT2SAS_INTEL_RMS25LB080_BRANDING);
2580 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2581 ioc->name, ioc->pdev->subsystem_device);
2584 case MPI25_MFGPAGE_DEVID_SAS3008:
2585 switch (ioc->pdev->subsystem_device) {
2586 case MPT3SAS_INTEL_RMS3JC080_SSDID:
2587 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2588 MPT3SAS_INTEL_RMS3JC080_BRANDING);
2591 case MPT3SAS_INTEL_RS3GC008_SSDID:
2592 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2593 MPT3SAS_INTEL_RS3GC008_BRANDING);
2595 case MPT3SAS_INTEL_RS3FC044_SSDID:
2596 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2597 MPT3SAS_INTEL_RS3FC044_BRANDING);
2599 case MPT3SAS_INTEL_RS3UC080_SSDID:
2600 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2601 MPT3SAS_INTEL_RS3UC080_BRANDING);
2605 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2606 ioc->name, ioc->pdev->subsystem_device);
2612 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2613 ioc->name, ioc->pdev->subsystem_device);
2617 case PCI_VENDOR_ID_DELL:
2618 switch (ioc->pdev->device) {
2619 case MPI2_MFGPAGE_DEVID_SAS2008:
2620 switch (ioc->pdev->subsystem_device) {
2621 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
2622 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2623 MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING);
2625 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
2626 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2627 MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING);
2629 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
2630 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2631 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING);
2633 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
2634 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2635 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING);
2637 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
2638 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2639 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING);
2641 case MPT2SAS_DELL_PERC_H200_SSDID:
2642 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2643 MPT2SAS_DELL_PERC_H200_BRANDING);
2645 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
2646 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2647 MPT2SAS_DELL_6GBPS_SAS_BRANDING);
2651 "Dell 6Gbps HBA: Subsystem ID: 0x%X\n",
2652 ioc->name, ioc->pdev->subsystem_device);
2656 case MPI25_MFGPAGE_DEVID_SAS3008:
2657 switch (ioc->pdev->subsystem_device) {
2658 case MPT3SAS_DELL_12G_HBA_SSDID:
2659 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2660 MPT3SAS_DELL_12G_HBA_BRANDING);
2664 "Dell 12Gbps HBA: Subsystem ID: 0x%X\n",
2665 ioc->name, ioc->pdev->subsystem_device);
2671 "Dell HBA: Subsystem ID: 0x%X\n", ioc->name,
2672 ioc->pdev->subsystem_device);
2676 case PCI_VENDOR_ID_CISCO:
2677 switch (ioc->pdev->device) {
2678 case MPI25_MFGPAGE_DEVID_SAS3008:
2679 switch (ioc->pdev->subsystem_device) {
2680 case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
2681 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2682 MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
2684 case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
2685 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2686 MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
2688 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
2689 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2690 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
2694 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
2695 ioc->name, ioc->pdev->subsystem_device);
2699 case MPI25_MFGPAGE_DEVID_SAS3108_1:
2700 switch (ioc->pdev->subsystem_device) {
2701 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
2702 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2703 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
2705 case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
2706 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2707 MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING
2712 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
2713 ioc->name, ioc->pdev->subsystem_device);
2719 "Cisco SAS HBA: Subsystem ID: 0x%X\n",
2720 ioc->name, ioc->pdev->subsystem_device);
2724 case MPT2SAS_HP_3PAR_SSVID:
2725 switch (ioc->pdev->device) {
2726 case MPI2_MFGPAGE_DEVID_SAS2004:
2727 switch (ioc->pdev->subsystem_device) {
2728 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
2729 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2730 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
2734 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
2735 ioc->name, ioc->pdev->subsystem_device);
2738 case MPI2_MFGPAGE_DEVID_SAS2308_2:
2739 switch (ioc->pdev->subsystem_device) {
2740 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
2741 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2742 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
2744 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
2745 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2746 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
2748 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
2749 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2750 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
2752 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
2753 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2754 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
2758 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
2759 ioc->name, ioc->pdev->subsystem_device);
2764 "HP SAS HBA: Subsystem ID: 0x%X\n",
2765 ioc->name, ioc->pdev->subsystem_device);
2774 * _base_display_ioc_capabilities - Disply IOC's capabilities.
2775 * @ioc: per adapter object
2780 _base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
2784 u32 iounit_pg1_flags;
2787 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2788 strncpy(desc, ioc->manu_pg0.ChipName, 16);
2789 pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
2790 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2792 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2793 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2794 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2795 ioc->facts.FWVersion.Word & 0x000000FF,
2796 ioc->pdev->revision,
2797 (bios_version & 0xFF000000) >> 24,
2798 (bios_version & 0x00FF0000) >> 16,
2799 (bios_version & 0x0000FF00) >> 8,
2800 bios_version & 0x000000FF);
2802 _base_display_OEMs_branding(ioc);
2804 pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
2806 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2807 pr_info("Initiator");
2811 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2812 pr_info("%sTarget", i ? "," : "");
2818 pr_info("Capabilities=(");
2820 if (!ioc->hide_ir_msg) {
2821 if (ioc->facts.IOCCapabilities &
2822 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2828 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2829 pr_info("%sTLR", i ? "," : "");
2833 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2834 pr_info("%sMulticast", i ? "," : "");
2838 if (ioc->facts.IOCCapabilities &
2839 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2840 pr_info("%sBIDI Target", i ? "," : "");
2844 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2845 pr_info("%sEEDP", i ? "," : "");
2849 if (ioc->facts.IOCCapabilities &
2850 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2851 pr_info("%sSnapshot Buffer", i ? "," : "");
2855 if (ioc->facts.IOCCapabilities &
2856 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2857 pr_info("%sDiag Trace Buffer", i ? "," : "");
2861 if (ioc->facts.IOCCapabilities &
2862 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2863 pr_info("%sDiag Extended Buffer", i ? "," : "");
2867 if (ioc->facts.IOCCapabilities &
2868 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2869 pr_info("%sTask Set Full", i ? "," : "");
2873 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2874 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2875 pr_info("%sNCQ", i ? "," : "");
2883 * mpt3sas_base_update_missing_delay - change the missing delay timers
2884 * @ioc: per adapter object
2885 * @device_missing_delay: amount of time till device is reported missing
2886 * @io_missing_delay: interval IO is returned when there is a missing device
2890 * Passed on the command line, this function will modify the device missing
2891 * delay, as well as the io missing delay. This should be called at driver
2895 mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
2896 u16 device_missing_delay, u8 io_missing_delay)
2898 u16 dmd, dmd_new, dmd_orignal;
2899 u8 io_missing_delay_original;
2901 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2902 Mpi2ConfigReply_t mpi_reply;
2906 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
2910 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2911 sizeof(Mpi2SasIOUnit1PhyData_t));
2912 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2913 if (!sas_iounit_pg1) {
2914 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2915 ioc->name, __FILE__, __LINE__, __func__);
2918 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2919 sas_iounit_pg1, sz))) {
2920 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2921 ioc->name, __FILE__, __LINE__, __func__);
2924 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2925 MPI2_IOCSTATUS_MASK;
2926 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2927 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2928 ioc->name, __FILE__, __LINE__, __func__);
2932 /* device missing delay */
2933 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2934 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2935 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2937 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2939 if (device_missing_delay > 0x7F) {
2940 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2941 device_missing_delay;
2943 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2945 dmd = device_missing_delay;
2946 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2948 /* io missing delay */
2949 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2950 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2952 if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2954 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2956 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2959 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2960 pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
2961 ioc->name, dmd_orignal, dmd_new);
2962 pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
2963 ioc->name, io_missing_delay_original,
2965 ioc->device_missing_delay = dmd_new;
2966 ioc->io_missing_delay = io_missing_delay;
2970 kfree(sas_iounit_pg1);
2973 * _base_static_config_pages - static start of day config pages
2974 * @ioc: per adapter object
2979 _base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
2981 Mpi2ConfigReply_t mpi_reply;
2982 u32 iounit_pg1_flags;
2984 mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
2985 if (ioc->ir_firmware)
2986 mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2990 * Ensure correct T10 PI operation if vendor left EEDPTagMode
2991 * flag unset in NVDATA.
2993 mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
2994 if (ioc->manu_pg11.EEDPTagMode == 0) {
2995 pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
2997 ioc->manu_pg11.EEDPTagMode &= ~0x3;
2998 ioc->manu_pg11.EEDPTagMode |= 0x1;
2999 mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
3003 mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
3004 mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
3005 mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
3006 mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
3007 mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
3008 mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
3009 _base_display_ioc_capabilities(ioc);
3012 * Enable task_set_full handling in iounit_pg1 when the
3013 * facts capabilities indicate that its supported.
3015 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
3016 if ((ioc->facts.IOCCapabilities &
3017 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
3019 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
3022 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
3023 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
3024 mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
3026 if (ioc->iounit_pg8.NumSensors)
3027 ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
3031 * _base_release_memory_pools - release memory
3032 * @ioc: per adapter object
3034 * Free memory allocated from _base_allocate_memory_pools.
3039 _base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
3042 struct reply_post_struct *rps;
3044 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3048 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
3049 ioc->request, ioc->request_dma);
3050 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3051 "request_pool(0x%p): free\n",
3052 ioc->name, ioc->request));
3053 ioc->request = NULL;
3057 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
3058 if (ioc->sense_dma_pool)
3059 pci_pool_destroy(ioc->sense_dma_pool);
3060 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3061 "sense_pool(0x%p): free\n",
3062 ioc->name, ioc->sense));
3067 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
3068 if (ioc->reply_dma_pool)
3069 pci_pool_destroy(ioc->reply_dma_pool);
3070 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3071 "reply_pool(0x%p): free\n",
3072 ioc->name, ioc->reply));
3076 if (ioc->reply_free) {
3077 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
3078 ioc->reply_free_dma);
3079 if (ioc->reply_free_dma_pool)
3080 pci_pool_destroy(ioc->reply_free_dma_pool);
3081 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3082 "reply_free_pool(0x%p): free\n",
3083 ioc->name, ioc->reply_free));
3084 ioc->reply_free = NULL;
3087 if (ioc->reply_post) {
3089 rps = &ioc->reply_post[i];
3090 if (rps->reply_post_free) {
3092 ioc->reply_post_free_dma_pool,
3093 rps->reply_post_free,
3094 rps->reply_post_free_dma);
3095 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3096 "reply_post_free_pool(0x%p): free\n",
3097 ioc->name, rps->reply_post_free));
3098 rps->reply_post_free = NULL;
3100 } while (ioc->rdpq_array_enable &&
3101 (++i < ioc->reply_queue_count));
3103 if (ioc->reply_post_free_dma_pool)
3104 pci_pool_destroy(ioc->reply_post_free_dma_pool);
3105 kfree(ioc->reply_post);
3108 if (ioc->config_page) {
3109 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3110 "config_page(0x%p): free\n", ioc->name,
3112 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
3113 ioc->config_page, ioc->config_page_dma);
3116 if (ioc->scsi_lookup) {
3117 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
3118 ioc->scsi_lookup = NULL;
3120 kfree(ioc->hpr_lookup);
3121 kfree(ioc->internal_lookup);
3122 if (ioc->chain_lookup) {
3123 for (i = 0; i < ioc->chain_depth; i++) {
3124 if (ioc->chain_lookup[i].chain_buffer)
3125 pci_pool_free(ioc->chain_dma_pool,
3126 ioc->chain_lookup[i].chain_buffer,
3127 ioc->chain_lookup[i].chain_buffer_dma);
3129 if (ioc->chain_dma_pool)
3130 pci_pool_destroy(ioc->chain_dma_pool);
3131 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
3132 ioc->chain_lookup = NULL;
3137 * _base_allocate_memory_pools - allocate start of day memory pools
3138 * @ioc: per adapter object
3139 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3141 * Returns 0 success, anything else error
3144 _base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
3146 struct mpt3sas_facts *facts;
3147 u16 max_sge_elements;
3148 u16 chains_needed_per_io;
3149 u32 sz, total_sz, reply_post_free_sz;
3151 u16 max_request_credit;
3152 unsigned short sg_tablesize;
3156 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3161 facts = &ioc->facts;
3163 /* command line tunables for max sgl entries */
3164 if (max_sgl_entries != -1)
3165 sg_tablesize = max_sgl_entries;
3167 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
3168 sg_tablesize = MPT2SAS_SG_DEPTH;
3170 sg_tablesize = MPT3SAS_SG_DEPTH;
3173 if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
3174 sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
3175 else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
3176 sg_tablesize = min_t(unsigned short, sg_tablesize,
3177 SCSI_MAX_SG_CHAIN_SEGMENTS);
3179 "sg_tablesize(%u) is bigger than kernel"
3180 " defined SCSI_MAX_SG_SEGMENTS(%u)\n", ioc->name,
3181 sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
3183 ioc->shost->sg_tablesize = sg_tablesize;
3185 ioc->hi_priority_depth = facts->HighPriorityCredit;
3186 ioc->internal_depth = ioc->hi_priority_depth + (5);
3187 /* command line tunables for max controller queue depth */
3188 if (max_queue_depth != -1 && max_queue_depth != 0) {
3189 max_request_credit = min_t(u16, max_queue_depth +
3190 ioc->hi_priority_depth + ioc->internal_depth,
3191 facts->RequestCredit);
3192 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
3193 max_request_credit = MAX_HBA_QUEUE_DEPTH;
3195 max_request_credit = min_t(u16, facts->RequestCredit,
3196 MAX_HBA_QUEUE_DEPTH);
3198 ioc->hba_queue_depth = max_request_credit;
3200 /* request frame size */
3201 ioc->request_sz = facts->IOCRequestFrameSize * 4;
3203 /* reply frame size */
3204 ioc->reply_sz = facts->ReplyFrameSize * 4;
3206 /* calculate the max scatter element size */
3207 sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
3211 /* calculate number of sg elements left over in the 1st frame */
3212 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
3213 sizeof(Mpi2SGEIOUnion_t)) + sge_size);
3214 ioc->max_sges_in_main_message = max_sge_elements/sge_size;
3216 /* now do the same for a chain buffer */
3217 max_sge_elements = ioc->request_sz - sge_size;
3218 ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
3221 * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
3223 chains_needed_per_io = ((ioc->shost->sg_tablesize -
3224 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
3226 if (chains_needed_per_io > facts->MaxChainDepth) {
3227 chains_needed_per_io = facts->MaxChainDepth;
3228 ioc->shost->sg_tablesize = min_t(u16,
3229 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
3230 * chains_needed_per_io), ioc->shost->sg_tablesize);
3232 ioc->chains_needed_per_io = chains_needed_per_io;
3234 /* reply free queue sizing - taking into account for 64 FW events */
3235 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
3237 /* calculate reply descriptor post queue depth */
3238 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
3239 ioc->reply_free_queue_depth + 1 ;
3240 /* align the reply post queue on the next 16 count boundary */
3241 if (ioc->reply_post_queue_depth % 16)
3242 ioc->reply_post_queue_depth += 16 -
3243 (ioc->reply_post_queue_depth % 16);
3246 if (ioc->reply_post_queue_depth >
3247 facts->MaxReplyDescriptorPostQueueDepth) {
3248 ioc->reply_post_queue_depth =
3249 facts->MaxReplyDescriptorPostQueueDepth -
3250 (facts->MaxReplyDescriptorPostQueueDepth % 16);
3251 ioc->hba_queue_depth =
3252 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
3253 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
3256 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
3257 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
3258 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
3259 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
3260 ioc->chains_needed_per_io));
3262 /* reply post queue, 16 byte align */
3263 reply_post_free_sz = ioc->reply_post_queue_depth *
3264 sizeof(Mpi2DefaultReplyDescriptor_t);
3266 sz = reply_post_free_sz;
3267 if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
3268 sz *= ioc->reply_queue_count;
3270 ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
3271 (ioc->reply_queue_count):1,
3272 sizeof(struct reply_post_struct), GFP_KERNEL);
3274 if (!ioc->reply_post) {
3275 pr_err(MPT3SAS_FMT "reply_post_free pool: kcalloc failed\n",
3279 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
3280 ioc->pdev, sz, 16, 0);
3281 if (!ioc->reply_post_free_dma_pool) {
3283 "reply_post_free pool: pci_pool_create failed\n",
3289 ioc->reply_post[i].reply_post_free =
3290 pci_pool_alloc(ioc->reply_post_free_dma_pool,
3292 &ioc->reply_post[i].reply_post_free_dma);
3293 if (!ioc->reply_post[i].reply_post_free) {
3295 "reply_post_free pool: pci_pool_alloc failed\n",
3299 memset(ioc->reply_post[i].reply_post_free, 0, sz);
3300 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3301 "reply post free pool (0x%p): depth(%d),"
3302 "element_size(%d), pool_size(%d kB)\n", ioc->name,
3303 ioc->reply_post[i].reply_post_free,
3304 ioc->reply_post_queue_depth, 8, sz/1024));
3305 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3306 "reply_post_free_dma = (0x%llx)\n", ioc->name,
3307 (unsigned long long)
3308 ioc->reply_post[i].reply_post_free_dma));
3310 } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
3312 if (ioc->dma_mask == 64) {
3313 if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
3315 "no suitable consistent DMA mask for %s\n",
3316 ioc->name, pci_name(ioc->pdev));
3321 ioc->scsiio_depth = ioc->hba_queue_depth -
3322 ioc->hi_priority_depth - ioc->internal_depth;
3324 /* set the scsi host can_queue depth
3325 * with some internal commands that could be outstanding
3327 ioc->shost->can_queue = ioc->scsiio_depth;
3328 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3329 "scsi host: can_queue depth (%d)\n",
3330 ioc->name, ioc->shost->can_queue));
3333 /* contiguous pool for request and chains, 16 byte align, one extra "
3336 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
3337 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
3339 /* hi-priority queue */
3340 sz += (ioc->hi_priority_depth * ioc->request_sz);
3342 /* internal queue */
3343 sz += (ioc->internal_depth * ioc->request_sz);
3345 ioc->request_dma_sz = sz;
3346 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
3347 if (!ioc->request) {
3348 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
3349 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
3350 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
3351 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
3352 if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
3355 ioc->hba_queue_depth = max_request_credit - retry_sz;
3356 goto retry_allocation;
3360 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
3361 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
3362 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
3363 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
3365 /* hi-priority queue */
3366 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
3368 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
3371 /* internal queue */
3372 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
3374 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
3377 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3378 "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
3379 ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
3380 (ioc->hba_queue_depth * ioc->request_sz)/1024));
3382 dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
3383 ioc->name, (unsigned long long) ioc->request_dma));
3386 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
3387 ioc->scsi_lookup_pages = get_order(sz);
3388 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
3389 GFP_KERNEL, ioc->scsi_lookup_pages);
3390 if (!ioc->scsi_lookup) {
3391 pr_err(MPT3SAS_FMT "scsi_lookup: get_free_pages failed, sz(%d)\n",
3392 ioc->name, (int)sz);
3396 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
3397 ioc->name, ioc->request, ioc->scsiio_depth));
3399 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
3400 sz = ioc->chain_depth * sizeof(struct chain_tracker);
3401 ioc->chain_pages = get_order(sz);
3402 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
3403 GFP_KERNEL, ioc->chain_pages);
3404 if (!ioc->chain_lookup) {
3405 pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages failed\n",
3409 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
3410 ioc->request_sz, 16, 0);
3411 if (!ioc->chain_dma_pool) {
3412 pr_err(MPT3SAS_FMT "chain_dma_pool: pci_pool_create failed\n",
3416 for (i = 0; i < ioc->chain_depth; i++) {
3417 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
3418 ioc->chain_dma_pool , GFP_KERNEL,
3419 &ioc->chain_lookup[i].chain_buffer_dma);
3420 if (!ioc->chain_lookup[i].chain_buffer) {
3421 ioc->chain_depth = i;
3424 total_sz += ioc->request_sz;
3427 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3428 "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
3429 ioc->name, ioc->chain_depth, ioc->request_sz,
3430 ((ioc->chain_depth * ioc->request_sz))/1024));
3432 /* initialize hi-priority queue smid's */
3433 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
3434 sizeof(struct request_tracker), GFP_KERNEL);
3435 if (!ioc->hpr_lookup) {
3436 pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
3440 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
3441 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3442 "hi_priority(0x%p): depth(%d), start smid(%d)\n",
3443 ioc->name, ioc->hi_priority,
3444 ioc->hi_priority_depth, ioc->hi_priority_smid));
3446 /* initialize internal queue smid's */
3447 ioc->internal_lookup = kcalloc(ioc->internal_depth,
3448 sizeof(struct request_tracker), GFP_KERNEL);
3449 if (!ioc->internal_lookup) {
3450 pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
3454 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
3455 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3456 "internal(0x%p): depth(%d), start smid(%d)\n",
3457 ioc->name, ioc->internal,
3458 ioc->internal_depth, ioc->internal_smid));
3460 /* sense buffers, 4 byte align */
3461 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
3462 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
3464 if (!ioc->sense_dma_pool) {
3465 pr_err(MPT3SAS_FMT "sense pool: pci_pool_create failed\n",
3469 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
3472 pr_err(MPT3SAS_FMT "sense pool: pci_pool_alloc failed\n",
3476 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3477 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
3478 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
3479 SCSI_SENSE_BUFFERSIZE, sz/1024));
3480 dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
3481 ioc->name, (unsigned long long)ioc->sense_dma));
3484 /* reply pool, 4 byte align */
3485 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
3486 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
3488 if (!ioc->reply_dma_pool) {
3489 pr_err(MPT3SAS_FMT "reply pool: pci_pool_create failed\n",
3493 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
3496 pr_err(MPT3SAS_FMT "reply pool: pci_pool_alloc failed\n",
3500 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
3501 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
3502 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3503 "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
3504 ioc->name, ioc->reply,
3505 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
3506 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
3507 ioc->name, (unsigned long long)ioc->reply_dma));
3510 /* reply free queue, 16 byte align */
3511 sz = ioc->reply_free_queue_depth * 4;
3512 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
3513 ioc->pdev, sz, 16, 0);
3514 if (!ioc->reply_free_dma_pool) {
3515 pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_create failed\n",
3519 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
3520 &ioc->reply_free_dma);
3521 if (!ioc->reply_free) {
3522 pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_alloc failed\n",
3526 memset(ioc->reply_free, 0, sz);
3527 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
3528 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
3529 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
3530 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3531 "reply_free_dma (0x%llx)\n",
3532 ioc->name, (unsigned long long)ioc->reply_free_dma));
3535 ioc->config_page_sz = 512;
3536 ioc->config_page = pci_alloc_consistent(ioc->pdev,
3537 ioc->config_page_sz, &ioc->config_page_dma);
3538 if (!ioc->config_page) {
3540 "config page: pci_pool_alloc failed\n",
3544 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3545 "config page(0x%p): size(%d)\n",
3546 ioc->name, ioc->config_page, ioc->config_page_sz));
3547 dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
3548 ioc->name, (unsigned long long)ioc->config_page_dma));
3549 total_sz += ioc->config_page_sz;
3551 pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
3552 ioc->name, total_sz/1024);
3554 "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
3555 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
3556 pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
3557 ioc->name, ioc->shost->sg_tablesize);
3565 * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
3566 * @ioc: Pointer to MPT_ADAPTER structure
3567 * @cooked: Request raw or cooked IOC state
3569 * Returns all IOC Doorbell register bits if cooked==0, else just the
3570 * Doorbell bits in MPI_IOC_STATE_MASK.
3573 mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
3577 s = readl(&ioc->chip->Doorbell);
3578 sc = s & MPI2_IOC_STATE_MASK;
3579 return cooked ? sc : s;
3583 * _base_wait_on_iocstate - waiting on a particular ioc state
3584 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
3585 * @timeout: timeout in second
3586 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3588 * Returns 0 for success, non-zero for failure.
3591 _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
3598 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3600 current_state = mpt3sas_base_get_iocstate(ioc, 1);
3601 if (current_state == ioc_state)
3603 if (count && current_state == MPI2_IOC_STATE_FAULT)
3605 if (sleep_flag == CAN_SLEEP)
3606 usleep_range(1000, 1500);
3612 return current_state;
3616 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
3617 * a write to the doorbell)
3618 * @ioc: per adapter object
3619 * @timeout: timeout in second
3620 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3622 * Returns 0 for success, non-zero for failure.
3624 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
3627 _base_diag_reset(struct MPT3SAS_ADAPTER *ioc, int sleep_flag);
3630 _base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout,
3637 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3639 int_status = readl(&ioc->chip->HostInterruptStatus);
3640 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
3641 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3642 "%s: successful count(%d), timeout(%d)\n",
3643 ioc->name, __func__, count, timeout));
3646 if (sleep_flag == CAN_SLEEP)
3647 usleep_range(1000, 1500);
3654 "%s: failed due to timeout count(%d), int_status(%x)!\n",
3655 ioc->name, __func__, count, int_status);
3660 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
3661 * @ioc: per adapter object
3662 * @timeout: timeout in second
3663 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3665 * Returns 0 for success, non-zero for failure.
3667 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
3671 _base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout,
3679 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3681 int_status = readl(&ioc->chip->HostInterruptStatus);
3682 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
3683 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3684 "%s: successful count(%d), timeout(%d)\n",
3685 ioc->name, __func__, count, timeout));
3687 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
3688 doorbell = readl(&ioc->chip->Doorbell);
3689 if ((doorbell & MPI2_IOC_STATE_MASK) ==
3690 MPI2_IOC_STATE_FAULT) {
3691 mpt3sas_base_fault_info(ioc , doorbell);
3694 } else if (int_status == 0xFFFFFFFF)
3697 if (sleep_flag == CAN_SLEEP)
3698 usleep_range(1000, 1500);
3706 "%s: failed due to timeout count(%d), int_status(%x)!\n",
3707 ioc->name, __func__, count, int_status);
3712 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
3713 * @ioc: per adapter object
3714 * @timeout: timeout in second
3715 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3717 * Returns 0 for success, non-zero for failure.
3721 _base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout,
3728 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3730 doorbell_reg = readl(&ioc->chip->Doorbell);
3731 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
3732 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3733 "%s: successful count(%d), timeout(%d)\n",
3734 ioc->name, __func__, count, timeout));
3737 if (sleep_flag == CAN_SLEEP)
3738 usleep_range(1000, 1500);
3745 "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
3746 ioc->name, __func__, count, doorbell_reg);
3751 * _base_send_ioc_reset - send doorbell reset
3752 * @ioc: per adapter object
3753 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
3754 * @timeout: timeout in second
3755 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3757 * Returns 0 for success, non-zero for failure.
3760 _base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout,
3766 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
3767 pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
3768 ioc->name, __func__);
3772 if (!(ioc->facts.IOCCapabilities &
3773 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
3776 pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
3778 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
3779 &ioc->chip->Doorbell);
3780 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
3784 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
3785 timeout, sleep_flag);
3788 "%s: failed going to ready state (ioc_state=0x%x)\n",
3789 ioc->name, __func__, ioc_state);
3794 pr_info(MPT3SAS_FMT "message unit reset: %s\n",
3795 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3800 * _base_handshake_req_reply_wait - send request thru doorbell interface
3801 * @ioc: per adapter object
3802 * @request_bytes: request length
3803 * @request: pointer having request payload
3804 * @reply_bytes: reply length
3805 * @reply: pointer to reply payload
3806 * @timeout: timeout in second
3807 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3809 * Returns 0 for success, non-zero for failure.
3812 _base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
3813 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
3815 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
3821 /* make sure doorbell is not in use */
3822 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
3824 "doorbell is in use (line=%d)\n",
3825 ioc->name, __LINE__);
3829 /* clear pending doorbell interrupts from previous state changes */
3830 if (readl(&ioc->chip->HostInterruptStatus) &
3831 MPI2_HIS_IOC2SYS_DB_STATUS)
3832 writel(0, &ioc->chip->HostInterruptStatus);
3834 /* send message to ioc */
3835 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
3836 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
3837 &ioc->chip->Doorbell);
3839 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
3841 "doorbell handshake int failed (line=%d)\n",
3842 ioc->name, __LINE__);
3845 writel(0, &ioc->chip->HostInterruptStatus);
3847 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
3849 "doorbell handshake ack failed (line=%d)\n",
3850 ioc->name, __LINE__);
3854 /* send message 32-bits at a time */
3855 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
3856 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
3857 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
3863 "doorbell handshake sending request failed (line=%d)\n",
3864 ioc->name, __LINE__);
3868 /* now wait for the reply */
3869 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
3871 "doorbell handshake int failed (line=%d)\n",
3872 ioc->name, __LINE__);
3876 /* read the first two 16-bits, it gives the total length of the reply */
3877 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3878 & MPI2_DOORBELL_DATA_MASK);
3879 writel(0, &ioc->chip->HostInterruptStatus);
3880 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3882 "doorbell handshake int failed (line=%d)\n",
3883 ioc->name, __LINE__);
3886 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3887 & MPI2_DOORBELL_DATA_MASK);
3888 writel(0, &ioc->chip->HostInterruptStatus);
3890 for (i = 2; i < default_reply->MsgLength * 2; i++) {
3891 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3893 "doorbell handshake int failed (line=%d)\n",
3894 ioc->name, __LINE__);
3897 if (i >= reply_bytes/2) /* overflow case */
3898 dummy = readl(&ioc->chip->Doorbell);
3900 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3901 & MPI2_DOORBELL_DATA_MASK);
3902 writel(0, &ioc->chip->HostInterruptStatus);
3905 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3906 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3907 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3908 "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
3910 writel(0, &ioc->chip->HostInterruptStatus);
3912 if (ioc->logging_level & MPT_DEBUG_INIT) {
3913 mfp = (__le32 *)reply;
3914 pr_info("\toffset:data\n");
3915 for (i = 0; i < reply_bytes/4; i++)
3916 pr_info("\t[0x%02x]:%08x\n", i*4,
3917 le32_to_cpu(mfp[i]));
3923 * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
3924 * @ioc: per adapter object
3925 * @mpi_reply: the reply payload from FW
3926 * @mpi_request: the request payload sent to FW
3928 * The SAS IO Unit Control Request message allows the host to perform low-level
3929 * operations, such as resets on the PHYs of the IO Unit, also allows the host
3930 * to obtain the IOC assigned device handles for a device if it has other
3931 * identifying information about the device, in addition allows the host to
3932 * remove IOC resources associated with the device.
3934 * Returns 0 for success, non-zero for failure.
3937 mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
3938 Mpi2SasIoUnitControlReply_t *mpi_reply,
3939 Mpi2SasIoUnitControlRequest_t *mpi_request)
3943 unsigned long timeleft;
3944 bool issue_reset = false;
3947 u16 wait_state_count;
3949 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3952 mutex_lock(&ioc->base_cmds.mutex);
3954 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
3955 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
3956 ioc->name, __func__);
3961 wait_state_count = 0;
3962 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3963 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3964 if (wait_state_count++ == 10) {
3966 "%s: failed due to ioc not operational\n",
3967 ioc->name, __func__);
3972 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3974 "%s: waiting for operational state(count=%d)\n",
3975 ioc->name, __func__, wait_state_count);
3978 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
3980 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3981 ioc->name, __func__);
3987 ioc->base_cmds.status = MPT3_CMD_PENDING;
3988 request = mpt3sas_base_get_msg_frame(ioc, smid);
3989 ioc->base_cmds.smid = smid;
3990 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3991 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3992 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3993 ioc->ioc_link_reset_in_progress = 1;
3994 init_completion(&ioc->base_cmds.done);
3995 mpt3sas_base_put_smid_default(ioc, smid);
3996 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3997 msecs_to_jiffies(10000));
3998 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3999 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
4000 ioc->ioc_link_reset_in_progress)
4001 ioc->ioc_link_reset_in_progress = 0;
4002 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4003 pr_err(MPT3SAS_FMT "%s: timeout\n",
4004 ioc->name, __func__);
4005 _debug_dump_mf(mpi_request,
4006 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
4007 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
4009 goto issue_host_reset;
4011 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
4012 memcpy(mpi_reply, ioc->base_cmds.reply,
4013 sizeof(Mpi2SasIoUnitControlReply_t));
4015 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
4016 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4021 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
4023 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4026 mutex_unlock(&ioc->base_cmds.mutex);
4031 * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
4032 * @ioc: per adapter object
4033 * @mpi_reply: the reply payload from FW
4034 * @mpi_request: the request payload sent to FW
4036 * The SCSI Enclosure Processor request message causes the IOC to
4037 * communicate with SES devices to control LED status signals.
4039 * Returns 0 for success, non-zero for failure.
4042 mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
4043 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
4047 unsigned long timeleft;
4048 bool issue_reset = false;
4051 u16 wait_state_count;
4053 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4056 mutex_lock(&ioc->base_cmds.mutex);
4058 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
4059 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
4060 ioc->name, __func__);
4065 wait_state_count = 0;
4066 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4067 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4068 if (wait_state_count++ == 10) {
4070 "%s: failed due to ioc not operational\n",
4071 ioc->name, __func__);
4076 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4078 "%s: waiting for operational state(count=%d)\n",
4080 __func__, wait_state_count);
4083 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4085 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4086 ioc->name, __func__);
4092 ioc->base_cmds.status = MPT3_CMD_PENDING;
4093 request = mpt3sas_base_get_msg_frame(ioc, smid);
4094 ioc->base_cmds.smid = smid;
4095 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
4096 init_completion(&ioc->base_cmds.done);
4097 mpt3sas_base_put_smid_default(ioc, smid);
4098 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
4099 msecs_to_jiffies(10000));
4100 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4101 pr_err(MPT3SAS_FMT "%s: timeout\n",
4102 ioc->name, __func__);
4103 _debug_dump_mf(mpi_request,
4104 sizeof(Mpi2SepRequest_t)/4);
4105 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
4106 issue_reset = false;
4107 goto issue_host_reset;
4109 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
4110 memcpy(mpi_reply, ioc->base_cmds.reply,
4111 sizeof(Mpi2SepReply_t));
4113 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
4114 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4119 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
4121 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4124 mutex_unlock(&ioc->base_cmds.mutex);
4129 * _base_get_port_facts - obtain port facts reply and save in ioc
4130 * @ioc: per adapter object
4131 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4133 * Returns 0 for success, non-zero for failure.
4136 _base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port, int sleep_flag)
4138 Mpi2PortFactsRequest_t mpi_request;
4139 Mpi2PortFactsReply_t mpi_reply;
4140 struct mpt3sas_port_facts *pfacts;
4141 int mpi_reply_sz, mpi_request_sz, r;
4143 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4146 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
4147 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
4148 memset(&mpi_request, 0, mpi_request_sz);
4149 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
4150 mpi_request.PortNumber = port;
4151 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
4152 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
4155 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
4156 ioc->name, __func__, r);
4160 pfacts = &ioc->pfacts[port];
4161 memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
4162 pfacts->PortNumber = mpi_reply.PortNumber;
4163 pfacts->VP_ID = mpi_reply.VP_ID;
4164 pfacts->VF_ID = mpi_reply.VF_ID;
4165 pfacts->MaxPostedCmdBuffers =
4166 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
4172 * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
4173 * @ioc: per adapter object
4175 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4177 * Returns 0 for success, non-zero for failure.
4180 _base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout,
4186 dinitprintk(ioc, printk(MPT3SAS_FMT "%s\n", ioc->name,
4189 if (ioc->pci_error_recovery) {
4190 dfailprintk(ioc, printk(MPT3SAS_FMT
4191 "%s: host in pci error recovery\n", ioc->name, __func__));
4195 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4196 dhsprintk(ioc, printk(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
4197 ioc->name, __func__, ioc_state));
4199 if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
4200 (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4203 if (ioc_state & MPI2_DOORBELL_USED) {
4204 dhsprintk(ioc, printk(MPT3SAS_FMT
4205 "unexpected doorbell active!\n", ioc->name));
4206 goto issue_diag_reset;
4209 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4210 mpt3sas_base_fault_info(ioc, ioc_state &
4211 MPI2_DOORBELL_DATA_MASK);
4212 goto issue_diag_reset;
4215 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
4216 timeout, sleep_flag);
4218 dfailprintk(ioc, printk(MPT3SAS_FMT
4219 "%s: failed going to ready state (ioc_state=0x%x)\n",
4220 ioc->name, __func__, ioc_state));
4225 rc = _base_diag_reset(ioc, sleep_flag);
4230 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
4231 * @ioc: per adapter object
4232 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4234 * Returns 0 for success, non-zero for failure.
4237 _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4239 Mpi2IOCFactsRequest_t mpi_request;
4240 Mpi2IOCFactsReply_t mpi_reply;
4241 struct mpt3sas_facts *facts;
4242 int mpi_reply_sz, mpi_request_sz, r;
4244 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4247 r = _base_wait_for_iocstate(ioc, 10, sleep_flag);
4249 dfailprintk(ioc, printk(MPT3SAS_FMT
4250 "%s: failed getting to correct state\n",
4251 ioc->name, __func__));
4254 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
4255 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
4256 memset(&mpi_request, 0, mpi_request_sz);
4257 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
4258 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
4259 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
4262 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
4263 ioc->name, __func__, r);
4267 facts = &ioc->facts;
4268 memset(facts, 0, sizeof(struct mpt3sas_facts));
4269 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
4270 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
4271 facts->VP_ID = mpi_reply.VP_ID;
4272 facts->VF_ID = mpi_reply.VF_ID;
4273 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
4274 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
4275 facts->WhoInit = mpi_reply.WhoInit;
4276 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
4277 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
4278 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
4279 facts->MaxReplyDescriptorPostQueueDepth =
4280 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
4281 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
4282 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
4283 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
4284 ioc->ir_firmware = 1;
4285 if ((facts->IOCCapabilities &
4286 MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE))
4287 ioc->rdpq_array_capable = 1;
4288 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
4289 facts->IOCRequestFrameSize =
4290 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
4291 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
4292 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
4293 ioc->shost->max_id = -1;
4294 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
4295 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
4296 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
4297 facts->HighPriorityCredit =
4298 le16_to_cpu(mpi_reply.HighPriorityCredit);
4299 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
4300 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
4302 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4303 "hba queue depth(%d), max chains per io(%d)\n",
4304 ioc->name, facts->RequestCredit,
4305 facts->MaxChainDepth));
4306 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4307 "request frame size(%d), reply frame size(%d)\n", ioc->name,
4308 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
4313 * _base_send_ioc_init - send ioc_init to firmware
4314 * @ioc: per adapter object
4315 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4317 * Returns 0 for success, non-zero for failure.
4320 _base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4322 Mpi2IOCInitRequest_t mpi_request;
4323 Mpi2IOCInitReply_t mpi_reply;
4325 struct timeval current_time;
4327 u32 reply_post_free_array_sz = 0;
4328 Mpi2IOCInitRDPQArrayEntry *reply_post_free_array = NULL;
4329 dma_addr_t reply_post_free_array_dma;
4331 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4334 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
4335 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
4336 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
4337 mpi_request.VF_ID = 0; /* TODO */
4338 mpi_request.VP_ID = 0;
4339 mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
4340 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
4342 if (_base_is_controller_msix_enabled(ioc))
4343 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
4344 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
4345 mpi_request.ReplyDescriptorPostQueueDepth =
4346 cpu_to_le16(ioc->reply_post_queue_depth);
4347 mpi_request.ReplyFreeQueueDepth =
4348 cpu_to_le16(ioc->reply_free_queue_depth);
4350 mpi_request.SenseBufferAddressHigh =
4351 cpu_to_le32((u64)ioc->sense_dma >> 32);
4352 mpi_request.SystemReplyAddressHigh =
4353 cpu_to_le32((u64)ioc->reply_dma >> 32);
4354 mpi_request.SystemRequestFrameBaseAddress =
4355 cpu_to_le64((u64)ioc->request_dma);
4356 mpi_request.ReplyFreeQueueAddress =
4357 cpu_to_le64((u64)ioc->reply_free_dma);
4359 if (ioc->rdpq_array_enable) {
4360 reply_post_free_array_sz = ioc->reply_queue_count *
4361 sizeof(Mpi2IOCInitRDPQArrayEntry);
4362 reply_post_free_array = pci_alloc_consistent(ioc->pdev,
4363 reply_post_free_array_sz, &reply_post_free_array_dma);
4364 if (!reply_post_free_array) {
4366 "reply_post_free_array: pci_alloc_consistent failed\n",
4371 memset(reply_post_free_array, 0, reply_post_free_array_sz);
4372 for (i = 0; i < ioc->reply_queue_count; i++)
4373 reply_post_free_array[i].RDPQBaseAddress =
4375 (u64)ioc->reply_post[i].reply_post_free_dma);
4376 mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
4377 mpi_request.ReplyDescriptorPostQueueAddress =
4378 cpu_to_le64((u64)reply_post_free_array_dma);
4380 mpi_request.ReplyDescriptorPostQueueAddress =
4381 cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
4384 /* This time stamp specifies number of milliseconds
4385 * since epoch ~ midnight January 1, 1970.
4387 do_gettimeofday(¤t_time);
4388 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
4389 (current_time.tv_usec / 1000));
4391 if (ioc->logging_level & MPT_DEBUG_INIT) {
4395 mfp = (__le32 *)&mpi_request;
4396 pr_info("\toffset:data\n");
4397 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
4398 pr_info("\t[0x%02x]:%08x\n", i*4,
4399 le32_to_cpu(mfp[i]));
4402 r = _base_handshake_req_reply_wait(ioc,
4403 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
4404 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
4408 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
4409 ioc->name, __func__, r);
4413 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4414 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
4415 mpi_reply.IOCLogInfo) {
4416 pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
4421 if (reply_post_free_array)
4422 pci_free_consistent(ioc->pdev, reply_post_free_array_sz,
4423 reply_post_free_array,
4424 reply_post_free_array_dma);
4429 * mpt3sas_port_enable_done - command completion routine for port enable
4430 * @ioc: per adapter object
4431 * @smid: system request message index
4432 * @msix_index: MSIX table index supplied by the OS
4433 * @reply: reply message frame(lower 32bit addr)
4435 * Return 1 meaning mf should be freed from _base_interrupt
4436 * 0 means the mf is freed from this function.
4439 mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
4442 MPI2DefaultReply_t *mpi_reply;
4445 if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
4448 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4452 if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
4455 ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
4456 ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
4457 ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
4458 memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
4459 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
4460 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4461 ioc->port_enable_failed = 1;
4463 if (ioc->is_driver_loading) {
4464 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
4465 mpt3sas_port_enable_complete(ioc);
4468 ioc->start_scan_failed = ioc_status;
4469 ioc->start_scan = 0;
4473 complete(&ioc->port_enable_cmds.done);
4478 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
4479 * @ioc: per adapter object
4480 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4482 * Returns 0 for success, non-zero for failure.
4485 _base_send_port_enable(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4487 Mpi2PortEnableRequest_t *mpi_request;
4488 Mpi2PortEnableReply_t *mpi_reply;
4489 unsigned long timeleft;
4494 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
4496 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
4497 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
4498 ioc->name, __func__);
4502 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
4504 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4505 ioc->name, __func__);
4509 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
4510 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4511 ioc->port_enable_cmds.smid = smid;
4512 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
4513 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
4515 init_completion(&ioc->port_enable_cmds.done);
4516 mpt3sas_base_put_smid_default(ioc, smid);
4517 timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
4519 if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
4520 pr_err(MPT3SAS_FMT "%s: timeout\n",
4521 ioc->name, __func__);
4522 _debug_dump_mf(mpi_request,
4523 sizeof(Mpi2PortEnableRequest_t)/4);
4524 if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
4531 mpi_reply = ioc->port_enable_cmds.reply;
4532 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
4533 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4534 pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
4535 ioc->name, __func__, ioc_status);
4541 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
4542 pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
4543 "SUCCESS" : "FAILED"));
4548 * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
4549 * @ioc: per adapter object
4551 * Returns 0 for success, non-zero for failure.
4554 mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
4556 Mpi2PortEnableRequest_t *mpi_request;
4559 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
4561 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
4562 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
4563 ioc->name, __func__);
4567 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
4569 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4570 ioc->name, __func__);
4574 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
4575 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4576 ioc->port_enable_cmds.smid = smid;
4577 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
4578 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
4580 mpt3sas_base_put_smid_default(ioc, smid);
4585 * _base_determine_wait_on_discovery - desposition
4586 * @ioc: per adapter object
4588 * Decide whether to wait on discovery to complete. Used to either
4589 * locate boot device, or report volumes ahead of physical devices.
4591 * Returns 1 for wait, 0 for don't wait
4594 _base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
4596 /* We wait for discovery to complete if IR firmware is loaded.
4597 * The sas topology events arrive before PD events, so we need time to
4598 * turn on the bit in ioc->pd_handles to indicate PD
4599 * Also, it maybe required to report Volumes ahead of physical
4600 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
4602 if (ioc->ir_firmware)
4605 /* if no Bios, then we don't need to wait */
4606 if (!ioc->bios_pg3.BiosVersion)
4609 /* Bios is present, then we drop down here.
4611 * If there any entries in the Bios Page 2, then we wait
4612 * for discovery to complete.
4615 /* Current Boot Device */
4616 if ((ioc->bios_pg2.CurrentBootDeviceForm &
4617 MPI2_BIOSPAGE2_FORM_MASK) ==
4618 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
4619 /* Request Boot Device */
4620 (ioc->bios_pg2.ReqBootDeviceForm &
4621 MPI2_BIOSPAGE2_FORM_MASK) ==
4622 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
4623 /* Alternate Request Boot Device */
4624 (ioc->bios_pg2.ReqAltBootDeviceForm &
4625 MPI2_BIOSPAGE2_FORM_MASK) ==
4626 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
4633 * _base_unmask_events - turn on notification for this event
4634 * @ioc: per adapter object
4635 * @event: firmware event
4637 * The mask is stored in ioc->event_masks.
4640 _base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
4647 desired_event = (1 << (event % 32));
4650 ioc->event_masks[0] &= ~desired_event;
4651 else if (event < 64)
4652 ioc->event_masks[1] &= ~desired_event;
4653 else if (event < 96)
4654 ioc->event_masks[2] &= ~desired_event;
4655 else if (event < 128)
4656 ioc->event_masks[3] &= ~desired_event;
4660 * _base_event_notification - send event notification
4661 * @ioc: per adapter object
4662 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4664 * Returns 0 for success, non-zero for failure.
4667 _base_event_notification(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4669 Mpi2EventNotificationRequest_t *mpi_request;
4670 unsigned long timeleft;
4675 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4678 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
4679 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
4680 ioc->name, __func__);
4684 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4686 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4687 ioc->name, __func__);
4690 ioc->base_cmds.status = MPT3_CMD_PENDING;
4691 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4692 ioc->base_cmds.smid = smid;
4693 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
4694 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4695 mpi_request->VF_ID = 0; /* TODO */
4696 mpi_request->VP_ID = 0;
4697 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4698 mpi_request->EventMasks[i] =
4699 cpu_to_le32(ioc->event_masks[i]);
4700 init_completion(&ioc->base_cmds.done);
4701 mpt3sas_base_put_smid_default(ioc, smid);
4702 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
4703 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4704 pr_err(MPT3SAS_FMT "%s: timeout\n",
4705 ioc->name, __func__);
4706 _debug_dump_mf(mpi_request,
4707 sizeof(Mpi2EventNotificationRequest_t)/4);
4708 if (ioc->base_cmds.status & MPT3_CMD_RESET)
4713 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
4714 ioc->name, __func__));
4715 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4720 * mpt3sas_base_validate_event_type - validating event types
4721 * @ioc: per adapter object
4722 * @event: firmware event
4724 * This will turn on firmware event notification when application
4725 * ask for that event. We don't mask events that are already enabled.
4728 mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
4731 u32 event_mask, desired_event;
4732 u8 send_update_to_fw;
4734 for (i = 0, send_update_to_fw = 0; i <
4735 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
4736 event_mask = ~event_type[i];
4738 for (j = 0; j < 32; j++) {
4739 if (!(event_mask & desired_event) &&
4740 (ioc->event_masks[i] & desired_event)) {
4741 ioc->event_masks[i] &= ~desired_event;
4742 send_update_to_fw = 1;
4744 desired_event = (desired_event << 1);
4748 if (!send_update_to_fw)
4751 mutex_lock(&ioc->base_cmds.mutex);
4752 _base_event_notification(ioc, CAN_SLEEP);
4753 mutex_unlock(&ioc->base_cmds.mutex);
4757 * _base_diag_reset - the "big hammer" start of day reset
4758 * @ioc: per adapter object
4759 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4761 * Returns 0 for success, non-zero for failure.
4764 _base_diag_reset(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4766 u32 host_diagnostic;
4771 pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
4773 drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
4778 /* Write magic sequence to WriteSequence register
4779 * Loop until in diagnostic mode
4781 drsprintk(ioc, pr_info(MPT3SAS_FMT
4782 "write magic sequence\n", ioc->name));
4783 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4784 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
4785 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
4786 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
4787 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
4788 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
4789 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
4792 if (sleep_flag == CAN_SLEEP)
4800 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4801 drsprintk(ioc, pr_info(MPT3SAS_FMT
4802 "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
4803 ioc->name, count, host_diagnostic));
4805 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
4807 hcb_size = readl(&ioc->chip->HCBSize);
4809 drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
4811 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
4812 &ioc->chip->HostDiagnostic);
4814 /*This delay allows the chip PCIe hardware time to finish reset tasks*/
4815 if (sleep_flag == CAN_SLEEP)
4816 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
4818 mdelay(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
4820 /* Approximately 300 second max wait */
4821 for (count = 0; count < (300000000 /
4822 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
4824 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4826 if (host_diagnostic == 0xFFFFFFFF)
4828 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
4831 /* Wait to pass the second read delay window */
4832 if (sleep_flag == CAN_SLEEP)
4833 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4836 mdelay(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4840 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
4842 drsprintk(ioc, pr_info(MPT3SAS_FMT
4843 "restart the adapter assuming the HCB Address points to good F/W\n",
4845 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
4846 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
4847 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
4849 drsprintk(ioc, pr_info(MPT3SAS_FMT
4850 "re-enable the HCDW\n", ioc->name));
4851 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
4852 &ioc->chip->HCBSize);
4855 drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
4857 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
4858 &ioc->chip->HostDiagnostic);
4860 drsprintk(ioc, pr_info(MPT3SAS_FMT
4861 "disable writes to the diagnostic register\n", ioc->name));
4862 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4864 drsprintk(ioc, pr_info(MPT3SAS_FMT
4865 "Wait for FW to go to the READY state\n", ioc->name));
4866 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
4870 "%s: failed going to ready state (ioc_state=0x%x)\n",
4871 ioc->name, __func__, ioc_state);
4875 pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
4879 pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
4884 * _base_make_ioc_ready - put controller in READY state
4885 * @ioc: per adapter object
4886 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4887 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4889 * Returns 0 for success, non-zero for failure.
4892 _base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
4893 enum reset_type type)
4899 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4902 if (ioc->pci_error_recovery)
4905 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4906 dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
4907 ioc->name, __func__, ioc_state));
4909 /* if in RESET state, it should move to READY state shortly */
4911 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
4912 while ((ioc_state & MPI2_IOC_STATE_MASK) !=
4913 MPI2_IOC_STATE_READY) {
4914 if (count++ == 10) {
4916 "%s: failed going to ready state (ioc_state=0x%x)\n",
4917 ioc->name, __func__, ioc_state);
4920 if (sleep_flag == CAN_SLEEP)
4924 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4928 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
4931 if (ioc_state & MPI2_DOORBELL_USED) {
4932 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4933 "unexpected doorbell active!\n",
4935 goto issue_diag_reset;
4938 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4939 mpt3sas_base_fault_info(ioc, ioc_state &
4940 MPI2_DOORBELL_DATA_MASK);
4941 goto issue_diag_reset;
4944 if (type == FORCE_BIG_HAMMER)
4945 goto issue_diag_reset;
4947 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4948 if (!(_base_send_ioc_reset(ioc,
4949 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
4954 rc = _base_diag_reset(ioc, CAN_SLEEP);
4959 * _base_make_ioc_operational - put controller in OPERATIONAL state
4960 * @ioc: per adapter object
4961 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4963 * Returns 0 for success, non-zero for failure.
4966 _base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4969 unsigned long flags;
4972 struct _tr_list *delayed_tr, *delayed_tr_next;
4974 struct adapter_reply_queue *reply_q;
4975 long reply_post_free;
4976 u32 reply_post_free_sz, index = 0;
4978 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4981 /* clean the delayed target reset list */
4982 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4983 &ioc->delayed_tr_list, list) {
4984 list_del(&delayed_tr->list);
4989 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4990 &ioc->delayed_tr_volume_list, list) {
4991 list_del(&delayed_tr->list);
4995 /* initialize the scsi lookup free list */
4996 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4997 INIT_LIST_HEAD(&ioc->free_list);
4999 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
5000 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
5001 ioc->scsi_lookup[i].cb_idx = 0xFF;
5002 ioc->scsi_lookup[i].smid = smid;
5003 ioc->scsi_lookup[i].scmd = NULL;
5004 ioc->scsi_lookup[i].direct_io = 0;
5005 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
5009 /* hi-priority queue */
5010 INIT_LIST_HEAD(&ioc->hpr_free_list);
5011 smid = ioc->hi_priority_smid;
5012 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
5013 ioc->hpr_lookup[i].cb_idx = 0xFF;
5014 ioc->hpr_lookup[i].smid = smid;
5015 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
5016 &ioc->hpr_free_list);
5019 /* internal queue */
5020 INIT_LIST_HEAD(&ioc->internal_free_list);
5021 smid = ioc->internal_smid;
5022 for (i = 0; i < ioc->internal_depth; i++, smid++) {
5023 ioc->internal_lookup[i].cb_idx = 0xFF;
5024 ioc->internal_lookup[i].smid = smid;
5025 list_add_tail(&ioc->internal_lookup[i].tracker_list,
5026 &ioc->internal_free_list);
5030 INIT_LIST_HEAD(&ioc->free_chain_list);
5031 for (i = 0; i < ioc->chain_depth; i++)
5032 list_add_tail(&ioc->chain_lookup[i].tracker_list,
5033 &ioc->free_chain_list);
5035 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5037 /* initialize Reply Free Queue */
5038 for (i = 0, reply_address = (u32)ioc->reply_dma ;
5039 i < ioc->reply_free_queue_depth ; i++, reply_address +=
5041 ioc->reply_free[i] = cpu_to_le32(reply_address);
5043 /* initialize reply queues */
5044 if (ioc->is_driver_loading)
5045 _base_assign_reply_queues(ioc);
5047 /* initialize Reply Post Free Queue */
5048 reply_post_free_sz = ioc->reply_post_queue_depth *
5049 sizeof(Mpi2DefaultReplyDescriptor_t);
5050 reply_post_free = (long)ioc->reply_post[index].reply_post_free;
5051 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
5052 reply_q->reply_post_host_index = 0;
5053 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
5055 for (i = 0; i < ioc->reply_post_queue_depth; i++)
5056 reply_q->reply_post_free[i].Words =
5057 cpu_to_le64(ULLONG_MAX);
5058 if (!_base_is_controller_msix_enabled(ioc))
5059 goto skip_init_reply_post_free_queue;
5061 * If RDPQ is enabled, switch to the next allocation.
5062 * Otherwise advance within the contiguous region.
5064 if (ioc->rdpq_array_enable)
5065 reply_post_free = (long)
5066 ioc->reply_post[++index].reply_post_free;
5068 reply_post_free += reply_post_free_sz;
5070 skip_init_reply_post_free_queue:
5072 r = _base_send_ioc_init(ioc, sleep_flag);
5076 /* initialize reply free host index */
5077 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
5078 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
5080 /* initialize reply post host index */
5081 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
5082 if (ioc->msix96_vector)
5083 writel((reply_q->msix_index & 7)<<
5084 MPI2_RPHI_MSIX_INDEX_SHIFT,
5085 ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
5087 writel(reply_q->msix_index <<
5088 MPI2_RPHI_MSIX_INDEX_SHIFT,
5089 &ioc->chip->ReplyPostHostIndex);
5091 if (!_base_is_controller_msix_enabled(ioc))
5092 goto skip_init_reply_post_host_index;
5095 skip_init_reply_post_host_index:
5097 _base_unmask_interrupts(ioc);
5098 r = _base_event_notification(ioc, sleep_flag);
5102 if (sleep_flag == CAN_SLEEP)
5103 _base_static_config_pages(ioc);
5106 if (ioc->is_driver_loading) {
5108 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
5111 le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
5112 MFG_PAGE10_HIDE_SSDS_MASK);
5113 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
5114 ioc->mfg_pg10_hide_flag = hide_flag;
5117 ioc->wait_for_discovery_to_complete =
5118 _base_determine_wait_on_discovery(ioc);
5120 return r; /* scan_start and scan_finished support */
5123 r = _base_send_port_enable(ioc, sleep_flag);
5131 * mpt3sas_base_free_resources - free resources controller resources
5132 * @ioc: per adapter object
5137 mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
5139 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5142 /* synchronizing freeing resource with pci_access_mutex lock */
5143 mutex_lock(&ioc->pci_access_mutex);
5144 if (ioc->chip_phys && ioc->chip) {
5145 _base_mask_interrupts(ioc);
5146 ioc->shost_recovery = 1;
5147 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
5148 ioc->shost_recovery = 0;
5151 mpt3sas_base_unmap_resources(ioc);
5152 mutex_unlock(&ioc->pci_access_mutex);
5157 * mpt3sas_base_attach - attach controller instance
5158 * @ioc: per adapter object
5160 * Returns 0 for success, non-zero for failure.
5163 mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
5166 int cpu_id, last_cpu_id = 0;
5168 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5171 /* setup cpu_msix_table */
5172 ioc->cpu_count = num_online_cpus();
5173 for_each_online_cpu(cpu_id)
5174 last_cpu_id = cpu_id;
5175 ioc->cpu_msix_table_sz = last_cpu_id + 1;
5176 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
5177 ioc->reply_queue_count = 1;
5178 if (!ioc->cpu_msix_table) {
5179 dfailprintk(ioc, pr_info(MPT3SAS_FMT
5180 "allocation for cpu_msix_table failed!!!\n",
5183 goto out_free_resources;
5186 if (ioc->is_warpdrive) {
5187 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
5188 sizeof(resource_size_t *), GFP_KERNEL);
5189 if (!ioc->reply_post_host_index) {
5190 dfailprintk(ioc, pr_info(MPT3SAS_FMT "allocation "
5191 "for cpu_msix_table failed!!!\n", ioc->name));
5193 goto out_free_resources;
5197 ioc->rdpq_array_enable_assigned = 0;
5199 r = mpt3sas_base_map_resources(ioc);
5201 goto out_free_resources;
5203 if (ioc->is_warpdrive) {
5204 ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
5205 &ioc->chip->ReplyPostHostIndex;
5207 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
5208 ioc->reply_post_host_index[i] =
5209 (resource_size_t __iomem *)
5210 ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
5214 pci_set_drvdata(ioc->pdev, ioc->shost);
5215 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
5217 goto out_free_resources;
5219 switch (ioc->hba_mpi_version_belonged) {
5221 ioc->build_sg_scmd = &_base_build_sg_scmd;
5222 ioc->build_sg = &_base_build_sg;
5223 ioc->build_zero_len_sge = &_base_build_zero_len_sge;
5228 * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
5229 * Target Status - all require the IEEE formated scatter gather
5232 ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
5233 ioc->build_sg = &_base_build_sg_ieee;
5234 ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
5235 ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
5240 * These function pointers for other requests that don't
5241 * the require IEEE scatter gather elements.
5243 * For example Configuration Pages and SAS IOUNIT Control don't.
5245 ioc->build_sg_mpi = &_base_build_sg;
5246 ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
5248 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
5250 goto out_free_resources;
5252 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
5253 sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
5256 goto out_free_resources;
5259 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
5260 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
5262 goto out_free_resources;
5265 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
5267 goto out_free_resources;
5269 init_waitqueue_head(&ioc->reset_wq);
5271 /* allocate memory pd handle bitmask list */
5272 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
5273 if (ioc->facts.MaxDevHandle % 8)
5274 ioc->pd_handles_sz++;
5275 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
5277 if (!ioc->pd_handles) {
5279 goto out_free_resources;
5281 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
5283 if (!ioc->blocking_handles) {
5285 goto out_free_resources;
5288 ioc->fwfault_debug = mpt3sas_fwfault_debug;
5290 /* base internal command bits */
5291 mutex_init(&ioc->base_cmds.mutex);
5292 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5293 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5295 /* port_enable command bits */
5296 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5297 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
5299 /* transport internal command bits */
5300 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5301 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
5302 mutex_init(&ioc->transport_cmds.mutex);
5304 /* scsih internal command bits */
5305 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5306 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
5307 mutex_init(&ioc->scsih_cmds.mutex);
5309 /* task management internal command bits */
5310 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5311 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
5312 mutex_init(&ioc->tm_cmds.mutex);
5314 /* config page internal command bits */
5315 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5316 ioc->config_cmds.status = MPT3_CMD_NOT_USED;
5317 mutex_init(&ioc->config_cmds.mutex);
5319 /* ctl module internal command bits */
5320 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
5321 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
5322 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
5323 mutex_init(&ioc->ctl_cmds.mutex);
5325 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
5326 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
5327 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
5328 !ioc->ctl_cmds.sense) {
5330 goto out_free_resources;
5333 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
5334 ioc->event_masks[i] = -1;
5336 /* here we enable the events we care about */
5337 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
5338 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
5339 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
5340 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
5341 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
5342 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
5343 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
5344 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
5345 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
5346 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
5347 _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
5349 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
5351 goto out_free_resources;
5353 ioc->non_operational_loop = 0;
5358 ioc->remove_host = 1;
5360 mpt3sas_base_free_resources(ioc);
5361 _base_release_memory_pools(ioc);
5362 pci_set_drvdata(ioc->pdev, NULL);
5363 kfree(ioc->cpu_msix_table);
5364 if (ioc->is_warpdrive)
5365 kfree(ioc->reply_post_host_index);
5366 kfree(ioc->pd_handles);
5367 kfree(ioc->blocking_handles);
5368 kfree(ioc->tm_cmds.reply);
5369 kfree(ioc->transport_cmds.reply);
5370 kfree(ioc->scsih_cmds.reply);
5371 kfree(ioc->config_cmds.reply);
5372 kfree(ioc->base_cmds.reply);
5373 kfree(ioc->port_enable_cmds.reply);
5374 kfree(ioc->ctl_cmds.reply);
5375 kfree(ioc->ctl_cmds.sense);
5377 ioc->ctl_cmds.reply = NULL;
5378 ioc->base_cmds.reply = NULL;
5379 ioc->tm_cmds.reply = NULL;
5380 ioc->scsih_cmds.reply = NULL;
5381 ioc->transport_cmds.reply = NULL;
5382 ioc->config_cmds.reply = NULL;
5389 * mpt3sas_base_detach - remove controller instance
5390 * @ioc: per adapter object
5395 mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
5397 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5400 mpt3sas_base_stop_watchdog(ioc);
5401 mpt3sas_base_free_resources(ioc);
5402 _base_release_memory_pools(ioc);
5403 pci_set_drvdata(ioc->pdev, NULL);
5404 kfree(ioc->cpu_msix_table);
5405 if (ioc->is_warpdrive)
5406 kfree(ioc->reply_post_host_index);
5407 kfree(ioc->pd_handles);
5408 kfree(ioc->blocking_handles);
5410 kfree(ioc->ctl_cmds.reply);
5411 kfree(ioc->ctl_cmds.sense);
5412 kfree(ioc->base_cmds.reply);
5413 kfree(ioc->port_enable_cmds.reply);
5414 kfree(ioc->tm_cmds.reply);
5415 kfree(ioc->transport_cmds.reply);
5416 kfree(ioc->scsih_cmds.reply);
5417 kfree(ioc->config_cmds.reply);
5421 * _base_reset_handler - reset callback handler (for base)
5422 * @ioc: per adapter object
5423 * @reset_phase: phase
5425 * The handler for doing any required cleanup or initialization.
5427 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
5428 * MPT3_IOC_DONE_RESET
5433 _base_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
5435 mpt3sas_scsih_reset_handler(ioc, reset_phase);
5436 mpt3sas_ctl_reset_handler(ioc, reset_phase);
5437 switch (reset_phase) {
5438 case MPT3_IOC_PRE_RESET:
5439 dtmprintk(ioc, pr_info(MPT3SAS_FMT
5440 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
5442 case MPT3_IOC_AFTER_RESET:
5443 dtmprintk(ioc, pr_info(MPT3SAS_FMT
5444 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
5445 if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
5446 ioc->transport_cmds.status |= MPT3_CMD_RESET;
5447 mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
5448 complete(&ioc->transport_cmds.done);
5450 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
5451 ioc->base_cmds.status |= MPT3_CMD_RESET;
5452 mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
5453 complete(&ioc->base_cmds.done);
5455 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
5456 ioc->port_enable_failed = 1;
5457 ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
5458 mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
5459 if (ioc->is_driver_loading) {
5460 ioc->start_scan_failed =
5461 MPI2_IOCSTATUS_INTERNAL_ERROR;
5462 ioc->start_scan = 0;
5463 ioc->port_enable_cmds.status =
5466 complete(&ioc->port_enable_cmds.done);
5468 if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
5469 ioc->config_cmds.status |= MPT3_CMD_RESET;
5470 mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
5471 ioc->config_cmds.smid = USHRT_MAX;
5472 complete(&ioc->config_cmds.done);
5475 case MPT3_IOC_DONE_RESET:
5476 dtmprintk(ioc, pr_info(MPT3SAS_FMT
5477 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
5483 * _wait_for_commands_to_complete - reset controller
5484 * @ioc: Pointer to MPT_ADAPTER structure
5485 * @sleep_flag: CAN_SLEEP or NO_SLEEP
5487 * This function waiting(3s) for all pending commands to complete
5488 * prior to putting controller in reset.
5491 _wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
5494 unsigned long flags;
5497 ioc->pending_io_count = 0;
5498 if (sleep_flag != CAN_SLEEP)
5501 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5502 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
5505 /* pending command count */
5506 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5507 for (i = 0; i < ioc->scsiio_depth; i++)
5508 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
5509 ioc->pending_io_count++;
5510 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5512 if (!ioc->pending_io_count)
5515 /* wait for pending commands to complete */
5516 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
5520 * mpt3sas_base_hard_reset_handler - reset controller
5521 * @ioc: Pointer to MPT_ADAPTER structure
5522 * @sleep_flag: CAN_SLEEP or NO_SLEEP
5523 * @type: FORCE_BIG_HAMMER or SOFT_RESET
5525 * Returns 0 for success, non-zero for failure.
5528 mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
5529 enum reset_type type)
5532 unsigned long flags;
5534 u8 is_fault = 0, is_trigger = 0;
5536 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
5539 if (ioc->pci_error_recovery) {
5540 pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
5541 ioc->name, __func__);
5546 if (mpt3sas_fwfault_debug)
5547 mpt3sas_halt_firmware(ioc);
5549 /* TODO - What we really should be doing is pulling
5550 * out all the code associated with NO_SLEEP; its never used.
5551 * That is legacy code from mpt fusion driver, ported over.
5552 * I will leave this BUG_ON here for now till its been resolved.
5554 BUG_ON(sleep_flag == NO_SLEEP);
5556 /* wait for an active reset in progress to complete */
5557 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
5560 } while (ioc->shost_recovery == 1);
5561 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
5563 return ioc->ioc_reset_in_progress_status;
5566 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5567 ioc->shost_recovery = 1;
5568 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5570 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
5571 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
5572 (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
5573 MPT3_DIAG_BUFFER_IS_RELEASED))) {
5575 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5576 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
5579 _base_reset_handler(ioc, MPT3_IOC_PRE_RESET);
5580 _wait_for_commands_to_complete(ioc, sleep_flag);
5581 _base_mask_interrupts(ioc);
5582 r = _base_make_ioc_ready(ioc, sleep_flag, type);
5585 _base_reset_handler(ioc, MPT3_IOC_AFTER_RESET);
5587 /* If this hard reset is called while port enable is active, then
5588 * there is no reason to call make_ioc_operational
5590 if (ioc->is_driver_loading && ioc->port_enable_failed) {
5591 ioc->remove_host = 1;
5595 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
5599 if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
5600 panic("%s: Issue occurred with flashing controller firmware."
5601 "Please reboot the system and ensure that the correct"
5602 " firmware version is running\n", ioc->name);
5604 r = _base_make_ioc_operational(ioc, sleep_flag);
5606 _base_reset_handler(ioc, MPT3_IOC_DONE_RESET);
5609 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
5610 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
5612 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5613 ioc->ioc_reset_in_progress_status = r;
5614 ioc->shost_recovery = 0;
5615 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5616 ioc->ioc_reset_count++;
5617 mutex_unlock(&ioc->reset_in_progress_mutex);
5620 if ((r == 0) && is_trigger) {
5622 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
5624 mpt3sas_trigger_master(ioc,
5625 MASTER_TRIGGER_ADAPTER_RESET);
5627 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,