iscsi_target_tmr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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. target_execute_cmd(se_cmd);
  235. return 0;
  236. }
  237. cmd->i_state = ISTATE_SEND_STATUS;
  238. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  239. return 0;
  240. }
  241. /*
  242. * Special case to deal with DataSequenceInOrder=No and Non-Immeidate
  243. * Unsolicited DataOut.
  244. */
  245. if (cmd->unsolicited_data) {
  246. cmd->unsolicited_data = 0;
  247. offset = cmd->next_burst_len = cmd->write_data_done;
  248. if ((conn->sess->sess_ops->FirstBurstLength - offset) >=
  249. cmd->se_cmd.data_length) {
  250. no_build_r2ts = 1;
  251. length = (cmd->se_cmd.data_length - offset);
  252. } else
  253. length = (conn->sess->sess_ops->FirstBurstLength - offset);
  254. spin_lock_bh(&cmd->r2t_lock);
  255. if (iscsit_add_r2t_to_list(cmd, offset, length, 0, 0) < 0) {
  256. spin_unlock_bh(&cmd->r2t_lock);
  257. return -1;
  258. }
  259. cmd->outstanding_r2ts++;
  260. spin_unlock_bh(&cmd->r2t_lock);
  261. if (no_build_r2ts)
  262. return 0;
  263. }
  264. /*
  265. * iscsit_build_r2ts_for_cmd() can handle the rest from here.
  266. */
  267. return iscsit_build_r2ts_for_cmd(cmd, conn, true);
  268. }
  269. static int iscsit_task_reassign_complete_read(
  270. struct iscsi_cmd *cmd,
  271. struct iscsi_tmr_req *tmr_req)
  272. {
  273. struct iscsi_conn *conn = cmd->conn;
  274. struct iscsi_datain_req *dr;
  275. struct se_cmd *se_cmd = &cmd->se_cmd;
  276. /*
  277. * The Initiator must not send a Data SNACK with a BegRun less than
  278. * the TMR TASK_REASSIGN's ExpDataSN.
  279. */
  280. if (!tmr_req->exp_data_sn) {
  281. cmd->cmd_flags &= ~ICF_GOT_DATACK_SNACK;
  282. cmd->acked_data_sn = 0;
  283. } else {
  284. cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
  285. cmd->acked_data_sn = (tmr_req->exp_data_sn - 1);
  286. }
  287. if (!(cmd->se_cmd.transport_state & CMD_T_SENT)) {
  288. pr_debug("READ ITT: 0x%08x: t_state: %d never sent to"
  289. " transport\n", cmd->init_task_tag,
  290. cmd->se_cmd.t_state);
  291. transport_handle_cdb_direct(se_cmd);
  292. return 0;
  293. }
  294. if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
  295. pr_err("READ ITT: 0x%08x: t_state: %d, never returned"
  296. " from transport\n", cmd->init_task_tag,
  297. cmd->se_cmd.t_state);
  298. return -1;
  299. }
  300. dr = iscsit_allocate_datain_req();
  301. if (!dr)
  302. return -1;
  303. /*
  304. * The TMR TASK_REASSIGN's ExpDataSN contains the next DataSN the
  305. * Initiator is expecting.
  306. */
  307. dr->data_sn = dr->begrun = tmr_req->exp_data_sn;
  308. dr->runlength = 0;
  309. dr->generate_recovery_values = 1;
  310. dr->recovery = DATAIN_CONNECTION_RECOVERY;
  311. iscsit_attach_datain_req(cmd, dr);
  312. cmd->i_state = ISTATE_SEND_DATAIN;
  313. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  314. return 0;
  315. }
  316. static int iscsit_task_reassign_complete_none(
  317. struct iscsi_cmd *cmd,
  318. struct iscsi_tmr_req *tmr_req)
  319. {
  320. struct iscsi_conn *conn = cmd->conn;
  321. cmd->i_state = ISTATE_SEND_STATUS;
  322. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  323. return 0;
  324. }
  325. static int iscsit_task_reassign_complete_scsi_cmnd(
  326. struct iscsi_tmr_req *tmr_req,
  327. struct iscsi_conn *conn)
  328. {
  329. struct iscsi_cmd *cmd = tmr_req->ref_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_conn_node, &conn->conn_cmd_list);
  346. spin_unlock_bh(&conn->cmd_lock);
  347. if (cmd->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 iscsi_cmd *cmd;
  371. int ret = 0;
  372. if (!tmr_req->ref_cmd) {
  373. pr_err("TMR Request is missing a RefCmd struct iscsi_cmd.\n");
  374. return -1;
  375. }
  376. cmd = tmr_req->ref_cmd;
  377. cmd->conn = conn;
  378. switch (cmd->iscsi_opcode) {
  379. case ISCSI_OP_NOOP_OUT:
  380. ret = iscsit_task_reassign_complete_nop_out(tmr_req, conn);
  381. break;
  382. case ISCSI_OP_SCSI_CMD:
  383. ret = iscsit_task_reassign_complete_scsi_cmnd(tmr_req, conn);
  384. break;
  385. default:
  386. pr_err("Illegal iSCSI Opcode 0x%02x during"
  387. " command realligence\n", cmd->iscsi_opcode);
  388. return -1;
  389. }
  390. if (ret != 0)
  391. return ret;
  392. pr_debug("Completed connection realligence for Opcode: 0x%02x,"
  393. " ITT: 0x%08x to CID: %hu.\n", cmd->iscsi_opcode,
  394. cmd->init_task_tag, conn->cid);
  395. return 0;
  396. }
  397. /*
  398. * Handles special after-the-fact actions related to TMRs.
  399. * Right now the only one that its really needed for is
  400. * connection recovery releated TASK_REASSIGN.
  401. */
  402. extern int iscsit_tmr_post_handler(struct iscsi_cmd *cmd, struct iscsi_conn *conn)
  403. {
  404. struct iscsi_tmr_req *tmr_req = cmd->tmr_req;
  405. struct se_tmr_req *se_tmr = cmd->se_cmd.se_tmr_req;
  406. if (tmr_req->task_reassign &&
  407. (se_tmr->response == ISCSI_TMF_RSP_COMPLETE))
  408. return iscsit_task_reassign_complete(tmr_req, conn);
  409. return 0;
  410. }
  411. /*
  412. * Nothing to do here, but leave it for good measure. :-)
  413. */
  414. int iscsit_task_reassign_prepare_read(
  415. struct iscsi_tmr_req *tmr_req,
  416. struct iscsi_conn *conn)
  417. {
  418. return 0;
  419. }
  420. static void iscsit_task_reassign_prepare_unsolicited_dataout(
  421. struct iscsi_cmd *cmd,
  422. struct iscsi_conn *conn)
  423. {
  424. int i, j;
  425. struct iscsi_pdu *pdu = NULL;
  426. struct iscsi_seq *seq = NULL;
  427. if (conn->sess->sess_ops->DataSequenceInOrder) {
  428. cmd->data_sn = 0;
  429. if (cmd->immediate_data)
  430. cmd->r2t_offset += (cmd->first_burst_len -
  431. cmd->seq_start_offset);
  432. if (conn->sess->sess_ops->DataPDUInOrder) {
  433. cmd->write_data_done -= (cmd->immediate_data) ?
  434. (cmd->first_burst_len -
  435. cmd->seq_start_offset) :
  436. cmd->first_burst_len;
  437. cmd->first_burst_len = 0;
  438. return;
  439. }
  440. for (i = 0; i < cmd->pdu_count; i++) {
  441. pdu = &cmd->pdu_list[i];
  442. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  443. continue;
  444. if ((pdu->offset >= cmd->seq_start_offset) &&
  445. ((pdu->offset + pdu->length) <=
  446. cmd->seq_end_offset)) {
  447. cmd->first_burst_len -= pdu->length;
  448. cmd->write_data_done -= pdu->length;
  449. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  450. }
  451. }
  452. } else {
  453. for (i = 0; i < cmd->seq_count; i++) {
  454. seq = &cmd->seq_list[i];
  455. if (seq->type != SEQTYPE_UNSOLICITED)
  456. continue;
  457. cmd->write_data_done -=
  458. (seq->offset - seq->orig_offset);
  459. cmd->first_burst_len = 0;
  460. seq->data_sn = 0;
  461. seq->offset = seq->orig_offset;
  462. seq->next_burst_len = 0;
  463. seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
  464. if (conn->sess->sess_ops->DataPDUInOrder)
  465. continue;
  466. for (j = 0; j < seq->pdu_count; j++) {
  467. pdu = &cmd->pdu_list[j+seq->pdu_start];
  468. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  469. continue;
  470. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  471. }
  472. }
  473. }
  474. }
  475. int iscsit_task_reassign_prepare_write(
  476. struct iscsi_tmr_req *tmr_req,
  477. struct iscsi_conn *conn)
  478. {
  479. struct iscsi_cmd *cmd = tmr_req->ref_cmd;
  480. struct iscsi_pdu *pdu = NULL;
  481. struct iscsi_r2t *r2t = NULL, *r2t_tmp;
  482. int first_incomplete_r2t = 1, i = 0;
  483. /*
  484. * The command was in the process of receiving Unsolicited DataOUT when
  485. * the connection failed.
  486. */
  487. if (cmd->unsolicited_data)
  488. iscsit_task_reassign_prepare_unsolicited_dataout(cmd, conn);
  489. /*
  490. * The Initiator is requesting R2Ts starting from zero, skip
  491. * checking acknowledged R2Ts and start checking struct iscsi_r2ts
  492. * greater than zero.
  493. */
  494. if (!tmr_req->exp_data_sn)
  495. goto drop_unacknowledged_r2ts;
  496. /*
  497. * We now check that the PDUs in DataOUT sequences below
  498. * the TMR TASK_REASSIGN ExpDataSN (R2TSN the Initiator is
  499. * expecting next) have all the DataOUT they require to complete
  500. * the DataOUT sequence. First scan from R2TSN 0 to TMR
  501. * TASK_REASSIGN ExpDataSN-1.
  502. *
  503. * If we have not received all DataOUT in question, we must
  504. * make sure to make the appropriate changes to values in
  505. * struct iscsi_cmd (and elsewhere depending on session parameters)
  506. * so iscsit_build_r2ts_for_cmd() in iscsit_task_reassign_complete_write()
  507. * will resend a new R2T for the DataOUT sequences in question.
  508. */
  509. spin_lock_bh(&cmd->r2t_lock);
  510. if (list_empty(&cmd->cmd_r2t_list)) {
  511. spin_unlock_bh(&cmd->r2t_lock);
  512. return -1;
  513. }
  514. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  515. if (r2t->r2t_sn >= tmr_req->exp_data_sn)
  516. continue;
  517. /*
  518. * Safely ignore Recovery R2Ts and R2Ts that have completed
  519. * DataOUT sequences.
  520. */
  521. if (r2t->seq_complete)
  522. continue;
  523. if (r2t->recovery_r2t)
  524. continue;
  525. /*
  526. * DataSequenceInOrder=Yes:
  527. *
  528. * Taking into account the iSCSI implementation requirement of
  529. * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
  530. * DataSequenceInOrder=Yes, we must take into consideration
  531. * the following:
  532. *
  533. * DataSequenceInOrder=No:
  534. *
  535. * Taking into account that the Initiator controls the (possibly
  536. * random) PDU Order in (possibly random) Sequence Order of
  537. * DataOUT the target requests with R2Ts, we must take into
  538. * consideration the following:
  539. *
  540. * DataPDUInOrder=Yes for DataSequenceInOrder=[Yes,No]:
  541. *
  542. * While processing non-complete R2T DataOUT sequence requests
  543. * the Target will re-request only the total sequence length
  544. * minus current received offset. This is because we must
  545. * assume the initiator will continue sending DataOUT from the
  546. * last PDU before the connection failed.
  547. *
  548. * DataPDUInOrder=No for DataSequenceInOrder=[Yes,No]:
  549. *
  550. * While processing non-complete R2T DataOUT sequence requests
  551. * the Target will re-request the entire DataOUT sequence if
  552. * any single PDU is missing from the sequence. This is because
  553. * we have no logical method to determine the next PDU offset,
  554. * and we must assume the Initiator will be sending any random
  555. * PDU offset in the current sequence after TASK_REASSIGN
  556. * has completed.
  557. */
  558. if (conn->sess->sess_ops->DataSequenceInOrder) {
  559. if (!first_incomplete_r2t) {
  560. cmd->r2t_offset -= r2t->xfer_len;
  561. goto next;
  562. }
  563. if (conn->sess->sess_ops->DataPDUInOrder) {
  564. cmd->data_sn = 0;
  565. cmd->r2t_offset -= (r2t->xfer_len -
  566. cmd->next_burst_len);
  567. first_incomplete_r2t = 0;
  568. goto next;
  569. }
  570. cmd->data_sn = 0;
  571. cmd->r2t_offset -= r2t->xfer_len;
  572. for (i = 0; i < cmd->pdu_count; i++) {
  573. pdu = &cmd->pdu_list[i];
  574. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  575. continue;
  576. if ((pdu->offset >= r2t->offset) &&
  577. (pdu->offset < (r2t->offset +
  578. r2t->xfer_len))) {
  579. cmd->next_burst_len -= pdu->length;
  580. cmd->write_data_done -= pdu->length;
  581. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  582. }
  583. }
  584. first_incomplete_r2t = 0;
  585. } else {
  586. struct iscsi_seq *seq;
  587. seq = iscsit_get_seq_holder(cmd, r2t->offset,
  588. r2t->xfer_len);
  589. if (!seq) {
  590. spin_unlock_bh(&cmd->r2t_lock);
  591. return -1;
  592. }
  593. cmd->write_data_done -=
  594. (seq->offset - seq->orig_offset);
  595. seq->data_sn = 0;
  596. seq->offset = seq->orig_offset;
  597. seq->next_burst_len = 0;
  598. seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
  599. cmd->seq_send_order--;
  600. if (conn->sess->sess_ops->DataPDUInOrder)
  601. goto next;
  602. for (i = 0; i < seq->pdu_count; i++) {
  603. pdu = &cmd->pdu_list[i+seq->pdu_start];
  604. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  605. continue;
  606. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  607. }
  608. }
  609. next:
  610. cmd->outstanding_r2ts--;
  611. }
  612. spin_unlock_bh(&cmd->r2t_lock);
  613. /*
  614. * We now drop all unacknowledged R2Ts, ie: ExpDataSN from TMR
  615. * TASK_REASSIGN to the last R2T in the list.. We are also careful
  616. * to check that the Initiator is not requesting R2Ts for DataOUT
  617. * sequences it has already completed.
  618. *
  619. * Free each R2T in question and adjust values in struct iscsi_cmd
  620. * accordingly so iscsit_build_r2ts_for_cmd() do the rest of
  621. * the work after the TMR TASK_REASSIGN Response is sent.
  622. */
  623. drop_unacknowledged_r2ts:
  624. cmd->cmd_flags &= ~ICF_SENT_LAST_R2T;
  625. cmd->r2t_sn = tmr_req->exp_data_sn;
  626. spin_lock_bh(&cmd->r2t_lock);
  627. list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list) {
  628. /*
  629. * Skip up to the R2T Sequence number provided by the
  630. * iSCSI TASK_REASSIGN TMR
  631. */
  632. if (r2t->r2t_sn < tmr_req->exp_data_sn)
  633. continue;
  634. if (r2t->seq_complete) {
  635. pr_err("Initiator is requesting R2Ts from"
  636. " R2TSN: 0x%08x, but R2TSN: 0x%08x, Offset: %u,"
  637. " Length: %u is already complete."
  638. " BAD INITIATOR ERL=2 IMPLEMENTATION!\n",
  639. tmr_req->exp_data_sn, r2t->r2t_sn,
  640. r2t->offset, r2t->xfer_len);
  641. spin_unlock_bh(&cmd->r2t_lock);
  642. return -1;
  643. }
  644. if (r2t->recovery_r2t) {
  645. iscsit_free_r2t(r2t, cmd);
  646. continue;
  647. }
  648. /* DataSequenceInOrder=Yes:
  649. *
  650. * Taking into account the iSCSI implementation requirement of
  651. * MaxOutstandingR2T=1 while ErrorRecoveryLevel>0 and
  652. * DataSequenceInOrder=Yes, it's safe to subtract the R2Ts
  653. * entire transfer length from the commands R2T offset marker.
  654. *
  655. * DataSequenceInOrder=No:
  656. *
  657. * We subtract the difference from struct iscsi_seq between the
  658. * current offset and original offset from cmd->write_data_done
  659. * for account for DataOUT PDUs already received. Then reset
  660. * the current offset to the original and zero out the current
  661. * burst length, to make sure we re-request the entire DataOUT
  662. * sequence.
  663. */
  664. if (conn->sess->sess_ops->DataSequenceInOrder)
  665. cmd->r2t_offset -= r2t->xfer_len;
  666. else
  667. cmd->seq_send_order--;
  668. cmd->outstanding_r2ts--;
  669. iscsit_free_r2t(r2t, cmd);
  670. }
  671. spin_unlock_bh(&cmd->r2t_lock);
  672. return 0;
  673. }
  674. /*
  675. * Performs sanity checks TMR TASK_REASSIGN's ExpDataSN for
  676. * a given struct iscsi_cmd.
  677. */
  678. int iscsit_check_task_reassign_expdatasn(
  679. struct iscsi_tmr_req *tmr_req,
  680. struct iscsi_conn *conn)
  681. {
  682. struct iscsi_cmd *ref_cmd = tmr_req->ref_cmd;
  683. if (ref_cmd->iscsi_opcode != ISCSI_OP_SCSI_CMD)
  684. return 0;
  685. if (ref_cmd->se_cmd.se_cmd_flags & SCF_SENT_CHECK_CONDITION)
  686. return 0;
  687. if (ref_cmd->data_direction == DMA_NONE)
  688. return 0;
  689. /*
  690. * For READs the TMR TASK_REASSIGNs ExpDataSN contains the next DataSN
  691. * of DataIN the Initiator is expecting.
  692. *
  693. * Also check that the Initiator is not re-requesting DataIN that has
  694. * already been acknowledged with a DataAck SNACK.
  695. */
  696. if (ref_cmd->data_direction == DMA_FROM_DEVICE) {
  697. if (tmr_req->exp_data_sn > ref_cmd->data_sn) {
  698. pr_err("Received ExpDataSN: 0x%08x for READ"
  699. " in TMR TASK_REASSIGN greater than command's"
  700. " DataSN: 0x%08x.\n", tmr_req->exp_data_sn,
  701. ref_cmd->data_sn);
  702. return -1;
  703. }
  704. if ((ref_cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
  705. (tmr_req->exp_data_sn <= ref_cmd->acked_data_sn)) {
  706. pr_err("Received ExpDataSN: 0x%08x for READ"
  707. " in TMR TASK_REASSIGN for previously"
  708. " acknowledged DataIN: 0x%08x,"
  709. " protocol error\n", tmr_req->exp_data_sn,
  710. ref_cmd->acked_data_sn);
  711. return -1;
  712. }
  713. return iscsit_task_reassign_prepare_read(tmr_req, conn);
  714. }
  715. /*
  716. * For WRITEs the TMR TASK_REASSIGNs ExpDataSN contains the next R2TSN
  717. * for R2Ts the Initiator is expecting.
  718. *
  719. * Do the magic in iscsit_task_reassign_prepare_write().
  720. */
  721. if (ref_cmd->data_direction == DMA_TO_DEVICE) {
  722. if (tmr_req->exp_data_sn > ref_cmd->r2t_sn) {
  723. pr_err("Received ExpDataSN: 0x%08x for WRITE"
  724. " in TMR TASK_REASSIGN greater than command's"
  725. " R2TSN: 0x%08x.\n", tmr_req->exp_data_sn,
  726. ref_cmd->r2t_sn);
  727. return -1;
  728. }
  729. return iscsit_task_reassign_prepare_write(tmr_req, conn);
  730. }
  731. pr_err("Unknown iSCSI data_direction: 0x%02x\n",
  732. ref_cmd->data_direction);
  733. return -1;
  734. }