iscsi_target_erl1.c 33 KB

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