basic modification from way back
[powerpc.git] / drivers / scsi / libiscsi.c
index d37048c..3f5b9b4 100644 (file)
@@ -25,6 +25,7 @@
 #include <linux/mutex.h>
 #include <linux/kfifo.h>
 #include <linux/delay.h>
+#include <asm/unaligned.h>
 #include <net/tcp.h>
 #include <scsi/scsi_cmnd.h>
 #include <scsi/scsi_device.h>
@@ -113,8 +114,7 @@ static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
         hdr->opcode = ISCSI_OP_SCSI_CMD;
         hdr->flags = ISCSI_ATTR_SIMPLE;
         int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
-        hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
-                         (session->age << ISCSI_AGE_SHIFT);
+        hdr->itt = build_itt(ctask->itt, conn->id, session->age);
         hdr->data_length = cpu_to_be32(sc->request_bufflen);
         hdr->cmdsn = cpu_to_be32(session->cmdsn);
         session->cmdsn++;
@@ -270,14 +270,14 @@ invalid_datalen:
                        goto out;
                }
 
-               senselen = be16_to_cpu(*(uint16_t *)data);
+               senselen = be16_to_cpu(get_unaligned((__be16 *) data));
                if (datalen < senselen)
                        goto invalid_datalen;
 
                memcpy(sc->sense_buffer, data + 2,
                       min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
                debug_scsi("copied %d bytes of sense\n",
-                          min(senselen, SCSI_SENSE_BUFFERSIZE));
+                          min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
        }
 
        if (sc->sc_data_direction == DMA_TO_DEVICE)
@@ -338,7 +338,7 @@ 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 = rejected_pdu.itt & ISCSI_ITT_MASK;
+                       itt = get_itt(rejected_pdu.itt);
                        printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
                                "due to DataDigest error.\n", itt,
                                rejected_pdu.opcode);
@@ -367,10 +367,10 @@ int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
        struct iscsi_mgmt_task *mtask;
        uint32_t itt;
 
-       if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
-               itt = hdr->itt & ISCSI_ITT_MASK;
+       if (hdr->itt != RESERVED_ITT)
+               itt = get_itt(hdr->itt);
        else
-               itt = hdr->itt;
+               itt = ~0U;
 
        if (itt < session->cmds_max) {
                ctask = session->cmds[itt];
@@ -440,7 +440,7 @@ int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
                        iscsi_tmf_rsp(conn, hdr);
                        break;
                case ISCSI_OP_NOOP_IN:
-                       if (hdr->ttt != ISCSI_RESERVED_TAG || datalen) {
+                       if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
                                rc = ISCSI_ERR_PROTO;
                                break;
                        }
@@ -457,7 +457,7 @@ int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
                        rc = ISCSI_ERR_BAD_OPCODE;
                        break;
                }
-       } else if (itt == ISCSI_RESERVED_TAG) {
+       } else if (itt == ~0U) {
                rc = iscsi_check_assign_cmdsn(session,
                                             (struct iscsi_nopin*)hdr);
                if (rc)
@@ -470,7 +470,7 @@ int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
                                break;
                        }
 
-                       if (hdr->ttt == ISCSI_RESERVED_TAG)
+                       if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
                                break;
 
                        if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
@@ -516,24 +516,24 @@ int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
        struct iscsi_cmd_task *ctask;
        uint32_t itt;
 
-       if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
-               if ((hdr->itt & ISCSI_AGE_MASK) !=
+       if (hdr->itt != RESERVED_ITT) {
+               if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
                    (session->age << ISCSI_AGE_SHIFT)) {
                        printk(KERN_ERR "iscsi: received itt %x expected "
-                               "session age (%x)\n", hdr->itt,
+                               "session age (%x)\n", (__force u32)hdr->itt,
                                session->age & ISCSI_AGE_MASK);
                        return ISCSI_ERR_BAD_ITT;
                }
 
-               if ((hdr->itt & ISCSI_CID_MASK) !=
+               if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
                    (conn->id << ISCSI_CID_SHIFT)) {
                        printk(KERN_ERR "iscsi: received itt %x, expected "
-                               "CID (%x)\n", hdr->itt, conn->id);
+                               "CID (%x)\n", (__force u32)hdr->itt, conn->id);
                        return ISCSI_ERR_BAD_ITT;
                }
-               itt = hdr->itt & ISCSI_ITT_MASK;
+               itt = get_itt(hdr->itt);
        } else
-               itt = hdr->itt;
+               itt = ~0U;
 
        if (itt < session->cmds_max) {
                ctask = session->cmds[itt];
@@ -578,7 +578,7 @@ void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
 }
 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
 
-static int iscsi_xmit_imm_task(struct iscsi_conn *conn)
+static int iscsi_xmit_mtask(struct iscsi_conn *conn)
 {
        struct iscsi_hdr *hdr = conn->mtask->hdr;
        int rc, was_logout = 0;
@@ -592,6 +592,9 @@ static int iscsi_xmit_imm_task(struct iscsi_conn *conn)
        if (rc)
                return rc;
 
+       /* done with this in-progress mtask */
+       conn->mtask = NULL;
+
        if (was_logout) {
                set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
                return -ENODATA;
@@ -644,11 +647,9 @@ static int iscsi_data_xmit(struct iscsi_conn *conn)
                conn->ctask = NULL;
        }
        if (conn->mtask) {
-               rc = iscsi_xmit_imm_task(conn);
+               rc = iscsi_xmit_mtask(conn);
                if (rc)
                        goto again;
-               /* done with this in-progress mtask */
-               conn->mtask = NULL;
        }
 
        /* process immediate first */
@@ -659,12 +660,10 @@ static int iscsi_data_xmit(struct iscsi_conn *conn)
                        list_add_tail(&conn->mtask->running,
                                      &conn->mgmt_run_list);
                        spin_unlock_bh(&conn->session->lock);
-                       rc = iscsi_xmit_imm_task(conn);
+                       rc = iscsi_xmit_mtask(conn);
                        if (rc)
                                goto again;
                }
-               /* done with this mtask */
-               conn->mtask = NULL;
        }
 
        /* process command queue */
@@ -702,12 +701,10 @@ static int iscsi_data_xmit(struct iscsi_conn *conn)
                        list_add_tail(&conn->mtask->running,
                                      &conn->mgmt_run_list);
                        spin_unlock_bh(&conn->session->lock);
-                       rc = tt->xmit_mgmt_task(conn, conn->mtask);
-                       if (rc)
+                       rc = iscsi_xmit_mtask(conn);
+                       if (rc)
                                goto again;
                }
-               /* done with this mtask */
-               conn->mtask = NULL;
        }
 
        return -ENODATA;
@@ -896,9 +893,8 @@ iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
        /*
         * pre-format CmdSN for outgoing PDU.
         */
-       if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
-               hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
-                          (session->age << ISCSI_AGE_SHIFT);
+       if (hdr->itt != RESERVED_ITT) {
+               hdr->itt = build_itt(mtask->itt, conn->id, session->age);
                nop->cmdsn = cpu_to_be32(session->cmdsn);
                if (conn->c_stage == ISCSI_CONN_STARTED &&
                    !(hdr->opcode & ISCSI_OP_IMMEDIATE))
@@ -1064,7 +1060,7 @@ static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
 
        spin_lock_bh(&session->lock);
        ctask->mtask = (struct iscsi_mgmt_task *)
-                       session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
+                       session->mgmt_cmds[get_itt(hdr->itt) -
                                        ISCSI_MGMT_ITT_OFFSET];
 
        if (conn->tmabort_state == TMABORT_INITIAL) {
@@ -1525,7 +1521,7 @@ iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
        }
        spin_unlock_bh(&session->lock);
 
-       data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
+       data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
        if (!data)
                goto login_mtask_data_alloc_fail;
        conn->login_mtask->data = conn->data = data;
@@ -1599,6 +1595,9 @@ void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
                wake_up(&conn->ehwait);
        }
 
+       /* flush queued up work because we free the connection below */
+       scsi_flush_work(session->host);
+
        spin_lock_bh(&session->lock);
        kfree(conn->data);
        kfree(conn->persistent_address);