Input: i8042 - add Thinkpad R31 to nomux list
[safe/jmp/linux-2.6] / drivers / scsi / libiscsi.c
index 92ee6d9..299e075 100644 (file)
@@ -362,10 +362,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)
 {
@@ -403,9 +404,13 @@ static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task,
                conn->session->queued_cmdsn--;
        else
                conn->session->tt->cleanup_task(conn, task);
+       /*
+        * Check if cleanup_task dropped the lock and the command completed,
+        */
+       if (!task->sc)
+               return;
 
        sc->result = err;
-
        if (!scsi_bidi_cmnd(sc))
                scsi_set_resid(sc, scsi_bufflen(sc));
        else {
@@ -586,7 +591,7 @@ invalid_datalen:
                        goto out;
                }
 
-               senselen = be16_to_cpu(get_unaligned((__be16 *) data));
+               senselen = get_unaligned_be16(data);
                if (datalen < senselen)
                        goto invalid_datalen;
 
@@ -697,6 +702,31 @@ 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;
+       uint32_t i;
+
+       if (itt == RESERVED_ITT)
+               return NULL;
+
+       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 +737,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,22 +788,36 @@ 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);
+       case ISCSI_OP_SCSI_DATA_IN:
+               task = iscsi_itt_to_ctask(conn, hdr->itt);
+               if (!task)
+                       return ISCSI_ERR_BAD_ITT;
+               break;
+       case ISCSI_OP_R2T:
+               /*
+                * 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:
-               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,
@@ -781,9 +825,6 @@ static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
                        __iscsi_put_task(task);
                }
                break;
-       case ISCSI_OP_R2T:
-               /* LLD handles this for now */
-               break;
        case ISCSI_OP_LOGOUT_RSP:
                iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
                if (datalen) {
@@ -841,6 +882,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,7 +899,6 @@ 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;
 
        if (itt == RESERVED_ITT)
@@ -867,8 +908,7 @@ int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
            (session->age << ISCSI_AGE_SHIFT)) {
                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;
        }
 
@@ -879,42 +919,36 @@ int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt)
                                   "%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;
 }
@@ -1620,16 +1654,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 +1767,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);
        fail_all_commands(conn, sc->device->lun, DID_ERROR);
        conn->tmf_state = TMF_INITIAL;
        spin_unlock(&session->lock);
-       write_unlock_bh(conn->recv_lock);
 
        iscsi_start_tx(conn);
        goto done;
@@ -1821,6 +1857,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);
@@ -1893,29 +1932,51 @@ 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,
-                   uint32_t initial_cmdsn)
+                   uint16_t cmds_max, int cmd_task_size,
+                   uint32_t initial_cmdsn, unsigned int id)
 {
        struct iscsi_session *session;
        struct iscsi_cls_session *cls_session;
-       int cmd_i, cmds_max;
+       int cmd_i, scsi_cmds, total_cmds = cmds_max;
 
+       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);
+               return NULL;
+       }
+
+       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));
@@ -1928,8 +1989,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;
@@ -1957,7 +2018,7 @@ iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost,
        if (!try_module_get(iscsit->owner))
                goto module_get_fail;
 
-       if (iscsi_add_session(cls_session, 0))
+       if (iscsi_add_session(cls_session, id))
                goto cls_session_fail;
        return cls_session;
 
@@ -1990,6 +2051,8 @@ void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
        kfree(session->username);
        kfree(session->username_in);
        kfree(session->targetname);
+       kfree(session->initiatorname);
+       kfree(session->ifacename);
 
        iscsi_destroy_session(cls_session);
        module_put(owner);
@@ -2236,17 +2299,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
@@ -2262,11 +2314,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
@@ -2453,6 +2500,14 @@ int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
                if (!conn->persistent_address)
                        return -ENOMEM;
                break;
+       case ISCSI_PARAM_IFACE_NAME:
+               if (!session->ifacename)
+                       session->ifacename = kstrdup(buf, GFP_KERNEL);
+               break;
+       case ISCSI_PARAM_INITIATOR_NAME:
+               if (!session->initiatorname)
+                       session->initiatorname = kstrdup(buf, GFP_KERNEL);
+               break;
        default:
                return -ENOSYS;
        }
@@ -2519,6 +2574,15 @@ int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
        case ISCSI_PARAM_PASSWORD_IN:
                len = sprintf(buf, "%s\n", session->password_in);
                break;
+       case ISCSI_PARAM_IFACE_NAME:
+               len = sprintf(buf, "%s\n", session->ifacename);
+               break;
+       case ISCSI_PARAM_INITIATOR_NAME:
+               if (!session->initiatorname)
+                       len = sprintf(buf, "%s\n", "unknown");
+               else
+                       len = sprintf(buf, "%s\n", session->initiatorname);
+               break;
        default:
                return -ENOSYS;
        }
@@ -2606,6 +2670,7 @@ int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
                else
                        len = sprintf(buf, "%s\n",
                                      ihost->local_address);
+               break;
        default:
                return -ENOSYS;
        }