iscsi_target_erl1.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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 <nab@linux-iscsi.org>
  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 iscsi_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 iscsi_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 iscsi_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 iscsi_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 iscsi_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 iscsi_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 iscsi_conn *conn = cmd->conn;
  196. struct iscsi_seq *first_seq = NULL, *seq = NULL;
  197. if (!cmd->seq_list) {
  198. pr_err("struct iscsi_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 iscsi_cmd *cmd,
  329. unsigned char *buf,
  330. u32 begrun,
  331. u32 runlength)
  332. {
  333. struct iscsi_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 iscsi_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 iscsi_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 iscsi_conn *conn,
  409. itt_t init_task_tag,
  410. u32 targ_xfer_tag,
  411. u32 begrun,
  412. u32 runlength)
  413. {
  414. struct iscsi_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 iscsi_conn *conn,
  464. u32 targ_xfer_tag,
  465. u32 begrun,
  466. u32 runlength)
  467. {
  468. struct iscsi_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 iscsi_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 iscsi_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 iscsi_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 iscsi_cmd *cmd,
  569. u32 pdu_offset,
  570. u32 pdu_length,
  571. u32 *r2t_offset,
  572. u32 *r2t_length)
  573. {
  574. int i;
  575. struct iscsi_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 iscsi_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 iscsi_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 iscsi_session->cmdsn_mutex held.
  699. */
  700. void iscsit_remove_ooo_cmdsn(
  701. struct iscsi_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 iscsi_conn *conn)
  708. {
  709. struct iscsi_ooo_cmdsn *ooo_cmdsn;
  710. struct iscsi_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 iscsi_session *sess)
  720. {
  721. int ooo_count = 0;
  722. struct iscsi_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. continue;
  745. }
  746. return ooo_count;
  747. }
  748. /*
  749. * Called either:
  750. *
  751. * 1. With sess->cmdsn_mutex held from iscsi_execute_ooo_cmdsns()
  752. * or iscsi_check_received_cmdsn().
  753. * 2. With no locks held directly from iscsi_handle_XXX_pdu() functions
  754. * for immediate commands.
  755. */
  756. int iscsit_execute_cmd(struct iscsi_cmd *cmd, int ooo)
  757. {
  758. struct se_cmd *se_cmd = &cmd->se_cmd;
  759. struct iscsi_conn *conn = cmd->conn;
  760. int lr = 0;
  761. spin_lock_bh(&cmd->istate_lock);
  762. if (ooo)
  763. cmd->cmd_flags &= ~ICF_OOO_CMDSN;
  764. switch (cmd->iscsi_opcode) {
  765. case ISCSI_OP_SCSI_CMD:
  766. /*
  767. * Go ahead and send the CHECK_CONDITION status for
  768. * any SCSI CDB exceptions that may have occurred.
  769. */
  770. if (cmd->sense_reason) {
  771. if (cmd->sense_reason == TCM_RESERVATION_CONFLICT) {
  772. cmd->i_state = ISTATE_SEND_STATUS;
  773. spin_unlock_bh(&cmd->istate_lock);
  774. iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
  775. cmd->i_state);
  776. return 0;
  777. }
  778. spin_unlock_bh(&cmd->istate_lock);
  779. if (cmd->se_cmd.transport_state & CMD_T_ABORTED)
  780. return 0;
  781. return transport_send_check_condition_and_sense(se_cmd,
  782. cmd->sense_reason, 0);
  783. }
  784. /*
  785. * Special case for delayed CmdSN with Immediate
  786. * Data and/or Unsolicited Data Out attached.
  787. */
  788. if (cmd->immediate_data) {
  789. if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
  790. spin_unlock_bh(&cmd->istate_lock);
  791. target_execute_cmd(&cmd->se_cmd);
  792. return 0;
  793. }
  794. spin_unlock_bh(&cmd->istate_lock);
  795. if (!(cmd->cmd_flags &
  796. ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
  797. if (cmd->se_cmd.transport_state & CMD_T_ABORTED)
  798. return 0;
  799. iscsit_set_dataout_sequence_values(cmd);
  800. conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
  801. }
  802. return 0;
  803. }
  804. /*
  805. * The default handler.
  806. */
  807. spin_unlock_bh(&cmd->istate_lock);
  808. if ((cmd->data_direction == DMA_TO_DEVICE) &&
  809. !(cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
  810. if (cmd->se_cmd.transport_state & CMD_T_ABORTED)
  811. return 0;
  812. iscsit_set_unsolicited_dataout(cmd);
  813. }
  814. return transport_handle_cdb_direct(&cmd->se_cmd);
  815. case ISCSI_OP_NOOP_OUT:
  816. case ISCSI_OP_TEXT:
  817. spin_unlock_bh(&cmd->istate_lock);
  818. iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
  819. break;
  820. case ISCSI_OP_SCSI_TMFUNC:
  821. if (cmd->se_cmd.se_tmr_req->response) {
  822. spin_unlock_bh(&cmd->istate_lock);
  823. iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
  824. cmd->i_state);
  825. return 0;
  826. }
  827. spin_unlock_bh(&cmd->istate_lock);
  828. return transport_generic_handle_tmr(&cmd->se_cmd);
  829. case ISCSI_OP_LOGOUT:
  830. spin_unlock_bh(&cmd->istate_lock);
  831. switch (cmd->logout_reason) {
  832. case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
  833. lr = iscsit_logout_closesession(cmd, cmd->conn);
  834. break;
  835. case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
  836. lr = iscsit_logout_closeconnection(cmd, cmd->conn);
  837. break;
  838. case ISCSI_LOGOUT_REASON_RECOVERY:
  839. lr = iscsit_logout_removeconnforrecovery(cmd, cmd->conn);
  840. break;
  841. default:
  842. pr_err("Unknown iSCSI Logout Request Code:"
  843. " 0x%02x\n", cmd->logout_reason);
  844. return -1;
  845. }
  846. return lr;
  847. default:
  848. spin_unlock_bh(&cmd->istate_lock);
  849. pr_err("Cannot perform out of order execution for"
  850. " unknown iSCSI Opcode: 0x%02x\n", cmd->iscsi_opcode);
  851. return -1;
  852. }
  853. return 0;
  854. }
  855. void iscsit_free_all_ooo_cmdsns(struct iscsi_session *sess)
  856. {
  857. struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
  858. mutex_lock(&sess->cmdsn_mutex);
  859. list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
  860. &sess->sess_ooo_cmdsn_list, ooo_list) {
  861. list_del(&ooo_cmdsn->ooo_list);
  862. kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
  863. }
  864. mutex_unlock(&sess->cmdsn_mutex);
  865. }
  866. int iscsit_handle_ooo_cmdsn(
  867. struct iscsi_session *sess,
  868. struct iscsi_cmd *cmd,
  869. u32 cmdsn)
  870. {
  871. int batch = 0;
  872. struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL, *ooo_tail = NULL;
  873. cmd->deferred_i_state = cmd->i_state;
  874. cmd->i_state = ISTATE_DEFERRED_CMD;
  875. cmd->cmd_flags |= ICF_OOO_CMDSN;
  876. if (list_empty(&sess->sess_ooo_cmdsn_list))
  877. batch = 1;
  878. else {
  879. ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
  880. typeof(*ooo_tail), ooo_list);
  881. if (ooo_tail->cmdsn != (cmdsn - 1))
  882. batch = 1;
  883. }
  884. ooo_cmdsn = iscsit_allocate_ooo_cmdsn();
  885. if (!ooo_cmdsn)
  886. return -ENOMEM;
  887. ooo_cmdsn->cmd = cmd;
  888. ooo_cmdsn->batch_count = (batch) ?
  889. (cmdsn - sess->exp_cmd_sn) : 1;
  890. ooo_cmdsn->cid = cmd->conn->cid;
  891. ooo_cmdsn->exp_cmdsn = sess->exp_cmd_sn;
  892. ooo_cmdsn->cmdsn = cmdsn;
  893. if (iscsit_attach_ooo_cmdsn(sess, ooo_cmdsn) < 0) {
  894. kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
  895. return -ENOMEM;
  896. }
  897. return 0;
  898. }
  899. static int iscsit_set_dataout_timeout_values(
  900. struct iscsi_cmd *cmd,
  901. u32 *offset,
  902. u32 *length)
  903. {
  904. struct iscsi_conn *conn = cmd->conn;
  905. struct iscsi_r2t *r2t;
  906. if (cmd->unsolicited_data) {
  907. *offset = 0;
  908. *length = (conn->sess->sess_ops->FirstBurstLength >
  909. cmd->se_cmd.data_length) ?
  910. cmd->se_cmd.data_length :
  911. conn->sess->sess_ops->FirstBurstLength;
  912. return 0;
  913. }
  914. spin_lock_bh(&cmd->r2t_lock);
  915. if (list_empty(&cmd->cmd_r2t_list)) {
  916. pr_err("cmd->cmd_r2t_list is empty!\n");
  917. spin_unlock_bh(&cmd->r2t_lock);
  918. return -1;
  919. }
  920. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  921. if (r2t->sent_r2t && !r2t->recovery_r2t && !r2t->seq_complete) {
  922. *offset = r2t->offset;
  923. *length = r2t->xfer_len;
  924. spin_unlock_bh(&cmd->r2t_lock);
  925. return 0;
  926. }
  927. }
  928. spin_unlock_bh(&cmd->r2t_lock);
  929. pr_err("Unable to locate any incomplete DataOUT"
  930. " sequences for ITT: 0x%08x.\n", cmd->init_task_tag);
  931. return -1;
  932. }
  933. /*
  934. * NOTE: Called from interrupt (timer) context.
  935. */
  936. void iscsit_handle_dataout_timeout(struct timer_list *t)
  937. {
  938. u32 pdu_length = 0, pdu_offset = 0;
  939. u32 r2t_length = 0, r2t_offset = 0;
  940. struct iscsi_cmd *cmd = from_timer(cmd, t, dataout_timer);
  941. struct iscsi_conn *conn = cmd->conn;
  942. struct iscsi_session *sess = NULL;
  943. struct iscsi_node_attrib *na;
  944. iscsit_inc_conn_usage_count(conn);
  945. spin_lock_bh(&cmd->dataout_timeout_lock);
  946. if (cmd->dataout_timer_flags & ISCSI_TF_STOP) {
  947. spin_unlock_bh(&cmd->dataout_timeout_lock);
  948. iscsit_dec_conn_usage_count(conn);
  949. return;
  950. }
  951. cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
  952. sess = conn->sess;
  953. na = iscsit_tpg_get_node_attrib(sess);
  954. if (!sess->sess_ops->ErrorRecoveryLevel) {
  955. pr_err("Unable to recover from DataOut timeout while"
  956. " in ERL=0, closing iSCSI connection for I_T Nexus"
  957. " %s,i,0x%6phN,%s,t,0x%02x\n",
  958. sess->sess_ops->InitiatorName, sess->isid,
  959. sess->tpg->tpg_tiqn->tiqn, (u32)sess->tpg->tpgt);
  960. goto failure;
  961. }
  962. if (++cmd->dataout_timeout_retries == na->dataout_timeout_retries) {
  963. pr_err("Command ITT: 0x%08x exceeded max retries"
  964. " for DataOUT timeout %u, closing iSCSI connection for"
  965. " I_T Nexus %s,i,0x%6phN,%s,t,0x%02x\n",
  966. cmd->init_task_tag, na->dataout_timeout_retries,
  967. sess->sess_ops->InitiatorName, sess->isid,
  968. sess->tpg->tpg_tiqn->tiqn, (u32)sess->tpg->tpgt);
  969. goto failure;
  970. }
  971. cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
  972. if (conn->sess->sess_ops->DataSequenceInOrder) {
  973. if (conn->sess->sess_ops->DataPDUInOrder) {
  974. pdu_offset = cmd->write_data_done;
  975. if ((pdu_offset + (conn->sess->sess_ops->MaxBurstLength -
  976. cmd->next_burst_len)) > cmd->se_cmd.data_length)
  977. pdu_length = (cmd->se_cmd.data_length -
  978. cmd->write_data_done);
  979. else
  980. pdu_length = (conn->sess->sess_ops->MaxBurstLength -
  981. cmd->next_burst_len);
  982. } else {
  983. pdu_offset = cmd->seq_start_offset;
  984. pdu_length = (cmd->seq_end_offset -
  985. cmd->seq_start_offset);
  986. }
  987. } else {
  988. if (iscsit_set_dataout_timeout_values(cmd, &pdu_offset,
  989. &pdu_length) < 0)
  990. goto failure;
  991. }
  992. if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
  993. &r2t_offset, &r2t_length) < 0)
  994. goto failure;
  995. pr_debug("Command ITT: 0x%08x timed out waiting for"
  996. " completion of %sDataOUT Sequence Offset: %u, Length: %u\n",
  997. cmd->init_task_tag, (cmd->unsolicited_data) ? "Unsolicited " :
  998. "", r2t_offset, r2t_length);
  999. if (iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length) < 0)
  1000. goto failure;
  1001. iscsit_start_dataout_timer(cmd, conn);
  1002. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1003. iscsit_dec_conn_usage_count(conn);
  1004. return;
  1005. failure:
  1006. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1007. iscsit_fill_cxn_timeout_err_stats(sess);
  1008. iscsit_cause_connection_reinstatement(conn, 0);
  1009. iscsit_dec_conn_usage_count(conn);
  1010. }
  1011. void iscsit_mod_dataout_timer(struct iscsi_cmd *cmd)
  1012. {
  1013. struct iscsi_conn *conn = cmd->conn;
  1014. struct iscsi_session *sess = conn->sess;
  1015. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1016. spin_lock_bh(&cmd->dataout_timeout_lock);
  1017. if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
  1018. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1019. return;
  1020. }
  1021. mod_timer(&cmd->dataout_timer,
  1022. (get_jiffies_64() + na->dataout_timeout * HZ));
  1023. pr_debug("Updated DataOUT timer for ITT: 0x%08x",
  1024. cmd->init_task_tag);
  1025. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1026. }
  1027. void iscsit_start_dataout_timer(
  1028. struct iscsi_cmd *cmd,
  1029. struct iscsi_conn *conn)
  1030. {
  1031. struct iscsi_session *sess = conn->sess;
  1032. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1033. lockdep_assert_held(&cmd->dataout_timeout_lock);
  1034. if (cmd->dataout_timer_flags & ISCSI_TF_RUNNING)
  1035. return;
  1036. pr_debug("Starting DataOUT timer for ITT: 0x%08x on"
  1037. " CID: %hu.\n", cmd->init_task_tag, conn->cid);
  1038. cmd->dataout_timer_flags &= ~ISCSI_TF_STOP;
  1039. cmd->dataout_timer_flags |= ISCSI_TF_RUNNING;
  1040. mod_timer(&cmd->dataout_timer, jiffies + na->dataout_timeout * HZ);
  1041. }
  1042. void iscsit_stop_dataout_timer(struct iscsi_cmd *cmd)
  1043. {
  1044. spin_lock_bh(&cmd->dataout_timeout_lock);
  1045. if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
  1046. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1047. return;
  1048. }
  1049. cmd->dataout_timer_flags |= ISCSI_TF_STOP;
  1050. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1051. del_timer_sync(&cmd->dataout_timer);
  1052. spin_lock_bh(&cmd->dataout_timeout_lock);
  1053. cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
  1054. pr_debug("Stopped DataOUT Timer for ITT: 0x%08x\n",
  1055. cmd->init_task_tag);
  1056. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1057. }
  1058. EXPORT_SYMBOL(iscsit_stop_dataout_timer);