iscsi_target_util.c 38 KB

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