iscsi_target_util.c 38 KB

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