1 /*******************************************************************************
2 * This file contains error recovery level two functions used by
3 * the iSCSI Target driver.
5 * (c) Copyright 2007-2013 Datera, Inc.
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 ******************************************************************************/
20 #include <scsi/iscsi_proto.h>
21 #include <target/target_core_base.h>
22 #include <target/target_core_fabric.h>
24 #include <target/iscsi/iscsi_target_core.h>
25 #include "iscsi_target_datain_values.h"
26 #include "iscsi_target_util.h"
27 #include "iscsi_target_erl0.h"
28 #include "iscsi_target_erl1.h"
29 #include "iscsi_target_erl2.h"
30 #include "iscsi_target.h"
33 * FIXME: Does RData SNACK apply here as well?
35 void iscsit_create_conn_recovery_datain_values(
36 struct iscsi_cmd *cmd,
40 struct iscsi_conn *conn = cmd->conn;
42 cmd->next_burst_len = 0;
43 cmd->read_data_done = 0;
45 while (be32_to_cpu(exp_data_sn) > data_sn) {
46 if ((cmd->next_burst_len +
47 conn->conn_ops->MaxRecvDataSegmentLength) <
48 conn->sess->sess_ops->MaxBurstLength) {
49 cmd->read_data_done +=
50 conn->conn_ops->MaxRecvDataSegmentLength;
51 cmd->next_burst_len +=
52 conn->conn_ops->MaxRecvDataSegmentLength;
54 cmd->read_data_done +=
55 (conn->sess->sess_ops->MaxBurstLength -
57 cmd->next_burst_len = 0;
63 void iscsit_create_conn_recovery_dataout_values(
64 struct iscsi_cmd *cmd)
66 u32 write_data_done = 0;
67 struct iscsi_conn *conn = cmd->conn;
70 cmd->next_burst_len = 0;
72 while (cmd->write_data_done > write_data_done) {
73 if ((write_data_done + conn->sess->sess_ops->MaxBurstLength) <=
75 write_data_done += conn->sess->sess_ops->MaxBurstLength;
80 cmd->write_data_done = write_data_done;
83 static int iscsit_attach_active_connection_recovery_entry(
84 struct iscsi_session *sess,
85 struct iscsi_conn_recovery *cr)
87 spin_lock(&sess->cr_a_lock);
88 list_add_tail(&cr->cr_list, &sess->cr_active_list);
89 spin_unlock(&sess->cr_a_lock);
94 static int iscsit_attach_inactive_connection_recovery_entry(
95 struct iscsi_session *sess,
96 struct iscsi_conn_recovery *cr)
98 spin_lock(&sess->cr_i_lock);
99 list_add_tail(&cr->cr_list, &sess->cr_inactive_list);
101 sess->conn_recovery_count++;
102 pr_debug("Incremented connection recovery count to %u for"
103 " SID: %u\n", sess->conn_recovery_count, sess->sid);
104 spin_unlock(&sess->cr_i_lock);
109 struct iscsi_conn_recovery *iscsit_get_inactive_connection_recovery_entry(
110 struct iscsi_session *sess,
113 struct iscsi_conn_recovery *cr;
115 spin_lock(&sess->cr_i_lock);
116 list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
117 if (cr->cid == cid) {
118 spin_unlock(&sess->cr_i_lock);
122 spin_unlock(&sess->cr_i_lock);
127 void iscsit_free_connection_recovery_entires(struct iscsi_session *sess)
129 struct iscsi_cmd *cmd, *cmd_tmp;
130 struct iscsi_conn_recovery *cr, *cr_tmp;
132 spin_lock(&sess->cr_a_lock);
133 list_for_each_entry_safe(cr, cr_tmp, &sess->cr_active_list, cr_list) {
134 list_del(&cr->cr_list);
135 spin_unlock(&sess->cr_a_lock);
137 spin_lock(&cr->conn_recovery_cmd_lock);
138 list_for_each_entry_safe(cmd, cmd_tmp,
139 &cr->conn_recovery_cmd_list, i_conn_node) {
141 list_del_init(&cmd->i_conn_node);
143 spin_unlock(&cr->conn_recovery_cmd_lock);
144 iscsit_free_cmd(cmd, true);
145 spin_lock(&cr->conn_recovery_cmd_lock);
147 spin_unlock(&cr->conn_recovery_cmd_lock);
148 spin_lock(&sess->cr_a_lock);
152 spin_unlock(&sess->cr_a_lock);
154 spin_lock(&sess->cr_i_lock);
155 list_for_each_entry_safe(cr, cr_tmp, &sess->cr_inactive_list, cr_list) {
156 list_del(&cr->cr_list);
157 spin_unlock(&sess->cr_i_lock);
159 spin_lock(&cr->conn_recovery_cmd_lock);
160 list_for_each_entry_safe(cmd, cmd_tmp,
161 &cr->conn_recovery_cmd_list, i_conn_node) {
163 list_del_init(&cmd->i_conn_node);
165 spin_unlock(&cr->conn_recovery_cmd_lock);
166 iscsit_free_cmd(cmd, true);
167 spin_lock(&cr->conn_recovery_cmd_lock);
169 spin_unlock(&cr->conn_recovery_cmd_lock);
170 spin_lock(&sess->cr_i_lock);
174 spin_unlock(&sess->cr_i_lock);
177 int iscsit_remove_active_connection_recovery_entry(
178 struct iscsi_conn_recovery *cr,
179 struct iscsi_session *sess)
181 spin_lock(&sess->cr_a_lock);
182 list_del(&cr->cr_list);
184 sess->conn_recovery_count--;
185 pr_debug("Decremented connection recovery count to %u for"
186 " SID: %u\n", sess->conn_recovery_count, sess->sid);
187 spin_unlock(&sess->cr_a_lock);
194 static void iscsit_remove_inactive_connection_recovery_entry(
195 struct iscsi_conn_recovery *cr,
196 struct iscsi_session *sess)
198 spin_lock(&sess->cr_i_lock);
199 list_del(&cr->cr_list);
200 spin_unlock(&sess->cr_i_lock);
204 * Called with cr->conn_recovery_cmd_lock help.
206 int iscsit_remove_cmd_from_connection_recovery(
207 struct iscsi_cmd *cmd,
208 struct iscsi_session *sess)
210 struct iscsi_conn_recovery *cr;
213 pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
214 " is NULL!\n", cmd->init_task_tag);
219 list_del_init(&cmd->i_conn_node);
220 return --cr->cmd_count;
223 void iscsit_discard_cr_cmds_by_expstatsn(
224 struct iscsi_conn_recovery *cr,
227 u32 dropped_count = 0;
228 struct iscsi_cmd *cmd, *cmd_tmp;
229 struct iscsi_session *sess = cr->sess;
231 spin_lock(&cr->conn_recovery_cmd_lock);
232 list_for_each_entry_safe(cmd, cmd_tmp,
233 &cr->conn_recovery_cmd_list, i_conn_node) {
235 if (((cmd->deferred_i_state != ISTATE_SENT_STATUS) &&
236 (cmd->deferred_i_state != ISTATE_REMOVE)) ||
237 (cmd->stat_sn >= exp_statsn)) {
242 pr_debug("Dropping Acknowledged ITT: 0x%08x, StatSN:"
243 " 0x%08x, CID: %hu.\n", cmd->init_task_tag,
244 cmd->stat_sn, cr->cid);
246 iscsit_remove_cmd_from_connection_recovery(cmd, sess);
248 spin_unlock(&cr->conn_recovery_cmd_lock);
249 iscsit_free_cmd(cmd, true);
250 spin_lock(&cr->conn_recovery_cmd_lock);
252 spin_unlock(&cr->conn_recovery_cmd_lock);
254 pr_debug("Dropped %u total acknowledged commands on"
255 " CID: %hu less than old ExpStatSN: 0x%08x\n",
256 dropped_count, cr->cid, exp_statsn);
258 if (!cr->cmd_count) {
259 pr_debug("No commands to be reassigned for failed"
260 " connection CID: %hu on SID: %u\n",
262 iscsit_remove_inactive_connection_recovery_entry(cr, sess);
263 iscsit_attach_active_connection_recovery_entry(sess, cr);
264 pr_debug("iSCSI connection recovery successful for CID:"
265 " %hu on SID: %u\n", cr->cid, sess->sid);
266 iscsit_remove_active_connection_recovery_entry(cr, sess);
268 iscsit_remove_inactive_connection_recovery_entry(cr, sess);
269 iscsit_attach_active_connection_recovery_entry(sess, cr);
273 int iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(struct iscsi_conn *conn)
275 u32 dropped_count = 0;
276 struct iscsi_cmd *cmd, *cmd_tmp;
277 struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
278 struct iscsi_session *sess = conn->sess;
280 mutex_lock(&sess->cmdsn_mutex);
281 list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
282 &sess->sess_ooo_cmdsn_list, ooo_list) {
284 if (ooo_cmdsn->cid != conn->cid)
288 pr_debug("Dropping unacknowledged CmdSN:"
289 " 0x%08x during connection recovery on CID: %hu\n",
290 ooo_cmdsn->cmdsn, conn->cid);
291 iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
293 mutex_unlock(&sess->cmdsn_mutex);
295 spin_lock_bh(&conn->cmd_lock);
296 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
297 if (!(cmd->cmd_flags & ICF_OOO_CMDSN))
300 list_del_init(&cmd->i_conn_node);
302 spin_unlock_bh(&conn->cmd_lock);
303 iscsit_free_cmd(cmd, true);
304 spin_lock_bh(&conn->cmd_lock);
306 spin_unlock_bh(&conn->cmd_lock);
308 pr_debug("Dropped %u total unacknowledged commands on CID:"
309 " %hu for ExpCmdSN: 0x%08x.\n", dropped_count, conn->cid,
314 int iscsit_prepare_cmds_for_realligance(struct iscsi_conn *conn)
317 struct iscsi_cmd *cmd, *cmd_tmp;
318 struct iscsi_conn_recovery *cr;
321 * Allocate an struct iscsi_conn_recovery for this connection.
322 * Each struct iscsi_cmd contains an struct iscsi_conn_recovery pointer
323 * (struct iscsi_cmd->cr) so we need to allocate this before preparing the
324 * connection's command list for connection recovery.
326 cr = kzalloc(sizeof(struct iscsi_conn_recovery), GFP_KERNEL);
328 pr_err("Unable to allocate memory for"
329 " struct iscsi_conn_recovery.\n");
332 INIT_LIST_HEAD(&cr->cr_list);
333 INIT_LIST_HEAD(&cr->conn_recovery_cmd_list);
334 spin_lock_init(&cr->conn_recovery_cmd_lock);
336 * Only perform connection recovery on ISCSI_OP_SCSI_CMD or
337 * ISCSI_OP_NOOP_OUT opcodes. For all other opcodes call
338 * list_del_init(&cmd->i_conn_node); to release the command to the
339 * session pool and remove it from the connection's list.
341 * Also stop the DataOUT timer, which will be restarted after
342 * sending the TMR response.
344 spin_lock_bh(&conn->cmd_lock);
345 list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
347 if ((cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD) &&
348 (cmd->iscsi_opcode != ISCSI_OP_NOOP_OUT)) {
349 pr_debug("Not performing realligence on"
350 " Opcode: 0x%02x, ITT: 0x%08x, CmdSN: 0x%08x,"
351 " CID: %hu\n", cmd->iscsi_opcode,
352 cmd->init_task_tag, cmd->cmd_sn, conn->cid);
354 list_del_init(&cmd->i_conn_node);
355 spin_unlock_bh(&conn->cmd_lock);
356 iscsit_free_cmd(cmd, true);
357 spin_lock_bh(&conn->cmd_lock);
362 * Special case where commands greater than or equal to
363 * the session's ExpCmdSN are attached to the connection
364 * list but not to the out of order CmdSN list. The one
365 * obvious case is when a command with immediate data
366 * attached must only check the CmdSN against ExpCmdSN
367 * after the data is received. The special case below
368 * is when the connection fails before data is received,
369 * but also may apply to other PDUs, so it has been
372 if (!(cmd->cmd_flags & ICF_OOO_CMDSN) && !cmd->immediate_cmd &&
373 iscsi_sna_gte(cmd->cmd_sn, conn->sess->exp_cmd_sn)) {
374 list_del_init(&cmd->i_conn_node);
375 spin_unlock_bh(&conn->cmd_lock);
376 iscsit_free_cmd(cmd, true);
377 spin_lock_bh(&conn->cmd_lock);
382 pr_debug("Preparing Opcode: 0x%02x, ITT: 0x%08x,"
383 " CmdSN: 0x%08x, StatSN: 0x%08x, CID: %hu for"
384 " realligence.\n", cmd->iscsi_opcode,
385 cmd->init_task_tag, cmd->cmd_sn, cmd->stat_sn,
388 cmd->deferred_i_state = cmd->i_state;
389 cmd->i_state = ISTATE_IN_CONNECTION_RECOVERY;
391 if (cmd->data_direction == DMA_TO_DEVICE)
392 iscsit_stop_dataout_timer(cmd);
394 cmd->sess = conn->sess;
396 list_del_init(&cmd->i_conn_node);
397 spin_unlock_bh(&conn->cmd_lock);
399 iscsit_free_all_datain_reqs(cmd);
401 transport_wait_for_tasks(&cmd->se_cmd);
403 * Add the struct iscsi_cmd to the connection recovery cmd list
405 spin_lock(&cr->conn_recovery_cmd_lock);
406 list_add_tail(&cmd->i_conn_node, &cr->conn_recovery_cmd_list);
407 spin_unlock(&cr->conn_recovery_cmd_lock);
409 spin_lock_bh(&conn->cmd_lock);
413 spin_unlock_bh(&conn->cmd_lock);
415 * Fill in the various values in the preallocated struct iscsi_conn_recovery.
418 cr->cmd_count = cmd_count;
419 cr->maxrecvdatasegmentlength = conn->conn_ops->MaxRecvDataSegmentLength;
420 cr->maxxmitdatasegmentlength = conn->conn_ops->MaxXmitDataSegmentLength;
421 cr->sess = conn->sess;
423 iscsit_attach_inactive_connection_recovery_entry(conn->sess, cr);
428 int iscsit_connection_recovery_transport_reset(struct iscsi_conn *conn)
430 atomic_set(&conn->connection_recovery, 1);
432 if (iscsit_close_connection(conn) < 0)