1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e48354ceSNicholas Bellinger /*******************************************************************************
3e48354ceSNicholas Bellinger * This file contains error recovery level one used by the iSCSI Target driver.
4e48354ceSNicholas Bellinger *
54c76251eSNicholas Bellinger * (c) Copyright 2007-2013 Datera, Inc.
6e48354ceSNicholas Bellinger *
7e48354ceSNicholas Bellinger * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8e48354ceSNicholas Bellinger *
9e48354ceSNicholas Bellinger ******************************************************************************/
10e48354ceSNicholas Bellinger
11e48354ceSNicholas Bellinger #include <linux/list.h>
128dcf07beSBart Van Assche #include <linux/slab.h>
13e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
14e48354ceSNicholas Bellinger #include <target/target_core_base.h>
15c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
163e1c81a9SNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
17e48354ceSNicholas Bellinger
1867f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
19e48354ceSNicholas Bellinger #include "iscsi_target_seq_pdu_list.h"
20e48354ceSNicholas Bellinger #include "iscsi_target_datain_values.h"
21e48354ceSNicholas Bellinger #include "iscsi_target_device.h"
22e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
23e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
24e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
25e48354ceSNicholas Bellinger #include "iscsi_target_erl1.h"
26e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h"
27e48354ceSNicholas Bellinger #include "iscsi_target.h"
28e48354ceSNicholas Bellinger
29919765e9SBart Van Assche #define OFFLOAD_BUF_SIZE 32768U
30e48354ceSNicholas Bellinger
31e48354ceSNicholas Bellinger /*
32e48354ceSNicholas Bellinger * Used to dump excess datain payload for certain error recovery
33e48354ceSNicholas Bellinger * situations. Receive in OFFLOAD_BUF_SIZE max of datain per rx_data().
34e48354ceSNicholas Bellinger *
35e48354ceSNicholas Bellinger * dump_padding_digest denotes if padding and data digests need
36e48354ceSNicholas Bellinger * to be dumped.
37e48354ceSNicholas Bellinger */
iscsit_dump_data_payload(struct iscsit_conn * conn,u32 buf_len,int dump_padding_digest)38e48354ceSNicholas Bellinger int iscsit_dump_data_payload(
39be36d683SMax Gurtovoy struct iscsit_conn *conn,
40e48354ceSNicholas Bellinger u32 buf_len,
41e48354ceSNicholas Bellinger int dump_padding_digest)
42e48354ceSNicholas Bellinger {
43658c3122SBart Van Assche char *buf;
44e48354ceSNicholas Bellinger int ret = DATAOUT_WITHIN_COMMAND_RECOVERY, rx_got;
45658c3122SBart Van Assche u32 length, offset = 0, size;
46e48354ceSNicholas Bellinger struct kvec iov;
47e48354ceSNicholas Bellinger
483e1c81a9SNicholas Bellinger if (conn->sess->sess_ops->RDMAExtensions)
493e1c81a9SNicholas Bellinger return 0;
503e1c81a9SNicholas Bellinger
51658c3122SBart Van Assche if (dump_padding_digest) {
52658c3122SBart Van Assche buf_len = ALIGN(buf_len, 4);
53658c3122SBart Van Assche if (conn->conn_ops->DataDigest)
54658c3122SBart Van Assche buf_len += ISCSI_CRC_LEN;
55658c3122SBart Van Assche }
56658c3122SBart Van Assche
57919765e9SBart Van Assche length = min(buf_len, OFFLOAD_BUF_SIZE);
58e48354ceSNicholas Bellinger
59e48354ceSNicholas Bellinger buf = kzalloc(length, GFP_ATOMIC);
60e48354ceSNicholas Bellinger if (!buf) {
61e48354ceSNicholas Bellinger pr_err("Unable to allocate %u bytes for offload"
62e48354ceSNicholas Bellinger " buffer.\n", length);
63e48354ceSNicholas Bellinger return -1;
64e48354ceSNicholas Bellinger }
65e48354ceSNicholas Bellinger memset(&iov, 0, sizeof(struct kvec));
66e48354ceSNicholas Bellinger
67e48354ceSNicholas Bellinger while (offset < buf_len) {
68919765e9SBart Van Assche size = min(buf_len - offset, length);
69e48354ceSNicholas Bellinger
70e48354ceSNicholas Bellinger iov.iov_len = size;
71e48354ceSNicholas Bellinger iov.iov_base = buf;
72e48354ceSNicholas Bellinger
73e48354ceSNicholas Bellinger rx_got = rx_data(conn, &iov, 1, size);
74e48354ceSNicholas Bellinger if (rx_got != size) {
75e48354ceSNicholas Bellinger ret = DATAOUT_CANNOT_RECOVER;
76658c3122SBart Van Assche break;
77e48354ceSNicholas Bellinger }
78e48354ceSNicholas Bellinger
79e48354ceSNicholas Bellinger offset += size;
80e48354ceSNicholas Bellinger }
81e48354ceSNicholas Bellinger
82e48354ceSNicholas Bellinger kfree(buf);
83e48354ceSNicholas Bellinger return ret;
84e48354ceSNicholas Bellinger }
85e48354ceSNicholas Bellinger
86e48354ceSNicholas Bellinger /*
87e48354ceSNicholas Bellinger * Used for retransmitting R2Ts from a R2T SNACK request.
88e48354ceSNicholas Bellinger */
iscsit_send_recovery_r2t_for_snack(struct iscsit_cmd * cmd,struct iscsi_r2t * r2t)89e48354ceSNicholas Bellinger static int iscsit_send_recovery_r2t_for_snack(
9066cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
91e48354ceSNicholas Bellinger struct iscsi_r2t *r2t)
92e48354ceSNicholas Bellinger {
93e48354ceSNicholas Bellinger /*
94e48354ceSNicholas Bellinger * If the struct iscsi_r2t has not been sent yet, we can safely
95e48354ceSNicholas Bellinger * ignore retransmission
96e48354ceSNicholas Bellinger * of the R2TSN in question.
97e48354ceSNicholas Bellinger */
98e48354ceSNicholas Bellinger spin_lock_bh(&cmd->r2t_lock);
99e48354ceSNicholas Bellinger if (!r2t->sent_r2t) {
100e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->r2t_lock);
101e48354ceSNicholas Bellinger return 0;
102e48354ceSNicholas Bellinger }
103e48354ceSNicholas Bellinger r2t->sent_r2t = 0;
104e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->r2t_lock);
105e48354ceSNicholas Bellinger
106e48354ceSNicholas Bellinger iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
107e48354ceSNicholas Bellinger
108e48354ceSNicholas Bellinger return 0;
109e48354ceSNicholas Bellinger }
110e48354ceSNicholas Bellinger
iscsit_handle_r2t_snack(struct iscsit_cmd * cmd,unsigned char * buf,u32 begrun,u32 runlength)111e48354ceSNicholas Bellinger static int iscsit_handle_r2t_snack(
11266cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
113e48354ceSNicholas Bellinger unsigned char *buf,
114e48354ceSNicholas Bellinger u32 begrun,
115e48354ceSNicholas Bellinger u32 runlength)
116e48354ceSNicholas Bellinger {
117e48354ceSNicholas Bellinger u32 last_r2tsn;
118e48354ceSNicholas Bellinger struct iscsi_r2t *r2t;
119e48354ceSNicholas Bellinger
120e48354ceSNicholas Bellinger /*
121e48354ceSNicholas Bellinger * Make sure the initiator is not requesting retransmission
122e48354ceSNicholas Bellinger * of R2TSNs already acknowledged by a TMR TASK_REASSIGN.
123e48354ceSNicholas Bellinger */
124e48354ceSNicholas Bellinger if ((cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
125e48354ceSNicholas Bellinger (begrun <= cmd->acked_data_sn)) {
126e48354ceSNicholas Bellinger pr_err("ITT: 0x%08x, R2T SNACK requesting"
127e48354ceSNicholas Bellinger " retransmission of R2TSN: 0x%08x to 0x%08x but already"
128e48354ceSNicholas Bellinger " acked to R2TSN: 0x%08x by TMR TASK_REASSIGN,"
129e48354ceSNicholas Bellinger " protocol error.\n", cmd->init_task_tag, begrun,
130e48354ceSNicholas Bellinger (begrun + runlength), cmd->acked_data_sn);
131e48354ceSNicholas Bellinger
1325a342521SBart Van Assche return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
133e48354ceSNicholas Bellinger }
134e48354ceSNicholas Bellinger
135e48354ceSNicholas Bellinger if (runlength) {
136e48354ceSNicholas Bellinger if ((begrun + runlength) > cmd->r2t_sn) {
137e48354ceSNicholas Bellinger pr_err("Command ITT: 0x%08x received R2T SNACK"
138e48354ceSNicholas Bellinger " with BegRun: 0x%08x, RunLength: 0x%08x, exceeds"
139e48354ceSNicholas Bellinger " current R2TSN: 0x%08x, protocol error.\n",
140e48354ceSNicholas Bellinger cmd->init_task_tag, begrun, runlength, cmd->r2t_sn);
141ba159914SNicholas Bellinger return iscsit_reject_cmd(cmd,
142ba159914SNicholas Bellinger ISCSI_REASON_BOOKMARK_INVALID, buf);
143e48354ceSNicholas Bellinger }
144e48354ceSNicholas Bellinger last_r2tsn = (begrun + runlength);
145e48354ceSNicholas Bellinger } else
146e48354ceSNicholas Bellinger last_r2tsn = cmd->r2t_sn;
147e48354ceSNicholas Bellinger
148e48354ceSNicholas Bellinger while (begrun < last_r2tsn) {
149e48354ceSNicholas Bellinger r2t = iscsit_get_holder_for_r2tsn(cmd, begrun);
150e48354ceSNicholas Bellinger if (!r2t)
151e48354ceSNicholas Bellinger return -1;
152e48354ceSNicholas Bellinger if (iscsit_send_recovery_r2t_for_snack(cmd, r2t) < 0)
153e48354ceSNicholas Bellinger return -1;
154e48354ceSNicholas Bellinger
155e48354ceSNicholas Bellinger begrun++;
156e48354ceSNicholas Bellinger }
157e48354ceSNicholas Bellinger
158e48354ceSNicholas Bellinger return 0;
159e48354ceSNicholas Bellinger }
160e48354ceSNicholas Bellinger
161e48354ceSNicholas Bellinger /*
162e48354ceSNicholas Bellinger * Generates Offsets and NextBurstLength based on Begrun and Runlength
163e48354ceSNicholas Bellinger * carried in a Data SNACK or ExpDataSN in TMR TASK_REASSIGN.
164e48354ceSNicholas Bellinger *
165e48354ceSNicholas Bellinger * For DataSequenceInOrder=Yes and DataPDUInOrder=[Yes,No] only.
166e48354ceSNicholas Bellinger *
167e48354ceSNicholas Bellinger * FIXME: How is this handled for a RData SNACK?
168e48354ceSNicholas Bellinger */
iscsit_create_recovery_datain_values_datasequenceinorder_yes(struct iscsit_cmd * cmd,struct iscsi_datain_req * dr)169e48354ceSNicholas Bellinger int iscsit_create_recovery_datain_values_datasequenceinorder_yes(
17066cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
171e48354ceSNicholas Bellinger struct iscsi_datain_req *dr)
172e48354ceSNicholas Bellinger {
173e48354ceSNicholas Bellinger u32 data_sn = 0, data_sn_count = 0;
174e48354ceSNicholas Bellinger u32 pdu_start = 0, seq_no = 0;
175e48354ceSNicholas Bellinger u32 begrun = dr->begrun;
176be36d683SMax Gurtovoy struct iscsit_conn *conn = cmd->conn;
177e48354ceSNicholas Bellinger
178e48354ceSNicholas Bellinger while (begrun > data_sn++) {
179e48354ceSNicholas Bellinger data_sn_count++;
180e48354ceSNicholas Bellinger if ((dr->next_burst_len +
181e48354ceSNicholas Bellinger conn->conn_ops->MaxRecvDataSegmentLength) <
182e48354ceSNicholas Bellinger conn->sess->sess_ops->MaxBurstLength) {
183e48354ceSNicholas Bellinger dr->read_data_done +=
184e48354ceSNicholas Bellinger conn->conn_ops->MaxRecvDataSegmentLength;
185e48354ceSNicholas Bellinger dr->next_burst_len +=
186e48354ceSNicholas Bellinger conn->conn_ops->MaxRecvDataSegmentLength;
187e48354ceSNicholas Bellinger } else {
188e48354ceSNicholas Bellinger dr->read_data_done +=
189e48354ceSNicholas Bellinger (conn->sess->sess_ops->MaxBurstLength -
190e48354ceSNicholas Bellinger dr->next_burst_len);
191e48354ceSNicholas Bellinger dr->next_burst_len = 0;
192e48354ceSNicholas Bellinger pdu_start += data_sn_count;
193e48354ceSNicholas Bellinger data_sn_count = 0;
194e48354ceSNicholas Bellinger seq_no++;
195e48354ceSNicholas Bellinger }
196e48354ceSNicholas Bellinger }
197e48354ceSNicholas Bellinger
198e48354ceSNicholas Bellinger if (!conn->sess->sess_ops->DataPDUInOrder) {
199e48354ceSNicholas Bellinger cmd->seq_no = seq_no;
200e48354ceSNicholas Bellinger cmd->pdu_start = pdu_start;
201e48354ceSNicholas Bellinger cmd->pdu_send_order = data_sn_count;
202e48354ceSNicholas Bellinger }
203e48354ceSNicholas Bellinger
204e48354ceSNicholas Bellinger return 0;
205e48354ceSNicholas Bellinger }
206e48354ceSNicholas Bellinger
207e48354ceSNicholas Bellinger /*
208e48354ceSNicholas Bellinger * Generates Offsets and NextBurstLength based on Begrun and Runlength
209e48354ceSNicholas Bellinger * carried in a Data SNACK or ExpDataSN in TMR TASK_REASSIGN.
210e48354ceSNicholas Bellinger *
211e48354ceSNicholas Bellinger * For DataSequenceInOrder=No and DataPDUInOrder=[Yes,No] only.
212e48354ceSNicholas Bellinger *
213e48354ceSNicholas Bellinger * FIXME: How is this handled for a RData SNACK?
214e48354ceSNicholas Bellinger */
iscsit_create_recovery_datain_values_datasequenceinorder_no(struct iscsit_cmd * cmd,struct iscsi_datain_req * dr)215e48354ceSNicholas Bellinger int iscsit_create_recovery_datain_values_datasequenceinorder_no(
21666cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
217e48354ceSNicholas Bellinger struct iscsi_datain_req *dr)
218e48354ceSNicholas Bellinger {
219e48354ceSNicholas Bellinger int found_seq = 0, i;
220e48354ceSNicholas Bellinger u32 data_sn, read_data_done = 0, seq_send_order = 0;
221e48354ceSNicholas Bellinger u32 begrun = dr->begrun;
222e48354ceSNicholas Bellinger u32 runlength = dr->runlength;
223be36d683SMax Gurtovoy struct iscsit_conn *conn = cmd->conn;
224e48354ceSNicholas Bellinger struct iscsi_seq *first_seq = NULL, *seq = NULL;
225e48354ceSNicholas Bellinger
226e48354ceSNicholas Bellinger if (!cmd->seq_list) {
22766cd9d4eSMax Gurtovoy pr_err("struct iscsit_cmd->seq_list is NULL!\n");
228e48354ceSNicholas Bellinger return -1;
229e48354ceSNicholas Bellinger }
230e48354ceSNicholas Bellinger
231e48354ceSNicholas Bellinger /*
232e48354ceSNicholas Bellinger * Calculate read_data_done for all sequences containing a
233e48354ceSNicholas Bellinger * first_datasn and last_datasn less than the BegRun.
234e48354ceSNicholas Bellinger *
235e48354ceSNicholas Bellinger * Locate the struct iscsi_seq the BegRun lies within and calculate
236e48354ceSNicholas Bellinger * NextBurstLenghth up to the DataSN based on MaxRecvDataSegmentLength.
237e48354ceSNicholas Bellinger *
238e48354ceSNicholas Bellinger * Also use struct iscsi_seq->seq_send_order to determine where to start.
239e48354ceSNicholas Bellinger */
240e48354ceSNicholas Bellinger for (i = 0; i < cmd->seq_count; i++) {
241e48354ceSNicholas Bellinger seq = &cmd->seq_list[i];
242e48354ceSNicholas Bellinger
243e48354ceSNicholas Bellinger if (!seq->seq_send_order)
244e48354ceSNicholas Bellinger first_seq = seq;
245e48354ceSNicholas Bellinger
246e48354ceSNicholas Bellinger /*
247e48354ceSNicholas Bellinger * No data has been transferred for this DataIN sequence, so the
248e48354ceSNicholas Bellinger * seq->first_datasn and seq->last_datasn have not been set.
249e48354ceSNicholas Bellinger */
250e48354ceSNicholas Bellinger if (!seq->sent) {
251e48354ceSNicholas Bellinger pr_err("Ignoring non-sent sequence 0x%08x ->"
252e48354ceSNicholas Bellinger " 0x%08x\n\n", seq->first_datasn,
253e48354ceSNicholas Bellinger seq->last_datasn);
254e48354ceSNicholas Bellinger continue;
255e48354ceSNicholas Bellinger }
256e48354ceSNicholas Bellinger
257e48354ceSNicholas Bellinger /*
258e48354ceSNicholas Bellinger * This DataIN sequence is precedes the received BegRun, add the
259e48354ceSNicholas Bellinger * total xfer_len of the sequence to read_data_done and reset
260e48354ceSNicholas Bellinger * seq->pdu_send_order.
261e48354ceSNicholas Bellinger */
262e48354ceSNicholas Bellinger if ((seq->first_datasn < begrun) &&
263e48354ceSNicholas Bellinger (seq->last_datasn < begrun)) {
264e48354ceSNicholas Bellinger pr_err("Pre BegRun sequence 0x%08x ->"
265e48354ceSNicholas Bellinger " 0x%08x\n", seq->first_datasn,
266e48354ceSNicholas Bellinger seq->last_datasn);
2678b1e1244SAndy Grover
268e48354ceSNicholas Bellinger read_data_done += cmd->seq_list[i].xfer_len;
269e48354ceSNicholas Bellinger seq->next_burst_len = seq->pdu_send_order = 0;
270e48354ceSNicholas Bellinger continue;
271e48354ceSNicholas Bellinger }
272e48354ceSNicholas Bellinger
273e48354ceSNicholas Bellinger /*
274e48354ceSNicholas Bellinger * The BegRun lies within this DataIN sequence.
275e48354ceSNicholas Bellinger */
276e48354ceSNicholas Bellinger if ((seq->first_datasn <= begrun) &&
277e48354ceSNicholas Bellinger (seq->last_datasn >= begrun)) {
278e48354ceSNicholas Bellinger pr_err("Found sequence begrun: 0x%08x in"
279e48354ceSNicholas Bellinger " 0x%08x -> 0x%08x\n", begrun,
280e48354ceSNicholas Bellinger seq->first_datasn, seq->last_datasn);
2818b1e1244SAndy Grover
282e48354ceSNicholas Bellinger seq_send_order = seq->seq_send_order;
283e48354ceSNicholas Bellinger data_sn = seq->first_datasn;
284e48354ceSNicholas Bellinger seq->next_burst_len = seq->pdu_send_order = 0;
285e48354ceSNicholas Bellinger found_seq = 1;
286e48354ceSNicholas Bellinger
287e48354ceSNicholas Bellinger /*
288e48354ceSNicholas Bellinger * For DataPDUInOrder=Yes, while the first DataSN of
289e48354ceSNicholas Bellinger * the sequence is less than the received BegRun, add
290e48354ceSNicholas Bellinger * the MaxRecvDataSegmentLength to read_data_done and
291e48354ceSNicholas Bellinger * to the sequence's next_burst_len;
292e48354ceSNicholas Bellinger *
293e48354ceSNicholas Bellinger * For DataPDUInOrder=No, while the first DataSN of the
294e48354ceSNicholas Bellinger * sequence is less than the received BegRun, find the
295e48354ceSNicholas Bellinger * struct iscsi_pdu of the DataSN in question and add the
296e48354ceSNicholas Bellinger * MaxRecvDataSegmentLength to read_data_done and to the
297e48354ceSNicholas Bellinger * sequence's next_burst_len;
298e48354ceSNicholas Bellinger */
299e48354ceSNicholas Bellinger if (conn->sess->sess_ops->DataPDUInOrder) {
300e48354ceSNicholas Bellinger while (data_sn < begrun) {
301e48354ceSNicholas Bellinger seq->pdu_send_order++;
302e48354ceSNicholas Bellinger read_data_done +=
303e48354ceSNicholas Bellinger conn->conn_ops->MaxRecvDataSegmentLength;
304e48354ceSNicholas Bellinger seq->next_burst_len +=
305e48354ceSNicholas Bellinger conn->conn_ops->MaxRecvDataSegmentLength;
306e48354ceSNicholas Bellinger data_sn++;
307e48354ceSNicholas Bellinger }
308e48354ceSNicholas Bellinger } else {
309e48354ceSNicholas Bellinger int j;
310e48354ceSNicholas Bellinger struct iscsi_pdu *pdu;
311e48354ceSNicholas Bellinger
312e48354ceSNicholas Bellinger while (data_sn < begrun) {
313e48354ceSNicholas Bellinger seq->pdu_send_order++;
314e48354ceSNicholas Bellinger
315e48354ceSNicholas Bellinger for (j = 0; j < seq->pdu_count; j++) {
316e48354ceSNicholas Bellinger pdu = &cmd->pdu_list[
317e48354ceSNicholas Bellinger seq->pdu_start + j];
318e48354ceSNicholas Bellinger if (pdu->data_sn == data_sn) {
319e48354ceSNicholas Bellinger read_data_done +=
320e48354ceSNicholas Bellinger pdu->length;
321e48354ceSNicholas Bellinger seq->next_burst_len +=
322e48354ceSNicholas Bellinger pdu->length;
323e48354ceSNicholas Bellinger }
324e48354ceSNicholas Bellinger }
325e48354ceSNicholas Bellinger data_sn++;
326e48354ceSNicholas Bellinger }
327e48354ceSNicholas Bellinger }
328e48354ceSNicholas Bellinger continue;
329e48354ceSNicholas Bellinger }
330e48354ceSNicholas Bellinger
331e48354ceSNicholas Bellinger /*
332e48354ceSNicholas Bellinger * This DataIN sequence is larger than the received BegRun,
333e48354ceSNicholas Bellinger * reset seq->pdu_send_order and continue.
334e48354ceSNicholas Bellinger */
335e48354ceSNicholas Bellinger if ((seq->first_datasn > begrun) ||
336e48354ceSNicholas Bellinger (seq->last_datasn > begrun)) {
337e48354ceSNicholas Bellinger pr_err("Post BegRun sequence 0x%08x -> 0x%08x\n",
338e48354ceSNicholas Bellinger seq->first_datasn, seq->last_datasn);
3398b1e1244SAndy Grover
340e48354ceSNicholas Bellinger seq->next_burst_len = seq->pdu_send_order = 0;
341e48354ceSNicholas Bellinger continue;
342e48354ceSNicholas Bellinger }
343e48354ceSNicholas Bellinger }
344e48354ceSNicholas Bellinger
345e48354ceSNicholas Bellinger if (!found_seq) {
346e48354ceSNicholas Bellinger if (!begrun) {
347e48354ceSNicholas Bellinger if (!first_seq) {
348e48354ceSNicholas Bellinger pr_err("ITT: 0x%08x, Begrun: 0x%08x"
349e48354ceSNicholas Bellinger " but first_seq is NULL\n",
350e48354ceSNicholas Bellinger cmd->init_task_tag, begrun);
351e48354ceSNicholas Bellinger return -1;
352e48354ceSNicholas Bellinger }
353e48354ceSNicholas Bellinger seq_send_order = first_seq->seq_send_order;
354e48354ceSNicholas Bellinger seq->next_burst_len = seq->pdu_send_order = 0;
355e48354ceSNicholas Bellinger goto done;
356e48354ceSNicholas Bellinger }
357e48354ceSNicholas Bellinger
358e48354ceSNicholas Bellinger pr_err("Unable to locate struct iscsi_seq for ITT: 0x%08x,"
359e48354ceSNicholas Bellinger " BegRun: 0x%08x, RunLength: 0x%08x while"
360e48354ceSNicholas Bellinger " DataSequenceInOrder=No and DataPDUInOrder=%s.\n",
361e48354ceSNicholas Bellinger cmd->init_task_tag, begrun, runlength,
362e48354ceSNicholas Bellinger (conn->sess->sess_ops->DataPDUInOrder) ? "Yes" : "No");
363e48354ceSNicholas Bellinger return -1;
364e48354ceSNicholas Bellinger }
365e48354ceSNicholas Bellinger
366e48354ceSNicholas Bellinger done:
367e48354ceSNicholas Bellinger dr->read_data_done = read_data_done;
368e48354ceSNicholas Bellinger dr->seq_send_order = seq_send_order;
369e48354ceSNicholas Bellinger
370e48354ceSNicholas Bellinger return 0;
371e48354ceSNicholas Bellinger }
372e48354ceSNicholas Bellinger
iscsit_handle_recovery_datain(struct iscsit_cmd * cmd,unsigned char * buf,u32 begrun,u32 runlength)373e48354ceSNicholas Bellinger static int iscsit_handle_recovery_datain(
37466cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
375e48354ceSNicholas Bellinger unsigned char *buf,
376e48354ceSNicholas Bellinger u32 begrun,
377e48354ceSNicholas Bellinger u32 runlength)
378e48354ceSNicholas Bellinger {
379be36d683SMax Gurtovoy struct iscsit_conn *conn = cmd->conn;
380e48354ceSNicholas Bellinger struct iscsi_datain_req *dr;
381e48354ceSNicholas Bellinger struct se_cmd *se_cmd = &cmd->se_cmd;
382e48354ceSNicholas Bellinger
3837d680f3bSChristoph Hellwig if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
384e48354ceSNicholas Bellinger pr_err("Ignoring ITT: 0x%08x Data SNACK\n",
385e48354ceSNicholas Bellinger cmd->init_task_tag);
386e48354ceSNicholas Bellinger return 0;
387e48354ceSNicholas Bellinger }
388e48354ceSNicholas Bellinger
389e48354ceSNicholas Bellinger /*
390e48354ceSNicholas Bellinger * Make sure the initiator is not requesting retransmission
391e48354ceSNicholas Bellinger * of DataSNs already acknowledged by a Data ACK SNACK.
392e48354ceSNicholas Bellinger */
393e48354ceSNicholas Bellinger if ((cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
394e48354ceSNicholas Bellinger (begrun <= cmd->acked_data_sn)) {
395e48354ceSNicholas Bellinger pr_err("ITT: 0x%08x, Data SNACK requesting"
396e48354ceSNicholas Bellinger " retransmission of DataSN: 0x%08x to 0x%08x but"
397e48354ceSNicholas Bellinger " already acked to DataSN: 0x%08x by Data ACK SNACK,"
398e48354ceSNicholas Bellinger " protocol error.\n", cmd->init_task_tag, begrun,
399e48354ceSNicholas Bellinger (begrun + runlength), cmd->acked_data_sn);
400e48354ceSNicholas Bellinger
401ba159914SNicholas Bellinger return iscsit_reject_cmd(cmd, ISCSI_REASON_PROTOCOL_ERROR, buf);
402e48354ceSNicholas Bellinger }
403e48354ceSNicholas Bellinger
404e48354ceSNicholas Bellinger /*
405e48354ceSNicholas Bellinger * Make sure BegRun and RunLength in the Data SNACK are sane.
406e48354ceSNicholas Bellinger * Note: (cmd->data_sn - 1) will carry the maximum DataSN sent.
407e48354ceSNicholas Bellinger */
408e48354ceSNicholas Bellinger if ((begrun + runlength) > (cmd->data_sn - 1)) {
409e48354ceSNicholas Bellinger pr_err("Initiator requesting BegRun: 0x%08x, RunLength"
410e48354ceSNicholas Bellinger ": 0x%08x greater than maximum DataSN: 0x%08x.\n",
411e48354ceSNicholas Bellinger begrun, runlength, (cmd->data_sn - 1));
412ba159914SNicholas Bellinger return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_INVALID,
413ba159914SNicholas Bellinger buf);
414e48354ceSNicholas Bellinger }
415e48354ceSNicholas Bellinger
416e48354ceSNicholas Bellinger dr = iscsit_allocate_datain_req();
417e48354ceSNicholas Bellinger if (!dr)
418ba159914SNicholas Bellinger return iscsit_reject_cmd(cmd, ISCSI_REASON_BOOKMARK_NO_RESOURCES,
419ba159914SNicholas Bellinger buf);
420e48354ceSNicholas Bellinger
421e48354ceSNicholas Bellinger dr->data_sn = dr->begrun = begrun;
422e48354ceSNicholas Bellinger dr->runlength = runlength;
423e48354ceSNicholas Bellinger dr->generate_recovery_values = 1;
424e48354ceSNicholas Bellinger dr->recovery = DATAIN_WITHIN_COMMAND_RECOVERY;
425e48354ceSNicholas Bellinger
426e48354ceSNicholas Bellinger iscsit_attach_datain_req(cmd, dr);
427e48354ceSNicholas Bellinger
428e48354ceSNicholas Bellinger cmd->i_state = ISTATE_SEND_DATAIN;
429e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
430e48354ceSNicholas Bellinger
431e48354ceSNicholas Bellinger return 0;
432e48354ceSNicholas Bellinger }
433e48354ceSNicholas Bellinger
iscsit_handle_recovery_datain_or_r2t(struct iscsit_conn * conn,unsigned char * buf,itt_t init_task_tag,u32 targ_xfer_tag,u32 begrun,u32 runlength)434e48354ceSNicholas Bellinger int iscsit_handle_recovery_datain_or_r2t(
435be36d683SMax Gurtovoy struct iscsit_conn *conn,
436e48354ceSNicholas Bellinger unsigned char *buf,
43766c7db68SChristoph Hellwig itt_t init_task_tag,
438e48354ceSNicholas Bellinger u32 targ_xfer_tag,
439e48354ceSNicholas Bellinger u32 begrun,
440e48354ceSNicholas Bellinger u32 runlength)
441e48354ceSNicholas Bellinger {
44266cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd;
443e48354ceSNicholas Bellinger
444e48354ceSNicholas Bellinger cmd = iscsit_find_cmd_from_itt(conn, init_task_tag);
445e48354ceSNicholas Bellinger if (!cmd)
446e48354ceSNicholas Bellinger return 0;
447e48354ceSNicholas Bellinger
448e48354ceSNicholas Bellinger /*
449e48354ceSNicholas Bellinger * FIXME: This will not work for bidi commands.
450e48354ceSNicholas Bellinger */
451e48354ceSNicholas Bellinger switch (cmd->data_direction) {
452e48354ceSNicholas Bellinger case DMA_TO_DEVICE:
453e48354ceSNicholas Bellinger return iscsit_handle_r2t_snack(cmd, buf, begrun, runlength);
454e48354ceSNicholas Bellinger case DMA_FROM_DEVICE:
455e48354ceSNicholas Bellinger return iscsit_handle_recovery_datain(cmd, buf, begrun,
456e48354ceSNicholas Bellinger runlength);
457e48354ceSNicholas Bellinger default:
458e48354ceSNicholas Bellinger pr_err("Unknown cmd->data_direction: 0x%02x\n",
459e48354ceSNicholas Bellinger cmd->data_direction);
460e48354ceSNicholas Bellinger return -1;
461e48354ceSNicholas Bellinger }
462e48354ceSNicholas Bellinger
463e48354ceSNicholas Bellinger return 0;
464e48354ceSNicholas Bellinger }
465e48354ceSNicholas Bellinger
466e48354ceSNicholas Bellinger /* #warning FIXME: Status SNACK needs to be dependent on OPCODE!!! */
iscsit_handle_status_snack(struct iscsit_conn * conn,itt_t init_task_tag,u32 targ_xfer_tag,u32 begrun,u32 runlength)467e48354ceSNicholas Bellinger int iscsit_handle_status_snack(
468be36d683SMax Gurtovoy struct iscsit_conn *conn,
46966c7db68SChristoph Hellwig itt_t init_task_tag,
470e48354ceSNicholas Bellinger u32 targ_xfer_tag,
471e48354ceSNicholas Bellinger u32 begrun,
472e48354ceSNicholas Bellinger u32 runlength)
473e48354ceSNicholas Bellinger {
47466cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd = NULL;
475e48354ceSNicholas Bellinger u32 last_statsn;
476e48354ceSNicholas Bellinger int found_cmd;
477e48354ceSNicholas Bellinger
478752d8680SNicholas Bellinger if (!begrun) {
479752d8680SNicholas Bellinger begrun = conn->exp_statsn;
480752d8680SNicholas Bellinger } else if (conn->exp_statsn > begrun) {
481e48354ceSNicholas Bellinger pr_err("Got Status SNACK Begrun: 0x%08x, RunLength:"
482e48354ceSNicholas Bellinger " 0x%08x but already got ExpStatSN: 0x%08x on CID:"
483e48354ceSNicholas Bellinger " %hu.\n", begrun, runlength, conn->exp_statsn,
484e48354ceSNicholas Bellinger conn->cid);
485e48354ceSNicholas Bellinger return 0;
486e48354ceSNicholas Bellinger }
487e48354ceSNicholas Bellinger
488e48354ceSNicholas Bellinger last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);
489e48354ceSNicholas Bellinger
490e48354ceSNicholas Bellinger while (begrun < last_statsn) {
491e48354ceSNicholas Bellinger found_cmd = 0;
492e48354ceSNicholas Bellinger
493e48354ceSNicholas Bellinger spin_lock_bh(&conn->cmd_lock);
4942fbb471eSAndy Grover list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
495e48354ceSNicholas Bellinger if (cmd->stat_sn == begrun) {
496e48354ceSNicholas Bellinger found_cmd = 1;
497e48354ceSNicholas Bellinger break;
498e48354ceSNicholas Bellinger }
499e48354ceSNicholas Bellinger }
500e48354ceSNicholas Bellinger spin_unlock_bh(&conn->cmd_lock);
501e48354ceSNicholas Bellinger
502e48354ceSNicholas Bellinger if (!found_cmd) {
503e48354ceSNicholas Bellinger pr_err("Unable to find StatSN: 0x%08x for"
504e48354ceSNicholas Bellinger " a Status SNACK, assuming this was a"
505e48354ceSNicholas Bellinger " protactic SNACK for an untransmitted"
506e48354ceSNicholas Bellinger " StatSN, ignoring.\n", begrun);
507e48354ceSNicholas Bellinger begrun++;
508e48354ceSNicholas Bellinger continue;
509e48354ceSNicholas Bellinger }
510e48354ceSNicholas Bellinger
511e48354ceSNicholas Bellinger spin_lock_bh(&cmd->istate_lock);
512e48354ceSNicholas Bellinger if (cmd->i_state == ISTATE_SEND_DATAIN) {
513e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
514e48354ceSNicholas Bellinger pr_err("Ignoring Status SNACK for BegRun:"
515e48354ceSNicholas Bellinger " 0x%08x, RunLength: 0x%08x, assuming this was"
516e48354ceSNicholas Bellinger " a protactic SNACK for an untransmitted"
517e48354ceSNicholas Bellinger " StatSN\n", begrun, runlength);
518e48354ceSNicholas Bellinger begrun++;
519e48354ceSNicholas Bellinger continue;
520e48354ceSNicholas Bellinger }
521e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
522e48354ceSNicholas Bellinger
523e48354ceSNicholas Bellinger cmd->i_state = ISTATE_SEND_STATUS_RECOVERY;
524e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
525e48354ceSNicholas Bellinger begrun++;
526e48354ceSNicholas Bellinger }
527e48354ceSNicholas Bellinger
528e48354ceSNicholas Bellinger return 0;
529e48354ceSNicholas Bellinger }
530e48354ceSNicholas Bellinger
iscsit_handle_data_ack(struct iscsit_conn * conn,u32 targ_xfer_tag,u32 begrun,u32 runlength)531e48354ceSNicholas Bellinger int iscsit_handle_data_ack(
532be36d683SMax Gurtovoy struct iscsit_conn *conn,
533e48354ceSNicholas Bellinger u32 targ_xfer_tag,
534e48354ceSNicholas Bellinger u32 begrun,
535e48354ceSNicholas Bellinger u32 runlength)
536e48354ceSNicholas Bellinger {
53766cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd = NULL;
538e48354ceSNicholas Bellinger
539e48354ceSNicholas Bellinger cmd = iscsit_find_cmd_from_ttt(conn, targ_xfer_tag);
540e48354ceSNicholas Bellinger if (!cmd) {
541e48354ceSNicholas Bellinger pr_err("Data ACK SNACK for TTT: 0x%08x is"
542e48354ceSNicholas Bellinger " invalid.\n", targ_xfer_tag);
543e48354ceSNicholas Bellinger return -1;
544e48354ceSNicholas Bellinger }
545e48354ceSNicholas Bellinger
546e48354ceSNicholas Bellinger if (begrun <= cmd->acked_data_sn) {
547e48354ceSNicholas Bellinger pr_err("ITT: 0x%08x Data ACK SNACK BegRUN: 0x%08x is"
548e48354ceSNicholas Bellinger " less than the already acked DataSN: 0x%08x.\n",
549e48354ceSNicholas Bellinger cmd->init_task_tag, begrun, cmd->acked_data_sn);
550e48354ceSNicholas Bellinger return -1;
551e48354ceSNicholas Bellinger }
552e48354ceSNicholas Bellinger
553e48354ceSNicholas Bellinger /*
554e48354ceSNicholas Bellinger * For Data ACK SNACK, BegRun is the next expected DataSN.
555e48354ceSNicholas Bellinger * (see iSCSI v19: 10.16.6)
556e48354ceSNicholas Bellinger */
557e48354ceSNicholas Bellinger cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
558e48354ceSNicholas Bellinger cmd->acked_data_sn = (begrun - 1);
559e48354ceSNicholas Bellinger
560e48354ceSNicholas Bellinger pr_debug("Received Data ACK SNACK for ITT: 0x%08x,"
561e48354ceSNicholas Bellinger " updated acked DataSN to 0x%08x.\n",
562e48354ceSNicholas Bellinger cmd->init_task_tag, cmd->acked_data_sn);
563e48354ceSNicholas Bellinger
564e48354ceSNicholas Bellinger return 0;
565e48354ceSNicholas Bellinger }
566e48354ceSNicholas Bellinger
iscsit_send_recovery_r2t(struct iscsit_cmd * cmd,u32 offset,u32 xfer_len)567e48354ceSNicholas Bellinger static int iscsit_send_recovery_r2t(
56866cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
569e48354ceSNicholas Bellinger u32 offset,
570e48354ceSNicholas Bellinger u32 xfer_len)
571e48354ceSNicholas Bellinger {
572e48354ceSNicholas Bellinger int ret;
573e48354ceSNicholas Bellinger
574e48354ceSNicholas Bellinger spin_lock_bh(&cmd->r2t_lock);
575e48354ceSNicholas Bellinger ret = iscsit_add_r2t_to_list(cmd, offset, xfer_len, 1, 0);
576e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->r2t_lock);
577e48354ceSNicholas Bellinger
578e48354ceSNicholas Bellinger return ret;
579e48354ceSNicholas Bellinger }
580e48354ceSNicholas Bellinger
iscsit_dataout_datapduinorder_no_fbit(struct iscsit_cmd * cmd,struct iscsi_pdu * pdu)581e48354ceSNicholas Bellinger int iscsit_dataout_datapduinorder_no_fbit(
58266cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
583e48354ceSNicholas Bellinger struct iscsi_pdu *pdu)
584e48354ceSNicholas Bellinger {
585e48354ceSNicholas Bellinger int i, send_recovery_r2t = 0, recovery = 0;
586e48354ceSNicholas Bellinger u32 length = 0, offset = 0, pdu_count = 0, xfer_len = 0;
587be36d683SMax Gurtovoy struct iscsit_conn *conn = cmd->conn;
588e48354ceSNicholas Bellinger struct iscsi_pdu *first_pdu = NULL;
589e48354ceSNicholas Bellinger
590e48354ceSNicholas Bellinger /*
591e48354ceSNicholas Bellinger * Get an struct iscsi_pdu pointer to the first PDU, and total PDU count
592e48354ceSNicholas Bellinger * of the DataOUT sequence.
593e48354ceSNicholas Bellinger */
594e48354ceSNicholas Bellinger if (conn->sess->sess_ops->DataSequenceInOrder) {
595e48354ceSNicholas Bellinger for (i = 0; i < cmd->pdu_count; i++) {
596e48354ceSNicholas Bellinger if (cmd->pdu_list[i].seq_no == pdu->seq_no) {
597e48354ceSNicholas Bellinger if (!first_pdu)
598e48354ceSNicholas Bellinger first_pdu = &cmd->pdu_list[i];
599e48354ceSNicholas Bellinger xfer_len += cmd->pdu_list[i].length;
600e48354ceSNicholas Bellinger pdu_count++;
601e48354ceSNicholas Bellinger } else if (pdu_count)
602e48354ceSNicholas Bellinger break;
603e48354ceSNicholas Bellinger }
604e48354ceSNicholas Bellinger } else {
605e48354ceSNicholas Bellinger struct iscsi_seq *seq = cmd->seq_ptr;
606e48354ceSNicholas Bellinger
607e48354ceSNicholas Bellinger first_pdu = &cmd->pdu_list[seq->pdu_start];
608e48354ceSNicholas Bellinger pdu_count = seq->pdu_count;
609e48354ceSNicholas Bellinger }
610e48354ceSNicholas Bellinger
611e48354ceSNicholas Bellinger if (!first_pdu || !pdu_count)
612e48354ceSNicholas Bellinger return DATAOUT_CANNOT_RECOVER;
613e48354ceSNicholas Bellinger
614e48354ceSNicholas Bellinger /*
615e48354ceSNicholas Bellinger * Loop through the ending DataOUT Sequence checking each struct iscsi_pdu.
616e48354ceSNicholas Bellinger * The following ugly logic does batching of not received PDUs.
617e48354ceSNicholas Bellinger */
618e48354ceSNicholas Bellinger for (i = 0; i < pdu_count; i++) {
619e48354ceSNicholas Bellinger if (first_pdu[i].status == ISCSI_PDU_RECEIVED_OK) {
620e48354ceSNicholas Bellinger if (!send_recovery_r2t)
621e48354ceSNicholas Bellinger continue;
622e48354ceSNicholas Bellinger
623e48354ceSNicholas Bellinger if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
624e48354ceSNicholas Bellinger return DATAOUT_CANNOT_RECOVER;
625e48354ceSNicholas Bellinger
626e48354ceSNicholas Bellinger send_recovery_r2t = length = offset = 0;
627e48354ceSNicholas Bellinger continue;
628e48354ceSNicholas Bellinger }
629e48354ceSNicholas Bellinger /*
630e48354ceSNicholas Bellinger * Set recovery = 1 for any missing, CRC failed, or timed
631e48354ceSNicholas Bellinger * out PDUs to let the DataOUT logic know that this sequence
632e48354ceSNicholas Bellinger * has not been completed yet.
633e48354ceSNicholas Bellinger *
634e48354ceSNicholas Bellinger * Also, only send a Recovery R2T for ISCSI_PDU_NOT_RECEIVED.
635e48354ceSNicholas Bellinger * We assume if the PDU either failed CRC or timed out
636e48354ceSNicholas Bellinger * that a Recovery R2T has already been sent.
637e48354ceSNicholas Bellinger */
638e48354ceSNicholas Bellinger recovery = 1;
639e48354ceSNicholas Bellinger
640e48354ceSNicholas Bellinger if (first_pdu[i].status != ISCSI_PDU_NOT_RECEIVED)
641e48354ceSNicholas Bellinger continue;
642e48354ceSNicholas Bellinger
643e48354ceSNicholas Bellinger if (!offset)
644e48354ceSNicholas Bellinger offset = first_pdu[i].offset;
645e48354ceSNicholas Bellinger length += first_pdu[i].length;
646e48354ceSNicholas Bellinger
647e48354ceSNicholas Bellinger send_recovery_r2t = 1;
648e48354ceSNicholas Bellinger }
649e48354ceSNicholas Bellinger
650e48354ceSNicholas Bellinger if (send_recovery_r2t)
651e48354ceSNicholas Bellinger if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
652e48354ceSNicholas Bellinger return DATAOUT_CANNOT_RECOVER;
653e48354ceSNicholas Bellinger
654e48354ceSNicholas Bellinger return (!recovery) ? DATAOUT_NORMAL : DATAOUT_WITHIN_COMMAND_RECOVERY;
655e48354ceSNicholas Bellinger }
656e48354ceSNicholas Bellinger
iscsit_recalculate_dataout_values(struct iscsit_cmd * cmd,u32 pdu_offset,u32 pdu_length,u32 * r2t_offset,u32 * r2t_length)657e48354ceSNicholas Bellinger static int iscsit_recalculate_dataout_values(
65866cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
659e48354ceSNicholas Bellinger u32 pdu_offset,
660e48354ceSNicholas Bellinger u32 pdu_length,
661e48354ceSNicholas Bellinger u32 *r2t_offset,
662e48354ceSNicholas Bellinger u32 *r2t_length)
663e48354ceSNicholas Bellinger {
664e48354ceSNicholas Bellinger int i;
665be36d683SMax Gurtovoy struct iscsit_conn *conn = cmd->conn;
666e48354ceSNicholas Bellinger struct iscsi_pdu *pdu = NULL;
667e48354ceSNicholas Bellinger
668e48354ceSNicholas Bellinger if (conn->sess->sess_ops->DataSequenceInOrder) {
669e48354ceSNicholas Bellinger cmd->data_sn = 0;
670e48354ceSNicholas Bellinger
671e48354ceSNicholas Bellinger if (conn->sess->sess_ops->DataPDUInOrder) {
672e48354ceSNicholas Bellinger *r2t_offset = cmd->write_data_done;
673e48354ceSNicholas Bellinger *r2t_length = (cmd->seq_end_offset -
674e48354ceSNicholas Bellinger cmd->write_data_done);
675e48354ceSNicholas Bellinger return 0;
676e48354ceSNicholas Bellinger }
677e48354ceSNicholas Bellinger
678e48354ceSNicholas Bellinger *r2t_offset = cmd->seq_start_offset;
679e48354ceSNicholas Bellinger *r2t_length = (cmd->seq_end_offset - cmd->seq_start_offset);
680e48354ceSNicholas Bellinger
681e48354ceSNicholas Bellinger for (i = 0; i < cmd->pdu_count; i++) {
682e48354ceSNicholas Bellinger pdu = &cmd->pdu_list[i];
683e48354ceSNicholas Bellinger
684e48354ceSNicholas Bellinger if (pdu->status != ISCSI_PDU_RECEIVED_OK)
685e48354ceSNicholas Bellinger continue;
686e48354ceSNicholas Bellinger
687e48354ceSNicholas Bellinger if ((pdu->offset >= cmd->seq_start_offset) &&
688e48354ceSNicholas Bellinger ((pdu->offset + pdu->length) <=
689e48354ceSNicholas Bellinger cmd->seq_end_offset)) {
690e48354ceSNicholas Bellinger if (!cmd->unsolicited_data)
691e48354ceSNicholas Bellinger cmd->next_burst_len -= pdu->length;
692e48354ceSNicholas Bellinger else
693e48354ceSNicholas Bellinger cmd->first_burst_len -= pdu->length;
694e48354ceSNicholas Bellinger
695e48354ceSNicholas Bellinger cmd->write_data_done -= pdu->length;
696e48354ceSNicholas Bellinger pdu->status = ISCSI_PDU_NOT_RECEIVED;
697e48354ceSNicholas Bellinger }
698e48354ceSNicholas Bellinger }
699e48354ceSNicholas Bellinger } else {
700e48354ceSNicholas Bellinger struct iscsi_seq *seq = NULL;
701e48354ceSNicholas Bellinger
702e48354ceSNicholas Bellinger seq = iscsit_get_seq_holder(cmd, pdu_offset, pdu_length);
703e48354ceSNicholas Bellinger if (!seq)
704e48354ceSNicholas Bellinger return -1;
705e48354ceSNicholas Bellinger
706e48354ceSNicholas Bellinger *r2t_offset = seq->orig_offset;
707e48354ceSNicholas Bellinger *r2t_length = seq->xfer_len;
708e48354ceSNicholas Bellinger
709e48354ceSNicholas Bellinger cmd->write_data_done -= (seq->offset - seq->orig_offset);
710e48354ceSNicholas Bellinger if (cmd->immediate_data)
711e48354ceSNicholas Bellinger cmd->first_burst_len = cmd->write_data_done;
712e48354ceSNicholas Bellinger
713e48354ceSNicholas Bellinger seq->data_sn = 0;
714e48354ceSNicholas Bellinger seq->offset = seq->orig_offset;
715e48354ceSNicholas Bellinger seq->next_burst_len = 0;
716e48354ceSNicholas Bellinger seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
717e48354ceSNicholas Bellinger
718e48354ceSNicholas Bellinger if (conn->sess->sess_ops->DataPDUInOrder)
719e48354ceSNicholas Bellinger return 0;
720e48354ceSNicholas Bellinger
721e48354ceSNicholas Bellinger for (i = 0; i < seq->pdu_count; i++) {
722e48354ceSNicholas Bellinger pdu = &cmd->pdu_list[i+seq->pdu_start];
723e48354ceSNicholas Bellinger
724e48354ceSNicholas Bellinger if (pdu->status != ISCSI_PDU_RECEIVED_OK)
725e48354ceSNicholas Bellinger continue;
726e48354ceSNicholas Bellinger
727e48354ceSNicholas Bellinger pdu->status = ISCSI_PDU_NOT_RECEIVED;
728e48354ceSNicholas Bellinger }
729e48354ceSNicholas Bellinger }
730e48354ceSNicholas Bellinger
731e48354ceSNicholas Bellinger return 0;
732e48354ceSNicholas Bellinger }
733e48354ceSNicholas Bellinger
iscsit_recover_dataout_sequence(struct iscsit_cmd * cmd,u32 pdu_offset,u32 pdu_length)734e48354ceSNicholas Bellinger int iscsit_recover_dataout_sequence(
73566cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
736e48354ceSNicholas Bellinger u32 pdu_offset,
737e48354ceSNicholas Bellinger u32 pdu_length)
738e48354ceSNicholas Bellinger {
739e48354ceSNicholas Bellinger u32 r2t_length = 0, r2t_offset = 0;
740e48354ceSNicholas Bellinger
741e48354ceSNicholas Bellinger spin_lock_bh(&cmd->istate_lock);
742e48354ceSNicholas Bellinger cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
743e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
744e48354ceSNicholas Bellinger
745e48354ceSNicholas Bellinger if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
746e48354ceSNicholas Bellinger &r2t_offset, &r2t_length) < 0)
747e48354ceSNicholas Bellinger return DATAOUT_CANNOT_RECOVER;
748e48354ceSNicholas Bellinger
749e48354ceSNicholas Bellinger iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length);
750e48354ceSNicholas Bellinger
751e48354ceSNicholas Bellinger return DATAOUT_WITHIN_COMMAND_RECOVERY;
752e48354ceSNicholas Bellinger }
753e48354ceSNicholas Bellinger
iscsit_allocate_ooo_cmdsn(void)754e48354ceSNicholas Bellinger static struct iscsi_ooo_cmdsn *iscsit_allocate_ooo_cmdsn(void)
755e48354ceSNicholas Bellinger {
756e48354ceSNicholas Bellinger struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL;
757e48354ceSNicholas Bellinger
758e48354ceSNicholas Bellinger ooo_cmdsn = kmem_cache_zalloc(lio_ooo_cache, GFP_ATOMIC);
759e48354ceSNicholas Bellinger if (!ooo_cmdsn) {
760e48354ceSNicholas Bellinger pr_err("Unable to allocate memory for"
761e48354ceSNicholas Bellinger " struct iscsi_ooo_cmdsn.\n");
762e48354ceSNicholas Bellinger return NULL;
763e48354ceSNicholas Bellinger }
764e48354ceSNicholas Bellinger INIT_LIST_HEAD(&ooo_cmdsn->ooo_list);
765e48354ceSNicholas Bellinger
766e48354ceSNicholas Bellinger return ooo_cmdsn;
767e48354ceSNicholas Bellinger }
768e48354ceSNicholas Bellinger
iscsit_attach_ooo_cmdsn(struct iscsit_session * sess,struct iscsi_ooo_cmdsn * ooo_cmdsn)769e48354ceSNicholas Bellinger static int iscsit_attach_ooo_cmdsn(
770*0873fe44SMax Gurtovoy struct iscsit_session *sess,
771e48354ceSNicholas Bellinger struct iscsi_ooo_cmdsn *ooo_cmdsn)
772e48354ceSNicholas Bellinger {
773e48354ceSNicholas Bellinger struct iscsi_ooo_cmdsn *ooo_tail, *ooo_tmp;
774618baaf7SBart Van Assche
775618baaf7SBart Van Assche lockdep_assert_held(&sess->cmdsn_mutex);
776618baaf7SBart Van Assche
777e48354ceSNicholas Bellinger /*
778e48354ceSNicholas Bellinger * We attach the struct iscsi_ooo_cmdsn entry to the out of order
779e48354ceSNicholas Bellinger * list in increasing CmdSN order.
780e48354ceSNicholas Bellinger * This allows iscsi_execute_ooo_cmdsns() to detect any
781e48354ceSNicholas Bellinger * additional CmdSN holes while performing delayed execution.
782e48354ceSNicholas Bellinger */
783e48354ceSNicholas Bellinger if (list_empty(&sess->sess_ooo_cmdsn_list))
784e48354ceSNicholas Bellinger list_add_tail(&ooo_cmdsn->ooo_list,
785e48354ceSNicholas Bellinger &sess->sess_ooo_cmdsn_list);
786e48354ceSNicholas Bellinger else {
787e48354ceSNicholas Bellinger ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
788e48354ceSNicholas Bellinger typeof(*ooo_tail), ooo_list);
789e48354ceSNicholas Bellinger /*
790e48354ceSNicholas Bellinger * CmdSN is greater than the tail of the list.
791e48354ceSNicholas Bellinger */
7923eccfdb0SShlomo Pongratz if (iscsi_sna_lt(ooo_tail->cmdsn, ooo_cmdsn->cmdsn))
793e48354ceSNicholas Bellinger list_add_tail(&ooo_cmdsn->ooo_list,
794e48354ceSNicholas Bellinger &sess->sess_ooo_cmdsn_list);
795e48354ceSNicholas Bellinger else {
796e48354ceSNicholas Bellinger /*
797e48354ceSNicholas Bellinger * CmdSN is either lower than the head, or somewhere
798e48354ceSNicholas Bellinger * in the middle.
799e48354ceSNicholas Bellinger */
800e48354ceSNicholas Bellinger list_for_each_entry(ooo_tmp, &sess->sess_ooo_cmdsn_list,
801e48354ceSNicholas Bellinger ooo_list) {
8023eccfdb0SShlomo Pongratz if (iscsi_sna_lt(ooo_tmp->cmdsn, ooo_cmdsn->cmdsn))
803e48354ceSNicholas Bellinger continue;
804e48354ceSNicholas Bellinger
8053eccfdb0SShlomo Pongratz /* Insert before this entry */
806e48354ceSNicholas Bellinger list_add(&ooo_cmdsn->ooo_list,
8073eccfdb0SShlomo Pongratz ooo_tmp->ooo_list.prev);
808e48354ceSNicholas Bellinger break;
809e48354ceSNicholas Bellinger }
810e48354ceSNicholas Bellinger }
811e48354ceSNicholas Bellinger }
812e48354ceSNicholas Bellinger
813e48354ceSNicholas Bellinger return 0;
814e48354ceSNicholas Bellinger }
815e48354ceSNicholas Bellinger
816e48354ceSNicholas Bellinger /*
817e48354ceSNicholas Bellinger * Removes an struct iscsi_ooo_cmdsn from a session's list,
818*0873fe44SMax Gurtovoy * called with struct iscsit_session->cmdsn_mutex held.
819e48354ceSNicholas Bellinger */
iscsit_remove_ooo_cmdsn(struct iscsit_session * sess,struct iscsi_ooo_cmdsn * ooo_cmdsn)820e48354ceSNicholas Bellinger void iscsit_remove_ooo_cmdsn(
821*0873fe44SMax Gurtovoy struct iscsit_session *sess,
822e48354ceSNicholas Bellinger struct iscsi_ooo_cmdsn *ooo_cmdsn)
823e48354ceSNicholas Bellinger {
824e48354ceSNicholas Bellinger list_del(&ooo_cmdsn->ooo_list);
825e48354ceSNicholas Bellinger kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
826e48354ceSNicholas Bellinger }
827e48354ceSNicholas Bellinger
iscsit_clear_ooo_cmdsns_for_conn(struct iscsit_conn * conn)828be36d683SMax Gurtovoy void iscsit_clear_ooo_cmdsns_for_conn(struct iscsit_conn *conn)
829e48354ceSNicholas Bellinger {
830e48354ceSNicholas Bellinger struct iscsi_ooo_cmdsn *ooo_cmdsn;
831*0873fe44SMax Gurtovoy struct iscsit_session *sess = conn->sess;
832e48354ceSNicholas Bellinger
833e48354ceSNicholas Bellinger mutex_lock(&sess->cmdsn_mutex);
834e48354ceSNicholas Bellinger list_for_each_entry(ooo_cmdsn, &sess->sess_ooo_cmdsn_list, ooo_list) {
835e48354ceSNicholas Bellinger if (ooo_cmdsn->cid != conn->cid)
836e48354ceSNicholas Bellinger continue;
837e48354ceSNicholas Bellinger
838e48354ceSNicholas Bellinger ooo_cmdsn->cmd = NULL;
839e48354ceSNicholas Bellinger }
840e48354ceSNicholas Bellinger mutex_unlock(&sess->cmdsn_mutex);
841e48354ceSNicholas Bellinger }
842e48354ceSNicholas Bellinger
iscsit_execute_ooo_cmdsns(struct iscsit_session * sess)843*0873fe44SMax Gurtovoy int iscsit_execute_ooo_cmdsns(struct iscsit_session *sess)
844e48354ceSNicholas Bellinger {
845e48354ceSNicholas Bellinger int ooo_count = 0;
84666cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd = NULL;
847e48354ceSNicholas Bellinger struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
848e48354ceSNicholas Bellinger
849618baaf7SBart Van Assche lockdep_assert_held(&sess->cmdsn_mutex);
850618baaf7SBart Van Assche
851e48354ceSNicholas Bellinger list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
852e48354ceSNicholas Bellinger &sess->sess_ooo_cmdsn_list, ooo_list) {
853e48354ceSNicholas Bellinger if (ooo_cmdsn->cmdsn != sess->exp_cmd_sn)
854e48354ceSNicholas Bellinger continue;
855e48354ceSNicholas Bellinger
856e48354ceSNicholas Bellinger if (!ooo_cmdsn->cmd) {
857e48354ceSNicholas Bellinger sess->exp_cmd_sn++;
858e48354ceSNicholas Bellinger iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
859e48354ceSNicholas Bellinger continue;
860e48354ceSNicholas Bellinger }
861e48354ceSNicholas Bellinger
862e48354ceSNicholas Bellinger cmd = ooo_cmdsn->cmd;
863e48354ceSNicholas Bellinger cmd->i_state = cmd->deferred_i_state;
864e48354ceSNicholas Bellinger ooo_count++;
865e48354ceSNicholas Bellinger sess->exp_cmd_sn++;
866e48354ceSNicholas Bellinger pr_debug("Executing out of order CmdSN: 0x%08x,"
867e48354ceSNicholas Bellinger " incremented ExpCmdSN to 0x%08x.\n",
868e48354ceSNicholas Bellinger cmd->cmd_sn, sess->exp_cmd_sn);
869e48354ceSNicholas Bellinger
870e48354ceSNicholas Bellinger iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
871e48354ceSNicholas Bellinger
872e48354ceSNicholas Bellinger if (iscsit_execute_cmd(cmd, 1) < 0)
873e48354ceSNicholas Bellinger return -1;
874e48354ceSNicholas Bellinger }
875e48354ceSNicholas Bellinger
876e48354ceSNicholas Bellinger return ooo_count;
877e48354ceSNicholas Bellinger }
878e48354ceSNicholas Bellinger
879e48354ceSNicholas Bellinger /*
880e48354ceSNicholas Bellinger * Called either:
881e48354ceSNicholas Bellinger *
882e48354ceSNicholas Bellinger * 1. With sess->cmdsn_mutex held from iscsi_execute_ooo_cmdsns()
883e48354ceSNicholas Bellinger * or iscsi_check_received_cmdsn().
884e48354ceSNicholas Bellinger * 2. With no locks held directly from iscsi_handle_XXX_pdu() functions
885e48354ceSNicholas Bellinger * for immediate commands.
886e48354ceSNicholas Bellinger */
iscsit_execute_cmd(struct iscsit_cmd * cmd,int ooo)88766cd9d4eSMax Gurtovoy int iscsit_execute_cmd(struct iscsit_cmd *cmd, int ooo)
888e48354ceSNicholas Bellinger {
889e48354ceSNicholas Bellinger struct se_cmd *se_cmd = &cmd->se_cmd;
890be36d683SMax Gurtovoy struct iscsit_conn *conn = cmd->conn;
891e48354ceSNicholas Bellinger int lr = 0;
892e48354ceSNicholas Bellinger
893e48354ceSNicholas Bellinger spin_lock_bh(&cmd->istate_lock);
894e48354ceSNicholas Bellinger if (ooo)
895e48354ceSNicholas Bellinger cmd->cmd_flags &= ~ICF_OOO_CMDSN;
896e48354ceSNicholas Bellinger
897e48354ceSNicholas Bellinger switch (cmd->iscsi_opcode) {
898e48354ceSNicholas Bellinger case ISCSI_OP_SCSI_CMD:
899e48354ceSNicholas Bellinger /*
900e48354ceSNicholas Bellinger * Go ahead and send the CHECK_CONDITION status for
901de103c93SChristoph Hellwig * any SCSI CDB exceptions that may have occurred.
902e48354ceSNicholas Bellinger */
903de103c93SChristoph Hellwig if (cmd->sense_reason) {
904de103c93SChristoph Hellwig if (cmd->sense_reason == TCM_RESERVATION_CONFLICT) {
905e48354ceSNicholas Bellinger cmd->i_state = ISTATE_SEND_STATUS;
906e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
907e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
908e48354ceSNicholas Bellinger cmd->i_state);
909e48354ceSNicholas Bellinger return 0;
910e48354ceSNicholas Bellinger }
911e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
912aaa00cc9SBart Van Assche if (cmd->se_cmd.transport_state & CMD_T_ABORTED)
913e48354ceSNicholas Bellinger return 0;
914e48354ceSNicholas Bellinger return transport_send_check_condition_and_sense(se_cmd,
915de103c93SChristoph Hellwig cmd->sense_reason, 0);
916e48354ceSNicholas Bellinger }
917e48354ceSNicholas Bellinger /*
918e48354ceSNicholas Bellinger * Special case for delayed CmdSN with Immediate
919e48354ceSNicholas Bellinger * Data and/or Unsolicited Data Out attached.
920e48354ceSNicholas Bellinger */
921e48354ceSNicholas Bellinger if (cmd->immediate_data) {
922e48354ceSNicholas Bellinger if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
923e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
92467441b68SChristoph Hellwig target_execute_cmd(&cmd->se_cmd);
92567441b68SChristoph Hellwig return 0;
926e48354ceSNicholas Bellinger }
927e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
928e48354ceSNicholas Bellinger
929e48354ceSNicholas Bellinger if (!(cmd->cmd_flags &
930e48354ceSNicholas Bellinger ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
931aaa00cc9SBart Van Assche if (cmd->se_cmd.transport_state & CMD_T_ABORTED)
932e48354ceSNicholas Bellinger return 0;
933e48354ceSNicholas Bellinger
934e48354ceSNicholas Bellinger iscsit_set_dataout_sequence_values(cmd);
9353e1c81a9SNicholas Bellinger conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
936e48354ceSNicholas Bellinger }
937e48354ceSNicholas Bellinger return 0;
938e48354ceSNicholas Bellinger }
939e48354ceSNicholas Bellinger /*
940e48354ceSNicholas Bellinger * The default handler.
941e48354ceSNicholas Bellinger */
942e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
943e48354ceSNicholas Bellinger
944e48354ceSNicholas Bellinger if ((cmd->data_direction == DMA_TO_DEVICE) &&
945e48354ceSNicholas Bellinger !(cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
946aaa00cc9SBart Van Assche if (cmd->se_cmd.transport_state & CMD_T_ABORTED)
947e48354ceSNicholas Bellinger return 0;
948e48354ceSNicholas Bellinger
9490300b114SBart Van Assche iscsit_set_unsolicited_dataout(cmd);
950e48354ceSNicholas Bellinger }
951e48354ceSNicholas Bellinger return transport_handle_cdb_direct(&cmd->se_cmd);
952e48354ceSNicholas Bellinger
953e48354ceSNicholas Bellinger case ISCSI_OP_NOOP_OUT:
954e48354ceSNicholas Bellinger case ISCSI_OP_TEXT:
955e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
956e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
957e48354ceSNicholas Bellinger break;
958e48354ceSNicholas Bellinger case ISCSI_OP_SCSI_TMFUNC:
959de103c93SChristoph Hellwig if (cmd->se_cmd.se_tmr_req->response) {
960e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
961e48354ceSNicholas Bellinger iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
962e48354ceSNicholas Bellinger cmd->i_state);
963e48354ceSNicholas Bellinger return 0;
964e48354ceSNicholas Bellinger }
965e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
966e48354ceSNicholas Bellinger
967e48354ceSNicholas Bellinger return transport_generic_handle_tmr(&cmd->se_cmd);
968e48354ceSNicholas Bellinger case ISCSI_OP_LOGOUT:
969e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
970e48354ceSNicholas Bellinger switch (cmd->logout_reason) {
971e48354ceSNicholas Bellinger case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
972e48354ceSNicholas Bellinger lr = iscsit_logout_closesession(cmd, cmd->conn);
973e48354ceSNicholas Bellinger break;
974e48354ceSNicholas Bellinger case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
975e48354ceSNicholas Bellinger lr = iscsit_logout_closeconnection(cmd, cmd->conn);
976e48354ceSNicholas Bellinger break;
977e48354ceSNicholas Bellinger case ISCSI_LOGOUT_REASON_RECOVERY:
978e48354ceSNicholas Bellinger lr = iscsit_logout_removeconnforrecovery(cmd, cmd->conn);
979e48354ceSNicholas Bellinger break;
980e48354ceSNicholas Bellinger default:
981e48354ceSNicholas Bellinger pr_err("Unknown iSCSI Logout Request Code:"
982e48354ceSNicholas Bellinger " 0x%02x\n", cmd->logout_reason);
983e48354ceSNicholas Bellinger return -1;
984e48354ceSNicholas Bellinger }
985e48354ceSNicholas Bellinger
986e48354ceSNicholas Bellinger return lr;
987e48354ceSNicholas Bellinger default:
988e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->istate_lock);
989e48354ceSNicholas Bellinger pr_err("Cannot perform out of order execution for"
990e48354ceSNicholas Bellinger " unknown iSCSI Opcode: 0x%02x\n", cmd->iscsi_opcode);
991e48354ceSNicholas Bellinger return -1;
992e48354ceSNicholas Bellinger }
993e48354ceSNicholas Bellinger
994e48354ceSNicholas Bellinger return 0;
995e48354ceSNicholas Bellinger }
996e48354ceSNicholas Bellinger
iscsit_free_all_ooo_cmdsns(struct iscsit_session * sess)997*0873fe44SMax Gurtovoy void iscsit_free_all_ooo_cmdsns(struct iscsit_session *sess)
998e48354ceSNicholas Bellinger {
999e48354ceSNicholas Bellinger struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
1000e48354ceSNicholas Bellinger
1001e48354ceSNicholas Bellinger mutex_lock(&sess->cmdsn_mutex);
1002e48354ceSNicholas Bellinger list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
1003e48354ceSNicholas Bellinger &sess->sess_ooo_cmdsn_list, ooo_list) {
1004e48354ceSNicholas Bellinger
1005e48354ceSNicholas Bellinger list_del(&ooo_cmdsn->ooo_list);
1006e48354ceSNicholas Bellinger kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
1007e48354ceSNicholas Bellinger }
1008e48354ceSNicholas Bellinger mutex_unlock(&sess->cmdsn_mutex);
1009e48354ceSNicholas Bellinger }
1010e48354ceSNicholas Bellinger
iscsit_handle_ooo_cmdsn(struct iscsit_session * sess,struct iscsit_cmd * cmd,u32 cmdsn)1011e48354ceSNicholas Bellinger int iscsit_handle_ooo_cmdsn(
1012*0873fe44SMax Gurtovoy struct iscsit_session *sess,
101366cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
1014e48354ceSNicholas Bellinger u32 cmdsn)
1015e48354ceSNicholas Bellinger {
1016e48354ceSNicholas Bellinger int batch = 0;
1017e48354ceSNicholas Bellinger struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL, *ooo_tail = NULL;
1018e48354ceSNicholas Bellinger
1019e48354ceSNicholas Bellinger cmd->deferred_i_state = cmd->i_state;
1020e48354ceSNicholas Bellinger cmd->i_state = ISTATE_DEFERRED_CMD;
1021e48354ceSNicholas Bellinger cmd->cmd_flags |= ICF_OOO_CMDSN;
1022e48354ceSNicholas Bellinger
1023e48354ceSNicholas Bellinger if (list_empty(&sess->sess_ooo_cmdsn_list))
1024e48354ceSNicholas Bellinger batch = 1;
1025e48354ceSNicholas Bellinger else {
1026e48354ceSNicholas Bellinger ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
1027e48354ceSNicholas Bellinger typeof(*ooo_tail), ooo_list);
1028e48354ceSNicholas Bellinger if (ooo_tail->cmdsn != (cmdsn - 1))
1029e48354ceSNicholas Bellinger batch = 1;
1030e48354ceSNicholas Bellinger }
1031e48354ceSNicholas Bellinger
1032e48354ceSNicholas Bellinger ooo_cmdsn = iscsit_allocate_ooo_cmdsn();
1033e48354ceSNicholas Bellinger if (!ooo_cmdsn)
1034561bf158SNicholas Bellinger return -ENOMEM;
1035e48354ceSNicholas Bellinger
1036e48354ceSNicholas Bellinger ooo_cmdsn->cmd = cmd;
1037e48354ceSNicholas Bellinger ooo_cmdsn->batch_count = (batch) ?
1038e48354ceSNicholas Bellinger (cmdsn - sess->exp_cmd_sn) : 1;
1039e48354ceSNicholas Bellinger ooo_cmdsn->cid = cmd->conn->cid;
1040e48354ceSNicholas Bellinger ooo_cmdsn->exp_cmdsn = sess->exp_cmd_sn;
1041e48354ceSNicholas Bellinger ooo_cmdsn->cmdsn = cmdsn;
1042e48354ceSNicholas Bellinger
1043e48354ceSNicholas Bellinger if (iscsit_attach_ooo_cmdsn(sess, ooo_cmdsn) < 0) {
1044e48354ceSNicholas Bellinger kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
1045561bf158SNicholas Bellinger return -ENOMEM;
1046e48354ceSNicholas Bellinger }
1047e48354ceSNicholas Bellinger
1048561bf158SNicholas Bellinger return 0;
1049e48354ceSNicholas Bellinger }
1050e48354ceSNicholas Bellinger
iscsit_set_dataout_timeout_values(struct iscsit_cmd * cmd,u32 * offset,u32 * length)1051e48354ceSNicholas Bellinger static int iscsit_set_dataout_timeout_values(
105266cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
1053e48354ceSNicholas Bellinger u32 *offset,
1054e48354ceSNicholas Bellinger u32 *length)
1055e48354ceSNicholas Bellinger {
1056be36d683SMax Gurtovoy struct iscsit_conn *conn = cmd->conn;
1057e48354ceSNicholas Bellinger struct iscsi_r2t *r2t;
1058e48354ceSNicholas Bellinger
1059e48354ceSNicholas Bellinger if (cmd->unsolicited_data) {
1060e48354ceSNicholas Bellinger *offset = 0;
1061e48354ceSNicholas Bellinger *length = (conn->sess->sess_ops->FirstBurstLength >
1062ebf1d95cSAndy Grover cmd->se_cmd.data_length) ?
1063ebf1d95cSAndy Grover cmd->se_cmd.data_length :
1064e48354ceSNicholas Bellinger conn->sess->sess_ops->FirstBurstLength;
1065e48354ceSNicholas Bellinger return 0;
1066e48354ceSNicholas Bellinger }
1067e48354ceSNicholas Bellinger
1068e48354ceSNicholas Bellinger spin_lock_bh(&cmd->r2t_lock);
1069e48354ceSNicholas Bellinger if (list_empty(&cmd->cmd_r2t_list)) {
1070e48354ceSNicholas Bellinger pr_err("cmd->cmd_r2t_list is empty!\n");
1071e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->r2t_lock);
1072e48354ceSNicholas Bellinger return -1;
1073e48354ceSNicholas Bellinger }
1074e48354ceSNicholas Bellinger
1075e48354ceSNicholas Bellinger list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
1076e48354ceSNicholas Bellinger if (r2t->sent_r2t && !r2t->recovery_r2t && !r2t->seq_complete) {
1077e48354ceSNicholas Bellinger *offset = r2t->offset;
1078e48354ceSNicholas Bellinger *length = r2t->xfer_len;
1079e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->r2t_lock);
1080e48354ceSNicholas Bellinger return 0;
1081e48354ceSNicholas Bellinger }
1082e48354ceSNicholas Bellinger }
1083e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->r2t_lock);
1084e48354ceSNicholas Bellinger
1085e48354ceSNicholas Bellinger pr_err("Unable to locate any incomplete DataOUT"
1086e48354ceSNicholas Bellinger " sequences for ITT: 0x%08x.\n", cmd->init_task_tag);
1087e48354ceSNicholas Bellinger
1088e48354ceSNicholas Bellinger return -1;
1089e48354ceSNicholas Bellinger }
1090e48354ceSNicholas Bellinger
1091e48354ceSNicholas Bellinger /*
1092e48354ceSNicholas Bellinger * NOTE: Called from interrupt (timer) context.
1093e48354ceSNicholas Bellinger */
iscsit_handle_dataout_timeout(struct timer_list * t)1094f7c9564aSKees Cook void iscsit_handle_dataout_timeout(struct timer_list *t)
1095e48354ceSNicholas Bellinger {
1096e48354ceSNicholas Bellinger u32 pdu_length = 0, pdu_offset = 0;
1097e48354ceSNicholas Bellinger u32 r2t_length = 0, r2t_offset = 0;
109866cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd = from_timer(cmd, t, dataout_timer);
1099be36d683SMax Gurtovoy struct iscsit_conn *conn = cmd->conn;
1100*0873fe44SMax Gurtovoy struct iscsit_session *sess = NULL;
1101e48354ceSNicholas Bellinger struct iscsi_node_attrib *na;
1102e48354ceSNicholas Bellinger
1103e48354ceSNicholas Bellinger iscsit_inc_conn_usage_count(conn);
1104e48354ceSNicholas Bellinger
1105e48354ceSNicholas Bellinger spin_lock_bh(&cmd->dataout_timeout_lock);
1106e48354ceSNicholas Bellinger if (cmd->dataout_timer_flags & ISCSI_TF_STOP) {
1107e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->dataout_timeout_lock);
1108e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn);
1109e48354ceSNicholas Bellinger return;
1110e48354ceSNicholas Bellinger }
1111e48354ceSNicholas Bellinger cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
1112e48354ceSNicholas Bellinger sess = conn->sess;
1113e48354ceSNicholas Bellinger na = iscsit_tpg_get_node_attrib(sess);
1114e48354ceSNicholas Bellinger
1115e48354ceSNicholas Bellinger if (!sess->sess_ops->ErrorRecoveryLevel) {
1116d9a771fdSDavid Disseldorp pr_err("Unable to recover from DataOut timeout while"
1117d9a771fdSDavid Disseldorp " in ERL=0, closing iSCSI connection for I_T Nexus"
1118d9a771fdSDavid Disseldorp " %s,i,0x%6phN,%s,t,0x%02x\n",
1119d9a771fdSDavid Disseldorp sess->sess_ops->InitiatorName, sess->isid,
1120d9a771fdSDavid Disseldorp sess->tpg->tpg_tiqn->tiqn, (u32)sess->tpg->tpgt);
1121e48354ceSNicholas Bellinger goto failure;
1122e48354ceSNicholas Bellinger }
1123e48354ceSNicholas Bellinger
1124e48354ceSNicholas Bellinger if (++cmd->dataout_timeout_retries == na->dataout_timeout_retries) {
1125d9a771fdSDavid Disseldorp pr_err("Command ITT: 0x%08x exceeded max retries"
1126d9a771fdSDavid Disseldorp " for DataOUT timeout %u, closing iSCSI connection for"
1127d9a771fdSDavid Disseldorp " I_T Nexus %s,i,0x%6phN,%s,t,0x%02x\n",
1128d9a771fdSDavid Disseldorp cmd->init_task_tag, na->dataout_timeout_retries,
1129d9a771fdSDavid Disseldorp sess->sess_ops->InitiatorName, sess->isid,
1130d9a771fdSDavid Disseldorp sess->tpg->tpg_tiqn->tiqn, (u32)sess->tpg->tpgt);
1131e48354ceSNicholas Bellinger goto failure;
1132e48354ceSNicholas Bellinger }
1133e48354ceSNicholas Bellinger
1134e48354ceSNicholas Bellinger cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
1135e48354ceSNicholas Bellinger
1136e48354ceSNicholas Bellinger if (conn->sess->sess_ops->DataSequenceInOrder) {
1137e48354ceSNicholas Bellinger if (conn->sess->sess_ops->DataPDUInOrder) {
1138e48354ceSNicholas Bellinger pdu_offset = cmd->write_data_done;
1139e48354ceSNicholas Bellinger if ((pdu_offset + (conn->sess->sess_ops->MaxBurstLength -
1140ebf1d95cSAndy Grover cmd->next_burst_len)) > cmd->se_cmd.data_length)
1141ebf1d95cSAndy Grover pdu_length = (cmd->se_cmd.data_length -
1142e48354ceSNicholas Bellinger cmd->write_data_done);
1143e48354ceSNicholas Bellinger else
1144e48354ceSNicholas Bellinger pdu_length = (conn->sess->sess_ops->MaxBurstLength -
1145e48354ceSNicholas Bellinger cmd->next_burst_len);
1146e48354ceSNicholas Bellinger } else {
1147e48354ceSNicholas Bellinger pdu_offset = cmd->seq_start_offset;
1148e48354ceSNicholas Bellinger pdu_length = (cmd->seq_end_offset -
1149e48354ceSNicholas Bellinger cmd->seq_start_offset);
1150e48354ceSNicholas Bellinger }
1151e48354ceSNicholas Bellinger } else {
1152e48354ceSNicholas Bellinger if (iscsit_set_dataout_timeout_values(cmd, &pdu_offset,
1153e48354ceSNicholas Bellinger &pdu_length) < 0)
1154e48354ceSNicholas Bellinger goto failure;
1155e48354ceSNicholas Bellinger }
1156e48354ceSNicholas Bellinger
1157e48354ceSNicholas Bellinger if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
1158e48354ceSNicholas Bellinger &r2t_offset, &r2t_length) < 0)
1159e48354ceSNicholas Bellinger goto failure;
1160e48354ceSNicholas Bellinger
1161e48354ceSNicholas Bellinger pr_debug("Command ITT: 0x%08x timed out waiting for"
1162e48354ceSNicholas Bellinger " completion of %sDataOUT Sequence Offset: %u, Length: %u\n",
1163e48354ceSNicholas Bellinger cmd->init_task_tag, (cmd->unsolicited_data) ? "Unsolicited " :
1164e48354ceSNicholas Bellinger "", r2t_offset, r2t_length);
1165e48354ceSNicholas Bellinger
1166e48354ceSNicholas Bellinger if (iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length) < 0)
1167e48354ceSNicholas Bellinger goto failure;
1168e48354ceSNicholas Bellinger
1169e48354ceSNicholas Bellinger iscsit_start_dataout_timer(cmd, conn);
1170e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->dataout_timeout_lock);
1171e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn);
1172e48354ceSNicholas Bellinger
1173e48354ceSNicholas Bellinger return;
1174e48354ceSNicholas Bellinger
1175e48354ceSNicholas Bellinger failure:
1176e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->dataout_timeout_lock);
117733b3f8caSDavid Disseldorp iscsit_fill_cxn_timeout_err_stats(sess);
1178e48354ceSNicholas Bellinger iscsit_cause_connection_reinstatement(conn, 0);
1179e48354ceSNicholas Bellinger iscsit_dec_conn_usage_count(conn);
1180e48354ceSNicholas Bellinger }
1181e48354ceSNicholas Bellinger
iscsit_mod_dataout_timer(struct iscsit_cmd * cmd)118266cd9d4eSMax Gurtovoy void iscsit_mod_dataout_timer(struct iscsit_cmd *cmd)
1183e48354ceSNicholas Bellinger {
1184be36d683SMax Gurtovoy struct iscsit_conn *conn = cmd->conn;
1185*0873fe44SMax Gurtovoy struct iscsit_session *sess = conn->sess;
1186e8904dc5SJesper Juhl struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1187e48354ceSNicholas Bellinger
1188e48354ceSNicholas Bellinger spin_lock_bh(&cmd->dataout_timeout_lock);
1189e48354ceSNicholas Bellinger if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
1190e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->dataout_timeout_lock);
1191e48354ceSNicholas Bellinger return;
1192e48354ceSNicholas Bellinger }
1193e48354ceSNicholas Bellinger
1194e48354ceSNicholas Bellinger mod_timer(&cmd->dataout_timer,
1195e48354ceSNicholas Bellinger (get_jiffies_64() + na->dataout_timeout * HZ));
1196e48354ceSNicholas Bellinger pr_debug("Updated DataOUT timer for ITT: 0x%08x",
1197e48354ceSNicholas Bellinger cmd->init_task_tag);
1198e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->dataout_timeout_lock);
1199e48354ceSNicholas Bellinger }
1200e48354ceSNicholas Bellinger
iscsit_start_dataout_timer(struct iscsit_cmd * cmd,struct iscsit_conn * conn)1201e48354ceSNicholas Bellinger void iscsit_start_dataout_timer(
120266cd9d4eSMax Gurtovoy struct iscsit_cmd *cmd,
1203be36d683SMax Gurtovoy struct iscsit_conn *conn)
1204e48354ceSNicholas Bellinger {
1205*0873fe44SMax Gurtovoy struct iscsit_session *sess = conn->sess;
1206e8904dc5SJesper Juhl struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
1207e48354ceSNicholas Bellinger
1208618baaf7SBart Van Assche lockdep_assert_held(&cmd->dataout_timeout_lock);
1209618baaf7SBart Van Assche
1210e48354ceSNicholas Bellinger if (cmd->dataout_timer_flags & ISCSI_TF_RUNNING)
1211e48354ceSNicholas Bellinger return;
1212e48354ceSNicholas Bellinger
1213e48354ceSNicholas Bellinger pr_debug("Starting DataOUT timer for ITT: 0x%08x on"
1214e48354ceSNicholas Bellinger " CID: %hu.\n", cmd->init_task_tag, conn->cid);
1215e48354ceSNicholas Bellinger
1216e48354ceSNicholas Bellinger cmd->dataout_timer_flags &= ~ISCSI_TF_STOP;
1217e48354ceSNicholas Bellinger cmd->dataout_timer_flags |= ISCSI_TF_RUNNING;
12188a47aa9dSBart Van Assche mod_timer(&cmd->dataout_timer, jiffies + na->dataout_timeout * HZ);
1219e48354ceSNicholas Bellinger }
1220e48354ceSNicholas Bellinger
iscsit_stop_dataout_timer(struct iscsit_cmd * cmd)122166cd9d4eSMax Gurtovoy void iscsit_stop_dataout_timer(struct iscsit_cmd *cmd)
1222e48354ceSNicholas Bellinger {
1223e48354ceSNicholas Bellinger spin_lock_bh(&cmd->dataout_timeout_lock);
1224e48354ceSNicholas Bellinger if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
1225e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->dataout_timeout_lock);
1226e48354ceSNicholas Bellinger return;
1227e48354ceSNicholas Bellinger }
1228e48354ceSNicholas Bellinger cmd->dataout_timer_flags |= ISCSI_TF_STOP;
1229e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->dataout_timeout_lock);
1230e48354ceSNicholas Bellinger
1231e48354ceSNicholas Bellinger del_timer_sync(&cmd->dataout_timer);
1232e48354ceSNicholas Bellinger
1233e48354ceSNicholas Bellinger spin_lock_bh(&cmd->dataout_timeout_lock);
1234e48354ceSNicholas Bellinger cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
1235e48354ceSNicholas Bellinger pr_debug("Stopped DataOUT Timer for ITT: 0x%08x\n",
1236e48354ceSNicholas Bellinger cmd->init_task_tag);
1237e48354ceSNicholas Bellinger spin_unlock_bh(&cmd->dataout_timeout_lock);
1238e48354ceSNicholas Bellinger }
12393e1c81a9SNicholas Bellinger EXPORT_SYMBOL(iscsit_stop_dataout_timer);
1240