1 /******************************************************************************
2 * This file contains error recovery level zero functions used by
3 * the iSCSI Target driver.
5 * (c) Copyright 2007-2013 Datera, Inc.
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 ******************************************************************************/
20 #include <scsi/iscsi_proto.h>
21 #include <target/target_core_base.h>
22 #include <target/target_core_fabric.h>
24 #include <target/iscsi/iscsi_target_core.h>
25 #include "iscsi_target_seq_pdu_list.h"
26 #include "iscsi_target_erl0.h"
27 #include "iscsi_target_erl1.h"
28 #include "iscsi_target_erl2.h"
29 #include "iscsi_target_util.h"
30 #include "iscsi_target.h"
33 * Used to set values in struct iscsi_cmd that iscsit_dataout_check_sequence()
34 * checks against to determine a PDU's Offset+Length is within the current
35 * DataOUT Sequence. Used for DataSequenceInOrder=Yes only.
37 void iscsit_set_dataout_sequence_values(
38 struct iscsi_cmd *cmd)
40 struct iscsi_conn *conn = cmd->conn;
42 * Still set seq_start_offset and seq_end_offset for Unsolicited
43 * DataOUT, even if DataSequenceInOrder=No.
45 if (cmd->unsolicited_data) {
46 cmd->seq_start_offset = cmd->write_data_done;
47 cmd->seq_end_offset = (cmd->write_data_done +
48 ((cmd->se_cmd.data_length >
49 conn->sess->sess_ops->FirstBurstLength) ?
50 conn->sess->sess_ops->FirstBurstLength : cmd->se_cmd.data_length));
54 if (!conn->sess->sess_ops->DataSequenceInOrder)
57 if (!cmd->seq_start_offset && !cmd->seq_end_offset) {
58 cmd->seq_start_offset = cmd->write_data_done;
59 cmd->seq_end_offset = (cmd->se_cmd.data_length >
60 conn->sess->sess_ops->MaxBurstLength) ?
61 (cmd->write_data_done +
62 conn->sess->sess_ops->MaxBurstLength) : cmd->se_cmd.data_length;
64 cmd->seq_start_offset = cmd->seq_end_offset;
65 cmd->seq_end_offset = ((cmd->seq_end_offset +
66 conn->sess->sess_ops->MaxBurstLength) >=
67 cmd->se_cmd.data_length) ? cmd->se_cmd.data_length :
68 (cmd->seq_end_offset +
69 conn->sess->sess_ops->MaxBurstLength);
73 static int iscsit_dataout_within_command_recovery_check(
74 struct iscsi_cmd *cmd,
77 struct iscsi_conn *conn = cmd->conn;
78 struct iscsi_data *hdr = (struct iscsi_data *) buf;
79 u32 payload_length = ntoh24(hdr->dlength);
82 * We do the within-command recovery checks here as it is
83 * the first function called in iscsi_check_pre_dataout().
84 * Basically, if we are in within-command recovery and
85 * the PDU does not contain the offset the sequence needs,
88 * This only applies to DataPDUInOrder=Yes, for
89 * DataPDUInOrder=No we only re-request the failed PDU
90 * and check that all PDUs in a sequence are received
91 * upon end of sequence.
93 if (conn->sess->sess_ops->DataSequenceInOrder) {
94 if ((cmd->cmd_flags & ICF_WITHIN_COMMAND_RECOVERY) &&
95 cmd->write_data_done != be32_to_cpu(hdr->offset))
98 cmd->cmd_flags &= ~ICF_WITHIN_COMMAND_RECOVERY;
100 struct iscsi_seq *seq;
102 seq = iscsit_get_seq_holder(cmd, be32_to_cpu(hdr->offset),
105 return DATAOUT_CANNOT_RECOVER;
107 * Set the struct iscsi_seq pointer to reuse later.
111 if (conn->sess->sess_ops->DataPDUInOrder) {
113 DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY &&
114 (seq->offset != be32_to_cpu(hdr->offset) ||
115 seq->data_sn != be32_to_cpu(hdr->datasn)))
119 DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY &&
120 seq->data_sn != be32_to_cpu(hdr->datasn))
124 if (seq->status == DATAOUT_SEQUENCE_COMPLETE)
127 if (seq->status != DATAOUT_SEQUENCE_COMPLETE)
131 return DATAOUT_NORMAL;
134 pr_err("Dumping DataOUT PDU Offset: %u Length: %d DataSN:"
135 " 0x%08x\n", hdr->offset, payload_length, hdr->datasn);
136 return iscsit_dump_data_payload(conn, payload_length, 1);
139 static int iscsit_dataout_check_unsolicited_sequence(
140 struct iscsi_cmd *cmd,
144 struct iscsi_conn *conn = cmd->conn;
145 struct iscsi_data *hdr = (struct iscsi_data *) buf;
146 u32 payload_length = ntoh24(hdr->dlength);
149 if ((be32_to_cpu(hdr->offset) < cmd->seq_start_offset) ||
150 ((be32_to_cpu(hdr->offset) + payload_length) > cmd->seq_end_offset)) {
151 pr_err("Command ITT: 0x%08x with Offset: %u,"
152 " Length: %u outside of Unsolicited Sequence %u:%u while"
153 " DataSequenceInOrder=Yes.\n", cmd->init_task_tag,
154 be32_to_cpu(hdr->offset), payload_length, cmd->seq_start_offset,
155 cmd->seq_end_offset);
156 return DATAOUT_CANNOT_RECOVER;
159 first_burst_len = (cmd->first_burst_len + payload_length);
161 if (first_burst_len > conn->sess->sess_ops->FirstBurstLength) {
162 pr_err("Total %u bytes exceeds FirstBurstLength: %u"
163 " for this Unsolicited DataOut Burst.\n",
164 first_burst_len, conn->sess->sess_ops->FirstBurstLength);
165 transport_send_check_condition_and_sense(&cmd->se_cmd,
166 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
167 return DATAOUT_CANNOT_RECOVER;
171 * Perform various MaxBurstLength and ISCSI_FLAG_CMD_FINAL sanity
172 * checks for the current Unsolicited DataOUT Sequence.
174 if (hdr->flags & ISCSI_FLAG_CMD_FINAL) {
176 * Ignore ISCSI_FLAG_CMD_FINAL checks while DataPDUInOrder=No, end of
177 * sequence checks are handled in
178 * iscsit_dataout_datapduinorder_no_fbit().
180 if (!conn->sess->sess_ops->DataPDUInOrder)
183 if ((first_burst_len != cmd->se_cmd.data_length) &&
184 (first_burst_len != conn->sess->sess_ops->FirstBurstLength)) {
185 pr_err("Unsolicited non-immediate data"
186 " received %u does not equal FirstBurstLength: %u, and"
187 " does not equal ExpXferLen %u.\n", first_burst_len,
188 conn->sess->sess_ops->FirstBurstLength,
189 cmd->se_cmd.data_length);
190 transport_send_check_condition_and_sense(&cmd->se_cmd,
191 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
192 return DATAOUT_CANNOT_RECOVER;
195 if (first_burst_len == conn->sess->sess_ops->FirstBurstLength) {
196 pr_err("Command ITT: 0x%08x reached"
197 " FirstBurstLength: %u, but ISCSI_FLAG_CMD_FINAL is not set. protocol"
198 " error.\n", cmd->init_task_tag,
199 conn->sess->sess_ops->FirstBurstLength);
200 return DATAOUT_CANNOT_RECOVER;
202 if (first_burst_len == cmd->se_cmd.data_length) {
203 pr_err("Command ITT: 0x%08x reached"
204 " ExpXferLen: %u, but ISCSI_FLAG_CMD_FINAL is not set. protocol"
205 " error.\n", cmd->init_task_tag, cmd->se_cmd.data_length);
206 return DATAOUT_CANNOT_RECOVER;
211 return DATAOUT_NORMAL;
214 static int iscsit_dataout_check_sequence(
215 struct iscsi_cmd *cmd,
219 struct iscsi_conn *conn = cmd->conn;
220 struct iscsi_seq *seq = NULL;
221 struct iscsi_data *hdr = (struct iscsi_data *) buf;
222 u32 payload_length = ntoh24(hdr->dlength);
225 * For DataSequenceInOrder=Yes: Check that the offset and offset+length
226 * is within range as defined by iscsi_set_dataout_sequence_values().
228 * For DataSequenceInOrder=No: Check that an struct iscsi_seq exists for
229 * offset+length tuple.
231 if (conn->sess->sess_ops->DataSequenceInOrder) {
233 * Due to possibility of recovery DataOUT sent by the initiator
234 * fullfilling an Recovery R2T, it's best to just dump the
235 * payload here, instead of erroring out.
237 if ((be32_to_cpu(hdr->offset) < cmd->seq_start_offset) ||
238 ((be32_to_cpu(hdr->offset) + payload_length) > cmd->seq_end_offset)) {
239 pr_err("Command ITT: 0x%08x with Offset: %u,"
240 " Length: %u outside of Sequence %u:%u while"
241 " DataSequenceInOrder=Yes.\n", cmd->init_task_tag,
242 be32_to_cpu(hdr->offset), payload_length, cmd->seq_start_offset,
243 cmd->seq_end_offset);
245 if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
246 return DATAOUT_CANNOT_RECOVER;
247 return DATAOUT_WITHIN_COMMAND_RECOVERY;
250 next_burst_len = (cmd->next_burst_len + payload_length);
252 seq = iscsit_get_seq_holder(cmd, be32_to_cpu(hdr->offset),
255 return DATAOUT_CANNOT_RECOVER;
257 * Set the struct iscsi_seq pointer to reuse later.
261 if (seq->status == DATAOUT_SEQUENCE_COMPLETE) {
262 if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
263 return DATAOUT_CANNOT_RECOVER;
264 return DATAOUT_WITHIN_COMMAND_RECOVERY;
267 next_burst_len = (seq->next_burst_len + payload_length);
270 if (next_burst_len > conn->sess->sess_ops->MaxBurstLength) {
271 pr_err("Command ITT: 0x%08x, NextBurstLength: %u and"
272 " Length: %u exceeds MaxBurstLength: %u. protocol"
273 " error.\n", cmd->init_task_tag,
274 (next_burst_len - payload_length),
275 payload_length, conn->sess->sess_ops->MaxBurstLength);
276 return DATAOUT_CANNOT_RECOVER;
280 * Perform various MaxBurstLength and ISCSI_FLAG_CMD_FINAL sanity
281 * checks for the current DataOUT Sequence.
283 if (hdr->flags & ISCSI_FLAG_CMD_FINAL) {
285 * Ignore ISCSI_FLAG_CMD_FINAL checks while DataPDUInOrder=No, end of
286 * sequence checks are handled in
287 * iscsit_dataout_datapduinorder_no_fbit().
289 if (!conn->sess->sess_ops->DataPDUInOrder)
292 if (conn->sess->sess_ops->DataSequenceInOrder) {
293 if ((next_burst_len <
294 conn->sess->sess_ops->MaxBurstLength) &&
295 ((cmd->write_data_done + payload_length) <
296 cmd->se_cmd.data_length)) {
297 pr_err("Command ITT: 0x%08x set ISCSI_FLAG_CMD_FINAL"
298 " before end of DataOUT sequence, protocol"
299 " error.\n", cmd->init_task_tag);
300 return DATAOUT_CANNOT_RECOVER;
303 if (next_burst_len < seq->xfer_len) {
304 pr_err("Command ITT: 0x%08x set ISCSI_FLAG_CMD_FINAL"
305 " before end of DataOUT sequence, protocol"
306 " error.\n", cmd->init_task_tag);
307 return DATAOUT_CANNOT_RECOVER;
311 if (conn->sess->sess_ops->DataSequenceInOrder) {
312 if (next_burst_len ==
313 conn->sess->sess_ops->MaxBurstLength) {
314 pr_err("Command ITT: 0x%08x reached"
315 " MaxBurstLength: %u, but ISCSI_FLAG_CMD_FINAL is"
316 " not set, protocol error.", cmd->init_task_tag,
317 conn->sess->sess_ops->MaxBurstLength);
318 return DATAOUT_CANNOT_RECOVER;
320 if ((cmd->write_data_done + payload_length) ==
321 cmd->se_cmd.data_length) {
322 pr_err("Command ITT: 0x%08x reached"
323 " last DataOUT PDU in sequence but ISCSI_FLAG_"
324 "CMD_FINAL is not set, protocol error.\n",
326 return DATAOUT_CANNOT_RECOVER;
329 if (next_burst_len == seq->xfer_len) {
330 pr_err("Command ITT: 0x%08x reached"
331 " last DataOUT PDU in sequence but ISCSI_FLAG_"
332 "CMD_FINAL is not set, protocol error.\n",
334 return DATAOUT_CANNOT_RECOVER;
340 return DATAOUT_NORMAL;
343 static int iscsit_dataout_check_datasn(
344 struct iscsi_cmd *cmd,
348 struct iscsi_conn *conn = cmd->conn;
349 struct iscsi_data *hdr = (struct iscsi_data *) buf;
350 u32 payload_length = ntoh24(hdr->dlength);
353 * Considering the target has no method of re-requesting DataOUT
354 * by DataSN, if we receieve a greater DataSN than expected we
355 * assume the functions for DataPDUInOrder=[Yes,No] below will
358 * If the DataSN is less than expected, dump the payload.
360 if (conn->sess->sess_ops->DataSequenceInOrder)
361 data_sn = cmd->data_sn;
363 struct iscsi_seq *seq = cmd->seq_ptr;
364 data_sn = seq->data_sn;
367 if (be32_to_cpu(hdr->datasn) > data_sn) {
368 pr_err("Command ITT: 0x%08x, received DataSN: 0x%08x"
369 " higher than expected 0x%08x.\n", cmd->init_task_tag,
370 be32_to_cpu(hdr->datasn), data_sn);
372 } else if (be32_to_cpu(hdr->datasn) < data_sn) {
373 pr_err("Command ITT: 0x%08x, received DataSN: 0x%08x"
374 " lower than expected 0x%08x, discarding payload.\n",
375 cmd->init_task_tag, be32_to_cpu(hdr->datasn), data_sn);
379 return DATAOUT_NORMAL;
382 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
383 pr_err("Unable to perform within-command recovery"
385 return DATAOUT_CANNOT_RECOVER;
388 if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
389 return DATAOUT_CANNOT_RECOVER;
391 return DATAOUT_WITHIN_COMMAND_RECOVERY;
394 static int iscsit_dataout_pre_datapduinorder_yes(
395 struct iscsi_cmd *cmd,
398 int dump = 0, recovery = 0;
399 struct iscsi_conn *conn = cmd->conn;
400 struct iscsi_data *hdr = (struct iscsi_data *) buf;
401 u32 payload_length = ntoh24(hdr->dlength);
404 * For DataSequenceInOrder=Yes: If the offset is greater than the global
405 * DataPDUInOrder=Yes offset counter in struct iscsi_cmd a protcol error has
406 * occurred and fail the connection.
408 * For DataSequenceInOrder=No: If the offset is greater than the per
409 * sequence DataPDUInOrder=Yes offset counter in struct iscsi_seq a protocol
410 * error has occurred and fail the connection.
412 if (conn->sess->sess_ops->DataSequenceInOrder) {
413 if (be32_to_cpu(hdr->offset) != cmd->write_data_done) {
414 pr_err("Command ITT: 0x%08x, received offset"
415 " %u different than expected %u.\n", cmd->init_task_tag,
416 be32_to_cpu(hdr->offset), cmd->write_data_done);
421 struct iscsi_seq *seq = cmd->seq_ptr;
423 if (be32_to_cpu(hdr->offset) > seq->offset) {
424 pr_err("Command ITT: 0x%08x, received offset"
425 " %u greater than expected %u.\n", cmd->init_task_tag,
426 be32_to_cpu(hdr->offset), seq->offset);
429 } else if (be32_to_cpu(hdr->offset) < seq->offset) {
430 pr_err("Command ITT: 0x%08x, received offset"
431 " %u less than expected %u, discarding payload.\n",
432 cmd->init_task_tag, be32_to_cpu(hdr->offset),
439 return DATAOUT_NORMAL;
442 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
443 pr_err("Unable to perform within-command recovery"
445 return DATAOUT_CANNOT_RECOVER;
448 if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
449 return DATAOUT_CANNOT_RECOVER;
451 return (recovery) ? iscsit_recover_dataout_sequence(cmd,
452 be32_to_cpu(hdr->offset), payload_length) :
453 (dump) ? DATAOUT_WITHIN_COMMAND_RECOVERY : DATAOUT_NORMAL;
456 static int iscsit_dataout_pre_datapduinorder_no(
457 struct iscsi_cmd *cmd,
460 struct iscsi_pdu *pdu;
461 struct iscsi_data *hdr = (struct iscsi_data *) buf;
462 u32 payload_length = ntoh24(hdr->dlength);
464 pdu = iscsit_get_pdu_holder(cmd, be32_to_cpu(hdr->offset),
467 return DATAOUT_CANNOT_RECOVER;
471 switch (pdu->status) {
472 case ISCSI_PDU_NOT_RECEIVED:
473 case ISCSI_PDU_CRC_FAILED:
474 case ISCSI_PDU_TIMED_OUT:
476 case ISCSI_PDU_RECEIVED_OK:
477 pr_err("Command ITT: 0x%08x received already gotten"
478 " Offset: %u, Length: %u\n", cmd->init_task_tag,
479 be32_to_cpu(hdr->offset), payload_length);
480 return iscsit_dump_data_payload(cmd->conn, payload_length, 1);
482 return DATAOUT_CANNOT_RECOVER;
485 return DATAOUT_NORMAL;
488 static int iscsit_dataout_update_r2t(struct iscsi_cmd *cmd, u32 offset, u32 length)
490 struct iscsi_r2t *r2t;
492 if (cmd->unsolicited_data)
495 r2t = iscsit_get_r2t_for_eos(cmd, offset, length);
499 spin_lock_bh(&cmd->r2t_lock);
500 r2t->seq_complete = 1;
501 cmd->outstanding_r2ts--;
502 spin_unlock_bh(&cmd->r2t_lock);
507 static int iscsit_dataout_update_datapduinorder_no(
508 struct iscsi_cmd *cmd,
513 struct iscsi_pdu *pdu = cmd->pdu_ptr;
515 pdu->data_sn = data_sn;
517 switch (pdu->status) {
518 case ISCSI_PDU_NOT_RECEIVED:
519 pdu->status = ISCSI_PDU_RECEIVED_OK;
521 case ISCSI_PDU_CRC_FAILED:
522 pdu->status = ISCSI_PDU_RECEIVED_OK;
524 case ISCSI_PDU_TIMED_OUT:
525 pdu->status = ISCSI_PDU_RECEIVED_OK;
528 return DATAOUT_CANNOT_RECOVER;
532 ret = iscsit_dataout_datapduinorder_no_fbit(cmd, pdu);
533 if (ret == DATAOUT_CANNOT_RECOVER)
537 return DATAOUT_NORMAL;
540 static int iscsit_dataout_post_crc_passed(
541 struct iscsi_cmd *cmd,
544 int ret, send_r2t = 0;
545 struct iscsi_conn *conn = cmd->conn;
546 struct iscsi_seq *seq = NULL;
547 struct iscsi_data *hdr = (struct iscsi_data *) buf;
548 u32 payload_length = ntoh24(hdr->dlength);
550 if (cmd->unsolicited_data) {
551 if ((cmd->first_burst_len + payload_length) ==
552 conn->sess->sess_ops->FirstBurstLength) {
553 if (iscsit_dataout_update_r2t(cmd, be32_to_cpu(hdr->offset),
555 return DATAOUT_CANNOT_RECOVER;
559 if (!conn->sess->sess_ops->DataPDUInOrder) {
560 ret = iscsit_dataout_update_datapduinorder_no(cmd,
561 be32_to_cpu(hdr->datasn),
562 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
563 if (ret == DATAOUT_CANNOT_RECOVER)
567 cmd->first_burst_len += payload_length;
569 if (conn->sess->sess_ops->DataSequenceInOrder)
574 seq->offset += payload_length;
579 seq->status = DATAOUT_SEQUENCE_COMPLETE;
580 cmd->first_burst_len = 0;
581 cmd->unsolicited_data = 0;
584 if (conn->sess->sess_ops->DataSequenceInOrder) {
585 if ((cmd->next_burst_len + payload_length) ==
586 conn->sess->sess_ops->MaxBurstLength) {
587 if (iscsit_dataout_update_r2t(cmd,
588 be32_to_cpu(hdr->offset),
590 return DATAOUT_CANNOT_RECOVER;
594 if (!conn->sess->sess_ops->DataPDUInOrder) {
595 ret = iscsit_dataout_update_datapduinorder_no(
596 cmd, be32_to_cpu(hdr->datasn),
597 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
598 if (ret == DATAOUT_CANNOT_RECOVER)
602 cmd->next_burst_len += payload_length;
606 cmd->next_burst_len = 0;
610 if ((seq->next_burst_len + payload_length) ==
612 if (iscsit_dataout_update_r2t(cmd,
613 be32_to_cpu(hdr->offset),
615 return DATAOUT_CANNOT_RECOVER;
619 if (!conn->sess->sess_ops->DataPDUInOrder) {
620 ret = iscsit_dataout_update_datapduinorder_no(
621 cmd, be32_to_cpu(hdr->datasn),
622 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
623 if (ret == DATAOUT_CANNOT_RECOVER)
628 seq->offset += payload_length;
629 seq->next_burst_len += payload_length;
632 seq->next_burst_len = 0;
633 seq->status = DATAOUT_SEQUENCE_COMPLETE;
638 if (send_r2t && conn->sess->sess_ops->DataSequenceInOrder)
641 cmd->write_data_done += payload_length;
643 if (cmd->write_data_done == cmd->se_cmd.data_length)
644 return DATAOUT_SEND_TO_TRANSPORT;
646 return DATAOUT_SEND_R2T;
648 return DATAOUT_NORMAL;
651 static int iscsit_dataout_post_crc_failed(
652 struct iscsi_cmd *cmd,
655 struct iscsi_conn *conn = cmd->conn;
656 struct iscsi_pdu *pdu;
657 struct iscsi_data *hdr = (struct iscsi_data *) buf;
658 u32 payload_length = ntoh24(hdr->dlength);
660 if (conn->sess->sess_ops->DataPDUInOrder)
663 * The rest of this function is only called when DataPDUInOrder=No.
667 switch (pdu->status) {
668 case ISCSI_PDU_NOT_RECEIVED:
669 pdu->status = ISCSI_PDU_CRC_FAILED;
671 case ISCSI_PDU_CRC_FAILED:
673 case ISCSI_PDU_TIMED_OUT:
674 pdu->status = ISCSI_PDU_CRC_FAILED;
677 return DATAOUT_CANNOT_RECOVER;
681 return iscsit_recover_dataout_sequence(cmd, be32_to_cpu(hdr->offset),
686 * Called from iscsit_handle_data_out() before DataOUT Payload is received
689 int iscsit_check_pre_dataout(
690 struct iscsi_cmd *cmd,
694 struct iscsi_conn *conn = cmd->conn;
696 ret = iscsit_dataout_within_command_recovery_check(cmd, buf);
697 if ((ret == DATAOUT_WITHIN_COMMAND_RECOVERY) ||
698 (ret == DATAOUT_CANNOT_RECOVER))
701 ret = iscsit_dataout_check_datasn(cmd, buf);
702 if ((ret == DATAOUT_WITHIN_COMMAND_RECOVERY) ||
703 (ret == DATAOUT_CANNOT_RECOVER))
706 if (cmd->unsolicited_data) {
707 ret = iscsit_dataout_check_unsolicited_sequence(cmd, buf);
708 if ((ret == DATAOUT_WITHIN_COMMAND_RECOVERY) ||
709 (ret == DATAOUT_CANNOT_RECOVER))
712 ret = iscsit_dataout_check_sequence(cmd, buf);
713 if ((ret == DATAOUT_WITHIN_COMMAND_RECOVERY) ||
714 (ret == DATAOUT_CANNOT_RECOVER))
718 return (conn->sess->sess_ops->DataPDUInOrder) ?
719 iscsit_dataout_pre_datapduinorder_yes(cmd, buf) :
720 iscsit_dataout_pre_datapduinorder_no(cmd, buf);
724 * Called from iscsit_handle_data_out() after DataOUT Payload is received
727 int iscsit_check_post_dataout(
728 struct iscsi_cmd *cmd,
732 struct iscsi_conn *conn = cmd->conn;
734 cmd->dataout_timeout_retries = 0;
736 if (!data_crc_failed)
737 return iscsit_dataout_post_crc_passed(cmd, buf);
739 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
740 pr_err("Unable to recover from DataOUT CRC"
741 " failure while ERL=0, closing session.\n");
742 iscsit_reject_cmd(cmd, ISCSI_REASON_DATA_DIGEST_ERROR,
744 return DATAOUT_CANNOT_RECOVER;
747 iscsit_reject_cmd(cmd, ISCSI_REASON_DATA_DIGEST_ERROR, buf);
748 return iscsit_dataout_post_crc_failed(cmd, buf);
752 static void iscsit_handle_time2retain_timeout(unsigned long data)
754 struct iscsi_session *sess = (struct iscsi_session *) data;
755 struct iscsi_portal_group *tpg = sess->tpg;
756 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
758 spin_lock_bh(&se_tpg->session_lock);
759 if (sess->time2retain_timer_flags & ISCSI_TF_STOP) {
760 spin_unlock_bh(&se_tpg->session_lock);
763 if (atomic_read(&sess->session_reinstatement)) {
764 pr_err("Exiting Time2Retain handler because"
765 " session_reinstatement=1\n");
766 spin_unlock_bh(&se_tpg->session_lock);
769 sess->time2retain_timer_flags |= ISCSI_TF_EXPIRED;
771 pr_err("Time2Retain timer expired for SID: %u, cleaning up"
772 " iSCSI session.\n", sess->sid);
774 struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
777 spin_lock(&tiqn->sess_err_stats.lock);
778 strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
779 (void *)sess->sess_ops->InitiatorName);
780 tiqn->sess_err_stats.last_sess_failure_type =
781 ISCSI_SESS_ERR_CXN_TIMEOUT;
782 tiqn->sess_err_stats.cxn_timeout_errors++;
783 atomic_long_inc(&sess->conn_timeout_errors);
784 spin_unlock(&tiqn->sess_err_stats.lock);
788 spin_unlock_bh(&se_tpg->session_lock);
789 target_put_session(sess->se_sess);
792 void iscsit_start_time2retain_handler(struct iscsi_session *sess)
796 * Only start Time2Retain timer when the associated TPG is still in
797 * an ACTIVE (eg: not disabled or shutdown) state.
799 spin_lock(&sess->tpg->tpg_state_lock);
800 tpg_active = (sess->tpg->tpg_state == TPG_STATE_ACTIVE);
801 spin_unlock(&sess->tpg->tpg_state_lock);
806 if (sess->time2retain_timer_flags & ISCSI_TF_RUNNING)
809 pr_debug("Starting Time2Retain timer for %u seconds on"
810 " SID: %u\n", sess->sess_ops->DefaultTime2Retain, sess->sid);
812 init_timer(&sess->time2retain_timer);
813 sess->time2retain_timer.expires =
814 (get_jiffies_64() + sess->sess_ops->DefaultTime2Retain * HZ);
815 sess->time2retain_timer.data = (unsigned long)sess;
816 sess->time2retain_timer.function = iscsit_handle_time2retain_timeout;
817 sess->time2retain_timer_flags &= ~ISCSI_TF_STOP;
818 sess->time2retain_timer_flags |= ISCSI_TF_RUNNING;
819 add_timer(&sess->time2retain_timer);
823 * Called with spin_lock_bh(&struct se_portal_group->session_lock) held
825 int iscsit_stop_time2retain_timer(struct iscsi_session *sess)
827 struct iscsi_portal_group *tpg = sess->tpg;
828 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
830 if (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)
833 if (!(sess->time2retain_timer_flags & ISCSI_TF_RUNNING))
836 sess->time2retain_timer_flags |= ISCSI_TF_STOP;
837 spin_unlock(&se_tpg->session_lock);
839 del_timer_sync(&sess->time2retain_timer);
841 spin_lock(&se_tpg->session_lock);
842 sess->time2retain_timer_flags &= ~ISCSI_TF_RUNNING;
843 pr_debug("Stopped Time2Retain Timer for SID: %u\n",
848 void iscsit_connection_reinstatement_rcfr(struct iscsi_conn *conn)
850 spin_lock_bh(&conn->state_lock);
851 if (atomic_read(&conn->connection_exit)) {
852 spin_unlock_bh(&conn->state_lock);
856 if (atomic_read(&conn->transport_failed)) {
857 spin_unlock_bh(&conn->state_lock);
860 spin_unlock_bh(&conn->state_lock);
862 if (conn->tx_thread && conn->tx_thread_active)
863 send_sig(SIGINT, conn->tx_thread, 1);
864 if (conn->rx_thread && conn->rx_thread_active)
865 send_sig(SIGINT, conn->rx_thread, 1);
868 wait_for_completion(&conn->conn_wait_rcfr_comp);
869 complete(&conn->conn_post_wait_comp);
872 void iscsit_cause_connection_reinstatement(struct iscsi_conn *conn, int sleep)
874 spin_lock_bh(&conn->state_lock);
875 if (atomic_read(&conn->connection_exit)) {
876 spin_unlock_bh(&conn->state_lock);
880 if (atomic_read(&conn->transport_failed)) {
881 spin_unlock_bh(&conn->state_lock);
885 if (atomic_read(&conn->connection_reinstatement)) {
886 spin_unlock_bh(&conn->state_lock);
890 if (conn->tx_thread && conn->tx_thread_active)
891 send_sig(SIGINT, conn->tx_thread, 1);
892 if (conn->rx_thread && conn->rx_thread_active)
893 send_sig(SIGINT, conn->rx_thread, 1);
895 atomic_set(&conn->connection_reinstatement, 1);
897 spin_unlock_bh(&conn->state_lock);
901 atomic_set(&conn->sleep_on_conn_wait_comp, 1);
902 spin_unlock_bh(&conn->state_lock);
904 wait_for_completion(&conn->conn_wait_comp);
905 complete(&conn->conn_post_wait_comp);
907 EXPORT_SYMBOL(iscsit_cause_connection_reinstatement);
909 void iscsit_fall_back_to_erl0(struct iscsi_session *sess)
911 pr_debug("Falling back to ErrorRecoveryLevel=0 for SID:"
914 atomic_set(&sess->session_fall_back_to_erl0, 1);
917 static void iscsit_handle_connection_cleanup(struct iscsi_conn *conn)
919 struct iscsi_session *sess = conn->sess;
921 if ((sess->sess_ops->ErrorRecoveryLevel == 2) &&
922 !atomic_read(&sess->session_reinstatement) &&
923 !atomic_read(&sess->session_fall_back_to_erl0))
924 iscsit_connection_recovery_transport_reset(conn);
926 pr_debug("Performing cleanup for failed iSCSI"
927 " Connection ID: %hu from %s\n", conn->cid,
928 sess->sess_ops->InitiatorName);
929 iscsit_close_connection(conn);
933 void iscsit_take_action_for_connection_exit(struct iscsi_conn *conn)
935 spin_lock_bh(&conn->state_lock);
936 if (atomic_read(&conn->connection_exit)) {
937 spin_unlock_bh(&conn->state_lock);
940 atomic_set(&conn->connection_exit, 1);
942 if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) {
943 spin_unlock_bh(&conn->state_lock);
944 iscsit_close_connection(conn);
948 if (conn->conn_state == TARG_CONN_STATE_CLEANUP_WAIT) {
949 spin_unlock_bh(&conn->state_lock);
953 pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
954 conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
955 spin_unlock_bh(&conn->state_lock);
957 iscsit_handle_connection_cleanup(conn);
961 * This is the simple function that makes the magic of
962 * sync and steering happen in the follow paradoxical order:
964 * 0) Receive conn->of_marker (bytes left until next OFMarker)
965 * bytes into an offload buffer. When we pass the exact number
966 * of bytes in conn->of_marker, iscsit_dump_data_payload() and hence
967 * rx_data() will automatically receive the identical u32 marker
968 * values and store it in conn->of_marker_offset;
969 * 1) Now conn->of_marker_offset will contain the offset to the start
970 * of the next iSCSI PDU. Dump these remaining bytes into another
973 * Next byte in the TCP stream will contain the next iSCSI PDU!
976 int iscsit_recover_from_unknown_opcode(struct iscsi_conn *conn)
979 * Make sure the remaining bytes to next maker is a sane value.
981 if (conn->of_marker > (conn->conn_ops->OFMarkInt * 4)) {
982 pr_err("Remaining bytes to OFMarker: %u exceeds"
983 " OFMarkInt bytes: %u.\n", conn->of_marker,
984 conn->conn_ops->OFMarkInt * 4);
988 pr_debug("Advancing %u bytes in TCP stream to get to the"
989 " next OFMarker.\n", conn->of_marker);
991 if (iscsit_dump_data_payload(conn, conn->of_marker, 0) < 0)
995 * Make sure the offset marker we retrived is a valid value.
997 if (conn->of_marker_offset > (ISCSI_HDR_LEN + (ISCSI_CRC_LEN * 2) +
998 conn->conn_ops->MaxRecvDataSegmentLength)) {
999 pr_err("OfMarker offset value: %u exceeds limit.\n",
1000 conn->of_marker_offset);
1004 pr_debug("Discarding %u bytes of TCP stream to get to the"
1005 " next iSCSI Opcode.\n", conn->of_marker_offset);
1007 if (iscsit_dump_data_payload(conn, conn->of_marker_offset, 0) < 0)