Linux 3.11-rc3
[cris-mirror.git] / drivers / target / iscsi / iscsi_target_tmr.c
blobb997e5da47d359f17001ca59d086cdf4d87978f4
1 /*******************************************************************************
2 * This file contains the iSCSI Target specific Task Management functions.
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ******************************************************************************/
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 #include <target/iscsi/iscsi_transport.h>
28 #include "iscsi_target_core.h"
29 #include "iscsi_target_seq_pdu_list.h"
30 #include "iscsi_target_datain_values.h"
31 #include "iscsi_target_device.h"
32 #include "iscsi_target_erl0.h"
33 #include "iscsi_target_erl1.h"
34 #include "iscsi_target_erl2.h"
35 #include "iscsi_target_tmr.h"
36 #include "iscsi_target_tpg.h"
37 #include "iscsi_target_util.h"
38 #include "iscsi_target.h"
40 u8 iscsit_tmr_abort_task(
41 struct iscsi_cmd *cmd,
42 unsigned char *buf)
44 struct iscsi_cmd *ref_cmd;
45 struct iscsi_conn *conn = cmd->conn;
46 struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
47 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
48 struct iscsi_tm *hdr = (struct iscsi_tm *) buf;
50 ref_cmd = iscsit_find_cmd_from_itt(conn, hdr->rtt);
51 if (!ref_cmd) {
52 pr_err("Unable to locate RefTaskTag: 0x%08x on CID:"
53 " %hu.\n", hdr->rtt, conn->cid);
54 return (iscsi_sna_gte(be32_to_cpu(hdr->refcmdsn), conn->sess->exp_cmd_sn) &&
55 iscsi_sna_lte(be32_to_cpu(hdr->refcmdsn), conn->sess->max_cmd_sn)) ?
56 ISCSI_TMF_RSP_COMPLETE : ISCSI_TMF_RSP_NO_TASK;
58 if (ref_cmd->cmd_sn != be32_to_cpu(hdr->refcmdsn)) {
59 pr_err("RefCmdSN 0x%08x does not equal"
60 " task's CmdSN 0x%08x. Rejecting ABORT_TASK.\n",
61 hdr->refcmdsn, ref_cmd->cmd_sn);
62 return ISCSI_TMF_RSP_REJECTED;
65 se_tmr->ref_task_tag = (__force u32)hdr->rtt;
66 tmr_req->ref_cmd = ref_cmd;
67 tmr_req->exp_data_sn = be32_to_cpu(hdr->exp_datasn);
69 return ISCSI_TMF_RSP_COMPLETE;
73 * Called from iscsit_handle_task_mgt_cmd().
75 int iscsit_tmr_task_warm_reset(
76 struct iscsi_conn *conn,
77 struct iscsi_tmr_req *tmr_req,
78 unsigned char *buf)
80 struct iscsi_session *sess = conn->sess;
81 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
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;
90 * Do the real work in transport_generic_do_tmr().
92 return 0;
95 int iscsit_tmr_task_cold_reset(
96 struct iscsi_conn *conn,
97 struct iscsi_tmr_req *tmr_req,
98 unsigned char *buf)
100 struct iscsi_session *sess = conn->sess;
101 struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
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;
110 * Do the real work in transport_generic_do_tmr().
112 return 0;
115 u8 iscsit_tmr_task_reassign(
116 struct iscsi_cmd *cmd,
117 unsigned char *buf)
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;
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);
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;
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;
148 * Temporary check to prevent connection recovery for
149 * connections with a differing Max*DataSegmentLength.
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;
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;
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;
174 se_tmr->ref_task_tag = (__force u32)hdr->rtt;
175 tmr_req->ref_cmd = ref_cmd;
176 tmr_req->exp_data_sn = be32_to_cpu(hdr->exp_datasn);
177 tmr_req->conn_recovery = cr;
178 tmr_req->task_reassign = 1;
180 * Command can now be reassigned to a new connection.
181 * The task management response must be sent before the
182 * reassignment actually happens. See iscsi_tmr_post_handler().
184 return ISCSI_TMF_RSP_COMPLETE;
187 static void iscsit_task_reassign_remove_cmd(
188 struct iscsi_cmd *cmd,
189 struct iscsi_conn_recovery *cr,
190 struct iscsi_session *sess)
192 int ret;
194 spin_lock(&cr->conn_recovery_cmd_lock);
195 ret = iscsit_remove_cmd_from_connection_recovery(cmd, sess);
196 spin_unlock(&cr->conn_recovery_cmd_lock);
197 if (!ret) {
198 pr_debug("iSCSI connection recovery successful for CID:"
199 " %hu on SID: %u\n", cr->cid, sess->sid);
200 iscsit_remove_active_connection_recovery_entry(cr, sess);
204 static int iscsit_task_reassign_complete_nop_out(
205 struct iscsi_tmr_req *tmr_req,
206 struct iscsi_conn *conn)
208 struct iscsi_cmd *cmd = tmr_req->ref_cmd;
209 struct iscsi_conn_recovery *cr;
211 if (!cmd->cr) {
212 pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
213 " is NULL!\n", cmd->init_task_tag);
214 return -1;
216 cr = cmd->cr;
219 * Reset the StatSN so a new one for this commands new connection
220 * will be assigned.
221 * Reset the ExpStatSN as well so we may receive Status SNACKs.
223 cmd->stat_sn = cmd->exp_stat_sn = 0;
225 iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
227 spin_lock_bh(&conn->cmd_lock);
228 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
229 spin_unlock_bh(&conn->cmd_lock);
231 cmd->i_state = ISTATE_SEND_NOPIN;
232 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
233 return 0;
236 static int iscsit_task_reassign_complete_write(
237 struct iscsi_cmd *cmd,
238 struct iscsi_tmr_req *tmr_req)
240 int no_build_r2ts = 0;
241 u32 length = 0, offset = 0;
242 struct iscsi_conn *conn = cmd->conn;
243 struct se_cmd *se_cmd = &cmd->se_cmd;
245 * The Initiator must not send a R2T SNACK with a Begrun less than
246 * the TMR TASK_REASSIGN's ExpDataSN.
248 if (!tmr_req->exp_data_sn) {
249 cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
250 cmd->acked_data_sn = 0;
251 } else {
252 cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
253 cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
257 * The TMR TASK_REASSIGN's ExpDataSN contains the next R2TSN the
258 * Initiator is expecting. The Target controls all WRITE operations
259 * so if we have received all DataOUT we can safety ignore Initiator.
261 if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
262 if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
263 pr_debug("WRITE ITT: 0x%08x: t_state: %d"
264 " never sent to transport\n",
265 cmd->init_task_tag, cmd->se_cmd.t_state);
266 target_execute_cmd(se_cmd);
267 return 0;
270 cmd->i_state = ISTATE_SEND_STATUS;
271 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
272 return 0;
276 * Special case to deal with DataSequenceInOrder=No and Non-Immeidate
277 * Unsolicited DataOut.
279 if (cmd->unsolicited_data) {
280 cmd->unsolicited_data = 0;
282 offset = cmd->next_burst_len = cmd->write_data_done;
284 if ((conn->sess->sess_ops->FirstBurstLength - offset) >=
285 cmd->se_cmd.data_length) {
286 no_build_r2ts = 1;
287 length = (cmd->se_cmd.data_length - offset);
288 } else
289 length = (conn->sess->sess_ops->FirstBurstLength - offset);
291 spin_lock_bh(&cmd->r2t_lock);
292 if (iscsit_add_r2t_to_list(cmd, offset, length, 0, 0) < 0) {
293 spin_unlock_bh(&cmd->r2t_lock);
294 return -1;
296 cmd->outstanding_r2ts++;
297 spin_unlock_bh(&cmd->r2t_lock);
299 if (no_build_r2ts)
300 return 0;
303 * iscsit_build_r2ts_for_cmd() can handle the rest from here.
305 return conn->conn_transport->iscsit_get_dataout(conn, cmd, true);
308 static int iscsit_task_reassign_complete_read(
309 struct iscsi_cmd *cmd,
310 struct iscsi_tmr_req *tmr_req)
312 struct iscsi_conn *conn = cmd->conn;
313 struct iscsi_datain_req *dr;
314 struct se_cmd *se_cmd = &cmd->se_cmd;
316 * The Initiator must not send a Data SNACK with a BegRun less than
317 * the TMR TASK_REASSIGN's ExpDataSN.
319 if (!tmr_req->exp_data_sn) {
320 cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
321 cmd->acked_data_sn = 0;
322 } else {
323 cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
324 cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
327 if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
328 pr_debug("READ ITT: 0x%08x: t_state: %d never sent to"
329 " transport\n", cmd->init_task_tag,
330 cmd->se_cmd.t_state);
331 transport_handle_cdb_direct(se_cmd);
332 return 0;
335 if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
336 pr_err("READ ITT: 0x%08x: t_state: %d, never returned"
337 " from transport\n", cmd->init_task_tag,
338 cmd->se_cmd.t_state);
339 return -1;
342 dr = iscsit_allocate_datain_req();
343 if (!dr)
344 return -1;
346 * The TMR TASK_REASSIGN's ExpDataSN contains the next DataSN the
347 * Initiator is expecting.
349 dr->data_sn = dr->begrun = tmr_req->exp_data_sn;
350 dr->runlength = 0;
351 dr->generate_recovery_values = 1;
352 dr->recovery = DATAIN_CONNECTION_RECOVERY;
354 iscsit_attach_datain_req(cmd, dr);
356 cmd->i_state = ISTATE_SEND_DATAIN;
357 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
358 return 0;
361 static int iscsit_task_reassign_complete_none(
362 struct iscsi_cmd *cmd,
363 struct iscsi_tmr_req *tmr_req)
365 struct iscsi_conn *conn = cmd->conn;
367 cmd->i_state = ISTATE_SEND_STATUS;
368 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
369 return 0;
372 static int iscsit_task_reassign_complete_scsi_cmnd(
373 struct iscsi_tmr_req *tmr_req,
374 struct iscsi_conn *conn)
376 struct iscsi_cmd *cmd = tmr_req->ref_cmd;
377 struct iscsi_conn_recovery *cr;
379 if (!cmd->cr) {
380 pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
381 " is NULL!\n", cmd->init_task_tag);
382 return -1;
384 cr = cmd->cr;
387 * Reset the StatSN so a new one for this commands new connection
388 * will be assigned.
389 * Reset the ExpStatSN as well so we may receive Status SNACKs.
391 cmd->stat_sn = cmd->exp_stat_sn = 0;
393 iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
395 spin_lock_bh(&conn->cmd_lock);
396 list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
397 spin_unlock_bh(&conn->cmd_lock);
399 if (cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
400 cmd->i_state = ISTATE_SEND_STATUS;
401 iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
402 return 0;
405 switch (cmd->data_direction) {
406 case DMA_TO_DEVICE:
407 return iscsit_task_reassign_complete_write(cmd, tmr_req);
408 case DMA_FROM_DEVICE:
409 return iscsit_task_reassign_complete_read(cmd, tmr_req);
410 case DMA_NONE:
411 return iscsit_task_reassign_complete_none(cmd, tmr_req);
412 default:
413 pr_err("Unknown cmd->data_direction: 0x%02x\n",
414 cmd->data_direction);
415 return -1;
418 return 0;
421 static int iscsit_task_reassign_complete(
422 struct iscsi_tmr_req *tmr_req,
423 struct iscsi_conn *conn)
425 struct iscsi_cmd *cmd;
426 int ret = 0;
428 if (!tmr_req->ref_cmd) {
429 pr_err("TMR Request is missing a RefCmd struct iscsi_cmd.\n");
430 return -1;
432 cmd = tmr_req->ref_cmd;
434 cmd->conn = conn;
436 switch (cmd->iscsi_opcode) {
437 case ISCSI_OP_NOOP_OUT:
438 ret = iscsit_task_reassign_complete_nop_out(tmr_req, conn);
439 break;
440 case ISCSI_OP_SCSI_CMD:
441 ret = iscsit_task_reassign_complete_scsi_cmnd(tmr_req, conn);
442 break;
443 default:
444 pr_err("Illegal iSCSI Opcode 0x%02x during"
445 " command realligence\n", cmd->iscsi_opcode);
446 return -1;
449 if (ret != 0)
450 return ret;
452 pr_debug("Completed connection realligence for Opcode: 0x%02x,"
453 " ITT: 0x%08x to CID: %hu.\n", cmd->iscsi_opcode,
454 cmd->init_task_tag, conn->cid);
456 return 0;
460 * Handles special after-the-fact actions related to TMRs.
461 * Right now the only one that its really needed for is
462 * connection recovery releated TASK_REASSIGN.
464 int iscsit_tmr_post_handler(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
466 struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
467 struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
469 if (tmr_req->task_reassign &&
470 (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
471 return iscsit_task_reassign_complete(tmr_req, conn);
473 return 0;
475 EXPORT_SYMBOL(iscsit_tmr_post_handler);
478 * Nothing to do here, but leave it for good measure. :-)
480 static int iscsit_task_reassign_prepare_read(
481 struct iscsi_tmr_req *tmr_req,
482 struct iscsi_conn *conn)
484 return 0;
487 static void iscsit_task_reassign_prepare_unsolicited_dataout(
488 struct iscsi_cmd *cmd,
489 struct iscsi_conn *conn)
491 int i, j;
492 struct iscsi_pdu *pdu = NULL;
493 struct iscsi_seq *seq = NULL;
495 if (conn->sess->sess_ops->DataSequenceInOrder) {
496 cmd->data_sn = 0;
498 if (cmd->immediate_data)
499 cmd->r2t_offset += (cmd->first_burst_len -
500 cmd->seq_start_offset);
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;
511 for (i = 0; i < cmd->pdu_count; i++) {
512 pdu = &cmd->pdu_list[i];
514 if (pdu->status != ISCSI_PDU_RECEIVED_OK)
515 continue;
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;
525 } else {
526 for (i = 0; i < cmd->seq_count; i++) {
527 seq = &cmd->seq_list[i];
529 if (seq->type != SEQTYPE_UNSOLICITED)
530 continue;
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;
540 if (conn->sess->sess_ops->DataPDUInOrder)
541 continue;
543 for (j = 0; j < seq->pdu_count; j++) {
544 pdu = &cmd->pdu_list[j+seq->pdu_start];
546 if (pdu->status != ISCSI_PDU_RECEIVED_OK)
547 continue;
549 pdu->status = ISCSI_PDU_NOT_RECEIVED;
555 static int iscsit_task_reassign_prepare_write(
556 struct iscsi_tmr_req *tmr_req,
557 struct iscsi_conn *conn)
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;
565 * The command was in the process of receiving Unsolicited DataOUT when
566 * the connection failed.
568 if (cmd->unsolicited_data)
569 iscsit_task_reassign_prepare_unsolicited_dataout(cmd, conn);
572 * The Initiator is requesting R2Ts starting from zero, skip
573 * checking acknowledged R2Ts and start checking struct iscsi_r2ts
574 * greater than zero.
576 if (!tmr_req->exp_data_sn)
577 goto drop_unacknowledged_r2ts;
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.
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.
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;
598 list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
600 if (r2t->r2t_sn >= tmr_req->exp_data_sn)
601 continue;
603 * Safely ignore Recovery R2Ts and R2Ts that have completed
604 * DataOUT sequences.
606 if (r2t->seq_complete)
607 continue;
609 if (r2t->recovery_r2t)
610 continue;
613 * DataSequenceInOrder=Yes:
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:
620 * DataSequenceInOrder=No:
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:
627 * DataPDUInOrder=Yes for DataSequenceInOrder=[Yes,No]:
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.
635 * DataPDUInOrder=No for DataSequenceInOrder=[Yes,No]:
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.
645 if (conn->sess->sess_ops->DataSequenceInOrder) {
646 if (!first_incomplete_r2t) {
647 cmd->r2t_offset -= r2t->xfer_len;
648 goto next;
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;
659 cmd->data_sn = 0;
660 cmd->r2t_offset -= r2t->xfer_len;
662 for (i = 0; i < cmd->pdu_count; i++) {
663 pdu = &cmd->pdu_list[i];
665 if (pdu->status != ISCSI_PDU_RECEIVED_OK)
666 continue;
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;
677 first_incomplete_r2t = 0;
678 } else {
679 struct iscsi_seq *seq;
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;
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;
695 cmd->seq_send_order--;
697 if (conn->sess->sess_ops->DataPDUInOrder)
698 goto next;
700 for (i = 0; i < seq->pdu_count; i++) {
701 pdu = &cmd->pdu_list[i+seq->pdu_start];
703 if (pdu->status != ISCSI_PDU_RECEIVED_OK)
704 continue;
706 pdu->status = ISCSI_PDU_NOT_RECEIVED;
710 next:
711 cmd->outstanding_r2ts--;
713 spin_unlock_bh(&cmd->r2t_lock);
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.
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.
725 drop_unacknowledged_r2ts:
727 cmd->cmd_flags &= ~ICF_SENT_LAST_R2T;
728 cmd->r2t_sn = tmr_req->exp_data_sn;
730 spin_lock_bh(&cmd->r2t_lock);
731 list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list) {
733 * Skip up to the R2T Sequence number provided by the
734 * iSCSI TASK_REASSIGN TMR
736 if (r2t->r2t_sn < tmr_req->exp_data_sn)
737 continue;
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;
750 if (r2t->recovery_r2t) {
751 iscsit_free_r2t(r2t, cmd);
752 continue;
755 /* DataSequenceInOrder=Yes:
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.
762 * DataSequenceInOrder=No:
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.
771 if (conn->sess->sess_ops->DataSequenceInOrder)
772 cmd->r2t_offset -= r2t->xfer_len;
773 else
774 cmd->seq_send_order--;
776 cmd->outstanding_r2ts--;
777 iscsit_free_r2t(r2t, cmd);
779 spin_unlock_bh(&cmd->r2t_lock);
781 return 0;
785 * Performs sanity checks TMR TASK_REASSIGN's ExpDataSN for
786 * a given struct iscsi_cmd.
788 int iscsit_check_task_reassign_expdatasn(
789 struct iscsi_tmr_req *tmr_req,
790 struct iscsi_conn *conn)
792 struct iscsi_cmd *ref_cmd = tmr_req->ref_cmd;
794 if (ref_cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD)
795 return 0;
797 if (ref_cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION)
798 return 0;
800 if (ref_cmd->data_direction == DMA_NONE)
801 return 0;
804 * For READs the TMR TASK_REASSIGNs ExpDataSN contains the next DataSN
805 * of DataIN the Initiator is expecting.
807 * Also check that the Initiator is not re-requesting DataIN that has
808 * already been acknowledged with a DataAck SNACK.
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;
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;
827 return iscsit_task_reassign_prepare_read(tmr_req, conn);
831 * For WRITEs the TMR TASK_REASSIGNs ExpDataSN contains the next R2TSN
832 * for R2Ts the Initiator is expecting.
834 * Do the magic in iscsit_task_reassign_prepare_write().
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;
844 return iscsit_task_reassign_prepare_write(tmr_req, conn);
847 pr_err("Unknown iSCSI data_direction: 0x%02x\n",
848 ref_cmd->data_direction);
850 return -1;