iscsi_target_tmr.c 24 KB

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