Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jack/linux-fs
[sfrench/cifs-2.6.git] / drivers / target / iscsi / iscsi_target_erl0.c
1 /******************************************************************************
2  * This file contains error recovery level zero functions used by
3  * the iSCSI Target driver.
4  *
5  * (c) Copyright 2007-2013 Datera, Inc.
6  *
7  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8  *
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.
13  *
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  ******************************************************************************/
19
20 #include <scsi/iscsi_proto.h>
21 #include <target/target_core_base.h>
22 #include <target/target_core_fabric.h>
23
24 #include "iscsi_target_core.h"
25 #include "iscsi_target_seq_pdu_list.h"
26 #include "iscsi_target_tq.h"
27 #include "iscsi_target_erl0.h"
28 #include "iscsi_target_erl1.h"
29 #include "iscsi_target_erl2.h"
30 #include "iscsi_target_util.h"
31 #include "iscsi_target.h"
32
33 /*
34  *      Used to set values in struct iscsi_cmd that iscsit_dataout_check_sequence()
35  *      checks against to determine a PDU's Offset+Length is within the current
36  *      DataOUT Sequence.  Used for DataSequenceInOrder=Yes only.
37  */
38 void iscsit_set_dataout_sequence_values(
39         struct iscsi_cmd *cmd)
40 {
41         struct iscsi_conn *conn = cmd->conn;
42         /*
43          * Still set seq_start_offset and seq_end_offset for Unsolicited
44          * DataOUT, even if DataSequenceInOrder=No.
45          */
46         if (cmd->unsolicited_data) {
47                 cmd->seq_start_offset = cmd->write_data_done;
48                 cmd->seq_end_offset = (cmd->write_data_done +
49                         ((cmd->se_cmd.data_length >
50                           conn->sess->sess_ops->FirstBurstLength) ?
51                          conn->sess->sess_ops->FirstBurstLength : cmd->se_cmd.data_length));
52                 return;
53         }
54
55         if (!conn->sess->sess_ops->DataSequenceInOrder)
56                 return;
57
58         if (!cmd->seq_start_offset && !cmd->seq_end_offset) {
59                 cmd->seq_start_offset = cmd->write_data_done;
60                 cmd->seq_end_offset = (cmd->se_cmd.data_length >
61                         conn->sess->sess_ops->MaxBurstLength) ?
62                         (cmd->write_data_done +
63                         conn->sess->sess_ops->MaxBurstLength) : cmd->se_cmd.data_length;
64         } else {
65                 cmd->seq_start_offset = cmd->seq_end_offset;
66                 cmd->seq_end_offset = ((cmd->seq_end_offset +
67                         conn->sess->sess_ops->MaxBurstLength) >=
68                         cmd->se_cmd.data_length) ? cmd->se_cmd.data_length :
69                         (cmd->seq_end_offset +
70                          conn->sess->sess_ops->MaxBurstLength);
71         }
72 }
73
74 static int iscsit_dataout_within_command_recovery_check(
75         struct iscsi_cmd *cmd,
76         unsigned char *buf)
77 {
78         struct iscsi_conn *conn = cmd->conn;
79         struct iscsi_data *hdr = (struct iscsi_data *) buf;
80         u32 payload_length = ntoh24(hdr->dlength);
81
82         /*
83          * We do the within-command recovery checks here as it is
84          * the first function called in iscsi_check_pre_dataout().
85          * Basically, if we are in within-command recovery and
86          * the PDU does not contain the offset the sequence needs,
87          * dump the payload.
88          *
89          * This only applies to DataPDUInOrder=Yes, for
90          * DataPDUInOrder=No we only re-request the failed PDU
91          * and check that all PDUs in a sequence are received
92          * upon end of sequence.
93          */
94         if (conn->sess->sess_ops->DataSequenceInOrder) {
95                 if ((cmd->cmd_flags & ICF_WITHIN_COMMAND_RECOVERY) &&
96                     cmd->write_data_done != be32_to_cpu(hdr->offset))
97                         goto dump;
98
99                 cmd->cmd_flags &= ~ICF_WITHIN_COMMAND_RECOVERY;
100         } else {
101                 struct iscsi_seq *seq;
102
103                 seq = iscsit_get_seq_holder(cmd, be32_to_cpu(hdr->offset),
104                                             payload_length);
105                 if (!seq)
106                         return DATAOUT_CANNOT_RECOVER;
107                 /*
108                  * Set the struct iscsi_seq pointer to reuse later.
109                  */
110                 cmd->seq_ptr = seq;
111
112                 if (conn->sess->sess_ops->DataPDUInOrder) {
113                         if (seq->status ==
114                             DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY &&
115                            (seq->offset != be32_to_cpu(hdr->offset) ||
116                             seq->data_sn != be32_to_cpu(hdr->datasn)))
117                                 goto dump;
118                 } else {
119                         if (seq->status ==
120                              DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY &&
121                             seq->data_sn != be32_to_cpu(hdr->datasn))
122                                 goto dump;
123                 }
124
125                 if (seq->status == DATAOUT_SEQUENCE_COMPLETE)
126                         goto dump;
127
128                 if (seq->status != DATAOUT_SEQUENCE_COMPLETE)
129                         seq->status = 0;
130         }
131
132         return DATAOUT_NORMAL;
133
134 dump:
135         pr_err("Dumping DataOUT PDU Offset: %u Length: %d DataSN:"
136                 " 0x%08x\n", hdr->offset, payload_length, hdr->datasn);
137         return iscsit_dump_data_payload(conn, payload_length, 1);
138 }
139
140 static int iscsit_dataout_check_unsolicited_sequence(
141         struct iscsi_cmd *cmd,
142         unsigned char *buf)
143 {
144         u32 first_burst_len;
145         struct iscsi_conn *conn = cmd->conn;
146         struct iscsi_data *hdr = (struct iscsi_data *) buf;
147         u32 payload_length = ntoh24(hdr->dlength);
148
149
150         if ((be32_to_cpu(hdr->offset) < cmd->seq_start_offset) ||
151            ((be32_to_cpu(hdr->offset) + payload_length) > cmd->seq_end_offset)) {
152                 pr_err("Command ITT: 0x%08x with Offset: %u,"
153                 " Length: %u outside of Unsolicited Sequence %u:%u while"
154                 " DataSequenceInOrder=Yes.\n", cmd->init_task_tag,
155                 be32_to_cpu(hdr->offset), payload_length, cmd->seq_start_offset,
156                         cmd->seq_end_offset);
157                 return DATAOUT_CANNOT_RECOVER;
158         }
159
160         first_burst_len = (cmd->first_burst_len + payload_length);
161
162         if (first_burst_len > conn->sess->sess_ops->FirstBurstLength) {
163                 pr_err("Total %u bytes exceeds FirstBurstLength: %u"
164                         " for this Unsolicited DataOut Burst.\n",
165                         first_burst_len, conn->sess->sess_ops->FirstBurstLength);
166                 transport_send_check_condition_and_sense(&cmd->se_cmd,
167                                 TCM_INCORRECT_AMOUNT_OF_DATA, 0);
168                 return DATAOUT_CANNOT_RECOVER;
169         }
170
171         /*
172          * Perform various MaxBurstLength and ISCSI_FLAG_CMD_FINAL sanity
173          * checks for the current Unsolicited DataOUT Sequence.
174          */
175         if (hdr->flags & ISCSI_FLAG_CMD_FINAL) {
176                 /*
177                  * Ignore ISCSI_FLAG_CMD_FINAL checks while DataPDUInOrder=No, end of
178                  * sequence checks are handled in
179                  * iscsit_dataout_datapduinorder_no_fbit().
180                  */
181                 if (!conn->sess->sess_ops->DataPDUInOrder)
182                         goto out;
183
184                 if ((first_burst_len != cmd->se_cmd.data_length) &&
185                     (first_burst_len != conn->sess->sess_ops->FirstBurstLength)) {
186                         pr_err("Unsolicited non-immediate data"
187                         " received %u does not equal FirstBurstLength: %u, and"
188                         " does not equal ExpXferLen %u.\n", first_burst_len,
189                                 conn->sess->sess_ops->FirstBurstLength,
190                                 cmd->se_cmd.data_length);
191                         transport_send_check_condition_and_sense(&cmd->se_cmd,
192                                         TCM_INCORRECT_AMOUNT_OF_DATA, 0);
193                         return DATAOUT_CANNOT_RECOVER;
194                 }
195         } else {
196                 if (first_burst_len == conn->sess->sess_ops->FirstBurstLength) {
197                         pr_err("Command ITT: 0x%08x reached"
198                         " FirstBurstLength: %u, but ISCSI_FLAG_CMD_FINAL is not set. protocol"
199                                 " error.\n", cmd->init_task_tag,
200                                 conn->sess->sess_ops->FirstBurstLength);
201                         return DATAOUT_CANNOT_RECOVER;
202                 }
203                 if (first_burst_len == cmd->se_cmd.data_length) {
204                         pr_err("Command ITT: 0x%08x reached"
205                         " ExpXferLen: %u, but ISCSI_FLAG_CMD_FINAL is not set. protocol"
206                         " error.\n", cmd->init_task_tag, cmd->se_cmd.data_length);
207                         return DATAOUT_CANNOT_RECOVER;
208                 }
209         }
210
211 out:
212         return DATAOUT_NORMAL;
213 }
214
215 static int iscsit_dataout_check_sequence(
216         struct iscsi_cmd *cmd,
217         unsigned char *buf)
218 {
219         u32 next_burst_len;
220         struct iscsi_conn *conn = cmd->conn;
221         struct iscsi_seq *seq = NULL;
222         struct iscsi_data *hdr = (struct iscsi_data *) buf;
223         u32 payload_length = ntoh24(hdr->dlength);
224
225         /*
226          * For DataSequenceInOrder=Yes: Check that the offset and offset+length
227          * is within range as defined by iscsi_set_dataout_sequence_values().
228          *
229          * For DataSequenceInOrder=No: Check that an struct iscsi_seq exists for
230          * offset+length tuple.
231          */
232         if (conn->sess->sess_ops->DataSequenceInOrder) {
233                 /*
234                  * Due to possibility of recovery DataOUT sent by the initiator
235                  * fullfilling an Recovery R2T, it's best to just dump the
236                  * payload here, instead of erroring out.
237                  */
238                 if ((be32_to_cpu(hdr->offset) < cmd->seq_start_offset) ||
239                    ((be32_to_cpu(hdr->offset) + payload_length) > cmd->seq_end_offset)) {
240                         pr_err("Command ITT: 0x%08x with Offset: %u,"
241                         " Length: %u outside of Sequence %u:%u while"
242                         " DataSequenceInOrder=Yes.\n", cmd->init_task_tag,
243                         be32_to_cpu(hdr->offset), payload_length, cmd->seq_start_offset,
244                                 cmd->seq_end_offset);
245
246                         if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
247                                 return DATAOUT_CANNOT_RECOVER;
248                         return DATAOUT_WITHIN_COMMAND_RECOVERY;
249                 }
250
251                 next_burst_len = (cmd->next_burst_len + payload_length);
252         } else {
253                 seq = iscsit_get_seq_holder(cmd, be32_to_cpu(hdr->offset),
254                                             payload_length);
255                 if (!seq)
256                         return DATAOUT_CANNOT_RECOVER;
257                 /*
258                  * Set the struct iscsi_seq pointer to reuse later.
259                  */
260                 cmd->seq_ptr = seq;
261
262                 if (seq->status == DATAOUT_SEQUENCE_COMPLETE) {
263                         if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
264                                 return DATAOUT_CANNOT_RECOVER;
265                         return DATAOUT_WITHIN_COMMAND_RECOVERY;
266                 }
267
268                 next_burst_len = (seq->next_burst_len + payload_length);
269         }
270
271         if (next_burst_len > conn->sess->sess_ops->MaxBurstLength) {
272                 pr_err("Command ITT: 0x%08x, NextBurstLength: %u and"
273                         " Length: %u exceeds MaxBurstLength: %u. protocol"
274                         " error.\n", cmd->init_task_tag,
275                         (next_burst_len - payload_length),
276                         payload_length, conn->sess->sess_ops->MaxBurstLength);
277                 return DATAOUT_CANNOT_RECOVER;
278         }
279
280         /*
281          * Perform various MaxBurstLength and ISCSI_FLAG_CMD_FINAL sanity
282          * checks for the current DataOUT Sequence.
283          */
284         if (hdr->flags & ISCSI_FLAG_CMD_FINAL) {
285                 /*
286                  * Ignore ISCSI_FLAG_CMD_FINAL checks while DataPDUInOrder=No, end of
287                  * sequence checks are handled in
288                  * iscsit_dataout_datapduinorder_no_fbit().
289                  */
290                 if (!conn->sess->sess_ops->DataPDUInOrder)
291                         goto out;
292
293                 if (conn->sess->sess_ops->DataSequenceInOrder) {
294                         if ((next_burst_len <
295                              conn->sess->sess_ops->MaxBurstLength) &&
296                            ((cmd->write_data_done + payload_length) <
297                              cmd->se_cmd.data_length)) {
298                                 pr_err("Command ITT: 0x%08x set ISCSI_FLAG_CMD_FINAL"
299                                 " before end of DataOUT sequence, protocol"
300                                 " error.\n", cmd->init_task_tag);
301                                 return DATAOUT_CANNOT_RECOVER;
302                         }
303                 } else {
304                         if (next_burst_len < seq->xfer_len) {
305                                 pr_err("Command ITT: 0x%08x set ISCSI_FLAG_CMD_FINAL"
306                                 " before end of DataOUT sequence, protocol"
307                                 " error.\n", cmd->init_task_tag);
308                                 return DATAOUT_CANNOT_RECOVER;
309                         }
310                 }
311         } else {
312                 if (conn->sess->sess_ops->DataSequenceInOrder) {
313                         if (next_burst_len ==
314                                         conn->sess->sess_ops->MaxBurstLength) {
315                                 pr_err("Command ITT: 0x%08x reached"
316                                 " MaxBurstLength: %u, but ISCSI_FLAG_CMD_FINAL is"
317                                 " not set, protocol error.", cmd->init_task_tag,
318                                         conn->sess->sess_ops->MaxBurstLength);
319                                 return DATAOUT_CANNOT_RECOVER;
320                         }
321                         if ((cmd->write_data_done + payload_length) ==
322                                         cmd->se_cmd.data_length) {
323                                 pr_err("Command ITT: 0x%08x reached"
324                                 " last DataOUT PDU in sequence but ISCSI_FLAG_"
325                                 "CMD_FINAL is not set, protocol error.\n",
326                                         cmd->init_task_tag);
327                                 return DATAOUT_CANNOT_RECOVER;
328                         }
329                 } else {
330                         if (next_burst_len == seq->xfer_len) {
331                                 pr_err("Command ITT: 0x%08x reached"
332                                 " last DataOUT PDU in sequence but ISCSI_FLAG_"
333                                 "CMD_FINAL is not set, protocol error.\n",
334                                         cmd->init_task_tag);
335                                 return DATAOUT_CANNOT_RECOVER;
336                         }
337                 }
338         }
339
340 out:
341         return DATAOUT_NORMAL;
342 }
343
344 static int iscsit_dataout_check_datasn(
345         struct iscsi_cmd *cmd,
346         unsigned char *buf)
347 {
348         u32 data_sn = 0;
349         struct iscsi_conn *conn = cmd->conn;
350         struct iscsi_data *hdr = (struct iscsi_data *) buf;
351         u32 payload_length = ntoh24(hdr->dlength);
352
353         /*
354          * Considering the target has no method of re-requesting DataOUT
355          * by DataSN, if we receieve a greater DataSN than expected we
356          * assume the functions for DataPDUInOrder=[Yes,No] below will
357          * handle it.
358          *
359          * If the DataSN is less than expected, dump the payload.
360          */
361         if (conn->sess->sess_ops->DataSequenceInOrder)
362                 data_sn = cmd->data_sn;
363         else {
364                 struct iscsi_seq *seq = cmd->seq_ptr;
365                 data_sn = seq->data_sn;
366         }
367
368         if (be32_to_cpu(hdr->datasn) > data_sn) {
369                 pr_err("Command ITT: 0x%08x, received DataSN: 0x%08x"
370                         " higher than expected 0x%08x.\n", cmd->init_task_tag,
371                                 be32_to_cpu(hdr->datasn), data_sn);
372                 goto recover;
373         } else if (be32_to_cpu(hdr->datasn) < data_sn) {
374                 pr_err("Command ITT: 0x%08x, received DataSN: 0x%08x"
375                         " lower than expected 0x%08x, discarding payload.\n",
376                         cmd->init_task_tag, be32_to_cpu(hdr->datasn), data_sn);
377                 goto dump;
378         }
379
380         return DATAOUT_NORMAL;
381
382 recover:
383         if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
384                 pr_err("Unable to perform within-command recovery"
385                                 " while ERL=0.\n");
386                 return DATAOUT_CANNOT_RECOVER;
387         }
388 dump:
389         if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
390                 return DATAOUT_CANNOT_RECOVER;
391
392         return DATAOUT_WITHIN_COMMAND_RECOVERY;
393 }
394
395 static int iscsit_dataout_pre_datapduinorder_yes(
396         struct iscsi_cmd *cmd,
397         unsigned char *buf)
398 {
399         int dump = 0, recovery = 0;
400         struct iscsi_conn *conn = cmd->conn;
401         struct iscsi_data *hdr = (struct iscsi_data *) buf;
402         u32 payload_length = ntoh24(hdr->dlength);
403
404         /*
405          * For DataSequenceInOrder=Yes: If the offset is greater than the global
406          * DataPDUInOrder=Yes offset counter in struct iscsi_cmd a protcol error has
407          * occurred and fail the connection.
408          *
409          * For DataSequenceInOrder=No: If the offset is greater than the per
410          * sequence DataPDUInOrder=Yes offset counter in struct iscsi_seq a protocol
411          * error has occurred and fail the connection.
412          */
413         if (conn->sess->sess_ops->DataSequenceInOrder) {
414                 if (be32_to_cpu(hdr->offset) != cmd->write_data_done) {
415                         pr_err("Command ITT: 0x%08x, received offset"
416                         " %u different than expected %u.\n", cmd->init_task_tag,
417                                 be32_to_cpu(hdr->offset), cmd->write_data_done);
418                         recovery = 1;
419                         goto recover;
420                 }
421         } else {
422                 struct iscsi_seq *seq = cmd->seq_ptr;
423
424                 if (be32_to_cpu(hdr->offset) > seq->offset) {
425                         pr_err("Command ITT: 0x%08x, received offset"
426                         " %u greater than expected %u.\n", cmd->init_task_tag,
427                                 be32_to_cpu(hdr->offset), seq->offset);
428                         recovery = 1;
429                         goto recover;
430                 } else if (be32_to_cpu(hdr->offset) < seq->offset) {
431                         pr_err("Command ITT: 0x%08x, received offset"
432                         " %u less than expected %u, discarding payload.\n",
433                                 cmd->init_task_tag, be32_to_cpu(hdr->offset),
434                                 seq->offset);
435                         dump = 1;
436                         goto dump;
437                 }
438         }
439
440         return DATAOUT_NORMAL;
441
442 recover:
443         if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
444                 pr_err("Unable to perform within-command recovery"
445                                 " while ERL=0.\n");
446                 return DATAOUT_CANNOT_RECOVER;
447         }
448 dump:
449         if (iscsit_dump_data_payload(conn, payload_length, 1) < 0)
450                 return DATAOUT_CANNOT_RECOVER;
451
452         return (recovery) ? iscsit_recover_dataout_sequence(cmd,
453                 be32_to_cpu(hdr->offset), payload_length) :
454                (dump) ? DATAOUT_WITHIN_COMMAND_RECOVERY : DATAOUT_NORMAL;
455 }
456
457 static int iscsit_dataout_pre_datapduinorder_no(
458         struct iscsi_cmd *cmd,
459         unsigned char *buf)
460 {
461         struct iscsi_pdu *pdu;
462         struct iscsi_data *hdr = (struct iscsi_data *) buf;
463         u32 payload_length = ntoh24(hdr->dlength);
464
465         pdu = iscsit_get_pdu_holder(cmd, be32_to_cpu(hdr->offset),
466                                     payload_length);
467         if (!pdu)
468                 return DATAOUT_CANNOT_RECOVER;
469
470         cmd->pdu_ptr = pdu;
471
472         switch (pdu->status) {
473         case ISCSI_PDU_NOT_RECEIVED:
474         case ISCSI_PDU_CRC_FAILED:
475         case ISCSI_PDU_TIMED_OUT:
476                 break;
477         case ISCSI_PDU_RECEIVED_OK:
478                 pr_err("Command ITT: 0x%08x received already gotten"
479                         " Offset: %u, Length: %u\n", cmd->init_task_tag,
480                                 be32_to_cpu(hdr->offset), payload_length);
481                 return iscsit_dump_data_payload(cmd->conn, payload_length, 1);
482         default:
483                 return DATAOUT_CANNOT_RECOVER;
484         }
485
486         return DATAOUT_NORMAL;
487 }
488
489 static int iscsit_dataout_update_r2t(struct iscsi_cmd *cmd, u32 offset, u32 length)
490 {
491         struct iscsi_r2t *r2t;
492
493         if (cmd->unsolicited_data)
494                 return 0;
495
496         r2t = iscsit_get_r2t_for_eos(cmd, offset, length);
497         if (!r2t)
498                 return -1;
499
500         spin_lock_bh(&cmd->r2t_lock);
501         r2t->seq_complete = 1;
502         cmd->outstanding_r2ts--;
503         spin_unlock_bh(&cmd->r2t_lock);
504
505         return 0;
506 }
507
508 static int iscsit_dataout_update_datapduinorder_no(
509         struct iscsi_cmd *cmd,
510         u32 data_sn,
511         int f_bit)
512 {
513         int ret = 0;
514         struct iscsi_pdu *pdu = cmd->pdu_ptr;
515
516         pdu->data_sn = data_sn;
517
518         switch (pdu->status) {
519         case ISCSI_PDU_NOT_RECEIVED:
520                 pdu->status = ISCSI_PDU_RECEIVED_OK;
521                 break;
522         case ISCSI_PDU_CRC_FAILED:
523                 pdu->status = ISCSI_PDU_RECEIVED_OK;
524                 break;
525         case ISCSI_PDU_TIMED_OUT:
526                 pdu->status = ISCSI_PDU_RECEIVED_OK;
527                 break;
528         default:
529                 return DATAOUT_CANNOT_RECOVER;
530         }
531
532         if (f_bit) {
533                 ret = iscsit_dataout_datapduinorder_no_fbit(cmd, pdu);
534                 if (ret == DATAOUT_CANNOT_RECOVER)
535                         return ret;
536         }
537
538         return DATAOUT_NORMAL;
539 }
540
541 static int iscsit_dataout_post_crc_passed(
542         struct iscsi_cmd *cmd,
543         unsigned char *buf)
544 {
545         int ret, send_r2t = 0;
546         struct iscsi_conn *conn = cmd->conn;
547         struct iscsi_seq *seq = NULL;
548         struct iscsi_data *hdr = (struct iscsi_data *) buf;
549         u32 payload_length = ntoh24(hdr->dlength);
550
551         if (cmd->unsolicited_data) {
552                 if ((cmd->first_burst_len + payload_length) ==
553                      conn->sess->sess_ops->FirstBurstLength) {
554                         if (iscsit_dataout_update_r2t(cmd, be32_to_cpu(hdr->offset),
555                                         payload_length) < 0)
556                                 return DATAOUT_CANNOT_RECOVER;
557                         send_r2t = 1;
558                 }
559
560                 if (!conn->sess->sess_ops->DataPDUInOrder) {
561                         ret = iscsit_dataout_update_datapduinorder_no(cmd,
562                                 be32_to_cpu(hdr->datasn),
563                                 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
564                         if (ret == DATAOUT_CANNOT_RECOVER)
565                                 return ret;
566                 }
567
568                 cmd->first_burst_len += payload_length;
569
570                 if (conn->sess->sess_ops->DataSequenceInOrder)
571                         cmd->data_sn++;
572                 else {
573                         seq = cmd->seq_ptr;
574                         seq->data_sn++;
575                         seq->offset += payload_length;
576                 }
577
578                 if (send_r2t) {
579                         if (seq)
580                                 seq->status = DATAOUT_SEQUENCE_COMPLETE;
581                         cmd->first_burst_len = 0;
582                         cmd->unsolicited_data = 0;
583                 }
584         } else {
585                 if (conn->sess->sess_ops->DataSequenceInOrder) {
586                         if ((cmd->next_burst_len + payload_length) ==
587                              conn->sess->sess_ops->MaxBurstLength) {
588                                 if (iscsit_dataout_update_r2t(cmd,
589                                                 be32_to_cpu(hdr->offset),
590                                                 payload_length) < 0)
591                                         return DATAOUT_CANNOT_RECOVER;
592                                 send_r2t = 1;
593                         }
594
595                         if (!conn->sess->sess_ops->DataPDUInOrder) {
596                                 ret = iscsit_dataout_update_datapduinorder_no(
597                                                 cmd, be32_to_cpu(hdr->datasn),
598                                                 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
599                                 if (ret == DATAOUT_CANNOT_RECOVER)
600                                         return ret;
601                         }
602
603                         cmd->next_burst_len += payload_length;
604                         cmd->data_sn++;
605
606                         if (send_r2t)
607                                 cmd->next_burst_len = 0;
608                 } else {
609                         seq = cmd->seq_ptr;
610
611                         if ((seq->next_burst_len + payload_length) ==
612                              seq->xfer_len) {
613                                 if (iscsit_dataout_update_r2t(cmd,
614                                                 be32_to_cpu(hdr->offset),
615                                                 payload_length) < 0)
616                                         return DATAOUT_CANNOT_RECOVER;
617                                 send_r2t = 1;
618                         }
619
620                         if (!conn->sess->sess_ops->DataPDUInOrder) {
621                                 ret = iscsit_dataout_update_datapduinorder_no(
622                                                 cmd, be32_to_cpu(hdr->datasn),
623                                                 (hdr->flags & ISCSI_FLAG_CMD_FINAL));
624                                 if (ret == DATAOUT_CANNOT_RECOVER)
625                                         return ret;
626                         }
627
628                         seq->data_sn++;
629                         seq->offset += payload_length;
630                         seq->next_burst_len += payload_length;
631
632                         if (send_r2t) {
633                                 seq->next_burst_len = 0;
634                                 seq->status = DATAOUT_SEQUENCE_COMPLETE;
635                         }
636                 }
637         }
638
639         if (send_r2t && conn->sess->sess_ops->DataSequenceInOrder)
640                 cmd->data_sn = 0;
641
642         cmd->write_data_done += payload_length;
643
644         if (cmd->write_data_done == cmd->se_cmd.data_length)
645                 return DATAOUT_SEND_TO_TRANSPORT;
646         else if (send_r2t)
647                 return DATAOUT_SEND_R2T;
648         else
649                 return DATAOUT_NORMAL;
650 }
651
652 static int iscsit_dataout_post_crc_failed(
653         struct iscsi_cmd *cmd,
654         unsigned char *buf)
655 {
656         struct iscsi_conn *conn = cmd->conn;
657         struct iscsi_pdu *pdu;
658         struct iscsi_data *hdr = (struct iscsi_data *) buf;
659         u32 payload_length = ntoh24(hdr->dlength);
660
661         if (conn->sess->sess_ops->DataPDUInOrder)
662                 goto recover;
663         /*
664          * The rest of this function is only called when DataPDUInOrder=No.
665          */
666         pdu = cmd->pdu_ptr;
667
668         switch (pdu->status) {
669         case ISCSI_PDU_NOT_RECEIVED:
670                 pdu->status = ISCSI_PDU_CRC_FAILED;
671                 break;
672         case ISCSI_PDU_CRC_FAILED:
673                 break;
674         case ISCSI_PDU_TIMED_OUT:
675                 pdu->status = ISCSI_PDU_CRC_FAILED;
676                 break;
677         default:
678                 return DATAOUT_CANNOT_RECOVER;
679         }
680
681 recover:
682         return iscsit_recover_dataout_sequence(cmd, be32_to_cpu(hdr->offset),
683                                                 payload_length);
684 }
685
686 /*
687  *      Called from iscsit_handle_data_out() before DataOUT Payload is received
688  *      and CRC computed.
689  */
690 int iscsit_check_pre_dataout(
691         struct iscsi_cmd *cmd,
692         unsigned char *buf)
693 {
694         int ret;
695         struct iscsi_conn *conn = cmd->conn;
696
697         ret = iscsit_dataout_within_command_recovery_check(cmd, buf);
698         if ((ret == DATAOUT_WITHIN_COMMAND_RECOVERY) ||
699             (ret == DATAOUT_CANNOT_RECOVER))
700                 return ret;
701
702         ret = iscsit_dataout_check_datasn(cmd, buf);
703         if ((ret == DATAOUT_WITHIN_COMMAND_RECOVERY) ||
704             (ret == DATAOUT_CANNOT_RECOVER))
705                 return ret;
706
707         if (cmd->unsolicited_data) {
708                 ret = iscsit_dataout_check_unsolicited_sequence(cmd, buf);
709                 if ((ret == DATAOUT_WITHIN_COMMAND_RECOVERY) ||
710                     (ret == DATAOUT_CANNOT_RECOVER))
711                         return ret;
712         } else {
713                 ret = iscsit_dataout_check_sequence(cmd, buf);
714                 if ((ret == DATAOUT_WITHIN_COMMAND_RECOVERY) ||
715                     (ret == DATAOUT_CANNOT_RECOVER))
716                         return ret;
717         }
718
719         return (conn->sess->sess_ops->DataPDUInOrder) ?
720                 iscsit_dataout_pre_datapduinorder_yes(cmd, buf) :
721                 iscsit_dataout_pre_datapduinorder_no(cmd, buf);
722 }
723
724 /*
725  *      Called from iscsit_handle_data_out() after DataOUT Payload is received
726  *      and CRC computed.
727  */
728 int iscsit_check_post_dataout(
729         struct iscsi_cmd *cmd,
730         unsigned char *buf,
731         u8 data_crc_failed)
732 {
733         struct iscsi_conn *conn = cmd->conn;
734
735         cmd->dataout_timeout_retries = 0;
736
737         if (!data_crc_failed)
738                 return iscsit_dataout_post_crc_passed(cmd, buf);
739         else {
740                 if (!conn->sess->sess_ops->ErrorRecoveryLevel) {
741                         pr_err("Unable to recover from DataOUT CRC"
742                                 " failure while ERL=0, closing session.\n");
743                         iscsit_reject_cmd(cmd, ISCSI_REASON_DATA_DIGEST_ERROR,
744                                           buf);
745                         return DATAOUT_CANNOT_RECOVER;
746                 }
747
748                 iscsit_reject_cmd(cmd, ISCSI_REASON_DATA_DIGEST_ERROR, buf);
749                 return iscsit_dataout_post_crc_failed(cmd, buf);
750         }
751 }
752
753 static void iscsit_handle_time2retain_timeout(unsigned long data)
754 {
755         struct iscsi_session *sess = (struct iscsi_session *) data;
756         struct iscsi_portal_group *tpg = sess->tpg;
757         struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
758
759         spin_lock_bh(&se_tpg->session_lock);
760         if (sess->time2retain_timer_flags & ISCSI_TF_STOP) {
761                 spin_unlock_bh(&se_tpg->session_lock);
762                 return;
763         }
764         if (atomic_read(&sess->session_reinstatement)) {
765                 pr_err("Exiting Time2Retain handler because"
766                                 " session_reinstatement=1\n");
767                 spin_unlock_bh(&se_tpg->session_lock);
768                 return;
769         }
770         sess->time2retain_timer_flags |= ISCSI_TF_EXPIRED;
771
772         pr_err("Time2Retain timer expired for SID: %u, cleaning up"
773                         " iSCSI session.\n", sess->sid);
774         {
775         struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
776
777         if (tiqn) {
778                 spin_lock(&tiqn->sess_err_stats.lock);
779                 strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
780                         (void *)sess->sess_ops->InitiatorName);
781                 tiqn->sess_err_stats.last_sess_failure_type =
782                                 ISCSI_SESS_ERR_CXN_TIMEOUT;
783                 tiqn->sess_err_stats.cxn_timeout_errors++;
784                 atomic_long_inc(&sess->conn_timeout_errors);
785                 spin_unlock(&tiqn->sess_err_stats.lock);
786         }
787         }
788
789         spin_unlock_bh(&se_tpg->session_lock);
790         target_put_session(sess->se_sess);
791 }
792
793 void iscsit_start_time2retain_handler(struct iscsi_session *sess)
794 {
795         int tpg_active;
796         /*
797          * Only start Time2Retain timer when the associated TPG is still in
798          * an ACTIVE (eg: not disabled or shutdown) state.
799          */
800         spin_lock(&sess->tpg->tpg_state_lock);
801         tpg_active = (sess->tpg->tpg_state == TPG_STATE_ACTIVE);
802         spin_unlock(&sess->tpg->tpg_state_lock);
803
804         if (!tpg_active)
805                 return;
806
807         if (sess->time2retain_timer_flags & ISCSI_TF_RUNNING)
808                 return;
809
810         pr_debug("Starting Time2Retain timer for %u seconds on"
811                 " SID: %u\n", sess->sess_ops->DefaultTime2Retain, sess->sid);
812
813         init_timer(&sess->time2retain_timer);
814         sess->time2retain_timer.expires =
815                 (get_jiffies_64() + sess->sess_ops->DefaultTime2Retain * HZ);
816         sess->time2retain_timer.data = (unsigned long)sess;
817         sess->time2retain_timer.function = iscsit_handle_time2retain_timeout;
818         sess->time2retain_timer_flags &= ~ISCSI_TF_STOP;
819         sess->time2retain_timer_flags |= ISCSI_TF_RUNNING;
820         add_timer(&sess->time2retain_timer);
821 }
822
823 /*
824  *      Called with spin_lock_bh(&struct se_portal_group->session_lock) held
825  */
826 int iscsit_stop_time2retain_timer(struct iscsi_session *sess)
827 {
828         struct iscsi_portal_group *tpg = sess->tpg;
829         struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
830
831         if (sess->time2retain_timer_flags & ISCSI_TF_EXPIRED)
832                 return -1;
833
834         if (!(sess->time2retain_timer_flags & ISCSI_TF_RUNNING))
835                 return 0;
836
837         sess->time2retain_timer_flags |= ISCSI_TF_STOP;
838         spin_unlock(&se_tpg->session_lock);
839
840         del_timer_sync(&sess->time2retain_timer);
841
842         spin_lock(&se_tpg->session_lock);
843         sess->time2retain_timer_flags &= ~ISCSI_TF_RUNNING;
844         pr_debug("Stopped Time2Retain Timer for SID: %u\n",
845                         sess->sid);
846         return 0;
847 }
848
849 void iscsit_connection_reinstatement_rcfr(struct iscsi_conn *conn)
850 {
851         spin_lock_bh(&conn->state_lock);
852         if (atomic_read(&conn->connection_exit)) {
853                 spin_unlock_bh(&conn->state_lock);
854                 goto sleep;
855         }
856
857         if (atomic_read(&conn->transport_failed)) {
858                 spin_unlock_bh(&conn->state_lock);
859                 goto sleep;
860         }
861         spin_unlock_bh(&conn->state_lock);
862
863         iscsi_thread_set_force_reinstatement(conn);
864
865 sleep:
866         wait_for_completion(&conn->conn_wait_rcfr_comp);
867         complete(&conn->conn_post_wait_comp);
868 }
869
870 void iscsit_cause_connection_reinstatement(struct iscsi_conn *conn, int sleep)
871 {
872         spin_lock_bh(&conn->state_lock);
873         if (atomic_read(&conn->connection_exit)) {
874                 spin_unlock_bh(&conn->state_lock);
875                 return;
876         }
877
878         if (atomic_read(&conn->transport_failed)) {
879                 spin_unlock_bh(&conn->state_lock);
880                 return;
881         }
882
883         if (atomic_read(&conn->connection_reinstatement)) {
884                 spin_unlock_bh(&conn->state_lock);
885                 return;
886         }
887
888         if (iscsi_thread_set_force_reinstatement(conn) < 0) {
889                 spin_unlock_bh(&conn->state_lock);
890                 return;
891         }
892
893         atomic_set(&conn->connection_reinstatement, 1);
894         if (!sleep) {
895                 spin_unlock_bh(&conn->state_lock);
896                 return;
897         }
898
899         atomic_set(&conn->sleep_on_conn_wait_comp, 1);
900         spin_unlock_bh(&conn->state_lock);
901
902         wait_for_completion(&conn->conn_wait_comp);
903         complete(&conn->conn_post_wait_comp);
904 }
905 EXPORT_SYMBOL(iscsit_cause_connection_reinstatement);
906
907 void iscsit_fall_back_to_erl0(struct iscsi_session *sess)
908 {
909         pr_debug("Falling back to ErrorRecoveryLevel=0 for SID:"
910                         " %u\n", sess->sid);
911
912         atomic_set(&sess->session_fall_back_to_erl0, 1);
913 }
914
915 static void iscsit_handle_connection_cleanup(struct iscsi_conn *conn)
916 {
917         struct iscsi_session *sess = conn->sess;
918
919         if ((sess->sess_ops->ErrorRecoveryLevel == 2) &&
920             !atomic_read(&sess->session_reinstatement) &&
921             !atomic_read(&sess->session_fall_back_to_erl0))
922                 iscsit_connection_recovery_transport_reset(conn);
923         else {
924                 pr_debug("Performing cleanup for failed iSCSI"
925                         " Connection ID: %hu from %s\n", conn->cid,
926                         sess->sess_ops->InitiatorName);
927                 iscsit_close_connection(conn);
928         }
929 }
930
931 void iscsit_take_action_for_connection_exit(struct iscsi_conn *conn)
932 {
933         spin_lock_bh(&conn->state_lock);
934         if (atomic_read(&conn->connection_exit)) {
935                 spin_unlock_bh(&conn->state_lock);
936                 return;
937         }
938         atomic_set(&conn->connection_exit, 1);
939
940         if (conn->conn_state == TARG_CONN_STATE_IN_LOGOUT) {
941                 spin_unlock_bh(&conn->state_lock);
942                 iscsit_close_connection(conn);
943                 return;
944         }
945
946         if (conn->conn_state == TARG_CONN_STATE_CLEANUP_WAIT) {
947                 spin_unlock_bh(&conn->state_lock);
948                 return;
949         }
950
951         pr_debug("Moving to TARG_CONN_STATE_CLEANUP_WAIT.\n");
952         conn->conn_state = TARG_CONN_STATE_CLEANUP_WAIT;
953         spin_unlock_bh(&conn->state_lock);
954
955         iscsit_handle_connection_cleanup(conn);
956 }
957
958 /*
959  *      This is the simple function that makes the magic of
960  *      sync and steering happen in the follow paradoxical order:
961  *
962  *      0) Receive conn->of_marker (bytes left until next OFMarker)
963  *         bytes into an offload buffer.  When we pass the exact number
964  *         of bytes in conn->of_marker, iscsit_dump_data_payload() and hence
965  *         rx_data() will automatically receive the identical u32 marker
966  *         values and store it in conn->of_marker_offset;
967  *      1) Now conn->of_marker_offset will contain the offset to the start
968  *         of the next iSCSI PDU.  Dump these remaining bytes into another
969  *         offload buffer.
970  *      2) We are done!
971  *         Next byte in the TCP stream will contain the next iSCSI PDU!
972  *         Cool Huh?!
973  */
974 int iscsit_recover_from_unknown_opcode(struct iscsi_conn *conn)
975 {
976         /*
977          * Make sure the remaining bytes to next maker is a sane value.
978          */
979         if (conn->of_marker > (conn->conn_ops->OFMarkInt * 4)) {
980                 pr_err("Remaining bytes to OFMarker: %u exceeds"
981                         " OFMarkInt bytes: %u.\n", conn->of_marker,
982                                 conn->conn_ops->OFMarkInt * 4);
983                 return -1;
984         }
985
986         pr_debug("Advancing %u bytes in TCP stream to get to the"
987                         " next OFMarker.\n", conn->of_marker);
988
989         if (iscsit_dump_data_payload(conn, conn->of_marker, 0) < 0)
990                 return -1;
991
992         /*
993          * Make sure the offset marker we retrived is a valid value.
994          */
995         if (conn->of_marker_offset > (ISCSI_HDR_LEN + (ISCSI_CRC_LEN * 2) +
996             conn->conn_ops->MaxRecvDataSegmentLength)) {
997                 pr_err("OfMarker offset value: %u exceeds limit.\n",
998                         conn->of_marker_offset);
999                 return -1;
1000         }
1001
1002         pr_debug("Discarding %u bytes of TCP stream to get to the"
1003                         " next iSCSI Opcode.\n", conn->of_marker_offset);
1004
1005         if (iscsit_dump_data_payload(conn, conn->of_marker_offset, 0) < 0)
1006                 return -1;
1007
1008         return 0;
1009 }