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