2 * This is the Fusion MPT base driver providing common API layer interface
3 * for access to MPT (Message Passing Technology) firmware.
5 * This code is based on drivers/scsi/mpt3sas/mpt3sas_base.c
6 * Copyright (C) 2012-2014 LSI Corporation
7 * Copyright (C) 2013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/slab.h>
51 #include <linux/types.h>
52 #include <linux/pci.h>
53 #include <linux/kdev_t.h>
54 #include <linux/blkdev.h>
55 #include <linux/delay.h>
56 #include <linux/interrupt.h>
57 #include <linux/dma-mapping.h>
59 #include <linux/time.h>
60 #include <linux/kthread.h>
61 #include <linux/aer.h>
64 #include "mpt3sas_base.h"
66 static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
69 #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
71 /* maximum controller queue depth */
72 #define MAX_HBA_QUEUE_DEPTH 30000
73 #define MAX_CHAIN_DEPTH 100000
74 static int max_queue_depth = -1;
75 module_param(max_queue_depth, int, 0);
76 MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
78 static int max_sgl_entries = -1;
79 module_param(max_sgl_entries, int, 0);
80 MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
82 static int msix_disable = -1;
83 module_param(msix_disable, int, 0);
84 MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
86 static int max_msix_vectors = -1;
87 module_param(max_msix_vectors, int, 0);
88 MODULE_PARM_DESC(max_msix_vectors,
91 static int mpt3sas_fwfault_debug;
92 MODULE_PARM_DESC(mpt3sas_fwfault_debug,
93 " enable detection of firmware fault and halt firmware - (default=0)");
96 _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc, int sleep_flag);
99 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
103 _scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
105 int ret = param_set_int(val, kp);
106 struct MPT3SAS_ADAPTER *ioc;
111 pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
112 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
113 ioc->fwfault_debug = mpt3sas_fwfault_debug;
116 module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
117 param_get_int, &mpt3sas_fwfault_debug, 0644);
120 * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
121 * @arg: input argument, used to derive ioc
123 * Return 0 if controller is removed from pci subsystem.
124 * Return -1 for other case.
126 static int mpt3sas_remove_dead_ioc_func(void *arg)
128 struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
129 struct pci_dev *pdev;
137 pci_stop_and_remove_bus_device_locked(pdev);
142 * _base_fault_reset_work - workq handling ioc fault conditions
143 * @work: input argument, used to derive ioc
149 _base_fault_reset_work(struct work_struct *work)
151 struct MPT3SAS_ADAPTER *ioc =
152 container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
156 struct task_struct *p;
159 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
160 if (ioc->shost_recovery)
162 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
164 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
165 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
166 pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
170 * Call _scsih_flush_pending_cmds callback so that we flush all
171 * pending commands back to OS. This call is required to aovid
172 * deadlock at block layer. Dead IOC will fail to do diag reset,
173 * and this call is safe since dead ioc will never return any
174 * command back from HW.
176 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
178 * Set remove_host flag early since kernel thread will
179 * take some time to execute.
181 ioc->remove_host = 1;
182 /*Remove the Dead Host */
183 p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
184 "mpt3sas_dead_ioc_%d", ioc->id);
187 "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
188 ioc->name, __func__);
191 "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
192 ioc->name, __func__);
193 return; /* don't rearm timer */
196 if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
197 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
199 pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
200 __func__, (rc == 0) ? "success" : "failed");
201 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
202 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
203 mpt3sas_base_fault_info(ioc, doorbell &
204 MPI2_DOORBELL_DATA_MASK);
205 if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
206 MPI2_IOC_STATE_OPERATIONAL)
207 return; /* don't rearm timer */
210 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
212 if (ioc->fault_reset_work_q)
213 queue_delayed_work(ioc->fault_reset_work_q,
214 &ioc->fault_reset_work,
215 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
216 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
220 * mpt3sas_base_start_watchdog - start the fault_reset_work_q
221 * @ioc: per adapter object
227 mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
231 if (ioc->fault_reset_work_q)
234 /* initialize fault polling */
236 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
237 snprintf(ioc->fault_reset_work_q_name,
238 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
239 ioc->fault_reset_work_q =
240 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
241 if (!ioc->fault_reset_work_q) {
242 pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
243 ioc->name, __func__, __LINE__);
246 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
247 if (ioc->fault_reset_work_q)
248 queue_delayed_work(ioc->fault_reset_work_q,
249 &ioc->fault_reset_work,
250 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
251 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
255 * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
256 * @ioc: per adapter object
262 mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
265 struct workqueue_struct *wq;
267 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
268 wq = ioc->fault_reset_work_q;
269 ioc->fault_reset_work_q = NULL;
270 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
272 if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
274 destroy_workqueue(wq);
279 * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
280 * @ioc: per adapter object
281 * @fault_code: fault code
286 mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
288 pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
289 ioc->name, fault_code);
293 * mpt3sas_halt_firmware - halt's mpt controller firmware
294 * @ioc: per adapter object
296 * For debugging timeout related issues. Writing 0xCOFFEE00
297 * to the doorbell register will halt controller firmware. With
298 * the purpose to stop both driver and firmware, the enduser can
299 * obtain a ring buffer from controller UART.
302 mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
306 if (!ioc->fwfault_debug)
311 doorbell = readl(&ioc->chip->Doorbell);
312 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
313 mpt3sas_base_fault_info(ioc , doorbell);
315 writel(0xC0FFEE00, &ioc->chip->Doorbell);
316 pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
320 if (ioc->fwfault_debug == 2)
324 panic("panic in %s\n", __func__);
328 * _base_sas_ioc_info - verbose translation of the ioc status
329 * @ioc: per adapter object
330 * @mpi_reply: reply mf payload returned from firmware
331 * @request_hdr: request mf
336 _base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
337 MPI2RequestHeader_t *request_hdr)
339 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
343 char *func_str = NULL;
345 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
346 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
347 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
348 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
351 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
354 switch (ioc_status) {
356 /****************************************************************************
357 * Common IOCStatus values for all replies
358 ****************************************************************************/
360 case MPI2_IOCSTATUS_INVALID_FUNCTION:
361 desc = "invalid function";
363 case MPI2_IOCSTATUS_BUSY:
366 case MPI2_IOCSTATUS_INVALID_SGL:
367 desc = "invalid sgl";
369 case MPI2_IOCSTATUS_INTERNAL_ERROR:
370 desc = "internal error";
372 case MPI2_IOCSTATUS_INVALID_VPID:
373 desc = "invalid vpid";
375 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
376 desc = "insufficient resources";
378 case MPI2_IOCSTATUS_INVALID_FIELD:
379 desc = "invalid field";
381 case MPI2_IOCSTATUS_INVALID_STATE:
382 desc = "invalid state";
384 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
385 desc = "op state not supported";
388 /****************************************************************************
389 * Config IOCStatus values
390 ****************************************************************************/
392 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
393 desc = "config invalid action";
395 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
396 desc = "config invalid type";
398 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
399 desc = "config invalid page";
401 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
402 desc = "config invalid data";
404 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
405 desc = "config no defaults";
407 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
408 desc = "config cant commit";
411 /****************************************************************************
413 ****************************************************************************/
415 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
416 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
417 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
418 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
419 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
420 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
421 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
422 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
423 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
424 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
425 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
426 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
429 /****************************************************************************
430 * For use by SCSI Initiator and SCSI Target end-to-end data protection
431 ****************************************************************************/
433 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
434 desc = "eedp guard error";
436 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
437 desc = "eedp ref tag error";
439 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
440 desc = "eedp app tag error";
443 /****************************************************************************
445 ****************************************************************************/
447 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
448 desc = "target invalid io index";
450 case MPI2_IOCSTATUS_TARGET_ABORTED:
451 desc = "target aborted";
453 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
454 desc = "target no conn retryable";
456 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
457 desc = "target no connection";
459 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
460 desc = "target xfer count mismatch";
462 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
463 desc = "target data offset error";
465 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
466 desc = "target too much write data";
468 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
469 desc = "target iu too short";
471 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
472 desc = "target ack nak timeout";
474 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
475 desc = "target nak received";
478 /****************************************************************************
479 * Serial Attached SCSI values
480 ****************************************************************************/
482 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
483 desc = "smp request failed";
485 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
486 desc = "smp data overrun";
489 /****************************************************************************
490 * Diagnostic Buffer Post / Diagnostic Release values
491 ****************************************************************************/
493 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
494 desc = "diagnostic released";
503 switch (request_hdr->Function) {
504 case MPI2_FUNCTION_CONFIG:
505 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
506 func_str = "config_page";
508 case MPI2_FUNCTION_SCSI_TASK_MGMT:
509 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
510 func_str = "task_mgmt";
512 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
513 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
514 func_str = "sas_iounit_ctl";
516 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
517 frame_sz = sizeof(Mpi2SepRequest_t);
518 func_str = "enclosure";
520 case MPI2_FUNCTION_IOC_INIT:
521 frame_sz = sizeof(Mpi2IOCInitRequest_t);
522 func_str = "ioc_init";
524 case MPI2_FUNCTION_PORT_ENABLE:
525 frame_sz = sizeof(Mpi2PortEnableRequest_t);
526 func_str = "port_enable";
528 case MPI2_FUNCTION_SMP_PASSTHROUGH:
529 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
530 func_str = "smp_passthru";
534 func_str = "unknown";
538 pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
539 ioc->name, desc, ioc_status, request_hdr, func_str);
541 _debug_dump_mf(request_hdr, frame_sz/4);
545 * _base_display_event_data - verbose translation of firmware asyn events
546 * @ioc: per adapter object
547 * @mpi_reply: reply mf payload returned from firmware
552 _base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
553 Mpi2EventNotificationReply_t *mpi_reply)
558 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
561 event = le16_to_cpu(mpi_reply->Event);
564 case MPI2_EVENT_LOG_DATA:
567 case MPI2_EVENT_STATE_CHANGE:
568 desc = "Status Change";
570 case MPI2_EVENT_HARD_RESET_RECEIVED:
571 desc = "Hard Reset Received";
573 case MPI2_EVENT_EVENT_CHANGE:
574 desc = "Event Change";
576 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
577 desc = "Device Status Change";
579 case MPI2_EVENT_IR_OPERATION_STATUS:
580 desc = "IR Operation Status";
582 case MPI2_EVENT_SAS_DISCOVERY:
584 Mpi2EventDataSasDiscovery_t *event_data =
585 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
586 pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
587 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
589 if (event_data->DiscoveryStatus)
590 pr_info("discovery_status(0x%08x)",
591 le32_to_cpu(event_data->DiscoveryStatus));
595 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
596 desc = "SAS Broadcast Primitive";
598 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
599 desc = "SAS Init Device Status Change";
601 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
602 desc = "SAS Init Table Overflow";
604 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
605 desc = "SAS Topology Change List";
607 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
608 desc = "SAS Enclosure Device Status Change";
610 case MPI2_EVENT_IR_VOLUME:
613 case MPI2_EVENT_IR_PHYSICAL_DISK:
614 desc = "IR Physical Disk";
616 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
617 desc = "IR Configuration Change List";
619 case MPI2_EVENT_LOG_ENTRY_ADDED:
620 desc = "Log Entry Added";
622 case MPI2_EVENT_TEMP_THRESHOLD:
623 desc = "Temperature Threshold";
630 pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
634 * _base_sas_log_info - verbose translation of firmware log info
635 * @ioc: per adapter object
636 * @log_info: log info
641 _base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
652 union loginfo_type sas_loginfo;
653 char *originator_str = NULL;
655 sas_loginfo.loginfo = log_info;
656 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
659 /* each nexus loss loginfo */
660 if (log_info == 0x31170000)
663 /* eat the loginfos associated with task aborts */
664 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
665 0x31140000 || log_info == 0x31130000))
668 switch (sas_loginfo.dw.originator) {
670 originator_str = "IOP";
673 originator_str = "PL";
676 originator_str = "IR";
681 "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
683 originator_str, sas_loginfo.dw.code,
684 sas_loginfo.dw.subcode);
688 * _base_display_reply_info -
689 * @ioc: per adapter object
690 * @smid: system request message index
691 * @msix_index: MSIX table index supplied by the OS
692 * @reply: reply message frame(lower 32bit addr)
697 _base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
700 MPI2DefaultReply_t *mpi_reply;
704 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
705 if (unlikely(!mpi_reply)) {
706 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
707 ioc->name, __FILE__, __LINE__, __func__);
710 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
712 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
713 (ioc->logging_level & MPT_DEBUG_REPLY)) {
714 _base_sas_ioc_info(ioc , mpi_reply,
715 mpt3sas_base_get_msg_frame(ioc, smid));
718 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
719 loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
720 _base_sas_log_info(ioc, loginfo);
723 if (ioc_status || loginfo) {
724 ioc_status &= MPI2_IOCSTATUS_MASK;
725 mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
730 * mpt3sas_base_done - base internal command completion routine
731 * @ioc: per adapter object
732 * @smid: system request message index
733 * @msix_index: MSIX table index supplied by the OS
734 * @reply: reply message frame(lower 32bit addr)
736 * Return 1 meaning mf should be freed from _base_interrupt
737 * 0 means the mf is freed from this function.
740 mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
743 MPI2DefaultReply_t *mpi_reply;
745 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
746 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
749 if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
752 ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
754 ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
755 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
757 ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
759 complete(&ioc->base_cmds.done);
764 * _base_async_event - main callback handler for firmware asyn events
765 * @ioc: per adapter object
766 * @msix_index: MSIX table index supplied by the OS
767 * @reply: reply message frame(lower 32bit addr)
769 * Return 1 meaning mf should be freed from _base_interrupt
770 * 0 means the mf is freed from this function.
773 _base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
775 Mpi2EventNotificationReply_t *mpi_reply;
776 Mpi2EventAckRequest_t *ack_request;
779 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
782 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
785 _base_display_event_data(ioc, mpi_reply);
787 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
789 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
791 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
792 ioc->name, __func__);
796 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
797 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
798 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
799 ack_request->Event = mpi_reply->Event;
800 ack_request->EventContext = mpi_reply->EventContext;
801 ack_request->VF_ID = 0; /* TODO */
802 ack_request->VP_ID = 0;
803 mpt3sas_base_put_smid_default(ioc, smid);
807 /* scsih callback handler */
808 mpt3sas_scsih_event_callback(ioc, msix_index, reply);
810 /* ctl callback handler */
811 mpt3sas_ctl_event_callback(ioc, msix_index, reply);
817 * _base_get_cb_idx - obtain the callback index
818 * @ioc: per adapter object
819 * @smid: system request message index
821 * Return callback index.
824 _base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
829 if (smid < ioc->hi_priority_smid) {
831 cb_idx = ioc->scsi_lookup[i].cb_idx;
832 } else if (smid < ioc->internal_smid) {
833 i = smid - ioc->hi_priority_smid;
834 cb_idx = ioc->hpr_lookup[i].cb_idx;
835 } else if (smid <= ioc->hba_queue_depth) {
836 i = smid - ioc->internal_smid;
837 cb_idx = ioc->internal_lookup[i].cb_idx;
844 * _base_mask_interrupts - disable interrupts
845 * @ioc: per adapter object
847 * Disabling ResetIRQ, Reply and Doorbell Interrupts
852 _base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
856 ioc->mask_interrupts = 1;
857 him_register = readl(&ioc->chip->HostInterruptMask);
858 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
859 writel(him_register, &ioc->chip->HostInterruptMask);
860 readl(&ioc->chip->HostInterruptMask);
864 * _base_unmask_interrupts - enable interrupts
865 * @ioc: per adapter object
867 * Enabling only Reply Interrupts
872 _base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
876 him_register = readl(&ioc->chip->HostInterruptMask);
877 him_register &= ~MPI2_HIM_RIM;
878 writel(him_register, &ioc->chip->HostInterruptMask);
879 ioc->mask_interrupts = 0;
882 union reply_descriptor {
891 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
892 * @irq: irq number (not used)
893 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
894 * @r: pt_regs pointer (not used)
896 * Return IRQ_HANDLE if processed, else IRQ_NONE.
899 _base_interrupt(int irq, void *bus_id)
901 struct adapter_reply_queue *reply_q = bus_id;
902 union reply_descriptor rd;
904 u8 request_desript_type;
908 u8 msix_index = reply_q->msix_index;
909 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
910 Mpi2ReplyDescriptorsUnion_t *rpf;
913 if (ioc->mask_interrupts)
916 if (!atomic_add_unless(&reply_q->busy, 1, 1))
919 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
920 request_desript_type = rpf->Default.ReplyFlags
921 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
922 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
923 atomic_dec(&reply_q->busy);
930 rd.word = le64_to_cpu(rpf->Words);
931 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
934 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
935 if (request_desript_type ==
936 MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
937 request_desript_type ==
938 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
939 cb_idx = _base_get_cb_idx(ioc, smid);
940 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
941 (likely(mpt_callbacks[cb_idx] != NULL))) {
942 rc = mpt_callbacks[cb_idx](ioc, smid,
945 mpt3sas_base_free_smid(ioc, smid);
947 } else if (request_desript_type ==
948 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
950 rpf->AddressReply.ReplyFrameAddress);
951 if (reply > ioc->reply_dma_max_address ||
952 reply < ioc->reply_dma_min_address)
955 cb_idx = _base_get_cb_idx(ioc, smid);
956 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
957 (likely(mpt_callbacks[cb_idx] != NULL))) {
958 rc = mpt_callbacks[cb_idx](ioc, smid,
961 _base_display_reply_info(ioc,
962 smid, msix_index, reply);
964 mpt3sas_base_free_smid(ioc,
968 _base_async_event(ioc, msix_index, reply);
971 /* reply free queue handling */
973 ioc->reply_free_host_index =
974 (ioc->reply_free_host_index ==
975 (ioc->reply_free_queue_depth - 1)) ?
976 0 : ioc->reply_free_host_index + 1;
977 ioc->reply_free[ioc->reply_free_host_index] =
980 writel(ioc->reply_free_host_index,
981 &ioc->chip->ReplyFreeHostIndex);
985 rpf->Words = cpu_to_le64(ULLONG_MAX);
986 reply_q->reply_post_host_index =
987 (reply_q->reply_post_host_index ==
988 (ioc->reply_post_queue_depth - 1)) ? 0 :
989 reply_q->reply_post_host_index + 1;
990 request_desript_type =
991 reply_q->reply_post_free[reply_q->reply_post_host_index].
992 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
994 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
996 if (!reply_q->reply_post_host_index)
997 rpf = reply_q->reply_post_free;
1004 if (!completed_cmds) {
1005 atomic_dec(&reply_q->busy);
1011 /* Update Reply Post Host Index.
1012 * For those HBA's which support combined reply queue feature
1013 * 1. Get the correct Supplemental Reply Post Host Index Register.
1014 * i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
1015 * Index Register address bank i.e replyPostRegisterIndex[],
1016 * 2. Then update this register with new reply host index value
1017 * in ReplyPostIndex field and the MSIxIndex field with
1018 * msix_index value reduced to a value between 0 and 7,
1019 * using a modulo 8 operation. Since each Supplemental Reply Post
1020 * Host Index Register supports 8 MSI-X vectors.
1022 * For other HBA's just update the Reply Post Host Index register with
1023 * new reply host index value in ReplyPostIndex Field and msix_index
1024 * value in MSIxIndex field.
1026 if (ioc->msix96_vector)
1027 writel(reply_q->reply_post_host_index | ((msix_index & 7) <<
1028 MPI2_RPHI_MSIX_INDEX_SHIFT),
1029 ioc->replyPostRegisterIndex[msix_index/8]);
1031 writel(reply_q->reply_post_host_index | (msix_index <<
1032 MPI2_RPHI_MSIX_INDEX_SHIFT),
1033 &ioc->chip->ReplyPostHostIndex);
1034 atomic_dec(&reply_q->busy);
1039 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1040 * @ioc: per adapter object
1044 _base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
1046 return (ioc->facts.IOCCapabilities &
1047 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1051 * mpt3sas_base_flush_reply_queues - flushing the MSIX reply queues
1052 * @ioc: per adapter object
1053 * Context: ISR conext
1055 * Called when a Task Management request has completed. We want
1056 * to flush the other reply queues so all the outstanding IO has been
1057 * completed back to OS before we process the TM completetion.
1062 mpt3sas_base_flush_reply_queues(struct MPT3SAS_ADAPTER *ioc)
1064 struct adapter_reply_queue *reply_q;
1066 /* If MSIX capability is turned off
1067 * then multi-queues are not enabled
1069 if (!_base_is_controller_msix_enabled(ioc))
1072 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1073 if (ioc->shost_recovery)
1075 /* TMs are on msix_index == 0 */
1076 if (reply_q->msix_index == 0)
1078 _base_interrupt(reply_q->vector, (void *)reply_q);
1083 * mpt3sas_base_release_callback_handler - clear interrupt callback handler
1084 * @cb_idx: callback index
1089 mpt3sas_base_release_callback_handler(u8 cb_idx)
1091 mpt_callbacks[cb_idx] = NULL;
1095 * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
1096 * @cb_func: callback function
1101 mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1105 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1106 if (mpt_callbacks[cb_idx] == NULL)
1109 mpt_callbacks[cb_idx] = cb_func;
1114 * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
1119 mpt3sas_base_initialize_callback_handler(void)
1123 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1124 mpt3sas_base_release_callback_handler(cb_idx);
1129 * _base_build_zero_len_sge - build zero length sg entry
1130 * @ioc: per adapter object
1131 * @paddr: virtual address for SGE
1133 * Create a zero length scatter gather entry to insure the IOCs hardware has
1134 * something to use if the target device goes brain dead and tries
1135 * to send data even when none is asked for.
1140 _base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1142 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1143 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1144 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1145 MPI2_SGE_FLAGS_SHIFT);
1146 ioc->base_add_sg_single(paddr, flags_length, -1);
1150 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1151 * @paddr: virtual address for SGE
1152 * @flags_length: SGE flags and data transfer length
1153 * @dma_addr: Physical address
1158 _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1160 Mpi2SGESimple32_t *sgel = paddr;
1162 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1163 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1164 sgel->FlagsLength = cpu_to_le32(flags_length);
1165 sgel->Address = cpu_to_le32(dma_addr);
1170 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1171 * @paddr: virtual address for SGE
1172 * @flags_length: SGE flags and data transfer length
1173 * @dma_addr: Physical address
1178 _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1180 Mpi2SGESimple64_t *sgel = paddr;
1182 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1183 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1184 sgel->FlagsLength = cpu_to_le32(flags_length);
1185 sgel->Address = cpu_to_le64(dma_addr);
1189 * _base_get_chain_buffer_tracker - obtain chain tracker
1190 * @ioc: per adapter object
1191 * @smid: smid associated to an IO request
1193 * Returns chain tracker(from ioc->free_chain_list)
1195 static struct chain_tracker *
1196 _base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1198 struct chain_tracker *chain_req;
1199 unsigned long flags;
1201 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1202 if (list_empty(&ioc->free_chain_list)) {
1203 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1204 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1205 "chain buffers not available\n", ioc->name));
1208 chain_req = list_entry(ioc->free_chain_list.next,
1209 struct chain_tracker, tracker_list);
1210 list_del_init(&chain_req->tracker_list);
1211 list_add_tail(&chain_req->tracker_list,
1212 &ioc->scsi_lookup[smid - 1].chain_list);
1213 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1219 * _base_build_sg - build generic sg
1220 * @ioc: per adapter object
1221 * @psge: virtual address for SGE
1222 * @data_out_dma: physical address for WRITES
1223 * @data_out_sz: data xfer size for WRITES
1224 * @data_in_dma: physical address for READS
1225 * @data_in_sz: data xfer size for READS
1230 _base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
1231 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1236 if (!data_out_sz && !data_in_sz) {
1237 _base_build_zero_len_sge(ioc, psge);
1241 if (data_out_sz && data_in_sz) {
1242 /* WRITE sgel first */
1243 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1244 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1245 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1246 ioc->base_add_sg_single(psge, sgl_flags |
1247 data_out_sz, data_out_dma);
1250 psge += ioc->sge_size;
1252 /* READ sgel last */
1253 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1254 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1255 MPI2_SGE_FLAGS_END_OF_LIST);
1256 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1257 ioc->base_add_sg_single(psge, sgl_flags |
1258 data_in_sz, data_in_dma);
1259 } else if (data_out_sz) /* WRITE */ {
1260 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1261 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1262 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
1263 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1264 ioc->base_add_sg_single(psge, sgl_flags |
1265 data_out_sz, data_out_dma);
1266 } else if (data_in_sz) /* READ */ {
1267 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1268 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1269 MPI2_SGE_FLAGS_END_OF_LIST);
1270 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1271 ioc->base_add_sg_single(psge, sgl_flags |
1272 data_in_sz, data_in_dma);
1276 /* IEEE format sgls */
1279 * _base_add_sg_single_ieee - add sg element for IEEE format
1280 * @paddr: virtual address for SGE
1282 * @chain_offset: number of 128 byte elements from start of segment
1283 * @length: data transfer length
1284 * @dma_addr: Physical address
1289 _base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
1290 dma_addr_t dma_addr)
1292 Mpi25IeeeSgeChain64_t *sgel = paddr;
1294 sgel->Flags = flags;
1295 sgel->NextChainOffset = chain_offset;
1296 sgel->Length = cpu_to_le32(length);
1297 sgel->Address = cpu_to_le64(dma_addr);
1301 * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
1302 * @ioc: per adapter object
1303 * @paddr: virtual address for SGE
1305 * Create a zero length scatter gather entry to insure the IOCs hardware has
1306 * something to use if the target device goes brain dead and tries
1307 * to send data even when none is asked for.
1312 _base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1314 u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1315 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
1316 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
1317 _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
1321 * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
1322 * @ioc: per adapter object
1323 * @scmd: scsi command
1324 * @smid: system request message index
1327 * The main routine that builds scatter gather table from a given
1328 * scsi request sent via the .queuecommand main handler.
1330 * Returns 0 success, anything else error
1333 _base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
1334 struct scsi_cmnd *scmd, u16 smid)
1336 Mpi2SCSIIORequest_t *mpi_request;
1337 dma_addr_t chain_dma;
1338 struct scatterlist *sg_scmd;
1339 void *sg_local, *chain;
1343 u32 sges_in_segment;
1344 u8 simple_sgl_flags;
1345 u8 simple_sgl_flags_last;
1347 struct chain_tracker *chain_req;
1349 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1351 /* init scatter gather flags */
1352 simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1353 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1354 simple_sgl_flags_last = simple_sgl_flags |
1355 MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
1356 chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1357 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1359 sg_scmd = scsi_sglist(scmd);
1360 sges_left = scsi_dma_map(scmd);
1361 if (sges_left < 0) {
1362 sdev_printk(KERN_ERR, scmd->device,
1363 "pci_map_sg failed: request for %d bytes!\n",
1364 scsi_bufflen(scmd));
1368 sg_local = &mpi_request->SGL;
1369 sges_in_segment = (ioc->request_sz -
1370 offsetof(Mpi2SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
1371 if (sges_left <= sges_in_segment)
1372 goto fill_in_last_segment;
1374 mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
1375 (offsetof(Mpi2SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
1377 /* fill in main message segment when there is a chain following */
1378 while (sges_in_segment > 1) {
1379 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1380 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1381 sg_scmd = sg_next(sg_scmd);
1382 sg_local += ioc->sge_size_ieee;
1387 /* initializing the pointers */
1388 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1391 chain = chain_req->chain_buffer;
1392 chain_dma = chain_req->chain_buffer_dma;
1394 sges_in_segment = (sges_left <=
1395 ioc->max_sges_in_chain_message) ? sges_left :
1396 ioc->max_sges_in_chain_message;
1397 chain_offset = (sges_left == sges_in_segment) ?
1398 0 : sges_in_segment;
1399 chain_length = sges_in_segment * ioc->sge_size_ieee;
1401 chain_length += ioc->sge_size_ieee;
1402 _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
1403 chain_offset, chain_length, chain_dma);
1407 goto fill_in_last_segment;
1409 /* fill in chain segments */
1410 while (sges_in_segment) {
1411 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1412 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1413 sg_scmd = sg_next(sg_scmd);
1414 sg_local += ioc->sge_size_ieee;
1419 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1422 chain = chain_req->chain_buffer;
1423 chain_dma = chain_req->chain_buffer_dma;
1427 fill_in_last_segment:
1429 /* fill the last segment */
1430 while (sges_left > 0) {
1432 _base_add_sg_single_ieee(sg_local,
1433 simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
1434 sg_dma_address(sg_scmd));
1436 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1437 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1438 sg_scmd = sg_next(sg_scmd);
1439 sg_local += ioc->sge_size_ieee;
1447 * _base_build_sg_ieee - build generic sg for IEEE format
1448 * @ioc: per adapter object
1449 * @psge: virtual address for SGE
1450 * @data_out_dma: physical address for WRITES
1451 * @data_out_sz: data xfer size for WRITES
1452 * @data_in_dma: physical address for READS
1453 * @data_in_sz: data xfer size for READS
1458 _base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
1459 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1464 if (!data_out_sz && !data_in_sz) {
1465 _base_build_zero_len_sge_ieee(ioc, psge);
1469 if (data_out_sz && data_in_sz) {
1470 /* WRITE sgel first */
1471 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1472 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1473 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
1477 psge += ioc->sge_size_ieee;
1479 /* READ sgel last */
1480 sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
1481 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
1483 } else if (data_out_sz) /* WRITE */ {
1484 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1485 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
1486 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1487 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
1489 } else if (data_in_sz) /* READ */ {
1490 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1491 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
1492 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1493 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
1498 #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1501 * _base_config_dma_addressing - set dma addressing
1502 * @ioc: per adapter object
1503 * @pdev: PCI device struct
1505 * Returns 0 for success, non-zero for failure.
1508 _base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
1511 u64 consistent_dma_mask;
1514 consistent_dma_mask = DMA_BIT_MASK(64);
1516 consistent_dma_mask = DMA_BIT_MASK(32);
1518 if (sizeof(dma_addr_t) > 4) {
1519 const uint64_t required_mask =
1520 dma_get_required_mask(&pdev->dev);
1521 if ((required_mask > DMA_BIT_MASK(32)) &&
1522 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1523 !pci_set_consistent_dma_mask(pdev, consistent_dma_mask)) {
1524 ioc->base_add_sg_single = &_base_add_sg_single_64;
1525 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
1531 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1532 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1533 ioc->base_add_sg_single = &_base_add_sg_single_32;
1534 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
1542 "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
1543 ioc->name, ioc->dma_mask, convert_to_kb(s.totalram));
1549 _base_change_consistent_dma_mask(struct MPT3SAS_ADAPTER *ioc,
1550 struct pci_dev *pdev)
1552 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
1553 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
1560 * _base_check_enable_msix - checks MSIX capabable.
1561 * @ioc: per adapter object
1563 * Check to see if card is capable of MSIX, and set number
1564 * of available msix vectors
1567 _base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
1570 u16 message_control;
1572 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1574 dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
1579 /* get msix vector count */
1581 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1582 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1583 dinitprintk(ioc, pr_info(MPT3SAS_FMT
1584 "msix is supported, vector_count(%d)\n",
1585 ioc->name, ioc->msix_vector_count));
1590 * _base_free_irq - free irq
1591 * @ioc: per adapter object
1593 * Freeing respective reply_queue from the list.
1596 _base_free_irq(struct MPT3SAS_ADAPTER *ioc)
1598 struct adapter_reply_queue *reply_q, *next;
1600 if (list_empty(&ioc->reply_queue_list))
1603 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1604 list_del(&reply_q->list);
1605 irq_set_affinity_hint(reply_q->vector, NULL);
1606 free_cpumask_var(reply_q->affinity_hint);
1607 synchronize_irq(reply_q->vector);
1608 free_irq(reply_q->vector, reply_q);
1614 * _base_request_irq - request irq
1615 * @ioc: per adapter object
1616 * @index: msix index into vector table
1617 * @vector: irq vector
1619 * Inserting respective reply_queue into the list.
1622 _base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index, u32 vector)
1624 struct adapter_reply_queue *reply_q;
1627 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1629 pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
1630 ioc->name, (int)sizeof(struct adapter_reply_queue));
1634 reply_q->msix_index = index;
1635 reply_q->vector = vector;
1637 if (!alloc_cpumask_var(&reply_q->affinity_hint, GFP_KERNEL))
1639 cpumask_clear(reply_q->affinity_hint);
1641 atomic_set(&reply_q->busy, 0);
1642 if (ioc->msix_enable)
1643 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
1644 driver_name, ioc->id, index);
1646 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
1647 driver_name, ioc->id);
1648 r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1651 pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
1652 reply_q->name, vector);
1657 INIT_LIST_HEAD(&reply_q->list);
1658 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1663 * _base_assign_reply_queues - assigning msix index for each cpu
1664 * @ioc: per adapter object
1666 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1668 * It would nice if we could call irq_set_affinity, however it is not
1669 * an exported symbol
1672 _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
1674 unsigned int cpu, nr_cpus, nr_msix, index = 0;
1675 struct adapter_reply_queue *reply_q;
1677 if (!_base_is_controller_msix_enabled(ioc))
1680 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1682 nr_cpus = num_online_cpus();
1683 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
1684 ioc->facts.MaxMSIxVectors);
1688 cpu = cpumask_first(cpu_online_mask);
1690 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1692 unsigned int i, group = nr_cpus / nr_msix;
1697 if (index < nr_cpus % nr_msix)
1700 for (i = 0 ; i < group ; i++) {
1701 ioc->cpu_msix_table[cpu] = index;
1702 cpumask_or(reply_q->affinity_hint,
1703 reply_q->affinity_hint, get_cpu_mask(cpu));
1704 cpu = cpumask_next(cpu, cpu_online_mask);
1707 if (irq_set_affinity_hint(reply_q->vector,
1708 reply_q->affinity_hint))
1709 dinitprintk(ioc, pr_info(MPT3SAS_FMT
1710 "error setting affinity hint for irq vector %d\n",
1711 ioc->name, reply_q->vector));
1717 * _base_disable_msix - disables msix
1718 * @ioc: per adapter object
1722 _base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
1724 if (!ioc->msix_enable)
1726 pci_disable_msix(ioc->pdev);
1727 ioc->msix_enable = 0;
1731 * _base_enable_msix - enables msix, failback to io_apic
1732 * @ioc: per adapter object
1736 _base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
1738 struct msix_entry *entries, *a;
1743 if (msix_disable == -1 || msix_disable == 0)
1749 if (_base_check_enable_msix(ioc) != 0)
1752 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
1753 ioc->msix_vector_count);
1755 printk(MPT3SAS_FMT "MSI-X vectors supported: %d, no of cores"
1756 ": %d, max_msix_vectors: %d\n", ioc->name, ioc->msix_vector_count,
1757 ioc->cpu_count, max_msix_vectors);
1759 if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
1760 max_msix_vectors = 8;
1762 if (max_msix_vectors > 0) {
1763 ioc->reply_queue_count = min_t(int, max_msix_vectors,
1764 ioc->reply_queue_count);
1765 ioc->msix_vector_count = ioc->reply_queue_count;
1766 } else if (max_msix_vectors == 0)
1769 entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1772 dfailprintk(ioc, pr_info(MPT3SAS_FMT
1773 "kcalloc failed @ at %s:%d/%s() !!!\n",
1774 ioc->name, __FILE__, __LINE__, __func__));
1778 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
1781 r = pci_enable_msix_exact(ioc->pdev, entries, ioc->reply_queue_count);
1783 dfailprintk(ioc, pr_info(MPT3SAS_FMT
1784 "pci_enable_msix_exact failed (r=%d) !!!\n",
1790 ioc->msix_enable = 1;
1791 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
1792 r = _base_request_irq(ioc, i, a->vector);
1794 _base_free_irq(ioc);
1795 _base_disable_msix(ioc);
1804 /* failback to io_apic interrupt routing */
1807 ioc->reply_queue_count = 1;
1808 r = _base_request_irq(ioc, 0, ioc->pdev->irq);
1814 * mpt3sas_base_unmap_resources - free controller resources
1815 * @ioc: per adapter object
1818 mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
1820 struct pci_dev *pdev = ioc->pdev;
1822 dexitprintk(ioc, printk(MPT3SAS_FMT "%s\n",
1823 ioc->name, __func__));
1825 _base_free_irq(ioc);
1826 _base_disable_msix(ioc);
1828 if (ioc->msix96_vector)
1829 kfree(ioc->replyPostRegisterIndex);
1831 if (ioc->chip_phys) {
1836 if (pci_is_enabled(pdev)) {
1837 pci_release_selected_regions(ioc->pdev, ioc->bars);
1838 pci_disable_pcie_error_reporting(pdev);
1839 pci_disable_device(pdev);
1844 * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
1845 * @ioc: per adapter object
1847 * Returns 0 for success, non-zero for failure.
1850 mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
1852 struct pci_dev *pdev = ioc->pdev;
1858 struct adapter_reply_queue *reply_q;
1860 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
1861 ioc->name, __func__));
1863 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1864 if (pci_enable_device_mem(pdev)) {
1865 pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
1872 if (pci_request_selected_regions(pdev, ioc->bars,
1874 pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
1881 /* AER (Advanced Error Reporting) hooks */
1882 pci_enable_pcie_error_reporting(pdev);
1884 pci_set_master(pdev);
1887 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1888 pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
1889 ioc->name, pci_name(pdev));
1894 for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
1895 (!memap_sz || !pio_sz); i++) {
1896 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
1899 pio_chip = (u64)pci_resource_start(pdev, i);
1900 pio_sz = pci_resource_len(pdev, i);
1901 } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1904 ioc->chip_phys = pci_resource_start(pdev, i);
1905 chip_phys = (u64)ioc->chip_phys;
1906 memap_sz = pci_resource_len(pdev, i);
1907 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1911 if (ioc->chip == NULL) {
1912 pr_err(MPT3SAS_FMT "unable to map adapter memory! "
1913 " or resource not found\n", ioc->name);
1918 _base_mask_interrupts(ioc);
1920 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
1924 if (!ioc->rdpq_array_enable_assigned) {
1925 ioc->rdpq_array_enable = ioc->rdpq_array_capable;
1926 ioc->rdpq_array_enable_assigned = 1;
1929 r = _base_enable_msix(ioc);
1933 /* Use the Combined reply queue feature only for SAS3 C0 & higher
1934 * revision HBAs and also only when reply queue count is greater than 8
1936 if (ioc->msix96_vector && ioc->reply_queue_count > 8) {
1937 /* Determine the Supplemental Reply Post Host Index Registers
1938 * Addresse. Supplemental Reply Post Host Index Registers
1939 * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
1940 * each register is at offset bytes of
1941 * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
1943 ioc->replyPostRegisterIndex = kcalloc(
1944 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT,
1945 sizeof(resource_size_t *), GFP_KERNEL);
1946 if (!ioc->replyPostRegisterIndex) {
1947 dfailprintk(ioc, printk(MPT3SAS_FMT
1948 "allocation for reply Post Register Index failed!!!\n",
1954 for (i = 0; i < MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT; i++) {
1955 ioc->replyPostRegisterIndex[i] = (resource_size_t *)
1956 ((u8 *)&ioc->chip->Doorbell +
1957 MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
1958 (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
1961 ioc->msix96_vector = 0;
1963 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
1964 pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
1965 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1966 "IO-APIC enabled"), reply_q->vector);
1968 pr_info(MPT3SAS_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1969 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1970 pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
1971 ioc->name, (unsigned long long)pio_chip, pio_sz);
1973 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1974 pci_save_state(pdev);
1978 mpt3sas_base_unmap_resources(ioc);
1983 * mpt3sas_base_get_msg_frame - obtain request mf pointer
1984 * @ioc: per adapter object
1985 * @smid: system request message index(smid zero is invalid)
1987 * Returns virt pointer to message frame.
1990 mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1992 return (void *)(ioc->request + (smid * ioc->request_sz));
1996 * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
1997 * @ioc: per adapter object
1998 * @smid: system request message index
2000 * Returns virt pointer to sense buffer.
2003 mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2005 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
2009 * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
2010 * @ioc: per adapter object
2011 * @smid: system request message index
2013 * Returns phys pointer to the low 32bit address of the sense buffer.
2016 mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2018 return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
2019 SCSI_SENSE_BUFFERSIZE));
2023 * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
2024 * @ioc: per adapter object
2025 * @phys_addr: lower 32 physical addr of the reply
2027 * Converts 32bit lower physical addr into a virt address.
2030 mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
2034 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
2038 * mpt3sas_base_get_smid - obtain a free smid from internal queue
2039 * @ioc: per adapter object
2040 * @cb_idx: callback index
2042 * Returns smid (zero is invalid)
2045 mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2047 unsigned long flags;
2048 struct request_tracker *request;
2051 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2052 if (list_empty(&ioc->internal_free_list)) {
2053 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2054 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2055 ioc->name, __func__);
2059 request = list_entry(ioc->internal_free_list.next,
2060 struct request_tracker, tracker_list);
2061 request->cb_idx = cb_idx;
2062 smid = request->smid;
2063 list_del(&request->tracker_list);
2064 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2069 * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
2070 * @ioc: per adapter object
2071 * @cb_idx: callback index
2072 * @scmd: pointer to scsi command object
2074 * Returns smid (zero is invalid)
2077 mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
2078 struct scsi_cmnd *scmd)
2080 unsigned long flags;
2081 struct scsiio_tracker *request;
2084 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2085 if (list_empty(&ioc->free_list)) {
2086 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2087 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2088 ioc->name, __func__);
2092 request = list_entry(ioc->free_list.next,
2093 struct scsiio_tracker, tracker_list);
2094 request->scmd = scmd;
2095 request->cb_idx = cb_idx;
2096 smid = request->smid;
2097 list_del(&request->tracker_list);
2098 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2103 * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
2104 * @ioc: per adapter object
2105 * @cb_idx: callback index
2107 * Returns smid (zero is invalid)
2110 mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2112 unsigned long flags;
2113 struct request_tracker *request;
2116 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2117 if (list_empty(&ioc->hpr_free_list)) {
2118 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2122 request = list_entry(ioc->hpr_free_list.next,
2123 struct request_tracker, tracker_list);
2124 request->cb_idx = cb_idx;
2125 smid = request->smid;
2126 list_del(&request->tracker_list);
2127 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2132 * mpt3sas_base_free_smid - put smid back on free_list
2133 * @ioc: per adapter object
2134 * @smid: system request message index
2139 mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2141 unsigned long flags;
2143 struct chain_tracker *chain_req, *next;
2145 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2146 if (smid < ioc->hi_priority_smid) {
2149 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
2150 list_for_each_entry_safe(chain_req, next,
2151 &ioc->scsi_lookup[i].chain_list, tracker_list) {
2152 list_del_init(&chain_req->tracker_list);
2153 list_add(&chain_req->tracker_list,
2154 &ioc->free_chain_list);
2157 ioc->scsi_lookup[i].cb_idx = 0xFF;
2158 ioc->scsi_lookup[i].scmd = NULL;
2159 list_add(&ioc->scsi_lookup[i].tracker_list, &ioc->free_list);
2160 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2163 * See _wait_for_commands_to_complete() call with regards
2166 if (ioc->shost_recovery && ioc->pending_io_count) {
2167 if (ioc->pending_io_count == 1)
2168 wake_up(&ioc->reset_wq);
2169 ioc->pending_io_count--;
2172 } else if (smid < ioc->internal_smid) {
2174 i = smid - ioc->hi_priority_smid;
2175 ioc->hpr_lookup[i].cb_idx = 0xFF;
2176 list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
2177 } else if (smid <= ioc->hba_queue_depth) {
2178 /* internal queue */
2179 i = smid - ioc->internal_smid;
2180 ioc->internal_lookup[i].cb_idx = 0xFF;
2181 list_add(&ioc->internal_lookup[i].tracker_list,
2182 &ioc->internal_free_list);
2184 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2188 * _base_writeq - 64 bit write to MMIO
2189 * @ioc: per adapter object
2191 * @addr: address in MMIO space
2192 * @writeq_lock: spin lock
2194 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
2195 * care of 32 bit environment where its not quarenteed to send the entire word
2198 #if defined(writeq) && defined(CONFIG_64BIT)
2200 _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2202 writeq(cpu_to_le64(b), addr);
2206 _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2208 unsigned long flags;
2209 __u64 data_out = cpu_to_le64(b);
2211 spin_lock_irqsave(writeq_lock, flags);
2212 writel((u32)(data_out), addr);
2213 writel((u32)(data_out >> 32), (addr + 4));
2214 spin_unlock_irqrestore(writeq_lock, flags);
2219 _base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
2221 return ioc->cpu_msix_table[raw_smp_processor_id()];
2225 * mpt3sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
2226 * @ioc: per adapter object
2227 * @smid: system request message index
2228 * @handle: device handle
2233 mpt3sas_base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
2235 Mpi2RequestDescriptorUnion_t descriptor;
2236 u64 *request = (u64 *)&descriptor;
2239 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
2240 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2241 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2242 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2243 descriptor.SCSIIO.LMID = 0;
2244 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2245 &ioc->scsi_lookup_lock);
2249 * mpt3sas_base_put_smid_fast_path - send fast path request to firmware
2250 * @ioc: per adapter object
2251 * @smid: system request message index
2252 * @handle: device handle
2257 mpt3sas_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
2260 Mpi2RequestDescriptorUnion_t descriptor;
2261 u64 *request = (u64 *)&descriptor;
2263 descriptor.SCSIIO.RequestFlags =
2264 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
2265 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2266 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2267 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2268 descriptor.SCSIIO.LMID = 0;
2269 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2270 &ioc->scsi_lookup_lock);
2274 * mpt3sas_base_put_smid_hi_priority - send Task Managment request to firmware
2275 * @ioc: per adapter object
2276 * @smid: system request message index
2281 mpt3sas_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2283 Mpi2RequestDescriptorUnion_t descriptor;
2284 u64 *request = (u64 *)&descriptor;
2286 descriptor.HighPriority.RequestFlags =
2287 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
2288 descriptor.HighPriority.MSIxIndex = 0;
2289 descriptor.HighPriority.SMID = cpu_to_le16(smid);
2290 descriptor.HighPriority.LMID = 0;
2291 descriptor.HighPriority.Reserved1 = 0;
2292 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2293 &ioc->scsi_lookup_lock);
2297 * mpt3sas_base_put_smid_default - Default, primarily used for config pages
2298 * @ioc: per adapter object
2299 * @smid: system request message index
2304 mpt3sas_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2306 Mpi2RequestDescriptorUnion_t descriptor;
2307 u64 *request = (u64 *)&descriptor;
2309 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2310 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
2311 descriptor.Default.SMID = cpu_to_le16(smid);
2312 descriptor.Default.LMID = 0;
2313 descriptor.Default.DescriptorTypeDependent = 0;
2314 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2315 &ioc->scsi_lookup_lock);
2319 * _base_display_intel_branding - Display branding string
2320 * @ioc: per adapter object
2325 _base_display_intel_branding(struct MPT3SAS_ADAPTER *ioc)
2327 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
2330 switch (ioc->pdev->device) {
2331 case MPI25_MFGPAGE_DEVID_SAS3008:
2332 switch (ioc->pdev->subsystem_device) {
2333 case MPT3SAS_INTEL_RMS3JC080_SSDID:
2334 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2335 MPT3SAS_INTEL_RMS3JC080_BRANDING);
2338 case MPT3SAS_INTEL_RS3GC008_SSDID:
2339 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2340 MPT3SAS_INTEL_RS3GC008_BRANDING);
2342 case MPT3SAS_INTEL_RS3FC044_SSDID:
2343 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2344 MPT3SAS_INTEL_RS3FC044_BRANDING);
2346 case MPT3SAS_INTEL_RS3UC080_SSDID:
2347 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2348 MPT3SAS_INTEL_RS3UC080_BRANDING);
2352 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2353 ioc->name, ioc->pdev->subsystem_device);
2359 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2360 ioc->name, ioc->pdev->subsystem_device);
2368 * _base_display_dell_branding - Display branding string
2369 * @ioc: per adapter object
2374 _base_display_dell_branding(struct MPT3SAS_ADAPTER *ioc)
2376 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
2379 switch (ioc->pdev->device) {
2380 case MPI25_MFGPAGE_DEVID_SAS3008:
2381 switch (ioc->pdev->subsystem_device) {
2382 case MPT3SAS_DELL_12G_HBA_SSDID:
2383 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2384 MPT3SAS_DELL_12G_HBA_BRANDING);
2388 "Dell 12Gbps HBA: Subsystem ID: 0x%X\n", ioc->name,
2389 ioc->pdev->subsystem_device);
2395 "Dell 12Gbps HBA: Subsystem ID: 0x%X\n", ioc->name,
2396 ioc->pdev->subsystem_device);
2402 * _base_display_cisco_branding - Display branding string
2403 * @ioc: per adapter object
2408 _base_display_cisco_branding(struct MPT3SAS_ADAPTER *ioc)
2410 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_CISCO)
2413 switch (ioc->pdev->device) {
2414 case MPI25_MFGPAGE_DEVID_SAS3008:
2415 switch (ioc->pdev->subsystem_device) {
2416 case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
2417 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2418 MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
2420 case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
2421 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2422 MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
2424 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
2425 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2426 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
2430 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
2431 ioc->name, ioc->pdev->subsystem_device);
2435 case MPI25_MFGPAGE_DEVID_SAS3108_1:
2436 switch (ioc->pdev->subsystem_device) {
2437 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
2438 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2439 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
2441 case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
2442 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2443 MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING);
2447 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
2448 ioc->name, ioc->pdev->subsystem_device);
2454 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
2455 ioc->name, ioc->pdev->subsystem_device);
2461 * _base_display_ioc_capabilities - Disply IOC's capabilities.
2462 * @ioc: per adapter object
2467 _base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
2471 u32 iounit_pg1_flags;
2474 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2475 strncpy(desc, ioc->manu_pg0.ChipName, 16);
2476 pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
2477 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2479 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2480 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2481 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2482 ioc->facts.FWVersion.Word & 0x000000FF,
2483 ioc->pdev->revision,
2484 (bios_version & 0xFF000000) >> 24,
2485 (bios_version & 0x00FF0000) >> 16,
2486 (bios_version & 0x0000FF00) >> 8,
2487 bios_version & 0x000000FF);
2489 _base_display_intel_branding(ioc);
2490 _base_display_dell_branding(ioc);
2491 _base_display_cisco_branding(ioc);
2493 pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
2495 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2496 pr_info("Initiator");
2500 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2501 pr_info("%sTarget", i ? "," : "");
2507 pr_info("Capabilities=(");
2509 if (ioc->facts.IOCCapabilities &
2510 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2515 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2516 pr_info("%sTLR", i ? "," : "");
2520 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2521 pr_info("%sMulticast", i ? "," : "");
2525 if (ioc->facts.IOCCapabilities &
2526 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2527 pr_info("%sBIDI Target", i ? "," : "");
2531 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2532 pr_info("%sEEDP", i ? "," : "");
2536 if (ioc->facts.IOCCapabilities &
2537 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2538 pr_info("%sSnapshot Buffer", i ? "," : "");
2542 if (ioc->facts.IOCCapabilities &
2543 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2544 pr_info("%sDiag Trace Buffer", i ? "," : "");
2548 if (ioc->facts.IOCCapabilities &
2549 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2550 pr_info("%sDiag Extended Buffer", i ? "," : "");
2554 if (ioc->facts.IOCCapabilities &
2555 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2556 pr_info("%sTask Set Full", i ? "," : "");
2560 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2561 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2562 pr_info("%sNCQ", i ? "," : "");
2570 * mpt3sas_base_update_missing_delay - change the missing delay timers
2571 * @ioc: per adapter object
2572 * @device_missing_delay: amount of time till device is reported missing
2573 * @io_missing_delay: interval IO is returned when there is a missing device
2577 * Passed on the command line, this function will modify the device missing
2578 * delay, as well as the io missing delay. This should be called at driver
2582 mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
2583 u16 device_missing_delay, u8 io_missing_delay)
2585 u16 dmd, dmd_new, dmd_orignal;
2586 u8 io_missing_delay_original;
2588 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2589 Mpi2ConfigReply_t mpi_reply;
2593 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
2597 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2598 sizeof(Mpi2SasIOUnit1PhyData_t));
2599 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2600 if (!sas_iounit_pg1) {
2601 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2602 ioc->name, __FILE__, __LINE__, __func__);
2605 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2606 sas_iounit_pg1, sz))) {
2607 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2608 ioc->name, __FILE__, __LINE__, __func__);
2611 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2612 MPI2_IOCSTATUS_MASK;
2613 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2614 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2615 ioc->name, __FILE__, __LINE__, __func__);
2619 /* device missing delay */
2620 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2621 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2622 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2624 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2626 if (device_missing_delay > 0x7F) {
2627 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2628 device_missing_delay;
2630 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2632 dmd = device_missing_delay;
2633 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2635 /* io missing delay */
2636 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2637 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2639 if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2641 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2643 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2646 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2647 pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
2648 ioc->name, dmd_orignal, dmd_new);
2649 pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
2650 ioc->name, io_missing_delay_original,
2652 ioc->device_missing_delay = dmd_new;
2653 ioc->io_missing_delay = io_missing_delay;
2657 kfree(sas_iounit_pg1);
2660 * _base_static_config_pages - static start of day config pages
2661 * @ioc: per adapter object
2666 _base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
2668 Mpi2ConfigReply_t mpi_reply;
2669 u32 iounit_pg1_flags;
2671 mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
2672 if (ioc->ir_firmware)
2673 mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2677 * Ensure correct T10 PI operation if vendor left EEDPTagMode
2678 * flag unset in NVDATA.
2680 mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
2681 if (ioc->manu_pg11.EEDPTagMode == 0) {
2682 pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
2684 ioc->manu_pg11.EEDPTagMode &= ~0x3;
2685 ioc->manu_pg11.EEDPTagMode |= 0x1;
2686 mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
2690 mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
2691 mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
2692 mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
2693 mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
2694 mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2695 mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
2696 _base_display_ioc_capabilities(ioc);
2699 * Enable task_set_full handling in iounit_pg1 when the
2700 * facts capabilities indicate that its supported.
2702 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2703 if ((ioc->facts.IOCCapabilities &
2704 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
2706 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2709 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2710 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
2711 mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2713 if (ioc->iounit_pg8.NumSensors)
2714 ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
2718 * _base_release_memory_pools - release memory
2719 * @ioc: per adapter object
2721 * Free memory allocated from _base_allocate_memory_pools.
2726 _base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
2729 struct reply_post_struct *rps;
2731 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2735 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2736 ioc->request, ioc->request_dma);
2737 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2738 "request_pool(0x%p): free\n",
2739 ioc->name, ioc->request));
2740 ioc->request = NULL;
2744 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2745 if (ioc->sense_dma_pool)
2746 pci_pool_destroy(ioc->sense_dma_pool);
2747 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2748 "sense_pool(0x%p): free\n",
2749 ioc->name, ioc->sense));
2754 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2755 if (ioc->reply_dma_pool)
2756 pci_pool_destroy(ioc->reply_dma_pool);
2757 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2758 "reply_pool(0x%p): free\n",
2759 ioc->name, ioc->reply));
2763 if (ioc->reply_free) {
2764 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2765 ioc->reply_free_dma);
2766 if (ioc->reply_free_dma_pool)
2767 pci_pool_destroy(ioc->reply_free_dma_pool);
2768 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2769 "reply_free_pool(0x%p): free\n",
2770 ioc->name, ioc->reply_free));
2771 ioc->reply_free = NULL;
2774 if (ioc->reply_post) {
2776 rps = &ioc->reply_post[i];
2777 if (rps->reply_post_free) {
2779 ioc->reply_post_free_dma_pool,
2780 rps->reply_post_free,
2781 rps->reply_post_free_dma);
2782 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2783 "reply_post_free_pool(0x%p): free\n",
2784 ioc->name, rps->reply_post_free));
2785 rps->reply_post_free = NULL;
2787 } while (ioc->rdpq_array_enable &&
2788 (++i < ioc->reply_queue_count));
2790 if (ioc->reply_post_free_dma_pool)
2791 pci_pool_destroy(ioc->reply_post_free_dma_pool);
2792 kfree(ioc->reply_post);
2795 if (ioc->config_page) {
2796 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2797 "config_page(0x%p): free\n", ioc->name,
2799 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2800 ioc->config_page, ioc->config_page_dma);
2803 if (ioc->scsi_lookup) {
2804 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2805 ioc->scsi_lookup = NULL;
2807 kfree(ioc->hpr_lookup);
2808 kfree(ioc->internal_lookup);
2809 if (ioc->chain_lookup) {
2810 for (i = 0; i < ioc->chain_depth; i++) {
2811 if (ioc->chain_lookup[i].chain_buffer)
2812 pci_pool_free(ioc->chain_dma_pool,
2813 ioc->chain_lookup[i].chain_buffer,
2814 ioc->chain_lookup[i].chain_buffer_dma);
2816 if (ioc->chain_dma_pool)
2817 pci_pool_destroy(ioc->chain_dma_pool);
2818 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2819 ioc->chain_lookup = NULL;
2824 * _base_allocate_memory_pools - allocate start of day memory pools
2825 * @ioc: per adapter object
2826 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2828 * Returns 0 success, anything else error
2831 _base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
2833 struct mpt3sas_facts *facts;
2834 u16 max_sge_elements;
2835 u16 chains_needed_per_io;
2836 u32 sz, total_sz, reply_post_free_sz;
2838 u16 max_request_credit;
2839 unsigned short sg_tablesize;
2843 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2848 facts = &ioc->facts;
2850 /* command line tunables for max sgl entries */
2851 if (max_sgl_entries != -1)
2852 sg_tablesize = max_sgl_entries;
2854 sg_tablesize = MPT3SAS_SG_DEPTH;
2856 if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
2857 sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
2858 else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
2859 sg_tablesize = min_t(unsigned short, sg_tablesize,
2860 SCSI_MAX_SG_CHAIN_SEGMENTS);
2862 "sg_tablesize(%u) is bigger than kernel"
2863 " defined SCSI_MAX_SG_SEGMENTS(%u)\n", ioc->name,
2864 sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
2866 ioc->shost->sg_tablesize = sg_tablesize;
2868 ioc->hi_priority_depth = facts->HighPriorityCredit;
2869 ioc->internal_depth = ioc->hi_priority_depth + (5);
2870 /* command line tunables for max controller queue depth */
2871 if (max_queue_depth != -1 && max_queue_depth != 0) {
2872 max_request_credit = min_t(u16, max_queue_depth +
2873 ioc->hi_priority_depth + ioc->internal_depth,
2874 facts->RequestCredit);
2875 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
2876 max_request_credit = MAX_HBA_QUEUE_DEPTH;
2878 max_request_credit = min_t(u16, facts->RequestCredit,
2879 MAX_HBA_QUEUE_DEPTH);
2881 ioc->hba_queue_depth = max_request_credit;
2883 /* request frame size */
2884 ioc->request_sz = facts->IOCRequestFrameSize * 4;
2886 /* reply frame size */
2887 ioc->reply_sz = facts->ReplyFrameSize * 4;
2889 /* calculate the max scatter element size */
2890 sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
2894 /* calculate number of sg elements left over in the 1st frame */
2895 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2896 sizeof(Mpi2SGEIOUnion_t)) + sge_size);
2897 ioc->max_sges_in_main_message = max_sge_elements/sge_size;
2899 /* now do the same for a chain buffer */
2900 max_sge_elements = ioc->request_sz - sge_size;
2901 ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
2904 * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2906 chains_needed_per_io = ((ioc->shost->sg_tablesize -
2907 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2909 if (chains_needed_per_io > facts->MaxChainDepth) {
2910 chains_needed_per_io = facts->MaxChainDepth;
2911 ioc->shost->sg_tablesize = min_t(u16,
2912 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2913 * chains_needed_per_io), ioc->shost->sg_tablesize);
2915 ioc->chains_needed_per_io = chains_needed_per_io;
2917 /* reply free queue sizing - taking into account for 64 FW events */
2918 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2920 /* calculate reply descriptor post queue depth */
2921 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
2922 ioc->reply_free_queue_depth + 1 ;
2923 /* align the reply post queue on the next 16 count boundary */
2924 if (ioc->reply_post_queue_depth % 16)
2925 ioc->reply_post_queue_depth += 16 -
2926 (ioc->reply_post_queue_depth % 16);
2929 if (ioc->reply_post_queue_depth >
2930 facts->MaxReplyDescriptorPostQueueDepth) {
2931 ioc->reply_post_queue_depth =
2932 facts->MaxReplyDescriptorPostQueueDepth -
2933 (facts->MaxReplyDescriptorPostQueueDepth % 16);
2934 ioc->hba_queue_depth =
2935 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
2936 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2939 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
2940 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2941 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2942 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2943 ioc->chains_needed_per_io));
2945 /* reply post queue, 16 byte align */
2946 reply_post_free_sz = ioc->reply_post_queue_depth *
2947 sizeof(Mpi2DefaultReplyDescriptor_t);
2949 sz = reply_post_free_sz;
2950 if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
2951 sz *= ioc->reply_queue_count;
2953 ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
2954 (ioc->reply_queue_count):1,
2955 sizeof(struct reply_post_struct), GFP_KERNEL);
2957 if (!ioc->reply_post) {
2958 pr_err(MPT3SAS_FMT "reply_post_free pool: kcalloc failed\n",
2962 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2963 ioc->pdev, sz, 16, 0);
2964 if (!ioc->reply_post_free_dma_pool) {
2966 "reply_post_free pool: pci_pool_create failed\n",
2972 ioc->reply_post[i].reply_post_free =
2973 pci_pool_alloc(ioc->reply_post_free_dma_pool,
2975 &ioc->reply_post[i].reply_post_free_dma);
2976 if (!ioc->reply_post[i].reply_post_free) {
2978 "reply_post_free pool: pci_pool_alloc failed\n",
2982 memset(ioc->reply_post[i].reply_post_free, 0, sz);
2983 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2984 "reply post free pool (0x%p): depth(%d),"
2985 "element_size(%d), pool_size(%d kB)\n", ioc->name,
2986 ioc->reply_post[i].reply_post_free,
2987 ioc->reply_post_queue_depth, 8, sz/1024));
2988 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2989 "reply_post_free_dma = (0x%llx)\n", ioc->name,
2990 (unsigned long long)
2991 ioc->reply_post[i].reply_post_free_dma));
2993 } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
2995 if (ioc->dma_mask == 64) {
2996 if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
2998 "no suitable consistent DMA mask for %s\n",
2999 ioc->name, pci_name(ioc->pdev));
3004 ioc->scsiio_depth = ioc->hba_queue_depth -
3005 ioc->hi_priority_depth - ioc->internal_depth;
3007 /* set the scsi host can_queue depth
3008 * with some internal commands that could be outstanding
3010 ioc->shost->can_queue = ioc->scsiio_depth;
3011 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3012 "scsi host: can_queue depth (%d)\n",
3013 ioc->name, ioc->shost->can_queue));
3016 /* contiguous pool for request and chains, 16 byte align, one extra "
3019 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
3020 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
3022 /* hi-priority queue */
3023 sz += (ioc->hi_priority_depth * ioc->request_sz);
3025 /* internal queue */
3026 sz += (ioc->internal_depth * ioc->request_sz);
3028 ioc->request_dma_sz = sz;
3029 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
3030 if (!ioc->request) {
3031 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
3032 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
3033 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
3034 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
3035 if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
3038 ioc->hba_queue_depth = max_request_credit - retry_sz;
3039 goto retry_allocation;
3043 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
3044 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
3045 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
3046 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
3048 /* hi-priority queue */
3049 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
3051 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
3054 /* internal queue */
3055 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
3057 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
3060 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3061 "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
3062 ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
3063 (ioc->hba_queue_depth * ioc->request_sz)/1024));
3065 dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
3066 ioc->name, (unsigned long long) ioc->request_dma));
3069 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
3070 ioc->scsi_lookup_pages = get_order(sz);
3071 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
3072 GFP_KERNEL, ioc->scsi_lookup_pages);
3073 if (!ioc->scsi_lookup) {
3074 pr_err(MPT3SAS_FMT "scsi_lookup: get_free_pages failed, sz(%d)\n",
3075 ioc->name, (int)sz);
3079 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
3080 ioc->name, ioc->request, ioc->scsiio_depth));
3082 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
3083 sz = ioc->chain_depth * sizeof(struct chain_tracker);
3084 ioc->chain_pages = get_order(sz);
3085 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
3086 GFP_KERNEL, ioc->chain_pages);
3087 if (!ioc->chain_lookup) {
3088 pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages failed\n",
3092 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
3093 ioc->request_sz, 16, 0);
3094 if (!ioc->chain_dma_pool) {
3095 pr_err(MPT3SAS_FMT "chain_dma_pool: pci_pool_create failed\n",
3099 for (i = 0; i < ioc->chain_depth; i++) {
3100 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
3101 ioc->chain_dma_pool , GFP_KERNEL,
3102 &ioc->chain_lookup[i].chain_buffer_dma);
3103 if (!ioc->chain_lookup[i].chain_buffer) {
3104 ioc->chain_depth = i;
3107 total_sz += ioc->request_sz;
3110 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3111 "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
3112 ioc->name, ioc->chain_depth, ioc->request_sz,
3113 ((ioc->chain_depth * ioc->request_sz))/1024));
3115 /* initialize hi-priority queue smid's */
3116 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
3117 sizeof(struct request_tracker), GFP_KERNEL);
3118 if (!ioc->hpr_lookup) {
3119 pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
3123 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
3124 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3125 "hi_priority(0x%p): depth(%d), start smid(%d)\n",
3126 ioc->name, ioc->hi_priority,
3127 ioc->hi_priority_depth, ioc->hi_priority_smid));
3129 /* initialize internal queue smid's */
3130 ioc->internal_lookup = kcalloc(ioc->internal_depth,
3131 sizeof(struct request_tracker), GFP_KERNEL);
3132 if (!ioc->internal_lookup) {
3133 pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
3137 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
3138 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3139 "internal(0x%p): depth(%d), start smid(%d)\n",
3140 ioc->name, ioc->internal,
3141 ioc->internal_depth, ioc->internal_smid));
3143 /* sense buffers, 4 byte align */
3144 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
3145 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
3147 if (!ioc->sense_dma_pool) {
3148 pr_err(MPT3SAS_FMT "sense pool: pci_pool_create failed\n",
3152 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
3155 pr_err(MPT3SAS_FMT "sense pool: pci_pool_alloc failed\n",
3159 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3160 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
3161 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
3162 SCSI_SENSE_BUFFERSIZE, sz/1024));
3163 dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
3164 ioc->name, (unsigned long long)ioc->sense_dma));
3167 /* reply pool, 4 byte align */
3168 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
3169 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
3171 if (!ioc->reply_dma_pool) {
3172 pr_err(MPT3SAS_FMT "reply pool: pci_pool_create failed\n",
3176 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
3179 pr_err(MPT3SAS_FMT "reply pool: pci_pool_alloc failed\n",
3183 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
3184 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
3185 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3186 "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
3187 ioc->name, ioc->reply,
3188 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
3189 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
3190 ioc->name, (unsigned long long)ioc->reply_dma));
3193 /* reply free queue, 16 byte align */
3194 sz = ioc->reply_free_queue_depth * 4;
3195 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
3196 ioc->pdev, sz, 16, 0);
3197 if (!ioc->reply_free_dma_pool) {
3198 pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_create failed\n",
3202 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
3203 &ioc->reply_free_dma);
3204 if (!ioc->reply_free) {
3205 pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_alloc failed\n",
3209 memset(ioc->reply_free, 0, sz);
3210 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
3211 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
3212 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
3213 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3214 "reply_free_dma (0x%llx)\n",
3215 ioc->name, (unsigned long long)ioc->reply_free_dma));
3218 ioc->config_page_sz = 512;
3219 ioc->config_page = pci_alloc_consistent(ioc->pdev,
3220 ioc->config_page_sz, &ioc->config_page_dma);
3221 if (!ioc->config_page) {
3223 "config page: pci_pool_alloc failed\n",
3227 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3228 "config page(0x%p): size(%d)\n",
3229 ioc->name, ioc->config_page, ioc->config_page_sz));
3230 dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
3231 ioc->name, (unsigned long long)ioc->config_page_dma));
3232 total_sz += ioc->config_page_sz;
3234 pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
3235 ioc->name, total_sz/1024);
3237 "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
3238 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
3239 pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
3240 ioc->name, ioc->shost->sg_tablesize);
3248 * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
3249 * @ioc: Pointer to MPT_ADAPTER structure
3250 * @cooked: Request raw or cooked IOC state
3252 * Returns all IOC Doorbell register bits if cooked==0, else just the
3253 * Doorbell bits in MPI_IOC_STATE_MASK.
3256 mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
3260 s = readl(&ioc->chip->Doorbell);
3261 sc = s & MPI2_IOC_STATE_MASK;
3262 return cooked ? sc : s;
3266 * _base_wait_on_iocstate - waiting on a particular ioc state
3267 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
3268 * @timeout: timeout in second
3269 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3271 * Returns 0 for success, non-zero for failure.
3274 _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
3281 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3283 current_state = mpt3sas_base_get_iocstate(ioc, 1);
3284 if (current_state == ioc_state)
3286 if (count && current_state == MPI2_IOC_STATE_FAULT)
3288 if (sleep_flag == CAN_SLEEP)
3289 usleep_range(1000, 1500);
3295 return current_state;
3299 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
3300 * a write to the doorbell)
3301 * @ioc: per adapter object
3302 * @timeout: timeout in second
3303 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3305 * Returns 0 for success, non-zero for failure.
3307 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
3310 _base_diag_reset(struct MPT3SAS_ADAPTER *ioc, int sleep_flag);
3313 _base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout,
3320 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3322 int_status = readl(&ioc->chip->HostInterruptStatus);
3323 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
3324 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3325 "%s: successful count(%d), timeout(%d)\n",
3326 ioc->name, __func__, count, timeout));
3329 if (sleep_flag == CAN_SLEEP)
3330 usleep_range(1000, 1500);
3337 "%s: failed due to timeout count(%d), int_status(%x)!\n",
3338 ioc->name, __func__, count, int_status);
3343 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
3344 * @ioc: per adapter object
3345 * @timeout: timeout in second
3346 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3348 * Returns 0 for success, non-zero for failure.
3350 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
3354 _base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout,
3362 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3364 int_status = readl(&ioc->chip->HostInterruptStatus);
3365 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
3366 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3367 "%s: successful count(%d), timeout(%d)\n",
3368 ioc->name, __func__, count, timeout));
3370 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
3371 doorbell = readl(&ioc->chip->Doorbell);
3372 if ((doorbell & MPI2_IOC_STATE_MASK) ==
3373 MPI2_IOC_STATE_FAULT) {
3374 mpt3sas_base_fault_info(ioc , doorbell);
3377 } else if (int_status == 0xFFFFFFFF)
3380 if (sleep_flag == CAN_SLEEP)
3381 usleep_range(1000, 1500);
3389 "%s: failed due to timeout count(%d), int_status(%x)!\n",
3390 ioc->name, __func__, count, int_status);
3395 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
3396 * @ioc: per adapter object
3397 * @timeout: timeout in second
3398 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3400 * Returns 0 for success, non-zero for failure.
3404 _base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout,
3411 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3413 doorbell_reg = readl(&ioc->chip->Doorbell);
3414 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
3415 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3416 "%s: successful count(%d), timeout(%d)\n",
3417 ioc->name, __func__, count, timeout));
3420 if (sleep_flag == CAN_SLEEP)
3421 usleep_range(1000, 1500);
3428 "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
3429 ioc->name, __func__, count, doorbell_reg);
3434 * _base_send_ioc_reset - send doorbell reset
3435 * @ioc: per adapter object
3436 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
3437 * @timeout: timeout in second
3438 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3440 * Returns 0 for success, non-zero for failure.
3443 _base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout,
3449 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
3450 pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
3451 ioc->name, __func__);
3455 if (!(ioc->facts.IOCCapabilities &
3456 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
3459 pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
3461 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
3462 &ioc->chip->Doorbell);
3463 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
3467 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
3468 timeout, sleep_flag);
3471 "%s: failed going to ready state (ioc_state=0x%x)\n",
3472 ioc->name, __func__, ioc_state);
3477 pr_info(MPT3SAS_FMT "message unit reset: %s\n",
3478 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3483 * _base_handshake_req_reply_wait - send request thru doorbell interface
3484 * @ioc: per adapter object
3485 * @request_bytes: request length
3486 * @request: pointer having request payload
3487 * @reply_bytes: reply length
3488 * @reply: pointer to reply payload
3489 * @timeout: timeout in second
3490 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3492 * Returns 0 for success, non-zero for failure.
3495 _base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
3496 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
3498 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
3504 /* make sure doorbell is not in use */
3505 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
3507 "doorbell is in use (line=%d)\n",
3508 ioc->name, __LINE__);
3512 /* clear pending doorbell interrupts from previous state changes */
3513 if (readl(&ioc->chip->HostInterruptStatus) &
3514 MPI2_HIS_IOC2SYS_DB_STATUS)
3515 writel(0, &ioc->chip->HostInterruptStatus);
3517 /* send message to ioc */
3518 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
3519 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
3520 &ioc->chip->Doorbell);
3522 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
3524 "doorbell handshake int failed (line=%d)\n",
3525 ioc->name, __LINE__);
3528 writel(0, &ioc->chip->HostInterruptStatus);
3530 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
3532 "doorbell handshake ack failed (line=%d)\n",
3533 ioc->name, __LINE__);
3537 /* send message 32-bits at a time */
3538 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
3539 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
3540 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
3546 "doorbell handshake sending request failed (line=%d)\n",
3547 ioc->name, __LINE__);
3551 /* now wait for the reply */
3552 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
3554 "doorbell handshake int failed (line=%d)\n",
3555 ioc->name, __LINE__);
3559 /* read the first two 16-bits, it gives the total length of the reply */
3560 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3561 & MPI2_DOORBELL_DATA_MASK);
3562 writel(0, &ioc->chip->HostInterruptStatus);
3563 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3565 "doorbell handshake int failed (line=%d)\n",
3566 ioc->name, __LINE__);
3569 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3570 & MPI2_DOORBELL_DATA_MASK);
3571 writel(0, &ioc->chip->HostInterruptStatus);
3573 for (i = 2; i < default_reply->MsgLength * 2; i++) {
3574 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3576 "doorbell handshake int failed (line=%d)\n",
3577 ioc->name, __LINE__);
3580 if (i >= reply_bytes/2) /* overflow case */
3581 dummy = readl(&ioc->chip->Doorbell);
3583 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3584 & MPI2_DOORBELL_DATA_MASK);
3585 writel(0, &ioc->chip->HostInterruptStatus);
3588 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3589 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3590 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3591 "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
3593 writel(0, &ioc->chip->HostInterruptStatus);
3595 if (ioc->logging_level & MPT_DEBUG_INIT) {
3596 mfp = (__le32 *)reply;
3597 pr_info("\toffset:data\n");
3598 for (i = 0; i < reply_bytes/4; i++)
3599 pr_info("\t[0x%02x]:%08x\n", i*4,
3600 le32_to_cpu(mfp[i]));
3606 * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
3607 * @ioc: per adapter object
3608 * @mpi_reply: the reply payload from FW
3609 * @mpi_request: the request payload sent to FW
3611 * The SAS IO Unit Control Request message allows the host to perform low-level
3612 * operations, such as resets on the PHYs of the IO Unit, also allows the host
3613 * to obtain the IOC assigned device handles for a device if it has other
3614 * identifying information about the device, in addition allows the host to
3615 * remove IOC resources associated with the device.
3617 * Returns 0 for success, non-zero for failure.
3620 mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
3621 Mpi2SasIoUnitControlReply_t *mpi_reply,
3622 Mpi2SasIoUnitControlRequest_t *mpi_request)
3626 unsigned long timeleft;
3627 bool issue_reset = false;
3630 u16 wait_state_count;
3632 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3635 mutex_lock(&ioc->base_cmds.mutex);
3637 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
3638 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
3639 ioc->name, __func__);
3644 wait_state_count = 0;
3645 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3646 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3647 if (wait_state_count++ == 10) {
3649 "%s: failed due to ioc not operational\n",
3650 ioc->name, __func__);
3655 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3657 "%s: waiting for operational state(count=%d)\n",
3658 ioc->name, __func__, wait_state_count);
3661 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
3663 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3664 ioc->name, __func__);
3670 ioc->base_cmds.status = MPT3_CMD_PENDING;
3671 request = mpt3sas_base_get_msg_frame(ioc, smid);
3672 ioc->base_cmds.smid = smid;
3673 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3674 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3675 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3676 ioc->ioc_link_reset_in_progress = 1;
3677 init_completion(&ioc->base_cmds.done);
3678 mpt3sas_base_put_smid_default(ioc, smid);
3679 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3680 msecs_to_jiffies(10000));
3681 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3682 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
3683 ioc->ioc_link_reset_in_progress)
3684 ioc->ioc_link_reset_in_progress = 0;
3685 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
3686 pr_err(MPT3SAS_FMT "%s: timeout\n",
3687 ioc->name, __func__);
3688 _debug_dump_mf(mpi_request,
3689 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
3690 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
3692 goto issue_host_reset;
3694 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
3695 memcpy(mpi_reply, ioc->base_cmds.reply,
3696 sizeof(Mpi2SasIoUnitControlReply_t));
3698 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
3699 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3704 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3706 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3709 mutex_unlock(&ioc->base_cmds.mutex);
3714 * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
3715 * @ioc: per adapter object
3716 * @mpi_reply: the reply payload from FW
3717 * @mpi_request: the request payload sent to FW
3719 * The SCSI Enclosure Processor request message causes the IOC to
3720 * communicate with SES devices to control LED status signals.
3722 * Returns 0 for success, non-zero for failure.
3725 mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
3726 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
3730 unsigned long timeleft;
3731 bool issue_reset = false;
3734 u16 wait_state_count;
3736 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3739 mutex_lock(&ioc->base_cmds.mutex);
3741 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
3742 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
3743 ioc->name, __func__);
3748 wait_state_count = 0;
3749 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3750 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3751 if (wait_state_count++ == 10) {
3753 "%s: failed due to ioc not operational\n",
3754 ioc->name, __func__);
3759 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3761 "%s: waiting for operational state(count=%d)\n",
3763 __func__, wait_state_count);
3766 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
3768 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3769 ioc->name, __func__);
3775 ioc->base_cmds.status = MPT3_CMD_PENDING;
3776 request = mpt3sas_base_get_msg_frame(ioc, smid);
3777 ioc->base_cmds.smid = smid;
3778 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
3779 init_completion(&ioc->base_cmds.done);
3780 mpt3sas_base_put_smid_default(ioc, smid);
3781 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3782 msecs_to_jiffies(10000));
3783 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
3784 pr_err(MPT3SAS_FMT "%s: timeout\n",
3785 ioc->name, __func__);
3786 _debug_dump_mf(mpi_request,
3787 sizeof(Mpi2SepRequest_t)/4);
3788 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
3789 issue_reset = false;
3790 goto issue_host_reset;
3792 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
3793 memcpy(mpi_reply, ioc->base_cmds.reply,
3794 sizeof(Mpi2SepReply_t));
3796 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3797 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3802 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3804 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3807 mutex_unlock(&ioc->base_cmds.mutex);
3812 * _base_get_port_facts - obtain port facts reply and save in ioc
3813 * @ioc: per adapter object
3814 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3816 * Returns 0 for success, non-zero for failure.
3819 _base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port, int sleep_flag)
3821 Mpi2PortFactsRequest_t mpi_request;
3822 Mpi2PortFactsReply_t mpi_reply;
3823 struct mpt3sas_port_facts *pfacts;
3824 int mpi_reply_sz, mpi_request_sz, r;
3826 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3829 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3830 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3831 memset(&mpi_request, 0, mpi_request_sz);
3832 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3833 mpi_request.PortNumber = port;
3834 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3835 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3838 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
3839 ioc->name, __func__, r);
3843 pfacts = &ioc->pfacts[port];
3844 memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
3845 pfacts->PortNumber = mpi_reply.PortNumber;
3846 pfacts->VP_ID = mpi_reply.VP_ID;
3847 pfacts->VF_ID = mpi_reply.VF_ID;
3848 pfacts->MaxPostedCmdBuffers =
3849 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3855 * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
3856 * @ioc: per adapter object
3858 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3860 * Returns 0 for success, non-zero for failure.
3863 _base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout,
3869 dinitprintk(ioc, printk(MPT3SAS_FMT "%s\n", ioc->name,
3872 if (ioc->pci_error_recovery) {
3873 dfailprintk(ioc, printk(MPT3SAS_FMT
3874 "%s: host in pci error recovery\n", ioc->name, __func__));
3878 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
3879 dhsprintk(ioc, printk(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
3880 ioc->name, __func__, ioc_state));
3882 if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
3883 (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
3886 if (ioc_state & MPI2_DOORBELL_USED) {
3887 dhsprintk(ioc, printk(MPT3SAS_FMT
3888 "unexpected doorbell active!\n", ioc->name));
3889 goto issue_diag_reset;
3892 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3893 mpt3sas_base_fault_info(ioc, ioc_state &
3894 MPI2_DOORBELL_DATA_MASK);
3895 goto issue_diag_reset;
3898 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
3899 timeout, sleep_flag);
3901 dfailprintk(ioc, printk(MPT3SAS_FMT
3902 "%s: failed going to ready state (ioc_state=0x%x)\n",
3903 ioc->name, __func__, ioc_state));
3908 rc = _base_diag_reset(ioc, sleep_flag);
3913 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3914 * @ioc: per adapter object
3915 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3917 * Returns 0 for success, non-zero for failure.
3920 _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
3922 Mpi2IOCFactsRequest_t mpi_request;
3923 Mpi2IOCFactsReply_t mpi_reply;
3924 struct mpt3sas_facts *facts;
3925 int mpi_reply_sz, mpi_request_sz, r;
3927 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3930 r = _base_wait_for_iocstate(ioc, 10, sleep_flag);
3932 dfailprintk(ioc, printk(MPT3SAS_FMT
3933 "%s: failed getting to correct state\n",
3934 ioc->name, __func__));
3937 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3938 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3939 memset(&mpi_request, 0, mpi_request_sz);
3940 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3941 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3942 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3945 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
3946 ioc->name, __func__, r);
3950 facts = &ioc->facts;
3951 memset(facts, 0, sizeof(struct mpt3sas_facts));
3952 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3953 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3954 facts->VP_ID = mpi_reply.VP_ID;
3955 facts->VF_ID = mpi_reply.VF_ID;
3956 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3957 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3958 facts->WhoInit = mpi_reply.WhoInit;
3959 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
3960 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
3961 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3962 facts->MaxReplyDescriptorPostQueueDepth =
3963 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3964 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3965 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3966 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3967 ioc->ir_firmware = 1;
3968 if ((facts->IOCCapabilities &
3969 MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE))
3970 ioc->rdpq_array_capable = 1;
3971 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3972 facts->IOCRequestFrameSize =
3973 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3974 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3975 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3976 ioc->shost->max_id = -1;
3977 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3978 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3979 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3980 facts->HighPriorityCredit =
3981 le16_to_cpu(mpi_reply.HighPriorityCredit);
3982 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3983 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3985 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3986 "hba queue depth(%d), max chains per io(%d)\n",
3987 ioc->name, facts->RequestCredit,
3988 facts->MaxChainDepth));
3989 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3990 "request frame size(%d), reply frame size(%d)\n", ioc->name,
3991 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3996 * _base_send_ioc_init - send ioc_init to firmware
3997 * @ioc: per adapter object
3998 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4000 * Returns 0 for success, non-zero for failure.
4003 _base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4005 Mpi2IOCInitRequest_t mpi_request;
4006 Mpi2IOCInitReply_t mpi_reply;
4008 struct timeval current_time;
4010 u32 reply_post_free_array_sz = 0;
4011 Mpi2IOCInitRDPQArrayEntry *reply_post_free_array = NULL;
4012 dma_addr_t reply_post_free_array_dma;
4014 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4017 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
4018 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
4019 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
4020 mpi_request.VF_ID = 0; /* TODO */
4021 mpi_request.VP_ID = 0;
4022 mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
4023 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
4025 if (_base_is_controller_msix_enabled(ioc))
4026 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
4027 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
4028 mpi_request.ReplyDescriptorPostQueueDepth =
4029 cpu_to_le16(ioc->reply_post_queue_depth);
4030 mpi_request.ReplyFreeQueueDepth =
4031 cpu_to_le16(ioc->reply_free_queue_depth);
4033 mpi_request.SenseBufferAddressHigh =
4034 cpu_to_le32((u64)ioc->sense_dma >> 32);
4035 mpi_request.SystemReplyAddressHigh =
4036 cpu_to_le32((u64)ioc->reply_dma >> 32);
4037 mpi_request.SystemRequestFrameBaseAddress =
4038 cpu_to_le64((u64)ioc->request_dma);
4039 mpi_request.ReplyFreeQueueAddress =
4040 cpu_to_le64((u64)ioc->reply_free_dma);
4042 if (ioc->rdpq_array_enable) {
4043 reply_post_free_array_sz = ioc->reply_queue_count *
4044 sizeof(Mpi2IOCInitRDPQArrayEntry);
4045 reply_post_free_array = pci_alloc_consistent(ioc->pdev,
4046 reply_post_free_array_sz, &reply_post_free_array_dma);
4047 if (!reply_post_free_array) {
4049 "reply_post_free_array: pci_alloc_consistent failed\n",
4054 memset(reply_post_free_array, 0, reply_post_free_array_sz);
4055 for (i = 0; i < ioc->reply_queue_count; i++)
4056 reply_post_free_array[i].RDPQBaseAddress =
4058 (u64)ioc->reply_post[i].reply_post_free_dma);
4059 mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
4060 mpi_request.ReplyDescriptorPostQueueAddress =
4061 cpu_to_le64((u64)reply_post_free_array_dma);
4063 mpi_request.ReplyDescriptorPostQueueAddress =
4064 cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
4067 /* This time stamp specifies number of milliseconds
4068 * since epoch ~ midnight January 1, 1970.
4070 do_gettimeofday(¤t_time);
4071 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
4072 (current_time.tv_usec / 1000));
4074 if (ioc->logging_level & MPT_DEBUG_INIT) {
4078 mfp = (__le32 *)&mpi_request;
4079 pr_info("\toffset:data\n");
4080 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
4081 pr_info("\t[0x%02x]:%08x\n", i*4,
4082 le32_to_cpu(mfp[i]));
4085 r = _base_handshake_req_reply_wait(ioc,
4086 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
4087 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
4091 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
4092 ioc->name, __func__, r);
4096 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4097 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
4098 mpi_reply.IOCLogInfo) {
4099 pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
4104 if (reply_post_free_array)
4105 pci_free_consistent(ioc->pdev, reply_post_free_array_sz,
4106 reply_post_free_array,
4107 reply_post_free_array_dma);
4112 * mpt3sas_port_enable_done - command completion routine for port enable
4113 * @ioc: per adapter object
4114 * @smid: system request message index
4115 * @msix_index: MSIX table index supplied by the OS
4116 * @reply: reply message frame(lower 32bit addr)
4118 * Return 1 meaning mf should be freed from _base_interrupt
4119 * 0 means the mf is freed from this function.
4122 mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
4125 MPI2DefaultReply_t *mpi_reply;
4128 if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
4131 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4135 if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
4138 ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
4139 ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
4140 ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
4141 memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
4142 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
4143 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4144 ioc->port_enable_failed = 1;
4146 if (ioc->is_driver_loading) {
4147 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
4148 mpt3sas_port_enable_complete(ioc);
4151 ioc->start_scan_failed = ioc_status;
4152 ioc->start_scan = 0;
4156 complete(&ioc->port_enable_cmds.done);
4161 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
4162 * @ioc: per adapter object
4163 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4165 * Returns 0 for success, non-zero for failure.
4168 _base_send_port_enable(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4170 Mpi2PortEnableRequest_t *mpi_request;
4171 Mpi2PortEnableReply_t *mpi_reply;
4172 unsigned long timeleft;
4177 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
4179 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
4180 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
4181 ioc->name, __func__);
4185 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
4187 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4188 ioc->name, __func__);
4192 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
4193 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4194 ioc->port_enable_cmds.smid = smid;
4195 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
4196 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
4198 init_completion(&ioc->port_enable_cmds.done);
4199 mpt3sas_base_put_smid_default(ioc, smid);
4200 timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
4202 if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
4203 pr_err(MPT3SAS_FMT "%s: timeout\n",
4204 ioc->name, __func__);
4205 _debug_dump_mf(mpi_request,
4206 sizeof(Mpi2PortEnableRequest_t)/4);
4207 if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
4214 mpi_reply = ioc->port_enable_cmds.reply;
4215 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
4216 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4217 pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
4218 ioc->name, __func__, ioc_status);
4224 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
4225 pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
4226 "SUCCESS" : "FAILED"));
4231 * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
4232 * @ioc: per adapter object
4234 * Returns 0 for success, non-zero for failure.
4237 mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
4239 Mpi2PortEnableRequest_t *mpi_request;
4242 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
4244 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
4245 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
4246 ioc->name, __func__);
4250 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
4252 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4253 ioc->name, __func__);
4257 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
4258 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4259 ioc->port_enable_cmds.smid = smid;
4260 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
4261 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
4263 mpt3sas_base_put_smid_default(ioc, smid);
4268 * _base_determine_wait_on_discovery - desposition
4269 * @ioc: per adapter object
4271 * Decide whether to wait on discovery to complete. Used to either
4272 * locate boot device, or report volumes ahead of physical devices.
4274 * Returns 1 for wait, 0 for don't wait
4277 _base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
4279 /* We wait for discovery to complete if IR firmware is loaded.
4280 * The sas topology events arrive before PD events, so we need time to
4281 * turn on the bit in ioc->pd_handles to indicate PD
4282 * Also, it maybe required to report Volumes ahead of physical
4283 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
4285 if (ioc->ir_firmware)
4288 /* if no Bios, then we don't need to wait */
4289 if (!ioc->bios_pg3.BiosVersion)
4292 /* Bios is present, then we drop down here.
4294 * If there any entries in the Bios Page 2, then we wait
4295 * for discovery to complete.
4298 /* Current Boot Device */
4299 if ((ioc->bios_pg2.CurrentBootDeviceForm &
4300 MPI2_BIOSPAGE2_FORM_MASK) ==
4301 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
4302 /* Request Boot Device */
4303 (ioc->bios_pg2.ReqBootDeviceForm &
4304 MPI2_BIOSPAGE2_FORM_MASK) ==
4305 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
4306 /* Alternate Request Boot Device */
4307 (ioc->bios_pg2.ReqAltBootDeviceForm &
4308 MPI2_BIOSPAGE2_FORM_MASK) ==
4309 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
4316 * _base_unmask_events - turn on notification for this event
4317 * @ioc: per adapter object
4318 * @event: firmware event
4320 * The mask is stored in ioc->event_masks.
4323 _base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
4330 desired_event = (1 << (event % 32));
4333 ioc->event_masks[0] &= ~desired_event;
4334 else if (event < 64)
4335 ioc->event_masks[1] &= ~desired_event;
4336 else if (event < 96)
4337 ioc->event_masks[2] &= ~desired_event;
4338 else if (event < 128)
4339 ioc->event_masks[3] &= ~desired_event;
4343 * _base_event_notification - send event notification
4344 * @ioc: per adapter object
4345 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4347 * Returns 0 for success, non-zero for failure.
4350 _base_event_notification(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4352 Mpi2EventNotificationRequest_t *mpi_request;
4353 unsigned long timeleft;
4358 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4361 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
4362 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
4363 ioc->name, __func__);
4367 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4369 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4370 ioc->name, __func__);
4373 ioc->base_cmds.status = MPT3_CMD_PENDING;
4374 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4375 ioc->base_cmds.smid = smid;
4376 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
4377 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4378 mpi_request->VF_ID = 0; /* TODO */
4379 mpi_request->VP_ID = 0;
4380 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4381 mpi_request->EventMasks[i] =
4382 cpu_to_le32(ioc->event_masks[i]);
4383 init_completion(&ioc->base_cmds.done);
4384 mpt3sas_base_put_smid_default(ioc, smid);
4385 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
4386 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4387 pr_err(MPT3SAS_FMT "%s: timeout\n",
4388 ioc->name, __func__);
4389 _debug_dump_mf(mpi_request,
4390 sizeof(Mpi2EventNotificationRequest_t)/4);
4391 if (ioc->base_cmds.status & MPT3_CMD_RESET)
4396 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
4397 ioc->name, __func__));
4398 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4403 * mpt3sas_base_validate_event_type - validating event types
4404 * @ioc: per adapter object
4405 * @event: firmware event
4407 * This will turn on firmware event notification when application
4408 * ask for that event. We don't mask events that are already enabled.
4411 mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
4414 u32 event_mask, desired_event;
4415 u8 send_update_to_fw;
4417 for (i = 0, send_update_to_fw = 0; i <
4418 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
4419 event_mask = ~event_type[i];
4421 for (j = 0; j < 32; j++) {
4422 if (!(event_mask & desired_event) &&
4423 (ioc->event_masks[i] & desired_event)) {
4424 ioc->event_masks[i] &= ~desired_event;
4425 send_update_to_fw = 1;
4427 desired_event = (desired_event << 1);
4431 if (!send_update_to_fw)
4434 mutex_lock(&ioc->base_cmds.mutex);
4435 _base_event_notification(ioc, CAN_SLEEP);
4436 mutex_unlock(&ioc->base_cmds.mutex);
4440 * _base_diag_reset - the "big hammer" start of day reset
4441 * @ioc: per adapter object
4442 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4444 * Returns 0 for success, non-zero for failure.
4447 _base_diag_reset(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4449 u32 host_diagnostic;
4454 pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
4456 drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
4461 /* Write magic sequence to WriteSequence register
4462 * Loop until in diagnostic mode
4464 drsprintk(ioc, pr_info(MPT3SAS_FMT
4465 "write magic sequence\n", ioc->name));
4466 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4467 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
4468 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
4469 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
4470 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
4471 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
4472 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
4475 if (sleep_flag == CAN_SLEEP)
4483 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4484 drsprintk(ioc, pr_info(MPT3SAS_FMT
4485 "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
4486 ioc->name, count, host_diagnostic));
4488 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
4490 hcb_size = readl(&ioc->chip->HCBSize);
4492 drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
4494 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
4495 &ioc->chip->HostDiagnostic);
4497 /*This delay allows the chip PCIe hardware time to finish reset tasks*/
4498 if (sleep_flag == CAN_SLEEP)
4499 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
4501 mdelay(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
4503 /* Approximately 300 second max wait */
4504 for (count = 0; count < (300000000 /
4505 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
4507 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4509 if (host_diagnostic == 0xFFFFFFFF)
4511 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
4514 /* Wait to pass the second read delay window */
4515 if (sleep_flag == CAN_SLEEP)
4516 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4519 mdelay(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4523 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
4525 drsprintk(ioc, pr_info(MPT3SAS_FMT
4526 "restart the adapter assuming the HCB Address points to good F/W\n",
4528 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
4529 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
4530 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
4532 drsprintk(ioc, pr_info(MPT3SAS_FMT
4533 "re-enable the HCDW\n", ioc->name));
4534 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
4535 &ioc->chip->HCBSize);
4538 drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
4540 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
4541 &ioc->chip->HostDiagnostic);
4543 drsprintk(ioc, pr_info(MPT3SAS_FMT
4544 "disable writes to the diagnostic register\n", ioc->name));
4545 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4547 drsprintk(ioc, pr_info(MPT3SAS_FMT
4548 "Wait for FW to go to the READY state\n", ioc->name));
4549 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
4553 "%s: failed going to ready state (ioc_state=0x%x)\n",
4554 ioc->name, __func__, ioc_state);
4558 pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
4562 pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
4567 * _base_make_ioc_ready - put controller in READY state
4568 * @ioc: per adapter object
4569 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4570 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4572 * Returns 0 for success, non-zero for failure.
4575 _base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
4576 enum reset_type type)
4582 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4585 if (ioc->pci_error_recovery)
4588 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4589 dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
4590 ioc->name, __func__, ioc_state));
4592 /* if in RESET state, it should move to READY state shortly */
4594 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
4595 while ((ioc_state & MPI2_IOC_STATE_MASK) !=
4596 MPI2_IOC_STATE_READY) {
4597 if (count++ == 10) {
4599 "%s: failed going to ready state (ioc_state=0x%x)\n",
4600 ioc->name, __func__, ioc_state);
4603 if (sleep_flag == CAN_SLEEP)
4607 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4611 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
4614 if (ioc_state & MPI2_DOORBELL_USED) {
4615 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4616 "unexpected doorbell active!\n",
4618 goto issue_diag_reset;
4621 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4622 mpt3sas_base_fault_info(ioc, ioc_state &
4623 MPI2_DOORBELL_DATA_MASK);
4624 goto issue_diag_reset;
4627 if (type == FORCE_BIG_HAMMER)
4628 goto issue_diag_reset;
4630 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4631 if (!(_base_send_ioc_reset(ioc,
4632 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
4637 rc = _base_diag_reset(ioc, CAN_SLEEP);
4642 * _base_make_ioc_operational - put controller in OPERATIONAL state
4643 * @ioc: per adapter object
4644 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4646 * Returns 0 for success, non-zero for failure.
4649 _base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4652 unsigned long flags;
4655 struct _tr_list *delayed_tr, *delayed_tr_next;
4656 struct adapter_reply_queue *reply_q;
4657 long reply_post_free;
4658 u32 reply_post_free_sz, index = 0;
4660 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4663 /* clean the delayed target reset list */
4664 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4665 &ioc->delayed_tr_list, list) {
4666 list_del(&delayed_tr->list);
4671 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4672 &ioc->delayed_tr_volume_list, list) {
4673 list_del(&delayed_tr->list);
4677 /* initialize the scsi lookup free list */
4678 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4679 INIT_LIST_HEAD(&ioc->free_list);
4681 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
4682 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
4683 ioc->scsi_lookup[i].cb_idx = 0xFF;
4684 ioc->scsi_lookup[i].smid = smid;
4685 ioc->scsi_lookup[i].scmd = NULL;
4686 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
4690 /* hi-priority queue */
4691 INIT_LIST_HEAD(&ioc->hpr_free_list);
4692 smid = ioc->hi_priority_smid;
4693 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
4694 ioc->hpr_lookup[i].cb_idx = 0xFF;
4695 ioc->hpr_lookup[i].smid = smid;
4696 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
4697 &ioc->hpr_free_list);
4700 /* internal queue */
4701 INIT_LIST_HEAD(&ioc->internal_free_list);
4702 smid = ioc->internal_smid;
4703 for (i = 0; i < ioc->internal_depth; i++, smid++) {
4704 ioc->internal_lookup[i].cb_idx = 0xFF;
4705 ioc->internal_lookup[i].smid = smid;
4706 list_add_tail(&ioc->internal_lookup[i].tracker_list,
4707 &ioc->internal_free_list);
4711 INIT_LIST_HEAD(&ioc->free_chain_list);
4712 for (i = 0; i < ioc->chain_depth; i++)
4713 list_add_tail(&ioc->chain_lookup[i].tracker_list,
4714 &ioc->free_chain_list);
4716 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4718 /* initialize Reply Free Queue */
4719 for (i = 0, reply_address = (u32)ioc->reply_dma ;
4720 i < ioc->reply_free_queue_depth ; i++, reply_address +=
4722 ioc->reply_free[i] = cpu_to_le32(reply_address);
4724 /* initialize reply queues */
4725 if (ioc->is_driver_loading)
4726 _base_assign_reply_queues(ioc);
4728 /* initialize Reply Post Free Queue */
4729 reply_post_free_sz = ioc->reply_post_queue_depth *
4730 sizeof(Mpi2DefaultReplyDescriptor_t);
4731 reply_post_free = (long)ioc->reply_post[index].reply_post_free;
4732 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4733 reply_q->reply_post_host_index = 0;
4734 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
4736 for (i = 0; i < ioc->reply_post_queue_depth; i++)
4737 reply_q->reply_post_free[i].Words =
4738 cpu_to_le64(ULLONG_MAX);
4739 if (!_base_is_controller_msix_enabled(ioc))
4740 goto skip_init_reply_post_free_queue;
4742 * If RDPQ is enabled, switch to the next allocation.
4743 * Otherwise advance within the contiguous region.
4745 if (ioc->rdpq_array_enable)
4746 reply_post_free = (long)
4747 ioc->reply_post[++index].reply_post_free;
4749 reply_post_free += reply_post_free_sz;
4751 skip_init_reply_post_free_queue:
4753 r = _base_send_ioc_init(ioc, sleep_flag);
4757 /* initialize reply free host index */
4758 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
4759 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
4761 /* initialize reply post host index */
4762 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4763 if (ioc->msix96_vector)
4764 writel((reply_q->msix_index & 7)<<
4765 MPI2_RPHI_MSIX_INDEX_SHIFT,
4766 ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
4768 writel(reply_q->msix_index <<
4769 MPI2_RPHI_MSIX_INDEX_SHIFT,
4770 &ioc->chip->ReplyPostHostIndex);
4772 if (!_base_is_controller_msix_enabled(ioc))
4773 goto skip_init_reply_post_host_index;
4776 skip_init_reply_post_host_index:
4778 _base_unmask_interrupts(ioc);
4779 r = _base_event_notification(ioc, sleep_flag);
4783 if (sleep_flag == CAN_SLEEP)
4784 _base_static_config_pages(ioc);
4787 if (ioc->is_driver_loading) {
4788 ioc->wait_for_discovery_to_complete =
4789 _base_determine_wait_on_discovery(ioc);
4791 return r; /* scan_start and scan_finished support */
4794 r = _base_send_port_enable(ioc, sleep_flag);
4802 * mpt3sas_base_free_resources - free resources controller resources
4803 * @ioc: per adapter object
4808 mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
4810 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4813 if (ioc->chip_phys && ioc->chip) {
4814 _base_mask_interrupts(ioc);
4815 ioc->shost_recovery = 1;
4816 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4817 ioc->shost_recovery = 0;
4820 mpt3sas_base_unmap_resources(ioc);
4825 * mpt3sas_base_attach - attach controller instance
4826 * @ioc: per adapter object
4828 * Returns 0 for success, non-zero for failure.
4831 mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
4834 int cpu_id, last_cpu_id = 0;
4837 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4840 /* setup cpu_msix_table */
4841 ioc->cpu_count = num_online_cpus();
4842 for_each_online_cpu(cpu_id)
4843 last_cpu_id = cpu_id;
4844 ioc->cpu_msix_table_sz = last_cpu_id + 1;
4845 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
4846 ioc->reply_queue_count = 1;
4847 if (!ioc->cpu_msix_table) {
4848 dfailprintk(ioc, pr_info(MPT3SAS_FMT
4849 "allocation for cpu_msix_table failed!!!\n",
4852 goto out_free_resources;
4855 /* Check whether the controller revision is C0 or above.
4856 * only C0 and above revision controllers support 96 MSI-X vectors.
4858 revision = ioc->pdev->revision;
4860 if ((ioc->pdev->device == MPI25_MFGPAGE_DEVID_SAS3004 ||
4861 ioc->pdev->device == MPI25_MFGPAGE_DEVID_SAS3008 ||
4862 ioc->pdev->device == MPI25_MFGPAGE_DEVID_SAS3108_1 ||
4863 ioc->pdev->device == MPI25_MFGPAGE_DEVID_SAS3108_2 ||
4864 ioc->pdev->device == MPI25_MFGPAGE_DEVID_SAS3108_5 ||
4865 ioc->pdev->device == MPI25_MFGPAGE_DEVID_SAS3108_6) &&
4867 ioc->msix96_vector = 1;
4869 ioc->rdpq_array_enable_assigned = 0;
4871 r = mpt3sas_base_map_resources(ioc);
4873 goto out_free_resources;
4876 pci_set_drvdata(ioc->pdev, ioc->shost);
4877 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
4879 goto out_free_resources;
4883 * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
4884 * Target Status - all require the IEEE formated scatter gather
4888 ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
4889 ioc->build_sg = &_base_build_sg_ieee;
4890 ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
4891 ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
4894 * These function pointers for other requests that don't
4895 * the require IEEE scatter gather elements.
4897 * For example Configuration Pages and SAS IOUNIT Control don't.
4899 ioc->build_sg_mpi = &_base_build_sg;
4900 ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
4902 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4904 goto out_free_resources;
4906 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
4907 sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
4910 goto out_free_resources;
4913 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
4914 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
4916 goto out_free_resources;
4919 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
4921 goto out_free_resources;
4923 init_waitqueue_head(&ioc->reset_wq);
4925 /* allocate memory pd handle bitmask list */
4926 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
4927 if (ioc->facts.MaxDevHandle % 8)
4928 ioc->pd_handles_sz++;
4929 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
4931 if (!ioc->pd_handles) {
4933 goto out_free_resources;
4935 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
4937 if (!ioc->blocking_handles) {
4939 goto out_free_resources;
4942 ioc->fwfault_debug = mpt3sas_fwfault_debug;
4944 /* base internal command bits */
4945 mutex_init(&ioc->base_cmds.mutex);
4946 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4947 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4949 /* port_enable command bits */
4950 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4951 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
4953 /* transport internal command bits */
4954 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4955 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
4956 mutex_init(&ioc->transport_cmds.mutex);
4958 /* scsih internal command bits */
4959 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4960 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
4961 mutex_init(&ioc->scsih_cmds.mutex);
4963 /* task management internal command bits */
4964 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4965 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
4966 mutex_init(&ioc->tm_cmds.mutex);
4968 /* config page internal command bits */
4969 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4970 ioc->config_cmds.status = MPT3_CMD_NOT_USED;
4971 mutex_init(&ioc->config_cmds.mutex);
4973 /* ctl module internal command bits */
4974 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4975 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4976 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
4977 mutex_init(&ioc->ctl_cmds.mutex);
4979 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4980 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4981 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
4982 !ioc->ctl_cmds.sense) {
4984 goto out_free_resources;
4987 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4988 ioc->event_masks[i] = -1;
4990 /* here we enable the events we care about */
4991 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
4992 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
4993 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
4994 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4995 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
4996 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
4997 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
4998 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
4999 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
5000 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
5001 _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
5003 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
5005 goto out_free_resources;
5011 ioc->remove_host = 1;
5013 mpt3sas_base_free_resources(ioc);
5014 _base_release_memory_pools(ioc);
5015 pci_set_drvdata(ioc->pdev, NULL);
5016 kfree(ioc->cpu_msix_table);
5017 kfree(ioc->pd_handles);
5018 kfree(ioc->blocking_handles);
5019 kfree(ioc->tm_cmds.reply);
5020 kfree(ioc->transport_cmds.reply);
5021 kfree(ioc->scsih_cmds.reply);
5022 kfree(ioc->config_cmds.reply);
5023 kfree(ioc->base_cmds.reply);
5024 kfree(ioc->port_enable_cmds.reply);
5025 kfree(ioc->ctl_cmds.reply);
5026 kfree(ioc->ctl_cmds.sense);
5028 ioc->ctl_cmds.reply = NULL;
5029 ioc->base_cmds.reply = NULL;
5030 ioc->tm_cmds.reply = NULL;
5031 ioc->scsih_cmds.reply = NULL;
5032 ioc->transport_cmds.reply = NULL;
5033 ioc->config_cmds.reply = NULL;
5040 * mpt3sas_base_detach - remove controller instance
5041 * @ioc: per adapter object
5046 mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
5048 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5051 mpt3sas_base_stop_watchdog(ioc);
5052 mpt3sas_base_free_resources(ioc);
5053 _base_release_memory_pools(ioc);
5054 pci_set_drvdata(ioc->pdev, NULL);
5055 kfree(ioc->cpu_msix_table);
5056 kfree(ioc->pd_handles);
5057 kfree(ioc->blocking_handles);
5059 kfree(ioc->ctl_cmds.reply);
5060 kfree(ioc->ctl_cmds.sense);
5061 kfree(ioc->base_cmds.reply);
5062 kfree(ioc->port_enable_cmds.reply);
5063 kfree(ioc->tm_cmds.reply);
5064 kfree(ioc->transport_cmds.reply);
5065 kfree(ioc->scsih_cmds.reply);
5066 kfree(ioc->config_cmds.reply);
5070 * _base_reset_handler - reset callback handler (for base)
5071 * @ioc: per adapter object
5072 * @reset_phase: phase
5074 * The handler for doing any required cleanup or initialization.
5076 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
5077 * MPT3_IOC_DONE_RESET
5082 _base_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
5084 mpt3sas_scsih_reset_handler(ioc, reset_phase);
5085 mpt3sas_ctl_reset_handler(ioc, reset_phase);
5086 switch (reset_phase) {
5087 case MPT3_IOC_PRE_RESET:
5088 dtmprintk(ioc, pr_info(MPT3SAS_FMT
5089 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
5091 case MPT3_IOC_AFTER_RESET:
5092 dtmprintk(ioc, pr_info(MPT3SAS_FMT
5093 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
5094 if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
5095 ioc->transport_cmds.status |= MPT3_CMD_RESET;
5096 mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
5097 complete(&ioc->transport_cmds.done);
5099 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
5100 ioc->base_cmds.status |= MPT3_CMD_RESET;
5101 mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
5102 complete(&ioc->base_cmds.done);
5104 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
5105 ioc->port_enable_failed = 1;
5106 ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
5107 mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
5108 if (ioc->is_driver_loading) {
5109 ioc->start_scan_failed =
5110 MPI2_IOCSTATUS_INTERNAL_ERROR;
5111 ioc->start_scan = 0;
5112 ioc->port_enable_cmds.status =
5115 complete(&ioc->port_enable_cmds.done);
5117 if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
5118 ioc->config_cmds.status |= MPT3_CMD_RESET;
5119 mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
5120 ioc->config_cmds.smid = USHRT_MAX;
5121 complete(&ioc->config_cmds.done);
5124 case MPT3_IOC_DONE_RESET:
5125 dtmprintk(ioc, pr_info(MPT3SAS_FMT
5126 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
5132 * _wait_for_commands_to_complete - reset controller
5133 * @ioc: Pointer to MPT_ADAPTER structure
5134 * @sleep_flag: CAN_SLEEP or NO_SLEEP
5136 * This function waiting(3s) for all pending commands to complete
5137 * prior to putting controller in reset.
5140 _wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
5143 unsigned long flags;
5146 ioc->pending_io_count = 0;
5147 if (sleep_flag != CAN_SLEEP)
5150 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5151 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
5154 /* pending command count */
5155 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5156 for (i = 0; i < ioc->scsiio_depth; i++)
5157 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
5158 ioc->pending_io_count++;
5159 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5161 if (!ioc->pending_io_count)
5164 /* wait for pending commands to complete */
5165 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
5169 * mpt3sas_base_hard_reset_handler - reset controller
5170 * @ioc: Pointer to MPT_ADAPTER structure
5171 * @sleep_flag: CAN_SLEEP or NO_SLEEP
5172 * @type: FORCE_BIG_HAMMER or SOFT_RESET
5174 * Returns 0 for success, non-zero for failure.
5177 mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
5178 enum reset_type type)
5181 unsigned long flags;
5183 u8 is_fault = 0, is_trigger = 0;
5185 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
5188 if (ioc->pci_error_recovery) {
5189 pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
5190 ioc->name, __func__);
5195 if (mpt3sas_fwfault_debug)
5196 mpt3sas_halt_firmware(ioc);
5198 /* TODO - What we really should be doing is pulling
5199 * out all the code associated with NO_SLEEP; its never used.
5200 * That is legacy code from mpt fusion driver, ported over.
5201 * I will leave this BUG_ON here for now till its been resolved.
5203 BUG_ON(sleep_flag == NO_SLEEP);
5205 /* wait for an active reset in progress to complete */
5206 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
5209 } while (ioc->shost_recovery == 1);
5210 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
5212 return ioc->ioc_reset_in_progress_status;
5215 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5216 ioc->shost_recovery = 1;
5217 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5219 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
5220 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
5221 (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
5222 MPT3_DIAG_BUFFER_IS_RELEASED))) {
5224 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5225 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
5228 _base_reset_handler(ioc, MPT3_IOC_PRE_RESET);
5229 _wait_for_commands_to_complete(ioc, sleep_flag);
5230 _base_mask_interrupts(ioc);
5231 r = _base_make_ioc_ready(ioc, sleep_flag, type);
5234 _base_reset_handler(ioc, MPT3_IOC_AFTER_RESET);
5236 /* If this hard reset is called while port enable is active, then
5237 * there is no reason to call make_ioc_operational
5239 if (ioc->is_driver_loading && ioc->port_enable_failed) {
5240 ioc->remove_host = 1;
5244 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
5248 if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
5249 panic("%s: Issue occurred with flashing controller firmware."
5250 "Please reboot the system and ensure that the correct"
5251 " firmware version is running\n", ioc->name);
5253 r = _base_make_ioc_operational(ioc, sleep_flag);
5255 _base_reset_handler(ioc, MPT3_IOC_DONE_RESET);
5258 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
5259 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
5261 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
5262 ioc->ioc_reset_in_progress_status = r;
5263 ioc->shost_recovery = 0;
5264 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
5265 ioc->ioc_reset_count++;
5266 mutex_unlock(&ioc->reset_in_progress_mutex);
5269 if ((r == 0) && is_trigger) {
5271 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
5273 mpt3sas_trigger_master(ioc,
5274 MASTER_TRIGGER_ADAPTER_RESET);
5276 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,