iscsi_target_tmr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * This file contains the iSCSI Target specific Task Management functions.
  4. *
  5. * (c) Copyright 2007-2013 Datera, Inc.
  6. *
  7. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  8. *
  9. ******************************************************************************/
  10. #include <asm/unaligned.h>
  11. #include <scsi/scsi_proto.h>
  12. #include <scsi/iscsi_proto.h>
  13. #include <target/target_core_base.h>
  14. #include <target/target_core_fabric.h>
  15. #include <target/iscsi/iscsi_transport.h>
  16. #include <target/iscsi/iscsi_target_core.h>
  17. #include "iscsi_target_seq_pdu_list.h"
  18. #include "iscsi_target_datain_values.h"
  19. #include "iscsi_target_device.h"
  20. #include "iscsi_target_erl0.h"
  21. #include "iscsi_target_erl1.h"
  22. #include "iscsi_target_erl2.h"
  23. #include "iscsi_target_tmr.h"
  24. #include "iscsi_target_tpg.h"
  25. #include "iscsi_target_util.h"
  26. #include "iscsi_target.h"
  27. u8 iscsit_tmr_abort_task(
  28. struct iscsi_cmd *cmd,
  29. unsigned char *buf)
  30. {
  31. struct iscsi_cmd *ref_cmd;
  32. struct iscsi_conn *conn = cmd->conn;
  33. struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
  34. struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
  35. struct iscsi_tm *hdr = (struct iscsi_tm *) buf;
  36. ref_cmd = iscsit_find_cmd_from_itt(conn, hdr->rtt);
  37. if (!ref_cmd) {
  38. pr_err("Unable to locate RefTaskTag: 0x%08x on CID:"
  39. " %hu.\n", hdr->rtt, conn->cid);
  40. return (iscsi_sna_gte(be32_to_cpu(hdr->refcmdsn), conn->sess->exp_cmd_sn) &&
  41. iscsi_sna_lte(be32_to_cpu(hdr->refcmdsn), (u32) atomic_read(&conn->sess->max_cmd_sn))) ?
  42. ISCSI_TMF_RSP_COMPLETE : ISCSI_TMF_RSP_NO_TASK;
  43. }
  44. if (ref_cmd->cmd_sn != be32_to_cpu(hdr->refcmdsn)) {
  45. pr_err("RefCmdSN 0x%08x does not equal"
  46. " task's CmdSN 0x%08x. Rejecting ABORT_TASK.\n",
  47. hdr->refcmdsn, ref_cmd->cmd_sn);
  48. return ISCSI_TMF_RSP_REJECTED;
  49. }
  50. se_tmr->ref_task_tag = (__force u32)hdr->rtt;
  51. tmr_req->ref_cmd = ref_cmd;
  52. tmr_req->exp_data_sn = be32_to_cpu(hdr->exp_datasn);
  53. return ISCSI_TMF_RSP_COMPLETE;
  54. }
  55. /*
  56. * Called from iscsit_handle_task_mgt_cmd().
  57. */
  58. int iscsit_tmr_task_warm_reset(
  59. struct iscsi_conn *conn,
  60. struct iscsi_tmr_req *tmr_req,
  61. unsigned char *buf)
  62. {
  63. struct iscsi_session *sess = conn->sess;
  64. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  65. if (!na->tmr_warm_reset) {
  66. pr_err("TMR Opcode TARGET_WARM_RESET authorization"
  67. " failed for Initiator Node: %s\n",
  68. sess->se_sess->se_node_acl->initiatorname);
  69. return -1;
  70. }
  71. /*
  72. * Do the real work in transport_generic_do_tmr().
  73. */
  74. return 0;
  75. }
  76. int iscsit_tmr_task_cold_reset(
  77. struct iscsi_conn *conn,
  78. struct iscsi_tmr_req *tmr_req,
  79. unsigned char *buf)
  80. {
  81. struct iscsi_session *sess = conn->sess;
  82. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  83. if (!na->tmr_cold_reset) {
  84. pr_err("TMR Opcode TARGET_COLD_RESET authorization"
  85. " failed for Initiator Node: %s\n",
  86. sess->se_sess->se_node_acl->initiatorname);
  87. return -1;
  88. }
  89. /*
  90. * Do the real work in transport_generic_do_tmr().
  91. */
  92. return 0;
  93. }
  94. u8 iscsit_tmr_task_reassign(
  95. struct iscsi_cmd *cmd,
  96. unsigned char *buf)
  97. {
  98. struct iscsi_cmd *ref_cmd = NULL;
  99. struct iscsi_conn *conn = cmd->conn;
  100. struct iscsi_conn_recovery *cr = NULL;
  101. struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
  102. struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
  103. struct iscsi_tm *hdr = (struct iscsi_tm *) buf;
  104. u64 ret, ref_lun;
  105. pr_debug("Got TASK_REASSIGN TMR ITT: 0x%08x,"
  106. " RefTaskTag: 0x%08x, ExpDataSN: 0x%08x, CID: %hu\n",
  107. hdr->itt, hdr->rtt, hdr->exp_datasn, conn->cid);
  108. if (conn->sess->sess_ops->ErrorRecoveryLevel != 2) {
  109. pr_err("TMR TASK_REASSIGN not supported in ERL<2,"
  110. " ignoring request.\n");
  111. return ISCSI_TMF_RSP_NOT_SUPPORTED;
  112. }
  113. ret = iscsit_find_cmd_for_recovery(conn->sess, &ref_cmd, &cr, hdr->rtt);
  114. if (ret == -2) {
  115. pr_err("Command ITT: 0x%08x is still alligent to CID:"
  116. " %hu\n", ref_cmd->init_task_tag, cr->cid);
  117. return ISCSI_TMF_RSP_TASK_ALLEGIANT;
  118. } else if (ret == -1) {
  119. pr_err("Unable to locate RefTaskTag: 0x%08x in"
  120. " connection recovery command list.\n", hdr->rtt);
  121. return ISCSI_TMF_RSP_NO_TASK;
  122. }
  123. /*
  124. * Temporary check to prevent connection recovery for
  125. * connections with a differing Max*DataSegmentLength.
  126. */
  127. if (cr->maxrecvdatasegmentlength !=
  128. conn->conn_ops->MaxRecvDataSegmentLength) {
  129. pr_err("Unable to perform connection recovery for"
  130. " differing MaxRecvDataSegmentLength, rejecting"
  131. " TMR TASK_REASSIGN.\n");
  132. return ISCSI_TMF_RSP_REJECTED;
  133. }
  134. if (cr->maxxmitdatasegmentlength !=
  135. conn->conn_ops->MaxXmitDataSegmentLength) {
  136. pr_err("Unable to perform connection recovery for"
  137. " differing MaxXmitDataSegmentLength, rejecting"
  138. " TMR TASK_REASSIGN.\n");
  139. return ISCSI_TMF_RSP_REJECTED;
  140. }
  141. ref_lun = scsilun_to_int(&hdr->lun);
  142. if (ref_lun != ref_cmd->se_cmd.orig_fe_lun) {
  143. pr_err("Unable to perform connection recovery for"
  144. " differing ref_lun: %llu ref_cmd orig_fe_lun: %llu\n",
  145. ref_lun, ref_cmd->se_cmd.orig_fe_lun);
  146. return ISCSI_TMF_RSP_REJECTED;
  147. }
  148. se_tmr->ref_task_tag = (__force u32)hdr->rtt;
  149. tmr_req->ref_cmd = ref_cmd;
  150. tmr_req->exp_data_sn = be32_to_cpu(hdr->exp_datasn);
  151. tmr_req->conn_recovery = cr;
  152. tmr_req->task_reassign = 1;
  153. /*
  154. * Command can now be reassigned to a new connection.
  155. * The task management response must be sent before the
  156. * reassignment actually happens. See iscsi_tmr_post_handler().
  157. */
  158. return ISCSI_TMF_RSP_COMPLETE;
  159. }
  160. static void iscsit_task_reassign_remove_cmd(
  161. struct iscsi_cmd *cmd,
  162. struct iscsi_conn_recovery *cr,
  163. struct iscsi_session *sess)
  164. {
  165. int ret;
  166. spin_lock(&cr->conn_recovery_cmd_lock);
  167. ret = iscsit_remove_cmd_from_connection_recovery(cmd, sess);
  168. spin_unlock(&cr->conn_recovery_cmd_lock);
  169. if (!ret) {
  170. pr_debug("iSCSI connection recovery successful for CID:"
  171. " %hu on SID: %u\n", cr->cid, sess->sid);
  172. iscsit_remove_active_connection_recovery_entry(cr, sess);
  173. }
  174. }
  175. static int iscsit_task_reassign_complete_nop_out(
  176. struct iscsi_tmr_req *tmr_req,
  177. struct iscsi_conn *conn)
  178. {
  179. struct iscsi_cmd *cmd = tmr_req->ref_cmd;
  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. return -1;
  185. }
  186. cr = cmd->cr;
  187. /*
  188. * Reset the StatSN so a new one for this commands new connection
  189. * will be assigned.
  190. * Reset the ExpStatSN as well so we may receive Status SNACKs.
  191. */
  192. cmd->stat_sn = cmd->exp_stat_sn = 0;
  193. iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
  194. spin_lock_bh(&conn->cmd_lock);
  195. list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
  196. spin_unlock_bh(&conn->cmd_lock);
  197. cmd->i_state = ISTATE_SEND_NOPIN;
  198. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  199. return 0;
  200. }
  201. static int iscsit_task_reassign_complete_write(
  202. struct iscsi_cmd *cmd,
  203. struct iscsi_tmr_req *tmr_req)
  204. {
  205. int no_build_r2ts = 0;
  206. u32 length = 0, offset = 0;
  207. struct iscsi_conn *conn = cmd->conn;
  208. struct se_cmd *se_cmd = &cmd->se_cmd;
  209. /*
  210. * The Initiator must not send a R2T SNACK with a Begrun less than
  211. * the TMR TASK_REASSIGN's ExpDataSN.
  212. */
  213. if (!tmr_req->exp_data_sn) {
  214. cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
  215. cmd->acked_data_sn = 0;
  216. } else {
  217. cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
  218. cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
  219. }
  220. /*
  221. * The TMR TASK_REASSIGN's ExpDataSN contains the next R2TSN the
  222. * Initiator is expecting. The Target controls all WRITE operations
  223. * so if we have received all DataOUT we can safety ignore Initiator.
  224. */
  225. if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
  226. if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
  227. pr_debug("WRITE ITT: 0x%08x: t_state: %d"
  228. " never sent to transport\n",
  229. cmd->init_task_tag, cmd->se_cmd.t_state);
  230. target_execute_cmd(se_cmd);
  231. return 0;
  232. }
  233. cmd->i_state = ISTATE_SEND_STATUS;
  234. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  235. return 0;
  236. }
  237. /*
  238. * Special case to deal with DataSequenceInOrder=No and Non-Immeidate
  239. * Unsolicited DataOut.
  240. */
  241. if (cmd->unsolicited_data) {
  242. cmd->unsolicited_data = 0;
  243. offset = cmd->next_burst_len = cmd->write_data_done;
  244. if ((conn->sess->sess_ops->FirstBurstLength - offset) >=
  245. cmd->se_cmd.data_length) {
  246. no_build_r2ts = 1;
  247. length = (cmd->se_cmd.data_length - offset);
  248. } else
  249. length = (conn->sess->sess_ops->FirstBurstLength - offset);
  250. spin_lock_bh(&cmd->r2t_lock);
  251. if (iscsit_add_r2t_to_list(cmd, offset, length, 0, 0) < 0) {
  252. spin_unlock_bh(&cmd->r2t_lock);
  253. return -1;
  254. }
  255. cmd->outstanding_r2ts++;
  256. spin_unlock_bh(&cmd->r2t_lock);
  257. if (no_build_r2ts)
  258. return 0;
  259. }
  260. /*
  261. * iscsit_build_r2ts_for_cmd() can handle the rest from here.
  262. */
  263. return conn->conn_transport->iscsit_get_dataout(conn, cmd, true);
  264. }
  265. static int iscsit_task_reassign_complete_read(
  266. struct iscsi_cmd *cmd,
  267. struct iscsi_tmr_req *tmr_req)
  268. {
  269. struct iscsi_conn *conn = cmd->conn;
  270. struct iscsi_datain_req *dr;
  271. struct se_cmd *se_cmd = &cmd->se_cmd;
  272. /*
  273. * The Initiator must not send a Data SNACK with a BegRun less than
  274. * the TMR TASK_REASSIGN's ExpDataSN.
  275. */
  276. if (!tmr_req->exp_data_sn) {
  277. cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
  278. cmd->acked_data_sn = 0;
  279. } else {
  280. cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
  281. cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
  282. }
  283. if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
  284. pr_debug("READ ITT: 0x%08x: t_state: %d never sent to"
  285. " transport\n", cmd->init_task_tag,
  286. cmd->se_cmd.t_state);
  287. transport_handle_cdb_direct(se_cmd);
  288. return 0;
  289. }
  290. if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
  291. pr_err("READ ITT: 0x%08x: t_state: %d, never returned"
  292. " from transport\n", cmd->init_task_tag,
  293. cmd->se_cmd.t_state);
  294. return -1;
  295. }
  296. dr = iscsit_allocate_datain_req();
  297. if (!dr)
  298. return -1;
  299. /*
  300. * The TMR TASK_REASSIGN's ExpDataSN contains the next DataSN the
  301. * Initiator is expecting.
  302. */
  303. dr->data_sn = dr->begrun = tmr_req->exp_data_sn;
  304. dr->runlength = 0;
  305. dr->generate_recovery_values = 1;
  306. dr->recovery = DATAIN_CONNECTION_RECOVERY;
  307. iscsit_attach_datain_req(cmd, dr);
  308. cmd->i_state = ISTATE_SEND_DATAIN;
  309. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  310. return 0;
  311. }
  312. static int iscsit_task_reassign_complete_none(
  313. struct iscsi_cmd *cmd,
  314. struct iscsi_tmr_req *tmr_req)
  315. {
  316. struct iscsi_conn *conn = cmd->conn;
  317. cmd->i_state = ISTATE_SEND_STATUS;
  318. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  319. return 0;
  320. }
  321. static int iscsit_task_reassign_complete_scsi_cmnd(
  322. struct iscsi_tmr_req *tmr_req,
  323. struct iscsi_conn *conn)
  324. {
  325. struct iscsi_cmd *cmd = tmr_req->ref_cmd;
  326. struct iscsi_conn_recovery *cr;
  327. if (!cmd->cr) {
  328. pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
  329. " is NULL!\n", cmd->init_task_tag);
  330. return -1;
  331. }
  332. cr = cmd->cr;
  333. /*
  334. * Reset the StatSN so a new one for this commands new connection
  335. * will be assigned.
  336. * Reset the ExpStatSN as well so we may receive Status SNACKs.
  337. */
  338. cmd->stat_sn = cmd->exp_stat_sn = 0;
  339. iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
  340. spin_lock_bh(&conn->cmd_lock);
  341. list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
  342. spin_unlock_bh(&conn->cmd_lock);
  343. if (cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  344. cmd->i_state = ISTATE_SEND_STATUS;
  345. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  346. return 0;
  347. }
  348. switch (cmd->data_direction) {
  349. case DMA_TO_DEVICE:
  350. return iscsit_task_reassign_complete_write(cmd, tmr_req);
  351. case DMA_FROM_DEVICE:
  352. return iscsit_task_reassign_complete_read(cmd, tmr_req);
  353. case DMA_NONE:
  354. return iscsit_task_reassign_complete_none(cmd, tmr_req);
  355. default:
  356. pr_err("Unknown cmd->data_direction: 0x%02x\n",
  357. cmd->data_direction);
  358. return -1;
  359. }
  360. return 0;
  361. }
  362. static int iscsit_task_reassign_complete(
  363. struct iscsi_tmr_req *tmr_req,
  364. struct iscsi_conn *conn)
  365. {
  366. struct iscsi_cmd *cmd;
  367. int ret = 0;
  368. if (!tmr_req->ref_cmd) {
  369. pr_err("TMR Request is missing a RefCmd struct iscsi_cmd.\n");
  370. return -1;
  371. }
  372. cmd = tmr_req->ref_cmd;
  373. cmd->conn = conn;
  374. switch (cmd->iscsi_opcode) {
  375. case ISCSI_OP_NOOP_OUT:
  376. ret = iscsit_task_reassign_complete_nop_out(tmr_req, conn);
  377. break;
  378. case ISCSI_OP_SCSI_CMD:
  379. ret = iscsit_task_reassign_complete_scsi_cmnd(tmr_req, conn);
  380. break;
  381. default:
  382. pr_err("Illegal iSCSI Opcode 0x%02x during"
  383. " command reallegiance\n", cmd->iscsi_opcode);
  384. return -1;
  385. }
  386. if (ret != 0)
  387. return ret;
  388. pr_debug("Completed connection reallegiance for Opcode: 0x%02x,"
  389. " ITT: 0x%08x to CID: %hu.\n", cmd->iscsi_opcode,
  390. cmd->init_task_tag, conn->cid);
  391. return 0;
  392. }
  393. /*
  394. * Handles special after-the-fact actions related to TMRs.
  395. * Right now the only one that its really needed for is
  396. * connection recovery releated TASK_REASSIGN.
  397. */
  398. int iscsit_tmr_post_handler(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  399. {
  400. struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
  401. struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
  402. if (tmr_req->task_reassign &&
  403. (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
  404. return iscsit_task_reassign_complete(tmr_req, conn);
  405. return 0;
  406. }
  407. EXPORT_SYMBOL(iscsit_tmr_post_handler);
  408. /*
  409. * Nothing to do here, but leave it for good measure. :-)
  410. */
  411. static int iscsit_task_reassign_prepare_read(
  412. struct iscsi_tmr_req *tmr_req,
  413. struct iscsi_conn *conn)
  414. {
  415. return 0;
  416. }
  417. static void iscsit_task_reassign_prepare_unsolicited_dataout(
  418. struct iscsi_cmd *cmd,
  419. struct iscsi_conn *conn)
  420. {
  421. int i, j;
  422. struct iscsi_pdu *pdu = NULL;
  423. struct iscsi_seq *seq = NULL;
  424. if (conn->sess->sess_ops->DataSequenceInOrder) {
  425. cmd->data_sn = 0;
  426. if (cmd->immediate_data)
  427. cmd->r2t_offset += (cmd->first_burst_len -
  428. cmd->seq_start_offset);
  429. if (conn->sess->sess_ops->DataPDUInOrder) {
  430. cmd->write_data_done -= (cmd->immediate_data) ?
  431. (cmd->first_burst_len -
  432. cmd->seq_start_offset) :
  433. cmd->first_burst_len;
  434. cmd->first_burst_len = 0;
  435. return;
  436. }
  437. for (i = 0; i < cmd->pdu_count; i++) {
  438. pdu = &cmd->pdu_list[i];
  439. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  440. continue;
  441. if ((pdu->offset >= cmd->seq_start_offset) &&
  442. ((pdu->offset + pdu->length) <=
  443. cmd->seq_end_offset)) {
  444. cmd->first_burst_len -= pdu->length;
  445. cmd->write_data_done -= pdu->length;
  446. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  447. }
  448. }
  449. } else {
  450. for (i = 0; i < cmd->seq_count; i++) {
  451. seq = &cmd->seq_list[i];
  452. if (seq->type != SEQTYPE_UNSOLICITED)
  453. continue;
  454. cmd->write_data_done -=
  455. (seq->offset - seq->orig_offset);
  456. cmd->first_burst_len = 0;
  457. seq->data_sn = 0;
  458. seq->offset = seq->orig_offset;
  459. seq->next_burst_len = 0;
  460. seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
  461. if (conn->sess->sess_ops->DataPDUInOrder)
  462. continue;
  463. for (j = 0; j < seq->pdu_count; j++) {
  464. pdu = &cmd->pdu_list[j+seq->pdu_start];
  465. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  466. continue;
  467. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  468. }
  469. }
  470. }
  471. }
  472. static int iscsit_task_reassign_prepare_write(
  473. struct iscsi_tmr_req *tmr_req,
  474. struct iscsi_conn *conn)
  475. {
  476. struct iscsi_cmd *cmd = tmr_req->ref_cmd;
  477. struct iscsi_pdu *pdu = NULL;
  478. struct iscsi_r2t *r2t = NULL, *r2t_tmp;
  479. int first_incomplete_r2t = 1, i = 0;
  480. /*
  481. * The command was in the process of receiving Unsolicited DataOUT when
  482. * the connection failed.
  483. */
  484. if (cmd->unsolicited_data)
  485. iscsit_task_reassign_prepare_unsolicited_dataout(cmd, conn);
  486. /*
  487. * The Initiator is requesting R2Ts starting from zero, skip
  488. * checking acknowledged R2Ts and start checking struct iscsi_r2ts
  489. * greater than zero.
  490. */
  491. if (!tmr_req->exp_data_sn)
  492. goto drop_unacknowledged_r2ts;
  493. /*
  494. * We now check that the PDUs in DataOUT sequences below
  495. * the TMR TASK_REASSIGN ExpDataSN (R2TSN the Initiator is
  496. * expecting next) have all the DataOUT they require to complete
  497. * the DataOUT sequence. First scan from R2TSN 0 to TMR
  498. * TASK_REASSIGN ExpDataSN-1.
  499. *
  500. * If we have not received all DataOUT in question, we must
  501. * make sure to make the appropriate changes to values in
  502. * struct iscsi_cmd (and elsewhere depending on session parameters)
  503. * so iscsit_build_r2ts_for_cmd() in iscsit_task_reassign_complete_write()
  504. * will resend a new R2T for the DataOUT sequences in question.
  505. */
  506. spin_lock_bh(&cmd->r2t_lock);
  507. if (list_empty(&cmd->cmd_r2t_list)) {
  508. spin_unlock_bh(&cmd->r2t_lock);
  509. return -1;
  510. }
  511. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  512. if (r2t->r2t_sn >= tmr_req->exp_data_sn)
  513. continue;
  514. /*
  515. * Safely ignore Recovery R2Ts and R2Ts that have completed
  516. * DataOUT sequences.
  517. */
  518. if (r2t->seq_complete)
  519. continue;
  520. if (r2t->recovery_r2t)
  521. continue;
  522. /*
  523. * DataSequenceInOrder=Yes:
  524. *
  525. * Taking into account the iSCSI implementation requirement of
  526. * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
  527. * DataSequenceInOrder=Yes, we must take into consideration
  528. * the following:
  529. *
  530. * DataSequenceInOrder=No:
  531. *
  532. * Taking into account that the Initiator controls the (possibly
  533. * random) PDU Order in (possibly random) Sequence Order of
  534. * DataOUT the target requests with R2Ts, we must take into
  535. * consideration the following:
  536. *
  537. * DataPDUInOrder=Yes for DataSequenceInOrder=[Yes,No]:
  538. *
  539. * While processing non-complete R2T DataOUT sequence requests
  540. * the Target will re-request only the total sequence length
  541. * minus current received offset. This is because we must
  542. * assume the initiator will continue sending DataOUT from the
  543. * last PDU before the connection failed.
  544. *
  545. * DataPDUInOrder=No for DataSequenceInOrder=[Yes,No]:
  546. *
  547. * While processing non-complete R2T DataOUT sequence requests
  548. * the Target will re-request the entire DataOUT sequence if
  549. * any single PDU is missing from the sequence. This is because
  550. * we have no logical method to determine the next PDU offset,
  551. * and we must assume the Initiator will be sending any random
  552. * PDU offset in the current sequence after TASK_REASSIGN
  553. * has completed.
  554. */
  555. if (conn->sess->sess_ops->DataSequenceInOrder) {
  556. if (!first_incomplete_r2t) {
  557. cmd->r2t_offset -= r2t->xfer_len;
  558. goto next;
  559. }
  560. if (conn->sess->sess_ops->DataPDUInOrder) {
  561. cmd->data_sn = 0;
  562. cmd->r2t_offset -= (r2t->xfer_len -
  563. cmd->next_burst_len);
  564. first_incomplete_r2t = 0;
  565. goto next;
  566. }
  567. cmd->data_sn = 0;
  568. cmd->r2t_offset -= r2t->xfer_len;
  569. for (i = 0; i < cmd->pdu_count; i++) {
  570. pdu = &cmd->pdu_list[i];
  571. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  572. continue;
  573. if ((pdu->offset >= r2t->offset) &&
  574. (pdu->offset < (r2t->offset +
  575. r2t->xfer_len))) {
  576. cmd->next_burst_len -= pdu->length;
  577. cmd->write_data_done -= pdu->length;
  578. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  579. }
  580. }
  581. first_incomplete_r2t = 0;
  582. } else {
  583. struct iscsi_seq *seq;
  584. seq = iscsit_get_seq_holder(cmd, r2t->offset,
  585. r2t->xfer_len);
  586. if (!seq) {
  587. spin_unlock_bh(&cmd->r2t_lock);
  588. return -1;
  589. }
  590. cmd->write_data_done -=
  591. (seq->offset - seq->orig_offset);
  592. seq->data_sn = 0;
  593. seq->offset = seq->orig_offset;
  594. seq->next_burst_len = 0;
  595. seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
  596. cmd->seq_send_order--;
  597. if (conn->sess->sess_ops->DataPDUInOrder)
  598. goto next;
  599. for (i = 0; i < seq->pdu_count; i++) {
  600. pdu = &cmd->pdu_list[i+seq->pdu_start];
  601. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  602. continue;
  603. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  604. }
  605. }
  606. next:
  607. cmd->outstanding_r2ts--;
  608. }
  609. spin_unlock_bh(&cmd->r2t_lock);
  610. /*
  611. * We now drop all unacknowledged R2Ts, ie: ExpDataSN from TMR
  612. * TASK_REASSIGN to the last R2T in the list.. We are also careful
  613. * to check that the Initiator is not requesting R2Ts for DataOUT
  614. * sequences it has already completed.
  615. *
  616. * Free each R2T in question and adjust values in struct iscsi_cmd
  617. * accordingly so iscsit_build_r2ts_for_cmd() do the rest of
  618. * the work after the TMR TASK_REASSIGN Response is sent.
  619. */
  620. drop_unacknowledged_r2ts:
  621. cmd->cmd_flags &= ~ICF_SENT_LAST_R2T;
  622. cmd->r2t_sn = tmr_req->exp_data_sn;
  623. spin_lock_bh(&cmd->r2t_lock);
  624. list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list) {
  625. /*
  626. * Skip up to the R2T Sequence number provided by the
  627. * iSCSI TASK_REASSIGN TMR
  628. */
  629. if (r2t->r2t_sn < tmr_req->exp_data_sn)
  630. continue;
  631. if (r2t->seq_complete) {
  632. pr_err("Initiator is requesting R2Ts from"
  633. " R2TSN: 0x%08x, but R2TSN: 0x%08x, Offset: %u,"
  634. " Length: %u is already complete."
  635. " BAD INITIATOR ERL=2 IMPLEMENTATION!\n",
  636. tmr_req->exp_data_sn, r2t->r2t_sn,
  637. r2t->offset, r2t->xfer_len);
  638. spin_unlock_bh(&cmd->r2t_lock);
  639. return -1;
  640. }
  641. if (r2t->recovery_r2t) {
  642. iscsit_free_r2t(r2t, cmd);
  643. continue;
  644. }
  645. /* DataSequenceInOrder=Yes:
  646. *
  647. * Taking into account the iSCSI implementation requirement of
  648. * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
  649. * DataSequenceInOrder=Yes, it's safe to subtract the R2Ts
  650. * entire transfer length from the commands R2T offset marker.
  651. *
  652. * DataSequenceInOrder=No:
  653. *
  654. * We subtract the difference from struct iscsi_seq between the
  655. * current offset and original offset from cmd->write_data_done
  656. * for account for DataOUT PDUs already received. Then reset
  657. * the current offset to the original and zero out the current
  658. * burst length, to make sure we re-request the entire DataOUT
  659. * sequence.
  660. */
  661. if (conn->sess->sess_ops->DataSequenceInOrder)
  662. cmd->r2t_offset -= r2t->xfer_len;
  663. else
  664. cmd->seq_send_order--;
  665. cmd->outstanding_r2ts--;
  666. iscsit_free_r2t(r2t, cmd);
  667. }
  668. spin_unlock_bh(&cmd->r2t_lock);
  669. return 0;
  670. }
  671. /*
  672. * Performs sanity checks TMR TASK_REASSIGN's ExpDataSN for
  673. * a given struct iscsi_cmd.
  674. */
  675. int iscsit_check_task_reassign_expdatasn(
  676. struct iscsi_tmr_req *tmr_req,
  677. struct iscsi_conn *conn)
  678. {
  679. struct iscsi_cmd *ref_cmd = tmr_req->ref_cmd;
  680. if (ref_cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD)
  681. return 0;
  682. if (ref_cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION)
  683. return 0;
  684. if (ref_cmd->data_direction == DMA_NONE)
  685. return 0;
  686. /*
  687. * For READs the TMR TASK_REASSIGNs ExpDataSN contains the next DataSN
  688. * of DataIN the Initiator is expecting.
  689. *
  690. * Also check that the Initiator is not re-requesting DataIN that has
  691. * already been acknowledged with a DataAck SNACK.
  692. */
  693. if (ref_cmd->data_direction == DMA_FROM_DEVICE) {
  694. if (tmr_req->exp_data_sn > ref_cmd->data_sn) {
  695. pr_err("Received ExpDataSN: 0x%08x for READ"
  696. " in TMR TASK_REASSIGN greater than command's"
  697. " DataSN: 0x%08x.\n", tmr_req->exp_data_sn,
  698. ref_cmd->data_sn);
  699. return -1;
  700. }
  701. if ((ref_cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
  702. (tmr_req->exp_data_sn <= ref_cmd->acked_data_sn)) {
  703. pr_err("Received ExpDataSN: 0x%08x for READ"
  704. " in TMR TASK_REASSIGN for previously"
  705. " acknowledged DataIN: 0x%08x,"
  706. " protocol error\n", tmr_req->exp_data_sn,
  707. ref_cmd->acked_data_sn);
  708. return -1;
  709. }
  710. return iscsit_task_reassign_prepare_read(tmr_req, conn);
  711. }
  712. /*
  713. * For WRITEs the TMR TASK_REASSIGNs ExpDataSN contains the next R2TSN
  714. * for R2Ts the Initiator is expecting.
  715. *
  716. * Do the magic in iscsit_task_reassign_prepare_write().
  717. */
  718. if (ref_cmd->data_direction == DMA_TO_DEVICE) {
  719. if (tmr_req->exp_data_sn > ref_cmd->r2t_sn) {
  720. pr_err("Received ExpDataSN: 0x%08x for WRITE"
  721. " in TMR TASK_REASSIGN greater than command's"
  722. " R2TSN: 0x%08x.\n", tmr_req->exp_data_sn,
  723. ref_cmd->r2t_sn);
  724. return -1;
  725. }
  726. return iscsit_task_reassign_prepare_write(tmr_req, conn);
  727. }
  728. pr_err("Unknown iSCSI data_direction: 0x%02x\n",
  729. ref_cmd->data_direction);
  730. return -1;
  731. }