iscsi_target_util.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. /*******************************************************************************
  2. * This file contains the iSCSI Target specific utility functions.
  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 <linux/percpu_ida.h>
  20. #include <scsi/scsi_tcq.h>
  21. #include <scsi/iscsi_proto.h>
  22. #include <target/target_core_base.h>
  23. #include <target/target_core_fabric.h>
  24. #include <target/iscsi/iscsi_transport.h>
  25. #include <target/iscsi/iscsi_target_core.h>
  26. #include "iscsi_target_parameters.h"
  27. #include "iscsi_target_seq_pdu_list.h"
  28. #include "iscsi_target_datain_values.h"
  29. #include "iscsi_target_erl0.h"
  30. #include "iscsi_target_erl1.h"
  31. #include "iscsi_target_erl2.h"
  32. #include "iscsi_target_tpg.h"
  33. #include "iscsi_target_util.h"
  34. #include "iscsi_target.h"
  35. #define PRINT_BUFF(buff, len) \
  36. { \
  37. int zzz; \
  38. \
  39. pr_debug("%d:\n", __LINE__); \
  40. for (zzz = 0; zzz < len; zzz++) { \
  41. if (zzz % 16 == 0) { \
  42. if (zzz) \
  43. pr_debug("\n"); \
  44. pr_debug("%4i: ", zzz); \
  45. } \
  46. pr_debug("%02x ", (unsigned char) (buff)[zzz]); \
  47. } \
  48. if ((len + 1) % 16) \
  49. pr_debug("\n"); \
  50. }
  51. extern struct list_head g_tiqn_list;
  52. extern spinlock_t tiqn_lock;
  53. /*
  54. * Called with cmd->r2t_lock held.
  55. */
  56. int iscsit_add_r2t_to_list(
  57. struct iscsi_cmd *cmd,
  58. u32 offset,
  59. u32 xfer_len,
  60. int recovery,
  61. u32 r2t_sn)
  62. {
  63. struct iscsi_r2t *r2t;
  64. r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC);
  65. if (!r2t) {
  66. pr_err("Unable to allocate memory for struct iscsi_r2t.\n");
  67. return -1;
  68. }
  69. INIT_LIST_HEAD(&r2t->r2t_list);
  70. r2t->recovery_r2t = recovery;
  71. r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn;
  72. r2t->offset = offset;
  73. r2t->xfer_len = xfer_len;
  74. list_add_tail(&r2t->r2t_list, &cmd->cmd_r2t_list);
  75. spin_unlock_bh(&cmd->r2t_lock);
  76. iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
  77. spin_lock_bh(&cmd->r2t_lock);
  78. return 0;
  79. }
  80. struct iscsi_r2t *iscsit_get_r2t_for_eos(
  81. struct iscsi_cmd *cmd,
  82. u32 offset,
  83. u32 length)
  84. {
  85. struct iscsi_r2t *r2t;
  86. spin_lock_bh(&cmd->r2t_lock);
  87. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  88. if ((r2t->offset <= offset) &&
  89. (r2t->offset + r2t->xfer_len) >= (offset + length)) {
  90. spin_unlock_bh(&cmd->r2t_lock);
  91. return r2t;
  92. }
  93. }
  94. spin_unlock_bh(&cmd->r2t_lock);
  95. pr_err("Unable to locate R2T for Offset: %u, Length:"
  96. " %u\n", offset, length);
  97. return NULL;
  98. }
  99. struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsi_cmd *cmd)
  100. {
  101. struct iscsi_r2t *r2t;
  102. spin_lock_bh(&cmd->r2t_lock);
  103. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  104. if (!r2t->sent_r2t) {
  105. spin_unlock_bh(&cmd->r2t_lock);
  106. return r2t;
  107. }
  108. }
  109. spin_unlock_bh(&cmd->r2t_lock);
  110. pr_err("Unable to locate next R2T to send for ITT:"
  111. " 0x%08x.\n", cmd->init_task_tag);
  112. return NULL;
  113. }
  114. /*
  115. * Called with cmd->r2t_lock held.
  116. */
  117. void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsi_cmd *cmd)
  118. {
  119. list_del(&r2t->r2t_list);
  120. kmem_cache_free(lio_r2t_cache, r2t);
  121. }
  122. void iscsit_free_r2ts_from_list(struct iscsi_cmd *cmd)
  123. {
  124. struct iscsi_r2t *r2t, *r2t_tmp;
  125. spin_lock_bh(&cmd->r2t_lock);
  126. list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list)
  127. iscsit_free_r2t(r2t, cmd);
  128. spin_unlock_bh(&cmd->r2t_lock);
  129. }
  130. /*
  131. * May be called from software interrupt (timer) context for allocating
  132. * iSCSI NopINs.
  133. */
  134. struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, int state)
  135. {
  136. struct iscsi_cmd *cmd;
  137. struct se_session *se_sess = conn->sess->se_sess;
  138. int size, tag;
  139. tag = percpu_ida_alloc(&se_sess->sess_tag_pool, state);
  140. if (tag < 0)
  141. return NULL;
  142. size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
  143. cmd = (struct iscsi_cmd *)(se_sess->sess_cmd_map + (tag * size));
  144. memset(cmd, 0, size);
  145. cmd->se_cmd.map_tag = tag;
  146. cmd->conn = conn;
  147. INIT_LIST_HEAD(&cmd->i_conn_node);
  148. INIT_LIST_HEAD(&cmd->datain_list);
  149. INIT_LIST_HEAD(&cmd->cmd_r2t_list);
  150. spin_lock_init(&cmd->datain_lock);
  151. spin_lock_init(&cmd->dataout_timeout_lock);
  152. spin_lock_init(&cmd->istate_lock);
  153. spin_lock_init(&cmd->error_lock);
  154. spin_lock_init(&cmd->r2t_lock);
  155. return cmd;
  156. }
  157. EXPORT_SYMBOL(iscsit_allocate_cmd);
  158. struct iscsi_seq *iscsit_get_seq_holder_for_datain(
  159. struct iscsi_cmd *cmd,
  160. u32 seq_send_order)
  161. {
  162. u32 i;
  163. for (i = 0; i < cmd->seq_count; i++)
  164. if (cmd->seq_list[i].seq_send_order == seq_send_order)
  165. return &cmd->seq_list[i];
  166. return NULL;
  167. }
  168. struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
  169. {
  170. u32 i;
  171. if (!cmd->seq_list) {
  172. pr_err("struct iscsi_cmd->seq_list is NULL!\n");
  173. return NULL;
  174. }
  175. for (i = 0; i < cmd->seq_count; i++) {
  176. if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
  177. continue;
  178. if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
  179. cmd->seq_send_order++;
  180. return &cmd->seq_list[i];
  181. }
  182. }
  183. return NULL;
  184. }
  185. struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
  186. struct iscsi_cmd *cmd,
  187. u32 r2t_sn)
  188. {
  189. struct iscsi_r2t *r2t;
  190. spin_lock_bh(&cmd->r2t_lock);
  191. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  192. if (r2t->r2t_sn == r2t_sn) {
  193. spin_unlock_bh(&cmd->r2t_lock);
  194. return r2t;
  195. }
  196. }
  197. spin_unlock_bh(&cmd->r2t_lock);
  198. return NULL;
  199. }
  200. static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
  201. {
  202. int ret;
  203. /*
  204. * This is the proper method of checking received CmdSN against
  205. * ExpCmdSN and MaxCmdSN values, as well as accounting for out
  206. * or order CmdSNs due to multiple connection sessions and/or
  207. * CRC failures.
  208. */
  209. if (iscsi_sna_gt(cmdsn, sess->max_cmd_sn)) {
  210. pr_err("Received CmdSN: 0x%08x is greater than"
  211. " MaxCmdSN: 0x%08x, ignoring.\n", cmdsn,
  212. sess->max_cmd_sn);
  213. ret = CMDSN_MAXCMDSN_OVERRUN;
  214. } else if (cmdsn == sess->exp_cmd_sn) {
  215. sess->exp_cmd_sn++;
  216. pr_debug("Received CmdSN matches ExpCmdSN,"
  217. " incremented ExpCmdSN to: 0x%08x\n",
  218. sess->exp_cmd_sn);
  219. ret = CMDSN_NORMAL_OPERATION;
  220. } else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
  221. pr_debug("Received CmdSN: 0x%08x is greater"
  222. " than ExpCmdSN: 0x%08x, not acknowledging.\n",
  223. cmdsn, sess->exp_cmd_sn);
  224. ret = CMDSN_HIGHER_THAN_EXP;
  225. } else {
  226. pr_err("Received CmdSN: 0x%08x is less than"
  227. " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
  228. sess->exp_cmd_sn);
  229. ret = CMDSN_LOWER_THAN_EXP;
  230. }
  231. return ret;
  232. }
  233. /*
  234. * Commands may be received out of order if MC/S is in use.
  235. * Ensure they are executed in CmdSN order.
  236. */
  237. int iscsit_sequence_cmd(struct iscsi_conn *conn, struct iscsi_cmd *cmd,
  238. unsigned char *buf, __be32 cmdsn)
  239. {
  240. int ret, cmdsn_ret;
  241. bool reject = false;
  242. u8 reason = ISCSI_REASON_BOOKMARK_NO_RESOURCES;
  243. mutex_lock(&conn->sess->cmdsn_mutex);
  244. cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, be32_to_cpu(cmdsn));
  245. switch (cmdsn_ret) {
  246. case CMDSN_NORMAL_OPERATION:
  247. ret = iscsit_execute_cmd(cmd, 0);
  248. if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
  249. iscsit_execute_ooo_cmdsns(conn->sess);
  250. else if (ret < 0) {
  251. reject = true;
  252. ret = CMDSN_ERROR_CANNOT_RECOVER;
  253. }
  254. break;
  255. case CMDSN_HIGHER_THAN_EXP:
  256. ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, be32_to_cpu(cmdsn));
  257. if (ret < 0) {
  258. reject = true;
  259. ret = CMDSN_ERROR_CANNOT_RECOVER;
  260. break;
  261. }
  262. ret = CMDSN_HIGHER_THAN_EXP;
  263. break;
  264. case CMDSN_LOWER_THAN_EXP:
  265. case CMDSN_MAXCMDSN_OVERRUN:
  266. default:
  267. cmd->i_state = ISTATE_REMOVE;
  268. iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
  269. /*
  270. * Existing callers for iscsit_sequence_cmd() will silently
  271. * ignore commands with CMDSN_LOWER_THAN_EXP, so force this
  272. * return for CMDSN_MAXCMDSN_OVERRUN as well..
  273. */
  274. ret = CMDSN_LOWER_THAN_EXP;
  275. break;
  276. }
  277. mutex_unlock(&conn->sess->cmdsn_mutex);
  278. if (reject)
  279. iscsit_reject_cmd(cmd, reason, buf);
  280. return ret;
  281. }
  282. EXPORT_SYMBOL(iscsit_sequence_cmd);
  283. int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
  284. {
  285. struct iscsi_conn *conn = cmd->conn;
  286. struct se_cmd *se_cmd = &cmd->se_cmd;
  287. struct iscsi_data *hdr = (struct iscsi_data *) buf;
  288. u32 payload_length = ntoh24(hdr->dlength);
  289. if (conn->sess->sess_ops->InitialR2T) {
  290. pr_err("Received unexpected unsolicited data"
  291. " while InitialR2T=Yes, protocol error.\n");
  292. transport_send_check_condition_and_sense(se_cmd,
  293. TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
  294. return -1;
  295. }
  296. if ((cmd->first_burst_len + payload_length) >
  297. conn->sess->sess_ops->FirstBurstLength) {
  298. pr_err("Total %u bytes exceeds FirstBurstLength: %u"
  299. " for this Unsolicited DataOut Burst.\n",
  300. (cmd->first_burst_len + payload_length),
  301. conn->sess->sess_ops->FirstBurstLength);
  302. transport_send_check_condition_and_sense(se_cmd,
  303. TCM_INCORRECT_AMOUNT_OF_DATA, 0);
  304. return -1;
  305. }
  306. if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
  307. return 0;
  308. if (((cmd->first_burst_len + payload_length) != cmd->se_cmd.data_length) &&
  309. ((cmd->first_burst_len + payload_length) !=
  310. conn->sess->sess_ops->FirstBurstLength)) {
  311. pr_err("Unsolicited non-immediate data received %u"
  312. " does not equal FirstBurstLength: %u, and does"
  313. " not equal ExpXferLen %u.\n",
  314. (cmd->first_burst_len + payload_length),
  315. conn->sess->sess_ops->FirstBurstLength, cmd->se_cmd.data_length);
  316. transport_send_check_condition_and_sense(se_cmd,
  317. TCM_INCORRECT_AMOUNT_OF_DATA, 0);
  318. return -1;
  319. }
  320. return 0;
  321. }
  322. struct iscsi_cmd *iscsit_find_cmd_from_itt(
  323. struct iscsi_conn *conn,
  324. itt_t init_task_tag)
  325. {
  326. struct iscsi_cmd *cmd;
  327. spin_lock_bh(&conn->cmd_lock);
  328. list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
  329. if (cmd->init_task_tag == init_task_tag) {
  330. spin_unlock_bh(&conn->cmd_lock);
  331. return cmd;
  332. }
  333. }
  334. spin_unlock_bh(&conn->cmd_lock);
  335. pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
  336. init_task_tag, conn->cid);
  337. return NULL;
  338. }
  339. EXPORT_SYMBOL(iscsit_find_cmd_from_itt);
  340. struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
  341. struct iscsi_conn *conn,
  342. itt_t init_task_tag,
  343. u32 length)
  344. {
  345. struct iscsi_cmd *cmd;
  346. spin_lock_bh(&conn->cmd_lock);
  347. list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
  348. if (cmd->cmd_flags & ICF_GOT_LAST_DATAOUT)
  349. continue;
  350. if (cmd->init_task_tag == init_task_tag) {
  351. spin_unlock_bh(&conn->cmd_lock);
  352. return cmd;
  353. }
  354. }
  355. spin_unlock_bh(&conn->cmd_lock);
  356. pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
  357. " dumping payload\n", init_task_tag, conn->cid);
  358. if (length)
  359. iscsit_dump_data_payload(conn, length, 1);
  360. return NULL;
  361. }
  362. struct iscsi_cmd *iscsit_find_cmd_from_ttt(
  363. struct iscsi_conn *conn,
  364. u32 targ_xfer_tag)
  365. {
  366. struct iscsi_cmd *cmd = NULL;
  367. spin_lock_bh(&conn->cmd_lock);
  368. list_for_each_entry(cmd, &conn->conn_cmd_list, i_conn_node) {
  369. if (cmd->targ_xfer_tag == targ_xfer_tag) {
  370. spin_unlock_bh(&conn->cmd_lock);
  371. return cmd;
  372. }
  373. }
  374. spin_unlock_bh(&conn->cmd_lock);
  375. pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
  376. targ_xfer_tag, conn->cid);
  377. return NULL;
  378. }
  379. int iscsit_find_cmd_for_recovery(
  380. struct iscsi_session *sess,
  381. struct iscsi_cmd **cmd_ptr,
  382. struct iscsi_conn_recovery **cr_ptr,
  383. itt_t init_task_tag)
  384. {
  385. struct iscsi_cmd *cmd = NULL;
  386. struct iscsi_conn_recovery *cr;
  387. /*
  388. * Scan through the inactive connection recovery list's command list.
  389. * If init_task_tag matches the command is still alligent.
  390. */
  391. spin_lock(&sess->cr_i_lock);
  392. list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
  393. spin_lock(&cr->conn_recovery_cmd_lock);
  394. list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
  395. if (cmd->init_task_tag == init_task_tag) {
  396. spin_unlock(&cr->conn_recovery_cmd_lock);
  397. spin_unlock(&sess->cr_i_lock);
  398. *cr_ptr = cr;
  399. *cmd_ptr = cmd;
  400. return -2;
  401. }
  402. }
  403. spin_unlock(&cr->conn_recovery_cmd_lock);
  404. }
  405. spin_unlock(&sess->cr_i_lock);
  406. /*
  407. * Scan through the active connection recovery list's command list.
  408. * If init_task_tag matches the command is ready to be reassigned.
  409. */
  410. spin_lock(&sess->cr_a_lock);
  411. list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
  412. spin_lock(&cr->conn_recovery_cmd_lock);
  413. list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_conn_node) {
  414. if (cmd->init_task_tag == init_task_tag) {
  415. spin_unlock(&cr->conn_recovery_cmd_lock);
  416. spin_unlock(&sess->cr_a_lock);
  417. *cr_ptr = cr;
  418. *cmd_ptr = cmd;
  419. return 0;
  420. }
  421. }
  422. spin_unlock(&cr->conn_recovery_cmd_lock);
  423. }
  424. spin_unlock(&sess->cr_a_lock);
  425. return -1;
  426. }
  427. void iscsit_add_cmd_to_immediate_queue(
  428. struct iscsi_cmd *cmd,
  429. struct iscsi_conn *conn,
  430. u8 state)
  431. {
  432. struct iscsi_queue_req *qr;
  433. qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
  434. if (!qr) {
  435. pr_err("Unable to allocate memory for"
  436. " struct iscsi_queue_req\n");
  437. return;
  438. }
  439. INIT_LIST_HEAD(&qr->qr_list);
  440. qr->cmd = cmd;
  441. qr->state = state;
  442. spin_lock_bh(&conn->immed_queue_lock);
  443. list_add_tail(&qr->qr_list, &conn->immed_queue_list);
  444. atomic_inc(&cmd->immed_queue_count);
  445. atomic_set(&conn->check_immediate_queue, 1);
  446. spin_unlock_bh(&conn->immed_queue_lock);
  447. wake_up(&conn->queues_wq);
  448. }
  449. struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
  450. {
  451. struct iscsi_queue_req *qr;
  452. spin_lock_bh(&conn->immed_queue_lock);
  453. if (list_empty(&conn->immed_queue_list)) {
  454. spin_unlock_bh(&conn->immed_queue_lock);
  455. return NULL;
  456. }
  457. qr = list_first_entry(&conn->immed_queue_list,
  458. struct iscsi_queue_req, qr_list);
  459. list_del(&qr->qr_list);
  460. if (qr->cmd)
  461. atomic_dec(&qr->cmd->immed_queue_count);
  462. spin_unlock_bh(&conn->immed_queue_lock);
  463. return qr;
  464. }
  465. static void iscsit_remove_cmd_from_immediate_queue(
  466. struct iscsi_cmd *cmd,
  467. struct iscsi_conn *conn)
  468. {
  469. struct iscsi_queue_req *qr, *qr_tmp;
  470. spin_lock_bh(&conn->immed_queue_lock);
  471. if (!atomic_read(&cmd->immed_queue_count)) {
  472. spin_unlock_bh(&conn->immed_queue_lock);
  473. return;
  474. }
  475. list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
  476. if (qr->cmd != cmd)
  477. continue;
  478. atomic_dec(&qr->cmd->immed_queue_count);
  479. list_del(&qr->qr_list);
  480. kmem_cache_free(lio_qr_cache, qr);
  481. }
  482. spin_unlock_bh(&conn->immed_queue_lock);
  483. if (atomic_read(&cmd->immed_queue_count)) {
  484. pr_err("ITT: 0x%08x immed_queue_count: %d\n",
  485. cmd->init_task_tag,
  486. atomic_read(&cmd->immed_queue_count));
  487. }
  488. }
  489. void iscsit_add_cmd_to_response_queue(
  490. struct iscsi_cmd *cmd,
  491. struct iscsi_conn *conn,
  492. u8 state)
  493. {
  494. struct iscsi_queue_req *qr;
  495. qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
  496. if (!qr) {
  497. pr_err("Unable to allocate memory for"
  498. " struct iscsi_queue_req\n");
  499. return;
  500. }
  501. INIT_LIST_HEAD(&qr->qr_list);
  502. qr->cmd = cmd;
  503. qr->state = state;
  504. spin_lock_bh(&conn->response_queue_lock);
  505. list_add_tail(&qr->qr_list, &conn->response_queue_list);
  506. atomic_inc(&cmd->response_queue_count);
  507. spin_unlock_bh(&conn->response_queue_lock);
  508. wake_up(&conn->queues_wq);
  509. }
  510. struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
  511. {
  512. struct iscsi_queue_req *qr;
  513. spin_lock_bh(&conn->response_queue_lock);
  514. if (list_empty(&conn->response_queue_list)) {
  515. spin_unlock_bh(&conn->response_queue_lock);
  516. return NULL;
  517. }
  518. qr = list_first_entry(&conn->response_queue_list,
  519. struct iscsi_queue_req, qr_list);
  520. list_del(&qr->qr_list);
  521. if (qr->cmd)
  522. atomic_dec(&qr->cmd->response_queue_count);
  523. spin_unlock_bh(&conn->response_queue_lock);
  524. return qr;
  525. }
  526. static void iscsit_remove_cmd_from_response_queue(
  527. struct iscsi_cmd *cmd,
  528. struct iscsi_conn *conn)
  529. {
  530. struct iscsi_queue_req *qr, *qr_tmp;
  531. spin_lock_bh(&conn->response_queue_lock);
  532. if (!atomic_read(&cmd->response_queue_count)) {
  533. spin_unlock_bh(&conn->response_queue_lock);
  534. return;
  535. }
  536. list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
  537. qr_list) {
  538. if (qr->cmd != cmd)
  539. continue;
  540. atomic_dec(&qr->cmd->response_queue_count);
  541. list_del(&qr->qr_list);
  542. kmem_cache_free(lio_qr_cache, qr);
  543. }
  544. spin_unlock_bh(&conn->response_queue_lock);
  545. if (atomic_read(&cmd->response_queue_count)) {
  546. pr_err("ITT: 0x%08x response_queue_count: %d\n",
  547. cmd->init_task_tag,
  548. atomic_read(&cmd->response_queue_count));
  549. }
  550. }
  551. bool iscsit_conn_all_queues_empty(struct iscsi_conn *conn)
  552. {
  553. bool empty;
  554. spin_lock_bh(&conn->immed_queue_lock);
  555. empty = list_empty(&conn->immed_queue_list);
  556. spin_unlock_bh(&conn->immed_queue_lock);
  557. if (!empty)
  558. return empty;
  559. spin_lock_bh(&conn->response_queue_lock);
  560. empty = list_empty(&conn->response_queue_list);
  561. spin_unlock_bh(&conn->response_queue_lock);
  562. return empty;
  563. }
  564. void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
  565. {
  566. struct iscsi_queue_req *qr, *qr_tmp;
  567. spin_lock_bh(&conn->immed_queue_lock);
  568. list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
  569. list_del(&qr->qr_list);
  570. if (qr->cmd)
  571. atomic_dec(&qr->cmd->immed_queue_count);
  572. kmem_cache_free(lio_qr_cache, qr);
  573. }
  574. spin_unlock_bh(&conn->immed_queue_lock);
  575. spin_lock_bh(&conn->response_queue_lock);
  576. list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
  577. qr_list) {
  578. list_del(&qr->qr_list);
  579. if (qr->cmd)
  580. atomic_dec(&qr->cmd->response_queue_count);
  581. kmem_cache_free(lio_qr_cache, qr);
  582. }
  583. spin_unlock_bh(&conn->response_queue_lock);
  584. }
  585. void iscsit_release_cmd(struct iscsi_cmd *cmd)
  586. {
  587. struct iscsi_session *sess;
  588. struct se_cmd *se_cmd = &cmd->se_cmd;
  589. if (cmd->conn)
  590. sess = cmd->conn->sess;
  591. else
  592. sess = cmd->sess;
  593. BUG_ON(!sess || !sess->se_sess);
  594. kfree(cmd->buf_ptr);
  595. kfree(cmd->pdu_list);
  596. kfree(cmd->seq_list);
  597. kfree(cmd->tmr_req);
  598. kfree(cmd->iov_data);
  599. kfree(cmd->text_in_ptr);
  600. percpu_ida_free(&sess->se_sess->sess_tag_pool, se_cmd->map_tag);
  601. }
  602. EXPORT_SYMBOL(iscsit_release_cmd);
  603. void __iscsit_free_cmd(struct iscsi_cmd *cmd, bool scsi_cmd,
  604. bool check_queues)
  605. {
  606. struct iscsi_conn *conn = cmd->conn;
  607. if (scsi_cmd) {
  608. if (cmd->data_direction == DMA_TO_DEVICE) {
  609. iscsit_stop_dataout_timer(cmd);
  610. iscsit_free_r2ts_from_list(cmd);
  611. }
  612. if (cmd->data_direction == DMA_FROM_DEVICE)
  613. iscsit_free_all_datain_reqs(cmd);
  614. }
  615. if (conn && check_queues) {
  616. iscsit_remove_cmd_from_immediate_queue(cmd, conn);
  617. iscsit_remove_cmd_from_response_queue(cmd, conn);
  618. }
  619. }
  620. void iscsit_free_cmd(struct iscsi_cmd *cmd, bool shutdown)
  621. {
  622. struct se_cmd *se_cmd = NULL;
  623. int rc;
  624. /*
  625. * Determine if a struct se_cmd is associated with
  626. * this struct iscsi_cmd.
  627. */
  628. switch (cmd->iscsi_opcode) {
  629. case ISCSI_OP_SCSI_CMD:
  630. se_cmd = &cmd->se_cmd;
  631. __iscsit_free_cmd(cmd, true, shutdown);
  632. /*
  633. * Fallthrough
  634. */
  635. case ISCSI_OP_SCSI_TMFUNC:
  636. rc = transport_generic_free_cmd(&cmd->se_cmd, shutdown);
  637. if (!rc && shutdown && se_cmd && se_cmd->se_sess) {
  638. __iscsit_free_cmd(cmd, true, shutdown);
  639. target_put_sess_cmd(se_cmd);
  640. }
  641. break;
  642. case ISCSI_OP_REJECT:
  643. /*
  644. * Handle special case for REJECT when iscsi_add_reject*() has
  645. * overwritten the original iscsi_opcode assignment, and the
  646. * associated cmd->se_cmd needs to be released.
  647. */
  648. if (cmd->se_cmd.se_tfo != NULL) {
  649. se_cmd = &cmd->se_cmd;
  650. __iscsit_free_cmd(cmd, true, shutdown);
  651. rc = transport_generic_free_cmd(&cmd->se_cmd, shutdown);
  652. if (!rc && shutdown && se_cmd->se_sess) {
  653. __iscsit_free_cmd(cmd, true, shutdown);
  654. target_put_sess_cmd(se_cmd);
  655. }
  656. break;
  657. }
  658. /* Fall-through */
  659. default:
  660. __iscsit_free_cmd(cmd, false, shutdown);
  661. iscsit_release_cmd(cmd);
  662. break;
  663. }
  664. }
  665. int iscsit_check_session_usage_count(struct iscsi_session *sess)
  666. {
  667. spin_lock_bh(&sess->session_usage_lock);
  668. if (sess->session_usage_count != 0) {
  669. sess->session_waiting_on_uc = 1;
  670. spin_unlock_bh(&sess->session_usage_lock);
  671. if (in_interrupt())
  672. return 2;
  673. wait_for_completion(&sess->session_waiting_on_uc_comp);
  674. return 1;
  675. }
  676. spin_unlock_bh(&sess->session_usage_lock);
  677. return 0;
  678. }
  679. void iscsit_dec_session_usage_count(struct iscsi_session *sess)
  680. {
  681. spin_lock_bh(&sess->session_usage_lock);
  682. sess->session_usage_count--;
  683. if (!sess->session_usage_count && sess->session_waiting_on_uc)
  684. complete(&sess->session_waiting_on_uc_comp);
  685. spin_unlock_bh(&sess->session_usage_lock);
  686. }
  687. void iscsit_inc_session_usage_count(struct iscsi_session *sess)
  688. {
  689. spin_lock_bh(&sess->session_usage_lock);
  690. sess->session_usage_count++;
  691. spin_unlock_bh(&sess->session_usage_lock);
  692. }
  693. struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
  694. {
  695. struct iscsi_conn *conn;
  696. spin_lock_bh(&sess->conn_lock);
  697. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  698. if ((conn->cid == cid) &&
  699. (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
  700. iscsit_inc_conn_usage_count(conn);
  701. spin_unlock_bh(&sess->conn_lock);
  702. return conn;
  703. }
  704. }
  705. spin_unlock_bh(&sess->conn_lock);
  706. return NULL;
  707. }
  708. struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
  709. {
  710. struct iscsi_conn *conn;
  711. spin_lock_bh(&sess->conn_lock);
  712. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  713. if (conn->cid == cid) {
  714. iscsit_inc_conn_usage_count(conn);
  715. spin_lock(&conn->state_lock);
  716. atomic_set(&conn->connection_wait_rcfr, 1);
  717. spin_unlock(&conn->state_lock);
  718. spin_unlock_bh(&sess->conn_lock);
  719. return conn;
  720. }
  721. }
  722. spin_unlock_bh(&sess->conn_lock);
  723. return NULL;
  724. }
  725. void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
  726. {
  727. spin_lock_bh(&conn->conn_usage_lock);
  728. if (conn->conn_usage_count != 0) {
  729. conn->conn_waiting_on_uc = 1;
  730. spin_unlock_bh(&conn->conn_usage_lock);
  731. wait_for_completion(&conn->conn_waiting_on_uc_comp);
  732. return;
  733. }
  734. spin_unlock_bh(&conn->conn_usage_lock);
  735. }
  736. void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
  737. {
  738. spin_lock_bh(&conn->conn_usage_lock);
  739. conn->conn_usage_count--;
  740. if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
  741. complete(&conn->conn_waiting_on_uc_comp);
  742. spin_unlock_bh(&conn->conn_usage_lock);
  743. }
  744. void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
  745. {
  746. spin_lock_bh(&conn->conn_usage_lock);
  747. conn->conn_usage_count++;
  748. spin_unlock_bh(&conn->conn_usage_lock);
  749. }
  750. static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
  751. {
  752. u8 state;
  753. struct iscsi_cmd *cmd;
  754. cmd = iscsit_allocate_cmd(conn, TASK_RUNNING);
  755. if (!cmd)
  756. return -1;
  757. cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
  758. state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
  759. ISTATE_SEND_NOPIN_NO_RESPONSE;
  760. cmd->init_task_tag = RESERVED_ITT;
  761. cmd->targ_xfer_tag = (want_response) ?
  762. session_get_next_ttt(conn->sess) : 0xFFFFFFFF;
  763. spin_lock_bh(&conn->cmd_lock);
  764. list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
  765. spin_unlock_bh(&conn->cmd_lock);
  766. if (want_response)
  767. iscsit_start_nopin_response_timer(conn);
  768. iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
  769. return 0;
  770. }
  771. static void iscsit_handle_nopin_response_timeout(unsigned long data)
  772. {
  773. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  774. iscsit_inc_conn_usage_count(conn);
  775. spin_lock_bh(&conn->nopin_timer_lock);
  776. if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
  777. spin_unlock_bh(&conn->nopin_timer_lock);
  778. iscsit_dec_conn_usage_count(conn);
  779. return;
  780. }
  781. pr_debug("Did not receive response to NOPIN on CID: %hu on"
  782. " SID: %u, failing connection.\n", conn->cid,
  783. conn->sess->sid);
  784. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  785. spin_unlock_bh(&conn->nopin_timer_lock);
  786. {
  787. struct iscsi_portal_group *tpg = conn->sess->tpg;
  788. struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
  789. if (tiqn) {
  790. spin_lock_bh(&tiqn->sess_err_stats.lock);
  791. strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
  792. conn->sess->sess_ops->InitiatorName);
  793. tiqn->sess_err_stats.last_sess_failure_type =
  794. ISCSI_SESS_ERR_CXN_TIMEOUT;
  795. tiqn->sess_err_stats.cxn_timeout_errors++;
  796. atomic_long_inc(&conn->sess->conn_timeout_errors);
  797. spin_unlock_bh(&tiqn->sess_err_stats.lock);
  798. }
  799. }
  800. iscsit_cause_connection_reinstatement(conn, 0);
  801. iscsit_dec_conn_usage_count(conn);
  802. }
  803. void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
  804. {
  805. struct iscsi_session *sess = conn->sess;
  806. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  807. spin_lock_bh(&conn->nopin_timer_lock);
  808. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  809. spin_unlock_bh(&conn->nopin_timer_lock);
  810. return;
  811. }
  812. mod_timer(&conn->nopin_response_timer,
  813. (get_jiffies_64() + na->nopin_response_timeout * HZ));
  814. spin_unlock_bh(&conn->nopin_timer_lock);
  815. }
  816. /*
  817. * Called with conn->nopin_timer_lock held.
  818. */
  819. void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
  820. {
  821. struct iscsi_session *sess = conn->sess;
  822. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  823. spin_lock_bh(&conn->nopin_timer_lock);
  824. if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
  825. spin_unlock_bh(&conn->nopin_timer_lock);
  826. return;
  827. }
  828. init_timer(&conn->nopin_response_timer);
  829. conn->nopin_response_timer.expires =
  830. (get_jiffies_64() + na->nopin_response_timeout * HZ);
  831. conn->nopin_response_timer.data = (unsigned long)conn;
  832. conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
  833. conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
  834. conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
  835. add_timer(&conn->nopin_response_timer);
  836. pr_debug("Started NOPIN Response Timer on CID: %d to %u"
  837. " seconds\n", conn->cid, na->nopin_response_timeout);
  838. spin_unlock_bh(&conn->nopin_timer_lock);
  839. }
  840. void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
  841. {
  842. spin_lock_bh(&conn->nopin_timer_lock);
  843. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  844. spin_unlock_bh(&conn->nopin_timer_lock);
  845. return;
  846. }
  847. conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
  848. spin_unlock_bh(&conn->nopin_timer_lock);
  849. del_timer_sync(&conn->nopin_response_timer);
  850. spin_lock_bh(&conn->nopin_timer_lock);
  851. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  852. spin_unlock_bh(&conn->nopin_timer_lock);
  853. }
  854. static void iscsit_handle_nopin_timeout(unsigned long data)
  855. {
  856. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  857. iscsit_inc_conn_usage_count(conn);
  858. spin_lock_bh(&conn->nopin_timer_lock);
  859. if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
  860. spin_unlock_bh(&conn->nopin_timer_lock);
  861. iscsit_dec_conn_usage_count(conn);
  862. return;
  863. }
  864. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  865. spin_unlock_bh(&conn->nopin_timer_lock);
  866. iscsit_add_nopin(conn, 1);
  867. iscsit_dec_conn_usage_count(conn);
  868. }
  869. /*
  870. * Called with conn->nopin_timer_lock held.
  871. */
  872. void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
  873. {
  874. struct iscsi_session *sess = conn->sess;
  875. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  876. /*
  877. * NOPIN timeout is disabled.
  878. */
  879. if (!na->nopin_timeout)
  880. return;
  881. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
  882. return;
  883. init_timer(&conn->nopin_timer);
  884. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  885. conn->nopin_timer.data = (unsigned long)conn;
  886. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  887. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  888. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  889. add_timer(&conn->nopin_timer);
  890. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  891. " interval\n", conn->cid, na->nopin_timeout);
  892. }
  893. void iscsit_start_nopin_timer(struct iscsi_conn *conn)
  894. {
  895. struct iscsi_session *sess = conn->sess;
  896. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  897. /*
  898. * NOPIN timeout is disabled..
  899. */
  900. if (!na->nopin_timeout)
  901. return;
  902. spin_lock_bh(&conn->nopin_timer_lock);
  903. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
  904. spin_unlock_bh(&conn->nopin_timer_lock);
  905. return;
  906. }
  907. init_timer(&conn->nopin_timer);
  908. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  909. conn->nopin_timer.data = (unsigned long)conn;
  910. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  911. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  912. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  913. add_timer(&conn->nopin_timer);
  914. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  915. " interval\n", conn->cid, na->nopin_timeout);
  916. spin_unlock_bh(&conn->nopin_timer_lock);
  917. }
  918. void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
  919. {
  920. spin_lock_bh(&conn->nopin_timer_lock);
  921. if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
  922. spin_unlock_bh(&conn->nopin_timer_lock);
  923. return;
  924. }
  925. conn->nopin_timer_flags |= ISCSI_TF_STOP;
  926. spin_unlock_bh(&conn->nopin_timer_lock);
  927. del_timer_sync(&conn->nopin_timer);
  928. spin_lock_bh(&conn->nopin_timer_lock);
  929. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  930. spin_unlock_bh(&conn->nopin_timer_lock);
  931. }
  932. int iscsit_send_tx_data(
  933. struct iscsi_cmd *cmd,
  934. struct iscsi_conn *conn,
  935. int use_misc)
  936. {
  937. int tx_sent, tx_size;
  938. u32 iov_count;
  939. struct kvec *iov;
  940. send_data:
  941. tx_size = cmd->tx_size;
  942. if (!use_misc) {
  943. iov = &cmd->iov_data[0];
  944. iov_count = cmd->iov_data_count;
  945. } else {
  946. iov = &cmd->iov_misc[0];
  947. iov_count = cmd->iov_misc_count;
  948. }
  949. tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
  950. if (tx_size != tx_sent) {
  951. if (tx_sent == -EAGAIN) {
  952. pr_err("tx_data() returned -EAGAIN\n");
  953. goto send_data;
  954. } else
  955. return -1;
  956. }
  957. cmd->tx_size = 0;
  958. return 0;
  959. }
  960. int iscsit_fe_sendpage_sg(
  961. struct iscsi_cmd *cmd,
  962. struct iscsi_conn *conn)
  963. {
  964. struct scatterlist *sg = cmd->first_data_sg;
  965. struct kvec iov;
  966. u32 tx_hdr_size, data_len;
  967. u32 offset = cmd->first_data_sg_off;
  968. int tx_sent, iov_off;
  969. send_hdr:
  970. tx_hdr_size = ISCSI_HDR_LEN;
  971. if (conn->conn_ops->HeaderDigest)
  972. tx_hdr_size += ISCSI_CRC_LEN;
  973. iov.iov_base = cmd->pdu;
  974. iov.iov_len = tx_hdr_size;
  975. tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
  976. if (tx_hdr_size != tx_sent) {
  977. if (tx_sent == -EAGAIN) {
  978. pr_err("tx_data() returned -EAGAIN\n");
  979. goto send_hdr;
  980. }
  981. return -1;
  982. }
  983. data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
  984. /*
  985. * Set iov_off used by padding and data digest tx_data() calls below
  986. * in order to determine proper offset into cmd->iov_data[]
  987. */
  988. if (conn->conn_ops->DataDigest) {
  989. data_len -= ISCSI_CRC_LEN;
  990. if (cmd->padding)
  991. iov_off = (cmd->iov_data_count - 2);
  992. else
  993. iov_off = (cmd->iov_data_count - 1);
  994. } else {
  995. iov_off = (cmd->iov_data_count - 1);
  996. }
  997. /*
  998. * Perform sendpage() for each page in the scatterlist
  999. */
  1000. while (data_len) {
  1001. u32 space = (sg->length - offset);
  1002. u32 sub_len = min_t(u32, data_len, space);
  1003. send_pg:
  1004. tx_sent = conn->sock->ops->sendpage(conn->sock,
  1005. sg_page(sg), sg->offset + offset, sub_len, 0);
  1006. if (tx_sent != sub_len) {
  1007. if (tx_sent == -EAGAIN) {
  1008. pr_err("tcp_sendpage() returned"
  1009. " -EAGAIN\n");
  1010. goto send_pg;
  1011. }
  1012. pr_err("tcp_sendpage() failure: %d\n",
  1013. tx_sent);
  1014. return -1;
  1015. }
  1016. data_len -= sub_len;
  1017. offset = 0;
  1018. sg = sg_next(sg);
  1019. }
  1020. send_padding:
  1021. if (cmd->padding) {
  1022. struct kvec *iov_p = &cmd->iov_data[iov_off++];
  1023. tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
  1024. if (cmd->padding != tx_sent) {
  1025. if (tx_sent == -EAGAIN) {
  1026. pr_err("tx_data() returned -EAGAIN\n");
  1027. goto send_padding;
  1028. }
  1029. return -1;
  1030. }
  1031. }
  1032. send_datacrc:
  1033. if (conn->conn_ops->DataDigest) {
  1034. struct kvec *iov_d = &cmd->iov_data[iov_off];
  1035. tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
  1036. if (ISCSI_CRC_LEN != tx_sent) {
  1037. if (tx_sent == -EAGAIN) {
  1038. pr_err("tx_data() returned -EAGAIN\n");
  1039. goto send_datacrc;
  1040. }
  1041. return -1;
  1042. }
  1043. }
  1044. return 0;
  1045. }
  1046. /*
  1047. * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
  1048. * back to the Initiator when an expection condition occurs with the
  1049. * errors set in status_class and status_detail.
  1050. *
  1051. * Parameters: iSCSI Connection, Status Class, Status Detail.
  1052. * Returns: 0 on success, -1 on error.
  1053. */
  1054. int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
  1055. {
  1056. struct iscsi_login_rsp *hdr;
  1057. struct iscsi_login *login = conn->conn_login;
  1058. login->login_failed = 1;
  1059. iscsit_collect_login_stats(conn, status_class, status_detail);
  1060. memset(&login->rsp[0], 0, ISCSI_HDR_LEN);
  1061. hdr = (struct iscsi_login_rsp *)&login->rsp[0];
  1062. hdr->opcode = ISCSI_OP_LOGIN_RSP;
  1063. hdr->status_class = status_class;
  1064. hdr->status_detail = status_detail;
  1065. hdr->itt = conn->login_itt;
  1066. return conn->conn_transport->iscsit_put_login_tx(conn, login, 0);
  1067. }
  1068. void iscsit_print_session_params(struct iscsi_session *sess)
  1069. {
  1070. struct iscsi_conn *conn;
  1071. pr_debug("-----------------------------[Session Params for"
  1072. " SID: %u]-----------------------------\n", sess->sid);
  1073. spin_lock_bh(&sess->conn_lock);
  1074. list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
  1075. iscsi_dump_conn_ops(conn->conn_ops);
  1076. spin_unlock_bh(&sess->conn_lock);
  1077. iscsi_dump_sess_ops(sess->sess_ops);
  1078. }
  1079. static int iscsit_do_rx_data(
  1080. struct iscsi_conn *conn,
  1081. struct iscsi_data_count *count)
  1082. {
  1083. int data = count->data_length, rx_loop = 0, total_rx = 0;
  1084. struct msghdr msg;
  1085. if (!conn || !conn->sock || !conn->conn_ops)
  1086. return -1;
  1087. memset(&msg, 0, sizeof(struct msghdr));
  1088. iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC,
  1089. count->iov, count->iov_count, data);
  1090. while (total_rx < data) {
  1091. rx_loop = sock_recvmsg(conn->sock, &msg,
  1092. (data - total_rx), MSG_WAITALL);
  1093. if (rx_loop <= 0) {
  1094. pr_debug("rx_loop: %d total_rx: %d\n",
  1095. rx_loop, total_rx);
  1096. return rx_loop;
  1097. }
  1098. total_rx += rx_loop;
  1099. pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
  1100. rx_loop, total_rx, data);
  1101. }
  1102. return total_rx;
  1103. }
  1104. static int iscsit_do_tx_data(
  1105. struct iscsi_conn *conn,
  1106. struct iscsi_data_count *count)
  1107. {
  1108. int ret, iov_len;
  1109. struct kvec *iov_p;
  1110. struct msghdr msg;
  1111. if (!conn || !conn->sock || !conn->conn_ops)
  1112. return -1;
  1113. if (count->data_length <= 0) {
  1114. pr_err("Data length is: %d\n", count->data_length);
  1115. return -1;
  1116. }
  1117. memset(&msg, 0, sizeof(struct msghdr));
  1118. iov_p = count->iov;
  1119. iov_len = count->iov_count;
  1120. ret = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
  1121. count->data_length);
  1122. if (ret != count->data_length) {
  1123. pr_err("Unexpected ret: %d send data %d\n",
  1124. ret, count->data_length);
  1125. return -EPIPE;
  1126. }
  1127. pr_debug("ret: %d, sent data: %d\n", ret, count->data_length);
  1128. return ret;
  1129. }
  1130. int rx_data(
  1131. struct iscsi_conn *conn,
  1132. struct kvec *iov,
  1133. int iov_count,
  1134. int data)
  1135. {
  1136. struct iscsi_data_count c;
  1137. if (!conn || !conn->sock || !conn->conn_ops)
  1138. return -1;
  1139. memset(&c, 0, sizeof(struct iscsi_data_count));
  1140. c.iov = iov;
  1141. c.iov_count = iov_count;
  1142. c.data_length = data;
  1143. c.type = ISCSI_RX_DATA;
  1144. return iscsit_do_rx_data(conn, &c);
  1145. }
  1146. int tx_data(
  1147. struct iscsi_conn *conn,
  1148. struct kvec *iov,
  1149. int iov_count,
  1150. int data)
  1151. {
  1152. struct iscsi_data_count c;
  1153. if (!conn || !conn->sock || !conn->conn_ops)
  1154. return -1;
  1155. memset(&c, 0, sizeof(struct iscsi_data_count));
  1156. c.iov = iov;
  1157. c.iov_count = iov_count;
  1158. c.data_length = data;
  1159. c.type = ISCSI_TX_DATA;
  1160. return iscsit_do_tx_data(conn, &c);
  1161. }
  1162. void iscsit_collect_login_stats(
  1163. struct iscsi_conn *conn,
  1164. u8 status_class,
  1165. u8 status_detail)
  1166. {
  1167. struct iscsi_param *intrname = NULL;
  1168. struct iscsi_tiqn *tiqn;
  1169. struct iscsi_login_stats *ls;
  1170. tiqn = iscsit_snmp_get_tiqn(conn);
  1171. if (!tiqn)
  1172. return;
  1173. ls = &tiqn->login_stats;
  1174. spin_lock(&ls->lock);
  1175. if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
  1176. ((get_jiffies_64() - ls->last_fail_time) < 10)) {
  1177. /* We already have the failure info for this login */
  1178. spin_unlock(&ls->lock);
  1179. return;
  1180. }
  1181. if (status_class == ISCSI_STATUS_CLS_SUCCESS)
  1182. ls->accepts++;
  1183. else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
  1184. ls->redirects++;
  1185. ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
  1186. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1187. (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
  1188. ls->authenticate_fails++;
  1189. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
  1190. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1191. (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
  1192. ls->authorize_fails++;
  1193. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
  1194. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1195. (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
  1196. ls->negotiate_fails++;
  1197. ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
  1198. } else {
  1199. ls->other_fails++;
  1200. ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
  1201. }
  1202. /* Save initiator name, ip address and time, if it is a failed login */
  1203. if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
  1204. if (conn->param_list)
  1205. intrname = iscsi_find_param_from_key(INITIATORNAME,
  1206. conn->param_list);
  1207. strlcpy(ls->last_intr_fail_name,
  1208. (intrname ? intrname->value : "Unknown"),
  1209. sizeof(ls->last_intr_fail_name));
  1210. ls->last_intr_fail_ip_family = conn->login_family;
  1211. snprintf(ls->last_intr_fail_ip_addr, IPV6_ADDRESS_SPACE,
  1212. "%s", conn->login_ip);
  1213. ls->last_fail_time = get_jiffies_64();
  1214. }
  1215. spin_unlock(&ls->lock);
  1216. }
  1217. struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
  1218. {
  1219. struct iscsi_portal_group *tpg;
  1220. if (!conn || !conn->sess)
  1221. return NULL;
  1222. tpg = conn->sess->tpg;
  1223. if (!tpg)
  1224. return NULL;
  1225. if (!tpg->tpg_tiqn)
  1226. return NULL;
  1227. return tpg->tpg_tiqn;
  1228. }