iscsi_target_erl2.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*******************************************************************************
  2. * This file contains error recovery level two functions used by
  3. * the iSCSI Target driver.
  4. *
  5. * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
  6. *
  7. * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
  8. *
  9. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  10. *
  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.
  15. *
  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. ******************************************************************************/
  21. #include <scsi/iscsi_proto.h>
  22. #include <target/target_core_base.h>
  23. #include <target/target_core_fabric.h>
  24. #include "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"
  31. /*
  32. * FIXME: Does RData SNACK apply here as well?
  33. */
  34. void iscsit_create_conn_recovery_datain_values(
  35. struct iscsi_cmd *cmd,
  36. __be32 exp_data_sn)
  37. {
  38. u32 data_sn = 0;
  39. struct iscsi_conn *conn = cmd->conn;
  40. cmd->next_burst_len = 0;
  41. cmd->read_data_done = 0;
  42. while (be32_to_cpu(exp_data_sn) > data_sn) {
  43. if ((cmd->next_burst_len +
  44. conn->conn_ops->MaxRecvDataSegmentLength) <
  45. conn->sess->sess_ops->MaxBurstLength) {
  46. cmd->read_data_done +=
  47. conn->conn_ops->MaxRecvDataSegmentLength;
  48. cmd->next_burst_len +=
  49. conn->conn_ops->MaxRecvDataSegmentLength;
  50. } else {
  51. cmd->read_data_done +=
  52. (conn->sess->sess_ops->MaxBurstLength -
  53. cmd->next_burst_len);
  54. cmd->next_burst_len = 0;
  55. }
  56. data_sn++;
  57. }
  58. }
  59. void iscsit_create_conn_recovery_dataout_values(
  60. struct iscsi_cmd *cmd)
  61. {
  62. u32 write_data_done = 0;
  63. struct iscsi_conn *conn = cmd->conn;
  64. cmd->data_sn = 0;
  65. cmd->next_burst_len = 0;
  66. while (cmd->write_data_done > write_data_done) {
  67. if ((write_data_done + conn->sess->sess_ops->MaxBurstLength) <=
  68. cmd->write_data_done)
  69. write_data_done += conn->sess->sess_ops->MaxBurstLength;
  70. else
  71. break;
  72. }
  73. cmd->write_data_done = write_data_done;
  74. }
  75. static int iscsit_attach_active_connection_recovery_entry(
  76. struct iscsi_session *sess,
  77. struct iscsi_conn_recovery *cr)
  78. {
  79. spin_lock(&sess->cr_a_lock);
  80. list_add_tail(&cr->cr_list, &sess->cr_active_list);
  81. spin_unlock(&sess->cr_a_lock);
  82. return 0;
  83. }
  84. static int iscsit_attach_inactive_connection_recovery_entry(
  85. struct iscsi_session *sess,
  86. struct iscsi_conn_recovery *cr)
  87. {
  88. spin_lock(&sess->cr_i_lock);
  89. list_add_tail(&cr->cr_list, &sess->cr_inactive_list);
  90. sess->conn_recovery_count++;
  91. pr_debug("Incremented connection recovery count to %u for"
  92. " SID: %u\n", sess->conn_recovery_count, sess->sid);
  93. spin_unlock(&sess->cr_i_lock);
  94. return 0;
  95. }
  96. struct iscsi_conn_recovery *iscsit_get_inactive_connection_recovery_entry(
  97. struct iscsi_session *sess,
  98. u16 cid)
  99. {
  100. struct iscsi_conn_recovery *cr;
  101. spin_lock(&sess->cr_i_lock);
  102. list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
  103. if (cr->cid == cid) {
  104. spin_unlock(&sess->cr_i_lock);
  105. return cr;
  106. }
  107. }
  108. spin_unlock(&sess->cr_i_lock);
  109. return NULL;
  110. }
  111. void iscsit_free_connection_recovery_entires(struct iscsi_session *sess)
  112. {
  113. struct iscsi_cmd *cmd, *cmd_tmp;
  114. struct iscsi_conn_recovery *cr, *cr_tmp;
  115. spin_lock(&sess->cr_a_lock);
  116. list_for_each_entry_safe(cr, cr_tmp, &sess->cr_active_list, cr_list) {
  117. list_del(&cr->cr_list);
  118. spin_unlock(&sess->cr_a_lock);
  119. spin_lock(&cr->conn_recovery_cmd_lock);
  120. list_for_each_entry_safe(cmd, cmd_tmp,
  121. &cr->conn_recovery_cmd_list, i_conn_node) {
  122. list_del(&cmd->i_conn_node);
  123. cmd->conn = NULL;
  124. spin_unlock(&cr->conn_recovery_cmd_lock);
  125. iscsit_free_cmd(cmd);
  126. spin_lock(&cr->conn_recovery_cmd_lock);
  127. }
  128. spin_unlock(&cr->conn_recovery_cmd_lock);
  129. spin_lock(&sess->cr_a_lock);
  130. kfree(cr);
  131. }
  132. spin_unlock(&sess->cr_a_lock);
  133. spin_lock(&sess->cr_i_lock);
  134. list_for_each_entry_safe(cr, cr_tmp, &sess->cr_inactive_list, cr_list) {
  135. list_del(&cr->cr_list);
  136. spin_unlock(&sess->cr_i_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) {
  140. list_del(&cmd->i_conn_node);
  141. cmd->conn = NULL;
  142. spin_unlock(&cr->conn_recovery_cmd_lock);
  143. iscsit_free_cmd(cmd);
  144. spin_lock(&cr->conn_recovery_cmd_lock);
  145. }
  146. spin_unlock(&cr->conn_recovery_cmd_lock);
  147. spin_lock(&sess->cr_i_lock);
  148. kfree(cr);
  149. }
  150. spin_unlock(&sess->cr_i_lock);
  151. }
  152. int iscsit_remove_active_connection_recovery_entry(
  153. struct iscsi_conn_recovery *cr,
  154. struct iscsi_session *sess)
  155. {
  156. spin_lock(&sess->cr_a_lock);
  157. list_del(&cr->cr_list);
  158. sess->conn_recovery_count--;
  159. pr_debug("Decremented connection recovery count to %u for"
  160. " SID: %u\n", sess->conn_recovery_count, sess->sid);
  161. spin_unlock(&sess->cr_a_lock);
  162. kfree(cr);
  163. return 0;
  164. }
  165. static void iscsit_remove_inactive_connection_recovery_entry(
  166. struct iscsi_conn_recovery *cr,
  167. struct iscsi_session *sess)
  168. {
  169. spin_lock(&sess->cr_i_lock);
  170. list_del(&cr->cr_list);
  171. spin_unlock(&sess->cr_i_lock);
  172. }
  173. /*
  174. * Called with cr->conn_recovery_cmd_lock help.
  175. */
  176. int iscsit_remove_cmd_from_connection_recovery(
  177. struct iscsi_cmd *cmd,
  178. struct iscsi_session *sess)
  179. {
  180. struct iscsi_conn_recovery *cr;
  181. if (!cmd->cr) {
  182. pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
  183. " is NULL!\n", cmd->init_task_tag);
  184. BUG();
  185. }
  186. cr = cmd->cr;
  187. list_del(&cmd->i_conn_node);
  188. return --cr->cmd_count;
  189. }
  190. void iscsit_discard_cr_cmds_by_expstatsn(
  191. struct iscsi_conn_recovery *cr,
  192. u32 exp_statsn)
  193. {
  194. u32 dropped_count = 0;
  195. struct iscsi_cmd *cmd, *cmd_tmp;
  196. struct iscsi_session *sess = cr->sess;
  197. spin_lock(&cr->conn_recovery_cmd_lock);
  198. list_for_each_entry_safe(cmd, cmd_tmp,
  199. &cr->conn_recovery_cmd_list, i_conn_node) {
  200. if (((cmd->deferred_i_state != ISTATE_SENT_STATUS) &&
  201. (cmd->deferred_i_state != ISTATE_REMOVE)) ||
  202. (cmd->stat_sn >= exp_statsn)) {
  203. continue;
  204. }
  205. dropped_count++;
  206. pr_debug("Dropping Acknowledged ITT: 0x%08x, StatSN:"
  207. " 0x%08x, CID: %hu.\n", cmd->init_task_tag,
  208. cmd->stat_sn, cr->cid);
  209. iscsit_remove_cmd_from_connection_recovery(cmd, sess);
  210. spin_unlock(&cr->conn_recovery_cmd_lock);
  211. iscsit_free_cmd(cmd);
  212. spin_lock(&cr->conn_recovery_cmd_lock);
  213. }
  214. spin_unlock(&cr->conn_recovery_cmd_lock);
  215. pr_debug("Dropped %u total acknowledged commands on"
  216. " CID: %hu less than old ExpStatSN: 0x%08x\n",
  217. dropped_count, cr->cid, exp_statsn);
  218. if (!cr->cmd_count) {
  219. pr_debug("No commands to be reassigned for failed"
  220. " connection CID: %hu on SID: %u\n",
  221. cr->cid, sess->sid);
  222. iscsit_remove_inactive_connection_recovery_entry(cr, sess);
  223. iscsit_attach_active_connection_recovery_entry(sess, cr);
  224. pr_debug("iSCSI connection recovery successful for CID:"
  225. " %hu on SID: %u\n", cr->cid, sess->sid);
  226. iscsit_remove_active_connection_recovery_entry(cr, sess);
  227. } else {
  228. iscsit_remove_inactive_connection_recovery_entry(cr, sess);
  229. iscsit_attach_active_connection_recovery_entry(sess, cr);
  230. }
  231. }
  232. int iscsit_discard_unacknowledged_ooo_cmdsns_for_conn(struct iscsi_conn *conn)
  233. {
  234. u32 dropped_count = 0;
  235. struct iscsi_cmd *cmd, *cmd_tmp;
  236. struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
  237. struct iscsi_session *sess = conn->sess;
  238. mutex_lock(&sess->cmdsn_mutex);
  239. list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
  240. &sess->sess_ooo_cmdsn_list, ooo_list) {
  241. if (ooo_cmdsn->cid != conn->cid)
  242. continue;
  243. dropped_count++;
  244. pr_debug("Dropping unacknowledged CmdSN:"
  245. " 0x%08x during connection recovery on CID: %hu\n",
  246. ooo_cmdsn->cmdsn, conn->cid);
  247. iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
  248. }
  249. mutex_unlock(&sess->cmdsn_mutex);
  250. spin_lock_bh(&conn->cmd_lock);
  251. list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
  252. if (!(cmd->cmd_flags & ICF_OOO_CMDSN))
  253. continue;
  254. list_del(&cmd->i_conn_node);
  255. spin_unlock_bh(&conn->cmd_lock);
  256. iscsit_free_cmd(cmd);
  257. spin_lock_bh(&conn->cmd_lock);
  258. }
  259. spin_unlock_bh(&conn->cmd_lock);
  260. pr_debug("Dropped %u total unacknowledged commands on CID:"
  261. " %hu for ExpCmdSN: 0x%08x.\n", dropped_count, conn->cid,
  262. sess->exp_cmd_sn);
  263. return 0;
  264. }
  265. int iscsit_prepare_cmds_for_realligance(struct iscsi_conn *conn)
  266. {
  267. u32 cmd_count = 0;
  268. struct iscsi_cmd *cmd, *cmd_tmp;
  269. struct iscsi_conn_recovery *cr;
  270. /*
  271. * Allocate an struct iscsi_conn_recovery for this connection.
  272. * Each struct iscsi_cmd contains an struct iscsi_conn_recovery pointer
  273. * (struct iscsi_cmd->cr) so we need to allocate this before preparing the
  274. * connection's command list for connection recovery.
  275. */
  276. cr = kzalloc(sizeof(struct iscsi_conn_recovery), GFP_KERNEL);
  277. if (!cr) {
  278. pr_err("Unable to allocate memory for"
  279. " struct iscsi_conn_recovery.\n");
  280. return -1;
  281. }
  282. INIT_LIST_HEAD(&cr->cr_list);
  283. INIT_LIST_HEAD(&cr->conn_recovery_cmd_list);
  284. spin_lock_init(&cr->conn_recovery_cmd_lock);
  285. /*
  286. * Only perform connection recovery on ISCSI_OP_SCSI_CMD or
  287. * ISCSI_OP_NOOP_OUT opcodes. For all other opcodes call
  288. * list_del(&cmd->i_conn_node); to release the command to the
  289. * session pool and remove it from the connection's list.
  290. *
  291. * Also stop the DataOUT timer, which will be restarted after
  292. * sending the TMR response.
  293. */
  294. spin_lock_bh(&conn->cmd_lock);
  295. list_for_each_entry_safe(cmd, cmd_tmp, &conn->conn_cmd_list, i_conn_node) {
  296. if ((cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD) &&
  297. (cmd->iscsi_opcode != ISCSI_OP_NOOP_OUT)) {
  298. pr_debug("Not performing realligence on"
  299. " Opcode: 0x%02x, ITT: 0x%08x, CmdSN: 0x%08x,"
  300. " CID: %hu\n", cmd->iscsi_opcode,
  301. cmd->init_task_tag, cmd->cmd_sn, conn->cid);
  302. list_del(&cmd->i_conn_node);
  303. spin_unlock_bh(&conn->cmd_lock);
  304. iscsit_free_cmd(cmd);
  305. spin_lock_bh(&conn->cmd_lock);
  306. continue;
  307. }
  308. /*
  309. * Special case where commands greater than or equal to
  310. * the session's ExpCmdSN are attached to the connection
  311. * list but not to the out of order CmdSN list. The one
  312. * obvious case is when a command with immediate data
  313. * attached must only check the CmdSN against ExpCmdSN
  314. * after the data is received. The special case below
  315. * is when the connection fails before data is received,
  316. * but also may apply to other PDUs, so it has been
  317. * made generic here.
  318. */
  319. if (!(cmd->cmd_flags & ICF_OOO_CMDSN) && !cmd->immediate_cmd &&
  320. iscsi_sna_gte(cmd->cmd_sn, conn->sess->exp_cmd_sn)) {
  321. list_del(&cmd->i_conn_node);
  322. spin_unlock_bh(&conn->cmd_lock);
  323. iscsit_free_cmd(cmd);
  324. spin_lock_bh(&conn->cmd_lock);
  325. continue;
  326. }
  327. cmd_count++;
  328. pr_debug("Preparing Opcode: 0x%02x, ITT: 0x%08x,"
  329. " CmdSN: 0x%08x, StatSN: 0x%08x, CID: %hu for"
  330. " realligence.\n", cmd->iscsi_opcode,
  331. cmd->init_task_tag, cmd->cmd_sn, cmd->stat_sn,
  332. conn->cid);
  333. cmd->deferred_i_state = cmd->i_state;
  334. cmd->i_state = ISTATE_IN_CONNECTION_RECOVERY;
  335. if (cmd->data_direction == DMA_TO_DEVICE)
  336. iscsit_stop_dataout_timer(cmd);
  337. cmd->sess = conn->sess;
  338. list_del(&cmd->i_conn_node);
  339. spin_unlock_bh(&conn->cmd_lock);
  340. iscsit_free_all_datain_reqs(cmd);
  341. transport_wait_for_tasks(&cmd->se_cmd);
  342. /*
  343. * Add the struct iscsi_cmd to the connection recovery cmd list
  344. */
  345. spin_lock(&cr->conn_recovery_cmd_lock);
  346. list_add_tail(&cmd->i_conn_node, &cr->conn_recovery_cmd_list);
  347. spin_unlock(&cr->conn_recovery_cmd_lock);
  348. spin_lock_bh(&conn->cmd_lock);
  349. cmd->cr = cr;
  350. cmd->conn = NULL;
  351. }
  352. spin_unlock_bh(&conn->cmd_lock);
  353. /*
  354. * Fill in the various values in the preallocated struct iscsi_conn_recovery.
  355. */
  356. cr->cid = conn->cid;
  357. cr->cmd_count = cmd_count;
  358. cr->maxrecvdatasegmentlength = conn->conn_ops->MaxRecvDataSegmentLength;
  359. cr->maxxmitdatasegmentlength = conn->conn_ops->MaxXmitDataSegmentLength;
  360. cr->sess = conn->sess;
  361. iscsit_attach_inactive_connection_recovery_entry(conn->sess, cr);
  362. return 0;
  363. }
  364. int iscsit_connection_recovery_transport_reset(struct iscsi_conn *conn)
  365. {
  366. atomic_set(&conn->connection_recovery, 1);
  367. if (iscsit_close_connection(conn) < 0)
  368. return -1;
  369. return 0;
  370. }