[SCSI] libiscsi: pass opcode into alloc_pdu callout
[safe/jmp/linux-2.6] / drivers / scsi / libiscsi.c
index c1af2aa..ddf5397 100644 (file)
@@ -88,34 +88,47 @@ iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
 }
 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
 
-void iscsi_prep_unsolicit_data_pdu(struct iscsi_task *task,
-                                  struct iscsi_data *hdr)
+/**
+ * iscsi_prep_data_out_pdu - initialize Data-Out
+ * @task: scsi command task
+ * @r2t: R2T info
+ * @hdr: iscsi data in pdu
+ *
+ * Notes:
+ *     Initialize Data-Out within this R2T sequence and finds
+ *     proper data_offset within this SCSI command.
+ *
+ *     This function is called with connection lock taken.
+ **/
+void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t,
+                          struct iscsi_data *hdr)
 {
        struct iscsi_conn *conn = task->conn;
+       unsigned int left = r2t->data_length - r2t->sent;
+
+       task->hdr_len = sizeof(struct iscsi_data);
 
        memset(hdr, 0, sizeof(struct iscsi_data));
-       hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
-       hdr->datasn = cpu_to_be32(task->unsol_datasn);
-       task->unsol_datasn++;
+       hdr->ttt = r2t->ttt;
+       hdr->datasn = cpu_to_be32(r2t->datasn);
+       r2t->datasn++;
        hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
-       memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
-
-       hdr->itt = task->hdr->itt;
-       hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
-       hdr->offset = cpu_to_be32(task->unsol_offset);
-
-       if (task->unsol_count > conn->max_xmit_dlength) {
+       memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
+       hdr->itt = task->hdr_itt;
+       hdr->exp_statsn = r2t->exp_statsn;
+       hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent);
+       if (left > conn->max_xmit_dlength) {
                hton24(hdr->dlength, conn->max_xmit_dlength);
-               task->data_count = conn->max_xmit_dlength;
-               task->unsol_offset += task->data_count;
+               r2t->data_count = conn->max_xmit_dlength;
                hdr->flags = 0;
        } else {
-               hton24(hdr->dlength, task->unsol_count);
-               task->data_count = task->unsol_count;
+               hton24(hdr->dlength, left);
+               r2t->data_count = left;
                hdr->flags = ISCSI_FLAG_CMD_FINAL;
        }
+       conn->dataout_pdus_cnt++;
 }
-EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
+EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu);
 
 static int iscsi_add_hdr(struct iscsi_task *task, unsigned len)
 {
@@ -206,11 +219,24 @@ static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
 {
        struct iscsi_conn *conn = task->conn;
        struct iscsi_session *session = conn->session;
-       struct iscsi_cmd *hdr = task->hdr;
        struct scsi_cmnd *sc = task->sc;
+       struct iscsi_cmd *hdr;
        unsigned hdrlength, cmd_len;
+       itt_t itt;
        int rc;
 
+       rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD);
+       if (rc)
+               return rc;
+       hdr = (struct iscsi_cmd *) task->hdr;
+       itt = hdr->itt;
+       memset(hdr, 0, sizeof(*hdr));
+
+       if (session->tt->parse_pdu_itt)
+               hdr->itt = task->hdr_itt = itt;
+       else
+               hdr->itt = task->hdr_itt = build_itt(task->itt,
+                                                    task->conn->session->age);
        task->hdr_len = 0;
        rc = iscsi_add_hdr(task, sizeof(*hdr));
        if (rc)
@@ -218,8 +244,8 @@ static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
        hdr->opcode = ISCSI_OP_SCSI_CMD;
        hdr->flags = ISCSI_ATTR_SIMPLE;
        int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
-       hdr->itt = build_itt(task->itt, session->age);
-       hdr->cmdsn = cpu_to_be32(session->cmdsn);
+       memcpy(task->lun, hdr->lun, sizeof(task->lun));
+       hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn);
        session->cmdsn++;
        hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
        cmd_len = sc->cmd_len;
@@ -242,6 +268,8 @@ static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
        }
        if (sc->sc_data_direction == DMA_TO_DEVICE) {
                unsigned out_len = scsi_out(sc)->length;
+               struct iscsi_r2t_info *r2t = &task->unsol_r2t;
+
                hdr->data_length = cpu_to_be32(out_len);
                hdr->flags |= ISCSI_FLAG_CMD_WRITE;
                /*
@@ -254,13 +282,11 @@ static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
                 *                      without R2T ack right after
                 *                      immediate data
                 *
-                *      r2t_data_count  bytes to be sent via R2T ack's
+                *      r2t data_length bytes to be sent via R2T ack's
                 *
                 *      pad_count       bytes to be sent as zero-padding
                 */
-               task->unsol_count = 0;
-               task->unsol_offset = 0;
-               task->unsol_datasn = 0;
+               memset(r2t, 0, sizeof(*r2t));
 
                if (session->imm_data_en) {
                        if (out_len >= session->first_burst)
@@ -274,12 +300,14 @@ static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
                        zero_data(hdr->dlength);
 
                if (!session->initial_r2t_en) {
-                       task->unsol_count = min(session->first_burst, out_len)
-                                                            - task->imm_count;
-                       task->unsol_offset = task->imm_count;
+                       r2t->data_length = min(session->first_burst, out_len) -
+                                              task->imm_count;
+                       r2t->data_offset = task->imm_count;
+                       r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
+                       r2t->exp_statsn = cpu_to_be32(conn->exp_statsn);
                }
 
-               if (!task->unsol_count)
+               if (!task->unsol_r2t.data_length)
                        /* No unsolicit Data-Out's */
                        hdr->flags |= ISCSI_FLAG_CMD_FINAL;
        } else {
@@ -300,8 +328,7 @@ static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task)
        WARN_ON(hdrlength >= 256);
        hdr->hlength = hdrlength & 0xFF;
 
-       if (conn->session->tt->init_task &&
-           conn->session->tt->init_task(task))
+       if (session->tt->init_task && session->tt->init_task(task))
                return -EIO;
 
        task->state = ISCSI_TASK_RUNNING;
@@ -332,6 +359,7 @@ static void iscsi_complete_command(struct iscsi_task *task)
        struct iscsi_session *session = conn->session;
        struct scsi_cmnd *sc = task->sc;
 
+       session->tt->cleanup_task(task);
        list_del_init(&task->running);
        task->state = ISCSI_TASK_COMPLETED;
        task->sc = NULL;
@@ -362,10 +390,11 @@ static void iscsi_complete_command(struct iscsi_task *task)
        }
 }
 
-static void __iscsi_get_task(struct iscsi_task *task)
+void __iscsi_get_task(struct iscsi_task *task)
 {
        atomic_inc(&task->refcount);
 }
+EXPORT_SYMBOL_GPL(__iscsi_get_task);
 
 static void __iscsi_put_task(struct iscsi_task *task)
 {
@@ -401,11 +430,8 @@ static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
                 * the cmd in the sequencing
                 */
                conn->session->queued_cmdsn--;
-       else
-               conn->session->tt->cleanup_task(conn, task);
 
        sc->result = err;
-
        if (!scsi_bidi_cmnd(sc))
                scsi_set_resid(sc, scsi_bufflen(sc));
        else {
@@ -423,7 +449,7 @@ static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
                                struct iscsi_task *task)
 {
        struct iscsi_session *session = conn->session;
-       struct iscsi_hdr *hdr = (struct iscsi_hdr *)task->hdr;
+       struct iscsi_hdr *hdr = task->hdr;
        struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
 
        if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
@@ -437,7 +463,6 @@ static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
         */
        nop->cmdsn = cpu_to_be32(session->cmdsn);
        if (hdr->itt != RESERVED_ITT) {
-               hdr->itt = build_itt(task->itt, session->age);
                /*
                 * TODO: We always use immediate, so we never hit this.
                 * If we start to send tmfs or nops as non-immediate then
@@ -456,6 +481,7 @@ static int iscsi_prep_mgmt_task(struct iscsi_conn *conn,
        if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
                session->state = ISCSI_STATE_LOGGING_OUT;
 
+       task->state = ISCSI_TASK_RUNNING;
        list_move_tail(&task->running, &conn->mgmt_run_list);
        debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
                   hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
@@ -469,6 +495,7 @@ __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
 {
        struct iscsi_session *session = conn->session;
        struct iscsi_task *task;
+       itt_t itt;
 
        if (session->state == ISCSI_STATE_TERMINATE)
                return NULL;
@@ -489,12 +516,6 @@ __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
                if (!__kfifo_get(session->cmdpool.queue,
                                 (void*)&task, sizeof(void*)))
                        return NULL;
-
-               if ((hdr->opcode == (ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE)) &&
-                    hdr->ttt == RESERVED_ITT) {
-                       conn->ping_task = task;
-                       conn->last_ping = jiffies;
-               }
        }
        /*
         * released in complete pdu for task we expect a response for, and
@@ -511,23 +532,47 @@ __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
        } else
                task->data_count = 0;
 
+       if (conn->session->tt->alloc_pdu(task, hdr->opcode)) {
+               iscsi_conn_printk(KERN_ERR, conn, "Could not allocate "
+                                "pdu for mgmt task.\n");
+               goto requeue_task;
+       }
+       itt = task->hdr->itt;
+       task->hdr_len = sizeof(struct iscsi_hdr);
        memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr));
+
+       if (hdr->itt != RESERVED_ITT) {
+               if (session->tt->parse_pdu_itt)
+                       task->hdr->itt = itt;
+               else
+                       task->hdr->itt = build_itt(task->itt,
+                                                  task->conn->session->age);
+       }
+
        INIT_LIST_HEAD(&task->running);
        list_add_tail(&task->running, &conn->mgmtqueue);
 
        if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
-               if (iscsi_prep_mgmt_task(conn, task)) {
-                       __iscsi_put_task(task);
-                       return NULL;
-               }
+               if (iscsi_prep_mgmt_task(conn, task))
+                       goto free_task;
 
                if (session->tt->xmit_task(task))
-                       task = NULL;
+                       goto free_task;
 
        } else
                scsi_queue_work(conn->session->host, &conn->xmitwork);
 
        return task;
+
+free_task:
+       __iscsi_put_task(task);
+       return NULL;
+
+requeue_task:
+       if (task != conn->login_task)
+               __kfifo_put(session->cmdpool.queue, (void*)&task,
+                           sizeof(void*));
+       return NULL;
 }
 
 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
@@ -586,7 +631,7 @@ invalid_datalen:
                        goto out;
                }
 
-               senselen = be16_to_cpu(get_unaligned((__be16 *) data));
+               senselen = get_unaligned_be16(data);
                if (datalen < senselen)
                        goto invalid_datalen;
 
@@ -628,6 +673,40 @@ out:
        __iscsi_put_task(task);
 }
 
+/**
+ * iscsi_data_in_rsp - SCSI Data-In Response processing
+ * @conn: iscsi connection
+ * @hdr:  iscsi pdu
+ * @task: scsi command task
+ **/
+static void
+iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
+                 struct iscsi_task *task)
+{
+       struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr;
+       struct scsi_cmnd *sc = task->sc;
+
+       if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
+               return;
+
+       sc->result = (DID_OK << 16) | rhdr->cmd_status;
+       conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
+       if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
+                          ISCSI_FLAG_DATA_OVERFLOW)) {
+               int res_count = be32_to_cpu(rhdr->residual_count);
+
+               if (res_count > 0 &&
+                   (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
+                    res_count <= scsi_in(sc)->length))
+                       scsi_in(sc)->resid = res_count;
+               else
+                       sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
+       }
+
+       conn->scsirsp_pdus_cnt++;
+       __iscsi_put_task(task);
+}
+
 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
 {
        struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
@@ -669,6 +748,11 @@ static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
        task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
        if (!task)
                iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
+       else if (!rhdr) {
+               /* only track our nops */
+               conn->ping_task = task;
+               conn->last_ping = jiffies;
+       }
 }
 
 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
@@ -676,7 +760,6 @@ static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
 {
        struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
        struct iscsi_hdr rejected_pdu;
-       uint32_t itt;
 
        conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
 
@@ -686,10 +769,9 @@ static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
 
                if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
                        memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
-                       itt = get_itt(rejected_pdu.itt);
                        iscsi_conn_printk(KERN_ERR, conn,
-                                         "itt 0x%x had pdu (op 0x%x) rejected "
-                                         "due to DataDigest error.\n", itt,
+                                         "pdu (op 0x%x) rejected "
+                                         "due to DataDigest error.\n",
                                          rejected_pdu.opcode);
                }
        }
@@ -697,6 +779,34 @@ static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
 }
 
 /**
+ * iscsi_itt_to_task - look up task by itt
+ * @conn: iscsi connection
+ * @itt: itt
+ *
+ * This should be used for mgmt tasks like login and nops, or if
+ * the LDD's itt space does not include the session age.
+ *
+ * The session lock must be held.
+ */
+static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt)
+{
+       struct iscsi_session *session = conn->session;
+       int i;
+
+       if (itt == RESERVED_ITT)
+               return NULL;
+
+       if (session->tt->parse_pdu_itt)
+               session->tt->parse_pdu_itt(conn, itt, &i, NULL);
+       else
+               i = get_itt(itt);
+       if (i >= session->cmds_max)
+               return NULL;
+
+       return session->cmds[i];
+}
+
+/**
  * __iscsi_complete_pdu - complete pdu
  * @conn: iscsi conn
  * @hdr: iscsi header
@@ -707,8 +817,8 @@ static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  * queuecommand or send generic. session lock must be held and verify
  * itt must have been called.
  */
-static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
-                               char *data, int datalen)
+int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
+                        char *data, int datalen)
 {
        struct iscsi_session *session = conn->session;
        int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
@@ -758,31 +868,37 @@ static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
                goto out;
        }
 
-       task = session->cmds[itt];
        switch(opcode) {
        case ISCSI_OP_SCSI_CMD_RSP:
-               if (!task->sc) {
-                       rc = ISCSI_ERR_NO_SCSI_CMD;
-                       break;
-               }
-               BUG_ON((void*)task != task->sc->SCp.ptr);
-               iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
-               break;
        case ISCSI_OP_SCSI_DATA_IN:
-               if (!task->sc) {
-                       rc = ISCSI_ERR_NO_SCSI_CMD;
-                       break;
-               }
-               BUG_ON((void*)task != task->sc->SCp.ptr);
-               if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
-                       conn->scsirsp_pdus_cnt++;
-                       iscsi_update_cmdsn(session,
-                                          (struct iscsi_nopin*) hdr);
-                       __iscsi_put_task(task);
-               }
+               task = iscsi_itt_to_ctask(conn, hdr->itt);
+               if (!task)
+                       return ISCSI_ERR_BAD_ITT;
                break;
        case ISCSI_OP_R2T:
-               /* LLD handles this for now */
+               /*
+                * LLD handles R2Ts if they need to.
+                */
+               return 0;
+       case ISCSI_OP_LOGOUT_RSP:
+       case ISCSI_OP_LOGIN_RSP:
+       case ISCSI_OP_TEXT_RSP:
+       case ISCSI_OP_SCSI_TMFUNC_RSP:
+       case ISCSI_OP_NOOP_IN:
+               task = iscsi_itt_to_task(conn, hdr->itt);
+               if (!task)
+                       return ISCSI_ERR_BAD_ITT;
+               break;
+       default:
+               return ISCSI_ERR_BAD_OPCODE;
+       }
+
+       switch(opcode) {
+       case ISCSI_OP_SCSI_CMD_RSP:
+               iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen);
+               break;
+       case ISCSI_OP_SCSI_DATA_IN:
+               iscsi_data_in_rsp(conn, hdr, task);
                break;
        case ISCSI_OP_LOGOUT_RSP:
                iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
@@ -841,6 +957,7 @@ recv_pdu:
        __iscsi_put_task(task);
        return rc;
 }
+EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
 
 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
                       char *data, int datalen)
@@ -857,69 +974,98 @@ EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
 int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
 {
        struct iscsi_session *session = conn->session;
-       struct iscsi_task *task;
-       uint32_t i;
+       int age = 0, i = 0;
 
        if (itt == RESERVED_ITT)
                return 0;
 
-       if (((__force u32)itt & ISCSI_AGE_MASK) !=
-           (session->age << ISCSI_AGE_SHIFT)) {
+       if (session->tt->parse_pdu_itt)
+               session->tt->parse_pdu_itt(conn, itt, &i, &age);
+       else {
+               i = get_itt(itt);
+               age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK;
+       }
+
+       if (age != session->age) {
                iscsi_conn_printk(KERN_ERR, conn,
                                  "received itt %x expected session age (%x)\n",
-                                 (__force u32)itt,
-                                 session->age & ISCSI_AGE_MASK);
+                                 (__force u32)itt, session->age);
                return ISCSI_ERR_BAD_ITT;
        }
 
-       i = get_itt(itt);
        if (i >= session->cmds_max) {
                iscsi_conn_printk(KERN_ERR, conn,
                                  "received invalid itt index %u (max cmds "
                                   "%u.\n", i, session->cmds_max);
                return ISCSI_ERR_BAD_ITT;
        }
-
-       task = session->cmds[i];
-       if (task->sc && task->sc->SCp.phase != session->age) {
-               iscsi_conn_printk(KERN_ERR, conn,
-                                 "iscsi: task's session age %d, "
-                                 "expected %d\n", task->sc->SCp.phase,
-                                 session->age);
-               return ISCSI_ERR_SESSION_FAILED;
-       }
        return 0;
 }
 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
 
-struct iscsi_task *
-iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
+/**
+ * iscsi_itt_to_ctask - look up ctask by itt
+ * @conn: iscsi connection
+ * @itt: itt
+ *
+ * This should be used for cmd tasks.
+ *
+ * The session lock must be held.
+ */
+struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt)
 {
-       struct iscsi_session *session = conn->session;
        struct iscsi_task *task;
-       uint32_t i;
 
        if (iscsi_verify_itt(conn, itt))
                return NULL;
 
-       if (itt == RESERVED_ITT)
-               return NULL;
-
-       i = get_itt(itt);
-       if (i >= session->cmds_max)
-               return NULL;
-
-       task = session->cmds[i];
-       if (!task->sc)
+       task = iscsi_itt_to_task(conn, itt);
+       if (!task || !task->sc)
                return NULL;
 
-       if (task->sc->SCp.phase != session->age)
+       if (task->sc->SCp.phase != conn->session->age) {
+               iscsi_session_printk(KERN_ERR, conn->session,
+                                 "task's session age %d, expected %d\n",
+                                 task->sc->SCp.phase, conn->session->age);
                return NULL;
+       }
 
        return task;
 }
 EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask);
 
+void iscsi_session_failure(struct iscsi_cls_session *cls_session,
+                          enum iscsi_err err)
+{
+       struct iscsi_session *session = cls_session->dd_data;
+       struct iscsi_conn *conn;
+       struct device *dev;
+       unsigned long flags;
+
+       spin_lock_irqsave(&session->lock, flags);
+       conn = session->leadconn;
+       if (session->state == ISCSI_STATE_TERMINATE || !conn) {
+               spin_unlock_irqrestore(&session->lock, flags);
+               return;
+       }
+
+       dev = get_device(&conn->cls_conn->dev);
+       spin_unlock_irqrestore(&session->lock, flags);
+       if (!dev)
+               return;
+       /*
+        * if the host is being removed bypass the connection
+        * recovery initialization because we are going to kill
+        * the session.
+        */
+       if (err == ISCSI_ERR_INVALID_HOST)
+               iscsi_conn_error_event(conn->cls_conn, err);
+       else
+               iscsi_conn_failure(conn, err);
+       put_device(dev);
+}
+EXPORT_SYMBOL_GPL(iscsi_session_failure);
+
 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
 {
        struct iscsi_session *session = conn->session;
@@ -934,9 +1080,10 @@ void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
        if (conn->stop_stage == 0)
                session->state = ISCSI_STATE_FAILED;
        spin_unlock_irqrestore(&session->lock, flags);
+
        set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
        set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
-       iscsi_conn_error(conn->cls_conn, err);
+       iscsi_conn_error_event(conn->cls_conn, err);
 }
 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
 
@@ -1046,8 +1193,13 @@ check_mgmt:
                        fail_command(conn, conn->task, DID_IMM_RETRY << 16);
                        continue;
                }
-               if (iscsi_prep_scsi_cmd_pdu(conn->task)) {
-                       fail_command(conn, conn->task, DID_ABORT << 16);
+               rc = iscsi_prep_scsi_cmd_pdu(conn->task);
+               if (rc) {
+                       if (rc == -ENOMEM) {
+                               conn->task = NULL;
+                               goto again;
+                       } else
+                               fail_command(conn, conn->task, DID_ABORT << 16);
                        continue;
                }
                rc = iscsi_xmit_task(conn);
@@ -1105,6 +1257,26 @@ static void iscsi_xmitworker(struct work_struct *work)
        } while (rc >= 0 || rc == -EAGAIN);
 }
 
+static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn,
+                                                 struct scsi_cmnd *sc)
+{
+       struct iscsi_task *task;
+
+       if (!__kfifo_get(conn->session->cmdpool.queue,
+                        (void *) &task, sizeof(void *)))
+               return NULL;
+
+       sc->SCp.phase = conn->session->age;
+       sc->SCp.ptr = (char *) task;
+
+       atomic_set(&task->refcount, 1);
+       task->state = ISCSI_TASK_PENDING;
+       task->conn = conn;
+       task->sc = sc;
+       INIT_LIST_HEAD(&task->running);
+       return task;
+}
+
 enum {
        FAILURE_BAD_HOST = 1,
        FAILURE_SESSION_FAILED,
@@ -1160,15 +1332,13 @@ int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
                switch (session->state) {
                case ISCSI_STATE_IN_RECOVERY:
                        reason = FAILURE_SESSION_IN_RECOVERY;
-                       sc->result = DID_IMM_RETRY << 16;
-                       break;
+                       goto reject;
                case ISCSI_STATE_LOGGING_OUT:
                        reason = FAILURE_SESSION_LOGGING_OUT;
-                       sc->result = DID_IMM_RETRY << 16;
-                       break;
+                       goto reject;
                case ISCSI_STATE_RECOVERY_FAILED:
                        reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
-                       sc->result = DID_NO_CONNECT << 16;
+                       sc->result = DID_TRANSPORT_FAILFAST << 16;
                        break;
                case ISCSI_STATE_TERMINATE:
                        reason = FAILURE_SESSION_TERMINATE;
@@ -1193,33 +1363,27 @@ int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
                goto reject;
        }
 
-       if (!__kfifo_get(session->cmdpool.queue, (void*)&task,
-                        sizeof(void*))) {
+       task = iscsi_alloc_task(conn, sc);
+       if (!task) {
                reason = FAILURE_OOM;
                goto reject;
        }
-       sc->SCp.phase = session->age;
-       sc->SCp.ptr = (char *)task;
-
-       atomic_set(&task->refcount, 1);
-       task->state = ISCSI_TASK_PENDING;
-       task->conn = conn;
-       task->sc = sc;
-       INIT_LIST_HEAD(&task->running);
        list_add_tail(&task->running, &conn->xmitqueue);
 
        if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) {
-               if (iscsi_prep_scsi_cmd_pdu(task)) {
-                       sc->result = DID_ABORT << 16;
-                       sc->scsi_done = NULL;
-                       iscsi_complete_command(task);
-                       goto fault;
+               reason = iscsi_prep_scsi_cmd_pdu(task);
+               if (reason) {
+                       if (reason == -ENOMEM) {
+                               reason = FAILURE_OOM;
+                               goto prepd_reject;
+                       } else {
+                               sc->result = DID_ABORT << 16;
+                               goto prepd_fault;
+                       }
                }
                if (session->tt->xmit_task(task)) {
-                       sc->scsi_done = NULL;
-                       iscsi_complete_command(task);
                        reason = FAILURE_SESSION_NOT_READY;
-                       goto reject;
+                       goto prepd_reject;
                }
        } else
                scsi_queue_work(session->host, &conn->xmitwork);
@@ -1229,12 +1393,18 @@ int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
        spin_lock(host->host_lock);
        return 0;
 
+prepd_reject:
+       sc->scsi_done = NULL;
+       iscsi_complete_command(task);
 reject:
        spin_unlock(&session->lock);
        debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
        spin_lock(host->host_lock);
-       return SCSI_MLQUEUE_HOST_BUSY;
+       return SCSI_MLQUEUE_TARGET_BUSY;
 
+prepd_fault:
+       sc->scsi_done = NULL;
+       iscsi_complete_command(task);
 fault:
        spin_unlock(&session->lock);
        debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
@@ -1273,7 +1443,7 @@ void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
 }
 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
 
-int iscsi_eh_host_reset(struct scsi_cmnd *sc)
+int iscsi_eh_target_reset(struct scsi_cmnd *sc)
 {
        struct iscsi_cls_session *cls_session;
        struct iscsi_session *session;
@@ -1287,7 +1457,7 @@ int iscsi_eh_host_reset(struct scsi_cmnd *sc)
        spin_lock_bh(&session->lock);
        if (session->state == ISCSI_STATE_TERMINATE) {
 failed:
-               debug_scsi("failing host reset: session terminated "
+               debug_scsi("failing target reset: session terminated "
                           "[CID %d age %d]\n", conn->id, session->age);
                spin_unlock_bh(&session->lock);
                mutex_unlock(&session->eh_mutex);
@@ -1302,7 +1472,7 @@ failed:
         */
        iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
 
-       debug_scsi("iscsi_eh_host_reset wait for relogin\n");
+       debug_scsi("iscsi_eh_target_reset wait for relogin\n");
        wait_event_interruptible(conn->ehwait,
                                 session->state == ISCSI_STATE_TERMINATE ||
                                 session->state == ISCSI_STATE_LOGGED_IN ||
@@ -1314,14 +1484,14 @@ failed:
        spin_lock_bh(&session->lock);
        if (session->state == ISCSI_STATE_LOGGED_IN)
                iscsi_session_printk(KERN_INFO, session,
-                                    "host reset succeeded\n");
+                                    "target reset succeeded\n");
        else
                goto failed;
        spin_unlock_bh(&session->lock);
        mutex_unlock(&session->eh_mutex);
        return SUCCESS;
 }
-EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
+EXPORT_SYMBOL_GPL(iscsi_eh_target_reset);
 
 static void iscsi_tmf_timedout(unsigned long data)
 {
@@ -1422,7 +1592,7 @@ static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
                if (lun == task->sc->device->lun || lun == -1) {
                        debug_scsi("failing in progress sc %p itt 0x%x\n",
                                   task->sc, task->itt);
-                       fail_command(conn, task, DID_BUS_BUSY << 16);
+                       fail_command(conn, task, error << 16);
                }
        }
 }
@@ -1442,12 +1612,12 @@ static void iscsi_start_tx(struct iscsi_conn *conn)
                scsi_queue_work(conn->session->host, &conn->xmitwork);
 }
 
-static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
+static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
 {
        struct iscsi_cls_session *cls_session;
        struct iscsi_session *session;
        struct iscsi_conn *conn;
-       enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
+       enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED;
 
        cls_session = starget_to_session(scsi_target(scmd->device));
        session = cls_session->dd_data;
@@ -1460,14 +1630,14 @@ static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
                 * We are probably in the middle of iscsi recovery so let
                 * that complete and handle the error.
                 */
-               rc = EH_RESET_TIMER;
+               rc = BLK_EH_RESET_TIMER;
                goto done;
        }
 
        conn = session->leadconn;
        if (!conn) {
                /* In the middle of shuting down */
-               rc = EH_RESET_TIMER;
+               rc = BLK_EH_RESET_TIMER;
                goto done;
        }
 
@@ -1479,20 +1649,21 @@ static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
         */
        if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
                            (conn->ping_timeout * HZ), jiffies))
-               rc = EH_RESET_TIMER;
+               rc = BLK_EH_RESET_TIMER;
        /*
         * if we are about to check the transport then give the command
         * more time
         */
        if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
                           jiffies))
-               rc = EH_RESET_TIMER;
+               rc = BLK_EH_RESET_TIMER;
        /* if in the middle of checking the transport then give us more time */
        if (conn->ping_task)
-               rc = EH_RESET_TIMER;
+               rc = BLK_EH_RESET_TIMER;
 done:
        spin_unlock(&session->lock);
-       debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
+       debug_scsi("return %s\n", rc == BLK_EH_RESET_TIMER ?
+                                       "timer reset" : "nh");
        return rc;
 }
 
@@ -1545,9 +1716,9 @@ static void iscsi_prep_abort_task_pdu(struct iscsi_task *task,
        hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
        hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
        hdr->flags |= ISCSI_FLAG_CMD_FINAL;
-       memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
-       hdr->rtt = task->hdr->itt;
-       hdr->refcmdsn = task->hdr->cmdsn;
+       memcpy(hdr->lun, task->lun, sizeof(hdr->lun));
+       hdr->rtt = task->hdr_itt;
+       hdr->refcmdsn = task->cmdsn;
 }
 
 int iscsi_eh_abort(struct scsi_cmnd *sc)
@@ -1620,16 +1791,20 @@ int iscsi_eh_abort(struct scsi_cmnd *sc)
        switch (conn->tmf_state) {
        case TMF_SUCCESS:
                spin_unlock_bh(&session->lock);
+               /*
+                * stop tx side incase the target had sent a abort rsp but
+                * the initiator was still writing out data.
+                */
                iscsi_suspend_tx(conn);
                /*
-                * clean up task if aborted. grab the recv lock as a writer
+                * we do not stop the recv side because targets have been
+                * good and have never sent us a successful tmf response
+                * then sent more data for the cmd.
                 */
-               write_lock_bh(conn->recv_lock);
                spin_lock(&session->lock);
                fail_command(conn, task, DID_ABORT << 16);
                conn->tmf_state = TMF_INITIAL;
                spin_unlock(&session->lock);
-               write_unlock_bh(conn->recv_lock);
                iscsi_start_tx(conn);
                goto success_unlocked;
        case TMF_TIMEDOUT:
@@ -1729,13 +1904,11 @@ int iscsi_eh_device_reset(struct scsi_cmnd *sc)
        spin_unlock_bh(&session->lock);
 
        iscsi_suspend_tx(conn);
-       /* need to grab the recv lock then session lock */
-       write_lock_bh(conn->recv_lock);
-       spin_lock(&session->lock);
+
+       spin_lock_bh(&session->lock);
        fail_all_commands(conn, sc->device->lun, DID_ERROR);
        conn->tmf_state = TMF_INITIAL;
-       spin_unlock(&session->lock);
-       write_unlock_bh(conn->recv_lock);
+       spin_unlock_bh(&session->lock);
 
        iscsi_start_tx(conn);
        goto done;
@@ -1821,6 +1994,9 @@ EXPORT_SYMBOL_GPL(iscsi_pool_free);
  */
 int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev)
 {
+       if (!shost->can_queue)
+               shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX;
+
        return scsi_add_host(shost, pdev);
 }
 EXPORT_SYMBOL_GPL(iscsi_host_add);
@@ -1838,6 +2014,7 @@ struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
                                   int dd_data_size, uint16_t qdepth)
 {
        struct Scsi_Host *shost;
+       struct iscsi_host *ihost;
 
        shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size);
        if (!shost)
@@ -1852,22 +2029,43 @@ struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht,
                qdepth = ISCSI_DEF_CMD_PER_LUN;
        }
        shost->cmd_per_lun = qdepth;
+
+       ihost = shost_priv(shost);
+       spin_lock_init(&ihost->lock);
+       ihost->state = ISCSI_HOST_SETUP;
+       ihost->num_sessions = 0;
+       init_waitqueue_head(&ihost->session_removal_wq);
        return shost;
 }
 EXPORT_SYMBOL_GPL(iscsi_host_alloc);
 
+static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session)
+{
+       iscsi_session_failure(cls_session, ISCSI_ERR_INVALID_HOST);
+}
+
 /**
  * iscsi_host_remove - remove host and sessions
  * @shost: scsi host
  *
- * This will also remove any sessions attached to the host, but if userspace
- * is managing the session at the same time this will break. TODO: add
- * refcounting to the netlink iscsi interface so a rmmod or host hot unplug
- * does not remove the memory from under us.
+ * If there are any sessions left, this will initiate the removal and wait
+ * for the completion.
  */
 void iscsi_host_remove(struct Scsi_Host *shost)
 {
-       iscsi_host_for_each_session(shost, iscsi_session_teardown);
+       struct iscsi_host *ihost = shost_priv(shost);
+       unsigned long flags;
+
+       spin_lock_irqsave(&ihost->lock, flags);
+       ihost->state = ISCSI_HOST_REMOVED;
+       spin_unlock_irqrestore(&ihost->lock, flags);
+
+       iscsi_host_for_each_session(shost, iscsi_notify_host_removed);
+       wait_event_interruptible(ihost->session_removal_wq,
+                                ihost->num_sessions == 0);
+       if (signal_pending(current))
+               flush_signals(current);
+
        scsi_remove_host(shost);
 }
 EXPORT_SYMBOL_GPL(iscsi_host_remove);
@@ -1883,6 +2081,27 @@ void iscsi_host_free(struct Scsi_Host *shost)
 }
 EXPORT_SYMBOL_GPL(iscsi_host_free);
 
+static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost)
+{
+       struct iscsi_host *ihost = shost_priv(shost);
+       unsigned long flags;
+
+       shost = scsi_host_get(shost);
+       if (!shost) {
+               printk(KERN_ERR "Invalid state. Cannot notify host removal "
+                     "of session teardown event because host already "
+                     "removed.\n");
+               return;
+       }
+
+       spin_lock_irqsave(&ihost->lock, flags);
+       ihost->num_sessions--;
+       if (ihost->num_sessions == 0)
+               wake_up(&ihost->session_removal_wq);
+       spin_unlock_irqrestore(&ihost->lock, flags);
+       scsi_host_put(shost);
+}
+
 /**
  * iscsi_session_setup - create iscsi cls session and host and session
  * @iscsit: iscsi transport template
@@ -1893,34 +2112,66 @@ EXPORT_SYMBOL_GPL(iscsi_host_free);
  *
  * This can be used by software iscsi_transports that allocate
  * a session per scsi host.
+ *
+ * Callers should set cmds_max to the largest total numer (mgmt + scsi) of
+ * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks
+ * for nop handling and login/logout requests.
  */
 struct iscsi_cls_session *
 iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
-                   uint16_t scsi_cmds_max, int cmd_task_size,
+                   uint16_t cmds_max, int cmd_task_size,
                    uint32_t initial_cmdsn, unsigned int id)
 {
+       struct iscsi_host *ihost = shost_priv(shost);
        struct iscsi_session *session;
        struct iscsi_cls_session *cls_session;
-       int cmd_i, cmds_max;
+       int cmd_i, scsi_cmds, total_cmds = cmds_max;
+       unsigned long flags;
 
+       spin_lock_irqsave(&ihost->lock, flags);
+       if (ihost->state == ISCSI_HOST_REMOVED) {
+               spin_unlock_irqrestore(&ihost->lock, flags);
+               return NULL;
+       }
+       ihost->num_sessions++;
+       spin_unlock_irqrestore(&ihost->lock, flags);
+
+       if (!total_cmds)
+               total_cmds = ISCSI_DEF_XMIT_CMDS_MAX;
        /*
-        * The iscsi layer needs some tasks for nop handling and tmfs.
+        * The iscsi layer needs some tasks for nop handling and tmfs,
+        * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX
+        * + 1 command for scsi IO.
         */
-       if (scsi_cmds_max < 1)
-               scsi_cmds_max = ISCSI_MGMT_CMDS_MAX;
-       if ((scsi_cmds_max + ISCSI_MGMT_CMDS_MAX) >= ISCSI_MGMT_ITT_OFFSET) {
-               printk(KERN_ERR "iscsi: invalid can_queue of %d. "
-                      "can_queue must be less than %d.\n",
-                      scsi_cmds_max,
-                      ISCSI_MGMT_ITT_OFFSET - ISCSI_MGMT_CMDS_MAX);
-               scsi_cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
+       if (total_cmds < ISCSI_TOTAL_CMDS_MIN) {
+               printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
+                      "must be a power of two that is at least %d.\n",
+                      total_cmds, ISCSI_TOTAL_CMDS_MIN);
+               goto dec_session_count;
+       }
+
+       if (total_cmds > ISCSI_TOTAL_CMDS_MAX) {
+               printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
+                      "must be a power of 2 less than or equal to %d.\n",
+                      cmds_max, ISCSI_TOTAL_CMDS_MAX);
+               total_cmds = ISCSI_TOTAL_CMDS_MAX;
+       }
+
+       if (!is_power_of_2(total_cmds)) {
+               printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue "
+                      "must be a power of 2.\n", total_cmds);
+               total_cmds = rounddown_pow_of_two(total_cmds);
+               if (total_cmds < ISCSI_TOTAL_CMDS_MIN)
+                       return NULL;
+               printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n",
+                      total_cmds);
        }
-       cmds_max = roundup_pow_of_two(scsi_cmds_max + ISCSI_MGMT_CMDS_MAX);
+       scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX;
 
        cls_session = iscsi_alloc_session(shost, iscsit,
                                          sizeof(struct iscsi_session));
        if (!cls_session)
-               return NULL;
+               goto dec_session_count;
        session = cls_session->dd_data;
        session->cls_session = cls_session;
        session->host = shost;
@@ -1928,8 +2179,8 @@ iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
        session->fast_abort = 1;
        session->lu_reset_timeout = 15;
        session->abort_timeout = 10;
-       session->scsi_cmds_max = scsi_cmds_max;
-       session->cmds_max = cmds_max;
+       session->scsi_cmds_max = scsi_cmds;
+       session->cmds_max = total_cmds;
        session->queued_cmdsn = session->cmdsn = initial_cmdsn;
        session->exp_cmdsn = initial_cmdsn + 1;
        session->max_cmdsn = initial_cmdsn + 1;
@@ -1959,6 +2210,7 @@ iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
 
        if (iscsi_add_session(cls_session, id))
                goto cls_session_fail;
+
        return cls_session;
 
 cls_session_fail:
@@ -1967,6 +2219,8 @@ module_get_fail:
        iscsi_pool_free(&session->cmdpool);
 cmdpool_alloc_fail:
        iscsi_free_session(cls_session);
+dec_session_count:
+       iscsi_host_dec_session_cnt(shost);
        return NULL;
 }
 EXPORT_SYMBOL_GPL(iscsi_session_setup);
@@ -1982,6 +2236,7 @@ void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
 {
        struct iscsi_session *session = cls_session->dd_data;
        struct module *owner = cls_session->transport->owner;
+       struct Scsi_Host *shost = session->host;
 
        iscsi_pool_free(&session->cmdpool);
 
@@ -1994,6 +2249,7 @@ void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
        kfree(session->ifacename);
 
        iscsi_destroy_session(cls_session);
+       iscsi_host_dec_session_cnt(shost);
        module_put(owner);
 }
 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
@@ -2049,7 +2305,8 @@ iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size,
        }
        spin_unlock_bh(&session->lock);
 
-       data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
+       data = (char *) __get_free_pages(GFP_KERNEL,
+                                        get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
        if (!data)
                goto login_task_data_alloc_fail;
        conn->login_task->data = conn->data = data;
@@ -2120,7 +2377,8 @@ void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
        iscsi_suspend_tx(conn);
 
        spin_lock_bh(&session->lock);
-       kfree(conn->data);
+       free_pages((unsigned long) conn->data,
+                  get_order(ISCSI_DEF_MAX_RECV_SEG_LEN));
        kfree(conn->persistent_address);
        __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task,
                    sizeof(void*));
@@ -2238,17 +2496,6 @@ static void iscsi_start_session_recovery(struct iscsi_session *session,
        }
 
        /*
-        * The LLD either freed/unset the lock on us, or userspace called
-        * stop but did not create a proper connection (connection was never
-        * bound or it was unbound then stop was called).
-        */
-       if (!conn->recv_lock) {
-               spin_unlock_bh(&session->lock);
-               mutex_unlock(&session->eh_mutex);
-               return;
-       }
-
-       /*
         * When this is called for the in_login state, we only want to clean
         * up the login task and connection. We do not need to block and set
         * the recovery state again
@@ -2264,11 +2511,6 @@ static void iscsi_start_session_recovery(struct iscsi_session *session,
        spin_unlock_bh(&session->lock);
 
        iscsi_suspend_tx(conn);
-
-       write_lock_bh(conn->recv_lock);
-       set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
-       write_unlock_bh(conn->recv_lock);
-
        /*
         * for connection level recovery we should not calculate
         * header digest. conn->hdr_size used for optimization
@@ -2289,8 +2531,10 @@ static void iscsi_start_session_recovery(struct iscsi_session *session,
         * flush queues.
         */
        spin_lock_bh(&session->lock);
-       fail_all_commands(conn, -1,
-                       STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
+       if (flag == STOP_CONN_RECOVER)
+               fail_all_commands(conn, -1, DID_TRANSPORT_DISRUPTED);
+       else
+               fail_all_commands(conn, -1, DID_ERROR);
        flush_control_queues(session, conn);
        spin_unlock_bh(&session->lock);
        mutex_unlock(&session->eh_mutex);