1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e48354ceSNicholas Bellinger /*******************************************************************************
3e48354ceSNicholas Bellinger  * This file contains the iSCSI Target specific Task Management functions.
4e48354ceSNicholas Bellinger  *
54c76251eSNicholas Bellinger  * (c) Copyright 2007-2013 Datera, Inc.
6e48354ceSNicholas Bellinger  *
7e48354ceSNicholas Bellinger  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8e48354ceSNicholas Bellinger  *
9e48354ceSNicholas Bellinger  ******************************************************************************/
10e48354ceSNicholas Bellinger 
11e48354ceSNicholas Bellinger #include <asm/unaligned.h>
12ba929992SBart Van Assche #include <scsi/scsi_proto.h>
13e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
14e48354ceSNicholas Bellinger #include <target/target_core_base.h>
15c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
163e1c81a9SNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
17e48354ceSNicholas Bellinger 
1867f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
19e48354ceSNicholas Bellinger #include "iscsi_target_seq_pdu_list.h"
20e48354ceSNicholas Bellinger #include "iscsi_target_datain_values.h"
21e48354ceSNicholas Bellinger #include "iscsi_target_device.h"
22e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
23e48354ceSNicholas Bellinger #include "iscsi_target_erl1.h"
24e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h"
25e48354ceSNicholas Bellinger #include "iscsi_target_tmr.h"
26e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
27e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
28e48354ceSNicholas Bellinger #include "iscsi_target.h"
29e48354ceSNicholas Bellinger 
iscsit_tmr_abort_task(struct iscsit_cmd * cmd,unsigned char * buf)30e48354ceSNicholas Bellinger u8 iscsit_tmr_abort_task(
3166cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
32e48354ceSNicholas Bellinger 	unsigned char *buf)
33e48354ceSNicholas Bellinger {
3466cd9d4eSMax Gurtovoy 	struct iscsit_cmd *ref_cmd;
35be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
36e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
37e48354ceSNicholas Bellinger 	struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
38e48354ceSNicholas Bellinger 	struct iscsi_tm *hdr = (struct iscsi_tm *) buf;
39e48354ceSNicholas Bellinger 
40e48354ceSNicholas Bellinger 	ref_cmd = iscsit_find_cmd_from_itt(conn, hdr->rtt);
41e48354ceSNicholas Bellinger 	if (!ref_cmd) {
42e48354ceSNicholas Bellinger 		pr_err("Unable to locate RefTaskTag: 0x%08x on CID:"
43e48354ceSNicholas Bellinger 			" %hu.\n", hdr->rtt, conn->cid);
4464c13330SSteve Hodgson 		return (iscsi_sna_gte(be32_to_cpu(hdr->refcmdsn), conn->sess->exp_cmd_sn) &&
45109e2381SRoland Dreier 			iscsi_sna_lte(be32_to_cpu(hdr->refcmdsn), (u32) atomic_read(&conn->sess->max_cmd_sn))) ?
46e48354ceSNicholas Bellinger 			ISCSI_TMF_RSP_COMPLETE : ISCSI_TMF_RSP_NO_TASK;
47e48354ceSNicholas Bellinger 	}
4850e5c87dSChristoph Hellwig 	if (ref_cmd->cmd_sn != be32_to_cpu(hdr->refcmdsn)) {
49e48354ceSNicholas Bellinger 		pr_err("RefCmdSN 0x%08x does not equal"
50e48354ceSNicholas Bellinger 			" task's CmdSN 0x%08x. Rejecting ABORT_TASK.\n",
51e48354ceSNicholas Bellinger 			hdr->refcmdsn, ref_cmd->cmd_sn);
52e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_REJECTED;
53e48354ceSNicholas Bellinger 	}
54e48354ceSNicholas Bellinger 
5566c7db68SChristoph Hellwig 	se_tmr->ref_task_tag		= (__force u32)hdr->rtt;
569f3eb93eSChristoph Hellwig 	tmr_req->ref_cmd		= ref_cmd;
5750e5c87dSChristoph Hellwig 	tmr_req->exp_data_sn		= be32_to_cpu(hdr->exp_datasn);
58e48354ceSNicholas Bellinger 
59e48354ceSNicholas Bellinger 	return ISCSI_TMF_RSP_COMPLETE;
60e48354ceSNicholas Bellinger }
61e48354ceSNicholas Bellinger 
62e48354ceSNicholas Bellinger /*
63e48354ceSNicholas Bellinger  *	Called from iscsit_handle_task_mgt_cmd().
64e48354ceSNicholas Bellinger  */
iscsit_tmr_task_warm_reset(struct iscsit_conn * conn,struct iscsi_tmr_req * tmr_req,unsigned char * buf)65e48354ceSNicholas Bellinger int iscsit_tmr_task_warm_reset(
66be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
67e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req,
68e48354ceSNicholas Bellinger 	unsigned char *buf)
69e48354ceSNicholas Bellinger {
70*0873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
71e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
728b1e1244SAndy Grover 
73e48354ceSNicholas Bellinger 	if (!na->tmr_warm_reset) {
74e48354ceSNicholas Bellinger 		pr_err("TMR Opcode TARGET_WARM_RESET authorization"
75e48354ceSNicholas Bellinger 			" failed for Initiator Node: %s\n",
76e48354ceSNicholas Bellinger 			sess->se_sess->se_node_acl->initiatorname);
77e48354ceSNicholas Bellinger 		return -1;
78e48354ceSNicholas Bellinger 	}
79e48354ceSNicholas Bellinger 	/*
80e48354ceSNicholas Bellinger 	 * Do the real work in transport_generic_do_tmr().
81e48354ceSNicholas Bellinger 	 */
82e48354ceSNicholas Bellinger 	return 0;
83e48354ceSNicholas Bellinger }
84e48354ceSNicholas Bellinger 
iscsit_tmr_task_cold_reset(struct iscsit_conn * conn,struct iscsi_tmr_req * tmr_req,unsigned char * buf)85e48354ceSNicholas Bellinger int iscsit_tmr_task_cold_reset(
86be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
87e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req,
88e48354ceSNicholas Bellinger 	unsigned char *buf)
89e48354ceSNicholas Bellinger {
90*0873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
91e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
92e48354ceSNicholas Bellinger 
93e48354ceSNicholas Bellinger 	if (!na->tmr_cold_reset) {
94e48354ceSNicholas Bellinger 		pr_err("TMR Opcode TARGET_COLD_RESET authorization"
95e48354ceSNicholas Bellinger 			" failed for Initiator Node: %s\n",
96e48354ceSNicholas Bellinger 			sess->se_sess->se_node_acl->initiatorname);
97e48354ceSNicholas Bellinger 		return -1;
98e48354ceSNicholas Bellinger 	}
99e48354ceSNicholas Bellinger 	/*
100e48354ceSNicholas Bellinger 	 * Do the real work in transport_generic_do_tmr().
101e48354ceSNicholas Bellinger 	 */
102e48354ceSNicholas Bellinger 	return 0;
103e48354ceSNicholas Bellinger }
104e48354ceSNicholas Bellinger 
iscsit_tmr_task_reassign(struct iscsit_cmd * cmd,unsigned char * buf)105e48354ceSNicholas Bellinger u8 iscsit_tmr_task_reassign(
10666cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
107e48354ceSNicholas Bellinger 	unsigned char *buf)
108e48354ceSNicholas Bellinger {
10966cd9d4eSMax Gurtovoy 	struct iscsit_cmd *ref_cmd = NULL;
110be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
111e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr = NULL;
112e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
113e48354ceSNicholas Bellinger 	struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
114e48354ceSNicholas Bellinger 	struct iscsi_tm *hdr = (struct iscsi_tm *) buf;
115f2d30680SHannes Reinecke 	u64 ret, ref_lun;
116e48354ceSNicholas Bellinger 
117e48354ceSNicholas Bellinger 	pr_debug("Got TASK_REASSIGN TMR ITT: 0x%08x,"
118e48354ceSNicholas Bellinger 		" RefTaskTag: 0x%08x, ExpDataSN: 0x%08x, CID: %hu\n",
119e48354ceSNicholas Bellinger 		hdr->itt, hdr->rtt, hdr->exp_datasn, conn->cid);
120e48354ceSNicholas Bellinger 
121e48354ceSNicholas Bellinger 	if (conn->sess->sess_ops->ErrorRecoveryLevel != 2) {
122e48354ceSNicholas Bellinger 		pr_err("TMR TASK_REASSIGN not supported in ERL<2,"
123e48354ceSNicholas Bellinger 				" ignoring request.\n");
124e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_NOT_SUPPORTED;
125e48354ceSNicholas Bellinger 	}
126e48354ceSNicholas Bellinger 
127e48354ceSNicholas Bellinger 	ret = iscsit_find_cmd_for_recovery(conn->sess, &ref_cmd, &cr, hdr->rtt);
128e48354ceSNicholas Bellinger 	if (ret == -2) {
129e48354ceSNicholas Bellinger 		pr_err("Command ITT: 0x%08x is still alligent to CID:"
130e48354ceSNicholas Bellinger 			" %hu\n", ref_cmd->init_task_tag, cr->cid);
131e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_TASK_ALLEGIANT;
132e48354ceSNicholas Bellinger 	} else if (ret == -1) {
133e48354ceSNicholas Bellinger 		pr_err("Unable to locate RefTaskTag: 0x%08x in"
134e48354ceSNicholas Bellinger 			" connection recovery command list.\n", hdr->rtt);
135e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_NO_TASK;
136e48354ceSNicholas Bellinger 	}
137e48354ceSNicholas Bellinger 	/*
138e48354ceSNicholas Bellinger 	 * Temporary check to prevent connection recovery for
1391c417f39SNicholas Bellinger 	 * connections with a differing Max*DataSegmentLength.
140e48354ceSNicholas Bellinger 	 */
141e48354ceSNicholas Bellinger 	if (cr->maxrecvdatasegmentlength !=
142e48354ceSNicholas Bellinger 	    conn->conn_ops->MaxRecvDataSegmentLength) {
143e48354ceSNicholas Bellinger 		pr_err("Unable to perform connection recovery for"
144e48354ceSNicholas Bellinger 			" differing MaxRecvDataSegmentLength, rejecting"
145e48354ceSNicholas Bellinger 			" TMR TASK_REASSIGN.\n");
146e48354ceSNicholas Bellinger 		return ISCSI_TMF_RSP_REJECTED;
147e48354ceSNicholas Bellinger 	}
1481c417f39SNicholas Bellinger 	if (cr->maxxmitdatasegmentlength !=
1491c417f39SNicholas Bellinger 	    conn->conn_ops->MaxXmitDataSegmentLength) {
1501c417f39SNicholas Bellinger 		pr_err("Unable to perform connection recovery for"
1511c417f39SNicholas Bellinger 			" differing MaxXmitDataSegmentLength, rejecting"
1521c417f39SNicholas Bellinger 			" TMR TASK_REASSIGN.\n");
1531c417f39SNicholas Bellinger 		return ISCSI_TMF_RSP_REJECTED;
1541c417f39SNicholas Bellinger 	}
155e48354ceSNicholas Bellinger 
1569f3eb93eSChristoph Hellwig 	ref_lun = scsilun_to_int(&hdr->lun);
1579f3eb93eSChristoph Hellwig 	if (ref_lun != ref_cmd->se_cmd.orig_fe_lun) {
1589f3eb93eSChristoph Hellwig 		pr_err("Unable to perform connection recovery for"
159f2d30680SHannes Reinecke 			" differing ref_lun: %llu ref_cmd orig_fe_lun: %llu\n",
1609f3eb93eSChristoph Hellwig 			ref_lun, ref_cmd->se_cmd.orig_fe_lun);
1619f3eb93eSChristoph Hellwig 		return ISCSI_TMF_RSP_REJECTED;
1629f3eb93eSChristoph Hellwig 	}
1639f3eb93eSChristoph Hellwig 
16466c7db68SChristoph Hellwig 	se_tmr->ref_task_tag		= (__force u32)hdr->rtt;
1659f3eb93eSChristoph Hellwig 	tmr_req->ref_cmd		= ref_cmd;
16650e5c87dSChristoph Hellwig 	tmr_req->exp_data_sn		= be32_to_cpu(hdr->exp_datasn);
167e48354ceSNicholas Bellinger 	tmr_req->conn_recovery		= cr;
168e48354ceSNicholas Bellinger 	tmr_req->task_reassign		= 1;
169e48354ceSNicholas Bellinger 	/*
170e48354ceSNicholas Bellinger 	 * Command can now be reassigned to a new connection.
171e48354ceSNicholas Bellinger 	 * The task management response must be sent before the
172e48354ceSNicholas Bellinger 	 * reassignment actually happens.  See iscsi_tmr_post_handler().
173e48354ceSNicholas Bellinger 	 */
174e48354ceSNicholas Bellinger 	return ISCSI_TMF_RSP_COMPLETE;
175e48354ceSNicholas Bellinger }
176e48354ceSNicholas Bellinger 
iscsit_task_reassign_remove_cmd(struct iscsit_cmd * cmd,struct iscsi_conn_recovery * cr,struct iscsit_session * sess)177e48354ceSNicholas Bellinger static void iscsit_task_reassign_remove_cmd(
17866cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
179e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr,
180*0873fe44SMax Gurtovoy 	struct iscsit_session *sess)
181e48354ceSNicholas Bellinger {
182e48354ceSNicholas Bellinger 	int ret;
183e48354ceSNicholas Bellinger 
184e48354ceSNicholas Bellinger 	spin_lock(&cr->conn_recovery_cmd_lock);
185e48354ceSNicholas Bellinger 	ret = iscsit_remove_cmd_from_connection_recovery(cmd, sess);
186e48354ceSNicholas Bellinger 	spin_unlock(&cr->conn_recovery_cmd_lock);
187e48354ceSNicholas Bellinger 	if (!ret) {
188e48354ceSNicholas Bellinger 		pr_debug("iSCSI connection recovery successful for CID:"
189e48354ceSNicholas Bellinger 			" %hu on SID: %u\n", cr->cid, sess->sid);
190e48354ceSNicholas Bellinger 		iscsit_remove_active_connection_recovery_entry(cr, sess);
191e48354ceSNicholas Bellinger 	}
192e48354ceSNicholas Bellinger }
193e48354ceSNicholas Bellinger 
iscsit_task_reassign_complete_nop_out(struct iscsi_tmr_req * tmr_req,struct iscsit_conn * conn)194e48354ceSNicholas Bellinger static int iscsit_task_reassign_complete_nop_out(
195e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req,
196be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
197e48354ceSNicholas Bellinger {
19866cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd = tmr_req->ref_cmd;
199e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr;
200e48354ceSNicholas Bellinger 
201e48354ceSNicholas Bellinger 	if (!cmd->cr) {
202e48354ceSNicholas Bellinger 		pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
203e48354ceSNicholas Bellinger 			" is NULL!\n", cmd->init_task_tag);
204e48354ceSNicholas Bellinger 		return -1;
205e48354ceSNicholas Bellinger 	}
206e48354ceSNicholas Bellinger 	cr = cmd->cr;
207e48354ceSNicholas Bellinger 
208e48354ceSNicholas Bellinger 	/*
209e48354ceSNicholas Bellinger 	 * Reset the StatSN so a new one for this commands new connection
210e48354ceSNicholas Bellinger 	 * will be assigned.
211e48354ceSNicholas Bellinger 	 * Reset the ExpStatSN as well so we may receive Status SNACKs.
212e48354ceSNicholas Bellinger 	 */
213e48354ceSNicholas Bellinger 	cmd->stat_sn = cmd->exp_stat_sn = 0;
214e48354ceSNicholas Bellinger 
215e48354ceSNicholas Bellinger 	iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
216e48354ceSNicholas Bellinger 
217e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2182fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
219e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
220e48354ceSNicholas Bellinger 
221e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_NOPIN;
222e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
223e48354ceSNicholas Bellinger 	return 0;
224e48354ceSNicholas Bellinger }
225e48354ceSNicholas Bellinger 
iscsit_task_reassign_complete_write(struct iscsit_cmd * cmd,struct iscsi_tmr_req * tmr_req)226e48354ceSNicholas Bellinger static int iscsit_task_reassign_complete_write(
22766cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
228e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req)
229e48354ceSNicholas Bellinger {
230e48354ceSNicholas Bellinger 	int no_build_r2ts = 0;
231e48354ceSNicholas Bellinger 	u32 length = 0, offset = 0;
232be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
233e48354ceSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
234e48354ceSNicholas Bellinger 	/*
235e48354ceSNicholas Bellinger 	 * The Initiator must not send a R2T SNACK with a Begrun less than
236e48354ceSNicholas Bellinger 	 * the TMR TASK_REASSIGN's ExpDataSN.
237e48354ceSNicholas Bellinger 	 */
238e48354ceSNicholas Bellinger 	if (!tmr_req->exp_data_sn) {
239e48354ceSNicholas Bellinger 		cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
240e48354ceSNicholas Bellinger 		cmd->acked_data_sn = 0;
241e48354ceSNicholas Bellinger 	} else {
242e48354ceSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
243e48354ceSNicholas Bellinger 		cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
244e48354ceSNicholas Bellinger 	}
245e48354ceSNicholas Bellinger 
246e48354ceSNicholas Bellinger 	/*
247e48354ceSNicholas Bellinger 	 * The TMR TASK_REASSIGN's ExpDataSN contains the next R2TSN the
248e48354ceSNicholas Bellinger 	 * Initiator is expecting.  The Target controls all WRITE operations
249e48354ceSNicholas Bellinger 	 * so if we have received all DataOUT we can safety ignore Initiator.
250e48354ceSNicholas Bellinger 	 */
251e48354ceSNicholas Bellinger 	if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
2527d680f3bSChristoph Hellwig 		if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
253e48354ceSNicholas Bellinger 			pr_debug("WRITE ITT: 0x%08x: t_state: %d"
254e48354ceSNicholas Bellinger 				" never sent to transport\n",
255e48354ceSNicholas Bellinger 				cmd->init_task_tag, cmd->se_cmd.t_state);
25667441b68SChristoph Hellwig 			target_execute_cmd(se_cmd);
25767441b68SChristoph Hellwig 			return 0;
258e48354ceSNicholas Bellinger 		}
259e48354ceSNicholas Bellinger 
260e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_SEND_STATUS;
261e48354ceSNicholas Bellinger 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
262e48354ceSNicholas Bellinger 		return 0;
263e48354ceSNicholas Bellinger 	}
264e48354ceSNicholas Bellinger 
265e48354ceSNicholas Bellinger 	/*
266e48354ceSNicholas Bellinger 	 * Special case to deal with DataSequenceInOrder=No and Non-Immeidate
267e48354ceSNicholas Bellinger 	 * Unsolicited DataOut.
268e48354ceSNicholas Bellinger 	 */
269e48354ceSNicholas Bellinger 	if (cmd->unsolicited_data) {
270e48354ceSNicholas Bellinger 		cmd->unsolicited_data = 0;
271e48354ceSNicholas Bellinger 
272e48354ceSNicholas Bellinger 		offset = cmd->next_burst_len = cmd->write_data_done;
273e48354ceSNicholas Bellinger 
274e48354ceSNicholas Bellinger 		if ((conn->sess->sess_ops->FirstBurstLength - offset) >=
275ebf1d95cSAndy Grover 		     cmd->se_cmd.data_length) {
276e48354ceSNicholas Bellinger 			no_build_r2ts = 1;
277ebf1d95cSAndy Grover 			length = (cmd->se_cmd.data_length - offset);
278e48354ceSNicholas Bellinger 		} else
279e48354ceSNicholas Bellinger 			length = (conn->sess->sess_ops->FirstBurstLength - offset);
280e48354ceSNicholas Bellinger 
281e48354ceSNicholas Bellinger 		spin_lock_bh(&cmd->r2t_lock);
282e48354ceSNicholas Bellinger 		if (iscsit_add_r2t_to_list(cmd, offset, length, 0, 0) < 0) {
283e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->r2t_lock);
284e48354ceSNicholas Bellinger 			return -1;
285e48354ceSNicholas Bellinger 		}
286e48354ceSNicholas Bellinger 		cmd->outstanding_r2ts++;
287e48354ceSNicholas Bellinger 		spin_unlock_bh(&cmd->r2t_lock);
288e48354ceSNicholas Bellinger 
289e48354ceSNicholas Bellinger 		if (no_build_r2ts)
290e48354ceSNicholas Bellinger 			return 0;
291e48354ceSNicholas Bellinger 	}
292e48354ceSNicholas Bellinger 	/*
293e48354ceSNicholas Bellinger 	 * iscsit_build_r2ts_for_cmd() can handle the rest from here.
294e48354ceSNicholas Bellinger 	 */
2953e1c81a9SNicholas Bellinger 	return conn->conn_transport->iscsit_get_dataout(conn, cmd, true);
296e48354ceSNicholas Bellinger }
297e48354ceSNicholas Bellinger 
iscsit_task_reassign_complete_read(struct iscsit_cmd * cmd,struct iscsi_tmr_req * tmr_req)298e48354ceSNicholas Bellinger static int iscsit_task_reassign_complete_read(
29966cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
300e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req)
301e48354ceSNicholas Bellinger {
302be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
303e48354ceSNicholas Bellinger 	struct iscsi_datain_req *dr;
304e48354ceSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
305e48354ceSNicholas Bellinger 	/*
306e48354ceSNicholas Bellinger 	 * The Initiator must not send a Data SNACK with a BegRun less than
307e48354ceSNicholas Bellinger 	 * the TMR TASK_REASSIGN's ExpDataSN.
308e48354ceSNicholas Bellinger 	 */
309e48354ceSNicholas Bellinger 	if (!tmr_req->exp_data_sn) {
310e48354ceSNicholas Bellinger 		cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
311e48354ceSNicholas Bellinger 		cmd->acked_data_sn = 0;
312e48354ceSNicholas Bellinger 	} else {
313e48354ceSNicholas Bellinger 		cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
314e48354ceSNicholas Bellinger 		cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
315e48354ceSNicholas Bellinger 	}
316e48354ceSNicholas Bellinger 
3177d680f3bSChristoph Hellwig 	if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
318e48354ceSNicholas Bellinger 		pr_debug("READ ITT: 0x%08x: t_state: %d never sent to"
319e48354ceSNicholas Bellinger 			" transport\n", cmd->init_task_tag,
320e48354ceSNicholas Bellinger 			cmd->se_cmd.t_state);
321acf3ecc4SChristoph Hellwig 		transport_handle_cdb_direct(se_cmd);
322e48354ceSNicholas Bellinger 		return 0;
323e48354ceSNicholas Bellinger 	}
324e48354ceSNicholas Bellinger 
3257d680f3bSChristoph Hellwig 	if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
326e48354ceSNicholas Bellinger 		pr_err("READ ITT: 0x%08x: t_state: %d, never returned"
327e48354ceSNicholas Bellinger 			" from transport\n", cmd->init_task_tag,
328e48354ceSNicholas Bellinger 			cmd->se_cmd.t_state);
329e48354ceSNicholas Bellinger 		return -1;
330e48354ceSNicholas Bellinger 	}
331e48354ceSNicholas Bellinger 
332e48354ceSNicholas Bellinger 	dr = iscsit_allocate_datain_req();
333e48354ceSNicholas Bellinger 	if (!dr)
334e48354ceSNicholas Bellinger 		return -1;
335e48354ceSNicholas Bellinger 	/*
336e48354ceSNicholas Bellinger 	 * The TMR TASK_REASSIGN's ExpDataSN contains the next DataSN the
337e48354ceSNicholas Bellinger 	 * Initiator is expecting.
338e48354ceSNicholas Bellinger 	 */
339e48354ceSNicholas Bellinger 	dr->data_sn = dr->begrun = tmr_req->exp_data_sn;
340e48354ceSNicholas Bellinger 	dr->runlength = 0;
341e48354ceSNicholas Bellinger 	dr->generate_recovery_values = 1;
342e48354ceSNicholas Bellinger 	dr->recovery = DATAIN_CONNECTION_RECOVERY;
343e48354ceSNicholas Bellinger 
344e48354ceSNicholas Bellinger 	iscsit_attach_datain_req(cmd, dr);
345e48354ceSNicholas Bellinger 
346e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_DATAIN;
347e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
348e48354ceSNicholas Bellinger 	return 0;
349e48354ceSNicholas Bellinger }
350e48354ceSNicholas Bellinger 
iscsit_task_reassign_complete_none(struct iscsit_cmd * cmd,struct iscsi_tmr_req * tmr_req)351e48354ceSNicholas Bellinger static int iscsit_task_reassign_complete_none(
35266cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
353e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req)
354e48354ceSNicholas Bellinger {
355be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
356e48354ceSNicholas Bellinger 
357e48354ceSNicholas Bellinger 	cmd->i_state = ISTATE_SEND_STATUS;
358e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
359e48354ceSNicholas Bellinger 	return 0;
360e48354ceSNicholas Bellinger }
361e48354ceSNicholas Bellinger 
iscsit_task_reassign_complete_scsi_cmnd(struct iscsi_tmr_req * tmr_req,struct iscsit_conn * conn)362e48354ceSNicholas Bellinger static int iscsit_task_reassign_complete_scsi_cmnd(
363e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req,
364be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
365e48354ceSNicholas Bellinger {
36666cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd = tmr_req->ref_cmd;
367e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr;
368e48354ceSNicholas Bellinger 
369e48354ceSNicholas Bellinger 	if (!cmd->cr) {
370e48354ceSNicholas Bellinger 		pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
371e48354ceSNicholas Bellinger 			" is NULL!\n", cmd->init_task_tag);
372e48354ceSNicholas Bellinger 		return -1;
373e48354ceSNicholas Bellinger 	}
374e48354ceSNicholas Bellinger 	cr = cmd->cr;
375e48354ceSNicholas Bellinger 
376e48354ceSNicholas Bellinger 	/*
377e48354ceSNicholas Bellinger 	 * Reset the StatSN so a new one for this commands new connection
378e48354ceSNicholas Bellinger 	 * will be assigned.
379e48354ceSNicholas Bellinger 	 * Reset the ExpStatSN as well so we may receive Status SNACKs.
380e48354ceSNicholas Bellinger 	 */
381e48354ceSNicholas Bellinger 	cmd->stat_sn = cmd->exp_stat_sn = 0;
382e48354ceSNicholas Bellinger 
383e48354ceSNicholas Bellinger 	iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
384e48354ceSNicholas Bellinger 
385e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
3862fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
387e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
388e48354ceSNicholas Bellinger 
3899f3eb93eSChristoph Hellwig 	if (cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
390e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_SEND_STATUS;
391e48354ceSNicholas Bellinger 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
392e48354ceSNicholas Bellinger 		return 0;
393e48354ceSNicholas Bellinger 	}
394e48354ceSNicholas Bellinger 
395e48354ceSNicholas Bellinger 	switch (cmd->data_direction) {
396e48354ceSNicholas Bellinger 	case DMA_TO_DEVICE:
397e48354ceSNicholas Bellinger 		return iscsit_task_reassign_complete_write(cmd, tmr_req);
398e48354ceSNicholas Bellinger 	case DMA_FROM_DEVICE:
399e48354ceSNicholas Bellinger 		return iscsit_task_reassign_complete_read(cmd, tmr_req);
400e48354ceSNicholas Bellinger 	case DMA_NONE:
401e48354ceSNicholas Bellinger 		return iscsit_task_reassign_complete_none(cmd, tmr_req);
402e48354ceSNicholas Bellinger 	default:
403e48354ceSNicholas Bellinger 		pr_err("Unknown cmd->data_direction: 0x%02x\n",
404e48354ceSNicholas Bellinger 				cmd->data_direction);
405e48354ceSNicholas Bellinger 		return -1;
406e48354ceSNicholas Bellinger 	}
407e48354ceSNicholas Bellinger 
408e48354ceSNicholas Bellinger 	return 0;
409e48354ceSNicholas Bellinger }
410e48354ceSNicholas Bellinger 
iscsit_task_reassign_complete(struct iscsi_tmr_req * tmr_req,struct iscsit_conn * conn)411e48354ceSNicholas Bellinger static int iscsit_task_reassign_complete(
412e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req,
413be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
414e48354ceSNicholas Bellinger {
41566cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
416e48354ceSNicholas Bellinger 	int ret = 0;
417e48354ceSNicholas Bellinger 
4189f3eb93eSChristoph Hellwig 	if (!tmr_req->ref_cmd) {
41966cd9d4eSMax Gurtovoy 		pr_err("TMR Request is missing a RefCmd struct iscsit_cmd.\n");
420e48354ceSNicholas Bellinger 		return -1;
421e48354ceSNicholas Bellinger 	}
4229f3eb93eSChristoph Hellwig 	cmd = tmr_req->ref_cmd;
423e48354ceSNicholas Bellinger 
424e48354ceSNicholas Bellinger 	cmd->conn = conn;
425e48354ceSNicholas Bellinger 
426e48354ceSNicholas Bellinger 	switch (cmd->iscsi_opcode) {
427e48354ceSNicholas Bellinger 	case ISCSI_OP_NOOP_OUT:
428e48354ceSNicholas Bellinger 		ret = iscsit_task_reassign_complete_nop_out(tmr_req, conn);
429e48354ceSNicholas Bellinger 		break;
430e48354ceSNicholas Bellinger 	case ISCSI_OP_SCSI_CMD:
431e48354ceSNicholas Bellinger 		ret = iscsit_task_reassign_complete_scsi_cmnd(tmr_req, conn);
432e48354ceSNicholas Bellinger 		break;
433e48354ceSNicholas Bellinger 	default:
434e48354ceSNicholas Bellinger 		 pr_err("Illegal iSCSI Opcode 0x%02x during"
43553c561dcSBart Van Assche 			" command reallegiance\n", cmd->iscsi_opcode);
436e48354ceSNicholas Bellinger 		return -1;
437e48354ceSNicholas Bellinger 	}
438e48354ceSNicholas Bellinger 
439e48354ceSNicholas Bellinger 	if (ret != 0)
440e48354ceSNicholas Bellinger 		return ret;
441e48354ceSNicholas Bellinger 
44253c561dcSBart Van Assche 	pr_debug("Completed connection reallegiance for Opcode: 0x%02x,"
443e48354ceSNicholas Bellinger 		" ITT: 0x%08x to CID: %hu.\n", cmd->iscsi_opcode,
444e48354ceSNicholas Bellinger 			cmd->init_task_tag, conn->cid);
445e48354ceSNicholas Bellinger 
446e48354ceSNicholas Bellinger 	return 0;
447e48354ceSNicholas Bellinger }
448e48354ceSNicholas Bellinger 
449e48354ceSNicholas Bellinger /*
450e48354ceSNicholas Bellinger  *	Handles special after-the-fact actions related to TMRs.
451e48354ceSNicholas Bellinger  *	Right now the only one that its really needed for is
452e48354ceSNicholas Bellinger  *	connection recovery releated TASK_REASSIGN.
453e48354ceSNicholas Bellinger  */
iscsit_tmr_post_handler(struct iscsit_cmd * cmd,struct iscsit_conn * conn)454be36d683SMax Gurtovoy int iscsit_tmr_post_handler(struct iscsit_cmd *cmd, struct iscsit_conn *conn)
455e48354ceSNicholas Bellinger {
456e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
457e48354ceSNicholas Bellinger 	struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
458e48354ceSNicholas Bellinger 
459e48354ceSNicholas Bellinger 	if (tmr_req->task_reassign &&
460e48354ceSNicholas Bellinger 	   (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
461e48354ceSNicholas Bellinger 		return iscsit_task_reassign_complete(tmr_req, conn);
462e48354ceSNicholas Bellinger 
463e48354ceSNicholas Bellinger 	return 0;
464e48354ceSNicholas Bellinger }
4652ec5a8c1SNicholas Bellinger EXPORT_SYMBOL(iscsit_tmr_post_handler);
466e48354ceSNicholas Bellinger 
467e48354ceSNicholas Bellinger /*
468e48354ceSNicholas Bellinger  *	Nothing to do here, but leave it for good measure. :-)
469e48354ceSNicholas Bellinger  */
iscsit_task_reassign_prepare_read(struct iscsi_tmr_req * tmr_req,struct iscsit_conn * conn)470fceb5bc7SChristoph Hellwig static int iscsit_task_reassign_prepare_read(
471e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req,
472be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
473e48354ceSNicholas Bellinger {
474e48354ceSNicholas Bellinger 	return 0;
475e48354ceSNicholas Bellinger }
476e48354ceSNicholas Bellinger 
iscsit_task_reassign_prepare_unsolicited_dataout(struct iscsit_cmd * cmd,struct iscsit_conn * conn)477e48354ceSNicholas Bellinger static void iscsit_task_reassign_prepare_unsolicited_dataout(
47866cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
479be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
480e48354ceSNicholas Bellinger {
481e48354ceSNicholas Bellinger 	int i, j;
482e48354ceSNicholas Bellinger 	struct iscsi_pdu *pdu = NULL;
483e48354ceSNicholas Bellinger 	struct iscsi_seq *seq = NULL;
484e48354ceSNicholas Bellinger 
485e48354ceSNicholas Bellinger 	if (conn->sess->sess_ops->DataSequenceInOrder) {
486e48354ceSNicholas Bellinger 		cmd->data_sn = 0;
487e48354ceSNicholas Bellinger 
488e48354ceSNicholas Bellinger 		if (cmd->immediate_data)
489e48354ceSNicholas Bellinger 			cmd->r2t_offset += (cmd->first_burst_len -
490e48354ceSNicholas Bellinger 				cmd->seq_start_offset);
491e48354ceSNicholas Bellinger 
492e48354ceSNicholas Bellinger 		if (conn->sess->sess_ops->DataPDUInOrder) {
493e48354ceSNicholas Bellinger 			cmd->write_data_done -= (cmd->immediate_data) ?
494e48354ceSNicholas Bellinger 						(cmd->first_burst_len -
495e48354ceSNicholas Bellinger 						 cmd->seq_start_offset) :
496e48354ceSNicholas Bellinger 						 cmd->first_burst_len;
497e48354ceSNicholas Bellinger 			cmd->first_burst_len = 0;
498e48354ceSNicholas Bellinger 			return;
499e48354ceSNicholas Bellinger 		}
500e48354ceSNicholas Bellinger 
501e48354ceSNicholas Bellinger 		for (i = 0; i < cmd->pdu_count; i++) {
502e48354ceSNicholas Bellinger 			pdu = &cmd->pdu_list[i];
503e48354ceSNicholas Bellinger 
504e48354ceSNicholas Bellinger 			if (pdu->status != ISCSI_PDU_RECEIVED_OK)
505e48354ceSNicholas Bellinger 				continue;
506e48354ceSNicholas Bellinger 
507e48354ceSNicholas Bellinger 			if ((pdu->offset >= cmd->seq_start_offset) &&
508e48354ceSNicholas Bellinger 			   ((pdu->offset + pdu->length) <=
509e48354ceSNicholas Bellinger 			     cmd->seq_end_offset)) {
510e48354ceSNicholas Bellinger 				cmd->first_burst_len -= pdu->length;
511e48354ceSNicholas Bellinger 				cmd->write_data_done -= pdu->length;
512e48354ceSNicholas Bellinger 				pdu->status = ISCSI_PDU_NOT_RECEIVED;
513e48354ceSNicholas Bellinger 			}
514e48354ceSNicholas Bellinger 		}
515e48354ceSNicholas Bellinger 	} else {
516e48354ceSNicholas Bellinger 		for (i = 0; i < cmd->seq_count; i++) {
517e48354ceSNicholas Bellinger 			seq = &cmd->seq_list[i];
518e48354ceSNicholas Bellinger 
519e48354ceSNicholas Bellinger 			if (seq->type != SEQTYPE_UNSOLICITED)
520e48354ceSNicholas Bellinger 				continue;
521e48354ceSNicholas Bellinger 
522e48354ceSNicholas Bellinger 			cmd->write_data_done -=
523e48354ceSNicholas Bellinger 					(seq->offset - seq->orig_offset);
524e48354ceSNicholas Bellinger 			cmd->first_burst_len = 0;
525e48354ceSNicholas Bellinger 			seq->data_sn = 0;
526e48354ceSNicholas Bellinger 			seq->offset = seq->orig_offset;
527e48354ceSNicholas Bellinger 			seq->next_burst_len = 0;
528e48354ceSNicholas Bellinger 			seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
529e48354ceSNicholas Bellinger 
530e48354ceSNicholas Bellinger 			if (conn->sess->sess_ops->DataPDUInOrder)
531e48354ceSNicholas Bellinger 				continue;
532e48354ceSNicholas Bellinger 
533e48354ceSNicholas Bellinger 			for (j = 0; j < seq->pdu_count; j++) {
534e48354ceSNicholas Bellinger 				pdu = &cmd->pdu_list[j+seq->pdu_start];
535e48354ceSNicholas Bellinger 
536e48354ceSNicholas Bellinger 				if (pdu->status != ISCSI_PDU_RECEIVED_OK)
537e48354ceSNicholas Bellinger 					continue;
538e48354ceSNicholas Bellinger 
539e48354ceSNicholas Bellinger 				pdu->status = ISCSI_PDU_NOT_RECEIVED;
540e48354ceSNicholas Bellinger 			}
541e48354ceSNicholas Bellinger 		}
542e48354ceSNicholas Bellinger 	}
543e48354ceSNicholas Bellinger }
544e48354ceSNicholas Bellinger 
iscsit_task_reassign_prepare_write(struct iscsi_tmr_req * tmr_req,struct iscsit_conn * conn)545fceb5bc7SChristoph Hellwig static int iscsit_task_reassign_prepare_write(
546e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req,
547be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
548e48354ceSNicholas Bellinger {
54966cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd = tmr_req->ref_cmd;
550e48354ceSNicholas Bellinger 	struct iscsi_pdu *pdu = NULL;
551e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t = NULL, *r2t_tmp;
552e48354ceSNicholas Bellinger 	int first_incomplete_r2t = 1, i = 0;
553e48354ceSNicholas Bellinger 
554e48354ceSNicholas Bellinger 	/*
555e48354ceSNicholas Bellinger 	 * The command was in the process of receiving Unsolicited DataOUT when
556e48354ceSNicholas Bellinger 	 * the connection failed.
557e48354ceSNicholas Bellinger 	 */
558e48354ceSNicholas Bellinger 	if (cmd->unsolicited_data)
559e48354ceSNicholas Bellinger 		iscsit_task_reassign_prepare_unsolicited_dataout(cmd, conn);
560e48354ceSNicholas Bellinger 
561e48354ceSNicholas Bellinger 	/*
562e48354ceSNicholas Bellinger 	 * The Initiator is requesting R2Ts starting from zero,  skip
563e48354ceSNicholas Bellinger 	 * checking acknowledged R2Ts and start checking struct iscsi_r2ts
564e48354ceSNicholas Bellinger 	 * greater than zero.
565e48354ceSNicholas Bellinger 	 */
566e48354ceSNicholas Bellinger 	if (!tmr_req->exp_data_sn)
567e48354ceSNicholas Bellinger 		goto drop_unacknowledged_r2ts;
568e48354ceSNicholas Bellinger 
569e48354ceSNicholas Bellinger 	/*
570e48354ceSNicholas Bellinger 	 * We now check that the PDUs in DataOUT sequences below
571e48354ceSNicholas Bellinger 	 * the TMR TASK_REASSIGN ExpDataSN (R2TSN the Initiator is
572e48354ceSNicholas Bellinger 	 * expecting next) have all the DataOUT they require to complete
573e48354ceSNicholas Bellinger 	 * the DataOUT sequence.  First scan from R2TSN 0 to TMR
574e48354ceSNicholas Bellinger 	 * TASK_REASSIGN ExpDataSN-1.
575e48354ceSNicholas Bellinger 	 *
576e48354ceSNicholas Bellinger 	 * If we have not received all DataOUT in question,  we must
577e48354ceSNicholas Bellinger 	 * make sure to make the appropriate changes to values in
57866cd9d4eSMax Gurtovoy 	 * struct iscsit_cmd (and elsewhere depending on session parameters)
579e48354ceSNicholas Bellinger 	 * so iscsit_build_r2ts_for_cmd() in iscsit_task_reassign_complete_write()
580e48354ceSNicholas Bellinger 	 * will resend a new R2T for the DataOUT sequences in question.
581e48354ceSNicholas Bellinger 	 */
582e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
583e48354ceSNicholas Bellinger 	if (list_empty(&cmd->cmd_r2t_list)) {
584e48354ceSNicholas Bellinger 		spin_unlock_bh(&cmd->r2t_lock);
585e48354ceSNicholas Bellinger 		return -1;
586e48354ceSNicholas Bellinger 	}
587e48354ceSNicholas Bellinger 
588e48354ceSNicholas Bellinger 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
589e48354ceSNicholas Bellinger 
590e48354ceSNicholas Bellinger 		if (r2t->r2t_sn >= tmr_req->exp_data_sn)
591e48354ceSNicholas Bellinger 			continue;
592e48354ceSNicholas Bellinger 		/*
593e48354ceSNicholas Bellinger 		 * Safely ignore Recovery R2Ts and R2Ts that have completed
594e48354ceSNicholas Bellinger 		 * DataOUT sequences.
595e48354ceSNicholas Bellinger 		 */
596e48354ceSNicholas Bellinger 		if (r2t->seq_complete)
597e48354ceSNicholas Bellinger 			continue;
598e48354ceSNicholas Bellinger 
599e48354ceSNicholas Bellinger 		if (r2t->recovery_r2t)
600e48354ceSNicholas Bellinger 			continue;
601e48354ceSNicholas Bellinger 
602e48354ceSNicholas Bellinger 		/*
603e48354ceSNicholas Bellinger 		 *                 DataSequenceInOrder=Yes:
604e48354ceSNicholas Bellinger 		 *
605e48354ceSNicholas Bellinger 		 * Taking into account the iSCSI implementation requirement of
606e48354ceSNicholas Bellinger 		 * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
607e48354ceSNicholas Bellinger 		 * DataSequenceInOrder=Yes, we must take into consideration
608e48354ceSNicholas Bellinger 		 * the following:
609e48354ceSNicholas Bellinger 		 *
610e48354ceSNicholas Bellinger 		 *                  DataSequenceInOrder=No:
611e48354ceSNicholas Bellinger 		 *
612e48354ceSNicholas Bellinger 		 * Taking into account that the Initiator controls the (possibly
613e48354ceSNicholas Bellinger 		 * random) PDU Order in (possibly random) Sequence Order of
614e48354ceSNicholas Bellinger 		 * DataOUT the target requests with R2Ts,  we must take into
615e48354ceSNicholas Bellinger 		 * consideration the following:
616e48354ceSNicholas Bellinger 		 *
617e48354ceSNicholas Bellinger 		 *      DataPDUInOrder=Yes for DataSequenceInOrder=[Yes,No]:
618e48354ceSNicholas Bellinger 		 *
619e48354ceSNicholas Bellinger 		 * While processing non-complete R2T DataOUT sequence requests
620e48354ceSNicholas Bellinger 		 * the Target will re-request only the total sequence length
621e48354ceSNicholas Bellinger 		 * minus current received offset.  This is because we must
622e48354ceSNicholas Bellinger 		 * assume the initiator will continue sending DataOUT from the
623e48354ceSNicholas Bellinger 		 * last PDU before the connection failed.
624e48354ceSNicholas Bellinger 		 *
625e48354ceSNicholas Bellinger 		 *      DataPDUInOrder=No for DataSequenceInOrder=[Yes,No]:
626e48354ceSNicholas Bellinger 		 *
627e48354ceSNicholas Bellinger 		 * While processing non-complete R2T DataOUT sequence requests
628e48354ceSNicholas Bellinger 		 * the Target will re-request the entire DataOUT sequence if
629e48354ceSNicholas Bellinger 		 * any single PDU is missing from the sequence.  This is because
630e48354ceSNicholas Bellinger 		 * we have no logical method to determine the next PDU offset,
631e48354ceSNicholas Bellinger 		 * and we must assume the Initiator will be sending any random
632e48354ceSNicholas Bellinger 		 * PDU offset in the current sequence after TASK_REASSIGN
633e48354ceSNicholas Bellinger 		 * has completed.
634e48354ceSNicholas Bellinger 		 */
635e48354ceSNicholas Bellinger 		if (conn->sess->sess_ops->DataSequenceInOrder) {
636e48354ceSNicholas Bellinger 			if (!first_incomplete_r2t) {
637e48354ceSNicholas Bellinger 				cmd->r2t_offset -= r2t->xfer_len;
638e48354ceSNicholas Bellinger 				goto next;
639e48354ceSNicholas Bellinger 			}
640e48354ceSNicholas Bellinger 
641e48354ceSNicholas Bellinger 			if (conn->sess->sess_ops->DataPDUInOrder) {
642e48354ceSNicholas Bellinger 				cmd->data_sn = 0;
643e48354ceSNicholas Bellinger 				cmd->r2t_offset -= (r2t->xfer_len -
644e48354ceSNicholas Bellinger 					cmd->next_burst_len);
645e48354ceSNicholas Bellinger 				first_incomplete_r2t = 0;
646e48354ceSNicholas Bellinger 				goto next;
647e48354ceSNicholas Bellinger 			}
648e48354ceSNicholas Bellinger 
649e48354ceSNicholas Bellinger 			cmd->data_sn = 0;
650e48354ceSNicholas Bellinger 			cmd->r2t_offset -= r2t->xfer_len;
651e48354ceSNicholas Bellinger 
652e48354ceSNicholas Bellinger 			for (i = 0; i < cmd->pdu_count; i++) {
653e48354ceSNicholas Bellinger 				pdu = &cmd->pdu_list[i];
654e48354ceSNicholas Bellinger 
655e48354ceSNicholas Bellinger 				if (pdu->status != ISCSI_PDU_RECEIVED_OK)
656e48354ceSNicholas Bellinger 					continue;
657e48354ceSNicholas Bellinger 
658e48354ceSNicholas Bellinger 				if ((pdu->offset >= r2t->offset) &&
659e48354ceSNicholas Bellinger 				    (pdu->offset < (r2t->offset +
660e48354ceSNicholas Bellinger 						r2t->xfer_len))) {
661e48354ceSNicholas Bellinger 					cmd->next_burst_len -= pdu->length;
662e48354ceSNicholas Bellinger 					cmd->write_data_done -= pdu->length;
663e48354ceSNicholas Bellinger 					pdu->status = ISCSI_PDU_NOT_RECEIVED;
664e48354ceSNicholas Bellinger 				}
665e48354ceSNicholas Bellinger 			}
666e48354ceSNicholas Bellinger 
667e48354ceSNicholas Bellinger 			first_incomplete_r2t = 0;
668e48354ceSNicholas Bellinger 		} else {
669e48354ceSNicholas Bellinger 			struct iscsi_seq *seq;
670e48354ceSNicholas Bellinger 
671e48354ceSNicholas Bellinger 			seq = iscsit_get_seq_holder(cmd, r2t->offset,
672e48354ceSNicholas Bellinger 					r2t->xfer_len);
673e48354ceSNicholas Bellinger 			if (!seq) {
674e48354ceSNicholas Bellinger 				spin_unlock_bh(&cmd->r2t_lock);
675e48354ceSNicholas Bellinger 				return -1;
676e48354ceSNicholas Bellinger 			}
677e48354ceSNicholas Bellinger 
678e48354ceSNicholas Bellinger 			cmd->write_data_done -=
679e48354ceSNicholas Bellinger 					(seq->offset - seq->orig_offset);
680e48354ceSNicholas Bellinger 			seq->data_sn = 0;
681e48354ceSNicholas Bellinger 			seq->offset = seq->orig_offset;
682e48354ceSNicholas Bellinger 			seq->next_burst_len = 0;
683e48354ceSNicholas Bellinger 			seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
684e48354ceSNicholas Bellinger 
685e48354ceSNicholas Bellinger 			cmd->seq_send_order--;
686e48354ceSNicholas Bellinger 
687e48354ceSNicholas Bellinger 			if (conn->sess->sess_ops->DataPDUInOrder)
688e48354ceSNicholas Bellinger 				goto next;
689e48354ceSNicholas Bellinger 
690e48354ceSNicholas Bellinger 			for (i = 0; i < seq->pdu_count; i++) {
691e48354ceSNicholas Bellinger 				pdu = &cmd->pdu_list[i+seq->pdu_start];
692e48354ceSNicholas Bellinger 
693e48354ceSNicholas Bellinger 				if (pdu->status != ISCSI_PDU_RECEIVED_OK)
694e48354ceSNicholas Bellinger 					continue;
695e48354ceSNicholas Bellinger 
696e48354ceSNicholas Bellinger 				pdu->status = ISCSI_PDU_NOT_RECEIVED;
697e48354ceSNicholas Bellinger 			}
698e48354ceSNicholas Bellinger 		}
699e48354ceSNicholas Bellinger 
700e48354ceSNicholas Bellinger next:
701e48354ceSNicholas Bellinger 		cmd->outstanding_r2ts--;
702e48354ceSNicholas Bellinger 	}
703e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
704e48354ceSNicholas Bellinger 
705e48354ceSNicholas Bellinger 	/*
706e48354ceSNicholas Bellinger 	 * We now drop all unacknowledged R2Ts, ie: ExpDataSN from TMR
707e48354ceSNicholas Bellinger 	 * TASK_REASSIGN to the last R2T in the list..  We are also careful
708e48354ceSNicholas Bellinger 	 * to check that the Initiator is not requesting R2Ts for DataOUT
709e48354ceSNicholas Bellinger 	 * sequences it has already completed.
710e48354ceSNicholas Bellinger 	 *
71166cd9d4eSMax Gurtovoy 	 * Free each R2T in question and adjust values in struct iscsit_cmd
712e48354ceSNicholas Bellinger 	 * accordingly so iscsit_build_r2ts_for_cmd() do the rest of
713e48354ceSNicholas Bellinger 	 * the work after the TMR TASK_REASSIGN Response is sent.
714e48354ceSNicholas Bellinger 	 */
715e48354ceSNicholas Bellinger drop_unacknowledged_r2ts:
716e48354ceSNicholas Bellinger 
717e48354ceSNicholas Bellinger 	cmd->cmd_flags &= ~ICF_SENT_LAST_R2T;
718e48354ceSNicholas Bellinger 	cmd->r2t_sn = tmr_req->exp_data_sn;
719e48354ceSNicholas Bellinger 
720e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
721e48354ceSNicholas Bellinger 	list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list) {
722e48354ceSNicholas Bellinger 		/*
723e48354ceSNicholas Bellinger 		 * Skip up to the R2T Sequence number provided by the
724e48354ceSNicholas Bellinger 		 * iSCSI TASK_REASSIGN TMR
725e48354ceSNicholas Bellinger 		 */
726e48354ceSNicholas Bellinger 		if (r2t->r2t_sn < tmr_req->exp_data_sn)
727e48354ceSNicholas Bellinger 			continue;
728e48354ceSNicholas Bellinger 
729e48354ceSNicholas Bellinger 		if (r2t->seq_complete) {
730e48354ceSNicholas Bellinger 			pr_err("Initiator is requesting R2Ts from"
731e48354ceSNicholas Bellinger 				" R2TSN: 0x%08x, but R2TSN: 0x%08x, Offset: %u,"
732e48354ceSNicholas Bellinger 				" Length: %u is already complete."
733e48354ceSNicholas Bellinger 				"   BAD INITIATOR ERL=2 IMPLEMENTATION!\n",
734e48354ceSNicholas Bellinger 				tmr_req->exp_data_sn, r2t->r2t_sn,
735e48354ceSNicholas Bellinger 				r2t->offset, r2t->xfer_len);
736e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->r2t_lock);
737e48354ceSNicholas Bellinger 			return -1;
738e48354ceSNicholas Bellinger 		}
739e48354ceSNicholas Bellinger 
740e48354ceSNicholas Bellinger 		if (r2t->recovery_r2t) {
741e48354ceSNicholas Bellinger 			iscsit_free_r2t(r2t, cmd);
742e48354ceSNicholas Bellinger 			continue;
743e48354ceSNicholas Bellinger 		}
744e48354ceSNicholas Bellinger 
745e48354ceSNicholas Bellinger 		/*		   DataSequenceInOrder=Yes:
746e48354ceSNicholas Bellinger 		 *
747e48354ceSNicholas Bellinger 		 * Taking into account the iSCSI implementation requirement of
748e48354ceSNicholas Bellinger 		 * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
749e48354ceSNicholas Bellinger 		 * DataSequenceInOrder=Yes, it's safe to subtract the R2Ts
750e48354ceSNicholas Bellinger 		 * entire transfer length from the commands R2T offset marker.
751e48354ceSNicholas Bellinger 		 *
752e48354ceSNicholas Bellinger 		 *		   DataSequenceInOrder=No:
753e48354ceSNicholas Bellinger 		 *
754e48354ceSNicholas Bellinger 		 * We subtract the difference from struct iscsi_seq between the
755e48354ceSNicholas Bellinger 		 * current offset and original offset from cmd->write_data_done
756e48354ceSNicholas Bellinger 		 * for account for DataOUT PDUs already received.  Then reset
757e48354ceSNicholas Bellinger 		 * the current offset to the original and zero out the current
758e48354ceSNicholas Bellinger 		 * burst length,  to make sure we re-request the entire DataOUT
759e48354ceSNicholas Bellinger 		 * sequence.
760e48354ceSNicholas Bellinger 		 */
761e48354ceSNicholas Bellinger 		if (conn->sess->sess_ops->DataSequenceInOrder)
762e48354ceSNicholas Bellinger 			cmd->r2t_offset -= r2t->xfer_len;
763e48354ceSNicholas Bellinger 		else
764e48354ceSNicholas Bellinger 			cmd->seq_send_order--;
765e48354ceSNicholas Bellinger 
766e48354ceSNicholas Bellinger 		cmd->outstanding_r2ts--;
767e48354ceSNicholas Bellinger 		iscsit_free_r2t(r2t, cmd);
768e48354ceSNicholas Bellinger 	}
769e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
770e48354ceSNicholas Bellinger 
771e48354ceSNicholas Bellinger 	return 0;
772e48354ceSNicholas Bellinger }
773e48354ceSNicholas Bellinger 
774e48354ceSNicholas Bellinger /*
775e48354ceSNicholas Bellinger  *	Performs sanity checks TMR TASK_REASSIGN's ExpDataSN for
77666cd9d4eSMax Gurtovoy  *	a given struct iscsit_cmd.
777e48354ceSNicholas Bellinger  */
iscsit_check_task_reassign_expdatasn(struct iscsi_tmr_req * tmr_req,struct iscsit_conn * conn)778e48354ceSNicholas Bellinger int iscsit_check_task_reassign_expdatasn(
779e48354ceSNicholas Bellinger 	struct iscsi_tmr_req *tmr_req,
780be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
781e48354ceSNicholas Bellinger {
78266cd9d4eSMax Gurtovoy 	struct iscsit_cmd *ref_cmd = tmr_req->ref_cmd;
783e48354ceSNicholas Bellinger 
784e48354ceSNicholas Bellinger 	if (ref_cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD)
785e48354ceSNicholas Bellinger 		return 0;
786e48354ceSNicholas Bellinger 
7879f3eb93eSChristoph Hellwig 	if (ref_cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION)
788e48354ceSNicholas Bellinger 		return 0;
789e48354ceSNicholas Bellinger 
790e48354ceSNicholas Bellinger 	if (ref_cmd->data_direction == DMA_NONE)
791e48354ceSNicholas Bellinger 		return 0;
792e48354ceSNicholas Bellinger 
793e48354ceSNicholas Bellinger 	/*
794e48354ceSNicholas Bellinger 	 * For READs the TMR TASK_REASSIGNs ExpDataSN contains the next DataSN
795e48354ceSNicholas Bellinger 	 * of DataIN the Initiator is expecting.
796e48354ceSNicholas Bellinger 	 *
797e48354ceSNicholas Bellinger 	 * Also check that the Initiator is not re-requesting DataIN that has
798e48354ceSNicholas Bellinger 	 * already been acknowledged with a DataAck SNACK.
799e48354ceSNicholas Bellinger 	 */
800e48354ceSNicholas Bellinger 	if (ref_cmd->data_direction == DMA_FROM_DEVICE) {
801e48354ceSNicholas Bellinger 		if (tmr_req->exp_data_sn > ref_cmd->data_sn) {
802e48354ceSNicholas Bellinger 			pr_err("Received ExpDataSN: 0x%08x for READ"
803e48354ceSNicholas Bellinger 				" in TMR TASK_REASSIGN greater than command's"
804e48354ceSNicholas Bellinger 				" DataSN: 0x%08x.\n", tmr_req->exp_data_sn,
805e48354ceSNicholas Bellinger 				ref_cmd->data_sn);
806e48354ceSNicholas Bellinger 			return -1;
807e48354ceSNicholas Bellinger 		}
808e48354ceSNicholas Bellinger 		if ((ref_cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
809e48354ceSNicholas Bellinger 		    (tmr_req->exp_data_sn <= ref_cmd->acked_data_sn)) {
810e48354ceSNicholas Bellinger 			pr_err("Received ExpDataSN: 0x%08x for READ"
811e48354ceSNicholas Bellinger 				" in TMR TASK_REASSIGN for previously"
812e48354ceSNicholas Bellinger 				" acknowledged DataIN: 0x%08x,"
813e48354ceSNicholas Bellinger 				" protocol error\n", tmr_req->exp_data_sn,
814e48354ceSNicholas Bellinger 				ref_cmd->acked_data_sn);
815e48354ceSNicholas Bellinger 			return -1;
816e48354ceSNicholas Bellinger 		}
817e48354ceSNicholas Bellinger 		return iscsit_task_reassign_prepare_read(tmr_req, conn);
818e48354ceSNicholas Bellinger 	}
819e48354ceSNicholas Bellinger 
820e48354ceSNicholas Bellinger 	/*
821e48354ceSNicholas Bellinger 	 * For WRITEs the TMR TASK_REASSIGNs ExpDataSN contains the next R2TSN
822e48354ceSNicholas Bellinger 	 * for R2Ts the Initiator is expecting.
823e48354ceSNicholas Bellinger 	 *
824e48354ceSNicholas Bellinger 	 * Do the magic in iscsit_task_reassign_prepare_write().
825e48354ceSNicholas Bellinger 	 */
826e48354ceSNicholas Bellinger 	if (ref_cmd->data_direction == DMA_TO_DEVICE) {
827e48354ceSNicholas Bellinger 		if (tmr_req->exp_data_sn > ref_cmd->r2t_sn) {
828e48354ceSNicholas Bellinger 			pr_err("Received ExpDataSN: 0x%08x for WRITE"
829e48354ceSNicholas Bellinger 				" in TMR TASK_REASSIGN greater than command's"
830e48354ceSNicholas Bellinger 				" R2TSN: 0x%08x.\n", tmr_req->exp_data_sn,
831e48354ceSNicholas Bellinger 					ref_cmd->r2t_sn);
832e48354ceSNicholas Bellinger 			return -1;
833e48354ceSNicholas Bellinger 		}
834e48354ceSNicholas Bellinger 		return iscsit_task_reassign_prepare_write(tmr_req, conn);
835e48354ceSNicholas Bellinger 	}
836e48354ceSNicholas Bellinger 
837e48354ceSNicholas Bellinger 	pr_err("Unknown iSCSI data_direction: 0x%02x\n",
838e48354ceSNicholas Bellinger 			ref_cmd->data_direction);
839e48354ceSNicholas Bellinger 
840e48354ceSNicholas Bellinger 	return -1;
841e48354ceSNicholas Bellinger }
842