• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * This file contains the iSCSI Target specific utility functions.
3  *
4  * (c) Copyright 2007-2013 Datera, Inc.
5  *
6  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  ******************************************************************************/
18 
19 #include <linux/list.h>
20 #include <linux/percpu_ida.h>
21 #include <scsi/scsi_tcq.h>
22 #include <scsi/iscsi_proto.h>
23 #include <target/target_core_base.h>
24 #include <target/target_core_fabric.h>
25 #include <target/iscsi/iscsi_transport.h>
26 
27 #include <target/iscsi/iscsi_target_core.h>
28 #include "iscsi_target_parameters.h"
29 #include "iscsi_target_seq_pdu_list.h"
30 #include "iscsi_target_datain_values.h"
31 #include "iscsi_target_erl0.h"
32 #include "iscsi_target_erl1.h"
33 #include "iscsi_target_erl2.h"
34 #include "iscsi_target_tpg.h"
35 #include "iscsi_target_util.h"
36 #include "iscsi_target.h"
37 
38 #define PRINT_BUFF(buff, len)					\
39 {								\
40 	int zzz;						\
41 								\
42 	pr_debug("%d:\n", __LINE__);				\
43 	for (zzz = 0; zzz < len; zzz++) {			\
44 		if (zzz % 16 == 0) {				\
45 			if (zzz)				\
46 				pr_debug("\n");			\
47 			pr_debug("%4i: ", zzz);			\
48 		}						\
49 		pr_debug("%02x ", (unsigned char) (buff)[zzz]);	\
50 	}							\
51 	if ((len + 1) % 16)					\
52 		pr_debug("\n");					\
53 }
54 
55 extern struct list_head g_tiqn_list;
56 extern spinlock_t tiqn_lock;
57 
58 /*
59  *	Called with cmd->r2t_lock held.
60  */
iscsit_add_r2t_to_list(struct iscsi_cmd * cmd,u32 offset,u32 xfer_len,int recovery,u32 r2t_sn)61 int iscsit_add_r2t_to_list(
62 	struct iscsi_cmd *cmd,
63 	u32 offset,
64 	u32 xfer_len,
65 	int recovery,
66 	u32 r2t_sn)
67 {
68 	struct iscsi_r2t *r2t;
69 
70 	r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC);
71 	if (!r2t) {
72 		pr_err("Unable to allocate memory for struct iscsi_r2t.\n");
73 		return -1;
74 	}
75 	INIT_LIST_HEAD(&r2t->r2t_list);
76 
77 	r2t->recovery_r2t = recovery;
78 	r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn;
79 	r2t->offset = offset;
80 	r2t->xfer_len = xfer_len;
81 	list_add_tail(&r2t->r2t_list, &cmd->cmd_r2t_list);
82 	spin_unlock_bh(&cmd->r2t_lock);
83 
84 	iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
85 
86 	spin_lock_bh(&cmd->r2t_lock);
87 	return 0;
88 }
89 
iscsit_get_r2t_for_eos(struct iscsi_cmd * cmd,u32 offset,u32 length)90 struct iscsi_r2t *iscsit_get_r2t_for_eos(
91 	struct iscsi_cmd *cmd,
92 	u32 offset,
93 	u32 length)
94 {
95 	struct iscsi_r2t *r2t;
96 
97 	spin_lock_bh(&cmd->r2t_lock);
98 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
99 		if ((r2t->offset <= offset) &&
100 		    (r2t->offset + r2t->xfer_len) >= (offset + length)) {
101 			spin_unlock_bh(&cmd->r2t_lock);
102 			return r2t;
103 		}
104 	}
105 	spin_unlock_bh(&cmd->r2t_lock);
106 
107 	pr_err("Unable to locate R2T for Offset: %u, Length:"
108 			" %u\n", offset, length);
109 	return NULL;
110 }
111 
iscsit_get_r2t_from_list(struct iscsi_cmd * cmd)112 struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsi_cmd *cmd)
113 {
114 	struct iscsi_r2t *r2t;
115 
116 	spin_lock_bh(&cmd->r2t_lock);
117 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
118 		if (!r2t->sent_r2t) {
119 			spin_unlock_bh(&cmd->r2t_lock);
120 			return r2t;
121 		}
122 	}
123 	spin_unlock_bh(&cmd->r2t_lock);
124 
125 	pr_err("Unable to locate next R2T to send for ITT:"
126 			" 0x%08x.\n", cmd->init_task_tag);
127 	return NULL;
128 }
129 
130 /*
131  *	Called with cmd->r2t_lock held.
132  */
iscsit_free_r2t(struct iscsi_r2t * r2t,struct iscsi_cmd * cmd)133 void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsi_cmd *cmd)
134 {
135 	list_del(&r2t->r2t_list);
136 	kmem_cache_free(lio_r2t_cache, r2t);
137 }
138 
iscsit_free_r2ts_from_list(struct iscsi_cmd * cmd)139 void iscsit_free_r2ts_from_list(struct iscsi_cmd *cmd)
140 {
141 	struct iscsi_r2t *r2t, *r2t_tmp;
142 
143 	spin_lock_bh(&cmd->r2t_lock);
144 	list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list)
145 		iscsit_free_r2t(r2t, cmd);
146 	spin_unlock_bh(&cmd->r2t_lock);
147 }
148 
149 /*
150  * May be called from software interrupt (timer) context for allocating
151  * iSCSI NopINs.
152  */
iscsit_allocate_cmd(struct iscsi_conn * conn,int state)153 struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, int state)
154 {
155 	struct iscsi_cmd *cmd;
156 	struct se_session *se_sess = conn->sess->se_sess;
157 	int size, tag;
158 
159 	tag = percpu_ida_alloc(&se_sess->sess_tag_pool, state);
160 	if (tag < 0)
161 		return NULL;
162 
163 	size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
164 	cmd = (struct iscsi_cmd *)(se_sess->sess_cmd_map + (tag * size));
165 	memset(cmd, 0, size);
166 
167 	cmd->se_cmd.map_tag = tag;
168 	cmd->conn = conn;
169 	INIT_LIST_HEAD(&cmd->i_conn_node);
170 	INIT_LIST_HEAD(&cmd->datain_list);
171 	INIT_LIST_HEAD(&cmd->cmd_r2t_list);
172 	spin_lock_init(&cmd->datain_lock);
173 	spin_lock_init(&cmd->dataout_timeout_lock);
174 	spin_lock_init(&cmd->istate_lock);
175 	spin_lock_init(&cmd->error_lock);
176 	spin_lock_init(&cmd->r2t_lock);
177 
178 	return cmd;
179 }
180 EXPORT_SYMBOL(iscsit_allocate_cmd);
181 
iscsit_get_seq_holder_for_datain(struct iscsi_cmd * cmd,u32 seq_send_order)182 struct iscsi_seq *iscsit_get_seq_holder_for_datain(
183 	struct iscsi_cmd *cmd,
184 	u32 seq_send_order)
185 {
186 	u32 i;
187 
188 	for (i = 0; i < cmd->seq_count; i++)
189 		if (cmd->seq_list[i].seq_send_order == seq_send_order)
190 			return &cmd->seq_list[i];
191 
192 	return NULL;
193 }
194 
iscsit_get_seq_holder_for_r2t(struct iscsi_cmd * cmd)195 struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
196 {
197 	u32 i;
198 
199 	if (!cmd->seq_list) {
200 		pr_err("struct iscsi_cmd->seq_list is NULL!\n");
201 		return NULL;
202 	}
203 
204 	for (i = 0; i < cmd->seq_count; i++) {
205 		if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
206 			continue;
207 		if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
208 			cmd->seq_send_order++;
209 			return &cmd->seq_list[i];
210 		}
211 	}
212 
213 	return NULL;
214 }
215 
iscsit_get_holder_for_r2tsn(struct iscsi_cmd * cmd,u32 r2t_sn)216 struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
217 	struct iscsi_cmd *cmd,
218 	u32 r2t_sn)
219 {
220 	struct iscsi_r2t *r2t;
221 
222 	spin_lock_bh(&cmd->r2t_lock);
223 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
224 		if (r2t->r2t_sn == r2t_sn) {
225 			spin_unlock_bh(&cmd->r2t_lock);
226 			return r2t;
227 		}
228 	}
229 	spin_unlock_bh(&cmd->r2t_lock);
230 
231 	return NULL;
232 }
233 
iscsit_check_received_cmdsn(struct iscsi_session * sess,u32 cmdsn)234 static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
235 {
236 	u32 max_cmdsn;
237 	int ret;
238 
239 	/*
240 	 * This is the proper method of checking received CmdSN against
241 	 * ExpCmdSN and MaxCmdSN values, as well as accounting for out
242 	 * or order CmdSNs due to multiple connection sessions and/or
243 	 * CRC failures.
244 	 */
245 	max_cmdsn = atomic_read(&sess->max_cmd_sn);
246 	if (iscsi_sna_gt(cmdsn, max_cmdsn)) {
247 		pr_err("Received CmdSN: 0x%08x is greater than"
248 		       " MaxCmdSN: 0x%08x, ignoring.\n", cmdsn, max_cmdsn);
249 		ret = CMDSN_MAXCMDSN_OVERRUN;
250 
251 	} else if (cmdsn == sess->exp_cmd_sn) {
252 		sess->exp_cmd_sn++;
253 		pr_debug("Received CmdSN matches ExpCmdSN,"
254 		      " incremented ExpCmdSN to: 0x%08x\n",
255 		      sess->exp_cmd_sn);
256 		ret = CMDSN_NORMAL_OPERATION;
257 
258 	} else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
259 		pr_debug("Received CmdSN: 0x%08x is greater"
260 		      " than ExpCmdSN: 0x%08x, not acknowledging.\n",
261 		      cmdsn, sess->exp_cmd_sn);
262 		ret = CMDSN_HIGHER_THAN_EXP;
263 
264 	} else {
265 		pr_err("Received CmdSN: 0x%08x is less than"
266 		       " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
267 		       sess->exp_cmd_sn);
268 		ret = CMDSN_LOWER_THAN_EXP;
269 	}
270 
271 	return ret;
272 }
273 
274 /*
275  * Commands may be received out of order if MC/S is in use.
276  * Ensure they are executed in CmdSN order.
277  */
iscsit_sequence_cmd(struct iscsi_conn * conn,struct iscsi_cmd * cmd,unsigned char * buf,__be32 cmdsn)278 int iscsit_sequence_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
279 			unsigned char *buf, __be32 cmdsn)
280 {
281 	int ret, cmdsn_ret;
282 	bool reject = false;
283 	u8 reason = ISCSI_REASON_BOOKMARK_NO_RESOURCES;
284 
285 	mutex_lock(&conn->sess->cmdsn_mutex);
286 
287 	cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, be32_to_cpu(cmdsn));
288 	switch (cmdsn_ret) {
289 	case CMDSN_NORMAL_OPERATION:
290 		ret = iscsit_execute_cmd(cmd, 0);
291 		if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
292 			iscsit_execute_ooo_cmdsns(conn->sess);
293 		else if (ret < 0) {
294 			reject = true;
295 			ret = CMDSN_ERROR_CANNOT_RECOVER;
296 		}
297 		break;
298 	case CMDSN_HIGHER_THAN_EXP:
299 		ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, be32_to_cpu(cmdsn));
300 		if (ret < 0) {
301 			reject = true;
302 			ret = CMDSN_ERROR_CANNOT_RECOVER;
303 			break;
304 		}
305 		ret = CMDSN_HIGHER_THAN_EXP;
306 		break;
307 	case CMDSN_LOWER_THAN_EXP:
308 	case CMDSN_MAXCMDSN_OVERRUN:
309 	default:
310 		cmd->i_state = ISTATE_REMOVE;
311 		iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
312 		/*
313 		 * Existing callers for iscsit_sequence_cmd() will silently
314 		 * ignore commands with CMDSN_LOWER_THAN_EXP, so force this
315 		 * return for CMDSN_MAXCMDSN_OVERRUN as well..
316 		 */
317 		ret = CMDSN_LOWER_THAN_EXP;
318 		break;
319 	}
320 	mutex_unlock(&conn->sess->cmdsn_mutex);
321 
322 	if (reject)
323 		iscsit_reject_cmd(cmd, reason, buf);
324 
325 	return ret;
326 }
327 EXPORT_SYMBOL(iscsit_sequence_cmd);
328 
iscsit_check_unsolicited_dataout(struct iscsi_cmd * cmd,unsigned char * buf)329 int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
330 {
331 	struct iscsi_conn *conn = cmd->conn;
332 	struct se_cmd *se_cmd = &cmd->se_cmd;
333 	struct iscsi_data *hdr = (struct iscsi_data *) buf;
334 	u32 payload_length = ntoh24(hdr->dlength);
335 
336 	if (conn->sess->sess_ops->InitialR2T) {
337 		pr_err("Received unexpected unsolicited data"
338 			" while InitialR2T=Yes, protocol error.\n");
339 		transport_send_check_condition_and_sense(se_cmd,
340 				TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
341 		return -1;
342 	}
343 
344 	if ((cmd->first_burst_len + payload_length) >
345 	     conn->sess->sess_ops->FirstBurstLength) {
346 		pr_err("Total %u bytes exceeds FirstBurstLength: %u"
347 			" for this Unsolicited DataOut Burst.\n",
348 			(cmd->first_burst_len + payload_length),
349 				conn->sess->sess_ops->FirstBurstLength);
350 		transport_send_check_condition_and_sense(se_cmd,
351 				TCM_INCORRECT_AMOUNT_OF_DATA, 0);
352 		return -1;
353 	}
354 
355 	if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
356 		return 0;
357 
358 	if (((cmd->first_burst_len + payload_length) != cmd->se_cmd.data_length) &&
359 	    ((cmd->first_burst_len + payload_length) !=
360 	      conn->sess->sess_ops->FirstBurstLength)) {
361 		pr_err("Unsolicited non-immediate data received %u"
362 			" does not equal FirstBurstLength: %u, and does"
363 			" not equal ExpXferLen %u.\n",
364 			(cmd->first_burst_len + payload_length),
365 			conn->sess->sess_ops->FirstBurstLength, cmd->se_cmd.data_length);
366 		transport_send_check_condition_and_sense(se_cmd,
367 				TCM_INCORRECT_AMOUNT_OF_DATA, 0);
368 		return -1;
369 	}
370 	return 0;
371 }
372 
iscsit_find_cmd_from_itt(struct iscsi_conn * conn,itt_t init_task_tag)373 struct iscsi_cmd *iscsit_find_cmd_from_itt(
374 	struct iscsi_conn *conn,
375 	itt_t init_task_tag)
376 {
377 	struct iscsi_cmd *cmd;
378 
379 	spin_lock_bh(&conn->cmd_lock);
380 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
381 		if (cmd->init_task_tag == init_task_tag) {
382 			spin_unlock_bh(&conn->cmd_lock);
383 			return cmd;
384 		}
385 	}
386 	spin_unlock_bh(&conn->cmd_lock);
387 
388 	pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
389 			init_task_tag, conn->cid);
390 	return NULL;
391 }
392 EXPORT_SYMBOL(iscsit_find_cmd_from_itt);
393 
iscsit_find_cmd_from_itt_or_dump(struct iscsi_conn * conn,itt_t init_task_tag,u32 length)394 struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
395 	struct iscsi_conn *conn,
396 	itt_t init_task_tag,
397 	u32 length)
398 {
399 	struct iscsi_cmd *cmd;
400 
401 	spin_lock_bh(&conn->cmd_lock);
402 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
403 		if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT)
404 			continue;
405 		if (cmd->init_task_tag == init_task_tag) {
406 			spin_unlock_bh(&conn->cmd_lock);
407 			return cmd;
408 		}
409 	}
410 	spin_unlock_bh(&conn->cmd_lock);
411 
412 	pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
413 			" dumping payload\n", init_task_tag, conn->cid);
414 	if (length)
415 		iscsit_dump_data_payload(conn, length, 1);
416 
417 	return NULL;
418 }
419 
iscsit_find_cmd_from_ttt(struct iscsi_conn * conn,u32 targ_xfer_tag)420 struct iscsi_cmd *iscsit_find_cmd_from_ttt(
421 	struct iscsi_conn *conn,
422 	u32 targ_xfer_tag)
423 {
424 	struct iscsi_cmd *cmd = NULL;
425 
426 	spin_lock_bh(&conn->cmd_lock);
427 	list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
428 		if (cmd->targ_xfer_tag == targ_xfer_tag) {
429 			spin_unlock_bh(&conn->cmd_lock);
430 			return cmd;
431 		}
432 	}
433 	spin_unlock_bh(&conn->cmd_lock);
434 
435 	pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
436 			targ_xfer_tag, conn->cid);
437 	return NULL;
438 }
439 
iscsit_find_cmd_for_recovery(struct iscsi_session * sess,struct iscsi_cmd ** cmd_ptr,struct iscsi_conn_recovery ** cr_ptr,itt_t init_task_tag)440 int iscsit_find_cmd_for_recovery(
441 	struct iscsi_session *sess,
442 	struct iscsi_cmd **cmd_ptr,
443 	struct iscsi_conn_recovery **cr_ptr,
444 	itt_t init_task_tag)
445 {
446 	struct iscsi_cmd *cmd = NULL;
447 	struct iscsi_conn_recovery *cr;
448 	/*
449 	 * Scan through the inactive connection recovery list's command list.
450 	 * If init_task_tag matches the command is still alligent.
451 	 */
452 	spin_lock(&sess->cr_i_lock);
453 	list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
454 		spin_lock(&cr->conn_recovery_cmd_lock);
455 		list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
456 			if (cmd->init_task_tag == init_task_tag) {
457 				spin_unlock(&cr->conn_recovery_cmd_lock);
458 				spin_unlock(&sess->cr_i_lock);
459 
460 				*cr_ptr = cr;
461 				*cmd_ptr = cmd;
462 				return -2;
463 			}
464 		}
465 		spin_unlock(&cr->conn_recovery_cmd_lock);
466 	}
467 	spin_unlock(&sess->cr_i_lock);
468 	/*
469 	 * Scan through the active connection recovery list's command list.
470 	 * If init_task_tag matches the command is ready to be reassigned.
471 	 */
472 	spin_lock(&sess->cr_a_lock);
473 	list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
474 		spin_lock(&cr->conn_recovery_cmd_lock);
475 		list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
476 			if (cmd->init_task_tag == init_task_tag) {
477 				spin_unlock(&cr->conn_recovery_cmd_lock);
478 				spin_unlock(&sess->cr_a_lock);
479 
480 				*cr_ptr = cr;
481 				*cmd_ptr = cmd;
482 				return 0;
483 			}
484 		}
485 		spin_unlock(&cr->conn_recovery_cmd_lock);
486 	}
487 	spin_unlock(&sess->cr_a_lock);
488 
489 	return -1;
490 }
491 
iscsit_add_cmd_to_immediate_queue(struct iscsi_cmd * cmd,struct iscsi_conn * conn,u8 state)492 void iscsit_add_cmd_to_immediate_queue(
493 	struct iscsi_cmd *cmd,
494 	struct iscsi_conn *conn,
495 	u8 state)
496 {
497 	struct iscsi_queue_req *qr;
498 
499 	qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
500 	if (!qr) {
501 		pr_err("Unable to allocate memory for"
502 				" struct iscsi_queue_req\n");
503 		return;
504 	}
505 	INIT_LIST_HEAD(&qr->qr_list);
506 	qr->cmd = cmd;
507 	qr->state = state;
508 
509 	spin_lock_bh(&conn->immed_queue_lock);
510 	list_add_tail(&qr->qr_list, &conn->immed_queue_list);
511 	atomic_inc(&cmd->immed_queue_count);
512 	atomic_set(&conn->check_immediate_queue, 1);
513 	spin_unlock_bh(&conn->immed_queue_lock);
514 
515 	wake_up(&conn->queues_wq);
516 }
517 
iscsit_get_cmd_from_immediate_queue(struct iscsi_conn * conn)518 struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
519 {
520 	struct iscsi_queue_req *qr;
521 
522 	spin_lock_bh(&conn->immed_queue_lock);
523 	if (list_empty(&conn->immed_queue_list)) {
524 		spin_unlock_bh(&conn->immed_queue_lock);
525 		return NULL;
526 	}
527 	qr = list_first_entry(&conn->immed_queue_list,
528 			      struct iscsi_queue_req, qr_list);
529 
530 	list_del(&qr->qr_list);
531 	if (qr->cmd)
532 		atomic_dec(&qr->cmd->immed_queue_count);
533 	spin_unlock_bh(&conn->immed_queue_lock);
534 
535 	return qr;
536 }
537 
iscsit_remove_cmd_from_immediate_queue(struct iscsi_cmd * cmd,struct iscsi_conn * conn)538 static void iscsit_remove_cmd_from_immediate_queue(
539 	struct iscsi_cmd *cmd,
540 	struct iscsi_conn *conn)
541 {
542 	struct iscsi_queue_req *qr, *qr_tmp;
543 
544 	spin_lock_bh(&conn->immed_queue_lock);
545 	if (!atomic_read(&cmd->immed_queue_count)) {
546 		spin_unlock_bh(&conn->immed_queue_lock);
547 		return;
548 	}
549 
550 	list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
551 		if (qr->cmd != cmd)
552 			continue;
553 
554 		atomic_dec(&qr->cmd->immed_queue_count);
555 		list_del(&qr->qr_list);
556 		kmem_cache_free(lio_qr_cache, qr);
557 	}
558 	spin_unlock_bh(&conn->immed_queue_lock);
559 
560 	if (atomic_read(&cmd->immed_queue_count)) {
561 		pr_err("ITT: 0x%08x immed_queue_count: %d\n",
562 			cmd->init_task_tag,
563 			atomic_read(&cmd->immed_queue_count));
564 	}
565 }
566 
iscsit_add_cmd_to_response_queue(struct iscsi_cmd * cmd,struct iscsi_conn * conn,u8 state)567 void iscsit_add_cmd_to_response_queue(
568 	struct iscsi_cmd *cmd,
569 	struct iscsi_conn *conn,
570 	u8 state)
571 {
572 	struct iscsi_queue_req *qr;
573 
574 	qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
575 	if (!qr) {
576 		pr_err("Unable to allocate memory for"
577 			" struct iscsi_queue_req\n");
578 		return;
579 	}
580 	INIT_LIST_HEAD(&qr->qr_list);
581 	qr->cmd = cmd;
582 	qr->state = state;
583 
584 	spin_lock_bh(&conn->response_queue_lock);
585 	list_add_tail(&qr->qr_list, &conn->response_queue_list);
586 	atomic_inc(&cmd->response_queue_count);
587 	spin_unlock_bh(&conn->response_queue_lock);
588 
589 	wake_up(&conn->queues_wq);
590 }
591 
iscsit_get_cmd_from_response_queue(struct iscsi_conn * conn)592 struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
593 {
594 	struct iscsi_queue_req *qr;
595 
596 	spin_lock_bh(&conn->response_queue_lock);
597 	if (list_empty(&conn->response_queue_list)) {
598 		spin_unlock_bh(&conn->response_queue_lock);
599 		return NULL;
600 	}
601 
602 	qr = list_first_entry(&conn->response_queue_list,
603 			      struct iscsi_queue_req, qr_list);
604 
605 	list_del(&qr->qr_list);
606 	if (qr->cmd)
607 		atomic_dec(&qr->cmd->response_queue_count);
608 	spin_unlock_bh(&conn->response_queue_lock);
609 
610 	return qr;
611 }
612 
iscsit_remove_cmd_from_response_queue(struct iscsi_cmd * cmd,struct iscsi_conn * conn)613 static void iscsit_remove_cmd_from_response_queue(
614 	struct iscsi_cmd *cmd,
615 	struct iscsi_conn *conn)
616 {
617 	struct iscsi_queue_req *qr, *qr_tmp;
618 
619 	spin_lock_bh(&conn->response_queue_lock);
620 	if (!atomic_read(&cmd->response_queue_count)) {
621 		spin_unlock_bh(&conn->response_queue_lock);
622 		return;
623 	}
624 
625 	list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
626 				qr_list) {
627 		if (qr->cmd != cmd)
628 			continue;
629 
630 		atomic_dec(&qr->cmd->response_queue_count);
631 		list_del(&qr->qr_list);
632 		kmem_cache_free(lio_qr_cache, qr);
633 	}
634 	spin_unlock_bh(&conn->response_queue_lock);
635 
636 	if (atomic_read(&cmd->response_queue_count)) {
637 		pr_err("ITT: 0x%08x response_queue_count: %d\n",
638 			cmd->init_task_tag,
639 			atomic_read(&cmd->response_queue_count));
640 	}
641 }
642 
iscsit_conn_all_queues_empty(struct iscsi_conn * conn)643 bool iscsit_conn_all_queues_empty(struct iscsi_conn *conn)
644 {
645 	bool empty;
646 
647 	spin_lock_bh(&conn->immed_queue_lock);
648 	empty = list_empty(&conn->immed_queue_list);
649 	spin_unlock_bh(&conn->immed_queue_lock);
650 
651 	if (!empty)
652 		return empty;
653 
654 	spin_lock_bh(&conn->response_queue_lock);
655 	empty = list_empty(&conn->response_queue_list);
656 	spin_unlock_bh(&conn->response_queue_lock);
657 
658 	return empty;
659 }
660 
iscsit_free_queue_reqs_for_conn(struct iscsi_conn * conn)661 void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
662 {
663 	struct iscsi_queue_req *qr, *qr_tmp;
664 
665 	spin_lock_bh(&conn->immed_queue_lock);
666 	list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
667 		list_del(&qr->qr_list);
668 		if (qr->cmd)
669 			atomic_dec(&qr->cmd->immed_queue_count);
670 
671 		kmem_cache_free(lio_qr_cache, qr);
672 	}
673 	spin_unlock_bh(&conn->immed_queue_lock);
674 
675 	spin_lock_bh(&conn->response_queue_lock);
676 	list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
677 			qr_list) {
678 		list_del(&qr->qr_list);
679 		if (qr->cmd)
680 			atomic_dec(&qr->cmd->response_queue_count);
681 
682 		kmem_cache_free(lio_qr_cache, qr);
683 	}
684 	spin_unlock_bh(&conn->response_queue_lock);
685 }
686 
iscsit_release_cmd(struct iscsi_cmd * cmd)687 void iscsit_release_cmd(struct iscsi_cmd *cmd)
688 {
689 	struct iscsi_session *sess;
690 	struct se_cmd *se_cmd = &cmd->se_cmd;
691 
692 	if (cmd->conn)
693 		sess = cmd->conn->sess;
694 	else
695 		sess = cmd->sess;
696 
697 	BUG_ON(!sess || !sess->se_sess);
698 
699 	kfree(cmd->buf_ptr);
700 	kfree(cmd->pdu_list);
701 	kfree(cmd->seq_list);
702 	kfree(cmd->tmr_req);
703 	kfree(cmd->iov_data);
704 	kfree(cmd->text_in_ptr);
705 
706 	percpu_ida_free(&sess->se_sess->sess_tag_pool, se_cmd->map_tag);
707 }
708 EXPORT_SYMBOL(iscsit_release_cmd);
709 
__iscsit_free_cmd(struct iscsi_cmd * cmd,bool scsi_cmd,bool check_queues)710 void __iscsit_free_cmd(struct iscsi_cmd *cmd, bool scsi_cmd,
711 		       bool check_queues)
712 {
713 	struct iscsi_conn *conn = cmd->conn;
714 
715 	if (scsi_cmd) {
716 		if (cmd->data_direction == DMA_TO_DEVICE) {
717 			iscsit_stop_dataout_timer(cmd);
718 			iscsit_free_r2ts_from_list(cmd);
719 		}
720 		if (cmd->data_direction == DMA_FROM_DEVICE)
721 			iscsit_free_all_datain_reqs(cmd);
722 	}
723 
724 	if (conn && check_queues) {
725 		iscsit_remove_cmd_from_immediate_queue(cmd, conn);
726 		iscsit_remove_cmd_from_response_queue(cmd, conn);
727 	}
728 }
729 
iscsit_free_cmd(struct iscsi_cmd * cmd,bool shutdown)730 void iscsit_free_cmd(struct iscsi_cmd *cmd, bool shutdown)
731 {
732 	struct se_cmd *se_cmd = NULL;
733 	int rc;
734 	bool op_scsi = false;
735 	/*
736 	 * Determine if a struct se_cmd is associated with
737 	 * this struct iscsi_cmd.
738 	 */
739 	switch (cmd->iscsi_opcode) {
740 	case ISCSI_OP_SCSI_CMD:
741 		op_scsi = true;
742 		/*
743 		 * Fallthrough
744 		 */
745 	case ISCSI_OP_SCSI_TMFUNC:
746 		se_cmd = &cmd->se_cmd;
747 		__iscsit_free_cmd(cmd, op_scsi, shutdown);
748 		rc = transport_generic_free_cmd(se_cmd, shutdown);
749 		if (!rc && shutdown && se_cmd->se_sess) {
750 			__iscsit_free_cmd(cmd, op_scsi, shutdown);
751 			target_put_sess_cmd(se_cmd);
752 		}
753 		break;
754 	case ISCSI_OP_REJECT:
755 		/*
756 		 * Handle special case for REJECT when iscsi_add_reject*() has
757 		 * overwritten the original iscsi_opcode assignment, and the
758 		 * associated cmd->se_cmd needs to be released.
759 		 */
760 		if (cmd->se_cmd.se_tfo != NULL) {
761 			se_cmd = &cmd->se_cmd;
762 			__iscsit_free_cmd(cmd, true, shutdown);
763 
764 			rc = transport_generic_free_cmd(&cmd->se_cmd, shutdown);
765 			if (!rc && shutdown && se_cmd->se_sess) {
766 				__iscsit_free_cmd(cmd, true, shutdown);
767 				target_put_sess_cmd(se_cmd);
768 			}
769 			break;
770 		}
771 		/* Fall-through */
772 	default:
773 		__iscsit_free_cmd(cmd, false, shutdown);
774 		iscsit_release_cmd(cmd);
775 		break;
776 	}
777 }
778 
iscsit_check_session_usage_count(struct iscsi_session * sess)779 int iscsit_check_session_usage_count(struct iscsi_session *sess)
780 {
781 	spin_lock_bh(&sess->session_usage_lock);
782 	if (sess->session_usage_count != 0) {
783 		sess->session_waiting_on_uc = 1;
784 		spin_unlock_bh(&sess->session_usage_lock);
785 		if (in_interrupt())
786 			return 2;
787 
788 		wait_for_completion(&sess->session_waiting_on_uc_comp);
789 		return 1;
790 	}
791 	spin_unlock_bh(&sess->session_usage_lock);
792 
793 	return 0;
794 }
795 
iscsit_dec_session_usage_count(struct iscsi_session * sess)796 void iscsit_dec_session_usage_count(struct iscsi_session *sess)
797 {
798 	spin_lock_bh(&sess->session_usage_lock);
799 	sess->session_usage_count--;
800 
801 	if (!sess->session_usage_count && sess->session_waiting_on_uc)
802 		complete(&sess->session_waiting_on_uc_comp);
803 
804 	spin_unlock_bh(&sess->session_usage_lock);
805 }
806 
iscsit_inc_session_usage_count(struct iscsi_session * sess)807 void iscsit_inc_session_usage_count(struct iscsi_session *sess)
808 {
809 	spin_lock_bh(&sess->session_usage_lock);
810 	sess->session_usage_count++;
811 	spin_unlock_bh(&sess->session_usage_lock);
812 }
813 
iscsit_get_conn_from_cid(struct iscsi_session * sess,u16 cid)814 struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
815 {
816 	struct iscsi_conn *conn;
817 
818 	spin_lock_bh(&sess->conn_lock);
819 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
820 		if ((conn->cid == cid) &&
821 		    (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
822 			iscsit_inc_conn_usage_count(conn);
823 			spin_unlock_bh(&sess->conn_lock);
824 			return conn;
825 		}
826 	}
827 	spin_unlock_bh(&sess->conn_lock);
828 
829 	return NULL;
830 }
831 
iscsit_get_conn_from_cid_rcfr(struct iscsi_session * sess,u16 cid)832 struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
833 {
834 	struct iscsi_conn *conn;
835 
836 	spin_lock_bh(&sess->conn_lock);
837 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
838 		if (conn->cid == cid) {
839 			iscsit_inc_conn_usage_count(conn);
840 			spin_lock(&conn->state_lock);
841 			atomic_set(&conn->connection_wait_rcfr, 1);
842 			spin_unlock(&conn->state_lock);
843 			spin_unlock_bh(&sess->conn_lock);
844 			return conn;
845 		}
846 	}
847 	spin_unlock_bh(&sess->conn_lock);
848 
849 	return NULL;
850 }
851 
iscsit_check_conn_usage_count(struct iscsi_conn * conn)852 void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
853 {
854 	spin_lock_bh(&conn->conn_usage_lock);
855 	if (conn->conn_usage_count != 0) {
856 		conn->conn_waiting_on_uc = 1;
857 		spin_unlock_bh(&conn->conn_usage_lock);
858 
859 		wait_for_completion(&conn->conn_waiting_on_uc_comp);
860 		return;
861 	}
862 	spin_unlock_bh(&conn->conn_usage_lock);
863 }
864 
iscsit_dec_conn_usage_count(struct iscsi_conn * conn)865 void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
866 {
867 	spin_lock_bh(&conn->conn_usage_lock);
868 	conn->conn_usage_count--;
869 
870 	if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
871 		complete(&conn->conn_waiting_on_uc_comp);
872 
873 	spin_unlock_bh(&conn->conn_usage_lock);
874 }
875 
iscsit_inc_conn_usage_count(struct iscsi_conn * conn)876 void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
877 {
878 	spin_lock_bh(&conn->conn_usage_lock);
879 	conn->conn_usage_count++;
880 	spin_unlock_bh(&conn->conn_usage_lock);
881 }
882 
iscsit_add_nopin(struct iscsi_conn * conn,int want_response)883 static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
884 {
885 	u8 state;
886 	struct iscsi_cmd *cmd;
887 
888 	cmd = iscsit_allocate_cmd(conn, TASK_RUNNING);
889 	if (!cmd)
890 		return -1;
891 
892 	cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
893 	state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
894 				ISTATE_SEND_NOPIN_NO_RESPONSE;
895 	cmd->init_task_tag = RESERVED_ITT;
896 	cmd->targ_xfer_tag = (want_response) ?
897 			     session_get_next_ttt(conn->sess) : 0xFFFFFFFF;
898 	spin_lock_bh(&conn->cmd_lock);
899 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
900 	spin_unlock_bh(&conn->cmd_lock);
901 
902 	if (want_response)
903 		iscsit_start_nopin_response_timer(conn);
904 	iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
905 
906 	return 0;
907 }
908 
iscsit_handle_nopin_response_timeout(unsigned long data)909 static void iscsit_handle_nopin_response_timeout(unsigned long data)
910 {
911 	struct iscsi_conn *conn = (struct iscsi_conn *) data;
912 
913 	iscsit_inc_conn_usage_count(conn);
914 
915 	spin_lock_bh(&conn->nopin_timer_lock);
916 	if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
917 		spin_unlock_bh(&conn->nopin_timer_lock);
918 		iscsit_dec_conn_usage_count(conn);
919 		return;
920 	}
921 
922 	pr_debug("Did not receive response to NOPIN on CID: %hu on"
923 		" SID: %u, failing connection.\n", conn->cid,
924 			conn->sess->sid);
925 	conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
926 	spin_unlock_bh(&conn->nopin_timer_lock);
927 
928 	{
929 	struct iscsi_portal_group *tpg = conn->sess->tpg;
930 	struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
931 
932 	if (tiqn) {
933 		spin_lock_bh(&tiqn->sess_err_stats.lock);
934 		strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
935 				conn->sess->sess_ops->InitiatorName);
936 		tiqn->sess_err_stats.last_sess_failure_type =
937 				ISCSI_SESS_ERR_CXN_TIMEOUT;
938 		tiqn->sess_err_stats.cxn_timeout_errors++;
939 		atomic_long_inc(&conn->sess->conn_timeout_errors);
940 		spin_unlock_bh(&tiqn->sess_err_stats.lock);
941 	}
942 	}
943 
944 	iscsit_cause_connection_reinstatement(conn, 0);
945 	iscsit_dec_conn_usage_count(conn);
946 }
947 
iscsit_mod_nopin_response_timer(struct iscsi_conn * conn)948 void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
949 {
950 	struct iscsi_session *sess = conn->sess;
951 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
952 
953 	spin_lock_bh(&conn->nopin_timer_lock);
954 	if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
955 		spin_unlock_bh(&conn->nopin_timer_lock);
956 		return;
957 	}
958 
959 	mod_timer(&conn->nopin_response_timer,
960 		(get_jiffies_64() + na->nopin_response_timeout * HZ));
961 	spin_unlock_bh(&conn->nopin_timer_lock);
962 }
963 
964 /*
965  *	Called with conn->nopin_timer_lock held.
966  */
iscsit_start_nopin_response_timer(struct iscsi_conn * conn)967 void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
968 {
969 	struct iscsi_session *sess = conn->sess;
970 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
971 
972 	spin_lock_bh(&conn->nopin_timer_lock);
973 	if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
974 		spin_unlock_bh(&conn->nopin_timer_lock);
975 		return;
976 	}
977 
978 	init_timer(&conn->nopin_response_timer);
979 	conn->nopin_response_timer.expires =
980 		(get_jiffies_64() + na->nopin_response_timeout * HZ);
981 	conn->nopin_response_timer.data = (unsigned long)conn;
982 	conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
983 	conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
984 	conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
985 	add_timer(&conn->nopin_response_timer);
986 
987 	pr_debug("Started NOPIN Response Timer on CID: %d to %u"
988 		" seconds\n", conn->cid, na->nopin_response_timeout);
989 	spin_unlock_bh(&conn->nopin_timer_lock);
990 }
991 
iscsit_stop_nopin_response_timer(struct iscsi_conn * conn)992 void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
993 {
994 	spin_lock_bh(&conn->nopin_timer_lock);
995 	if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
996 		spin_unlock_bh(&conn->nopin_timer_lock);
997 		return;
998 	}
999 	conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
1000 	spin_unlock_bh(&conn->nopin_timer_lock);
1001 
1002 	del_timer_sync(&conn->nopin_response_timer);
1003 
1004 	spin_lock_bh(&conn->nopin_timer_lock);
1005 	conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
1006 	spin_unlock_bh(&conn->nopin_timer_lock);
1007 }
1008 
iscsit_handle_nopin_timeout(unsigned long data)1009 static void iscsit_handle_nopin_timeout(unsigned long data)
1010 {
1011 	struct iscsi_conn *conn = (struct iscsi_conn *) data;
1012 
1013 	iscsit_inc_conn_usage_count(conn);
1014 
1015 	spin_lock_bh(&conn->nopin_timer_lock);
1016 	if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
1017 		spin_unlock_bh(&conn->nopin_timer_lock);
1018 		iscsit_dec_conn_usage_count(conn);
1019 		return;
1020 	}
1021 	conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1022 	spin_unlock_bh(&conn->nopin_timer_lock);
1023 
1024 	iscsit_add_nopin(conn, 1);
1025 	iscsit_dec_conn_usage_count(conn);
1026 }
1027 
1028 /*
1029  * Called with conn->nopin_timer_lock held.
1030  */
__iscsit_start_nopin_timer(struct iscsi_conn * conn)1031 void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
1032 {
1033 	struct iscsi_session *sess = conn->sess;
1034 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1035 	/*
1036 	* NOPIN timeout is disabled.
1037 	 */
1038 	if (!na->nopin_timeout)
1039 		return;
1040 
1041 	if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
1042 		return;
1043 
1044 	init_timer(&conn->nopin_timer);
1045 	conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1046 	conn->nopin_timer.data = (unsigned long)conn;
1047 	conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1048 	conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1049 	conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1050 	add_timer(&conn->nopin_timer);
1051 
1052 	pr_debug("Started NOPIN Timer on CID: %d at %u second"
1053 		" interval\n", conn->cid, na->nopin_timeout);
1054 }
1055 
iscsit_start_nopin_timer(struct iscsi_conn * conn)1056 void iscsit_start_nopin_timer(struct iscsi_conn *conn)
1057 {
1058 	struct iscsi_session *sess = conn->sess;
1059 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1060 	/*
1061 	 * NOPIN timeout is disabled..
1062 	 */
1063 	if (!na->nopin_timeout)
1064 		return;
1065 
1066 	spin_lock_bh(&conn->nopin_timer_lock);
1067 	if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
1068 		spin_unlock_bh(&conn->nopin_timer_lock);
1069 		return;
1070 	}
1071 
1072 	init_timer(&conn->nopin_timer);
1073 	conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
1074 	conn->nopin_timer.data = (unsigned long)conn;
1075 	conn->nopin_timer.function = iscsit_handle_nopin_timeout;
1076 	conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
1077 	conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
1078 	add_timer(&conn->nopin_timer);
1079 
1080 	pr_debug("Started NOPIN Timer on CID: %d at %u second"
1081 			" interval\n", conn->cid, na->nopin_timeout);
1082 	spin_unlock_bh(&conn->nopin_timer_lock);
1083 }
1084 
iscsit_stop_nopin_timer(struct iscsi_conn * conn)1085 void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
1086 {
1087 	spin_lock_bh(&conn->nopin_timer_lock);
1088 	if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
1089 		spin_unlock_bh(&conn->nopin_timer_lock);
1090 		return;
1091 	}
1092 	conn->nopin_timer_flags |= ISCSI_TF_STOP;
1093 	spin_unlock_bh(&conn->nopin_timer_lock);
1094 
1095 	del_timer_sync(&conn->nopin_timer);
1096 
1097 	spin_lock_bh(&conn->nopin_timer_lock);
1098 	conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
1099 	spin_unlock_bh(&conn->nopin_timer_lock);
1100 }
1101 
iscsit_send_tx_data(struct iscsi_cmd * cmd,struct iscsi_conn * conn,int use_misc)1102 int iscsit_send_tx_data(
1103 	struct iscsi_cmd *cmd,
1104 	struct iscsi_conn *conn,
1105 	int use_misc)
1106 {
1107 	int tx_sent, tx_size;
1108 	u32 iov_count;
1109 	struct kvec *iov;
1110 
1111 send_data:
1112 	tx_size = cmd->tx_size;
1113 
1114 	if (!use_misc) {
1115 		iov = &cmd->iov_data[0];
1116 		iov_count = cmd->iov_data_count;
1117 	} else {
1118 		iov = &cmd->iov_misc[0];
1119 		iov_count = cmd->iov_misc_count;
1120 	}
1121 
1122 	tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
1123 	if (tx_size != tx_sent) {
1124 		if (tx_sent == -EAGAIN) {
1125 			pr_err("tx_data() returned -EAGAIN\n");
1126 			goto send_data;
1127 		} else
1128 			return -1;
1129 	}
1130 	cmd->tx_size = 0;
1131 
1132 	return 0;
1133 }
1134 
iscsit_fe_sendpage_sg(struct iscsi_cmd * cmd,struct iscsi_conn * conn)1135 int iscsit_fe_sendpage_sg(
1136 	struct iscsi_cmd *cmd,
1137 	struct iscsi_conn *conn)
1138 {
1139 	struct scatterlist *sg = cmd->first_data_sg;
1140 	struct kvec iov;
1141 	u32 tx_hdr_size, data_len;
1142 	u32 offset = cmd->first_data_sg_off;
1143 	int tx_sent, iov_off;
1144 
1145 send_hdr:
1146 	tx_hdr_size = ISCSI_HDR_LEN;
1147 	if (conn->conn_ops->HeaderDigest)
1148 		tx_hdr_size += ISCSI_CRC_LEN;
1149 
1150 	iov.iov_base = cmd->pdu;
1151 	iov.iov_len = tx_hdr_size;
1152 
1153 	tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
1154 	if (tx_hdr_size != tx_sent) {
1155 		if (tx_sent == -EAGAIN) {
1156 			pr_err("tx_data() returned -EAGAIN\n");
1157 			goto send_hdr;
1158 		}
1159 		return -1;
1160 	}
1161 
1162 	data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
1163 	/*
1164 	 * Set iov_off used by padding and data digest tx_data() calls below
1165 	 * in order to determine proper offset into cmd->iov_data[]
1166 	 */
1167 	if (conn->conn_ops->DataDigest) {
1168 		data_len -= ISCSI_CRC_LEN;
1169 		if (cmd->padding)
1170 			iov_off = (cmd->iov_data_count - 2);
1171 		else
1172 			iov_off = (cmd->iov_data_count - 1);
1173 	} else {
1174 		iov_off = (cmd->iov_data_count - 1);
1175 	}
1176 	/*
1177 	 * Perform sendpage() for each page in the scatterlist
1178 	 */
1179 	while (data_len) {
1180 		u32 space = (sg->length - offset);
1181 		u32 sub_len = min_t(u32, data_len, space);
1182 send_pg:
1183 		tx_sent = conn->sock->ops->sendpage(conn->sock,
1184 					sg_page(sg), sg->offset + offset, sub_len, 0);
1185 		if (tx_sent != sub_len) {
1186 			if (tx_sent == -EAGAIN) {
1187 				pr_err("tcp_sendpage() returned"
1188 						" -EAGAIN\n");
1189 				goto send_pg;
1190 			}
1191 
1192 			pr_err("tcp_sendpage() failure: %d\n",
1193 					tx_sent);
1194 			return -1;
1195 		}
1196 
1197 		data_len -= sub_len;
1198 		offset = 0;
1199 		sg = sg_next(sg);
1200 	}
1201 
1202 send_padding:
1203 	if (cmd->padding) {
1204 		struct kvec *iov_p = &cmd->iov_data[iov_off++];
1205 
1206 		tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
1207 		if (cmd->padding != tx_sent) {
1208 			if (tx_sent == -EAGAIN) {
1209 				pr_err("tx_data() returned -EAGAIN\n");
1210 				goto send_padding;
1211 			}
1212 			return -1;
1213 		}
1214 	}
1215 
1216 send_datacrc:
1217 	if (conn->conn_ops->DataDigest) {
1218 		struct kvec *iov_d = &cmd->iov_data[iov_off];
1219 
1220 		tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
1221 		if (ISCSI_CRC_LEN != tx_sent) {
1222 			if (tx_sent == -EAGAIN) {
1223 				pr_err("tx_data() returned -EAGAIN\n");
1224 				goto send_datacrc;
1225 			}
1226 			return -1;
1227 		}
1228 	}
1229 
1230 	return 0;
1231 }
1232 
1233 /*
1234  *      This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
1235  *      back to the Initiator when an expection condition occurs with the
1236  *      errors set in status_class and status_detail.
1237  *
1238  *      Parameters:     iSCSI Connection, Status Class, Status Detail.
1239  *      Returns:        0 on success, -1 on error.
1240  */
iscsit_tx_login_rsp(struct iscsi_conn * conn,u8 status_class,u8 status_detail)1241 int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
1242 {
1243 	struct iscsi_login_rsp *hdr;
1244 	struct iscsi_login *login = conn->conn_login;
1245 
1246 	login->login_failed = 1;
1247 	iscsit_collect_login_stats(conn, status_class, status_detail);
1248 
1249 	memset(&login->rsp[0], 0, ISCSI_HDR_LEN);
1250 
1251 	hdr	= (struct iscsi_login_rsp *)&login->rsp[0];
1252 	hdr->opcode		= ISCSI_OP_LOGIN_RSP;
1253 	hdr->status_class	= status_class;
1254 	hdr->status_detail	= status_detail;
1255 	hdr->itt		= conn->login_itt;
1256 
1257 	return conn->conn_transport->iscsit_put_login_tx(conn, login, 0);
1258 }
1259 
iscsit_print_session_params(struct iscsi_session * sess)1260 void iscsit_print_session_params(struct iscsi_session *sess)
1261 {
1262 	struct iscsi_conn *conn;
1263 
1264 	pr_debug("-----------------------------[Session Params for"
1265 		" SID: %u]-----------------------------\n", sess->sid);
1266 	spin_lock_bh(&sess->conn_lock);
1267 	list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
1268 		iscsi_dump_conn_ops(conn->conn_ops);
1269 	spin_unlock_bh(&sess->conn_lock);
1270 
1271 	iscsi_dump_sess_ops(sess->sess_ops);
1272 }
1273 
iscsit_do_rx_data(struct iscsi_conn * conn,struct iscsi_data_count * count)1274 static int iscsit_do_rx_data(
1275 	struct iscsi_conn *conn,
1276 	struct iscsi_data_count *count)
1277 {
1278 	int data = count->data_length, rx_loop = 0, total_rx = 0;
1279 	struct msghdr msg;
1280 
1281 	if (!conn || !conn->sock || !conn->conn_ops)
1282 		return -1;
1283 
1284 	memset(&msg, 0, sizeof(struct msghdr));
1285 	iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC,
1286 		      count->iov, count->iov_count, data);
1287 
1288 	while (total_rx < data) {
1289 		rx_loop = sock_recvmsg(conn->sock, &msg,
1290 				      (data - total_rx), MSG_WAITALL);
1291 		if (rx_loop <= 0) {
1292 			pr_debug("rx_loop: %d total_rx: %d\n",
1293 				rx_loop, total_rx);
1294 			return rx_loop;
1295 		}
1296 		total_rx += rx_loop;
1297 		pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
1298 				rx_loop, total_rx, data);
1299 	}
1300 
1301 	return total_rx;
1302 }
1303 
iscsit_do_tx_data(struct iscsi_conn * conn,struct iscsi_data_count * count)1304 static int iscsit_do_tx_data(
1305 	struct iscsi_conn *conn,
1306 	struct iscsi_data_count *count)
1307 {
1308 	int ret, iov_len;
1309 	struct kvec *iov_p;
1310 	struct msghdr msg;
1311 
1312 	if (!conn || !conn->sock || !conn->conn_ops)
1313 		return -1;
1314 
1315 	if (count->data_length <= 0) {
1316 		pr_err("Data length is: %d\n", count->data_length);
1317 		return -1;
1318 	}
1319 
1320 	memset(&msg, 0, sizeof(struct msghdr));
1321 
1322 	iov_p = count->iov;
1323 	iov_len = count->iov_count;
1324 
1325 	ret = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
1326 			     count->data_length);
1327 	if (ret != count->data_length) {
1328 		pr_err("Unexpected ret: %d send data %d\n",
1329 		       ret, count->data_length);
1330 		return -EPIPE;
1331 	}
1332 	pr_debug("ret: %d, sent data: %d\n", ret, count->data_length);
1333 
1334 	return ret;
1335 }
1336 
rx_data(struct iscsi_conn * conn,struct kvec * iov,int iov_count,int data)1337 int rx_data(
1338 	struct iscsi_conn *conn,
1339 	struct kvec *iov,
1340 	int iov_count,
1341 	int data)
1342 {
1343 	struct iscsi_data_count c;
1344 
1345 	if (!conn || !conn->sock || !conn->conn_ops)
1346 		return -1;
1347 
1348 	memset(&c, 0, sizeof(struct iscsi_data_count));
1349 	c.iov = iov;
1350 	c.iov_count = iov_count;
1351 	c.data_length = data;
1352 	c.type = ISCSI_RX_DATA;
1353 
1354 	return iscsit_do_rx_data(conn, &c);
1355 }
1356 
tx_data(struct iscsi_conn * conn,struct kvec * iov,int iov_count,int data)1357 int tx_data(
1358 	struct iscsi_conn *conn,
1359 	struct kvec *iov,
1360 	int iov_count,
1361 	int data)
1362 {
1363 	struct iscsi_data_count c;
1364 
1365 	if (!conn || !conn->sock || !conn->conn_ops)
1366 		return -1;
1367 
1368 	memset(&c, 0, sizeof(struct iscsi_data_count));
1369 	c.iov = iov;
1370 	c.iov_count = iov_count;
1371 	c.data_length = data;
1372 	c.type = ISCSI_TX_DATA;
1373 
1374 	return iscsit_do_tx_data(conn, &c);
1375 }
1376 
sockaddr_equal(struct sockaddr_storage * x,struct sockaddr_storage * y)1377 static bool sockaddr_equal(struct sockaddr_storage *x, struct sockaddr_storage *y)
1378 {
1379 	switch (x->ss_family) {
1380 	case AF_INET: {
1381 		struct sockaddr_in *sinx = (struct sockaddr_in *)x;
1382 		struct sockaddr_in *siny = (struct sockaddr_in *)y;
1383 		if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
1384 			return false;
1385 		if (sinx->sin_port != siny->sin_port)
1386 			return false;
1387 		break;
1388 	}
1389 	case AF_INET6: {
1390 		struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
1391 		struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
1392 		if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
1393 			return false;
1394 		if (sinx->sin6_port != siny->sin6_port)
1395 			return false;
1396 		break;
1397 	}
1398 	default:
1399 		return false;
1400 	}
1401 	return true;
1402 }
1403 
iscsit_collect_login_stats(struct iscsi_conn * conn,u8 status_class,u8 status_detail)1404 void iscsit_collect_login_stats(
1405 	struct iscsi_conn *conn,
1406 	u8 status_class,
1407 	u8 status_detail)
1408 {
1409 	struct iscsi_param *intrname = NULL;
1410 	struct iscsi_tiqn *tiqn;
1411 	struct iscsi_login_stats *ls;
1412 
1413 	tiqn = iscsit_snmp_get_tiqn(conn);
1414 	if (!tiqn)
1415 		return;
1416 
1417 	ls = &tiqn->login_stats;
1418 
1419 	spin_lock(&ls->lock);
1420 	if (sockaddr_equal(&conn->login_sockaddr, &ls->last_intr_fail_sockaddr) &&
1421 	    ((get_jiffies_64() - ls->last_fail_time) < 10)) {
1422 		/* We already have the failure info for this login */
1423 		spin_unlock(&ls->lock);
1424 		return;
1425 	}
1426 
1427 	if (status_class == ISCSI_STATUS_CLS_SUCCESS)
1428 		ls->accepts++;
1429 	else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
1430 		ls->redirects++;
1431 		ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
1432 	} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR)  &&
1433 		 (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
1434 		ls->authenticate_fails++;
1435 		ls->last_fail_type =  ISCSI_LOGIN_FAIL_AUTHENTICATE;
1436 	} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR)  &&
1437 		 (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
1438 		ls->authorize_fails++;
1439 		ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
1440 	} else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
1441 		 (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
1442 		ls->negotiate_fails++;
1443 		ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
1444 	} else {
1445 		ls->other_fails++;
1446 		ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
1447 	}
1448 
1449 	/* Save initiator name, ip address and time, if it is a failed login */
1450 	if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
1451 		if (conn->param_list)
1452 			intrname = iscsi_find_param_from_key(INITIATORNAME,
1453 							     conn->param_list);
1454 		strlcpy(ls->last_intr_fail_name,
1455 		       (intrname ? intrname->value : "Unknown"),
1456 		       sizeof(ls->last_intr_fail_name));
1457 
1458 		ls->last_intr_fail_ip_family = conn->login_family;
1459 
1460 		ls->last_intr_fail_sockaddr = conn->login_sockaddr;
1461 		ls->last_fail_time = get_jiffies_64();
1462 	}
1463 
1464 	spin_unlock(&ls->lock);
1465 }
1466 
iscsit_snmp_get_tiqn(struct iscsi_conn * conn)1467 struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
1468 {
1469 	struct iscsi_portal_group *tpg;
1470 
1471 	if (!conn || !conn->sess)
1472 		return NULL;
1473 
1474 	tpg = conn->sess->tpg;
1475 	if (!tpg)
1476 		return NULL;
1477 
1478 	if (!tpg->tpg_tiqn)
1479 		return NULL;
1480 
1481 	return tpg->tpg_tiqn;
1482 }
1483