1 /*******************************************************************************
2  * This file contains error recovery level one used by the iSCSI Target driver.
3  *
4  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5  *
6  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7  *
8  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  ******************************************************************************/
20 
21 #include <linux/list.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 "iscsi_target_core.h"
28 #include "iscsi_target_seq_pdu_list.h"
29 #include "iscsi_target_datain_values.h"
30 #include "iscsi_target_device.h"
31 #include "iscsi_target_tpg.h"
32 #include "iscsi_target_util.h"
33 #include "iscsi_target_erl0.h"
34 #include "iscsi_target_erl1.h"
35 #include "iscsi_target_erl2.h"
36 #include "iscsi_target.h"
37 
38 #define OFFLOAD_BUF_SIZE	32768
39 
40 /*
41  *	Used to dump excess datain payload for certain error recovery
42  *	situations.  Receive in OFFLOAD_BUF_SIZE max of datain per rx_data().
43  *
44  *	dump_padding_digest denotes if padding and data digests need
45  *	to be dumped.
46  */
47 int iscsit_dump_data_payload(
48 	struct iscsi_conn *conn,
49 	u32 buf_len,
50 	int dump_padding_digest)
51 {
52 	char *buf, pad_bytes[4];
53 	int ret = DATAOUT_WITHIN_COMMAND_RECOVERY, rx_got;
54 	u32 length, padding, offset = 0, size;
55 	struct kvec iov;
56 
57 	if (conn->sess->sess_ops->RDMAExtensions)
58 		return 0;
59 
60 	length = (buf_len > OFFLOAD_BUF_SIZE) ? OFFLOAD_BUF_SIZE : buf_len;
61 
62 	buf = kzalloc(length, GFP_ATOMIC);
63 	if (!buf) {
64 		pr_err("Unable to allocate %u bytes for offload"
65 				" buffer.\n", length);
66 		return -1;
67 	}
68 	memset(&iov, 0, sizeof(struct kvec));
69 
70 	while (offset < buf_len) {
71 		size = ((offset + length) > buf_len) ?
72 			(buf_len - offset) : length;
73 
74 		iov.iov_len = size;
75 		iov.iov_base = buf;
76 
77 		rx_got = rx_data(conn, &iov, 1, size);
78 		if (rx_got != size) {
79 			ret = DATAOUT_CANNOT_RECOVER;
80 			goto out;
81 		}
82 
83 		offset += size;
84 	}
85 
86 	if (!dump_padding_digest)
87 		goto out;
88 
89 	padding = ((-buf_len) & 3);
90 	if (padding != 0) {
91 		iov.iov_len = padding;
92 		iov.iov_base = pad_bytes;
93 
94 		rx_got = rx_data(conn, &iov, 1, padding);
95 		if (rx_got != padding) {
96 			ret = DATAOUT_CANNOT_RECOVER;
97 			goto out;
98 		}
99 	}
100 
101 	if (conn->conn_ops->DataDigest) {
102 		u32 data_crc;
103 
104 		iov.iov_len = ISCSI_CRC_LEN;
105 		iov.iov_base = &data_crc;
106 
107 		rx_got = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
108 		if (rx_got != ISCSI_CRC_LEN) {
109 			ret = DATAOUT_CANNOT_RECOVER;
110 			goto out;
111 		}
112 	}
113 
114 out:
115 	kfree(buf);
116 	return ret;
117 }
118 
119 /*
120  *	Used for retransmitting R2Ts from a R2T SNACK request.
121  */
122 static int iscsit_send_recovery_r2t_for_snack(
123 	struct iscsi_cmd *cmd,
124 	struct iscsi_r2t *r2t)
125 {
126 	/*
127 	 * If the struct iscsi_r2t has not been sent yet, we can safely
128 	 * ignore retransmission
129 	 * of the R2TSN in question.
130 	 */
131 	spin_lock_bh(&cmd->r2t_lock);
132 	if (!r2t->sent_r2t) {
133 		spin_unlock_bh(&cmd->r2t_lock);
134 		return 0;
135 	}
136 	r2t->sent_r2t = 0;
137 	spin_unlock_bh(&cmd->r2t_lock);
138 
139 	iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
140 
141 	return 0;
142 }
143 
144 static int iscsit_handle_r2t_snack(
145 	struct iscsi_cmd *cmd,
146 	unsigned char *buf,
147 	u32 begrun,
148 	u32 runlength)
149 {
150 	u32 last_r2tsn;
151 	struct iscsi_r2t *r2t;
152 
153 	/*
154 	 * Make sure the initiator is not requesting retransmission
155 	 * of R2TSNs already acknowledged by a TMR TASK_REASSIGN.
156 	 */
157 	if ((cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
158 	    (begrun <= cmd->acked_data_sn)) {
159 		pr_err("ITT: 0x%08x, R2T SNACK requesting"
160 			" retransmission of R2TSN: 0x%08x to 0x%08x but already"
161 			" acked to  R2TSN: 0x%08x by TMR TASK_REASSIGN,"
162 			" protocol error.\n", cmd->init_task_tag, begrun,
163 			(begrun + runlength), cmd->acked_data_sn);
164 
165 			return iscsit_add_reject_from_cmd(
166 					ISCSI_REASON_PROTOCOL_ERROR,
167 					1, 0, buf, cmd);
168 	}
169 
170 	if (runlength) {
171 		if ((begrun + runlength) > cmd->r2t_sn) {
172 			pr_err("Command ITT: 0x%08x received R2T SNACK"
173 			" with BegRun: 0x%08x, RunLength: 0x%08x, exceeds"
174 			" current R2TSN: 0x%08x, protocol error.\n",
175 			cmd->init_task_tag, begrun, runlength, cmd->r2t_sn);
176 			return iscsit_add_reject_from_cmd(
177 				ISCSI_REASON_BOOKMARK_INVALID, 1, 0, buf, cmd);
178 		}
179 		last_r2tsn = (begrun + runlength);
180 	} else
181 		last_r2tsn = cmd->r2t_sn;
182 
183 	while (begrun < last_r2tsn) {
184 		r2t = iscsit_get_holder_for_r2tsn(cmd, begrun);
185 		if (!r2t)
186 			return -1;
187 		if (iscsit_send_recovery_r2t_for_snack(cmd, r2t) < 0)
188 			return -1;
189 
190 		begrun++;
191 	}
192 
193 	return 0;
194 }
195 
196 /*
197  *	Generates Offsets and NextBurstLength based on Begrun and Runlength
198  *	carried in a Data SNACK or ExpDataSN in TMR TASK_REASSIGN.
199  *
200  *	For DataSequenceInOrder=Yes and DataPDUInOrder=[Yes,No] only.
201  *
202  *	FIXME: How is this handled for a RData SNACK?
203  */
204 int iscsit_create_recovery_datain_values_datasequenceinorder_yes(
205 	struct iscsi_cmd *cmd,
206 	struct iscsi_datain_req *dr)
207 {
208 	u32 data_sn = 0, data_sn_count = 0;
209 	u32 pdu_start = 0, seq_no = 0;
210 	u32 begrun = dr->begrun;
211 	struct iscsi_conn *conn = cmd->conn;
212 
213 	while (begrun > data_sn++) {
214 		data_sn_count++;
215 		if ((dr->next_burst_len +
216 		     conn->conn_ops->MaxRecvDataSegmentLength) <
217 		     conn->sess->sess_ops->MaxBurstLength) {
218 			dr->read_data_done +=
219 				conn->conn_ops->MaxRecvDataSegmentLength;
220 			dr->next_burst_len +=
221 				conn->conn_ops->MaxRecvDataSegmentLength;
222 		} else {
223 			dr->read_data_done +=
224 				(conn->sess->sess_ops->MaxBurstLength -
225 				 dr->next_burst_len);
226 			dr->next_burst_len = 0;
227 			pdu_start += data_sn_count;
228 			data_sn_count = 0;
229 			seq_no++;
230 		}
231 	}
232 
233 	if (!conn->sess->sess_ops->DataPDUInOrder) {
234 		cmd->seq_no = seq_no;
235 		cmd->pdu_start = pdu_start;
236 		cmd->pdu_send_order = data_sn_count;
237 	}
238 
239 	return 0;
240 }
241 
242 /*
243  *	Generates Offsets and NextBurstLength based on Begrun and Runlength
244  *	carried in a Data SNACK or ExpDataSN in TMR TASK_REASSIGN.
245  *
246  *	For DataSequenceInOrder=No and DataPDUInOrder=[Yes,No] only.
247  *
248  *	FIXME: How is this handled for a RData SNACK?
249  */
250 int iscsit_create_recovery_datain_values_datasequenceinorder_no(
251 	struct iscsi_cmd *cmd,
252 	struct iscsi_datain_req *dr)
253 {
254 	int found_seq = 0, i;
255 	u32 data_sn, read_data_done = 0, seq_send_order = 0;
256 	u32 begrun = dr->begrun;
257 	u32 runlength = dr->runlength;
258 	struct iscsi_conn *conn = cmd->conn;
259 	struct iscsi_seq *first_seq = NULL, *seq = NULL;
260 
261 	if (!cmd->seq_list) {
262 		pr_err("struct iscsi_cmd->seq_list is NULL!\n");
263 		return -1;
264 	}
265 
266 	/*
267 	 * Calculate read_data_done for all sequences containing a
268 	 * first_datasn and last_datasn less than the BegRun.
269 	 *
270 	 * Locate the struct iscsi_seq the BegRun lies within and calculate
271 	 * NextBurstLenghth up to the DataSN based on MaxRecvDataSegmentLength.
272 	 *
273 	 * Also use struct iscsi_seq->seq_send_order to determine where to start.
274 	 */
275 	for (i = 0; i < cmd->seq_count; i++) {
276 		seq = &cmd->seq_list[i];
277 
278 		if (!seq->seq_send_order)
279 			first_seq = seq;
280 
281 		/*
282 		 * No data has been transferred for this DataIN sequence, so the
283 		 * seq->first_datasn and seq->last_datasn have not been set.
284 		 */
285 		if (!seq->sent) {
286 			pr_err("Ignoring non-sent sequence 0x%08x ->"
287 				" 0x%08x\n\n", seq->first_datasn,
288 				seq->last_datasn);
289 			continue;
290 		}
291 
292 		/*
293 		 * This DataIN sequence is precedes the received BegRun, add the
294 		 * total xfer_len of the sequence to read_data_done and reset
295 		 * seq->pdu_send_order.
296 		 */
297 		if ((seq->first_datasn < begrun) &&
298 				(seq->last_datasn < begrun)) {
299 			pr_err("Pre BegRun sequence 0x%08x ->"
300 				" 0x%08x\n", seq->first_datasn,
301 				seq->last_datasn);
302 
303 			read_data_done += cmd->seq_list[i].xfer_len;
304 			seq->next_burst_len = seq->pdu_send_order = 0;
305 			continue;
306 		}
307 
308 		/*
309 		 * The BegRun lies within this DataIN sequence.
310 		 */
311 		if ((seq->first_datasn <= begrun) &&
312 				(seq->last_datasn >= begrun)) {
313 			pr_err("Found sequence begrun: 0x%08x in"
314 				" 0x%08x -> 0x%08x\n", begrun,
315 				seq->first_datasn, seq->last_datasn);
316 
317 			seq_send_order = seq->seq_send_order;
318 			data_sn = seq->first_datasn;
319 			seq->next_burst_len = seq->pdu_send_order = 0;
320 			found_seq = 1;
321 
322 			/*
323 			 * For DataPDUInOrder=Yes, while the first DataSN of
324 			 * the sequence is less than the received BegRun, add
325 			 * the MaxRecvDataSegmentLength to read_data_done and
326 			 * to the sequence's next_burst_len;
327 			 *
328 			 * For DataPDUInOrder=No, while the first DataSN of the
329 			 * sequence is less than the received BegRun, find the
330 			 * struct iscsi_pdu of the DataSN in question and add the
331 			 * MaxRecvDataSegmentLength to read_data_done and to the
332 			 * sequence's next_burst_len;
333 			 */
334 			if (conn->sess->sess_ops->DataPDUInOrder) {
335 				while (data_sn < begrun) {
336 					seq->pdu_send_order++;
337 					read_data_done +=
338 						conn->conn_ops->MaxRecvDataSegmentLength;
339 					seq->next_burst_len +=
340 						conn->conn_ops->MaxRecvDataSegmentLength;
341 					data_sn++;
342 				}
343 			} else {
344 				int j;
345 				struct iscsi_pdu *pdu;
346 
347 				while (data_sn < begrun) {
348 					seq->pdu_send_order++;
349 
350 					for (j = 0; j < seq->pdu_count; j++) {
351 						pdu = &cmd->pdu_list[
352 							seq->pdu_start + j];
353 						if (pdu->data_sn == data_sn) {
354 							read_data_done +=
355 								pdu->length;
356 							seq->next_burst_len +=
357 								pdu->length;
358 						}
359 					}
360 					data_sn++;
361 				}
362 			}
363 			continue;
364 		}
365 
366 		/*
367 		 * This DataIN sequence is larger than the received BegRun,
368 		 * reset seq->pdu_send_order and continue.
369 		 */
370 		if ((seq->first_datasn > begrun) ||
371 				(seq->last_datasn > begrun)) {
372 			pr_err("Post BegRun sequence 0x%08x -> 0x%08x\n",
373 					seq->first_datasn, seq->last_datasn);
374 
375 			seq->next_burst_len = seq->pdu_send_order = 0;
376 			continue;
377 		}
378 	}
379 
380 	if (!found_seq) {
381 		if (!begrun) {
382 			if (!first_seq) {
383 				pr_err("ITT: 0x%08x, Begrun: 0x%08x"
384 					" but first_seq is NULL\n",
385 					cmd->init_task_tag, begrun);
386 				return -1;
387 			}
388 			seq_send_order = first_seq->seq_send_order;
389 			seq->next_burst_len = seq->pdu_send_order = 0;
390 			goto done;
391 		}
392 
393 		pr_err("Unable to locate struct iscsi_seq for ITT: 0x%08x,"
394 			" BegRun: 0x%08x, RunLength: 0x%08x while"
395 			" DataSequenceInOrder=No and DataPDUInOrder=%s.\n",
396 				cmd->init_task_tag, begrun, runlength,
397 			(conn->sess->sess_ops->DataPDUInOrder) ? "Yes" : "No");
398 		return -1;
399 	}
400 
401 done:
402 	dr->read_data_done = read_data_done;
403 	dr->seq_send_order = seq_send_order;
404 
405 	return 0;
406 }
407 
408 static int iscsit_handle_recovery_datain(
409 	struct iscsi_cmd *cmd,
410 	unsigned char *buf,
411 	u32 begrun,
412 	u32 runlength)
413 {
414 	struct iscsi_conn *conn = cmd->conn;
415 	struct iscsi_datain_req *dr;
416 	struct se_cmd *se_cmd = &cmd->se_cmd;
417 
418 	if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
419 		pr_err("Ignoring ITT: 0x%08x Data SNACK\n",
420 				cmd->init_task_tag);
421 		return 0;
422 	}
423 
424 	/*
425 	 * Make sure the initiator is not requesting retransmission
426 	 * of DataSNs already acknowledged by a Data ACK SNACK.
427 	 */
428 	if ((cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
429 	    (begrun <= cmd->acked_data_sn)) {
430 		pr_err("ITT: 0x%08x, Data SNACK requesting"
431 			" retransmission of DataSN: 0x%08x to 0x%08x but"
432 			" already acked to DataSN: 0x%08x by Data ACK SNACK,"
433 			" protocol error.\n", cmd->init_task_tag, begrun,
434 			(begrun + runlength), cmd->acked_data_sn);
435 
436 		return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
437 				1, 0, buf, cmd);
438 	}
439 
440 	/*
441 	 * Make sure BegRun and RunLength in the Data SNACK are sane.
442 	 * Note: (cmd->data_sn - 1) will carry the maximum DataSN sent.
443 	 */
444 	if ((begrun + runlength) > (cmd->data_sn - 1)) {
445 		pr_err("Initiator requesting BegRun: 0x%08x, RunLength"
446 			": 0x%08x greater than maximum DataSN: 0x%08x.\n",
447 				begrun, runlength, (cmd->data_sn - 1));
448 		return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
449 				1, 0, buf, cmd);
450 	}
451 
452 	dr = iscsit_allocate_datain_req();
453 	if (!dr)
454 		return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
455 				1, 0, buf, cmd);
456 
457 	dr->data_sn = dr->begrun = begrun;
458 	dr->runlength = runlength;
459 	dr->generate_recovery_values = 1;
460 	dr->recovery = DATAIN_WITHIN_COMMAND_RECOVERY;
461 
462 	iscsit_attach_datain_req(cmd, dr);
463 
464 	cmd->i_state = ISTATE_SEND_DATAIN;
465 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
466 
467 	return 0;
468 }
469 
470 int iscsit_handle_recovery_datain_or_r2t(
471 	struct iscsi_conn *conn,
472 	unsigned char *buf,
473 	itt_t init_task_tag,
474 	u32 targ_xfer_tag,
475 	u32 begrun,
476 	u32 runlength)
477 {
478 	struct iscsi_cmd *cmd;
479 
480 	cmd = iscsit_find_cmd_from_itt(conn, init_task_tag);
481 	if (!cmd)
482 		return 0;
483 
484 	/*
485 	 * FIXME: This will not work for bidi commands.
486 	 */
487 	switch (cmd->data_direction) {
488 	case DMA_TO_DEVICE:
489 		return iscsit_handle_r2t_snack(cmd, buf, begrun, runlength);
490 	case DMA_FROM_DEVICE:
491 		return iscsit_handle_recovery_datain(cmd, buf, begrun,
492 				runlength);
493 	default:
494 		pr_err("Unknown cmd->data_direction: 0x%02x\n",
495 				cmd->data_direction);
496 		return -1;
497 	}
498 
499 	return 0;
500 }
501 
502 /* #warning FIXME: Status SNACK needs to be dependent on OPCODE!!! */
503 int iscsit_handle_status_snack(
504 	struct iscsi_conn *conn,
505 	itt_t init_task_tag,
506 	u32 targ_xfer_tag,
507 	u32 begrun,
508 	u32 runlength)
509 {
510 	struct iscsi_cmd *cmd = NULL;
511 	u32 last_statsn;
512 	int found_cmd;
513 
514 	if (conn->exp_statsn > begrun) {
515 		pr_err("Got Status SNACK Begrun: 0x%08x, RunLength:"
516 			" 0x%08x but already got ExpStatSN: 0x%08x on CID:"
517 			" %hu.\n", begrun, runlength, conn->exp_statsn,
518 			conn->cid);
519 		return 0;
520 	}
521 
522 	last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);
523 
524 	while (begrun < last_statsn) {
525 		found_cmd = 0;
526 
527 		spin_lock_bh(&conn->cmd_lock);
528 		list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
529 			if (cmd->stat_sn == begrun) {
530 				found_cmd = 1;
531 				break;
532 			}
533 		}
534 		spin_unlock_bh(&conn->cmd_lock);
535 
536 		if (!found_cmd) {
537 			pr_err("Unable to find StatSN: 0x%08x for"
538 				" a Status SNACK, assuming this was a"
539 				" protactic SNACK for an untransmitted"
540 				" StatSN, ignoring.\n", begrun);
541 			begrun++;
542 			continue;
543 		}
544 
545 		spin_lock_bh(&cmd->istate_lock);
546 		if (cmd->i_state == ISTATE_SEND_DATAIN) {
547 			spin_unlock_bh(&cmd->istate_lock);
548 			pr_err("Ignoring Status SNACK for BegRun:"
549 				" 0x%08x, RunLength: 0x%08x, assuming this was"
550 				" a protactic SNACK for an untransmitted"
551 				" StatSN\n", begrun, runlength);
552 			begrun++;
553 			continue;
554 		}
555 		spin_unlock_bh(&cmd->istate_lock);
556 
557 		cmd->i_state = ISTATE_SEND_STATUS_RECOVERY;
558 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
559 		begrun++;
560 	}
561 
562 	return 0;
563 }
564 
565 int iscsit_handle_data_ack(
566 	struct iscsi_conn *conn,
567 	u32 targ_xfer_tag,
568 	u32 begrun,
569 	u32 runlength)
570 {
571 	struct iscsi_cmd *cmd = NULL;
572 
573 	cmd = iscsit_find_cmd_from_ttt(conn, targ_xfer_tag);
574 	if (!cmd) {
575 		pr_err("Data ACK SNACK for TTT: 0x%08x is"
576 			" invalid.\n", targ_xfer_tag);
577 		return -1;
578 	}
579 
580 	if (begrun <= cmd->acked_data_sn) {
581 		pr_err("ITT: 0x%08x Data ACK SNACK BegRUN: 0x%08x is"
582 			" less than the already acked DataSN: 0x%08x.\n",
583 			cmd->init_task_tag, begrun, cmd->acked_data_sn);
584 		return -1;
585 	}
586 
587 	/*
588 	 * For Data ACK SNACK, BegRun is the next expected DataSN.
589 	 * (see iSCSI v19: 10.16.6)
590 	 */
591 	cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
592 	cmd->acked_data_sn = (begrun - 1);
593 
594 	pr_debug("Received Data ACK SNACK for ITT: 0x%08x,"
595 		" updated acked DataSN to 0x%08x.\n",
596 			cmd->init_task_tag, cmd->acked_data_sn);
597 
598 	return 0;
599 }
600 
601 static int iscsit_send_recovery_r2t(
602 	struct iscsi_cmd *cmd,
603 	u32 offset,
604 	u32 xfer_len)
605 {
606 	int ret;
607 
608 	spin_lock_bh(&cmd->r2t_lock);
609 	ret = iscsit_add_r2t_to_list(cmd, offset, xfer_len, 1, 0);
610 	spin_unlock_bh(&cmd->r2t_lock);
611 
612 	return ret;
613 }
614 
615 int iscsit_dataout_datapduinorder_no_fbit(
616 	struct iscsi_cmd *cmd,
617 	struct iscsi_pdu *pdu)
618 {
619 	int i, send_recovery_r2t = 0, recovery = 0;
620 	u32 length = 0, offset = 0, pdu_count = 0, xfer_len = 0;
621 	struct iscsi_conn *conn = cmd->conn;
622 	struct iscsi_pdu *first_pdu = NULL;
623 
624 	/*
625 	 * Get an struct iscsi_pdu pointer to the first PDU, and total PDU count
626 	 * of the DataOUT sequence.
627 	 */
628 	if (conn->sess->sess_ops->DataSequenceInOrder) {
629 		for (i = 0; i < cmd->pdu_count; i++) {
630 			if (cmd->pdu_list[i].seq_no == pdu->seq_no) {
631 				if (!first_pdu)
632 					first_pdu = &cmd->pdu_list[i];
633 				 xfer_len += cmd->pdu_list[i].length;
634 				 pdu_count++;
635 			} else if (pdu_count)
636 				break;
637 		}
638 	} else {
639 		struct iscsi_seq *seq = cmd->seq_ptr;
640 
641 		first_pdu = &cmd->pdu_list[seq->pdu_start];
642 		pdu_count = seq->pdu_count;
643 	}
644 
645 	if (!first_pdu || !pdu_count)
646 		return DATAOUT_CANNOT_RECOVER;
647 
648 	/*
649 	 * Loop through the ending DataOUT Sequence checking each struct iscsi_pdu.
650 	 * The following ugly logic does batching of not received PDUs.
651 	 */
652 	for (i = 0; i < pdu_count; i++) {
653 		if (first_pdu[i].status == ISCSI_PDU_RECEIVED_OK) {
654 			if (!send_recovery_r2t)
655 				continue;
656 
657 			if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
658 				return DATAOUT_CANNOT_RECOVER;
659 
660 			send_recovery_r2t = length = offset = 0;
661 			continue;
662 		}
663 		/*
664 		 * Set recovery = 1 for any missing, CRC failed, or timed
665 		 * out PDUs to let the DataOUT logic know that this sequence
666 		 * has not been completed yet.
667 		 *
668 		 * Also, only send a Recovery R2T for ISCSI_PDU_NOT_RECEIVED.
669 		 * We assume if the PDU either failed CRC or timed out
670 		 * that a Recovery R2T has already been sent.
671 		 */
672 		recovery = 1;
673 
674 		if (first_pdu[i].status != ISCSI_PDU_NOT_RECEIVED)
675 			continue;
676 
677 		if (!offset)
678 			offset = first_pdu[i].offset;
679 		length += first_pdu[i].length;
680 
681 		send_recovery_r2t = 1;
682 	}
683 
684 	if (send_recovery_r2t)
685 		if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
686 			return DATAOUT_CANNOT_RECOVER;
687 
688 	return (!recovery) ? DATAOUT_NORMAL : DATAOUT_WITHIN_COMMAND_RECOVERY;
689 }
690 
691 static int iscsit_recalculate_dataout_values(
692 	struct iscsi_cmd *cmd,
693 	u32 pdu_offset,
694 	u32 pdu_length,
695 	u32 *r2t_offset,
696 	u32 *r2t_length)
697 {
698 	int i;
699 	struct iscsi_conn *conn = cmd->conn;
700 	struct iscsi_pdu *pdu = NULL;
701 
702 	if (conn->sess->sess_ops->DataSequenceInOrder) {
703 		cmd->data_sn = 0;
704 
705 		if (conn->sess->sess_ops->DataPDUInOrder) {
706 			*r2t_offset = cmd->write_data_done;
707 			*r2t_length = (cmd->seq_end_offset -
708 					cmd->write_data_done);
709 			return 0;
710 		}
711 
712 		*r2t_offset = cmd->seq_start_offset;
713 		*r2t_length = (cmd->seq_end_offset - cmd->seq_start_offset);
714 
715 		for (i = 0; i < cmd->pdu_count; i++) {
716 			pdu = &cmd->pdu_list[i];
717 
718 			if (pdu->status != ISCSI_PDU_RECEIVED_OK)
719 				continue;
720 
721 			if ((pdu->offset >= cmd->seq_start_offset) &&
722 			   ((pdu->offset + pdu->length) <=
723 			     cmd->seq_end_offset)) {
724 				if (!cmd->unsolicited_data)
725 					cmd->next_burst_len -= pdu->length;
726 				else
727 					cmd->first_burst_len -= pdu->length;
728 
729 				cmd->write_data_done -= pdu->length;
730 				pdu->status = ISCSI_PDU_NOT_RECEIVED;
731 			}
732 		}
733 	} else {
734 		struct iscsi_seq *seq = NULL;
735 
736 		seq = iscsit_get_seq_holder(cmd, pdu_offset, pdu_length);
737 		if (!seq)
738 			return -1;
739 
740 		*r2t_offset = seq->orig_offset;
741 		*r2t_length = seq->xfer_len;
742 
743 		cmd->write_data_done -= (seq->offset - seq->orig_offset);
744 		if (cmd->immediate_data)
745 			cmd->first_burst_len = cmd->write_data_done;
746 
747 		seq->data_sn = 0;
748 		seq->offset = seq->orig_offset;
749 		seq->next_burst_len = 0;
750 		seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
751 
752 		if (conn->sess->sess_ops->DataPDUInOrder)
753 			return 0;
754 
755 		for (i = 0; i < seq->pdu_count; i++) {
756 			pdu = &cmd->pdu_list[i+seq->pdu_start];
757 
758 			if (pdu->status != ISCSI_PDU_RECEIVED_OK)
759 				continue;
760 
761 			pdu->status = ISCSI_PDU_NOT_RECEIVED;
762 		}
763 	}
764 
765 	return 0;
766 }
767 
768 int iscsit_recover_dataout_sequence(
769 	struct iscsi_cmd *cmd,
770 	u32 pdu_offset,
771 	u32 pdu_length)
772 {
773 	u32 r2t_length = 0, r2t_offset = 0;
774 
775 	spin_lock_bh(&cmd->istate_lock);
776 	cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
777 	spin_unlock_bh(&cmd->istate_lock);
778 
779 	if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
780 			&r2t_offset, &r2t_length) < 0)
781 		return DATAOUT_CANNOT_RECOVER;
782 
783 	iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length);
784 
785 	return DATAOUT_WITHIN_COMMAND_RECOVERY;
786 }
787 
788 static struct iscsi_ooo_cmdsn *iscsit_allocate_ooo_cmdsn(void)
789 {
790 	struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL;
791 
792 	ooo_cmdsn = kmem_cache_zalloc(lio_ooo_cache, GFP_ATOMIC);
793 	if (!ooo_cmdsn) {
794 		pr_err("Unable to allocate memory for"
795 			" struct iscsi_ooo_cmdsn.\n");
796 		return NULL;
797 	}
798 	INIT_LIST_HEAD(&ooo_cmdsn->ooo_list);
799 
800 	return ooo_cmdsn;
801 }
802 
803 /*
804  *	Called with sess->cmdsn_mutex held.
805  */
806 static int iscsit_attach_ooo_cmdsn(
807 	struct iscsi_session *sess,
808 	struct iscsi_ooo_cmdsn *ooo_cmdsn)
809 {
810 	struct iscsi_ooo_cmdsn *ooo_tail, *ooo_tmp;
811 	/*
812 	 * We attach the struct iscsi_ooo_cmdsn entry to the out of order
813 	 * list in increasing CmdSN order.
814 	 * This allows iscsi_execute_ooo_cmdsns() to detect any
815 	 * additional CmdSN holes while performing delayed execution.
816 	 */
817 	if (list_empty(&sess->sess_ooo_cmdsn_list))
818 		list_add_tail(&ooo_cmdsn->ooo_list,
819 				&sess->sess_ooo_cmdsn_list);
820 	else {
821 		ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
822 				typeof(*ooo_tail), ooo_list);
823 		/*
824 		 * CmdSN is greater than the tail of the list.
825 		 */
826 		if (ooo_tail->cmdsn < ooo_cmdsn->cmdsn)
827 			list_add_tail(&ooo_cmdsn->ooo_list,
828 					&sess->sess_ooo_cmdsn_list);
829 		else {
830 			/*
831 			 * CmdSN is either lower than the head,  or somewhere
832 			 * in the middle.
833 			 */
834 			list_for_each_entry(ooo_tmp, &sess->sess_ooo_cmdsn_list,
835 						ooo_list) {
836 				if (ooo_tmp->cmdsn < ooo_cmdsn->cmdsn)
837 					continue;
838 
839 				list_add(&ooo_cmdsn->ooo_list,
840 					&ooo_tmp->ooo_list);
841 				break;
842 			}
843 		}
844 	}
845 
846 	return 0;
847 }
848 
849 /*
850  *	Removes an struct iscsi_ooo_cmdsn from a session's list,
851  *	called with struct iscsi_session->cmdsn_mutex held.
852  */
853 void iscsit_remove_ooo_cmdsn(
854 	struct iscsi_session *sess,
855 	struct iscsi_ooo_cmdsn *ooo_cmdsn)
856 {
857 	list_del(&ooo_cmdsn->ooo_list);
858 	kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
859 }
860 
861 void iscsit_clear_ooo_cmdsns_for_conn(struct iscsi_conn *conn)
862 {
863 	struct iscsi_ooo_cmdsn *ooo_cmdsn;
864 	struct iscsi_session *sess = conn->sess;
865 
866 	mutex_lock(&sess->cmdsn_mutex);
867 	list_for_each_entry(ooo_cmdsn, &sess->sess_ooo_cmdsn_list, ooo_list) {
868 		if (ooo_cmdsn->cid != conn->cid)
869 			continue;
870 
871 		ooo_cmdsn->cmd = NULL;
872 	}
873 	mutex_unlock(&sess->cmdsn_mutex);
874 }
875 
876 /*
877  *	Called with sess->cmdsn_mutex held.
878  */
879 int iscsit_execute_ooo_cmdsns(struct iscsi_session *sess)
880 {
881 	int ooo_count = 0;
882 	struct iscsi_cmd *cmd = NULL;
883 	struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
884 
885 	list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
886 				&sess->sess_ooo_cmdsn_list, ooo_list) {
887 		if (ooo_cmdsn->cmdsn != sess->exp_cmd_sn)
888 			continue;
889 
890 		if (!ooo_cmdsn->cmd) {
891 			sess->exp_cmd_sn++;
892 			iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
893 			continue;
894 		}
895 
896 		cmd = ooo_cmdsn->cmd;
897 		cmd->i_state = cmd->deferred_i_state;
898 		ooo_count++;
899 		sess->exp_cmd_sn++;
900 		pr_debug("Executing out of order CmdSN: 0x%08x,"
901 			" incremented ExpCmdSN to 0x%08x.\n",
902 			cmd->cmd_sn, sess->exp_cmd_sn);
903 
904 		iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
905 
906 		if (iscsit_execute_cmd(cmd, 1) < 0)
907 			return -1;
908 
909 		continue;
910 	}
911 
912 	return ooo_count;
913 }
914 
915 /*
916  *	Called either:
917  *
918  *	1. With sess->cmdsn_mutex held from iscsi_execute_ooo_cmdsns()
919  *	or iscsi_check_received_cmdsn().
920  *	2. With no locks held directly from iscsi_handle_XXX_pdu() functions
921  *	for immediate commands.
922  */
923 int iscsit_execute_cmd(struct iscsi_cmd *cmd, int ooo)
924 {
925 	struct se_cmd *se_cmd = &cmd->se_cmd;
926 	struct iscsi_conn *conn = cmd->conn;
927 	int lr = 0;
928 
929 	spin_lock_bh(&cmd->istate_lock);
930 	if (ooo)
931 		cmd->cmd_flags &= ~ICF_OOO_CMDSN;
932 
933 	switch (cmd->iscsi_opcode) {
934 	case ISCSI_OP_SCSI_CMD:
935 		/*
936 		 * Go ahead and send the CHECK_CONDITION status for
937 		 * any SCSI CDB exceptions that may have occurred.
938 		 */
939 		if (cmd->sense_reason) {
940 			if (cmd->sense_reason == TCM_RESERVATION_CONFLICT) {
941 				cmd->i_state = ISTATE_SEND_STATUS;
942 				spin_unlock_bh(&cmd->istate_lock);
943 				iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
944 						cmd->i_state);
945 				return 0;
946 			}
947 			spin_unlock_bh(&cmd->istate_lock);
948 			/*
949 			 * Determine if delayed TASK_ABORTED status for WRITEs
950 			 * should be sent now if no unsolicited data out
951 			 * payloads are expected, or if the delayed status
952 			 * should be sent after unsolicited data out with
953 			 * ISCSI_FLAG_CMD_FINAL set in iscsi_handle_data_out()
954 			 */
955 			if (transport_check_aborted_status(se_cmd,
956 					(cmd->unsolicited_data == 0)) != 0)
957 				return 0;
958 			/*
959 			 * Otherwise send CHECK_CONDITION and sense for
960 			 * exception
961 			 */
962 			return transport_send_check_condition_and_sense(se_cmd,
963 					cmd->sense_reason, 0);
964 		}
965 		/*
966 		 * Special case for delayed CmdSN with Immediate
967 		 * Data and/or Unsolicited Data Out attached.
968 		 */
969 		if (cmd->immediate_data) {
970 			if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
971 				spin_unlock_bh(&cmd->istate_lock);
972 				target_execute_cmd(&cmd->se_cmd);
973 				return 0;
974 			}
975 			spin_unlock_bh(&cmd->istate_lock);
976 
977 			if (!(cmd->cmd_flags &
978 					ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
979 				/*
980 				 * Send the delayed TASK_ABORTED status for
981 				 * WRITEs if no more unsolicitied data is
982 				 * expected.
983 				 */
984 				if (transport_check_aborted_status(se_cmd, 1)
985 						!= 0)
986 					return 0;
987 
988 				iscsit_set_dataout_sequence_values(cmd);
989 				conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
990 			}
991 			return 0;
992 		}
993 		/*
994 		 * The default handler.
995 		 */
996 		spin_unlock_bh(&cmd->istate_lock);
997 
998 		if ((cmd->data_direction == DMA_TO_DEVICE) &&
999 		    !(cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
1000 			/*
1001 			 * Send the delayed TASK_ABORTED status for WRITEs if
1002 			 * no more nsolicitied data is expected.
1003 			 */
1004 			if (transport_check_aborted_status(se_cmd, 1) != 0)
1005 				return 0;
1006 
1007 			iscsit_set_unsoliticed_dataout(cmd);
1008 		}
1009 		return transport_handle_cdb_direct(&cmd->se_cmd);
1010 
1011 	case ISCSI_OP_NOOP_OUT:
1012 	case ISCSI_OP_TEXT:
1013 		spin_unlock_bh(&cmd->istate_lock);
1014 		iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
1015 		break;
1016 	case ISCSI_OP_SCSI_TMFUNC:
1017 		if (cmd->se_cmd.se_tmr_req->response) {
1018 			spin_unlock_bh(&cmd->istate_lock);
1019 			iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
1020 					cmd->i_state);
1021 			return 0;
1022 		}
1023 		spin_unlock_bh(&cmd->istate_lock);
1024 
1025 		return transport_generic_handle_tmr(&cmd->se_cmd);
1026 	case ISCSI_OP_LOGOUT:
1027 		spin_unlock_bh(&cmd->istate_lock);
1028 		switch (cmd->logout_reason) {
1029 		case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
1030 			lr = iscsit_logout_closesession(cmd, cmd->conn);
1031 			break;
1032 		case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
1033 			lr = iscsit_logout_closeconnection(cmd, cmd->conn);
1034 			break;
1035 		case ISCSI_LOGOUT_REASON_RECOVERY:
1036 			lr = iscsit_logout_removeconnforrecovery(cmd, cmd->conn);
1037 			break;
1038 		default:
1039 			pr_err("Unknown iSCSI Logout Request Code:"
1040 				" 0x%02x\n", cmd->logout_reason);
1041 			return -1;
1042 		}
1043 
1044 		return lr;
1045 	default:
1046 		spin_unlock_bh(&cmd->istate_lock);
1047 		pr_err("Cannot perform out of order execution for"
1048 		" unknown iSCSI Opcode: 0x%02x\n", cmd->iscsi_opcode);
1049 		return -1;
1050 	}
1051 
1052 	return 0;
1053 }
1054 
1055 void iscsit_free_all_ooo_cmdsns(struct iscsi_session *sess)
1056 {
1057 	struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
1058 
1059 	mutex_lock(&sess->cmdsn_mutex);
1060 	list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
1061 			&sess->sess_ooo_cmdsn_list, ooo_list) {
1062 
1063 		list_del(&ooo_cmdsn->ooo_list);
1064 		kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
1065 	}
1066 	mutex_unlock(&sess->cmdsn_mutex);
1067 }
1068 
1069 int iscsit_handle_ooo_cmdsn(
1070 	struct iscsi_session *sess,
1071 	struct iscsi_cmd *cmd,
1072 	u32 cmdsn)
1073 {
1074 	int batch = 0;
1075 	struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL, *ooo_tail = NULL;
1076 
1077 	cmd->deferred_i_state		= cmd->i_state;
1078 	cmd->i_state			= ISTATE_DEFERRED_CMD;
1079 	cmd->cmd_flags			|= ICF_OOO_CMDSN;
1080 
1081 	if (list_empty(&sess->sess_ooo_cmdsn_list))
1082 		batch = 1;
1083 	else {
1084 		ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
1085 				typeof(*ooo_tail), ooo_list);
1086 		if (ooo_tail->cmdsn != (cmdsn - 1))
1087 			batch = 1;
1088 	}
1089 
1090 	ooo_cmdsn = iscsit_allocate_ooo_cmdsn();
1091 	if (!ooo_cmdsn)
1092 		return CMDSN_ERROR_CANNOT_RECOVER;
1093 
1094 	ooo_cmdsn->cmd			= cmd;
1095 	ooo_cmdsn->batch_count		= (batch) ?
1096 					  (cmdsn - sess->exp_cmd_sn) : 1;
1097 	ooo_cmdsn->cid			= cmd->conn->cid;
1098 	ooo_cmdsn->exp_cmdsn		= sess->exp_cmd_sn;
1099 	ooo_cmdsn->cmdsn		= cmdsn;
1100 
1101 	if (iscsit_attach_ooo_cmdsn(sess, ooo_cmdsn) < 0) {
1102 		kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
1103 		return CMDSN_ERROR_CANNOT_RECOVER;
1104 	}
1105 
1106 	return CMDSN_HIGHER_THAN_EXP;
1107 }
1108 
1109 static int iscsit_set_dataout_timeout_values(
1110 	struct iscsi_cmd *cmd,
1111 	u32 *offset,
1112 	u32 *length)
1113 {
1114 	struct iscsi_conn *conn = cmd->conn;
1115 	struct iscsi_r2t *r2t;
1116 
1117 	if (cmd->unsolicited_data) {
1118 		*offset = 0;
1119 		*length = (conn->sess->sess_ops->FirstBurstLength >
1120 			   cmd->se_cmd.data_length) ?
1121 			   cmd->se_cmd.data_length :
1122 			   conn->sess->sess_ops->FirstBurstLength;
1123 		return 0;
1124 	}
1125 
1126 	spin_lock_bh(&cmd->r2t_lock);
1127 	if (list_empty(&cmd->cmd_r2t_list)) {
1128 		pr_err("cmd->cmd_r2t_list is empty!\n");
1129 		spin_unlock_bh(&cmd->r2t_lock);
1130 		return -1;
1131 	}
1132 
1133 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
1134 		if (r2t->sent_r2t && !r2t->recovery_r2t && !r2t->seq_complete) {
1135 			*offset = r2t->offset;
1136 			*length = r2t->xfer_len;
1137 			spin_unlock_bh(&cmd->r2t_lock);
1138 			return 0;
1139 		}
1140 	}
1141 	spin_unlock_bh(&cmd->r2t_lock);
1142 
1143 	pr_err("Unable to locate any incomplete DataOUT"
1144 		" sequences for ITT: 0x%08x.\n", cmd->init_task_tag);
1145 
1146 	return -1;
1147 }
1148 
1149 /*
1150  *	NOTE: Called from interrupt (timer) context.
1151  */
1152 static void iscsit_handle_dataout_timeout(unsigned long data)
1153 {
1154 	u32 pdu_length = 0, pdu_offset = 0;
1155 	u32 r2t_length = 0, r2t_offset = 0;
1156 	struct iscsi_cmd *cmd = (struct iscsi_cmd *) data;
1157 	struct iscsi_conn *conn = cmd->conn;
1158 	struct iscsi_session *sess = NULL;
1159 	struct iscsi_node_attrib *na;
1160 
1161 	iscsit_inc_conn_usage_count(conn);
1162 
1163 	spin_lock_bh(&cmd->dataout_timeout_lock);
1164 	if (cmd->dataout_timer_flags & ISCSI_TF_STOP) {
1165 		spin_unlock_bh(&cmd->dataout_timeout_lock);
1166 		iscsit_dec_conn_usage_count(conn);
1167 		return;
1168 	}
1169 	cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
1170 	sess = conn->sess;
1171 	na = iscsit_tpg_get_node_attrib(sess);
1172 
1173 	if (!sess->sess_ops->ErrorRecoveryLevel) {
1174 		pr_debug("Unable to recover from DataOut timeout while"
1175 			" in ERL=0.\n");
1176 		goto failure;
1177 	}
1178 
1179 	if (++cmd->dataout_timeout_retries == na->dataout_timeout_retries) {
1180 		pr_debug("Command ITT: 0x%08x exceeded max retries"
1181 			" for DataOUT timeout %u, closing iSCSI connection.\n",
1182 			cmd->init_task_tag, na->dataout_timeout_retries);
1183 		goto failure;
1184 	}
1185 
1186 	cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
1187 
1188 	if (conn->sess->sess_ops->DataSequenceInOrder) {
1189 		if (conn->sess->sess_ops->DataPDUInOrder) {
1190 			pdu_offset = cmd->write_data_done;
1191 			if ((pdu_offset + (conn->sess->sess_ops->MaxBurstLength -
1192 			     cmd->next_burst_len)) > cmd->se_cmd.data_length)
1193 				pdu_length = (cmd->se_cmd.data_length -
1194 					cmd->write_data_done);
1195 			else
1196 				pdu_length = (conn->sess->sess_ops->MaxBurstLength -
1197 						cmd->next_burst_len);
1198 		} else {
1199 			pdu_offset = cmd->seq_start_offset;
1200 			pdu_length = (cmd->seq_end_offset -
1201 				cmd->seq_start_offset);
1202 		}
1203 	} else {
1204 		if (iscsit_set_dataout_timeout_values(cmd, &pdu_offset,
1205 				&pdu_length) < 0)
1206 			goto failure;
1207 	}
1208 
1209 	if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
1210 			&r2t_offset, &r2t_length) < 0)
1211 		goto failure;
1212 
1213 	pr_debug("Command ITT: 0x%08x timed out waiting for"
1214 		" completion of %sDataOUT Sequence Offset: %u, Length: %u\n",
1215 		cmd->init_task_tag, (cmd->unsolicited_data) ? "Unsolicited " :
1216 		"", r2t_offset, r2t_length);
1217 
1218 	if (iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length) < 0)
1219 		goto failure;
1220 
1221 	iscsit_start_dataout_timer(cmd, conn);
1222 	spin_unlock_bh(&cmd->dataout_timeout_lock);
1223 	iscsit_dec_conn_usage_count(conn);
1224 
1225 	return;
1226 
1227 failure:
1228 	spin_unlock_bh(&cmd->dataout_timeout_lock);
1229 	iscsit_cause_connection_reinstatement(conn, 0);
1230 	iscsit_dec_conn_usage_count(conn);
1231 }
1232 
1233 void iscsit_mod_dataout_timer(struct iscsi_cmd *cmd)
1234 {
1235 	struct iscsi_conn *conn = cmd->conn;
1236 	struct iscsi_session *sess = conn->sess;
1237 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1238 
1239 	spin_lock_bh(&cmd->dataout_timeout_lock);
1240 	if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
1241 		spin_unlock_bh(&cmd->dataout_timeout_lock);
1242 		return;
1243 	}
1244 
1245 	mod_timer(&cmd->dataout_timer,
1246 		(get_jiffies_64() + na->dataout_timeout * HZ));
1247 	pr_debug("Updated DataOUT timer for ITT: 0x%08x",
1248 			cmd->init_task_tag);
1249 	spin_unlock_bh(&cmd->dataout_timeout_lock);
1250 }
1251 
1252 /*
1253  *	Called with cmd->dataout_timeout_lock held.
1254  */
1255 void iscsit_start_dataout_timer(
1256 	struct iscsi_cmd *cmd,
1257 	struct iscsi_conn *conn)
1258 {
1259 	struct iscsi_session *sess = conn->sess;
1260 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1261 
1262 	if (cmd->dataout_timer_flags & ISCSI_TF_RUNNING)
1263 		return;
1264 
1265 	pr_debug("Starting DataOUT timer for ITT: 0x%08x on"
1266 		" CID: %hu.\n", cmd->init_task_tag, conn->cid);
1267 
1268 	init_timer(&cmd->dataout_timer);
1269 	cmd->dataout_timer.expires = (get_jiffies_64() + na->dataout_timeout * HZ);
1270 	cmd->dataout_timer.data = (unsigned long)cmd;
1271 	cmd->dataout_timer.function = iscsit_handle_dataout_timeout;
1272 	cmd->dataout_timer_flags &= ~ISCSI_TF_STOP;
1273 	cmd->dataout_timer_flags |= ISCSI_TF_RUNNING;
1274 	add_timer(&cmd->dataout_timer);
1275 }
1276 
1277 void iscsit_stop_dataout_timer(struct iscsi_cmd *cmd)
1278 {
1279 	spin_lock_bh(&cmd->dataout_timeout_lock);
1280 	if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
1281 		spin_unlock_bh(&cmd->dataout_timeout_lock);
1282 		return;
1283 	}
1284 	cmd->dataout_timer_flags |= ISCSI_TF_STOP;
1285 	spin_unlock_bh(&cmd->dataout_timeout_lock);
1286 
1287 	del_timer_sync(&cmd->dataout_timer);
1288 
1289 	spin_lock_bh(&cmd->dataout_timeout_lock);
1290 	cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
1291 	pr_debug("Stopped DataOUT Timer for ITT: 0x%08x\n",
1292 			cmd->init_task_tag);
1293 	spin_unlock_bh(&cmd->dataout_timeout_lock);
1294 }
1295 EXPORT_SYMBOL(iscsit_stop_dataout_timer);
1296