enum sci_status sci_remote_node_context_resume(struct sci_remote_node_context *sci_rnc,
						    scics_sds_remote_node_context_callback cb_fn,
						    void *cb_p)
{
	enum scis_sds_remote_node_context_states state;

	state = sci_rnc->sm.current_state_id;
	switch (state) {
	case SCI_RNC_INITIAL:
		if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
			return SCI_FAILURE_INVALID_STATE;

		sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
		sci_remote_node_context_construct_buffer(sci_rnc);
		sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
		return SCI_SUCCESS;
	case SCI_RNC_POSTING:
	case SCI_RNC_INVALIDATING:
	case SCI_RNC_RESUMING:
		if (sci_rnc->destination_state != SCIC_SDS_REMOTE_NODE_DESTINATION_STATE_READY)
			return SCI_FAILURE_INVALID_STATE;

		sci_rnc->user_callback = cb_fn;
		sci_rnc->user_cookie   = cb_p;
		return SCI_SUCCESS;
	case SCI_RNC_TX_SUSPENDED: {
		struct isci_remote_device *idev = rnc_to_dev(sci_rnc);
		struct domain_device *dev = idev->domain_dev;

		sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);

		/* TODO: consider adding a resume action of NONE, INVALIDATE, WRITE_TLCR */
		if (dev->dev_type == SAS_END_DEV || dev_is_expander(dev))
			sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
		else if (dev->dev_type == SATA_DEV || (dev->tproto & SAS_PROTOCOL_STP)) {
			if (idev->is_direct_attached) {
				/* @todo Fix this since I am being silly in writing to the STPTLDARNI register. */
				sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
			} else {
				sci_change_state(&sci_rnc->sm, SCI_RNC_INVALIDATING);
			}
		} else
			return SCI_FAILURE;
		return SCI_SUCCESS;
	}
	case SCI_RNC_TX_RX_SUSPENDED:
		sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
		sci_change_state(&sci_rnc->sm, SCI_RNC_RESUMING);
		return SCI_FAILURE_INVALID_STATE;
	case SCI_RNC_AWAIT_SUSPENSION:
		sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn, cb_p);
		return SCI_SUCCESS;
	default:
		dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
			 "%s: invalid state %d\n", __func__, state);
		return SCI_FAILURE_INVALID_STATE;
	}
}
enum sci_status sci_remote_node_context_resume(struct sci_remote_node_context *sci_rnc,
						    scics_sds_remote_node_context_callback cb_fn,
						    void *cb_p)
{
	enum scis_sds_remote_node_context_states state;
	struct isci_remote_device *idev = rnc_to_dev(sci_rnc);

	state = sci_rnc->sm.current_state_id;
	dev_dbg(scirdev_to_dev(idev),
		"%s: state %s, cb_fn = %p, cb_p = %p; dest_state = %d; "
			"dev resume path %s\n",
		__func__, rnc_state_name(state), cb_fn, cb_p,
		sci_rnc->destination_state,
		test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)
			? "<abort active>" : "<normal>");

	switch (state) {
	case SCI_RNC_INITIAL:
		if (sci_rnc->remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
			return SCI_FAILURE_INVALID_STATE;

		sci_remote_node_context_setup_to_resume(sci_rnc, cb_fn,	cb_p,
							RNC_DEST_READY);
		if (!test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)) {
			sci_remote_node_context_construct_buffer(sci_rnc);
			sci_change_state(&sci_rnc->sm, SCI_RNC_POSTING);
		}
		return SCI_SUCCESS;

	case SCI_RNC_POSTING:
	case SCI_RNC_INVALIDATING:
	case SCI_RNC_RESUMING:
		/* We are still waiting to post when a resume was
		 * requested.
		 */
		switch (sci_rnc->destination_state) {
		case RNC_DEST_SUSPENDED:
		case RNC_DEST_SUSPENDED_RESUME:
			/* Previously waiting to suspend after posting.
			 * Now continue onto resumption.
			 */
			sci_remote_node_context_setup_to_resume(
				sci_rnc, cb_fn, cb_p,
				RNC_DEST_SUSPENDED_RESUME);
			break;
		default:
			sci_remote_node_context_setup_to_resume(
				sci_rnc, cb_fn, cb_p,
				RNC_DEST_READY);
			break;
		}
		return SCI_SUCCESS;

	case SCI_RNC_TX_SUSPENDED:
	case SCI_RNC_TX_RX_SUSPENDED:
		{
			struct domain_device *dev = idev->domain_dev;
			/* If this is an expander attached SATA device we must
			 * invalidate and repost the RNC since this is the only
			 * way to clear the TCi to NCQ tag mapping table for
			 * the RNi. All other device types we can just resume.
			 */
			sci_remote_node_context_setup_to_resume(
				sci_rnc, cb_fn, cb_p, RNC_DEST_READY);

			if (!test_bit(IDEV_ABORT_PATH_ACTIVE, &idev->flags)) {
				if ((dev_is_sata(dev) && dev->parent) ||
				    (sci_rnc->destination_state == RNC_DEST_FINAL))
					sci_change_state(&sci_rnc->sm,
							 SCI_RNC_INVALIDATING);
				else
					sci_change_state(&sci_rnc->sm,
							 SCI_RNC_RESUMING);
			}
		}
		return SCI_SUCCESS;

	case SCI_RNC_AWAIT_SUSPENSION:
		sci_remote_node_context_setup_to_resume(
			sci_rnc, cb_fn, cb_p, RNC_DEST_SUSPENDED_RESUME);
		return SCI_SUCCESS;
	default:
		dev_warn(scirdev_to_dev(rnc_to_dev(sci_rnc)),
			 "%s: invalid state %s\n", __func__,
			 rnc_state_name(state));
		return SCI_FAILURE_INVALID_STATE;
	}
}