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-2009 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 600 /* 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->shost_recovery)
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);
123 * mpt2sas_base_start_watchdog - start the fault_reset_work_q
124 * @ioc: pointer to scsi command object
130 mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
134 if (ioc->fault_reset_work_q)
137 /* initialize fault polling */
138 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
139 snprintf(ioc->fault_reset_work_q_name,
140 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
141 ioc->fault_reset_work_q =
142 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
143 if (!ioc->fault_reset_work_q) {
144 printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
145 ioc->name, __func__, __LINE__);
148 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
149 if (ioc->fault_reset_work_q)
150 queue_delayed_work(ioc->fault_reset_work_q,
151 &ioc->fault_reset_work,
152 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
153 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
157 * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
158 * @ioc: pointer to scsi command object
164 mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
167 struct workqueue_struct *wq;
169 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
170 wq = ioc->fault_reset_work_q;
171 ioc->fault_reset_work_q = NULL;
172 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
174 if (!cancel_delayed_work(&ioc->fault_reset_work))
176 destroy_workqueue(wq);
180 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
182 * _base_sas_ioc_info - verbose translation of the ioc status
183 * @ioc: pointer to scsi command object
184 * @mpi_reply: reply mf payload returned from firmware
185 * @request_hdr: request mf
190 _base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
191 MPI2RequestHeader_t *request_hdr)
193 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
197 char *func_str = NULL;
199 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
200 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
201 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
202 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
205 switch (ioc_status) {
207 /****************************************************************************
208 * Common IOCStatus values for all replies
209 ****************************************************************************/
211 case MPI2_IOCSTATUS_INVALID_FUNCTION:
212 desc = "invalid function";
214 case MPI2_IOCSTATUS_BUSY:
217 case MPI2_IOCSTATUS_INVALID_SGL:
218 desc = "invalid sgl";
220 case MPI2_IOCSTATUS_INTERNAL_ERROR:
221 desc = "internal error";
223 case MPI2_IOCSTATUS_INVALID_VPID:
224 desc = "invalid vpid";
226 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
227 desc = "insufficient resources";
229 case MPI2_IOCSTATUS_INVALID_FIELD:
230 desc = "invalid field";
232 case MPI2_IOCSTATUS_INVALID_STATE:
233 desc = "invalid state";
235 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
236 desc = "op state not supported";
239 /****************************************************************************
240 * Config IOCStatus values
241 ****************************************************************************/
243 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
244 desc = "config invalid action";
246 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
247 desc = "config invalid type";
249 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
250 desc = "config invalid page";
252 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
253 desc = "config invalid data";
255 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
256 desc = "config no defaults";
258 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
259 desc = "config cant commit";
262 /****************************************************************************
264 ****************************************************************************/
266 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
267 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
268 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
269 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
270 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
271 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
272 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
273 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
274 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
275 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
276 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
277 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
280 /****************************************************************************
281 * For use by SCSI Initiator and SCSI Target end-to-end data protection
282 ****************************************************************************/
284 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
285 desc = "eedp guard error";
287 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
288 desc = "eedp ref tag error";
290 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
291 desc = "eedp app tag error";
294 /****************************************************************************
296 ****************************************************************************/
298 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
299 desc = "target invalid io index";
301 case MPI2_IOCSTATUS_TARGET_ABORTED:
302 desc = "target aborted";
304 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
305 desc = "target no conn retryable";
307 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
308 desc = "target no connection";
310 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
311 desc = "target xfer count mismatch";
313 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
314 desc = "target data offset error";
316 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
317 desc = "target too much write data";
319 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
320 desc = "target iu too short";
322 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
323 desc = "target ack nak timeout";
325 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
326 desc = "target nak received";
329 /****************************************************************************
330 * Serial Attached SCSI values
331 ****************************************************************************/
333 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
334 desc = "smp request failed";
336 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
337 desc = "smp data overrun";
340 /****************************************************************************
341 * Diagnostic Buffer Post / Diagnostic Release values
342 ****************************************************************************/
344 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
345 desc = "diagnostic released";
354 switch (request_hdr->Function) {
355 case MPI2_FUNCTION_CONFIG:
356 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
357 func_str = "config_page";
359 case MPI2_FUNCTION_SCSI_TASK_MGMT:
360 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
361 func_str = "task_mgmt";
363 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
364 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
365 func_str = "sas_iounit_ctl";
367 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
368 frame_sz = sizeof(Mpi2SepRequest_t);
369 func_str = "enclosure";
371 case MPI2_FUNCTION_IOC_INIT:
372 frame_sz = sizeof(Mpi2IOCInitRequest_t);
373 func_str = "ioc_init";
375 case MPI2_FUNCTION_PORT_ENABLE:
376 frame_sz = sizeof(Mpi2PortEnableRequest_t);
377 func_str = "port_enable";
379 case MPI2_FUNCTION_SMP_PASSTHROUGH:
380 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
381 func_str = "smp_passthru";
385 func_str = "unknown";
389 printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
390 " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
392 _debug_dump_mf(request_hdr, frame_sz/4);
396 * _base_display_event_data - verbose translation of firmware asyn events
397 * @ioc: pointer to scsi command object
398 * @mpi_reply: reply mf payload returned from firmware
403 _base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
404 Mpi2EventNotificationReply_t *mpi_reply)
409 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
412 event = le16_to_cpu(mpi_reply->Event);
415 case MPI2_EVENT_LOG_DATA:
418 case MPI2_EVENT_STATE_CHANGE:
419 desc = "Status Change";
421 case MPI2_EVENT_HARD_RESET_RECEIVED:
422 desc = "Hard Reset Received";
424 case MPI2_EVENT_EVENT_CHANGE:
425 desc = "Event Change";
427 case MPI2_EVENT_TASK_SET_FULL:
428 desc = "Task Set Full";
430 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
431 desc = "Device Status Change";
433 case MPI2_EVENT_IR_OPERATION_STATUS:
434 desc = "IR Operation Status";
436 case MPI2_EVENT_SAS_DISCOVERY:
439 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
440 desc = "SAS Broadcast Primitive";
442 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
443 desc = "SAS Init Device Status Change";
445 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
446 desc = "SAS Init Table Overflow";
448 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
449 desc = "SAS Topology Change List";
451 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
452 desc = "SAS Enclosure Device Status Change";
454 case MPI2_EVENT_IR_VOLUME:
457 case MPI2_EVENT_IR_PHYSICAL_DISK:
458 desc = "IR Physical Disk";
460 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
461 desc = "IR Configuration Change List";
463 case MPI2_EVENT_LOG_ENTRY_ADDED:
464 desc = "Log Entry Added";
471 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
476 * _base_sas_log_info - verbose translation of firmware log info
477 * @ioc: pointer to scsi command object
478 * @log_info: log info
483 _base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
494 union loginfo_type sas_loginfo;
495 char *originator_str = NULL;
497 sas_loginfo.loginfo = log_info;
498 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
501 /* each nexus loss loginfo */
502 if (log_info == 0x31170000)
505 /* eat the loginfos associated with task aborts */
506 if (ioc->ignore_loginfos && (log_info == 30050000 || log_info ==
507 0x31140000 || log_info == 0x31130000))
510 switch (sas_loginfo.dw.originator) {
512 originator_str = "IOP";
515 originator_str = "PL";
518 originator_str = "IR";
522 printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
523 "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
524 originator_str, sas_loginfo.dw.code,
525 sas_loginfo.dw.subcode);
529 * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
530 * @ioc: pointer to scsi command object
531 * @fault_code: fault code
536 mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
538 printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
539 ioc->name, fault_code);
543 * _base_display_reply_info -
544 * @ioc: pointer to scsi command object
545 * @smid: system request message index
546 * @msix_index: MSIX table index supplied by the OS
547 * @reply: reply message frame(lower 32bit addr)
552 _base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
555 MPI2DefaultReply_t *mpi_reply;
558 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
559 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
560 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
561 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
562 (ioc->logging_level & MPT_DEBUG_REPLY)) {
563 _base_sas_ioc_info(ioc , mpi_reply,
564 mpt2sas_base_get_msg_frame(ioc, smid));
567 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
568 _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
572 * mpt2sas_base_done - base internal command completion routine
573 * @ioc: pointer to scsi command object
574 * @smid: system request message index
575 * @msix_index: MSIX table index supplied by the OS
576 * @reply: reply message frame(lower 32bit addr)
581 mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
584 MPI2DefaultReply_t *mpi_reply;
586 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
587 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
590 if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
593 ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
595 ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
596 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
598 ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
599 complete(&ioc->base_cmds.done);
603 * _base_async_event - main callback handler for firmware asyn events
604 * @ioc: pointer to scsi command object
605 * @msix_index: MSIX table index supplied by the OS
606 * @reply: reply message frame(lower 32bit addr)
611 _base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
613 Mpi2EventNotificationReply_t *mpi_reply;
614 Mpi2EventAckRequest_t *ack_request;
617 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
620 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
622 #ifdef CONFIG_SCSI_MPT2SAS_LOGGING
623 _base_display_event_data(ioc, mpi_reply);
625 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
627 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
629 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
630 ioc->name, __func__);
634 ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
635 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
636 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
637 ack_request->Event = mpi_reply->Event;
638 ack_request->EventContext = mpi_reply->EventContext;
639 ack_request->VF_ID = 0; /* TODO */
640 ack_request->VP_ID = 0;
641 mpt2sas_base_put_smid_default(ioc, smid);
645 /* scsih callback handler */
646 mpt2sas_scsih_event_callback(ioc, msix_index, reply);
648 /* ctl callback handler */
649 mpt2sas_ctl_event_callback(ioc, msix_index, reply);
653 * _base_get_cb_idx - obtain the callback index
654 * @ioc: per adapter object
655 * @smid: system request message index
657 * Return callback index.
660 _base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
665 if (smid >= ioc->hi_priority_smid) {
666 if (smid < ioc->internal_smid) {
667 i = smid - ioc->hi_priority_smid;
668 cb_idx = ioc->hpr_lookup[i].cb_idx;
670 i = smid - ioc->internal_smid;
671 cb_idx = ioc->internal_lookup[i].cb_idx;
675 cb_idx = ioc->scsi_lookup[i].cb_idx;
681 * _base_mask_interrupts - disable interrupts
682 * @ioc: pointer to scsi command object
684 * Disabling ResetIRQ, Reply and Doorbell Interrupts
689 _base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
693 ioc->mask_interrupts = 1;
694 him_register = readl(&ioc->chip->HostInterruptMask);
695 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
696 writel(him_register, &ioc->chip->HostInterruptMask);
697 readl(&ioc->chip->HostInterruptMask);
701 * _base_unmask_interrupts - enable interrupts
702 * @ioc: pointer to scsi command object
704 * Enabling only Reply Interrupts
709 _base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
713 writel(0, &ioc->chip->HostInterruptStatus);
714 him_register = readl(&ioc->chip->HostInterruptMask);
715 him_register &= ~MPI2_HIM_RIM;
716 writel(him_register, &ioc->chip->HostInterruptMask);
717 ioc->mask_interrupts = 0;
720 union reply_descriptor {
729 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
730 * @irq: irq number (not used)
731 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
732 * @r: pt_regs pointer (not used)
734 * Return IRQ_HANDLE if processed, else IRQ_NONE.
737 _base_interrupt(int irq, void *bus_id)
739 union reply_descriptor rd;
741 u8 request_desript_type;
746 struct MPT2SAS_ADAPTER *ioc = bus_id;
747 Mpi2ReplyDescriptorsUnion_t *rpf;
749 if (ioc->mask_interrupts)
752 rpf = &ioc->reply_post_free[ioc->reply_post_host_index];
753 request_desript_type = rpf->Default.ReplyFlags
754 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
755 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
760 rd.word = rpf->Words;
761 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
765 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
766 msix_index = rpf->Default.MSIxIndex;
767 if (request_desript_type ==
768 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
770 (rpf->AddressReply.ReplyFrameAddress);
771 } else if (request_desript_type ==
772 MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
774 else if (request_desript_type ==
775 MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
778 cb_idx = _base_get_cb_idx(ioc, smid);
779 if (smid && cb_idx != 0xFF) {
780 mpt_callbacks[cb_idx](ioc, smid, msix_index,
783 _base_display_reply_info(ioc, smid, msix_index,
785 mpt2sas_base_free_smid(ioc, smid);
788 _base_async_event(ioc, msix_index, reply);
790 /* reply free queue handling */
792 ioc->reply_free_host_index =
793 (ioc->reply_free_host_index ==
794 (ioc->reply_free_queue_depth - 1)) ?
795 0 : ioc->reply_free_host_index + 1;
796 ioc->reply_free[ioc->reply_free_host_index] =
799 writel(ioc->reply_free_host_index,
800 &ioc->chip->ReplyFreeHostIndex);
805 rpf->Words = ULLONG_MAX;
806 ioc->reply_post_host_index = (ioc->reply_post_host_index ==
807 (ioc->reply_post_queue_depth - 1)) ? 0 :
808 ioc->reply_post_host_index + 1;
809 request_desript_type =
810 ioc->reply_post_free[ioc->reply_post_host_index].Default.
811 ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
813 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
815 if (!ioc->reply_post_host_index)
816 rpf = ioc->reply_post_free;
827 writel(ioc->reply_post_host_index, &ioc->chip->ReplyPostHostIndex);
832 * mpt2sas_base_release_callback_handler - clear interupt callback handler
833 * @cb_idx: callback index
838 mpt2sas_base_release_callback_handler(u8 cb_idx)
840 mpt_callbacks[cb_idx] = NULL;
844 * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
845 * @cb_func: callback function
850 mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
854 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
855 if (mpt_callbacks[cb_idx] == NULL)
858 mpt_callbacks[cb_idx] = cb_func;
863 * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
868 mpt2sas_base_initialize_callback_handler(void)
872 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
873 mpt2sas_base_release_callback_handler(cb_idx);
877 * mpt2sas_base_build_zero_len_sge - build zero length sg entry
878 * @ioc: per adapter object
879 * @paddr: virtual address for SGE
881 * Create a zero length scatter gather entry to insure the IOCs hardware has
882 * something to use if the target device goes brain dead and tries
883 * to send data even when none is asked for.
888 mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
890 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
891 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
892 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
893 MPI2_SGE_FLAGS_SHIFT);
894 ioc->base_add_sg_single(paddr, flags_length, -1);
898 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
899 * @paddr: virtual address for SGE
900 * @flags_length: SGE flags and data transfer length
901 * @dma_addr: Physical address
906 _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
908 Mpi2SGESimple32_t *sgel = paddr;
910 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
911 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
912 sgel->FlagsLength = cpu_to_le32(flags_length);
913 sgel->Address = cpu_to_le32(dma_addr);
918 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
919 * @paddr: virtual address for SGE
920 * @flags_length: SGE flags and data transfer length
921 * @dma_addr: Physical address
926 _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
928 Mpi2SGESimple64_t *sgel = paddr;
930 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
931 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
932 sgel->FlagsLength = cpu_to_le32(flags_length);
933 sgel->Address = cpu_to_le64(dma_addr);
936 #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
939 * _base_config_dma_addressing - set dma addressing
940 * @ioc: per adapter object
941 * @pdev: PCI device struct
943 * Returns 0 for success, non-zero for failure.
946 _base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
951 if (sizeof(dma_addr_t) > 4) {
952 const uint64_t required_mask =
953 dma_get_required_mask(&pdev->dev);
954 if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
955 DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
957 ioc->base_add_sg_single = &_base_add_sg_single_64;
958 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
964 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
965 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
966 ioc->base_add_sg_single = &_base_add_sg_single_32;
967 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
974 printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
975 "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
981 * _base_save_msix_table - backup msix vector table
982 * @ioc: per adapter object
984 * This address an errata where diag reset clears out the table
987 _base_save_msix_table(struct MPT2SAS_ADAPTER *ioc)
991 if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
994 for (i = 0; i < ioc->msix_vector_count; i++)
995 ioc->msix_table_backup[i] = ioc->msix_table[i];
999 * _base_restore_msix_table - this restores the msix vector table
1000 * @ioc: per adapter object
1004 _base_restore_msix_table(struct MPT2SAS_ADAPTER *ioc)
1008 if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
1011 for (i = 0; i < ioc->msix_vector_count; i++)
1012 ioc->msix_table[i] = ioc->msix_table_backup[i];
1016 * _base_check_enable_msix - checks MSIX capabable.
1017 * @ioc: per adapter object
1019 * Check to see if card is capable of MSIX, and set number
1020 * of avaliable msix vectors
1023 _base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1026 u16 message_control;
1027 u32 msix_table_offset;
1029 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1031 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
1032 "supported\n", ioc->name));
1036 /* get msix vector count */
1037 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1038 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1040 /* get msix table */
1041 pci_read_config_dword(ioc->pdev, base + 4, &msix_table_offset);
1042 msix_table_offset &= 0xFFFFFFF8;
1043 ioc->msix_table = (u32 *)((void *)ioc->chip + msix_table_offset);
1045 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
1046 "vector_count(%d), table_offset(0x%08x), table(%p)\n", ioc->name,
1047 ioc->msix_vector_count, msix_table_offset, ioc->msix_table));
1052 * _base_disable_msix - disables msix
1053 * @ioc: per adapter object
1057 _base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
1059 if (ioc->msix_enable) {
1060 pci_disable_msix(ioc->pdev);
1061 kfree(ioc->msix_table_backup);
1062 ioc->msix_table_backup = NULL;
1063 ioc->msix_enable = 0;
1068 * _base_enable_msix - enables msix, failback to io_apic
1069 * @ioc: per adapter object
1073 _base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1075 struct msix_entry entries;
1079 if (msix_disable == -1 || msix_disable == 0)
1085 if (_base_check_enable_msix(ioc) != 0)
1088 ioc->msix_table_backup = kcalloc(ioc->msix_vector_count,
1089 sizeof(u32), GFP_KERNEL);
1090 if (!ioc->msix_table_backup) {
1091 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
1092 "msix_table_backup failed!!!\n", ioc->name));
1096 memset(&entries, 0, sizeof(struct msix_entry));
1097 r = pci_enable_msix(ioc->pdev, &entries, 1);
1099 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
1100 "failed (r=%d) !!!\n", ioc->name, r));
1104 r = request_irq(entries.vector, _base_interrupt, IRQF_SHARED,
1107 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "unable to allocate "
1108 "interrupt %d !!!\n", ioc->name, entries.vector));
1109 pci_disable_msix(ioc->pdev);
1113 ioc->pci_irq = entries.vector;
1114 ioc->msix_enable = 1;
1117 /* failback to io_apic interrupt routing */
1120 r = request_irq(ioc->pdev->irq, _base_interrupt, IRQF_SHARED,
1123 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1124 ioc->name, ioc->pdev->irq);
1129 ioc->pci_irq = ioc->pdev->irq;
1137 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1138 * @ioc: per adapter object
1140 * Returns 0 for success, non-zero for failure.
1143 mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1145 struct pci_dev *pdev = ioc->pdev;
1150 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n",
1151 ioc->name, __func__));
1153 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1154 if (pci_enable_device_mem(pdev)) {
1155 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1156 "failed\n", ioc->name);
1161 if (pci_request_selected_regions(pdev, ioc->bars,
1162 MPT2SAS_DRIVER_NAME)) {
1163 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1164 "failed\n", ioc->name);
1169 pci_set_master(pdev);
1171 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1172 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1173 ioc->name, pci_name(pdev));
1178 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
1179 if (pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE_IO) {
1182 ioc->pio_chip = pci_resource_start(pdev, i);
1183 pio_sz = pci_resource_len(pdev, i);
1187 ioc->chip_phys = pci_resource_start(pdev, i);
1188 memap_sz = pci_resource_len(pdev, i);
1189 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1190 if (ioc->chip == NULL) {
1191 printk(MPT2SAS_ERR_FMT "unable to map adapter "
1192 "memory!\n", ioc->name);
1199 _base_mask_interrupts(ioc);
1200 r = _base_enable_msix(ioc);
1204 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1205 ioc->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1206 "IO-APIC enabled"), ioc->pci_irq);
1207 printk(MPT2SAS_INFO_FMT "iomem(0x%lx), mapped(0x%p), size(%d)\n",
1208 ioc->name, ioc->chip_phys, ioc->chip, memap_sz);
1209 printk(MPT2SAS_INFO_FMT "ioport(0x%lx), size(%d)\n",
1210 ioc->name, ioc->pio_chip, pio_sz);
1219 pci_release_selected_regions(ioc->pdev, ioc->bars);
1220 pci_disable_device(pdev);
1225 * mpt2sas_base_get_msg_frame - obtain request mf pointer
1226 * @ioc: per adapter object
1227 * @smid: system request message index(smid zero is invalid)
1229 * Returns virt pointer to message frame.
1232 mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1234 return (void *)(ioc->request + (smid * ioc->request_sz));
1238 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1239 * @ioc: per adapter object
1240 * @smid: system request message index
1242 * Returns virt pointer to sense buffer.
1245 mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1247 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1251 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1252 * @ioc: per adapter object
1253 * @smid: system request message index
1255 * Returns phys pointer to sense buffer.
1258 mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1260 return ioc->sense_dma + ((smid - 1) * SCSI_SENSE_BUFFERSIZE);
1264 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1265 * @ioc: per adapter object
1266 * @phys_addr: lower 32 physical addr of the reply
1268 * Converts 32bit lower physical addr into a virt address.
1271 mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1275 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1279 * mpt2sas_base_get_smid - obtain a free smid from internal queue
1280 * @ioc: per adapter object
1281 * @cb_idx: callback index
1283 * Returns smid (zero is invalid)
1286 mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1288 unsigned long flags;
1289 struct request_tracker *request;
1292 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1293 if (list_empty(&ioc->internal_free_list)) {
1294 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1295 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1296 ioc->name, __func__);
1300 request = list_entry(ioc->internal_free_list.next,
1301 struct request_tracker, tracker_list);
1302 request->cb_idx = cb_idx;
1303 smid = request->smid;
1304 list_del(&request->tracker_list);
1305 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1310 * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1311 * @ioc: per adapter object
1312 * @cb_idx: callback index
1313 * @scmd: pointer to scsi command object
1315 * Returns smid (zero is invalid)
1318 mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
1319 struct scsi_cmnd *scmd)
1321 unsigned long flags;
1322 struct request_tracker *request;
1325 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1326 if (list_empty(&ioc->free_list)) {
1327 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1328 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1329 ioc->name, __func__);
1333 request = list_entry(ioc->free_list.next,
1334 struct request_tracker, tracker_list);
1335 request->scmd = scmd;
1336 request->cb_idx = cb_idx;
1337 smid = request->smid;
1338 list_del(&request->tracker_list);
1339 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1344 * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1345 * @ioc: per adapter object
1346 * @cb_idx: callback index
1348 * Returns smid (zero is invalid)
1351 mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1353 unsigned long flags;
1354 struct request_tracker *request;
1357 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1358 if (list_empty(&ioc->hpr_free_list)) {
1359 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1363 request = list_entry(ioc->hpr_free_list.next,
1364 struct request_tracker, tracker_list);
1365 request->cb_idx = cb_idx;
1366 smid = request->smid;
1367 list_del(&request->tracker_list);
1368 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1374 * mpt2sas_base_free_smid - put smid back on free_list
1375 * @ioc: per adapter object
1376 * @smid: system request message index
1381 mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1383 unsigned long flags;
1386 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1387 if (smid >= ioc->hi_priority_smid) {
1388 if (smid < ioc->internal_smid) {
1390 i = smid - ioc->hi_priority_smid;
1391 ioc->hpr_lookup[i].cb_idx = 0xFF;
1392 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
1393 &ioc->hpr_free_list);
1395 /* internal queue */
1396 i = smid - ioc->internal_smid;
1397 ioc->internal_lookup[i].cb_idx = 0xFF;
1398 list_add_tail(&ioc->internal_lookup[i].tracker_list,
1399 &ioc->internal_free_list);
1401 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1407 ioc->scsi_lookup[i].cb_idx = 0xFF;
1408 ioc->scsi_lookup[i].scmd = NULL;
1409 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
1411 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1414 * See _wait_for_commands_to_complete() call with regards to this code.
1416 if (ioc->shost_recovery && ioc->pending_io_count) {
1417 if (ioc->pending_io_count == 1)
1418 wake_up(&ioc->reset_wq);
1419 ioc->pending_io_count--;
1424 * _base_writeq - 64 bit write to MMIO
1425 * @ioc: per adapter object
1427 * @addr: address in MMIO space
1428 * @writeq_lock: spin lock
1430 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1431 * care of 32 bit environment where its not quarenteed to send the entire word
1435 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1436 spinlock_t *writeq_lock)
1438 unsigned long flags;
1439 __u64 data_out = cpu_to_le64(b);
1441 spin_lock_irqsave(writeq_lock, flags);
1442 writel((u32)(data_out), addr);
1443 writel((u32)(data_out >> 32), (addr + 4));
1444 spin_unlock_irqrestore(writeq_lock, flags);
1447 static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1448 spinlock_t *writeq_lock)
1450 writeq(cpu_to_le64(b), addr);
1455 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1456 * @ioc: per adapter object
1457 * @smid: system request message index
1458 * @handle: device handle
1463 mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
1465 Mpi2RequestDescriptorUnion_t descriptor;
1466 u64 *request = (u64 *)&descriptor;
1469 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1470 descriptor.SCSIIO.MSIxIndex = 0; /* TODO */
1471 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1472 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1473 descriptor.SCSIIO.LMID = 0;
1474 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1475 &ioc->scsi_lookup_lock);
1480 * mpt2sas_base_put_smid_hi_priority - send Task Managment request to firmware
1481 * @ioc: per adapter object
1482 * @smid: system request message index
1487 mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1489 Mpi2RequestDescriptorUnion_t descriptor;
1490 u64 *request = (u64 *)&descriptor;
1492 descriptor.HighPriority.RequestFlags =
1493 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1494 descriptor.HighPriority.MSIxIndex = 0; /* TODO */
1495 descriptor.HighPriority.SMID = cpu_to_le16(smid);
1496 descriptor.HighPriority.LMID = 0;
1497 descriptor.HighPriority.Reserved1 = 0;
1498 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1499 &ioc->scsi_lookup_lock);
1503 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1504 * @ioc: per adapter object
1505 * @smid: system request message index
1510 mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1512 Mpi2RequestDescriptorUnion_t descriptor;
1513 u64 *request = (u64 *)&descriptor;
1515 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1516 descriptor.Default.MSIxIndex = 0; /* TODO */
1517 descriptor.Default.SMID = cpu_to_le16(smid);
1518 descriptor.Default.LMID = 0;
1519 descriptor.Default.DescriptorTypeDependent = 0;
1520 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1521 &ioc->scsi_lookup_lock);
1525 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1526 * @ioc: per adapter object
1527 * @smid: system request message index
1528 * @io_index: value used to track the IO
1533 mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
1536 Mpi2RequestDescriptorUnion_t descriptor;
1537 u64 *request = (u64 *)&descriptor;
1539 descriptor.SCSITarget.RequestFlags =
1540 MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
1541 descriptor.SCSITarget.MSIxIndex = 0; /* TODO */
1542 descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1543 descriptor.SCSITarget.LMID = 0;
1544 descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1545 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1546 &ioc->scsi_lookup_lock);
1550 * _base_display_dell_branding - Disply branding string
1551 * @ioc: per adapter object
1556 _base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1558 char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1560 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1563 memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1564 switch (ioc->pdev->subsystem_device) {
1565 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1566 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1567 MPT2SAS_DELL_BRANDING_SIZE - 1);
1569 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1570 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1571 MPT2SAS_DELL_BRANDING_SIZE - 1);
1573 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1574 strncpy(dell_branding,
1575 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1576 MPT2SAS_DELL_BRANDING_SIZE - 1);
1578 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1579 strncpy(dell_branding,
1580 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
1581 MPT2SAS_DELL_BRANDING_SIZE - 1);
1583 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
1584 strncpy(dell_branding,
1585 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
1586 MPT2SAS_DELL_BRANDING_SIZE - 1);
1588 case MPT2SAS_DELL_PERC_H200_SSDID:
1589 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
1590 MPT2SAS_DELL_BRANDING_SIZE - 1);
1592 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
1593 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
1594 MPT2SAS_DELL_BRANDING_SIZE - 1);
1597 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
1601 printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
1602 " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
1603 ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
1604 ioc->pdev->subsystem_device);
1608 * _base_display_ioc_capabilities - Disply IOC's capabilities.
1609 * @ioc: per adapter object
1614 _base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
1619 u32 iounit_pg1_flags;
1621 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
1622 strncpy(desc, ioc->manu_pg0.ChipName, 16);
1623 printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
1624 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
1626 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
1627 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
1628 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
1629 ioc->facts.FWVersion.Word & 0x000000FF,
1631 (ioc->bios_pg3.BiosVersion & 0xFF000000) >> 24,
1632 (ioc->bios_pg3.BiosVersion & 0x00FF0000) >> 16,
1633 (ioc->bios_pg3.BiosVersion & 0x0000FF00) >> 8,
1634 ioc->bios_pg3.BiosVersion & 0x000000FF);
1636 _base_display_dell_branding(ioc);
1638 printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
1640 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
1641 printk("Initiator");
1645 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
1646 printk("%sTarget", i ? "," : "");
1652 printk("Capabilities=(");
1654 if (ioc->facts.IOCCapabilities &
1655 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
1660 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
1661 printk("%sTLR", i ? "," : "");
1665 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
1666 printk("%sMulticast", i ? "," : "");
1670 if (ioc->facts.IOCCapabilities &
1671 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
1672 printk("%sBIDI Target", i ? "," : "");
1676 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
1677 printk("%sEEDP", i ? "," : "");
1681 if (ioc->facts.IOCCapabilities &
1682 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
1683 printk("%sSnapshot Buffer", i ? "," : "");
1687 if (ioc->facts.IOCCapabilities &
1688 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
1689 printk("%sDiag Trace Buffer", i ? "," : "");
1693 if (ioc->facts.IOCCapabilities &
1694 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
1695 printk("%sTask Set Full", i ? "," : "");
1699 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
1700 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
1701 printk("%sNCQ", i ? "," : "");
1709 * _base_static_config_pages - static start of day config pages
1710 * @ioc: per adapter object
1715 _base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
1717 Mpi2ConfigReply_t mpi_reply;
1718 u32 iounit_pg1_flags;
1720 mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
1721 if (ioc->ir_firmware)
1722 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
1724 mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
1725 mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
1726 mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
1727 mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
1728 mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
1729 _base_display_ioc_capabilities(ioc);
1732 * Enable task_set_full handling in iounit_pg1 when the
1733 * facts capabilities indicate that its supported.
1735 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
1736 if ((ioc->facts.IOCCapabilities &
1737 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
1739 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
1742 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
1743 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
1744 mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
1748 * _base_release_memory_pools - release memory
1749 * @ioc: per adapter object
1751 * Free memory allocated from _base_allocate_memory_pools.
1756 _base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
1758 dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1762 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
1763 ioc->request, ioc->request_dma);
1764 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
1765 ": free\n", ioc->name, ioc->request));
1766 ioc->request = NULL;
1770 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
1771 if (ioc->sense_dma_pool)
1772 pci_pool_destroy(ioc->sense_dma_pool);
1773 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
1774 ": free\n", ioc->name, ioc->sense));
1779 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
1780 if (ioc->reply_dma_pool)
1781 pci_pool_destroy(ioc->reply_dma_pool);
1782 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
1783 ": free\n", ioc->name, ioc->reply));
1787 if (ioc->reply_free) {
1788 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
1789 ioc->reply_free_dma);
1790 if (ioc->reply_free_dma_pool)
1791 pci_pool_destroy(ioc->reply_free_dma_pool);
1792 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
1793 "(0x%p): free\n", ioc->name, ioc->reply_free));
1794 ioc->reply_free = NULL;
1797 if (ioc->reply_post_free) {
1798 pci_pool_free(ioc->reply_post_free_dma_pool,
1799 ioc->reply_post_free, ioc->reply_post_free_dma);
1800 if (ioc->reply_post_free_dma_pool)
1801 pci_pool_destroy(ioc->reply_post_free_dma_pool);
1802 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
1803 "reply_post_free_pool(0x%p): free\n", ioc->name,
1804 ioc->reply_post_free));
1805 ioc->reply_post_free = NULL;
1808 if (ioc->config_page) {
1809 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
1810 "config_page(0x%p): free\n", ioc->name,
1812 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
1813 ioc->config_page, ioc->config_page_dma);
1816 kfree(ioc->scsi_lookup);
1817 kfree(ioc->hpr_lookup);
1818 kfree(ioc->internal_lookup);
1823 * _base_allocate_memory_pools - allocate start of day memory pools
1824 * @ioc: per adapter object
1825 * @sleep_flag: CAN_SLEEP or NO_SLEEP
1827 * Returns 0 success, anything else error
1830 _base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
1832 Mpi2IOCFactsReply_t *facts;
1833 u32 queue_size, queue_diff;
1834 u16 max_sge_elements;
1835 u16 num_of_reply_frames;
1836 u16 chains_needed_per_io;
1839 u16 max_request_credit;
1841 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1845 facts = &ioc->facts;
1847 /* command line tunables for max sgl entries */
1848 if (max_sgl_entries != -1) {
1849 ioc->shost->sg_tablesize = (max_sgl_entries <
1850 MPT2SAS_SG_DEPTH) ? max_sgl_entries :
1853 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
1856 /* command line tunables for max controller queue depth */
1857 if (max_queue_depth != -1) {
1858 max_request_credit = (max_queue_depth < facts->RequestCredit)
1859 ? max_queue_depth : facts->RequestCredit;
1861 max_request_credit = (facts->RequestCredit >
1862 MPT2SAS_MAX_REQUEST_QUEUE) ? MPT2SAS_MAX_REQUEST_QUEUE :
1863 facts->RequestCredit;
1866 ioc->hba_queue_depth = max_request_credit;
1867 ioc->hi_priority_depth = facts->HighPriorityCredit;
1868 ioc->internal_depth = ioc->hi_priority_depth + 5;
1870 /* request frame size */
1871 ioc->request_sz = facts->IOCRequestFrameSize * 4;
1873 /* reply frame size */
1874 ioc->reply_sz = facts->ReplyFrameSize * 4;
1878 /* calculate number of sg elements left over in the 1st frame */
1879 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
1880 sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
1881 ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
1883 /* now do the same for a chain buffer */
1884 max_sge_elements = ioc->request_sz - ioc->sge_size;
1885 ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
1887 ioc->chain_offset_value_for_main_message =
1888 ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
1889 (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
1892 * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
1894 chains_needed_per_io = ((ioc->shost->sg_tablesize -
1895 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
1897 if (chains_needed_per_io > facts->MaxChainDepth) {
1898 chains_needed_per_io = facts->MaxChainDepth;
1899 ioc->shost->sg_tablesize = min_t(u16,
1900 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
1901 * chains_needed_per_io), ioc->shost->sg_tablesize);
1903 ioc->chains_needed_per_io = chains_needed_per_io;
1905 /* reply free queue sizing - taking into account for events */
1906 num_of_reply_frames = ioc->hba_queue_depth + 32;
1908 /* number of replies frames can't be a multiple of 16 */
1909 /* decrease number of reply frames by 1 */
1910 if (!(num_of_reply_frames % 16))
1911 num_of_reply_frames--;
1913 /* calculate number of reply free queue entries
1914 * (must be multiple of 16)
1917 /* (we know reply_free_queue_depth is not a multiple of 16) */
1918 queue_size = num_of_reply_frames;
1919 queue_size += 16 - (queue_size % 16);
1920 ioc->reply_free_queue_depth = queue_size;
1922 /* reply descriptor post queue sizing */
1923 /* this size should be the number of request frames + number of reply
1927 queue_size = ioc->hba_queue_depth + num_of_reply_frames + 1;
1928 /* round up to 16 byte boundary */
1929 if (queue_size % 16)
1930 queue_size += 16 - (queue_size % 16);
1932 /* check against IOC maximum reply post queue depth */
1933 if (queue_size > facts->MaxReplyDescriptorPostQueueDepth) {
1934 queue_diff = queue_size -
1935 facts->MaxReplyDescriptorPostQueueDepth;
1937 /* round queue_diff up to multiple of 16 */
1938 if (queue_diff % 16)
1939 queue_diff += 16 - (queue_diff % 16);
1941 /* adjust hba_queue_depth, reply_free_queue_depth,
1944 ioc->hba_queue_depth -= queue_diff;
1945 ioc->reply_free_queue_depth -= queue_diff;
1946 queue_size -= queue_diff;
1948 ioc->reply_post_queue_depth = queue_size;
1950 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
1951 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
1952 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
1953 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
1954 ioc->chains_needed_per_io));
1956 ioc->scsiio_depth = ioc->hba_queue_depth -
1957 ioc->hi_priority_depth - ioc->internal_depth;
1959 /* set the scsi host can_queue depth
1960 * with some internal commands that could be outstanding
1962 ioc->shost->can_queue = ioc->scsiio_depth - (2);
1963 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
1964 "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
1966 /* contiguous pool for request and chains, 16 byte align, one extra "
1969 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
1970 sz = ((ioc->scsiio_depth + 1 + ioc->chain_depth) * ioc->request_sz);
1972 /* hi-priority queue */
1973 sz += (ioc->hi_priority_depth * ioc->request_sz);
1975 /* internal queue */
1976 sz += (ioc->internal_depth * ioc->request_sz);
1978 ioc->request_dma_sz = sz;
1979 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
1980 if (!ioc->request) {
1981 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
1982 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
1983 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
1984 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
1985 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
1988 ioc->hba_queue_depth = max_request_credit - retry_sz;
1989 goto retry_allocation;
1993 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
1994 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
1995 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
1996 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
1999 /* hi-priority queue */
2000 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
2002 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
2005 /* internal queue */
2006 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2008 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2011 ioc->chain = ioc->internal + (ioc->internal_depth *
2013 ioc->chain_dma = ioc->internal_dma + (ioc->internal_depth *
2016 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2017 "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2018 ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2019 (ioc->hba_queue_depth * ioc->request_sz)/1024));
2020 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool(0x%p): depth"
2021 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->chain,
2022 ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2023 ioc->request_sz))/1024));
2024 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2025 ioc->name, (unsigned long long) ioc->request_dma));
2028 ioc->scsi_lookup = kcalloc(ioc->scsiio_depth,
2029 sizeof(struct request_tracker), GFP_KERNEL);
2030 if (!ioc->scsi_lookup) {
2031 printk(MPT2SAS_ERR_FMT "scsi_lookup: kcalloc failed\n",
2036 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2037 "depth(%d)\n", ioc->name, ioc->request,
2038 ioc->scsiio_depth));
2040 /* initialize hi-priority queue smid's */
2041 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2042 sizeof(struct request_tracker), GFP_KERNEL);
2043 if (!ioc->hpr_lookup) {
2044 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2048 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2049 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2050 "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2051 ioc->hi_priority_depth, ioc->hi_priority_smid));
2053 /* initialize internal queue smid's */
2054 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2055 sizeof(struct request_tracker), GFP_KERNEL);
2056 if (!ioc->internal_lookup) {
2057 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2061 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2062 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2063 "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2064 ioc->internal_depth, ioc->internal_smid));
2066 /* sense buffers, 4 byte align */
2067 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
2068 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2070 if (!ioc->sense_dma_pool) {
2071 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2075 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2078 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2082 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2083 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
2084 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
2085 SCSI_SENSE_BUFFERSIZE, sz/1024));
2086 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2087 ioc->name, (unsigned long long)ioc->sense_dma));
2090 /* reply pool, 4 byte align */
2091 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2092 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2094 if (!ioc->reply_dma_pool) {
2095 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2099 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2102 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2106 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2107 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2108 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2109 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2110 ioc->name, (unsigned long long)ioc->reply_dma));
2113 /* reply free queue, 16 byte align */
2114 sz = ioc->reply_free_queue_depth * 4;
2115 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2116 ioc->pdev, sz, 16, 0);
2117 if (!ioc->reply_free_dma_pool) {
2118 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2119 "failed\n", ioc->name);
2122 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2123 &ioc->reply_free_dma);
2124 if (!ioc->reply_free) {
2125 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2126 "failed\n", ioc->name);
2129 memset(ioc->reply_free, 0, sz);
2130 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2131 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2132 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2133 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2134 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2137 /* reply post queue, 16 byte align */
2138 sz = ioc->reply_post_queue_depth * sizeof(Mpi2DefaultReplyDescriptor_t);
2139 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2140 ioc->pdev, sz, 16, 0);
2141 if (!ioc->reply_post_free_dma_pool) {
2142 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
2143 "failed\n", ioc->name);
2146 ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2147 GFP_KERNEL, &ioc->reply_post_free_dma);
2148 if (!ioc->reply_post_free) {
2149 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
2150 "failed\n", ioc->name);
2153 memset(ioc->reply_post_free, 0, sz);
2154 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
2155 "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2156 ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2158 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
2159 "(0x%llx)\n", ioc->name, (unsigned long long)
2160 ioc->reply_post_free_dma));
2163 ioc->config_page_sz = 512;
2164 ioc->config_page = pci_alloc_consistent(ioc->pdev,
2165 ioc->config_page_sz, &ioc->config_page_dma);
2166 if (!ioc->config_page) {
2167 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2168 "failed\n", ioc->name);
2171 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2172 "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2173 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2174 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2175 total_sz += ioc->config_page_sz;
2177 printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2178 ioc->name, total_sz/1024);
2179 printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2180 "Max Controller Queue Depth(%d)\n",
2181 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2182 printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2183 ioc->name, ioc->shost->sg_tablesize);
2187 _base_release_memory_pools(ioc);
2193 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2194 * @ioc: Pointer to MPT_ADAPTER structure
2195 * @cooked: Request raw or cooked IOC state
2197 * Returns all IOC Doorbell register bits if cooked==0, else just the
2198 * Doorbell bits in MPI_IOC_STATE_MASK.
2201 mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2205 s = readl(&ioc->chip->Doorbell);
2206 sc = s & MPI2_IOC_STATE_MASK;
2207 return cooked ? sc : s;
2211 * _base_wait_on_iocstate - waiting on a particular ioc state
2212 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2213 * @timeout: timeout in second
2214 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2216 * Returns 0 for success, non-zero for failure.
2219 _base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2226 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2228 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2229 if (current_state == ioc_state)
2231 if (count && current_state == MPI2_IOC_STATE_FAULT)
2233 if (sleep_flag == CAN_SLEEP)
2240 return current_state;
2244 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2245 * a write to the doorbell)
2246 * @ioc: per adapter object
2247 * @timeout: timeout in second
2248 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2250 * Returns 0 for success, non-zero for failure.
2252 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2255 _base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2262 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2264 int_status = readl(&ioc->chip->HostInterruptStatus);
2265 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2266 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2267 "successfull count(%d), timeout(%d)\n", ioc->name,
2268 __func__, count, timeout));
2271 if (sleep_flag == CAN_SLEEP)
2278 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2279 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2284 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2285 * @ioc: per adapter object
2286 * @timeout: timeout in second
2287 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2289 * Returns 0 for success, non-zero for failure.
2291 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2295 _base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2303 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2305 int_status = readl(&ioc->chip->HostInterruptStatus);
2306 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
2307 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2308 "successfull count(%d), timeout(%d)\n", ioc->name,
2309 __func__, count, timeout));
2311 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2312 doorbell = readl(&ioc->chip->Doorbell);
2313 if ((doorbell & MPI2_IOC_STATE_MASK) ==
2314 MPI2_IOC_STATE_FAULT) {
2315 mpt2sas_base_fault_info(ioc , doorbell);
2318 } else if (int_status == 0xFFFFFFFF)
2321 if (sleep_flag == CAN_SLEEP)
2329 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2330 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2335 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2336 * @ioc: per adapter object
2337 * @timeout: timeout in second
2338 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2340 * Returns 0 for success, non-zero for failure.
2344 _base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2351 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2353 doorbell_reg = readl(&ioc->chip->Doorbell);
2354 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
2355 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2356 "successfull count(%d), timeout(%d)\n", ioc->name,
2357 __func__, count, timeout));
2360 if (sleep_flag == CAN_SLEEP)
2367 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2368 "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2373 * _base_send_ioc_reset - send doorbell reset
2374 * @ioc: per adapter object
2375 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2376 * @timeout: timeout in second
2377 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2379 * Returns 0 for success, non-zero for failure.
2382 _base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2388 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2389 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2390 ioc->name, __func__);
2394 if (!(ioc->facts.IOCCapabilities &
2395 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2398 printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
2400 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
2401 &ioc->chip->Doorbell);
2402 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
2406 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
2407 timeout, sleep_flag);
2409 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
2410 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
2415 printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
2416 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
2421 * _base_handshake_req_reply_wait - send request thru doorbell interface
2422 * @ioc: per adapter object
2423 * @request_bytes: request length
2424 * @request: pointer having request payload
2425 * @reply_bytes: reply length
2426 * @reply: pointer to reply payload
2427 * @timeout: timeout in second
2428 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2430 * Returns 0 for success, non-zero for failure.
2433 _base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
2434 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
2436 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
2442 /* make sure doorbell is not in use */
2443 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
2444 printk(MPT2SAS_ERR_FMT "doorbell is in use "
2445 " (line=%d)\n", ioc->name, __LINE__);
2449 /* clear pending doorbell interrupts from previous state changes */
2450 if (readl(&ioc->chip->HostInterruptStatus) &
2451 MPI2_HIS_IOC2SYS_DB_STATUS)
2452 writel(0, &ioc->chip->HostInterruptStatus);
2454 /* send message to ioc */
2455 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
2456 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
2457 &ioc->chip->Doorbell);
2459 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2460 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2461 "int failed (line=%d)\n", ioc->name, __LINE__);
2464 writel(0, &ioc->chip->HostInterruptStatus);
2466 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
2467 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2468 "ack failed (line=%d)\n", ioc->name, __LINE__);
2472 /* send message 32-bits at a time */
2473 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
2474 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
2475 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
2480 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2481 "sending request failed (line=%d)\n", ioc->name, __LINE__);
2485 /* now wait for the reply */
2486 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
2487 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2488 "int failed (line=%d)\n", ioc->name, __LINE__);
2492 /* read the first two 16-bits, it gives the total length of the reply */
2493 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2494 & MPI2_DOORBELL_DATA_MASK);
2495 writel(0, &ioc->chip->HostInterruptStatus);
2496 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2497 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2498 "int failed (line=%d)\n", ioc->name, __LINE__);
2501 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2502 & MPI2_DOORBELL_DATA_MASK);
2503 writel(0, &ioc->chip->HostInterruptStatus);
2505 for (i = 2; i < default_reply->MsgLength * 2; i++) {
2506 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2507 printk(MPT2SAS_ERR_FMT "doorbell "
2508 "handshake int failed (line=%d)\n", ioc->name,
2512 if (i >= reply_bytes/2) /* overflow case */
2513 dummy = readl(&ioc->chip->Doorbell);
2515 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2516 & MPI2_DOORBELL_DATA_MASK);
2517 writel(0, &ioc->chip->HostInterruptStatus);
2520 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
2521 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
2522 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
2523 " (line=%d)\n", ioc->name, __LINE__));
2525 writel(0, &ioc->chip->HostInterruptStatus);
2527 if (ioc->logging_level & MPT_DEBUG_INIT) {
2529 printk(KERN_DEBUG "\toffset:data\n");
2530 for (i = 0; i < reply_bytes/4; i++)
2531 printk(KERN_DEBUG "\t[0x%02x]:%08x\n", i*4,
2532 le32_to_cpu(mfp[i]));
2538 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
2539 * @ioc: per adapter object
2540 * @mpi_reply: the reply payload from FW
2541 * @mpi_request: the request payload sent to FW
2543 * The SAS IO Unit Control Request message allows the host to perform low-level
2544 * operations, such as resets on the PHYs of the IO Unit, also allows the host
2545 * to obtain the IOC assigned device handles for a device if it has other
2546 * identifying information about the device, in addition allows the host to
2547 * remove IOC resources associated with the device.
2549 * Returns 0 for success, non-zero for failure.
2552 mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
2553 Mpi2SasIoUnitControlReply_t *mpi_reply,
2554 Mpi2SasIoUnitControlRequest_t *mpi_request)
2558 unsigned long timeleft;
2562 u16 wait_state_count;
2564 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2567 mutex_lock(&ioc->base_cmds.mutex);
2569 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
2570 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
2571 ioc->name, __func__);
2576 wait_state_count = 0;
2577 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2578 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2579 if (wait_state_count++ == 10) {
2580 printk(MPT2SAS_ERR_FMT
2581 "%s: failed due to ioc not operational\n",
2582 ioc->name, __func__);
2587 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2588 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2589 "operational state(count=%d)\n", ioc->name,
2590 __func__, wait_state_count);
2593 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2595 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2596 ioc->name, __func__);
2602 ioc->base_cmds.status = MPT2_CMD_PENDING;
2603 request = mpt2sas_base_get_msg_frame(ioc, smid);
2604 ioc->base_cmds.smid = smid;
2605 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
2606 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
2607 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
2608 ioc->ioc_link_reset_in_progress = 1;
2609 mpt2sas_base_put_smid_default(ioc, smid);
2610 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
2611 msecs_to_jiffies(10000));
2612 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
2613 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
2614 ioc->ioc_link_reset_in_progress)
2615 ioc->ioc_link_reset_in_progress = 0;
2616 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2617 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2618 ioc->name, __func__);
2619 _debug_dump_mf(mpi_request,
2620 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
2621 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
2623 goto issue_host_reset;
2625 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
2626 memcpy(mpi_reply, ioc->base_cmds.reply,
2627 sizeof(Mpi2SasIoUnitControlReply_t));
2629 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
2630 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2635 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2637 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2640 mutex_unlock(&ioc->base_cmds.mutex);
2646 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
2647 * @ioc: per adapter object
2648 * @mpi_reply: the reply payload from FW
2649 * @mpi_request: the request payload sent to FW
2651 * The SCSI Enclosure Processor request message causes the IOC to
2652 * communicate with SES devices to control LED status signals.
2654 * Returns 0 for success, non-zero for failure.
2657 mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
2658 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
2662 unsigned long timeleft;
2666 u16 wait_state_count;
2668 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2671 mutex_lock(&ioc->base_cmds.mutex);
2673 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
2674 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
2675 ioc->name, __func__);
2680 wait_state_count = 0;
2681 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2682 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2683 if (wait_state_count++ == 10) {
2684 printk(MPT2SAS_ERR_FMT
2685 "%s: failed due to ioc not operational\n",
2686 ioc->name, __func__);
2691 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2692 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2693 "operational state(count=%d)\n", ioc->name,
2694 __func__, wait_state_count);
2697 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2699 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2700 ioc->name, __func__);
2706 ioc->base_cmds.status = MPT2_CMD_PENDING;
2707 request = mpt2sas_base_get_msg_frame(ioc, smid);
2708 ioc->base_cmds.smid = smid;
2709 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
2710 mpt2sas_base_put_smid_default(ioc, smid);
2711 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
2712 msecs_to_jiffies(10000));
2713 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2714 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2715 ioc->name, __func__);
2716 _debug_dump_mf(mpi_request,
2717 sizeof(Mpi2SepRequest_t)/4);
2718 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
2720 goto issue_host_reset;
2722 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
2723 memcpy(mpi_reply, ioc->base_cmds.reply,
2724 sizeof(Mpi2SepReply_t));
2726 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
2727 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2732 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2734 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2737 mutex_unlock(&ioc->base_cmds.mutex);
2742 * _base_get_port_facts - obtain port facts reply and save in ioc
2743 * @ioc: per adapter object
2744 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2746 * Returns 0 for success, non-zero for failure.
2749 _base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
2751 Mpi2PortFactsRequest_t mpi_request;
2752 Mpi2PortFactsReply_t mpi_reply, *pfacts;
2753 int mpi_reply_sz, mpi_request_sz, r;
2755 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2758 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
2759 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
2760 memset(&mpi_request, 0, mpi_request_sz);
2761 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
2762 mpi_request.PortNumber = port;
2763 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
2764 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
2767 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
2768 ioc->name, __func__, r);
2772 pfacts = &ioc->pfacts[port];
2773 memset(pfacts, 0, sizeof(Mpi2PortFactsReply_t));
2774 pfacts->PortNumber = mpi_reply.PortNumber;
2775 pfacts->VP_ID = mpi_reply.VP_ID;
2776 pfacts->VF_ID = mpi_reply.VF_ID;
2777 pfacts->MaxPostedCmdBuffers =
2778 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
2784 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
2785 * @ioc: per adapter object
2786 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2788 * Returns 0 for success, non-zero for failure.
2791 _base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2793 Mpi2IOCFactsRequest_t mpi_request;
2794 Mpi2IOCFactsReply_t mpi_reply, *facts;
2795 int mpi_reply_sz, mpi_request_sz, r;
2797 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2800 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
2801 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
2802 memset(&mpi_request, 0, mpi_request_sz);
2803 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
2804 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
2805 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
2808 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
2809 ioc->name, __func__, r);
2813 facts = &ioc->facts;
2814 memset(facts, 0, sizeof(Mpi2IOCFactsReply_t));
2815 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
2816 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
2817 facts->VP_ID = mpi_reply.VP_ID;
2818 facts->VF_ID = mpi_reply.VF_ID;
2819 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
2820 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
2821 facts->WhoInit = mpi_reply.WhoInit;
2822 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
2823 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
2824 facts->MaxReplyDescriptorPostQueueDepth =
2825 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
2826 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
2827 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
2828 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
2829 ioc->ir_firmware = 1;
2830 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
2831 facts->IOCRequestFrameSize =
2832 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
2833 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
2834 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
2835 ioc->shost->max_id = -1;
2836 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
2837 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
2838 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
2839 facts->HighPriorityCredit =
2840 le16_to_cpu(mpi_reply.HighPriorityCredit);
2841 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
2842 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
2844 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
2845 "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
2846 facts->MaxChainDepth));
2847 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
2848 "reply frame size(%d)\n", ioc->name,
2849 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
2854 * _base_send_ioc_init - send ioc_init to firmware
2855 * @ioc: per adapter object
2856 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2858 * Returns 0 for success, non-zero for failure.
2861 _base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2863 Mpi2IOCInitRequest_t mpi_request;
2864 Mpi2IOCInitReply_t mpi_reply;
2867 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2870 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
2871 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
2872 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
2873 mpi_request.VF_ID = 0; /* TODO */
2874 mpi_request.VP_ID = 0;
2875 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
2876 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
2878 /* In MPI Revision I (0xA), the SystemReplyFrameSize(offset 0x18) was
2879 * removed and made reserved. For those with older firmware will need
2880 * this fix. It was decided that the Reply and Request frame sizes are
2883 if ((ioc->facts.HeaderVersion >> 8) < 0xA) {
2884 mpi_request.Reserved7 = cpu_to_le16(ioc->reply_sz);
2885 /* mpi_request.SystemReplyFrameSize =
2886 * cpu_to_le16(ioc->reply_sz);
2890 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
2891 mpi_request.ReplyDescriptorPostQueueDepth =
2892 cpu_to_le16(ioc->reply_post_queue_depth);
2893 mpi_request.ReplyFreeQueueDepth =
2894 cpu_to_le16(ioc->reply_free_queue_depth);
2896 #if BITS_PER_LONG > 32
2897 mpi_request.SenseBufferAddressHigh =
2898 cpu_to_le32(ioc->sense_dma >> 32);
2899 mpi_request.SystemReplyAddressHigh =
2900 cpu_to_le32(ioc->reply_dma >> 32);
2901 mpi_request.SystemRequestFrameBaseAddress =
2902 cpu_to_le64(ioc->request_dma);
2903 mpi_request.ReplyFreeQueueAddress =
2904 cpu_to_le64(ioc->reply_free_dma);
2905 mpi_request.ReplyDescriptorPostQueueAddress =
2906 cpu_to_le64(ioc->reply_post_free_dma);
2908 mpi_request.SystemRequestFrameBaseAddress =
2909 cpu_to_le32(ioc->request_dma);
2910 mpi_request.ReplyFreeQueueAddress =
2911 cpu_to_le32(ioc->reply_free_dma);
2912 mpi_request.ReplyDescriptorPostQueueAddress =
2913 cpu_to_le32(ioc->reply_post_free_dma);
2916 if (ioc->logging_level & MPT_DEBUG_INIT) {
2920 mfp = (u32 *)&mpi_request;
2921 printk(KERN_DEBUG "\toffset:data\n");
2922 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
2923 printk(KERN_DEBUG "\t[0x%02x]:%08x\n", i*4,
2924 le32_to_cpu(mfp[i]));
2927 r = _base_handshake_req_reply_wait(ioc,
2928 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
2929 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
2933 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
2934 ioc->name, __func__, r);
2938 if (mpi_reply.IOCStatus != MPI2_IOCSTATUS_SUCCESS ||
2939 mpi_reply.IOCLogInfo) {
2940 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
2948 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
2949 * @ioc: per adapter object
2950 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2952 * Returns 0 for success, non-zero for failure.
2955 _base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2957 Mpi2PortEnableRequest_t *mpi_request;
2959 unsigned long timeleft;
2963 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
2965 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
2966 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
2967 ioc->name, __func__);
2971 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2973 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2974 ioc->name, __func__);
2978 ioc->base_cmds.status = MPT2_CMD_PENDING;
2979 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2980 ioc->base_cmds.smid = smid;
2981 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
2982 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
2983 mpi_request->VF_ID = 0; /* TODO */
2984 mpi_request->VP_ID = 0;
2986 mpt2sas_base_put_smid_default(ioc, smid);
2987 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
2989 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2990 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2991 ioc->name, __func__);
2992 _debug_dump_mf(mpi_request,
2993 sizeof(Mpi2PortEnableRequest_t)/4);
2994 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3000 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: complete\n",
3001 ioc->name, __func__));
3003 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_OPERATIONAL,
3006 printk(MPT2SAS_ERR_FMT "%s: failed going to operational state "
3007 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3011 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3012 printk(MPT2SAS_INFO_FMT "port enable: %s\n",
3013 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3018 * _base_unmask_events - turn on notification for this event
3019 * @ioc: per adapter object
3020 * @event: firmware event
3022 * The mask is stored in ioc->event_masks.
3025 _base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3032 desired_event = (1 << (event % 32));
3035 ioc->event_masks[0] &= ~desired_event;
3036 else if (event < 64)
3037 ioc->event_masks[1] &= ~desired_event;
3038 else if (event < 96)
3039 ioc->event_masks[2] &= ~desired_event;
3040 else if (event < 128)
3041 ioc->event_masks[3] &= ~desired_event;
3045 * _base_event_notification - send event notification
3046 * @ioc: per adapter object
3047 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3049 * Returns 0 for success, non-zero for failure.
3052 _base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3054 Mpi2EventNotificationRequest_t *mpi_request;
3055 unsigned long timeleft;
3060 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3063 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3064 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3065 ioc->name, __func__);
3069 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3071 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3072 ioc->name, __func__);
3075 ioc->base_cmds.status = MPT2_CMD_PENDING;
3076 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3077 ioc->base_cmds.smid = smid;
3078 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
3079 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3080 mpi_request->VF_ID = 0; /* TODO */
3081 mpi_request->VP_ID = 0;
3082 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3083 mpi_request->EventMasks[i] =
3084 le32_to_cpu(ioc->event_masks[i]);
3085 mpt2sas_base_put_smid_default(ioc, smid);
3086 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
3087 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3088 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3089 ioc->name, __func__);
3090 _debug_dump_mf(mpi_request,
3091 sizeof(Mpi2EventNotificationRequest_t)/4);
3092 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3097 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: complete\n",
3098 ioc->name, __func__));
3099 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3104 * mpt2sas_base_validate_event_type - validating event types
3105 * @ioc: per adapter object
3106 * @event: firmware event
3108 * This will turn on firmware event notification when application
3109 * ask for that event. We don't mask events that are already enabled.
3112 mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
3115 u32 event_mask, desired_event;
3116 u8 send_update_to_fw;
3118 for (i = 0, send_update_to_fw = 0; i <
3119 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
3120 event_mask = ~event_type[i];
3122 for (j = 0; j < 32; j++) {
3123 if (!(event_mask & desired_event) &&
3124 (ioc->event_masks[i] & desired_event)) {
3125 ioc->event_masks[i] &= ~desired_event;
3126 send_update_to_fw = 1;
3128 desired_event = (desired_event << 1);
3132 if (!send_update_to_fw)
3135 mutex_lock(&ioc->base_cmds.mutex);
3136 _base_event_notification(ioc, CAN_SLEEP);
3137 mutex_unlock(&ioc->base_cmds.mutex);
3141 * _base_diag_reset - the "big hammer" start of day reset
3142 * @ioc: per adapter object
3143 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3145 * Returns 0 for success, non-zero for failure.
3148 _base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3150 u32 host_diagnostic;
3155 printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
3157 _base_save_msix_table(ioc);
3159 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "clear interrupts\n",
3161 writel(0, &ioc->chip->HostInterruptStatus);
3165 /* Write magic sequence to WriteSequence register
3166 * Loop until in diagnostic mode
3168 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "write magic "
3169 "sequence\n", ioc->name));
3170 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3171 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
3172 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
3173 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
3174 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
3175 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
3176 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
3179 if (sleep_flag == CAN_SLEEP)
3187 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3188 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "wrote magic "
3189 "sequence: count(%d), host_diagnostic(0x%08x)\n",
3190 ioc->name, count, host_diagnostic));
3192 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
3194 hcb_size = readl(&ioc->chip->HCBSize);
3196 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "diag reset: issued\n",
3198 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
3199 &ioc->chip->HostDiagnostic);
3201 /* don't access any registers for 50 milliseconds */
3204 /* 300 second max wait */
3205 for (count = 0; count < 3000000 ; count++) {
3207 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3209 if (host_diagnostic == 0xFFFFFFFF)
3211 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3215 if (sleep_flag == CAN_SLEEP)
3221 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3223 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "restart the adapter "
3224 "assuming the HCB Address points to good F/W\n",
3226 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3227 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3228 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3230 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT
3231 "re-enable the HCDW\n", ioc->name));
3232 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3233 &ioc->chip->HCBSize);
3236 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "restart the adapter\n",
3238 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3239 &ioc->chip->HostDiagnostic);
3241 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "disable writes to the "
3242 "diagnostic register\n", ioc->name));
3243 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3245 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "Wait for FW to go to the "
3246 "READY state\n", ioc->name));
3247 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3250 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3251 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3255 _base_restore_msix_table(ioc);
3256 printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3260 printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3265 * _base_make_ioc_ready - put controller in READY state
3266 * @ioc: per adapter object
3267 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3268 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3270 * Returns 0 for success, non-zero for failure.
3273 _base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3274 enum reset_type type)
3278 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3281 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
3282 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: ioc_state(0x%08x)\n",
3283 ioc->name, __func__, ioc_state));
3285 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
3288 if (ioc_state & MPI2_DOORBELL_USED) {
3289 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
3290 "active!\n", ioc->name));
3291 goto issue_diag_reset;
3294 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3295 mpt2sas_base_fault_info(ioc, ioc_state &
3296 MPI2_DOORBELL_DATA_MASK);
3297 goto issue_diag_reset;
3300 if (type == FORCE_BIG_HAMMER)
3301 goto issue_diag_reset;
3303 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
3304 if (!(_base_send_ioc_reset(ioc,
3305 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP)))
3309 return _base_diag_reset(ioc, CAN_SLEEP);
3313 * _base_make_ioc_operational - put controller in OPERATIONAL state
3314 * @ioc: per adapter object
3315 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3317 * Returns 0 for success, non-zero for failure.
3320 _base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3323 unsigned long flags;
3327 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3330 /* initialize the scsi lookup free list */
3331 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3332 INIT_LIST_HEAD(&ioc->free_list);
3334 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
3335 ioc->scsi_lookup[i].cb_idx = 0xFF;
3336 ioc->scsi_lookup[i].smid = smid;
3337 ioc->scsi_lookup[i].scmd = NULL;
3338 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
3342 /* hi-priority queue */
3343 INIT_LIST_HEAD(&ioc->hpr_free_list);
3344 smid = ioc->hi_priority_smid;
3345 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
3346 ioc->hpr_lookup[i].cb_idx = 0xFF;
3347 ioc->hpr_lookup[i].smid = smid;
3348 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
3349 &ioc->hpr_free_list);
3352 /* internal queue */
3353 INIT_LIST_HEAD(&ioc->internal_free_list);
3354 smid = ioc->internal_smid;
3355 for (i = 0; i < ioc->internal_depth; i++, smid++) {
3356 ioc->internal_lookup[i].cb_idx = 0xFF;
3357 ioc->internal_lookup[i].smid = smid;
3358 list_add_tail(&ioc->internal_lookup[i].tracker_list,
3359 &ioc->internal_free_list);
3361 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3363 /* initialize Reply Free Queue */
3364 for (i = 0, reply_address = (u32)ioc->reply_dma ;
3365 i < ioc->reply_free_queue_depth ; i++, reply_address +=
3367 ioc->reply_free[i] = cpu_to_le32(reply_address);
3369 /* initialize Reply Post Free Queue */
3370 for (i = 0; i < ioc->reply_post_queue_depth; i++)
3371 ioc->reply_post_free[i].Words = ULLONG_MAX;
3373 r = _base_send_ioc_init(ioc, sleep_flag);
3377 /* initialize the index's */
3378 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
3379 ioc->reply_post_host_index = 0;
3380 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
3381 writel(0, &ioc->chip->ReplyPostHostIndex);
3383 _base_unmask_interrupts(ioc);
3384 r = _base_event_notification(ioc, sleep_flag);
3388 if (sleep_flag == CAN_SLEEP)
3389 _base_static_config_pages(ioc);
3391 r = _base_send_port_enable(ioc, sleep_flag);
3399 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
3400 * @ioc: per adapter object
3405 mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
3407 struct pci_dev *pdev = ioc->pdev;
3409 dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3412 _base_mask_interrupts(ioc);
3413 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
3415 synchronize_irq(pdev->irq);
3416 free_irq(ioc->pci_irq, ioc);
3418 _base_disable_msix(ioc);
3423 pci_release_selected_regions(ioc->pdev, ioc->bars);
3424 pci_disable_device(pdev);
3429 * mpt2sas_base_attach - attach controller instance
3430 * @ioc: per adapter object
3432 * Returns 0 for success, non-zero for failure.
3435 mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
3439 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3442 r = mpt2sas_base_map_resources(ioc);
3446 pci_set_drvdata(ioc->pdev, ioc->shost);
3447 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
3449 goto out_free_resources;
3451 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
3453 goto out_free_resources;
3455 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
3456 sizeof(Mpi2PortFactsReply_t), GFP_KERNEL);
3458 goto out_free_resources;
3460 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
3461 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
3463 goto out_free_resources;
3466 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
3468 goto out_free_resources;
3470 init_waitqueue_head(&ioc->reset_wq);
3472 /* base internal command bits */
3473 mutex_init(&ioc->base_cmds.mutex);
3474 init_completion(&ioc->base_cmds.done);
3475 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3476 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3478 /* transport internal command bits */
3479 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3480 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
3481 mutex_init(&ioc->transport_cmds.mutex);
3482 init_completion(&ioc->transport_cmds.done);
3484 /* task management internal command bits */
3485 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3486 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3487 mutex_init(&ioc->tm_cmds.mutex);
3489 /* config page internal command bits */
3490 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3491 ioc->config_cmds.status = MPT2_CMD_NOT_USED;
3492 mutex_init(&ioc->config_cmds.mutex);
3494 /* ctl module internal command bits */
3495 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3496 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
3497 mutex_init(&ioc->ctl_cmds.mutex);
3498 init_completion(&ioc->ctl_cmds.done);
3500 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3501 ioc->event_masks[i] = -1;
3503 /* here we enable the events we care about */
3504 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
3505 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
3506 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
3507 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3508 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
3509 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
3510 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
3511 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
3512 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
3513 _base_unmask_events(ioc, MPI2_EVENT_TASK_SET_FULL);
3514 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
3515 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
3517 goto out_free_resources;
3519 mpt2sas_base_start_watchdog(ioc);
3524 ioc->remove_host = 1;
3525 mpt2sas_base_free_resources(ioc);
3526 _base_release_memory_pools(ioc);
3527 pci_set_drvdata(ioc->pdev, NULL);
3528 kfree(ioc->tm_cmds.reply);
3529 kfree(ioc->transport_cmds.reply);
3530 kfree(ioc->config_cmds.reply);
3531 kfree(ioc->base_cmds.reply);
3532 kfree(ioc->ctl_cmds.reply);
3534 ioc->ctl_cmds.reply = NULL;
3535 ioc->base_cmds.reply = NULL;
3536 ioc->tm_cmds.reply = NULL;
3537 ioc->transport_cmds.reply = NULL;
3538 ioc->config_cmds.reply = NULL;
3545 * mpt2sas_base_detach - remove controller instance
3546 * @ioc: per adapter object
3551 mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
3554 dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3557 mpt2sas_base_stop_watchdog(ioc);
3558 mpt2sas_base_free_resources(ioc);
3559 _base_release_memory_pools(ioc);
3560 pci_set_drvdata(ioc->pdev, NULL);
3562 kfree(ioc->ctl_cmds.reply);
3563 kfree(ioc->base_cmds.reply);
3564 kfree(ioc->tm_cmds.reply);
3565 kfree(ioc->transport_cmds.reply);
3566 kfree(ioc->config_cmds.reply);
3570 * _base_reset_handler - reset callback handler (for base)
3571 * @ioc: per adapter object
3572 * @reset_phase: phase
3574 * The handler for doing any required cleanup or initialization.
3576 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
3577 * MPT2_IOC_DONE_RESET
3582 _base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
3584 switch (reset_phase) {
3585 case MPT2_IOC_PRE_RESET:
3586 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
3587 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
3589 case MPT2_IOC_AFTER_RESET:
3590 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
3591 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
3592 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
3593 ioc->transport_cmds.status |= MPT2_CMD_RESET;
3594 mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
3595 complete(&ioc->transport_cmds.done);
3597 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3598 ioc->base_cmds.status |= MPT2_CMD_RESET;
3599 mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
3600 complete(&ioc->base_cmds.done);
3602 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
3603 ioc->config_cmds.status |= MPT2_CMD_RESET;
3604 mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
3605 ioc->config_cmds.smid = USHORT_MAX;
3606 complete(&ioc->config_cmds.done);
3609 case MPT2_IOC_DONE_RESET:
3610 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
3611 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
3614 mpt2sas_scsih_reset_handler(ioc, reset_phase);
3615 mpt2sas_ctl_reset_handler(ioc, reset_phase);
3619 * _wait_for_commands_to_complete - reset controller
3620 * @ioc: Pointer to MPT_ADAPTER structure
3621 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3623 * This function waiting(3s) for all pending commands to complete
3624 * prior to putting controller in reset.
3627 _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3630 unsigned long flags;
3633 ioc->pending_io_count = 0;
3634 if (sleep_flag != CAN_SLEEP)
3637 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
3638 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
3641 /* pending command count */
3642 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3643 for (i = 0; i < ioc->scsiio_depth; i++)
3644 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
3645 ioc->pending_io_count++;
3646 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3648 if (!ioc->pending_io_count)
3651 /* wait for pending commands to complete */
3652 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 3 * HZ);
3656 * mpt2sas_base_hard_reset_handler - reset controller
3657 * @ioc: Pointer to MPT_ADAPTER structure
3658 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3659 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3661 * Returns 0 for success, non-zero for failure.
3664 mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3665 enum reset_type type)
3668 unsigned long flags;
3670 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
3673 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
3674 if (ioc->shost_recovery) {
3675 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
3676 printk(MPT2SAS_ERR_FMT "%s: busy\n",
3677 ioc->name, __func__);
3680 ioc->shost_recovery = 1;
3681 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
3683 _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
3684 _wait_for_commands_to_complete(ioc, sleep_flag);
3685 _base_mask_interrupts(ioc);
3686 r = _base_make_ioc_ready(ioc, sleep_flag, type);
3689 _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
3690 r = _base_make_ioc_operational(ioc, sleep_flag);
3692 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
3694 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: %s\n",
3695 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
3697 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
3698 ioc->shost_recovery = 0;
3699 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
3702 _base_reset_handler(ioc, MPT2_IOC_RUNNING);