iscsi_target_erl1.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. /*******************************************************************************
  2. * This file contains error recovery level one used by the iSCSI Target driver.
  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 <linux/list.h>
  21. #include <scsi/iscsi_proto.h>
  22. #include <target/target_core_base.h>
  23. #include <target/target_core_fabric.h>
  24. #include "iscsi_target_core.h"
  25. #include "iscsi_target_seq_pdu_list.h"
  26. #include "iscsi_target_datain_values.h"
  27. #include "iscsi_target_device.h"
  28. #include "iscsi_target_tpg.h"
  29. #include "iscsi_target_util.h"
  30. #include "iscsi_target_erl0.h"
  31. #include "iscsi_target_erl1.h"
  32. #include "iscsi_target_erl2.h"
  33. #include "iscsi_target.h"
  34. #define OFFLOAD_BUF_SIZE 32768
  35. /*
  36. * Used to dump excess datain payload for certain error recovery
  37. * situations. Receive in OFFLOAD_BUF_SIZE max of datain per rx_data().
  38. *
  39. * dump_padding_digest denotes if padding and data digests need
  40. * to be dumped.
  41. */
  42. int iscsit_dump_data_payload(
  43. struct iscsi_conn *conn,
  44. u32 buf_len,
  45. int dump_padding_digest)
  46. {
  47. char *buf, pad_bytes[4];
  48. int ret = DATAOUT_WITHIN_COMMAND_RECOVERY, rx_got;
  49. u32 length, padding, offset = 0, size;
  50. struct kvec iov;
  51. length = (buf_len > OFFLOAD_BUF_SIZE) ? OFFLOAD_BUF_SIZE : buf_len;
  52. buf = kzalloc(length, GFP_ATOMIC);
  53. if (!buf) {
  54. pr_err("Unable to allocate %u bytes for offload"
  55. " buffer.\n", length);
  56. return -1;
  57. }
  58. memset(&iov, 0, sizeof(struct kvec));
  59. while (offset < buf_len) {
  60. size = ((offset + length) > buf_len) ?
  61. (buf_len - offset) : length;
  62. iov.iov_len = size;
  63. iov.iov_base = buf;
  64. rx_got = rx_data(conn, &iov, 1, size);
  65. if (rx_got != size) {
  66. ret = DATAOUT_CANNOT_RECOVER;
  67. goto out;
  68. }
  69. offset += size;
  70. }
  71. if (!dump_padding_digest)
  72. goto out;
  73. padding = ((-buf_len) & 3);
  74. if (padding != 0) {
  75. iov.iov_len = padding;
  76. iov.iov_base = pad_bytes;
  77. rx_got = rx_data(conn, &iov, 1, padding);
  78. if (rx_got != padding) {
  79. ret = DATAOUT_CANNOT_RECOVER;
  80. goto out;
  81. }
  82. }
  83. if (conn->conn_ops->DataDigest) {
  84. u32 data_crc;
  85. iov.iov_len = ISCSI_CRC_LEN;
  86. iov.iov_base = &data_crc;
  87. rx_got = rx_data(conn, &iov, 1, ISCSI_CRC_LEN);
  88. if (rx_got != ISCSI_CRC_LEN) {
  89. ret = DATAOUT_CANNOT_RECOVER;
  90. goto out;
  91. }
  92. }
  93. out:
  94. kfree(buf);
  95. return ret;
  96. }
  97. /*
  98. * Used for retransmitting R2Ts from a R2T SNACK request.
  99. */
  100. static int iscsit_send_recovery_r2t_for_snack(
  101. struct iscsi_cmd *cmd,
  102. struct iscsi_r2t *r2t)
  103. {
  104. /*
  105. * If the struct iscsi_r2t has not been sent yet, we can safely
  106. * ignore retransmission
  107. * of the R2TSN in question.
  108. */
  109. spin_lock_bh(&cmd->r2t_lock);
  110. if (!r2t->sent_r2t) {
  111. spin_unlock_bh(&cmd->r2t_lock);
  112. return 0;
  113. }
  114. r2t->sent_r2t = 0;
  115. spin_unlock_bh(&cmd->r2t_lock);
  116. iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
  117. return 0;
  118. }
  119. static int iscsit_handle_r2t_snack(
  120. struct iscsi_cmd *cmd,
  121. unsigned char *buf,
  122. u32 begrun,
  123. u32 runlength)
  124. {
  125. u32 last_r2tsn;
  126. struct iscsi_r2t *r2t;
  127. /*
  128. * Make sure the initiator is not requesting retransmission
  129. * of R2TSNs already acknowledged by a TMR TASK_REASSIGN.
  130. */
  131. if ((cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
  132. (begrun <= cmd->acked_data_sn)) {
  133. pr_err("ITT: 0x%08x, R2T SNACK requesting"
  134. " retransmission of R2TSN: 0x%08x to 0x%08x but already"
  135. " acked to R2TSN: 0x%08x by TMR TASK_REASSIGN,"
  136. " protocol error.\n", cmd->init_task_tag, begrun,
  137. (begrun + runlength), cmd->acked_data_sn);
  138. return iscsit_add_reject_from_cmd(
  139. ISCSI_REASON_PROTOCOL_ERROR,
  140. 1, 0, buf, cmd);
  141. }
  142. if (runlength) {
  143. if ((begrun + runlength) > cmd->r2t_sn) {
  144. pr_err("Command ITT: 0x%08x received R2T SNACK"
  145. " with BegRun: 0x%08x, RunLength: 0x%08x, exceeds"
  146. " current R2TSN: 0x%08x, protocol error.\n",
  147. cmd->init_task_tag, begrun, runlength, cmd->r2t_sn);
  148. return iscsit_add_reject_from_cmd(
  149. ISCSI_REASON_BOOKMARK_INVALID, 1, 0, buf, cmd);
  150. }
  151. last_r2tsn = (begrun + runlength);
  152. } else
  153. last_r2tsn = cmd->r2t_sn;
  154. while (begrun < last_r2tsn) {
  155. r2t = iscsit_get_holder_for_r2tsn(cmd, begrun);
  156. if (!r2t)
  157. return -1;
  158. if (iscsit_send_recovery_r2t_for_snack(cmd, r2t) < 0)
  159. return -1;
  160. begrun++;
  161. }
  162. return 0;
  163. }
  164. /*
  165. * Generates Offsets and NextBurstLength based on Begrun and Runlength
  166. * carried in a Data SNACK or ExpDataSN in TMR TASK_REASSIGN.
  167. *
  168. * For DataSequenceInOrder=Yes and DataPDUInOrder=[Yes,No] only.
  169. *
  170. * FIXME: How is this handled for a RData SNACK?
  171. */
  172. int iscsit_create_recovery_datain_values_datasequenceinorder_yes(
  173. struct iscsi_cmd *cmd,
  174. struct iscsi_datain_req *dr)
  175. {
  176. u32 data_sn = 0, data_sn_count = 0;
  177. u32 pdu_start = 0, seq_no = 0;
  178. u32 begrun = dr->begrun;
  179. struct iscsi_conn *conn = cmd->conn;
  180. while (begrun > data_sn++) {
  181. data_sn_count++;
  182. if ((dr->next_burst_len +
  183. conn->conn_ops->MaxRecvDataSegmentLength) <
  184. conn->sess->sess_ops->MaxBurstLength) {
  185. dr->read_data_done +=
  186. conn->conn_ops->MaxRecvDataSegmentLength;
  187. dr->next_burst_len +=
  188. conn->conn_ops->MaxRecvDataSegmentLength;
  189. } else {
  190. dr->read_data_done +=
  191. (conn->sess->sess_ops->MaxBurstLength -
  192. dr->next_burst_len);
  193. dr->next_burst_len = 0;
  194. pdu_start += data_sn_count;
  195. data_sn_count = 0;
  196. seq_no++;
  197. }
  198. }
  199. if (!conn->sess->sess_ops->DataPDUInOrder) {
  200. cmd->seq_no = seq_no;
  201. cmd->pdu_start = pdu_start;
  202. cmd->pdu_send_order = data_sn_count;
  203. }
  204. return 0;
  205. }
  206. /*
  207. * Generates Offsets and NextBurstLength based on Begrun and Runlength
  208. * carried in a Data SNACK or ExpDataSN in TMR TASK_REASSIGN.
  209. *
  210. * For DataSequenceInOrder=No and DataPDUInOrder=[Yes,No] only.
  211. *
  212. * FIXME: How is this handled for a RData SNACK?
  213. */
  214. int iscsit_create_recovery_datain_values_datasequenceinorder_no(
  215. struct iscsi_cmd *cmd,
  216. struct iscsi_datain_req *dr)
  217. {
  218. int found_seq = 0, i;
  219. u32 data_sn, read_data_done = 0, seq_send_order = 0;
  220. u32 begrun = dr->begrun;
  221. u32 runlength = dr->runlength;
  222. struct iscsi_conn *conn = cmd->conn;
  223. struct iscsi_seq *first_seq = NULL, *seq = NULL;
  224. if (!cmd->seq_list) {
  225. pr_err("struct iscsi_cmd->seq_list is NULL!\n");
  226. return -1;
  227. }
  228. /*
  229. * Calculate read_data_done for all sequences containing a
  230. * first_datasn and last_datasn less than the BegRun.
  231. *
  232. * Locate the struct iscsi_seq the BegRun lies within and calculate
  233. * NextBurstLenghth up to the DataSN based on MaxRecvDataSegmentLength.
  234. *
  235. * Also use struct iscsi_seq->seq_send_order to determine where to start.
  236. */
  237. for (i = 0; i < cmd->seq_count; i++) {
  238. seq = &cmd->seq_list[i];
  239. if (!seq->seq_send_order)
  240. first_seq = seq;
  241. /*
  242. * No data has been transferred for this DataIN sequence, so the
  243. * seq->first_datasn and seq->last_datasn have not been set.
  244. */
  245. if (!seq->sent) {
  246. pr_err("Ignoring non-sent sequence 0x%08x ->"
  247. " 0x%08x\n\n", seq->first_datasn,
  248. seq->last_datasn);
  249. continue;
  250. }
  251. /*
  252. * This DataIN sequence is precedes the received BegRun, add the
  253. * total xfer_len of the sequence to read_data_done and reset
  254. * seq->pdu_send_order.
  255. */
  256. if ((seq->first_datasn < begrun) &&
  257. (seq->last_datasn < begrun)) {
  258. pr_err("Pre BegRun sequence 0x%08x ->"
  259. " 0x%08x\n", seq->first_datasn,
  260. seq->last_datasn);
  261. read_data_done += cmd->seq_list[i].xfer_len;
  262. seq->next_burst_len = seq->pdu_send_order = 0;
  263. continue;
  264. }
  265. /*
  266. * The BegRun lies within this DataIN sequence.
  267. */
  268. if ((seq->first_datasn <= begrun) &&
  269. (seq->last_datasn >= begrun)) {
  270. pr_err("Found sequence begrun: 0x%08x in"
  271. " 0x%08x -> 0x%08x\n", begrun,
  272. seq->first_datasn, seq->last_datasn);
  273. seq_send_order = seq->seq_send_order;
  274. data_sn = seq->first_datasn;
  275. seq->next_burst_len = seq->pdu_send_order = 0;
  276. found_seq = 1;
  277. /*
  278. * For DataPDUInOrder=Yes, while the first DataSN of
  279. * the sequence is less than the received BegRun, add
  280. * the MaxRecvDataSegmentLength to read_data_done and
  281. * to the sequence's next_burst_len;
  282. *
  283. * For DataPDUInOrder=No, while the first DataSN of the
  284. * sequence is less than the received BegRun, find the
  285. * struct iscsi_pdu of the DataSN in question and add the
  286. * MaxRecvDataSegmentLength to read_data_done and to the
  287. * sequence's next_burst_len;
  288. */
  289. if (conn->sess->sess_ops->DataPDUInOrder) {
  290. while (data_sn < begrun) {
  291. seq->pdu_send_order++;
  292. read_data_done +=
  293. conn->conn_ops->MaxRecvDataSegmentLength;
  294. seq->next_burst_len +=
  295. conn->conn_ops->MaxRecvDataSegmentLength;
  296. data_sn++;
  297. }
  298. } else {
  299. int j;
  300. struct iscsi_pdu *pdu;
  301. while (data_sn < begrun) {
  302. seq->pdu_send_order++;
  303. for (j = 0; j < seq->pdu_count; j++) {
  304. pdu = &cmd->pdu_list[
  305. seq->pdu_start + j];
  306. if (pdu->data_sn == data_sn) {
  307. read_data_done +=
  308. pdu->length;
  309. seq->next_burst_len +=
  310. pdu->length;
  311. }
  312. }
  313. data_sn++;
  314. }
  315. }
  316. continue;
  317. }
  318. /*
  319. * This DataIN sequence is larger than the received BegRun,
  320. * reset seq->pdu_send_order and continue.
  321. */
  322. if ((seq->first_datasn > begrun) ||
  323. (seq->last_datasn > begrun)) {
  324. pr_err("Post BegRun sequence 0x%08x -> 0x%08x\n",
  325. seq->first_datasn, seq->last_datasn);
  326. seq->next_burst_len = seq->pdu_send_order = 0;
  327. continue;
  328. }
  329. }
  330. if (!found_seq) {
  331. if (!begrun) {
  332. if (!first_seq) {
  333. pr_err("ITT: 0x%08x, Begrun: 0x%08x"
  334. " but first_seq is NULL\n",
  335. cmd->init_task_tag, begrun);
  336. return -1;
  337. }
  338. seq_send_order = first_seq->seq_send_order;
  339. seq->next_burst_len = seq->pdu_send_order = 0;
  340. goto done;
  341. }
  342. pr_err("Unable to locate struct iscsi_seq for ITT: 0x%08x,"
  343. " BegRun: 0x%08x, RunLength: 0x%08x while"
  344. " DataSequenceInOrder=No and DataPDUInOrder=%s.\n",
  345. cmd->init_task_tag, begrun, runlength,
  346. (conn->sess->sess_ops->DataPDUInOrder) ? "Yes" : "No");
  347. return -1;
  348. }
  349. done:
  350. dr->read_data_done = read_data_done;
  351. dr->seq_send_order = seq_send_order;
  352. return 0;
  353. }
  354. static int iscsit_handle_recovery_datain(
  355. struct iscsi_cmd *cmd,
  356. unsigned char *buf,
  357. u32 begrun,
  358. u32 runlength)
  359. {
  360. struct iscsi_conn *conn = cmd->conn;
  361. struct iscsi_datain_req *dr;
  362. struct se_cmd *se_cmd = &cmd->se_cmd;
  363. if (!(se_cmd->transport_state & CMD_T_COMPLETE)) {
  364. pr_err("Ignoring ITT: 0x%08x Data SNACK\n",
  365. cmd->init_task_tag);
  366. return 0;
  367. }
  368. /*
  369. * Make sure the initiator is not requesting retransmission
  370. * of DataSNs already acknowledged by a Data ACK SNACK.
  371. */
  372. if ((cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
  373. (begrun <= cmd->acked_data_sn)) {
  374. pr_err("ITT: 0x%08x, Data SNACK requesting"
  375. " retransmission of DataSN: 0x%08x to 0x%08x but"
  376. " already acked to DataSN: 0x%08x by Data ACK SNACK,"
  377. " protocol error.\n", cmd->init_task_tag, begrun,
  378. (begrun + runlength), cmd->acked_data_sn);
  379. return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
  380. 1, 0, buf, cmd);
  381. }
  382. /*
  383. * Make sure BegRun and RunLength in the Data SNACK are sane.
  384. * Note: (cmd->data_sn - 1) will carry the maximum DataSN sent.
  385. */
  386. if ((begrun + runlength) > (cmd->data_sn - 1)) {
  387. pr_err("Initiator requesting BegRun: 0x%08x, RunLength"
  388. ": 0x%08x greater than maximum DataSN: 0x%08x.\n",
  389. begrun, runlength, (cmd->data_sn - 1));
  390. return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
  391. 1, 0, buf, cmd);
  392. }
  393. dr = iscsit_allocate_datain_req();
  394. if (!dr)
  395. return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
  396. 1, 0, buf, cmd);
  397. dr->data_sn = dr->begrun = begrun;
  398. dr->runlength = runlength;
  399. dr->generate_recovery_values = 1;
  400. dr->recovery = DATAIN_WITHIN_COMMAND_RECOVERY;
  401. iscsit_attach_datain_req(cmd, dr);
  402. cmd->i_state = ISTATE_SEND_DATAIN;
  403. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  404. return 0;
  405. }
  406. int iscsit_handle_recovery_datain_or_r2t(
  407. struct iscsi_conn *conn,
  408. unsigned char *buf,
  409. u32 init_task_tag,
  410. u32 targ_xfer_tag,
  411. u32 begrun,
  412. u32 runlength)
  413. {
  414. struct iscsi_cmd *cmd;
  415. cmd = iscsit_find_cmd_from_itt(conn, init_task_tag);
  416. if (!cmd)
  417. return 0;
  418. /*
  419. * FIXME: This will not work for bidi commands.
  420. */
  421. switch (cmd->data_direction) {
  422. case DMA_TO_DEVICE:
  423. return iscsit_handle_r2t_snack(cmd, buf, begrun, runlength);
  424. case DMA_FROM_DEVICE:
  425. return iscsit_handle_recovery_datain(cmd, buf, begrun,
  426. runlength);
  427. default:
  428. pr_err("Unknown cmd->data_direction: 0x%02x\n",
  429. cmd->data_direction);
  430. return -1;
  431. }
  432. return 0;
  433. }
  434. /* #warning FIXME: Status SNACK needs to be dependent on OPCODE!!! */
  435. int iscsit_handle_status_snack(
  436. struct iscsi_conn *conn,
  437. u32 init_task_tag,
  438. u32 targ_xfer_tag,
  439. u32 begrun,
  440. u32 runlength)
  441. {
  442. struct iscsi_cmd *cmd = NULL;
  443. u32 last_statsn;
  444. int found_cmd;
  445. if (conn->exp_statsn > begrun) {
  446. pr_err("Got Status SNACK Begrun: 0x%08x, RunLength:"
  447. " 0x%08x but already got ExpStatSN: 0x%08x on CID:"
  448. " %hu.\n", begrun, runlength, conn->exp_statsn,
  449. conn->cid);
  450. return 0;
  451. }
  452. last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);
  453. while (begrun < last_statsn) {
  454. found_cmd = 0;
  455. spin_lock_bh(&conn->cmd_lock);
  456. list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
  457. if (cmd->stat_sn == begrun) {
  458. found_cmd = 1;
  459. break;
  460. }
  461. }
  462. spin_unlock_bh(&conn->cmd_lock);
  463. if (!found_cmd) {
  464. pr_err("Unable to find StatSN: 0x%08x for"
  465. " a Status SNACK, assuming this was a"
  466. " protactic SNACK for an untransmitted"
  467. " StatSN, ignoring.\n", begrun);
  468. begrun++;
  469. continue;
  470. }
  471. spin_lock_bh(&cmd->istate_lock);
  472. if (cmd->i_state == ISTATE_SEND_DATAIN) {
  473. spin_unlock_bh(&cmd->istate_lock);
  474. pr_err("Ignoring Status SNACK for BegRun:"
  475. " 0x%08x, RunLength: 0x%08x, assuming this was"
  476. " a protactic SNACK for an untransmitted"
  477. " StatSN\n", begrun, runlength);
  478. begrun++;
  479. continue;
  480. }
  481. spin_unlock_bh(&cmd->istate_lock);
  482. cmd->i_state = ISTATE_SEND_STATUS_RECOVERY;
  483. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  484. begrun++;
  485. }
  486. return 0;
  487. }
  488. int iscsit_handle_data_ack(
  489. struct iscsi_conn *conn,
  490. u32 targ_xfer_tag,
  491. u32 begrun,
  492. u32 runlength)
  493. {
  494. struct iscsi_cmd *cmd = NULL;
  495. cmd = iscsit_find_cmd_from_ttt(conn, targ_xfer_tag);
  496. if (!cmd) {
  497. pr_err("Data ACK SNACK for TTT: 0x%08x is"
  498. " invalid.\n", targ_xfer_tag);
  499. return -1;
  500. }
  501. if (begrun <= cmd->acked_data_sn) {
  502. pr_err("ITT: 0x%08x Data ACK SNACK BegRUN: 0x%08x is"
  503. " less than the already acked DataSN: 0x%08x.\n",
  504. cmd->init_task_tag, begrun, cmd->acked_data_sn);
  505. return -1;
  506. }
  507. /*
  508. * For Data ACK SNACK, BegRun is the next expected DataSN.
  509. * (see iSCSI v19: 10.16.6)
  510. */
  511. cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
  512. cmd->acked_data_sn = (begrun - 1);
  513. pr_debug("Received Data ACK SNACK for ITT: 0x%08x,"
  514. " updated acked DataSN to 0x%08x.\n",
  515. cmd->init_task_tag, cmd->acked_data_sn);
  516. return 0;
  517. }
  518. static int iscsit_send_recovery_r2t(
  519. struct iscsi_cmd *cmd,
  520. u32 offset,
  521. u32 xfer_len)
  522. {
  523. int ret;
  524. spin_lock_bh(&cmd->r2t_lock);
  525. ret = iscsit_add_r2t_to_list(cmd, offset, xfer_len, 1, 0);
  526. spin_unlock_bh(&cmd->r2t_lock);
  527. return ret;
  528. }
  529. int iscsit_dataout_datapduinorder_no_fbit(
  530. struct iscsi_cmd *cmd,
  531. struct iscsi_pdu *pdu)
  532. {
  533. int i, send_recovery_r2t = 0, recovery = 0;
  534. u32 length = 0, offset = 0, pdu_count = 0, xfer_len = 0;
  535. struct iscsi_conn *conn = cmd->conn;
  536. struct iscsi_pdu *first_pdu = NULL;
  537. /*
  538. * Get an struct iscsi_pdu pointer to the first PDU, and total PDU count
  539. * of the DataOUT sequence.
  540. */
  541. if (conn->sess->sess_ops->DataSequenceInOrder) {
  542. for (i = 0; i < cmd->pdu_count; i++) {
  543. if (cmd->pdu_list[i].seq_no == pdu->seq_no) {
  544. if (!first_pdu)
  545. first_pdu = &cmd->pdu_list[i];
  546. xfer_len += cmd->pdu_list[i].length;
  547. pdu_count++;
  548. } else if (pdu_count)
  549. break;
  550. }
  551. } else {
  552. struct iscsi_seq *seq = cmd->seq_ptr;
  553. first_pdu = &cmd->pdu_list[seq->pdu_start];
  554. pdu_count = seq->pdu_count;
  555. }
  556. if (!first_pdu || !pdu_count)
  557. return DATAOUT_CANNOT_RECOVER;
  558. /*
  559. * Loop through the ending DataOUT Sequence checking each struct iscsi_pdu.
  560. * The following ugly logic does batching of not received PDUs.
  561. */
  562. for (i = 0; i < pdu_count; i++) {
  563. if (first_pdu[i].status == ISCSI_PDU_RECEIVED_OK) {
  564. if (!send_recovery_r2t)
  565. continue;
  566. if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
  567. return DATAOUT_CANNOT_RECOVER;
  568. send_recovery_r2t = length = offset = 0;
  569. continue;
  570. }
  571. /*
  572. * Set recovery = 1 for any missing, CRC failed, or timed
  573. * out PDUs to let the DataOUT logic know that this sequence
  574. * has not been completed yet.
  575. *
  576. * Also, only send a Recovery R2T for ISCSI_PDU_NOT_RECEIVED.
  577. * We assume if the PDU either failed CRC or timed out
  578. * that a Recovery R2T has already been sent.
  579. */
  580. recovery = 1;
  581. if (first_pdu[i].status != ISCSI_PDU_NOT_RECEIVED)
  582. continue;
  583. if (!offset)
  584. offset = first_pdu[i].offset;
  585. length += first_pdu[i].length;
  586. send_recovery_r2t = 1;
  587. }
  588. if (send_recovery_r2t)
  589. if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
  590. return DATAOUT_CANNOT_RECOVER;
  591. return (!recovery) ? DATAOUT_NORMAL : DATAOUT_WITHIN_COMMAND_RECOVERY;
  592. }
  593. static int iscsit_recalculate_dataout_values(
  594. struct iscsi_cmd *cmd,
  595. u32 pdu_offset,
  596. u32 pdu_length,
  597. u32 *r2t_offset,
  598. u32 *r2t_length)
  599. {
  600. int i;
  601. struct iscsi_conn *conn = cmd->conn;
  602. struct iscsi_pdu *pdu = NULL;
  603. if (conn->sess->sess_ops->DataSequenceInOrder) {
  604. cmd->data_sn = 0;
  605. if (conn->sess->sess_ops->DataPDUInOrder) {
  606. *r2t_offset = cmd->write_data_done;
  607. *r2t_length = (cmd->seq_end_offset -
  608. cmd->write_data_done);
  609. return 0;
  610. }
  611. *r2t_offset = cmd->seq_start_offset;
  612. *r2t_length = (cmd->seq_end_offset - cmd->seq_start_offset);
  613. for (i = 0; i < cmd->pdu_count; i++) {
  614. pdu = &cmd->pdu_list[i];
  615. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  616. continue;
  617. if ((pdu->offset >= cmd->seq_start_offset) &&
  618. ((pdu->offset + pdu->length) <=
  619. cmd->seq_end_offset)) {
  620. if (!cmd->unsolicited_data)
  621. cmd->next_burst_len -= pdu->length;
  622. else
  623. cmd->first_burst_len -= pdu->length;
  624. cmd->write_data_done -= pdu->length;
  625. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  626. }
  627. }
  628. } else {
  629. struct iscsi_seq *seq = NULL;
  630. seq = iscsit_get_seq_holder(cmd, pdu_offset, pdu_length);
  631. if (!seq)
  632. return -1;
  633. *r2t_offset = seq->orig_offset;
  634. *r2t_length = seq->xfer_len;
  635. cmd->write_data_done -= (seq->offset - seq->orig_offset);
  636. if (cmd->immediate_data)
  637. cmd->first_burst_len = cmd->write_data_done;
  638. seq->data_sn = 0;
  639. seq->offset = seq->orig_offset;
  640. seq->next_burst_len = 0;
  641. seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
  642. if (conn->sess->sess_ops->DataPDUInOrder)
  643. return 0;
  644. for (i = 0; i < seq->pdu_count; i++) {
  645. pdu = &cmd->pdu_list[i+seq->pdu_start];
  646. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  647. continue;
  648. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  649. }
  650. }
  651. return 0;
  652. }
  653. int iscsit_recover_dataout_sequence(
  654. struct iscsi_cmd *cmd,
  655. u32 pdu_offset,
  656. u32 pdu_length)
  657. {
  658. u32 r2t_length = 0, r2t_offset = 0;
  659. spin_lock_bh(&cmd->istate_lock);
  660. cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
  661. spin_unlock_bh(&cmd->istate_lock);
  662. if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
  663. &r2t_offset, &r2t_length) < 0)
  664. return DATAOUT_CANNOT_RECOVER;
  665. iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length);
  666. return DATAOUT_WITHIN_COMMAND_RECOVERY;
  667. }
  668. static struct iscsi_ooo_cmdsn *iscsit_allocate_ooo_cmdsn(void)
  669. {
  670. struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL;
  671. ooo_cmdsn = kmem_cache_zalloc(lio_ooo_cache, GFP_ATOMIC);
  672. if (!ooo_cmdsn) {
  673. pr_err("Unable to allocate memory for"
  674. " struct iscsi_ooo_cmdsn.\n");
  675. return NULL;
  676. }
  677. INIT_LIST_HEAD(&ooo_cmdsn->ooo_list);
  678. return ooo_cmdsn;
  679. }
  680. /*
  681. * Called with sess->cmdsn_mutex held.
  682. */
  683. static int iscsit_attach_ooo_cmdsn(
  684. struct iscsi_session *sess,
  685. struct iscsi_ooo_cmdsn *ooo_cmdsn)
  686. {
  687. struct iscsi_ooo_cmdsn *ooo_tail, *ooo_tmp;
  688. /*
  689. * We attach the struct iscsi_ooo_cmdsn entry to the out of order
  690. * list in increasing CmdSN order.
  691. * This allows iscsi_execute_ooo_cmdsns() to detect any
  692. * additional CmdSN holes while performing delayed execution.
  693. */
  694. if (list_empty(&sess->sess_ooo_cmdsn_list))
  695. list_add_tail(&ooo_cmdsn->ooo_list,
  696. &sess->sess_ooo_cmdsn_list);
  697. else {
  698. ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
  699. typeof(*ooo_tail), ooo_list);
  700. /*
  701. * CmdSN is greater than the tail of the list.
  702. */
  703. if (ooo_tail->cmdsn < ooo_cmdsn->cmdsn)
  704. list_add_tail(&ooo_cmdsn->ooo_list,
  705. &sess->sess_ooo_cmdsn_list);
  706. else {
  707. /*
  708. * CmdSN is either lower than the head, or somewhere
  709. * in the middle.
  710. */
  711. list_for_each_entry(ooo_tmp, &sess->sess_ooo_cmdsn_list,
  712. ooo_list) {
  713. if (ooo_tmp->cmdsn < ooo_cmdsn->cmdsn)
  714. continue;
  715. list_add(&ooo_cmdsn->ooo_list,
  716. &ooo_tmp->ooo_list);
  717. break;
  718. }
  719. }
  720. }
  721. return 0;
  722. }
  723. /*
  724. * Removes an struct iscsi_ooo_cmdsn from a session's list,
  725. * called with struct iscsi_session->cmdsn_mutex held.
  726. */
  727. void iscsit_remove_ooo_cmdsn(
  728. struct iscsi_session *sess,
  729. struct iscsi_ooo_cmdsn *ooo_cmdsn)
  730. {
  731. list_del(&ooo_cmdsn->ooo_list);
  732. kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
  733. }
  734. void iscsit_clear_ooo_cmdsns_for_conn(struct iscsi_conn *conn)
  735. {
  736. struct iscsi_ooo_cmdsn *ooo_cmdsn;
  737. struct iscsi_session *sess = conn->sess;
  738. mutex_lock(&sess->cmdsn_mutex);
  739. list_for_each_entry(ooo_cmdsn, &sess->sess_ooo_cmdsn_list, ooo_list) {
  740. if (ooo_cmdsn->cid != conn->cid)
  741. continue;
  742. ooo_cmdsn->cmd = NULL;
  743. }
  744. mutex_unlock(&sess->cmdsn_mutex);
  745. }
  746. /*
  747. * Called with sess->cmdsn_mutex held.
  748. */
  749. int iscsit_execute_ooo_cmdsns(struct iscsi_session *sess)
  750. {
  751. int ooo_count = 0;
  752. struct iscsi_cmd *cmd = NULL;
  753. struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
  754. list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
  755. &sess->sess_ooo_cmdsn_list, ooo_list) {
  756. if (ooo_cmdsn->cmdsn != sess->exp_cmd_sn)
  757. continue;
  758. if (!ooo_cmdsn->cmd) {
  759. sess->exp_cmd_sn++;
  760. iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
  761. continue;
  762. }
  763. cmd = ooo_cmdsn->cmd;
  764. cmd->i_state = cmd->deferred_i_state;
  765. ooo_count++;
  766. sess->exp_cmd_sn++;
  767. pr_debug("Executing out of order CmdSN: 0x%08x,"
  768. " incremented ExpCmdSN to 0x%08x.\n",
  769. cmd->cmd_sn, sess->exp_cmd_sn);
  770. iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
  771. if (iscsit_execute_cmd(cmd, 1) < 0)
  772. return -1;
  773. continue;
  774. }
  775. return ooo_count;
  776. }
  777. /*
  778. * Called either:
  779. *
  780. * 1. With sess->cmdsn_mutex held from iscsi_execute_ooo_cmdsns()
  781. * or iscsi_check_received_cmdsn().
  782. * 2. With no locks held directly from iscsi_handle_XXX_pdu() functions
  783. * for immediate commands.
  784. */
  785. int iscsit_execute_cmd(struct iscsi_cmd *cmd, int ooo)
  786. {
  787. struct se_cmd *se_cmd = &cmd->se_cmd;
  788. int lr = 0;
  789. spin_lock_bh(&cmd->istate_lock);
  790. if (ooo)
  791. cmd->cmd_flags &= ~ICF_OOO_CMDSN;
  792. switch (cmd->iscsi_opcode) {
  793. case ISCSI_OP_SCSI_CMD:
  794. /*
  795. * Go ahead and send the CHECK_CONDITION status for
  796. * any SCSI CDB exceptions that may have occurred, also
  797. * handle the SCF_SCSI_RESERVATION_CONFLICT case here as well.
  798. */
  799. if (se_cmd->se_cmd_flags & SCF_SCSI_CDB_EXCEPTION) {
  800. if (se_cmd->scsi_sense_reason == TCM_RESERVATION_CONFLICT) {
  801. cmd->i_state = ISTATE_SEND_STATUS;
  802. spin_unlock_bh(&cmd->istate_lock);
  803. iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
  804. cmd->i_state);
  805. return 0;
  806. }
  807. spin_unlock_bh(&cmd->istate_lock);
  808. /*
  809. * Determine if delayed TASK_ABORTED status for WRITEs
  810. * should be sent now if no unsolicited data out
  811. * payloads are expected, or if the delayed status
  812. * should be sent after unsolicited data out with
  813. * ISCSI_FLAG_CMD_FINAL set in iscsi_handle_data_out()
  814. */
  815. if (transport_check_aborted_status(se_cmd,
  816. (cmd->unsolicited_data == 0)) != 0)
  817. return 0;
  818. /*
  819. * Otherwise send CHECK_CONDITION and sense for
  820. * exception
  821. */
  822. return transport_send_check_condition_and_sense(se_cmd,
  823. se_cmd->scsi_sense_reason, 0);
  824. }
  825. /*
  826. * Special case for delayed CmdSN with Immediate
  827. * Data and/or Unsolicited Data Out attached.
  828. */
  829. if (cmd->immediate_data) {
  830. if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
  831. spin_unlock_bh(&cmd->istate_lock);
  832. return transport_generic_handle_data(
  833. &cmd->se_cmd);
  834. }
  835. spin_unlock_bh(&cmd->istate_lock);
  836. if (!(cmd->cmd_flags &
  837. ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
  838. /*
  839. * Send the delayed TASK_ABORTED status for
  840. * WRITEs if no more unsolicitied data is
  841. * expected.
  842. */
  843. if (transport_check_aborted_status(se_cmd, 1)
  844. != 0)
  845. return 0;
  846. iscsit_set_dataout_sequence_values(cmd);
  847. iscsit_build_r2ts_for_cmd(cmd, cmd->conn, false);
  848. }
  849. return 0;
  850. }
  851. /*
  852. * The default handler.
  853. */
  854. spin_unlock_bh(&cmd->istate_lock);
  855. if ((cmd->data_direction == DMA_TO_DEVICE) &&
  856. !(cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
  857. /*
  858. * Send the delayed TASK_ABORTED status for WRITEs if
  859. * no more nsolicitied data is expected.
  860. */
  861. if (transport_check_aborted_status(se_cmd, 1) != 0)
  862. return 0;
  863. iscsit_set_dataout_sequence_values(cmd);
  864. spin_lock_bh(&cmd->dataout_timeout_lock);
  865. iscsit_start_dataout_timer(cmd, cmd->conn);
  866. spin_unlock_bh(&cmd->dataout_timeout_lock);
  867. }
  868. return transport_handle_cdb_direct(&cmd->se_cmd);
  869. case ISCSI_OP_NOOP_OUT:
  870. case ISCSI_OP_TEXT:
  871. spin_unlock_bh(&cmd->istate_lock);
  872. iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
  873. break;
  874. case ISCSI_OP_SCSI_TMFUNC:
  875. if (se_cmd->se_cmd_flags & SCF_SCSI_CDB_EXCEPTION) {
  876. spin_unlock_bh(&cmd->istate_lock);
  877. iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
  878. cmd->i_state);
  879. return 0;
  880. }
  881. spin_unlock_bh(&cmd->istate_lock);
  882. return transport_generic_handle_tmr(&cmd->se_cmd);
  883. case ISCSI_OP_LOGOUT:
  884. spin_unlock_bh(&cmd->istate_lock);
  885. switch (cmd->logout_reason) {
  886. case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
  887. lr = iscsit_logout_closesession(cmd, cmd->conn);
  888. break;
  889. case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
  890. lr = iscsit_logout_closeconnection(cmd, cmd->conn);
  891. break;
  892. case ISCSI_LOGOUT_REASON_RECOVERY:
  893. lr = iscsit_logout_removeconnforrecovery(cmd, cmd->conn);
  894. break;
  895. default:
  896. pr_err("Unknown iSCSI Logout Request Code:"
  897. " 0x%02x\n", cmd->logout_reason);
  898. return -1;
  899. }
  900. return lr;
  901. default:
  902. spin_unlock_bh(&cmd->istate_lock);
  903. pr_err("Cannot perform out of order execution for"
  904. " unknown iSCSI Opcode: 0x%02x\n", cmd->iscsi_opcode);
  905. return -1;
  906. }
  907. return 0;
  908. }
  909. void iscsit_free_all_ooo_cmdsns(struct iscsi_session *sess)
  910. {
  911. struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
  912. mutex_lock(&sess->cmdsn_mutex);
  913. list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
  914. &sess->sess_ooo_cmdsn_list, ooo_list) {
  915. list_del(&ooo_cmdsn->ooo_list);
  916. kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
  917. }
  918. mutex_unlock(&sess->cmdsn_mutex);
  919. }
  920. int iscsit_handle_ooo_cmdsn(
  921. struct iscsi_session *sess,
  922. struct iscsi_cmd *cmd,
  923. u32 cmdsn)
  924. {
  925. int batch = 0;
  926. struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL, *ooo_tail = NULL;
  927. cmd->deferred_i_state = cmd->i_state;
  928. cmd->i_state = ISTATE_DEFERRED_CMD;
  929. cmd->cmd_flags |= ICF_OOO_CMDSN;
  930. if (list_empty(&sess->sess_ooo_cmdsn_list))
  931. batch = 1;
  932. else {
  933. ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
  934. typeof(*ooo_tail), ooo_list);
  935. if (ooo_tail->cmdsn != (cmdsn - 1))
  936. batch = 1;
  937. }
  938. ooo_cmdsn = iscsit_allocate_ooo_cmdsn();
  939. if (!ooo_cmdsn)
  940. return CMDSN_ERROR_CANNOT_RECOVER;
  941. ooo_cmdsn->cmd = cmd;
  942. ooo_cmdsn->batch_count = (batch) ?
  943. (cmdsn - sess->exp_cmd_sn) : 1;
  944. ooo_cmdsn->cid = cmd->conn->cid;
  945. ooo_cmdsn->exp_cmdsn = sess->exp_cmd_sn;
  946. ooo_cmdsn->cmdsn = cmdsn;
  947. if (iscsit_attach_ooo_cmdsn(sess, ooo_cmdsn) < 0) {
  948. kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
  949. return CMDSN_ERROR_CANNOT_RECOVER;
  950. }
  951. return CMDSN_HIGHER_THAN_EXP;
  952. }
  953. static int iscsit_set_dataout_timeout_values(
  954. struct iscsi_cmd *cmd,
  955. u32 *offset,
  956. u32 *length)
  957. {
  958. struct iscsi_conn *conn = cmd->conn;
  959. struct iscsi_r2t *r2t;
  960. if (cmd->unsolicited_data) {
  961. *offset = 0;
  962. *length = (conn->sess->sess_ops->FirstBurstLength >
  963. cmd->se_cmd.data_length) ?
  964. cmd->se_cmd.data_length :
  965. conn->sess->sess_ops->FirstBurstLength;
  966. return 0;
  967. }
  968. spin_lock_bh(&cmd->r2t_lock);
  969. if (list_empty(&cmd->cmd_r2t_list)) {
  970. pr_err("cmd->cmd_r2t_list is empty!\n");
  971. spin_unlock_bh(&cmd->r2t_lock);
  972. return -1;
  973. }
  974. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  975. if (r2t->sent_r2t && !r2t->recovery_r2t && !r2t->seq_complete) {
  976. *offset = r2t->offset;
  977. *length = r2t->xfer_len;
  978. spin_unlock_bh(&cmd->r2t_lock);
  979. return 0;
  980. }
  981. }
  982. spin_unlock_bh(&cmd->r2t_lock);
  983. pr_err("Unable to locate any incomplete DataOUT"
  984. " sequences for ITT: 0x%08x.\n", cmd->init_task_tag);
  985. return -1;
  986. }
  987. /*
  988. * NOTE: Called from interrupt (timer) context.
  989. */
  990. static void iscsit_handle_dataout_timeout(unsigned long data)
  991. {
  992. u32 pdu_length = 0, pdu_offset = 0;
  993. u32 r2t_length = 0, r2t_offset = 0;
  994. struct iscsi_cmd *cmd = (struct iscsi_cmd *) data;
  995. struct iscsi_conn *conn = cmd->conn;
  996. struct iscsi_session *sess = NULL;
  997. struct iscsi_node_attrib *na;
  998. iscsit_inc_conn_usage_count(conn);
  999. spin_lock_bh(&cmd->dataout_timeout_lock);
  1000. if (cmd->dataout_timer_flags & ISCSI_TF_STOP) {
  1001. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1002. iscsit_dec_conn_usage_count(conn);
  1003. return;
  1004. }
  1005. cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
  1006. sess = conn->sess;
  1007. na = iscsit_tpg_get_node_attrib(sess);
  1008. if (!sess->sess_ops->ErrorRecoveryLevel) {
  1009. pr_debug("Unable to recover from DataOut timeout while"
  1010. " in ERL=0.\n");
  1011. goto failure;
  1012. }
  1013. if (++cmd->dataout_timeout_retries == na->dataout_timeout_retries) {
  1014. pr_debug("Command ITT: 0x%08x exceeded max retries"
  1015. " for DataOUT timeout %u, closing iSCSI connection.\n",
  1016. cmd->init_task_tag, na->dataout_timeout_retries);
  1017. goto failure;
  1018. }
  1019. cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
  1020. if (conn->sess->sess_ops->DataSequenceInOrder) {
  1021. if (conn->sess->sess_ops->DataPDUInOrder) {
  1022. pdu_offset = cmd->write_data_done;
  1023. if ((pdu_offset + (conn->sess->sess_ops->MaxBurstLength -
  1024. cmd->next_burst_len)) > cmd->se_cmd.data_length)
  1025. pdu_length = (cmd->se_cmd.data_length -
  1026. cmd->write_data_done);
  1027. else
  1028. pdu_length = (conn->sess->sess_ops->MaxBurstLength -
  1029. cmd->next_burst_len);
  1030. } else {
  1031. pdu_offset = cmd->seq_start_offset;
  1032. pdu_length = (cmd->seq_end_offset -
  1033. cmd->seq_start_offset);
  1034. }
  1035. } else {
  1036. if (iscsit_set_dataout_timeout_values(cmd, &pdu_offset,
  1037. &pdu_length) < 0)
  1038. goto failure;
  1039. }
  1040. if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
  1041. &r2t_offset, &r2t_length) < 0)
  1042. goto failure;
  1043. pr_debug("Command ITT: 0x%08x timed out waiting for"
  1044. " completion of %sDataOUT Sequence Offset: %u, Length: %u\n",
  1045. cmd->init_task_tag, (cmd->unsolicited_data) ? "Unsolicited " :
  1046. "", r2t_offset, r2t_length);
  1047. if (iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length) < 0)
  1048. goto failure;
  1049. iscsit_start_dataout_timer(cmd, conn);
  1050. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1051. iscsit_dec_conn_usage_count(conn);
  1052. return;
  1053. failure:
  1054. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1055. iscsit_cause_connection_reinstatement(conn, 0);
  1056. iscsit_dec_conn_usage_count(conn);
  1057. }
  1058. void iscsit_mod_dataout_timer(struct iscsi_cmd *cmd)
  1059. {
  1060. struct iscsi_conn *conn = cmd->conn;
  1061. struct iscsi_session *sess = conn->sess;
  1062. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1063. spin_lock_bh(&cmd->dataout_timeout_lock);
  1064. if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
  1065. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1066. return;
  1067. }
  1068. mod_timer(&cmd->dataout_timer,
  1069. (get_jiffies_64() + na->dataout_timeout * HZ));
  1070. pr_debug("Updated DataOUT timer for ITT: 0x%08x",
  1071. cmd->init_task_tag);
  1072. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1073. }
  1074. /*
  1075. * Called with cmd->dataout_timeout_lock held.
  1076. */
  1077. void iscsit_start_dataout_timer(
  1078. struct iscsi_cmd *cmd,
  1079. struct iscsi_conn *conn)
  1080. {
  1081. struct iscsi_session *sess = conn->sess;
  1082. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1083. if (cmd->dataout_timer_flags & ISCSI_TF_RUNNING)
  1084. return;
  1085. pr_debug("Starting DataOUT timer for ITT: 0x%08x on"
  1086. " CID: %hu.\n", cmd->init_task_tag, conn->cid);
  1087. init_timer(&cmd->dataout_timer);
  1088. cmd->dataout_timer.expires = (get_jiffies_64() + na->dataout_timeout * HZ);
  1089. cmd->dataout_timer.data = (unsigned long)cmd;
  1090. cmd->dataout_timer.function = iscsit_handle_dataout_timeout;
  1091. cmd->dataout_timer_flags &= ~ISCSI_TF_STOP;
  1092. cmd->dataout_timer_flags |= ISCSI_TF_RUNNING;
  1093. add_timer(&cmd->dataout_timer);
  1094. }
  1095. void iscsit_stop_dataout_timer(struct iscsi_cmd *cmd)
  1096. {
  1097. spin_lock_bh(&cmd->dataout_timeout_lock);
  1098. if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
  1099. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1100. return;
  1101. }
  1102. cmd->dataout_timer_flags |= ISCSI_TF_STOP;
  1103. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1104. del_timer_sync(&cmd->dataout_timer);
  1105. spin_lock_bh(&cmd->dataout_timeout_lock);
  1106. cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
  1107. pr_debug("Stopped DataOUT Timer for ITT: 0x%08x\n",
  1108. cmd->init_task_tag);
  1109. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1110. }