Merge branch 'r6040-next'
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_datain_values.c
blob647d4a5dca5281838b904ba67fd03d8a0ea80642
1 /*******************************************************************************
2 * This file contains the iSCSI Target DataIN value generation functions.
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 ******************************************************************************/
19 #include <scsi/iscsi_proto.h>
21 #include <target/iscsi/iscsi_target_core.h>
22 #include "iscsi_target_seq_pdu_list.h"
23 #include "iscsi_target_erl1.h"
24 #include "iscsi_target_util.h"
25 #include "iscsi_target.h"
26 #include "iscsi_target_datain_values.h"
28 struct iscsi_datain_req *iscsit_allocate_datain_req(void)
30 struct iscsi_datain_req *dr;
32 dr = kmem_cache_zalloc(lio_dr_cache, GFP_ATOMIC);
33 if (!dr) {
34 pr_err("Unable to allocate memory for"
35 " struct iscsi_datain_req\n");
36 return NULL;
38 INIT_LIST_HEAD(&dr->cmd_datain_node);
40 return dr;
43 void iscsit_attach_datain_req(struct iscsi_cmd *cmd, struct iscsi_datain_req *dr)
45 spin_lock(&cmd->datain_lock);
46 list_add_tail(&dr->cmd_datain_node, &cmd->datain_list);
47 spin_unlock(&cmd->datain_lock);
50 void iscsit_free_datain_req(struct iscsi_cmd *cmd, struct iscsi_datain_req *dr)
52 spin_lock(&cmd->datain_lock);
53 list_del(&dr->cmd_datain_node);
54 spin_unlock(&cmd->datain_lock);
56 kmem_cache_free(lio_dr_cache, dr);
59 void iscsit_free_all_datain_reqs(struct iscsi_cmd *cmd)
61 struct iscsi_datain_req *dr, *dr_tmp;
63 spin_lock(&cmd->datain_lock);
64 list_for_each_entry_safe(dr, dr_tmp, &cmd->datain_list, cmd_datain_node) {
65 list_del(&dr->cmd_datain_node);
66 kmem_cache_free(lio_dr_cache, dr);
68 spin_unlock(&cmd->datain_lock);
71 struct iscsi_datain_req *iscsit_get_datain_req(struct iscsi_cmd *cmd)
73 if (list_empty(&cmd->datain_list)) {
74 pr_err("cmd->datain_list is empty for ITT:"
75 " 0x%08x\n", cmd->init_task_tag);
76 return NULL;
79 return list_first_entry(&cmd->datain_list, struct iscsi_datain_req,
80 cmd_datain_node);
84 * For Normal and Recovery DataSequenceInOrder=Yes and DataPDUInOrder=Yes.
86 static struct iscsi_datain_req *iscsit_set_datain_values_yes_and_yes(
87 struct iscsi_cmd *cmd,
88 struct iscsi_datain *datain)
90 u32 next_burst_len, read_data_done, read_data_left;
91 struct iscsi_conn *conn = cmd->conn;
92 struct iscsi_datain_req *dr;
94 dr = iscsit_get_datain_req(cmd);
95 if (!dr)
96 return NULL;
98 if (dr->recovery && dr->generate_recovery_values) {
99 if (iscsit_create_recovery_datain_values_datasequenceinorder_yes(
100 cmd, dr) < 0)
101 return NULL;
103 dr->generate_recovery_values = 0;
106 next_burst_len = (!dr->recovery) ?
107 cmd->next_burst_len : dr->next_burst_len;
108 read_data_done = (!dr->recovery) ?
109 cmd->read_data_done : dr->read_data_done;
111 read_data_left = (cmd->se_cmd.data_length - read_data_done);
112 if (!read_data_left) {
113 pr_err("ITT: 0x%08x read_data_left is zero!\n",
114 cmd->init_task_tag);
115 return NULL;
118 if ((read_data_left <= conn->conn_ops->MaxRecvDataSegmentLength) &&
119 (read_data_left <= (conn->sess->sess_ops->MaxBurstLength -
120 next_burst_len))) {
121 datain->length = read_data_left;
123 datain->flags |= (ISCSI_FLAG_CMD_FINAL | ISCSI_FLAG_DATA_STATUS);
124 if (conn->sess->sess_ops->ErrorRecoveryLevel > 0)
125 datain->flags |= ISCSI_FLAG_DATA_ACK;
126 } else {
127 if ((next_burst_len +
128 conn->conn_ops->MaxRecvDataSegmentLength) <
129 conn->sess->sess_ops->MaxBurstLength) {
130 datain->length =
131 conn->conn_ops->MaxRecvDataSegmentLength;
132 next_burst_len += datain->length;
133 } else {
134 datain->length = (conn->sess->sess_ops->MaxBurstLength -
135 next_burst_len);
136 next_burst_len = 0;
138 datain->flags |= ISCSI_FLAG_CMD_FINAL;
139 if (conn->sess->sess_ops->ErrorRecoveryLevel > 0)
140 datain->flags |= ISCSI_FLAG_DATA_ACK;
144 datain->data_sn = (!dr->recovery) ? cmd->data_sn++ : dr->data_sn++;
145 datain->offset = read_data_done;
147 if (!dr->recovery) {
148 cmd->next_burst_len = next_burst_len;
149 cmd->read_data_done += datain->length;
150 } else {
151 dr->next_burst_len = next_burst_len;
152 dr->read_data_done += datain->length;
155 if (!dr->recovery) {
156 if (datain->flags & ISCSI_FLAG_DATA_STATUS)
157 dr->dr_complete = DATAIN_COMPLETE_NORMAL;
159 return dr;
162 if (!dr->runlength) {
163 if (datain->flags & ISCSI_FLAG_DATA_STATUS) {
164 dr->dr_complete =
165 (dr->recovery == DATAIN_WITHIN_COMMAND_RECOVERY) ?
166 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY :
167 DATAIN_COMPLETE_CONNECTION_RECOVERY;
169 } else {
170 if ((dr->begrun + dr->runlength) == dr->data_sn) {
171 dr->dr_complete =
172 (dr->recovery == DATAIN_WITHIN_COMMAND_RECOVERY) ?
173 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY :
174 DATAIN_COMPLETE_CONNECTION_RECOVERY;
178 return dr;
182 * For Normal and Recovery DataSequenceInOrder=No and DataPDUInOrder=Yes.
184 static struct iscsi_datain_req *iscsit_set_datain_values_no_and_yes(
185 struct iscsi_cmd *cmd,
186 struct iscsi_datain *datain)
188 u32 offset, read_data_done, read_data_left, seq_send_order;
189 struct iscsi_conn *conn = cmd->conn;
190 struct iscsi_datain_req *dr;
191 struct iscsi_seq *seq;
193 dr = iscsit_get_datain_req(cmd);
194 if (!dr)
195 return NULL;
197 if (dr->recovery && dr->generate_recovery_values) {
198 if (iscsit_create_recovery_datain_values_datasequenceinorder_no(
199 cmd, dr) < 0)
200 return NULL;
202 dr->generate_recovery_values = 0;
205 read_data_done = (!dr->recovery) ?
206 cmd->read_data_done : dr->read_data_done;
207 seq_send_order = (!dr->recovery) ?
208 cmd->seq_send_order : dr->seq_send_order;
210 read_data_left = (cmd->se_cmd.data_length - read_data_done);
211 if (!read_data_left) {
212 pr_err("ITT: 0x%08x read_data_left is zero!\n",
213 cmd->init_task_tag);
214 return NULL;
217 seq = iscsit_get_seq_holder_for_datain(cmd, seq_send_order);
218 if (!seq)
219 return NULL;
221 seq->sent = 1;
223 if (!dr->recovery && !seq->next_burst_len)
224 seq->first_datasn = cmd->data_sn;
226 offset = (seq->offset + seq->next_burst_len);
228 if ((offset + conn->conn_ops->MaxRecvDataSegmentLength) >=
229 cmd->se_cmd.data_length) {
230 datain->length = (cmd->se_cmd.data_length - offset);
231 datain->offset = offset;
233 datain->flags |= ISCSI_FLAG_CMD_FINAL;
234 if (conn->sess->sess_ops->ErrorRecoveryLevel > 0)
235 datain->flags |= ISCSI_FLAG_DATA_ACK;
237 seq->next_burst_len = 0;
238 seq_send_order++;
239 } else {
240 if ((seq->next_burst_len +
241 conn->conn_ops->MaxRecvDataSegmentLength) <
242 conn->sess->sess_ops->MaxBurstLength) {
243 datain->length =
244 conn->conn_ops->MaxRecvDataSegmentLength;
245 datain->offset = (seq->offset + seq->next_burst_len);
247 seq->next_burst_len += datain->length;
248 } else {
249 datain->length = (conn->sess->sess_ops->MaxBurstLength -
250 seq->next_burst_len);
251 datain->offset = (seq->offset + seq->next_burst_len);
253 datain->flags |= ISCSI_FLAG_CMD_FINAL;
254 if (conn->sess->sess_ops->ErrorRecoveryLevel > 0)
255 datain->flags |= ISCSI_FLAG_DATA_ACK;
257 seq->next_burst_len = 0;
258 seq_send_order++;
262 if ((read_data_done + datain->length) == cmd->se_cmd.data_length)
263 datain->flags |= ISCSI_FLAG_DATA_STATUS;
265 datain->data_sn = (!dr->recovery) ? cmd->data_sn++ : dr->data_sn++;
266 if (!dr->recovery) {
267 cmd->seq_send_order = seq_send_order;
268 cmd->read_data_done += datain->length;
269 } else {
270 dr->seq_send_order = seq_send_order;
271 dr->read_data_done += datain->length;
274 if (!dr->recovery) {
275 if (datain->flags & ISCSI_FLAG_CMD_FINAL)
276 seq->last_datasn = datain->data_sn;
277 if (datain->flags & ISCSI_FLAG_DATA_STATUS)
278 dr->dr_complete = DATAIN_COMPLETE_NORMAL;
280 return dr;
283 if (!dr->runlength) {
284 if (datain->flags & ISCSI_FLAG_DATA_STATUS) {
285 dr->dr_complete =
286 (dr->recovery == DATAIN_WITHIN_COMMAND_RECOVERY) ?
287 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY :
288 DATAIN_COMPLETE_CONNECTION_RECOVERY;
290 } else {
291 if ((dr->begrun + dr->runlength) == dr->data_sn) {
292 dr->dr_complete =
293 (dr->recovery == DATAIN_WITHIN_COMMAND_RECOVERY) ?
294 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY :
295 DATAIN_COMPLETE_CONNECTION_RECOVERY;
299 return dr;
303 * For Normal and Recovery DataSequenceInOrder=Yes and DataPDUInOrder=No.
305 static struct iscsi_datain_req *iscsit_set_datain_values_yes_and_no(
306 struct iscsi_cmd *cmd,
307 struct iscsi_datain *datain)
309 u32 next_burst_len, read_data_done, read_data_left;
310 struct iscsi_conn *conn = cmd->conn;
311 struct iscsi_datain_req *dr;
312 struct iscsi_pdu *pdu;
314 dr = iscsit_get_datain_req(cmd);
315 if (!dr)
316 return NULL;
318 if (dr->recovery && dr->generate_recovery_values) {
319 if (iscsit_create_recovery_datain_values_datasequenceinorder_yes(
320 cmd, dr) < 0)
321 return NULL;
323 dr->generate_recovery_values = 0;
326 next_burst_len = (!dr->recovery) ?
327 cmd->next_burst_len : dr->next_burst_len;
328 read_data_done = (!dr->recovery) ?
329 cmd->read_data_done : dr->read_data_done;
331 read_data_left = (cmd->se_cmd.data_length - read_data_done);
332 if (!read_data_left) {
333 pr_err("ITT: 0x%08x read_data_left is zero!\n",
334 cmd->init_task_tag);
335 return dr;
338 pdu = iscsit_get_pdu_holder_for_seq(cmd, NULL);
339 if (!pdu)
340 return dr;
342 if ((read_data_done + pdu->length) == cmd->se_cmd.data_length) {
343 pdu->flags |= (ISCSI_FLAG_CMD_FINAL | ISCSI_FLAG_DATA_STATUS);
344 if (conn->sess->sess_ops->ErrorRecoveryLevel > 0)
345 pdu->flags |= ISCSI_FLAG_DATA_ACK;
347 next_burst_len = 0;
348 } else {
349 if ((next_burst_len + conn->conn_ops->MaxRecvDataSegmentLength) <
350 conn->sess->sess_ops->MaxBurstLength)
351 next_burst_len += pdu->length;
352 else {
353 pdu->flags |= ISCSI_FLAG_CMD_FINAL;
354 if (conn->sess->sess_ops->ErrorRecoveryLevel > 0)
355 pdu->flags |= ISCSI_FLAG_DATA_ACK;
357 next_burst_len = 0;
361 pdu->data_sn = (!dr->recovery) ? cmd->data_sn++ : dr->data_sn++;
362 if (!dr->recovery) {
363 cmd->next_burst_len = next_burst_len;
364 cmd->read_data_done += pdu->length;
365 } else {
366 dr->next_burst_len = next_burst_len;
367 dr->read_data_done += pdu->length;
370 datain->flags = pdu->flags;
371 datain->length = pdu->length;
372 datain->offset = pdu->offset;
373 datain->data_sn = pdu->data_sn;
375 if (!dr->recovery) {
376 if (datain->flags & ISCSI_FLAG_DATA_STATUS)
377 dr->dr_complete = DATAIN_COMPLETE_NORMAL;
379 return dr;
382 if (!dr->runlength) {
383 if (datain->flags & ISCSI_FLAG_DATA_STATUS) {
384 dr->dr_complete =
385 (dr->recovery == DATAIN_WITHIN_COMMAND_RECOVERY) ?
386 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY :
387 DATAIN_COMPLETE_CONNECTION_RECOVERY;
389 } else {
390 if ((dr->begrun + dr->runlength) == dr->data_sn) {
391 dr->dr_complete =
392 (dr->recovery == DATAIN_WITHIN_COMMAND_RECOVERY) ?
393 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY :
394 DATAIN_COMPLETE_CONNECTION_RECOVERY;
398 return dr;
402 * For Normal and Recovery DataSequenceInOrder=No and DataPDUInOrder=No.
404 static struct iscsi_datain_req *iscsit_set_datain_values_no_and_no(
405 struct iscsi_cmd *cmd,
406 struct iscsi_datain *datain)
408 u32 read_data_done, read_data_left, seq_send_order;
409 struct iscsi_conn *conn = cmd->conn;
410 struct iscsi_datain_req *dr;
411 struct iscsi_pdu *pdu;
412 struct iscsi_seq *seq = NULL;
414 dr = iscsit_get_datain_req(cmd);
415 if (!dr)
416 return NULL;
418 if (dr->recovery && dr->generate_recovery_values) {
419 if (iscsit_create_recovery_datain_values_datasequenceinorder_no(
420 cmd, dr) < 0)
421 return NULL;
423 dr->generate_recovery_values = 0;
426 read_data_done = (!dr->recovery) ?
427 cmd->read_data_done : dr->read_data_done;
428 seq_send_order = (!dr->recovery) ?
429 cmd->seq_send_order : dr->seq_send_order;
431 read_data_left = (cmd->se_cmd.data_length - read_data_done);
432 if (!read_data_left) {
433 pr_err("ITT: 0x%08x read_data_left is zero!\n",
434 cmd->init_task_tag);
435 return NULL;
438 seq = iscsit_get_seq_holder_for_datain(cmd, seq_send_order);
439 if (!seq)
440 return NULL;
442 seq->sent = 1;
444 if (!dr->recovery && !seq->next_burst_len)
445 seq->first_datasn = cmd->data_sn;
447 pdu = iscsit_get_pdu_holder_for_seq(cmd, seq);
448 if (!pdu)
449 return NULL;
451 if (seq->pdu_send_order == seq->pdu_count) {
452 pdu->flags |= ISCSI_FLAG_CMD_FINAL;
453 if (conn->sess->sess_ops->ErrorRecoveryLevel > 0)
454 pdu->flags |= ISCSI_FLAG_DATA_ACK;
456 seq->next_burst_len = 0;
457 seq_send_order++;
458 } else
459 seq->next_burst_len += pdu->length;
461 if ((read_data_done + pdu->length) == cmd->se_cmd.data_length)
462 pdu->flags |= ISCSI_FLAG_DATA_STATUS;
464 pdu->data_sn = (!dr->recovery) ? cmd->data_sn++ : dr->data_sn++;
465 if (!dr->recovery) {
466 cmd->seq_send_order = seq_send_order;
467 cmd->read_data_done += pdu->length;
468 } else {
469 dr->seq_send_order = seq_send_order;
470 dr->read_data_done += pdu->length;
473 datain->flags = pdu->flags;
474 datain->length = pdu->length;
475 datain->offset = pdu->offset;
476 datain->data_sn = pdu->data_sn;
478 if (!dr->recovery) {
479 if (datain->flags & ISCSI_FLAG_CMD_FINAL)
480 seq->last_datasn = datain->data_sn;
481 if (datain->flags & ISCSI_FLAG_DATA_STATUS)
482 dr->dr_complete = DATAIN_COMPLETE_NORMAL;
484 return dr;
487 if (!dr->runlength) {
488 if (datain->flags & ISCSI_FLAG_DATA_STATUS) {
489 dr->dr_complete =
490 (dr->recovery == DATAIN_WITHIN_COMMAND_RECOVERY) ?
491 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY :
492 DATAIN_COMPLETE_CONNECTION_RECOVERY;
494 } else {
495 if ((dr->begrun + dr->runlength) == dr->data_sn) {
496 dr->dr_complete =
497 (dr->recovery == DATAIN_WITHIN_COMMAND_RECOVERY) ?
498 DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY :
499 DATAIN_COMPLETE_CONNECTION_RECOVERY;
503 return dr;
506 struct iscsi_datain_req *iscsit_get_datain_values(
507 struct iscsi_cmd *cmd,
508 struct iscsi_datain *datain)
510 struct iscsi_conn *conn = cmd->conn;
512 if (conn->sess->sess_ops->DataSequenceInOrder &&
513 conn->sess->sess_ops->DataPDUInOrder)
514 return iscsit_set_datain_values_yes_and_yes(cmd, datain);
515 else if (!conn->sess->sess_ops->DataSequenceInOrder &&
516 conn->sess->sess_ops->DataPDUInOrder)
517 return iscsit_set_datain_values_no_and_yes(cmd, datain);
518 else if (conn->sess->sess_ops->DataSequenceInOrder &&
519 !conn->sess->sess_ops->DataPDUInOrder)
520 return iscsit_set_datain_values_yes_and_no(cmd, datain);
521 else if (!conn->sess->sess_ops->DataSequenceInOrder &&
522 !conn->sess->sess_ops->DataPDUInOrder)
523 return iscsit_set_datain_values_no_and_no(cmd, datain);
525 return NULL;
527 EXPORT_SYMBOL(iscsit_get_datain_values);