Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*******************************************************************************
0003  * This file contains error recovery level one used by the iSCSI Target driver.
0004  *
0005  * (c) Copyright 2007-2013 Datera, Inc.
0006  *
0007  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
0008  *
0009  ******************************************************************************/
0010 
0011 #include <linux/list.h>
0012 #include <linux/slab.h>
0013 #include <scsi/iscsi_proto.h>
0014 #include <target/target_core_base.h>
0015 #include <target/target_core_fabric.h>
0016 #include <target/iscsi/iscsi_transport.h>
0017 
0018 #include <target/iscsi/iscsi_target_core.h>
0019 #include "iscsi_target_seq_pdu_list.h"
0020 #include "iscsi_target_datain_values.h"
0021 #include "iscsi_target_device.h"
0022 #include "iscsi_target_tpg.h"
0023 #include "iscsi_target_util.h"
0024 #include "iscsi_target_erl0.h"
0025 #include "iscsi_target_erl1.h"
0026 #include "iscsi_target_erl2.h"
0027 #include "iscsi_target.h"
0028 
0029 #define OFFLOAD_BUF_SIZE    32768U
0030 
0031 /*
0032  *  Used to dump excess datain payload for certain error recovery
0033  *  situations.  Receive in OFFLOAD_BUF_SIZE max of datain per rx_data().
0034  *
0035  *  dump_padding_digest denotes if padding and data digests need
0036  *  to be dumped.
0037  */
0038 int iscsit_dump_data_payload(
0039     struct iscsit_conn *conn,
0040     u32 buf_len,
0041     int dump_padding_digest)
0042 {
0043     char *buf;
0044     int ret = DATAOUT_WITHIN_COMMAND_RECOVERY, rx_got;
0045     u32 length, offset = 0, size;
0046     struct kvec iov;
0047 
0048     if (conn->sess->sess_ops->RDMAExtensions)
0049         return 0;
0050 
0051     if (dump_padding_digest) {
0052         buf_len = ALIGN(buf_len, 4);
0053         if (conn->conn_ops->DataDigest)
0054             buf_len += ISCSI_CRC_LEN;
0055     }
0056 
0057     length = min(buf_len, OFFLOAD_BUF_SIZE);
0058 
0059     buf = kzalloc(length, GFP_ATOMIC);
0060     if (!buf) {
0061         pr_err("Unable to allocate %u bytes for offload"
0062                 " buffer.\n", length);
0063         return -1;
0064     }
0065     memset(&iov, 0, sizeof(struct kvec));
0066 
0067     while (offset < buf_len) {
0068         size = min(buf_len - offset, length);
0069 
0070         iov.iov_len = size;
0071         iov.iov_base = buf;
0072 
0073         rx_got = rx_data(conn, &iov, 1, size);
0074         if (rx_got != size) {
0075             ret = DATAOUT_CANNOT_RECOVER;
0076             break;
0077         }
0078 
0079         offset += size;
0080     }
0081 
0082     kfree(buf);
0083     return ret;
0084 }
0085 
0086 /*
0087  *  Used for retransmitting R2Ts from a R2T SNACK request.
0088  */
0089 static int iscsit_send_recovery_r2t_for_snack(
0090     struct iscsit_cmd *cmd,
0091     struct iscsi_r2t *r2t)
0092 {
0093     /*
0094      * If the struct iscsi_r2t has not been sent yet, we can safely
0095      * ignore retransmission
0096      * of the R2TSN in question.
0097      */
0098     spin_lock_bh(&cmd->r2t_lock);
0099     if (!r2t->sent_r2t) {
0100         spin_unlock_bh(&cmd->r2t_lock);
0101         return 0;
0102     }
0103     r2t->sent_r2t = 0;
0104     spin_unlock_bh(&cmd->r2t_lock);
0105 
0106     iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
0107 
0108     return 0;
0109 }
0110 
0111 static int iscsit_handle_r2t_snack(
0112     struct iscsit_cmd *cmd,
0113     unsigned char *buf,
0114     u32 begrun,
0115     u32 runlength)
0116 {
0117     u32 last_r2tsn;
0118     struct iscsi_r2t *r2t;
0119 
0120     /*
0121      * Make sure the initiator is not requesting retransmission
0122      * of R2TSNs already acknowledged by a TMR TASK_REASSIGN.
0123      */
0124     if ((cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
0125         (begrun <= cmd->acked_data_sn)) {
0126         pr_err("ITT: 0x%08x, R2T SNACK requesting"
0127             " retransmission of R2TSN: 0x%08x to 0x%08x but already"
0128             " acked to  R2TSN: 0x%08x by TMR TASK_REASSIGN,"
0129             " protocol error.\n", cmd->init_task_tag, begrun,
0130             (begrun + runlength), cmd->acked_data_sn);
0131 
0132         return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
0133     }
0134 
0135     if (runlength) {
0136         if ((begrun + runlength) > cmd->r2t_sn) {
0137             pr_err("Command ITT: 0x%08x received R2T SNACK"
0138             " with BegRun: 0x%08x, RunLength: 0x%08x, exceeds"
0139             " current R2TSN: 0x%08x, protocol error.\n",
0140             cmd->init_task_tag, begrun, runlength, cmd->r2t_sn);
0141             return iscsit_reject_cmd(cmd,
0142                     ISCSI_REASON_BOOKMARK_INVALID, buf);
0143         }
0144         last_r2tsn = (begrun + runlength);
0145     } else
0146         last_r2tsn = cmd->r2t_sn;
0147 
0148     while (begrun < last_r2tsn) {
0149         r2t = iscsit_get_holder_for_r2tsn(cmd, begrun);
0150         if (!r2t)
0151             return -1;
0152         if (iscsit_send_recovery_r2t_for_snack(cmd, r2t) < 0)
0153             return -1;
0154 
0155         begrun++;
0156     }
0157 
0158     return 0;
0159 }
0160 
0161 /*
0162  *  Generates Offsets and NextBurstLength based on Begrun and Runlength
0163  *  carried in a Data SNACK or ExpDataSN in TMR TASK_REASSIGN.
0164  *
0165  *  For DataSequenceInOrder=Yes and DataPDUInOrder=[Yes,No] only.
0166  *
0167  *  FIXME: How is this handled for a RData SNACK?
0168  */
0169 int iscsit_create_recovery_datain_values_datasequenceinorder_yes(
0170     struct iscsit_cmd *cmd,
0171     struct iscsi_datain_req *dr)
0172 {
0173     u32 data_sn = 0, data_sn_count = 0;
0174     u32 pdu_start = 0, seq_no = 0;
0175     u32 begrun = dr->begrun;
0176     struct iscsit_conn *conn = cmd->conn;
0177 
0178     while (begrun > data_sn++) {
0179         data_sn_count++;
0180         if ((dr->next_burst_len +
0181              conn->conn_ops->MaxRecvDataSegmentLength) <
0182              conn->sess->sess_ops->MaxBurstLength) {
0183             dr->read_data_done +=
0184                 conn->conn_ops->MaxRecvDataSegmentLength;
0185             dr->next_burst_len +=
0186                 conn->conn_ops->MaxRecvDataSegmentLength;
0187         } else {
0188             dr->read_data_done +=
0189                 (conn->sess->sess_ops->MaxBurstLength -
0190                  dr->next_burst_len);
0191             dr->next_burst_len = 0;
0192             pdu_start += data_sn_count;
0193             data_sn_count = 0;
0194             seq_no++;
0195         }
0196     }
0197 
0198     if (!conn->sess->sess_ops->DataPDUInOrder) {
0199         cmd->seq_no = seq_no;
0200         cmd->pdu_start = pdu_start;
0201         cmd->pdu_send_order = data_sn_count;
0202     }
0203 
0204     return 0;
0205 }
0206 
0207 /*
0208  *  Generates Offsets and NextBurstLength based on Begrun and Runlength
0209  *  carried in a Data SNACK or ExpDataSN in TMR TASK_REASSIGN.
0210  *
0211  *  For DataSequenceInOrder=No and DataPDUInOrder=[Yes,No] only.
0212  *
0213  *  FIXME: How is this handled for a RData SNACK?
0214  */
0215 int iscsit_create_recovery_datain_values_datasequenceinorder_no(
0216     struct iscsit_cmd *cmd,
0217     struct iscsi_datain_req *dr)
0218 {
0219     int found_seq = 0, i;
0220     u32 data_sn, read_data_done = 0, seq_send_order = 0;
0221     u32 begrun = dr->begrun;
0222     u32 runlength = dr->runlength;
0223     struct iscsit_conn *conn = cmd->conn;
0224     struct iscsi_seq *first_seq = NULL, *seq = NULL;
0225 
0226     if (!cmd->seq_list) {
0227         pr_err("struct iscsit_cmd->seq_list is NULL!\n");
0228         return -1;
0229     }
0230 
0231     /*
0232      * Calculate read_data_done for all sequences containing a
0233      * first_datasn and last_datasn less than the BegRun.
0234      *
0235      * Locate the struct iscsi_seq the BegRun lies within and calculate
0236      * NextBurstLenghth up to the DataSN based on MaxRecvDataSegmentLength.
0237      *
0238      * Also use struct iscsi_seq->seq_send_order to determine where to start.
0239      */
0240     for (i = 0; i < cmd->seq_count; i++) {
0241         seq = &cmd->seq_list[i];
0242 
0243         if (!seq->seq_send_order)
0244             first_seq = seq;
0245 
0246         /*
0247          * No data has been transferred for this DataIN sequence, so the
0248          * seq->first_datasn and seq->last_datasn have not been set.
0249          */
0250         if (!seq->sent) {
0251             pr_err("Ignoring non-sent sequence 0x%08x ->"
0252                 " 0x%08x\n\n", seq->first_datasn,
0253                 seq->last_datasn);
0254             continue;
0255         }
0256 
0257         /*
0258          * This DataIN sequence is precedes the received BegRun, add the
0259          * total xfer_len of the sequence to read_data_done and reset
0260          * seq->pdu_send_order.
0261          */
0262         if ((seq->first_datasn < begrun) &&
0263                 (seq->last_datasn < begrun)) {
0264             pr_err("Pre BegRun sequence 0x%08x ->"
0265                 " 0x%08x\n", seq->first_datasn,
0266                 seq->last_datasn);
0267 
0268             read_data_done += cmd->seq_list[i].xfer_len;
0269             seq->next_burst_len = seq->pdu_send_order = 0;
0270             continue;
0271         }
0272 
0273         /*
0274          * The BegRun lies within this DataIN sequence.
0275          */
0276         if ((seq->first_datasn <= begrun) &&
0277                 (seq->last_datasn >= begrun)) {
0278             pr_err("Found sequence begrun: 0x%08x in"
0279                 " 0x%08x -> 0x%08x\n", begrun,
0280                 seq->first_datasn, seq->last_datasn);
0281 
0282             seq_send_order = seq->seq_send_order;
0283             data_sn = seq->first_datasn;
0284             seq->next_burst_len = seq->pdu_send_order = 0;
0285             found_seq = 1;
0286 
0287             /*
0288              * For DataPDUInOrder=Yes, while the first DataSN of
0289              * the sequence is less than the received BegRun, add
0290              * the MaxRecvDataSegmentLength to read_data_done and
0291              * to the sequence's next_burst_len;
0292              *
0293              * For DataPDUInOrder=No, while the first DataSN of the
0294              * sequence is less than the received BegRun, find the
0295              * struct iscsi_pdu of the DataSN in question and add the
0296              * MaxRecvDataSegmentLength to read_data_done and to the
0297              * sequence's next_burst_len;
0298              */
0299             if (conn->sess->sess_ops->DataPDUInOrder) {
0300                 while (data_sn < begrun) {
0301                     seq->pdu_send_order++;
0302                     read_data_done +=
0303                         conn->conn_ops->MaxRecvDataSegmentLength;
0304                     seq->next_burst_len +=
0305                         conn->conn_ops->MaxRecvDataSegmentLength;
0306                     data_sn++;
0307                 }
0308             } else {
0309                 int j;
0310                 struct iscsi_pdu *pdu;
0311 
0312                 while (data_sn < begrun) {
0313                     seq->pdu_send_order++;
0314 
0315                     for (j = 0; j < seq->pdu_count; j++) {
0316                         pdu = &cmd->pdu_list[
0317                             seq->pdu_start + j];
0318                         if (pdu->data_sn == data_sn) {
0319                             read_data_done +=
0320                                 pdu->length;
0321                             seq->next_burst_len +=
0322                                 pdu->length;
0323                         }
0324                     }
0325                     data_sn++;
0326                 }
0327             }
0328             continue;
0329         }
0330 
0331         /*
0332          * This DataIN sequence is larger than the received BegRun,
0333          * reset seq->pdu_send_order and continue.
0334          */
0335         if ((seq->first_datasn > begrun) ||
0336                 (seq->last_datasn > begrun)) {
0337             pr_err("Post BegRun sequence 0x%08x -> 0x%08x\n",
0338                     seq->first_datasn, seq->last_datasn);
0339 
0340             seq->next_burst_len = seq->pdu_send_order = 0;
0341             continue;
0342         }
0343     }
0344 
0345     if (!found_seq) {
0346         if (!begrun) {
0347             if (!first_seq) {
0348                 pr_err("ITT: 0x%08x, Begrun: 0x%08x"
0349                     " but first_seq is NULL\n",
0350                     cmd->init_task_tag, begrun);
0351                 return -1;
0352             }
0353             seq_send_order = first_seq->seq_send_order;
0354             seq->next_burst_len = seq->pdu_send_order = 0;
0355             goto done;
0356         }
0357 
0358         pr_err("Unable to locate struct iscsi_seq for ITT: 0x%08x,"
0359             " BegRun: 0x%08x, RunLength: 0x%08x while"
0360             " DataSequenceInOrder=No and DataPDUInOrder=%s.\n",
0361                 cmd->init_task_tag, begrun, runlength,
0362             (conn->sess->sess_ops->DataPDUInOrder) ? "Yes" : "No");
0363         return -1;
0364     }
0365 
0366 done:
0367     dr->read_data_done = read_data_done;
0368     dr->seq_send_order = seq_send_order;
0369 
0370     return 0;
0371 }
0372 
0373 static int iscsit_handle_recovery_datain(
0374     struct iscsit_cmd *cmd,
0375     unsigned char *buf,
0376     u32 begrun,
0377     u32 runlength)
0378 {
0379     struct iscsit_conn *conn = cmd->conn;
0380     struct iscsi_datain_req *dr;
0381     struct se_cmd *se_cmd = &cmd->se_cmd;
0382 
0383     if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
0384         pr_err("Ignoring ITT: 0x%08x Data SNACK\n",
0385                 cmd->init_task_tag);
0386         return 0;
0387     }
0388 
0389     /*
0390      * Make sure the initiator is not requesting retransmission
0391      * of DataSNs already acknowledged by a Data ACK SNACK.
0392      */
0393     if ((cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
0394         (begrun <= cmd->acked_data_sn)) {
0395         pr_err("ITT: 0x%08x, Data SNACK requesting"
0396             " retransmission of DataSN: 0x%08x to 0x%08x but"
0397             " already acked to DataSN: 0x%08x by Data ACK SNACK,"
0398             " protocol error.\n", cmd->init_task_tag, begrun,
0399             (begrun + runlength), cmd->acked_data_sn);
0400 
0401         return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
0402     }
0403 
0404     /*
0405      * Make sure BegRun and RunLength in the Data SNACK are sane.
0406      * Note: (cmd->data_sn - 1) will carry the maximum DataSN sent.
0407      */
0408     if ((begrun + runlength) > (cmd->data_sn - 1)) {
0409         pr_err("Initiator requesting BegRun: 0x%08x, RunLength"
0410             ": 0x%08x greater than maximum DataSN: 0x%08x.\n",
0411                 begrun, runlength, (cmd->data_sn - 1));
0412         return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID,
0413                      buf);
0414     }
0415 
0416     dr = iscsit_allocate_datain_req();
0417     if (!dr)
0418         return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_NO_RESOURCES,
0419                      buf);
0420 
0421     dr->data_sn = dr->begrun = begrun;
0422     dr->runlength = runlength;
0423     dr->generate_recovery_values = 1;
0424     dr->recovery = DATAIN_WITHIN_COMMAND_RECOVERY;
0425 
0426     iscsit_attach_datain_req(cmd, dr);
0427 
0428     cmd->i_state = ISTATE_SEND_DATAIN;
0429     iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
0430 
0431     return 0;
0432 }
0433 
0434 int iscsit_handle_recovery_datain_or_r2t(
0435     struct iscsit_conn *conn,
0436     unsigned char *buf,
0437     itt_t init_task_tag,
0438     u32 targ_xfer_tag,
0439     u32 begrun,
0440     u32 runlength)
0441 {
0442     struct iscsit_cmd *cmd;
0443 
0444     cmd = iscsit_find_cmd_from_itt(conn, init_task_tag);
0445     if (!cmd)
0446         return 0;
0447 
0448     /*
0449      * FIXME: This will not work for bidi commands.
0450      */
0451     switch (cmd->data_direction) {
0452     case DMA_TO_DEVICE:
0453         return iscsit_handle_r2t_snack(cmd, buf, begrun, runlength);
0454     case DMA_FROM_DEVICE:
0455         return iscsit_handle_recovery_datain(cmd, buf, begrun,
0456                 runlength);
0457     default:
0458         pr_err("Unknown cmd->data_direction: 0x%02x\n",
0459                 cmd->data_direction);
0460         return -1;
0461     }
0462 
0463     return 0;
0464 }
0465 
0466 /* #warning FIXME: Status SNACK needs to be dependent on OPCODE!!! */
0467 int iscsit_handle_status_snack(
0468     struct iscsit_conn *conn,
0469     itt_t init_task_tag,
0470     u32 targ_xfer_tag,
0471     u32 begrun,
0472     u32 runlength)
0473 {
0474     struct iscsit_cmd *cmd = NULL;
0475     u32 last_statsn;
0476     int found_cmd;
0477 
0478     if (!begrun) {
0479         begrun = conn->exp_statsn;
0480     } else if (conn->exp_statsn > begrun) {
0481         pr_err("Got Status SNACK Begrun: 0x%08x, RunLength:"
0482             " 0x%08x but already got ExpStatSN: 0x%08x on CID:"
0483             " %hu.\n", begrun, runlength, conn->exp_statsn,
0484             conn->cid);
0485         return 0;
0486     }
0487 
0488     last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);
0489 
0490     while (begrun < last_statsn) {
0491         found_cmd = 0;
0492 
0493         spin_lock_bh(&conn->cmd_lock);
0494         list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
0495             if (cmd->stat_sn == begrun) {
0496                 found_cmd = 1;
0497                 break;
0498             }
0499         }
0500         spin_unlock_bh(&conn->cmd_lock);
0501 
0502         if (!found_cmd) {
0503             pr_err("Unable to find StatSN: 0x%08x for"
0504                 " a Status SNACK, assuming this was a"
0505                 " protactic SNACK for an untransmitted"
0506                 " StatSN, ignoring.\n", begrun);
0507             begrun++;
0508             continue;
0509         }
0510 
0511         spin_lock_bh(&cmd->istate_lock);
0512         if (cmd->i_state == ISTATE_SEND_DATAIN) {
0513             spin_unlock_bh(&cmd->istate_lock);
0514             pr_err("Ignoring Status SNACK for BegRun:"
0515                 " 0x%08x, RunLength: 0x%08x, assuming this was"
0516                 " a protactic SNACK for an untransmitted"
0517                 " StatSN\n", begrun, runlength);
0518             begrun++;
0519             continue;
0520         }
0521         spin_unlock_bh(&cmd->istate_lock);
0522 
0523         cmd->i_state = ISTATE_SEND_STATUS_RECOVERY;
0524         iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
0525         begrun++;
0526     }
0527 
0528     return 0;
0529 }
0530 
0531 int iscsit_handle_data_ack(
0532     struct iscsit_conn *conn,
0533     u32 targ_xfer_tag,
0534     u32 begrun,
0535     u32 runlength)
0536 {
0537     struct iscsit_cmd *cmd = NULL;
0538 
0539     cmd = iscsit_find_cmd_from_ttt(conn, targ_xfer_tag);
0540     if (!cmd) {
0541         pr_err("Data ACK SNACK for TTT: 0x%08x is"
0542             " invalid.\n", targ_xfer_tag);
0543         return -1;
0544     }
0545 
0546     if (begrun <= cmd->acked_data_sn) {
0547         pr_err("ITT: 0x%08x Data ACK SNACK BegRUN: 0x%08x is"
0548             " less than the already acked DataSN: 0x%08x.\n",
0549             cmd->init_task_tag, begrun, cmd->acked_data_sn);
0550         return -1;
0551     }
0552 
0553     /*
0554      * For Data ACK SNACK, BegRun is the next expected DataSN.
0555      * (see iSCSI v19: 10.16.6)
0556      */
0557     cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
0558     cmd->acked_data_sn = (begrun - 1);
0559 
0560     pr_debug("Received Data ACK SNACK for ITT: 0x%08x,"
0561         " updated acked DataSN to 0x%08x.\n",
0562             cmd->init_task_tag, cmd->acked_data_sn);
0563 
0564     return 0;
0565 }
0566 
0567 static int iscsit_send_recovery_r2t(
0568     struct iscsit_cmd *cmd,
0569     u32 offset,
0570     u32 xfer_len)
0571 {
0572     int ret;
0573 
0574     spin_lock_bh(&cmd->r2t_lock);
0575     ret = iscsit_add_r2t_to_list(cmd, offset, xfer_len, 1, 0);
0576     spin_unlock_bh(&cmd->r2t_lock);
0577 
0578     return ret;
0579 }
0580 
0581 int iscsit_dataout_datapduinorder_no_fbit(
0582     struct iscsit_cmd *cmd,
0583     struct iscsi_pdu *pdu)
0584 {
0585     int i, send_recovery_r2t = 0, recovery = 0;
0586     u32 length = 0, offset = 0, pdu_count = 0, xfer_len = 0;
0587     struct iscsit_conn *conn = cmd->conn;
0588     struct iscsi_pdu *first_pdu = NULL;
0589 
0590     /*
0591      * Get an struct iscsi_pdu pointer to the first PDU, and total PDU count
0592      * of the DataOUT sequence.
0593      */
0594     if (conn->sess->sess_ops->DataSequenceInOrder) {
0595         for (i = 0; i < cmd->pdu_count; i++) {
0596             if (cmd->pdu_list[i].seq_no == pdu->seq_no) {
0597                 if (!first_pdu)
0598                     first_pdu = &cmd->pdu_list[i];
0599                 xfer_len += cmd->pdu_list[i].length;
0600                 pdu_count++;
0601             } else if (pdu_count)
0602                 break;
0603         }
0604     } else {
0605         struct iscsi_seq *seq = cmd->seq_ptr;
0606 
0607         first_pdu = &cmd->pdu_list[seq->pdu_start];
0608         pdu_count = seq->pdu_count;
0609     }
0610 
0611     if (!first_pdu || !pdu_count)
0612         return DATAOUT_CANNOT_RECOVER;
0613 
0614     /*
0615      * Loop through the ending DataOUT Sequence checking each struct iscsi_pdu.
0616      * The following ugly logic does batching of not received PDUs.
0617      */
0618     for (i = 0; i < pdu_count; i++) {
0619         if (first_pdu[i].status == ISCSI_PDU_RECEIVED_OK) {
0620             if (!send_recovery_r2t)
0621                 continue;
0622 
0623             if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
0624                 return DATAOUT_CANNOT_RECOVER;
0625 
0626             send_recovery_r2t = length = offset = 0;
0627             continue;
0628         }
0629         /*
0630          * Set recovery = 1 for any missing, CRC failed, or timed
0631          * out PDUs to let the DataOUT logic know that this sequence
0632          * has not been completed yet.
0633          *
0634          * Also, only send a Recovery R2T for ISCSI_PDU_NOT_RECEIVED.
0635          * We assume if the PDU either failed CRC or timed out
0636          * that a Recovery R2T has already been sent.
0637          */
0638         recovery = 1;
0639 
0640         if (first_pdu[i].status != ISCSI_PDU_NOT_RECEIVED)
0641             continue;
0642 
0643         if (!offset)
0644             offset = first_pdu[i].offset;
0645         length += first_pdu[i].length;
0646 
0647         send_recovery_r2t = 1;
0648     }
0649 
0650     if (send_recovery_r2t)
0651         if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
0652             return DATAOUT_CANNOT_RECOVER;
0653 
0654     return (!recovery) ? DATAOUT_NORMAL : DATAOUT_WITHIN_COMMAND_RECOVERY;
0655 }
0656 
0657 static int iscsit_recalculate_dataout_values(
0658     struct iscsit_cmd *cmd,
0659     u32 pdu_offset,
0660     u32 pdu_length,
0661     u32 *r2t_offset,
0662     u32 *r2t_length)
0663 {
0664     int i;
0665     struct iscsit_conn *conn = cmd->conn;
0666     struct iscsi_pdu *pdu = NULL;
0667 
0668     if (conn->sess->sess_ops->DataSequenceInOrder) {
0669         cmd->data_sn = 0;
0670 
0671         if (conn->sess->sess_ops->DataPDUInOrder) {
0672             *r2t_offset = cmd->write_data_done;
0673             *r2t_length = (cmd->seq_end_offset -
0674                     cmd->write_data_done);
0675             return 0;
0676         }
0677 
0678         *r2t_offset = cmd->seq_start_offset;
0679         *r2t_length = (cmd->seq_end_offset - cmd->seq_start_offset);
0680 
0681         for (i = 0; i < cmd->pdu_count; i++) {
0682             pdu = &cmd->pdu_list[i];
0683 
0684             if (pdu->status != ISCSI_PDU_RECEIVED_OK)
0685                 continue;
0686 
0687             if ((pdu->offset >= cmd->seq_start_offset) &&
0688                ((pdu->offset + pdu->length) <=
0689                  cmd->seq_end_offset)) {
0690                 if (!cmd->unsolicited_data)
0691                     cmd->next_burst_len -= pdu->length;
0692                 else
0693                     cmd->first_burst_len -= pdu->length;
0694 
0695                 cmd->write_data_done -= pdu->length;
0696                 pdu->status = ISCSI_PDU_NOT_RECEIVED;
0697             }
0698         }
0699     } else {
0700         struct iscsi_seq *seq = NULL;
0701 
0702         seq = iscsit_get_seq_holder(cmd, pdu_offset, pdu_length);
0703         if (!seq)
0704             return -1;
0705 
0706         *r2t_offset = seq->orig_offset;
0707         *r2t_length = seq->xfer_len;
0708 
0709         cmd->write_data_done -= (seq->offset - seq->orig_offset);
0710         if (cmd->immediate_data)
0711             cmd->first_burst_len = cmd->write_data_done;
0712 
0713         seq->data_sn = 0;
0714         seq->offset = seq->orig_offset;
0715         seq->next_burst_len = 0;
0716         seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
0717 
0718         if (conn->sess->sess_ops->DataPDUInOrder)
0719             return 0;
0720 
0721         for (i = 0; i < seq->pdu_count; i++) {
0722             pdu = &cmd->pdu_list[i+seq->pdu_start];
0723 
0724             if (pdu->status != ISCSI_PDU_RECEIVED_OK)
0725                 continue;
0726 
0727             pdu->status = ISCSI_PDU_NOT_RECEIVED;
0728         }
0729     }
0730 
0731     return 0;
0732 }
0733 
0734 int iscsit_recover_dataout_sequence(
0735     struct iscsit_cmd *cmd,
0736     u32 pdu_offset,
0737     u32 pdu_length)
0738 {
0739     u32 r2t_length = 0, r2t_offset = 0;
0740 
0741     spin_lock_bh(&cmd->istate_lock);
0742     cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
0743     spin_unlock_bh(&cmd->istate_lock);
0744 
0745     if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
0746             &r2t_offset, &r2t_length) < 0)
0747         return DATAOUT_CANNOT_RECOVER;
0748 
0749     iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length);
0750 
0751     return DATAOUT_WITHIN_COMMAND_RECOVERY;
0752 }
0753 
0754 static struct iscsi_ooo_cmdsn *iscsit_allocate_ooo_cmdsn(void)
0755 {
0756     struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL;
0757 
0758     ooo_cmdsn = kmem_cache_zalloc(lio_ooo_cache, GFP_ATOMIC);
0759     if (!ooo_cmdsn) {
0760         pr_err("Unable to allocate memory for"
0761             " struct iscsi_ooo_cmdsn.\n");
0762         return NULL;
0763     }
0764     INIT_LIST_HEAD(&ooo_cmdsn->ooo_list);
0765 
0766     return ooo_cmdsn;
0767 }
0768 
0769 static int iscsit_attach_ooo_cmdsn(
0770     struct iscsit_session *sess,
0771     struct iscsi_ooo_cmdsn *ooo_cmdsn)
0772 {
0773     struct iscsi_ooo_cmdsn *ooo_tail, *ooo_tmp;
0774 
0775     lockdep_assert_held(&sess->cmdsn_mutex);
0776 
0777     /*
0778      * We attach the struct iscsi_ooo_cmdsn entry to the out of order
0779      * list in increasing CmdSN order.
0780      * This allows iscsi_execute_ooo_cmdsns() to detect any
0781      * additional CmdSN holes while performing delayed execution.
0782      */
0783     if (list_empty(&sess->sess_ooo_cmdsn_list))
0784         list_add_tail(&ooo_cmdsn->ooo_list,
0785                 &sess->sess_ooo_cmdsn_list);
0786     else {
0787         ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
0788                 typeof(*ooo_tail), ooo_list);
0789         /*
0790          * CmdSN is greater than the tail of the list.
0791          */
0792         if (iscsi_sna_lt(ooo_tail->cmdsn, ooo_cmdsn->cmdsn))
0793             list_add_tail(&ooo_cmdsn->ooo_list,
0794                     &sess->sess_ooo_cmdsn_list);
0795         else {
0796             /*
0797              * CmdSN is either lower than the head,  or somewhere
0798              * in the middle.
0799              */
0800             list_for_each_entry(ooo_tmp, &sess->sess_ooo_cmdsn_list,
0801                         ooo_list) {
0802                 if (iscsi_sna_lt(ooo_tmp->cmdsn, ooo_cmdsn->cmdsn))
0803                     continue;
0804 
0805                 /* Insert before this entry */
0806                 list_add(&ooo_cmdsn->ooo_list,
0807                     ooo_tmp->ooo_list.prev);
0808                 break;
0809             }
0810         }
0811     }
0812 
0813     return 0;
0814 }
0815 
0816 /*
0817  *  Removes an struct iscsi_ooo_cmdsn from a session's list,
0818  *  called with struct iscsit_session->cmdsn_mutex held.
0819  */
0820 void iscsit_remove_ooo_cmdsn(
0821     struct iscsit_session *sess,
0822     struct iscsi_ooo_cmdsn *ooo_cmdsn)
0823 {
0824     list_del(&ooo_cmdsn->ooo_list);
0825     kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
0826 }
0827 
0828 void iscsit_clear_ooo_cmdsns_for_conn(struct iscsit_conn *conn)
0829 {
0830     struct iscsi_ooo_cmdsn *ooo_cmdsn;
0831     struct iscsit_session *sess = conn->sess;
0832 
0833     mutex_lock(&sess->cmdsn_mutex);
0834     list_for_each_entry(ooo_cmdsn, &sess->sess_ooo_cmdsn_list, ooo_list) {
0835         if (ooo_cmdsn->cid != conn->cid)
0836             continue;
0837 
0838         ooo_cmdsn->cmd = NULL;
0839     }
0840     mutex_unlock(&sess->cmdsn_mutex);
0841 }
0842 
0843 int iscsit_execute_ooo_cmdsns(struct iscsit_session *sess)
0844 {
0845     int ooo_count = 0;
0846     struct iscsit_cmd *cmd = NULL;
0847     struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
0848 
0849     lockdep_assert_held(&sess->cmdsn_mutex);
0850 
0851     list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
0852                 &sess->sess_ooo_cmdsn_list, ooo_list) {
0853         if (ooo_cmdsn->cmdsn != sess->exp_cmd_sn)
0854             continue;
0855 
0856         if (!ooo_cmdsn->cmd) {
0857             sess->exp_cmd_sn++;
0858             iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
0859             continue;
0860         }
0861 
0862         cmd = ooo_cmdsn->cmd;
0863         cmd->i_state = cmd->deferred_i_state;
0864         ooo_count++;
0865         sess->exp_cmd_sn++;
0866         pr_debug("Executing out of order CmdSN: 0x%08x,"
0867             " incremented ExpCmdSN to 0x%08x.\n",
0868             cmd->cmd_sn, sess->exp_cmd_sn);
0869 
0870         iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
0871 
0872         if (iscsit_execute_cmd(cmd, 1) < 0)
0873             return -1;
0874     }
0875 
0876     return ooo_count;
0877 }
0878 
0879 /*
0880  *  Called either:
0881  *
0882  *  1. With sess->cmdsn_mutex held from iscsi_execute_ooo_cmdsns()
0883  *  or iscsi_check_received_cmdsn().
0884  *  2. With no locks held directly from iscsi_handle_XXX_pdu() functions
0885  *  for immediate commands.
0886  */
0887 int iscsit_execute_cmd(struct iscsit_cmd *cmd, int ooo)
0888 {
0889     struct se_cmd *se_cmd = &cmd->se_cmd;
0890     struct iscsit_conn *conn = cmd->conn;
0891     int lr = 0;
0892 
0893     spin_lock_bh(&cmd->istate_lock);
0894     if (ooo)
0895         cmd->cmd_flags &= ~ICF_OOO_CMDSN;
0896 
0897     switch (cmd->iscsi_opcode) {
0898     case ISCSI_OP_SCSI_CMD:
0899         /*
0900          * Go ahead and send the CHECK_CONDITION status for
0901          * any SCSI CDB exceptions that may have occurred.
0902          */
0903         if (cmd->sense_reason) {
0904             if (cmd->sense_reason == TCM_RESERVATION_CONFLICT) {
0905                 cmd->i_state = ISTATE_SEND_STATUS;
0906                 spin_unlock_bh(&cmd->istate_lock);
0907                 iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
0908                         cmd->i_state);
0909                 return 0;
0910             }
0911             spin_unlock_bh(&cmd->istate_lock);
0912             if (cmd->se_cmd.transport_state & CMD_T_ABORTED)
0913                 return 0;
0914             return transport_send_check_condition_and_sense(se_cmd,
0915                     cmd->sense_reason, 0);
0916         }
0917         /*
0918          * Special case for delayed CmdSN with Immediate
0919          * Data and/or Unsolicited Data Out attached.
0920          */
0921         if (cmd->immediate_data) {
0922             if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
0923                 spin_unlock_bh(&cmd->istate_lock);
0924                 target_execute_cmd(&cmd->se_cmd);
0925                 return 0;
0926             }
0927             spin_unlock_bh(&cmd->istate_lock);
0928 
0929             if (!(cmd->cmd_flags &
0930                     ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
0931                 if (cmd->se_cmd.transport_state & CMD_T_ABORTED)
0932                     return 0;
0933 
0934                 iscsit_set_dataout_sequence_values(cmd);
0935                 conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
0936             }
0937             return 0;
0938         }
0939         /*
0940          * The default handler.
0941          */
0942         spin_unlock_bh(&cmd->istate_lock);
0943 
0944         if ((cmd->data_direction == DMA_TO_DEVICE) &&
0945             !(cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
0946             if (cmd->se_cmd.transport_state & CMD_T_ABORTED)
0947                 return 0;
0948 
0949             iscsit_set_unsolicited_dataout(cmd);
0950         }
0951         return transport_handle_cdb_direct(&cmd->se_cmd);
0952 
0953     case ISCSI_OP_NOOP_OUT:
0954     case ISCSI_OP_TEXT:
0955         spin_unlock_bh(&cmd->istate_lock);
0956         iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
0957         break;
0958     case ISCSI_OP_SCSI_TMFUNC:
0959         if (cmd->se_cmd.se_tmr_req->response) {
0960             spin_unlock_bh(&cmd->istate_lock);
0961             iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
0962                     cmd->i_state);
0963             return 0;
0964         }
0965         spin_unlock_bh(&cmd->istate_lock);
0966 
0967         return transport_generic_handle_tmr(&cmd->se_cmd);
0968     case ISCSI_OP_LOGOUT:
0969         spin_unlock_bh(&cmd->istate_lock);
0970         switch (cmd->logout_reason) {
0971         case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
0972             lr = iscsit_logout_closesession(cmd, cmd->conn);
0973             break;
0974         case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
0975             lr = iscsit_logout_closeconnection(cmd, cmd->conn);
0976             break;
0977         case ISCSI_LOGOUT_REASON_RECOVERY:
0978             lr = iscsit_logout_removeconnforrecovery(cmd, cmd->conn);
0979             break;
0980         default:
0981             pr_err("Unknown iSCSI Logout Request Code:"
0982                 " 0x%02x\n", cmd->logout_reason);
0983             return -1;
0984         }
0985 
0986         return lr;
0987     default:
0988         spin_unlock_bh(&cmd->istate_lock);
0989         pr_err("Cannot perform out of order execution for"
0990         " unknown iSCSI Opcode: 0x%02x\n", cmd->iscsi_opcode);
0991         return -1;
0992     }
0993 
0994     return 0;
0995 }
0996 
0997 void iscsit_free_all_ooo_cmdsns(struct iscsit_session *sess)
0998 {
0999     struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
1000 
1001     mutex_lock(&sess->cmdsn_mutex);
1002     list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
1003             &sess->sess_ooo_cmdsn_list, ooo_list) {
1004 
1005         list_del(&ooo_cmdsn->ooo_list);
1006         kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
1007     }
1008     mutex_unlock(&sess->cmdsn_mutex);
1009 }
1010 
1011 int iscsit_handle_ooo_cmdsn(
1012     struct iscsit_session *sess,
1013     struct iscsit_cmd *cmd,
1014     u32 cmdsn)
1015 {
1016     int batch = 0;
1017     struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL, *ooo_tail = NULL;
1018 
1019     cmd->deferred_i_state       = cmd->i_state;
1020     cmd->i_state            = ISTATE_DEFERRED_CMD;
1021     cmd->cmd_flags          |= ICF_OOO_CMDSN;
1022 
1023     if (list_empty(&sess->sess_ooo_cmdsn_list))
1024         batch = 1;
1025     else {
1026         ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
1027                 typeof(*ooo_tail), ooo_list);
1028         if (ooo_tail->cmdsn != (cmdsn - 1))
1029             batch = 1;
1030     }
1031 
1032     ooo_cmdsn = iscsit_allocate_ooo_cmdsn();
1033     if (!ooo_cmdsn)
1034         return -ENOMEM;
1035 
1036     ooo_cmdsn->cmd          = cmd;
1037     ooo_cmdsn->batch_count      = (batch) ?
1038                       (cmdsn - sess->exp_cmd_sn) : 1;
1039     ooo_cmdsn->cid          = cmd->conn->cid;
1040     ooo_cmdsn->exp_cmdsn        = sess->exp_cmd_sn;
1041     ooo_cmdsn->cmdsn        = cmdsn;
1042 
1043     if (iscsit_attach_ooo_cmdsn(sess, ooo_cmdsn) < 0) {
1044         kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
1045         return -ENOMEM;
1046     }
1047 
1048     return 0;
1049 }
1050 
1051 static int iscsit_set_dataout_timeout_values(
1052     struct iscsit_cmd *cmd,
1053     u32 *offset,
1054     u32 *length)
1055 {
1056     struct iscsit_conn *conn = cmd->conn;
1057     struct iscsi_r2t *r2t;
1058 
1059     if (cmd->unsolicited_data) {
1060         *offset = 0;
1061         *length = (conn->sess->sess_ops->FirstBurstLength >
1062                cmd->se_cmd.data_length) ?
1063                cmd->se_cmd.data_length :
1064                conn->sess->sess_ops->FirstBurstLength;
1065         return 0;
1066     }
1067 
1068     spin_lock_bh(&cmd->r2t_lock);
1069     if (list_empty(&cmd->cmd_r2t_list)) {
1070         pr_err("cmd->cmd_r2t_list is empty!\n");
1071         spin_unlock_bh(&cmd->r2t_lock);
1072         return -1;
1073     }
1074 
1075     list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
1076         if (r2t->sent_r2t && !r2t->recovery_r2t && !r2t->seq_complete) {
1077             *offset = r2t->offset;
1078             *length = r2t->xfer_len;
1079             spin_unlock_bh(&cmd->r2t_lock);
1080             return 0;
1081         }
1082     }
1083     spin_unlock_bh(&cmd->r2t_lock);
1084 
1085     pr_err("Unable to locate any incomplete DataOUT"
1086         " sequences for ITT: 0x%08x.\n", cmd->init_task_tag);
1087 
1088     return -1;
1089 }
1090 
1091 /*
1092  *  NOTE: Called from interrupt (timer) context.
1093  */
1094 void iscsit_handle_dataout_timeout(struct timer_list *t)
1095 {
1096     u32 pdu_length = 0, pdu_offset = 0;
1097     u32 r2t_length = 0, r2t_offset = 0;
1098     struct iscsit_cmd *cmd = from_timer(cmd, t, dataout_timer);
1099     struct iscsit_conn *conn = cmd->conn;
1100     struct iscsit_session *sess = NULL;
1101     struct iscsi_node_attrib *na;
1102 
1103     iscsit_inc_conn_usage_count(conn);
1104 
1105     spin_lock_bh(&cmd->dataout_timeout_lock);
1106     if (cmd->dataout_timer_flags & ISCSI_TF_STOP) {
1107         spin_unlock_bh(&cmd->dataout_timeout_lock);
1108         iscsit_dec_conn_usage_count(conn);
1109         return;
1110     }
1111     cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
1112     sess = conn->sess;
1113     na = iscsit_tpg_get_node_attrib(sess);
1114 
1115     if (!sess->sess_ops->ErrorRecoveryLevel) {
1116         pr_err("Unable to recover from DataOut timeout while"
1117             " in ERL=0, closing iSCSI connection for I_T Nexus"
1118             " %s,i,0x%6phN,%s,t,0x%02x\n",
1119             sess->sess_ops->InitiatorName, sess->isid,
1120             sess->tpg->tpg_tiqn->tiqn, (u32)sess->tpg->tpgt);
1121         goto failure;
1122     }
1123 
1124     if (++cmd->dataout_timeout_retries == na->dataout_timeout_retries) {
1125         pr_err("Command ITT: 0x%08x exceeded max retries"
1126             " for DataOUT timeout %u, closing iSCSI connection for"
1127             " I_T Nexus %s,i,0x%6phN,%s,t,0x%02x\n",
1128             cmd->init_task_tag, na->dataout_timeout_retries,
1129             sess->sess_ops->InitiatorName, sess->isid,
1130             sess->tpg->tpg_tiqn->tiqn, (u32)sess->tpg->tpgt);
1131         goto failure;
1132     }
1133 
1134     cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
1135 
1136     if (conn->sess->sess_ops->DataSequenceInOrder) {
1137         if (conn->sess->sess_ops->DataPDUInOrder) {
1138             pdu_offset = cmd->write_data_done;
1139             if ((pdu_offset + (conn->sess->sess_ops->MaxBurstLength -
1140                  cmd->next_burst_len)) > cmd->se_cmd.data_length)
1141                 pdu_length = (cmd->se_cmd.data_length -
1142                     cmd->write_data_done);
1143             else
1144                 pdu_length = (conn->sess->sess_ops->MaxBurstLength -
1145                         cmd->next_burst_len);
1146         } else {
1147             pdu_offset = cmd->seq_start_offset;
1148             pdu_length = (cmd->seq_end_offset -
1149                 cmd->seq_start_offset);
1150         }
1151     } else {
1152         if (iscsit_set_dataout_timeout_values(cmd, &pdu_offset,
1153                 &pdu_length) < 0)
1154             goto failure;
1155     }
1156 
1157     if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
1158             &r2t_offset, &r2t_length) < 0)
1159         goto failure;
1160 
1161     pr_debug("Command ITT: 0x%08x timed out waiting for"
1162         " completion of %sDataOUT Sequence Offset: %u, Length: %u\n",
1163         cmd->init_task_tag, (cmd->unsolicited_data) ? "Unsolicited " :
1164         "", r2t_offset, r2t_length);
1165 
1166     if (iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length) < 0)
1167         goto failure;
1168 
1169     iscsit_start_dataout_timer(cmd, conn);
1170     spin_unlock_bh(&cmd->dataout_timeout_lock);
1171     iscsit_dec_conn_usage_count(conn);
1172 
1173     return;
1174 
1175 failure:
1176     spin_unlock_bh(&cmd->dataout_timeout_lock);
1177     iscsit_fill_cxn_timeout_err_stats(sess);
1178     iscsit_cause_connection_reinstatement(conn, 0);
1179     iscsit_dec_conn_usage_count(conn);
1180 }
1181 
1182 void iscsit_mod_dataout_timer(struct iscsit_cmd *cmd)
1183 {
1184     struct iscsit_conn *conn = cmd->conn;
1185     struct iscsit_session *sess = conn->sess;
1186     struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1187 
1188     spin_lock_bh(&cmd->dataout_timeout_lock);
1189     if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
1190         spin_unlock_bh(&cmd->dataout_timeout_lock);
1191         return;
1192     }
1193 
1194     mod_timer(&cmd->dataout_timer,
1195         (get_jiffies_64() + na->dataout_timeout * HZ));
1196     pr_debug("Updated DataOUT timer for ITT: 0x%08x",
1197             cmd->init_task_tag);
1198     spin_unlock_bh(&cmd->dataout_timeout_lock);
1199 }
1200 
1201 void iscsit_start_dataout_timer(
1202     struct iscsit_cmd *cmd,
1203     struct iscsit_conn *conn)
1204 {
1205     struct iscsit_session *sess = conn->sess;
1206     struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1207 
1208     lockdep_assert_held(&cmd->dataout_timeout_lock);
1209 
1210     if (cmd->dataout_timer_flags & ISCSI_TF_RUNNING)
1211         return;
1212 
1213     pr_debug("Starting DataOUT timer for ITT: 0x%08x on"
1214         " CID: %hu.\n", cmd->init_task_tag, conn->cid);
1215 
1216     cmd->dataout_timer_flags &= ~ISCSI_TF_STOP;
1217     cmd->dataout_timer_flags |= ISCSI_TF_RUNNING;
1218     mod_timer(&cmd->dataout_timer, jiffies + na->dataout_timeout * HZ);
1219 }
1220 
1221 void iscsit_stop_dataout_timer(struct iscsit_cmd *cmd)
1222 {
1223     spin_lock_bh(&cmd->dataout_timeout_lock);
1224     if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
1225         spin_unlock_bh(&cmd->dataout_timeout_lock);
1226         return;
1227     }
1228     cmd->dataout_timer_flags |= ISCSI_TF_STOP;
1229     spin_unlock_bh(&cmd->dataout_timeout_lock);
1230 
1231     del_timer_sync(&cmd->dataout_timer);
1232 
1233     spin_lock_bh(&cmd->dataout_timeout_lock);
1234     cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
1235     pr_debug("Stopped DataOUT Timer for ITT: 0x%08x\n",
1236             cmd->init_task_tag);
1237     spin_unlock_bh(&cmd->dataout_timeout_lock);
1238 }
1239 EXPORT_SYMBOL(iscsit_stop_dataout_timer);