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 iscsit_release_cmd(cmd
);
149 transport_generic_free_cmd(&cmd
->se_cmd
, 1);
150 spin_lock(&cr
->conn_recovery_cmd_lock
);
152 spin_unlock(&cr
->conn_recovery_cmd_lock
);
153 spin_lock(&sess
->cr_a_lock
);
157 spin_unlock(&sess
->cr_a_lock
);
159 spin_lock(&sess
->cr_i_lock
);
160 list_for_each_entry_safe(cr
, cr_tmp
, &sess
->cr_inactive_list
, cr_list
) {
161 list_del(&cr
->cr_list
);
162 spin_unlock(&sess
->cr_i_lock
);
164 spin_lock(&cr
->conn_recovery_cmd_lock
);
165 list_for_each_entry_safe(cmd
, cmd_tmp
,
166 &cr
->conn_recovery_cmd_list
, i_list
) {
168 list_del(&cmd
->i_list
);
170 spin_unlock(&cr
->conn_recovery_cmd_lock
);
171 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
))
172 iscsit_release_cmd(cmd
);
174 transport_generic_free_cmd(&cmd
->se_cmd
, 1);
175 spin_lock(&cr
->conn_recovery_cmd_lock
);
177 spin_unlock(&cr
->conn_recovery_cmd_lock
);
178 spin_lock(&sess
->cr_i_lock
);
182 spin_unlock(&sess
->cr_i_lock
);
185 int iscsit_remove_active_connection_recovery_entry(
186 struct iscsi_conn_recovery
*cr
,
187 struct iscsi_session
*sess
)
189 spin_lock(&sess
->cr_a_lock
);
190 list_del(&cr
->cr_list
);
192 sess
->conn_recovery_count
--;
193 pr_debug("Decremented connection recovery count to %u for"
194 " SID: %u\n", sess
->conn_recovery_count
, sess
->sid
);
195 spin_unlock(&sess
->cr_a_lock
);
202 int iscsit_remove_inactive_connection_recovery_entry(
203 struct iscsi_conn_recovery
*cr
,
204 struct iscsi_session
*sess
)
206 spin_lock(&sess
->cr_i_lock
);
207 list_del(&cr
->cr_list
);
208 spin_unlock(&sess
->cr_i_lock
);
214 * Called with cr->conn_recovery_cmd_lock help.
216 int iscsit_remove_cmd_from_connection_recovery(
217 struct iscsi_cmd
*cmd
,
218 struct iscsi_session
*sess
)
220 struct iscsi_conn_recovery
*cr
;
223 pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
224 " is NULL!\n", cmd
->init_task_tag
);
229 list_del(&cmd
->i_list
);
230 return --cr
->cmd_count
;
233 void iscsit_discard_cr_cmds_by_expstatsn(
234 struct iscsi_conn_recovery
*cr
,
237 u32 dropped_count
= 0;
238 struct iscsi_cmd
*cmd
, *cmd_tmp
;
239 struct iscsi_session
*sess
= cr
->sess
;
241 spin_lock(&cr
->conn_recovery_cmd_lock
);
242 list_for_each_entry_safe(cmd
, cmd_tmp
,
243 &cr
->conn_recovery_cmd_list
, i_list
) {
245 if (((cmd
->deferred_i_state
!= ISTATE_SENT_STATUS
) &&
246 (cmd
->deferred_i_state
!= ISTATE_REMOVE
)) ||
247 (cmd
->stat_sn
>= exp_statsn
)) {
252 pr_debug("Dropping Acknowledged ITT: 0x%08x, StatSN:"
253 " 0x%08x, CID: %hu.\n", cmd
->init_task_tag
,
254 cmd
->stat_sn
, cr
->cid
);
256 iscsit_remove_cmd_from_connection_recovery(cmd
, sess
);
258 spin_unlock(&cr
->conn_recovery_cmd_lock
);
259 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
))
260 iscsit_release_cmd(cmd
);
262 transport_generic_free_cmd(&cmd
->se_cmd
, 1);
263 spin_lock(&cr
->conn_recovery_cmd_lock
);
265 spin_unlock(&cr
->conn_recovery_cmd_lock
);
267 pr_debug("Dropped %u total acknowledged commands on"
268 " CID: %hu less than old ExpStatSN: 0x%08x\n",
269 dropped_count
, cr
->cid
, exp_statsn
);
271 if (!cr
->cmd_count
) {
272 pr_debug("No commands to be reassigned for failed"
273 " connection CID: %hu on SID: %u\n",
275 iscsit_remove_inactive_connection_recovery_entry(cr
, sess
);
276 iscsit_attach_active_connection_recovery_entry(sess
, cr
);
277 pr_debug("iSCSI connection recovery successful for CID:"
278 " %hu on SID: %u\n", cr
->cid
, sess
->sid
);
279 iscsit_remove_active_connection_recovery_entry(cr
, sess
);
281 iscsit_remove_inactive_connection_recovery_entry(cr
, sess
);
282 iscsit_attach_active_connection_recovery_entry(sess
, cr
);
286 int iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(struct iscsi_conn
*conn
)
288 u32 dropped_count
= 0;
289 struct iscsi_cmd
*cmd
, *cmd_tmp
;
290 struct iscsi_ooo_cmdsn
*ooo_cmdsn
, *ooo_cmdsn_tmp
;
291 struct iscsi_session
*sess
= conn
->sess
;
293 mutex_lock(&sess
->cmdsn_mutex
);
294 list_for_each_entry_safe(ooo_cmdsn
, ooo_cmdsn_tmp
,
295 &sess
->sess_ooo_cmdsn_list
, ooo_list
) {
297 if (ooo_cmdsn
->cid
!= conn
->cid
)
301 pr_debug("Dropping unacknowledged CmdSN:"
302 " 0x%08x during connection recovery on CID: %hu\n",
303 ooo_cmdsn
->cmdsn
, conn
->cid
);
304 iscsit_remove_ooo_cmdsn(sess
, ooo_cmdsn
);
306 mutex_unlock(&sess
->cmdsn_mutex
);
308 spin_lock_bh(&conn
->cmd_lock
);
309 list_for_each_entry_safe(cmd
, cmd_tmp
, &conn
->conn_cmd_list
, i_list
) {
310 if (!(cmd
->cmd_flags
& ICF_OOO_CMDSN
))
313 list_del(&cmd
->i_list
);
315 spin_unlock_bh(&conn
->cmd_lock
);
316 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
))
317 iscsit_release_cmd(cmd
);
319 transport_generic_free_cmd(&cmd
->se_cmd
, 1);
320 spin_lock_bh(&conn
->cmd_lock
);
322 spin_unlock_bh(&conn
->cmd_lock
);
324 pr_debug("Dropped %u total unacknowledged commands on CID:"
325 " %hu for ExpCmdSN: 0x%08x.\n", dropped_count
, conn
->cid
,
330 int iscsit_prepare_cmds_for_realligance(struct iscsi_conn
*conn
)
333 struct iscsi_cmd
*cmd
, *cmd_tmp
;
334 struct iscsi_conn_recovery
*cr
;
337 * Allocate an struct iscsi_conn_recovery for this connection.
338 * Each struct iscsi_cmd contains an struct iscsi_conn_recovery pointer
339 * (struct iscsi_cmd->cr) so we need to allocate this before preparing the
340 * connection's command list for connection recovery.
342 cr
= kzalloc(sizeof(struct iscsi_conn_recovery
), GFP_KERNEL
);
344 pr_err("Unable to allocate memory for"
345 " struct iscsi_conn_recovery.\n");
348 INIT_LIST_HEAD(&cr
->cr_list
);
349 INIT_LIST_HEAD(&cr
->conn_recovery_cmd_list
);
350 spin_lock_init(&cr
->conn_recovery_cmd_lock
);
352 * Only perform connection recovery on ISCSI_OP_SCSI_CMD or
353 * ISCSI_OP_NOOP_OUT opcodes. For all other opcodes call
354 * list_del(&cmd->i_list); to release the command to the
355 * session pool and remove it from the connection's list.
357 * Also stop the DataOUT timer, which will be restarted after
358 * sending the TMR response.
360 spin_lock_bh(&conn
->cmd_lock
);
361 list_for_each_entry_safe(cmd
, cmd_tmp
, &conn
->conn_cmd_list
, i_list
) {
363 if ((cmd
->iscsi_opcode
!= ISCSI_OP_SCSI_CMD
) &&
364 (cmd
->iscsi_opcode
!= ISCSI_OP_NOOP_OUT
)) {
365 pr_debug("Not performing realligence on"
366 " Opcode: 0x%02x, ITT: 0x%08x, CmdSN: 0x%08x,"
367 " CID: %hu\n", cmd
->iscsi_opcode
,
368 cmd
->init_task_tag
, cmd
->cmd_sn
, conn
->cid
);
370 list_del(&cmd
->i_list
);
371 spin_unlock_bh(&conn
->cmd_lock
);
373 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
))
374 iscsit_release_cmd(cmd
);
376 transport_generic_free_cmd(&cmd
->se_cmd
, 1);
377 spin_lock_bh(&conn
->cmd_lock
);
382 * Special case where commands greater than or equal to
383 * the session's ExpCmdSN are attached to the connection
384 * list but not to the out of order CmdSN list. The one
385 * obvious case is when a command with immediate data
386 * attached must only check the CmdSN against ExpCmdSN
387 * after the data is received. The special case below
388 * is when the connection fails before data is received,
389 * but also may apply to other PDUs, so it has been
392 if (!(cmd
->cmd_flags
& ICF_OOO_CMDSN
) && !cmd
->immediate_cmd
&&
393 (cmd
->cmd_sn
>= conn
->sess
->exp_cmd_sn
)) {
394 list_del(&cmd
->i_list
);
395 spin_unlock_bh(&conn
->cmd_lock
);
397 if (!(cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
))
398 iscsit_release_cmd(cmd
);
400 transport_generic_free_cmd(&cmd
->se_cmd
, 1);
401 spin_lock_bh(&conn
->cmd_lock
);
406 pr_debug("Preparing Opcode: 0x%02x, ITT: 0x%08x,"
407 " CmdSN: 0x%08x, StatSN: 0x%08x, CID: %hu for"
408 " realligence.\n", cmd
->iscsi_opcode
,
409 cmd
->init_task_tag
, cmd
->cmd_sn
, cmd
->stat_sn
,
412 cmd
->deferred_i_state
= cmd
->i_state
;
413 cmd
->i_state
= ISTATE_IN_CONNECTION_RECOVERY
;
415 if (cmd
->data_direction
== DMA_TO_DEVICE
)
416 iscsit_stop_dataout_timer(cmd
);
418 cmd
->sess
= conn
->sess
;
420 list_del(&cmd
->i_list
);
421 spin_unlock_bh(&conn
->cmd_lock
);
423 iscsit_free_all_datain_reqs(cmd
);
425 if (cmd
->se_cmd
.se_cmd_flags
& SCF_SE_LUN_CMD
)
426 transport_wait_for_tasks(&cmd
->se_cmd
);
428 * Add the struct iscsi_cmd to the connection recovery cmd list
430 spin_lock(&cr
->conn_recovery_cmd_lock
);
431 list_add_tail(&cmd
->i_list
, &cr
->conn_recovery_cmd_list
);
432 spin_unlock(&cr
->conn_recovery_cmd_lock
);
434 spin_lock_bh(&conn
->cmd_lock
);
438 spin_unlock_bh(&conn
->cmd_lock
);
440 * Fill in the various values in the preallocated struct iscsi_conn_recovery.
443 cr
->cmd_count
= cmd_count
;
444 cr
->maxrecvdatasegmentlength
= conn
->conn_ops
->MaxRecvDataSegmentLength
;
445 cr
->sess
= conn
->sess
;
447 iscsit_attach_inactive_connection_recovery_entry(conn
->sess
, cr
);
452 int iscsit_connection_recovery_transport_reset(struct iscsi_conn
*conn
)
454 atomic_set(&conn
->connection_recovery
, 1);
456 if (iscsit_close_connection(conn
) < 0)