1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e48354ceSNicholas Bellinger /*******************************************************************************
3e48354ceSNicholas Bellinger  * This file contains error recovery level two functions used by
4e48354ceSNicholas Bellinger  * the iSCSI Target driver.
5e48354ceSNicholas Bellinger  *
64c76251eSNicholas Bellinger  * (c) Copyright 2007-2013 Datera, Inc.
7e48354ceSNicholas Bellinger  *
8e48354ceSNicholas Bellinger  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9e48354ceSNicholas Bellinger  *
10e48354ceSNicholas Bellinger  ******************************************************************************/
11e48354ceSNicholas Bellinger 
128dcf07beSBart Van Assche #include <linux/slab.h>
13e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
14e48354ceSNicholas Bellinger #include <target/target_core_base.h>
15c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
16e48354ceSNicholas Bellinger 
1767f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
18e48354ceSNicholas Bellinger #include "iscsi_target_datain_values.h"
19e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
20e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
21e48354ceSNicholas Bellinger #include "iscsi_target_erl1.h"
22e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h"
23e48354ceSNicholas Bellinger #include "iscsi_target.h"
24e48354ceSNicholas Bellinger 
25e48354ceSNicholas Bellinger /*
26e48354ceSNicholas Bellinger  *	FIXME: Does RData SNACK apply here as well?
27e48354ceSNicholas Bellinger  */
iscsit_create_conn_recovery_datain_values(struct iscsit_cmd * cmd,__be32 exp_data_sn)28e48354ceSNicholas Bellinger void iscsit_create_conn_recovery_datain_values(
2966cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
3050e5c87dSChristoph Hellwig 	__be32 exp_data_sn)
31e48354ceSNicholas Bellinger {
32e48354ceSNicholas Bellinger 	u32 data_sn = 0;
33be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
34e48354ceSNicholas Bellinger 
35e48354ceSNicholas Bellinger 	cmd->next_burst_len = 0;
36e48354ceSNicholas Bellinger 	cmd->read_data_done = 0;
37e48354ceSNicholas Bellinger 
3850e5c87dSChristoph Hellwig 	while (be32_to_cpu(exp_data_sn) > data_sn) {
39e48354ceSNicholas Bellinger 		if ((cmd->next_burst_len +
40e48354ceSNicholas Bellinger 		     conn->conn_ops->MaxRecvDataSegmentLength) <
41e48354ceSNicholas Bellinger 		     conn->sess->sess_ops->MaxBurstLength) {
42e48354ceSNicholas Bellinger 			cmd->read_data_done +=
43e48354ceSNicholas Bellinger 			       conn->conn_ops->MaxRecvDataSegmentLength;
44e48354ceSNicholas Bellinger 			cmd->next_burst_len +=
45e48354ceSNicholas Bellinger 			       conn->conn_ops->MaxRecvDataSegmentLength;
46e48354ceSNicholas Bellinger 		} else {
47e48354ceSNicholas Bellinger 			cmd->read_data_done +=
48e48354ceSNicholas Bellinger 				(conn->sess->sess_ops->MaxBurstLength -
49e48354ceSNicholas Bellinger 				cmd->next_burst_len);
50e48354ceSNicholas Bellinger 			cmd->next_burst_len = 0;
51e48354ceSNicholas Bellinger 		}
52e48354ceSNicholas Bellinger 		data_sn++;
53e48354ceSNicholas Bellinger 	}
54e48354ceSNicholas Bellinger }
55e48354ceSNicholas Bellinger 
iscsit_create_conn_recovery_dataout_values(struct iscsit_cmd * cmd)56e48354ceSNicholas Bellinger void iscsit_create_conn_recovery_dataout_values(
5766cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd)
58e48354ceSNicholas Bellinger {
59e48354ceSNicholas Bellinger 	u32 write_data_done = 0;
60be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
61e48354ceSNicholas Bellinger 
62e48354ceSNicholas Bellinger 	cmd->data_sn = 0;
63e48354ceSNicholas Bellinger 	cmd->next_burst_len = 0;
64e48354ceSNicholas Bellinger 
65e48354ceSNicholas Bellinger 	while (cmd->write_data_done > write_data_done) {
66e48354ceSNicholas Bellinger 		if ((write_data_done + conn->sess->sess_ops->MaxBurstLength) <=
67e48354ceSNicholas Bellinger 		     cmd->write_data_done)
68e48354ceSNicholas Bellinger 			write_data_done += conn->sess->sess_ops->MaxBurstLength;
69e48354ceSNicholas Bellinger 		else
70e48354ceSNicholas Bellinger 			break;
71e48354ceSNicholas Bellinger 	}
72e48354ceSNicholas Bellinger 
73e48354ceSNicholas Bellinger 	cmd->write_data_done = write_data_done;
74e48354ceSNicholas Bellinger }
75e48354ceSNicholas Bellinger 
iscsit_attach_active_connection_recovery_entry(struct iscsit_session * sess,struct iscsi_conn_recovery * cr)76e48354ceSNicholas Bellinger static int iscsit_attach_active_connection_recovery_entry(
770873fe44SMax Gurtovoy 	struct iscsit_session *sess,
78e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr)
79e48354ceSNicholas Bellinger {
80e48354ceSNicholas Bellinger 	spin_lock(&sess->cr_a_lock);
81e48354ceSNicholas Bellinger 	list_add_tail(&cr->cr_list, &sess->cr_active_list);
82e48354ceSNicholas Bellinger 	spin_unlock(&sess->cr_a_lock);
83e48354ceSNicholas Bellinger 
84e48354ceSNicholas Bellinger 	return 0;
85e48354ceSNicholas Bellinger }
86e48354ceSNicholas Bellinger 
iscsit_attach_inactive_connection_recovery_entry(struct iscsit_session * sess,struct iscsi_conn_recovery * cr)87e48354ceSNicholas Bellinger static int iscsit_attach_inactive_connection_recovery_entry(
880873fe44SMax Gurtovoy 	struct iscsit_session *sess,
89e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr)
90e48354ceSNicholas Bellinger {
91e48354ceSNicholas Bellinger 	spin_lock(&sess->cr_i_lock);
92e48354ceSNicholas Bellinger 	list_add_tail(&cr->cr_list, &sess->cr_inactive_list);
93e48354ceSNicholas Bellinger 
94e48354ceSNicholas Bellinger 	sess->conn_recovery_count++;
95e48354ceSNicholas Bellinger 	pr_debug("Incremented connection recovery count to %u for"
96e48354ceSNicholas Bellinger 		" SID: %u\n", sess->conn_recovery_count, sess->sid);
97e48354ceSNicholas Bellinger 	spin_unlock(&sess->cr_i_lock);
98e48354ceSNicholas Bellinger 
99e48354ceSNicholas Bellinger 	return 0;
100e48354ceSNicholas Bellinger }
101e48354ceSNicholas Bellinger 
iscsit_get_inactive_connection_recovery_entry(struct iscsit_session * sess,u16 cid)102e48354ceSNicholas Bellinger struct iscsi_conn_recovery *iscsit_get_inactive_connection_recovery_entry(
1030873fe44SMax Gurtovoy 	struct iscsit_session *sess,
104e48354ceSNicholas Bellinger 	u16 cid)
105e48354ceSNicholas Bellinger {
106e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr;
107e48354ceSNicholas Bellinger 
108e48354ceSNicholas Bellinger 	spin_lock(&sess->cr_i_lock);
109e48354ceSNicholas Bellinger 	list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
110e48354ceSNicholas Bellinger 		if (cr->cid == cid) {
111e48354ceSNicholas Bellinger 			spin_unlock(&sess->cr_i_lock);
112e48354ceSNicholas Bellinger 			return cr;
113e48354ceSNicholas Bellinger 		}
114e48354ceSNicholas Bellinger 	}
115e48354ceSNicholas Bellinger 	spin_unlock(&sess->cr_i_lock);
116e48354ceSNicholas Bellinger 
117e48354ceSNicholas Bellinger 	return NULL;
118e48354ceSNicholas Bellinger }
119e48354ceSNicholas Bellinger 
iscsit_free_connection_recovery_entries(struct iscsit_session * sess)1200873fe44SMax Gurtovoy void iscsit_free_connection_recovery_entries(struct iscsit_session *sess)
121e48354ceSNicholas Bellinger {
12266cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd, *cmd_tmp;
123e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr, *cr_tmp;
124e48354ceSNicholas Bellinger 
125e48354ceSNicholas Bellinger 	spin_lock(&sess->cr_a_lock);
126e48354ceSNicholas Bellinger 	list_for_each_entry_safe(cr, cr_tmp, &sess->cr_active_list, cr_list) {
127e48354ceSNicholas Bellinger 		list_del(&cr->cr_list);
128e48354ceSNicholas Bellinger 		spin_unlock(&sess->cr_a_lock);
129e48354ceSNicholas Bellinger 
130e48354ceSNicholas Bellinger 		spin_lock(&cr->conn_recovery_cmd_lock);
131e48354ceSNicholas Bellinger 		list_for_each_entry_safe(cmd, cmd_tmp,
1322fbb471eSAndy Grover 				&cr->conn_recovery_cmd_list, i_conn_node) {
133e48354ceSNicholas Bellinger 
1345159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
135e48354ceSNicholas Bellinger 			cmd->conn = NULL;
136e48354ceSNicholas Bellinger 			spin_unlock(&cr->conn_recovery_cmd_lock);
137aafc9d15SNicholas Bellinger 			iscsit_free_cmd(cmd, true);
138e48354ceSNicholas Bellinger 			spin_lock(&cr->conn_recovery_cmd_lock);
139e48354ceSNicholas Bellinger 		}
140e48354ceSNicholas Bellinger 		spin_unlock(&cr->conn_recovery_cmd_lock);
141e48354ceSNicholas Bellinger 		spin_lock(&sess->cr_a_lock);
142e48354ceSNicholas Bellinger 
143e48354ceSNicholas Bellinger 		kfree(cr);
144e48354ceSNicholas Bellinger 	}
145e48354ceSNicholas Bellinger 	spin_unlock(&sess->cr_a_lock);
146e48354ceSNicholas Bellinger 
147e48354ceSNicholas Bellinger 	spin_lock(&sess->cr_i_lock);
148e48354ceSNicholas Bellinger 	list_for_each_entry_safe(cr, cr_tmp, &sess->cr_inactive_list, cr_list) {
149e48354ceSNicholas Bellinger 		list_del(&cr->cr_list);
150e48354ceSNicholas Bellinger 		spin_unlock(&sess->cr_i_lock);
151e48354ceSNicholas Bellinger 
152e48354ceSNicholas Bellinger 		spin_lock(&cr->conn_recovery_cmd_lock);
153e48354ceSNicholas Bellinger 		list_for_each_entry_safe(cmd, cmd_tmp,
1542fbb471eSAndy Grover 				&cr->conn_recovery_cmd_list, i_conn_node) {
155e48354ceSNicholas Bellinger 
1565159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
157e48354ceSNicholas Bellinger 			cmd->conn = NULL;
158e48354ceSNicholas Bellinger 			spin_unlock(&cr->conn_recovery_cmd_lock);
159aafc9d15SNicholas Bellinger 			iscsit_free_cmd(cmd, true);
160e48354ceSNicholas Bellinger 			spin_lock(&cr->conn_recovery_cmd_lock);
161e48354ceSNicholas Bellinger 		}
162e48354ceSNicholas Bellinger 		spin_unlock(&cr->conn_recovery_cmd_lock);
163e48354ceSNicholas Bellinger 		spin_lock(&sess->cr_i_lock);
164e48354ceSNicholas Bellinger 
165e48354ceSNicholas Bellinger 		kfree(cr);
166e48354ceSNicholas Bellinger 	}
167e48354ceSNicholas Bellinger 	spin_unlock(&sess->cr_i_lock);
168e48354ceSNicholas Bellinger }
169e48354ceSNicholas Bellinger 
iscsit_remove_active_connection_recovery_entry(struct iscsi_conn_recovery * cr,struct iscsit_session * sess)170e48354ceSNicholas Bellinger int iscsit_remove_active_connection_recovery_entry(
171e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr,
1720873fe44SMax Gurtovoy 	struct iscsit_session *sess)
173e48354ceSNicholas Bellinger {
174e48354ceSNicholas Bellinger 	spin_lock(&sess->cr_a_lock);
175e48354ceSNicholas Bellinger 	list_del(&cr->cr_list);
176e48354ceSNicholas Bellinger 
177e48354ceSNicholas Bellinger 	sess->conn_recovery_count--;
178e48354ceSNicholas Bellinger 	pr_debug("Decremented connection recovery count to %u for"
179e48354ceSNicholas Bellinger 		" SID: %u\n", sess->conn_recovery_count, sess->sid);
180e48354ceSNicholas Bellinger 	spin_unlock(&sess->cr_a_lock);
181e48354ceSNicholas Bellinger 
182e48354ceSNicholas Bellinger 	kfree(cr);
183e48354ceSNicholas Bellinger 
184e48354ceSNicholas Bellinger 	return 0;
185e48354ceSNicholas Bellinger }
186e48354ceSNicholas Bellinger 
iscsit_remove_inactive_connection_recovery_entry(struct iscsi_conn_recovery * cr,struct iscsit_session * sess)187fceb5bc7SChristoph Hellwig static void iscsit_remove_inactive_connection_recovery_entry(
188e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr,
1890873fe44SMax Gurtovoy 	struct iscsit_session *sess)
190e48354ceSNicholas Bellinger {
191e48354ceSNicholas Bellinger 	spin_lock(&sess->cr_i_lock);
192e48354ceSNicholas Bellinger 	list_del(&cr->cr_list);
193e48354ceSNicholas Bellinger 	spin_unlock(&sess->cr_i_lock);
194e48354ceSNicholas Bellinger }
195e48354ceSNicholas Bellinger 
196e48354ceSNicholas Bellinger /*
197e48354ceSNicholas Bellinger  *	Called with cr->conn_recovery_cmd_lock help.
198e48354ceSNicholas Bellinger  */
iscsit_remove_cmd_from_connection_recovery(struct iscsit_cmd * cmd,struct iscsit_session * sess)199e48354ceSNicholas Bellinger int iscsit_remove_cmd_from_connection_recovery(
20066cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
2010873fe44SMax Gurtovoy 	struct iscsit_session *sess)
202e48354ceSNicholas Bellinger {
203e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr;
204e48354ceSNicholas Bellinger 
205e48354ceSNicholas Bellinger 	if (!cmd->cr) {
206e48354ceSNicholas Bellinger 		pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
207e48354ceSNicholas Bellinger 			" is NULL!\n", cmd->init_task_tag);
208e48354ceSNicholas Bellinger 		BUG();
209e48354ceSNicholas Bellinger 	}
210e48354ceSNicholas Bellinger 	cr = cmd->cr;
211e48354ceSNicholas Bellinger 
2125159d763SNicholas Bellinger 	list_del_init(&cmd->i_conn_node);
213e48354ceSNicholas Bellinger 	return --cr->cmd_count;
214e48354ceSNicholas Bellinger }
215e48354ceSNicholas Bellinger 
iscsit_discard_cr_cmds_by_expstatsn(struct iscsi_conn_recovery * cr,u32 exp_statsn)216e48354ceSNicholas Bellinger void iscsit_discard_cr_cmds_by_expstatsn(
217e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr,
218e48354ceSNicholas Bellinger 	u32 exp_statsn)
219e48354ceSNicholas Bellinger {
220e48354ceSNicholas Bellinger 	u32 dropped_count = 0;
22166cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd, *cmd_tmp;
2220873fe44SMax Gurtovoy 	struct iscsit_session *sess = cr->sess;
223e48354ceSNicholas Bellinger 
224e48354ceSNicholas Bellinger 	spin_lock(&cr->conn_recovery_cmd_lock);
225e48354ceSNicholas Bellinger 	list_for_each_entry_safe(cmd, cmd_tmp,
2262fbb471eSAndy Grover 			&cr->conn_recovery_cmd_list, i_conn_node) {
227e48354ceSNicholas Bellinger 
228e48354ceSNicholas Bellinger 		if (((cmd->deferred_i_state != ISTATE_SENT_STATUS) &&
229e48354ceSNicholas Bellinger 		     (cmd->deferred_i_state != ISTATE_REMOVE)) ||
230e48354ceSNicholas Bellinger 		     (cmd->stat_sn >= exp_statsn)) {
231e48354ceSNicholas Bellinger 			continue;
232e48354ceSNicholas Bellinger 		}
233e48354ceSNicholas Bellinger 
234e48354ceSNicholas Bellinger 		dropped_count++;
235e48354ceSNicholas Bellinger 		pr_debug("Dropping Acknowledged ITT: 0x%08x, StatSN:"
236e48354ceSNicholas Bellinger 			" 0x%08x, CID: %hu.\n", cmd->init_task_tag,
237e48354ceSNicholas Bellinger 				cmd->stat_sn, cr->cid);
238e48354ceSNicholas Bellinger 
239e48354ceSNicholas Bellinger 		iscsit_remove_cmd_from_connection_recovery(cmd, sess);
240e48354ceSNicholas Bellinger 
241e48354ceSNicholas Bellinger 		spin_unlock(&cr->conn_recovery_cmd_lock);
242aafc9d15SNicholas Bellinger 		iscsit_free_cmd(cmd, true);
243e48354ceSNicholas Bellinger 		spin_lock(&cr->conn_recovery_cmd_lock);
244e48354ceSNicholas Bellinger 	}
245e48354ceSNicholas Bellinger 	spin_unlock(&cr->conn_recovery_cmd_lock);
246e48354ceSNicholas Bellinger 
247e48354ceSNicholas Bellinger 	pr_debug("Dropped %u total acknowledged commands on"
248e48354ceSNicholas Bellinger 		" CID: %hu less than old ExpStatSN: 0x%08x\n",
249e48354ceSNicholas Bellinger 			dropped_count, cr->cid, exp_statsn);
250e48354ceSNicholas Bellinger 
251e48354ceSNicholas Bellinger 	if (!cr->cmd_count) {
252e48354ceSNicholas Bellinger 		pr_debug("No commands to be reassigned for failed"
253e48354ceSNicholas Bellinger 			" connection CID: %hu on SID: %u\n",
254e48354ceSNicholas Bellinger 			cr->cid, sess->sid);
255e48354ceSNicholas Bellinger 		iscsit_remove_inactive_connection_recovery_entry(cr, sess);
256e48354ceSNicholas Bellinger 		iscsit_attach_active_connection_recovery_entry(sess, cr);
257e48354ceSNicholas Bellinger 		pr_debug("iSCSI connection recovery successful for CID:"
258e48354ceSNicholas Bellinger 			" %hu on SID: %u\n", cr->cid, sess->sid);
259e48354ceSNicholas Bellinger 		iscsit_remove_active_connection_recovery_entry(cr, sess);
260e48354ceSNicholas Bellinger 	} else {
261e48354ceSNicholas Bellinger 		iscsit_remove_inactive_connection_recovery_entry(cr, sess);
262e48354ceSNicholas Bellinger 		iscsit_attach_active_connection_recovery_entry(sess, cr);
263e48354ceSNicholas Bellinger 	}
264e48354ceSNicholas Bellinger }
265e48354ceSNicholas Bellinger 
iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(struct iscsit_conn * conn)266be36d683SMax Gurtovoy int iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(struct iscsit_conn *conn)
267e48354ceSNicholas Bellinger {
268e48354ceSNicholas Bellinger 	u32 dropped_count = 0;
26966cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd, *cmd_tmp;
270e48354ceSNicholas Bellinger 	struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
2710873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
272e48354ceSNicholas Bellinger 
273e48354ceSNicholas Bellinger 	mutex_lock(&sess->cmdsn_mutex);
274e48354ceSNicholas Bellinger 	list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
275e48354ceSNicholas Bellinger 			&sess->sess_ooo_cmdsn_list, ooo_list) {
276e48354ceSNicholas Bellinger 
277e48354ceSNicholas Bellinger 		if (ooo_cmdsn->cid != conn->cid)
278e48354ceSNicholas Bellinger 			continue;
279e48354ceSNicholas Bellinger 
280e48354ceSNicholas Bellinger 		dropped_count++;
281e48354ceSNicholas Bellinger 		pr_debug("Dropping unacknowledged CmdSN:"
282e48354ceSNicholas Bellinger 		" 0x%08x during connection recovery on CID: %hu\n",
283e48354ceSNicholas Bellinger 			ooo_cmdsn->cmdsn, conn->cid);
284e48354ceSNicholas Bellinger 		iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
285e48354ceSNicholas Bellinger 	}
286e48354ceSNicholas Bellinger 	mutex_unlock(&sess->cmdsn_mutex);
287e48354ceSNicholas Bellinger 
288e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
2892fbb471eSAndy Grover 	list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
290e48354ceSNicholas Bellinger 		if (!(cmd->cmd_flags & ICF_OOO_CMDSN))
291e48354ceSNicholas Bellinger 			continue;
292e48354ceSNicholas Bellinger 
2935159d763SNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
294e48354ceSNicholas Bellinger 
295e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
296aafc9d15SNicholas Bellinger 		iscsit_free_cmd(cmd, true);
297e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
298e48354ceSNicholas Bellinger 	}
299e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
300e48354ceSNicholas Bellinger 
301e48354ceSNicholas Bellinger 	pr_debug("Dropped %u total unacknowledged commands on CID:"
302e48354ceSNicholas Bellinger 		" %hu for ExpCmdSN: 0x%08x.\n", dropped_count, conn->cid,
303e48354ceSNicholas Bellinger 				sess->exp_cmd_sn);
304e48354ceSNicholas Bellinger 	return 0;
305e48354ceSNicholas Bellinger }
306e48354ceSNicholas Bellinger 
iscsit_prepare_cmds_for_reallegiance(struct iscsit_conn * conn)307be36d683SMax Gurtovoy int iscsit_prepare_cmds_for_reallegiance(struct iscsit_conn *conn)
308e48354ceSNicholas Bellinger {
309e48354ceSNicholas Bellinger 	u32 cmd_count = 0;
31066cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd, *cmd_tmp;
311e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr;
312e48354ceSNicholas Bellinger 
313e48354ceSNicholas Bellinger 	/*
314e48354ceSNicholas Bellinger 	 * Allocate an struct iscsi_conn_recovery for this connection.
31566cd9d4eSMax Gurtovoy 	 * Each struct iscsit_cmd contains an struct iscsi_conn_recovery pointer
31666cd9d4eSMax Gurtovoy 	 * (struct iscsit_cmd->cr) so we need to allocate this before preparing the
317e48354ceSNicholas Bellinger 	 * connection's command list for connection recovery.
318e48354ceSNicholas Bellinger 	 */
319e48354ceSNicholas Bellinger 	cr = kzalloc(sizeof(struct iscsi_conn_recovery), GFP_KERNEL);
320e48354ceSNicholas Bellinger 	if (!cr) {
321e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for"
322e48354ceSNicholas Bellinger 			" struct iscsi_conn_recovery.\n");
323e48354ceSNicholas Bellinger 		return -1;
324e48354ceSNicholas Bellinger 	}
325e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&cr->cr_list);
326e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&cr->conn_recovery_cmd_list);
327e48354ceSNicholas Bellinger 	spin_lock_init(&cr->conn_recovery_cmd_lock);
328e48354ceSNicholas Bellinger 	/*
329e48354ceSNicholas Bellinger 	 * Only perform connection recovery on ISCSI_OP_SCSI_CMD or
330e48354ceSNicholas Bellinger 	 * ISCSI_OP_NOOP_OUT opcodes.  For all other opcodes call
3315159d763SNicholas Bellinger 	 * list_del_init(&cmd->i_conn_node); to release the command to the
332e48354ceSNicholas Bellinger 	 * session pool and remove it from the connection's list.
333e48354ceSNicholas Bellinger 	 *
334e48354ceSNicholas Bellinger 	 * Also stop the DataOUT timer, which will be restarted after
335e48354ceSNicholas Bellinger 	 * sending the TMR response.
336e48354ceSNicholas Bellinger 	 */
337e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
3382fbb471eSAndy Grover 	list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
339e48354ceSNicholas Bellinger 
340e48354ceSNicholas Bellinger 		if ((cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD) &&
341e48354ceSNicholas Bellinger 		    (cmd->iscsi_opcode != ISCSI_OP_NOOP_OUT)) {
34253c561dcSBart Van Assche 			pr_debug("Not performing reallegiance on"
343e48354ceSNicholas Bellinger 				" Opcode: 0x%02x, ITT: 0x%08x, CmdSN: 0x%08x,"
344e48354ceSNicholas Bellinger 				" CID: %hu\n", cmd->iscsi_opcode,
345e48354ceSNicholas Bellinger 				cmd->init_task_tag, cmd->cmd_sn, conn->cid);
346e48354ceSNicholas Bellinger 
3475159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
348e48354ceSNicholas Bellinger 			spin_unlock_bh(&conn->cmd_lock);
349aafc9d15SNicholas Bellinger 			iscsit_free_cmd(cmd, true);
350e48354ceSNicholas Bellinger 			spin_lock_bh(&conn->cmd_lock);
351e48354ceSNicholas Bellinger 			continue;
352e48354ceSNicholas Bellinger 		}
353e48354ceSNicholas Bellinger 
354e48354ceSNicholas Bellinger 		/*
355e48354ceSNicholas Bellinger 		 * Special case where commands greater than or equal to
356e48354ceSNicholas Bellinger 		 * the session's ExpCmdSN are attached to the connection
357e48354ceSNicholas Bellinger 		 * list but not to the out of order CmdSN list.  The one
358e48354ceSNicholas Bellinger 		 * obvious case is when a command with immediate data
359e48354ceSNicholas Bellinger 		 * attached must only check the CmdSN against ExpCmdSN
360e48354ceSNicholas Bellinger 		 * after the data is received.  The special case below
361e48354ceSNicholas Bellinger 		 * is when the connection fails before data is received,
362e48354ceSNicholas Bellinger 		 * but also may apply to other PDUs, so it has been
363e48354ceSNicholas Bellinger 		 * made generic here.
364e48354ceSNicholas Bellinger 		 */
365e48354ceSNicholas Bellinger 		if (!(cmd->cmd_flags & ICF_OOO_CMDSN) && !cmd->immediate_cmd &&
36664fe4f4fSRoland Dreier 		     iscsi_sna_gte(cmd->cmd_sn, conn->sess->exp_cmd_sn)) {
3675159d763SNicholas Bellinger 			list_del_init(&cmd->i_conn_node);
368e48354ceSNicholas Bellinger 			spin_unlock_bh(&conn->cmd_lock);
369aafc9d15SNicholas Bellinger 			iscsit_free_cmd(cmd, true);
370e48354ceSNicholas Bellinger 			spin_lock_bh(&conn->cmd_lock);
371e48354ceSNicholas Bellinger 			continue;
372e48354ceSNicholas Bellinger 		}
373e48354ceSNicholas Bellinger 
374e48354ceSNicholas Bellinger 		cmd_count++;
375e48354ceSNicholas Bellinger 		pr_debug("Preparing Opcode: 0x%02x, ITT: 0x%08x,"
376e48354ceSNicholas Bellinger 			" CmdSN: 0x%08x, StatSN: 0x%08x, CID: %hu for"
37753c561dcSBart Van Assche 			" reallegiance.\n", cmd->iscsi_opcode,
378e48354ceSNicholas Bellinger 			cmd->init_task_tag, cmd->cmd_sn, cmd->stat_sn,
379e48354ceSNicholas Bellinger 			conn->cid);
380e48354ceSNicholas Bellinger 
381e48354ceSNicholas Bellinger 		cmd->deferred_i_state = cmd->i_state;
382e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_IN_CONNECTION_RECOVERY;
383e48354ceSNicholas Bellinger 
384e48354ceSNicholas Bellinger 		if (cmd->data_direction == DMA_TO_DEVICE)
385e48354ceSNicholas Bellinger 			iscsit_stop_dataout_timer(cmd);
386e48354ceSNicholas Bellinger 
387e48354ceSNicholas Bellinger 		cmd->sess = conn->sess;
388e48354ceSNicholas Bellinger 
3895159d763SNicholas Bellinger 		list_del_init(&cmd->i_conn_node);
390e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->cmd_lock);
391e48354ceSNicholas Bellinger 
392e48354ceSNicholas Bellinger 		iscsit_free_all_datain_reqs(cmd);
393e48354ceSNicholas Bellinger 
394d14921d6SNicholas Bellinger 		transport_wait_for_tasks(&cmd->se_cmd);
395e48354ceSNicholas Bellinger 		/*
39666cd9d4eSMax Gurtovoy 		 * Add the struct iscsit_cmd to the connection recovery cmd list
397e48354ceSNicholas Bellinger 		 */
398e48354ceSNicholas Bellinger 		spin_lock(&cr->conn_recovery_cmd_lock);
3992fbb471eSAndy Grover 		list_add_tail(&cmd->i_conn_node, &cr->conn_recovery_cmd_list);
400e48354ceSNicholas Bellinger 		spin_unlock(&cr->conn_recovery_cmd_lock);
401e48354ceSNicholas Bellinger 
402e48354ceSNicholas Bellinger 		spin_lock_bh(&conn->cmd_lock);
403e48354ceSNicholas Bellinger 		cmd->cr = cr;
404e48354ceSNicholas Bellinger 		cmd->conn = NULL;
405e48354ceSNicholas Bellinger 	}
406e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
407e48354ceSNicholas Bellinger 	/*
408e48354ceSNicholas Bellinger 	 * Fill in the various values in the preallocated struct iscsi_conn_recovery.
409e48354ceSNicholas Bellinger 	 */
410e48354ceSNicholas Bellinger 	cr->cid = conn->cid;
411e48354ceSNicholas Bellinger 	cr->cmd_count = cmd_count;
412e48354ceSNicholas Bellinger 	cr->maxrecvdatasegmentlength = conn->conn_ops->MaxRecvDataSegmentLength;
4131c417f39SNicholas Bellinger 	cr->maxxmitdatasegmentlength = conn->conn_ops->MaxXmitDataSegmentLength;
414e48354ceSNicholas Bellinger 	cr->sess = conn->sess;
415e48354ceSNicholas Bellinger 
416e48354ceSNicholas Bellinger 	iscsit_attach_inactive_connection_recovery_entry(conn->sess, cr);
417e48354ceSNicholas Bellinger 
418e48354ceSNicholas Bellinger 	return 0;
419e48354ceSNicholas Bellinger }
420e48354ceSNicholas Bellinger 
iscsit_connection_recovery_transport_reset(struct iscsit_conn * conn)421be36d683SMax Gurtovoy int iscsit_connection_recovery_transport_reset(struct iscsit_conn *conn)
422e48354ceSNicholas Bellinger {
423e48354ceSNicholas Bellinger 	atomic_set(&conn->connection_recovery, 1);
424e48354ceSNicholas Bellinger 
425e48354ceSNicholas Bellinger 	if (iscsit_close_connection(conn) < 0)
426e48354ceSNicholas Bellinger 		return -1;
427e48354ceSNicholas Bellinger 
428e48354ceSNicholas Bellinger 	return 0;
429e48354ceSNicholas Bellinger }
430