iscsi_target_tmr.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*******************************************************************************
  2. * This file contains the iSCSI Target specific Task Management functions.
  3. *
  4. * (c) Copyright 2007-2013 Datera, Inc.
  5. *
  6. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. ******************************************************************************/
  18. #include <asm/unaligned.h>
  19. #include <scsi/scsi_proto.h>
  20. #include <scsi/iscsi_proto.h>
  21. #include <target/target_core_base.h>
  22. #include <target/target_core_fabric.h>
  23. #include <target/iscsi/iscsi_transport.h>
  24. #include <target/iscsi/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 (iscsi_sna_gte(be32_to_cpu(hdr->refcmdsn), conn->sess->exp_cmd_sn) &&
  49. iscsi_sna_lte(be32_to_cpu(hdr->refcmdsn), (u32) atomic_read(&conn->sess->max_cmd_sn))) ?
  50. ISCSI_TMF_RSP_COMPLETE : ISCSI_TMF_RSP_NO_TASK;
  51. }
  52. if (ref_cmd->cmd_sn != be32_to_cpu(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 = (__force u32)hdr->rtt;
  59. tmr_req->ref_cmd = ref_cmd;
  60. tmr_req->exp_data_sn = be32_to_cpu(hdr->exp_datasn);
  61. return ISCSI_TMF_RSP_COMPLETE;
  62. }
  63. /*
  64. * Called from iscsit_handle_task_mgt_cmd().
  65. */
  66. int iscsit_tmr_task_warm_reset(
  67. struct iscsi_conn *conn,
  68. struct iscsi_tmr_req *tmr_req,
  69. unsigned char *buf)
  70. {
  71. struct iscsi_session *sess = conn->sess;
  72. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  73. if (!na->tmr_warm_reset) {
  74. pr_err("TMR Opcode TARGET_WARM_RESET authorization"
  75. " failed for Initiator Node: %s\n",
  76. sess->se_sess->se_node_acl->initiatorname);
  77. return -1;
  78. }
  79. /*
  80. * Do the real work in transport_generic_do_tmr().
  81. */
  82. return 0;
  83. }
  84. int iscsit_tmr_task_cold_reset(
  85. struct iscsi_conn *conn,
  86. struct iscsi_tmr_req *tmr_req,
  87. unsigned char *buf)
  88. {
  89. struct iscsi_session *sess = conn->sess;
  90. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  91. if (!na->tmr_cold_reset) {
  92. pr_err("TMR Opcode TARGET_COLD_RESET authorization"
  93. " failed for Initiator Node: %s\n",
  94. sess->se_sess->se_node_acl->initiatorname);
  95. return -1;
  96. }
  97. /*
  98. * Do the real work in transport_generic_do_tmr().
  99. */
  100. return 0;
  101. }
  102. u8 iscsit_tmr_task_reassign(
  103. struct iscsi_cmd *cmd,
  104. unsigned char *buf)
  105. {
  106. struct iscsi_cmd *ref_cmd = NULL;
  107. struct iscsi_conn *conn = cmd->conn;
  108. struct iscsi_conn_recovery *cr = NULL;
  109. struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
  110. struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
  111. struct iscsi_tm *hdr = (struct iscsi_tm *) buf;
  112. u64 ret, ref_lun;
  113. pr_debug("Got TASK_REASSIGN TMR ITT: 0x%08x,"
  114. " RefTaskTag: 0x%08x, ExpDataSN: 0x%08x, CID: %hu\n",
  115. hdr->itt, hdr->rtt, hdr->exp_datasn, conn->cid);
  116. if (conn->sess->sess_ops->ErrorRecoveryLevel != 2) {
  117. pr_err("TMR TASK_REASSIGN not supported in ERL<2,"
  118. " ignoring request.\n");
  119. return ISCSI_TMF_RSP_NOT_SUPPORTED;
  120. }
  121. ret = iscsit_find_cmd_for_recovery(conn->sess, &ref_cmd, &cr, hdr->rtt);
  122. if (ret == -2) {
  123. pr_err("Command ITT: 0x%08x is still alligent to CID:"
  124. " %hu\n", ref_cmd->init_task_tag, cr->cid);
  125. return ISCSI_TMF_RSP_TASK_ALLEGIANT;
  126. } else if (ret == -1) {
  127. pr_err("Unable to locate RefTaskTag: 0x%08x in"
  128. " connection recovery command list.\n", hdr->rtt);
  129. return ISCSI_TMF_RSP_NO_TASK;
  130. }
  131. /*
  132. * Temporary check to prevent connection recovery for
  133. * connections with a differing Max*DataSegmentLength.
  134. */
  135. if (cr->maxrecvdatasegmentlength !=
  136. conn->conn_ops->MaxRecvDataSegmentLength) {
  137. pr_err("Unable to perform connection recovery for"
  138. " differing MaxRecvDataSegmentLength, rejecting"
  139. " TMR TASK_REASSIGN.\n");
  140. return ISCSI_TMF_RSP_REJECTED;
  141. }
  142. if (cr->maxxmitdatasegmentlength !=
  143. conn->conn_ops->MaxXmitDataSegmentLength) {
  144. pr_err("Unable to perform connection recovery for"
  145. " differing MaxXmitDataSegmentLength, rejecting"
  146. " TMR TASK_REASSIGN.\n");
  147. return ISCSI_TMF_RSP_REJECTED;
  148. }
  149. ref_lun = scsilun_to_int(&hdr->lun);
  150. if (ref_lun != ref_cmd->se_cmd.orig_fe_lun) {
  151. pr_err("Unable to perform connection recovery for"
  152. " differing ref_lun: %llu ref_cmd orig_fe_lun: %llu\n",
  153. ref_lun, ref_cmd->se_cmd.orig_fe_lun);
  154. return ISCSI_TMF_RSP_REJECTED;
  155. }
  156. se_tmr->ref_task_tag = (__force u32)hdr->rtt;
  157. tmr_req->ref_cmd = ref_cmd;
  158. tmr_req->exp_data_sn = be32_to_cpu(hdr->exp_datasn);
  159. tmr_req->conn_recovery = cr;
  160. tmr_req->task_reassign = 1;
  161. /*
  162. * Command can now be reassigned to a new connection.
  163. * The task management response must be sent before the
  164. * reassignment actually happens. See iscsi_tmr_post_handler().
  165. */
  166. return ISCSI_TMF_RSP_COMPLETE;
  167. }
  168. static void iscsit_task_reassign_remove_cmd(
  169. struct iscsi_cmd *cmd,
  170. struct iscsi_conn_recovery *cr,
  171. struct iscsi_session *sess)
  172. {
  173. int ret;
  174. spin_lock(&cr->conn_recovery_cmd_lock);
  175. ret = iscsit_remove_cmd_from_connection_recovery(cmd, sess);
  176. spin_unlock(&cr->conn_recovery_cmd_lock);
  177. if (!ret) {
  178. pr_debug("iSCSI connection recovery successful for CID:"
  179. " %hu on SID: %u\n", cr->cid, sess->sid);
  180. iscsit_remove_active_connection_recovery_entry(cr, sess);
  181. }
  182. }
  183. static int iscsit_task_reassign_complete_nop_out(
  184. struct iscsi_tmr_req *tmr_req,
  185. struct iscsi_conn *conn)
  186. {
  187. struct iscsi_cmd *cmd = tmr_req->ref_cmd;
  188. struct iscsi_conn_recovery *cr;
  189. if (!cmd->cr) {
  190. pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
  191. " is NULL!\n", cmd->init_task_tag);
  192. return -1;
  193. }
  194. cr = cmd->cr;
  195. /*
  196. * Reset the StatSN so a new one for this commands new connection
  197. * will be assigned.
  198. * Reset the ExpStatSN as well so we may receive Status SNACKs.
  199. */
  200. cmd->stat_sn = cmd->exp_stat_sn = 0;
  201. iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
  202. spin_lock_bh(&conn->cmd_lock);
  203. list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
  204. spin_unlock_bh(&conn->cmd_lock);
  205. cmd->i_state = ISTATE_SEND_NOPIN;
  206. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  207. return 0;
  208. }
  209. static int iscsit_task_reassign_complete_write(
  210. struct iscsi_cmd *cmd,
  211. struct iscsi_tmr_req *tmr_req)
  212. {
  213. int no_build_r2ts = 0;
  214. u32 length = 0, offset = 0;
  215. struct iscsi_conn *conn = cmd->conn;
  216. struct se_cmd *se_cmd = &cmd->se_cmd;
  217. /*
  218. * The Initiator must not send a R2T SNACK with a Begrun less than
  219. * the TMR TASK_REASSIGN's ExpDataSN.
  220. */
  221. if (!tmr_req->exp_data_sn) {
  222. cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
  223. cmd->acked_data_sn = 0;
  224. } else {
  225. cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
  226. cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
  227. }
  228. /*
  229. * The TMR TASK_REASSIGN's ExpDataSN contains the next R2TSN the
  230. * Initiator is expecting. The Target controls all WRITE operations
  231. * so if we have received all DataOUT we can safety ignore Initiator.
  232. */
  233. if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
  234. if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
  235. pr_debug("WRITE ITT: 0x%08x: t_state: %d"
  236. " never sent to transport\n",
  237. cmd->init_task_tag, cmd->se_cmd.t_state);
  238. target_execute_cmd(se_cmd);
  239. return 0;
  240. }
  241. cmd->i_state = ISTATE_SEND_STATUS;
  242. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  243. return 0;
  244. }
  245. /*
  246. * Special case to deal with DataSequenceInOrder=No and Non-Immeidate
  247. * Unsolicited DataOut.
  248. */
  249. if (cmd->unsolicited_data) {
  250. cmd->unsolicited_data = 0;
  251. offset = cmd->next_burst_len = cmd->write_data_done;
  252. if ((conn->sess->sess_ops->FirstBurstLength - offset) >=
  253. cmd->se_cmd.data_length) {
  254. no_build_r2ts = 1;
  255. length = (cmd->se_cmd.data_length - offset);
  256. } else
  257. length = (conn->sess->sess_ops->FirstBurstLength - offset);
  258. spin_lock_bh(&cmd->r2t_lock);
  259. if (iscsit_add_r2t_to_list(cmd, offset, length, 0, 0) < 0) {
  260. spin_unlock_bh(&cmd->r2t_lock);
  261. return -1;
  262. }
  263. cmd->outstanding_r2ts++;
  264. spin_unlock_bh(&cmd->r2t_lock);
  265. if (no_build_r2ts)
  266. return 0;
  267. }
  268. /*
  269. * iscsit_build_r2ts_for_cmd() can handle the rest from here.
  270. */
  271. return conn->conn_transport->iscsit_get_dataout(conn, cmd, true);
  272. }
  273. static int iscsit_task_reassign_complete_read(
  274. struct iscsi_cmd *cmd,
  275. struct iscsi_tmr_req *tmr_req)
  276. {
  277. struct iscsi_conn *conn = cmd->conn;
  278. struct iscsi_datain_req *dr;
  279. struct se_cmd *se_cmd = &cmd->se_cmd;
  280. /*
  281. * The Initiator must not send a Data SNACK with a BegRun less than
  282. * the TMR TASK_REASSIGN's ExpDataSN.
  283. */
  284. if (!tmr_req->exp_data_sn) {
  285. cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
  286. cmd->acked_data_sn = 0;
  287. } else {
  288. cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
  289. cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
  290. }
  291. if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
  292. pr_debug("READ ITT: 0x%08x: t_state: %d never sent to"
  293. " transport\n", cmd->init_task_tag,
  294. cmd->se_cmd.t_state);
  295. transport_handle_cdb_direct(se_cmd);
  296. return 0;
  297. }
  298. if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
  299. pr_err("READ ITT: 0x%08x: t_state: %d, never returned"
  300. " from transport\n", cmd->init_task_tag,
  301. cmd->se_cmd.t_state);
  302. return -1;
  303. }
  304. dr = iscsit_allocate_datain_req();
  305. if (!dr)
  306. return -1;
  307. /*
  308. * The TMR TASK_REASSIGN's ExpDataSN contains the next DataSN the
  309. * Initiator is expecting.
  310. */
  311. dr->data_sn = dr->begrun = tmr_req->exp_data_sn;
  312. dr->runlength = 0;
  313. dr->generate_recovery_values = 1;
  314. dr->recovery = DATAIN_CONNECTION_RECOVERY;
  315. iscsit_attach_datain_req(cmd, dr);
  316. cmd->i_state = ISTATE_SEND_DATAIN;
  317. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  318. return 0;
  319. }
  320. static int iscsit_task_reassign_complete_none(
  321. struct iscsi_cmd *cmd,
  322. struct iscsi_tmr_req *tmr_req)
  323. {
  324. struct iscsi_conn *conn = cmd->conn;
  325. cmd->i_state = ISTATE_SEND_STATUS;
  326. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  327. return 0;
  328. }
  329. static int iscsit_task_reassign_complete_scsi_cmnd(
  330. struct iscsi_tmr_req *tmr_req,
  331. struct iscsi_conn *conn)
  332. {
  333. struct iscsi_cmd *cmd = tmr_req->ref_cmd;
  334. struct iscsi_conn_recovery *cr;
  335. if (!cmd->cr) {
  336. pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
  337. " is NULL!\n", cmd->init_task_tag);
  338. return -1;
  339. }
  340. cr = cmd->cr;
  341. /*
  342. * Reset the StatSN so a new one for this commands new connection
  343. * will be assigned.
  344. * Reset the ExpStatSN as well so we may receive Status SNACKs.
  345. */
  346. cmd->stat_sn = cmd->exp_stat_sn = 0;
  347. iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
  348. spin_lock_bh(&conn->cmd_lock);
  349. list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
  350. spin_unlock_bh(&conn->cmd_lock);
  351. if (cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  352. cmd->i_state = ISTATE_SEND_STATUS;
  353. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  354. return 0;
  355. }
  356. switch (cmd->data_direction) {
  357. case DMA_TO_DEVICE:
  358. return iscsit_task_reassign_complete_write(cmd, tmr_req);
  359. case DMA_FROM_DEVICE:
  360. return iscsit_task_reassign_complete_read(cmd, tmr_req);
  361. case DMA_NONE:
  362. return iscsit_task_reassign_complete_none(cmd, tmr_req);
  363. default:
  364. pr_err("Unknown cmd->data_direction: 0x%02x\n",
  365. cmd->data_direction);
  366. return -1;
  367. }
  368. return 0;
  369. }
  370. static int iscsit_task_reassign_complete(
  371. struct iscsi_tmr_req *tmr_req,
  372. struct iscsi_conn *conn)
  373. {
  374. struct iscsi_cmd *cmd;
  375. int ret = 0;
  376. if (!tmr_req->ref_cmd) {
  377. pr_err("TMR Request is missing a RefCmd struct iscsi_cmd.\n");
  378. return -1;
  379. }
  380. cmd = tmr_req->ref_cmd;
  381. cmd->conn = conn;
  382. switch (cmd->iscsi_opcode) {
  383. case ISCSI_OP_NOOP_OUT:
  384. ret = iscsit_task_reassign_complete_nop_out(tmr_req, conn);
  385. break;
  386. case ISCSI_OP_SCSI_CMD:
  387. ret = iscsit_task_reassign_complete_scsi_cmnd(tmr_req, conn);
  388. break;
  389. default:
  390. pr_err("Illegal iSCSI Opcode 0x%02x during"
  391. " command realligence\n", cmd->iscsi_opcode);
  392. return -1;
  393. }
  394. if (ret != 0)
  395. return ret;
  396. pr_debug("Completed connection realligence for Opcode: 0x%02x,"
  397. " ITT: 0x%08x to CID: %hu.\n", cmd->iscsi_opcode,
  398. cmd->init_task_tag, conn->cid);
  399. return 0;
  400. }
  401. /*
  402. * Handles special after-the-fact actions related to TMRs.
  403. * Right now the only one that its really needed for is
  404. * connection recovery releated TASK_REASSIGN.
  405. */
  406. int iscsit_tmr_post_handler(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  407. {
  408. struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
  409. struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
  410. if (tmr_req->task_reassign &&
  411. (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
  412. return iscsit_task_reassign_complete(tmr_req, conn);
  413. return 0;
  414. }
  415. EXPORT_SYMBOL(iscsit_tmr_post_handler);
  416. /*
  417. * Nothing to do here, but leave it for good measure. :-)
  418. */
  419. static int iscsit_task_reassign_prepare_read(
  420. struct iscsi_tmr_req *tmr_req,
  421. struct iscsi_conn *conn)
  422. {
  423. return 0;
  424. }
  425. static void iscsit_task_reassign_prepare_unsolicited_dataout(
  426. struct iscsi_cmd *cmd,
  427. struct iscsi_conn *conn)
  428. {
  429. int i, j;
  430. struct iscsi_pdu *pdu = NULL;
  431. struct iscsi_seq *seq = NULL;
  432. if (conn->sess->sess_ops->DataSequenceInOrder) {
  433. cmd->data_sn = 0;
  434. if (cmd->immediate_data)
  435. cmd->r2t_offset += (cmd->first_burst_len -
  436. cmd->seq_start_offset);
  437. if (conn->sess->sess_ops->DataPDUInOrder) {
  438. cmd->write_data_done -= (cmd->immediate_data) ?
  439. (cmd->first_burst_len -
  440. cmd->seq_start_offset) :
  441. cmd->first_burst_len;
  442. cmd->first_burst_len = 0;
  443. return;
  444. }
  445. for (i = 0; i < cmd->pdu_count; i++) {
  446. pdu = &cmd->pdu_list[i];
  447. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  448. continue;
  449. if ((pdu->offset >= cmd->seq_start_offset) &&
  450. ((pdu->offset + pdu->length) <=
  451. cmd->seq_end_offset)) {
  452. cmd->first_burst_len -= pdu->length;
  453. cmd->write_data_done -= pdu->length;
  454. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  455. }
  456. }
  457. } else {
  458. for (i = 0; i < cmd->seq_count; i++) {
  459. seq = &cmd->seq_list[i];
  460. if (seq->type != SEQTYPE_UNSOLICITED)
  461. continue;
  462. cmd->write_data_done -=
  463. (seq->offset - seq->orig_offset);
  464. cmd->first_burst_len = 0;
  465. seq->data_sn = 0;
  466. seq->offset = seq->orig_offset;
  467. seq->next_burst_len = 0;
  468. seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
  469. if (conn->sess->sess_ops->DataPDUInOrder)
  470. continue;
  471. for (j = 0; j < seq->pdu_count; j++) {
  472. pdu = &cmd->pdu_list[j+seq->pdu_start];
  473. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  474. continue;
  475. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  476. }
  477. }
  478. }
  479. }
  480. static int iscsit_task_reassign_prepare_write(
  481. struct iscsi_tmr_req *tmr_req,
  482. struct iscsi_conn *conn)
  483. {
  484. struct iscsi_cmd *cmd = tmr_req->ref_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 iscsi_cmd *ref_cmd = tmr_req->ref_cmd;
  688. if (ref_cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD)
  689. return 0;
  690. if (ref_cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION)
  691. return 0;
  692. if (ref_cmd->data_direction == DMA_NONE)
  693. return 0;
  694. /*
  695. * For READs the TMR TASK_REASSIGNs ExpDataSN contains the next DataSN
  696. * of DataIN the Initiator is expecting.
  697. *
  698. * Also check that the Initiator is not re-requesting DataIN that has
  699. * already been acknowledged with a DataAck SNACK.
  700. */
  701. if (ref_cmd->data_direction == DMA_FROM_DEVICE) {
  702. if (tmr_req->exp_data_sn > ref_cmd->data_sn) {
  703. pr_err("Received ExpDataSN: 0x%08x for READ"
  704. " in TMR TASK_REASSIGN greater than command's"
  705. " DataSN: 0x%08x.\n", tmr_req->exp_data_sn,
  706. ref_cmd->data_sn);
  707. return -1;
  708. }
  709. if ((ref_cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
  710. (tmr_req->exp_data_sn <= ref_cmd->acked_data_sn)) {
  711. pr_err("Received ExpDataSN: 0x%08x for READ"
  712. " in TMR TASK_REASSIGN for previously"
  713. " acknowledged DataIN: 0x%08x,"
  714. " protocol error\n", tmr_req->exp_data_sn,
  715. ref_cmd->acked_data_sn);
  716. return -1;
  717. }
  718. return iscsit_task_reassign_prepare_read(tmr_req, conn);
  719. }
  720. /*
  721. * For WRITEs the TMR TASK_REASSIGNs ExpDataSN contains the next R2TSN
  722. * for R2Ts the Initiator is expecting.
  723. *
  724. * Do the magic in iscsit_task_reassign_prepare_write().
  725. */
  726. if (ref_cmd->data_direction == DMA_TO_DEVICE) {
  727. if (tmr_req->exp_data_sn > ref_cmd->r2t_sn) {
  728. pr_err("Received ExpDataSN: 0x%08x for WRITE"
  729. " in TMR TASK_REASSIGN greater than command's"
  730. " R2TSN: 0x%08x.\n", tmr_req->exp_data_sn,
  731. ref_cmd->r2t_sn);
  732. return -1;
  733. }
  734. return iscsit_task_reassign_prepare_write(tmr_req, conn);
  735. }
  736. pr_err("Unknown iSCSI data_direction: 0x%02x\n",
  737. ref_cmd->data_direction);
  738. return -1;
  739. }