Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
iscsi_target_erl1.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * This file contains error recovery level one used by the iSCSI Target driver.
3  *
4  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5  *
6  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7  *
8  * Author: Nicholas A. Bellinger <[email protected]>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  ******************************************************************************/
20 
21 #include <linux/list.h>
22 #include <scsi/iscsi_proto.h>
25 
26 #include "iscsi_target_core.h"
29 #include "iscsi_target_device.h"
30 #include "iscsi_target_tpg.h"
31 #include "iscsi_target_util.h"
32 #include "iscsi_target_erl0.h"
33 #include "iscsi_target_erl1.h"
34 #include "iscsi_target_erl2.h"
35 #include "iscsi_target.h"
36 
37 #define OFFLOAD_BUF_SIZE 32768
38 
39 /*
40  * Used to dump excess datain payload for certain error recovery
41  * situations. Receive in OFFLOAD_BUF_SIZE max of datain per rx_data().
42  *
43  * dump_padding_digest denotes if padding and data digests need
44  * to be dumped.
45  */
47  struct iscsi_conn *conn,
48  u32 buf_len,
49  int dump_padding_digest)
50 {
51  char *buf, pad_bytes[4];
53  u32 length, padding, offset = 0, size;
54  struct kvec iov;
55 
56  length = (buf_len > OFFLOAD_BUF_SIZE) ? OFFLOAD_BUF_SIZE : buf_len;
57 
58  buf = kzalloc(length, GFP_ATOMIC);
59  if (!buf) {
60  pr_err("Unable to allocate %u bytes for offload"
61  " buffer.\n", length);
62  return -1;
63  }
64  memset(&iov, 0, sizeof(struct kvec));
65 
66  while (offset < buf_len) {
67  size = ((offset + length) > buf_len) ?
68  (buf_len - offset) : length;
69 
70  iov.iov_len = size;
71  iov.iov_base = buf;
72 
73  rx_got = rx_data(conn, &iov, 1, size);
74  if (rx_got != size) {
76  goto out;
77  }
78 
79  offset += size;
80  }
81 
82  if (!dump_padding_digest)
83  goto out;
84 
85  padding = ((-buf_len) & 3);
86  if (padding != 0) {
87  iov.iov_len = padding;
88  iov.iov_base = pad_bytes;
89 
90  rx_got = rx_data(conn, &iov, 1, padding);
91  if (rx_got != padding) {
93  goto out;
94  }
95  }
96 
97  if (conn->conn_ops->DataDigest) {
98  u32 data_crc;
99 
100  iov.iov_len = ISCSI_CRC_LEN;
101  iov.iov_base = &data_crc;
102 
103  rx_got = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
104  if (rx_got != ISCSI_CRC_LEN) {
106  goto out;
107  }
108  }
109 
110 out:
111  kfree(buf);
112  return ret;
113 }
114 
115 /*
116  * Used for retransmitting R2Ts from a R2T SNACK request.
117  */
118 static int iscsit_send_recovery_r2t_for_snack(
119  struct iscsi_cmd *cmd,
120  struct iscsi_r2t *r2t)
121 {
122  /*
123  * If the struct iscsi_r2t has not been sent yet, we can safely
124  * ignore retransmission
125  * of the R2TSN in question.
126  */
127  spin_lock_bh(&cmd->r2t_lock);
128  if (!r2t->sent_r2t) {
129  spin_unlock_bh(&cmd->r2t_lock);
130  return 0;
131  }
132  r2t->sent_r2t = 0;
133  spin_unlock_bh(&cmd->r2t_lock);
134 
136 
137  return 0;
138 }
139 
140 static int iscsit_handle_r2t_snack(
141  struct iscsi_cmd *cmd,
142  unsigned char *buf,
143  u32 begrun,
144  u32 runlength)
145 {
146  u32 last_r2tsn;
147  struct iscsi_r2t *r2t;
148 
149  /*
150  * Make sure the initiator is not requesting retransmission
151  * of R2TSNs already acknowledged by a TMR TASK_REASSIGN.
152  */
153  if ((cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
154  (begrun <= cmd->acked_data_sn)) {
155  pr_err("ITT: 0x%08x, R2T SNACK requesting"
156  " retransmission of R2TSN: 0x%08x to 0x%08x but already"
157  " acked to R2TSN: 0x%08x by TMR TASK_REASSIGN,"
158  " protocol error.\n", cmd->init_task_tag, begrun,
159  (begrun + runlength), cmd->acked_data_sn);
160 
163  1, 0, buf, cmd);
164  }
165 
166  if (runlength) {
167  if ((begrun + runlength) > cmd->r2t_sn) {
168  pr_err("Command ITT: 0x%08x received R2T SNACK"
169  " with BegRun: 0x%08x, RunLength: 0x%08x, exceeds"
170  " current R2TSN: 0x%08x, protocol error.\n",
171  cmd->init_task_tag, begrun, runlength, cmd->r2t_sn);
173  ISCSI_REASON_BOOKMARK_INVALID, 1, 0, buf, cmd);
174  }
175  last_r2tsn = (begrun + runlength);
176  } else
177  last_r2tsn = cmd->r2t_sn;
178 
179  while (begrun < last_r2tsn) {
180  r2t = iscsit_get_holder_for_r2tsn(cmd, begrun);
181  if (!r2t)
182  return -1;
183  if (iscsit_send_recovery_r2t_for_snack(cmd, r2t) < 0)
184  return -1;
185 
186  begrun++;
187  }
188 
189  return 0;
190 }
191 
192 /*
193  * Generates Offsets and NextBurstLength based on Begrun and Runlength
194  * carried in a Data SNACK or ExpDataSN in TMR TASK_REASSIGN.
195  *
196  * For DataSequenceInOrder=Yes and DataPDUInOrder=[Yes,No] only.
197  *
198  * FIXME: How is this handled for a RData SNACK?
199  */
201  struct iscsi_cmd *cmd,
202  struct iscsi_datain_req *dr)
203 {
204  u32 data_sn = 0, data_sn_count = 0;
205  u32 pdu_start = 0, seq_no = 0;
206  u32 begrun = dr->begrun;
207  struct iscsi_conn *conn = cmd->conn;
208 
209  while (begrun > data_sn++) {
210  data_sn_count++;
211  if ((dr->next_burst_len +
212  conn->conn_ops->MaxRecvDataSegmentLength) <
213  conn->sess->sess_ops->MaxBurstLength) {
214  dr->read_data_done +=
215  conn->conn_ops->MaxRecvDataSegmentLength;
216  dr->next_burst_len +=
217  conn->conn_ops->MaxRecvDataSegmentLength;
218  } else {
219  dr->read_data_done +=
220  (conn->sess->sess_ops->MaxBurstLength -
221  dr->next_burst_len);
222  dr->next_burst_len = 0;
223  pdu_start += data_sn_count;
224  data_sn_count = 0;
225  seq_no++;
226  }
227  }
228 
229  if (!conn->sess->sess_ops->DataPDUInOrder) {
230  cmd->seq_no = seq_no;
231  cmd->pdu_start = pdu_start;
232  cmd->pdu_send_order = data_sn_count;
233  }
234 
235  return 0;
236 }
237 
238 /*
239  * Generates Offsets and NextBurstLength based on Begrun and Runlength
240  * carried in a Data SNACK or ExpDataSN in TMR TASK_REASSIGN.
241  *
242  * For DataSequenceInOrder=No and DataPDUInOrder=[Yes,No] only.
243  *
244  * FIXME: How is this handled for a RData SNACK?
245  */
247  struct iscsi_cmd *cmd,
248  struct iscsi_datain_req *dr)
249 {
250  int found_seq = 0, i;
251  u32 data_sn, read_data_done = 0, seq_send_order = 0;
252  u32 begrun = dr->begrun;
253  u32 runlength = dr->runlength;
254  struct iscsi_conn *conn = cmd->conn;
255  struct iscsi_seq *first_seq = NULL, *seq = NULL;
256 
257  if (!cmd->seq_list) {
258  pr_err("struct iscsi_cmd->seq_list is NULL!\n");
259  return -1;
260  }
261 
262  /*
263  * Calculate read_data_done for all sequences containing a
264  * first_datasn and last_datasn less than the BegRun.
265  *
266  * Locate the struct iscsi_seq the BegRun lies within and calculate
267  * NextBurstLenghth up to the DataSN based on MaxRecvDataSegmentLength.
268  *
269  * Also use struct iscsi_seq->seq_send_order to determine where to start.
270  */
271  for (i = 0; i < cmd->seq_count; i++) {
272  seq = &cmd->seq_list[i];
273 
274  if (!seq->seq_send_order)
275  first_seq = seq;
276 
277  /*
278  * No data has been transferred for this DataIN sequence, so the
279  * seq->first_datasn and seq->last_datasn have not been set.
280  */
281  if (!seq->sent) {
282  pr_err("Ignoring non-sent sequence 0x%08x ->"
283  " 0x%08x\n\n", seq->first_datasn,
284  seq->last_datasn);
285  continue;
286  }
287 
288  /*
289  * This DataIN sequence is precedes the received BegRun, add the
290  * total xfer_len of the sequence to read_data_done and reset
291  * seq->pdu_send_order.
292  */
293  if ((seq->first_datasn < begrun) &&
294  (seq->last_datasn < begrun)) {
295  pr_err("Pre BegRun sequence 0x%08x ->"
296  " 0x%08x\n", seq->first_datasn,
297  seq->last_datasn);
298 
299  read_data_done += cmd->seq_list[i].xfer_len;
300  seq->next_burst_len = seq->pdu_send_order = 0;
301  continue;
302  }
303 
304  /*
305  * The BegRun lies within this DataIN sequence.
306  */
307  if ((seq->first_datasn <= begrun) &&
308  (seq->last_datasn >= begrun)) {
309  pr_err("Found sequence begrun: 0x%08x in"
310  " 0x%08x -> 0x%08x\n", begrun,
311  seq->first_datasn, seq->last_datasn);
312 
313  seq_send_order = seq->seq_send_order;
314  data_sn = seq->first_datasn;
315  seq->next_burst_len = seq->pdu_send_order = 0;
316  found_seq = 1;
317 
318  /*
319  * For DataPDUInOrder=Yes, while the first DataSN of
320  * the sequence is less than the received BegRun, add
321  * the MaxRecvDataSegmentLength to read_data_done and
322  * to the sequence's next_burst_len;
323  *
324  * For DataPDUInOrder=No, while the first DataSN of the
325  * sequence is less than the received BegRun, find the
326  * struct iscsi_pdu of the DataSN in question and add the
327  * MaxRecvDataSegmentLength to read_data_done and to the
328  * sequence's next_burst_len;
329  */
330  if (conn->sess->sess_ops->DataPDUInOrder) {
331  while (data_sn < begrun) {
332  seq->pdu_send_order++;
333  read_data_done +=
334  conn->conn_ops->MaxRecvDataSegmentLength;
335  seq->next_burst_len +=
336  conn->conn_ops->MaxRecvDataSegmentLength;
337  data_sn++;
338  }
339  } else {
340  int j;
341  struct iscsi_pdu *pdu;
342 
343  while (data_sn < begrun) {
344  seq->pdu_send_order++;
345 
346  for (j = 0; j < seq->pdu_count; j++) {
347  pdu = &cmd->pdu_list[
348  seq->pdu_start + j];
349  if (pdu->data_sn == data_sn) {
350  read_data_done +=
351  pdu->length;
352  seq->next_burst_len +=
353  pdu->length;
354  }
355  }
356  data_sn++;
357  }
358  }
359  continue;
360  }
361 
362  /*
363  * This DataIN sequence is larger than the received BegRun,
364  * reset seq->pdu_send_order and continue.
365  */
366  if ((seq->first_datasn > begrun) ||
367  (seq->last_datasn > begrun)) {
368  pr_err("Post BegRun sequence 0x%08x -> 0x%08x\n",
369  seq->first_datasn, seq->last_datasn);
370 
371  seq->next_burst_len = seq->pdu_send_order = 0;
372  continue;
373  }
374  }
375 
376  if (!found_seq) {
377  if (!begrun) {
378  if (!first_seq) {
379  pr_err("ITT: 0x%08x, Begrun: 0x%08x"
380  " but first_seq is NULL\n",
381  cmd->init_task_tag, begrun);
382  return -1;
383  }
384  seq_send_order = first_seq->seq_send_order;
385  seq->next_burst_len = seq->pdu_send_order = 0;
386  goto done;
387  }
388 
389  pr_err("Unable to locate struct iscsi_seq for ITT: 0x%08x,"
390  " BegRun: 0x%08x, RunLength: 0x%08x while"
391  " DataSequenceInOrder=No and DataPDUInOrder=%s.\n",
392  cmd->init_task_tag, begrun, runlength,
393  (conn->sess->sess_ops->DataPDUInOrder) ? "Yes" : "No");
394  return -1;
395  }
396 
397 done:
398  dr->read_data_done = read_data_done;
399  dr->seq_send_order = seq_send_order;
400 
401  return 0;
402 }
403 
404 static int iscsit_handle_recovery_datain(
405  struct iscsi_cmd *cmd,
406  unsigned char *buf,
407  u32 begrun,
408  u32 runlength)
409 {
410  struct iscsi_conn *conn = cmd->conn;
411  struct iscsi_datain_req *dr;
412  struct se_cmd *se_cmd = &cmd->se_cmd;
413 
414  if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
415  pr_err("Ignoring ITT: 0x%08x Data SNACK\n",
416  cmd->init_task_tag);
417  return 0;
418  }
419 
420  /*
421  * Make sure the initiator is not requesting retransmission
422  * of DataSNs already acknowledged by a Data ACK SNACK.
423  */
424  if ((cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
425  (begrun <= cmd->acked_data_sn)) {
426  pr_err("ITT: 0x%08x, Data SNACK requesting"
427  " retransmission of DataSN: 0x%08x to 0x%08x but"
428  " already acked to DataSN: 0x%08x by Data ACK SNACK,"
429  " protocol error.\n", cmd->init_task_tag, begrun,
430  (begrun + runlength), cmd->acked_data_sn);
431 
433  1, 0, buf, cmd);
434  }
435 
436  /*
437  * Make sure BegRun and RunLength in the Data SNACK are sane.
438  * Note: (cmd->data_sn - 1) will carry the maximum DataSN sent.
439  */
440  if ((begrun + runlength) > (cmd->data_sn - 1)) {
441  pr_err("Initiator requesting BegRun: 0x%08x, RunLength"
442  ": 0x%08x greater than maximum DataSN: 0x%08x.\n",
443  begrun, runlength, (cmd->data_sn - 1));
445  1, 0, buf, cmd);
446  }
447 
449  if (!dr)
451  1, 0, buf, cmd);
452 
453  dr->data_sn = dr->begrun = begrun;
454  dr->runlength = runlength;
455  dr->generate_recovery_values = 1;
457 
458  iscsit_attach_datain_req(cmd, dr);
459 
461  iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
462 
463  return 0;
464 }
465 
467  struct iscsi_conn *conn,
468  unsigned char *buf,
469  itt_t init_task_tag,
470  u32 targ_xfer_tag,
471  u32 begrun,
472  u32 runlength)
473 {
474  struct iscsi_cmd *cmd;
475 
476  cmd = iscsit_find_cmd_from_itt(conn, init_task_tag);
477  if (!cmd)
478  return 0;
479 
480  /*
481  * FIXME: This will not work for bidi commands.
482  */
483  switch (cmd->data_direction) {
484  case DMA_TO_DEVICE:
485  return iscsit_handle_r2t_snack(cmd, buf, begrun, runlength);
486  case DMA_FROM_DEVICE:
487  return iscsit_handle_recovery_datain(cmd, buf, begrun,
488  runlength);
489  default:
490  pr_err("Unknown cmd->data_direction: 0x%02x\n",
491  cmd->data_direction);
492  return -1;
493  }
494 
495  return 0;
496 }
497 
498 /* #warning FIXME: Status SNACK needs to be dependent on OPCODE!!! */
500  struct iscsi_conn *conn,
503  u32 begrun,
504  u32 runlength)
505 {
506  struct iscsi_cmd *cmd = NULL;
507  u32 last_statsn;
508  int found_cmd;
509 
510  if (conn->exp_statsn > begrun) {
511  pr_err("Got Status SNACK Begrun: 0x%08x, RunLength:"
512  " 0x%08x but already got ExpStatSN: 0x%08x on CID:"
513  " %hu.\n", begrun, runlength, conn->exp_statsn,
514  conn->cid);
515  return 0;
516  }
517 
518  last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);
519 
520  while (begrun < last_statsn) {
521  found_cmd = 0;
522 
523  spin_lock_bh(&conn->cmd_lock);
525  if (cmd->stat_sn == begrun) {
526  found_cmd = 1;
527  break;
528  }
529  }
530  spin_unlock_bh(&conn->cmd_lock);
531 
532  if (!found_cmd) {
533  pr_err("Unable to find StatSN: 0x%08x for"
534  " a Status SNACK, assuming this was a"
535  " protactic SNACK for an untransmitted"
536  " StatSN, ignoring.\n", begrun);
537  begrun++;
538  continue;
539  }
540 
541  spin_lock_bh(&cmd->istate_lock);
542  if (cmd->i_state == ISTATE_SEND_DATAIN) {
543  spin_unlock_bh(&cmd->istate_lock);
544  pr_err("Ignoring Status SNACK for BegRun:"
545  " 0x%08x, RunLength: 0x%08x, assuming this was"
546  " a protactic SNACK for an untransmitted"
547  " StatSN\n", begrun, runlength);
548  begrun++;
549  continue;
550  }
551  spin_unlock_bh(&cmd->istate_lock);
552 
554  iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
555  begrun++;
556  }
557 
558  return 0;
559 }
560 
562  struct iscsi_conn *conn,
564  u32 begrun,
565  u32 runlength)
566 {
567  struct iscsi_cmd *cmd = NULL;
568 
569  cmd = iscsit_find_cmd_from_ttt(conn, targ_xfer_tag);
570  if (!cmd) {
571  pr_err("Data ACK SNACK for TTT: 0x%08x is"
572  " invalid.\n", targ_xfer_tag);
573  return -1;
574  }
575 
576  if (begrun <= cmd->acked_data_sn) {
577  pr_err("ITT: 0x%08x Data ACK SNACK BegRUN: 0x%08x is"
578  " less than the already acked DataSN: 0x%08x.\n",
579  cmd->init_task_tag, begrun, cmd->acked_data_sn);
580  return -1;
581  }
582 
583  /*
584  * For Data ACK SNACK, BegRun is the next expected DataSN.
585  * (see iSCSI v19: 10.16.6)
586  */
588  cmd->acked_data_sn = (begrun - 1);
589 
590  pr_debug("Received Data ACK SNACK for ITT: 0x%08x,"
591  " updated acked DataSN to 0x%08x.\n",
592  cmd->init_task_tag, cmd->acked_data_sn);
593 
594  return 0;
595 }
596 
597 static int iscsit_send_recovery_r2t(
598  struct iscsi_cmd *cmd,
599  u32 offset,
600  u32 xfer_len)
601 {
602  int ret;
603 
604  spin_lock_bh(&cmd->r2t_lock);
605  ret = iscsit_add_r2t_to_list(cmd, offset, xfer_len, 1, 0);
606  spin_unlock_bh(&cmd->r2t_lock);
607 
608  return ret;
609 }
610 
612  struct iscsi_cmd *cmd,
613  struct iscsi_pdu *pdu)
614 {
615  int i, send_recovery_r2t = 0, recovery = 0;
616  u32 length = 0, offset = 0, pdu_count = 0, xfer_len = 0;
617  struct iscsi_conn *conn = cmd->conn;
618  struct iscsi_pdu *first_pdu = NULL;
619 
620  /*
621  * Get an struct iscsi_pdu pointer to the first PDU, and total PDU count
622  * of the DataOUT sequence.
623  */
624  if (conn->sess->sess_ops->DataSequenceInOrder) {
625  for (i = 0; i < cmd->pdu_count; i++) {
626  if (cmd->pdu_list[i].seq_no == pdu->seq_no) {
627  if (!first_pdu)
628  first_pdu = &cmd->pdu_list[i];
629  xfer_len += cmd->pdu_list[i].length;
630  pdu_count++;
631  } else if (pdu_count)
632  break;
633  }
634  } else {
635  struct iscsi_seq *seq = cmd->seq_ptr;
636 
637  first_pdu = &cmd->pdu_list[seq->pdu_start];
638  pdu_count = seq->pdu_count;
639  }
640 
641  if (!first_pdu || !pdu_count)
642  return DATAOUT_CANNOT_RECOVER;
643 
644  /*
645  * Loop through the ending DataOUT Sequence checking each struct iscsi_pdu.
646  * The following ugly logic does batching of not received PDUs.
647  */
648  for (i = 0; i < pdu_count; i++) {
649  if (first_pdu[i].status == ISCSI_PDU_RECEIVED_OK) {
650  if (!send_recovery_r2t)
651  continue;
652 
653  if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
654  return DATAOUT_CANNOT_RECOVER;
655 
656  send_recovery_r2t = length = offset = 0;
657  continue;
658  }
659  /*
660  * Set recovery = 1 for any missing, CRC failed, or timed
661  * out PDUs to let the DataOUT logic know that this sequence
662  * has not been completed yet.
663  *
664  * Also, only send a Recovery R2T for ISCSI_PDU_NOT_RECEIVED.
665  * We assume if the PDU either failed CRC or timed out
666  * that a Recovery R2T has already been sent.
667  */
668  recovery = 1;
669 
670  if (first_pdu[i].status != ISCSI_PDU_NOT_RECEIVED)
671  continue;
672 
673  if (!offset)
674  offset = first_pdu[i].offset;
675  length += first_pdu[i].length;
676 
677  send_recovery_r2t = 1;
678  }
679 
680  if (send_recovery_r2t)
681  if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
682  return DATAOUT_CANNOT_RECOVER;
683 
684  return (!recovery) ? DATAOUT_NORMAL : DATAOUT_WITHIN_COMMAND_RECOVERY;
685 }
686 
687 static int iscsit_recalculate_dataout_values(
688  struct iscsi_cmd *cmd,
689  u32 pdu_offset,
690  u32 pdu_length,
691  u32 *r2t_offset,
692  u32 *r2t_length)
693 {
694  int i;
695  struct iscsi_conn *conn = cmd->conn;
696  struct iscsi_pdu *pdu = NULL;
697 
698  if (conn->sess->sess_ops->DataSequenceInOrder) {
699  cmd->data_sn = 0;
700 
701  if (conn->sess->sess_ops->DataPDUInOrder) {
702  *r2t_offset = cmd->write_data_done;
703  *r2t_length = (cmd->seq_end_offset -
704  cmd->write_data_done);
705  return 0;
706  }
707 
708  *r2t_offset = cmd->seq_start_offset;
709  *r2t_length = (cmd->seq_end_offset - cmd->seq_start_offset);
710 
711  for (i = 0; i < cmd->pdu_count; i++) {
712  pdu = &cmd->pdu_list[i];
713 
714  if (pdu->status != ISCSI_PDU_RECEIVED_OK)
715  continue;
716 
717  if ((pdu->offset >= cmd->seq_start_offset) &&
718  ((pdu->offset + pdu->length) <=
719  cmd->seq_end_offset)) {
720  if (!cmd->unsolicited_data)
721  cmd->next_burst_len -= pdu->length;
722  else
723  cmd->first_burst_len -= pdu->length;
724 
725  cmd->write_data_done -= pdu->length;
727  }
728  }
729  } else {
730  struct iscsi_seq *seq = NULL;
731 
732  seq = iscsit_get_seq_holder(cmd, pdu_offset, pdu_length);
733  if (!seq)
734  return -1;
735 
736  *r2t_offset = seq->orig_offset;
737  *r2t_length = seq->xfer_len;
738 
739  cmd->write_data_done -= (seq->offset - seq->orig_offset);
740  if (cmd->immediate_data)
741  cmd->first_burst_len = cmd->write_data_done;
742 
743  seq->data_sn = 0;
744  seq->offset = seq->orig_offset;
745  seq->next_burst_len = 0;
747 
748  if (conn->sess->sess_ops->DataPDUInOrder)
749  return 0;
750 
751  for (i = 0; i < seq->pdu_count; i++) {
752  pdu = &cmd->pdu_list[i+seq->pdu_start];
753 
754  if (pdu->status != ISCSI_PDU_RECEIVED_OK)
755  continue;
756 
758  }
759  }
760 
761  return 0;
762 }
763 
765  struct iscsi_cmd *cmd,
766  u32 pdu_offset,
767  u32 pdu_length)
768 {
769  u32 r2t_length = 0, r2t_offset = 0;
770 
771  spin_lock_bh(&cmd->istate_lock);
773  spin_unlock_bh(&cmd->istate_lock);
774 
775  if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
776  &r2t_offset, &r2t_length) < 0)
777  return DATAOUT_CANNOT_RECOVER;
778 
779  iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length);
780 
782 }
783 
784 static struct iscsi_ooo_cmdsn *iscsit_allocate_ooo_cmdsn(void)
785 {
786  struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL;
787 
788  ooo_cmdsn = kmem_cache_zalloc(lio_ooo_cache, GFP_ATOMIC);
789  if (!ooo_cmdsn) {
790  pr_err("Unable to allocate memory for"
791  " struct iscsi_ooo_cmdsn.\n");
792  return NULL;
793  }
794  INIT_LIST_HEAD(&ooo_cmdsn->ooo_list);
795 
796  return ooo_cmdsn;
797 }
798 
799 /*
800  * Called with sess->cmdsn_mutex held.
801  */
802 static int iscsit_attach_ooo_cmdsn(
803  struct iscsi_session *sess,
804  struct iscsi_ooo_cmdsn *ooo_cmdsn)
805 {
806  struct iscsi_ooo_cmdsn *ooo_tail, *ooo_tmp;
807  /*
808  * We attach the struct iscsi_ooo_cmdsn entry to the out of order
809  * list in increasing CmdSN order.
810  * This allows iscsi_execute_ooo_cmdsns() to detect any
811  * additional CmdSN holes while performing delayed execution.
812  */
813  if (list_empty(&sess->sess_ooo_cmdsn_list))
814  list_add_tail(&ooo_cmdsn->ooo_list,
815  &sess->sess_ooo_cmdsn_list);
816  else {
817  ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
818  typeof(*ooo_tail), ooo_list);
819  /*
820  * CmdSN is greater than the tail of the list.
821  */
822  if (ooo_tail->cmdsn < ooo_cmdsn->cmdsn)
823  list_add_tail(&ooo_cmdsn->ooo_list,
824  &sess->sess_ooo_cmdsn_list);
825  else {
826  /*
827  * CmdSN is either lower than the head, or somewhere
828  * in the middle.
829  */
831  ooo_list) {
832  if (ooo_tmp->cmdsn < ooo_cmdsn->cmdsn)
833  continue;
834 
835  list_add(&ooo_cmdsn->ooo_list,
836  &ooo_tmp->ooo_list);
837  break;
838  }
839  }
840  }
841 
842  return 0;
843 }
844 
845 /*
846  * Removes an struct iscsi_ooo_cmdsn from a session's list,
847  * called with struct iscsi_session->cmdsn_mutex held.
848  */
850  struct iscsi_session *sess,
851  struct iscsi_ooo_cmdsn *ooo_cmdsn)
852 {
853  list_del(&ooo_cmdsn->ooo_list);
854  kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
855 }
856 
858 {
859  struct iscsi_ooo_cmdsn *ooo_cmdsn;
860  struct iscsi_session *sess = conn->sess;
861 
862  mutex_lock(&sess->cmdsn_mutex);
863  list_for_each_entry(ooo_cmdsn, &sess->sess_ooo_cmdsn_list, ooo_list) {
864  if (ooo_cmdsn->cid != conn->cid)
865  continue;
866 
867  ooo_cmdsn->cmd = NULL;
868  }
869  mutex_unlock(&sess->cmdsn_mutex);
870 }
871 
872 /*
873  * Called with sess->cmdsn_mutex held.
874  */
876 {
877  int ooo_count = 0;
878  struct iscsi_cmd *cmd = NULL;
879  struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
880 
881  list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
882  &sess->sess_ooo_cmdsn_list, ooo_list) {
883  if (ooo_cmdsn->cmdsn != sess->exp_cmd_sn)
884  continue;
885 
886  if (!ooo_cmdsn->cmd) {
887  sess->exp_cmd_sn++;
888  iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
889  continue;
890  }
891 
892  cmd = ooo_cmdsn->cmd;
893  cmd->i_state = cmd->deferred_i_state;
894  ooo_count++;
895  sess->exp_cmd_sn++;
896  pr_debug("Executing out of order CmdSN: 0x%08x,"
897  " incremented ExpCmdSN to 0x%08x.\n",
898  cmd->cmd_sn, sess->exp_cmd_sn);
899 
900  iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
901 
902  if (iscsit_execute_cmd(cmd, 1) < 0)
903  return -1;
904 
905  continue;
906  }
907 
908  return ooo_count;
909 }
910 
911 /*
912  * Called either:
913  *
914  * 1. With sess->cmdsn_mutex held from iscsi_execute_ooo_cmdsns()
915  * or iscsi_check_received_cmdsn().
916  * 2. With no locks held directly from iscsi_handle_XXX_pdu() functions
917  * for immediate commands.
918  */
919 int iscsit_execute_cmd(struct iscsi_cmd *cmd, int ooo)
920 {
921  struct se_cmd *se_cmd = &cmd->se_cmd;
922  int lr = 0;
923 
924  spin_lock_bh(&cmd->istate_lock);
925  if (ooo)
926  cmd->cmd_flags &= ~ICF_OOO_CMDSN;
927 
928  switch (cmd->iscsi_opcode) {
929  case ISCSI_OP_SCSI_CMD:
930  /*
931  * Go ahead and send the CHECK_CONDITION status for
932  * any SCSI CDB exceptions that may have occurred, also
933  * handle the SCF_SCSI_RESERVATION_CONFLICT case here as well.
934  */
935  if (se_cmd->se_cmd_flags & SCF_SCSI_CDB_EXCEPTION) {
938  spin_unlock_bh(&cmd->istate_lock);
940  cmd->i_state);
941  return 0;
942  }
943  spin_unlock_bh(&cmd->istate_lock);
944  /*
945  * Determine if delayed TASK_ABORTED status for WRITEs
946  * should be sent now if no unsolicited data out
947  * payloads are expected, or if the delayed status
948  * should be sent after unsolicited data out with
949  * ISCSI_FLAG_CMD_FINAL set in iscsi_handle_data_out()
950  */
952  (cmd->unsolicited_data == 0)) != 0)
953  return 0;
954  /*
955  * Otherwise send CHECK_CONDITION and sense for
956  * exception
957  */
959  se_cmd->scsi_sense_reason, 0);
960  }
961  /*
962  * Special case for delayed CmdSN with Immediate
963  * Data and/or Unsolicited Data Out attached.
964  */
965  if (cmd->immediate_data) {
966  if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
967  spin_unlock_bh(&cmd->istate_lock);
968  target_execute_cmd(&cmd->se_cmd);
969  return 0;
970  }
971  spin_unlock_bh(&cmd->istate_lock);
972 
973  if (!(cmd->cmd_flags &
975  /*
976  * Send the delayed TASK_ABORTED status for
977  * WRITEs if no more unsolicitied data is
978  * expected.
979  */
980  if (transport_check_aborted_status(se_cmd, 1)
981  != 0)
982  return 0;
983 
985  iscsit_build_r2ts_for_cmd(cmd, cmd->conn, false);
986  }
987  return 0;
988  }
989  /*
990  * The default handler.
991  */
992  spin_unlock_bh(&cmd->istate_lock);
993 
994  if ((cmd->data_direction == DMA_TO_DEVICE) &&
996  /*
997  * Send the delayed TASK_ABORTED status for WRITEs if
998  * no more nsolicitied data is expected.
999  */
1000  if (transport_check_aborted_status(se_cmd, 1) != 0)
1001  return 0;
1002 
1004  spin_lock_bh(&cmd->dataout_timeout_lock);
1005  iscsit_start_dataout_timer(cmd, cmd->conn);
1006  spin_unlock_bh(&cmd->dataout_timeout_lock);
1007  }
1008  return transport_handle_cdb_direct(&cmd->se_cmd);
1009 
1010  case ISCSI_OP_NOOP_OUT:
1011  case ISCSI_OP_TEXT:
1012  spin_unlock_bh(&cmd->istate_lock);
1014  break;
1015  case ISCSI_OP_SCSI_TMFUNC:
1016  if (se_cmd->se_cmd_flags & SCF_SCSI_CDB_EXCEPTION) {
1017  spin_unlock_bh(&cmd->istate_lock);
1019  cmd->i_state);
1020  return 0;
1021  }
1022  spin_unlock_bh(&cmd->istate_lock);
1023 
1024  return transport_generic_handle_tmr(&cmd->se_cmd);
1025  case ISCSI_OP_LOGOUT:
1026  spin_unlock_bh(&cmd->istate_lock);
1027  switch (cmd->logout_reason) {
1029  lr = iscsit_logout_closesession(cmd, cmd->conn);
1030  break;
1032  lr = iscsit_logout_closeconnection(cmd, cmd->conn);
1033  break;
1035  lr = iscsit_logout_removeconnforrecovery(cmd, cmd->conn);
1036  break;
1037  default:
1038  pr_err("Unknown iSCSI Logout Request Code:"
1039  " 0x%02x\n", cmd->logout_reason);
1040  return -1;
1041  }
1042 
1043  return lr;
1044  default:
1045  spin_unlock_bh(&cmd->istate_lock);
1046  pr_err("Cannot perform out of order execution for"
1047  " unknown iSCSI Opcode: 0x%02x\n", cmd->iscsi_opcode);
1048  return -1;
1049  }
1050 
1051  return 0;
1052 }
1053 
1055 {
1056  struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
1057 
1058  mutex_lock(&sess->cmdsn_mutex);
1059  list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
1060  &sess->sess_ooo_cmdsn_list, ooo_list) {
1061 
1062  list_del(&ooo_cmdsn->ooo_list);
1063  kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
1064  }
1065  mutex_unlock(&sess->cmdsn_mutex);
1066 }
1067 
1069  struct iscsi_session *sess,
1070  struct iscsi_cmd *cmd,
1071  u32 cmdsn)
1072 {
1073  int batch = 0;
1074  struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL, *ooo_tail = NULL;
1075 
1076  cmd->deferred_i_state = cmd->i_state;
1078  cmd->cmd_flags |= ICF_OOO_CMDSN;
1079 
1080  if (list_empty(&sess->sess_ooo_cmdsn_list))
1081  batch = 1;
1082  else {
1083  ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
1084  typeof(*ooo_tail), ooo_list);
1085  if (ooo_tail->cmdsn != (cmdsn - 1))
1086  batch = 1;
1087  }
1088 
1089  ooo_cmdsn = iscsit_allocate_ooo_cmdsn();
1090  if (!ooo_cmdsn)
1092 
1093  ooo_cmdsn->cmd = cmd;
1094  ooo_cmdsn->batch_count = (batch) ?
1095  (cmdsn - sess->exp_cmd_sn) : 1;
1096  ooo_cmdsn->cid = cmd->conn->cid;
1097  ooo_cmdsn->exp_cmdsn = sess->exp_cmd_sn;
1098  ooo_cmdsn->cmdsn = cmdsn;
1099 
1100  if (iscsit_attach_ooo_cmdsn(sess, ooo_cmdsn) < 0) {
1101  kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
1103  }
1104 
1105  return CMDSN_HIGHER_THAN_EXP;
1106 }
1107 
1108 static int iscsit_set_dataout_timeout_values(
1109  struct iscsi_cmd *cmd,
1110  u32 *offset,
1111  u32 *length)
1112 {
1113  struct iscsi_conn *conn = cmd->conn;
1114  struct iscsi_r2t *r2t;
1115 
1116  if (cmd->unsolicited_data) {
1117  *offset = 0;
1118  *length = (conn->sess->sess_ops->FirstBurstLength >
1119  cmd->se_cmd.data_length) ?
1120  cmd->se_cmd.data_length :
1121  conn->sess->sess_ops->FirstBurstLength;
1122  return 0;
1123  }
1124 
1125  spin_lock_bh(&cmd->r2t_lock);
1126  if (list_empty(&cmd->cmd_r2t_list)) {
1127  pr_err("cmd->cmd_r2t_list is empty!\n");
1128  spin_unlock_bh(&cmd->r2t_lock);
1129  return -1;
1130  }
1131 
1133  if (r2t->sent_r2t && !r2t->recovery_r2t && !r2t->seq_complete) {
1134  *offset = r2t->offset;
1135  *length = r2t->xfer_len;
1136  spin_unlock_bh(&cmd->r2t_lock);
1137  return 0;
1138  }
1139  }
1140  spin_unlock_bh(&cmd->r2t_lock);
1141 
1142  pr_err("Unable to locate any incomplete DataOUT"
1143  " sequences for ITT: 0x%08x.\n", cmd->init_task_tag);
1144 
1145  return -1;
1146 }
1147 
1148 /*
1149  * NOTE: Called from interrupt (timer) context.
1150  */
1151 static void iscsit_handle_dataout_timeout(unsigned long data)
1152 {
1153  u32 pdu_length = 0, pdu_offset = 0;
1154  u32 r2t_length = 0, r2t_offset = 0;
1155  struct iscsi_cmd *cmd = (struct iscsi_cmd *) data;
1156  struct iscsi_conn *conn = cmd->conn;
1157  struct iscsi_session *sess = NULL;
1158  struct iscsi_node_attrib *na;
1159 
1161 
1162  spin_lock_bh(&cmd->dataout_timeout_lock);
1163  if (cmd->dataout_timer_flags & ISCSI_TF_STOP) {
1164  spin_unlock_bh(&cmd->dataout_timeout_lock);
1166  return;
1167  }
1169  sess = conn->sess;
1170  na = iscsit_tpg_get_node_attrib(sess);
1171 
1172  if (!sess->sess_ops->ErrorRecoveryLevel) {
1173  pr_debug("Unable to recover from DataOut timeout while"
1174  " in ERL=0.\n");
1175  goto failure;
1176  }
1177 
1179  pr_debug("Command ITT: 0x%08x exceeded max retries"
1180  " for DataOUT timeout %u, closing iSCSI connection.\n",
1182  goto failure;
1183  }
1184 
1186 
1187  if (conn->sess->sess_ops->DataSequenceInOrder) {
1188  if (conn->sess->sess_ops->DataPDUInOrder) {
1189  pdu_offset = cmd->write_data_done;
1190  if ((pdu_offset + (conn->sess->sess_ops->MaxBurstLength -
1191  cmd->next_burst_len)) > cmd->se_cmd.data_length)
1192  pdu_length = (cmd->se_cmd.data_length -
1193  cmd->write_data_done);
1194  else
1195  pdu_length = (conn->sess->sess_ops->MaxBurstLength -
1196  cmd->next_burst_len);
1197  } else {
1198  pdu_offset = cmd->seq_start_offset;
1199  pdu_length = (cmd->seq_end_offset -
1200  cmd->seq_start_offset);
1201  }
1202  } else {
1203  if (iscsit_set_dataout_timeout_values(cmd, &pdu_offset,
1204  &pdu_length) < 0)
1205  goto failure;
1206  }
1207 
1208  if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
1209  &r2t_offset, &r2t_length) < 0)
1210  goto failure;
1211 
1212  pr_debug("Command ITT: 0x%08x timed out waiting for"
1213  " completion of %sDataOUT Sequence Offset: %u, Length: %u\n",
1214  cmd->init_task_tag, (cmd->unsolicited_data) ? "Unsolicited " :
1215  "", r2t_offset, r2t_length);
1216 
1217  if (iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length) < 0)
1218  goto failure;
1219 
1220  iscsit_start_dataout_timer(cmd, conn);
1221  spin_unlock_bh(&cmd->dataout_timeout_lock);
1223 
1224  return;
1225 
1226 failure:
1227  spin_unlock_bh(&cmd->dataout_timeout_lock);
1230 }
1231 
1233 {
1234  struct iscsi_conn *conn = cmd->conn;
1235  struct iscsi_session *sess = conn->sess;
1236  struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1237 
1238  spin_lock_bh(&cmd->dataout_timeout_lock);
1239  if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
1240  spin_unlock_bh(&cmd->dataout_timeout_lock);
1241  return;
1242  }
1243 
1244  mod_timer(&cmd->dataout_timer,
1245  (get_jiffies_64() + na->dataout_timeout * HZ));
1246  pr_debug("Updated DataOUT timer for ITT: 0x%08x",
1247  cmd->init_task_tag);
1248  spin_unlock_bh(&cmd->dataout_timeout_lock);
1249 }
1250 
1251 /*
1252  * Called with cmd->dataout_timeout_lock held.
1253  */
1255  struct iscsi_cmd *cmd,
1256  struct iscsi_conn *conn)
1257 {
1258  struct iscsi_session *sess = conn->sess;
1259  struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1260 
1262  return;
1263 
1264  pr_debug("Starting DataOUT timer for ITT: 0x%08x on"
1265  " CID: %hu.\n", cmd->init_task_tag, conn->cid);
1266 
1267  init_timer(&cmd->dataout_timer);
1268  cmd->dataout_timer.expires = (get_jiffies_64() + na->dataout_timeout * HZ);
1269  cmd->dataout_timer.data = (unsigned long)cmd;
1270  cmd->dataout_timer.function = iscsit_handle_dataout_timeout;
1273  add_timer(&cmd->dataout_timer);
1274 }
1275 
1277 {
1278  spin_lock_bh(&cmd->dataout_timeout_lock);
1279  if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
1280  spin_unlock_bh(&cmd->dataout_timeout_lock);
1281  return;
1282  }
1284  spin_unlock_bh(&cmd->dataout_timeout_lock);
1285 
1287 
1288  spin_lock_bh(&cmd->dataout_timeout_lock);
1290  pr_debug("Stopped DataOUT Timer for ITT: 0x%08x\n",
1291  cmd->init_task_tag);
1292  spin_unlock_bh(&cmd->dataout_timeout_lock);
1293 }