iscsi_target_tmr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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 "iscsi_target_core.h"
  26. #include "iscsi_target_seq_pdu_list.h"
  27. #include "iscsi_target_datain_values.h"
  28. #include "iscsi_target_device.h"
  29. #include "iscsi_target_erl0.h"
  30. #include "iscsi_target_erl1.h"
  31. #include "iscsi_target_erl2.h"
  32. #include "iscsi_target_tmr.h"
  33. #include "iscsi_target_tpg.h"
  34. #include "iscsi_target_util.h"
  35. #include "iscsi_target.h"
  36. u8 iscsit_tmr_abort_task(
  37. struct iscsi_cmd *cmd,
  38. unsigned char *buf)
  39. {
  40. struct iscsi_cmd *ref_cmd;
  41. struct iscsi_conn *conn = cmd->conn;
  42. struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
  43. struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
  44. struct iscsi_tm *hdr = (struct iscsi_tm *) buf;
  45. ref_cmd = iscsit_find_cmd_from_itt(conn, hdr->rtt);
  46. if (!ref_cmd) {
  47. pr_err("Unable to locate RefTaskTag: 0x%08x on CID:"
  48. " %hu.\n", hdr->rtt, conn->cid);
  49. return ((hdr->refcmdsn >= conn->sess->exp_cmd_sn) &&
  50. (hdr->refcmdsn <= conn->sess->max_cmd_sn)) ?
  51. ISCSI_TMF_RSP_COMPLETE : ISCSI_TMF_RSP_NO_TASK;
  52. }
  53. if (ref_cmd->cmd_sn != hdr->refcmdsn) {
  54. pr_err("RefCmdSN 0x%08x does not equal"
  55. " task's CmdSN 0x%08x. Rejecting ABORT_TASK.\n",
  56. hdr->refcmdsn, ref_cmd->cmd_sn);
  57. return ISCSI_TMF_RSP_REJECTED;
  58. }
  59. se_tmr->ref_task_tag = hdr->rtt;
  60. tmr_req->ref_cmd = ref_cmd;
  61. tmr_req->ref_cmd_sn = hdr->refcmdsn;
  62. tmr_req->exp_data_sn = 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 MaxRecvDataSegmentLength.
  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. ref_lun = scsilun_to_int(&hdr->lun);
  145. if (ref_lun != ref_cmd->se_cmd.orig_fe_lun) {
  146. pr_err("Unable to perform connection recovery for"
  147. " differing ref_lun: %d ref_cmd orig_fe_lun: %u\n",
  148. ref_lun, ref_cmd->se_cmd.orig_fe_lun);
  149. return ISCSI_TMF_RSP_REJECTED;
  150. }
  151. se_tmr->ref_task_tag = hdr->rtt;
  152. tmr_req->ref_cmd = ref_cmd;
  153. tmr_req->ref_cmd_sn = hdr->refcmdsn;
  154. tmr_req->exp_data_sn = hdr->exp_datasn;
  155. tmr_req->conn_recovery = cr;
  156. tmr_req->task_reassign = 1;
  157. /*
  158. * Command can now be reassigned to a new connection.
  159. * The task management response must be sent before the
  160. * reassignment actually happens. See iscsi_tmr_post_handler().
  161. */
  162. return ISCSI_TMF_RSP_COMPLETE;
  163. }
  164. static void iscsit_task_reassign_remove_cmd(
  165. struct iscsi_cmd *cmd,
  166. struct iscsi_conn_recovery *cr,
  167. struct iscsi_session *sess)
  168. {
  169. int ret;
  170. spin_lock(&cr->conn_recovery_cmd_lock);
  171. ret = iscsit_remove_cmd_from_connection_recovery(cmd, sess);
  172. spin_unlock(&cr->conn_recovery_cmd_lock);
  173. if (!ret) {
  174. pr_debug("iSCSI connection recovery successful for CID:"
  175. " %hu on SID: %u\n", cr->cid, sess->sid);
  176. iscsit_remove_active_connection_recovery_entry(cr, sess);
  177. }
  178. }
  179. static int iscsit_task_reassign_complete_nop_out(
  180. struct iscsi_tmr_req *tmr_req,
  181. struct iscsi_conn *conn)
  182. {
  183. struct iscsi_cmd *cmd = tmr_req->ref_cmd;
  184. struct iscsi_conn_recovery *cr;
  185. if (!cmd->cr) {
  186. pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
  187. " is NULL!\n", cmd->init_task_tag);
  188. return -1;
  189. }
  190. cr = cmd->cr;
  191. /*
  192. * Reset the StatSN so a new one for this commands new connection
  193. * will be assigned.
  194. * Reset the ExpStatSN as well so we may receive Status SNACKs.
  195. */
  196. cmd->stat_sn = cmd->exp_stat_sn = 0;
  197. iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
  198. spin_lock_bh(&conn->cmd_lock);
  199. list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
  200. spin_unlock_bh(&conn->cmd_lock);
  201. cmd->i_state = ISTATE_SEND_NOPIN;
  202. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  203. return 0;
  204. }
  205. static int iscsit_task_reassign_complete_write(
  206. struct iscsi_cmd *cmd,
  207. struct iscsi_tmr_req *tmr_req)
  208. {
  209. int no_build_r2ts = 0;
  210. u32 length = 0, offset = 0;
  211. struct iscsi_conn *conn = cmd->conn;
  212. struct se_cmd *se_cmd = &cmd->se_cmd;
  213. /*
  214. * The Initiator must not send a R2T SNACK with a Begrun less than
  215. * the TMR TASK_REASSIGN's ExpDataSN.
  216. */
  217. if (!tmr_req->exp_data_sn) {
  218. cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
  219. cmd->acked_data_sn = 0;
  220. } else {
  221. cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
  222. cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
  223. }
  224. /*
  225. * The TMR TASK_REASSIGN's ExpDataSN contains the next R2TSN the
  226. * Initiator is expecting. The Target controls all WRITE operations
  227. * so if we have received all DataOUT we can safety ignore Initiator.
  228. */
  229. if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
  230. if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
  231. pr_debug("WRITE ITT: 0x%08x: t_state: %d"
  232. " never sent to transport\n",
  233. cmd->init_task_tag, cmd->se_cmd.t_state);
  234. return transport_generic_handle_data(se_cmd);
  235. }
  236. cmd->i_state = ISTATE_SEND_STATUS;
  237. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  238. return 0;
  239. }
  240. /*
  241. * Special case to deal with DataSequenceInOrder=No and Non-Immeidate
  242. * Unsolicited DataOut.
  243. */
  244. if (cmd->unsolicited_data) {
  245. cmd->unsolicited_data = 0;
  246. offset = cmd->next_burst_len = cmd->write_data_done;
  247. if ((conn->sess->sess_ops->FirstBurstLength - offset) >=
  248. cmd->se_cmd.data_length) {
  249. no_build_r2ts = 1;
  250. length = (cmd->se_cmd.data_length - offset);
  251. } else
  252. length = (conn->sess->sess_ops->FirstBurstLength - offset);
  253. spin_lock_bh(&cmd->r2t_lock);
  254. if (iscsit_add_r2t_to_list(cmd, offset, length, 0, 0) < 0) {
  255. spin_unlock_bh(&cmd->r2t_lock);
  256. return -1;
  257. }
  258. cmd->outstanding_r2ts++;
  259. spin_unlock_bh(&cmd->r2t_lock);
  260. if (no_build_r2ts)
  261. return 0;
  262. }
  263. /*
  264. * iscsit_build_r2ts_for_cmd() can handle the rest from here.
  265. */
  266. return iscsit_build_r2ts_for_cmd(cmd, conn, true);
  267. }
  268. static int iscsit_task_reassign_complete_read(
  269. struct iscsi_cmd *cmd,
  270. struct iscsi_tmr_req *tmr_req)
  271. {
  272. struct iscsi_conn *conn = cmd->conn;
  273. struct iscsi_datain_req *dr;
  274. struct se_cmd *se_cmd = &cmd->se_cmd;
  275. /*
  276. * The Initiator must not send a Data SNACK with a BegRun less than
  277. * the TMR TASK_REASSIGN's ExpDataSN.
  278. */
  279. if (!tmr_req->exp_data_sn) {
  280. cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
  281. cmd->acked_data_sn = 0;
  282. } else {
  283. cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
  284. cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
  285. }
  286. if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
  287. pr_debug("READ ITT: 0x%08x: t_state: %d never sent to"
  288. " transport\n", cmd->init_task_tag,
  289. cmd->se_cmd.t_state);
  290. transport_handle_cdb_direct(se_cmd);
  291. return 0;
  292. }
  293. if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
  294. pr_err("READ ITT: 0x%08x: t_state: %d, never returned"
  295. " from transport\n", cmd->init_task_tag,
  296. cmd->se_cmd.t_state);
  297. return -1;
  298. }
  299. dr = iscsit_allocate_datain_req();
  300. if (!dr)
  301. return -1;
  302. /*
  303. * The TMR TASK_REASSIGN's ExpDataSN contains the next DataSN the
  304. * Initiator is expecting.
  305. */
  306. dr->data_sn = dr->begrun = tmr_req->exp_data_sn;
  307. dr->runlength = 0;
  308. dr->generate_recovery_values = 1;
  309. dr->recovery = DATAIN_CONNECTION_RECOVERY;
  310. iscsit_attach_datain_req(cmd, dr);
  311. cmd->i_state = ISTATE_SEND_DATAIN;
  312. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  313. return 0;
  314. }
  315. static int iscsit_task_reassign_complete_none(
  316. struct iscsi_cmd *cmd,
  317. struct iscsi_tmr_req *tmr_req)
  318. {
  319. struct iscsi_conn *conn = cmd->conn;
  320. cmd->i_state = ISTATE_SEND_STATUS;
  321. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  322. return 0;
  323. }
  324. static int iscsit_task_reassign_complete_scsi_cmnd(
  325. struct iscsi_tmr_req *tmr_req,
  326. struct iscsi_conn *conn)
  327. {
  328. struct iscsi_cmd *cmd = tmr_req->ref_cmd;
  329. struct iscsi_conn_recovery *cr;
  330. if (!cmd->cr) {
  331. pr_err("struct iscsi_conn_recovery pointer for ITT: 0x%08x"
  332. " is NULL!\n", cmd->init_task_tag);
  333. return -1;
  334. }
  335. cr = cmd->cr;
  336. /*
  337. * Reset the StatSN so a new one for this commands new connection
  338. * will be assigned.
  339. * Reset the ExpStatSN as well so we may receive Status SNACKs.
  340. */
  341. cmd->stat_sn = cmd->exp_stat_sn = 0;
  342. iscsit_task_reassign_remove_cmd(cmd, cr, conn->sess);
  343. spin_lock_bh(&conn->cmd_lock);
  344. list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
  345. spin_unlock_bh(&conn->cmd_lock);
  346. if (cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION) {
  347. cmd->i_state = ISTATE_SEND_STATUS;
  348. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  349. return 0;
  350. }
  351. switch (cmd->data_direction) {
  352. case DMA_TO_DEVICE:
  353. return iscsit_task_reassign_complete_write(cmd, tmr_req);
  354. case DMA_FROM_DEVICE:
  355. return iscsit_task_reassign_complete_read(cmd, tmr_req);
  356. case DMA_NONE:
  357. return iscsit_task_reassign_complete_none(cmd, tmr_req);
  358. default:
  359. pr_err("Unknown cmd->data_direction: 0x%02x\n",
  360. cmd->data_direction);
  361. return -1;
  362. }
  363. return 0;
  364. }
  365. static int iscsit_task_reassign_complete(
  366. struct iscsi_tmr_req *tmr_req,
  367. struct iscsi_conn *conn)
  368. {
  369. struct iscsi_cmd *cmd;
  370. int ret = 0;
  371. if (!tmr_req->ref_cmd) {
  372. pr_err("TMR Request is missing a RefCmd struct iscsi_cmd.\n");
  373. return -1;
  374. }
  375. cmd = tmr_req->ref_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 iscsi_cmd *cmd = tmr_req->ref_cmd;
  479. struct iscsi_pdu *pdu = NULL;
  480. struct iscsi_r2t *r2t = NULL, *r2t_tmp;
  481. int first_incomplete_r2t = 1, i = 0;
  482. /*
  483. * The command was in the process of receiving Unsolicited DataOUT when
  484. * the connection failed.
  485. */
  486. if (cmd->unsolicited_data)
  487. iscsit_task_reassign_prepare_unsolicited_dataout(cmd, conn);
  488. /*
  489. * The Initiator is requesting R2Ts starting from zero, skip
  490. * checking acknowledged R2Ts and start checking struct iscsi_r2ts
  491. * greater than zero.
  492. */
  493. if (!tmr_req->exp_data_sn)
  494. goto drop_unacknowledged_r2ts;
  495. /*
  496. * We now check that the PDUs in DataOUT sequences below
  497. * the TMR TASK_REASSIGN ExpDataSN (R2TSN the Initiator is
  498. * expecting next) have all the DataOUT they require to complete
  499. * the DataOUT sequence. First scan from R2TSN 0 to TMR
  500. * TASK_REASSIGN ExpDataSN-1.
  501. *
  502. * If we have not received all DataOUT in question, we must
  503. * make sure to make the appropriate changes to values in
  504. * struct iscsi_cmd (and elsewhere depending on session parameters)
  505. * so iscsit_build_r2ts_for_cmd() in iscsit_task_reassign_complete_write()
  506. * will resend a new R2T for the DataOUT sequences in question.
  507. */
  508. spin_lock_bh(&cmd->r2t_lock);
  509. if (list_empty(&cmd->cmd_r2t_list)) {
  510. spin_unlock_bh(&cmd->r2t_lock);
  511. return -1;
  512. }
  513. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  514. if (r2t->r2t_sn >= tmr_req->exp_data_sn)
  515. continue;
  516. /*
  517. * Safely ignore Recovery R2Ts and R2Ts that have completed
  518. * DataOUT sequences.
  519. */
  520. if (r2t->seq_complete)
  521. continue;
  522. if (r2t->recovery_r2t)
  523. continue;
  524. /*
  525. * DataSequenceInOrder=Yes:
  526. *
  527. * Taking into account the iSCSI implementation requirement of
  528. * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
  529. * DataSequenceInOrder=Yes, we must take into consideration
  530. * the following:
  531. *
  532. * DataSequenceInOrder=No:
  533. *
  534. * Taking into account that the Initiator controls the (possibly
  535. * random) PDU Order in (possibly random) Sequence Order of
  536. * DataOUT the target requests with R2Ts, we must take into
  537. * consideration the following:
  538. *
  539. * DataPDUInOrder=Yes for DataSequenceInOrder=[Yes,No]:
  540. *
  541. * While processing non-complete R2T DataOUT sequence requests
  542. * the Target will re-request only the total sequence length
  543. * minus current received offset. This is because we must
  544. * assume the initiator will continue sending DataOUT from the
  545. * last PDU before the connection failed.
  546. *
  547. * DataPDUInOrder=No for DataSequenceInOrder=[Yes,No]:
  548. *
  549. * While processing non-complete R2T DataOUT sequence requests
  550. * the Target will re-request the entire DataOUT sequence if
  551. * any single PDU is missing from the sequence. This is because
  552. * we have no logical method to determine the next PDU offset,
  553. * and we must assume the Initiator will be sending any random
  554. * PDU offset in the current sequence after TASK_REASSIGN
  555. * has completed.
  556. */
  557. if (conn->sess->sess_ops->DataSequenceInOrder) {
  558. if (!first_incomplete_r2t) {
  559. cmd->r2t_offset -= r2t->xfer_len;
  560. goto next;
  561. }
  562. if (conn->sess->sess_ops->DataPDUInOrder) {
  563. cmd->data_sn = 0;
  564. cmd->r2t_offset -= (r2t->xfer_len -
  565. cmd->next_burst_len);
  566. first_incomplete_r2t = 0;
  567. goto next;
  568. }
  569. cmd->data_sn = 0;
  570. cmd->r2t_offset -= r2t->xfer_len;
  571. for (i = 0; i < cmd->pdu_count; i++) {
  572. pdu = &cmd->pdu_list[i];
  573. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  574. continue;
  575. if ((pdu->offset >= r2t->offset) &&
  576. (pdu->offset < (r2t->offset +
  577. r2t->xfer_len))) {
  578. cmd->next_burst_len -= pdu->length;
  579. cmd->write_data_done -= pdu->length;
  580. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  581. }
  582. }
  583. first_incomplete_r2t = 0;
  584. } else {
  585. struct iscsi_seq *seq;
  586. seq = iscsit_get_seq_holder(cmd, r2t->offset,
  587. r2t->xfer_len);
  588. if (!seq) {
  589. spin_unlock_bh(&cmd->r2t_lock);
  590. return -1;
  591. }
  592. cmd->write_data_done -=
  593. (seq->offset - seq->orig_offset);
  594. seq->data_sn = 0;
  595. seq->offset = seq->orig_offset;
  596. seq->next_burst_len = 0;
  597. seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
  598. cmd->seq_send_order--;
  599. if (conn->sess->sess_ops->DataPDUInOrder)
  600. goto next;
  601. for (i = 0; i < seq->pdu_count; i++) {
  602. pdu = &cmd->pdu_list[i+seq->pdu_start];
  603. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  604. continue;
  605. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  606. }
  607. }
  608. next:
  609. cmd->outstanding_r2ts--;
  610. }
  611. spin_unlock_bh(&cmd->r2t_lock);
  612. /*
  613. * We now drop all unacknowledged R2Ts, ie: ExpDataSN from TMR
  614. * TASK_REASSIGN to the last R2T in the list.. We are also careful
  615. * to check that the Initiator is not requesting R2Ts for DataOUT
  616. * sequences it has already completed.
  617. *
  618. * Free each R2T in question and adjust values in struct iscsi_cmd
  619. * accordingly so iscsit_build_r2ts_for_cmd() do the rest of
  620. * the work after the TMR TASK_REASSIGN Response is sent.
  621. */
  622. drop_unacknowledged_r2ts:
  623. cmd->cmd_flags &= ~ICF_SENT_LAST_R2T;
  624. cmd->r2t_sn = tmr_req->exp_data_sn;
  625. spin_lock_bh(&cmd->r2t_lock);
  626. list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list) {
  627. /*
  628. * Skip up to the R2T Sequence number provided by the
  629. * iSCSI TASK_REASSIGN TMR
  630. */
  631. if (r2t->r2t_sn < tmr_req->exp_data_sn)
  632. continue;
  633. if (r2t->seq_complete) {
  634. pr_err("Initiator is requesting R2Ts from"
  635. " R2TSN: 0x%08x, but R2TSN: 0x%08x, Offset: %u,"
  636. " Length: %u is already complete."
  637. " BAD INITIATOR ERL=2 IMPLEMENTATION!\n",
  638. tmr_req->exp_data_sn, r2t->r2t_sn,
  639. r2t->offset, r2t->xfer_len);
  640. spin_unlock_bh(&cmd->r2t_lock);
  641. return -1;
  642. }
  643. if (r2t->recovery_r2t) {
  644. iscsit_free_r2t(r2t, cmd);
  645. continue;
  646. }
  647. /* DataSequenceInOrder=Yes:
  648. *
  649. * Taking into account the iSCSI implementation requirement of
  650. * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
  651. * DataSequenceInOrder=Yes, it's safe to subtract the R2Ts
  652. * entire transfer length from the commands R2T offset marker.
  653. *
  654. * DataSequenceInOrder=No:
  655. *
  656. * We subtract the difference from struct iscsi_seq between the
  657. * current offset and original offset from cmd->write_data_done
  658. * for account for DataOUT PDUs already received. Then reset
  659. * the current offset to the original and zero out the current
  660. * burst length, to make sure we re-request the entire DataOUT
  661. * sequence.
  662. */
  663. if (conn->sess->sess_ops->DataSequenceInOrder)
  664. cmd->r2t_offset -= r2t->xfer_len;
  665. else
  666. cmd->seq_send_order--;
  667. cmd->outstanding_r2ts--;
  668. iscsit_free_r2t(r2t, cmd);
  669. }
  670. spin_unlock_bh(&cmd->r2t_lock);
  671. return 0;
  672. }
  673. /*
  674. * Performs sanity checks TMR TASK_REASSIGN's ExpDataSN for
  675. * a given struct iscsi_cmd.
  676. */
  677. int iscsit_check_task_reassign_expdatasn(
  678. struct iscsi_tmr_req *tmr_req,
  679. struct iscsi_conn *conn)
  680. {
  681. struct iscsi_cmd *ref_cmd = tmr_req->ref_cmd;
  682. if (ref_cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD)
  683. return 0;
  684. if (ref_cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION)
  685. return 0;
  686. if (ref_cmd->data_direction == DMA_NONE)
  687. return 0;
  688. /*
  689. * For READs the TMR TASK_REASSIGNs ExpDataSN contains the next DataSN
  690. * of DataIN the Initiator is expecting.
  691. *
  692. * Also check that the Initiator is not re-requesting DataIN that has
  693. * already been acknowledged with a DataAck SNACK.
  694. */
  695. if (ref_cmd->data_direction == DMA_FROM_DEVICE) {
  696. if (tmr_req->exp_data_sn > ref_cmd->data_sn) {
  697. pr_err("Received ExpDataSN: 0x%08x for READ"
  698. " in TMR TASK_REASSIGN greater than command's"
  699. " DataSN: 0x%08x.\n", tmr_req->exp_data_sn,
  700. ref_cmd->data_sn);
  701. return -1;
  702. }
  703. if ((ref_cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
  704. (tmr_req->exp_data_sn <= ref_cmd->acked_data_sn)) {
  705. pr_err("Received ExpDataSN: 0x%08x for READ"
  706. " in TMR TASK_REASSIGN for previously"
  707. " acknowledged DataIN: 0x%08x,"
  708. " protocol error\n", tmr_req->exp_data_sn,
  709. ref_cmd->acked_data_sn);
  710. return -1;
  711. }
  712. return iscsit_task_reassign_prepare_read(tmr_req, conn);
  713. }
  714. /*
  715. * For WRITEs the TMR TASK_REASSIGNs ExpDataSN contains the next R2TSN
  716. * for R2Ts the Initiator is expecting.
  717. *
  718. * Do the magic in iscsit_task_reassign_prepare_write().
  719. */
  720. if (ref_cmd->data_direction == DMA_TO_DEVICE) {
  721. if (tmr_req->exp_data_sn > ref_cmd->r2t_sn) {
  722. pr_err("Received ExpDataSN: 0x%08x for WRITE"
  723. " in TMR TASK_REASSIGN greater than command's"
  724. " R2TSN: 0x%08x.\n", tmr_req->exp_data_sn,
  725. ref_cmd->r2t_sn);
  726. return -1;
  727. }
  728. return iscsit_task_reassign_prepare_write(tmr_req, conn);
  729. }
  730. pr_err("Unknown iSCSI data_direction: 0x%02x\n",
  731. ref_cmd->data_direction);
  732. return -1;
  733. }