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_fabric.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 (be32_to_cpu(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_conn_node
) {
143 list_del(&cmd
->i_conn_node
);
145 spin_unlock(&cr
->conn_recovery_cmd_lock
);
146 iscsit_free_cmd(cmd
, true);
147 spin_lock(&cr
->conn_recovery_cmd_lock
);
149 spin_unlock(&cr
->conn_recovery_cmd_lock
);
150 spin_lock(&sess
->cr_a_lock
);
154 spin_unlock(&sess
->cr_a_lock
);
156 spin_lock(&sess
->cr_i_lock
);
157 list_for_each_entry_safe(cr
, cr_tmp
, &sess
->cr_inactive_list
, cr_list
) {
158 list_del(&cr
->cr_list
);
159 spin_unlock(&sess
->cr_i_lock
);
161 spin_lock(&cr
->conn_recovery_cmd_lock
);
162 list_for_each_entry_safe(cmd
, cmd_tmp
,
163 &cr
->conn_recovery_cmd_list
, i_conn_node
) {
165 list_del(&cmd
->i_conn_node
);
167 spin_unlock(&cr
->conn_recovery_cmd_lock
);
168 iscsit_free_cmd(cmd
, true);
169 spin_lock(&cr
->conn_recovery_cmd_lock
);
171 spin_unlock(&cr
->conn_recovery_cmd_lock
);
172 spin_lock(&sess
->cr_i_lock
);
176 spin_unlock(&sess
->cr_i_lock
);
179 int iscsit_remove_active_connection_recovery_entry(
180 struct iscsi_conn_recovery
*cr
,
181 struct iscsi_session
*sess
)
183 spin_lock(&sess
->cr_a_lock
);
184 list_del(&cr
->cr_list
);
186 sess
->conn_recovery_count
--;
187 pr_debug("Decremented connection recovery count to %u for"
188 " SID: %u\n", sess
->conn_recovery_count
, sess
->sid
);
189 spin_unlock(&sess
->cr_a_lock
);
196 static void iscsit_remove_inactive_connection_recovery_entry(
197 struct iscsi_conn_recovery
*cr
,
198 struct iscsi_session
*sess
)
200 spin_lock(&sess
->cr_i_lock
);
201 list_del(&cr
->cr_list
);
202 spin_unlock(&sess
->cr_i_lock
);
206 * Called with cr->conn_recovery_cmd_lock help.
208 int iscsit_remove_cmd_from_connection_recovery(
209 struct iscsi_cmd
*cmd
,
210 struct iscsi_session
*sess
)
212 struct iscsi_conn_recovery
*cr
;
215 pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
216 " is NULL!\n", cmd
->init_task_tag
);
221 list_del(&cmd
->i_conn_node
);
222 return --cr
->cmd_count
;
225 void iscsit_discard_cr_cmds_by_expstatsn(
226 struct iscsi_conn_recovery
*cr
,
229 u32 dropped_count
= 0;
230 struct iscsi_cmd
*cmd
, *cmd_tmp
;
231 struct iscsi_session
*sess
= cr
->sess
;
233 spin_lock(&cr
->conn_recovery_cmd_lock
);
234 list_for_each_entry_safe(cmd
, cmd_tmp
,
235 &cr
->conn_recovery_cmd_list
, i_conn_node
) {
237 if (((cmd
->deferred_i_state
!= ISTATE_SENT_STATUS
) &&
238 (cmd
->deferred_i_state
!= ISTATE_REMOVE
)) ||
239 (cmd
->stat_sn
>= exp_statsn
)) {
244 pr_debug("Dropping Acknowledged ITT: 0x%08x, StatSN:"
245 " 0x%08x, CID: %hu.\n", cmd
->init_task_tag
,
246 cmd
->stat_sn
, cr
->cid
);
248 iscsit_remove_cmd_from_connection_recovery(cmd
, sess
);
250 spin_unlock(&cr
->conn_recovery_cmd_lock
);
251 iscsit_free_cmd(cmd
, true);
252 spin_lock(&cr
->conn_recovery_cmd_lock
);
254 spin_unlock(&cr
->conn_recovery_cmd_lock
);
256 pr_debug("Dropped %u total acknowledged commands on"
257 " CID: %hu less than old ExpStatSN: 0x%08x\n",
258 dropped_count
, cr
->cid
, exp_statsn
);
260 if (!cr
->cmd_count
) {
261 pr_debug("No commands to be reassigned for failed"
262 " connection CID: %hu on SID: %u\n",
264 iscsit_remove_inactive_connection_recovery_entry(cr
, sess
);
265 iscsit_attach_active_connection_recovery_entry(sess
, cr
);
266 pr_debug("iSCSI connection recovery successful for CID:"
267 " %hu on SID: %u\n", cr
->cid
, sess
->sid
);
268 iscsit_remove_active_connection_recovery_entry(cr
, sess
);
270 iscsit_remove_inactive_connection_recovery_entry(cr
, sess
);
271 iscsit_attach_active_connection_recovery_entry(sess
, cr
);
275 int iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(struct iscsi_conn
*conn
)
277 u32 dropped_count
= 0;
278 struct iscsi_cmd
*cmd
, *cmd_tmp
;
279 struct iscsi_ooo_cmdsn
*ooo_cmdsn
, *ooo_cmdsn_tmp
;
280 struct iscsi_session
*sess
= conn
->sess
;
282 mutex_lock(&sess
->cmdsn_mutex
);
283 list_for_each_entry_safe(ooo_cmdsn
, ooo_cmdsn_tmp
,
284 &sess
->sess_ooo_cmdsn_list
, ooo_list
) {
286 if (ooo_cmdsn
->cid
!= conn
->cid
)
290 pr_debug("Dropping unacknowledged CmdSN:"
291 " 0x%08x during connection recovery on CID: %hu\n",
292 ooo_cmdsn
->cmdsn
, conn
->cid
);
293 iscsit_remove_ooo_cmdsn(sess
, ooo_cmdsn
);
295 mutex_unlock(&sess
->cmdsn_mutex
);
297 spin_lock_bh(&conn
->cmd_lock
);
298 list_for_each_entry_safe(cmd
, cmd_tmp
, &conn
->conn_cmd_list
, i_conn_node
) {
299 if (!(cmd
->cmd_flags
& ICF_OOO_CMDSN
))
302 list_del(&cmd
->i_conn_node
);
304 spin_unlock_bh(&conn
->cmd_lock
);
305 iscsit_free_cmd(cmd
, true);
306 spin_lock_bh(&conn
->cmd_lock
);
308 spin_unlock_bh(&conn
->cmd_lock
);
310 pr_debug("Dropped %u total unacknowledged commands on CID:"
311 " %hu for ExpCmdSN: 0x%08x.\n", dropped_count
, conn
->cid
,
316 int iscsit_prepare_cmds_for_realligance(struct iscsi_conn
*conn
)
319 struct iscsi_cmd
*cmd
, *cmd_tmp
;
320 struct iscsi_conn_recovery
*cr
;
323 * Allocate an struct iscsi_conn_recovery for this connection.
324 * Each struct iscsi_cmd contains an struct iscsi_conn_recovery pointer
325 * (struct iscsi_cmd->cr) so we need to allocate this before preparing the
326 * connection's command list for connection recovery.
328 cr
= kzalloc(sizeof(struct iscsi_conn_recovery
), GFP_KERNEL
);
330 pr_err("Unable to allocate memory for"
331 " struct iscsi_conn_recovery.\n");
334 INIT_LIST_HEAD(&cr
->cr_list
);
335 INIT_LIST_HEAD(&cr
->conn_recovery_cmd_list
);
336 spin_lock_init(&cr
->conn_recovery_cmd_lock
);
338 * Only perform connection recovery on ISCSI_OP_SCSI_CMD or
339 * ISCSI_OP_NOOP_OUT opcodes. For all other opcodes call
340 * list_del(&cmd->i_conn_node); to release the command to the
341 * session pool and remove it from the connection's list.
343 * Also stop the DataOUT timer, which will be restarted after
344 * sending the TMR response.
346 spin_lock_bh(&conn
->cmd_lock
);
347 list_for_each_entry_safe(cmd
, cmd_tmp
, &conn
->conn_cmd_list
, i_conn_node
) {
349 if ((cmd
->iscsi_opcode
!= ISCSI_OP_SCSI_CMD
) &&
350 (cmd
->iscsi_opcode
!= ISCSI_OP_NOOP_OUT
)) {
351 pr_debug("Not performing realligence on"
352 " Opcode: 0x%02x, ITT: 0x%08x, CmdSN: 0x%08x,"
353 " CID: %hu\n", cmd
->iscsi_opcode
,
354 cmd
->init_task_tag
, cmd
->cmd_sn
, conn
->cid
);
356 list_del(&cmd
->i_conn_node
);
357 spin_unlock_bh(&conn
->cmd_lock
);
358 iscsit_free_cmd(cmd
, true);
359 spin_lock_bh(&conn
->cmd_lock
);
364 * Special case where commands greater than or equal to
365 * the session's ExpCmdSN are attached to the connection
366 * list but not to the out of order CmdSN list. The one
367 * obvious case is when a command with immediate data
368 * attached must only check the CmdSN against ExpCmdSN
369 * after the data is received. The special case below
370 * is when the connection fails before data is received,
371 * but also may apply to other PDUs, so it has been
374 if (!(cmd
->cmd_flags
& ICF_OOO_CMDSN
) && !cmd
->immediate_cmd
&&
375 iscsi_sna_gte(cmd
->cmd_sn
, conn
->sess
->exp_cmd_sn
)) {
376 list_del(&cmd
->i_conn_node
);
377 spin_unlock_bh(&conn
->cmd_lock
);
378 iscsit_free_cmd(cmd
, true);
379 spin_lock_bh(&conn
->cmd_lock
);
384 pr_debug("Preparing Opcode: 0x%02x, ITT: 0x%08x,"
385 " CmdSN: 0x%08x, StatSN: 0x%08x, CID: %hu for"
386 " realligence.\n", cmd
->iscsi_opcode
,
387 cmd
->init_task_tag
, cmd
->cmd_sn
, cmd
->stat_sn
,
390 cmd
->deferred_i_state
= cmd
->i_state
;
391 cmd
->i_state
= ISTATE_IN_CONNECTION_RECOVERY
;
393 if (cmd
->data_direction
== DMA_TO_DEVICE
)
394 iscsit_stop_dataout_timer(cmd
);
396 cmd
->sess
= conn
->sess
;
398 list_del(&cmd
->i_conn_node
);
399 spin_unlock_bh(&conn
->cmd_lock
);
401 iscsit_free_all_datain_reqs(cmd
);
403 transport_wait_for_tasks(&cmd
->se_cmd
);
405 * Add the struct iscsi_cmd to the connection recovery cmd list
407 spin_lock(&cr
->conn_recovery_cmd_lock
);
408 list_add_tail(&cmd
->i_conn_node
, &cr
->conn_recovery_cmd_list
);
409 spin_unlock(&cr
->conn_recovery_cmd_lock
);
411 spin_lock_bh(&conn
->cmd_lock
);
415 spin_unlock_bh(&conn
->cmd_lock
);
417 * Fill in the various values in the preallocated struct iscsi_conn_recovery.
420 cr
->cmd_count
= cmd_count
;
421 cr
->maxrecvdatasegmentlength
= conn
->conn_ops
->MaxRecvDataSegmentLength
;
422 cr
->maxxmitdatasegmentlength
= conn
->conn_ops
->MaxXmitDataSegmentLength
;
423 cr
->sess
= conn
->sess
;
425 iscsit_attach_inactive_connection_recovery_entry(conn
->sess
, cr
);
430 int iscsit_connection_recovery_transport_reset(struct iscsi_conn
*conn
)
432 atomic_set(&conn
->connection_recovery
, 1);
434 if (iscsit_close_connection(conn
) < 0)