iscsi_target_erl2.c 12 KB

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