1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e48354ceSNicholas Bellinger /*******************************************************************************
3e48354ceSNicholas Bellinger  * This file contains the iSCSI Target specific utility 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 <linux/list.h>
1210e9cbb6SMatthew Wilcox #include <linux/sched/signal.h>
138dcf07beSBart Van Assche #include <net/ipv6.h>         /* ipv6_addr_equal() */
14e48354ceSNicholas Bellinger #include <scsi/scsi_tcq.h>
15e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
16e48354ceSNicholas Bellinger #include <target/target_core_base.h>
17c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
18baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
19e48354ceSNicholas Bellinger 
2067f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
21e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
22e48354ceSNicholas Bellinger #include "iscsi_target_seq_pdu_list.h"
23e48354ceSNicholas Bellinger #include "iscsi_target_datain_values.h"
24e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
25e48354ceSNicholas Bellinger #include "iscsi_target_erl1.h"
26e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h"
27e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
28e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
29e48354ceSNicholas Bellinger #include "iscsi_target.h"
30e48354ceSNicholas Bellinger 
31e48354ceSNicholas Bellinger extern struct list_head g_tiqn_list;
32e48354ceSNicholas Bellinger extern spinlock_t tiqn_lock;
33e48354ceSNicholas Bellinger 
iscsit_add_r2t_to_list(struct iscsit_cmd * cmd,u32 offset,u32 xfer_len,int recovery,u32 r2t_sn)34e48354ceSNicholas Bellinger int iscsit_add_r2t_to_list(
3566cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
36e48354ceSNicholas Bellinger 	u32 offset,
37e48354ceSNicholas Bellinger 	u32 xfer_len,
38e48354ceSNicholas Bellinger 	int recovery,
39e48354ceSNicholas Bellinger 	u32 r2t_sn)
40e48354ceSNicholas Bellinger {
41e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t;
42e48354ceSNicholas Bellinger 
43618baaf7SBart Van Assche 	lockdep_assert_held(&cmd->r2t_lock);
44618baaf7SBart Van Assche 
4596e8e26dSBart Van Assche 	WARN_ON_ONCE((s32)xfer_len < 0);
4696e8e26dSBart Van Assche 
47e48354ceSNicholas Bellinger 	r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC);
48e48354ceSNicholas Bellinger 	if (!r2t) {
49e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for struct iscsi_r2t.\n");
50e48354ceSNicholas Bellinger 		return -1;
51e48354ceSNicholas Bellinger 	}
52e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&r2t->r2t_list);
53e48354ceSNicholas Bellinger 
54e48354ceSNicholas Bellinger 	r2t->recovery_r2t = recovery;
55e48354ceSNicholas Bellinger 	r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn;
56e48354ceSNicholas Bellinger 	r2t->offset = offset;
57e48354ceSNicholas Bellinger 	r2t->xfer_len = xfer_len;
58e48354ceSNicholas Bellinger 	list_add_tail(&r2t->r2t_list, &cmd->cmd_r2t_list);
59e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
60e48354ceSNicholas Bellinger 
61e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
62e48354ceSNicholas Bellinger 
63e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
64e48354ceSNicholas Bellinger 	return 0;
65e48354ceSNicholas Bellinger }
66e48354ceSNicholas Bellinger 
iscsit_get_r2t_for_eos(struct iscsit_cmd * cmd,u32 offset,u32 length)67e48354ceSNicholas Bellinger struct iscsi_r2t *iscsit_get_r2t_for_eos(
6866cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
69e48354ceSNicholas Bellinger 	u32 offset,
70e48354ceSNicholas Bellinger 	u32 length)
71e48354ceSNicholas Bellinger {
72e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t;
73e48354ceSNicholas Bellinger 
74e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
75e48354ceSNicholas Bellinger 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
76e48354ceSNicholas Bellinger 		if ((r2t->offset <= offset) &&
77e48354ceSNicholas Bellinger 		    (r2t->offset + r2t->xfer_len) >= (offset + length)) {
78e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->r2t_lock);
79e48354ceSNicholas Bellinger 			return r2t;
80e48354ceSNicholas Bellinger 		}
81e48354ceSNicholas Bellinger 	}
82e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
83e48354ceSNicholas Bellinger 
84e48354ceSNicholas Bellinger 	pr_err("Unable to locate R2T for Offset: %u, Length:"
85e48354ceSNicholas Bellinger 			" %u\n", offset, length);
86e48354ceSNicholas Bellinger 	return NULL;
87e48354ceSNicholas Bellinger }
88e48354ceSNicholas Bellinger 
iscsit_get_r2t_from_list(struct iscsit_cmd * cmd)8966cd9d4eSMax Gurtovoy struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsit_cmd *cmd)
90e48354ceSNicholas Bellinger {
91e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t;
92e48354ceSNicholas Bellinger 
93e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
94e48354ceSNicholas Bellinger 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
95e48354ceSNicholas Bellinger 		if (!r2t->sent_r2t) {
96e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->r2t_lock);
97e48354ceSNicholas Bellinger 			return r2t;
98e48354ceSNicholas Bellinger 		}
99e48354ceSNicholas Bellinger 	}
100e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
101e48354ceSNicholas Bellinger 
102e48354ceSNicholas Bellinger 	pr_err("Unable to locate next R2T to send for ITT:"
103e48354ceSNicholas Bellinger 			" 0x%08x.\n", cmd->init_task_tag);
104e48354ceSNicholas Bellinger 	return NULL;
105e48354ceSNicholas Bellinger }
106e48354ceSNicholas Bellinger 
iscsit_free_r2t(struct iscsi_r2t * r2t,struct iscsit_cmd * cmd)10766cd9d4eSMax Gurtovoy void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsit_cmd *cmd)
108e48354ceSNicholas Bellinger {
109618baaf7SBart Van Assche 	lockdep_assert_held(&cmd->r2t_lock);
110618baaf7SBart Van Assche 
111e48354ceSNicholas Bellinger 	list_del(&r2t->r2t_list);
112e48354ceSNicholas Bellinger 	kmem_cache_free(lio_r2t_cache, r2t);
113e48354ceSNicholas Bellinger }
114e48354ceSNicholas Bellinger 
iscsit_free_r2ts_from_list(struct iscsit_cmd * cmd)11566cd9d4eSMax Gurtovoy void iscsit_free_r2ts_from_list(struct iscsit_cmd *cmd)
116e48354ceSNicholas Bellinger {
117e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t, *r2t_tmp;
118e48354ceSNicholas Bellinger 
119e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
120e48354ceSNicholas Bellinger 	list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list)
121e48354ceSNicholas Bellinger 		iscsit_free_r2t(r2t, cmd);
122e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
123e48354ceSNicholas Bellinger }
124e48354ceSNicholas Bellinger 
iscsit_wait_for_tag(struct se_session * se_sess,int state,int * cpup)12510e9cbb6SMatthew Wilcox static int iscsit_wait_for_tag(struct se_session *se_sess, int state, int *cpup)
12610e9cbb6SMatthew Wilcox {
12710e9cbb6SMatthew Wilcox 	int tag = -1;
1285d2ee712SJens Axboe 	DEFINE_SBQ_WAIT(wait);
12910e9cbb6SMatthew Wilcox 	struct sbq_wait_state *ws;
1305d2ee712SJens Axboe 	struct sbitmap_queue *sbq;
13110e9cbb6SMatthew Wilcox 
13210e9cbb6SMatthew Wilcox 	if (state == TASK_RUNNING)
13310e9cbb6SMatthew Wilcox 		return tag;
13410e9cbb6SMatthew Wilcox 
1355d2ee712SJens Axboe 	sbq = &se_sess->sess_tag_pool;
1365d2ee712SJens Axboe 	ws = &sbq->ws[0];
13710e9cbb6SMatthew Wilcox 	for (;;) {
1385d2ee712SJens Axboe 		sbitmap_prepare_to_wait(sbq, ws, &wait, state);
13910e9cbb6SMatthew Wilcox 		if (signal_pending_state(state, current))
14010e9cbb6SMatthew Wilcox 			break;
1415d2ee712SJens Axboe 		tag = sbitmap_queue_get(sbq, cpup);
14210e9cbb6SMatthew Wilcox 		if (tag >= 0)
14310e9cbb6SMatthew Wilcox 			break;
14410e9cbb6SMatthew Wilcox 		schedule();
14510e9cbb6SMatthew Wilcox 	}
14610e9cbb6SMatthew Wilcox 
1475d2ee712SJens Axboe 	sbitmap_finish_wait(sbq, ws, &wait);
14810e9cbb6SMatthew Wilcox 	return tag;
14910e9cbb6SMatthew Wilcox }
15010e9cbb6SMatthew Wilcox 
151e48354ceSNicholas Bellinger /*
152e48354ceSNicholas Bellinger  * May be called from software interrupt (timer) context for allocating
153e48354ceSNicholas Bellinger  * iSCSI NopINs.
154e48354ceSNicholas Bellinger  */
iscsit_allocate_cmd(struct iscsit_conn * conn,int state)155be36d683SMax Gurtovoy struct iscsit_cmd *iscsit_allocate_cmd(struct iscsit_conn *conn, int state)
156e48354ceSNicholas Bellinger {
15766cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
158988e3a85SNicholas Bellinger 	struct se_session *se_sess = conn->sess->se_sess;
15910e9cbb6SMatthew Wilcox 	int size, tag, cpu;
160e48354ceSNicholas Bellinger 
16110e9cbb6SMatthew Wilcox 	tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
16210e9cbb6SMatthew Wilcox 	if (tag < 0)
16310e9cbb6SMatthew Wilcox 		tag = iscsit_wait_for_tag(se_sess, state, &cpu);
1646f6b5d1eSKent Overstreet 	if (tag < 0)
1656f6b5d1eSKent Overstreet 		return NULL;
1666f6b5d1eSKent Overstreet 
16766cd9d4eSMax Gurtovoy 	size = sizeof(struct iscsit_cmd) + conn->conn_transport->priv_size;
16866cd9d4eSMax Gurtovoy 	cmd = (struct iscsit_cmd *)(se_sess->sess_cmd_map + (tag * size));
169988e3a85SNicholas Bellinger 	memset(cmd, 0, size);
170988e3a85SNicholas Bellinger 
171988e3a85SNicholas Bellinger 	cmd->se_cmd.map_tag = tag;
17210e9cbb6SMatthew Wilcox 	cmd->se_cmd.map_cpu = cpu;
173e48354ceSNicholas Bellinger 	cmd->conn = conn;
1744412a671SBart Van Assche 	cmd->data_direction = DMA_NONE;
1752fbb471eSAndy Grover 	INIT_LIST_HEAD(&cmd->i_conn_node);
176e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&cmd->datain_list);
177e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&cmd->cmd_r2t_list);
178e48354ceSNicholas Bellinger 	spin_lock_init(&cmd->datain_lock);
179e48354ceSNicholas Bellinger 	spin_lock_init(&cmd->dataout_timeout_lock);
180e48354ceSNicholas Bellinger 	spin_lock_init(&cmd->istate_lock);
181e48354ceSNicholas Bellinger 	spin_lock_init(&cmd->error_lock);
182e48354ceSNicholas Bellinger 	spin_lock_init(&cmd->r2t_lock);
183f7c9564aSKees Cook 	timer_setup(&cmd->dataout_timer, iscsit_handle_dataout_timeout, 0);
184e48354ceSNicholas Bellinger 
185e48354ceSNicholas Bellinger 	return cmd;
186e48354ceSNicholas Bellinger }
187cdb72665SNicholas Bellinger EXPORT_SYMBOL(iscsit_allocate_cmd);
188e48354ceSNicholas Bellinger 
iscsit_get_seq_holder_for_datain(struct iscsit_cmd * cmd,u32 seq_send_order)189e48354ceSNicholas Bellinger struct iscsi_seq *iscsit_get_seq_holder_for_datain(
19066cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
191e48354ceSNicholas Bellinger 	u32 seq_send_order)
192e48354ceSNicholas Bellinger {
193e48354ceSNicholas Bellinger 	u32 i;
194e48354ceSNicholas Bellinger 
195e48354ceSNicholas Bellinger 	for (i = 0; i < cmd->seq_count; i++)
196e48354ceSNicholas Bellinger 		if (cmd->seq_list[i].seq_send_order == seq_send_order)
197e48354ceSNicholas Bellinger 			return &cmd->seq_list[i];
198e48354ceSNicholas Bellinger 
199e48354ceSNicholas Bellinger 	return NULL;
200e48354ceSNicholas Bellinger }
201e48354ceSNicholas Bellinger 
iscsit_get_seq_holder_for_r2t(struct iscsit_cmd * cmd)20266cd9d4eSMax Gurtovoy struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsit_cmd *cmd)
203e48354ceSNicholas Bellinger {
204e48354ceSNicholas Bellinger 	u32 i;
205e48354ceSNicholas Bellinger 
206e48354ceSNicholas Bellinger 	if (!cmd->seq_list) {
20766cd9d4eSMax Gurtovoy 		pr_err("struct iscsit_cmd->seq_list is NULL!\n");
208e48354ceSNicholas Bellinger 		return NULL;
209e48354ceSNicholas Bellinger 	}
210e48354ceSNicholas Bellinger 
211e48354ceSNicholas Bellinger 	for (i = 0; i < cmd->seq_count; i++) {
212e48354ceSNicholas Bellinger 		if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
213e48354ceSNicholas Bellinger 			continue;
214e48354ceSNicholas Bellinger 		if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
215e48354ceSNicholas Bellinger 			cmd->seq_send_order++;
216e48354ceSNicholas Bellinger 			return &cmd->seq_list[i];
217e48354ceSNicholas Bellinger 		}
218e48354ceSNicholas Bellinger 	}
219e48354ceSNicholas Bellinger 
220e48354ceSNicholas Bellinger 	return NULL;
221e48354ceSNicholas Bellinger }
222e48354ceSNicholas Bellinger 
iscsit_get_holder_for_r2tsn(struct iscsit_cmd * cmd,u32 r2t_sn)223e48354ceSNicholas Bellinger struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
22466cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
225e48354ceSNicholas Bellinger 	u32 r2t_sn)
226e48354ceSNicholas Bellinger {
227e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t;
228e48354ceSNicholas Bellinger 
229e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
230e48354ceSNicholas Bellinger 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
231e48354ceSNicholas Bellinger 		if (r2t->r2t_sn == r2t_sn) {
232e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->r2t_lock);
233e48354ceSNicholas Bellinger 			return r2t;
234e48354ceSNicholas Bellinger 		}
235e48354ceSNicholas Bellinger 	}
236e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
237e48354ceSNicholas Bellinger 
238e48354ceSNicholas Bellinger 	return NULL;
239e48354ceSNicholas Bellinger }
240e48354ceSNicholas Bellinger 
iscsit_check_received_cmdsn(struct iscsit_session * sess,u32 cmdsn)2410873fe44SMax Gurtovoy static inline int iscsit_check_received_cmdsn(struct iscsit_session *sess, u32 cmdsn)
242e48354ceSNicholas Bellinger {
243109e2381SRoland Dreier 	u32 max_cmdsn;
244e48354ceSNicholas Bellinger 	int ret;
245e48354ceSNicholas Bellinger 
246e48354ceSNicholas Bellinger 	/*
247e48354ceSNicholas Bellinger 	 * This is the proper method of checking received CmdSN against
248e48354ceSNicholas Bellinger 	 * ExpCmdSN and MaxCmdSN values, as well as accounting for out
249e48354ceSNicholas Bellinger 	 * or order CmdSNs due to multiple connection sessions and/or
250e48354ceSNicholas Bellinger 	 * CRC failures.
251e48354ceSNicholas Bellinger 	 */
252109e2381SRoland Dreier 	max_cmdsn = atomic_read(&sess->max_cmd_sn);
253109e2381SRoland Dreier 	if (iscsi_sna_gt(cmdsn, max_cmdsn)) {
254e48354ceSNicholas Bellinger 		pr_err("Received CmdSN: 0x%08x is greater than"
255109e2381SRoland Dreier 		       " MaxCmdSN: 0x%08x, ignoring.\n", cmdsn, max_cmdsn);
256ea7e32beSNicholas Bellinger 		ret = CMDSN_MAXCMDSN_OVERRUN;
257e48354ceSNicholas Bellinger 
258e48354ceSNicholas Bellinger 	} else if (cmdsn == sess->exp_cmd_sn) {
259e48354ceSNicholas Bellinger 		sess->exp_cmd_sn++;
260e48354ceSNicholas Bellinger 		pr_debug("Received CmdSN matches ExpCmdSN,"
261e48354ceSNicholas Bellinger 		      " incremented ExpCmdSN to: 0x%08x\n",
262e48354ceSNicholas Bellinger 		      sess->exp_cmd_sn);
263e48354ceSNicholas Bellinger 		ret = CMDSN_NORMAL_OPERATION;
264e48354ceSNicholas Bellinger 
265e48354ceSNicholas Bellinger 	} else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
266e48354ceSNicholas Bellinger 		pr_debug("Received CmdSN: 0x%08x is greater"
267e48354ceSNicholas Bellinger 		      " than ExpCmdSN: 0x%08x, not acknowledging.\n",
268e48354ceSNicholas Bellinger 		      cmdsn, sess->exp_cmd_sn);
269e48354ceSNicholas Bellinger 		ret = CMDSN_HIGHER_THAN_EXP;
270e48354ceSNicholas Bellinger 
271e48354ceSNicholas Bellinger 	} else {
272e48354ceSNicholas Bellinger 		pr_err("Received CmdSN: 0x%08x is less than"
273e48354ceSNicholas Bellinger 		       " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
274e48354ceSNicholas Bellinger 		       sess->exp_cmd_sn);
275e48354ceSNicholas Bellinger 		ret = CMDSN_LOWER_THAN_EXP;
276e48354ceSNicholas Bellinger 	}
277e48354ceSNicholas Bellinger 
278e48354ceSNicholas Bellinger 	return ret;
279e48354ceSNicholas Bellinger }
280e48354ceSNicholas Bellinger 
281e48354ceSNicholas Bellinger /*
282e48354ceSNicholas Bellinger  * Commands may be received out of order if MC/S is in use.
283e48354ceSNicholas Bellinger  * Ensure they are executed in CmdSN order.
284e48354ceSNicholas Bellinger  */
iscsit_sequence_cmd(struct iscsit_conn * conn,struct iscsit_cmd * cmd,unsigned char * buf,__be32 cmdsn)285be36d683SMax Gurtovoy int iscsit_sequence_cmd(struct iscsit_conn *conn, struct iscsit_cmd *cmd,
286561bf158SNicholas Bellinger 			unsigned char *buf, __be32 cmdsn)
287e48354ceSNicholas Bellinger {
288561bf158SNicholas Bellinger 	int ret, cmdsn_ret;
289561bf158SNicholas Bellinger 	bool reject = false;
290561bf158SNicholas Bellinger 	u8 reason = ISCSI_REASON_BOOKMARK_NO_RESOURCES;
291e48354ceSNicholas Bellinger 
292e48354ceSNicholas Bellinger 	mutex_lock(&conn->sess->cmdsn_mutex);
293e48354ceSNicholas Bellinger 
29450e5c87dSChristoph Hellwig 	cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, be32_to_cpu(cmdsn));
295e48354ceSNicholas Bellinger 	switch (cmdsn_ret) {
296e48354ceSNicholas Bellinger 	case CMDSN_NORMAL_OPERATION:
297e48354ceSNicholas Bellinger 		ret = iscsit_execute_cmd(cmd, 0);
298e48354ceSNicholas Bellinger 		if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
299e48354ceSNicholas Bellinger 			iscsit_execute_ooo_cmdsns(conn->sess);
300561bf158SNicholas Bellinger 		else if (ret < 0) {
301561bf158SNicholas Bellinger 			reject = true;
302561bf158SNicholas Bellinger 			ret = CMDSN_ERROR_CANNOT_RECOVER;
303561bf158SNicholas Bellinger 		}
304e48354ceSNicholas Bellinger 		break;
305e48354ceSNicholas Bellinger 	case CMDSN_HIGHER_THAN_EXP:
30650e5c87dSChristoph Hellwig 		ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, be32_to_cpu(cmdsn));
307561bf158SNicholas Bellinger 		if (ret < 0) {
308561bf158SNicholas Bellinger 			reject = true;
309561bf158SNicholas Bellinger 			ret = CMDSN_ERROR_CANNOT_RECOVER;
310561bf158SNicholas Bellinger 			break;
311561bf158SNicholas Bellinger 		}
312561bf158SNicholas Bellinger 		ret = CMDSN_HIGHER_THAN_EXP;
313e48354ceSNicholas Bellinger 		break;
314e48354ceSNicholas Bellinger 	case CMDSN_LOWER_THAN_EXP:
315ea7e32beSNicholas Bellinger 	case CMDSN_MAXCMDSN_OVERRUN:
316ea7e32beSNicholas Bellinger 	default:
317e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_REMOVE;
318e48354ceSNicholas Bellinger 		iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
319ea7e32beSNicholas Bellinger 		/*
320ea7e32beSNicholas Bellinger 		 * Existing callers for iscsit_sequence_cmd() will silently
321ea7e32beSNicholas Bellinger 		 * ignore commands with CMDSN_LOWER_THAN_EXP, so force this
322ea7e32beSNicholas Bellinger 		 * return for CMDSN_MAXCMDSN_OVERRUN as well..
323ea7e32beSNicholas Bellinger 		 */
324ea7e32beSNicholas Bellinger 		ret = CMDSN_LOWER_THAN_EXP;
325e48354ceSNicholas Bellinger 		break;
326e48354ceSNicholas Bellinger 	}
327e48354ceSNicholas Bellinger 	mutex_unlock(&conn->sess->cmdsn_mutex);
328e48354ceSNicholas Bellinger 
329561bf158SNicholas Bellinger 	if (reject)
330561bf158SNicholas Bellinger 		iscsit_reject_cmd(cmd, reason, buf);
331561bf158SNicholas Bellinger 
332e48354ceSNicholas Bellinger 	return ret;
333e48354ceSNicholas Bellinger }
3343e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_sequence_cmd);
335e48354ceSNicholas Bellinger 
iscsit_check_unsolicited_dataout(struct iscsit_cmd * cmd,unsigned char * buf)33666cd9d4eSMax Gurtovoy int iscsit_check_unsolicited_dataout(struct iscsit_cmd *cmd, unsigned char *buf)
337e48354ceSNicholas Bellinger {
338be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
339e48354ceSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
340e48354ceSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *) buf;
341e48354ceSNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
342e48354ceSNicholas Bellinger 
343e48354ceSNicholas Bellinger 	if (conn->sess->sess_ops->InitialR2T) {
344e48354ceSNicholas Bellinger 		pr_err("Received unexpected unsolicited data"
345e48354ceSNicholas Bellinger 			" while InitialR2T=Yes, protocol error.\n");
346e48354ceSNicholas Bellinger 		transport_send_check_condition_and_sense(se_cmd,
347e48354ceSNicholas Bellinger 				TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
348e48354ceSNicholas Bellinger 		return -1;
349e48354ceSNicholas Bellinger 	}
350e48354ceSNicholas Bellinger 
351e48354ceSNicholas Bellinger 	if ((cmd->first_burst_len + payload_length) >
352e48354ceSNicholas Bellinger 	     conn->sess->sess_ops->FirstBurstLength) {
353e48354ceSNicholas Bellinger 		pr_err("Total %u bytes exceeds FirstBurstLength: %u"
354e48354ceSNicholas Bellinger 			" for this Unsolicited DataOut Burst.\n",
355e48354ceSNicholas Bellinger 			(cmd->first_burst_len + payload_length),
356e48354ceSNicholas Bellinger 				conn->sess->sess_ops->FirstBurstLength);
357e48354ceSNicholas Bellinger 		transport_send_check_condition_and_sense(se_cmd,
358e48354ceSNicholas Bellinger 				TCM_INCORRECT_AMOUNT_OF_DATA, 0);
359e48354ceSNicholas Bellinger 		return -1;
360e48354ceSNicholas Bellinger 	}
361e48354ceSNicholas Bellinger 
362e48354ceSNicholas Bellinger 	if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
363e48354ceSNicholas Bellinger 		return 0;
364e48354ceSNicholas Bellinger 
365ebf1d95cSAndy Grover 	if (((cmd->first_burst_len + payload_length) != cmd->se_cmd.data_length) &&
366e48354ceSNicholas Bellinger 	    ((cmd->first_burst_len + payload_length) !=
367e48354ceSNicholas Bellinger 	      conn->sess->sess_ops->FirstBurstLength)) {
368e48354ceSNicholas Bellinger 		pr_err("Unsolicited non-immediate data received %u"
369e48354ceSNicholas Bellinger 			" does not equal FirstBurstLength: %u, and does"
370e48354ceSNicholas Bellinger 			" not equal ExpXferLen %u.\n",
371e48354ceSNicholas Bellinger 			(cmd->first_burst_len + payload_length),
372ebf1d95cSAndy Grover 			conn->sess->sess_ops->FirstBurstLength, cmd->se_cmd.data_length);
373e48354ceSNicholas Bellinger 		transport_send_check_condition_and_sense(se_cmd,
374e48354ceSNicholas Bellinger 				TCM_INCORRECT_AMOUNT_OF_DATA, 0);
375e48354ceSNicholas Bellinger 		return -1;
376e48354ceSNicholas Bellinger 	}
377e48354ceSNicholas Bellinger 	return 0;
378e48354ceSNicholas Bellinger }
379e48354ceSNicholas Bellinger 
iscsit_find_cmd_from_itt(struct iscsit_conn * conn,itt_t init_task_tag)38066cd9d4eSMax Gurtovoy struct iscsit_cmd *iscsit_find_cmd_from_itt(
381be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
38266c7db68SChristoph Hellwig 	itt_t init_task_tag)
383e48354ceSNicholas Bellinger {
38466cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
385e48354ceSNicholas Bellinger 
386e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
3872fbb471eSAndy Grover 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
388e48354ceSNicholas Bellinger 		if (cmd->init_task_tag == init_task_tag) {
389e48354ceSNicholas Bellinger 			spin_unlock_bh(&conn->cmd_lock);
390e48354ceSNicholas Bellinger 			return cmd;
391e48354ceSNicholas Bellinger 		}
392e48354ceSNicholas Bellinger 	}
393e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
394e48354ceSNicholas Bellinger 
395e48354ceSNicholas Bellinger 	pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
396e48354ceSNicholas Bellinger 			init_task_tag, conn->cid);
397e48354ceSNicholas Bellinger 	return NULL;
398e48354ceSNicholas Bellinger }
399e4f4e801SSagi Grimberg EXPORT_SYMBOL(iscsit_find_cmd_from_itt);
400e48354ceSNicholas Bellinger 
iscsit_find_cmd_from_itt_or_dump(struct iscsit_conn * conn,itt_t init_task_tag,u32 length)40166cd9d4eSMax Gurtovoy struct iscsit_cmd *iscsit_find_cmd_from_itt_or_dump(
402be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
40366c7db68SChristoph Hellwig 	itt_t init_task_tag,
404e48354ceSNicholas Bellinger 	u32 length)
405e48354ceSNicholas Bellinger {
40666cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
407e48354ceSNicholas Bellinger 
408e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
4092fbb471eSAndy Grover 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
4103687db88SNicholas Bellinger 		if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT)
4113687db88SNicholas Bellinger 			continue;
412e48354ceSNicholas Bellinger 		if (cmd->init_task_tag == init_task_tag) {
413e48354ceSNicholas Bellinger 			spin_unlock_bh(&conn->cmd_lock);
414e48354ceSNicholas Bellinger 			return cmd;
415e48354ceSNicholas Bellinger 		}
416e48354ceSNicholas Bellinger 	}
417e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
418e48354ceSNicholas Bellinger 
419e48354ceSNicholas Bellinger 	pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
420e48354ceSNicholas Bellinger 			" dumping payload\n", init_task_tag, conn->cid);
421e48354ceSNicholas Bellinger 	if (length)
422e48354ceSNicholas Bellinger 		iscsit_dump_data_payload(conn, length, 1);
423e48354ceSNicholas Bellinger 
424e48354ceSNicholas Bellinger 	return NULL;
425e48354ceSNicholas Bellinger }
4269a584bf9SVarun Prakash EXPORT_SYMBOL(iscsit_find_cmd_from_itt_or_dump);
427e48354ceSNicholas Bellinger 
iscsit_find_cmd_from_ttt(struct iscsit_conn * conn,u32 targ_xfer_tag)42866cd9d4eSMax Gurtovoy struct iscsit_cmd *iscsit_find_cmd_from_ttt(
429be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
430e48354ceSNicholas Bellinger 	u32 targ_xfer_tag)
431e48354ceSNicholas Bellinger {
43266cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd = NULL;
433e48354ceSNicholas Bellinger 
434e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
4352fbb471eSAndy Grover 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
436e48354ceSNicholas Bellinger 		if (cmd->targ_xfer_tag == targ_xfer_tag) {
437e48354ceSNicholas Bellinger 			spin_unlock_bh(&conn->cmd_lock);
438e48354ceSNicholas Bellinger 			return cmd;
439e48354ceSNicholas Bellinger 		}
440e48354ceSNicholas Bellinger 	}
441e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
442e48354ceSNicholas Bellinger 
443e48354ceSNicholas Bellinger 	pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
444e48354ceSNicholas Bellinger 			targ_xfer_tag, conn->cid);
445e48354ceSNicholas Bellinger 	return NULL;
446e48354ceSNicholas Bellinger }
447e48354ceSNicholas Bellinger 
iscsit_find_cmd_for_recovery(struct iscsit_session * sess,struct iscsit_cmd ** cmd_ptr,struct iscsi_conn_recovery ** cr_ptr,itt_t init_task_tag)448e48354ceSNicholas Bellinger int iscsit_find_cmd_for_recovery(
4490873fe44SMax Gurtovoy 	struct iscsit_session *sess,
45066cd9d4eSMax Gurtovoy 	struct iscsit_cmd **cmd_ptr,
451e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery **cr_ptr,
45266c7db68SChristoph Hellwig 	itt_t init_task_tag)
453e48354ceSNicholas Bellinger {
45466cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd = NULL;
455e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr;
456e48354ceSNicholas Bellinger 	/*
457e48354ceSNicholas Bellinger 	 * Scan through the inactive connection recovery list's command list.
458e48354ceSNicholas Bellinger 	 * If init_task_tag matches the command is still alligent.
459e48354ceSNicholas Bellinger 	 */
460e48354ceSNicholas Bellinger 	spin_lock(&sess->cr_i_lock);
461e48354ceSNicholas Bellinger 	list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
462e48354ceSNicholas Bellinger 		spin_lock(&cr->conn_recovery_cmd_lock);
4632fbb471eSAndy Grover 		list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
464e48354ceSNicholas Bellinger 			if (cmd->init_task_tag == init_task_tag) {
465e48354ceSNicholas Bellinger 				spin_unlock(&cr->conn_recovery_cmd_lock);
466e48354ceSNicholas Bellinger 				spin_unlock(&sess->cr_i_lock);
467e48354ceSNicholas Bellinger 
468e48354ceSNicholas Bellinger 				*cr_ptr = cr;
469e48354ceSNicholas Bellinger 				*cmd_ptr = cmd;
470e48354ceSNicholas Bellinger 				return -2;
471e48354ceSNicholas Bellinger 			}
472e48354ceSNicholas Bellinger 		}
473e48354ceSNicholas Bellinger 		spin_unlock(&cr->conn_recovery_cmd_lock);
474e48354ceSNicholas Bellinger 	}
475e48354ceSNicholas Bellinger 	spin_unlock(&sess->cr_i_lock);
476e48354ceSNicholas Bellinger 	/*
477e48354ceSNicholas Bellinger 	 * Scan through the active connection recovery list's command list.
478e48354ceSNicholas Bellinger 	 * If init_task_tag matches the command is ready to be reassigned.
479e48354ceSNicholas Bellinger 	 */
480e48354ceSNicholas Bellinger 	spin_lock(&sess->cr_a_lock);
481e48354ceSNicholas Bellinger 	list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
482e48354ceSNicholas Bellinger 		spin_lock(&cr->conn_recovery_cmd_lock);
4832fbb471eSAndy Grover 		list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
484e48354ceSNicholas Bellinger 			if (cmd->init_task_tag == init_task_tag) {
485e48354ceSNicholas Bellinger 				spin_unlock(&cr->conn_recovery_cmd_lock);
486e48354ceSNicholas Bellinger 				spin_unlock(&sess->cr_a_lock);
487e48354ceSNicholas Bellinger 
488e48354ceSNicholas Bellinger 				*cr_ptr = cr;
489e48354ceSNicholas Bellinger 				*cmd_ptr = cmd;
490e48354ceSNicholas Bellinger 				return 0;
491e48354ceSNicholas Bellinger 			}
492e48354ceSNicholas Bellinger 		}
493e48354ceSNicholas Bellinger 		spin_unlock(&cr->conn_recovery_cmd_lock);
494e48354ceSNicholas Bellinger 	}
495e48354ceSNicholas Bellinger 	spin_unlock(&sess->cr_a_lock);
496e48354ceSNicholas Bellinger 
497e48354ceSNicholas Bellinger 	return -1;
498e48354ceSNicholas Bellinger }
499e48354ceSNicholas Bellinger 
iscsit_add_cmd_to_immediate_queue(struct iscsit_cmd * cmd,struct iscsit_conn * conn,u8 state)500e48354ceSNicholas Bellinger void iscsit_add_cmd_to_immediate_queue(
50166cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
502be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
503e48354ceSNicholas Bellinger 	u8 state)
504e48354ceSNicholas Bellinger {
505e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr;
506e48354ceSNicholas Bellinger 
507e48354ceSNicholas Bellinger 	qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
508e48354ceSNicholas Bellinger 	if (!qr) {
509e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for"
510e48354ceSNicholas Bellinger 				" struct iscsi_queue_req\n");
511e48354ceSNicholas Bellinger 		return;
512e48354ceSNicholas Bellinger 	}
513e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&qr->qr_list);
514e48354ceSNicholas Bellinger 	qr->cmd = cmd;
515e48354ceSNicholas Bellinger 	qr->state = state;
516e48354ceSNicholas Bellinger 
517e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->immed_queue_lock);
518e48354ceSNicholas Bellinger 	list_add_tail(&qr->qr_list, &conn->immed_queue_list);
519e48354ceSNicholas Bellinger 	atomic_inc(&cmd->immed_queue_count);
520e48354ceSNicholas Bellinger 	atomic_set(&conn->check_immediate_queue, 1);
521e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->immed_queue_lock);
522e48354ceSNicholas Bellinger 
523d5627acbSRoland Dreier 	wake_up(&conn->queues_wq);
524e48354ceSNicholas Bellinger }
525d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_add_cmd_to_immediate_queue);
526e48354ceSNicholas Bellinger 
iscsit_get_cmd_from_immediate_queue(struct iscsit_conn * conn)527be36d683SMax Gurtovoy struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsit_conn *conn)
528e48354ceSNicholas Bellinger {
529e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr;
530e48354ceSNicholas Bellinger 
531e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->immed_queue_lock);
532e48354ceSNicholas Bellinger 	if (list_empty(&conn->immed_queue_list)) {
533e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->immed_queue_lock);
534e48354ceSNicholas Bellinger 		return NULL;
535e48354ceSNicholas Bellinger 	}
5361f981de5SRoland Dreier 	qr = list_first_entry(&conn->immed_queue_list,
5371f981de5SRoland Dreier 			      struct iscsi_queue_req, qr_list);
538e48354ceSNicholas Bellinger 
539e48354ceSNicholas Bellinger 	list_del(&qr->qr_list);
540e48354ceSNicholas Bellinger 	if (qr->cmd)
541e48354ceSNicholas Bellinger 		atomic_dec(&qr->cmd->immed_queue_count);
542e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->immed_queue_lock);
543e48354ceSNicholas Bellinger 
544e48354ceSNicholas Bellinger 	return qr;
545e48354ceSNicholas Bellinger }
546e48354ceSNicholas Bellinger 
iscsit_remove_cmd_from_immediate_queue(struct iscsit_cmd * cmd,struct iscsit_conn * conn)547e48354ceSNicholas Bellinger static void iscsit_remove_cmd_from_immediate_queue(
54866cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
549be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
550e48354ceSNicholas Bellinger {
551e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr, *qr_tmp;
552e48354ceSNicholas Bellinger 
553e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->immed_queue_lock);
554e48354ceSNicholas Bellinger 	if (!atomic_read(&cmd->immed_queue_count)) {
555e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->immed_queue_lock);
556e48354ceSNicholas Bellinger 		return;
557e48354ceSNicholas Bellinger 	}
558e48354ceSNicholas Bellinger 
559e48354ceSNicholas Bellinger 	list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
560e48354ceSNicholas Bellinger 		if (qr->cmd != cmd)
561e48354ceSNicholas Bellinger 			continue;
562e48354ceSNicholas Bellinger 
563e48354ceSNicholas Bellinger 		atomic_dec(&qr->cmd->immed_queue_count);
564e48354ceSNicholas Bellinger 		list_del(&qr->qr_list);
565e48354ceSNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
566e48354ceSNicholas Bellinger 	}
567e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->immed_queue_lock);
568e48354ceSNicholas Bellinger 
569e48354ceSNicholas Bellinger 	if (atomic_read(&cmd->immed_queue_count)) {
570e48354ceSNicholas Bellinger 		pr_err("ITT: 0x%08x immed_queue_count: %d\n",
571e48354ceSNicholas Bellinger 			cmd->init_task_tag,
572e48354ceSNicholas Bellinger 			atomic_read(&cmd->immed_queue_count));
573e48354ceSNicholas Bellinger 	}
574e48354ceSNicholas Bellinger }
575e48354ceSNicholas Bellinger 
iscsit_add_cmd_to_response_queue(struct iscsit_cmd * cmd,struct iscsit_conn * conn,u8 state)576a4467018SNicholas Bellinger int iscsit_add_cmd_to_response_queue(
57766cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
578be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
579e48354ceSNicholas Bellinger 	u8 state)
580e48354ceSNicholas Bellinger {
581e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr;
582e48354ceSNicholas Bellinger 
583e48354ceSNicholas Bellinger 	qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
584e48354ceSNicholas Bellinger 	if (!qr) {
585e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for"
586e48354ceSNicholas Bellinger 			" struct iscsi_queue_req\n");
587a4467018SNicholas Bellinger 		return -ENOMEM;
588e48354ceSNicholas Bellinger 	}
589e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&qr->qr_list);
590e48354ceSNicholas Bellinger 	qr->cmd = cmd;
591e48354ceSNicholas Bellinger 	qr->state = state;
592e48354ceSNicholas Bellinger 
593e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->response_queue_lock);
594e48354ceSNicholas Bellinger 	list_add_tail(&qr->qr_list, &conn->response_queue_list);
595e48354ceSNicholas Bellinger 	atomic_inc(&cmd->response_queue_count);
596e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->response_queue_lock);
597e48354ceSNicholas Bellinger 
598d5627acbSRoland Dreier 	wake_up(&conn->queues_wq);
599a4467018SNicholas Bellinger 	return 0;
600e48354ceSNicholas Bellinger }
601e48354ceSNicholas Bellinger 
iscsit_get_cmd_from_response_queue(struct iscsit_conn * conn)602be36d683SMax Gurtovoy struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsit_conn *conn)
603e48354ceSNicholas Bellinger {
604e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr;
605e48354ceSNicholas Bellinger 
606e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->response_queue_lock);
607e48354ceSNicholas Bellinger 	if (list_empty(&conn->response_queue_list)) {
608e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->response_queue_lock);
609e48354ceSNicholas Bellinger 		return NULL;
610e48354ceSNicholas Bellinger 	}
611e48354ceSNicholas Bellinger 
6121f981de5SRoland Dreier 	qr = list_first_entry(&conn->response_queue_list,
6131f981de5SRoland Dreier 			      struct iscsi_queue_req, qr_list);
614e48354ceSNicholas Bellinger 
615e48354ceSNicholas Bellinger 	list_del(&qr->qr_list);
616e48354ceSNicholas Bellinger 	if (qr->cmd)
617e48354ceSNicholas Bellinger 		atomic_dec(&qr->cmd->response_queue_count);
618e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->response_queue_lock);
619e48354ceSNicholas Bellinger 
620e48354ceSNicholas Bellinger 	return qr;
621e48354ceSNicholas Bellinger }
622e48354ceSNicholas Bellinger 
iscsit_remove_cmd_from_response_queue(struct iscsit_cmd * cmd,struct iscsit_conn * conn)623e48354ceSNicholas Bellinger static void iscsit_remove_cmd_from_response_queue(
62466cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
625be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
626e48354ceSNicholas Bellinger {
627e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr, *qr_tmp;
628e48354ceSNicholas Bellinger 
629e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->response_queue_lock);
630e48354ceSNicholas Bellinger 	if (!atomic_read(&cmd->response_queue_count)) {
631e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->response_queue_lock);
632e48354ceSNicholas Bellinger 		return;
633e48354ceSNicholas Bellinger 	}
634e48354ceSNicholas Bellinger 
635e48354ceSNicholas Bellinger 	list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
636e48354ceSNicholas Bellinger 				qr_list) {
637e48354ceSNicholas Bellinger 		if (qr->cmd != cmd)
638e48354ceSNicholas Bellinger 			continue;
639e48354ceSNicholas Bellinger 
640e48354ceSNicholas Bellinger 		atomic_dec(&qr->cmd->response_queue_count);
641e48354ceSNicholas Bellinger 		list_del(&qr->qr_list);
642e48354ceSNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
643e48354ceSNicholas Bellinger 	}
644e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->response_queue_lock);
645e48354ceSNicholas Bellinger 
646e48354ceSNicholas Bellinger 	if (atomic_read(&cmd->response_queue_count)) {
647e48354ceSNicholas Bellinger 		pr_err("ITT: 0x%08x response_queue_count: %d\n",
648e48354ceSNicholas Bellinger 			cmd->init_task_tag,
649e48354ceSNicholas Bellinger 			atomic_read(&cmd->response_queue_count));
650e48354ceSNicholas Bellinger 	}
651e48354ceSNicholas Bellinger }
652e48354ceSNicholas Bellinger 
iscsit_conn_all_queues_empty(struct iscsit_conn * conn)653be36d683SMax Gurtovoy bool iscsit_conn_all_queues_empty(struct iscsit_conn *conn)
654d5627acbSRoland Dreier {
655d5627acbSRoland Dreier 	bool empty;
656d5627acbSRoland Dreier 
657d5627acbSRoland Dreier 	spin_lock_bh(&conn->immed_queue_lock);
658d5627acbSRoland Dreier 	empty = list_empty(&conn->immed_queue_list);
659d5627acbSRoland Dreier 	spin_unlock_bh(&conn->immed_queue_lock);
660d5627acbSRoland Dreier 
661d5627acbSRoland Dreier 	if (!empty)
662d5627acbSRoland Dreier 		return empty;
663d5627acbSRoland Dreier 
664d5627acbSRoland Dreier 	spin_lock_bh(&conn->response_queue_lock);
665d5627acbSRoland Dreier 	empty = list_empty(&conn->response_queue_list);
666d5627acbSRoland Dreier 	spin_unlock_bh(&conn->response_queue_lock);
667d5627acbSRoland Dreier 
668d5627acbSRoland Dreier 	return empty;
669d5627acbSRoland Dreier }
670d5627acbSRoland Dreier 
iscsit_free_queue_reqs_for_conn(struct iscsit_conn * conn)671be36d683SMax Gurtovoy void iscsit_free_queue_reqs_for_conn(struct iscsit_conn *conn)
672e48354ceSNicholas Bellinger {
673e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr, *qr_tmp;
674e48354ceSNicholas Bellinger 
675e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->immed_queue_lock);
676e48354ceSNicholas Bellinger 	list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
677e48354ceSNicholas Bellinger 		list_del(&qr->qr_list);
678e48354ceSNicholas Bellinger 		if (qr->cmd)
679e48354ceSNicholas Bellinger 			atomic_dec(&qr->cmd->immed_queue_count);
680e48354ceSNicholas Bellinger 
681e48354ceSNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
682e48354ceSNicholas Bellinger 	}
683e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->immed_queue_lock);
684e48354ceSNicholas Bellinger 
685e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->response_queue_lock);
686e48354ceSNicholas Bellinger 	list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
687e48354ceSNicholas Bellinger 			qr_list) {
688e48354ceSNicholas Bellinger 		list_del(&qr->qr_list);
689e48354ceSNicholas Bellinger 		if (qr->cmd)
690e48354ceSNicholas Bellinger 			atomic_dec(&qr->cmd->response_queue_count);
691e48354ceSNicholas Bellinger 
692e48354ceSNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
693e48354ceSNicholas Bellinger 	}
694e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->response_queue_lock);
695e48354ceSNicholas Bellinger }
696e48354ceSNicholas Bellinger 
iscsit_release_cmd(struct iscsit_cmd * cmd)69766cd9d4eSMax Gurtovoy void iscsit_release_cmd(struct iscsit_cmd *cmd)
698e48354ceSNicholas Bellinger {
6990873fe44SMax Gurtovoy 	struct iscsit_session *sess;
700988e3a85SNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
701988e3a85SNicholas Bellinger 
7026eaf69e4SBart Van Assche 	WARN_ON(!list_empty(&cmd->i_conn_node));
7036eaf69e4SBart Van Assche 
704988e3a85SNicholas Bellinger 	if (cmd->conn)
705988e3a85SNicholas Bellinger 		sess = cmd->conn->sess;
706988e3a85SNicholas Bellinger 	else
707988e3a85SNicholas Bellinger 		sess = cmd->sess;
708988e3a85SNicholas Bellinger 
709988e3a85SNicholas Bellinger 	BUG_ON(!sess || !sess->se_sess);
710988e3a85SNicholas Bellinger 
711e48354ceSNicholas Bellinger 	kfree(cmd->buf_ptr);
712e48354ceSNicholas Bellinger 	kfree(cmd->pdu_list);
713e48354ceSNicholas Bellinger 	kfree(cmd->seq_list);
714e48354ceSNicholas Bellinger 	kfree(cmd->tmr_req);
7150ca650c1SBart Van Assche 	kfree(cmd->overflow_buf);
716e48354ceSNicholas Bellinger 	kfree(cmd->iov_data);
7179864ca9dSNicholas Bellinger 	kfree(cmd->text_in_ptr);
718e48354ceSNicholas Bellinger 
71983c2b54bSMatthew Wilcox 	target_free_tag(sess->se_sess, se_cmd);
720e48354ceSNicholas Bellinger }
721d703ce2fSNicholas Bellinger EXPORT_SYMBOL(iscsit_release_cmd);
722e48354ceSNicholas Bellinger 
__iscsit_free_cmd(struct iscsit_cmd * cmd,bool check_queues)72366cd9d4eSMax Gurtovoy void __iscsit_free_cmd(struct iscsit_cmd *cmd, bool check_queues)
724d270190aSNicholas Bellinger {
725be36d683SMax Gurtovoy 	struct iscsit_conn *conn = cmd->conn;
726aafc9d15SNicholas Bellinger 
7276eaf69e4SBart Van Assche 	WARN_ON(!list_empty(&cmd->i_conn_node));
7286eaf69e4SBart Van Assche 
729aafc9d15SNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE) {
730aafc9d15SNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
731aafc9d15SNicholas Bellinger 		iscsit_free_r2ts_from_list(cmd);
732aafc9d15SNicholas Bellinger 	}
733aafc9d15SNicholas Bellinger 	if (cmd->data_direction == DMA_FROM_DEVICE)
734aafc9d15SNicholas Bellinger 		iscsit_free_all_datain_reqs(cmd);
735aafc9d15SNicholas Bellinger 
736aafc9d15SNicholas Bellinger 	if (conn && check_queues) {
737aafc9d15SNicholas Bellinger 		iscsit_remove_cmd_from_immediate_queue(cmd, conn);
738aafc9d15SNicholas Bellinger 		iscsit_remove_cmd_from_response_queue(cmd, conn);
739aafc9d15SNicholas Bellinger 	}
7407ec811a8SVarun Prakash 
7411e65cc16SBart Van Assche 	if (conn && conn->conn_transport->iscsit_unmap_cmd)
7421e65cc16SBart Van Assche 		conn->conn_transport->iscsit_unmap_cmd(conn, cmd);
743aafc9d15SNicholas Bellinger }
744aafc9d15SNicholas Bellinger 
iscsit_free_cmd(struct iscsit_cmd * cmd,bool shutdown)74566cd9d4eSMax Gurtovoy void iscsit_free_cmd(struct iscsit_cmd *cmd, bool shutdown)
746aafc9d15SNicholas Bellinger {
747d1c26857SBart Van Assche 	struct se_cmd *se_cmd = cmd->se_cmd.se_tfo ? &cmd->se_cmd : NULL;
748aafc9d15SNicholas Bellinger 	int rc;
7494412a671SBart Van Assche 
750b0055acaSBart Van Assche 	WARN_ON(!list_empty(&cmd->i_conn_node));
751b0055acaSBart Van Assche 
7524412a671SBart Van Assche 	__iscsit_free_cmd(cmd, shutdown);
753d1c26857SBart Van Assche 	if (se_cmd) {
754efb2ea77SNicholas Bellinger 		rc = transport_generic_free_cmd(se_cmd, shutdown);
755c8a75afbSBart Van Assche 		if (!rc && shutdown && se_cmd->se_sess) {
756c8a75afbSBart Van Assche 			__iscsit_free_cmd(cmd, shutdown);
757afc16604SBart Van Assche 			target_put_sess_cmd(se_cmd);
758c8a75afbSBart Van Assche 		}
759d1c26857SBart Van Assche 	} else {
760d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
761d270190aSNicholas Bellinger 	}
762d270190aSNicholas Bellinger }
763d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_free_cmd);
764d270190aSNicholas Bellinger 
iscsit_check_session_usage_count(struct iscsit_session * sess,bool can_sleep)7650873fe44SMax Gurtovoy bool iscsit_check_session_usage_count(struct iscsit_session *sess,
766f88a10f8SSebastian Andrzej Siewior 				      bool can_sleep)
767e48354ceSNicholas Bellinger {
768e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->session_usage_lock);
769e48354ceSNicholas Bellinger 	if (sess->session_usage_count != 0) {
770e48354ceSNicholas Bellinger 		sess->session_waiting_on_uc = 1;
771e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->session_usage_lock);
772efc9d730SSebastian Andrzej Siewior 		if (!can_sleep)
773f88a10f8SSebastian Andrzej Siewior 			return true;
774e48354ceSNicholas Bellinger 
775e48354ceSNicholas Bellinger 		wait_for_completion(&sess->session_waiting_on_uc_comp);
776f88a10f8SSebastian Andrzej Siewior 		return false;
777e48354ceSNicholas Bellinger 	}
778e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->session_usage_lock);
779e48354ceSNicholas Bellinger 
780f88a10f8SSebastian Andrzej Siewior 	return false;
781e48354ceSNicholas Bellinger }
782e48354ceSNicholas Bellinger 
iscsit_dec_session_usage_count(struct iscsit_session * sess)7830873fe44SMax Gurtovoy void iscsit_dec_session_usage_count(struct iscsit_session *sess)
784e48354ceSNicholas Bellinger {
785e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->session_usage_lock);
786e48354ceSNicholas Bellinger 	sess->session_usage_count--;
787e48354ceSNicholas Bellinger 
788e48354ceSNicholas Bellinger 	if (!sess->session_usage_count && sess->session_waiting_on_uc)
789e48354ceSNicholas Bellinger 		complete(&sess->session_waiting_on_uc_comp);
790e48354ceSNicholas Bellinger 
791e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->session_usage_lock);
792e48354ceSNicholas Bellinger }
793e48354ceSNicholas Bellinger 
iscsit_inc_session_usage_count(struct iscsit_session * sess)7940873fe44SMax Gurtovoy void iscsit_inc_session_usage_count(struct iscsit_session *sess)
795e48354ceSNicholas Bellinger {
796e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->session_usage_lock);
797e48354ceSNicholas Bellinger 	sess->session_usage_count++;
798e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->session_usage_lock);
799e48354ceSNicholas Bellinger }
800e48354ceSNicholas Bellinger 
iscsit_get_conn_from_cid(struct iscsit_session * sess,u16 cid)8010873fe44SMax Gurtovoy struct iscsit_conn *iscsit_get_conn_from_cid(struct iscsit_session *sess, u16 cid)
802e48354ceSNicholas Bellinger {
803be36d683SMax Gurtovoy 	struct iscsit_conn *conn;
804e48354ceSNicholas Bellinger 
805e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
806e48354ceSNicholas Bellinger 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
807e48354ceSNicholas Bellinger 		if ((conn->cid == cid) &&
808e48354ceSNicholas Bellinger 		    (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
809e48354ceSNicholas Bellinger 			iscsit_inc_conn_usage_count(conn);
810e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
811e48354ceSNicholas Bellinger 			return conn;
812e48354ceSNicholas Bellinger 		}
813e48354ceSNicholas Bellinger 	}
814e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
815e48354ceSNicholas Bellinger 
816e48354ceSNicholas Bellinger 	return NULL;
817e48354ceSNicholas Bellinger }
818e48354ceSNicholas Bellinger 
iscsit_get_conn_from_cid_rcfr(struct iscsit_session * sess,u16 cid)8190873fe44SMax Gurtovoy struct iscsit_conn *iscsit_get_conn_from_cid_rcfr(struct iscsit_session *sess, u16 cid)
820e48354ceSNicholas Bellinger {
821be36d683SMax Gurtovoy 	struct iscsit_conn *conn;
822e48354ceSNicholas Bellinger 
823e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
824e48354ceSNicholas Bellinger 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
825e48354ceSNicholas Bellinger 		if (conn->cid == cid) {
826e48354ceSNicholas Bellinger 			iscsit_inc_conn_usage_count(conn);
827e48354ceSNicholas Bellinger 			spin_lock(&conn->state_lock);
828e48354ceSNicholas Bellinger 			atomic_set(&conn->connection_wait_rcfr, 1);
829e48354ceSNicholas Bellinger 			spin_unlock(&conn->state_lock);
830e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
831e48354ceSNicholas Bellinger 			return conn;
832e48354ceSNicholas Bellinger 		}
833e48354ceSNicholas Bellinger 	}
834e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
835e48354ceSNicholas Bellinger 
836e48354ceSNicholas Bellinger 	return NULL;
837e48354ceSNicholas Bellinger }
838e48354ceSNicholas Bellinger 
iscsit_check_conn_usage_count(struct iscsit_conn * conn)839be36d683SMax Gurtovoy void iscsit_check_conn_usage_count(struct iscsit_conn *conn)
840e48354ceSNicholas Bellinger {
841e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->conn_usage_lock);
842e48354ceSNicholas Bellinger 	if (conn->conn_usage_count != 0) {
843e48354ceSNicholas Bellinger 		conn->conn_waiting_on_uc = 1;
844e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->conn_usage_lock);
845e48354ceSNicholas Bellinger 
846e48354ceSNicholas Bellinger 		wait_for_completion(&conn->conn_waiting_on_uc_comp);
847e48354ceSNicholas Bellinger 		return;
848e48354ceSNicholas Bellinger 	}
849e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->conn_usage_lock);
850e48354ceSNicholas Bellinger }
851e48354ceSNicholas Bellinger 
iscsit_dec_conn_usage_count(struct iscsit_conn * conn)852be36d683SMax Gurtovoy void iscsit_dec_conn_usage_count(struct iscsit_conn *conn)
853e48354ceSNicholas Bellinger {
854e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->conn_usage_lock);
855e48354ceSNicholas Bellinger 	conn->conn_usage_count--;
856e48354ceSNicholas Bellinger 
857e48354ceSNicholas Bellinger 	if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
858e48354ceSNicholas Bellinger 		complete(&conn->conn_waiting_on_uc_comp);
859e48354ceSNicholas Bellinger 
860e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->conn_usage_lock);
861e48354ceSNicholas Bellinger }
862e48354ceSNicholas Bellinger 
iscsit_inc_conn_usage_count(struct iscsit_conn * conn)863be36d683SMax Gurtovoy void iscsit_inc_conn_usage_count(struct iscsit_conn *conn)
864e48354ceSNicholas Bellinger {
865e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->conn_usage_lock);
866e48354ceSNicholas Bellinger 	conn->conn_usage_count++;
867e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->conn_usage_lock);
868e48354ceSNicholas Bellinger }
869e48354ceSNicholas Bellinger 
iscsit_add_nopin(struct iscsit_conn * conn,int want_response)870be36d683SMax Gurtovoy static int iscsit_add_nopin(struct iscsit_conn *conn, int want_response)
871e48354ceSNicholas Bellinger {
872e48354ceSNicholas Bellinger 	u8 state;
87366cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd;
874e48354ceSNicholas Bellinger 
875676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_RUNNING);
876e48354ceSNicholas Bellinger 	if (!cmd)
877e48354ceSNicholas Bellinger 		return -1;
878e48354ceSNicholas Bellinger 
879e48354ceSNicholas Bellinger 	cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
880e48354ceSNicholas Bellinger 	state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
881e48354ceSNicholas Bellinger 				ISTATE_SEND_NOPIN_NO_RESPONSE;
88266c7db68SChristoph Hellwig 	cmd->init_task_tag = RESERVED_ITT;
883c1e34b64SSagi Grimberg 	cmd->targ_xfer_tag = (want_response) ?
884c1e34b64SSagi Grimberg 			     session_get_next_ttt(conn->sess) : 0xFFFFFFFF;
885e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
8862fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
887e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
888e48354ceSNicholas Bellinger 
889e48354ceSNicholas Bellinger 	if (want_response)
890e48354ceSNicholas Bellinger 		iscsit_start_nopin_response_timer(conn);
891e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
892e48354ceSNicholas Bellinger 
893e48354ceSNicholas Bellinger 	return 0;
894e48354ceSNicholas Bellinger }
895e48354ceSNicholas Bellinger 
iscsit_handle_nopin_response_timeout(struct timer_list * t)896f7c9564aSKees Cook void iscsit_handle_nopin_response_timeout(struct timer_list *t)
897e48354ceSNicholas Bellinger {
898be36d683SMax Gurtovoy 	struct iscsit_conn *conn = from_timer(conn, t, nopin_response_timer);
8990873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
900e48354ceSNicholas Bellinger 
901e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
902e48354ceSNicholas Bellinger 
903e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
904e48354ceSNicholas Bellinger 	if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
905e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
906e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn);
907e48354ceSNicholas Bellinger 		return;
908e48354ceSNicholas Bellinger 	}
909e48354ceSNicholas Bellinger 
910c62ae300SDavid Disseldorp 	pr_err("Did not receive response to NOPIN on CID: %hu, failing"
911c62ae300SDavid Disseldorp 		" connection for I_T Nexus %s,i,0x%6phN,%s,t,0x%02x\n",
912c62ae300SDavid Disseldorp 		conn->cid, sess->sess_ops->InitiatorName, sess->isid,
913c62ae300SDavid Disseldorp 		sess->tpg->tpg_tiqn->tiqn, (u32)sess->tpg->tpgt);
914e48354ceSNicholas Bellinger 	conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
915e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
916e48354ceSNicholas Bellinger 
917dce6190cSDavid Disseldorp 	iscsit_fill_cxn_timeout_err_stats(sess);
918e48354ceSNicholas Bellinger 	iscsit_cause_connection_reinstatement(conn, 0);
919e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
920e48354ceSNicholas Bellinger }
921e48354ceSNicholas Bellinger 
iscsit_mod_nopin_response_timer(struct iscsit_conn * conn)922be36d683SMax Gurtovoy void iscsit_mod_nopin_response_timer(struct iscsit_conn *conn)
923e48354ceSNicholas Bellinger {
9240873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
925e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
926e48354ceSNicholas Bellinger 
927e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
928e48354ceSNicholas Bellinger 	if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
929e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
930e48354ceSNicholas Bellinger 		return;
931e48354ceSNicholas Bellinger 	}
932e48354ceSNicholas Bellinger 
933e48354ceSNicholas Bellinger 	mod_timer(&conn->nopin_response_timer,
934e48354ceSNicholas Bellinger 		(get_jiffies_64() + na->nopin_response_timeout * HZ));
935e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
936e48354ceSNicholas Bellinger }
937e48354ceSNicholas Bellinger 
iscsit_start_nopin_response_timer(struct iscsit_conn * conn)938be36d683SMax Gurtovoy void iscsit_start_nopin_response_timer(struct iscsit_conn *conn)
939e48354ceSNicholas Bellinger {
9400873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
941e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
942e48354ceSNicholas Bellinger 
943e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
944e48354ceSNicholas Bellinger 	if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
945e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
946e48354ceSNicholas Bellinger 		return;
947e48354ceSNicholas Bellinger 	}
948e48354ceSNicholas Bellinger 
949e48354ceSNicholas Bellinger 	conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
950e48354ceSNicholas Bellinger 	conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
9518a47aa9dSBart Van Assche 	mod_timer(&conn->nopin_response_timer,
9528a47aa9dSBart Van Assche 		  jiffies + na->nopin_response_timeout * HZ);
953e48354ceSNicholas Bellinger 
954e48354ceSNicholas Bellinger 	pr_debug("Started NOPIN Response Timer on CID: %d to %u"
955e48354ceSNicholas Bellinger 		" seconds\n", conn->cid, na->nopin_response_timeout);
956e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
957e48354ceSNicholas Bellinger }
958e48354ceSNicholas Bellinger 
iscsit_stop_nopin_response_timer(struct iscsit_conn * conn)959be36d683SMax Gurtovoy void iscsit_stop_nopin_response_timer(struct iscsit_conn *conn)
960e48354ceSNicholas Bellinger {
961e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
962e48354ceSNicholas Bellinger 	if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
963e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
964e48354ceSNicholas Bellinger 		return;
965e48354ceSNicholas Bellinger 	}
966e48354ceSNicholas Bellinger 	conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
967e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
968e48354ceSNicholas Bellinger 
969e48354ceSNicholas Bellinger 	del_timer_sync(&conn->nopin_response_timer);
970e48354ceSNicholas Bellinger 
971e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
972e48354ceSNicholas Bellinger 	conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
973e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
974e48354ceSNicholas Bellinger }
975e48354ceSNicholas Bellinger 
iscsit_handle_nopin_timeout(struct timer_list * t)976f7c9564aSKees Cook void iscsit_handle_nopin_timeout(struct timer_list *t)
977e48354ceSNicholas Bellinger {
978be36d683SMax Gurtovoy 	struct iscsit_conn *conn = from_timer(conn, t, nopin_timer);
979e48354ceSNicholas Bellinger 
980e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
981e48354ceSNicholas Bellinger 
982e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
983e48354ceSNicholas Bellinger 	if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
984e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
985e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn);
986e48354ceSNicholas Bellinger 		return;
987e48354ceSNicholas Bellinger 	}
988e48354ceSNicholas Bellinger 	conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
989e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
990e48354ceSNicholas Bellinger 
991e48354ceSNicholas Bellinger 	iscsit_add_nopin(conn, 1);
992e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
993e48354ceSNicholas Bellinger }
994e48354ceSNicholas Bellinger 
__iscsit_start_nopin_timer(struct iscsit_conn * conn)995be36d683SMax Gurtovoy void __iscsit_start_nopin_timer(struct iscsit_conn *conn)
996e48354ceSNicholas Bellinger {
9970873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
998e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
999618baaf7SBart Van Assche 
1000618baaf7SBart Van Assche 	lockdep_assert_held(&conn->nopin_timer_lock);
1001618baaf7SBart Van Assche 
1002e48354ceSNicholas Bellinger 	/*
1003e48354ceSNicholas Bellinger 	* NOPIN timeout is disabled.
1004e48354ceSNicholas Bellinger 	 */
1005e48354ceSNicholas Bellinger 	if (!na->nopin_timeout)
1006e48354ceSNicholas Bellinger 		return;
1007e48354ceSNicholas Bellinger 
1008e48354ceSNicholas Bellinger 	if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
1009e48354ceSNicholas Bellinger 		return;
1010e48354ceSNicholas Bellinger 
1011e48354ceSNicholas Bellinger 	conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1012e48354ceSNicholas Bellinger 	conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
10138a47aa9dSBart Van Assche 	mod_timer(&conn->nopin_timer, jiffies + na->nopin_timeout * HZ);
1014e48354ceSNicholas Bellinger 
1015e48354ceSNicholas Bellinger 	pr_debug("Started NOPIN Timer on CID: %d at %u second"
1016e48354ceSNicholas Bellinger 		" interval\n", conn->cid, na->nopin_timeout);
1017e48354ceSNicholas Bellinger }
1018e48354ceSNicholas Bellinger 
iscsit_start_nopin_timer(struct iscsit_conn * conn)1019be36d683SMax Gurtovoy void iscsit_start_nopin_timer(struct iscsit_conn *conn)
1020e48354ceSNicholas Bellinger {
1021e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
1022aeb50279SMike Christie 	__iscsit_start_nopin_timer(conn);
1023e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
1024e48354ceSNicholas Bellinger }
1025e48354ceSNicholas Bellinger 
iscsit_stop_nopin_timer(struct iscsit_conn * conn)1026be36d683SMax Gurtovoy void iscsit_stop_nopin_timer(struct iscsit_conn *conn)
1027e48354ceSNicholas Bellinger {
1028e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
1029e48354ceSNicholas Bellinger 	if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
1030e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
1031e48354ceSNicholas Bellinger 		return;
1032e48354ceSNicholas Bellinger 	}
1033e48354ceSNicholas Bellinger 	conn->nopin_timer_flags |= ISCSI_TF_STOP;
1034e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
1035e48354ceSNicholas Bellinger 
1036e48354ceSNicholas Bellinger 	del_timer_sync(&conn->nopin_timer);
1037e48354ceSNicholas Bellinger 
1038e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
1039e48354ceSNicholas Bellinger 	conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1040e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
1041e48354ceSNicholas Bellinger }
1042e48354ceSNicholas Bellinger 
iscsit_login_timeout(struct timer_list * t)104313247018SMaurizio Lombardi void iscsit_login_timeout(struct timer_list *t)
104413247018SMaurizio Lombardi {
104513247018SMaurizio Lombardi 	struct iscsit_conn *conn = from_timer(conn, t, login_timer);
104613247018SMaurizio Lombardi 	struct iscsi_login *login = conn->login;
104713247018SMaurizio Lombardi 
104813247018SMaurizio Lombardi 	pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
104913247018SMaurizio Lombardi 
105013247018SMaurizio Lombardi 	spin_lock_bh(&conn->login_timer_lock);
105113247018SMaurizio Lombardi 	login->login_failed = 1;
105213247018SMaurizio Lombardi 
105313247018SMaurizio Lombardi 	if (conn->login_kworker) {
105413247018SMaurizio Lombardi 		pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
105513247018SMaurizio Lombardi 			 conn->login_kworker->comm, conn->login_kworker->pid);
105613247018SMaurizio Lombardi 		send_sig(SIGINT, conn->login_kworker, 1);
105713247018SMaurizio Lombardi 	} else {
105813247018SMaurizio Lombardi 		schedule_delayed_work(&conn->login_work, 0);
105913247018SMaurizio Lombardi 	}
106013247018SMaurizio Lombardi 	spin_unlock_bh(&conn->login_timer_lock);
106113247018SMaurizio Lombardi }
106213247018SMaurizio Lombardi 
iscsit_start_login_timer(struct iscsit_conn * conn,struct task_struct * kthr)106313247018SMaurizio Lombardi void iscsit_start_login_timer(struct iscsit_conn *conn, struct task_struct *kthr)
106413247018SMaurizio Lombardi {
106513247018SMaurizio Lombardi 	pr_debug("Login timer started\n");
106613247018SMaurizio Lombardi 
106713247018SMaurizio Lombardi 	conn->login_kworker = kthr;
106813247018SMaurizio Lombardi 	mod_timer(&conn->login_timer, jiffies + TA_LOGIN_TIMEOUT * HZ);
106913247018SMaurizio Lombardi }
107013247018SMaurizio Lombardi 
iscsit_set_login_timer_kworker(struct iscsit_conn * conn,struct task_struct * kthr)107113247018SMaurizio Lombardi int iscsit_set_login_timer_kworker(struct iscsit_conn *conn, struct task_struct *kthr)
107213247018SMaurizio Lombardi {
107313247018SMaurizio Lombardi 	struct iscsi_login *login = conn->login;
107413247018SMaurizio Lombardi 	int ret = 0;
107513247018SMaurizio Lombardi 
107613247018SMaurizio Lombardi 	spin_lock_bh(&conn->login_timer_lock);
107713247018SMaurizio Lombardi 	if (login->login_failed) {
107813247018SMaurizio Lombardi 		/* The timer has already expired */
107913247018SMaurizio Lombardi 		ret = -1;
108013247018SMaurizio Lombardi 	} else {
108113247018SMaurizio Lombardi 		conn->login_kworker = kthr;
108213247018SMaurizio Lombardi 	}
108313247018SMaurizio Lombardi 	spin_unlock_bh(&conn->login_timer_lock);
108413247018SMaurizio Lombardi 
108513247018SMaurizio Lombardi 	return ret;
108613247018SMaurizio Lombardi }
108713247018SMaurizio Lombardi 
iscsit_stop_login_timer(struct iscsit_conn * conn)108813247018SMaurizio Lombardi void iscsit_stop_login_timer(struct iscsit_conn *conn)
108913247018SMaurizio Lombardi {
109013247018SMaurizio Lombardi 	pr_debug("Login timer stopped\n");
109113247018SMaurizio Lombardi 	timer_delete_sync(&conn->login_timer);
109213247018SMaurizio Lombardi }
109313247018SMaurizio Lombardi 
iscsit_send_tx_data(struct iscsit_cmd * cmd,struct iscsit_conn * conn,int use_misc)1094e48354ceSNicholas Bellinger int iscsit_send_tx_data(
109566cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
1096be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
1097e48354ceSNicholas Bellinger 	int use_misc)
1098e48354ceSNicholas Bellinger {
1099e48354ceSNicholas Bellinger 	int tx_sent, tx_size;
1100e48354ceSNicholas Bellinger 	u32 iov_count;
1101e48354ceSNicholas Bellinger 	struct kvec *iov;
1102e48354ceSNicholas Bellinger 
1103e48354ceSNicholas Bellinger send_data:
1104e48354ceSNicholas Bellinger 	tx_size = cmd->tx_size;
1105e48354ceSNicholas Bellinger 
1106e48354ceSNicholas Bellinger 	if (!use_misc) {
1107e48354ceSNicholas Bellinger 		iov = &cmd->iov_data[0];
1108e48354ceSNicholas Bellinger 		iov_count = cmd->iov_data_count;
1109e48354ceSNicholas Bellinger 	} else {
1110e48354ceSNicholas Bellinger 		iov = &cmd->iov_misc[0];
1111e48354ceSNicholas Bellinger 		iov_count = cmd->iov_misc_count;
1112e48354ceSNicholas Bellinger 	}
1113e48354ceSNicholas Bellinger 
1114e48354ceSNicholas Bellinger 	tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
1115e48354ceSNicholas Bellinger 	if (tx_size != tx_sent) {
1116e48354ceSNicholas Bellinger 		if (tx_sent == -EAGAIN) {
1117e48354ceSNicholas Bellinger 			pr_err("tx_data() returned -EAGAIN\n");
1118e48354ceSNicholas Bellinger 			goto send_data;
1119e48354ceSNicholas Bellinger 		} else
1120e48354ceSNicholas Bellinger 			return -1;
1121e48354ceSNicholas Bellinger 	}
1122e48354ceSNicholas Bellinger 	cmd->tx_size = 0;
1123e48354ceSNicholas Bellinger 
1124e48354ceSNicholas Bellinger 	return 0;
1125e48354ceSNicholas Bellinger }
1126e48354ceSNicholas Bellinger 
iscsit_fe_sendpage_sg(struct iscsit_cmd * cmd,struct iscsit_conn * conn)1127e48354ceSNicholas Bellinger int iscsit_fe_sendpage_sg(
112866cd9d4eSMax Gurtovoy 	struct iscsit_cmd *cmd,
1129be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
1130e48354ceSNicholas Bellinger {
1131e48354ceSNicholas Bellinger 	struct scatterlist *sg = cmd->first_data_sg;
1132*d2fe2107SDavid Howells 	struct bio_vec bvec;
1133*d2fe2107SDavid Howells 	struct msghdr msghdr = { .msg_flags = MSG_SPLICE_PAGES,	};
1134e48354ceSNicholas Bellinger 	struct kvec iov;
1135e48354ceSNicholas Bellinger 	u32 tx_hdr_size, data_len;
1136e48354ceSNicholas Bellinger 	u32 offset = cmd->first_data_sg_off;
113740b05497SNicholas Bellinger 	int tx_sent, iov_off;
1138e48354ceSNicholas Bellinger 
1139e48354ceSNicholas Bellinger send_hdr:
1140e48354ceSNicholas Bellinger 	tx_hdr_size = ISCSI_HDR_LEN;
1141e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest)
1142e48354ceSNicholas Bellinger 		tx_hdr_size += ISCSI_CRC_LEN;
1143e48354ceSNicholas Bellinger 
1144e48354ceSNicholas Bellinger 	iov.iov_base = cmd->pdu;
1145e48354ceSNicholas Bellinger 	iov.iov_len = tx_hdr_size;
1146e48354ceSNicholas Bellinger 
1147e48354ceSNicholas Bellinger 	tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
1148e48354ceSNicholas Bellinger 	if (tx_hdr_size != tx_sent) {
1149e48354ceSNicholas Bellinger 		if (tx_sent == -EAGAIN) {
1150e48354ceSNicholas Bellinger 			pr_err("tx_data() returned -EAGAIN\n");
1151e48354ceSNicholas Bellinger 			goto send_hdr;
1152e48354ceSNicholas Bellinger 		}
1153e48354ceSNicholas Bellinger 		return -1;
1154e48354ceSNicholas Bellinger 	}
1155e48354ceSNicholas Bellinger 
1156e48354ceSNicholas Bellinger 	data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
115740b05497SNicholas Bellinger 	/*
115840b05497SNicholas Bellinger 	 * Set iov_off used by padding and data digest tx_data() calls below
115940b05497SNicholas Bellinger 	 * in order to determine proper offset into cmd->iov_data[]
116040b05497SNicholas Bellinger 	 */
116140b05497SNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
1162e48354ceSNicholas Bellinger 		data_len -= ISCSI_CRC_LEN;
116340b05497SNicholas Bellinger 		if (cmd->padding)
116440b05497SNicholas Bellinger 			iov_off = (cmd->iov_data_count - 2);
116540b05497SNicholas Bellinger 		else
116640b05497SNicholas Bellinger 			iov_off = (cmd->iov_data_count - 1);
116740b05497SNicholas Bellinger 	} else {
116840b05497SNicholas Bellinger 		iov_off = (cmd->iov_data_count - 1);
116940b05497SNicholas Bellinger 	}
1170e48354ceSNicholas Bellinger 	/*
1171e48354ceSNicholas Bellinger 	 * Perform sendpage() for each page in the scatterlist
1172e48354ceSNicholas Bellinger 	 */
1173e48354ceSNicholas Bellinger 	while (data_len) {
1174e48354ceSNicholas Bellinger 		u32 space = (sg->length - offset);
1175e48354ceSNicholas Bellinger 		u32 sub_len = min_t(u32, data_len, space);
1176e48354ceSNicholas Bellinger send_pg:
1177*d2fe2107SDavid Howells 		bvec_set_page(&bvec, sg_page(sg), sub_len, sg->offset + offset);
1178*d2fe2107SDavid Howells 		iov_iter_bvec(&msghdr.msg_iter, ITER_SOURCE, &bvec, 1, sub_len);
1179*d2fe2107SDavid Howells 
1180*d2fe2107SDavid Howells 		tx_sent = conn->sock->ops->sendmsg(conn->sock, &msghdr,
1181*d2fe2107SDavid Howells 						   sub_len);
1182e48354ceSNicholas Bellinger 		if (tx_sent != sub_len) {
1183e48354ceSNicholas Bellinger 			if (tx_sent == -EAGAIN) {
1184*d2fe2107SDavid Howells 				pr_err("sendmsg/splice returned -EAGAIN\n");
1185e48354ceSNicholas Bellinger 				goto send_pg;
1186e48354ceSNicholas Bellinger 			}
1187e48354ceSNicholas Bellinger 
1188*d2fe2107SDavid Howells 			pr_err("sendmsg/splice failure: %d\n", tx_sent);
1189e48354ceSNicholas Bellinger 			return -1;
1190e48354ceSNicholas Bellinger 		}
1191e48354ceSNicholas Bellinger 
1192e48354ceSNicholas Bellinger 		data_len -= sub_len;
1193e48354ceSNicholas Bellinger 		offset = 0;
1194e48354ceSNicholas Bellinger 		sg = sg_next(sg);
1195e48354ceSNicholas Bellinger 	}
1196e48354ceSNicholas Bellinger 
1197e48354ceSNicholas Bellinger send_padding:
1198e48354ceSNicholas Bellinger 	if (cmd->padding) {
119940b05497SNicholas Bellinger 		struct kvec *iov_p = &cmd->iov_data[iov_off++];
1200e48354ceSNicholas Bellinger 
1201e48354ceSNicholas Bellinger 		tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
1202e48354ceSNicholas Bellinger 		if (cmd->padding != tx_sent) {
1203e48354ceSNicholas Bellinger 			if (tx_sent == -EAGAIN) {
1204e48354ceSNicholas Bellinger 				pr_err("tx_data() returned -EAGAIN\n");
1205e48354ceSNicholas Bellinger 				goto send_padding;
1206e48354ceSNicholas Bellinger 			}
1207e48354ceSNicholas Bellinger 			return -1;
1208e48354ceSNicholas Bellinger 		}
1209e48354ceSNicholas Bellinger 	}
1210e48354ceSNicholas Bellinger 
1211e48354ceSNicholas Bellinger send_datacrc:
1212e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
121340b05497SNicholas Bellinger 		struct kvec *iov_d = &cmd->iov_data[iov_off];
1214e48354ceSNicholas Bellinger 
1215e48354ceSNicholas Bellinger 		tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
1216e48354ceSNicholas Bellinger 		if (ISCSI_CRC_LEN != tx_sent) {
1217e48354ceSNicholas Bellinger 			if (tx_sent == -EAGAIN) {
1218e48354ceSNicholas Bellinger 				pr_err("tx_data() returned -EAGAIN\n");
1219e48354ceSNicholas Bellinger 				goto send_datacrc;
1220e48354ceSNicholas Bellinger 			}
1221e48354ceSNicholas Bellinger 			return -1;
1222e48354ceSNicholas Bellinger 		}
1223e48354ceSNicholas Bellinger 	}
1224e48354ceSNicholas Bellinger 
1225e48354ceSNicholas Bellinger 	return 0;
1226e48354ceSNicholas Bellinger }
1227e48354ceSNicholas Bellinger 
1228e48354ceSNicholas Bellinger /*
1229e48354ceSNicholas Bellinger  *      This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
1230e48354ceSNicholas Bellinger  *      back to the Initiator when an expection condition occurs with the
1231e48354ceSNicholas Bellinger  *      errors set in status_class and status_detail.
1232e48354ceSNicholas Bellinger  *
1233e48354ceSNicholas Bellinger  *      Parameters:     iSCSI Connection, Status Class, Status Detail.
1234e48354ceSNicholas Bellinger  *      Returns:        0 on success, -1 on error.
1235e48354ceSNicholas Bellinger  */
iscsit_tx_login_rsp(struct iscsit_conn * conn,u8 status_class,u8 status_detail)1236be36d683SMax Gurtovoy int iscsit_tx_login_rsp(struct iscsit_conn *conn, u8 status_class, u8 status_detail)
1237e48354ceSNicholas Bellinger {
1238e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *hdr;
1239baa4d64bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1240e48354ceSNicholas Bellinger 
1241baa4d64bSNicholas Bellinger 	login->login_failed = 1;
1242e48354ceSNicholas Bellinger 	iscsit_collect_login_stats(conn, status_class, status_detail);
1243e48354ceSNicholas Bellinger 
124468349756SNicholas Bellinger 	memset(&login->rsp[0], 0, ISCSI_HDR_LEN);
124568349756SNicholas Bellinger 
1246baa4d64bSNicholas Bellinger 	hdr	= (struct iscsi_login_rsp *)&login->rsp[0];
1247e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_LOGIN_RSP;
1248e48354ceSNicholas Bellinger 	hdr->status_class	= status_class;
1249e48354ceSNicholas Bellinger 	hdr->status_detail	= status_detail;
125066c7db68SChristoph Hellwig 	hdr->itt		= conn->login_itt;
1251e48354ceSNicholas Bellinger 
1252baa4d64bSNicholas Bellinger 	return conn->conn_transport->iscsit_put_login_tx(conn, login, 0);
1253e48354ceSNicholas Bellinger }
1254e48354ceSNicholas Bellinger 
iscsit_print_session_params(struct iscsit_session * sess)12550873fe44SMax Gurtovoy void iscsit_print_session_params(struct iscsit_session *sess)
1256e48354ceSNicholas Bellinger {
1257be36d683SMax Gurtovoy 	struct iscsit_conn *conn;
1258e48354ceSNicholas Bellinger 
1259e48354ceSNicholas Bellinger 	pr_debug("-----------------------------[Session Params for"
1260e48354ceSNicholas Bellinger 		" SID: %u]-----------------------------\n", sess->sid);
1261e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
1262e48354ceSNicholas Bellinger 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
1263e48354ceSNicholas Bellinger 		iscsi_dump_conn_ops(conn->conn_ops);
1264e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
1265e48354ceSNicholas Bellinger 
1266e48354ceSNicholas Bellinger 	iscsi_dump_sess_ops(sess->sess_ops);
1267e48354ceSNicholas Bellinger }
1268e48354ceSNicholas Bellinger 
rx_data(struct iscsit_conn * conn,struct kvec * iov,int iov_count,int data)12697c59daceSMaurizio Lombardi int rx_data(
1270be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
12717c59daceSMaurizio Lombardi 	struct kvec *iov,
12727c59daceSMaurizio Lombardi 	int iov_count,
12737c59daceSMaurizio Lombardi 	int data)
1274e48354ceSNicholas Bellinger {
12757c59daceSMaurizio Lombardi 	int rx_loop = 0, total_rx = 0;
1276e48354ceSNicholas Bellinger 	struct msghdr msg;
1277e48354ceSNicholas Bellinger 
1278e48354ceSNicholas Bellinger 	if (!conn || !conn->sock || !conn->conn_ops)
1279e48354ceSNicholas Bellinger 		return -1;
1280e48354ceSNicholas Bellinger 
1281e48354ceSNicholas Bellinger 	memset(&msg, 0, sizeof(struct msghdr));
1282de4eda9dSAl Viro 	iov_iter_kvec(&msg.msg_iter, ITER_DEST, iov, iov_count, data);
1283e48354ceSNicholas Bellinger 
12842da62906SAl Viro 	while (msg_data_left(&msg)) {
12852da62906SAl Viro 		rx_loop = sock_recvmsg(conn->sock, &msg, MSG_WAITALL);
1286e48354ceSNicholas Bellinger 		if (rx_loop <= 0) {
1287e48354ceSNicholas Bellinger 			pr_debug("rx_loop: %d total_rx: %d\n",
1288e48354ceSNicholas Bellinger 				rx_loop, total_rx);
1289e48354ceSNicholas Bellinger 			return rx_loop;
1290e48354ceSNicholas Bellinger 		}
1291e48354ceSNicholas Bellinger 		total_rx += rx_loop;
1292e48354ceSNicholas Bellinger 		pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
1293e48354ceSNicholas Bellinger 				rx_loop, total_rx, data);
1294e48354ceSNicholas Bellinger 	}
1295e48354ceSNicholas Bellinger 
1296e48354ceSNicholas Bellinger 	return total_rx;
1297e48354ceSNicholas Bellinger }
1298e48354ceSNicholas Bellinger 
tx_data(struct iscsit_conn * conn,struct kvec * iov,int iov_count,int data)1299e48354ceSNicholas Bellinger int tx_data(
1300be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
1301e48354ceSNicholas Bellinger 	struct kvec *iov,
1302e48354ceSNicholas Bellinger 	int iov_count,
1303e48354ceSNicholas Bellinger 	int data)
1304e48354ceSNicholas Bellinger {
13054113e47bSAl Viro 	struct msghdr msg;
13064113e47bSAl Viro 	int total_tx = 0;
1307e48354ceSNicholas Bellinger 
1308e48354ceSNicholas Bellinger 	if (!conn || !conn->sock || !conn->conn_ops)
1309e48354ceSNicholas Bellinger 		return -1;
1310e48354ceSNicholas Bellinger 
13114113e47bSAl Viro 	if (data <= 0) {
13124113e47bSAl Viro 		pr_err("Data length is: %d\n", data);
13134113e47bSAl Viro 		return -1;
13144113e47bSAl Viro 	}
1315e48354ceSNicholas Bellinger 
13164113e47bSAl Viro 	memset(&msg, 0, sizeof(struct msghdr));
13174113e47bSAl Viro 
1318de4eda9dSAl Viro 	iov_iter_kvec(&msg.msg_iter, ITER_SOURCE, iov, iov_count, data);
13194113e47bSAl Viro 
13204113e47bSAl Viro 	while (msg_data_left(&msg)) {
13214113e47bSAl Viro 		int tx_loop = sock_sendmsg(conn->sock, &msg);
13224113e47bSAl Viro 		if (tx_loop <= 0) {
13234113e47bSAl Viro 			pr_debug("tx_loop: %d total_tx %d\n",
13244113e47bSAl Viro 				tx_loop, total_tx);
13254113e47bSAl Viro 			return tx_loop;
13264113e47bSAl Viro 		}
13274113e47bSAl Viro 		total_tx += tx_loop;
13284113e47bSAl Viro 		pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
13294113e47bSAl Viro 					tx_loop, total_tx, data);
13304113e47bSAl Viro 	}
13314113e47bSAl Viro 
13324113e47bSAl Viro 	return total_tx;
1333e48354ceSNicholas Bellinger }
1334e48354ceSNicholas Bellinger 
iscsit_collect_login_stats(struct iscsit_conn * conn,u8 status_class,u8 status_detail)1335e48354ceSNicholas Bellinger void iscsit_collect_login_stats(
1336be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
1337e48354ceSNicholas Bellinger 	u8 status_class,
1338e48354ceSNicholas Bellinger 	u8 status_detail)
1339e48354ceSNicholas Bellinger {
1340e48354ceSNicholas Bellinger 	struct iscsi_param *intrname = NULL;
1341e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1342e48354ceSNicholas Bellinger 	struct iscsi_login_stats *ls;
1343e48354ceSNicholas Bellinger 
1344e48354ceSNicholas Bellinger 	tiqn = iscsit_snmp_get_tiqn(conn);
1345e48354ceSNicholas Bellinger 	if (!tiqn)
1346e48354ceSNicholas Bellinger 		return;
1347e48354ceSNicholas Bellinger 
1348e48354ceSNicholas Bellinger 	ls = &tiqn->login_stats;
1349e48354ceSNicholas Bellinger 
1350e48354ceSNicholas Bellinger 	spin_lock(&ls->lock);
1351e48354ceSNicholas Bellinger 	if (status_class == ISCSI_STATUS_CLS_SUCCESS)
1352e48354ceSNicholas Bellinger 		ls->accepts++;
1353e48354ceSNicholas Bellinger 	else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
1354e48354ceSNicholas Bellinger 		ls->redirects++;
1355e48354ceSNicholas Bellinger 		ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
1356e48354ceSNicholas Bellinger 	} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR)  &&
1357e48354ceSNicholas Bellinger 		 (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
1358e48354ceSNicholas Bellinger 		ls->authenticate_fails++;
1359e48354ceSNicholas Bellinger 		ls->last_fail_type =  ISCSI_LOGIN_FAIL_AUTHENTICATE;
1360e48354ceSNicholas Bellinger 	} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR)  &&
1361e48354ceSNicholas Bellinger 		 (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
1362e48354ceSNicholas Bellinger 		ls->authorize_fails++;
1363e48354ceSNicholas Bellinger 		ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
1364e48354ceSNicholas Bellinger 	} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1365e48354ceSNicholas Bellinger 		 (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
1366e48354ceSNicholas Bellinger 		ls->negotiate_fails++;
1367e48354ceSNicholas Bellinger 		ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
1368e48354ceSNicholas Bellinger 	} else {
1369e48354ceSNicholas Bellinger 		ls->other_fails++;
1370e48354ceSNicholas Bellinger 		ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
1371e48354ceSNicholas Bellinger 	}
1372e48354ceSNicholas Bellinger 
1373e48354ceSNicholas Bellinger 	/* Save initiator name, ip address and time, if it is a failed login */
1374e48354ceSNicholas Bellinger 	if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
1375e48354ceSNicholas Bellinger 		if (conn->param_list)
1376e48354ceSNicholas Bellinger 			intrname = iscsi_find_param_from_key(INITIATORNAME,
1377e48354ceSNicholas Bellinger 							     conn->param_list);
1378fdc84d11SJoern Engel 		strscpy(ls->last_intr_fail_name,
1379fdc84d11SJoern Engel 		       (intrname ? intrname->value : "Unknown"),
1380fdc84d11SJoern Engel 		       sizeof(ls->last_intr_fail_name));
1381e48354ceSNicholas Bellinger 
1382baa4d64bSNicholas Bellinger 		ls->last_intr_fail_ip_family = conn->login_family;
1383baa4d64bSNicholas Bellinger 
1384dc58f760SAndy Grover 		ls->last_intr_fail_sockaddr = conn->login_sockaddr;
1385e48354ceSNicholas Bellinger 		ls->last_fail_time = get_jiffies_64();
1386e48354ceSNicholas Bellinger 	}
1387e48354ceSNicholas Bellinger 
1388e48354ceSNicholas Bellinger 	spin_unlock(&ls->lock);
1389e48354ceSNicholas Bellinger }
1390e48354ceSNicholas Bellinger 
iscsit_snmp_get_tiqn(struct iscsit_conn * conn)1391be36d683SMax Gurtovoy struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsit_conn *conn)
1392e48354ceSNicholas Bellinger {
1393e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg;
1394e48354ceSNicholas Bellinger 
139517c61ad6SNicholas Bellinger 	if (!conn)
1396e48354ceSNicholas Bellinger 		return NULL;
1397e48354ceSNicholas Bellinger 
139817c61ad6SNicholas Bellinger 	tpg = conn->tpg;
1399e48354ceSNicholas Bellinger 	if (!tpg)
1400e48354ceSNicholas Bellinger 		return NULL;
1401e48354ceSNicholas Bellinger 
1402e48354ceSNicholas Bellinger 	if (!tpg->tpg_tiqn)
1403e48354ceSNicholas Bellinger 		return NULL;
1404e48354ceSNicholas Bellinger 
1405e48354ceSNicholas Bellinger 	return tpg->tpg_tiqn;
1406e48354ceSNicholas Bellinger }
1407dce6190cSDavid Disseldorp 
iscsit_fill_cxn_timeout_err_stats(struct iscsit_session * sess)14080873fe44SMax Gurtovoy void iscsit_fill_cxn_timeout_err_stats(struct iscsit_session *sess)
1409dce6190cSDavid Disseldorp {
1410dce6190cSDavid Disseldorp 	struct iscsi_portal_group *tpg = sess->tpg;
1411dce6190cSDavid Disseldorp 	struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
1412dce6190cSDavid Disseldorp 
1413dce6190cSDavid Disseldorp 	if (!tiqn)
1414dce6190cSDavid Disseldorp 		return;
1415dce6190cSDavid Disseldorp 
1416dce6190cSDavid Disseldorp 	spin_lock_bh(&tiqn->sess_err_stats.lock);
1417dce6190cSDavid Disseldorp 	strscpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
1418dce6190cSDavid Disseldorp 			sess->sess_ops->InitiatorName,
1419dce6190cSDavid Disseldorp 			sizeof(tiqn->sess_err_stats.last_sess_fail_rem_name));
1420dce6190cSDavid Disseldorp 	tiqn->sess_err_stats.last_sess_failure_type =
1421dce6190cSDavid Disseldorp 			ISCSI_SESS_ERR_CXN_TIMEOUT;
1422dce6190cSDavid Disseldorp 	tiqn->sess_err_stats.cxn_timeout_errors++;
1423dce6190cSDavid Disseldorp 	atomic_long_inc(&sess->conn_timeout_errors);
1424dce6190cSDavid Disseldorp 	spin_unlock_bh(&tiqn->sess_err_stats.lock);
1425dce6190cSDavid Disseldorp }
1426