iscsi_target_erl1.c 31 KB

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