iscsi_target_erl1.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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_transport.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. #if 0
  247. pr_err("Ignoring non-sent sequence 0x%08x ->"
  248. " 0x%08x\n\n", seq->first_datasn,
  249. seq->last_datasn);
  250. #endif
  251. continue;
  252. }
  253. /*
  254. * This DataIN sequence is precedes the received BegRun, add the
  255. * total xfer_len of the sequence to read_data_done and reset
  256. * seq->pdu_send_order.
  257. */
  258. if ((seq->first_datasn < begrun) &&
  259. (seq->last_datasn < begrun)) {
  260. #if 0
  261. pr_err("Pre BegRun sequence 0x%08x ->"
  262. " 0x%08x\n", seq->first_datasn,
  263. seq->last_datasn);
  264. #endif
  265. read_data_done += cmd->seq_list[i].xfer_len;
  266. seq->next_burst_len = seq->pdu_send_order = 0;
  267. continue;
  268. }
  269. /*
  270. * The BegRun lies within this DataIN sequence.
  271. */
  272. if ((seq->first_datasn <= begrun) &&
  273. (seq->last_datasn >= begrun)) {
  274. #if 0
  275. pr_err("Found sequence begrun: 0x%08x in"
  276. " 0x%08x -> 0x%08x\n", begrun,
  277. seq->first_datasn, seq->last_datasn);
  278. #endif
  279. seq_send_order = seq->seq_send_order;
  280. data_sn = seq->first_datasn;
  281. seq->next_burst_len = seq->pdu_send_order = 0;
  282. found_seq = 1;
  283. /*
  284. * For DataPDUInOrder=Yes, while the first DataSN of
  285. * the sequence is less than the received BegRun, add
  286. * the MaxRecvDataSegmentLength to read_data_done and
  287. * to the sequence's next_burst_len;
  288. *
  289. * For DataPDUInOrder=No, while the first DataSN of the
  290. * sequence is less than the received BegRun, find the
  291. * struct iscsi_pdu of the DataSN in question and add the
  292. * MaxRecvDataSegmentLength to read_data_done and to the
  293. * sequence's next_burst_len;
  294. */
  295. if (conn->sess->sess_ops->DataPDUInOrder) {
  296. while (data_sn < begrun) {
  297. seq->pdu_send_order++;
  298. read_data_done +=
  299. conn->conn_ops->MaxRecvDataSegmentLength;
  300. seq->next_burst_len +=
  301. conn->conn_ops->MaxRecvDataSegmentLength;
  302. data_sn++;
  303. }
  304. } else {
  305. int j;
  306. struct iscsi_pdu *pdu;
  307. while (data_sn < begrun) {
  308. seq->pdu_send_order++;
  309. for (j = 0; j < seq->pdu_count; j++) {
  310. pdu = &cmd->pdu_list[
  311. seq->pdu_start + j];
  312. if (pdu->data_sn == data_sn) {
  313. read_data_done +=
  314. pdu->length;
  315. seq->next_burst_len +=
  316. pdu->length;
  317. }
  318. }
  319. data_sn++;
  320. }
  321. }
  322. continue;
  323. }
  324. /*
  325. * This DataIN sequence is larger than the received BegRun,
  326. * reset seq->pdu_send_order and continue.
  327. */
  328. if ((seq->first_datasn > begrun) ||
  329. (seq->last_datasn > begrun)) {
  330. #if 0
  331. pr_err("Post BegRun sequence 0x%08x -> 0x%08x\n",
  332. seq->first_datasn, seq->last_datasn);
  333. #endif
  334. seq->next_burst_len = seq->pdu_send_order = 0;
  335. continue;
  336. }
  337. }
  338. if (!found_seq) {
  339. if (!begrun) {
  340. if (!first_seq) {
  341. pr_err("ITT: 0x%08x, Begrun: 0x%08x"
  342. " but first_seq is NULL\n",
  343. cmd->init_task_tag, begrun);
  344. return -1;
  345. }
  346. seq_send_order = first_seq->seq_send_order;
  347. seq->next_burst_len = seq->pdu_send_order = 0;
  348. goto done;
  349. }
  350. pr_err("Unable to locate struct iscsi_seq for ITT: 0x%08x,"
  351. " BegRun: 0x%08x, RunLength: 0x%08x while"
  352. " DataSequenceInOrder=No and DataPDUInOrder=%s.\n",
  353. cmd->init_task_tag, begrun, runlength,
  354. (conn->sess->sess_ops->DataPDUInOrder) ? "Yes" : "No");
  355. return -1;
  356. }
  357. done:
  358. dr->read_data_done = read_data_done;
  359. dr->seq_send_order = seq_send_order;
  360. return 0;
  361. }
  362. static int iscsit_handle_recovery_datain(
  363. struct iscsi_cmd *cmd,
  364. unsigned char *buf,
  365. u32 begrun,
  366. u32 runlength)
  367. {
  368. struct iscsi_conn *conn = cmd->conn;
  369. struct iscsi_datain_req *dr;
  370. struct se_cmd *se_cmd = &cmd->se_cmd;
  371. if (!atomic_read(&se_cmd->t_transport_complete)) {
  372. pr_err("Ignoring ITT: 0x%08x Data SNACK\n",
  373. cmd->init_task_tag);
  374. return 0;
  375. }
  376. /*
  377. * Make sure the initiator is not requesting retransmission
  378. * of DataSNs already acknowledged by a Data ACK SNACK.
  379. */
  380. if ((cmd->cmd_flags & ICF_GOT_DATACK_SNACK) &&
  381. (begrun <= cmd->acked_data_sn)) {
  382. pr_err("ITT: 0x%08x, Data SNACK requesting"
  383. " retransmission of DataSN: 0x%08x to 0x%08x but"
  384. " already acked to DataSN: 0x%08x by Data ACK SNACK,"
  385. " protocol error.\n", cmd->init_task_tag, begrun,
  386. (begrun + runlength), cmd->acked_data_sn);
  387. return iscsit_add_reject_from_cmd(ISCSI_REASON_PROTOCOL_ERROR,
  388. 1, 0, buf, cmd);
  389. }
  390. /*
  391. * Make sure BegRun and RunLength in the Data SNACK are sane.
  392. * Note: (cmd->data_sn - 1) will carry the maximum DataSN sent.
  393. */
  394. if ((begrun + runlength) > (cmd->data_sn - 1)) {
  395. pr_err("Initiator requesting BegRun: 0x%08x, RunLength"
  396. ": 0x%08x greater than maximum DataSN: 0x%08x.\n",
  397. begrun, runlength, (cmd->data_sn - 1));
  398. return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_INVALID,
  399. 1, 0, buf, cmd);
  400. }
  401. dr = iscsit_allocate_datain_req();
  402. if (!dr)
  403. return iscsit_add_reject_from_cmd(ISCSI_REASON_BOOKMARK_NO_RESOURCES,
  404. 1, 0, buf, cmd);
  405. dr->data_sn = dr->begrun = begrun;
  406. dr->runlength = runlength;
  407. dr->generate_recovery_values = 1;
  408. dr->recovery = DATAIN_WITHIN_COMMAND_RECOVERY;
  409. iscsit_attach_datain_req(cmd, dr);
  410. cmd->i_state = ISTATE_SEND_DATAIN;
  411. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  412. return 0;
  413. }
  414. int iscsit_handle_recovery_datain_or_r2t(
  415. struct iscsi_conn *conn,
  416. unsigned char *buf,
  417. u32 init_task_tag,
  418. u32 targ_xfer_tag,
  419. u32 begrun,
  420. u32 runlength)
  421. {
  422. struct iscsi_cmd *cmd;
  423. cmd = iscsit_find_cmd_from_itt(conn, init_task_tag);
  424. if (!cmd)
  425. return 0;
  426. /*
  427. * FIXME: This will not work for bidi commands.
  428. */
  429. switch (cmd->data_direction) {
  430. case DMA_TO_DEVICE:
  431. return iscsit_handle_r2t_snack(cmd, buf, begrun, runlength);
  432. case DMA_FROM_DEVICE:
  433. return iscsit_handle_recovery_datain(cmd, buf, begrun,
  434. runlength);
  435. default:
  436. pr_err("Unknown cmd->data_direction: 0x%02x\n",
  437. cmd->data_direction);
  438. return -1;
  439. }
  440. return 0;
  441. }
  442. /* #warning FIXME: Status SNACK needs to be dependent on OPCODE!!! */
  443. int iscsit_handle_status_snack(
  444. struct iscsi_conn *conn,
  445. u32 init_task_tag,
  446. u32 targ_xfer_tag,
  447. u32 begrun,
  448. u32 runlength)
  449. {
  450. struct iscsi_cmd *cmd = NULL;
  451. u32 last_statsn;
  452. int found_cmd;
  453. if (conn->exp_statsn > begrun) {
  454. pr_err("Got Status SNACK Begrun: 0x%08x, RunLength:"
  455. " 0x%08x but already got ExpStatSN: 0x%08x on CID:"
  456. " %hu.\n", begrun, runlength, conn->exp_statsn,
  457. conn->cid);
  458. return 0;
  459. }
  460. last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);
  461. while (begrun < last_statsn) {
  462. found_cmd = 0;
  463. spin_lock_bh(&conn->cmd_lock);
  464. list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
  465. if (cmd->stat_sn == begrun) {
  466. found_cmd = 1;
  467. break;
  468. }
  469. }
  470. spin_unlock_bh(&conn->cmd_lock);
  471. if (!found_cmd) {
  472. pr_err("Unable to find StatSN: 0x%08x for"
  473. " a Status SNACK, assuming this was a"
  474. " protactic SNACK for an untransmitted"
  475. " StatSN, ignoring.\n", begrun);
  476. begrun++;
  477. continue;
  478. }
  479. spin_lock_bh(&cmd->istate_lock);
  480. if (cmd->i_state == ISTATE_SEND_DATAIN) {
  481. spin_unlock_bh(&cmd->istate_lock);
  482. pr_err("Ignoring Status SNACK for BegRun:"
  483. " 0x%08x, RunLength: 0x%08x, assuming this was"
  484. " a protactic SNACK for an untransmitted"
  485. " StatSN\n", begrun, runlength);
  486. begrun++;
  487. continue;
  488. }
  489. spin_unlock_bh(&cmd->istate_lock);
  490. cmd->i_state = ISTATE_SEND_STATUS_RECOVERY;
  491. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  492. begrun++;
  493. }
  494. return 0;
  495. }
  496. int iscsit_handle_data_ack(
  497. struct iscsi_conn *conn,
  498. u32 targ_xfer_tag,
  499. u32 begrun,
  500. u32 runlength)
  501. {
  502. struct iscsi_cmd *cmd = NULL;
  503. cmd = iscsit_find_cmd_from_ttt(conn, targ_xfer_tag);
  504. if (!cmd) {
  505. pr_err("Data ACK SNACK for TTT: 0x%08x is"
  506. " invalid.\n", targ_xfer_tag);
  507. return -1;
  508. }
  509. if (begrun <= cmd->acked_data_sn) {
  510. pr_err("ITT: 0x%08x Data ACK SNACK BegRUN: 0x%08x is"
  511. " less than the already acked DataSN: 0x%08x.\n",
  512. cmd->init_task_tag, begrun, cmd->acked_data_sn);
  513. return -1;
  514. }
  515. /*
  516. * For Data ACK SNACK, BegRun is the next expected DataSN.
  517. * (see iSCSI v19: 10.16.6)
  518. */
  519. cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
  520. cmd->acked_data_sn = (begrun - 1);
  521. pr_debug("Received Data ACK SNACK for ITT: 0x%08x,"
  522. " updated acked DataSN to 0x%08x.\n",
  523. cmd->init_task_tag, cmd->acked_data_sn);
  524. return 0;
  525. }
  526. static int iscsit_send_recovery_r2t(
  527. struct iscsi_cmd *cmd,
  528. u32 offset,
  529. u32 xfer_len)
  530. {
  531. int ret;
  532. spin_lock_bh(&cmd->r2t_lock);
  533. ret = iscsit_add_r2t_to_list(cmd, offset, xfer_len, 1, 0);
  534. spin_unlock_bh(&cmd->r2t_lock);
  535. return ret;
  536. }
  537. int iscsit_dataout_datapduinorder_no_fbit(
  538. struct iscsi_cmd *cmd,
  539. struct iscsi_pdu *pdu)
  540. {
  541. int i, send_recovery_r2t = 0, recovery = 0;
  542. u32 length = 0, offset = 0, pdu_count = 0, xfer_len = 0;
  543. struct iscsi_conn *conn = cmd->conn;
  544. struct iscsi_pdu *first_pdu = NULL;
  545. /*
  546. * Get an struct iscsi_pdu pointer to the first PDU, and total PDU count
  547. * of the DataOUT sequence.
  548. */
  549. if (conn->sess->sess_ops->DataSequenceInOrder) {
  550. for (i = 0; i < cmd->pdu_count; i++) {
  551. if (cmd->pdu_list[i].seq_no == pdu->seq_no) {
  552. if (!first_pdu)
  553. first_pdu = &cmd->pdu_list[i];
  554. xfer_len += cmd->pdu_list[i].length;
  555. pdu_count++;
  556. } else if (pdu_count)
  557. break;
  558. }
  559. } else {
  560. struct iscsi_seq *seq = cmd->seq_ptr;
  561. first_pdu = &cmd->pdu_list[seq->pdu_start];
  562. pdu_count = seq->pdu_count;
  563. }
  564. if (!first_pdu || !pdu_count)
  565. return DATAOUT_CANNOT_RECOVER;
  566. /*
  567. * Loop through the ending DataOUT Sequence checking each struct iscsi_pdu.
  568. * The following ugly logic does batching of not received PDUs.
  569. */
  570. for (i = 0; i < pdu_count; i++) {
  571. if (first_pdu[i].status == ISCSI_PDU_RECEIVED_OK) {
  572. if (!send_recovery_r2t)
  573. continue;
  574. if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
  575. return DATAOUT_CANNOT_RECOVER;
  576. send_recovery_r2t = length = offset = 0;
  577. continue;
  578. }
  579. /*
  580. * Set recovery = 1 for any missing, CRC failed, or timed
  581. * out PDUs to let the DataOUT logic know that this sequence
  582. * has not been completed yet.
  583. *
  584. * Also, only send a Recovery R2T for ISCSI_PDU_NOT_RECEIVED.
  585. * We assume if the PDU either failed CRC or timed out
  586. * that a Recovery R2T has already been sent.
  587. */
  588. recovery = 1;
  589. if (first_pdu[i].status != ISCSI_PDU_NOT_RECEIVED)
  590. continue;
  591. if (!offset)
  592. offset = first_pdu[i].offset;
  593. length += first_pdu[i].length;
  594. send_recovery_r2t = 1;
  595. }
  596. if (send_recovery_r2t)
  597. if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
  598. return DATAOUT_CANNOT_RECOVER;
  599. return (!recovery) ? DATAOUT_NORMAL : DATAOUT_WITHIN_COMMAND_RECOVERY;
  600. }
  601. static int iscsit_recalculate_dataout_values(
  602. struct iscsi_cmd *cmd,
  603. u32 pdu_offset,
  604. u32 pdu_length,
  605. u32 *r2t_offset,
  606. u32 *r2t_length)
  607. {
  608. int i;
  609. struct iscsi_conn *conn = cmd->conn;
  610. struct iscsi_pdu *pdu = NULL;
  611. if (conn->sess->sess_ops->DataSequenceInOrder) {
  612. cmd->data_sn = 0;
  613. if (conn->sess->sess_ops->DataPDUInOrder) {
  614. *r2t_offset = cmd->write_data_done;
  615. *r2t_length = (cmd->seq_end_offset -
  616. cmd->write_data_done);
  617. return 0;
  618. }
  619. *r2t_offset = cmd->seq_start_offset;
  620. *r2t_length = (cmd->seq_end_offset - cmd->seq_start_offset);
  621. for (i = 0; i < cmd->pdu_count; i++) {
  622. pdu = &cmd->pdu_list[i];
  623. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  624. continue;
  625. if ((pdu->offset >= cmd->seq_start_offset) &&
  626. ((pdu->offset + pdu->length) <=
  627. cmd->seq_end_offset)) {
  628. if (!cmd->unsolicited_data)
  629. cmd->next_burst_len -= pdu->length;
  630. else
  631. cmd->first_burst_len -= pdu->length;
  632. cmd->write_data_done -= pdu->length;
  633. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  634. }
  635. }
  636. } else {
  637. struct iscsi_seq *seq = NULL;
  638. seq = iscsit_get_seq_holder(cmd, pdu_offset, pdu_length);
  639. if (!seq)
  640. return -1;
  641. *r2t_offset = seq->orig_offset;
  642. *r2t_length = seq->xfer_len;
  643. cmd->write_data_done -= (seq->offset - seq->orig_offset);
  644. if (cmd->immediate_data)
  645. cmd->first_burst_len = cmd->write_data_done;
  646. seq->data_sn = 0;
  647. seq->offset = seq->orig_offset;
  648. seq->next_burst_len = 0;
  649. seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
  650. if (conn->sess->sess_ops->DataPDUInOrder)
  651. return 0;
  652. for (i = 0; i < seq->pdu_count; i++) {
  653. pdu = &cmd->pdu_list[i+seq->pdu_start];
  654. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  655. continue;
  656. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  657. }
  658. }
  659. return 0;
  660. }
  661. int iscsit_recover_dataout_sequence(
  662. struct iscsi_cmd *cmd,
  663. u32 pdu_offset,
  664. u32 pdu_length)
  665. {
  666. u32 r2t_length = 0, r2t_offset = 0;
  667. spin_lock_bh(&cmd->istate_lock);
  668. cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
  669. spin_unlock_bh(&cmd->istate_lock);
  670. if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
  671. &r2t_offset, &r2t_length) < 0)
  672. return DATAOUT_CANNOT_RECOVER;
  673. iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length);
  674. return DATAOUT_WITHIN_COMMAND_RECOVERY;
  675. }
  676. static struct iscsi_ooo_cmdsn *iscsit_allocate_ooo_cmdsn(void)
  677. {
  678. struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL;
  679. ooo_cmdsn = kmem_cache_zalloc(lio_ooo_cache, GFP_ATOMIC);
  680. if (!ooo_cmdsn) {
  681. pr_err("Unable to allocate memory for"
  682. " struct iscsi_ooo_cmdsn.\n");
  683. return NULL;
  684. }
  685. INIT_LIST_HEAD(&ooo_cmdsn->ooo_list);
  686. return ooo_cmdsn;
  687. }
  688. /*
  689. * Called with sess->cmdsn_mutex held.
  690. */
  691. static int iscsit_attach_ooo_cmdsn(
  692. struct iscsi_session *sess,
  693. struct iscsi_ooo_cmdsn *ooo_cmdsn)
  694. {
  695. struct iscsi_ooo_cmdsn *ooo_tail, *ooo_tmp;
  696. /*
  697. * We attach the struct iscsi_ooo_cmdsn entry to the out of order
  698. * list in increasing CmdSN order.
  699. * This allows iscsi_execute_ooo_cmdsns() to detect any
  700. * additional CmdSN holes while performing delayed execution.
  701. */
  702. if (list_empty(&sess->sess_ooo_cmdsn_list))
  703. list_add_tail(&ooo_cmdsn->ooo_list,
  704. &sess->sess_ooo_cmdsn_list);
  705. else {
  706. ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
  707. typeof(*ooo_tail), ooo_list);
  708. /*
  709. * CmdSN is greater than the tail of the list.
  710. */
  711. if (ooo_tail->cmdsn < ooo_cmdsn->cmdsn)
  712. list_add_tail(&ooo_cmdsn->ooo_list,
  713. &sess->sess_ooo_cmdsn_list);
  714. else {
  715. /*
  716. * CmdSN is either lower than the head, or somewhere
  717. * in the middle.
  718. */
  719. list_for_each_entry(ooo_tmp, &sess->sess_ooo_cmdsn_list,
  720. ooo_list) {
  721. if (ooo_tmp->cmdsn < ooo_cmdsn->cmdsn)
  722. continue;
  723. list_add(&ooo_cmdsn->ooo_list,
  724. &ooo_tmp->ooo_list);
  725. break;
  726. }
  727. }
  728. }
  729. return 0;
  730. }
  731. /*
  732. * Removes an struct iscsi_ooo_cmdsn from a session's list,
  733. * called with struct iscsi_session->cmdsn_mutex held.
  734. */
  735. void iscsit_remove_ooo_cmdsn(
  736. struct iscsi_session *sess,
  737. struct iscsi_ooo_cmdsn *ooo_cmdsn)
  738. {
  739. list_del(&ooo_cmdsn->ooo_list);
  740. kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
  741. }
  742. void iscsit_clear_ooo_cmdsns_for_conn(struct iscsi_conn *conn)
  743. {
  744. struct iscsi_ooo_cmdsn *ooo_cmdsn;
  745. struct iscsi_session *sess = conn->sess;
  746. mutex_lock(&sess->cmdsn_mutex);
  747. list_for_each_entry(ooo_cmdsn, &sess->sess_ooo_cmdsn_list, ooo_list) {
  748. if (ooo_cmdsn->cid != conn->cid)
  749. continue;
  750. ooo_cmdsn->cmd = NULL;
  751. }
  752. mutex_unlock(&sess->cmdsn_mutex);
  753. }
  754. /*
  755. * Called with sess->cmdsn_mutex held.
  756. */
  757. int iscsit_execute_ooo_cmdsns(struct iscsi_session *sess)
  758. {
  759. int ooo_count = 0;
  760. struct iscsi_cmd *cmd = NULL;
  761. struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
  762. list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
  763. &sess->sess_ooo_cmdsn_list, ooo_list) {
  764. if (ooo_cmdsn->cmdsn != sess->exp_cmd_sn)
  765. continue;
  766. if (!ooo_cmdsn->cmd) {
  767. sess->exp_cmd_sn++;
  768. iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
  769. continue;
  770. }
  771. cmd = ooo_cmdsn->cmd;
  772. cmd->i_state = cmd->deferred_i_state;
  773. ooo_count++;
  774. sess->exp_cmd_sn++;
  775. pr_debug("Executing out of order CmdSN: 0x%08x,"
  776. " incremented ExpCmdSN to 0x%08x.\n",
  777. cmd->cmd_sn, sess->exp_cmd_sn);
  778. iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
  779. if (iscsit_execute_cmd(cmd, 1) < 0)
  780. return -1;
  781. continue;
  782. }
  783. return ooo_count;
  784. }
  785. /*
  786. * Called either:
  787. *
  788. * 1. With sess->cmdsn_mutex held from iscsi_execute_ooo_cmdsns()
  789. * or iscsi_check_received_cmdsn().
  790. * 2. With no locks held directly from iscsi_handle_XXX_pdu() functions
  791. * for immediate commands.
  792. */
  793. int iscsit_execute_cmd(struct iscsi_cmd *cmd, int ooo)
  794. {
  795. struct se_cmd *se_cmd = &cmd->se_cmd;
  796. int lr = 0;
  797. spin_lock_bh(&cmd->istate_lock);
  798. if (ooo)
  799. cmd->cmd_flags &= ~ICF_OOO_CMDSN;
  800. switch (cmd->iscsi_opcode) {
  801. case ISCSI_OP_SCSI_CMD:
  802. /*
  803. * Go ahead and send the CHECK_CONDITION status for
  804. * any SCSI CDB exceptions that may have occurred, also
  805. * handle the SCF_SCSI_RESERVATION_CONFLICT case here as well.
  806. */
  807. if (se_cmd->se_cmd_flags & SCF_SCSI_CDB_EXCEPTION) {
  808. if (se_cmd->se_cmd_flags &
  809. SCF_SCSI_RESERVATION_CONFLICT) {
  810. cmd->i_state = ISTATE_SEND_STATUS;
  811. spin_unlock_bh(&cmd->istate_lock);
  812. iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
  813. cmd->i_state);
  814. return 0;
  815. }
  816. spin_unlock_bh(&cmd->istate_lock);
  817. /*
  818. * Determine if delayed TASK_ABORTED status for WRITEs
  819. * should be sent now if no unsolicited data out
  820. * payloads are expected, or if the delayed status
  821. * should be sent after unsolicited data out with
  822. * ISCSI_FLAG_CMD_FINAL set in iscsi_handle_data_out()
  823. */
  824. if (transport_check_aborted_status(se_cmd,
  825. (cmd->unsolicited_data == 0)) != 0)
  826. return 0;
  827. /*
  828. * Otherwise send CHECK_CONDITION and sense for
  829. * exception
  830. */
  831. return transport_send_check_condition_and_sense(se_cmd,
  832. se_cmd->scsi_sense_reason, 0);
  833. }
  834. /*
  835. * Special case for delayed CmdSN with Immediate
  836. * Data and/or Unsolicited Data Out attached.
  837. */
  838. if (cmd->immediate_data) {
  839. if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
  840. spin_unlock_bh(&cmd->istate_lock);
  841. return transport_generic_handle_data(
  842. &cmd->se_cmd);
  843. }
  844. spin_unlock_bh(&cmd->istate_lock);
  845. if (!(cmd->cmd_flags &
  846. ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
  847. /*
  848. * Send the delayed TASK_ABORTED status for
  849. * WRITEs if no more unsolicitied data is
  850. * expected.
  851. */
  852. if (transport_check_aborted_status(se_cmd, 1)
  853. != 0)
  854. return 0;
  855. iscsit_set_dataout_sequence_values(cmd);
  856. iscsit_build_r2ts_for_cmd(cmd, cmd->conn, 0);
  857. }
  858. return 0;
  859. }
  860. /*
  861. * The default handler.
  862. */
  863. spin_unlock_bh(&cmd->istate_lock);
  864. if ((cmd->data_direction == DMA_TO_DEVICE) &&
  865. !(cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
  866. /*
  867. * Send the delayed TASK_ABORTED status for WRITEs if
  868. * no more nsolicitied data is expected.
  869. */
  870. if (transport_check_aborted_status(se_cmd, 1) != 0)
  871. return 0;
  872. iscsit_set_dataout_sequence_values(cmd);
  873. spin_lock_bh(&cmd->dataout_timeout_lock);
  874. iscsit_start_dataout_timer(cmd, cmd->conn);
  875. spin_unlock_bh(&cmd->dataout_timeout_lock);
  876. }
  877. return transport_handle_cdb_direct(&cmd->se_cmd);
  878. case ISCSI_OP_NOOP_OUT:
  879. case ISCSI_OP_TEXT:
  880. spin_unlock_bh(&cmd->istate_lock);
  881. iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
  882. break;
  883. case ISCSI_OP_SCSI_TMFUNC:
  884. if (se_cmd->se_cmd_flags & SCF_SCSI_CDB_EXCEPTION) {
  885. spin_unlock_bh(&cmd->istate_lock);
  886. iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
  887. cmd->i_state);
  888. return 0;
  889. }
  890. spin_unlock_bh(&cmd->istate_lock);
  891. return transport_generic_handle_tmr(&cmd->se_cmd);
  892. case ISCSI_OP_LOGOUT:
  893. spin_unlock_bh(&cmd->istate_lock);
  894. switch (cmd->logout_reason) {
  895. case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
  896. lr = iscsit_logout_closesession(cmd, cmd->conn);
  897. break;
  898. case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
  899. lr = iscsit_logout_closeconnection(cmd, cmd->conn);
  900. break;
  901. case ISCSI_LOGOUT_REASON_RECOVERY:
  902. lr = iscsit_logout_removeconnforrecovery(cmd, cmd->conn);
  903. break;
  904. default:
  905. pr_err("Unknown iSCSI Logout Request Code:"
  906. " 0x%02x\n", cmd->logout_reason);
  907. return -1;
  908. }
  909. return lr;
  910. default:
  911. spin_unlock_bh(&cmd->istate_lock);
  912. pr_err("Cannot perform out of order execution for"
  913. " unknown iSCSI Opcode: 0x%02x\n", cmd->iscsi_opcode);
  914. return -1;
  915. }
  916. return 0;
  917. }
  918. void iscsit_free_all_ooo_cmdsns(struct iscsi_session *sess)
  919. {
  920. struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
  921. mutex_lock(&sess->cmdsn_mutex);
  922. list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
  923. &sess->sess_ooo_cmdsn_list, ooo_list) {
  924. list_del(&ooo_cmdsn->ooo_list);
  925. kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
  926. }
  927. mutex_unlock(&sess->cmdsn_mutex);
  928. }
  929. int iscsit_handle_ooo_cmdsn(
  930. struct iscsi_session *sess,
  931. struct iscsi_cmd *cmd,
  932. u32 cmdsn)
  933. {
  934. int batch = 0;
  935. struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL, *ooo_tail = NULL;
  936. cmd->deferred_i_state = cmd->i_state;
  937. cmd->i_state = ISTATE_DEFERRED_CMD;
  938. cmd->cmd_flags |= ICF_OOO_CMDSN;
  939. if (list_empty(&sess->sess_ooo_cmdsn_list))
  940. batch = 1;
  941. else {
  942. ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
  943. typeof(*ooo_tail), ooo_list);
  944. if (ooo_tail->cmdsn != (cmdsn - 1))
  945. batch = 1;
  946. }
  947. ooo_cmdsn = iscsit_allocate_ooo_cmdsn();
  948. if (!ooo_cmdsn)
  949. return CMDSN_ERROR_CANNOT_RECOVER;
  950. ooo_cmdsn->cmd = cmd;
  951. ooo_cmdsn->batch_count = (batch) ?
  952. (cmdsn - sess->exp_cmd_sn) : 1;
  953. ooo_cmdsn->cid = cmd->conn->cid;
  954. ooo_cmdsn->exp_cmdsn = sess->exp_cmd_sn;
  955. ooo_cmdsn->cmdsn = cmdsn;
  956. if (iscsit_attach_ooo_cmdsn(sess, ooo_cmdsn) < 0) {
  957. kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
  958. return CMDSN_ERROR_CANNOT_RECOVER;
  959. }
  960. return CMDSN_HIGHER_THAN_EXP;
  961. }
  962. static int iscsit_set_dataout_timeout_values(
  963. struct iscsi_cmd *cmd,
  964. u32 *offset,
  965. u32 *length)
  966. {
  967. struct iscsi_conn *conn = cmd->conn;
  968. struct iscsi_r2t *r2t;
  969. if (cmd->unsolicited_data) {
  970. *offset = 0;
  971. *length = (conn->sess->sess_ops->FirstBurstLength >
  972. cmd->data_length) ?
  973. cmd->data_length :
  974. conn->sess->sess_ops->FirstBurstLength;
  975. return 0;
  976. }
  977. spin_lock_bh(&cmd->r2t_lock);
  978. if (list_empty(&cmd->cmd_r2t_list)) {
  979. pr_err("cmd->cmd_r2t_list is empty!\n");
  980. spin_unlock_bh(&cmd->r2t_lock);
  981. return -1;
  982. }
  983. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  984. if (r2t->sent_r2t && !r2t->recovery_r2t && !r2t->seq_complete) {
  985. *offset = r2t->offset;
  986. *length = r2t->xfer_len;
  987. spin_unlock_bh(&cmd->r2t_lock);
  988. return 0;
  989. }
  990. }
  991. spin_unlock_bh(&cmd->r2t_lock);
  992. pr_err("Unable to locate any incomplete DataOUT"
  993. " sequences for ITT: 0x%08x.\n", cmd->init_task_tag);
  994. return -1;
  995. }
  996. /*
  997. * NOTE: Called from interrupt (timer) context.
  998. */
  999. static void iscsit_handle_dataout_timeout(unsigned long data)
  1000. {
  1001. u32 pdu_length = 0, pdu_offset = 0;
  1002. u32 r2t_length = 0, r2t_offset = 0;
  1003. struct iscsi_cmd *cmd = (struct iscsi_cmd *) data;
  1004. struct iscsi_conn *conn = cmd->conn;
  1005. struct iscsi_session *sess = NULL;
  1006. struct iscsi_node_attrib *na;
  1007. iscsit_inc_conn_usage_count(conn);
  1008. spin_lock_bh(&cmd->dataout_timeout_lock);
  1009. if (cmd->dataout_timer_flags & ISCSI_TF_STOP) {
  1010. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1011. iscsit_dec_conn_usage_count(conn);
  1012. return;
  1013. }
  1014. cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
  1015. sess = conn->sess;
  1016. na = iscsit_tpg_get_node_attrib(sess);
  1017. if (!sess->sess_ops->ErrorRecoveryLevel) {
  1018. pr_debug("Unable to recover from DataOut timeout while"
  1019. " in ERL=0.\n");
  1020. goto failure;
  1021. }
  1022. if (++cmd->dataout_timeout_retries == na->dataout_timeout_retries) {
  1023. pr_debug("Command ITT: 0x%08x exceeded max retries"
  1024. " for DataOUT timeout %u, closing iSCSI connection.\n",
  1025. cmd->init_task_tag, na->dataout_timeout_retries);
  1026. goto failure;
  1027. }
  1028. cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
  1029. if (conn->sess->sess_ops->DataSequenceInOrder) {
  1030. if (conn->sess->sess_ops->DataPDUInOrder) {
  1031. pdu_offset = cmd->write_data_done;
  1032. if ((pdu_offset + (conn->sess->sess_ops->MaxBurstLength -
  1033. cmd->next_burst_len)) > cmd->data_length)
  1034. pdu_length = (cmd->data_length -
  1035. cmd->write_data_done);
  1036. else
  1037. pdu_length = (conn->sess->sess_ops->MaxBurstLength -
  1038. cmd->next_burst_len);
  1039. } else {
  1040. pdu_offset = cmd->seq_start_offset;
  1041. pdu_length = (cmd->seq_end_offset -
  1042. cmd->seq_start_offset);
  1043. }
  1044. } else {
  1045. if (iscsit_set_dataout_timeout_values(cmd, &pdu_offset,
  1046. &pdu_length) < 0)
  1047. goto failure;
  1048. }
  1049. if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
  1050. &r2t_offset, &r2t_length) < 0)
  1051. goto failure;
  1052. pr_debug("Command ITT: 0x%08x timed out waiting for"
  1053. " completion of %sDataOUT Sequence Offset: %u, Length: %u\n",
  1054. cmd->init_task_tag, (cmd->unsolicited_data) ? "Unsolicited " :
  1055. "", r2t_offset, r2t_length);
  1056. if (iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length) < 0)
  1057. goto failure;
  1058. iscsit_start_dataout_timer(cmd, conn);
  1059. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1060. iscsit_dec_conn_usage_count(conn);
  1061. return;
  1062. failure:
  1063. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1064. iscsit_cause_connection_reinstatement(conn, 0);
  1065. iscsit_dec_conn_usage_count(conn);
  1066. }
  1067. void iscsit_mod_dataout_timer(struct iscsi_cmd *cmd)
  1068. {
  1069. struct iscsi_conn *conn = cmd->conn;
  1070. struct iscsi_session *sess = conn->sess;
  1071. struct iscsi_node_attrib *na = na = iscsit_tpg_get_node_attrib(sess);
  1072. spin_lock_bh(&cmd->dataout_timeout_lock);
  1073. if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
  1074. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1075. return;
  1076. }
  1077. mod_timer(&cmd->dataout_timer,
  1078. (get_jiffies_64() + na->dataout_timeout * HZ));
  1079. pr_debug("Updated DataOUT timer for ITT: 0x%08x",
  1080. cmd->init_task_tag);
  1081. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1082. }
  1083. /*
  1084. * Called with cmd->dataout_timeout_lock held.
  1085. */
  1086. void iscsit_start_dataout_timer(
  1087. struct iscsi_cmd *cmd,
  1088. struct iscsi_conn *conn)
  1089. {
  1090. struct iscsi_session *sess = conn->sess;
  1091. struct iscsi_node_attrib *na = na = iscsit_tpg_get_node_attrib(sess);
  1092. if (cmd->dataout_timer_flags & ISCSI_TF_RUNNING)
  1093. return;
  1094. pr_debug("Starting DataOUT timer for ITT: 0x%08x on"
  1095. " CID: %hu.\n", cmd->init_task_tag, conn->cid);
  1096. init_timer(&cmd->dataout_timer);
  1097. cmd->dataout_timer.expires = (get_jiffies_64() + na->dataout_timeout * HZ);
  1098. cmd->dataout_timer.data = (unsigned long)cmd;
  1099. cmd->dataout_timer.function = iscsit_handle_dataout_timeout;
  1100. cmd->dataout_timer_flags &= ~ISCSI_TF_STOP;
  1101. cmd->dataout_timer_flags |= ISCSI_TF_RUNNING;
  1102. add_timer(&cmd->dataout_timer);
  1103. }
  1104. void iscsit_stop_dataout_timer(struct iscsi_cmd *cmd)
  1105. {
  1106. spin_lock_bh(&cmd->dataout_timeout_lock);
  1107. if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
  1108. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1109. return;
  1110. }
  1111. cmd->dataout_timer_flags |= ISCSI_TF_STOP;
  1112. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1113. del_timer_sync(&cmd->dataout_timer);
  1114. spin_lock_bh(&cmd->dataout_timeout_lock);
  1115. cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
  1116. pr_debug("Stopped DataOUT Timer for ITT: 0x%08x\n",
  1117. cmd->init_task_tag);
  1118. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1119. }