iscsi_target_erl1.c 33 KB

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