1 /*******************************************************************************
2 * This file contains error recovery level two functions used by
3 * the iSCSI Target driver.
5 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
7 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
9 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 ******************************************************************************/
22 #include <scsi/iscsi_proto.h>
23 #include <target/target_core_base.h>
24 #include <target/target_core_transport.h>
26 #include "iscsi_target_core.h"
27 #include "iscsi_target_datain_values.h"
28 #include "iscsi_target_util.h"
29 #include "iscsi_target_erl0.h"
30 #include "iscsi_target_erl1.h"
31 #include "iscsi_target_erl2.h"
32 #include "iscsi_target.h"
35 * FIXME: Does RData SNACK apply here as well?
37 void iscsit_create_conn_recovery_datain_values(
38 struct iscsi_cmd
*cmd
,
42 struct iscsi_conn
*conn
= cmd
->conn
;
44 cmd
->next_burst_len
= 0;
45 cmd
->read_data_done
= 0;
47 while (exp_data_sn
> data_sn
) {
48 if ((cmd
->next_burst_len
+
49 conn
->conn_ops
->MaxRecvDataSegmentLength
) <
50 conn
->sess
->sess_ops
->MaxBurstLength
) {
51 cmd
->read_data_done
+=
52 conn
->conn_ops
->MaxRecvDataSegmentLength
;
53 cmd
->next_burst_len
+=
54 conn
->conn_ops
->MaxRecvDataSegmentLength
;
56 cmd
->read_data_done
+=
57 (conn
->sess
->sess_ops
->MaxBurstLength
-
59 cmd
->next_burst_len
= 0;
65 void iscsit_create_conn_recovery_dataout_values(
66 struct iscsi_cmd
*cmd
)
68 u32 write_data_done
= 0;
69 struct iscsi_conn
*conn
= cmd
->conn
;
72 cmd
->next_burst_len
= 0;
74 while (cmd
->write_data_done
> write_data_done
) {
75 if ((write_data_done
+ conn
->sess
->sess_ops
->MaxBurstLength
) <=
77 write_data_done
+= conn
->sess
->sess_ops
->MaxBurstLength
;
82 cmd
->write_data_done
= write_data_done
;
85 static int iscsit_attach_active_connection_recovery_entry(
86 struct iscsi_session
*sess
,
87 struct iscsi_conn_recovery
*cr
)
89 spin_lock(&sess
->cr_a_lock
);
90 list_add_tail(&cr
->cr_list
, &sess
->cr_active_list
);
91 spin_unlock(&sess
->cr_a_lock
);
96 static int iscsit_attach_inactive_connection_recovery_entry(
97 struct iscsi_session
*sess
,
98 struct iscsi_conn_recovery
*cr
)
100 spin_lock(&sess
->cr_i_lock
);
101 list_add_tail(&cr
->cr_list
, &sess
->cr_inactive_list
);
103 sess
->conn_recovery_count
++;
104 pr_debug("Incremented connection recovery count to %u for"
105 " SID: %u\n", sess
->conn_recovery_count
, sess
->sid
);
106 spin_unlock(&sess
->cr_i_lock
);
111 struct iscsi_conn_recovery
*iscsit_get_inactive_connection_recovery_entry(
112 struct iscsi_session
*sess
,
115 struct iscsi_conn_recovery
*cr
;
117 spin_lock(&sess
->cr_i_lock
);
118 list_for_each_entry(cr
, &sess
->cr_inactive_list
, cr_list
) {
119 if (cr
->cid
== cid
) {
120 spin_unlock(&sess
->cr_i_lock
);
124 spin_unlock(&sess
->cr_i_lock
);
129 void iscsit_free_connection_recovery_entires(struct iscsi_session
*sess
)
131 struct iscsi_cmd
*cmd
, *cmd_tmp
;
132 struct iscsi_conn_recovery
*cr
, *cr_tmp
;
134 spin_lock(&sess
->cr_a_lock
);
135 list_for_each_entry_safe(cr
, cr_tmp
, &sess
->cr_active_list
, cr_list
) {
136 list_del(&cr
->cr_list
);
137 spin_unlock(&sess
->cr_a_lock
);
139 spin_lock(&cr
->conn_recovery_cmd_lock
);
140 list_for_each_entry_safe(cmd
, cmd_tmp
,
141 &cr
->conn_recovery_cmd_list
, i_list
) {
143 list_del(&cmd
->i_list
);
145 spin_unlock(&cr
->conn_recovery_cmd_lock
);
146 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
) ||
147 !(cmd
->se_cmd
.transport_wait_for_tasks
))
148 iscsit_release_cmd(cmd
);
150 cmd
->se_cmd
.transport_wait_for_tasks(
152 spin_lock(&cr
->conn_recovery_cmd_lock
);
154 spin_unlock(&cr
->conn_recovery_cmd_lock
);
155 spin_lock(&sess
->cr_a_lock
);
159 spin_unlock(&sess
->cr_a_lock
);
161 spin_lock(&sess
->cr_i_lock
);
162 list_for_each_entry_safe(cr
, cr_tmp
, &sess
->cr_inactive_list
, cr_list
) {
163 list_del(&cr
->cr_list
);
164 spin_unlock(&sess
->cr_i_lock
);
166 spin_lock(&cr
->conn_recovery_cmd_lock
);
167 list_for_each_entry_safe(cmd
, cmd_tmp
,
168 &cr
->conn_recovery_cmd_list
, i_list
) {
170 list_del(&cmd
->i_list
);
172 spin_unlock(&cr
->conn_recovery_cmd_lock
);
173 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
) ||
174 !(cmd
->se_cmd
.transport_wait_for_tasks
))
175 iscsit_release_cmd(cmd
);
177 cmd
->se_cmd
.transport_wait_for_tasks(
179 spin_lock(&cr
->conn_recovery_cmd_lock
);
181 spin_unlock(&cr
->conn_recovery_cmd_lock
);
182 spin_lock(&sess
->cr_i_lock
);
186 spin_unlock(&sess
->cr_i_lock
);
189 int iscsit_remove_active_connection_recovery_entry(
190 struct iscsi_conn_recovery
*cr
,
191 struct iscsi_session
*sess
)
193 spin_lock(&sess
->cr_a_lock
);
194 list_del(&cr
->cr_list
);
196 sess
->conn_recovery_count
--;
197 pr_debug("Decremented connection recovery count to %u for"
198 " SID: %u\n", sess
->conn_recovery_count
, sess
->sid
);
199 spin_unlock(&sess
->cr_a_lock
);
206 int iscsit_remove_inactive_connection_recovery_entry(
207 struct iscsi_conn_recovery
*cr
,
208 struct iscsi_session
*sess
)
210 spin_lock(&sess
->cr_i_lock
);
211 list_del(&cr
->cr_list
);
212 spin_unlock(&sess
->cr_i_lock
);
218 * Called with cr->conn_recovery_cmd_lock help.
220 int iscsit_remove_cmd_from_connection_recovery(
221 struct iscsi_cmd
*cmd
,
222 struct iscsi_session
*sess
)
224 struct iscsi_conn_recovery
*cr
;
227 pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
228 " is NULL!\n", cmd
->init_task_tag
);
233 list_del(&cmd
->i_list
);
234 return --cr
->cmd_count
;
237 void iscsit_discard_cr_cmds_by_expstatsn(
238 struct iscsi_conn_recovery
*cr
,
241 u32 dropped_count
= 0;
242 struct iscsi_cmd
*cmd
, *cmd_tmp
;
243 struct iscsi_session
*sess
= cr
->sess
;
245 spin_lock(&cr
->conn_recovery_cmd_lock
);
246 list_for_each_entry_safe(cmd
, cmd_tmp
,
247 &cr
->conn_recovery_cmd_list
, i_list
) {
249 if (((cmd
->deferred_i_state
!= ISTATE_SENT_STATUS
) &&
250 (cmd
->deferred_i_state
!= ISTATE_REMOVE
)) ||
251 (cmd
->stat_sn
>= exp_statsn
)) {
256 pr_debug("Dropping Acknowledged ITT: 0x%08x, StatSN:"
257 " 0x%08x, CID: %hu.\n", cmd
->init_task_tag
,
258 cmd
->stat_sn
, cr
->cid
);
260 iscsit_remove_cmd_from_connection_recovery(cmd
, sess
);
262 spin_unlock(&cr
->conn_recovery_cmd_lock
);
263 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
) ||
264 !(cmd
->se_cmd
.transport_wait_for_tasks
))
265 iscsit_release_cmd(cmd
);
267 cmd
->se_cmd
.transport_wait_for_tasks(
269 spin_lock(&cr
->conn_recovery_cmd_lock
);
271 spin_unlock(&cr
->conn_recovery_cmd_lock
);
273 pr_debug("Dropped %u total acknowledged commands on"
274 " CID: %hu less than old ExpStatSN: 0x%08x\n",
275 dropped_count
, cr
->cid
, exp_statsn
);
277 if (!cr
->cmd_count
) {
278 pr_debug("No commands to be reassigned for failed"
279 " connection CID: %hu on SID: %u\n",
281 iscsit_remove_inactive_connection_recovery_entry(cr
, sess
);
282 iscsit_attach_active_connection_recovery_entry(sess
, cr
);
283 pr_debug("iSCSI connection recovery successful for CID:"
284 " %hu on SID: %u\n", cr
->cid
, sess
->sid
);
285 iscsit_remove_active_connection_recovery_entry(cr
, sess
);
287 iscsit_remove_inactive_connection_recovery_entry(cr
, sess
);
288 iscsit_attach_active_connection_recovery_entry(sess
, cr
);
292 int iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(struct iscsi_conn
*conn
)
294 u32 dropped_count
= 0;
295 struct iscsi_cmd
*cmd
, *cmd_tmp
;
296 struct iscsi_ooo_cmdsn
*ooo_cmdsn
, *ooo_cmdsn_tmp
;
297 struct iscsi_session
*sess
= conn
->sess
;
299 mutex_lock(&sess
->cmdsn_mutex
);
300 list_for_each_entry_safe(ooo_cmdsn
, ooo_cmdsn_tmp
,
301 &sess
->sess_ooo_cmdsn_list
, ooo_list
) {
303 if (ooo_cmdsn
->cid
!= conn
->cid
)
307 pr_debug("Dropping unacknowledged CmdSN:"
308 " 0x%08x during connection recovery on CID: %hu\n",
309 ooo_cmdsn
->cmdsn
, conn
->cid
);
310 iscsit_remove_ooo_cmdsn(sess
, ooo_cmdsn
);
312 mutex_unlock(&sess
->cmdsn_mutex
);
314 spin_lock_bh(&conn
->cmd_lock
);
315 list_for_each_entry_safe(cmd
, cmd_tmp
, &conn
->conn_cmd_list
, i_list
) {
316 if (!(cmd
->cmd_flags
& ICF_OOO_CMDSN
))
319 list_del(&cmd
->i_list
);
321 spin_unlock_bh(&conn
->cmd_lock
);
322 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
) ||
323 !(cmd
->se_cmd
.transport_wait_for_tasks
))
324 iscsit_release_cmd(cmd
);
326 cmd
->se_cmd
.transport_wait_for_tasks(
328 spin_lock_bh(&conn
->cmd_lock
);
330 spin_unlock_bh(&conn
->cmd_lock
);
332 pr_debug("Dropped %u total unacknowledged commands on CID:"
333 " %hu for ExpCmdSN: 0x%08x.\n", dropped_count
, conn
->cid
,
338 int iscsit_prepare_cmds_for_realligance(struct iscsi_conn
*conn
)
341 struct iscsi_cmd
*cmd
, *cmd_tmp
;
342 struct iscsi_conn_recovery
*cr
;
345 * Allocate an struct iscsi_conn_recovery for this connection.
346 * Each struct iscsi_cmd contains an struct iscsi_conn_recovery pointer
347 * (struct iscsi_cmd->cr) so we need to allocate this before preparing the
348 * connection's command list for connection recovery.
350 cr
= kzalloc(sizeof(struct iscsi_conn_recovery
), GFP_KERNEL
);
352 pr_err("Unable to allocate memory for"
353 " struct iscsi_conn_recovery.\n");
356 INIT_LIST_HEAD(&cr
->cr_list
);
357 INIT_LIST_HEAD(&cr
->conn_recovery_cmd_list
);
358 spin_lock_init(&cr
->conn_recovery_cmd_lock
);
360 * Only perform connection recovery on ISCSI_OP_SCSI_CMD or
361 * ISCSI_OP_NOOP_OUT opcodes. For all other opcodes call
362 * list_del(&cmd->i_list); to release the command to the
363 * session pool and remove it from the connection's list.
365 * Also stop the DataOUT timer, which will be restarted after
366 * sending the TMR response.
368 spin_lock_bh(&conn
->cmd_lock
);
369 list_for_each_entry_safe(cmd
, cmd_tmp
, &conn
->conn_cmd_list
, i_list
) {
371 if ((cmd
->iscsi_opcode
!= ISCSI_OP_SCSI_CMD
) &&
372 (cmd
->iscsi_opcode
!= ISCSI_OP_NOOP_OUT
)) {
373 pr_debug("Not performing realligence on"
374 " Opcode: 0x%02x, ITT: 0x%08x, CmdSN: 0x%08x,"
375 " CID: %hu\n", cmd
->iscsi_opcode
,
376 cmd
->init_task_tag
, cmd
->cmd_sn
, conn
->cid
);
378 list_del(&cmd
->i_list
);
379 spin_unlock_bh(&conn
->cmd_lock
);
381 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
) ||
382 !(cmd
->se_cmd
.transport_wait_for_tasks
))
383 iscsit_release_cmd(cmd
);
385 cmd
->se_cmd
.transport_wait_for_tasks(
387 spin_lock_bh(&conn
->cmd_lock
);
392 * Special case where commands greater than or equal to
393 * the session's ExpCmdSN are attached to the connection
394 * list but not to the out of order CmdSN list. The one
395 * obvious case is when a command with immediate data
396 * attached must only check the CmdSN against ExpCmdSN
397 * after the data is received. The special case below
398 * is when the connection fails before data is received,
399 * but also may apply to other PDUs, so it has been
402 if (!(cmd
->cmd_flags
& ICF_OOO_CMDSN
) && !cmd
->immediate_cmd
&&
403 (cmd
->cmd_sn
>= conn
->sess
->exp_cmd_sn
)) {
404 list_del(&cmd
->i_list
);
405 spin_unlock_bh(&conn
->cmd_lock
);
407 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
) ||
408 !(cmd
->se_cmd
.transport_wait_for_tasks
))
409 iscsit_release_cmd(cmd
);
411 cmd
->se_cmd
.transport_wait_for_tasks(
413 spin_lock_bh(&conn
->cmd_lock
);
418 pr_debug("Preparing Opcode: 0x%02x, ITT: 0x%08x,"
419 " CmdSN: 0x%08x, StatSN: 0x%08x, CID: %hu for"
420 " realligence.\n", cmd
->iscsi_opcode
,
421 cmd
->init_task_tag
, cmd
->cmd_sn
, cmd
->stat_sn
,
424 cmd
->deferred_i_state
= cmd
->i_state
;
425 cmd
->i_state
= ISTATE_IN_CONNECTION_RECOVERY
;
427 if (cmd
->data_direction
== DMA_TO_DEVICE
)
428 iscsit_stop_dataout_timer(cmd
);
430 cmd
->sess
= conn
->sess
;
432 list_del(&cmd
->i_list
);
433 spin_unlock_bh(&conn
->cmd_lock
);
435 iscsit_free_all_datain_reqs(cmd
);
437 if ((cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
) &&
438 cmd
->se_cmd
.transport_wait_for_tasks
)
439 cmd
->se_cmd
.transport_wait_for_tasks(&cmd
->se_cmd
,
442 * Add the struct iscsi_cmd to the connection recovery cmd list
444 spin_lock(&cr
->conn_recovery_cmd_lock
);
445 list_add_tail(&cmd
->i_list
, &cr
->conn_recovery_cmd_list
);
446 spin_unlock(&cr
->conn_recovery_cmd_lock
);
448 spin_lock_bh(&conn
->cmd_lock
);
452 spin_unlock_bh(&conn
->cmd_lock
);
454 * Fill in the various values in the preallocated struct iscsi_conn_recovery.
457 cr
->cmd_count
= cmd_count
;
458 cr
->maxrecvdatasegmentlength
= conn
->conn_ops
->MaxRecvDataSegmentLength
;
459 cr
->sess
= conn
->sess
;
461 iscsit_attach_inactive_connection_recovery_entry(conn
->sess
, cr
);
466 int iscsit_connection_recovery_transport_reset(struct iscsi_conn
*conn
)
468 atomic_set(&conn
->connection_recovery
, 1);
470 if (iscsit_close_connection(conn
) < 0)