iscsi_target_tmr.c 24 KB

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