iscsi_target_erl1.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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 "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 (conn->exp_statsn > begrun) {
  445. pr_err("Got Status SNACK Begrun: 0x%08x, RunLength:"
  446. " 0x%08x but already got ExpStatSN: 0x%08x on CID:"
  447. " %hu.\n", begrun, runlength, conn->exp_statsn,
  448. conn->cid);
  449. return 0;
  450. }
  451. last_statsn = (!runlength) ? conn->stat_sn : (begrun + runlength);
  452. while (begrun < last_statsn) {
  453. found_cmd = 0;
  454. spin_lock_bh(&conn->cmd_lock);
  455. list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
  456. if (cmd->stat_sn == begrun) {
  457. found_cmd = 1;
  458. break;
  459. }
  460. }
  461. spin_unlock_bh(&conn->cmd_lock);
  462. if (!found_cmd) {
  463. pr_err("Unable to find StatSN: 0x%08x for"
  464. " a Status SNACK, assuming this was a"
  465. " protactic SNACK for an untransmitted"
  466. " StatSN, ignoring.\n", begrun);
  467. begrun++;
  468. continue;
  469. }
  470. spin_lock_bh(&cmd->istate_lock);
  471. if (cmd->i_state == ISTATE_SEND_DATAIN) {
  472. spin_unlock_bh(&cmd->istate_lock);
  473. pr_err("Ignoring Status SNACK for BegRun:"
  474. " 0x%08x, RunLength: 0x%08x, assuming this was"
  475. " a protactic SNACK for an untransmitted"
  476. " StatSN\n", begrun, runlength);
  477. begrun++;
  478. continue;
  479. }
  480. spin_unlock_bh(&cmd->istate_lock);
  481. cmd->i_state = ISTATE_SEND_STATUS_RECOVERY;
  482. iscsit_add_cmd_to_response_queue(cmd, conn, cmd->i_state);
  483. begrun++;
  484. }
  485. return 0;
  486. }
  487. int iscsit_handle_data_ack(
  488. struct iscsi_conn *conn,
  489. u32 targ_xfer_tag,
  490. u32 begrun,
  491. u32 runlength)
  492. {
  493. struct iscsi_cmd *cmd = NULL;
  494. cmd = iscsit_find_cmd_from_ttt(conn, targ_xfer_tag);
  495. if (!cmd) {
  496. pr_err("Data ACK SNACK for TTT: 0x%08x is"
  497. " invalid.\n", targ_xfer_tag);
  498. return -1;
  499. }
  500. if (begrun <= cmd->acked_data_sn) {
  501. pr_err("ITT: 0x%08x Data ACK SNACK BegRUN: 0x%08x is"
  502. " less than the already acked DataSN: 0x%08x.\n",
  503. cmd->init_task_tag, begrun, cmd->acked_data_sn);
  504. return -1;
  505. }
  506. /*
  507. * For Data ACK SNACK, BegRun is the next expected DataSN.
  508. * (see iSCSI v19: 10.16.6)
  509. */
  510. cmd->cmd_flags |= ICF_GOT_DATACK_SNACK;
  511. cmd->acked_data_sn = (begrun - 1);
  512. pr_debug("Received Data ACK SNACK for ITT: 0x%08x,"
  513. " updated acked DataSN to 0x%08x.\n",
  514. cmd->init_task_tag, cmd->acked_data_sn);
  515. return 0;
  516. }
  517. static int iscsit_send_recovery_r2t(
  518. struct iscsi_cmd *cmd,
  519. u32 offset,
  520. u32 xfer_len)
  521. {
  522. int ret;
  523. spin_lock_bh(&cmd->r2t_lock);
  524. ret = iscsit_add_r2t_to_list(cmd, offset, xfer_len, 1, 0);
  525. spin_unlock_bh(&cmd->r2t_lock);
  526. return ret;
  527. }
  528. int iscsit_dataout_datapduinorder_no_fbit(
  529. struct iscsi_cmd *cmd,
  530. struct iscsi_pdu *pdu)
  531. {
  532. int i, send_recovery_r2t = 0, recovery = 0;
  533. u32 length = 0, offset = 0, pdu_count = 0, xfer_len = 0;
  534. struct iscsi_conn *conn = cmd->conn;
  535. struct iscsi_pdu *first_pdu = NULL;
  536. /*
  537. * Get an struct iscsi_pdu pointer to the first PDU, and total PDU count
  538. * of the DataOUT sequence.
  539. */
  540. if (conn->sess->sess_ops->DataSequenceInOrder) {
  541. for (i = 0; i < cmd->pdu_count; i++) {
  542. if (cmd->pdu_list[i].seq_no == pdu->seq_no) {
  543. if (!first_pdu)
  544. first_pdu = &cmd->pdu_list[i];
  545. xfer_len += cmd->pdu_list[i].length;
  546. pdu_count++;
  547. } else if (pdu_count)
  548. break;
  549. }
  550. } else {
  551. struct iscsi_seq *seq = cmd->seq_ptr;
  552. first_pdu = &cmd->pdu_list[seq->pdu_start];
  553. pdu_count = seq->pdu_count;
  554. }
  555. if (!first_pdu || !pdu_count)
  556. return DATAOUT_CANNOT_RECOVER;
  557. /*
  558. * Loop through the ending DataOUT Sequence checking each struct iscsi_pdu.
  559. * The following ugly logic does batching of not received PDUs.
  560. */
  561. for (i = 0; i < pdu_count; i++) {
  562. if (first_pdu[i].status == ISCSI_PDU_RECEIVED_OK) {
  563. if (!send_recovery_r2t)
  564. continue;
  565. if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
  566. return DATAOUT_CANNOT_RECOVER;
  567. send_recovery_r2t = length = offset = 0;
  568. continue;
  569. }
  570. /*
  571. * Set recovery = 1 for any missing, CRC failed, or timed
  572. * out PDUs to let the DataOUT logic know that this sequence
  573. * has not been completed yet.
  574. *
  575. * Also, only send a Recovery R2T for ISCSI_PDU_NOT_RECEIVED.
  576. * We assume if the PDU either failed CRC or timed out
  577. * that a Recovery R2T has already been sent.
  578. */
  579. recovery = 1;
  580. if (first_pdu[i].status != ISCSI_PDU_NOT_RECEIVED)
  581. continue;
  582. if (!offset)
  583. offset = first_pdu[i].offset;
  584. length += first_pdu[i].length;
  585. send_recovery_r2t = 1;
  586. }
  587. if (send_recovery_r2t)
  588. if (iscsit_send_recovery_r2t(cmd, offset, length) < 0)
  589. return DATAOUT_CANNOT_RECOVER;
  590. return (!recovery) ? DATAOUT_NORMAL : DATAOUT_WITHIN_COMMAND_RECOVERY;
  591. }
  592. static int iscsit_recalculate_dataout_values(
  593. struct iscsi_cmd *cmd,
  594. u32 pdu_offset,
  595. u32 pdu_length,
  596. u32 *r2t_offset,
  597. u32 *r2t_length)
  598. {
  599. int i;
  600. struct iscsi_conn *conn = cmd->conn;
  601. struct iscsi_pdu *pdu = NULL;
  602. if (conn->sess->sess_ops->DataSequenceInOrder) {
  603. cmd->data_sn = 0;
  604. if (conn->sess->sess_ops->DataPDUInOrder) {
  605. *r2t_offset = cmd->write_data_done;
  606. *r2t_length = (cmd->seq_end_offset -
  607. cmd->write_data_done);
  608. return 0;
  609. }
  610. *r2t_offset = cmd->seq_start_offset;
  611. *r2t_length = (cmd->seq_end_offset - cmd->seq_start_offset);
  612. for (i = 0; i < cmd->pdu_count; i++) {
  613. pdu = &cmd->pdu_list[i];
  614. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  615. continue;
  616. if ((pdu->offset >= cmd->seq_start_offset) &&
  617. ((pdu->offset + pdu->length) <=
  618. cmd->seq_end_offset)) {
  619. if (!cmd->unsolicited_data)
  620. cmd->next_burst_len -= pdu->length;
  621. else
  622. cmd->first_burst_len -= pdu->length;
  623. cmd->write_data_done -= pdu->length;
  624. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  625. }
  626. }
  627. } else {
  628. struct iscsi_seq *seq = NULL;
  629. seq = iscsit_get_seq_holder(cmd, pdu_offset, pdu_length);
  630. if (!seq)
  631. return -1;
  632. *r2t_offset = seq->orig_offset;
  633. *r2t_length = seq->xfer_len;
  634. cmd->write_data_done -= (seq->offset - seq->orig_offset);
  635. if (cmd->immediate_data)
  636. cmd->first_burst_len = cmd->write_data_done;
  637. seq->data_sn = 0;
  638. seq->offset = seq->orig_offset;
  639. seq->next_burst_len = 0;
  640. seq->status = DATAOUT_SEQUENCE_WITHIN_COMMAND_RECOVERY;
  641. if (conn->sess->sess_ops->DataPDUInOrder)
  642. return 0;
  643. for (i = 0; i < seq->pdu_count; i++) {
  644. pdu = &cmd->pdu_list[i+seq->pdu_start];
  645. if (pdu->status != ISCSI_PDU_RECEIVED_OK)
  646. continue;
  647. pdu->status = ISCSI_PDU_NOT_RECEIVED;
  648. }
  649. }
  650. return 0;
  651. }
  652. int iscsit_recover_dataout_sequence(
  653. struct iscsi_cmd *cmd,
  654. u32 pdu_offset,
  655. u32 pdu_length)
  656. {
  657. u32 r2t_length = 0, r2t_offset = 0;
  658. spin_lock_bh(&cmd->istate_lock);
  659. cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
  660. spin_unlock_bh(&cmd->istate_lock);
  661. if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
  662. &r2t_offset, &r2t_length) < 0)
  663. return DATAOUT_CANNOT_RECOVER;
  664. iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length);
  665. return DATAOUT_WITHIN_COMMAND_RECOVERY;
  666. }
  667. static struct iscsi_ooo_cmdsn *iscsit_allocate_ooo_cmdsn(void)
  668. {
  669. struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL;
  670. ooo_cmdsn = kmem_cache_zalloc(lio_ooo_cache, GFP_ATOMIC);
  671. if (!ooo_cmdsn) {
  672. pr_err("Unable to allocate memory for"
  673. " struct iscsi_ooo_cmdsn.\n");
  674. return NULL;
  675. }
  676. INIT_LIST_HEAD(&ooo_cmdsn->ooo_list);
  677. return ooo_cmdsn;
  678. }
  679. /*
  680. * Called with sess->cmdsn_mutex held.
  681. */
  682. static int iscsit_attach_ooo_cmdsn(
  683. struct iscsi_session *sess,
  684. struct iscsi_ooo_cmdsn *ooo_cmdsn)
  685. {
  686. struct iscsi_ooo_cmdsn *ooo_tail, *ooo_tmp;
  687. /*
  688. * We attach the struct iscsi_ooo_cmdsn entry to the out of order
  689. * list in increasing CmdSN order.
  690. * This allows iscsi_execute_ooo_cmdsns() to detect any
  691. * additional CmdSN holes while performing delayed execution.
  692. */
  693. if (list_empty(&sess->sess_ooo_cmdsn_list))
  694. list_add_tail(&ooo_cmdsn->ooo_list,
  695. &sess->sess_ooo_cmdsn_list);
  696. else {
  697. ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
  698. typeof(*ooo_tail), ooo_list);
  699. /*
  700. * CmdSN is greater than the tail of the list.
  701. */
  702. if (iscsi_sna_lt(ooo_tail->cmdsn, ooo_cmdsn->cmdsn))
  703. list_add_tail(&ooo_cmdsn->ooo_list,
  704. &sess->sess_ooo_cmdsn_list);
  705. else {
  706. /*
  707. * CmdSN is either lower than the head, or somewhere
  708. * in the middle.
  709. */
  710. list_for_each_entry(ooo_tmp, &sess->sess_ooo_cmdsn_list,
  711. ooo_list) {
  712. if (iscsi_sna_lt(ooo_tmp->cmdsn, ooo_cmdsn->cmdsn))
  713. continue;
  714. /* Insert before this entry */
  715. list_add(&ooo_cmdsn->ooo_list,
  716. ooo_tmp->ooo_list.prev);
  717. break;
  718. }
  719. }
  720. }
  721. return 0;
  722. }
  723. /*
  724. * Removes an struct iscsi_ooo_cmdsn from a session's list,
  725. * called with struct iscsi_session->cmdsn_mutex held.
  726. */
  727. void iscsit_remove_ooo_cmdsn(
  728. struct iscsi_session *sess,
  729. struct iscsi_ooo_cmdsn *ooo_cmdsn)
  730. {
  731. list_del(&ooo_cmdsn->ooo_list);
  732. kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
  733. }
  734. void iscsit_clear_ooo_cmdsns_for_conn(struct iscsi_conn *conn)
  735. {
  736. struct iscsi_ooo_cmdsn *ooo_cmdsn;
  737. struct iscsi_session *sess = conn->sess;
  738. mutex_lock(&sess->cmdsn_mutex);
  739. list_for_each_entry(ooo_cmdsn, &sess->sess_ooo_cmdsn_list, ooo_list) {
  740. if (ooo_cmdsn->cid != conn->cid)
  741. continue;
  742. ooo_cmdsn->cmd = NULL;
  743. }
  744. mutex_unlock(&sess->cmdsn_mutex);
  745. }
  746. /*
  747. * Called with sess->cmdsn_mutex held.
  748. */
  749. int iscsit_execute_ooo_cmdsns(struct iscsi_session *sess)
  750. {
  751. int ooo_count = 0;
  752. struct iscsi_cmd *cmd = NULL;
  753. struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
  754. list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
  755. &sess->sess_ooo_cmdsn_list, ooo_list) {
  756. if (ooo_cmdsn->cmdsn != sess->exp_cmd_sn)
  757. continue;
  758. if (!ooo_cmdsn->cmd) {
  759. sess->exp_cmd_sn++;
  760. iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
  761. continue;
  762. }
  763. cmd = ooo_cmdsn->cmd;
  764. cmd->i_state = cmd->deferred_i_state;
  765. ooo_count++;
  766. sess->exp_cmd_sn++;
  767. pr_debug("Executing out of order CmdSN: 0x%08x,"
  768. " incremented ExpCmdSN to 0x%08x.\n",
  769. cmd->cmd_sn, sess->exp_cmd_sn);
  770. iscsit_remove_ooo_cmdsn(sess, ooo_cmdsn);
  771. if (iscsit_execute_cmd(cmd, 1) < 0)
  772. return -1;
  773. continue;
  774. }
  775. return ooo_count;
  776. }
  777. /*
  778. * Called either:
  779. *
  780. * 1. With sess->cmdsn_mutex held from iscsi_execute_ooo_cmdsns()
  781. * or iscsi_check_received_cmdsn().
  782. * 2. With no locks held directly from iscsi_handle_XXX_pdu() functions
  783. * for immediate commands.
  784. */
  785. int iscsit_execute_cmd(struct iscsi_cmd *cmd, int ooo)
  786. {
  787. struct se_cmd *se_cmd = &cmd->se_cmd;
  788. struct iscsi_conn *conn = cmd->conn;
  789. int lr = 0;
  790. spin_lock_bh(&cmd->istate_lock);
  791. if (ooo)
  792. cmd->cmd_flags &= ~ICF_OOO_CMDSN;
  793. switch (cmd->iscsi_opcode) {
  794. case ISCSI_OP_SCSI_CMD:
  795. /*
  796. * Go ahead and send the CHECK_CONDITION status for
  797. * any SCSI CDB exceptions that may have occurred.
  798. */
  799. if (cmd->sense_reason) {
  800. if (cmd->sense_reason == TCM_RESERVATION_CONFLICT) {
  801. cmd->i_state = ISTATE_SEND_STATUS;
  802. spin_unlock_bh(&cmd->istate_lock);
  803. iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
  804. cmd->i_state);
  805. return 0;
  806. }
  807. spin_unlock_bh(&cmd->istate_lock);
  808. /*
  809. * Determine if delayed TASK_ABORTED status for WRITEs
  810. * should be sent now if no unsolicited data out
  811. * payloads are expected, or if the delayed status
  812. * should be sent after unsolicited data out with
  813. * ISCSI_FLAG_CMD_FINAL set in iscsi_handle_data_out()
  814. */
  815. if (transport_check_aborted_status(se_cmd,
  816. (cmd->unsolicited_data == 0)) != 0)
  817. return 0;
  818. /*
  819. * Otherwise send CHECK_CONDITION and sense for
  820. * exception
  821. */
  822. return transport_send_check_condition_and_sense(se_cmd,
  823. cmd->sense_reason, 0);
  824. }
  825. /*
  826. * Special case for delayed CmdSN with Immediate
  827. * Data and/or Unsolicited Data Out attached.
  828. */
  829. if (cmd->immediate_data) {
  830. if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT) {
  831. spin_unlock_bh(&cmd->istate_lock);
  832. target_execute_cmd(&cmd->se_cmd);
  833. return 0;
  834. }
  835. spin_unlock_bh(&cmd->istate_lock);
  836. if (!(cmd->cmd_flags &
  837. ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
  838. /*
  839. * Send the delayed TASK_ABORTED status for
  840. * WRITEs if no more unsolicitied data is
  841. * expected.
  842. */
  843. if (transport_check_aborted_status(se_cmd, 1)
  844. != 0)
  845. return 0;
  846. iscsit_set_dataout_sequence_values(cmd);
  847. conn->conn_transport->iscsit_get_dataout(conn, cmd, false);
  848. }
  849. return 0;
  850. }
  851. /*
  852. * The default handler.
  853. */
  854. spin_unlock_bh(&cmd->istate_lock);
  855. if ((cmd->data_direction == DMA_TO_DEVICE) &&
  856. !(cmd->cmd_flags & ICF_NON_IMMEDIATE_UNSOLICITED_DATA)) {
  857. /*
  858. * Send the delayed TASK_ABORTED status for WRITEs if
  859. * no more nsolicitied data is expected.
  860. */
  861. if (transport_check_aborted_status(se_cmd, 1) != 0)
  862. return 0;
  863. iscsit_set_unsoliticed_dataout(cmd);
  864. }
  865. return transport_handle_cdb_direct(&cmd->se_cmd);
  866. case ISCSI_OP_NOOP_OUT:
  867. case ISCSI_OP_TEXT:
  868. spin_unlock_bh(&cmd->istate_lock);
  869. iscsit_add_cmd_to_response_queue(cmd, cmd->conn, cmd->i_state);
  870. break;
  871. case ISCSI_OP_SCSI_TMFUNC:
  872. if (cmd->se_cmd.se_tmr_req->response) {
  873. spin_unlock_bh(&cmd->istate_lock);
  874. iscsit_add_cmd_to_response_queue(cmd, cmd->conn,
  875. cmd->i_state);
  876. return 0;
  877. }
  878. spin_unlock_bh(&cmd->istate_lock);
  879. return transport_generic_handle_tmr(&cmd->se_cmd);
  880. case ISCSI_OP_LOGOUT:
  881. spin_unlock_bh(&cmd->istate_lock);
  882. switch (cmd->logout_reason) {
  883. case ISCSI_LOGOUT_REASON_CLOSE_SESSION:
  884. lr = iscsit_logout_closesession(cmd, cmd->conn);
  885. break;
  886. case ISCSI_LOGOUT_REASON_CLOSE_CONNECTION:
  887. lr = iscsit_logout_closeconnection(cmd, cmd->conn);
  888. break;
  889. case ISCSI_LOGOUT_REASON_RECOVERY:
  890. lr = iscsit_logout_removeconnforrecovery(cmd, cmd->conn);
  891. break;
  892. default:
  893. pr_err("Unknown iSCSI Logout Request Code:"
  894. " 0x%02x\n", cmd->logout_reason);
  895. return -1;
  896. }
  897. return lr;
  898. default:
  899. spin_unlock_bh(&cmd->istate_lock);
  900. pr_err("Cannot perform out of order execution for"
  901. " unknown iSCSI Opcode: 0x%02x\n", cmd->iscsi_opcode);
  902. return -1;
  903. }
  904. return 0;
  905. }
  906. void iscsit_free_all_ooo_cmdsns(struct iscsi_session *sess)
  907. {
  908. struct iscsi_ooo_cmdsn *ooo_cmdsn, *ooo_cmdsn_tmp;
  909. mutex_lock(&sess->cmdsn_mutex);
  910. list_for_each_entry_safe(ooo_cmdsn, ooo_cmdsn_tmp,
  911. &sess->sess_ooo_cmdsn_list, ooo_list) {
  912. list_del(&ooo_cmdsn->ooo_list);
  913. kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
  914. }
  915. mutex_unlock(&sess->cmdsn_mutex);
  916. }
  917. int iscsit_handle_ooo_cmdsn(
  918. struct iscsi_session *sess,
  919. struct iscsi_cmd *cmd,
  920. u32 cmdsn)
  921. {
  922. int batch = 0;
  923. struct iscsi_ooo_cmdsn *ooo_cmdsn = NULL, *ooo_tail = NULL;
  924. cmd->deferred_i_state = cmd->i_state;
  925. cmd->i_state = ISTATE_DEFERRED_CMD;
  926. cmd->cmd_flags |= ICF_OOO_CMDSN;
  927. if (list_empty(&sess->sess_ooo_cmdsn_list))
  928. batch = 1;
  929. else {
  930. ooo_tail = list_entry(sess->sess_ooo_cmdsn_list.prev,
  931. typeof(*ooo_tail), ooo_list);
  932. if (ooo_tail->cmdsn != (cmdsn - 1))
  933. batch = 1;
  934. }
  935. ooo_cmdsn = iscsit_allocate_ooo_cmdsn();
  936. if (!ooo_cmdsn)
  937. return -ENOMEM;
  938. ooo_cmdsn->cmd = cmd;
  939. ooo_cmdsn->batch_count = (batch) ?
  940. (cmdsn - sess->exp_cmd_sn) : 1;
  941. ooo_cmdsn->cid = cmd->conn->cid;
  942. ooo_cmdsn->exp_cmdsn = sess->exp_cmd_sn;
  943. ooo_cmdsn->cmdsn = cmdsn;
  944. if (iscsit_attach_ooo_cmdsn(sess, ooo_cmdsn) < 0) {
  945. kmem_cache_free(lio_ooo_cache, ooo_cmdsn);
  946. return -ENOMEM;
  947. }
  948. return 0;
  949. }
  950. static int iscsit_set_dataout_timeout_values(
  951. struct iscsi_cmd *cmd,
  952. u32 *offset,
  953. u32 *length)
  954. {
  955. struct iscsi_conn *conn = cmd->conn;
  956. struct iscsi_r2t *r2t;
  957. if (cmd->unsolicited_data) {
  958. *offset = 0;
  959. *length = (conn->sess->sess_ops->FirstBurstLength >
  960. cmd->se_cmd.data_length) ?
  961. cmd->se_cmd.data_length :
  962. conn->sess->sess_ops->FirstBurstLength;
  963. return 0;
  964. }
  965. spin_lock_bh(&cmd->r2t_lock);
  966. if (list_empty(&cmd->cmd_r2t_list)) {
  967. pr_err("cmd->cmd_r2t_list is empty!\n");
  968. spin_unlock_bh(&cmd->r2t_lock);
  969. return -1;
  970. }
  971. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  972. if (r2t->sent_r2t && !r2t->recovery_r2t && !r2t->seq_complete) {
  973. *offset = r2t->offset;
  974. *length = r2t->xfer_len;
  975. spin_unlock_bh(&cmd->r2t_lock);
  976. return 0;
  977. }
  978. }
  979. spin_unlock_bh(&cmd->r2t_lock);
  980. pr_err("Unable to locate any incomplete DataOUT"
  981. " sequences for ITT: 0x%08x.\n", cmd->init_task_tag);
  982. return -1;
  983. }
  984. /*
  985. * NOTE: Called from interrupt (timer) context.
  986. */
  987. static void iscsit_handle_dataout_timeout(unsigned long data)
  988. {
  989. u32 pdu_length = 0, pdu_offset = 0;
  990. u32 r2t_length = 0, r2t_offset = 0;
  991. struct iscsi_cmd *cmd = (struct iscsi_cmd *) data;
  992. struct iscsi_conn *conn = cmd->conn;
  993. struct iscsi_session *sess = NULL;
  994. struct iscsi_node_attrib *na;
  995. iscsit_inc_conn_usage_count(conn);
  996. spin_lock_bh(&cmd->dataout_timeout_lock);
  997. if (cmd->dataout_timer_flags & ISCSI_TF_STOP) {
  998. spin_unlock_bh(&cmd->dataout_timeout_lock);
  999. iscsit_dec_conn_usage_count(conn);
  1000. return;
  1001. }
  1002. cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
  1003. sess = conn->sess;
  1004. na = iscsit_tpg_get_node_attrib(sess);
  1005. if (!sess->sess_ops->ErrorRecoveryLevel) {
  1006. pr_debug("Unable to recover from DataOut timeout while"
  1007. " in ERL=0.\n");
  1008. goto failure;
  1009. }
  1010. if (++cmd->dataout_timeout_retries == na->dataout_timeout_retries) {
  1011. pr_debug("Command ITT: 0x%08x exceeded max retries"
  1012. " for DataOUT timeout %u, closing iSCSI connection.\n",
  1013. cmd->init_task_tag, na->dataout_timeout_retries);
  1014. goto failure;
  1015. }
  1016. cmd->cmd_flags |= ICF_WITHIN_COMMAND_RECOVERY;
  1017. if (conn->sess->sess_ops->DataSequenceInOrder) {
  1018. if (conn->sess->sess_ops->DataPDUInOrder) {
  1019. pdu_offset = cmd->write_data_done;
  1020. if ((pdu_offset + (conn->sess->sess_ops->MaxBurstLength -
  1021. cmd->next_burst_len)) > cmd->se_cmd.data_length)
  1022. pdu_length = (cmd->se_cmd.data_length -
  1023. cmd->write_data_done);
  1024. else
  1025. pdu_length = (conn->sess->sess_ops->MaxBurstLength -
  1026. cmd->next_burst_len);
  1027. } else {
  1028. pdu_offset = cmd->seq_start_offset;
  1029. pdu_length = (cmd->seq_end_offset -
  1030. cmd->seq_start_offset);
  1031. }
  1032. } else {
  1033. if (iscsit_set_dataout_timeout_values(cmd, &pdu_offset,
  1034. &pdu_length) < 0)
  1035. goto failure;
  1036. }
  1037. if (iscsit_recalculate_dataout_values(cmd, pdu_offset, pdu_length,
  1038. &r2t_offset, &r2t_length) < 0)
  1039. goto failure;
  1040. pr_debug("Command ITT: 0x%08x timed out waiting for"
  1041. " completion of %sDataOUT Sequence Offset: %u, Length: %u\n",
  1042. cmd->init_task_tag, (cmd->unsolicited_data) ? "Unsolicited " :
  1043. "", r2t_offset, r2t_length);
  1044. if (iscsit_send_recovery_r2t(cmd, r2t_offset, r2t_length) < 0)
  1045. goto failure;
  1046. iscsit_start_dataout_timer(cmd, conn);
  1047. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1048. iscsit_dec_conn_usage_count(conn);
  1049. return;
  1050. failure:
  1051. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1052. iscsit_cause_connection_reinstatement(conn, 0);
  1053. iscsit_dec_conn_usage_count(conn);
  1054. }
  1055. void iscsit_mod_dataout_timer(struct iscsi_cmd *cmd)
  1056. {
  1057. struct iscsi_conn *conn = cmd->conn;
  1058. struct iscsi_session *sess = conn->sess;
  1059. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1060. spin_lock_bh(&cmd->dataout_timeout_lock);
  1061. if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
  1062. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1063. return;
  1064. }
  1065. mod_timer(&cmd->dataout_timer,
  1066. (get_jiffies_64() + na->dataout_timeout * HZ));
  1067. pr_debug("Updated DataOUT timer for ITT: 0x%08x",
  1068. cmd->init_task_tag);
  1069. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1070. }
  1071. /*
  1072. * Called with cmd->dataout_timeout_lock held.
  1073. */
  1074. void iscsit_start_dataout_timer(
  1075. struct iscsi_cmd *cmd,
  1076. struct iscsi_conn *conn)
  1077. {
  1078. struct iscsi_session *sess = conn->sess;
  1079. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1080. if (cmd->dataout_timer_flags & ISCSI_TF_RUNNING)
  1081. return;
  1082. pr_debug("Starting DataOUT timer for ITT: 0x%08x on"
  1083. " CID: %hu.\n", cmd->init_task_tag, conn->cid);
  1084. init_timer(&cmd->dataout_timer);
  1085. cmd->dataout_timer.expires = (get_jiffies_64() + na->dataout_timeout * HZ);
  1086. cmd->dataout_timer.data = (unsigned long)cmd;
  1087. cmd->dataout_timer.function = iscsit_handle_dataout_timeout;
  1088. cmd->dataout_timer_flags &= ~ISCSI_TF_STOP;
  1089. cmd->dataout_timer_flags |= ISCSI_TF_RUNNING;
  1090. add_timer(&cmd->dataout_timer);
  1091. }
  1092. void iscsit_stop_dataout_timer(struct iscsi_cmd *cmd)
  1093. {
  1094. spin_lock_bh(&cmd->dataout_timeout_lock);
  1095. if (!(cmd->dataout_timer_flags & ISCSI_TF_RUNNING)) {
  1096. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1097. return;
  1098. }
  1099. cmd->dataout_timer_flags |= ISCSI_TF_STOP;
  1100. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1101. del_timer_sync(&cmd->dataout_timer);
  1102. spin_lock_bh(&cmd->dataout_timeout_lock);
  1103. cmd->dataout_timer_flags &= ~ISCSI_TF_RUNNING;
  1104. pr_debug("Stopped DataOUT Timer for ITT: 0x%08x\n",
  1105. cmd->init_task_tag);
  1106. spin_unlock_bh(&cmd->dataout_timeout_lock);
  1107. }
  1108. EXPORT_SYMBOL(iscsit_stop_dataout_timer);