/* This function waiting(3s) for all pending commands to complete prior to putting controller in reset.  */
static void _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
/* This function waiting(3s) for all pending commands to complete prior to putting controller in reset.  */
static void _wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
{
	u32 ioc_state;
	unsigned long flags;
	u16 i;
	ioc->pending_io_count = 0;
	if (sleep_flag != CAN_SLEEP)
		return;
	ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
	if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
		return;
	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	for (i = 0; i < ioc->scsiio_depth; i++)
		if (ioc->scsi_lookup[i].cb_idx != 0xFF)
			ioc->pending_io_count++;
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
	if (!ioc->pending_io_count)
		return;
	wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 3 * HZ);
}