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