iscsi_target_tmr.c 24 KB

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