Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / target / iscsi / iscsi_target_erl2.c
blob91a4d170bda47a5b172842619ecb9cab48d45376
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,
39 u32 exp_data_sn)
41 u32 data_sn = 0;
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;
55 } else {
56 cmd->read_data_done +=
57 (conn->sess->sess_ops->MaxBurstLength -
58 cmd->next_burst_len);
59 cmd->next_burst_len = 0;
61 data_sn++;
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;
71 cmd->data_sn = 0;
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) <=
76 cmd->write_data_done)
77 write_data_done += conn->sess->sess_ops->MaxBurstLength;
78 else
79 break;
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);
93 return 0;
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);
108 return 0;
111 struct iscsi_conn_recovery *iscsit_get_inactive_connection_recovery_entry(
112 struct iscsi_session *sess,
113 u16 cid)
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);
121 return cr;
124 spin_unlock(&sess->cr_i_lock);
126 return NULL;
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);
144 cmd->conn = NULL;
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);
149 else
150 cmd->se_cmd.transport_wait_for_tasks(
151 &cmd->se_cmd, 1, 1);
152 spin_lock(&cr->conn_recovery_cmd_lock);
154 spin_unlock(&cr->conn_recovery_cmd_lock);
155 spin_lock(&sess->cr_a_lock);
157 kfree(cr);
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);
171 cmd->conn = NULL;
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);
176 else
177 cmd->se_cmd.transport_wait_for_tasks(
178 &cmd->se_cmd, 1, 1);
179 spin_lock(&cr->conn_recovery_cmd_lock);
181 spin_unlock(&cr->conn_recovery_cmd_lock);
182 spin_lock(&sess->cr_i_lock);
184 kfree(cr);
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);
201 kfree(cr);
203 return 0;
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);
214 return 0;
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;
226 if (!cmd->cr) {
227 pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
228 " is NULL!\n", cmd->init_task_tag);
229 BUG();
231 cr = cmd->cr;
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,
239 u32 exp_statsn)
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)) {
252 continue;
255 dropped_count++;
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);
266 else
267 cmd->se_cmd.transport_wait_for_tasks(
268 &cmd->se_cmd, 1, 0);
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",
280 cr->cid, sess->sid);
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);
286 } else {
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)
304 continue;
306 dropped_count++;
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))
317 continue;
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);
325 else
326 cmd->se_cmd.transport_wait_for_tasks(
327 &cmd->se_cmd, 1, 1);
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,
334 sess->exp_cmd_sn);
335 return 0;
338 int iscsit_prepare_cmds_for_realligance(struct iscsi_conn *conn)
340 u32 cmd_count = 0;
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);
351 if (!cr) {
352 pr_err("Unable to allocate memory for"
353 " struct iscsi_conn_recovery.\n");
354 return -1;
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);
384 else
385 cmd->se_cmd.transport_wait_for_tasks(
386 &cmd->se_cmd, 1, 0);
387 spin_lock_bh(&conn->cmd_lock);
388 continue;
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
400 * made generic here.
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);
410 else
411 cmd->se_cmd.transport_wait_for_tasks(
412 &cmd->se_cmd, 1, 1);
413 spin_lock_bh(&conn->cmd_lock);
414 continue;
417 cmd_count++;
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,
422 conn->cid);
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,
440 0, 0);
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);
449 cmd->cr = cr;
450 cmd->conn = NULL;
452 spin_unlock_bh(&conn->cmd_lock);
454 * Fill in the various values in the preallocated struct iscsi_conn_recovery.
456 cr->cid = conn->cid;
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);
463 return 0;
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)
471 return -1;
473 return 0;