/* session lock must be held and if not called for a task that is still pending or from the xmit thread, then xmit thread must be suspended.  */
static void fail_scsi_task(struct iscsi_task *task, int err)
/* session lock must be held and if not called for a task that is still pending or from the xmit thread, then xmit thread must be suspended.  */
static void fail_scsi_task(struct iscsi_task *task, int err)
{
	struct iscsi_conn *conn = task->conn;
	struct scsi_cmnd *sc;
	int state;
	sc = task->sc;
	if (!sc)
		return;
	if (task->state == ISCSI_TASK_PENDING) {
		conn->session->queued_cmdsn--;
		state = ISCSI_TASK_COMPLETED;
	} else if (err == DID_TRANSPORT_DISRUPTED)
		state = ISCSI_TASK_ABRT_SESS_RECOV;
	else
		state = ISCSI_TASK_ABRT_TMF;
	sc->result = err << 16;
	if (!scsi_bidi_cmnd(sc))
		scsi_set_resid(sc, scsi_bufflen(sc));
	else {
		scsi_out(sc)->resid = scsi_out(sc)->length;
		scsi_in(sc)->resid = scsi_in(sc)->length;
	}
	iscsi_complete_task(task, state);
}