WIP FPC-III support
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_tmr.c
blob7d618db80c515e5dcc785c8afa805cf86fbae4c3
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * This file contains the iSCSI Target specific Task Management functions.
5 * (c) Copyright 2007-2013 Datera, Inc.
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9 ******************************************************************************/
11 #include <asm/unaligned.h>
12 #include <scsi/scsi_proto.h>
13 #include <scsi/iscsi_proto.h>
14 #include <target/target_core_base.h>
15 #include <target/target_core_fabric.h>
16 #include <target/iscsi/iscsi_transport.h>
18 #include <target/iscsi/iscsi_target_core.h>
19 #include "iscsi_target_seq_pdu_list.h"
20 #include "iscsi_target_datain_values.h"
21 #include "iscsi_target_device.h"
22 #include "iscsi_target_erl0.h"
23 #include "iscsi_target_erl1.h"
24 #include "iscsi_target_erl2.h"
25 #include "iscsi_target_tmr.h"
26 #include "iscsi_target_tpg.h"
27 #include "iscsi_target_util.h"
28 #include "iscsi_target.h"
30 u8 iscsit_tmr_abort_task(
31 struct iscsi_cmd *cmd,
32 unsigned char *buf)
34 struct iscsi_cmd *ref_cmd;
35 struct iscsi_conn *conn = cmd->conn;
36 struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
37 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
38 struct iscsi_tm *hdr = (struct iscsi_tm *) buf;
40 ref_cmd = iscsit_find_cmd_from_itt(conn, hdr->rtt);
41 if (!ref_cmd) {
42 pr_err("Unable to locate RefTaskTag: 0x%08x on CID:"
43 " %hu.\n", hdr->rtt, conn->cid);
44 return (iscsi_sna_gte(be32_to_cpu(hdr->refcmdsn), conn->sess->exp_cmd_sn) &&
45 iscsi_sna_lte(be32_to_cpu(hdr->refcmdsn), (u32) atomic_read(&conn->sess->max_cmd_sn))) ?
46 ISCSI_TMF_RSP_COMPLETE : ISCSI_TMF_RSP_NO_TASK;
48 if (ref_cmd->cmd_sn != be32_to_cpu(hdr->refcmdsn)) {
49 pr_err("RefCmdSN 0x%08x does not equal"
50 " task's CmdSN 0x%08x. Rejecting ABORT_TASK.\n",
51 hdr->refcmdsn, ref_cmd->cmd_sn);
52 return ISCSI_TMF_RSP_REJECTED;
55 se_tmr->ref_task_tag = (__force u32)hdr->rtt;
56 tmr_req->ref_cmd = ref_cmd;
57 tmr_req->exp_data_sn = be32_to_cpu(hdr->exp_datasn);
59 return ISCSI_TMF_RSP_COMPLETE;
63 * Called from iscsit_handle_task_mgt_cmd().
65 int iscsit_tmr_task_warm_reset(
66 struct iscsi_conn *conn,
67 struct iscsi_tmr_req *tmr_req,
68 unsigned char *buf)
70 struct iscsi_session *sess = conn->sess;
71 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
73 if (!na->tmr_warm_reset) {
74 pr_err("TMR Opcode TARGET_WARM_RESET authorization"
75 " failed for Initiator Node: %s\n",
76 sess->se_sess->se_node_acl->initiatorname);
77 return -1;
80 * Do the real work in transport_generic_do_tmr().
82 return 0;
85 int iscsit_tmr_task_cold_reset(
86 struct iscsi_conn *conn,
87 struct iscsi_tmr_req *tmr_req,
88 unsigned char *buf)
90 struct iscsi_session *sess = conn->sess;
91 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
93 if (!na->tmr_cold_reset) {
94 pr_err("TMR Opcode TARGET_COLD_RESET authorization"
95 " failed for Initiator Node: %s\n",
96 sess->se_sess->se_node_acl->initiatorname);
97 return -1;
100 * Do the real work in transport_generic_do_tmr().
102 return 0;
105 u8 iscsit_tmr_task_reassign(
106 struct iscsi_cmd *cmd,
107 unsigned char *buf)
109 struct iscsi_cmd *ref_cmd = NULL;
110 struct iscsi_conn *conn = cmd->conn;
111 struct iscsi_conn_recovery *cr = NULL;
112 struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
113 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
114 struct iscsi_tm *hdr = (struct iscsi_tm *) buf;
115 u64 ret, ref_lun;
117 pr_debug("Got TASK_REASSIGN TMR ITT: 0x%08x,"
118 " RefTaskTag: 0x%08x, ExpDataSN: 0x%08x, CID: %hu\n",
119 hdr->itt, hdr->rtt, hdr->exp_datasn, conn->cid);
121 if (conn->sess->sess_ops->ErrorRecoveryLevel != 2) {
122 pr_err("TMR TASK_REASSIGN not supported in ERL<2,"
123 " ignoring request.\n");
124 return ISCSI_TMF_RSP_NOT_SUPPORTED;
127 ret = iscsit_find_cmd_for_recovery(conn->sess, &ref_cmd, &cr, hdr->rtt);
128 if (ret == -2) {
129 pr_err("Command ITT: 0x%08x is still alligent to CID:"
130 " %hu\n", ref_cmd->init_task_tag, cr->cid);
131 return ISCSI_TMF_RSP_TASK_ALLEGIANT;
132 } else if (ret == -1) {
133 pr_err("Unable to locate RefTaskTag: 0x%08x in"
134 " connection recovery command list.\n", hdr->rtt);
135 return ISCSI_TMF_RSP_NO_TASK;
138 * Temporary check to prevent connection recovery for
139 * connections with a differing Max*DataSegmentLength.
141 if (cr->maxrecvdatasegmentlength !=
142 conn->conn_ops->MaxRecvDataSegmentLength) {
143 pr_err("Unable to perform connection recovery for"
144 " differing MaxRecvDataSegmentLength, rejecting"
145 " TMR TASK_REASSIGN.\n");
146 return ISCSI_TMF_RSP_REJECTED;
148 if (cr->maxxmitdatasegmentlength !=
149 conn->conn_ops->MaxXmitDataSegmentLength) {
150 pr_err("Unable to perform connection recovery for"
151 " differing MaxXmitDataSegmentLength, rejecting"
152 " TMR TASK_REASSIGN.\n");
153 return ISCSI_TMF_RSP_REJECTED;
156 ref_lun = scsilun_to_int(&hdr->lun);
157 if (ref_lun != ref_cmd->se_cmd.orig_fe_lun) {
158 pr_err("Unable to perform connection recovery for"
159 " differing ref_lun: %llu ref_cmd orig_fe_lun: %llu\n",
160 ref_lun, ref_cmd->se_cmd.orig_fe_lun);
161 return ISCSI_TMF_RSP_REJECTED;
164 se_tmr->ref_task_tag = (__force u32)hdr->rtt;
165 tmr_req->ref_cmd = ref_cmd;
166 tmr_req->exp_data_sn = be32_to_cpu(hdr->exp_datasn);
167 tmr_req->conn_recovery = cr;
168 tmr_req->task_reassign = 1;
170 * Command can now be reassigned to a new connection.
171 * The task management response must be sent before the
172 * reassignment actually happens. See iscsi_tmr_post_handler().
174 return ISCSI_TMF_RSP_COMPLETE;
177 static void iscsit_task_reassign_remove_cmd(
178 struct iscsi_cmd *cmd,
179 struct iscsi_conn_recovery *cr,
180 struct iscsi_session *sess)
182 int ret;
184 spin_lock(&cr->conn_recovery_cmd_lock);
185 ret = iscsit_remove_cmd_from_connection_recovery(cmd, sess);
186 spin_unlock(&cr->conn_recovery_cmd_lock);
187 if (!ret) {
188 pr_debug("iSCSI connection recovery successful for CID:"
189 " %hu on SID: %u\n", cr->cid, sess->sid);
190 iscsit_remove_active_connection_recovery_entry(cr, sess);
194 static int iscsit_task_reassign_complete_nop_out(
195 struct iscsi_tmr_req *tmr_req,
196 struct iscsi_conn *conn)
198 struct iscsi_cmd *cmd = tmr_req->ref_cmd;
199 struct iscsi_conn_recovery *cr;
201 if (!cmd->cr) {
202 pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
203 " is NULL!\n", cmd->init_task_tag);
204 return -1;
206 cr = cmd->cr;
209 * Reset the StatSN so a new one for this commands new connection
210 * will be assigned.
211 * Reset the ExpStatSN as well so we may receive Status SNACKs.
213 cmd->stat_sn = cmd->exp_stat_sn = 0;
215 iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
217 spin_lock_bh(&conn->cmd_lock);
218 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
219 spin_unlock_bh(&conn->cmd_lock);
221 cmd->i_state = ISTATE_SEND_NOPIN;
222 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
223 return 0;
226 static int iscsit_task_reassign_complete_write(
227 struct iscsi_cmd *cmd,
228 struct iscsi_tmr_req *tmr_req)
230 int no_build_r2ts = 0;
231 u32 length = 0, offset = 0;
232 struct iscsi_conn *conn = cmd->conn;
233 struct se_cmd *se_cmd = &cmd->se_cmd;
235 * The Initiator must not send a R2T SNACK with a Begrun less than
236 * the TMR TASK_REASSIGN's ExpDataSN.
238 if (!tmr_req->exp_data_sn) {
239 cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
240 cmd->acked_data_sn = 0;
241 } else {
242 cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
243 cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
247 * The TMR TASK_REASSIGN's ExpDataSN contains the next R2TSN the
248 * Initiator is expecting. The Target controls all WRITE operations
249 * so if we have received all DataOUT we can safety ignore Initiator.
251 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
252 if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
253 pr_debug("WRITE ITT: 0x%08x: t_state: %d"
254 " never sent to transport\n",
255 cmd->init_task_tag, cmd->se_cmd.t_state);
256 target_execute_cmd(se_cmd);
257 return 0;
260 cmd->i_state = ISTATE_SEND_STATUS;
261 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
262 return 0;
266 * Special case to deal with DataSequenceInOrder=No and Non-Immeidate
267 * Unsolicited DataOut.
269 if (cmd->unsolicited_data) {
270 cmd->unsolicited_data = 0;
272 offset = cmd->next_burst_len = cmd->write_data_done;
274 if ((conn->sess->sess_ops->FirstBurstLength - offset) >=
275 cmd->se_cmd.data_length) {
276 no_build_r2ts = 1;
277 length = (cmd->se_cmd.data_length - offset);
278 } else
279 length = (conn->sess->sess_ops->FirstBurstLength - offset);
281 spin_lock_bh(&cmd->r2t_lock);
282 if (iscsit_add_r2t_to_list(cmd, offset, length, 0, 0) < 0) {
283 spin_unlock_bh(&cmd->r2t_lock);
284 return -1;
286 cmd->outstanding_r2ts++;
287 spin_unlock_bh(&cmd->r2t_lock);
289 if (no_build_r2ts)
290 return 0;
293 * iscsit_build_r2ts_for_cmd() can handle the rest from here.
295 return conn->conn_transport->iscsit_get_dataout(conn, cmd, true);
298 static int iscsit_task_reassign_complete_read(
299 struct iscsi_cmd *cmd,
300 struct iscsi_tmr_req *tmr_req)
302 struct iscsi_conn *conn = cmd->conn;
303 struct iscsi_datain_req *dr;
304 struct se_cmd *se_cmd = &cmd->se_cmd;
306 * The Initiator must not send a Data SNACK with a BegRun less than
307 * the TMR TASK_REASSIGN's ExpDataSN.
309 if (!tmr_req->exp_data_sn) {
310 cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
311 cmd->acked_data_sn = 0;
312 } else {
313 cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
314 cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
317 if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
318 pr_debug("READ ITT: 0x%08x: t_state: %d never sent to"
319 " transport\n", cmd->init_task_tag,
320 cmd->se_cmd.t_state);
321 transport_handle_cdb_direct(se_cmd);
322 return 0;
325 if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
326 pr_err("READ ITT: 0x%08x: t_state: %d, never returned"
327 " from transport\n", cmd->init_task_tag,
328 cmd->se_cmd.t_state);
329 return -1;
332 dr = iscsit_allocate_datain_req();
333 if (!dr)
334 return -1;
336 * The TMR TASK_REASSIGN's ExpDataSN contains the next DataSN the
337 * Initiator is expecting.
339 dr->data_sn = dr->begrun = tmr_req->exp_data_sn;
340 dr->runlength = 0;
341 dr->generate_recovery_values = 1;
342 dr->recovery = DATAIN_CONNECTION_RECOVERY;
344 iscsit_attach_datain_req(cmd, dr);
346 cmd->i_state = ISTATE_SEND_DATAIN;
347 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
348 return 0;
351 static int iscsit_task_reassign_complete_none(
352 struct iscsi_cmd *cmd,
353 struct iscsi_tmr_req *tmr_req)
355 struct iscsi_conn *conn = cmd->conn;
357 cmd->i_state = ISTATE_SEND_STATUS;
358 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
359 return 0;
362 static int iscsit_task_reassign_complete_scsi_cmnd(
363 struct iscsi_tmr_req *tmr_req,
364 struct iscsi_conn *conn)
366 struct iscsi_cmd *cmd = tmr_req->ref_cmd;
367 struct iscsi_conn_recovery *cr;
369 if (!cmd->cr) {
370 pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
371 " is NULL!\n", cmd->init_task_tag);
372 return -1;
374 cr = cmd->cr;
377 * Reset the StatSN so a new one for this commands new connection
378 * will be assigned.
379 * Reset the ExpStatSN as well so we may receive Status SNACKs.
381 cmd->stat_sn = cmd->exp_stat_sn = 0;
383 iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
385 spin_lock_bh(&conn->cmd_lock);
386 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
387 spin_unlock_bh(&conn->cmd_lock);
389 if (cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
390 cmd->i_state = ISTATE_SEND_STATUS;
391 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
392 return 0;
395 switch (cmd->data_direction) {
396 case DMA_TO_DEVICE:
397 return iscsit_task_reassign_complete_write(cmd, tmr_req);
398 case DMA_FROM_DEVICE:
399 return iscsit_task_reassign_complete_read(cmd, tmr_req);
400 case DMA_NONE:
401 return iscsit_task_reassign_complete_none(cmd, tmr_req);
402 default:
403 pr_err("Unknown cmd->data_direction: 0x%02x\n",
404 cmd->data_direction);
405 return -1;
408 return 0;
411 static int iscsit_task_reassign_complete(
412 struct iscsi_tmr_req *tmr_req,
413 struct iscsi_conn *conn)
415 struct iscsi_cmd *cmd;
416 int ret = 0;
418 if (!tmr_req->ref_cmd) {
419 pr_err("TMR Request is missing a RefCmd struct iscsi_cmd.\n");
420 return -1;
422 cmd = tmr_req->ref_cmd;
424 cmd->conn = conn;
426 switch (cmd->iscsi_opcode) {
427 case ISCSI_OP_NOOP_OUT:
428 ret = iscsit_task_reassign_complete_nop_out(tmr_req, conn);
429 break;
430 case ISCSI_OP_SCSI_CMD:
431 ret = iscsit_task_reassign_complete_scsi_cmnd(tmr_req, conn);
432 break;
433 default:
434 pr_err("Illegal iSCSI Opcode 0x%02x during"
435 " command reallegiance\n", cmd->iscsi_opcode);
436 return -1;
439 if (ret != 0)
440 return ret;
442 pr_debug("Completed connection reallegiance for Opcode: 0x%02x,"
443 " ITT: 0x%08x to CID: %hu.\n", cmd->iscsi_opcode,
444 cmd->init_task_tag, conn->cid);
446 return 0;
450 * Handles special after-the-fact actions related to TMRs.
451 * Right now the only one that its really needed for is
452 * connection recovery releated TASK_REASSIGN.
454 int iscsit_tmr_post_handler(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
456 struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
457 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
459 if (tmr_req->task_reassign &&
460 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
461 return iscsit_task_reassign_complete(tmr_req, conn);
463 return 0;
465 EXPORT_SYMBOL(iscsit_tmr_post_handler);
468 * Nothing to do here, but leave it for good measure. :-)
470 static int iscsit_task_reassign_prepare_read(
471 struct iscsi_tmr_req *tmr_req,
472 struct iscsi_conn *conn)
474 return 0;
477 static void iscsit_task_reassign_prepare_unsolicited_dataout(
478 struct iscsi_cmd *cmd,
479 struct iscsi_conn *conn)
481 int i, j;
482 struct iscsi_pdu *pdu = NULL;
483 struct iscsi_seq *seq = NULL;
485 if (conn->sess->sess_ops->DataSequenceInOrder) {
486 cmd->data_sn = 0;
488 if (cmd->immediate_data)
489 cmd->r2t_offset += (cmd->first_burst_len -
490 cmd->seq_start_offset);
492 if (conn->sess->sess_ops->DataPDUInOrder) {
493 cmd->write_data_done -= (cmd->immediate_data) ?
494 (cmd->first_burst_len -
495 cmd->seq_start_offset) :
496 cmd->first_burst_len;
497 cmd->first_burst_len = 0;
498 return;
501 for (i = 0; i < cmd->pdu_count; i++) {
502 pdu = &cmd->pdu_list[i];
504 if (pdu->status != ISCSI_PDU_RECEIVED_OK)
505 continue;
507 if ((pdu->offset >= cmd->seq_start_offset) &&
508 ((pdu->offset + pdu->length) <=
509 cmd->seq_end_offset)) {
510 cmd->first_burst_len -= pdu->length;
511 cmd->write_data_done -= pdu->length;
512 pdu->status = ISCSI_PDU_NOT_RECEIVED;
515 } else {
516 for (i = 0; i < cmd->seq_count; i++) {
517 seq = &cmd->seq_list[i];
519 if (seq->type != SEQTYPE_UNSOLICITED)
520 continue;
522 cmd->write_data_done -=
523 (seq->offset - seq->orig_offset);
524 cmd->first_burst_len = 0;
525 seq->data_sn = 0;
526 seq->offset = seq->orig_offset;
527 seq->next_burst_len = 0;
528 seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
530 if (conn->sess->sess_ops->DataPDUInOrder)
531 continue;
533 for (j = 0; j < seq->pdu_count; j++) {
534 pdu = &cmd->pdu_list[j+seq->pdu_start];
536 if (pdu->status != ISCSI_PDU_RECEIVED_OK)
537 continue;
539 pdu->status = ISCSI_PDU_NOT_RECEIVED;
545 static int iscsit_task_reassign_prepare_write(
546 struct iscsi_tmr_req *tmr_req,
547 struct iscsi_conn *conn)
549 struct iscsi_cmd *cmd = tmr_req->ref_cmd;
550 struct iscsi_pdu *pdu = NULL;
551 struct iscsi_r2t *r2t = NULL, *r2t_tmp;
552 int first_incomplete_r2t = 1, i = 0;
555 * The command was in the process of receiving Unsolicited DataOUT when
556 * the connection failed.
558 if (cmd->unsolicited_data)
559 iscsit_task_reassign_prepare_unsolicited_dataout(cmd, conn);
562 * The Initiator is requesting R2Ts starting from zero, skip
563 * checking acknowledged R2Ts and start checking struct iscsi_r2ts
564 * greater than zero.
566 if (!tmr_req->exp_data_sn)
567 goto drop_unacknowledged_r2ts;
570 * We now check that the PDUs in DataOUT sequences below
571 * the TMR TASK_REASSIGN ExpDataSN (R2TSN the Initiator is
572 * expecting next) have all the DataOUT they require to complete
573 * the DataOUT sequence. First scan from R2TSN 0 to TMR
574 * TASK_REASSIGN ExpDataSN-1.
576 * If we have not received all DataOUT in question, we must
577 * make sure to make the appropriate changes to values in
578 * struct iscsi_cmd (and elsewhere depending on session parameters)
579 * so iscsit_build_r2ts_for_cmd() in iscsit_task_reassign_complete_write()
580 * will resend a new R2T for the DataOUT sequences in question.
582 spin_lock_bh(&cmd->r2t_lock);
583 if (list_empty(&cmd->cmd_r2t_list)) {
584 spin_unlock_bh(&cmd->r2t_lock);
585 return -1;
588 list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
590 if (r2t->r2t_sn >= tmr_req->exp_data_sn)
591 continue;
593 * Safely ignore Recovery R2Ts and R2Ts that have completed
594 * DataOUT sequences.
596 if (r2t->seq_complete)
597 continue;
599 if (r2t->recovery_r2t)
600 continue;
603 * DataSequenceInOrder=Yes:
605 * Taking into account the iSCSI implementation requirement of
606 * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
607 * DataSequenceInOrder=Yes, we must take into consideration
608 * the following:
610 * DataSequenceInOrder=No:
612 * Taking into account that the Initiator controls the (possibly
613 * random) PDU Order in (possibly random) Sequence Order of
614 * DataOUT the target requests with R2Ts, we must take into
615 * consideration the following:
617 * DataPDUInOrder=Yes for DataSequenceInOrder=[Yes,No]:
619 * While processing non-complete R2T DataOUT sequence requests
620 * the Target will re-request only the total sequence length
621 * minus current received offset. This is because we must
622 * assume the initiator will continue sending DataOUT from the
623 * last PDU before the connection failed.
625 * DataPDUInOrder=No for DataSequenceInOrder=[Yes,No]:
627 * While processing non-complete R2T DataOUT sequence requests
628 * the Target will re-request the entire DataOUT sequence if
629 * any single PDU is missing from the sequence. This is because
630 * we have no logical method to determine the next PDU offset,
631 * and we must assume the Initiator will be sending any random
632 * PDU offset in the current sequence after TASK_REASSIGN
633 * has completed.
635 if (conn->sess->sess_ops->DataSequenceInOrder) {
636 if (!first_incomplete_r2t) {
637 cmd->r2t_offset -= r2t->xfer_len;
638 goto next;
641 if (conn->sess->sess_ops->DataPDUInOrder) {
642 cmd->data_sn = 0;
643 cmd->r2t_offset -= (r2t->xfer_len -
644 cmd->next_burst_len);
645 first_incomplete_r2t = 0;
646 goto next;
649 cmd->data_sn = 0;
650 cmd->r2t_offset -= r2t->xfer_len;
652 for (i = 0; i < cmd->pdu_count; i++) {
653 pdu = &cmd->pdu_list[i];
655 if (pdu->status != ISCSI_PDU_RECEIVED_OK)
656 continue;
658 if ((pdu->offset >= r2t->offset) &&
659 (pdu->offset < (r2t->offset +
660 r2t->xfer_len))) {
661 cmd->next_burst_len -= pdu->length;
662 cmd->write_data_done -= pdu->length;
663 pdu->status = ISCSI_PDU_NOT_RECEIVED;
667 first_incomplete_r2t = 0;
668 } else {
669 struct iscsi_seq *seq;
671 seq = iscsit_get_seq_holder(cmd, r2t->offset,
672 r2t->xfer_len);
673 if (!seq) {
674 spin_unlock_bh(&cmd->r2t_lock);
675 return -1;
678 cmd->write_data_done -=
679 (seq->offset - seq->orig_offset);
680 seq->data_sn = 0;
681 seq->offset = seq->orig_offset;
682 seq->next_burst_len = 0;
683 seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
685 cmd->seq_send_order--;
687 if (conn->sess->sess_ops->DataPDUInOrder)
688 goto next;
690 for (i = 0; i < seq->pdu_count; i++) {
691 pdu = &cmd->pdu_list[i+seq->pdu_start];
693 if (pdu->status != ISCSI_PDU_RECEIVED_OK)
694 continue;
696 pdu->status = ISCSI_PDU_NOT_RECEIVED;
700 next:
701 cmd->outstanding_r2ts--;
703 spin_unlock_bh(&cmd->r2t_lock);
706 * We now drop all unacknowledged R2Ts, ie: ExpDataSN from TMR
707 * TASK_REASSIGN to the last R2T in the list.. We are also careful
708 * to check that the Initiator is not requesting R2Ts for DataOUT
709 * sequences it has already completed.
711 * Free each R2T in question and adjust values in struct iscsi_cmd
712 * accordingly so iscsit_build_r2ts_for_cmd() do the rest of
713 * the work after the TMR TASK_REASSIGN Response is sent.
715 drop_unacknowledged_r2ts:
717 cmd->cmd_flags &= ~ICF_SENT_LAST_R2T;
718 cmd->r2t_sn = tmr_req->exp_data_sn;
720 spin_lock_bh(&cmd->r2t_lock);
721 list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list) {
723 * Skip up to the R2T Sequence number provided by the
724 * iSCSI TASK_REASSIGN TMR
726 if (r2t->r2t_sn < tmr_req->exp_data_sn)
727 continue;
729 if (r2t->seq_complete) {
730 pr_err("Initiator is requesting R2Ts from"
731 " R2TSN: 0x%08x, but R2TSN: 0x%08x, Offset: %u,"
732 " Length: %u is already complete."
733 " BAD INITIATOR ERL=2 IMPLEMENTATION!\n",
734 tmr_req->exp_data_sn, r2t->r2t_sn,
735 r2t->offset, r2t->xfer_len);
736 spin_unlock_bh(&cmd->r2t_lock);
737 return -1;
740 if (r2t->recovery_r2t) {
741 iscsit_free_r2t(r2t, cmd);
742 continue;
745 /* DataSequenceInOrder=Yes:
747 * Taking into account the iSCSI implementation requirement of
748 * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
749 * DataSequenceInOrder=Yes, it's safe to subtract the R2Ts
750 * entire transfer length from the commands R2T offset marker.
752 * DataSequenceInOrder=No:
754 * We subtract the difference from struct iscsi_seq between the
755 * current offset and original offset from cmd->write_data_done
756 * for account for DataOUT PDUs already received. Then reset
757 * the current offset to the original and zero out the current
758 * burst length, to make sure we re-request the entire DataOUT
759 * sequence.
761 if (conn->sess->sess_ops->DataSequenceInOrder)
762 cmd->r2t_offset -= r2t->xfer_len;
763 else
764 cmd->seq_send_order--;
766 cmd->outstanding_r2ts--;
767 iscsit_free_r2t(r2t, cmd);
769 spin_unlock_bh(&cmd->r2t_lock);
771 return 0;
775 * Performs sanity checks TMR TASK_REASSIGN's ExpDataSN for
776 * a given struct iscsi_cmd.
778 int iscsit_check_task_reassign_expdatasn(
779 struct iscsi_tmr_req *tmr_req,
780 struct iscsi_conn *conn)
782 struct iscsi_cmd *ref_cmd = tmr_req->ref_cmd;
784 if (ref_cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD)
785 return 0;
787 if (ref_cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION)
788 return 0;
790 if (ref_cmd->data_direction == DMA_NONE)
791 return 0;
794 * For READs the TMR TASK_REASSIGNs ExpDataSN contains the next DataSN
795 * of DataIN the Initiator is expecting.
797 * Also check that the Initiator is not re-requesting DataIN that has
798 * already been acknowledged with a DataAck SNACK.
800 if (ref_cmd->data_direction == DMA_FROM_DEVICE) {
801 if (tmr_req->exp_data_sn > ref_cmd->data_sn) {
802 pr_err("Received ExpDataSN: 0x%08x for READ"
803 " in TMR TASK_REASSIGN greater than command's"
804 " DataSN: 0x%08x.\n", tmr_req->exp_data_sn,
805 ref_cmd->data_sn);
806 return -1;
808 if ((ref_cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
809 (tmr_req->exp_data_sn <= ref_cmd->acked_data_sn)) {
810 pr_err("Received ExpDataSN: 0x%08x for READ"
811 " in TMR TASK_REASSIGN for previously"
812 " acknowledged DataIN: 0x%08x,"
813 " protocol error\n", tmr_req->exp_data_sn,
814 ref_cmd->acked_data_sn);
815 return -1;
817 return iscsit_task_reassign_prepare_read(tmr_req, conn);
821 * For WRITEs the TMR TASK_REASSIGNs ExpDataSN contains the next R2TSN
822 * for R2Ts the Initiator is expecting.
824 * Do the magic in iscsit_task_reassign_prepare_write().
826 if (ref_cmd->data_direction == DMA_TO_DEVICE) {
827 if (tmr_req->exp_data_sn > ref_cmd->r2t_sn) {
828 pr_err("Received ExpDataSN: 0x%08x for WRITE"
829 " in TMR TASK_REASSIGN greater than command's"
830 " R2TSN: 0x%08x.\n", tmr_req->exp_data_sn,
831 ref_cmd->r2t_sn);
832 return -1;
834 return iscsit_task_reassign_prepare_write(tmr_req, conn);
837 pr_err("Unknown iSCSI data_direction: 0x%02x\n",
838 ref_cmd->data_direction);
840 return -1;