2 * This is the Fusion MPT base driver providing common API layer interface
3 * for access to MPT (Message Passing Technology) firmware.
5 * This code is based on drivers/scsi/mpt2sas/mpt2_base.c
6 * Copyright (C) 2007-2008 LSI Corporation
7 * (mailto:DL-MPTFusionLinux@lsi.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
45 #include <linux/version.h>
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/slab.h>
51 #include <linux/types.h>
52 #include <linux/pci.h>
53 #include <linux/kdev_t.h>
54 #include <linux/blkdev.h>
55 #include <linux/delay.h>
56 #include <linux/interrupt.h>
57 #include <linux/dma-mapping.h>
58 #include <linux/sort.h>
61 #include "mpt2sas_base.h"
63 static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
65 #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
66 #define MPT2SAS_MAX_REQUEST_QUEUE 500 /* maximum controller queue depth */
68 static int max_queue_depth = -1;
69 module_param(max_queue_depth, int, 0);
70 MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
72 static int max_sgl_entries = -1;
73 module_param(max_sgl_entries, int, 0);
74 MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
76 static int msix_disable = -1;
77 module_param(msix_disable, int, 0);
78 MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
81 * _base_fault_reset_work - workq handling ioc fault conditions
82 * @work: input argument, used to derive ioc
88 _base_fault_reset_work(struct work_struct *work)
90 struct MPT2SAS_ADAPTER *ioc =
91 container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
96 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
97 if (ioc->ioc_reset_in_progress)
99 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
101 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
102 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
103 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
105 printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
106 __func__, (rc == 0) ? "success" : "failed");
107 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
108 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
109 mpt2sas_base_fault_info(ioc, doorbell &
110 MPI2_DOORBELL_DATA_MASK);
113 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
115 if (ioc->fault_reset_work_q)
116 queue_delayed_work(ioc->fault_reset_work_q,
117 &ioc->fault_reset_work,
118 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
119 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
122 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
124 * _base_sas_ioc_info - verbose translation of the ioc status
125 * @ioc: pointer to scsi command object
126 * @mpi_reply: reply mf payload returned from firmware
127 * @request_hdr: request mf
132 _base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
133 MPI2RequestHeader_t *request_hdr)
135 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
139 char *func_str = NULL;
141 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
142 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
143 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
144 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
147 switch (ioc_status) {
149 /****************************************************************************
150 * Common IOCStatus values for all replies
151 ****************************************************************************/
153 case MPI2_IOCSTATUS_INVALID_FUNCTION:
154 desc = "invalid function";
156 case MPI2_IOCSTATUS_BUSY:
159 case MPI2_IOCSTATUS_INVALID_SGL:
160 desc = "invalid sgl";
162 case MPI2_IOCSTATUS_INTERNAL_ERROR:
163 desc = "internal error";
165 case MPI2_IOCSTATUS_INVALID_VPID:
166 desc = "invalid vpid";
168 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
169 desc = "insufficient resources";
171 case MPI2_IOCSTATUS_INVALID_FIELD:
172 desc = "invalid field";
174 case MPI2_IOCSTATUS_INVALID_STATE:
175 desc = "invalid state";
177 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
178 desc = "op state not supported";
181 /****************************************************************************
182 * Config IOCStatus values
183 ****************************************************************************/
185 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
186 desc = "config invalid action";
188 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
189 desc = "config invalid type";
191 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
192 desc = "config invalid page";
194 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
195 desc = "config invalid data";
197 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
198 desc = "config no defaults";
200 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
201 desc = "config cant commit";
204 /****************************************************************************
206 ****************************************************************************/
208 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
209 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
210 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
211 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
212 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
213 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
214 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
215 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
216 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
217 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
218 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
219 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
222 /****************************************************************************
223 * For use by SCSI Initiator and SCSI Target end-to-end data protection
224 ****************************************************************************/
226 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
227 desc = "eedp guard error";
229 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
230 desc = "eedp ref tag error";
232 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
233 desc = "eedp app tag error";
236 /****************************************************************************
238 ****************************************************************************/
240 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
241 desc = "target invalid io index";
243 case MPI2_IOCSTATUS_TARGET_ABORTED:
244 desc = "target aborted";
246 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
247 desc = "target no conn retryable";
249 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
250 desc = "target no connection";
252 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
253 desc = "target xfer count mismatch";
255 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
256 desc = "target data offset error";
258 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
259 desc = "target too much write data";
261 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
262 desc = "target iu too short";
264 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
265 desc = "target ack nak timeout";
267 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
268 desc = "target nak received";
271 /****************************************************************************
272 * Serial Attached SCSI values
273 ****************************************************************************/
275 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
276 desc = "smp request failed";
278 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
279 desc = "smp data overrun";
282 /****************************************************************************
283 * Diagnostic Buffer Post / Diagnostic Release values
284 ****************************************************************************/
286 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
287 desc = "diagnostic released";
296 switch (request_hdr->Function) {
297 case MPI2_FUNCTION_CONFIG:
298 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
299 func_str = "config_page";
301 case MPI2_FUNCTION_SCSI_TASK_MGMT:
302 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
303 func_str = "task_mgmt";
305 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
306 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
307 func_str = "sas_iounit_ctl";
309 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
310 frame_sz = sizeof(Mpi2SepRequest_t);
311 func_str = "enclosure";
313 case MPI2_FUNCTION_IOC_INIT:
314 frame_sz = sizeof(Mpi2IOCInitRequest_t);
315 func_str = "ioc_init";
317 case MPI2_FUNCTION_PORT_ENABLE:
318 frame_sz = sizeof(Mpi2PortEnableRequest_t);
319 func_str = "port_enable";
321 case MPI2_FUNCTION_SMP_PASSTHROUGH:
322 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
323 func_str = "smp_passthru";
327 func_str = "unknown";
331 printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
332 " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
334 _debug_dump_mf(request_hdr, frame_sz/4);
338 * _base_display_event_data - verbose translation of firmware asyn events
339 * @ioc: pointer to scsi command object
340 * @mpi_reply: reply mf payload returned from firmware
345 _base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
346 Mpi2EventNotificationReply_t *mpi_reply)
351 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
354 event = le16_to_cpu(mpi_reply->Event);
357 case MPI2_EVENT_LOG_DATA:
360 case MPI2_EVENT_STATE_CHANGE:
361 desc = "Status Change";
363 case MPI2_EVENT_HARD_RESET_RECEIVED:
364 desc = "Hard Reset Received";
366 case MPI2_EVENT_EVENT_CHANGE:
367 desc = "Event Change";
369 case MPI2_EVENT_TASK_SET_FULL:
370 desc = "Task Set Full";
372 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
373 desc = "Device Status Change";
375 case MPI2_EVENT_IR_OPERATION_STATUS:
376 desc = "IR Operation Status";
378 case MPI2_EVENT_SAS_DISCOVERY:
381 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
382 desc = "SAS Broadcast Primitive";
384 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
385 desc = "SAS Init Device Status Change";
387 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
388 desc = "SAS Init Table Overflow";
390 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
391 desc = "SAS Topology Change List";
393 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
394 desc = "SAS Enclosure Device Status Change";
396 case MPI2_EVENT_IR_VOLUME:
399 case MPI2_EVENT_IR_PHYSICAL_DISK:
400 desc = "IR Physical Disk";
402 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
403 desc = "IR Configuration Change List";
405 case MPI2_EVENT_LOG_ENTRY_ADDED:
406 desc = "Log Entry Added";
413 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
418 * _base_sas_log_info - verbose translation of firmware log info
419 * @ioc: pointer to scsi command object
420 * @log_info: log info
425 _base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
436 union loginfo_type sas_loginfo;
437 char *originator_str = NULL;
439 sas_loginfo.loginfo = log_info;
440 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
443 /* each nexus loss loginfo */
444 if (log_info == 0x31170000)
447 /* eat the loginfos associated with task aborts */
448 if (ioc->ignore_loginfos && (log_info == 30050000 || log_info ==
449 0x31140000 || log_info == 0x31130000))
452 switch (sas_loginfo.dw.originator) {
454 originator_str = "IOP";
457 originator_str = "PL";
460 originator_str = "IR";
464 printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
465 "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
466 originator_str, sas_loginfo.dw.code,
467 sas_loginfo.dw.subcode);
471 * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
472 * @ioc: pointer to scsi command object
473 * @fault_code: fault code
478 mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
480 printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
481 ioc->name, fault_code);
485 * _base_display_reply_info -
486 * @ioc: pointer to scsi command object
487 * @smid: system request message index
488 * @VF_ID: virtual function id
489 * @reply: reply message frame(lower 32bit addr)
494 _base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID,
497 MPI2DefaultReply_t *mpi_reply;
500 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
501 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
502 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
503 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
504 (ioc->logging_level & MPT_DEBUG_REPLY)) {
505 _base_sas_ioc_info(ioc , mpi_reply,
506 mpt2sas_base_get_msg_frame(ioc, smid));
509 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
510 _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
514 * mpt2sas_base_done - base internal command completion routine
515 * @ioc: pointer to scsi command object
516 * @smid: system request message index
517 * @VF_ID: virtual function id
518 * @reply: reply message frame(lower 32bit addr)
523 mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 VF_ID, u32 reply)
525 MPI2DefaultReply_t *mpi_reply;
527 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
528 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
531 if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
534 ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
536 ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
537 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
539 ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
540 complete(&ioc->base_cmds.done);
544 * _base_async_event - main callback handler for firmware asyn events
545 * @ioc: pointer to scsi command object
546 * @VF_ID: virtual function id
547 * @reply: reply message frame(lower 32bit addr)
552 _base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, u32 reply)
554 Mpi2EventNotificationReply_t *mpi_reply;
555 Mpi2EventAckRequest_t *ack_request;
558 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
561 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
563 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
564 _base_display_event_data(ioc, mpi_reply);
566 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
568 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
570 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
571 ioc->name, __func__);
575 ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
576 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
577 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
578 ack_request->Event = mpi_reply->Event;
579 ack_request->EventContext = mpi_reply->EventContext;
580 ack_request->VF_ID = VF_ID;
581 mpt2sas_base_put_smid_default(ioc, smid, VF_ID);
585 /* scsih callback handler */
586 mpt2sas_scsih_event_callback(ioc, VF_ID, reply);
588 /* ctl callback handler */
589 mpt2sas_ctl_event_callback(ioc, VF_ID, reply);
593 * _base_mask_interrupts - disable interrupts
594 * @ioc: pointer to scsi command object
596 * Disabling ResetIRQ, Reply and Doorbell Interrupts
601 _base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
605 ioc->mask_interrupts = 1;
606 him_register = readl(&ioc->chip->HostInterruptMask);
607 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
608 writel(him_register, &ioc->chip->HostInterruptMask);
609 readl(&ioc->chip->HostInterruptMask);
613 * _base_unmask_interrupts - enable interrupts
614 * @ioc: pointer to scsi command object
616 * Enabling only Reply Interrupts
621 _base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
625 writel(0, &ioc->chip->HostInterruptStatus);
626 him_register = readl(&ioc->chip->HostInterruptMask);
627 him_register &= ~MPI2_HIM_RIM;
628 writel(him_register, &ioc->chip->HostInterruptMask);
629 ioc->mask_interrupts = 0;
633 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
634 * @irq: irq number (not used)
635 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
636 * @r: pt_regs pointer (not used)
638 * Return IRQ_HANDLE if processed, else IRQ_NONE.
641 _base_interrupt(int irq, void *bus_id)
643 union reply_descriptor {
650 union reply_descriptor rd;
651 u32 post_index, post_index_next, completed_cmds;
652 u8 request_desript_type;
658 struct MPT2SAS_ADAPTER *ioc = bus_id;
660 if (ioc->mask_interrupts)
663 post_index = ioc->reply_post_host_index;
664 request_desript_type = ioc->reply_post_free[post_index].
665 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
666 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
671 rd.word = ioc->reply_post_free[post_index].Words;
672 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
676 smid = le16_to_cpu(ioc->reply_post_free[post_index].
677 Default.DescriptorTypeDependent1);
678 VF_ID = ioc->reply_post_free[post_index].
680 if (request_desript_type ==
681 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
682 reply = le32_to_cpu(ioc->reply_post_free[post_index].
683 AddressReply.ReplyFrameAddress);
684 } else if (request_desript_type ==
685 MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
687 else if (request_desript_type ==
688 MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
691 cb_idx = ioc->scsi_lookup[smid - 1].cb_idx;
692 if (smid && cb_idx != 0xFF) {
693 mpt_callbacks[cb_idx](ioc, smid, VF_ID, reply);
695 _base_display_reply_info(ioc, smid, VF_ID,
697 mpt2sas_base_free_smid(ioc, smid);
700 _base_async_event(ioc, VF_ID, reply);
702 /* reply free queue handling */
704 ioc->reply_free_host_index =
705 (ioc->reply_free_host_index ==
706 (ioc->reply_free_queue_depth - 1)) ?
707 0 : ioc->reply_free_host_index + 1;
708 ioc->reply_free[ioc->reply_free_host_index] =
710 writel(ioc->reply_free_host_index,
711 &ioc->chip->ReplyFreeHostIndex);
716 post_index_next = (post_index == (ioc->reply_post_queue_depth -
717 1)) ? 0 : post_index + 1;
718 request_desript_type =
719 ioc->reply_post_free[post_index_next].Default.ReplyFlags
720 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
722 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
724 post_index = post_index_next;
732 /* reply post descriptor handling */
733 post_index_next = ioc->reply_post_host_index;
734 for (i = 0 ; i < completed_cmds; i++) {
735 post_index = post_index_next;
736 /* poison the reply post descriptor */
737 ioc->reply_post_free[post_index_next].Words = ULLONG_MAX;
738 post_index_next = (post_index ==
739 (ioc->reply_post_queue_depth - 1))
740 ? 0 : post_index + 1;
742 ioc->reply_post_host_index = post_index_next;
743 writel(post_index_next, &ioc->chip->ReplyPostHostIndex);
749 * mpt2sas_base_release_callback_handler - clear interupt callback handler
750 * @cb_idx: callback index
755 mpt2sas_base_release_callback_handler(u8 cb_idx)
757 mpt_callbacks[cb_idx] = NULL;
761 * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
762 * @cb_func: callback function
767 mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
771 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
772 if (mpt_callbacks[cb_idx] == NULL)
775 mpt_callbacks[cb_idx] = cb_func;
780 * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
785 mpt2sas_base_initialize_callback_handler(void)
789 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
790 mpt2sas_base_release_callback_handler(cb_idx);
794 * mpt2sas_base_build_zero_len_sge - build zero length sg entry
795 * @ioc: per adapter object
796 * @paddr: virtual address for SGE
798 * Create a zero length scatter gather entry to insure the IOCs hardware has
799 * something to use if the target device goes brain dead and tries
800 * to send data even when none is asked for.
805 mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
807 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
808 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
809 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
810 MPI2_SGE_FLAGS_SHIFT);
811 ioc->base_add_sg_single(paddr, flags_length, -1);
815 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
816 * @paddr: virtual address for SGE
817 * @flags_length: SGE flags and data transfer length
818 * @dma_addr: Physical address
823 _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
825 Mpi2SGESimple32_t *sgel = paddr;
827 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
828 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
829 sgel->FlagsLength = cpu_to_le32(flags_length);
830 sgel->Address = cpu_to_le32(dma_addr);
835 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
836 * @paddr: virtual address for SGE
837 * @flags_length: SGE flags and data transfer length
838 * @dma_addr: Physical address
843 _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
845 Mpi2SGESimple64_t *sgel = paddr;
847 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
848 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
849 sgel->FlagsLength = cpu_to_le32(flags_length);
850 sgel->Address = cpu_to_le64(dma_addr);
853 #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
856 * _base_config_dma_addressing - set dma addressing
857 * @ioc: per adapter object
858 * @pdev: PCI device struct
860 * Returns 0 for success, non-zero for failure.
863 _base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
868 if (sizeof(dma_addr_t) > 4) {
869 const uint64_t required_mask =
870 dma_get_required_mask(&pdev->dev);
871 if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
872 DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
874 ioc->base_add_sg_single = &_base_add_sg_single_64;
875 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
881 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
882 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
883 ioc->base_add_sg_single = &_base_add_sg_single_32;
884 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
891 printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
892 "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
898 * _base_save_msix_table - backup msix vector table
899 * @ioc: per adapter object
901 * This address an errata where diag reset clears out the table
904 _base_save_msix_table(struct MPT2SAS_ADAPTER *ioc)
908 if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
911 for (i = 0; i < ioc->msix_vector_count; i++)
912 ioc->msix_table_backup[i] = ioc->msix_table[i];
916 * _base_restore_msix_table - this restores the msix vector table
917 * @ioc: per adapter object
921 _base_restore_msix_table(struct MPT2SAS_ADAPTER *ioc)
925 if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
928 for (i = 0; i < ioc->msix_vector_count; i++)
929 ioc->msix_table[i] = ioc->msix_table_backup[i];
933 * _base_check_enable_msix - checks MSIX capabable.
934 * @ioc: per adapter object
936 * Check to see if card is capable of MSIX, and set number
937 * of avaliable msix vectors
940 _base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
944 u32 msix_table_offset;
946 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
948 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
949 "supported\n", ioc->name));
953 /* get msix vector count */
954 pci_read_config_word(ioc->pdev, base + 2, &message_control);
955 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
958 pci_read_config_dword(ioc->pdev, base + 4, &msix_table_offset);
959 msix_table_offset &= 0xFFFFFFF8;
960 ioc->msix_table = (u32 *)((void *)ioc->chip + msix_table_offset);
962 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
963 "vector_count(%d), table_offset(0x%08x), table(%p)\n", ioc->name,
964 ioc->msix_vector_count, msix_table_offset, ioc->msix_table));
969 * _base_disable_msix - disables msix
970 * @ioc: per adapter object
974 _base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
976 if (ioc->msix_enable) {
977 pci_disable_msix(ioc->pdev);
978 kfree(ioc->msix_table_backup);
979 ioc->msix_table_backup = NULL;
980 ioc->msix_enable = 0;
985 * _base_enable_msix - enables msix, failback to io_apic
986 * @ioc: per adapter object
990 _base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
992 struct msix_entry entries;
996 if (msix_disable == -1 || msix_disable == 0)
1002 if (_base_check_enable_msix(ioc) != 0)
1005 ioc->msix_table_backup = kcalloc(ioc->msix_vector_count,
1006 sizeof(u32), GFP_KERNEL);
1007 if (!ioc->msix_table_backup) {
1008 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
1009 "msix_table_backup failed!!!\n", ioc->name));
1013 memset(&entries, 0, sizeof(struct msix_entry));
1014 r = pci_enable_msix(ioc->pdev, &entries, 1);
1016 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
1017 "failed (r=%d) !!!\n", ioc->name, r));
1021 r = request_irq(entries.vector, _base_interrupt, IRQF_SHARED,
1024 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "unable to allocate "
1025 "interrupt %d !!!\n", ioc->name, entries.vector));
1026 pci_disable_msix(ioc->pdev);
1030 ioc->pci_irq = entries.vector;
1031 ioc->msix_enable = 1;
1034 /* failback to io_apic interrupt routing */
1037 r = request_irq(ioc->pdev->irq, _base_interrupt, IRQF_SHARED,
1040 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1041 ioc->name, ioc->pdev->irq);
1046 ioc->pci_irq = ioc->pdev->irq;
1054 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1055 * @ioc: per adapter object
1057 * Returns 0 for success, non-zero for failure.
1060 mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1062 struct pci_dev *pdev = ioc->pdev;
1067 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n",
1068 ioc->name, __func__));
1070 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1071 if (pci_enable_device_mem(pdev)) {
1072 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1073 "failed\n", ioc->name);
1078 if (pci_request_selected_regions(pdev, ioc->bars,
1079 MPT2SAS_DRIVER_NAME)) {
1080 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1081 "failed\n", ioc->name);
1086 pci_set_master(pdev);
1088 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1089 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1090 ioc->name, pci_name(pdev));
1095 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
1096 if (pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE_IO) {
1099 ioc->pio_chip = pci_resource_start(pdev, i);
1100 pio_sz = pci_resource_len(pdev, i);
1104 ioc->chip_phys = pci_resource_start(pdev, i);
1105 memap_sz = pci_resource_len(pdev, i);
1106 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1107 if (ioc->chip == NULL) {
1108 printk(MPT2SAS_ERR_FMT "unable to map adapter "
1109 "memory!\n", ioc->name);
1116 pci_set_drvdata(pdev, ioc->shost);
1117 _base_mask_interrupts(ioc);
1118 r = _base_enable_msix(ioc);
1122 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1123 ioc->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1124 "IO-APIC enabled"), ioc->pci_irq);
1125 printk(MPT2SAS_INFO_FMT "iomem(0x%lx), mapped(0x%p), size(%d)\n",
1126 ioc->name, ioc->chip_phys, ioc->chip, memap_sz);
1127 printk(MPT2SAS_INFO_FMT "ioport(0x%lx), size(%d)\n",
1128 ioc->name, ioc->pio_chip, pio_sz);
1137 pci_release_selected_regions(ioc->pdev, ioc->bars);
1138 pci_disable_device(pdev);
1139 pci_set_drvdata(pdev, NULL);
1144 * mpt2sas_base_get_msg_frame_dma - obtain request mf pointer phys addr
1145 * @ioc: per adapter object
1146 * @smid: system request message index(smid zero is invalid)
1148 * Returns phys pointer to message frame.
1151 mpt2sas_base_get_msg_frame_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1153 return ioc->request_dma + (smid * ioc->request_sz);
1157 * mpt2sas_base_get_msg_frame - obtain request mf pointer
1158 * @ioc: per adapter object
1159 * @smid: system request message index(smid zero is invalid)
1161 * Returns virt pointer to message frame.
1164 mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1166 return (void *)(ioc->request + (smid * ioc->request_sz));
1170 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1171 * @ioc: per adapter object
1172 * @smid: system request message index
1174 * Returns virt pointer to sense buffer.
1177 mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1179 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1183 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1184 * @ioc: per adapter object
1185 * @smid: system request message index
1187 * Returns phys pointer to sense buffer.
1190 mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1192 return ioc->sense_dma + ((smid - 1) * SCSI_SENSE_BUFFERSIZE);
1196 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1197 * @ioc: per adapter object
1198 * @phys_addr: lower 32 physical addr of the reply
1200 * Converts 32bit lower physical addr into a virt address.
1203 mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1207 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1211 * mpt2sas_base_get_smid - obtain a free smid
1212 * @ioc: per adapter object
1213 * @cb_idx: callback index
1215 * Returns smid (zero is invalid)
1218 mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1220 unsigned long flags;
1221 struct request_tracker *request;
1224 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1225 if (list_empty(&ioc->free_list)) {
1226 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1227 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1228 ioc->name, __func__);
1232 request = list_entry(ioc->free_list.next,
1233 struct request_tracker, tracker_list);
1234 request->cb_idx = cb_idx;
1235 smid = request->smid;
1236 list_del(&request->tracker_list);
1237 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1243 * mpt2sas_base_free_smid - put smid back on free_list
1244 * @ioc: per adapter object
1245 * @smid: system request message index
1250 mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1252 unsigned long flags;
1254 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1255 ioc->scsi_lookup[smid - 1].cb_idx = 0xFF;
1256 list_add_tail(&ioc->scsi_lookup[smid - 1].tracker_list,
1258 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1261 * See _wait_for_commands_to_complete() call with regards to this code.
1263 if (ioc->shost_recovery && ioc->pending_io_count) {
1264 if (ioc->pending_io_count == 1)
1265 wake_up(&ioc->reset_wq);
1266 ioc->pending_io_count--;
1271 * _base_writeq - 64 bit write to MMIO
1272 * @ioc: per adapter object
1274 * @addr: address in MMIO space
1275 * @writeq_lock: spin lock
1277 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1278 * care of 32 bit environment where its not quarenteed to send the entire word
1282 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1283 spinlock_t *writeq_lock)
1285 unsigned long flags;
1286 __u64 data_out = cpu_to_le64(b);
1288 spin_lock_irqsave(writeq_lock, flags);
1289 writel((u32)(data_out), addr);
1290 writel((u32)(data_out >> 32), (addr + 4));
1291 spin_unlock_irqrestore(writeq_lock, flags);
1294 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1295 spinlock_t *writeq_lock)
1297 writeq(cpu_to_le64(b), addr);
1302 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1303 * @ioc: per adapter object
1304 * @smid: system request message index
1305 * @vf_id: virtual function id
1306 * @handle: device handle
1311 mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 vf_id,
1314 Mpi2RequestDescriptorUnion_t descriptor;
1315 u64 *request = (u64 *)&descriptor;
1318 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1319 descriptor.SCSIIO.VF_ID = vf_id;
1320 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1321 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1322 descriptor.SCSIIO.LMID = 0;
1323 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1324 &ioc->scsi_lookup_lock);
1329 * mpt2sas_base_put_smid_hi_priority - send Task Managment request to firmware
1330 * @ioc: per adapter object
1331 * @smid: system request message index
1332 * @vf_id: virtual function id
1337 mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid,
1340 Mpi2RequestDescriptorUnion_t descriptor;
1341 u64 *request = (u64 *)&descriptor;
1343 descriptor.HighPriority.RequestFlags =
1344 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1345 descriptor.HighPriority.VF_ID = vf_id;
1346 descriptor.HighPriority.SMID = cpu_to_le16(smid);
1347 descriptor.HighPriority.LMID = 0;
1348 descriptor.HighPriority.Reserved1 = 0;
1349 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1350 &ioc->scsi_lookup_lock);
1354 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1355 * @ioc: per adapter object
1356 * @smid: system request message index
1357 * @vf_id: virtual function id
1362 mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 vf_id)
1364 Mpi2RequestDescriptorUnion_t descriptor;
1365 u64 *request = (u64 *)&descriptor;
1367 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1368 descriptor.Default.VF_ID = vf_id;
1369 descriptor.Default.SMID = cpu_to_le16(smid);
1370 descriptor.Default.LMID = 0;
1371 descriptor.Default.DescriptorTypeDependent = 0;
1372 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1373 &ioc->scsi_lookup_lock);
1377 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1378 * @ioc: per adapter object
1379 * @smid: system request message index
1380 * @vf_id: virtual function id
1381 * @io_index: value used to track the IO
1386 mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
1387 u8 vf_id, u16 io_index)
1389 Mpi2RequestDescriptorUnion_t descriptor;
1390 u64 *request = (u64 *)&descriptor;
1392 descriptor.SCSITarget.RequestFlags =
1393 MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
1394 descriptor.SCSITarget.VF_ID = vf_id;
1395 descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1396 descriptor.SCSITarget.LMID = 0;
1397 descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1398 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1399 &ioc->scsi_lookup_lock);
1403 * _base_display_dell_branding - Disply branding string
1404 * @ioc: per adapter object
1409 _base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1411 char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1413 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1416 memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1417 switch (ioc->pdev->subsystem_device) {
1418 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1419 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1420 MPT2SAS_DELL_BRANDING_SIZE - 1);
1422 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1423 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1424 MPT2SAS_DELL_BRANDING_SIZE - 1);
1426 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1427 strncpy(dell_branding,
1428 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1429 MPT2SAS_DELL_BRANDING_SIZE - 1);
1431 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1432 strncpy(dell_branding,
1433 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
1434 MPT2SAS_DELL_BRANDING_SIZE - 1);
1436 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
1437 strncpy(dell_branding,
1438 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
1439 MPT2SAS_DELL_BRANDING_SIZE - 1);
1441 case MPT2SAS_DELL_PERC_H200_SSDID:
1442 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
1443 MPT2SAS_DELL_BRANDING_SIZE - 1);
1445 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
1446 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
1447 MPT2SAS_DELL_BRANDING_SIZE - 1);
1450 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
1454 printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
1455 " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
1456 ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
1457 ioc->pdev->subsystem_device);
1461 * _base_display_ioc_capabilities - Disply IOC's capabilities.
1462 * @ioc: per adapter object
1467 _base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
1472 u32 iounit_pg1_flags;
1474 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
1475 strncpy(desc, ioc->manu_pg0.ChipName, 16);
1476 printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
1477 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
1479 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
1480 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
1481 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
1482 ioc->facts.FWVersion.Word & 0x000000FF,
1484 (ioc->bios_pg3.BiosVersion & 0xFF000000) >> 24,
1485 (ioc->bios_pg3.BiosVersion & 0x00FF0000) >> 16,
1486 (ioc->bios_pg3.BiosVersion & 0x0000FF00) >> 8,
1487 ioc->bios_pg3.BiosVersion & 0x000000FF);
1489 printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
1491 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
1492 printk("Initiator");
1496 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
1497 printk("%sTarget", i ? "," : "");
1501 _base_display_dell_branding(ioc);
1505 printk("Capabilities=(");
1507 if (ioc->facts.IOCCapabilities &
1508 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
1513 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
1514 printk("%sTLR", i ? "," : "");
1518 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
1519 printk("%sMulticast", i ? "," : "");
1523 if (ioc->facts.IOCCapabilities &
1524 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
1525 printk("%sBIDI Target", i ? "," : "");
1529 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
1530 printk("%sEEDP", i ? "," : "");
1534 if (ioc->facts.IOCCapabilities &
1535 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
1536 printk("%sSnapshot Buffer", i ? "," : "");
1540 if (ioc->facts.IOCCapabilities &
1541 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
1542 printk("%sDiag Trace Buffer", i ? "," : "");
1546 if (ioc->facts.IOCCapabilities &
1547 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
1548 printk("%sTask Set Full", i ? "," : "");
1552 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
1553 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
1554 printk("%sNCQ", i ? "," : "");
1562 * _base_static_config_pages - static start of day config pages
1563 * @ioc: per adapter object
1568 _base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
1570 Mpi2ConfigReply_t mpi_reply;
1571 u32 iounit_pg1_flags;
1573 mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
1574 mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
1575 mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
1576 mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
1577 mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
1578 mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
1579 _base_display_ioc_capabilities(ioc);
1582 * Enable task_set_full handling in iounit_pg1 when the
1583 * facts capabilities indicate that its supported.
1585 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
1586 if ((ioc->facts.IOCCapabilities &
1587 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
1589 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
1592 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
1593 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
1594 mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, ioc->iounit_pg1);
1598 * _base_release_memory_pools - release memory
1599 * @ioc: per adapter object
1601 * Free memory allocated from _base_allocate_memory_pools.
1606 _base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
1608 dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1612 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
1613 ioc->request, ioc->request_dma);
1614 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
1615 ": free\n", ioc->name, ioc->request));
1616 ioc->request = NULL;
1620 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
1621 if (ioc->sense_dma_pool)
1622 pci_pool_destroy(ioc->sense_dma_pool);
1623 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
1624 ": free\n", ioc->name, ioc->sense));
1629 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
1630 if (ioc->reply_dma_pool)
1631 pci_pool_destroy(ioc->reply_dma_pool);
1632 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
1633 ": free\n", ioc->name, ioc->reply));
1637 if (ioc->reply_free) {
1638 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
1639 ioc->reply_free_dma);
1640 if (ioc->reply_free_dma_pool)
1641 pci_pool_destroy(ioc->reply_free_dma_pool);
1642 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
1643 "(0x%p): free\n", ioc->name, ioc->reply_free));
1644 ioc->reply_free = NULL;
1647 if (ioc->reply_post_free) {
1648 pci_pool_free(ioc->reply_post_free_dma_pool,
1649 ioc->reply_post_free, ioc->reply_post_free_dma);
1650 if (ioc->reply_post_free_dma_pool)
1651 pci_pool_destroy(ioc->reply_post_free_dma_pool);
1652 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
1653 "reply_post_free_pool(0x%p): free\n", ioc->name,
1654 ioc->reply_post_free));
1655 ioc->reply_post_free = NULL;
1658 if (ioc->config_page) {
1659 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
1660 "config_page(0x%p): free\n", ioc->name,
1662 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
1663 ioc->config_page, ioc->config_page_dma);
1666 kfree(ioc->scsi_lookup);
1671 * _base_allocate_memory_pools - allocate start of day memory pools
1672 * @ioc: per adapter object
1673 * @sleep_flag: CAN_SLEEP or NO_SLEEP
1675 * Returns 0 success, anything else error
1678 _base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
1680 Mpi2IOCFactsReply_t *facts;
1681 u32 queue_size, queue_diff;
1682 u16 max_sge_elements;
1683 u16 num_of_reply_frames;
1684 u16 chains_needed_per_io;
1688 u16 max_request_credit;
1690 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1694 facts = &ioc->facts;
1696 /* command line tunables for max sgl entries */
1697 if (max_sgl_entries != -1) {
1698 ioc->shost->sg_tablesize = (max_sgl_entries <
1699 MPT2SAS_SG_DEPTH) ? max_sgl_entries :
1702 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
1705 /* command line tunables for max controller queue depth */
1706 if (max_queue_depth != -1) {
1707 max_request_credit = (max_queue_depth < facts->RequestCredit)
1708 ? max_queue_depth : facts->RequestCredit;
1710 max_request_credit = (facts->RequestCredit >
1711 MPT2SAS_MAX_REQUEST_QUEUE) ? MPT2SAS_MAX_REQUEST_QUEUE :
1712 facts->RequestCredit;
1714 ioc->request_depth = max_request_credit;
1716 /* request frame size */
1717 ioc->request_sz = facts->IOCRequestFrameSize * 4;
1719 /* reply frame size */
1720 ioc->reply_sz = facts->ReplyFrameSize * 4;
1724 /* calculate number of sg elements left over in the 1st frame */
1725 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
1726 sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
1727 ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
1729 /* now do the same for a chain buffer */
1730 max_sge_elements = ioc->request_sz - ioc->sge_size;
1731 ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
1733 ioc->chain_offset_value_for_main_message =
1734 ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
1735 (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
1738 * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
1740 chains_needed_per_io = ((ioc->shost->sg_tablesize -
1741 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
1743 if (chains_needed_per_io > facts->MaxChainDepth) {
1744 chains_needed_per_io = facts->MaxChainDepth;
1745 ioc->shost->sg_tablesize = min_t(u16,
1746 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
1747 * chains_needed_per_io), ioc->shost->sg_tablesize);
1749 ioc->chains_needed_per_io = chains_needed_per_io;
1751 /* reply free queue sizing - taking into account for events */
1752 num_of_reply_frames = ioc->request_depth + 32;
1754 /* number of replies frames can't be a multiple of 16 */
1755 /* decrease number of reply frames by 1 */
1756 if (!(num_of_reply_frames % 16))
1757 num_of_reply_frames--;
1759 /* calculate number of reply free queue entries
1760 * (must be multiple of 16)
1763 /* (we know reply_free_queue_depth is not a multiple of 16) */
1764 queue_size = num_of_reply_frames;
1765 queue_size += 16 - (queue_size % 16);
1766 ioc->reply_free_queue_depth = queue_size;
1768 /* reply descriptor post queue sizing */
1769 /* this size should be the number of request frames + number of reply
1773 queue_size = ioc->request_depth + num_of_reply_frames + 1;
1774 /* round up to 16 byte boundary */
1775 if (queue_size % 16)
1776 queue_size += 16 - (queue_size % 16);
1778 /* check against IOC maximum reply post queue depth */
1779 if (queue_size > facts->MaxReplyDescriptorPostQueueDepth) {
1780 queue_diff = queue_size -
1781 facts->MaxReplyDescriptorPostQueueDepth;
1783 /* round queue_diff up to multiple of 16 */
1784 if (queue_diff % 16)
1785 queue_diff += 16 - (queue_diff % 16);
1787 /* adjust request_depth, reply_free_queue_depth,
1790 ioc->request_depth -= queue_diff;
1791 ioc->reply_free_queue_depth -= queue_diff;
1792 queue_size -= queue_diff;
1794 ioc->reply_post_queue_depth = queue_size;
1796 /* max scsi host queue depth */
1797 ioc->shost->can_queue = ioc->request_depth - INTERNAL_CMDS_COUNT;
1798 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host queue: depth"
1799 "(%d)\n", ioc->name, ioc->shost->can_queue));
1801 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
1802 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
1803 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
1804 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
1805 ioc->chains_needed_per_io));
1807 /* contiguous pool for request and chains, 16 byte align, one extra "
1810 ioc->chain_depth = ioc->chains_needed_per_io * ioc->request_depth;
1811 sz = ((ioc->request_depth + 1 + ioc->chain_depth) * ioc->request_sz);
1813 ioc->request_dma_sz = sz;
1814 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
1815 if (!ioc->request) {
1816 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
1817 "failed: req_depth(%d), chains_per_io(%d), frame_sz(%d), "
1818 "total(%d kB)\n", ioc->name, ioc->request_depth,
1819 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
1820 if (ioc->request_depth < MPT2SAS_SAS_QUEUE_DEPTH)
1823 ioc->request_depth = max_request_credit - retry_sz;
1824 goto retry_allocation;
1828 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
1829 "succeed: req_depth(%d), chains_per_io(%d), frame_sz(%d), "
1830 "total(%d kb)\n", ioc->name, ioc->request_depth,
1831 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
1833 ioc->chain = ioc->request + ((ioc->request_depth + 1) *
1835 ioc->chain_dma = ioc->request_dma + ((ioc->request_depth + 1) *
1837 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
1838 "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
1839 ioc->request, ioc->request_depth, ioc->request_sz,
1840 ((ioc->request_depth + 1) * ioc->request_sz)/1024));
1841 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool(0x%p): depth"
1842 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->chain,
1843 ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
1844 ioc->request_sz))/1024));
1845 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
1846 ioc->name, (unsigned long long) ioc->request_dma));
1849 ioc->scsi_lookup = kcalloc(ioc->request_depth,
1850 sizeof(struct request_tracker), GFP_KERNEL);
1851 if (!ioc->scsi_lookup) {
1852 printk(MPT2SAS_ERR_FMT "scsi_lookup: kcalloc failed\n",
1857 /* initialize some bits */
1858 for (i = 0; i < ioc->request_depth; i++)
1859 ioc->scsi_lookup[i].smid = i + 1;
1861 /* sense buffers, 4 byte align */
1862 sz = ioc->request_depth * SCSI_SENSE_BUFFERSIZE;
1863 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
1865 if (!ioc->sense_dma_pool) {
1866 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
1870 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
1873 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
1877 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
1878 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
1879 "(%d kB)\n", ioc->name, ioc->sense, ioc->request_depth,
1880 SCSI_SENSE_BUFFERSIZE, sz/1024));
1881 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
1882 ioc->name, (unsigned long long)ioc->sense_dma));
1885 /* reply pool, 4 byte align */
1886 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
1887 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
1889 if (!ioc->reply_dma_pool) {
1890 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
1894 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
1897 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
1901 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
1902 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
1903 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
1904 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
1905 ioc->name, (unsigned long long)ioc->reply_dma));
1908 /* reply free queue, 16 byte align */
1909 sz = ioc->reply_free_queue_depth * 4;
1910 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
1911 ioc->pdev, sz, 16, 0);
1912 if (!ioc->reply_free_dma_pool) {
1913 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
1914 "failed\n", ioc->name);
1917 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
1918 &ioc->reply_free_dma);
1919 if (!ioc->reply_free) {
1920 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
1921 "failed\n", ioc->name);
1924 memset(ioc->reply_free, 0, sz);
1925 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
1926 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
1927 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
1928 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
1929 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
1932 /* reply post queue, 16 byte align */
1933 sz = ioc->reply_post_queue_depth * sizeof(Mpi2DefaultReplyDescriptor_t);
1934 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
1935 ioc->pdev, sz, 16, 0);
1936 if (!ioc->reply_post_free_dma_pool) {
1937 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
1938 "failed\n", ioc->name);
1941 ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
1942 GFP_KERNEL, &ioc->reply_post_free_dma);
1943 if (!ioc->reply_post_free) {
1944 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
1945 "failed\n", ioc->name);
1948 memset(ioc->reply_post_free, 0, sz);
1949 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
1950 "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
1951 ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
1953 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
1954 "(0x%llx)\n", ioc->name, (unsigned long long)
1955 ioc->reply_post_free_dma));
1958 ioc->config_page_sz = 512;
1959 ioc->config_page = pci_alloc_consistent(ioc->pdev,
1960 ioc->config_page_sz, &ioc->config_page_dma);
1961 if (!ioc->config_page) {
1962 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
1963 "failed\n", ioc->name);
1966 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
1967 "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
1968 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
1969 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
1970 total_sz += ioc->config_page_sz;
1972 printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
1973 ioc->name, total_sz/1024);
1974 printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
1975 "Max Controller Queue Depth(%d)\n",
1976 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
1977 printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
1978 ioc->name, ioc->shost->sg_tablesize);
1982 _base_release_memory_pools(ioc);
1988 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
1989 * @ioc: Pointer to MPT_ADAPTER structure
1990 * @cooked: Request raw or cooked IOC state
1992 * Returns all IOC Doorbell register bits if cooked==0, else just the
1993 * Doorbell bits in MPI_IOC_STATE_MASK.
1996 mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2000 s = readl(&ioc->chip->Doorbell);
2001 sc = s & MPI2_IOC_STATE_MASK;
2002 return cooked ? sc : s;
2006 * _base_wait_on_iocstate - waiting on a particular ioc state
2007 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2008 * @timeout: timeout in second
2009 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2011 * Returns 0 for success, non-zero for failure.
2014 _base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2021 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2023 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2024 if (current_state == ioc_state)
2026 if (count && current_state == MPI2_IOC_STATE_FAULT)
2028 if (sleep_flag == CAN_SLEEP)
2035 return current_state;
2039 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2040 * a write to the doorbell)
2041 * @ioc: per adapter object
2042 * @timeout: timeout in second
2043 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2045 * Returns 0 for success, non-zero for failure.
2047 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2050 _base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2057 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2059 int_status = readl(&ioc->chip->HostInterruptStatus);
2060 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2061 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2062 "successfull count(%d), timeout(%d)\n", ioc->name,
2063 __func__, count, timeout));
2066 if (sleep_flag == CAN_SLEEP)
2073 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2074 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2079 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2080 * @ioc: per adapter object
2081 * @timeout: timeout in second
2082 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2084 * Returns 0 for success, non-zero for failure.
2086 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2090 _base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2098 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2100 int_status = readl(&ioc->chip->HostInterruptStatus);
2101 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
2102 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2103 "successfull count(%d), timeout(%d)\n", ioc->name,
2104 __func__, count, timeout));
2106 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2107 doorbell = readl(&ioc->chip->Doorbell);
2108 if ((doorbell & MPI2_IOC_STATE_MASK) ==
2109 MPI2_IOC_STATE_FAULT) {
2110 mpt2sas_base_fault_info(ioc , doorbell);
2113 } else if (int_status == 0xFFFFFFFF)
2116 if (sleep_flag == CAN_SLEEP)
2124 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2125 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2130 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2131 * @ioc: per adapter object
2132 * @timeout: timeout in second
2133 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2135 * Returns 0 for success, non-zero for failure.
2139 _base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2146 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2148 doorbell_reg = readl(&ioc->chip->Doorbell);
2149 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
2150 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2151 "successfull count(%d), timeout(%d)\n", ioc->name,
2152 __func__, count, timeout));
2155 if (sleep_flag == CAN_SLEEP)
2162 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2163 "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2168 * _base_send_ioc_reset - send doorbell reset
2169 * @ioc: per adapter object
2170 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2171 * @timeout: timeout in second
2172 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2174 * Returns 0 for success, non-zero for failure.
2177 _base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2183 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2184 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2185 ioc->name, __func__);
2189 if (!(ioc->facts.IOCCapabilities &
2190 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2193 printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
2195 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
2196 &ioc->chip->Doorbell);
2197 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
2201 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
2202 timeout, sleep_flag);
2204 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
2205 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
2210 printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
2211 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
2216 * _base_handshake_req_reply_wait - send request thru doorbell interface
2217 * @ioc: per adapter object
2218 * @request_bytes: request length
2219 * @request: pointer having request payload
2220 * @reply_bytes: reply length
2221 * @reply: pointer to reply payload
2222 * @timeout: timeout in second
2223 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2225 * Returns 0 for success, non-zero for failure.
2228 _base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
2229 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
2231 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
2237 /* make sure doorbell is not in use */
2238 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
2239 printk(MPT2SAS_ERR_FMT "doorbell is in use "
2240 " (line=%d)\n", ioc->name, __LINE__);
2244 /* clear pending doorbell interrupts from previous state changes */
2245 if (readl(&ioc->chip->HostInterruptStatus) &
2246 MPI2_HIS_IOC2SYS_DB_STATUS)
2247 writel(0, &ioc->chip->HostInterruptStatus);
2249 /* send message to ioc */
2250 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
2251 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
2252 &ioc->chip->Doorbell);
2254 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2255 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2256 "int failed (line=%d)\n", ioc->name, __LINE__);
2259 writel(0, &ioc->chip->HostInterruptStatus);
2261 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
2262 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2263 "ack failed (line=%d)\n", ioc->name, __LINE__);
2267 /* send message 32-bits at a time */
2268 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
2269 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
2270 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
2275 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2276 "sending request failed (line=%d)\n", ioc->name, __LINE__);
2280 /* now wait for the reply */
2281 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
2282 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2283 "int failed (line=%d)\n", ioc->name, __LINE__);
2287 /* read the first two 16-bits, it gives the total length of the reply */
2288 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2289 & MPI2_DOORBELL_DATA_MASK);
2290 writel(0, &ioc->chip->HostInterruptStatus);
2291 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2292 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2293 "int failed (line=%d)\n", ioc->name, __LINE__);
2296 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2297 & MPI2_DOORBELL_DATA_MASK);
2298 writel(0, &ioc->chip->HostInterruptStatus);
2300 for (i = 2; i < default_reply->MsgLength * 2; i++) {
2301 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2302 printk(MPT2SAS_ERR_FMT "doorbell "
2303 "handshake int failed (line=%d)\n", ioc->name,
2307 if (i >= reply_bytes/2) /* overflow case */
2308 dummy = readl(&ioc->chip->Doorbell);
2310 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2311 & MPI2_DOORBELL_DATA_MASK);
2312 writel(0, &ioc->chip->HostInterruptStatus);
2315 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
2316 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
2317 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
2318 " (line=%d)\n", ioc->name, __LINE__));
2320 writel(0, &ioc->chip->HostInterruptStatus);
2322 if (ioc->logging_level & MPT_DEBUG_INIT) {
2324 printk(KERN_DEBUG "\toffset:data\n");
2325 for (i = 0; i < reply_bytes/4; i++)
2326 printk(KERN_DEBUG "\t[0x%02x]:%08x\n", i*4,
2327 le32_to_cpu(mfp[i]));
2333 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
2334 * @ioc: per adapter object
2335 * @mpi_reply: the reply payload from FW
2336 * @mpi_request: the request payload sent to FW
2338 * The SAS IO Unit Control Request message allows the host to perform low-level
2339 * operations, such as resets on the PHYs of the IO Unit, also allows the host
2340 * to obtain the IOC assigned device handles for a device if it has other
2341 * identifying information about the device, in addition allows the host to
2342 * remove IOC resources associated with the device.
2344 * Returns 0 for success, non-zero for failure.
2347 mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
2348 Mpi2SasIoUnitControlReply_t *mpi_reply,
2349 Mpi2SasIoUnitControlRequest_t *mpi_request)
2353 unsigned long timeleft;
2357 u16 wait_state_count;
2359 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2362 mutex_lock(&ioc->base_cmds.mutex);
2364 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
2365 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
2366 ioc->name, __func__);
2371 wait_state_count = 0;
2372 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2373 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2374 if (wait_state_count++ == 10) {
2375 printk(MPT2SAS_ERR_FMT
2376 "%s: failed due to ioc not operational\n",
2377 ioc->name, __func__);
2382 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2383 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2384 "operational state(count=%d)\n", ioc->name,
2385 __func__, wait_state_count);
2388 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2390 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2391 ioc->name, __func__);
2397 ioc->base_cmds.status = MPT2_CMD_PENDING;
2398 request = mpt2sas_base_get_msg_frame(ioc, smid);
2399 ioc->base_cmds.smid = smid;
2400 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
2401 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
2402 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
2403 ioc->ioc_link_reset_in_progress = 1;
2404 mpt2sas_base_put_smid_default(ioc, smid, mpi_request->VF_ID);
2405 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
2406 msecs_to_jiffies(10000));
2407 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
2408 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
2409 ioc->ioc_link_reset_in_progress)
2410 ioc->ioc_link_reset_in_progress = 0;
2411 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2412 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2413 ioc->name, __func__);
2414 _debug_dump_mf(mpi_request,
2415 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
2416 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
2418 goto issue_host_reset;
2420 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
2421 memcpy(mpi_reply, ioc->base_cmds.reply,
2422 sizeof(Mpi2SasIoUnitControlReply_t));
2424 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
2425 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2430 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2432 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2435 mutex_unlock(&ioc->base_cmds.mutex);
2441 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
2442 * @ioc: per adapter object
2443 * @mpi_reply: the reply payload from FW
2444 * @mpi_request: the request payload sent to FW
2446 * The SCSI Enclosure Processor request message causes the IOC to
2447 * communicate with SES devices to control LED status signals.
2449 * Returns 0 for success, non-zero for failure.
2452 mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
2453 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
2457 unsigned long timeleft;
2461 u16 wait_state_count;
2463 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2466 mutex_lock(&ioc->base_cmds.mutex);
2468 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
2469 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
2470 ioc->name, __func__);
2475 wait_state_count = 0;
2476 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2477 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2478 if (wait_state_count++ == 10) {
2479 printk(MPT2SAS_ERR_FMT
2480 "%s: failed due to ioc not operational\n",
2481 ioc->name, __func__);
2486 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2487 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2488 "operational state(count=%d)\n", ioc->name,
2489 __func__, wait_state_count);
2492 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2494 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2495 ioc->name, __func__);
2501 ioc->base_cmds.status = MPT2_CMD_PENDING;
2502 request = mpt2sas_base_get_msg_frame(ioc, smid);
2503 ioc->base_cmds.smid = smid;
2504 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
2505 mpt2sas_base_put_smid_default(ioc, smid, mpi_request->VF_ID);
2506 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
2507 msecs_to_jiffies(10000));
2508 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2509 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2510 ioc->name, __func__);
2511 _debug_dump_mf(mpi_request,
2512 sizeof(Mpi2SepRequest_t)/4);
2513 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
2515 goto issue_host_reset;
2517 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
2518 memcpy(mpi_reply, ioc->base_cmds.reply,
2519 sizeof(Mpi2SepReply_t));
2521 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
2522 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2527 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2529 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2532 mutex_unlock(&ioc->base_cmds.mutex);
2537 * _base_get_port_facts - obtain port facts reply and save in ioc
2538 * @ioc: per adapter object
2539 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2541 * Returns 0 for success, non-zero for failure.
2544 _base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
2546 Mpi2PortFactsRequest_t mpi_request;
2547 Mpi2PortFactsReply_t mpi_reply, *pfacts;
2548 int mpi_reply_sz, mpi_request_sz, r;
2550 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2553 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
2554 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
2555 memset(&mpi_request, 0, mpi_request_sz);
2556 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
2557 mpi_request.PortNumber = port;
2558 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
2559 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
2562 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
2563 ioc->name, __func__, r);
2567 pfacts = &ioc->pfacts[port];
2568 memset(pfacts, 0, sizeof(Mpi2PortFactsReply_t));
2569 pfacts->PortNumber = mpi_reply.PortNumber;
2570 pfacts->VP_ID = mpi_reply.VP_ID;
2571 pfacts->VF_ID = mpi_reply.VF_ID;
2572 pfacts->MaxPostedCmdBuffers =
2573 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
2579 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
2580 * @ioc: per adapter object
2581 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2583 * Returns 0 for success, non-zero for failure.
2586 _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2588 Mpi2IOCFactsRequest_t mpi_request;
2589 Mpi2IOCFactsReply_t mpi_reply, *facts;
2590 int mpi_reply_sz, mpi_request_sz, r;
2592 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2595 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
2596 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
2597 memset(&mpi_request, 0, mpi_request_sz);
2598 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
2599 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
2600 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
2603 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
2604 ioc->name, __func__, r);
2608 facts = &ioc->facts;
2609 memset(facts, 0, sizeof(Mpi2IOCFactsReply_t));
2610 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
2611 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
2612 facts->VP_ID = mpi_reply.VP_ID;
2613 facts->VF_ID = mpi_reply.VF_ID;
2614 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
2615 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
2616 facts->WhoInit = mpi_reply.WhoInit;
2617 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
2618 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
2619 facts->MaxReplyDescriptorPostQueueDepth =
2620 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
2621 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
2622 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
2623 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
2624 ioc->ir_firmware = 1;
2625 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
2626 facts->IOCRequestFrameSize =
2627 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
2628 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
2629 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
2630 ioc->shost->max_id = -1;
2631 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
2632 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
2633 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
2634 facts->HighPriorityCredit =
2635 le16_to_cpu(mpi_reply.HighPriorityCredit);
2636 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
2637 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
2639 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
2640 "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
2641 facts->MaxChainDepth));
2642 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
2643 "reply frame size(%d)\n", ioc->name,
2644 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
2649 * _base_send_ioc_init - send ioc_init to firmware
2650 * @ioc: per adapter object
2651 * @VF_ID: virtual function id
2652 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2654 * Returns 0 for success, non-zero for failure.
2657 _base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, int sleep_flag)
2659 Mpi2IOCInitRequest_t mpi_request;
2660 Mpi2IOCInitReply_t mpi_reply;
2663 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2666 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
2667 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
2668 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
2669 mpi_request.VF_ID = VF_ID;
2670 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
2671 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
2673 /* In MPI Revision I (0xA), the SystemReplyFrameSize(offset 0x18) was
2674 * removed and made reserved. For those with older firmware will need
2675 * this fix. It was decided that the Reply and Request frame sizes are
2678 if ((ioc->facts.HeaderVersion >> 8) < 0xA) {
2679 mpi_request.Reserved7 = cpu_to_le16(ioc->reply_sz);
2680 /* mpi_request.SystemReplyFrameSize =
2681 * cpu_to_le16(ioc->reply_sz);
2685 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
2686 mpi_request.ReplyDescriptorPostQueueDepth =
2687 cpu_to_le16(ioc->reply_post_queue_depth);
2688 mpi_request.ReplyFreeQueueDepth =
2689 cpu_to_le16(ioc->reply_free_queue_depth);
2691 #if BITS_PER_LONG > 32
2692 mpi_request.SenseBufferAddressHigh =
2693 cpu_to_le32(ioc->sense_dma >> 32);
2694 mpi_request.SystemReplyAddressHigh =
2695 cpu_to_le32(ioc->reply_dma >> 32);
2696 mpi_request.SystemRequestFrameBaseAddress =
2697 cpu_to_le64(ioc->request_dma);
2698 mpi_request.ReplyFreeQueueAddress =
2699 cpu_to_le64(ioc->reply_free_dma);
2700 mpi_request.ReplyDescriptorPostQueueAddress =
2701 cpu_to_le64(ioc->reply_post_free_dma);
2703 mpi_request.SystemRequestFrameBaseAddress =
2704 cpu_to_le32(ioc->request_dma);
2705 mpi_request.ReplyFreeQueueAddress =
2706 cpu_to_le32(ioc->reply_free_dma);
2707 mpi_request.ReplyDescriptorPostQueueAddress =
2708 cpu_to_le32(ioc->reply_post_free_dma);
2711 if (ioc->logging_level & MPT_DEBUG_INIT) {
2715 mfp = (u32 *)&mpi_request;
2716 printk(KERN_DEBUG "\toffset:data\n");
2717 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
2718 printk(KERN_DEBUG "\t[0x%02x]:%08x\n", i*4,
2719 le32_to_cpu(mfp[i]));
2722 r = _base_handshake_req_reply_wait(ioc,
2723 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
2724 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
2728 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
2729 ioc->name, __func__, r);
2733 if (mpi_reply.IOCStatus != MPI2_IOCSTATUS_SUCCESS ||
2734 mpi_reply.IOCLogInfo) {
2735 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
2743 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
2744 * @ioc: per adapter object
2745 * @VF_ID: virtual function id
2746 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2748 * Returns 0 for success, non-zero for failure.
2751 _base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, int sleep_flag)
2753 Mpi2PortEnableRequest_t *mpi_request;
2755 unsigned long timeleft;
2759 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
2761 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
2762 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
2763 ioc->name, __func__);
2767 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2769 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2770 ioc->name, __func__);
2774 ioc->base_cmds.status = MPT2_CMD_PENDING;
2775 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2776 ioc->base_cmds.smid = smid;
2777 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
2778 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
2779 mpi_request->VF_ID = VF_ID;
2781 mpt2sas_base_put_smid_default(ioc, smid, VF_ID);
2782 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
2784 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2785 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2786 ioc->name, __func__);
2787 _debug_dump_mf(mpi_request,
2788 sizeof(Mpi2PortEnableRequest_t)/4);
2789 if (ioc->base_cmds.status & MPT2_CMD_RESET)
2795 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: complete\n",
2796 ioc->name, __func__));
2798 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_OPERATIONAL,
2801 printk(MPT2SAS_ERR_FMT "%s: failed going to operational state "
2802 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
2806 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2807 printk(MPT2SAS_INFO_FMT "port enable: %s\n",
2808 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
2813 * _base_unmask_events - turn on notification for this event
2814 * @ioc: per adapter object
2815 * @event: firmware event
2817 * The mask is stored in ioc->event_masks.
2820 _base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
2827 desired_event = (1 << (event % 32));
2830 ioc->event_masks[0] &= ~desired_event;
2831 else if (event < 64)
2832 ioc->event_masks[1] &= ~desired_event;
2833 else if (event < 96)
2834 ioc->event_masks[2] &= ~desired_event;
2835 else if (event < 128)
2836 ioc->event_masks[3] &= ~desired_event;
2840 * _base_event_notification - send event notification
2841 * @ioc: per adapter object
2842 * @VF_ID: virtual function id
2843 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2845 * Returns 0 for success, non-zero for failure.
2848 _base_event_notification(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID, int sleep_flag)
2850 Mpi2EventNotificationRequest_t *mpi_request;
2851 unsigned long timeleft;
2856 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2859 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
2860 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
2861 ioc->name, __func__);
2865 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2867 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2868 ioc->name, __func__);
2871 ioc->base_cmds.status = MPT2_CMD_PENDING;
2872 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2873 ioc->base_cmds.smid = smid;
2874 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
2875 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
2876 mpi_request->VF_ID = VF_ID;
2877 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
2878 mpi_request->EventMasks[i] =
2879 le32_to_cpu(ioc->event_masks[i]);
2880 mpt2sas_base_put_smid_default(ioc, smid, VF_ID);
2881 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
2882 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2883 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2884 ioc->name, __func__);
2885 _debug_dump_mf(mpi_request,
2886 sizeof(Mpi2EventNotificationRequest_t)/4);
2887 if (ioc->base_cmds.status & MPT2_CMD_RESET)
2892 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: complete\n",
2893 ioc->name, __func__));
2894 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2899 * mpt2sas_base_validate_event_type - validating event types
2900 * @ioc: per adapter object
2901 * @event: firmware event
2903 * This will turn on firmware event notification when application
2904 * ask for that event. We don't mask events that are already enabled.
2907 mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
2910 u32 event_mask, desired_event;
2911 u8 send_update_to_fw;
2913 for (i = 0, send_update_to_fw = 0; i <
2914 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
2915 event_mask = ~event_type[i];
2917 for (j = 0; j < 32; j++) {
2918 if (!(event_mask & desired_event) &&
2919 (ioc->event_masks[i] & desired_event)) {
2920 ioc->event_masks[i] &= ~desired_event;
2921 send_update_to_fw = 1;
2923 desired_event = (desired_event << 1);
2927 if (!send_update_to_fw)
2930 mutex_lock(&ioc->base_cmds.mutex);
2931 _base_event_notification(ioc, 0, CAN_SLEEP);
2932 mutex_unlock(&ioc->base_cmds.mutex);
2936 * _base_diag_reset - the "big hammer" start of day reset
2937 * @ioc: per adapter object
2938 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2940 * Returns 0 for success, non-zero for failure.
2943 _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2945 u32 host_diagnostic;
2950 printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
2952 _base_save_msix_table(ioc);
2954 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "clear interrupts\n",
2956 writel(0, &ioc->chip->HostInterruptStatus);
2960 /* Write magic sequence to WriteSequence register
2961 * Loop until in diagnostic mode
2963 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "write magic "
2964 "sequence\n", ioc->name));
2965 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
2966 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
2967 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
2968 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
2969 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
2970 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
2971 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
2974 if (sleep_flag == CAN_SLEEP)
2982 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
2983 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "wrote magic "
2984 "sequence: count(%d), host_diagnostic(0x%08x)\n",
2985 ioc->name, count, host_diagnostic));
2987 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
2989 hcb_size = readl(&ioc->chip->HCBSize);
2991 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "diag reset: issued\n",
2993 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
2994 &ioc->chip->HostDiagnostic);
2996 /* don't access any registers for 50 milliseconds */
2999 /* 300 second max wait */
3000 for (count = 0; count < 3000000 ; count++) {
3002 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3004 if (host_diagnostic == 0xFFFFFFFF)
3006 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3010 if (sleep_flag == CAN_SLEEP)
3016 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3018 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "restart the adapter "
3019 "assuming the HCB Address points to good F/W\n",
3021 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3022 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3023 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3025 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT
3026 "re-enable the HCDW\n", ioc->name));
3027 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3028 &ioc->chip->HCBSize);
3031 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "restart the adapter\n",
3033 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3034 &ioc->chip->HostDiagnostic);
3036 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "disable writes to the "
3037 "diagnostic register\n", ioc->name));
3038 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3040 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "Wait for FW to go to the "
3041 "READY state\n", ioc->name));
3042 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3045 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3046 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3050 _base_restore_msix_table(ioc);
3051 printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3055 printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3060 * _base_make_ioc_ready - put controller in READY state
3061 * @ioc: per adapter object
3062 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3063 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3065 * Returns 0 for success, non-zero for failure.
3068 _base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3069 enum reset_type type)
3073 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3076 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
3077 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: ioc_state(0x%08x)\n",
3078 ioc->name, __func__, ioc_state));
3080 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
3083 if (ioc_state & MPI2_DOORBELL_USED) {
3084 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
3085 "active!\n", ioc->name));
3086 goto issue_diag_reset;
3089 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3090 mpt2sas_base_fault_info(ioc, ioc_state &
3091 MPI2_DOORBELL_DATA_MASK);
3092 goto issue_diag_reset;
3095 if (type == FORCE_BIG_HAMMER)
3096 goto issue_diag_reset;
3098 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
3099 if (!(_base_send_ioc_reset(ioc,
3100 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP)))
3104 return _base_diag_reset(ioc, CAN_SLEEP);
3108 * _base_make_ioc_operational - put controller in OPERATIONAL state
3109 * @ioc: per adapter object
3110 * @VF_ID: virtual function id
3111 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3113 * Returns 0 for success, non-zero for failure.
3116 _base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, u8 VF_ID,
3120 unsigned long flags;
3123 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3126 /* initialize the scsi lookup free list */
3127 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3128 INIT_LIST_HEAD(&ioc->free_list);
3129 for (i = 0; i < ioc->request_depth; i++) {
3130 ioc->scsi_lookup[i].cb_idx = 0xFF;
3131 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
3134 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3136 /* initialize Reply Free Queue */
3137 for (i = 0, reply_address = (u32)ioc->reply_dma ;
3138 i < ioc->reply_free_queue_depth ; i++, reply_address +=
3140 ioc->reply_free[i] = cpu_to_le32(reply_address);
3142 /* initialize Reply Post Free Queue */
3143 for (i = 0; i < ioc->reply_post_queue_depth; i++)
3144 ioc->reply_post_free[i].Words = ULLONG_MAX;
3146 r = _base_send_ioc_init(ioc, VF_ID, sleep_flag);
3150 /* initialize the index's */
3151 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
3152 ioc->reply_post_host_index = 0;
3153 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
3154 writel(0, &ioc->chip->ReplyPostHostIndex);
3156 _base_unmask_interrupts(ioc);
3157 r = _base_event_notification(ioc, VF_ID, sleep_flag);
3161 if (sleep_flag == CAN_SLEEP)
3162 _base_static_config_pages(ioc);
3164 r = _base_send_port_enable(ioc, VF_ID, sleep_flag);
3172 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
3173 * @ioc: per adapter object
3178 mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
3180 struct pci_dev *pdev = ioc->pdev;
3182 dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3185 _base_mask_interrupts(ioc);
3186 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
3188 synchronize_irq(pdev->irq);
3189 free_irq(ioc->pci_irq, ioc);
3191 _base_disable_msix(ioc);
3196 pci_release_selected_regions(ioc->pdev, ioc->bars);
3197 pci_disable_device(pdev);
3198 pci_set_drvdata(pdev, NULL);
3203 * mpt2sas_base_attach - attach controller instance
3204 * @ioc: per adapter object
3206 * Returns 0 for success, non-zero for failure.
3209 mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
3212 unsigned long flags;
3214 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3217 r = mpt2sas_base_map_resources(ioc);
3221 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
3223 goto out_free_resources;
3225 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
3227 goto out_free_resources;
3229 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
3231 goto out_free_resources;
3233 init_waitqueue_head(&ioc->reset_wq);
3235 /* base internal command bits */
3236 mutex_init(&ioc->base_cmds.mutex);
3237 init_completion(&ioc->base_cmds.done);
3238 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3239 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3241 /* transport internal command bits */
3242 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3243 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
3244 mutex_init(&ioc->transport_cmds.mutex);
3245 init_completion(&ioc->transport_cmds.done);
3247 /* task management internal command bits */
3248 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3249 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3250 mutex_init(&ioc->tm_cmds.mutex);
3251 init_completion(&ioc->tm_cmds.done);
3253 /* config page internal command bits */
3254 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3255 ioc->config_cmds.status = MPT2_CMD_NOT_USED;
3256 mutex_init(&ioc->config_cmds.mutex);
3257 init_completion(&ioc->config_cmds.done);
3259 /* ctl module internal command bits */
3260 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3261 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
3262 mutex_init(&ioc->ctl_cmds.mutex);
3263 init_completion(&ioc->ctl_cmds.done);
3265 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3266 ioc->event_masks[i] = -1;
3268 /* here we enable the events we care about */
3269 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
3270 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
3271 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
3272 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3273 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
3274 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
3275 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
3276 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
3277 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
3278 _base_unmask_events(ioc, MPI2_EVENT_TASK_SET_FULL);
3279 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
3281 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
3282 sizeof(Mpi2PortFactsReply_t), GFP_KERNEL);
3284 goto out_free_resources;
3286 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
3287 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
3289 goto out_free_resources;
3291 r = _base_make_ioc_operational(ioc, 0, CAN_SLEEP);
3293 goto out_free_resources;
3295 /* initialize fault polling */
3296 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
3297 snprintf(ioc->fault_reset_work_q_name,
3298 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
3299 ioc->fault_reset_work_q =
3300 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
3301 if (!ioc->fault_reset_work_q) {
3302 printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
3303 ioc->name, __func__, __LINE__);
3304 goto out_free_resources;
3306 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
3307 if (ioc->fault_reset_work_q)
3308 queue_delayed_work(ioc->fault_reset_work_q,
3309 &ioc->fault_reset_work,
3310 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
3311 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
3316 ioc->remove_host = 1;
3317 mpt2sas_base_free_resources(ioc);
3318 _base_release_memory_pools(ioc);
3319 kfree(ioc->tm_cmds.reply);
3320 kfree(ioc->transport_cmds.reply);
3321 kfree(ioc->config_cmds.reply);
3322 kfree(ioc->base_cmds.reply);
3323 kfree(ioc->ctl_cmds.reply);
3325 ioc->ctl_cmds.reply = NULL;
3326 ioc->base_cmds.reply = NULL;
3327 ioc->tm_cmds.reply = NULL;
3328 ioc->transport_cmds.reply = NULL;
3329 ioc->config_cmds.reply = NULL;
3336 * mpt2sas_base_detach - remove controller instance
3337 * @ioc: per adapter object
3342 mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
3344 unsigned long flags;
3345 struct workqueue_struct *wq;
3347 dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3350 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
3351 wq = ioc->fault_reset_work_q;
3352 ioc->fault_reset_work_q = NULL;
3353 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
3354 if (!cancel_delayed_work(&ioc->fault_reset_work))
3355 flush_workqueue(wq);
3356 destroy_workqueue(wq);
3358 mpt2sas_base_free_resources(ioc);
3359 _base_release_memory_pools(ioc);
3361 kfree(ioc->ctl_cmds.reply);
3362 kfree(ioc->base_cmds.reply);
3363 kfree(ioc->tm_cmds.reply);
3364 kfree(ioc->transport_cmds.reply);
3365 kfree(ioc->config_cmds.reply);
3369 * _base_reset_handler - reset callback handler (for base)
3370 * @ioc: per adapter object
3371 * @reset_phase: phase
3373 * The handler for doing any required cleanup or initialization.
3375 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
3376 * MPT2_IOC_DONE_RESET
3381 _base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
3383 switch (reset_phase) {
3384 case MPT2_IOC_PRE_RESET:
3385 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
3386 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
3388 case MPT2_IOC_AFTER_RESET:
3389 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
3390 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
3391 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
3392 ioc->transport_cmds.status |= MPT2_CMD_RESET;
3393 mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
3394 complete(&ioc->transport_cmds.done);
3396 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3397 ioc->base_cmds.status |= MPT2_CMD_RESET;
3398 mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
3399 complete(&ioc->base_cmds.done);
3401 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
3402 ioc->config_cmds.status |= MPT2_CMD_RESET;
3403 mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
3404 complete(&ioc->config_cmds.done);
3407 case MPT2_IOC_DONE_RESET:
3408 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
3409 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
3412 mpt2sas_scsih_reset_handler(ioc, reset_phase);
3413 mpt2sas_ctl_reset_handler(ioc, reset_phase);
3417 * _wait_for_commands_to_complete - reset controller
3418 * @ioc: Pointer to MPT_ADAPTER structure
3419 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3421 * This function waiting(3s) for all pending commands to complete
3422 * prior to putting controller in reset.
3425 _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3428 unsigned long flags;
3431 ioc->pending_io_count = 0;
3432 if (sleep_flag != CAN_SLEEP)
3435 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
3436 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
3439 /* pending command count */
3440 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3441 for (i = 0; i < ioc->request_depth; i++)
3442 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
3443 ioc->pending_io_count++;
3444 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3446 if (!ioc->pending_io_count)
3449 /* wait for pending commands to complete */
3450 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 3 * HZ);
3454 * mpt2sas_base_hard_reset_handler - reset controller
3455 * @ioc: Pointer to MPT_ADAPTER structure
3456 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3457 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3459 * Returns 0 for success, non-zero for failure.
3462 mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3463 enum reset_type type)
3466 unsigned long flags;
3468 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
3471 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
3472 if (ioc->ioc_reset_in_progress) {
3473 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
3474 printk(MPT2SAS_ERR_FMT "%s: busy\n",
3475 ioc->name, __func__);
3478 ioc->ioc_reset_in_progress = 1;
3479 ioc->shost_recovery = 1;
3480 if (ioc->shost->shost_state == SHOST_RUNNING) {
3481 /* set back to SHOST_RUNNING in mpt2sas_scsih.c */
3482 scsi_host_set_state(ioc->shost, SHOST_RECOVERY);
3483 printk(MPT2SAS_INFO_FMT "putting controller into "
3484 "SHOST_RECOVERY\n", ioc->name);
3486 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
3488 _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
3489 _wait_for_commands_to_complete(ioc, sleep_flag);
3490 _base_mask_interrupts(ioc);
3491 r = _base_make_ioc_ready(ioc, sleep_flag, type);
3494 _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
3495 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++)
3496 r = _base_make_ioc_operational(ioc, ioc->pfacts[i].VF_ID,
3499 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
3501 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: %s\n",
3502 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
3504 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
3505 ioc->ioc_reset_in_progress = 0;
3506 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);