2 * This is the Fusion MPT base driver providing common API layer interface
3 * for access to MPT (Message Passing Technology) firmware.
5 * This code is based on drivers/scsi/mpt2sas/mpt2_base.c
6 * Copyright (C) 2007-2014 LSI Corporation
7 * Copyright (C) 20013-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>
58 #include <linux/sort.h>
60 #include <linux/time.h>
61 #include <linux/kthread.h>
62 #include <linux/aer.h>
64 #include "mpt2sas_base.h"
66 static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
68 #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
70 #define MAX_HBA_QUEUE_DEPTH 30000
71 #define MAX_CHAIN_DEPTH 100000
72 static int max_queue_depth = -1;
73 module_param(max_queue_depth, int, 0);
74 MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
76 static int max_sgl_entries = -1;
77 module_param(max_sgl_entries, int, 0);
78 MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
80 static int msix_disable = -1;
81 module_param(msix_disable, int, 0);
82 MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
84 static int max_msix_vectors = -1;
85 module_param(max_msix_vectors, int, 0);
86 MODULE_PARM_DESC(max_msix_vectors, " max msix vectors ");
88 static int mpt2sas_fwfault_debug;
89 MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
90 "and halt firmware - (default=0)");
92 static int disable_discovery = -1;
93 module_param(disable_discovery, int, 0);
94 MODULE_PARM_DESC(disable_discovery, " disable discovery ");
97 _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag);
100 _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag);
103 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
107 _scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
109 int ret = param_set_int(val, kp);
110 struct MPT2SAS_ADAPTER *ioc;
115 printk(KERN_INFO "setting fwfault_debug(%d)\n", mpt2sas_fwfault_debug);
116 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
117 ioc->fwfault_debug = mpt2sas_fwfault_debug;
121 module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
122 param_get_int, &mpt2sas_fwfault_debug, 0644);
125 * mpt2sas_remove_dead_ioc_func - kthread context to remove dead ioc
126 * @arg: input argument, used to derive ioc
128 * Return 0 if controller is removed from pci subsystem.
129 * Return -1 for other case.
131 static int mpt2sas_remove_dead_ioc_func(void *arg)
133 struct MPT2SAS_ADAPTER *ioc = (struct MPT2SAS_ADAPTER *)arg;
134 struct pci_dev *pdev;
142 pci_stop_and_remove_bus_device_locked(pdev);
148 * _base_fault_reset_work - workq handling ioc fault conditions
149 * @work: input argument, used to derive ioc
155 _base_fault_reset_work(struct work_struct *work)
157 struct MPT2SAS_ADAPTER *ioc =
158 container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
162 struct task_struct *p;
164 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
165 if (ioc->shost_recovery || ioc->pci_error_recovery)
167 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
169 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
170 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
171 printk(MPT2SAS_INFO_FMT "%s : SAS host is non-operational !!!!\n",
172 ioc->name, __func__);
174 /* It may be possible that EEH recovery can resolve some of
175 * pci bus failure issues rather removing the dead ioc function
176 * by considering controller is in a non-operational state. So
177 * here priority is given to the EEH recovery. If it doesn't
178 * not resolve this issue, mpt2sas driver will consider this
179 * controller to non-operational state and remove the dead ioc
182 if (ioc->non_operational_loop++ < 5) {
183 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
189 * Call _scsih_flush_pending_cmds callback so that we flush all
190 * pending commands back to OS. This call is required to aovid
191 * deadlock at block layer. Dead IOC will fail to do diag reset,
192 * and this call is safe since dead ioc will never return any
193 * command back from HW.
195 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
197 * Set remove_host flag early since kernel thread will
198 * take some time to execute.
200 ioc->remove_host = 1;
201 /*Remove the Dead Host */
202 p = kthread_run(mpt2sas_remove_dead_ioc_func, ioc,
203 "mpt2sas_dead_ioc_%d", ioc->id);
205 printk(MPT2SAS_ERR_FMT
206 "%s: Running mpt2sas_dead_ioc thread failed !!!!\n",
207 ioc->name, __func__);
209 printk(MPT2SAS_ERR_FMT
210 "%s: Running mpt2sas_dead_ioc thread success !!!!\n",
211 ioc->name, __func__);
214 return; /* don't rearm timer */
217 ioc->non_operational_loop = 0;
219 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
220 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
222 printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
223 __func__, (rc == 0) ? "success" : "failed");
224 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
225 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
226 mpt2sas_base_fault_info(ioc, doorbell &
227 MPI2_DOORBELL_DATA_MASK);
230 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
232 if (ioc->fault_reset_work_q)
233 queue_delayed_work(ioc->fault_reset_work_q,
234 &ioc->fault_reset_work,
235 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
236 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
240 * mpt2sas_base_start_watchdog - start the fault_reset_work_q
241 * @ioc: per adapter object
247 mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
251 if (ioc->fault_reset_work_q)
254 /* 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 printk(MPT2SAS_ERR_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 * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
275 * @ioc: per adapter object
281 mpt2sas_base_stop_watchdog(struct MPT2SAS_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 * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
299 * @ioc: per adapter object
300 * @fault_code: fault code
305 mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
307 printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
308 ioc->name, fault_code);
312 * mpt2sas_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 mpt2sas_halt_firmware(struct MPT2SAS_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 mpt2sas_base_fault_info(ioc , doorbell);
334 writel(0xC0FFEE00, &ioc->chip->Doorbell);
335 printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
336 "timeout\n", ioc->name);
339 panic("panic in %s\n", __func__);
342 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
344 * _base_sas_ioc_info - verbose translation of the ioc status
345 * @ioc: per adapter object
346 * @mpi_reply: reply mf payload returned from firmware
347 * @request_hdr: request mf
352 _base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
353 MPI2RequestHeader_t *request_hdr)
355 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
359 char *func_str = NULL;
361 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
362 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
363 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
364 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
367 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
370 switch (ioc_status) {
372 /****************************************************************************
373 * Common IOCStatus values for all replies
374 ****************************************************************************/
376 case MPI2_IOCSTATUS_INVALID_FUNCTION:
377 desc = "invalid function";
379 case MPI2_IOCSTATUS_BUSY:
382 case MPI2_IOCSTATUS_INVALID_SGL:
383 desc = "invalid sgl";
385 case MPI2_IOCSTATUS_INTERNAL_ERROR:
386 desc = "internal error";
388 case MPI2_IOCSTATUS_INVALID_VPID:
389 desc = "invalid vpid";
391 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
392 desc = "insufficient resources";
394 case MPI2_IOCSTATUS_INVALID_FIELD:
395 desc = "invalid field";
397 case MPI2_IOCSTATUS_INVALID_STATE:
398 desc = "invalid state";
400 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
401 desc = "op state not supported";
404 /****************************************************************************
405 * Config IOCStatus values
406 ****************************************************************************/
408 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
409 desc = "config invalid action";
411 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
412 desc = "config invalid type";
414 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
415 desc = "config invalid page";
417 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
418 desc = "config invalid data";
420 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
421 desc = "config no defaults";
423 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
424 desc = "config cant commit";
427 /****************************************************************************
429 ****************************************************************************/
431 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
432 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
433 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
434 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
435 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
436 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
437 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
438 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
439 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
440 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
441 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
442 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
445 /****************************************************************************
446 * For use by SCSI Initiator and SCSI Target end-to-end data protection
447 ****************************************************************************/
449 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
450 desc = "eedp guard error";
452 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
453 desc = "eedp ref tag error";
455 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
456 desc = "eedp app tag error";
459 /****************************************************************************
461 ****************************************************************************/
463 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
464 desc = "target invalid io index";
466 case MPI2_IOCSTATUS_TARGET_ABORTED:
467 desc = "target aborted";
469 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
470 desc = "target no conn retryable";
472 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
473 desc = "target no connection";
475 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
476 desc = "target xfer count mismatch";
478 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
479 desc = "target data offset error";
481 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
482 desc = "target too much write data";
484 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
485 desc = "target iu too short";
487 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
488 desc = "target ack nak timeout";
490 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
491 desc = "target nak received";
494 /****************************************************************************
495 * Serial Attached SCSI values
496 ****************************************************************************/
498 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
499 desc = "smp request failed";
501 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
502 desc = "smp data overrun";
505 /****************************************************************************
506 * Diagnostic Buffer Post / Diagnostic Release values
507 ****************************************************************************/
509 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
510 desc = "diagnostic released";
519 switch (request_hdr->Function) {
520 case MPI2_FUNCTION_CONFIG:
521 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
522 func_str = "config_page";
524 case MPI2_FUNCTION_SCSI_TASK_MGMT:
525 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
526 func_str = "task_mgmt";
528 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
529 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
530 func_str = "sas_iounit_ctl";
532 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
533 frame_sz = sizeof(Mpi2SepRequest_t);
534 func_str = "enclosure";
536 case MPI2_FUNCTION_IOC_INIT:
537 frame_sz = sizeof(Mpi2IOCInitRequest_t);
538 func_str = "ioc_init";
540 case MPI2_FUNCTION_PORT_ENABLE:
541 frame_sz = sizeof(Mpi2PortEnableRequest_t);
542 func_str = "port_enable";
544 case MPI2_FUNCTION_SMP_PASSTHROUGH:
545 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
546 func_str = "smp_passthru";
550 func_str = "unknown";
554 printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
555 " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
557 _debug_dump_mf(request_hdr, frame_sz/4);
561 * _base_display_event_data - verbose translation of firmware asyn events
562 * @ioc: per adapter object
563 * @mpi_reply: reply mf payload returned from firmware
568 _base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
569 Mpi2EventNotificationReply_t *mpi_reply)
574 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
577 event = le16_to_cpu(mpi_reply->Event);
580 case MPI2_EVENT_LOG_DATA:
583 case MPI2_EVENT_STATE_CHANGE:
584 desc = "Status Change";
586 case MPI2_EVENT_HARD_RESET_RECEIVED:
587 desc = "Hard Reset Received";
589 case MPI2_EVENT_EVENT_CHANGE:
590 desc = "Event Change";
592 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
593 desc = "Device Status Change";
595 case MPI2_EVENT_IR_OPERATION_STATUS:
596 if (!ioc->hide_ir_msg)
597 desc = "IR Operation Status";
599 case MPI2_EVENT_SAS_DISCOVERY:
601 Mpi2EventDataSasDiscovery_t *event_data =
602 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
603 printk(MPT2SAS_INFO_FMT "Discovery: (%s)", ioc->name,
604 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
606 if (event_data->DiscoveryStatus)
607 printk("discovery_status(0x%08x)",
608 le32_to_cpu(event_data->DiscoveryStatus));
612 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
613 desc = "SAS Broadcast Primitive";
615 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
616 desc = "SAS Init Device Status Change";
618 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
619 desc = "SAS Init Table Overflow";
621 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
622 desc = "SAS Topology Change List";
624 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
625 desc = "SAS Enclosure Device Status Change";
627 case MPI2_EVENT_IR_VOLUME:
628 if (!ioc->hide_ir_msg)
631 case MPI2_EVENT_IR_PHYSICAL_DISK:
632 if (!ioc->hide_ir_msg)
633 desc = "IR Physical Disk";
635 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
636 if (!ioc->hide_ir_msg)
637 desc = "IR Configuration Change List";
639 case MPI2_EVENT_LOG_ENTRY_ADDED:
640 if (!ioc->hide_ir_msg)
641 desc = "Log Entry Added";
643 case MPI2_EVENT_TEMP_THRESHOLD:
644 desc = "Temperature Threshold";
651 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
656 * _base_sas_log_info - verbose translation of firmware log info
657 * @ioc: per adapter object
658 * @log_info: log info
663 _base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
674 union loginfo_type sas_loginfo;
675 char *originator_str = NULL;
677 sas_loginfo.loginfo = log_info;
678 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
681 /* each nexus loss loginfo */
682 if (log_info == 0x31170000)
685 /* eat the loginfos associated with task aborts */
686 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
687 0x31140000 || log_info == 0x31130000))
690 switch (sas_loginfo.dw.originator) {
692 originator_str = "IOP";
695 originator_str = "PL";
698 if (!ioc->hide_ir_msg)
699 originator_str = "IR";
701 originator_str = "WarpDrive";
705 printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
706 "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
707 originator_str, sas_loginfo.dw.code,
708 sas_loginfo.dw.subcode);
712 * _base_display_reply_info -
713 * @ioc: per adapter object
714 * @smid: system request message index
715 * @msix_index: MSIX table index supplied by the OS
716 * @reply: reply message frame(lower 32bit addr)
721 _base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
724 MPI2DefaultReply_t *mpi_reply;
727 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
728 if (unlikely(!mpi_reply)) {
729 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
730 ioc->name, __FILE__, __LINE__, __func__);
733 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
734 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
735 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
736 (ioc->logging_level & MPT_DEBUG_REPLY)) {
737 _base_sas_ioc_info(ioc , mpi_reply,
738 mpt2sas_base_get_msg_frame(ioc, smid));
741 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
742 _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
746 * mpt2sas_base_done - base internal command completion routine
747 * @ioc: per adapter object
748 * @smid: system request message index
749 * @msix_index: MSIX table index supplied by the OS
750 * @reply: reply message frame(lower 32bit addr)
752 * Return 1 meaning mf should be freed from _base_interrupt
753 * 0 means the mf is freed from this function.
756 mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
759 MPI2DefaultReply_t *mpi_reply;
761 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
762 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
765 if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
768 ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
770 ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
771 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
773 ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
775 complete(&ioc->base_cmds.done);
780 * _base_async_event - main callback handler for firmware asyn events
781 * @ioc: per adapter object
782 * @msix_index: MSIX table index supplied by the OS
783 * @reply: reply message frame(lower 32bit addr)
788 _base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
790 Mpi2EventNotificationReply_t *mpi_reply;
791 Mpi2EventAckRequest_t *ack_request;
794 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
797 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
799 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
800 _base_display_event_data(ioc, mpi_reply);
802 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
804 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
806 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
807 ioc->name, __func__);
811 ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
812 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
813 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
814 ack_request->Event = mpi_reply->Event;
815 ack_request->EventContext = mpi_reply->EventContext;
816 ack_request->VF_ID = 0; /* TODO */
817 ack_request->VP_ID = 0;
818 mpt2sas_base_put_smid_default(ioc, smid);
822 /* scsih callback handler */
823 mpt2sas_scsih_event_callback(ioc, msix_index, reply);
825 /* ctl callback handler */
826 mpt2sas_ctl_event_callback(ioc, msix_index, reply);
832 * _base_get_cb_idx - obtain the callback index
833 * @ioc: per adapter object
834 * @smid: system request message index
836 * Return callback index.
839 _base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
844 if (smid < ioc->hi_priority_smid) {
846 cb_idx = ioc->scsi_lookup[i].cb_idx;
847 } else if (smid < ioc->internal_smid) {
848 i = smid - ioc->hi_priority_smid;
849 cb_idx = ioc->hpr_lookup[i].cb_idx;
850 } else if (smid <= ioc->hba_queue_depth) {
851 i = smid - ioc->internal_smid;
852 cb_idx = ioc->internal_lookup[i].cb_idx;
859 * _base_mask_interrupts - disable interrupts
860 * @ioc: per adapter object
862 * Disabling ResetIRQ, Reply and Doorbell Interrupts
867 _base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
871 ioc->mask_interrupts = 1;
872 him_register = readl(&ioc->chip->HostInterruptMask);
873 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
874 writel(him_register, &ioc->chip->HostInterruptMask);
875 readl(&ioc->chip->HostInterruptMask);
879 * _base_unmask_interrupts - enable interrupts
880 * @ioc: per adapter object
882 * Enabling only Reply Interrupts
887 _base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
891 him_register = readl(&ioc->chip->HostInterruptMask);
892 him_register &= ~MPI2_HIM_RIM;
893 writel(him_register, &ioc->chip->HostInterruptMask);
894 ioc->mask_interrupts = 0;
897 union reply_descriptor {
906 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
907 * @irq: irq number (not used)
908 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
909 * @r: pt_regs pointer (not used)
911 * Return IRQ_HANDLE if processed, else IRQ_NONE.
914 _base_interrupt(int irq, void *bus_id)
916 struct adapter_reply_queue *reply_q = bus_id;
917 union reply_descriptor rd;
919 u8 request_desript_type;
923 u8 msix_index = reply_q->msix_index;
924 struct MPT2SAS_ADAPTER *ioc = reply_q->ioc;
925 Mpi2ReplyDescriptorsUnion_t *rpf;
928 if (ioc->mask_interrupts)
931 if (!atomic_add_unless(&reply_q->busy, 1, 1))
934 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
935 request_desript_type = rpf->Default.ReplyFlags
936 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
937 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
938 atomic_dec(&reply_q->busy);
945 rd.word = le64_to_cpu(rpf->Words);
946 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
949 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
950 if (request_desript_type ==
951 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
953 (rpf->AddressReply.ReplyFrameAddress);
954 if (reply > ioc->reply_dma_max_address ||
955 reply < ioc->reply_dma_min_address)
957 } else if (request_desript_type ==
958 MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
960 else if (request_desript_type ==
961 MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
964 cb_idx = _base_get_cb_idx(ioc, smid);
965 if ((likely(cb_idx < MPT_MAX_CALLBACKS))
966 && (likely(mpt_callbacks[cb_idx] != NULL))) {
967 rc = mpt_callbacks[cb_idx](ioc, smid,
970 _base_display_reply_info(ioc, smid,
973 mpt2sas_base_free_smid(ioc, smid);
977 _base_async_event(ioc, msix_index, reply);
979 /* reply free queue handling */
981 ioc->reply_free_host_index =
982 (ioc->reply_free_host_index ==
983 (ioc->reply_free_queue_depth - 1)) ?
984 0 : ioc->reply_free_host_index + 1;
985 ioc->reply_free[ioc->reply_free_host_index] =
988 writel(ioc->reply_free_host_index,
989 &ioc->chip->ReplyFreeHostIndex);
994 rpf->Words = cpu_to_le64(ULLONG_MAX);
995 reply_q->reply_post_host_index =
996 (reply_q->reply_post_host_index ==
997 (ioc->reply_post_queue_depth - 1)) ? 0 :
998 reply_q->reply_post_host_index + 1;
999 request_desript_type =
1000 reply_q->reply_post_free[reply_q->reply_post_host_index].
1001 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1003 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1005 if (!reply_q->reply_post_host_index)
1006 rpf = reply_q->reply_post_free;
1013 if (!completed_cmds) {
1014 atomic_dec(&reply_q->busy);
1018 if (ioc->is_warpdrive) {
1019 writel(reply_q->reply_post_host_index,
1020 ioc->reply_post_host_index[msix_index]);
1021 atomic_dec(&reply_q->busy);
1024 writel(reply_q->reply_post_host_index | (msix_index <<
1025 MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
1026 atomic_dec(&reply_q->busy);
1031 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1032 * @ioc: per adapter object
1036 _base_is_controller_msix_enabled(struct MPT2SAS_ADAPTER *ioc)
1038 return (ioc->facts.IOCCapabilities &
1039 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1043 * mpt2sas_base_flush_reply_queues - flushing the MSIX reply queues
1044 * @ioc: per adapter object
1045 * Context: ISR conext
1047 * Called when a Task Management request has completed. We want
1048 * to flush the other reply queues so all the outstanding IO has been
1049 * completed back to OS before we process the TM completetion.
1054 mpt2sas_base_flush_reply_queues(struct MPT2SAS_ADAPTER *ioc)
1056 struct adapter_reply_queue *reply_q;
1058 /* If MSIX capability is turned off
1059 * then multi-queues are not enabled
1061 if (!_base_is_controller_msix_enabled(ioc))
1064 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1065 if (ioc->shost_recovery)
1067 /* TMs are on msix_index == 0 */
1068 if (reply_q->msix_index == 0)
1070 _base_interrupt(reply_q->vector, (void *)reply_q);
1075 * mpt2sas_base_release_callback_handler - clear interrupt callback handler
1076 * @cb_idx: callback index
1081 mpt2sas_base_release_callback_handler(u8 cb_idx)
1083 mpt_callbacks[cb_idx] = NULL;
1087 * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
1088 * @cb_func: callback function
1093 mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1097 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1098 if (mpt_callbacks[cb_idx] == NULL)
1101 mpt_callbacks[cb_idx] = cb_func;
1106 * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
1111 mpt2sas_base_initialize_callback_handler(void)
1115 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1116 mpt2sas_base_release_callback_handler(cb_idx);
1120 * mpt2sas_base_build_zero_len_sge - build zero length sg entry
1121 * @ioc: per adapter object
1122 * @paddr: virtual address for SGE
1124 * Create a zero length scatter gather entry to insure the IOCs hardware has
1125 * something to use if the target device goes brain dead and tries
1126 * to send data even when none is asked for.
1131 mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
1133 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1134 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1135 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1136 MPI2_SGE_FLAGS_SHIFT);
1137 ioc->base_add_sg_single(paddr, flags_length, -1);
1141 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1142 * @paddr: virtual address for SGE
1143 * @flags_length: SGE flags and data transfer length
1144 * @dma_addr: Physical address
1149 _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1151 Mpi2SGESimple32_t *sgel = paddr;
1153 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1154 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1155 sgel->FlagsLength = cpu_to_le32(flags_length);
1156 sgel->Address = cpu_to_le32(dma_addr);
1161 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1162 * @paddr: virtual address for SGE
1163 * @flags_length: SGE flags and data transfer length
1164 * @dma_addr: Physical address
1169 _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1171 Mpi2SGESimple64_t *sgel = paddr;
1173 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1174 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1175 sgel->FlagsLength = cpu_to_le32(flags_length);
1176 sgel->Address = cpu_to_le64(dma_addr);
1179 #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1182 * _base_config_dma_addressing - set dma addressing
1183 * @ioc: per adapter object
1184 * @pdev: PCI device struct
1186 * Returns 0 for success, non-zero for failure.
1189 _base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
1192 u64 consistent_dma_mask;
1195 consistent_dma_mask = DMA_BIT_MASK(64);
1197 consistent_dma_mask = DMA_BIT_MASK(32);
1199 if (sizeof(dma_addr_t) > 4) {
1200 const uint64_t required_mask =
1201 dma_get_required_mask(&pdev->dev);
1202 if ((required_mask > DMA_BIT_MASK(32)) &&
1203 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1204 !pci_set_consistent_dma_mask(pdev, consistent_dma_mask)) {
1205 ioc->base_add_sg_single = &_base_add_sg_single_64;
1206 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
1212 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1213 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1214 ioc->base_add_sg_single = &_base_add_sg_single_32;
1215 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
1222 printk(MPT2SAS_INFO_FMT
1223 "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
1224 ioc->name, ioc->dma_mask, convert_to_kb(s.totalram));
1230 _base_change_consistent_dma_mask(struct MPT2SAS_ADAPTER *ioc,
1231 struct pci_dev *pdev)
1233 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
1234 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1240 * _base_check_enable_msix - checks MSIX capabable.
1241 * @ioc: per adapter object
1243 * Check to see if card is capable of MSIX, and set number
1244 * of available msix vectors
1247 _base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1250 u16 message_control;
1253 /* Check whether controller SAS2008 B0 controller,
1254 if it is SAS2008 B0 controller use IO-APIC instead of MSIX */
1255 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
1256 ioc->pdev->revision == 0x01) {
1260 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1262 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
1263 "supported\n", ioc->name));
1267 /* get msix vector count */
1268 /* NUMA_IO not supported for older controllers */
1269 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
1270 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
1271 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
1272 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
1273 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
1274 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
1275 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
1276 ioc->msix_vector_count = 1;
1278 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1279 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1281 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
1282 "vector_count(%d)\n", ioc->name, ioc->msix_vector_count));
1288 * _base_free_irq - free irq
1289 * @ioc: per adapter object
1291 * Freeing respective reply_queue from the list.
1294 _base_free_irq(struct MPT2SAS_ADAPTER *ioc)
1296 struct adapter_reply_queue *reply_q, *next;
1298 if (list_empty(&ioc->reply_queue_list))
1301 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1302 list_del(&reply_q->list);
1303 irq_set_affinity_hint(reply_q->vector, NULL);
1304 free_cpumask_var(reply_q->affinity_hint);
1305 synchronize_irq(reply_q->vector);
1306 free_irq(reply_q->vector, reply_q);
1312 * _base_request_irq - request irq
1313 * @ioc: per adapter object
1314 * @index: msix index into vector table
1315 * @vector: irq vector
1317 * Inserting respective reply_queue into the list.
1320 _base_request_irq(struct MPT2SAS_ADAPTER *ioc, u8 index, u32 vector)
1322 struct adapter_reply_queue *reply_q;
1325 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1327 printk(MPT2SAS_ERR_FMT "unable to allocate memory %d!\n",
1328 ioc->name, (int)sizeof(struct adapter_reply_queue));
1332 reply_q->msix_index = index;
1333 reply_q->vector = vector;
1335 if (!alloc_cpumask_var(&reply_q->affinity_hint, GFP_KERNEL))
1337 cpumask_clear(reply_q->affinity_hint);
1339 atomic_set(&reply_q->busy, 0);
1340 if (ioc->msix_enable)
1341 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
1342 MPT2SAS_DRIVER_NAME, ioc->id, index);
1344 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
1345 MPT2SAS_DRIVER_NAME, ioc->id);
1346 r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1349 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1350 reply_q->name, vector);
1355 INIT_LIST_HEAD(&reply_q->list);
1356 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1361 * _base_assign_reply_queues - assigning msix index for each cpu
1362 * @ioc: per adapter object
1364 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1366 * It would nice if we could call irq_set_affinity, however it is not
1367 * an exported symbol
1370 _base_assign_reply_queues(struct MPT2SAS_ADAPTER *ioc)
1372 unsigned int cpu, nr_cpus, nr_msix, index = 0;
1373 struct adapter_reply_queue *reply_q;
1375 if (!_base_is_controller_msix_enabled(ioc))
1378 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1380 nr_cpus = num_online_cpus();
1381 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
1382 ioc->facts.MaxMSIxVectors);
1386 cpu = cpumask_first(cpu_online_mask);
1388 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1390 unsigned int i, group = nr_cpus / nr_msix;
1395 if (index < nr_cpus % nr_msix)
1398 for (i = 0 ; i < group ; i++) {
1399 ioc->cpu_msix_table[cpu] = index;
1400 cpumask_or(reply_q->affinity_hint,
1401 reply_q->affinity_hint, get_cpu_mask(cpu));
1402 cpu = cpumask_next(cpu, cpu_online_mask);
1405 if (irq_set_affinity_hint(reply_q->vector,
1406 reply_q->affinity_hint))
1407 dinitprintk(ioc, pr_info(MPT2SAS_FMT
1408 "error setting affinity hint for irq vector %d\n",
1409 ioc->name, reply_q->vector));
1415 * _base_disable_msix - disables msix
1416 * @ioc: per adapter object
1420 _base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
1422 if (ioc->msix_enable) {
1423 pci_disable_msix(ioc->pdev);
1424 ioc->msix_enable = 0;
1429 * _base_enable_msix - enables msix, failback to io_apic
1430 * @ioc: per adapter object
1434 _base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1436 struct msix_entry *entries, *a;
1441 if (msix_disable == -1 || msix_disable == 0)
1447 if (_base_check_enable_msix(ioc) != 0)
1450 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
1451 ioc->msix_vector_count);
1453 if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
1454 max_msix_vectors = 8;
1456 if (max_msix_vectors > 0) {
1457 ioc->reply_queue_count = min_t(int, max_msix_vectors,
1458 ioc->reply_queue_count);
1459 ioc->msix_vector_count = ioc->reply_queue_count;
1460 } else if (max_msix_vectors == 0)
1463 printk(MPT2SAS_INFO_FMT
1464 "MSI-X vectors supported: %d, no of cores: %d, max_msix_vectors: %d\n",
1465 ioc->name, ioc->msix_vector_count, ioc->cpu_count, max_msix_vectors);
1467 entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1470 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "kcalloc "
1471 "failed @ at %s:%d/%s() !!!\n", ioc->name, __FILE__,
1472 __LINE__, __func__));
1476 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
1479 r = pci_enable_msix_exact(ioc->pdev, entries, ioc->reply_queue_count);
1481 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT
1482 "pci_enable_msix_exact failed (r=%d) !!!\n", ioc->name, r));
1487 ioc->msix_enable = 1;
1488 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
1489 r = _base_request_irq(ioc, i, a->vector);
1491 _base_free_irq(ioc);
1492 _base_disable_msix(ioc);
1501 /* failback to io_apic interrupt routing */
1504 ioc->reply_queue_count = 1;
1505 r = _base_request_irq(ioc, 0, ioc->pdev->irq);
1511 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1512 * @ioc: per adapter object
1514 * Returns 0 for success, non-zero for failure.
1517 mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1519 struct pci_dev *pdev = ioc->pdev;
1525 struct adapter_reply_queue *reply_q;
1527 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
1528 ioc->name, __func__));
1530 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1531 if (pci_enable_device_mem(pdev)) {
1532 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1533 "failed\n", ioc->name);
1539 if (pci_request_selected_regions(pdev, ioc->bars,
1540 MPT2SAS_DRIVER_NAME)) {
1541 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1542 "failed\n", ioc->name);
1548 /* AER (Advanced Error Reporting) hooks */
1549 pci_enable_pcie_error_reporting(pdev);
1551 pci_set_master(pdev);
1553 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1554 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1555 ioc->name, pci_name(pdev));
1560 for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
1561 (!memap_sz || !pio_sz); i++) {
1562 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
1565 pio_chip = (u64)pci_resource_start(pdev, i);
1566 pio_sz = pci_resource_len(pdev, i);
1570 /* verify memory resource is valid before using */
1571 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1572 ioc->chip_phys = pci_resource_start(pdev, i);
1573 chip_phys = (u64)ioc->chip_phys;
1574 memap_sz = pci_resource_len(pdev, i);
1575 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1580 if (ioc->chip == NULL) {
1581 printk(MPT2SAS_ERR_FMT "unable to map adapter memory! "
1582 "or resource not found\n", ioc->name);
1587 _base_mask_interrupts(ioc);
1589 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
1593 if (!ioc->rdpq_array_enable_assigned) {
1594 ioc->rdpq_array_enable = ioc->rdpq_array_capable;
1595 ioc->rdpq_array_enable_assigned = 1;
1598 r = _base_enable_msix(ioc);
1602 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
1603 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1604 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1605 "IO-APIC enabled"), reply_q->vector);
1607 printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1608 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1609 printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
1610 ioc->name, (unsigned long long)pio_chip, pio_sz);
1612 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1613 pci_save_state(pdev);
1621 pci_release_selected_regions(ioc->pdev, ioc->bars);
1622 pci_disable_pcie_error_reporting(pdev);
1623 pci_disable_device(pdev);
1628 * mpt2sas_base_get_msg_frame - obtain request mf pointer
1629 * @ioc: per adapter object
1630 * @smid: system request message index(smid zero is invalid)
1632 * Returns virt pointer to message frame.
1635 mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1637 return (void *)(ioc->request + (smid * ioc->request_sz));
1641 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1642 * @ioc: per adapter object
1643 * @smid: system request message index
1645 * Returns virt pointer to sense buffer.
1648 mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1650 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1654 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1655 * @ioc: per adapter object
1656 * @smid: system request message index
1658 * Returns phys pointer to the low 32bit address of the sense buffer.
1661 mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1663 return cpu_to_le32(ioc->sense_dma +
1664 ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1668 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1669 * @ioc: per adapter object
1670 * @phys_addr: lower 32 physical addr of the reply
1672 * Converts 32bit lower physical addr into a virt address.
1675 mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1679 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1683 * mpt2sas_base_get_smid - obtain a free smid from internal queue
1684 * @ioc: per adapter object
1685 * @cb_idx: callback index
1687 * Returns smid (zero is invalid)
1690 mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1692 unsigned long flags;
1693 struct request_tracker *request;
1696 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1697 if (list_empty(&ioc->internal_free_list)) {
1698 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1699 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1700 ioc->name, __func__);
1704 request = list_entry(ioc->internal_free_list.next,
1705 struct request_tracker, tracker_list);
1706 request->cb_idx = cb_idx;
1707 smid = request->smid;
1708 list_del(&request->tracker_list);
1709 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1714 * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1715 * @ioc: per adapter object
1716 * @cb_idx: callback index
1717 * @scmd: pointer to scsi command object
1719 * Returns smid (zero is invalid)
1722 mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
1723 struct scsi_cmnd *scmd)
1725 unsigned long flags;
1726 struct scsiio_tracker *request;
1729 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1730 if (list_empty(&ioc->free_list)) {
1731 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1732 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1733 ioc->name, __func__);
1737 request = list_entry(ioc->free_list.next,
1738 struct scsiio_tracker, tracker_list);
1739 request->scmd = scmd;
1740 request->cb_idx = cb_idx;
1741 smid = request->smid;
1742 list_del(&request->tracker_list);
1743 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1748 * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1749 * @ioc: per adapter object
1750 * @cb_idx: callback index
1752 * Returns smid (zero is invalid)
1755 mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1757 unsigned long flags;
1758 struct request_tracker *request;
1761 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1762 if (list_empty(&ioc->hpr_free_list)) {
1763 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1767 request = list_entry(ioc->hpr_free_list.next,
1768 struct request_tracker, tracker_list);
1769 request->cb_idx = cb_idx;
1770 smid = request->smid;
1771 list_del(&request->tracker_list);
1772 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1778 * mpt2sas_base_free_smid - put smid back on free_list
1779 * @ioc: per adapter object
1780 * @smid: system request message index
1785 mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1787 unsigned long flags;
1789 struct chain_tracker *chain_req, *next;
1791 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1792 if (smid < ioc->hi_priority_smid) {
1795 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
1796 list_for_each_entry_safe(chain_req, next,
1797 &ioc->scsi_lookup[i].chain_list, tracker_list) {
1798 list_del_init(&chain_req->tracker_list);
1799 list_add(&chain_req->tracker_list,
1800 &ioc->free_chain_list);
1803 ioc->scsi_lookup[i].cb_idx = 0xFF;
1804 ioc->scsi_lookup[i].scmd = NULL;
1805 ioc->scsi_lookup[i].direct_io = 0;
1806 list_add(&ioc->scsi_lookup[i].tracker_list,
1808 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1811 * See _wait_for_commands_to_complete() call with regards
1814 if (ioc->shost_recovery && ioc->pending_io_count) {
1815 if (ioc->pending_io_count == 1)
1816 wake_up(&ioc->reset_wq);
1817 ioc->pending_io_count--;
1820 } else if (smid < ioc->internal_smid) {
1822 i = smid - ioc->hi_priority_smid;
1823 ioc->hpr_lookup[i].cb_idx = 0xFF;
1824 list_add(&ioc->hpr_lookup[i].tracker_list,
1825 &ioc->hpr_free_list);
1826 } else if (smid <= ioc->hba_queue_depth) {
1827 /* internal queue */
1828 i = smid - ioc->internal_smid;
1829 ioc->internal_lookup[i].cb_idx = 0xFF;
1830 list_add(&ioc->internal_lookup[i].tracker_list,
1831 &ioc->internal_free_list);
1833 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1837 * _base_writeq - 64 bit write to MMIO
1838 * @ioc: per adapter object
1840 * @addr: address in MMIO space
1841 * @writeq_lock: spin lock
1843 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1844 * care of 32 bit environment where its not quarenteed to send the entire word
1848 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1849 spinlock_t *writeq_lock)
1851 unsigned long flags;
1852 __u64 data_out = cpu_to_le64(b);
1854 spin_lock_irqsave(writeq_lock, flags);
1855 writel((u32)(data_out), addr);
1856 writel((u32)(data_out >> 32), (addr + 4));
1857 spin_unlock_irqrestore(writeq_lock, flags);
1860 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1861 spinlock_t *writeq_lock)
1863 writeq(cpu_to_le64(b), addr);
1868 _base_get_msix_index(struct MPT2SAS_ADAPTER *ioc)
1870 return ioc->cpu_msix_table[raw_smp_processor_id()];
1874 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1875 * @ioc: per adapter object
1876 * @smid: system request message index
1877 * @handle: device handle
1882 mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
1884 Mpi2RequestDescriptorUnion_t descriptor;
1885 u64 *request = (u64 *)&descriptor;
1888 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1889 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
1890 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1891 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1892 descriptor.SCSIIO.LMID = 0;
1893 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1894 &ioc->scsi_lookup_lock);
1899 * mpt2sas_base_put_smid_hi_priority - send Task Management request to firmware
1900 * @ioc: per adapter object
1901 * @smid: system request message index
1906 mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1908 Mpi2RequestDescriptorUnion_t descriptor;
1909 u64 *request = (u64 *)&descriptor;
1911 descriptor.HighPriority.RequestFlags =
1912 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1913 descriptor.HighPriority.MSIxIndex = 0;
1914 descriptor.HighPriority.SMID = cpu_to_le16(smid);
1915 descriptor.HighPriority.LMID = 0;
1916 descriptor.HighPriority.Reserved1 = 0;
1917 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1918 &ioc->scsi_lookup_lock);
1922 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1923 * @ioc: per adapter object
1924 * @smid: system request message index
1929 mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1931 Mpi2RequestDescriptorUnion_t descriptor;
1932 u64 *request = (u64 *)&descriptor;
1934 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1935 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
1936 descriptor.Default.SMID = cpu_to_le16(smid);
1937 descriptor.Default.LMID = 0;
1938 descriptor.Default.DescriptorTypeDependent = 0;
1939 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1940 &ioc->scsi_lookup_lock);
1944 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1945 * @ioc: per adapter object
1946 * @smid: system request message index
1947 * @io_index: value used to track the IO
1952 mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
1955 Mpi2RequestDescriptorUnion_t descriptor;
1956 u64 *request = (u64 *)&descriptor;
1958 descriptor.SCSITarget.RequestFlags =
1959 MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
1960 descriptor.SCSITarget.MSIxIndex = _base_get_msix_index(ioc);
1961 descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1962 descriptor.SCSITarget.LMID = 0;
1963 descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1964 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1965 &ioc->scsi_lookup_lock);
1969 * _base_display_dell_branding - Disply branding string
1970 * @ioc: per adapter object
1975 _base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1977 char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1979 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1982 memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1983 switch (ioc->pdev->subsystem_device) {
1984 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1985 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1986 MPT2SAS_DELL_BRANDING_SIZE - 1);
1988 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1989 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1990 MPT2SAS_DELL_BRANDING_SIZE - 1);
1992 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1993 strncpy(dell_branding,
1994 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1995 MPT2SAS_DELL_BRANDING_SIZE - 1);
1997 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1998 strncpy(dell_branding,
1999 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
2000 MPT2SAS_DELL_BRANDING_SIZE - 1);
2002 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
2003 strncpy(dell_branding,
2004 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
2005 MPT2SAS_DELL_BRANDING_SIZE - 1);
2007 case MPT2SAS_DELL_PERC_H200_SSDID:
2008 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
2009 MPT2SAS_DELL_BRANDING_SIZE - 1);
2011 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
2012 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
2013 MPT2SAS_DELL_BRANDING_SIZE - 1);
2016 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
2020 printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
2021 " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
2022 ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
2023 ioc->pdev->subsystem_device);
2027 * _base_display_intel_branding - Display branding string
2028 * @ioc: per adapter object
2033 _base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
2035 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
2038 switch (ioc->pdev->device) {
2039 case MPI2_MFGPAGE_DEVID_SAS2008:
2040 switch (ioc->pdev->subsystem_device) {
2041 case MPT2SAS_INTEL_RMS2LL080_SSDID:
2042 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2043 MPT2SAS_INTEL_RMS2LL080_BRANDING);
2045 case MPT2SAS_INTEL_RMS2LL040_SSDID:
2046 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2047 MPT2SAS_INTEL_RMS2LL040_BRANDING);
2049 case MPT2SAS_INTEL_SSD910_SSDID:
2050 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2051 MPT2SAS_INTEL_SSD910_BRANDING);
2056 case MPI2_MFGPAGE_DEVID_SAS2308_2:
2057 switch (ioc->pdev->subsystem_device) {
2058 case MPT2SAS_INTEL_RS25GB008_SSDID:
2059 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2060 MPT2SAS_INTEL_RS25GB008_BRANDING);
2062 case MPT2SAS_INTEL_RMS25JB080_SSDID:
2063 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2064 MPT2SAS_INTEL_RMS25JB080_BRANDING);
2066 case MPT2SAS_INTEL_RMS25JB040_SSDID:
2067 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2068 MPT2SAS_INTEL_RMS25JB040_BRANDING);
2070 case MPT2SAS_INTEL_RMS25KB080_SSDID:
2071 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2072 MPT2SAS_INTEL_RMS25KB080_BRANDING);
2074 case MPT2SAS_INTEL_RMS25KB040_SSDID:
2075 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2076 MPT2SAS_INTEL_RMS25KB040_BRANDING);
2078 case MPT2SAS_INTEL_RMS25LB040_SSDID:
2079 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2080 MPT2SAS_INTEL_RMS25LB040_BRANDING);
2082 case MPT2SAS_INTEL_RMS25LB080_SSDID:
2083 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2084 MPT2SAS_INTEL_RMS25LB080_BRANDING);
2095 * _base_display_hp_branding - Display branding string
2096 * @ioc: per adapter object
2101 _base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
2103 if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
2106 switch (ioc->pdev->device) {
2107 case MPI2_MFGPAGE_DEVID_SAS2004:
2108 switch (ioc->pdev->subsystem_device) {
2109 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
2110 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2111 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
2116 case MPI2_MFGPAGE_DEVID_SAS2308_2:
2117 switch (ioc->pdev->subsystem_device) {
2118 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
2119 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2120 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
2122 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
2123 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2124 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
2126 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
2127 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2128 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
2130 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
2131 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2132 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
2143 * _base_display_ioc_capabilities - Disply IOC's capabilities.
2144 * @ioc: per adapter object
2149 _base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
2153 u32 iounit_pg1_flags;
2156 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2157 strncpy(desc, ioc->manu_pg0.ChipName, 16);
2158 printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
2159 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2161 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2162 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2163 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2164 ioc->facts.FWVersion.Word & 0x000000FF,
2165 ioc->pdev->revision,
2166 (bios_version & 0xFF000000) >> 24,
2167 (bios_version & 0x00FF0000) >> 16,
2168 (bios_version & 0x0000FF00) >> 8,
2169 bios_version & 0x000000FF);
2171 _base_display_dell_branding(ioc);
2172 _base_display_intel_branding(ioc);
2173 _base_display_hp_branding(ioc);
2175 printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
2177 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2178 printk("Initiator");
2182 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2183 printk("%sTarget", i ? "," : "");
2189 printk("Capabilities=(");
2191 if (!ioc->hide_ir_msg) {
2192 if (ioc->facts.IOCCapabilities &
2193 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2199 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2200 printk("%sTLR", i ? "," : "");
2204 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2205 printk("%sMulticast", i ? "," : "");
2209 if (ioc->facts.IOCCapabilities &
2210 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2211 printk("%sBIDI Target", i ? "," : "");
2215 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2216 printk("%sEEDP", i ? "," : "");
2220 if (ioc->facts.IOCCapabilities &
2221 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2222 printk("%sSnapshot Buffer", i ? "," : "");
2226 if (ioc->facts.IOCCapabilities &
2227 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2228 printk("%sDiag Trace Buffer", i ? "," : "");
2232 if (ioc->facts.IOCCapabilities &
2233 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2234 printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
2238 if (ioc->facts.IOCCapabilities &
2239 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2240 printk("%sTask Set Full", i ? "," : "");
2244 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2245 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2246 printk("%sNCQ", i ? "," : "");
2254 * mpt2sas_base_update_missing_delay - change the missing delay timers
2255 * @ioc: per adapter object
2256 * @device_missing_delay: amount of time till device is reported missing
2257 * @io_missing_delay: interval IO is returned when there is a missing device
2261 * Passed on the command line, this function will modify the device missing
2262 * delay, as well as the io missing delay. This should be called at driver
2266 mpt2sas_base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
2267 u16 device_missing_delay, u8 io_missing_delay)
2269 u16 dmd, dmd_new, dmd_orignal;
2270 u8 io_missing_delay_original;
2272 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2273 Mpi2ConfigReply_t mpi_reply;
2277 mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
2281 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2282 sizeof(Mpi2SasIOUnit1PhyData_t));
2283 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2284 if (!sas_iounit_pg1) {
2285 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2286 ioc->name, __FILE__, __LINE__, __func__);
2289 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2290 sas_iounit_pg1, sz))) {
2291 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2292 ioc->name, __FILE__, __LINE__, __func__);
2295 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2296 MPI2_IOCSTATUS_MASK;
2297 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2298 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2299 ioc->name, __FILE__, __LINE__, __func__);
2303 /* device missing delay */
2304 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2305 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2306 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2308 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2310 if (device_missing_delay > 0x7F) {
2311 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2312 device_missing_delay;
2314 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2316 dmd = device_missing_delay;
2317 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2319 /* io missing delay */
2320 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2321 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2323 if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2325 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2327 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2330 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2331 printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
2332 "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
2333 printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
2334 "new(%d)\n", ioc->name, io_missing_delay_original,
2336 ioc->device_missing_delay = dmd_new;
2337 ioc->io_missing_delay = io_missing_delay;
2341 kfree(sas_iounit_pg1);
2345 * _base_static_config_pages - static start of day config pages
2346 * @ioc: per adapter object
2351 _base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
2353 Mpi2ConfigReply_t mpi_reply;
2354 u32 iounit_pg1_flags;
2356 mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
2357 if (ioc->ir_firmware)
2358 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2360 mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
2361 mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
2362 mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
2363 mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
2364 mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2365 mpt2sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
2366 _base_display_ioc_capabilities(ioc);
2369 * Enable task_set_full handling in iounit_pg1 when the
2370 * facts capabilities indicate that its supported.
2372 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2373 if ((ioc->facts.IOCCapabilities &
2374 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
2376 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2379 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2380 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
2381 mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2383 if (ioc->iounit_pg8.NumSensors)
2384 ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
2388 * _base_release_memory_pools - release memory
2389 * @ioc: per adapter object
2391 * Free memory allocated from _base_allocate_memory_pools.
2396 _base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
2399 struct reply_post_struct *rps;
2401 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2405 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2406 ioc->request, ioc->request_dma);
2407 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
2408 ": free\n", ioc->name, ioc->request));
2409 ioc->request = NULL;
2413 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2414 if (ioc->sense_dma_pool)
2415 pci_pool_destroy(ioc->sense_dma_pool);
2416 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
2417 ": free\n", ioc->name, ioc->sense));
2422 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2423 if (ioc->reply_dma_pool)
2424 pci_pool_destroy(ioc->reply_dma_pool);
2425 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
2426 ": free\n", ioc->name, ioc->reply));
2430 if (ioc->reply_free) {
2431 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2432 ioc->reply_free_dma);
2433 if (ioc->reply_free_dma_pool)
2434 pci_pool_destroy(ioc->reply_free_dma_pool);
2435 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
2436 "(0x%p): free\n", ioc->name, ioc->reply_free));
2437 ioc->reply_free = NULL;
2440 if (ioc->reply_post) {
2442 rps = &ioc->reply_post[i];
2443 if (rps->reply_post_free) {
2445 ioc->reply_post_free_dma_pool,
2446 rps->reply_post_free,
2447 rps->reply_post_free_dma);
2448 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2449 "reply_post_free_pool(0x%p): free\n",
2450 ioc->name, rps->reply_post_free));
2451 rps->reply_post_free = NULL;
2453 } while (ioc->rdpq_array_enable &&
2454 (++i < ioc->reply_queue_count));
2456 if (ioc->reply_post_free_dma_pool)
2457 pci_pool_destroy(ioc->reply_post_free_dma_pool);
2458 kfree(ioc->reply_post);
2461 if (ioc->config_page) {
2462 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2463 "config_page(0x%p): free\n", ioc->name,
2465 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2466 ioc->config_page, ioc->config_page_dma);
2469 if (ioc->scsi_lookup) {
2470 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2471 ioc->scsi_lookup = NULL;
2473 kfree(ioc->hpr_lookup);
2474 kfree(ioc->internal_lookup);
2475 if (ioc->chain_lookup) {
2476 for (i = 0; i < ioc->chain_depth; i++) {
2477 if (ioc->chain_lookup[i].chain_buffer)
2478 pci_pool_free(ioc->chain_dma_pool,
2479 ioc->chain_lookup[i].chain_buffer,
2480 ioc->chain_lookup[i].chain_buffer_dma);
2482 if (ioc->chain_dma_pool)
2483 pci_pool_destroy(ioc->chain_dma_pool);
2484 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2485 ioc->chain_lookup = NULL;
2491 * _base_allocate_memory_pools - allocate start of day memory pools
2492 * @ioc: per adapter object
2493 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2495 * Returns 0 success, anything else error
2498 _base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2500 struct mpt2sas_facts *facts;
2501 u16 max_sge_elements;
2502 u16 chains_needed_per_io;
2503 u32 sz, total_sz, reply_post_free_sz;
2505 u16 max_request_credit;
2508 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2512 facts = &ioc->facts;
2514 /* command line tunables for max sgl entries */
2515 if (max_sgl_entries != -1) {
2516 ioc->shost->sg_tablesize = min_t(unsigned short,
2517 max_sgl_entries, SCSI_MAX_SG_CHAIN_SEGMENTS);
2518 if (ioc->shost->sg_tablesize > MPT2SAS_SG_DEPTH)
2519 printk(MPT2SAS_WARN_FMT
2520 "sg_tablesize(%u) is bigger than kernel defined"
2521 " SCSI_MAX_SG_SEGMENTS(%u)\n", ioc->name,
2522 ioc->shost->sg_tablesize, MPT2SAS_SG_DEPTH);
2524 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
2527 /* command line tunables for max controller queue depth */
2528 if (max_queue_depth != -1 && max_queue_depth != 0) {
2529 max_request_credit = min_t(u16, max_queue_depth +
2530 ioc->hi_priority_depth + ioc->internal_depth,
2531 facts->RequestCredit);
2532 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
2533 max_request_credit = MAX_HBA_QUEUE_DEPTH;
2535 max_request_credit = min_t(u16, facts->RequestCredit,
2536 MAX_HBA_QUEUE_DEPTH);
2538 ioc->hba_queue_depth = max_request_credit;
2539 ioc->hi_priority_depth = facts->HighPriorityCredit;
2540 ioc->internal_depth = ioc->hi_priority_depth + 5;
2542 /* request frame size */
2543 ioc->request_sz = facts->IOCRequestFrameSize * 4;
2545 /* reply frame size */
2546 ioc->reply_sz = facts->ReplyFrameSize * 4;
2550 /* calculate number of sg elements left over in the 1st frame */
2551 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2552 sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
2553 ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
2555 /* now do the same for a chain buffer */
2556 max_sge_elements = ioc->request_sz - ioc->sge_size;
2557 ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
2559 ioc->chain_offset_value_for_main_message =
2560 ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
2561 (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
2564 * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2566 chains_needed_per_io = ((ioc->shost->sg_tablesize -
2567 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2569 if (chains_needed_per_io > facts->MaxChainDepth) {
2570 chains_needed_per_io = facts->MaxChainDepth;
2571 ioc->shost->sg_tablesize = min_t(u16,
2572 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2573 * chains_needed_per_io), ioc->shost->sg_tablesize);
2575 ioc->chains_needed_per_io = chains_needed_per_io;
2577 /* reply free queue sizing - taking into account for 64 FW events */
2578 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2580 /* calculate reply descriptor post queue depth */
2581 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
2582 ioc->reply_free_queue_depth + 1;
2583 /* align the reply post queue on the next 16 count boundary */
2584 if (ioc->reply_post_queue_depth % 16)
2585 ioc->reply_post_queue_depth += 16 -
2586 (ioc->reply_post_queue_depth % 16);
2589 if (ioc->reply_post_queue_depth >
2590 facts->MaxReplyDescriptorPostQueueDepth) {
2591 ioc->reply_post_queue_depth =
2592 facts->MaxReplyDescriptorPostQueueDepth -
2593 (facts->MaxReplyDescriptorPostQueueDepth % 16);
2594 ioc->hba_queue_depth =
2595 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
2596 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2599 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
2600 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2601 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2602 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2603 ioc->chains_needed_per_io));
2605 /* reply post queue, 16 byte align */
2606 reply_post_free_sz = ioc->reply_post_queue_depth *
2607 sizeof(Mpi2DefaultReplyDescriptor_t);
2609 sz = reply_post_free_sz;
2610 if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
2611 sz *= ioc->reply_queue_count;
2613 ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
2614 (ioc->reply_queue_count):1,
2615 sizeof(struct reply_post_struct), GFP_KERNEL);
2617 if (!ioc->reply_post) {
2618 printk(MPT2SAS_ERR_FMT "reply_post_free pool: kcalloc failed\n",
2622 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2623 ioc->pdev, sz, 16, 0);
2624 if (!ioc->reply_post_free_dma_pool) {
2625 printk(MPT2SAS_ERR_FMT
2626 "reply_post_free pool: pci_pool_create failed\n",
2632 ioc->reply_post[i].reply_post_free =
2633 pci_pool_alloc(ioc->reply_post_free_dma_pool,
2635 &ioc->reply_post[i].reply_post_free_dma);
2636 if (!ioc->reply_post[i].reply_post_free) {
2637 printk(MPT2SAS_ERR_FMT
2638 "reply_post_free pool: pci_pool_alloc failed\n",
2642 memset(ioc->reply_post[i].reply_post_free, 0, sz);
2643 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2644 "reply post free pool (0x%p): depth(%d),"
2645 "element_size(%d), pool_size(%d kB)\n", ioc->name,
2646 ioc->reply_post[i].reply_post_free,
2647 ioc->reply_post_queue_depth, 8, sz/1024));
2648 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2649 "reply_post_free_dma = (0x%llx)\n", ioc->name,
2650 (unsigned long long)
2651 ioc->reply_post[i].reply_post_free_dma));
2653 } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
2655 if (ioc->dma_mask == 64) {
2656 if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
2657 printk(MPT2SAS_WARN_FMT
2658 "no suitable consistent DMA mask for %s\n",
2659 ioc->name, pci_name(ioc->pdev));
2664 ioc->scsiio_depth = ioc->hba_queue_depth -
2665 ioc->hi_priority_depth - ioc->internal_depth;
2667 /* set the scsi host can_queue depth
2668 * with some internal commands that could be outstanding
2670 ioc->shost->can_queue = ioc->scsiio_depth;
2671 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
2672 "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
2674 /* contiguous pool for request and chains, 16 byte align, one extra "
2677 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
2678 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
2680 /* hi-priority queue */
2681 sz += (ioc->hi_priority_depth * ioc->request_sz);
2683 /* internal queue */
2684 sz += (ioc->internal_depth * ioc->request_sz);
2686 ioc->request_dma_sz = sz;
2687 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2688 if (!ioc->request) {
2689 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2690 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2691 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
2692 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2693 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
2696 ioc->hba_queue_depth = max_request_credit - retry_sz;
2697 goto retry_allocation;
2701 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2702 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2703 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
2704 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2707 /* hi-priority queue */
2708 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
2710 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
2713 /* internal queue */
2714 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2716 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2720 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2721 "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2722 ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2723 (ioc->hba_queue_depth * ioc->request_sz)/1024));
2724 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2725 ioc->name, (unsigned long long) ioc->request_dma));
2728 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
2729 ioc->scsi_lookup_pages = get_order(sz);
2730 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
2731 GFP_KERNEL, ioc->scsi_lookup_pages);
2732 if (!ioc->scsi_lookup) {
2733 printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
2734 "sz(%d)\n", ioc->name, (int)sz);
2738 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2739 "depth(%d)\n", ioc->name, ioc->request,
2740 ioc->scsiio_depth));
2742 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
2743 sz = ioc->chain_depth * sizeof(struct chain_tracker);
2744 ioc->chain_pages = get_order(sz);
2746 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
2747 GFP_KERNEL, ioc->chain_pages);
2748 if (!ioc->chain_lookup) {
2749 printk(MPT2SAS_ERR_FMT "chain_lookup: get_free_pages failed, "
2750 "sz(%d)\n", ioc->name, (int)sz);
2753 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
2754 ioc->request_sz, 16, 0);
2755 if (!ioc->chain_dma_pool) {
2756 printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
2757 "failed\n", ioc->name);
2760 for (i = 0; i < ioc->chain_depth; i++) {
2761 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
2762 ioc->chain_dma_pool , GFP_KERNEL,
2763 &ioc->chain_lookup[i].chain_buffer_dma);
2764 if (!ioc->chain_lookup[i].chain_buffer) {
2765 ioc->chain_depth = i;
2768 total_sz += ioc->request_sz;
2771 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
2772 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2773 ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2774 ioc->request_sz))/1024));
2776 /* initialize hi-priority queue smid's */
2777 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2778 sizeof(struct request_tracker), GFP_KERNEL);
2779 if (!ioc->hpr_lookup) {
2780 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2784 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2785 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2786 "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2787 ioc->hi_priority_depth, ioc->hi_priority_smid));
2789 /* initialize internal queue smid's */
2790 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2791 sizeof(struct request_tracker), GFP_KERNEL);
2792 if (!ioc->internal_lookup) {
2793 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2797 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2798 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2799 "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2800 ioc->internal_depth, ioc->internal_smid));
2802 /* sense buffers, 4 byte align */
2803 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
2804 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2806 if (!ioc->sense_dma_pool) {
2807 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2811 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2814 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2818 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2819 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
2820 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
2821 SCSI_SENSE_BUFFERSIZE, sz/1024));
2822 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2823 ioc->name, (unsigned long long)ioc->sense_dma));
2826 /* reply pool, 4 byte align */
2827 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2828 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2830 if (!ioc->reply_dma_pool) {
2831 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2835 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2838 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2842 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2843 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
2844 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2845 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2846 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2847 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2848 ioc->name, (unsigned long long)ioc->reply_dma));
2851 /* reply free queue, 16 byte align */
2852 sz = ioc->reply_free_queue_depth * 4;
2853 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2854 ioc->pdev, sz, 16, 0);
2855 if (!ioc->reply_free_dma_pool) {
2856 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2857 "failed\n", ioc->name);
2860 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2861 &ioc->reply_free_dma);
2862 if (!ioc->reply_free) {
2863 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2864 "failed\n", ioc->name);
2867 memset(ioc->reply_free, 0, sz);
2868 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2869 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2870 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2871 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2872 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2875 ioc->config_page_sz = 512;
2876 ioc->config_page = pci_alloc_consistent(ioc->pdev,
2877 ioc->config_page_sz, &ioc->config_page_dma);
2878 if (!ioc->config_page) {
2879 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2880 "failed\n", ioc->name);
2883 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2884 "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2885 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2886 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2887 total_sz += ioc->config_page_sz;
2889 printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2890 ioc->name, total_sz/1024);
2891 printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2892 "Max Controller Queue Depth(%d)\n",
2893 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2894 printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2895 ioc->name, ioc->shost->sg_tablesize);
2904 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2905 * @ioc: Pointer to MPT_ADAPTER structure
2906 * @cooked: Request raw or cooked IOC state
2908 * Returns all IOC Doorbell register bits if cooked==0, else just the
2909 * Doorbell bits in MPI_IOC_STATE_MASK.
2912 mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2916 s = readl(&ioc->chip->Doorbell);
2917 sc = s & MPI2_IOC_STATE_MASK;
2918 return cooked ? sc : s;
2922 * _base_wait_on_iocstate - waiting on a particular ioc state
2923 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2924 * @timeout: timeout in second
2925 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2927 * Returns 0 for success, non-zero for failure.
2930 _base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2937 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2939 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2940 if (current_state == ioc_state)
2942 if (count && current_state == MPI2_IOC_STATE_FAULT)
2944 if (sleep_flag == CAN_SLEEP)
2951 return current_state;
2955 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2956 * a write to the doorbell)
2957 * @ioc: per adapter object
2958 * @timeout: timeout in second
2959 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2961 * Returns 0 for success, non-zero for failure.
2963 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2966 _base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2973 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2975 int_status = readl(&ioc->chip->HostInterruptStatus);
2976 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2977 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
2978 "successful count(%d), timeout(%d)\n", ioc->name,
2979 __func__, count, timeout));
2982 if (sleep_flag == CAN_SLEEP)
2989 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2990 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2995 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2996 * @ioc: per adapter object
2997 * @timeout: timeout in second
2998 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3000 * Returns 0 for success, non-zero for failure.
3002 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
3006 _base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
3014 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3016 int_status = readl(&ioc->chip->HostInterruptStatus);
3017 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
3018 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
3019 "successful count(%d), timeout(%d)\n", ioc->name,
3020 __func__, count, timeout));
3022 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
3023 doorbell = readl(&ioc->chip->Doorbell);
3024 if ((doorbell & MPI2_IOC_STATE_MASK) ==
3025 MPI2_IOC_STATE_FAULT) {
3026 mpt2sas_base_fault_info(ioc , doorbell);
3029 } else if (int_status == 0xFFFFFFFF)
3032 if (sleep_flag == CAN_SLEEP)
3040 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
3041 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
3046 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
3047 * @ioc: per adapter object
3048 * @timeout: timeout in second
3049 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3051 * Returns 0 for success, non-zero for failure.
3055 _base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
3062 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3064 doorbell_reg = readl(&ioc->chip->Doorbell);
3065 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
3066 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
3067 "successful count(%d), timeout(%d)\n", ioc->name,
3068 __func__, count, timeout));
3071 if (sleep_flag == CAN_SLEEP)
3078 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
3079 "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
3084 * _base_send_ioc_reset - send doorbell reset
3085 * @ioc: per adapter object
3086 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
3087 * @timeout: timeout in second
3088 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3090 * Returns 0 for success, non-zero for failure.
3093 _base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
3099 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
3100 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
3101 ioc->name, __func__);
3105 if (!(ioc->facts.IOCCapabilities &
3106 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
3109 printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
3111 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
3112 &ioc->chip->Doorbell);
3113 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
3117 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
3118 timeout, sleep_flag);
3120 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3121 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3126 printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
3127 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3132 * _base_handshake_req_reply_wait - send request thru doorbell interface
3133 * @ioc: per adapter object
3134 * @request_bytes: request length
3135 * @request: pointer having request payload
3136 * @reply_bytes: reply length
3137 * @reply: pointer to reply payload
3138 * @timeout: timeout in second
3139 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3141 * Returns 0 for success, non-zero for failure.
3144 _base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
3145 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
3147 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
3153 /* make sure doorbell is not in use */
3154 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
3155 printk(MPT2SAS_ERR_FMT "doorbell is in use "
3156 " (line=%d)\n", ioc->name, __LINE__);
3160 /* clear pending doorbell interrupts from previous state changes */
3161 if (readl(&ioc->chip->HostInterruptStatus) &
3162 MPI2_HIS_IOC2SYS_DB_STATUS)
3163 writel(0, &ioc->chip->HostInterruptStatus);
3165 /* send message to ioc */
3166 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
3167 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
3168 &ioc->chip->Doorbell);
3170 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
3171 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3172 "int failed (line=%d)\n", ioc->name, __LINE__);
3175 writel(0, &ioc->chip->HostInterruptStatus);
3177 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
3178 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3179 "ack failed (line=%d)\n", ioc->name, __LINE__);
3183 /* send message 32-bits at a time */
3184 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
3185 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
3186 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
3191 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3192 "sending request failed (line=%d)\n", ioc->name, __LINE__);
3196 /* now wait for the reply */
3197 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
3198 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3199 "int failed (line=%d)\n", ioc->name, __LINE__);
3203 /* read the first two 16-bits, it gives the total length of the reply */
3204 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3205 & MPI2_DOORBELL_DATA_MASK);
3206 writel(0, &ioc->chip->HostInterruptStatus);
3207 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3208 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3209 "int failed (line=%d)\n", ioc->name, __LINE__);
3212 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3213 & MPI2_DOORBELL_DATA_MASK);
3214 writel(0, &ioc->chip->HostInterruptStatus);
3216 for (i = 2; i < default_reply->MsgLength * 2; i++) {
3217 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3218 printk(MPT2SAS_ERR_FMT "doorbell "
3219 "handshake int failed (line=%d)\n", ioc->name,
3223 if (i >= reply_bytes/2) /* overflow case */
3224 dummy = readl(&ioc->chip->Doorbell);
3226 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3227 & MPI2_DOORBELL_DATA_MASK);
3228 writel(0, &ioc->chip->HostInterruptStatus);
3231 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3232 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3233 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
3234 " (line=%d)\n", ioc->name, __LINE__));
3236 writel(0, &ioc->chip->HostInterruptStatus);
3238 if (ioc->logging_level & MPT_DEBUG_INIT) {
3239 mfp = (__le32 *)reply;
3240 printk(KERN_INFO "\toffset:data\n");
3241 for (i = 0; i < reply_bytes/4; i++)
3242 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
3243 le32_to_cpu(mfp[i]));
3249 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
3250 * @ioc: per adapter object
3251 * @mpi_reply: the reply payload from FW
3252 * @mpi_request: the request payload sent to FW
3254 * The SAS IO Unit Control Request message allows the host to perform low-level
3255 * operations, such as resets on the PHYs of the IO Unit, also allows the host
3256 * to obtain the IOC assigned device handles for a device if it has other
3257 * identifying information about the device, in addition allows the host to
3258 * remove IOC resources associated with the device.
3260 * Returns 0 for success, non-zero for failure.
3263 mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
3264 Mpi2SasIoUnitControlReply_t *mpi_reply,
3265 Mpi2SasIoUnitControlRequest_t *mpi_request)
3269 unsigned long timeleft;
3270 bool issue_reset = false;
3273 u16 wait_state_count;
3275 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3278 mutex_lock(&ioc->base_cmds.mutex);
3280 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3281 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3282 ioc->name, __func__);
3287 wait_state_count = 0;
3288 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3289 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3290 if (wait_state_count++ == 10) {
3291 printk(MPT2SAS_ERR_FMT
3292 "%s: failed due to ioc not operational\n",
3293 ioc->name, __func__);
3298 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3299 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3300 "operational state(count=%d)\n", ioc->name,
3301 __func__, wait_state_count);
3304 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3306 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3307 ioc->name, __func__);
3313 ioc->base_cmds.status = MPT2_CMD_PENDING;
3314 request = mpt2sas_base_get_msg_frame(ioc, smid);
3315 ioc->base_cmds.smid = smid;
3316 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3317 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3318 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3319 ioc->ioc_link_reset_in_progress = 1;
3320 init_completion(&ioc->base_cmds.done);
3321 mpt2sas_base_put_smid_default(ioc, smid);
3322 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3323 msecs_to_jiffies(10000));
3324 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3325 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
3326 ioc->ioc_link_reset_in_progress)
3327 ioc->ioc_link_reset_in_progress = 0;
3328 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3329 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3330 ioc->name, __func__);
3331 _debug_dump_mf(mpi_request,
3332 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
3333 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3335 goto issue_host_reset;
3337 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3338 memcpy(mpi_reply, ioc->base_cmds.reply,
3339 sizeof(Mpi2SasIoUnitControlReply_t));
3341 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
3342 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3347 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3349 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3352 mutex_unlock(&ioc->base_cmds.mutex);
3358 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
3359 * @ioc: per adapter object
3360 * @mpi_reply: the reply payload from FW
3361 * @mpi_request: the request payload sent to FW
3363 * The SCSI Enclosure Processor request message causes the IOC to
3364 * communicate with SES devices to control LED status signals.
3366 * Returns 0 for success, non-zero for failure.
3369 mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
3370 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
3374 unsigned long timeleft;
3375 bool issue_reset = false;
3378 u16 wait_state_count;
3380 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3383 mutex_lock(&ioc->base_cmds.mutex);
3385 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3386 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3387 ioc->name, __func__);
3392 wait_state_count = 0;
3393 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3394 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3395 if (wait_state_count++ == 10) {
3396 printk(MPT2SAS_ERR_FMT
3397 "%s: failed due to ioc not operational\n",
3398 ioc->name, __func__);
3403 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3404 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3405 "operational state(count=%d)\n", ioc->name,
3406 __func__, wait_state_count);
3409 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3411 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3412 ioc->name, __func__);
3418 ioc->base_cmds.status = MPT2_CMD_PENDING;
3419 request = mpt2sas_base_get_msg_frame(ioc, smid);
3420 ioc->base_cmds.smid = smid;
3421 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
3422 init_completion(&ioc->base_cmds.done);
3423 mpt2sas_base_put_smid_default(ioc, smid);
3424 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3425 msecs_to_jiffies(10000));
3426 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3427 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3428 ioc->name, __func__);
3429 _debug_dump_mf(mpi_request,
3430 sizeof(Mpi2SepRequest_t)/4);
3431 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3433 goto issue_host_reset;
3435 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3436 memcpy(mpi_reply, ioc->base_cmds.reply,
3437 sizeof(Mpi2SepReply_t));
3439 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3440 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3445 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3447 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3450 mutex_unlock(&ioc->base_cmds.mutex);
3455 * _base_get_port_facts - obtain port facts reply and save in ioc
3456 * @ioc: per adapter object
3457 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3459 * Returns 0 for success, non-zero for failure.
3462 _base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
3464 Mpi2PortFactsRequest_t mpi_request;
3465 Mpi2PortFactsReply_t mpi_reply;
3466 struct mpt2sas_port_facts *pfacts;
3467 int mpi_reply_sz, mpi_request_sz, r;
3469 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3472 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3473 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3474 memset(&mpi_request, 0, mpi_request_sz);
3475 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3476 mpi_request.PortNumber = port;
3477 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3478 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3481 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3482 ioc->name, __func__, r);
3486 pfacts = &ioc->pfacts[port];
3487 memset(pfacts, 0, sizeof(struct mpt2sas_port_facts));
3488 pfacts->PortNumber = mpi_reply.PortNumber;
3489 pfacts->VP_ID = mpi_reply.VP_ID;
3490 pfacts->VF_ID = mpi_reply.VF_ID;
3491 pfacts->MaxPostedCmdBuffers =
3492 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3498 * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
3499 * @ioc: per adapter object
3501 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3503 * Returns 0 for success, non-zero for failure.
3506 _base_wait_for_iocstate(struct MPT2SAS_ADAPTER *ioc, int timeout,
3509 u32 ioc_state, doorbell;
3512 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3515 if (ioc->pci_error_recovery)
3518 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
3519 ioc_state = doorbell & MPI2_IOC_STATE_MASK;
3520 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
3521 ioc->name, __func__, ioc_state));
3523 switch (ioc_state) {
3524 case MPI2_IOC_STATE_READY:
3525 case MPI2_IOC_STATE_OPERATIONAL:
3529 if (doorbell & MPI2_DOORBELL_USED) {
3530 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT
3531 "unexpected doorbell activ!e\n", ioc->name));
3532 goto issue_diag_reset;
3535 if (ioc_state == MPI2_IOC_STATE_FAULT) {
3536 mpt2sas_base_fault_info(ioc, doorbell &
3537 MPI2_DOORBELL_DATA_MASK);
3538 goto issue_diag_reset;
3541 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
3542 timeout, sleep_flag);
3544 printk(MPT2SAS_ERR_FMT
3545 "%s: failed going to ready state (ioc_state=0x%x)\n",
3546 ioc->name, __func__, ioc_state);
3551 rc = _base_diag_reset(ioc, sleep_flag);
3556 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3557 * @ioc: per adapter object
3558 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3560 * Returns 0 for success, non-zero for failure.
3563 _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3565 Mpi2IOCFactsRequest_t mpi_request;
3566 Mpi2IOCFactsReply_t mpi_reply;
3567 struct mpt2sas_facts *facts;
3568 int mpi_reply_sz, mpi_request_sz, r;
3570 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3573 r = _base_wait_for_iocstate(ioc, 10, sleep_flag);
3575 printk(MPT2SAS_ERR_FMT "%s: failed getting to correct state\n",
3576 ioc->name, __func__);
3580 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3581 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3582 memset(&mpi_request, 0, mpi_request_sz);
3583 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3584 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3585 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3588 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3589 ioc->name, __func__, r);
3593 facts = &ioc->facts;
3594 memset(facts, 0, sizeof(struct mpt2sas_facts));
3595 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3596 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3597 facts->VP_ID = mpi_reply.VP_ID;
3598 facts->VF_ID = mpi_reply.VF_ID;
3599 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3600 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3601 facts->WhoInit = mpi_reply.WhoInit;
3602 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
3603 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
3604 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3605 facts->MaxReplyDescriptorPostQueueDepth =
3606 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3607 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3608 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3609 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3610 ioc->ir_firmware = 1;
3611 if ((facts->IOCCapabilities &
3612 MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE))
3613 ioc->rdpq_array_capable = 1;
3614 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3615 facts->IOCRequestFrameSize =
3616 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3617 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3618 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3619 ioc->shost->max_id = -1;
3620 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3621 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3622 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3623 facts->HighPriorityCredit =
3624 le16_to_cpu(mpi_reply.HighPriorityCredit);
3625 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3626 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3628 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
3629 "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
3630 facts->MaxChainDepth));
3631 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
3632 "reply frame size(%d)\n", ioc->name,
3633 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3638 * _base_send_ioc_init - send ioc_init to firmware
3639 * @ioc: per adapter object
3640 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3642 * Returns 0 for success, non-zero for failure.
3645 _base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3647 Mpi2IOCInitRequest_t mpi_request;
3648 Mpi2IOCInitReply_t mpi_reply;
3650 struct timeval current_time;
3652 u32 reply_post_free_array_sz = 0;
3653 Mpi2IOCInitRDPQArrayEntry *reply_post_free_array = NULL;
3654 dma_addr_t reply_post_free_array_dma;
3656 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
3659 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3660 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3661 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
3662 mpi_request.VF_ID = 0; /* TODO */
3663 mpi_request.VP_ID = 0;
3664 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3665 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3667 if (_base_is_controller_msix_enabled(ioc))
3668 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
3669 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3670 mpi_request.ReplyDescriptorPostQueueDepth =
3671 cpu_to_le16(ioc->reply_post_queue_depth);
3672 mpi_request.ReplyFreeQueueDepth =
3673 cpu_to_le16(ioc->reply_free_queue_depth);
3675 mpi_request.SenseBufferAddressHigh =
3676 cpu_to_le32((u64)ioc->sense_dma >> 32);
3677 mpi_request.SystemReplyAddressHigh =
3678 cpu_to_le32((u64)ioc->reply_dma >> 32);
3679 mpi_request.SystemRequestFrameBaseAddress =
3680 cpu_to_le64((u64)ioc->request_dma);
3681 mpi_request.ReplyFreeQueueAddress =
3682 cpu_to_le64((u64)ioc->reply_free_dma);
3684 if (ioc->rdpq_array_enable) {
3685 reply_post_free_array_sz = ioc->reply_queue_count *
3686 sizeof(Mpi2IOCInitRDPQArrayEntry);
3687 reply_post_free_array = pci_alloc_consistent(ioc->pdev,
3688 reply_post_free_array_sz, &reply_post_free_array_dma);
3689 if (!reply_post_free_array) {
3690 printk(MPT2SAS_ERR_FMT
3691 "reply_post_free_array: pci_alloc_consistent failed\n",
3696 memset(reply_post_free_array, 0, reply_post_free_array_sz);
3697 for (i = 0; i < ioc->reply_queue_count; i++)
3698 reply_post_free_array[i].RDPQBaseAddress =
3700 (u64)ioc->reply_post[i].reply_post_free_dma);
3701 mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
3702 mpi_request.ReplyDescriptorPostQueueAddress =
3703 cpu_to_le64((u64)reply_post_free_array_dma);
3705 mpi_request.ReplyDescriptorPostQueueAddress =
3706 cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
3709 /* This time stamp specifies number of milliseconds
3710 * since epoch ~ midnight January 1, 1970.
3712 do_gettimeofday(¤t_time);
3713 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3714 (current_time.tv_usec / 1000));
3716 if (ioc->logging_level & MPT_DEBUG_INIT) {
3720 mfp = (__le32 *)&mpi_request;
3721 printk(KERN_INFO "\toffset:data\n");
3722 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
3723 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
3724 le32_to_cpu(mfp[i]));
3727 r = _base_handshake_req_reply_wait(ioc,
3728 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3729 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3733 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3734 ioc->name, __func__, r);
3738 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3739 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
3740 mpi_reply.IOCLogInfo) {
3741 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
3746 if (reply_post_free_array)
3747 pci_free_consistent(ioc->pdev, reply_post_free_array_sz,
3748 reply_post_free_array,
3749 reply_post_free_array_dma);
3754 * mpt2sas_port_enable_done - command completion routine for port enable
3755 * @ioc: per adapter object
3756 * @smid: system request message index
3757 * @msix_index: MSIX table index supplied by the OS
3758 * @reply: reply message frame(lower 32bit addr)
3760 * Return 1 meaning mf should be freed from _base_interrupt
3761 * 0 means the mf is freed from this function.
3764 mpt2sas_port_enable_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3767 MPI2DefaultReply_t *mpi_reply;
3770 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3771 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
3774 if (ioc->port_enable_cmds.status == MPT2_CMD_NOT_USED)
3777 ioc->port_enable_cmds.status |= MPT2_CMD_COMPLETE;
3779 ioc->port_enable_cmds.status |= MPT2_CMD_REPLY_VALID;
3780 memcpy(ioc->port_enable_cmds.reply, mpi_reply,
3781 mpi_reply->MsgLength*4);
3783 ioc->port_enable_cmds.status &= ~MPT2_CMD_PENDING;
3785 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3787 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3788 ioc->port_enable_failed = 1;
3790 if (ioc->is_driver_loading) {
3791 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
3792 mpt2sas_port_enable_complete(ioc);
3795 ioc->start_scan_failed = ioc_status;
3796 ioc->start_scan = 0;
3800 complete(&ioc->port_enable_cmds.done);
3806 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3807 * @ioc: per adapter object
3808 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3810 * Returns 0 for success, non-zero for failure.
3813 _base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3815 Mpi2PortEnableRequest_t *mpi_request;
3816 Mpi2PortEnableReply_t *mpi_reply;
3817 unsigned long timeleft;
3822 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3824 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3825 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3826 ioc->name, __func__);
3830 smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3832 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3833 ioc->name, __func__);
3837 ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3838 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3839 ioc->port_enable_cmds.smid = smid;
3840 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3841 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3843 init_completion(&ioc->port_enable_cmds.done);
3844 mpt2sas_base_put_smid_default(ioc, smid);
3845 timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
3847 if (!(ioc->port_enable_cmds.status & MPT2_CMD_COMPLETE)) {
3848 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3849 ioc->name, __func__);
3850 _debug_dump_mf(mpi_request,
3851 sizeof(Mpi2PortEnableRequest_t)/4);
3852 if (ioc->port_enable_cmds.status & MPT2_CMD_RESET)
3858 mpi_reply = ioc->port_enable_cmds.reply;
3860 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3861 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3862 printk(MPT2SAS_ERR_FMT "%s: failed with (ioc_status=0x%08x)\n",
3863 ioc->name, __func__, ioc_status);
3868 ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
3869 printk(MPT2SAS_INFO_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
3870 "SUCCESS" : "FAILED"));
3875 * mpt2sas_port_enable - initiate firmware discovery (don't wait for reply)
3876 * @ioc: per adapter object
3878 * Returns 0 for success, non-zero for failure.
3881 mpt2sas_port_enable(struct MPT2SAS_ADAPTER *ioc)
3883 Mpi2PortEnableRequest_t *mpi_request;
3886 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3888 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3889 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3890 ioc->name, __func__);
3894 smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3896 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3897 ioc->name, __func__);
3901 ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3902 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3903 ioc->port_enable_cmds.smid = smid;
3904 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3905 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3907 mpt2sas_base_put_smid_default(ioc, smid);
3912 * _base_determine_wait_on_discovery - desposition
3913 * @ioc: per adapter object
3915 * Decide whether to wait on discovery to complete. Used to either
3916 * locate boot device, or report volumes ahead of physical devices.
3918 * Returns 1 for wait, 0 for don't wait
3921 _base_determine_wait_on_discovery(struct MPT2SAS_ADAPTER *ioc)
3923 /* We wait for discovery to complete if IR firmware is loaded.
3924 * The sas topology events arrive before PD events, so we need time to
3925 * turn on the bit in ioc->pd_handles to indicate PD
3926 * Also, it maybe required to report Volumes ahead of physical
3927 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
3929 if (ioc->ir_firmware)
3932 /* if no Bios, then we don't need to wait */
3933 if (!ioc->bios_pg3.BiosVersion)
3936 /* Bios is present, then we drop down here.
3938 * If there any entries in the Bios Page 2, then we wait
3939 * for discovery to complete.
3942 /* Current Boot Device */
3943 if ((ioc->bios_pg2.CurrentBootDeviceForm &
3944 MPI2_BIOSPAGE2_FORM_MASK) ==
3945 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3946 /* Request Boot Device */
3947 (ioc->bios_pg2.ReqBootDeviceForm &
3948 MPI2_BIOSPAGE2_FORM_MASK) ==
3949 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3950 /* Alternate Request Boot Device */
3951 (ioc->bios_pg2.ReqAltBootDeviceForm &
3952 MPI2_BIOSPAGE2_FORM_MASK) ==
3953 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
3961 * _base_unmask_events - turn on notification for this event
3962 * @ioc: per adapter object
3963 * @event: firmware event
3965 * The mask is stored in ioc->event_masks.
3968 _base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3975 desired_event = (1 << (event % 32));
3978 ioc->event_masks[0] &= ~desired_event;
3979 else if (event < 64)
3980 ioc->event_masks[1] &= ~desired_event;
3981 else if (event < 96)
3982 ioc->event_masks[2] &= ~desired_event;
3983 else if (event < 128)
3984 ioc->event_masks[3] &= ~desired_event;
3988 * _base_event_notification - send event notification
3989 * @ioc: per adapter object
3990 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3992 * Returns 0 for success, non-zero for failure.
3995 _base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3997 Mpi2EventNotificationRequest_t *mpi_request;
3998 unsigned long timeleft;
4003 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4006 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
4007 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
4008 ioc->name, __func__);
4012 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
4014 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
4015 ioc->name, __func__);
4018 ioc->base_cmds.status = MPT2_CMD_PENDING;
4019 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4020 ioc->base_cmds.smid = smid;
4021 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
4022 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4023 mpi_request->VF_ID = 0; /* TODO */
4024 mpi_request->VP_ID = 0;
4025 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4026 mpi_request->EventMasks[i] =
4027 cpu_to_le32(ioc->event_masks[i]);
4028 init_completion(&ioc->base_cmds.done);
4029 mpt2sas_base_put_smid_default(ioc, smid);
4030 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
4031 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
4032 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
4033 ioc->name, __func__);
4034 _debug_dump_mf(mpi_request,
4035 sizeof(Mpi2EventNotificationRequest_t)/4);
4036 if (ioc->base_cmds.status & MPT2_CMD_RESET)
4041 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
4042 ioc->name, __func__));
4043 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
4048 * mpt2sas_base_validate_event_type - validating event types
4049 * @ioc: per adapter object
4050 * @event: firmware event
4052 * This will turn on firmware event notification when application
4053 * ask for that event. We don't mask events that are already enabled.
4056 mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
4059 u32 event_mask, desired_event;
4060 u8 send_update_to_fw;
4062 for (i = 0, send_update_to_fw = 0; i <
4063 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
4064 event_mask = ~event_type[i];
4066 for (j = 0; j < 32; j++) {
4067 if (!(event_mask & desired_event) &&
4068 (ioc->event_masks[i] & desired_event)) {
4069 ioc->event_masks[i] &= ~desired_event;
4070 send_update_to_fw = 1;
4072 desired_event = (desired_event << 1);
4076 if (!send_update_to_fw)
4079 mutex_lock(&ioc->base_cmds.mutex);
4080 _base_event_notification(ioc, CAN_SLEEP);
4081 mutex_unlock(&ioc->base_cmds.mutex);
4085 * _base_diag_reset - the "big hammer" start of day reset
4086 * @ioc: per adapter object
4087 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4089 * Returns 0 for success, non-zero for failure.
4092 _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4094 u32 host_diagnostic;
4099 printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
4100 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
4105 /* Write magic sequence to WriteSequence register
4106 * Loop until in diagnostic mode
4108 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
4109 "sequence\n", ioc->name));
4110 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4111 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
4112 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
4113 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
4114 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
4115 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
4116 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
4119 if (sleep_flag == CAN_SLEEP)
4127 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4128 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
4129 "sequence: count(%d), host_diagnostic(0x%08x)\n",
4130 ioc->name, count, host_diagnostic));
4132 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
4134 hcb_size = readl(&ioc->chip->HCBSize);
4136 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
4138 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
4139 &ioc->chip->HostDiagnostic);
4141 /* This delay allows the chip PCIe hardware time to finish reset tasks*/
4142 if (sleep_flag == CAN_SLEEP)
4143 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
4145 mdelay(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
4147 /* Approximately 300 second max wait */
4148 for (count = 0; count < (300000000 /
4149 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
4151 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4153 if (host_diagnostic == 0xFFFFFFFF)
4155 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
4158 /* Wait to pass the second read delay window */
4159 if (sleep_flag == CAN_SLEEP)
4160 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4163 mdelay(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4167 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
4169 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
4170 "assuming the HCB Address points to good F/W\n",
4172 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
4173 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
4174 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
4176 drsprintk(ioc, printk(MPT2SAS_INFO_FMT
4177 "re-enable the HCDW\n", ioc->name));
4178 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
4179 &ioc->chip->HCBSize);
4182 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
4184 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
4185 &ioc->chip->HostDiagnostic);
4187 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
4188 "diagnostic register\n", ioc->name));
4189 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4191 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
4192 "READY state\n", ioc->name));
4193 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
4196 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
4197 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
4201 printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
4205 printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
4210 * _base_make_ioc_ready - put controller in READY state
4211 * @ioc: per adapter object
4212 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4213 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4215 * Returns 0 for success, non-zero for failure.
4218 _base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4219 enum reset_type type)
4224 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4227 if (ioc->pci_error_recovery)
4230 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4231 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
4232 ioc->name, __func__, ioc_state));
4234 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
4237 if (ioc_state & MPI2_DOORBELL_USED) {
4238 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
4239 "active!\n", ioc->name));
4240 goto issue_diag_reset;
4243 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4244 mpt2sas_base_fault_info(ioc, ioc_state &
4245 MPI2_DOORBELL_DATA_MASK);
4246 goto issue_diag_reset;
4249 if (type == FORCE_BIG_HAMMER)
4250 goto issue_diag_reset;
4252 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4253 if (!(_base_send_ioc_reset(ioc,
4254 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
4255 ioc->ioc_reset_count++;
4260 rc = _base_diag_reset(ioc, CAN_SLEEP);
4261 ioc->ioc_reset_count++;
4266 * _base_make_ioc_operational - put controller in OPERATIONAL state
4267 * @ioc: per adapter object
4268 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4270 * Returns 0 for success, non-zero for failure.
4273 _base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4276 unsigned long flags;
4279 struct _tr_list *delayed_tr, *delayed_tr_next;
4281 struct adapter_reply_queue *reply_q;
4282 long reply_post_free;
4283 u32 reply_post_free_sz, index = 0;
4285 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4288 /* clean the delayed target reset list */
4289 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4290 &ioc->delayed_tr_list, list) {
4291 list_del(&delayed_tr->list);
4295 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4296 &ioc->delayed_tr_volume_list, list) {
4297 list_del(&delayed_tr->list);
4301 /* initialize the scsi lookup free list */
4302 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4303 INIT_LIST_HEAD(&ioc->free_list);
4305 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
4306 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
4307 ioc->scsi_lookup[i].cb_idx = 0xFF;
4308 ioc->scsi_lookup[i].smid = smid;
4309 ioc->scsi_lookup[i].scmd = NULL;
4310 ioc->scsi_lookup[i].direct_io = 0;
4311 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
4315 /* hi-priority queue */
4316 INIT_LIST_HEAD(&ioc->hpr_free_list);
4317 smid = ioc->hi_priority_smid;
4318 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
4319 ioc->hpr_lookup[i].cb_idx = 0xFF;
4320 ioc->hpr_lookup[i].smid = smid;
4321 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
4322 &ioc->hpr_free_list);
4325 /* internal queue */
4326 INIT_LIST_HEAD(&ioc->internal_free_list);
4327 smid = ioc->internal_smid;
4328 for (i = 0; i < ioc->internal_depth; i++, smid++) {
4329 ioc->internal_lookup[i].cb_idx = 0xFF;
4330 ioc->internal_lookup[i].smid = smid;
4331 list_add_tail(&ioc->internal_lookup[i].tracker_list,
4332 &ioc->internal_free_list);
4336 INIT_LIST_HEAD(&ioc->free_chain_list);
4337 for (i = 0; i < ioc->chain_depth; i++)
4338 list_add_tail(&ioc->chain_lookup[i].tracker_list,
4339 &ioc->free_chain_list);
4341 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4343 /* initialize Reply Free Queue */
4344 for (i = 0, reply_address = (u32)ioc->reply_dma ;
4345 i < ioc->reply_free_queue_depth ; i++, reply_address +=
4347 ioc->reply_free[i] = cpu_to_le32(reply_address);
4349 /* initialize reply queues */
4350 if (ioc->is_driver_loading)
4351 _base_assign_reply_queues(ioc);
4353 /* initialize Reply Post Free Queue */
4354 reply_post_free_sz = ioc->reply_post_queue_depth *
4355 sizeof(Mpi2DefaultReplyDescriptor_t);
4356 reply_post_free = (long)ioc->reply_post[index].reply_post_free;
4357 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4358 reply_q->reply_post_host_index = 0;
4359 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
4361 for (i = 0; i < ioc->reply_post_queue_depth; i++)
4362 reply_q->reply_post_free[i].Words =
4363 cpu_to_le64(ULLONG_MAX);
4364 if (!_base_is_controller_msix_enabled(ioc))
4365 goto skip_init_reply_post_free_queue;
4367 * If RDPQ is enabled, switch to the next allocation.
4368 * Otherwise advance within the contiguous region.
4370 if (ioc->rdpq_array_enable)
4371 reply_post_free = (long)
4372 ioc->reply_post[++index].reply_post_free;
4374 reply_post_free += reply_post_free_sz;
4376 skip_init_reply_post_free_queue:
4378 r = _base_send_ioc_init(ioc, sleep_flag);
4382 /* initialize reply free host index */
4383 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
4384 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
4386 /* initialize reply post host index */
4387 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4388 writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
4389 &ioc->chip->ReplyPostHostIndex);
4390 if (!_base_is_controller_msix_enabled(ioc))
4391 goto skip_init_reply_post_host_index;
4394 skip_init_reply_post_host_index:
4396 _base_unmask_interrupts(ioc);
4398 r = _base_event_notification(ioc, sleep_flag);
4402 if (sleep_flag == CAN_SLEEP)
4403 _base_static_config_pages(ioc);
4406 if (ioc->is_driver_loading) {
4407 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
4410 le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
4411 MFG_PAGE10_HIDE_SSDS_MASK);
4412 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
4413 ioc->mfg_pg10_hide_flag = hide_flag;
4415 ioc->wait_for_discovery_to_complete =
4416 _base_determine_wait_on_discovery(ioc);
4417 return r; /* scan_start and scan_finished support */
4419 r = _base_send_port_enable(ioc, sleep_flag);
4427 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
4428 * @ioc: per adapter object
4433 mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
4435 struct pci_dev *pdev = ioc->pdev;
4437 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4440 if (ioc->chip_phys && ioc->chip) {
4441 _base_mask_interrupts(ioc);
4442 ioc->shost_recovery = 1;
4443 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4444 ioc->shost_recovery = 0;
4447 _base_free_irq(ioc);
4448 _base_disable_msix(ioc);
4450 if (ioc->chip_phys && ioc->chip)
4454 if (pci_is_enabled(pdev)) {
4455 pci_release_selected_regions(ioc->pdev, ioc->bars);
4456 pci_disable_pcie_error_reporting(pdev);
4457 pci_disable_device(pdev);
4463 * mpt2sas_base_attach - attach controller instance
4464 * @ioc: per adapter object
4466 * Returns 0 for success, non-zero for failure.
4469 mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
4472 int cpu_id, last_cpu_id = 0;
4474 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4477 /* setup cpu_msix_table */
4478 ioc->cpu_count = num_online_cpus();
4479 for_each_online_cpu(cpu_id)
4480 last_cpu_id = cpu_id;
4481 ioc->cpu_msix_table_sz = last_cpu_id + 1;
4482 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
4483 ioc->reply_queue_count = 1;
4484 if (!ioc->cpu_msix_table) {
4485 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
4486 "cpu_msix_table failed!!!\n", ioc->name));
4488 goto out_free_resources;
4491 if (ioc->is_warpdrive) {
4492 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
4493 sizeof(resource_size_t *), GFP_KERNEL);
4494 if (!ioc->reply_post_host_index) {
4495 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation "
4496 "for cpu_msix_table failed!!!\n", ioc->name));
4498 goto out_free_resources;
4502 ioc->rdpq_array_enable_assigned = 0;
4504 r = mpt2sas_base_map_resources(ioc);
4506 goto out_free_resources;
4508 if (ioc->is_warpdrive) {
4509 ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
4510 &ioc->chip->ReplyPostHostIndex;
4512 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
4513 ioc->reply_post_host_index[i] =
4514 (resource_size_t __iomem *)
4515 ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
4519 pci_set_drvdata(ioc->pdev, ioc->shost);
4520 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
4522 goto out_free_resources;
4524 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4526 goto out_free_resources;
4528 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
4529 sizeof(struct mpt2sas_port_facts), GFP_KERNEL);
4532 goto out_free_resources;
4535 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
4536 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
4538 goto out_free_resources;
4541 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
4543 goto out_free_resources;
4545 init_waitqueue_head(&ioc->reset_wq);
4546 /* allocate memory pd handle bitmask list */
4547 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
4548 if (ioc->facts.MaxDevHandle % 8)
4549 ioc->pd_handles_sz++;
4550 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
4552 if (!ioc->pd_handles) {
4554 goto out_free_resources;
4556 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
4558 if (!ioc->blocking_handles) {
4560 goto out_free_resources;
4562 ioc->fwfault_debug = mpt2sas_fwfault_debug;
4564 /* base internal command bits */
4565 mutex_init(&ioc->base_cmds.mutex);
4566 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4567 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
4569 /* port_enable command bits */
4570 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4571 ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
4573 /* transport internal command bits */
4574 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4575 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
4576 mutex_init(&ioc->transport_cmds.mutex);
4578 /* scsih internal command bits */
4579 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4580 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
4581 mutex_init(&ioc->scsih_cmds.mutex);
4583 /* task management internal command bits */
4584 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4585 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4586 mutex_init(&ioc->tm_cmds.mutex);
4588 /* config page internal command bits */
4589 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4590 ioc->config_cmds.status = MPT2_CMD_NOT_USED;
4591 mutex_init(&ioc->config_cmds.mutex);
4593 /* ctl module internal command bits */
4594 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4595 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4596 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
4597 mutex_init(&ioc->ctl_cmds.mutex);
4599 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4600 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4601 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
4602 !ioc->ctl_cmds.sense) {
4604 goto out_free_resources;
4607 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4608 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4609 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
4611 goto out_free_resources;
4614 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4615 ioc->event_masks[i] = -1;
4617 /* here we enable the events we care about */
4618 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
4619 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
4620 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
4621 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4622 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
4623 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
4624 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
4625 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
4626 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
4627 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
4628 _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
4629 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
4631 goto out_free_resources;
4633 ioc->non_operational_loop = 0;
4639 ioc->remove_host = 1;
4640 mpt2sas_base_free_resources(ioc);
4641 _base_release_memory_pools(ioc);
4642 pci_set_drvdata(ioc->pdev, NULL);
4643 kfree(ioc->cpu_msix_table);
4644 if (ioc->is_warpdrive)
4645 kfree(ioc->reply_post_host_index);
4646 kfree(ioc->pd_handles);
4647 kfree(ioc->blocking_handles);
4648 kfree(ioc->tm_cmds.reply);
4649 kfree(ioc->transport_cmds.reply);
4650 kfree(ioc->scsih_cmds.reply);
4651 kfree(ioc->config_cmds.reply);
4652 kfree(ioc->base_cmds.reply);
4653 kfree(ioc->port_enable_cmds.reply);
4654 kfree(ioc->ctl_cmds.reply);
4655 kfree(ioc->ctl_cmds.sense);
4657 ioc->ctl_cmds.reply = NULL;
4658 ioc->base_cmds.reply = NULL;
4659 ioc->tm_cmds.reply = NULL;
4660 ioc->scsih_cmds.reply = NULL;
4661 ioc->transport_cmds.reply = NULL;
4662 ioc->config_cmds.reply = NULL;
4669 * mpt2sas_base_detach - remove controller instance
4670 * @ioc: per adapter object
4675 mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
4678 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
4681 mpt2sas_base_stop_watchdog(ioc);
4682 mpt2sas_base_free_resources(ioc);
4683 _base_release_memory_pools(ioc);
4684 pci_set_drvdata(ioc->pdev, NULL);
4685 kfree(ioc->cpu_msix_table);
4686 if (ioc->is_warpdrive)
4687 kfree(ioc->reply_post_host_index);
4688 kfree(ioc->pd_handles);
4689 kfree(ioc->blocking_handles);
4691 kfree(ioc->ctl_cmds.reply);
4692 kfree(ioc->ctl_cmds.sense);
4693 kfree(ioc->base_cmds.reply);
4694 kfree(ioc->port_enable_cmds.reply);
4695 kfree(ioc->tm_cmds.reply);
4696 kfree(ioc->transport_cmds.reply);
4697 kfree(ioc->scsih_cmds.reply);
4698 kfree(ioc->config_cmds.reply);
4702 * _base_reset_handler - reset callback handler (for base)
4703 * @ioc: per adapter object
4704 * @reset_phase: phase
4706 * The handler for doing any required cleanup or initialization.
4708 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
4709 * MPT2_IOC_DONE_RESET
4714 _base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
4716 mpt2sas_scsih_reset_handler(ioc, reset_phase);
4717 mpt2sas_ctl_reset_handler(ioc, reset_phase);
4718 switch (reset_phase) {
4719 case MPT2_IOC_PRE_RESET:
4720 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4721 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
4723 case MPT2_IOC_AFTER_RESET:
4724 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4725 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
4726 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
4727 ioc->transport_cmds.status |= MPT2_CMD_RESET;
4728 mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
4729 complete(&ioc->transport_cmds.done);
4731 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
4732 ioc->base_cmds.status |= MPT2_CMD_RESET;
4733 mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
4734 complete(&ioc->base_cmds.done);
4736 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
4737 ioc->port_enable_failed = 1;
4738 ioc->port_enable_cmds.status |= MPT2_CMD_RESET;
4739 mpt2sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
4740 if (ioc->is_driver_loading) {
4741 ioc->start_scan_failed =
4742 MPI2_IOCSTATUS_INTERNAL_ERROR;
4743 ioc->start_scan = 0;
4744 ioc->port_enable_cmds.status =
4747 complete(&ioc->port_enable_cmds.done);
4750 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
4751 ioc->config_cmds.status |= MPT2_CMD_RESET;
4752 mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
4753 ioc->config_cmds.smid = USHRT_MAX;
4754 complete(&ioc->config_cmds.done);
4757 case MPT2_IOC_DONE_RESET:
4758 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
4759 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
4765 * _wait_for_commands_to_complete - reset controller
4766 * @ioc: Pointer to MPT_ADAPTER structure
4767 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4769 * This function waiting(3s) for all pending commands to complete
4770 * prior to putting controller in reset.
4773 _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4776 unsigned long flags;
4779 ioc->pending_io_count = 0;
4780 if (sleep_flag != CAN_SLEEP)
4783 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4784 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
4787 /* pending command count */
4788 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4789 for (i = 0; i < ioc->scsiio_depth; i++)
4790 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
4791 ioc->pending_io_count++;
4792 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4794 if (!ioc->pending_io_count)
4797 /* wait for pending commands to complete */
4798 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
4802 * mpt2sas_base_hard_reset_handler - reset controller
4803 * @ioc: Pointer to MPT_ADAPTER structure
4804 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4805 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4807 * Returns 0 for success, non-zero for failure.
4810 mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4811 enum reset_type type)
4814 unsigned long flags;
4816 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
4819 if (ioc->pci_error_recovery) {
4820 printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
4821 ioc->name, __func__);
4826 if (mpt2sas_fwfault_debug)
4827 mpt2sas_halt_firmware(ioc);
4829 /* TODO - What we really should be doing is pulling
4830 * out all the code associated with NO_SLEEP; its never used.
4831 * That is legacy code from mpt fusion driver, ported over.
4832 * I will leave this BUG_ON here for now till its been resolved.
4834 BUG_ON(sleep_flag == NO_SLEEP);
4836 /* wait for an active reset in progress to complete */
4837 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4840 } while (ioc->shost_recovery == 1);
4841 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
4843 return ioc->ioc_reset_in_progress_status;
4846 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4847 ioc->shost_recovery = 1;
4848 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4850 _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
4851 _wait_for_commands_to_complete(ioc, sleep_flag);
4852 _base_mask_interrupts(ioc);
4853 r = _base_make_ioc_ready(ioc, sleep_flag, type);
4856 _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
4858 /* If this hard reset is called while port enable is active, then
4859 * there is no reason to call make_ioc_operational
4861 if (ioc->is_driver_loading && ioc->port_enable_failed) {
4862 ioc->remove_host = 1;
4867 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
4871 if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
4872 panic("%s: Issue occurred with flashing controller firmware."
4873 "Please reboot the system and ensure that the correct"
4874 " firmware version is running\n", ioc->name);
4876 r = _base_make_ioc_operational(ioc, sleep_flag);
4878 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
4880 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
4881 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4883 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4884 ioc->ioc_reset_in_progress_status = r;
4885 ioc->shost_recovery = 0;
4886 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4887 mutex_unlock(&ioc->reset_in_progress_mutex);
4890 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,