1 /*******************************************************************************
2  * This file contains the iSCSI Target specific Task Management functions.
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 <asm/unaligned.h>
22 #include <scsi/scsi_device.h>
23 #include <scsi/iscsi_proto.h>
24 #include <target/target_core_base.h>
25 #include <target/target_core_fabric.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_erl0.h"
32 #include "iscsi_target_erl1.h"
33 #include "iscsi_target_erl2.h"
34 #include "iscsi_target_tmr.h"
35 #include "iscsi_target_tpg.h"
36 #include "iscsi_target_util.h"
37 #include "iscsi_target.h"
38 
39 u8 iscsit_tmr_abort_task(
40 	struct iscsi_cmd *cmd,
41 	unsigned char *buf)
42 {
43 	struct iscsi_cmd *ref_cmd;
44 	struct iscsi_conn *conn = cmd->conn;
45 	struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
46 	struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
47 	struct iscsi_tm *hdr = (struct iscsi_tm *) buf;
48 
49 	ref_cmd = iscsit_find_cmd_from_itt(conn, hdr->rtt);
50 	if (!ref_cmd) {
51 		pr_err("Unable to locate RefTaskTag: 0x%08x on CID:"
52 			" %hu.\n", hdr->rtt, conn->cid);
53 		return ((hdr->refcmdsn >= conn->sess->exp_cmd_sn) &&
54 			(hdr->refcmdsn <= conn->sess->max_cmd_sn)) ?
55 			ISCSI_TMF_RSP_COMPLETE : ISCSI_TMF_RSP_NO_TASK;
56 	}
57 	if (ref_cmd->cmd_sn != hdr->refcmdsn) {
58 		pr_err("RefCmdSN 0x%08x does not equal"
59 			" task's CmdSN 0x%08x. Rejecting ABORT_TASK.\n",
60 			hdr->refcmdsn, ref_cmd->cmd_sn);
61 		return ISCSI_TMF_RSP_REJECTED;
62 	}
63 
64 	se_tmr->ref_task_tag		= (__force u32)hdr->rtt;
65 	tmr_req->ref_cmd		= ref_cmd;
66 	tmr_req->ref_cmd_sn		= hdr->refcmdsn;
67 	tmr_req->exp_data_sn		= hdr->exp_datasn;
68 
69 	return ISCSI_TMF_RSP_COMPLETE;
70 }
71 
72 /*
73  *	Called from iscsit_handle_task_mgt_cmd().
74  */
75 int iscsit_tmr_task_warm_reset(
76 	struct iscsi_conn *conn,
77 	struct iscsi_tmr_req *tmr_req,
78 	unsigned char *buf)
79 {
80 	struct iscsi_session *sess = conn->sess;
81 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
82 
83 	if (!na->tmr_warm_reset) {
84 		pr_err("TMR Opcode TARGET_WARM_RESET authorization"
85 			" failed for Initiator Node: %s\n",
86 			sess->se_sess->se_node_acl->initiatorname);
87 		 return -1;
88 	}
89 	/*
90 	 * Do the real work in transport_generic_do_tmr().
91 	 */
92 	return 0;
93 }
94 
95 int iscsit_tmr_task_cold_reset(
96 	struct iscsi_conn *conn,
97 	struct iscsi_tmr_req *tmr_req,
98 	unsigned char *buf)
99 {
100 	struct iscsi_session *sess = conn->sess;
101 	struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
102 
103 	if (!na->tmr_cold_reset) {
104 		pr_err("TMR Opcode TARGET_COLD_RESET authorization"
105 			" failed for Initiator Node: %s\n",
106 			sess->se_sess->se_node_acl->initiatorname);
107 		return -1;
108 	}
109 	/*
110 	 * Do the real work in transport_generic_do_tmr().
111 	 */
112 	return 0;
113 }
114 
115 u8 iscsit_tmr_task_reassign(
116 	struct iscsi_cmd *cmd,
117 	unsigned char *buf)
118 {
119 	struct iscsi_cmd *ref_cmd = NULL;
120 	struct iscsi_conn *conn = cmd->conn;
121 	struct iscsi_conn_recovery *cr = NULL;
122 	struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
123 	struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
124 	struct iscsi_tm *hdr = (struct iscsi_tm *) buf;
125 	int ret, ref_lun;
126 
127 	pr_debug("Got TASK_REASSIGN TMR ITT: 0x%08x,"
128 		" RefTaskTag: 0x%08x, ExpDataSN: 0x%08x, CID: %hu\n",
129 		hdr->itt, hdr->rtt, hdr->exp_datasn, conn->cid);
130 
131 	if (conn->sess->sess_ops->ErrorRecoveryLevel != 2) {
132 		pr_err("TMR TASK_REASSIGN not supported in ERL<2,"
133 				" ignoring request.\n");
134 		return ISCSI_TMF_RSP_NOT_SUPPORTED;
135 	}
136 
137 	ret = iscsit_find_cmd_for_recovery(conn->sess, &ref_cmd, &cr, hdr->rtt);
138 	if (ret == -2) {
139 		pr_err("Command ITT: 0x%08x is still alligent to CID:"
140 			" %hu\n", ref_cmd->init_task_tag, cr->cid);
141 		return ISCSI_TMF_RSP_TASK_ALLEGIANT;
142 	} else if (ret == -1) {
143 		pr_err("Unable to locate RefTaskTag: 0x%08x in"
144 			" connection recovery command list.\n", hdr->rtt);
145 		return ISCSI_TMF_RSP_NO_TASK;
146 	}
147 	/*
148 	 * Temporary check to prevent connection recovery for
149 	 * connections with a differing Max*DataSegmentLength.
150 	 */
151 	if (cr->maxrecvdatasegmentlength !=
152 	    conn->conn_ops->MaxRecvDataSegmentLength) {
153 		pr_err("Unable to perform connection recovery for"
154 			" differing MaxRecvDataSegmentLength, rejecting"
155 			" TMR TASK_REASSIGN.\n");
156 		return ISCSI_TMF_RSP_REJECTED;
157 	}
158 	if (cr->maxxmitdatasegmentlength !=
159 	    conn->conn_ops->MaxXmitDataSegmentLength) {
160 		pr_err("Unable to perform connection recovery for"
161 			" differing MaxXmitDataSegmentLength, rejecting"
162 			" TMR TASK_REASSIGN.\n");
163 		return ISCSI_TMF_RSP_REJECTED;
164 	}
165 
166 	ref_lun = scsilun_to_int(&hdr->lun);
167 	if (ref_lun != ref_cmd->se_cmd.orig_fe_lun) {
168 		pr_err("Unable to perform connection recovery for"
169 			" differing ref_lun: %d ref_cmd orig_fe_lun: %u\n",
170 			ref_lun, ref_cmd->se_cmd.orig_fe_lun);
171 		return ISCSI_TMF_RSP_REJECTED;
172 	}
173 
174 	se_tmr->ref_task_tag		= (__force u32)hdr->rtt;
175 	tmr_req->ref_cmd		= ref_cmd;
176 	tmr_req->ref_cmd_sn		= hdr->refcmdsn;
177 	tmr_req->exp_data_sn		= hdr->exp_datasn;
178 	tmr_req->conn_recovery		= cr;
179 	tmr_req->task_reassign		= 1;
180 	/*
181 	 * Command can now be reassigned to a new connection.
182 	 * The task management response must be sent before the
183 	 * reassignment actually happens.  See iscsi_tmr_post_handler().
184 	 */
185 	return ISCSI_TMF_RSP_COMPLETE;
186 }
187 
188 static void iscsit_task_reassign_remove_cmd(
189 	struct iscsi_cmd *cmd,
190 	struct iscsi_conn_recovery *cr,
191 	struct iscsi_session *sess)
192 {
193 	int ret;
194 
195 	spin_lock(&cr->conn_recovery_cmd_lock);
196 	ret = iscsit_remove_cmd_from_connection_recovery(cmd, sess);
197 	spin_unlock(&cr->conn_recovery_cmd_lock);
198 	if (!ret) {
199 		pr_debug("iSCSI connection recovery successful for CID:"
200 			" %hu on SID: %u\n", cr->cid, sess->sid);
201 		iscsit_remove_active_connection_recovery_entry(cr, sess);
202 	}
203 }
204 
205 static int iscsit_task_reassign_complete_nop_out(
206 	struct iscsi_tmr_req *tmr_req,
207 	struct iscsi_conn *conn)
208 {
209 	struct iscsi_cmd *cmd = tmr_req->ref_cmd;
210 	struct iscsi_conn_recovery *cr;
211 
212 	if (!cmd->cr) {
213 		pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
214 			" is NULL!\n", cmd->init_task_tag);
215 		return -1;
216 	}
217 	cr = cmd->cr;
218 
219 	/*
220 	 * Reset the StatSN so a new one for this commands new connection
221 	 * will be assigned.
222 	 * Reset the ExpStatSN as well so we may receive Status SNACKs.
223 	 */
224 	cmd->stat_sn = cmd->exp_stat_sn = 0;
225 
226 	iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
227 
228 	spin_lock_bh(&conn->cmd_lock);
229 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
230 	spin_unlock_bh(&conn->cmd_lock);
231 
232 	cmd->i_state = ISTATE_SEND_NOPIN;
233 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
234 	return 0;
235 }
236 
237 static int iscsit_task_reassign_complete_write(
238 	struct iscsi_cmd *cmd,
239 	struct iscsi_tmr_req *tmr_req)
240 {
241 	int no_build_r2ts = 0;
242 	u32 length = 0, offset = 0;
243 	struct iscsi_conn *conn = cmd->conn;
244 	struct se_cmd *se_cmd = &cmd->se_cmd;
245 	/*
246 	 * The Initiator must not send a R2T SNACK with a Begrun less than
247 	 * the TMR TASK_REASSIGN's ExpDataSN.
248 	 */
249 	if (!tmr_req->exp_data_sn) {
250 		cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
251 		cmd->acked_data_sn = 0;
252 	} else {
253 		cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
254 		cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
255 	}
256 
257 	/*
258 	 * The TMR TASK_REASSIGN's ExpDataSN contains the next R2TSN the
259 	 * Initiator is expecting.  The Target controls all WRITE operations
260 	 * so if we have received all DataOUT we can safety ignore Initiator.
261 	 */
262 	if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
263 		if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
264 			pr_debug("WRITE ITT: 0x%08x: t_state: %d"
265 				" never sent to transport\n",
266 				cmd->init_task_tag, cmd->se_cmd.t_state);
267 			target_execute_cmd(se_cmd);
268 			return 0;
269 		}
270 
271 		cmd->i_state = ISTATE_SEND_STATUS;
272 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
273 		return 0;
274 	}
275 
276 	/*
277 	 * Special case to deal with DataSequenceInOrder=No and Non-Immeidate
278 	 * Unsolicited DataOut.
279 	 */
280 	if (cmd->unsolicited_data) {
281 		cmd->unsolicited_data = 0;
282 
283 		offset = cmd->next_burst_len = cmd->write_data_done;
284 
285 		if ((conn->sess->sess_ops->FirstBurstLength - offset) >=
286 		     cmd->se_cmd.data_length) {
287 			no_build_r2ts = 1;
288 			length = (cmd->se_cmd.data_length - offset);
289 		} else
290 			length = (conn->sess->sess_ops->FirstBurstLength - offset);
291 
292 		spin_lock_bh(&cmd->r2t_lock);
293 		if (iscsit_add_r2t_to_list(cmd, offset, length, 0, 0) < 0) {
294 			spin_unlock_bh(&cmd->r2t_lock);
295 			return -1;
296 		}
297 		cmd->outstanding_r2ts++;
298 		spin_unlock_bh(&cmd->r2t_lock);
299 
300 		if (no_build_r2ts)
301 			return 0;
302 	}
303 	/*
304 	 * iscsit_build_r2ts_for_cmd() can handle the rest from here.
305 	 */
306 	return iscsit_build_r2ts_for_cmd(cmd, conn, true);
307 }
308 
309 static int iscsit_task_reassign_complete_read(
310 	struct iscsi_cmd *cmd,
311 	struct iscsi_tmr_req *tmr_req)
312 {
313 	struct iscsi_conn *conn = cmd->conn;
314 	struct iscsi_datain_req *dr;
315 	struct se_cmd *se_cmd = &cmd->se_cmd;
316 	/*
317 	 * The Initiator must not send a Data SNACK with a BegRun less than
318 	 * the TMR TASK_REASSIGN's ExpDataSN.
319 	 */
320 	if (!tmr_req->exp_data_sn) {
321 		cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
322 		cmd->acked_data_sn = 0;
323 	} else {
324 		cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
325 		cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
326 	}
327 
328 	if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
329 		pr_debug("READ ITT: 0x%08x: t_state: %d never sent to"
330 			" transport\n", cmd->init_task_tag,
331 			cmd->se_cmd.t_state);
332 		transport_handle_cdb_direct(se_cmd);
333 		return 0;
334 	}
335 
336 	if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
337 		pr_err("READ ITT: 0x%08x: t_state: %d, never returned"
338 			" from transport\n", cmd->init_task_tag,
339 			cmd->se_cmd.t_state);
340 		return -1;
341 	}
342 
343 	dr = iscsit_allocate_datain_req();
344 	if (!dr)
345 		return -1;
346 	/*
347 	 * The TMR TASK_REASSIGN's ExpDataSN contains the next DataSN the
348 	 * Initiator is expecting.
349 	 */
350 	dr->data_sn = dr->begrun = tmr_req->exp_data_sn;
351 	dr->runlength = 0;
352 	dr->generate_recovery_values = 1;
353 	dr->recovery = DATAIN_CONNECTION_RECOVERY;
354 
355 	iscsit_attach_datain_req(cmd, dr);
356 
357 	cmd->i_state = ISTATE_SEND_DATAIN;
358 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
359 	return 0;
360 }
361 
362 static int iscsit_task_reassign_complete_none(
363 	struct iscsi_cmd *cmd,
364 	struct iscsi_tmr_req *tmr_req)
365 {
366 	struct iscsi_conn *conn = cmd->conn;
367 
368 	cmd->i_state = ISTATE_SEND_STATUS;
369 	iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
370 	return 0;
371 }
372 
373 static int iscsit_task_reassign_complete_scsi_cmnd(
374 	struct iscsi_tmr_req *tmr_req,
375 	struct iscsi_conn *conn)
376 {
377 	struct iscsi_cmd *cmd = tmr_req->ref_cmd;
378 	struct iscsi_conn_recovery *cr;
379 
380 	if (!cmd->cr) {
381 		pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
382 			" is NULL!\n", cmd->init_task_tag);
383 		return -1;
384 	}
385 	cr = cmd->cr;
386 
387 	/*
388 	 * Reset the StatSN so a new one for this commands new connection
389 	 * will be assigned.
390 	 * Reset the ExpStatSN as well so we may receive Status SNACKs.
391 	 */
392 	cmd->stat_sn = cmd->exp_stat_sn = 0;
393 
394 	iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
395 
396 	spin_lock_bh(&conn->cmd_lock);
397 	list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
398 	spin_unlock_bh(&conn->cmd_lock);
399 
400 	if (cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
401 		cmd->i_state = ISTATE_SEND_STATUS;
402 		iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
403 		return 0;
404 	}
405 
406 	switch (cmd->data_direction) {
407 	case DMA_TO_DEVICE:
408 		return iscsit_task_reassign_complete_write(cmd, tmr_req);
409 	case DMA_FROM_DEVICE:
410 		return iscsit_task_reassign_complete_read(cmd, tmr_req);
411 	case DMA_NONE:
412 		return iscsit_task_reassign_complete_none(cmd, tmr_req);
413 	default:
414 		pr_err("Unknown cmd->data_direction: 0x%02x\n",
415 				cmd->data_direction);
416 		return -1;
417 	}
418 
419 	return 0;
420 }
421 
422 static int iscsit_task_reassign_complete(
423 	struct iscsi_tmr_req *tmr_req,
424 	struct iscsi_conn *conn)
425 {
426 	struct iscsi_cmd *cmd;
427 	int ret = 0;
428 
429 	if (!tmr_req->ref_cmd) {
430 		pr_err("TMR Request is missing a RefCmd struct iscsi_cmd.\n");
431 		return -1;
432 	}
433 	cmd = tmr_req->ref_cmd;
434 
435 	cmd->conn = conn;
436 
437 	switch (cmd->iscsi_opcode) {
438 	case ISCSI_OP_NOOP_OUT:
439 		ret = iscsit_task_reassign_complete_nop_out(tmr_req, conn);
440 		break;
441 	case ISCSI_OP_SCSI_CMD:
442 		ret = iscsit_task_reassign_complete_scsi_cmnd(tmr_req, conn);
443 		break;
444 	default:
445 		 pr_err("Illegal iSCSI Opcode 0x%02x during"
446 			" command realligence\n", cmd->iscsi_opcode);
447 		return -1;
448 	}
449 
450 	if (ret != 0)
451 		return ret;
452 
453 	pr_debug("Completed connection realligence for Opcode: 0x%02x,"
454 		" ITT: 0x%08x to CID: %hu.\n", cmd->iscsi_opcode,
455 			cmd->init_task_tag, conn->cid);
456 
457 	return 0;
458 }
459 
460 /*
461  *	Handles special after-the-fact actions related to TMRs.
462  *	Right now the only one that its really needed for is
463  *	connection recovery releated TASK_REASSIGN.
464  */
465 int iscsit_tmr_post_handler(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
466 {
467 	struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
468 	struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
469 
470 	if (tmr_req->task_reassign &&
471 	   (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
472 		return iscsit_task_reassign_complete(tmr_req, conn);
473 
474 	return 0;
475 }
476 
477 /*
478  *	Nothing to do here, but leave it for good measure. :-)
479  */
480 static int iscsit_task_reassign_prepare_read(
481 	struct iscsi_tmr_req *tmr_req,
482 	struct iscsi_conn *conn)
483 {
484 	return 0;
485 }
486 
487 static void iscsit_task_reassign_prepare_unsolicited_dataout(
488 	struct iscsi_cmd *cmd,
489 	struct iscsi_conn *conn)
490 {
491 	int i, j;
492 	struct iscsi_pdu *pdu = NULL;
493 	struct iscsi_seq *seq = NULL;
494 
495 	if (conn->sess->sess_ops->DataSequenceInOrder) {
496 		cmd->data_sn = 0;
497 
498 		if (cmd->immediate_data)
499 			cmd->r2t_offset += (cmd->first_burst_len -
500 				cmd->seq_start_offset);
501 
502 		if (conn->sess->sess_ops->DataPDUInOrder) {
503 			cmd->write_data_done -= (cmd->immediate_data) ?
504 						(cmd->first_burst_len -
505 						 cmd->seq_start_offset) :
506 						 cmd->first_burst_len;
507 			cmd->first_burst_len = 0;
508 			return;
509 		}
510 
511 		for (i = 0; i < cmd->pdu_count; i++) {
512 			pdu = &cmd->pdu_list[i];
513 
514 			if (pdu->status != ISCSI_PDU_RECEIVED_OK)
515 				continue;
516 
517 			if ((pdu->offset >= cmd->seq_start_offset) &&
518 			   ((pdu->offset + pdu->length) <=
519 			     cmd->seq_end_offset)) {
520 				cmd->first_burst_len -= pdu->length;
521 				cmd->write_data_done -= pdu->length;
522 				pdu->status = ISCSI_PDU_NOT_RECEIVED;
523 			}
524 		}
525 	} else {
526 		for (i = 0; i < cmd->seq_count; i++) {
527 			seq = &cmd->seq_list[i];
528 
529 			if (seq->type != SEQTYPE_UNSOLICITED)
530 				continue;
531 
532 			cmd->write_data_done -=
533 					(seq->offset - seq->orig_offset);
534 			cmd->first_burst_len = 0;
535 			seq->data_sn = 0;
536 			seq->offset = seq->orig_offset;
537 			seq->next_burst_len = 0;
538 			seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
539 
540 			if (conn->sess->sess_ops->DataPDUInOrder)
541 				continue;
542 
543 			for (j = 0; j < seq->pdu_count; j++) {
544 				pdu = &cmd->pdu_list[j+seq->pdu_start];
545 
546 				if (pdu->status != ISCSI_PDU_RECEIVED_OK)
547 					continue;
548 
549 				pdu->status = ISCSI_PDU_NOT_RECEIVED;
550 			}
551 		}
552 	}
553 }
554 
555 static int iscsit_task_reassign_prepare_write(
556 	struct iscsi_tmr_req *tmr_req,
557 	struct iscsi_conn *conn)
558 {
559 	struct iscsi_cmd *cmd = tmr_req->ref_cmd;
560 	struct iscsi_pdu *pdu = NULL;
561 	struct iscsi_r2t *r2t = NULL, *r2t_tmp;
562 	int first_incomplete_r2t = 1, i = 0;
563 
564 	/*
565 	 * The command was in the process of receiving Unsolicited DataOUT when
566 	 * the connection failed.
567 	 */
568 	if (cmd->unsolicited_data)
569 		iscsit_task_reassign_prepare_unsolicited_dataout(cmd, conn);
570 
571 	/*
572 	 * The Initiator is requesting R2Ts starting from zero,  skip
573 	 * checking acknowledged R2Ts and start checking struct iscsi_r2ts
574 	 * greater than zero.
575 	 */
576 	if (!tmr_req->exp_data_sn)
577 		goto drop_unacknowledged_r2ts;
578 
579 	/*
580 	 * We now check that the PDUs in DataOUT sequences below
581 	 * the TMR TASK_REASSIGN ExpDataSN (R2TSN the Initiator is
582 	 * expecting next) have all the DataOUT they require to complete
583 	 * the DataOUT sequence.  First scan from R2TSN 0 to TMR
584 	 * TASK_REASSIGN ExpDataSN-1.
585 	 *
586 	 * If we have not received all DataOUT in question,  we must
587 	 * make sure to make the appropriate changes to values in
588 	 * struct iscsi_cmd (and elsewhere depending on session parameters)
589 	 * so iscsit_build_r2ts_for_cmd() in iscsit_task_reassign_complete_write()
590 	 * will resend a new R2T for the DataOUT sequences in question.
591 	 */
592 	spin_lock_bh(&cmd->r2t_lock);
593 	if (list_empty(&cmd->cmd_r2t_list)) {
594 		spin_unlock_bh(&cmd->r2t_lock);
595 		return -1;
596 	}
597 
598 	list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
599 
600 		if (r2t->r2t_sn >= tmr_req->exp_data_sn)
601 			continue;
602 		/*
603 		 * Safely ignore Recovery R2Ts and R2Ts that have completed
604 		 * DataOUT sequences.
605 		 */
606 		if (r2t->seq_complete)
607 			continue;
608 
609 		if (r2t->recovery_r2t)
610 			continue;
611 
612 		/*
613 		 *                 DataSequenceInOrder=Yes:
614 		 *
615 		 * Taking into account the iSCSI implementation requirement of
616 		 * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
617 		 * DataSequenceInOrder=Yes, we must take into consideration
618 		 * the following:
619 		 *
620 		 *                  DataSequenceInOrder=No:
621 		 *
622 		 * Taking into account that the Initiator controls the (possibly
623 		 * random) PDU Order in (possibly random) Sequence Order of
624 		 * DataOUT the target requests with R2Ts,  we must take into
625 		 * consideration the following:
626 		 *
627 		 *      DataPDUInOrder=Yes for DataSequenceInOrder=[Yes,No]:
628 		 *
629 		 * While processing non-complete R2T DataOUT sequence requests
630 		 * the Target will re-request only the total sequence length
631 		 * minus current received offset.  This is because we must
632 		 * assume the initiator will continue sending DataOUT from the
633 		 * last PDU before the connection failed.
634 		 *
635 		 *      DataPDUInOrder=No for DataSequenceInOrder=[Yes,No]:
636 		 *
637 		 * While processing non-complete R2T DataOUT sequence requests
638 		 * the Target will re-request the entire DataOUT sequence if
639 		 * any single PDU is missing from the sequence.  This is because
640 		 * we have no logical method to determine the next PDU offset,
641 		 * and we must assume the Initiator will be sending any random
642 		 * PDU offset in the current sequence after TASK_REASSIGN
643 		 * has completed.
644 		 */
645 		if (conn->sess->sess_ops->DataSequenceInOrder) {
646 			if (!first_incomplete_r2t) {
647 				cmd->r2t_offset -= r2t->xfer_len;
648 				goto next;
649 			}
650 
651 			if (conn->sess->sess_ops->DataPDUInOrder) {
652 				cmd->data_sn = 0;
653 				cmd->r2t_offset -= (r2t->xfer_len -
654 					cmd->next_burst_len);
655 				first_incomplete_r2t = 0;
656 				goto next;
657 			}
658 
659 			cmd->data_sn = 0;
660 			cmd->r2t_offset -= r2t->xfer_len;
661 
662 			for (i = 0; i < cmd->pdu_count; i++) {
663 				pdu = &cmd->pdu_list[i];
664 
665 				if (pdu->status != ISCSI_PDU_RECEIVED_OK)
666 					continue;
667 
668 				if ((pdu->offset >= r2t->offset) &&
669 				    (pdu->offset < (r2t->offset +
670 						r2t->xfer_len))) {
671 					cmd->next_burst_len -= pdu->length;
672 					cmd->write_data_done -= pdu->length;
673 					pdu->status = ISCSI_PDU_NOT_RECEIVED;
674 				}
675 			}
676 
677 			first_incomplete_r2t = 0;
678 		} else {
679 			struct iscsi_seq *seq;
680 
681 			seq = iscsit_get_seq_holder(cmd, r2t->offset,
682 					r2t->xfer_len);
683 			if (!seq) {
684 				spin_unlock_bh(&cmd->r2t_lock);
685 				return -1;
686 			}
687 
688 			cmd->write_data_done -=
689 					(seq->offset - seq->orig_offset);
690 			seq->data_sn = 0;
691 			seq->offset = seq->orig_offset;
692 			seq->next_burst_len = 0;
693 			seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
694 
695 			cmd->seq_send_order--;
696 
697 			if (conn->sess->sess_ops->DataPDUInOrder)
698 				goto next;
699 
700 			for (i = 0; i < seq->pdu_count; i++) {
701 				pdu = &cmd->pdu_list[i+seq->pdu_start];
702 
703 				if (pdu->status != ISCSI_PDU_RECEIVED_OK)
704 					continue;
705 
706 				pdu->status = ISCSI_PDU_NOT_RECEIVED;
707 			}
708 		}
709 
710 next:
711 		cmd->outstanding_r2ts--;
712 	}
713 	spin_unlock_bh(&cmd->r2t_lock);
714 
715 	/*
716 	 * We now drop all unacknowledged R2Ts, ie: ExpDataSN from TMR
717 	 * TASK_REASSIGN to the last R2T in the list..  We are also careful
718 	 * to check that the Initiator is not requesting R2Ts for DataOUT
719 	 * sequences it has already completed.
720 	 *
721 	 * Free each R2T in question and adjust values in struct iscsi_cmd
722 	 * accordingly so iscsit_build_r2ts_for_cmd() do the rest of
723 	 * the work after the TMR TASK_REASSIGN Response is sent.
724 	 */
725 drop_unacknowledged_r2ts:
726 
727 	cmd->cmd_flags &= ~ICF_SENT_LAST_R2T;
728 	cmd->r2t_sn = tmr_req->exp_data_sn;
729 
730 	spin_lock_bh(&cmd->r2t_lock);
731 	list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list) {
732 		/*
733 		 * Skip up to the R2T Sequence number provided by the
734 		 * iSCSI TASK_REASSIGN TMR
735 		 */
736 		if (r2t->r2t_sn < tmr_req->exp_data_sn)
737 			continue;
738 
739 		if (r2t->seq_complete) {
740 			pr_err("Initiator is requesting R2Ts from"
741 				" R2TSN: 0x%08x, but R2TSN: 0x%08x, Offset: %u,"
742 				" Length: %u is already complete."
743 				"   BAD INITIATOR ERL=2 IMPLEMENTATION!\n",
744 				tmr_req->exp_data_sn, r2t->r2t_sn,
745 				r2t->offset, r2t->xfer_len);
746 			spin_unlock_bh(&cmd->r2t_lock);
747 			return -1;
748 		}
749 
750 		if (r2t->recovery_r2t) {
751 			iscsit_free_r2t(r2t, cmd);
752 			continue;
753 		}
754 
755 		/*		   DataSequenceInOrder=Yes:
756 		 *
757 		 * Taking into account the iSCSI implementation requirement of
758 		 * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
759 		 * DataSequenceInOrder=Yes, it's safe to subtract the R2Ts
760 		 * entire transfer length from the commands R2T offset marker.
761 		 *
762 		 *		   DataSequenceInOrder=No:
763 		 *
764 		 * We subtract the difference from struct iscsi_seq between the
765 		 * current offset and original offset from cmd->write_data_done
766 		 * for account for DataOUT PDUs already received.  Then reset
767 		 * the current offset to the original and zero out the current
768 		 * burst length,  to make sure we re-request the entire DataOUT
769 		 * sequence.
770 		 */
771 		if (conn->sess->sess_ops->DataSequenceInOrder)
772 			cmd->r2t_offset -= r2t->xfer_len;
773 		else
774 			cmd->seq_send_order--;
775 
776 		cmd->outstanding_r2ts--;
777 		iscsit_free_r2t(r2t, cmd);
778 	}
779 	spin_unlock_bh(&cmd->r2t_lock);
780 
781 	return 0;
782 }
783 
784 /*
785  *	Performs sanity checks TMR TASK_REASSIGN's ExpDataSN for
786  *	a given struct iscsi_cmd.
787  */
788 int iscsit_check_task_reassign_expdatasn(
789 	struct iscsi_tmr_req *tmr_req,
790 	struct iscsi_conn *conn)
791 {
792 	struct iscsi_cmd *ref_cmd = tmr_req->ref_cmd;
793 
794 	if (ref_cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD)
795 		return 0;
796 
797 	if (ref_cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION)
798 		return 0;
799 
800 	if (ref_cmd->data_direction == DMA_NONE)
801 		return 0;
802 
803 	/*
804 	 * For READs the TMR TASK_REASSIGNs ExpDataSN contains the next DataSN
805 	 * of DataIN the Initiator is expecting.
806 	 *
807 	 * Also check that the Initiator is not re-requesting DataIN that has
808 	 * already been acknowledged with a DataAck SNACK.
809 	 */
810 	if (ref_cmd->data_direction == DMA_FROM_DEVICE) {
811 		if (tmr_req->exp_data_sn > ref_cmd->data_sn) {
812 			pr_err("Received ExpDataSN: 0x%08x for READ"
813 				" in TMR TASK_REASSIGN greater than command's"
814 				" DataSN: 0x%08x.\n", tmr_req->exp_data_sn,
815 				ref_cmd->data_sn);
816 			return -1;
817 		}
818 		if ((ref_cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
819 		    (tmr_req->exp_data_sn <= ref_cmd->acked_data_sn)) {
820 			pr_err("Received ExpDataSN: 0x%08x for READ"
821 				" in TMR TASK_REASSIGN for previously"
822 				" acknowledged DataIN: 0x%08x,"
823 				" protocol error\n", tmr_req->exp_data_sn,
824 				ref_cmd->acked_data_sn);
825 			return -1;
826 		}
827 		return iscsit_task_reassign_prepare_read(tmr_req, conn);
828 	}
829 
830 	/*
831 	 * For WRITEs the TMR TASK_REASSIGNs ExpDataSN contains the next R2TSN
832 	 * for R2Ts the Initiator is expecting.
833 	 *
834 	 * Do the magic in iscsit_task_reassign_prepare_write().
835 	 */
836 	if (ref_cmd->data_direction == DMA_TO_DEVICE) {
837 		if (tmr_req->exp_data_sn > ref_cmd->r2t_sn) {
838 			pr_err("Received ExpDataSN: 0x%08x for WRITE"
839 				" in TMR TASK_REASSIGN greater than command's"
840 				" R2TSN: 0x%08x.\n", tmr_req->exp_data_sn,
841 					ref_cmd->r2t_sn);
842 			return -1;
843 		}
844 		return iscsit_task_reassign_prepare_write(tmr_req, conn);
845 	}
846 
847 	pr_err("Unknown iSCSI data_direction: 0x%02x\n",
848 			ref_cmd->data_direction);
849 
850 	return -1;
851 }
852