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 #define PRINT_BUFF(buff, len)					\
32e48354ceSNicholas Bellinger {								\
33e48354ceSNicholas Bellinger 	int zzz;						\
34e48354ceSNicholas Bellinger 								\
35e48354ceSNicholas Bellinger 	pr_debug("%d:\n", __LINE__);				\
36e48354ceSNicholas Bellinger 	for (zzz = 0; zzz < len; zzz++) {			\
37e48354ceSNicholas Bellinger 		if (zzz % 16 == 0) {				\
38e48354ceSNicholas Bellinger 			if (zzz)				\
39e48354ceSNicholas Bellinger 				pr_debug("\n");			\
40e48354ceSNicholas Bellinger 			pr_debug("%4i: ", zzz);			\
41e48354ceSNicholas Bellinger 		}						\
42e48354ceSNicholas Bellinger 		pr_debug("%02x ", (unsigned char) (buff)[zzz]);	\
43e48354ceSNicholas Bellinger 	}							\
44e48354ceSNicholas Bellinger 	if ((len + 1) % 16)					\
45e48354ceSNicholas Bellinger 		pr_debug("\n");					\
46e48354ceSNicholas Bellinger }
47e48354ceSNicholas Bellinger 
48e48354ceSNicholas Bellinger extern struct list_head g_tiqn_list;
49e48354ceSNicholas Bellinger extern spinlock_t tiqn_lock;
50e48354ceSNicholas Bellinger 
51e48354ceSNicholas Bellinger int iscsit_add_r2t_to_list(
52e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
53e48354ceSNicholas Bellinger 	u32 offset,
54e48354ceSNicholas Bellinger 	u32 xfer_len,
55e48354ceSNicholas Bellinger 	int recovery,
56e48354ceSNicholas Bellinger 	u32 r2t_sn)
57e48354ceSNicholas Bellinger {
58e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t;
59e48354ceSNicholas Bellinger 
60618baaf7SBart Van Assche 	lockdep_assert_held(&cmd->r2t_lock);
61618baaf7SBart Van Assche 
6296e8e26dSBart Van Assche 	WARN_ON_ONCE((s32)xfer_len < 0);
6396e8e26dSBart Van Assche 
64e48354ceSNicholas Bellinger 	r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC);
65e48354ceSNicholas Bellinger 	if (!r2t) {
66e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for struct iscsi_r2t.\n");
67e48354ceSNicholas Bellinger 		return -1;
68e48354ceSNicholas Bellinger 	}
69e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&r2t->r2t_list);
70e48354ceSNicholas Bellinger 
71e48354ceSNicholas Bellinger 	r2t->recovery_r2t = recovery;
72e48354ceSNicholas Bellinger 	r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn;
73e48354ceSNicholas Bellinger 	r2t->offset = offset;
74e48354ceSNicholas Bellinger 	r2t->xfer_len = xfer_len;
75e48354ceSNicholas Bellinger 	list_add_tail(&r2t->r2t_list, &cmd->cmd_r2t_list);
76e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
77e48354ceSNicholas Bellinger 
78e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
79e48354ceSNicholas Bellinger 
80e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
81e48354ceSNicholas Bellinger 	return 0;
82e48354ceSNicholas Bellinger }
83e48354ceSNicholas Bellinger 
84e48354ceSNicholas Bellinger struct iscsi_r2t *iscsit_get_r2t_for_eos(
85e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
86e48354ceSNicholas Bellinger 	u32 offset,
87e48354ceSNicholas Bellinger 	u32 length)
88e48354ceSNicholas Bellinger {
89e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t;
90e48354ceSNicholas Bellinger 
91e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
92e48354ceSNicholas Bellinger 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
93e48354ceSNicholas Bellinger 		if ((r2t->offset <= offset) &&
94e48354ceSNicholas Bellinger 		    (r2t->offset + r2t->xfer_len) >= (offset + length)) {
95e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->r2t_lock);
96e48354ceSNicholas Bellinger 			return r2t;
97e48354ceSNicholas Bellinger 		}
98e48354ceSNicholas Bellinger 	}
99e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
100e48354ceSNicholas Bellinger 
101e48354ceSNicholas Bellinger 	pr_err("Unable to locate R2T for Offset: %u, Length:"
102e48354ceSNicholas Bellinger 			" %u\n", offset, length);
103e48354ceSNicholas Bellinger 	return NULL;
104e48354ceSNicholas Bellinger }
105e48354ceSNicholas Bellinger 
106e48354ceSNicholas Bellinger struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsi_cmd *cmd)
107e48354ceSNicholas Bellinger {
108e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t;
109e48354ceSNicholas Bellinger 
110e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
111e48354ceSNicholas Bellinger 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
112e48354ceSNicholas Bellinger 		if (!r2t->sent_r2t) {
113e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->r2t_lock);
114e48354ceSNicholas Bellinger 			return r2t;
115e48354ceSNicholas Bellinger 		}
116e48354ceSNicholas Bellinger 	}
117e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
118e48354ceSNicholas Bellinger 
119e48354ceSNicholas Bellinger 	pr_err("Unable to locate next R2T to send for ITT:"
120e48354ceSNicholas Bellinger 			" 0x%08x.\n", cmd->init_task_tag);
121e48354ceSNicholas Bellinger 	return NULL;
122e48354ceSNicholas Bellinger }
123e48354ceSNicholas Bellinger 
124e48354ceSNicholas Bellinger void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsi_cmd *cmd)
125e48354ceSNicholas Bellinger {
126618baaf7SBart Van Assche 	lockdep_assert_held(&cmd->r2t_lock);
127618baaf7SBart Van Assche 
128e48354ceSNicholas Bellinger 	list_del(&r2t->r2t_list);
129e48354ceSNicholas Bellinger 	kmem_cache_free(lio_r2t_cache, r2t);
130e48354ceSNicholas Bellinger }
131e48354ceSNicholas Bellinger 
132e48354ceSNicholas Bellinger void iscsit_free_r2ts_from_list(struct iscsi_cmd *cmd)
133e48354ceSNicholas Bellinger {
134e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t, *r2t_tmp;
135e48354ceSNicholas Bellinger 
136e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
137e48354ceSNicholas Bellinger 	list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list)
138e48354ceSNicholas Bellinger 		iscsit_free_r2t(r2t, cmd);
139e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
140e48354ceSNicholas Bellinger }
141e48354ceSNicholas Bellinger 
14210e9cbb6SMatthew Wilcox static int iscsit_wait_for_tag(struct se_session *se_sess, int state, int *cpup)
14310e9cbb6SMatthew Wilcox {
14410e9cbb6SMatthew Wilcox 	int tag = -1;
1455d2ee712SJens Axboe 	DEFINE_SBQ_WAIT(wait);
14610e9cbb6SMatthew Wilcox 	struct sbq_wait_state *ws;
1475d2ee712SJens Axboe 	struct sbitmap_queue *sbq;
14810e9cbb6SMatthew Wilcox 
14910e9cbb6SMatthew Wilcox 	if (state == TASK_RUNNING)
15010e9cbb6SMatthew Wilcox 		return tag;
15110e9cbb6SMatthew Wilcox 
1525d2ee712SJens Axboe 	sbq = &se_sess->sess_tag_pool;
1535d2ee712SJens Axboe 	ws = &sbq->ws[0];
15410e9cbb6SMatthew Wilcox 	for (;;) {
1555d2ee712SJens Axboe 		sbitmap_prepare_to_wait(sbq, ws, &wait, state);
15610e9cbb6SMatthew Wilcox 		if (signal_pending_state(state, current))
15710e9cbb6SMatthew Wilcox 			break;
1585d2ee712SJens Axboe 		tag = sbitmap_queue_get(sbq, cpup);
15910e9cbb6SMatthew Wilcox 		if (tag >= 0)
16010e9cbb6SMatthew Wilcox 			break;
16110e9cbb6SMatthew Wilcox 		schedule();
16210e9cbb6SMatthew Wilcox 	}
16310e9cbb6SMatthew Wilcox 
1645d2ee712SJens Axboe 	sbitmap_finish_wait(sbq, ws, &wait);
16510e9cbb6SMatthew Wilcox 	return tag;
16610e9cbb6SMatthew Wilcox }
16710e9cbb6SMatthew Wilcox 
168e48354ceSNicholas Bellinger /*
169e48354ceSNicholas Bellinger  * May be called from software interrupt (timer) context for allocating
170e48354ceSNicholas Bellinger  * iSCSI NopINs.
171e48354ceSNicholas Bellinger  */
172676687c6SNicholas Bellinger struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, int state)
173e48354ceSNicholas Bellinger {
174e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd;
175988e3a85SNicholas Bellinger 	struct se_session *se_sess = conn->sess->se_sess;
17610e9cbb6SMatthew Wilcox 	int size, tag, cpu;
177e48354ceSNicholas Bellinger 
17810e9cbb6SMatthew Wilcox 	tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
17910e9cbb6SMatthew Wilcox 	if (tag < 0)
18010e9cbb6SMatthew Wilcox 		tag = iscsit_wait_for_tag(se_sess, state, &cpu);
1816f6b5d1eSKent Overstreet 	if (tag < 0)
1826f6b5d1eSKent Overstreet 		return NULL;
1836f6b5d1eSKent Overstreet 
184988e3a85SNicholas Bellinger 	size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
185988e3a85SNicholas Bellinger 	cmd = (struct iscsi_cmd *)(se_sess->sess_cmd_map + (tag * size));
186988e3a85SNicholas Bellinger 	memset(cmd, 0, size);
187988e3a85SNicholas Bellinger 
188988e3a85SNicholas Bellinger 	cmd->se_cmd.map_tag = tag;
18910e9cbb6SMatthew Wilcox 	cmd->se_cmd.map_cpu = cpu;
190e48354ceSNicholas Bellinger 	cmd->conn = conn;
1914412a671SBart Van Assche 	cmd->data_direction = DMA_NONE;
1922fbb471eSAndy Grover 	INIT_LIST_HEAD(&cmd->i_conn_node);
193e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&cmd->datain_list);
194e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&cmd->cmd_r2t_list);
195e48354ceSNicholas Bellinger 	spin_lock_init(&cmd->datain_lock);
196e48354ceSNicholas Bellinger 	spin_lock_init(&cmd->dataout_timeout_lock);
197e48354ceSNicholas Bellinger 	spin_lock_init(&cmd->istate_lock);
198e48354ceSNicholas Bellinger 	spin_lock_init(&cmd->error_lock);
199e48354ceSNicholas Bellinger 	spin_lock_init(&cmd->r2t_lock);
200f7c9564aSKees Cook 	timer_setup(&cmd->dataout_timer, iscsit_handle_dataout_timeout, 0);
201e48354ceSNicholas Bellinger 
202e48354ceSNicholas Bellinger 	return cmd;
203e48354ceSNicholas Bellinger }
204cdb72665SNicholas Bellinger EXPORT_SYMBOL(iscsit_allocate_cmd);
205e48354ceSNicholas Bellinger 
206e48354ceSNicholas Bellinger struct iscsi_seq *iscsit_get_seq_holder_for_datain(
207e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
208e48354ceSNicholas Bellinger 	u32 seq_send_order)
209e48354ceSNicholas Bellinger {
210e48354ceSNicholas Bellinger 	u32 i;
211e48354ceSNicholas Bellinger 
212e48354ceSNicholas Bellinger 	for (i = 0; i < cmd->seq_count; i++)
213e48354ceSNicholas Bellinger 		if (cmd->seq_list[i].seq_send_order == seq_send_order)
214e48354ceSNicholas Bellinger 			return &cmd->seq_list[i];
215e48354ceSNicholas Bellinger 
216e48354ceSNicholas Bellinger 	return NULL;
217e48354ceSNicholas Bellinger }
218e48354ceSNicholas Bellinger 
219e48354ceSNicholas Bellinger struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
220e48354ceSNicholas Bellinger {
221e48354ceSNicholas Bellinger 	u32 i;
222e48354ceSNicholas Bellinger 
223e48354ceSNicholas Bellinger 	if (!cmd->seq_list) {
224e48354ceSNicholas Bellinger 		pr_err("struct iscsi_cmd->seq_list is NULL!\n");
225e48354ceSNicholas Bellinger 		return NULL;
226e48354ceSNicholas Bellinger 	}
227e48354ceSNicholas Bellinger 
228e48354ceSNicholas Bellinger 	for (i = 0; i < cmd->seq_count; i++) {
229e48354ceSNicholas Bellinger 		if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
230e48354ceSNicholas Bellinger 			continue;
231e48354ceSNicholas Bellinger 		if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
232e48354ceSNicholas Bellinger 			cmd->seq_send_order++;
233e48354ceSNicholas Bellinger 			return &cmd->seq_list[i];
234e48354ceSNicholas Bellinger 		}
235e48354ceSNicholas Bellinger 	}
236e48354ceSNicholas Bellinger 
237e48354ceSNicholas Bellinger 	return NULL;
238e48354ceSNicholas Bellinger }
239e48354ceSNicholas Bellinger 
240e48354ceSNicholas Bellinger struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
241e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
242e48354ceSNicholas Bellinger 	u32 r2t_sn)
243e48354ceSNicholas Bellinger {
244e48354ceSNicholas Bellinger 	struct iscsi_r2t *r2t;
245e48354ceSNicholas Bellinger 
246e48354ceSNicholas Bellinger 	spin_lock_bh(&cmd->r2t_lock);
247e48354ceSNicholas Bellinger 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
248e48354ceSNicholas Bellinger 		if (r2t->r2t_sn == r2t_sn) {
249e48354ceSNicholas Bellinger 			spin_unlock_bh(&cmd->r2t_lock);
250e48354ceSNicholas Bellinger 			return r2t;
251e48354ceSNicholas Bellinger 		}
252e48354ceSNicholas Bellinger 	}
253e48354ceSNicholas Bellinger 	spin_unlock_bh(&cmd->r2t_lock);
254e48354ceSNicholas Bellinger 
255e48354ceSNicholas Bellinger 	return NULL;
256e48354ceSNicholas Bellinger }
257e48354ceSNicholas Bellinger 
258e48354ceSNicholas Bellinger static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
259e48354ceSNicholas Bellinger {
260109e2381SRoland Dreier 	u32 max_cmdsn;
261e48354ceSNicholas Bellinger 	int ret;
262e48354ceSNicholas Bellinger 
263e48354ceSNicholas Bellinger 	/*
264e48354ceSNicholas Bellinger 	 * This is the proper method of checking received CmdSN against
265e48354ceSNicholas Bellinger 	 * ExpCmdSN and MaxCmdSN values, as well as accounting for out
266e48354ceSNicholas Bellinger 	 * or order CmdSNs due to multiple connection sessions and/or
267e48354ceSNicholas Bellinger 	 * CRC failures.
268e48354ceSNicholas Bellinger 	 */
269109e2381SRoland Dreier 	max_cmdsn = atomic_read(&sess->max_cmd_sn);
270109e2381SRoland Dreier 	if (iscsi_sna_gt(cmdsn, max_cmdsn)) {
271e48354ceSNicholas Bellinger 		pr_err("Received CmdSN: 0x%08x is greater than"
272109e2381SRoland Dreier 		       " MaxCmdSN: 0x%08x, ignoring.\n", cmdsn, max_cmdsn);
273ea7e32beSNicholas Bellinger 		ret = CMDSN_MAXCMDSN_OVERRUN;
274e48354ceSNicholas Bellinger 
275e48354ceSNicholas Bellinger 	} else if (cmdsn == sess->exp_cmd_sn) {
276e48354ceSNicholas Bellinger 		sess->exp_cmd_sn++;
277e48354ceSNicholas Bellinger 		pr_debug("Received CmdSN matches ExpCmdSN,"
278e48354ceSNicholas Bellinger 		      " incremented ExpCmdSN to: 0x%08x\n",
279e48354ceSNicholas Bellinger 		      sess->exp_cmd_sn);
280e48354ceSNicholas Bellinger 		ret = CMDSN_NORMAL_OPERATION;
281e48354ceSNicholas Bellinger 
282e48354ceSNicholas Bellinger 	} else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
283e48354ceSNicholas Bellinger 		pr_debug("Received CmdSN: 0x%08x is greater"
284e48354ceSNicholas Bellinger 		      " than ExpCmdSN: 0x%08x, not acknowledging.\n",
285e48354ceSNicholas Bellinger 		      cmdsn, sess->exp_cmd_sn);
286e48354ceSNicholas Bellinger 		ret = CMDSN_HIGHER_THAN_EXP;
287e48354ceSNicholas Bellinger 
288e48354ceSNicholas Bellinger 	} else {
289e48354ceSNicholas Bellinger 		pr_err("Received CmdSN: 0x%08x is less than"
290e48354ceSNicholas Bellinger 		       " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
291e48354ceSNicholas Bellinger 		       sess->exp_cmd_sn);
292e48354ceSNicholas Bellinger 		ret = CMDSN_LOWER_THAN_EXP;
293e48354ceSNicholas Bellinger 	}
294e48354ceSNicholas Bellinger 
295e48354ceSNicholas Bellinger 	return ret;
296e48354ceSNicholas Bellinger }
297e48354ceSNicholas Bellinger 
298e48354ceSNicholas Bellinger /*
299e48354ceSNicholas Bellinger  * Commands may be received out of order if MC/S is in use.
300e48354ceSNicholas Bellinger  * Ensure they are executed in CmdSN order.
301e48354ceSNicholas Bellinger  */
302561bf158SNicholas Bellinger int iscsit_sequence_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
303561bf158SNicholas Bellinger 			unsigned char *buf, __be32 cmdsn)
304e48354ceSNicholas Bellinger {
305561bf158SNicholas Bellinger 	int ret, cmdsn_ret;
306561bf158SNicholas Bellinger 	bool reject = false;
307561bf158SNicholas Bellinger 	u8 reason = ISCSI_REASON_BOOKMARK_NO_RESOURCES;
308e48354ceSNicholas Bellinger 
309e48354ceSNicholas Bellinger 	mutex_lock(&conn->sess->cmdsn_mutex);
310e48354ceSNicholas Bellinger 
31150e5c87dSChristoph Hellwig 	cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, be32_to_cpu(cmdsn));
312e48354ceSNicholas Bellinger 	switch (cmdsn_ret) {
313e48354ceSNicholas Bellinger 	case CMDSN_NORMAL_OPERATION:
314e48354ceSNicholas Bellinger 		ret = iscsit_execute_cmd(cmd, 0);
315e48354ceSNicholas Bellinger 		if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
316e48354ceSNicholas Bellinger 			iscsit_execute_ooo_cmdsns(conn->sess);
317561bf158SNicholas Bellinger 		else if (ret < 0) {
318561bf158SNicholas Bellinger 			reject = true;
319561bf158SNicholas Bellinger 			ret = CMDSN_ERROR_CANNOT_RECOVER;
320561bf158SNicholas Bellinger 		}
321e48354ceSNicholas Bellinger 		break;
322e48354ceSNicholas Bellinger 	case CMDSN_HIGHER_THAN_EXP:
32350e5c87dSChristoph Hellwig 		ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, be32_to_cpu(cmdsn));
324561bf158SNicholas Bellinger 		if (ret < 0) {
325561bf158SNicholas Bellinger 			reject = true;
326561bf158SNicholas Bellinger 			ret = CMDSN_ERROR_CANNOT_RECOVER;
327561bf158SNicholas Bellinger 			break;
328561bf158SNicholas Bellinger 		}
329561bf158SNicholas Bellinger 		ret = CMDSN_HIGHER_THAN_EXP;
330e48354ceSNicholas Bellinger 		break;
331e48354ceSNicholas Bellinger 	case CMDSN_LOWER_THAN_EXP:
332ea7e32beSNicholas Bellinger 	case CMDSN_MAXCMDSN_OVERRUN:
333ea7e32beSNicholas Bellinger 	default:
334e48354ceSNicholas Bellinger 		cmd->i_state = ISTATE_REMOVE;
335e48354ceSNicholas Bellinger 		iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
336ea7e32beSNicholas Bellinger 		/*
337ea7e32beSNicholas Bellinger 		 * Existing callers for iscsit_sequence_cmd() will silently
338ea7e32beSNicholas Bellinger 		 * ignore commands with CMDSN_LOWER_THAN_EXP, so force this
339ea7e32beSNicholas Bellinger 		 * return for CMDSN_MAXCMDSN_OVERRUN as well..
340ea7e32beSNicholas Bellinger 		 */
341ea7e32beSNicholas Bellinger 		ret = CMDSN_LOWER_THAN_EXP;
342e48354ceSNicholas Bellinger 		break;
343e48354ceSNicholas Bellinger 	}
344e48354ceSNicholas Bellinger 	mutex_unlock(&conn->sess->cmdsn_mutex);
345e48354ceSNicholas Bellinger 
346561bf158SNicholas Bellinger 	if (reject)
347561bf158SNicholas Bellinger 		iscsit_reject_cmd(cmd, reason, buf);
348561bf158SNicholas Bellinger 
349e48354ceSNicholas Bellinger 	return ret;
350e48354ceSNicholas Bellinger }
3513e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_sequence_cmd);
352e48354ceSNicholas Bellinger 
353e48354ceSNicholas Bellinger int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
354e48354ceSNicholas Bellinger {
355e48354ceSNicholas Bellinger 	struct iscsi_conn *conn = cmd->conn;
356e48354ceSNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
357e48354ceSNicholas Bellinger 	struct iscsi_data *hdr = (struct iscsi_data *) buf;
358e48354ceSNicholas Bellinger 	u32 payload_length = ntoh24(hdr->dlength);
359e48354ceSNicholas Bellinger 
360e48354ceSNicholas Bellinger 	if (conn->sess->sess_ops->InitialR2T) {
361e48354ceSNicholas Bellinger 		pr_err("Received unexpected unsolicited data"
362e48354ceSNicholas Bellinger 			" while InitialR2T=Yes, protocol error.\n");
363e48354ceSNicholas Bellinger 		transport_send_check_condition_and_sense(se_cmd,
364e48354ceSNicholas Bellinger 				TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
365e48354ceSNicholas Bellinger 		return -1;
366e48354ceSNicholas Bellinger 	}
367e48354ceSNicholas Bellinger 
368e48354ceSNicholas Bellinger 	if ((cmd->first_burst_len + payload_length) >
369e48354ceSNicholas Bellinger 	     conn->sess->sess_ops->FirstBurstLength) {
370e48354ceSNicholas Bellinger 		pr_err("Total %u bytes exceeds FirstBurstLength: %u"
371e48354ceSNicholas Bellinger 			" for this Unsolicited DataOut Burst.\n",
372e48354ceSNicholas Bellinger 			(cmd->first_burst_len + payload_length),
373e48354ceSNicholas Bellinger 				conn->sess->sess_ops->FirstBurstLength);
374e48354ceSNicholas Bellinger 		transport_send_check_condition_and_sense(se_cmd,
375e48354ceSNicholas Bellinger 				TCM_INCORRECT_AMOUNT_OF_DATA, 0);
376e48354ceSNicholas Bellinger 		return -1;
377e48354ceSNicholas Bellinger 	}
378e48354ceSNicholas Bellinger 
379e48354ceSNicholas Bellinger 	if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
380e48354ceSNicholas Bellinger 		return 0;
381e48354ceSNicholas Bellinger 
382ebf1d95cSAndy Grover 	if (((cmd->first_burst_len + payload_length) != cmd->se_cmd.data_length) &&
383e48354ceSNicholas Bellinger 	    ((cmd->first_burst_len + payload_length) !=
384e48354ceSNicholas Bellinger 	      conn->sess->sess_ops->FirstBurstLength)) {
385e48354ceSNicholas Bellinger 		pr_err("Unsolicited non-immediate data received %u"
386e48354ceSNicholas Bellinger 			" does not equal FirstBurstLength: %u, and does"
387e48354ceSNicholas Bellinger 			" not equal ExpXferLen %u.\n",
388e48354ceSNicholas Bellinger 			(cmd->first_burst_len + payload_length),
389ebf1d95cSAndy Grover 			conn->sess->sess_ops->FirstBurstLength, cmd->se_cmd.data_length);
390e48354ceSNicholas Bellinger 		transport_send_check_condition_and_sense(se_cmd,
391e48354ceSNicholas Bellinger 				TCM_INCORRECT_AMOUNT_OF_DATA, 0);
392e48354ceSNicholas Bellinger 		return -1;
393e48354ceSNicholas Bellinger 	}
394e48354ceSNicholas Bellinger 	return 0;
395e48354ceSNicholas Bellinger }
396e48354ceSNicholas Bellinger 
397e48354ceSNicholas Bellinger struct iscsi_cmd *iscsit_find_cmd_from_itt(
398e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
39966c7db68SChristoph Hellwig 	itt_t init_task_tag)
400e48354ceSNicholas Bellinger {
401e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd;
402e48354ceSNicholas Bellinger 
403e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
4042fbb471eSAndy Grover 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
405e48354ceSNicholas Bellinger 		if (cmd->init_task_tag == init_task_tag) {
406e48354ceSNicholas Bellinger 			spin_unlock_bh(&conn->cmd_lock);
407e48354ceSNicholas Bellinger 			return cmd;
408e48354ceSNicholas Bellinger 		}
409e48354ceSNicholas Bellinger 	}
410e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
411e48354ceSNicholas Bellinger 
412e48354ceSNicholas Bellinger 	pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
413e48354ceSNicholas Bellinger 			init_task_tag, conn->cid);
414e48354ceSNicholas Bellinger 	return NULL;
415e48354ceSNicholas Bellinger }
416e4f4e801SSagi Grimberg EXPORT_SYMBOL(iscsit_find_cmd_from_itt);
417e48354ceSNicholas Bellinger 
418e48354ceSNicholas Bellinger struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
419e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
42066c7db68SChristoph Hellwig 	itt_t init_task_tag,
421e48354ceSNicholas Bellinger 	u32 length)
422e48354ceSNicholas Bellinger {
423e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd;
424e48354ceSNicholas Bellinger 
425e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
4262fbb471eSAndy Grover 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
4273687db88SNicholas Bellinger 		if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT)
4283687db88SNicholas Bellinger 			continue;
429e48354ceSNicholas Bellinger 		if (cmd->init_task_tag == init_task_tag) {
430e48354ceSNicholas Bellinger 			spin_unlock_bh(&conn->cmd_lock);
431e48354ceSNicholas Bellinger 			return cmd;
432e48354ceSNicholas Bellinger 		}
433e48354ceSNicholas Bellinger 	}
434e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
435e48354ceSNicholas Bellinger 
436e48354ceSNicholas Bellinger 	pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
437e48354ceSNicholas Bellinger 			" dumping payload\n", init_task_tag, conn->cid);
438e48354ceSNicholas Bellinger 	if (length)
439e48354ceSNicholas Bellinger 		iscsit_dump_data_payload(conn, length, 1);
440e48354ceSNicholas Bellinger 
441e48354ceSNicholas Bellinger 	return NULL;
442e48354ceSNicholas Bellinger }
4439a584bf9SVarun Prakash EXPORT_SYMBOL(iscsit_find_cmd_from_itt_or_dump);
444e48354ceSNicholas Bellinger 
445e48354ceSNicholas Bellinger struct iscsi_cmd *iscsit_find_cmd_from_ttt(
446e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
447e48354ceSNicholas Bellinger 	u32 targ_xfer_tag)
448e48354ceSNicholas Bellinger {
449e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
450e48354ceSNicholas Bellinger 
451e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
4522fbb471eSAndy Grover 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
453e48354ceSNicholas Bellinger 		if (cmd->targ_xfer_tag == targ_xfer_tag) {
454e48354ceSNicholas Bellinger 			spin_unlock_bh(&conn->cmd_lock);
455e48354ceSNicholas Bellinger 			return cmd;
456e48354ceSNicholas Bellinger 		}
457e48354ceSNicholas Bellinger 	}
458e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
459e48354ceSNicholas Bellinger 
460e48354ceSNicholas Bellinger 	pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
461e48354ceSNicholas Bellinger 			targ_xfer_tag, conn->cid);
462e48354ceSNicholas Bellinger 	return NULL;
463e48354ceSNicholas Bellinger }
464e48354ceSNicholas Bellinger 
465e48354ceSNicholas Bellinger int iscsit_find_cmd_for_recovery(
466e48354ceSNicholas Bellinger 	struct iscsi_session *sess,
467e48354ceSNicholas Bellinger 	struct iscsi_cmd **cmd_ptr,
468e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery **cr_ptr,
46966c7db68SChristoph Hellwig 	itt_t init_task_tag)
470e48354ceSNicholas Bellinger {
471e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd = NULL;
472e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr;
473e48354ceSNicholas Bellinger 	/*
474e48354ceSNicholas Bellinger 	 * Scan through the inactive connection recovery list's command list.
475e48354ceSNicholas Bellinger 	 * If init_task_tag matches the command is still alligent.
476e48354ceSNicholas Bellinger 	 */
477e48354ceSNicholas Bellinger 	spin_lock(&sess->cr_i_lock);
478e48354ceSNicholas Bellinger 	list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
479e48354ceSNicholas Bellinger 		spin_lock(&cr->conn_recovery_cmd_lock);
4802fbb471eSAndy Grover 		list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
481e48354ceSNicholas Bellinger 			if (cmd->init_task_tag == init_task_tag) {
482e48354ceSNicholas Bellinger 				spin_unlock(&cr->conn_recovery_cmd_lock);
483e48354ceSNicholas Bellinger 				spin_unlock(&sess->cr_i_lock);
484e48354ceSNicholas Bellinger 
485e48354ceSNicholas Bellinger 				*cr_ptr = cr;
486e48354ceSNicholas Bellinger 				*cmd_ptr = cmd;
487e48354ceSNicholas Bellinger 				return -2;
488e48354ceSNicholas Bellinger 			}
489e48354ceSNicholas Bellinger 		}
490e48354ceSNicholas Bellinger 		spin_unlock(&cr->conn_recovery_cmd_lock);
491e48354ceSNicholas Bellinger 	}
492e48354ceSNicholas Bellinger 	spin_unlock(&sess->cr_i_lock);
493e48354ceSNicholas Bellinger 	/*
494e48354ceSNicholas Bellinger 	 * Scan through the active connection recovery list's command list.
495e48354ceSNicholas Bellinger 	 * If init_task_tag matches the command is ready to be reassigned.
496e48354ceSNicholas Bellinger 	 */
497e48354ceSNicholas Bellinger 	spin_lock(&sess->cr_a_lock);
498e48354ceSNicholas Bellinger 	list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
499e48354ceSNicholas Bellinger 		spin_lock(&cr->conn_recovery_cmd_lock);
5002fbb471eSAndy Grover 		list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
501e48354ceSNicholas Bellinger 			if (cmd->init_task_tag == init_task_tag) {
502e48354ceSNicholas Bellinger 				spin_unlock(&cr->conn_recovery_cmd_lock);
503e48354ceSNicholas Bellinger 				spin_unlock(&sess->cr_a_lock);
504e48354ceSNicholas Bellinger 
505e48354ceSNicholas Bellinger 				*cr_ptr = cr;
506e48354ceSNicholas Bellinger 				*cmd_ptr = cmd;
507e48354ceSNicholas Bellinger 				return 0;
508e48354ceSNicholas Bellinger 			}
509e48354ceSNicholas Bellinger 		}
510e48354ceSNicholas Bellinger 		spin_unlock(&cr->conn_recovery_cmd_lock);
511e48354ceSNicholas Bellinger 	}
512e48354ceSNicholas Bellinger 	spin_unlock(&sess->cr_a_lock);
513e48354ceSNicholas Bellinger 
514e48354ceSNicholas Bellinger 	return -1;
515e48354ceSNicholas Bellinger }
516e48354ceSNicholas Bellinger 
517e48354ceSNicholas Bellinger void iscsit_add_cmd_to_immediate_queue(
518e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
519e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
520e48354ceSNicholas Bellinger 	u8 state)
521e48354ceSNicholas Bellinger {
522e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr;
523e48354ceSNicholas Bellinger 
524e48354ceSNicholas Bellinger 	qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
525e48354ceSNicholas Bellinger 	if (!qr) {
526e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for"
527e48354ceSNicholas Bellinger 				" struct iscsi_queue_req\n");
528e48354ceSNicholas Bellinger 		return;
529e48354ceSNicholas Bellinger 	}
530e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&qr->qr_list);
531e48354ceSNicholas Bellinger 	qr->cmd = cmd;
532e48354ceSNicholas Bellinger 	qr->state = state;
533e48354ceSNicholas Bellinger 
534e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->immed_queue_lock);
535e48354ceSNicholas Bellinger 	list_add_tail(&qr->qr_list, &conn->immed_queue_list);
536e48354ceSNicholas Bellinger 	atomic_inc(&cmd->immed_queue_count);
537e48354ceSNicholas Bellinger 	atomic_set(&conn->check_immediate_queue, 1);
538e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->immed_queue_lock);
539e48354ceSNicholas Bellinger 
540d5627acbSRoland Dreier 	wake_up(&conn->queues_wq);
541e48354ceSNicholas Bellinger }
542d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_add_cmd_to_immediate_queue);
543e48354ceSNicholas Bellinger 
544e48354ceSNicholas Bellinger struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
545e48354ceSNicholas Bellinger {
546e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr;
547e48354ceSNicholas Bellinger 
548e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->immed_queue_lock);
549e48354ceSNicholas Bellinger 	if (list_empty(&conn->immed_queue_list)) {
550e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->immed_queue_lock);
551e48354ceSNicholas Bellinger 		return NULL;
552e48354ceSNicholas Bellinger 	}
5531f981de5SRoland Dreier 	qr = list_first_entry(&conn->immed_queue_list,
5541f981de5SRoland Dreier 			      struct iscsi_queue_req, qr_list);
555e48354ceSNicholas Bellinger 
556e48354ceSNicholas Bellinger 	list_del(&qr->qr_list);
557e48354ceSNicholas Bellinger 	if (qr->cmd)
558e48354ceSNicholas Bellinger 		atomic_dec(&qr->cmd->immed_queue_count);
559e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->immed_queue_lock);
560e48354ceSNicholas Bellinger 
561e48354ceSNicholas Bellinger 	return qr;
562e48354ceSNicholas Bellinger }
563e48354ceSNicholas Bellinger 
564e48354ceSNicholas Bellinger static void iscsit_remove_cmd_from_immediate_queue(
565e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
566e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
567e48354ceSNicholas Bellinger {
568e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr, *qr_tmp;
569e48354ceSNicholas Bellinger 
570e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->immed_queue_lock);
571e48354ceSNicholas Bellinger 	if (!atomic_read(&cmd->immed_queue_count)) {
572e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->immed_queue_lock);
573e48354ceSNicholas Bellinger 		return;
574e48354ceSNicholas Bellinger 	}
575e48354ceSNicholas Bellinger 
576e48354ceSNicholas Bellinger 	list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
577e48354ceSNicholas Bellinger 		if (qr->cmd != cmd)
578e48354ceSNicholas Bellinger 			continue;
579e48354ceSNicholas Bellinger 
580e48354ceSNicholas Bellinger 		atomic_dec(&qr->cmd->immed_queue_count);
581e48354ceSNicholas Bellinger 		list_del(&qr->qr_list);
582e48354ceSNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
583e48354ceSNicholas Bellinger 	}
584e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->immed_queue_lock);
585e48354ceSNicholas Bellinger 
586e48354ceSNicholas Bellinger 	if (atomic_read(&cmd->immed_queue_count)) {
587e48354ceSNicholas Bellinger 		pr_err("ITT: 0x%08x immed_queue_count: %d\n",
588e48354ceSNicholas Bellinger 			cmd->init_task_tag,
589e48354ceSNicholas Bellinger 			atomic_read(&cmd->immed_queue_count));
590e48354ceSNicholas Bellinger 	}
591e48354ceSNicholas Bellinger }
592e48354ceSNicholas Bellinger 
593a4467018SNicholas Bellinger int iscsit_add_cmd_to_response_queue(
594e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
595e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
596e48354ceSNicholas Bellinger 	u8 state)
597e48354ceSNicholas Bellinger {
598e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr;
599e48354ceSNicholas Bellinger 
600e48354ceSNicholas Bellinger 	qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
601e48354ceSNicholas Bellinger 	if (!qr) {
602e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for"
603e48354ceSNicholas Bellinger 			" struct iscsi_queue_req\n");
604a4467018SNicholas Bellinger 		return -ENOMEM;
605e48354ceSNicholas Bellinger 	}
606e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&qr->qr_list);
607e48354ceSNicholas Bellinger 	qr->cmd = cmd;
608e48354ceSNicholas Bellinger 	qr->state = state;
609e48354ceSNicholas Bellinger 
610e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->response_queue_lock);
611e48354ceSNicholas Bellinger 	list_add_tail(&qr->qr_list, &conn->response_queue_list);
612e48354ceSNicholas Bellinger 	atomic_inc(&cmd->response_queue_count);
613e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->response_queue_lock);
614e48354ceSNicholas Bellinger 
615d5627acbSRoland Dreier 	wake_up(&conn->queues_wq);
616a4467018SNicholas Bellinger 	return 0;
617e48354ceSNicholas Bellinger }
618e48354ceSNicholas Bellinger 
619e48354ceSNicholas Bellinger struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
620e48354ceSNicholas Bellinger {
621e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr;
622e48354ceSNicholas Bellinger 
623e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->response_queue_lock);
624e48354ceSNicholas Bellinger 	if (list_empty(&conn->response_queue_list)) {
625e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->response_queue_lock);
626e48354ceSNicholas Bellinger 		return NULL;
627e48354ceSNicholas Bellinger 	}
628e48354ceSNicholas Bellinger 
6291f981de5SRoland Dreier 	qr = list_first_entry(&conn->response_queue_list,
6301f981de5SRoland Dreier 			      struct iscsi_queue_req, qr_list);
631e48354ceSNicholas Bellinger 
632e48354ceSNicholas Bellinger 	list_del(&qr->qr_list);
633e48354ceSNicholas Bellinger 	if (qr->cmd)
634e48354ceSNicholas Bellinger 		atomic_dec(&qr->cmd->response_queue_count);
635e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->response_queue_lock);
636e48354ceSNicholas Bellinger 
637e48354ceSNicholas Bellinger 	return qr;
638e48354ceSNicholas Bellinger }
639e48354ceSNicholas Bellinger 
640e48354ceSNicholas Bellinger static void iscsit_remove_cmd_from_response_queue(
641e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
642e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
643e48354ceSNicholas Bellinger {
644e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr, *qr_tmp;
645e48354ceSNicholas Bellinger 
646e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->response_queue_lock);
647e48354ceSNicholas Bellinger 	if (!atomic_read(&cmd->response_queue_count)) {
648e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->response_queue_lock);
649e48354ceSNicholas Bellinger 		return;
650e48354ceSNicholas Bellinger 	}
651e48354ceSNicholas Bellinger 
652e48354ceSNicholas Bellinger 	list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
653e48354ceSNicholas Bellinger 				qr_list) {
654e48354ceSNicholas Bellinger 		if (qr->cmd != cmd)
655e48354ceSNicholas Bellinger 			continue;
656e48354ceSNicholas Bellinger 
657e48354ceSNicholas Bellinger 		atomic_dec(&qr->cmd->response_queue_count);
658e48354ceSNicholas Bellinger 		list_del(&qr->qr_list);
659e48354ceSNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
660e48354ceSNicholas Bellinger 	}
661e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->response_queue_lock);
662e48354ceSNicholas Bellinger 
663e48354ceSNicholas Bellinger 	if (atomic_read(&cmd->response_queue_count)) {
664e48354ceSNicholas Bellinger 		pr_err("ITT: 0x%08x response_queue_count: %d\n",
665e48354ceSNicholas Bellinger 			cmd->init_task_tag,
666e48354ceSNicholas Bellinger 			atomic_read(&cmd->response_queue_count));
667e48354ceSNicholas Bellinger 	}
668e48354ceSNicholas Bellinger }
669e48354ceSNicholas Bellinger 
670d5627acbSRoland Dreier bool iscsit_conn_all_queues_empty(struct iscsi_conn *conn)
671d5627acbSRoland Dreier {
672d5627acbSRoland Dreier 	bool empty;
673d5627acbSRoland Dreier 
674d5627acbSRoland Dreier 	spin_lock_bh(&conn->immed_queue_lock);
675d5627acbSRoland Dreier 	empty = list_empty(&conn->immed_queue_list);
676d5627acbSRoland Dreier 	spin_unlock_bh(&conn->immed_queue_lock);
677d5627acbSRoland Dreier 
678d5627acbSRoland Dreier 	if (!empty)
679d5627acbSRoland Dreier 		return empty;
680d5627acbSRoland Dreier 
681d5627acbSRoland Dreier 	spin_lock_bh(&conn->response_queue_lock);
682d5627acbSRoland Dreier 	empty = list_empty(&conn->response_queue_list);
683d5627acbSRoland Dreier 	spin_unlock_bh(&conn->response_queue_lock);
684d5627acbSRoland Dreier 
685d5627acbSRoland Dreier 	return empty;
686d5627acbSRoland Dreier }
687d5627acbSRoland Dreier 
688e48354ceSNicholas Bellinger void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
689e48354ceSNicholas Bellinger {
690e48354ceSNicholas Bellinger 	struct iscsi_queue_req *qr, *qr_tmp;
691e48354ceSNicholas Bellinger 
692e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->immed_queue_lock);
693e48354ceSNicholas Bellinger 	list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
694e48354ceSNicholas Bellinger 		list_del(&qr->qr_list);
695e48354ceSNicholas Bellinger 		if (qr->cmd)
696e48354ceSNicholas Bellinger 			atomic_dec(&qr->cmd->immed_queue_count);
697e48354ceSNicholas Bellinger 
698e48354ceSNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
699e48354ceSNicholas Bellinger 	}
700e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->immed_queue_lock);
701e48354ceSNicholas Bellinger 
702e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->response_queue_lock);
703e48354ceSNicholas Bellinger 	list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
704e48354ceSNicholas Bellinger 			qr_list) {
705e48354ceSNicholas Bellinger 		list_del(&qr->qr_list);
706e48354ceSNicholas Bellinger 		if (qr->cmd)
707e48354ceSNicholas Bellinger 			atomic_dec(&qr->cmd->response_queue_count);
708e48354ceSNicholas Bellinger 
709e48354ceSNicholas Bellinger 		kmem_cache_free(lio_qr_cache, qr);
710e48354ceSNicholas Bellinger 	}
711e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->response_queue_lock);
712e48354ceSNicholas Bellinger }
713e48354ceSNicholas Bellinger 
714e48354ceSNicholas Bellinger void iscsit_release_cmd(struct iscsi_cmd *cmd)
715e48354ceSNicholas Bellinger {
716988e3a85SNicholas Bellinger 	struct iscsi_session *sess;
717988e3a85SNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
718988e3a85SNicholas Bellinger 
7196eaf69e4SBart Van Assche 	WARN_ON(!list_empty(&cmd->i_conn_node));
7206eaf69e4SBart Van Assche 
721988e3a85SNicholas Bellinger 	if (cmd->conn)
722988e3a85SNicholas Bellinger 		sess = cmd->conn->sess;
723988e3a85SNicholas Bellinger 	else
724988e3a85SNicholas Bellinger 		sess = cmd->sess;
725988e3a85SNicholas Bellinger 
726988e3a85SNicholas Bellinger 	BUG_ON(!sess || !sess->se_sess);
727988e3a85SNicholas Bellinger 
728e48354ceSNicholas Bellinger 	kfree(cmd->buf_ptr);
729e48354ceSNicholas Bellinger 	kfree(cmd->pdu_list);
730e48354ceSNicholas Bellinger 	kfree(cmd->seq_list);
731e48354ceSNicholas Bellinger 	kfree(cmd->tmr_req);
7320ca650c1SBart Van Assche 	kfree(cmd->overflow_buf);
733e48354ceSNicholas Bellinger 	kfree(cmd->iov_data);
7349864ca9dSNicholas Bellinger 	kfree(cmd->text_in_ptr);
735e48354ceSNicholas Bellinger 
73683c2b54bSMatthew Wilcox 	target_free_tag(sess->se_sess, se_cmd);
737e48354ceSNicholas Bellinger }
738d703ce2fSNicholas Bellinger EXPORT_SYMBOL(iscsit_release_cmd);
739e48354ceSNicholas Bellinger 
7404412a671SBart Van Assche void __iscsit_free_cmd(struct iscsi_cmd *cmd, bool check_queues)
741d270190aSNicholas Bellinger {
742aafc9d15SNicholas Bellinger 	struct iscsi_conn *conn = cmd->conn;
743aafc9d15SNicholas Bellinger 
7446eaf69e4SBart Van Assche 	WARN_ON(!list_empty(&cmd->i_conn_node));
7456eaf69e4SBart Van Assche 
746aafc9d15SNicholas Bellinger 	if (cmd->data_direction == DMA_TO_DEVICE) {
747aafc9d15SNicholas Bellinger 		iscsit_stop_dataout_timer(cmd);
748aafc9d15SNicholas Bellinger 		iscsit_free_r2ts_from_list(cmd);
749aafc9d15SNicholas Bellinger 	}
750aafc9d15SNicholas Bellinger 	if (cmd->data_direction == DMA_FROM_DEVICE)
751aafc9d15SNicholas Bellinger 		iscsit_free_all_datain_reqs(cmd);
752aafc9d15SNicholas Bellinger 
753aafc9d15SNicholas Bellinger 	if (conn && check_queues) {
754aafc9d15SNicholas Bellinger 		iscsit_remove_cmd_from_immediate_queue(cmd, conn);
755aafc9d15SNicholas Bellinger 		iscsit_remove_cmd_from_response_queue(cmd, conn);
756aafc9d15SNicholas Bellinger 	}
7577ec811a8SVarun Prakash 
7581e65cc16SBart Van Assche 	if (conn && conn->conn_transport->iscsit_unmap_cmd)
7591e65cc16SBart Van Assche 		conn->conn_transport->iscsit_unmap_cmd(conn, cmd);
760aafc9d15SNicholas Bellinger }
761aafc9d15SNicholas Bellinger 
762aafc9d15SNicholas Bellinger void iscsit_free_cmd(struct iscsi_cmd *cmd, bool shutdown)
763aafc9d15SNicholas Bellinger {
764d1c26857SBart Van Assche 	struct se_cmd *se_cmd = cmd->se_cmd.se_tfo ? &cmd->se_cmd : NULL;
765aafc9d15SNicholas Bellinger 	int rc;
7664412a671SBart Van Assche 
767b0055acaSBart Van Assche 	WARN_ON(!list_empty(&cmd->i_conn_node));
768b0055acaSBart Van Assche 
7694412a671SBart Van Assche 	__iscsit_free_cmd(cmd, shutdown);
770d1c26857SBart Van Assche 	if (se_cmd) {
771efb2ea77SNicholas Bellinger 		rc = transport_generic_free_cmd(se_cmd, shutdown);
772c8a75afbSBart Van Assche 		if (!rc && shutdown && se_cmd->se_sess) {
773c8a75afbSBart Van Assche 			__iscsit_free_cmd(cmd, shutdown);
774afc16604SBart Van Assche 			target_put_sess_cmd(se_cmd);
775c8a75afbSBart Van Assche 		}
776d1c26857SBart Van Assche 	} else {
777d703ce2fSNicholas Bellinger 		iscsit_release_cmd(cmd);
778d270190aSNicholas Bellinger 	}
779d270190aSNicholas Bellinger }
780d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsit_free_cmd);
781d270190aSNicholas Bellinger 
782*f88a10f8SSebastian Andrzej Siewior bool iscsit_check_session_usage_count(struct iscsi_session *sess,
783*f88a10f8SSebastian Andrzej Siewior 				      bool can_sleep)
784e48354ceSNicholas Bellinger {
785e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->session_usage_lock);
786e48354ceSNicholas Bellinger 	if (sess->session_usage_count != 0) {
787e48354ceSNicholas Bellinger 		sess->session_waiting_on_uc = 1;
788e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->session_usage_lock);
789efc9d730SSebastian Andrzej Siewior 		if (!can_sleep)
790*f88a10f8SSebastian Andrzej Siewior 			return true;
791e48354ceSNicholas Bellinger 
792e48354ceSNicholas Bellinger 		wait_for_completion(&sess->session_waiting_on_uc_comp);
793*f88a10f8SSebastian Andrzej Siewior 		return false;
794e48354ceSNicholas Bellinger 	}
795e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->session_usage_lock);
796e48354ceSNicholas Bellinger 
797*f88a10f8SSebastian Andrzej Siewior 	return false;
798e48354ceSNicholas Bellinger }
799e48354ceSNicholas Bellinger 
800e48354ceSNicholas Bellinger void iscsit_dec_session_usage_count(struct iscsi_session *sess)
801e48354ceSNicholas Bellinger {
802e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->session_usage_lock);
803e48354ceSNicholas Bellinger 	sess->session_usage_count--;
804e48354ceSNicholas Bellinger 
805e48354ceSNicholas Bellinger 	if (!sess->session_usage_count && sess->session_waiting_on_uc)
806e48354ceSNicholas Bellinger 		complete(&sess->session_waiting_on_uc_comp);
807e48354ceSNicholas Bellinger 
808e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->session_usage_lock);
809e48354ceSNicholas Bellinger }
810e48354ceSNicholas Bellinger 
811e48354ceSNicholas Bellinger void iscsit_inc_session_usage_count(struct iscsi_session *sess)
812e48354ceSNicholas Bellinger {
813e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->session_usage_lock);
814e48354ceSNicholas Bellinger 	sess->session_usage_count++;
815e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->session_usage_lock);
816e48354ceSNicholas Bellinger }
817e48354ceSNicholas Bellinger 
818e48354ceSNicholas Bellinger struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
819e48354ceSNicholas Bellinger {
820e48354ceSNicholas Bellinger 	struct iscsi_conn *conn;
821e48354ceSNicholas Bellinger 
822e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
823e48354ceSNicholas Bellinger 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
824e48354ceSNicholas Bellinger 		if ((conn->cid == cid) &&
825e48354ceSNicholas Bellinger 		    (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
826e48354ceSNicholas Bellinger 			iscsit_inc_conn_usage_count(conn);
827e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
828e48354ceSNicholas Bellinger 			return conn;
829e48354ceSNicholas Bellinger 		}
830e48354ceSNicholas Bellinger 	}
831e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
832e48354ceSNicholas Bellinger 
833e48354ceSNicholas Bellinger 	return NULL;
834e48354ceSNicholas Bellinger }
835e48354ceSNicholas Bellinger 
836e48354ceSNicholas Bellinger struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
837e48354ceSNicholas Bellinger {
838e48354ceSNicholas Bellinger 	struct iscsi_conn *conn;
839e48354ceSNicholas Bellinger 
840e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
841e48354ceSNicholas Bellinger 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
842e48354ceSNicholas Bellinger 		if (conn->cid == cid) {
843e48354ceSNicholas Bellinger 			iscsit_inc_conn_usage_count(conn);
844e48354ceSNicholas Bellinger 			spin_lock(&conn->state_lock);
845e48354ceSNicholas Bellinger 			atomic_set(&conn->connection_wait_rcfr, 1);
846e48354ceSNicholas Bellinger 			spin_unlock(&conn->state_lock);
847e48354ceSNicholas Bellinger 			spin_unlock_bh(&sess->conn_lock);
848e48354ceSNicholas Bellinger 			return conn;
849e48354ceSNicholas Bellinger 		}
850e48354ceSNicholas Bellinger 	}
851e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
852e48354ceSNicholas Bellinger 
853e48354ceSNicholas Bellinger 	return NULL;
854e48354ceSNicholas Bellinger }
855e48354ceSNicholas Bellinger 
856e48354ceSNicholas Bellinger void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
857e48354ceSNicholas Bellinger {
858e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->conn_usage_lock);
859e48354ceSNicholas Bellinger 	if (conn->conn_usage_count != 0) {
860e48354ceSNicholas Bellinger 		conn->conn_waiting_on_uc = 1;
861e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->conn_usage_lock);
862e48354ceSNicholas Bellinger 
863e48354ceSNicholas Bellinger 		wait_for_completion(&conn->conn_waiting_on_uc_comp);
864e48354ceSNicholas Bellinger 		return;
865e48354ceSNicholas Bellinger 	}
866e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->conn_usage_lock);
867e48354ceSNicholas Bellinger }
868e48354ceSNicholas Bellinger 
869e48354ceSNicholas Bellinger void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
870e48354ceSNicholas Bellinger {
871e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->conn_usage_lock);
872e48354ceSNicholas Bellinger 	conn->conn_usage_count--;
873e48354ceSNicholas Bellinger 
874e48354ceSNicholas Bellinger 	if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
875e48354ceSNicholas Bellinger 		complete(&conn->conn_waiting_on_uc_comp);
876e48354ceSNicholas Bellinger 
877e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->conn_usage_lock);
878e48354ceSNicholas Bellinger }
879e48354ceSNicholas Bellinger 
880e48354ceSNicholas Bellinger void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
881e48354ceSNicholas Bellinger {
882e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->conn_usage_lock);
883e48354ceSNicholas Bellinger 	conn->conn_usage_count++;
884e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->conn_usage_lock);
885e48354ceSNicholas Bellinger }
886e48354ceSNicholas Bellinger 
887e48354ceSNicholas Bellinger static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
888e48354ceSNicholas Bellinger {
889e48354ceSNicholas Bellinger 	u8 state;
890e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd;
891e48354ceSNicholas Bellinger 
892676687c6SNicholas Bellinger 	cmd = iscsit_allocate_cmd(conn, TASK_RUNNING);
893e48354ceSNicholas Bellinger 	if (!cmd)
894e48354ceSNicholas Bellinger 		return -1;
895e48354ceSNicholas Bellinger 
896e48354ceSNicholas Bellinger 	cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
897e48354ceSNicholas Bellinger 	state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
898e48354ceSNicholas Bellinger 				ISTATE_SEND_NOPIN_NO_RESPONSE;
89966c7db68SChristoph Hellwig 	cmd->init_task_tag = RESERVED_ITT;
900c1e34b64SSagi Grimberg 	cmd->targ_xfer_tag = (want_response) ?
901c1e34b64SSagi Grimberg 			     session_get_next_ttt(conn->sess) : 0xFFFFFFFF;
902e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->cmd_lock);
9032fbb471eSAndy Grover 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
904e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->cmd_lock);
905e48354ceSNicholas Bellinger 
906e48354ceSNicholas Bellinger 	if (want_response)
907e48354ceSNicholas Bellinger 		iscsit_start_nopin_response_timer(conn);
908e48354ceSNicholas Bellinger 	iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
909e48354ceSNicholas Bellinger 
910e48354ceSNicholas Bellinger 	return 0;
911e48354ceSNicholas Bellinger }
912e48354ceSNicholas Bellinger 
913f7c9564aSKees Cook void iscsit_handle_nopin_response_timeout(struct timer_list *t)
914e48354ceSNicholas Bellinger {
915f7c9564aSKees Cook 	struct iscsi_conn *conn = from_timer(conn, t, nopin_response_timer);
916c62ae300SDavid Disseldorp 	struct iscsi_session *sess = conn->sess;
917e48354ceSNicholas Bellinger 
918e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
919e48354ceSNicholas Bellinger 
920e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
921e48354ceSNicholas Bellinger 	if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
922e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
923e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn);
924e48354ceSNicholas Bellinger 		return;
925e48354ceSNicholas Bellinger 	}
926e48354ceSNicholas Bellinger 
927c62ae300SDavid Disseldorp 	pr_err("Did not receive response to NOPIN on CID: %hu, failing"
928c62ae300SDavid Disseldorp 		" connection for I_T Nexus %s,i,0x%6phN,%s,t,0x%02x\n",
929c62ae300SDavid Disseldorp 		conn->cid, sess->sess_ops->InitiatorName, sess->isid,
930c62ae300SDavid Disseldorp 		sess->tpg->tpg_tiqn->tiqn, (u32)sess->tpg->tpgt);
931e48354ceSNicholas Bellinger 	conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
932e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
933e48354ceSNicholas Bellinger 
934dce6190cSDavid Disseldorp 	iscsit_fill_cxn_timeout_err_stats(sess);
935e48354ceSNicholas Bellinger 	iscsit_cause_connection_reinstatement(conn, 0);
936e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
937e48354ceSNicholas Bellinger }
938e48354ceSNicholas Bellinger 
939e48354ceSNicholas Bellinger void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
940e48354ceSNicholas Bellinger {
941e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
942e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
943e48354ceSNicholas Bellinger 
944e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
945e48354ceSNicholas Bellinger 	if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
946e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
947e48354ceSNicholas Bellinger 		return;
948e48354ceSNicholas Bellinger 	}
949e48354ceSNicholas Bellinger 
950e48354ceSNicholas Bellinger 	mod_timer(&conn->nopin_response_timer,
951e48354ceSNicholas Bellinger 		(get_jiffies_64() + na->nopin_response_timeout * HZ));
952e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
953e48354ceSNicholas Bellinger }
954e48354ceSNicholas Bellinger 
955e48354ceSNicholas Bellinger void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
956e48354ceSNicholas Bellinger {
957e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
958e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
959e48354ceSNicholas Bellinger 
960e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
961e48354ceSNicholas Bellinger 	if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
962e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
963e48354ceSNicholas Bellinger 		return;
964e48354ceSNicholas Bellinger 	}
965e48354ceSNicholas Bellinger 
966e48354ceSNicholas Bellinger 	conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
967e48354ceSNicholas Bellinger 	conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
9688a47aa9dSBart Van Assche 	mod_timer(&conn->nopin_response_timer,
9698a47aa9dSBart Van Assche 		  jiffies + na->nopin_response_timeout * HZ);
970e48354ceSNicholas Bellinger 
971e48354ceSNicholas Bellinger 	pr_debug("Started NOPIN Response Timer on CID: %d to %u"
972e48354ceSNicholas Bellinger 		" seconds\n", conn->cid, na->nopin_response_timeout);
973e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
974e48354ceSNicholas Bellinger }
975e48354ceSNicholas Bellinger 
976e48354ceSNicholas Bellinger void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
977e48354ceSNicholas Bellinger {
978e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
979e48354ceSNicholas Bellinger 	if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
980e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
981e48354ceSNicholas Bellinger 		return;
982e48354ceSNicholas Bellinger 	}
983e48354ceSNicholas Bellinger 	conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
984e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
985e48354ceSNicholas Bellinger 
986e48354ceSNicholas Bellinger 	del_timer_sync(&conn->nopin_response_timer);
987e48354ceSNicholas Bellinger 
988e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
989e48354ceSNicholas Bellinger 	conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
990e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
991e48354ceSNicholas Bellinger }
992e48354ceSNicholas Bellinger 
993f7c9564aSKees Cook void iscsit_handle_nopin_timeout(struct timer_list *t)
994e48354ceSNicholas Bellinger {
995f7c9564aSKees Cook 	struct iscsi_conn *conn = from_timer(conn, t, nopin_timer);
996e48354ceSNicholas Bellinger 
997e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
998e48354ceSNicholas Bellinger 
999e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
1000e48354ceSNicholas Bellinger 	if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
1001e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
1002e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn);
1003e48354ceSNicholas Bellinger 		return;
1004e48354ceSNicholas Bellinger 	}
1005e48354ceSNicholas Bellinger 	conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1006e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
1007e48354ceSNicholas Bellinger 
1008e48354ceSNicholas Bellinger 	iscsit_add_nopin(conn, 1);
1009e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
1010e48354ceSNicholas Bellinger }
1011e48354ceSNicholas Bellinger 
1012e48354ceSNicholas Bellinger void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
1013e48354ceSNicholas Bellinger {
1014e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1015e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1016618baaf7SBart Van Assche 
1017618baaf7SBart Van Assche 	lockdep_assert_held(&conn->nopin_timer_lock);
1018618baaf7SBart Van Assche 
1019e48354ceSNicholas Bellinger 	/*
1020e48354ceSNicholas Bellinger 	* NOPIN timeout is disabled.
1021e48354ceSNicholas Bellinger 	 */
1022e48354ceSNicholas Bellinger 	if (!na->nopin_timeout)
1023e48354ceSNicholas Bellinger 		return;
1024e48354ceSNicholas Bellinger 
1025e48354ceSNicholas Bellinger 	if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
1026e48354ceSNicholas Bellinger 		return;
1027e48354ceSNicholas Bellinger 
1028e48354ceSNicholas Bellinger 	conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1029e48354ceSNicholas Bellinger 	conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
10308a47aa9dSBart Van Assche 	mod_timer(&conn->nopin_timer, jiffies + na->nopin_timeout * HZ);
1031e48354ceSNicholas Bellinger 
1032e48354ceSNicholas Bellinger 	pr_debug("Started NOPIN Timer on CID: %d at %u second"
1033e48354ceSNicholas Bellinger 		" interval\n", conn->cid, na->nopin_timeout);
1034e48354ceSNicholas Bellinger }
1035e48354ceSNicholas Bellinger 
1036e48354ceSNicholas Bellinger void iscsit_start_nopin_timer(struct iscsi_conn *conn)
1037e48354ceSNicholas Bellinger {
1038e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
1039aeb50279SMike Christie 	__iscsit_start_nopin_timer(conn);
1040e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
1041e48354ceSNicholas Bellinger }
1042e48354ceSNicholas Bellinger 
1043e48354ceSNicholas Bellinger void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
1044e48354ceSNicholas Bellinger {
1045e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
1046e48354ceSNicholas Bellinger 	if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
1047e48354ceSNicholas Bellinger 		spin_unlock_bh(&conn->nopin_timer_lock);
1048e48354ceSNicholas Bellinger 		return;
1049e48354ceSNicholas Bellinger 	}
1050e48354ceSNicholas Bellinger 	conn->nopin_timer_flags |= ISCSI_TF_STOP;
1051e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
1052e48354ceSNicholas Bellinger 
1053e48354ceSNicholas Bellinger 	del_timer_sync(&conn->nopin_timer);
1054e48354ceSNicholas Bellinger 
1055e48354ceSNicholas Bellinger 	spin_lock_bh(&conn->nopin_timer_lock);
1056e48354ceSNicholas Bellinger 	conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1057e48354ceSNicholas Bellinger 	spin_unlock_bh(&conn->nopin_timer_lock);
1058e48354ceSNicholas Bellinger }
1059e48354ceSNicholas Bellinger 
1060e48354ceSNicholas Bellinger int iscsit_send_tx_data(
1061e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
1062e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
1063e48354ceSNicholas Bellinger 	int use_misc)
1064e48354ceSNicholas Bellinger {
1065e48354ceSNicholas Bellinger 	int tx_sent, tx_size;
1066e48354ceSNicholas Bellinger 	u32 iov_count;
1067e48354ceSNicholas Bellinger 	struct kvec *iov;
1068e48354ceSNicholas Bellinger 
1069e48354ceSNicholas Bellinger send_data:
1070e48354ceSNicholas Bellinger 	tx_size = cmd->tx_size;
1071e48354ceSNicholas Bellinger 
1072e48354ceSNicholas Bellinger 	if (!use_misc) {
1073e48354ceSNicholas Bellinger 		iov = &cmd->iov_data[0];
1074e48354ceSNicholas Bellinger 		iov_count = cmd->iov_data_count;
1075e48354ceSNicholas Bellinger 	} else {
1076e48354ceSNicholas Bellinger 		iov = &cmd->iov_misc[0];
1077e48354ceSNicholas Bellinger 		iov_count = cmd->iov_misc_count;
1078e48354ceSNicholas Bellinger 	}
1079e48354ceSNicholas Bellinger 
1080e48354ceSNicholas Bellinger 	tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
1081e48354ceSNicholas Bellinger 	if (tx_size != tx_sent) {
1082e48354ceSNicholas Bellinger 		if (tx_sent == -EAGAIN) {
1083e48354ceSNicholas Bellinger 			pr_err("tx_data() returned -EAGAIN\n");
1084e48354ceSNicholas Bellinger 			goto send_data;
1085e48354ceSNicholas Bellinger 		} else
1086e48354ceSNicholas Bellinger 			return -1;
1087e48354ceSNicholas Bellinger 	}
1088e48354ceSNicholas Bellinger 	cmd->tx_size = 0;
1089e48354ceSNicholas Bellinger 
1090e48354ceSNicholas Bellinger 	return 0;
1091e48354ceSNicholas Bellinger }
1092e48354ceSNicholas Bellinger 
1093e48354ceSNicholas Bellinger int iscsit_fe_sendpage_sg(
1094e48354ceSNicholas Bellinger 	struct iscsi_cmd *cmd,
1095e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
1096e48354ceSNicholas Bellinger {
1097e48354ceSNicholas Bellinger 	struct scatterlist *sg = cmd->first_data_sg;
1098e48354ceSNicholas Bellinger 	struct kvec iov;
1099e48354ceSNicholas Bellinger 	u32 tx_hdr_size, data_len;
1100e48354ceSNicholas Bellinger 	u32 offset = cmd->first_data_sg_off;
110140b05497SNicholas Bellinger 	int tx_sent, iov_off;
1102e48354ceSNicholas Bellinger 
1103e48354ceSNicholas Bellinger send_hdr:
1104e48354ceSNicholas Bellinger 	tx_hdr_size = ISCSI_HDR_LEN;
1105e48354ceSNicholas Bellinger 	if (conn->conn_ops->HeaderDigest)
1106e48354ceSNicholas Bellinger 		tx_hdr_size += ISCSI_CRC_LEN;
1107e48354ceSNicholas Bellinger 
1108e48354ceSNicholas Bellinger 	iov.iov_base = cmd->pdu;
1109e48354ceSNicholas Bellinger 	iov.iov_len = tx_hdr_size;
1110e48354ceSNicholas Bellinger 
1111e48354ceSNicholas Bellinger 	tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
1112e48354ceSNicholas Bellinger 	if (tx_hdr_size != tx_sent) {
1113e48354ceSNicholas Bellinger 		if (tx_sent == -EAGAIN) {
1114e48354ceSNicholas Bellinger 			pr_err("tx_data() returned -EAGAIN\n");
1115e48354ceSNicholas Bellinger 			goto send_hdr;
1116e48354ceSNicholas Bellinger 		}
1117e48354ceSNicholas Bellinger 		return -1;
1118e48354ceSNicholas Bellinger 	}
1119e48354ceSNicholas Bellinger 
1120e48354ceSNicholas Bellinger 	data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
112140b05497SNicholas Bellinger 	/*
112240b05497SNicholas Bellinger 	 * Set iov_off used by padding and data digest tx_data() calls below
112340b05497SNicholas Bellinger 	 * in order to determine proper offset into cmd->iov_data[]
112440b05497SNicholas Bellinger 	 */
112540b05497SNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
1126e48354ceSNicholas Bellinger 		data_len -= ISCSI_CRC_LEN;
112740b05497SNicholas Bellinger 		if (cmd->padding)
112840b05497SNicholas Bellinger 			iov_off = (cmd->iov_data_count - 2);
112940b05497SNicholas Bellinger 		else
113040b05497SNicholas Bellinger 			iov_off = (cmd->iov_data_count - 1);
113140b05497SNicholas Bellinger 	} else {
113240b05497SNicholas Bellinger 		iov_off = (cmd->iov_data_count - 1);
113340b05497SNicholas Bellinger 	}
1134e48354ceSNicholas Bellinger 	/*
1135e48354ceSNicholas Bellinger 	 * Perform sendpage() for each page in the scatterlist
1136e48354ceSNicholas Bellinger 	 */
1137e48354ceSNicholas Bellinger 	while (data_len) {
1138e48354ceSNicholas Bellinger 		u32 space = (sg->length - offset);
1139e48354ceSNicholas Bellinger 		u32 sub_len = min_t(u32, data_len, space);
1140e48354ceSNicholas Bellinger send_pg:
1141e48354ceSNicholas Bellinger 		tx_sent = conn->sock->ops->sendpage(conn->sock,
1142e48354ceSNicholas Bellinger 					sg_page(sg), sg->offset + offset, sub_len, 0);
1143e48354ceSNicholas Bellinger 		if (tx_sent != sub_len) {
1144e48354ceSNicholas Bellinger 			if (tx_sent == -EAGAIN) {
1145e48354ceSNicholas Bellinger 				pr_err("tcp_sendpage() returned"
1146e48354ceSNicholas Bellinger 						" -EAGAIN\n");
1147e48354ceSNicholas Bellinger 				goto send_pg;
1148e48354ceSNicholas Bellinger 			}
1149e48354ceSNicholas Bellinger 
1150e48354ceSNicholas Bellinger 			pr_err("tcp_sendpage() failure: %d\n",
1151e48354ceSNicholas Bellinger 					tx_sent);
1152e48354ceSNicholas Bellinger 			return -1;
1153e48354ceSNicholas Bellinger 		}
1154e48354ceSNicholas Bellinger 
1155e48354ceSNicholas Bellinger 		data_len -= sub_len;
1156e48354ceSNicholas Bellinger 		offset = 0;
1157e48354ceSNicholas Bellinger 		sg = sg_next(sg);
1158e48354ceSNicholas Bellinger 	}
1159e48354ceSNicholas Bellinger 
1160e48354ceSNicholas Bellinger send_padding:
1161e48354ceSNicholas Bellinger 	if (cmd->padding) {
116240b05497SNicholas Bellinger 		struct kvec *iov_p = &cmd->iov_data[iov_off++];
1163e48354ceSNicholas Bellinger 
1164e48354ceSNicholas Bellinger 		tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
1165e48354ceSNicholas Bellinger 		if (cmd->padding != tx_sent) {
1166e48354ceSNicholas Bellinger 			if (tx_sent == -EAGAIN) {
1167e48354ceSNicholas Bellinger 				pr_err("tx_data() returned -EAGAIN\n");
1168e48354ceSNicholas Bellinger 				goto send_padding;
1169e48354ceSNicholas Bellinger 			}
1170e48354ceSNicholas Bellinger 			return -1;
1171e48354ceSNicholas Bellinger 		}
1172e48354ceSNicholas Bellinger 	}
1173e48354ceSNicholas Bellinger 
1174e48354ceSNicholas Bellinger send_datacrc:
1175e48354ceSNicholas Bellinger 	if (conn->conn_ops->DataDigest) {
117640b05497SNicholas Bellinger 		struct kvec *iov_d = &cmd->iov_data[iov_off];
1177e48354ceSNicholas Bellinger 
1178e48354ceSNicholas Bellinger 		tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
1179e48354ceSNicholas Bellinger 		if (ISCSI_CRC_LEN != tx_sent) {
1180e48354ceSNicholas Bellinger 			if (tx_sent == -EAGAIN) {
1181e48354ceSNicholas Bellinger 				pr_err("tx_data() returned -EAGAIN\n");
1182e48354ceSNicholas Bellinger 				goto send_datacrc;
1183e48354ceSNicholas Bellinger 			}
1184e48354ceSNicholas Bellinger 			return -1;
1185e48354ceSNicholas Bellinger 		}
1186e48354ceSNicholas Bellinger 	}
1187e48354ceSNicholas Bellinger 
1188e48354ceSNicholas Bellinger 	return 0;
1189e48354ceSNicholas Bellinger }
1190e48354ceSNicholas Bellinger 
1191e48354ceSNicholas Bellinger /*
1192e48354ceSNicholas Bellinger  *      This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
1193e48354ceSNicholas Bellinger  *      back to the Initiator when an expection condition occurs with the
1194e48354ceSNicholas Bellinger  *      errors set in status_class and status_detail.
1195e48354ceSNicholas Bellinger  *
1196e48354ceSNicholas Bellinger  *      Parameters:     iSCSI Connection, Status Class, Status Detail.
1197e48354ceSNicholas Bellinger  *      Returns:        0 on success, -1 on error.
1198e48354ceSNicholas Bellinger  */
1199e48354ceSNicholas Bellinger int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
1200e48354ceSNicholas Bellinger {
1201e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *hdr;
1202baa4d64bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1203e48354ceSNicholas Bellinger 
1204baa4d64bSNicholas Bellinger 	login->login_failed = 1;
1205e48354ceSNicholas Bellinger 	iscsit_collect_login_stats(conn, status_class, status_detail);
1206e48354ceSNicholas Bellinger 
120768349756SNicholas Bellinger 	memset(&login->rsp[0], 0, ISCSI_HDR_LEN);
120868349756SNicholas Bellinger 
1209baa4d64bSNicholas Bellinger 	hdr	= (struct iscsi_login_rsp *)&login->rsp[0];
1210e48354ceSNicholas Bellinger 	hdr->opcode		= ISCSI_OP_LOGIN_RSP;
1211e48354ceSNicholas Bellinger 	hdr->status_class	= status_class;
1212e48354ceSNicholas Bellinger 	hdr->status_detail	= status_detail;
121366c7db68SChristoph Hellwig 	hdr->itt		= conn->login_itt;
1214e48354ceSNicholas Bellinger 
1215baa4d64bSNicholas Bellinger 	return conn->conn_transport->iscsit_put_login_tx(conn, login, 0);
1216e48354ceSNicholas Bellinger }
1217e48354ceSNicholas Bellinger 
1218e48354ceSNicholas Bellinger void iscsit_print_session_params(struct iscsi_session *sess)
1219e48354ceSNicholas Bellinger {
1220e48354ceSNicholas Bellinger 	struct iscsi_conn *conn;
1221e48354ceSNicholas Bellinger 
1222e48354ceSNicholas Bellinger 	pr_debug("-----------------------------[Session Params for"
1223e48354ceSNicholas Bellinger 		" SID: %u]-----------------------------\n", sess->sid);
1224e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
1225e48354ceSNicholas Bellinger 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
1226e48354ceSNicholas Bellinger 		iscsi_dump_conn_ops(conn->conn_ops);
1227e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
1228e48354ceSNicholas Bellinger 
1229e48354ceSNicholas Bellinger 	iscsi_dump_sess_ops(sess->sess_ops);
1230e48354ceSNicholas Bellinger }
1231e48354ceSNicholas Bellinger 
12327c59daceSMaurizio Lombardi int rx_data(
1233e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
12347c59daceSMaurizio Lombardi 	struct kvec *iov,
12357c59daceSMaurizio Lombardi 	int iov_count,
12367c59daceSMaurizio Lombardi 	int data)
1237e48354ceSNicholas Bellinger {
12387c59daceSMaurizio Lombardi 	int rx_loop = 0, total_rx = 0;
1239e48354ceSNicholas Bellinger 	struct msghdr msg;
1240e48354ceSNicholas Bellinger 
1241e48354ceSNicholas Bellinger 	if (!conn || !conn->sock || !conn->conn_ops)
1242e48354ceSNicholas Bellinger 		return -1;
1243e48354ceSNicholas Bellinger 
1244e48354ceSNicholas Bellinger 	memset(&msg, 0, sizeof(struct msghdr));
12457c59daceSMaurizio Lombardi 	iov_iter_kvec(&msg.msg_iter, READ, iov, iov_count, data);
1246e48354ceSNicholas Bellinger 
12472da62906SAl Viro 	while (msg_data_left(&msg)) {
12482da62906SAl Viro 		rx_loop = sock_recvmsg(conn->sock, &msg, MSG_WAITALL);
1249e48354ceSNicholas Bellinger 		if (rx_loop <= 0) {
1250e48354ceSNicholas Bellinger 			pr_debug("rx_loop: %d total_rx: %d\n",
1251e48354ceSNicholas Bellinger 				rx_loop, total_rx);
1252e48354ceSNicholas Bellinger 			return rx_loop;
1253e48354ceSNicholas Bellinger 		}
1254e48354ceSNicholas Bellinger 		total_rx += rx_loop;
1255e48354ceSNicholas Bellinger 		pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
1256e48354ceSNicholas Bellinger 				rx_loop, total_rx, data);
1257e48354ceSNicholas Bellinger 	}
1258e48354ceSNicholas Bellinger 
1259e48354ceSNicholas Bellinger 	return total_rx;
1260e48354ceSNicholas Bellinger }
1261e48354ceSNicholas Bellinger 
1262e48354ceSNicholas Bellinger int tx_data(
1263e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
1264e48354ceSNicholas Bellinger 	struct kvec *iov,
1265e48354ceSNicholas Bellinger 	int iov_count,
1266e48354ceSNicholas Bellinger 	int data)
1267e48354ceSNicholas Bellinger {
12684113e47bSAl Viro 	struct msghdr msg;
12694113e47bSAl Viro 	int total_tx = 0;
1270e48354ceSNicholas Bellinger 
1271e48354ceSNicholas Bellinger 	if (!conn || !conn->sock || !conn->conn_ops)
1272e48354ceSNicholas Bellinger 		return -1;
1273e48354ceSNicholas Bellinger 
12744113e47bSAl Viro 	if (data <= 0) {
12754113e47bSAl Viro 		pr_err("Data length is: %d\n", data);
12764113e47bSAl Viro 		return -1;
12774113e47bSAl Viro 	}
1278e48354ceSNicholas Bellinger 
12794113e47bSAl Viro 	memset(&msg, 0, sizeof(struct msghdr));
12804113e47bSAl Viro 
1281aa563d7bSDavid Howells 	iov_iter_kvec(&msg.msg_iter, WRITE, iov, iov_count, data);
12824113e47bSAl Viro 
12834113e47bSAl Viro 	while (msg_data_left(&msg)) {
12844113e47bSAl Viro 		int tx_loop = sock_sendmsg(conn->sock, &msg);
12854113e47bSAl Viro 		if (tx_loop <= 0) {
12864113e47bSAl Viro 			pr_debug("tx_loop: %d total_tx %d\n",
12874113e47bSAl Viro 				tx_loop, total_tx);
12884113e47bSAl Viro 			return tx_loop;
12894113e47bSAl Viro 		}
12904113e47bSAl Viro 		total_tx += tx_loop;
12914113e47bSAl Viro 		pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
12924113e47bSAl Viro 					tx_loop, total_tx, data);
12934113e47bSAl Viro 	}
12944113e47bSAl Viro 
12954113e47bSAl Viro 	return total_tx;
1296e48354ceSNicholas Bellinger }
1297e48354ceSNicholas Bellinger 
1298e48354ceSNicholas Bellinger void iscsit_collect_login_stats(
1299e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
1300e48354ceSNicholas Bellinger 	u8 status_class,
1301e48354ceSNicholas Bellinger 	u8 status_detail)
1302e48354ceSNicholas Bellinger {
1303e48354ceSNicholas Bellinger 	struct iscsi_param *intrname = NULL;
1304e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1305e48354ceSNicholas Bellinger 	struct iscsi_login_stats *ls;
1306e48354ceSNicholas Bellinger 
1307e48354ceSNicholas Bellinger 	tiqn = iscsit_snmp_get_tiqn(conn);
1308e48354ceSNicholas Bellinger 	if (!tiqn)
1309e48354ceSNicholas Bellinger 		return;
1310e48354ceSNicholas Bellinger 
1311e48354ceSNicholas Bellinger 	ls = &tiqn->login_stats;
1312e48354ceSNicholas Bellinger 
1313e48354ceSNicholas Bellinger 	spin_lock(&ls->lock);
1314e48354ceSNicholas Bellinger 	if (status_class == ISCSI_STATUS_CLS_SUCCESS)
1315e48354ceSNicholas Bellinger 		ls->accepts++;
1316e48354ceSNicholas Bellinger 	else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
1317e48354ceSNicholas Bellinger 		ls->redirects++;
1318e48354ceSNicholas Bellinger 		ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
1319e48354ceSNicholas Bellinger 	} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR)  &&
1320e48354ceSNicholas Bellinger 		 (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
1321e48354ceSNicholas Bellinger 		ls->authenticate_fails++;
1322e48354ceSNicholas Bellinger 		ls->last_fail_type =  ISCSI_LOGIN_FAIL_AUTHENTICATE;
1323e48354ceSNicholas Bellinger 	} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR)  &&
1324e48354ceSNicholas Bellinger 		 (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
1325e48354ceSNicholas Bellinger 		ls->authorize_fails++;
1326e48354ceSNicholas Bellinger 		ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
1327e48354ceSNicholas Bellinger 	} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1328e48354ceSNicholas Bellinger 		 (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
1329e48354ceSNicholas Bellinger 		ls->negotiate_fails++;
1330e48354ceSNicholas Bellinger 		ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
1331e48354ceSNicholas Bellinger 	} else {
1332e48354ceSNicholas Bellinger 		ls->other_fails++;
1333e48354ceSNicholas Bellinger 		ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
1334e48354ceSNicholas Bellinger 	}
1335e48354ceSNicholas Bellinger 
1336e48354ceSNicholas Bellinger 	/* Save initiator name, ip address and time, if it is a failed login */
1337e48354ceSNicholas Bellinger 	if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
1338e48354ceSNicholas Bellinger 		if (conn->param_list)
1339e48354ceSNicholas Bellinger 			intrname = iscsi_find_param_from_key(INITIATORNAME,
1340e48354ceSNicholas Bellinger 							     conn->param_list);
1341fdc84d11SJoern Engel 		strlcpy(ls->last_intr_fail_name,
1342fdc84d11SJoern Engel 		       (intrname ? intrname->value : "Unknown"),
1343fdc84d11SJoern Engel 		       sizeof(ls->last_intr_fail_name));
1344e48354ceSNicholas Bellinger 
1345baa4d64bSNicholas Bellinger 		ls->last_intr_fail_ip_family = conn->login_family;
1346baa4d64bSNicholas Bellinger 
1347dc58f760SAndy Grover 		ls->last_intr_fail_sockaddr = conn->login_sockaddr;
1348e48354ceSNicholas Bellinger 		ls->last_fail_time = get_jiffies_64();
1349e48354ceSNicholas Bellinger 	}
1350e48354ceSNicholas Bellinger 
1351e48354ceSNicholas Bellinger 	spin_unlock(&ls->lock);
1352e48354ceSNicholas Bellinger }
1353e48354ceSNicholas Bellinger 
1354e48354ceSNicholas Bellinger struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
1355e48354ceSNicholas Bellinger {
1356e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg;
1357e48354ceSNicholas Bellinger 
135817c61ad6SNicholas Bellinger 	if (!conn)
1359e48354ceSNicholas Bellinger 		return NULL;
1360e48354ceSNicholas Bellinger 
136117c61ad6SNicholas Bellinger 	tpg = conn->tpg;
1362e48354ceSNicholas Bellinger 	if (!tpg)
1363e48354ceSNicholas Bellinger 		return NULL;
1364e48354ceSNicholas Bellinger 
1365e48354ceSNicholas Bellinger 	if (!tpg->tpg_tiqn)
1366e48354ceSNicholas Bellinger 		return NULL;
1367e48354ceSNicholas Bellinger 
1368e48354ceSNicholas Bellinger 	return tpg->tpg_tiqn;
1369e48354ceSNicholas Bellinger }
1370dce6190cSDavid Disseldorp 
1371dce6190cSDavid Disseldorp void iscsit_fill_cxn_timeout_err_stats(struct iscsi_session *sess)
1372dce6190cSDavid Disseldorp {
1373dce6190cSDavid Disseldorp 	struct iscsi_portal_group *tpg = sess->tpg;
1374dce6190cSDavid Disseldorp 	struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
1375dce6190cSDavid Disseldorp 
1376dce6190cSDavid Disseldorp 	if (!tiqn)
1377dce6190cSDavid Disseldorp 		return;
1378dce6190cSDavid Disseldorp 
1379dce6190cSDavid Disseldorp 	spin_lock_bh(&tiqn->sess_err_stats.lock);
1380dce6190cSDavid Disseldorp 	strlcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
1381dce6190cSDavid Disseldorp 			sess->sess_ops->InitiatorName,
1382dce6190cSDavid Disseldorp 			sizeof(tiqn->sess_err_stats.last_sess_fail_rem_name));
1383dce6190cSDavid Disseldorp 	tiqn->sess_err_stats.last_sess_failure_type =
1384dce6190cSDavid Disseldorp 			ISCSI_SESS_ERR_CXN_TIMEOUT;
1385dce6190cSDavid Disseldorp 	tiqn->sess_err_stats.cxn_timeout_errors++;
1386dce6190cSDavid Disseldorp 	atomic_long_inc(&sess->conn_timeout_errors);
1387dce6190cSDavid Disseldorp 	spin_unlock_bh(&tiqn->sess_err_stats.lock);
1388dce6190cSDavid Disseldorp }
1389