iscsi_target_util.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817
  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_transport.h>
  25. #include <target/target_core_tmr.h>
  26. #include <target/target_core_fabric_ops.h>
  27. #include <target/target_core_configfs.h>
  28. #include "iscsi_target_core.h"
  29. #include "iscsi_target_parameters.h"
  30. #include "iscsi_target_seq_pdu_list.h"
  31. #include "iscsi_target_datain_values.h"
  32. #include "iscsi_target_erl0.h"
  33. #include "iscsi_target_erl1.h"
  34. #include "iscsi_target_erl2.h"
  35. #include "iscsi_target_tpg.h"
  36. #include "iscsi_target_tq.h"
  37. #include "iscsi_target_util.h"
  38. #include "iscsi_target.h"
  39. #define PRINT_BUFF(buff, len) \
  40. { \
  41. int zzz; \
  42. \
  43. pr_debug("%d:\n", __LINE__); \
  44. for (zzz = 0; zzz < len; zzz++) { \
  45. if (zzz % 16 == 0) { \
  46. if (zzz) \
  47. pr_debug("\n"); \
  48. pr_debug("%4i: ", zzz); \
  49. } \
  50. pr_debug("%02x ", (unsigned char) (buff)[zzz]); \
  51. } \
  52. if ((len + 1) % 16) \
  53. pr_debug("\n"); \
  54. }
  55. extern struct list_head g_tiqn_list;
  56. extern spinlock_t tiqn_lock;
  57. /*
  58. * Called with cmd->r2t_lock held.
  59. */
  60. int iscsit_add_r2t_to_list(
  61. struct iscsi_cmd *cmd,
  62. u32 offset,
  63. u32 xfer_len,
  64. int recovery,
  65. u32 r2t_sn)
  66. {
  67. struct iscsi_r2t *r2t;
  68. r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC);
  69. if (!r2t) {
  70. pr_err("Unable to allocate memory for struct iscsi_r2t.\n");
  71. return -1;
  72. }
  73. INIT_LIST_HEAD(&r2t->r2t_list);
  74. r2t->recovery_r2t = recovery;
  75. r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn;
  76. r2t->offset = offset;
  77. r2t->xfer_len = xfer_len;
  78. list_add_tail(&r2t->r2t_list, &cmd->cmd_r2t_list);
  79. spin_unlock_bh(&cmd->r2t_lock);
  80. iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
  81. spin_lock_bh(&cmd->r2t_lock);
  82. return 0;
  83. }
  84. struct iscsi_r2t *iscsit_get_r2t_for_eos(
  85. struct iscsi_cmd *cmd,
  86. u32 offset,
  87. u32 length)
  88. {
  89. struct iscsi_r2t *r2t;
  90. spin_lock_bh(&cmd->r2t_lock);
  91. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  92. if ((r2t->offset <= offset) &&
  93. (r2t->offset + r2t->xfer_len) >= (offset + length)) {
  94. spin_unlock_bh(&cmd->r2t_lock);
  95. return r2t;
  96. }
  97. }
  98. spin_unlock_bh(&cmd->r2t_lock);
  99. pr_err("Unable to locate R2T for Offset: %u, Length:"
  100. " %u\n", offset, length);
  101. return NULL;
  102. }
  103. struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsi_cmd *cmd)
  104. {
  105. struct iscsi_r2t *r2t;
  106. spin_lock_bh(&cmd->r2t_lock);
  107. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  108. if (!r2t->sent_r2t) {
  109. spin_unlock_bh(&cmd->r2t_lock);
  110. return r2t;
  111. }
  112. }
  113. spin_unlock_bh(&cmd->r2t_lock);
  114. pr_err("Unable to locate next R2T to send for ITT:"
  115. " 0x%08x.\n", cmd->init_task_tag);
  116. return NULL;
  117. }
  118. /*
  119. * Called with cmd->r2t_lock held.
  120. */
  121. void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsi_cmd *cmd)
  122. {
  123. list_del(&r2t->r2t_list);
  124. kmem_cache_free(lio_r2t_cache, r2t);
  125. }
  126. void iscsit_free_r2ts_from_list(struct iscsi_cmd *cmd)
  127. {
  128. struct iscsi_r2t *r2t, *r2t_tmp;
  129. spin_lock_bh(&cmd->r2t_lock);
  130. list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list)
  131. iscsit_free_r2t(r2t, cmd);
  132. spin_unlock_bh(&cmd->r2t_lock);
  133. }
  134. /*
  135. * May be called from software interrupt (timer) context for allocating
  136. * iSCSI NopINs.
  137. */
  138. struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp_mask)
  139. {
  140. struct iscsi_cmd *cmd;
  141. cmd = kmem_cache_zalloc(lio_cmd_cache, gfp_mask);
  142. if (!cmd) {
  143. pr_err("Unable to allocate memory for struct iscsi_cmd.\n");
  144. return NULL;
  145. }
  146. cmd->conn = conn;
  147. INIT_LIST_HEAD(&cmd->i_list);
  148. INIT_LIST_HEAD(&cmd->datain_list);
  149. INIT_LIST_HEAD(&cmd->cmd_r2t_list);
  150. init_completion(&cmd->reject_comp);
  151. spin_lock_init(&cmd->datain_lock);
  152. spin_lock_init(&cmd->dataout_timeout_lock);
  153. spin_lock_init(&cmd->istate_lock);
  154. spin_lock_init(&cmd->error_lock);
  155. spin_lock_init(&cmd->r2t_lock);
  156. return cmd;
  157. }
  158. /*
  159. * Called from iscsi_handle_scsi_cmd()
  160. */
  161. struct iscsi_cmd *iscsit_allocate_se_cmd(
  162. struct iscsi_conn *conn,
  163. u32 data_length,
  164. int data_direction,
  165. int iscsi_task_attr)
  166. {
  167. struct iscsi_cmd *cmd;
  168. struct se_cmd *se_cmd;
  169. int sam_task_attr;
  170. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  171. if (!cmd)
  172. return NULL;
  173. cmd->data_direction = data_direction;
  174. cmd->data_length = data_length;
  175. /*
  176. * Figure out the SAM Task Attribute for the incoming SCSI CDB
  177. */
  178. if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
  179. (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
  180. sam_task_attr = MSG_SIMPLE_TAG;
  181. else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
  182. sam_task_attr = MSG_ORDERED_TAG;
  183. else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
  184. sam_task_attr = MSG_HEAD_TAG;
  185. else if (iscsi_task_attr == ISCSI_ATTR_ACA)
  186. sam_task_attr = MSG_ACA_TAG;
  187. else {
  188. pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
  189. " MSG_SIMPLE_TAG\n", iscsi_task_attr);
  190. sam_task_attr = MSG_SIMPLE_TAG;
  191. }
  192. se_cmd = &cmd->se_cmd;
  193. /*
  194. * Initialize struct se_cmd descriptor from target_core_mod infrastructure
  195. */
  196. transport_init_se_cmd(se_cmd, &lio_target_fabric_configfs->tf_ops,
  197. conn->sess->se_sess, data_length, data_direction,
  198. sam_task_attr, &cmd->sense_buffer[0]);
  199. return cmd;
  200. }
  201. struct iscsi_cmd *iscsit_allocate_se_cmd_for_tmr(
  202. struct iscsi_conn *conn,
  203. u8 function)
  204. {
  205. struct iscsi_cmd *cmd;
  206. struct se_cmd *se_cmd;
  207. u8 tcm_function;
  208. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  209. if (!cmd)
  210. return NULL;
  211. cmd->data_direction = DMA_NONE;
  212. cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
  213. if (!cmd->tmr_req) {
  214. pr_err("Unable to allocate memory for"
  215. " Task Management command!\n");
  216. goto out;
  217. }
  218. /*
  219. * TASK_REASSIGN for ERL=2 / connection stays inside of
  220. * LIO-Target $FABRIC_MOD
  221. */
  222. if (function == ISCSI_TM_FUNC_TASK_REASSIGN)
  223. return cmd;
  224. se_cmd = &cmd->se_cmd;
  225. /*
  226. * Initialize struct se_cmd descriptor from target_core_mod infrastructure
  227. */
  228. transport_init_se_cmd(se_cmd, &lio_target_fabric_configfs->tf_ops,
  229. conn->sess->se_sess, 0, DMA_NONE,
  230. MSG_SIMPLE_TAG, &cmd->sense_buffer[0]);
  231. switch (function) {
  232. case ISCSI_TM_FUNC_ABORT_TASK:
  233. tcm_function = TMR_ABORT_TASK;
  234. break;
  235. case ISCSI_TM_FUNC_ABORT_TASK_SET:
  236. tcm_function = TMR_ABORT_TASK_SET;
  237. break;
  238. case ISCSI_TM_FUNC_CLEAR_ACA:
  239. tcm_function = TMR_CLEAR_ACA;
  240. break;
  241. case ISCSI_TM_FUNC_CLEAR_TASK_SET:
  242. tcm_function = TMR_CLEAR_TASK_SET;
  243. break;
  244. case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
  245. tcm_function = TMR_LUN_RESET;
  246. break;
  247. case ISCSI_TM_FUNC_TARGET_WARM_RESET:
  248. tcm_function = TMR_TARGET_WARM_RESET;
  249. break;
  250. case ISCSI_TM_FUNC_TARGET_COLD_RESET:
  251. tcm_function = TMR_TARGET_COLD_RESET;
  252. break;
  253. default:
  254. pr_err("Unknown iSCSI TMR Function:"
  255. " 0x%02x\n", function);
  256. goto out;
  257. }
  258. se_cmd->se_tmr_req = core_tmr_alloc_req(se_cmd,
  259. (void *)cmd->tmr_req, tcm_function);
  260. if (!se_cmd->se_tmr_req)
  261. goto out;
  262. cmd->tmr_req->se_tmr_req = se_cmd->se_tmr_req;
  263. return cmd;
  264. out:
  265. iscsit_release_cmd(cmd);
  266. return NULL;
  267. }
  268. int iscsit_decide_list_to_build(
  269. struct iscsi_cmd *cmd,
  270. u32 immediate_data_length)
  271. {
  272. struct iscsi_build_list bl;
  273. struct iscsi_conn *conn = cmd->conn;
  274. struct iscsi_session *sess = conn->sess;
  275. struct iscsi_node_attrib *na;
  276. if (sess->sess_ops->DataSequenceInOrder &&
  277. sess->sess_ops->DataPDUInOrder)
  278. return 0;
  279. if (cmd->data_direction == DMA_NONE)
  280. return 0;
  281. na = iscsit_tpg_get_node_attrib(sess);
  282. memset(&bl, 0, sizeof(struct iscsi_build_list));
  283. if (cmd->data_direction == DMA_FROM_DEVICE) {
  284. bl.data_direction = ISCSI_PDU_READ;
  285. bl.type = PDULIST_NORMAL;
  286. if (na->random_datain_pdu_offsets)
  287. bl.randomize |= RANDOM_DATAIN_PDU_OFFSETS;
  288. if (na->random_datain_seq_offsets)
  289. bl.randomize |= RANDOM_DATAIN_SEQ_OFFSETS;
  290. } else {
  291. bl.data_direction = ISCSI_PDU_WRITE;
  292. bl.immediate_data_length = immediate_data_length;
  293. if (na->random_r2t_offsets)
  294. bl.randomize |= RANDOM_R2T_OFFSETS;
  295. if (!cmd->immediate_data && !cmd->unsolicited_data)
  296. bl.type = PDULIST_NORMAL;
  297. else if (cmd->immediate_data && !cmd->unsolicited_data)
  298. bl.type = PDULIST_IMMEDIATE;
  299. else if (!cmd->immediate_data && cmd->unsolicited_data)
  300. bl.type = PDULIST_UNSOLICITED;
  301. else if (cmd->immediate_data && cmd->unsolicited_data)
  302. bl.type = PDULIST_IMMEDIATE_AND_UNSOLICITED;
  303. }
  304. return iscsit_do_build_list(cmd, &bl);
  305. }
  306. struct iscsi_seq *iscsit_get_seq_holder_for_datain(
  307. struct iscsi_cmd *cmd,
  308. u32 seq_send_order)
  309. {
  310. u32 i;
  311. for (i = 0; i < cmd->seq_count; i++)
  312. if (cmd->seq_list[i].seq_send_order == seq_send_order)
  313. return &cmd->seq_list[i];
  314. return NULL;
  315. }
  316. struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
  317. {
  318. u32 i;
  319. if (!cmd->seq_list) {
  320. pr_err("struct iscsi_cmd->seq_list is NULL!\n");
  321. return NULL;
  322. }
  323. for (i = 0; i < cmd->seq_count; i++) {
  324. if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
  325. continue;
  326. if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
  327. cmd->seq_send_order++;
  328. return &cmd->seq_list[i];
  329. }
  330. }
  331. return NULL;
  332. }
  333. struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
  334. struct iscsi_cmd *cmd,
  335. u32 r2t_sn)
  336. {
  337. struct iscsi_r2t *r2t;
  338. spin_lock_bh(&cmd->r2t_lock);
  339. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  340. if (r2t->r2t_sn == r2t_sn) {
  341. spin_unlock_bh(&cmd->r2t_lock);
  342. return r2t;
  343. }
  344. }
  345. spin_unlock_bh(&cmd->r2t_lock);
  346. return NULL;
  347. }
  348. static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
  349. {
  350. int ret;
  351. /*
  352. * This is the proper method of checking received CmdSN against
  353. * ExpCmdSN and MaxCmdSN values, as well as accounting for out
  354. * or order CmdSNs due to multiple connection sessions and/or
  355. * CRC failures.
  356. */
  357. if (iscsi_sna_gt(cmdsn, sess->max_cmd_sn)) {
  358. pr_err("Received CmdSN: 0x%08x is greater than"
  359. " MaxCmdSN: 0x%08x, protocol error.\n", cmdsn,
  360. sess->max_cmd_sn);
  361. ret = CMDSN_ERROR_CANNOT_RECOVER;
  362. } else if (cmdsn == sess->exp_cmd_sn) {
  363. sess->exp_cmd_sn++;
  364. pr_debug("Received CmdSN matches ExpCmdSN,"
  365. " incremented ExpCmdSN to: 0x%08x\n",
  366. sess->exp_cmd_sn);
  367. ret = CMDSN_NORMAL_OPERATION;
  368. } else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
  369. pr_debug("Received CmdSN: 0x%08x is greater"
  370. " than ExpCmdSN: 0x%08x, not acknowledging.\n",
  371. cmdsn, sess->exp_cmd_sn);
  372. ret = CMDSN_HIGHER_THAN_EXP;
  373. } else {
  374. pr_err("Received CmdSN: 0x%08x is less than"
  375. " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
  376. sess->exp_cmd_sn);
  377. ret = CMDSN_LOWER_THAN_EXP;
  378. }
  379. return ret;
  380. }
  381. /*
  382. * Commands may be received out of order if MC/S is in use.
  383. * Ensure they are executed in CmdSN order.
  384. */
  385. int iscsit_sequence_cmd(
  386. struct iscsi_conn *conn,
  387. struct iscsi_cmd *cmd,
  388. u32 cmdsn)
  389. {
  390. int ret;
  391. int cmdsn_ret;
  392. mutex_lock(&conn->sess->cmdsn_mutex);
  393. cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, cmdsn);
  394. switch (cmdsn_ret) {
  395. case CMDSN_NORMAL_OPERATION:
  396. ret = iscsit_execute_cmd(cmd, 0);
  397. if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
  398. iscsit_execute_ooo_cmdsns(conn->sess);
  399. break;
  400. case CMDSN_HIGHER_THAN_EXP:
  401. ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, cmdsn);
  402. break;
  403. case CMDSN_LOWER_THAN_EXP:
  404. cmd->i_state = ISTATE_REMOVE;
  405. iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
  406. ret = cmdsn_ret;
  407. break;
  408. default:
  409. ret = cmdsn_ret;
  410. break;
  411. }
  412. mutex_unlock(&conn->sess->cmdsn_mutex);
  413. return ret;
  414. }
  415. int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
  416. {
  417. struct iscsi_conn *conn = cmd->conn;
  418. struct se_cmd *se_cmd = &cmd->se_cmd;
  419. struct iscsi_data *hdr = (struct iscsi_data *) buf;
  420. u32 payload_length = ntoh24(hdr->dlength);
  421. if (conn->sess->sess_ops->InitialR2T) {
  422. pr_err("Received unexpected unsolicited data"
  423. " while InitialR2T=Yes, protocol error.\n");
  424. transport_send_check_condition_and_sense(se_cmd,
  425. TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
  426. return -1;
  427. }
  428. if ((cmd->first_burst_len + payload_length) >
  429. conn->sess->sess_ops->FirstBurstLength) {
  430. pr_err("Total %u bytes exceeds FirstBurstLength: %u"
  431. " for this Unsolicited DataOut Burst.\n",
  432. (cmd->first_burst_len + payload_length),
  433. conn->sess->sess_ops->FirstBurstLength);
  434. transport_send_check_condition_and_sense(se_cmd,
  435. TCM_INCORRECT_AMOUNT_OF_DATA, 0);
  436. return -1;
  437. }
  438. if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
  439. return 0;
  440. if (((cmd->first_burst_len + payload_length) != cmd->data_length) &&
  441. ((cmd->first_burst_len + payload_length) !=
  442. conn->sess->sess_ops->FirstBurstLength)) {
  443. pr_err("Unsolicited non-immediate data received %u"
  444. " does not equal FirstBurstLength: %u, and does"
  445. " not equal ExpXferLen %u.\n",
  446. (cmd->first_burst_len + payload_length),
  447. conn->sess->sess_ops->FirstBurstLength, cmd->data_length);
  448. transport_send_check_condition_and_sense(se_cmd,
  449. TCM_INCORRECT_AMOUNT_OF_DATA, 0);
  450. return -1;
  451. }
  452. return 0;
  453. }
  454. struct iscsi_cmd *iscsit_find_cmd_from_itt(
  455. struct iscsi_conn *conn,
  456. u32 init_task_tag)
  457. {
  458. struct iscsi_cmd *cmd;
  459. spin_lock_bh(&conn->cmd_lock);
  460. list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
  461. if (cmd->init_task_tag == init_task_tag) {
  462. spin_unlock_bh(&conn->cmd_lock);
  463. return cmd;
  464. }
  465. }
  466. spin_unlock_bh(&conn->cmd_lock);
  467. pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
  468. init_task_tag, conn->cid);
  469. return NULL;
  470. }
  471. struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
  472. struct iscsi_conn *conn,
  473. u32 init_task_tag,
  474. u32 length)
  475. {
  476. struct iscsi_cmd *cmd;
  477. spin_lock_bh(&conn->cmd_lock);
  478. list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
  479. if (cmd->init_task_tag == init_task_tag) {
  480. spin_unlock_bh(&conn->cmd_lock);
  481. return cmd;
  482. }
  483. }
  484. spin_unlock_bh(&conn->cmd_lock);
  485. pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
  486. " dumping payload\n", init_task_tag, conn->cid);
  487. if (length)
  488. iscsit_dump_data_payload(conn, length, 1);
  489. return NULL;
  490. }
  491. struct iscsi_cmd *iscsit_find_cmd_from_ttt(
  492. struct iscsi_conn *conn,
  493. u32 targ_xfer_tag)
  494. {
  495. struct iscsi_cmd *cmd = NULL;
  496. spin_lock_bh(&conn->cmd_lock);
  497. list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
  498. if (cmd->targ_xfer_tag == targ_xfer_tag) {
  499. spin_unlock_bh(&conn->cmd_lock);
  500. return cmd;
  501. }
  502. }
  503. spin_unlock_bh(&conn->cmd_lock);
  504. pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
  505. targ_xfer_tag, conn->cid);
  506. return NULL;
  507. }
  508. int iscsit_find_cmd_for_recovery(
  509. struct iscsi_session *sess,
  510. struct iscsi_cmd **cmd_ptr,
  511. struct iscsi_conn_recovery **cr_ptr,
  512. u32 init_task_tag)
  513. {
  514. struct iscsi_cmd *cmd = NULL;
  515. struct iscsi_conn_recovery *cr;
  516. /*
  517. * Scan through the inactive connection recovery list's command list.
  518. * If init_task_tag matches the command is still alligent.
  519. */
  520. spin_lock(&sess->cr_i_lock);
  521. list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
  522. spin_lock(&cr->conn_recovery_cmd_lock);
  523. list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_list) {
  524. if (cmd->init_task_tag == init_task_tag) {
  525. spin_unlock(&cr->conn_recovery_cmd_lock);
  526. spin_unlock(&sess->cr_i_lock);
  527. *cr_ptr = cr;
  528. *cmd_ptr = cmd;
  529. return -2;
  530. }
  531. }
  532. spin_unlock(&cr->conn_recovery_cmd_lock);
  533. }
  534. spin_unlock(&sess->cr_i_lock);
  535. /*
  536. * Scan through the active connection recovery list's command list.
  537. * If init_task_tag matches the command is ready to be reassigned.
  538. */
  539. spin_lock(&sess->cr_a_lock);
  540. list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
  541. spin_lock(&cr->conn_recovery_cmd_lock);
  542. list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_list) {
  543. if (cmd->init_task_tag == init_task_tag) {
  544. spin_unlock(&cr->conn_recovery_cmd_lock);
  545. spin_unlock(&sess->cr_a_lock);
  546. *cr_ptr = cr;
  547. *cmd_ptr = cmd;
  548. return 0;
  549. }
  550. }
  551. spin_unlock(&cr->conn_recovery_cmd_lock);
  552. }
  553. spin_unlock(&sess->cr_a_lock);
  554. return -1;
  555. }
  556. void iscsit_add_cmd_to_immediate_queue(
  557. struct iscsi_cmd *cmd,
  558. struct iscsi_conn *conn,
  559. u8 state)
  560. {
  561. struct iscsi_queue_req *qr;
  562. qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
  563. if (!qr) {
  564. pr_err("Unable to allocate memory for"
  565. " struct iscsi_queue_req\n");
  566. return;
  567. }
  568. INIT_LIST_HEAD(&qr->qr_list);
  569. qr->cmd = cmd;
  570. qr->state = state;
  571. spin_lock_bh(&conn->immed_queue_lock);
  572. list_add_tail(&qr->qr_list, &conn->immed_queue_list);
  573. atomic_inc(&cmd->immed_queue_count);
  574. atomic_set(&conn->check_immediate_queue, 1);
  575. spin_unlock_bh(&conn->immed_queue_lock);
  576. wake_up_process(conn->thread_set->tx_thread);
  577. }
  578. struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
  579. {
  580. struct iscsi_queue_req *qr;
  581. spin_lock_bh(&conn->immed_queue_lock);
  582. if (list_empty(&conn->immed_queue_list)) {
  583. spin_unlock_bh(&conn->immed_queue_lock);
  584. return NULL;
  585. }
  586. list_for_each_entry(qr, &conn->immed_queue_list, qr_list)
  587. break;
  588. list_del(&qr->qr_list);
  589. if (qr->cmd)
  590. atomic_dec(&qr->cmd->immed_queue_count);
  591. spin_unlock_bh(&conn->immed_queue_lock);
  592. return qr;
  593. }
  594. static void iscsit_remove_cmd_from_immediate_queue(
  595. struct iscsi_cmd *cmd,
  596. struct iscsi_conn *conn)
  597. {
  598. struct iscsi_queue_req *qr, *qr_tmp;
  599. spin_lock_bh(&conn->immed_queue_lock);
  600. if (!atomic_read(&cmd->immed_queue_count)) {
  601. spin_unlock_bh(&conn->immed_queue_lock);
  602. return;
  603. }
  604. list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
  605. if (qr->cmd != cmd)
  606. continue;
  607. atomic_dec(&qr->cmd->immed_queue_count);
  608. list_del(&qr->qr_list);
  609. kmem_cache_free(lio_qr_cache, qr);
  610. }
  611. spin_unlock_bh(&conn->immed_queue_lock);
  612. if (atomic_read(&cmd->immed_queue_count)) {
  613. pr_err("ITT: 0x%08x immed_queue_count: %d\n",
  614. cmd->init_task_tag,
  615. atomic_read(&cmd->immed_queue_count));
  616. }
  617. }
  618. void iscsit_add_cmd_to_response_queue(
  619. struct iscsi_cmd *cmd,
  620. struct iscsi_conn *conn,
  621. u8 state)
  622. {
  623. struct iscsi_queue_req *qr;
  624. qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
  625. if (!qr) {
  626. pr_err("Unable to allocate memory for"
  627. " struct iscsi_queue_req\n");
  628. return;
  629. }
  630. INIT_LIST_HEAD(&qr->qr_list);
  631. qr->cmd = cmd;
  632. qr->state = state;
  633. spin_lock_bh(&conn->response_queue_lock);
  634. list_add_tail(&qr->qr_list, &conn->response_queue_list);
  635. atomic_inc(&cmd->response_queue_count);
  636. spin_unlock_bh(&conn->response_queue_lock);
  637. wake_up_process(conn->thread_set->tx_thread);
  638. }
  639. struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
  640. {
  641. struct iscsi_queue_req *qr;
  642. spin_lock_bh(&conn->response_queue_lock);
  643. if (list_empty(&conn->response_queue_list)) {
  644. spin_unlock_bh(&conn->response_queue_lock);
  645. return NULL;
  646. }
  647. list_for_each_entry(qr, &conn->response_queue_list, qr_list)
  648. break;
  649. list_del(&qr->qr_list);
  650. if (qr->cmd)
  651. atomic_dec(&qr->cmd->response_queue_count);
  652. spin_unlock_bh(&conn->response_queue_lock);
  653. return qr;
  654. }
  655. static void iscsit_remove_cmd_from_response_queue(
  656. struct iscsi_cmd *cmd,
  657. struct iscsi_conn *conn)
  658. {
  659. struct iscsi_queue_req *qr, *qr_tmp;
  660. spin_lock_bh(&conn->response_queue_lock);
  661. if (!atomic_read(&cmd->response_queue_count)) {
  662. spin_unlock_bh(&conn->response_queue_lock);
  663. return;
  664. }
  665. list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
  666. qr_list) {
  667. if (qr->cmd != cmd)
  668. continue;
  669. atomic_dec(&qr->cmd->response_queue_count);
  670. list_del(&qr->qr_list);
  671. kmem_cache_free(lio_qr_cache, qr);
  672. }
  673. spin_unlock_bh(&conn->response_queue_lock);
  674. if (atomic_read(&cmd->response_queue_count)) {
  675. pr_err("ITT: 0x%08x response_queue_count: %d\n",
  676. cmd->init_task_tag,
  677. atomic_read(&cmd->response_queue_count));
  678. }
  679. }
  680. void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
  681. {
  682. struct iscsi_queue_req *qr, *qr_tmp;
  683. spin_lock_bh(&conn->immed_queue_lock);
  684. list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
  685. list_del(&qr->qr_list);
  686. if (qr->cmd)
  687. atomic_dec(&qr->cmd->immed_queue_count);
  688. kmem_cache_free(lio_qr_cache, qr);
  689. }
  690. spin_unlock_bh(&conn->immed_queue_lock);
  691. spin_lock_bh(&conn->response_queue_lock);
  692. list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
  693. qr_list) {
  694. list_del(&qr->qr_list);
  695. if (qr->cmd)
  696. atomic_dec(&qr->cmd->response_queue_count);
  697. kmem_cache_free(lio_qr_cache, qr);
  698. }
  699. spin_unlock_bh(&conn->response_queue_lock);
  700. }
  701. void iscsit_release_cmd(struct iscsi_cmd *cmd)
  702. {
  703. struct iscsi_conn *conn = cmd->conn;
  704. int i;
  705. iscsit_free_r2ts_from_list(cmd);
  706. iscsit_free_all_datain_reqs(cmd);
  707. kfree(cmd->buf_ptr);
  708. kfree(cmd->pdu_list);
  709. kfree(cmd->seq_list);
  710. kfree(cmd->tmr_req);
  711. kfree(cmd->iov_data);
  712. for (i = 0; i < cmd->t_mem_sg_nents; i++)
  713. __free_page(sg_page(&cmd->t_mem_sg[i]));
  714. kfree(cmd->t_mem_sg);
  715. if (conn) {
  716. iscsit_remove_cmd_from_immediate_queue(cmd, conn);
  717. iscsit_remove_cmd_from_response_queue(cmd, conn);
  718. }
  719. kmem_cache_free(lio_cmd_cache, cmd);
  720. }
  721. int iscsit_check_session_usage_count(struct iscsi_session *sess)
  722. {
  723. spin_lock_bh(&sess->session_usage_lock);
  724. if (sess->session_usage_count != 0) {
  725. sess->session_waiting_on_uc = 1;
  726. spin_unlock_bh(&sess->session_usage_lock);
  727. if (in_interrupt())
  728. return 2;
  729. wait_for_completion(&sess->session_waiting_on_uc_comp);
  730. return 1;
  731. }
  732. spin_unlock_bh(&sess->session_usage_lock);
  733. return 0;
  734. }
  735. void iscsit_dec_session_usage_count(struct iscsi_session *sess)
  736. {
  737. spin_lock_bh(&sess->session_usage_lock);
  738. sess->session_usage_count--;
  739. if (!sess->session_usage_count && sess->session_waiting_on_uc)
  740. complete(&sess->session_waiting_on_uc_comp);
  741. spin_unlock_bh(&sess->session_usage_lock);
  742. }
  743. void iscsit_inc_session_usage_count(struct iscsi_session *sess)
  744. {
  745. spin_lock_bh(&sess->session_usage_lock);
  746. sess->session_usage_count++;
  747. spin_unlock_bh(&sess->session_usage_lock);
  748. }
  749. /*
  750. * Used before iscsi_do[rx,tx]_data() to determine iov and [rx,tx]_marker
  751. * array counts needed for sync and steering.
  752. */
  753. static int iscsit_determine_sync_and_steering_counts(
  754. struct iscsi_conn *conn,
  755. struct iscsi_data_count *count)
  756. {
  757. u32 length = count->data_length;
  758. u32 marker, markint;
  759. count->sync_and_steering = 1;
  760. marker = (count->type == ISCSI_RX_DATA) ?
  761. conn->of_marker : conn->if_marker;
  762. markint = (count->type == ISCSI_RX_DATA) ?
  763. (conn->conn_ops->OFMarkInt * 4) :
  764. (conn->conn_ops->IFMarkInt * 4);
  765. count->ss_iov_count = count->iov_count;
  766. while (length > 0) {
  767. if (length >= marker) {
  768. count->ss_iov_count += 3;
  769. count->ss_marker_count += 2;
  770. length -= marker;
  771. marker = markint;
  772. } else
  773. length = 0;
  774. }
  775. return 0;
  776. }
  777. /*
  778. * Setup conn->if_marker and conn->of_marker values based upon
  779. * the initial marker-less interval. (see iSCSI v19 A.2)
  780. */
  781. int iscsit_set_sync_and_steering_values(struct iscsi_conn *conn)
  782. {
  783. int login_ifmarker_count = 0, login_ofmarker_count = 0, next_marker = 0;
  784. /*
  785. * IFMarkInt and OFMarkInt are negotiated as 32-bit words.
  786. */
  787. u32 IFMarkInt = (conn->conn_ops->IFMarkInt * 4);
  788. u32 OFMarkInt = (conn->conn_ops->OFMarkInt * 4);
  789. if (conn->conn_ops->OFMarker) {
  790. /*
  791. * Account for the first Login Command received not
  792. * via iscsi_recv_msg().
  793. */
  794. conn->of_marker += ISCSI_HDR_LEN;
  795. if (conn->of_marker <= OFMarkInt) {
  796. conn->of_marker = (OFMarkInt - conn->of_marker);
  797. } else {
  798. login_ofmarker_count = (conn->of_marker / OFMarkInt);
  799. next_marker = (OFMarkInt * (login_ofmarker_count + 1)) +
  800. (login_ofmarker_count * MARKER_SIZE);
  801. conn->of_marker = (next_marker - conn->of_marker);
  802. }
  803. conn->of_marker_offset = 0;
  804. pr_debug("Setting OFMarker value to %u based on Initial"
  805. " Markerless Interval.\n", conn->of_marker);
  806. }
  807. if (conn->conn_ops->IFMarker) {
  808. if (conn->if_marker <= IFMarkInt) {
  809. conn->if_marker = (IFMarkInt - conn->if_marker);
  810. } else {
  811. login_ifmarker_count = (conn->if_marker / IFMarkInt);
  812. next_marker = (IFMarkInt * (login_ifmarker_count + 1)) +
  813. (login_ifmarker_count * MARKER_SIZE);
  814. conn->if_marker = (next_marker - conn->if_marker);
  815. }
  816. pr_debug("Setting IFMarker value to %u based on Initial"
  817. " Markerless Interval.\n", conn->if_marker);
  818. }
  819. return 0;
  820. }
  821. struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
  822. {
  823. struct iscsi_conn *conn;
  824. spin_lock_bh(&sess->conn_lock);
  825. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  826. if ((conn->cid == cid) &&
  827. (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
  828. iscsit_inc_conn_usage_count(conn);
  829. spin_unlock_bh(&sess->conn_lock);
  830. return conn;
  831. }
  832. }
  833. spin_unlock_bh(&sess->conn_lock);
  834. return NULL;
  835. }
  836. struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
  837. {
  838. struct iscsi_conn *conn;
  839. spin_lock_bh(&sess->conn_lock);
  840. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  841. if (conn->cid == cid) {
  842. iscsit_inc_conn_usage_count(conn);
  843. spin_lock(&conn->state_lock);
  844. atomic_set(&conn->connection_wait_rcfr, 1);
  845. spin_unlock(&conn->state_lock);
  846. spin_unlock_bh(&sess->conn_lock);
  847. return conn;
  848. }
  849. }
  850. spin_unlock_bh(&sess->conn_lock);
  851. return NULL;
  852. }
  853. void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
  854. {
  855. spin_lock_bh(&conn->conn_usage_lock);
  856. if (conn->conn_usage_count != 0) {
  857. conn->conn_waiting_on_uc = 1;
  858. spin_unlock_bh(&conn->conn_usage_lock);
  859. wait_for_completion(&conn->conn_waiting_on_uc_comp);
  860. return;
  861. }
  862. spin_unlock_bh(&conn->conn_usage_lock);
  863. }
  864. void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
  865. {
  866. spin_lock_bh(&conn->conn_usage_lock);
  867. conn->conn_usage_count--;
  868. if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
  869. complete(&conn->conn_waiting_on_uc_comp);
  870. spin_unlock_bh(&conn->conn_usage_lock);
  871. }
  872. void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
  873. {
  874. spin_lock_bh(&conn->conn_usage_lock);
  875. conn->conn_usage_count++;
  876. spin_unlock_bh(&conn->conn_usage_lock);
  877. }
  878. static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
  879. {
  880. u8 state;
  881. struct iscsi_cmd *cmd;
  882. cmd = iscsit_allocate_cmd(conn, GFP_ATOMIC);
  883. if (!cmd)
  884. return -1;
  885. cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
  886. state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
  887. ISTATE_SEND_NOPIN_NO_RESPONSE;
  888. cmd->init_task_tag = 0xFFFFFFFF;
  889. spin_lock_bh(&conn->sess->ttt_lock);
  890. cmd->targ_xfer_tag = (want_response) ? conn->sess->targ_xfer_tag++ :
  891. 0xFFFFFFFF;
  892. if (want_response && (cmd->targ_xfer_tag == 0xFFFFFFFF))
  893. cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
  894. spin_unlock_bh(&conn->sess->ttt_lock);
  895. spin_lock_bh(&conn->cmd_lock);
  896. list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
  897. spin_unlock_bh(&conn->cmd_lock);
  898. if (want_response)
  899. iscsit_start_nopin_response_timer(conn);
  900. iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
  901. return 0;
  902. }
  903. static void iscsit_handle_nopin_response_timeout(unsigned long data)
  904. {
  905. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  906. iscsit_inc_conn_usage_count(conn);
  907. spin_lock_bh(&conn->nopin_timer_lock);
  908. if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
  909. spin_unlock_bh(&conn->nopin_timer_lock);
  910. iscsit_dec_conn_usage_count(conn);
  911. return;
  912. }
  913. pr_debug("Did not receive response to NOPIN on CID: %hu on"
  914. " SID: %u, failing connection.\n", conn->cid,
  915. conn->sess->sid);
  916. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  917. spin_unlock_bh(&conn->nopin_timer_lock);
  918. {
  919. struct iscsi_portal_group *tpg = conn->sess->tpg;
  920. struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
  921. if (tiqn) {
  922. spin_lock_bh(&tiqn->sess_err_stats.lock);
  923. strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
  924. (void *)conn->sess->sess_ops->InitiatorName);
  925. tiqn->sess_err_stats.last_sess_failure_type =
  926. ISCSI_SESS_ERR_CXN_TIMEOUT;
  927. tiqn->sess_err_stats.cxn_timeout_errors++;
  928. conn->sess->conn_timeout_errors++;
  929. spin_unlock_bh(&tiqn->sess_err_stats.lock);
  930. }
  931. }
  932. iscsit_cause_connection_reinstatement(conn, 0);
  933. iscsit_dec_conn_usage_count(conn);
  934. }
  935. void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
  936. {
  937. struct iscsi_session *sess = conn->sess;
  938. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  939. spin_lock_bh(&conn->nopin_timer_lock);
  940. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  941. spin_unlock_bh(&conn->nopin_timer_lock);
  942. return;
  943. }
  944. mod_timer(&conn->nopin_response_timer,
  945. (get_jiffies_64() + na->nopin_response_timeout * HZ));
  946. spin_unlock_bh(&conn->nopin_timer_lock);
  947. }
  948. /*
  949. * Called with conn->nopin_timer_lock held.
  950. */
  951. void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
  952. {
  953. struct iscsi_session *sess = conn->sess;
  954. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  955. spin_lock_bh(&conn->nopin_timer_lock);
  956. if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
  957. spin_unlock_bh(&conn->nopin_timer_lock);
  958. return;
  959. }
  960. init_timer(&conn->nopin_response_timer);
  961. conn->nopin_response_timer.expires =
  962. (get_jiffies_64() + na->nopin_response_timeout * HZ);
  963. conn->nopin_response_timer.data = (unsigned long)conn;
  964. conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
  965. conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
  966. conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
  967. add_timer(&conn->nopin_response_timer);
  968. pr_debug("Started NOPIN Response Timer on CID: %d to %u"
  969. " seconds\n", conn->cid, na->nopin_response_timeout);
  970. spin_unlock_bh(&conn->nopin_timer_lock);
  971. }
  972. void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
  973. {
  974. spin_lock_bh(&conn->nopin_timer_lock);
  975. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  976. spin_unlock_bh(&conn->nopin_timer_lock);
  977. return;
  978. }
  979. conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
  980. spin_unlock_bh(&conn->nopin_timer_lock);
  981. del_timer_sync(&conn->nopin_response_timer);
  982. spin_lock_bh(&conn->nopin_timer_lock);
  983. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  984. spin_unlock_bh(&conn->nopin_timer_lock);
  985. }
  986. static void iscsit_handle_nopin_timeout(unsigned long data)
  987. {
  988. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  989. iscsit_inc_conn_usage_count(conn);
  990. spin_lock_bh(&conn->nopin_timer_lock);
  991. if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
  992. spin_unlock_bh(&conn->nopin_timer_lock);
  993. iscsit_dec_conn_usage_count(conn);
  994. return;
  995. }
  996. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  997. spin_unlock_bh(&conn->nopin_timer_lock);
  998. iscsit_add_nopin(conn, 1);
  999. iscsit_dec_conn_usage_count(conn);
  1000. }
  1001. /*
  1002. * Called with conn->nopin_timer_lock held.
  1003. */
  1004. void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
  1005. {
  1006. struct iscsi_session *sess = conn->sess;
  1007. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1008. /*
  1009. * NOPIN timeout is disabled.
  1010. */
  1011. if (!na->nopin_timeout)
  1012. return;
  1013. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
  1014. return;
  1015. init_timer(&conn->nopin_timer);
  1016. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  1017. conn->nopin_timer.data = (unsigned long)conn;
  1018. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  1019. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  1020. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  1021. add_timer(&conn->nopin_timer);
  1022. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  1023. " interval\n", conn->cid, na->nopin_timeout);
  1024. }
  1025. void iscsit_start_nopin_timer(struct iscsi_conn *conn)
  1026. {
  1027. struct iscsi_session *sess = conn->sess;
  1028. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1029. /*
  1030. * NOPIN timeout is disabled..
  1031. */
  1032. if (!na->nopin_timeout)
  1033. return;
  1034. spin_lock_bh(&conn->nopin_timer_lock);
  1035. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
  1036. spin_unlock_bh(&conn->nopin_timer_lock);
  1037. return;
  1038. }
  1039. init_timer(&conn->nopin_timer);
  1040. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  1041. conn->nopin_timer.data = (unsigned long)conn;
  1042. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  1043. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  1044. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  1045. add_timer(&conn->nopin_timer);
  1046. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  1047. " interval\n", conn->cid, na->nopin_timeout);
  1048. spin_unlock_bh(&conn->nopin_timer_lock);
  1049. }
  1050. void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
  1051. {
  1052. spin_lock_bh(&conn->nopin_timer_lock);
  1053. if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
  1054. spin_unlock_bh(&conn->nopin_timer_lock);
  1055. return;
  1056. }
  1057. conn->nopin_timer_flags |= ISCSI_TF_STOP;
  1058. spin_unlock_bh(&conn->nopin_timer_lock);
  1059. del_timer_sync(&conn->nopin_timer);
  1060. spin_lock_bh(&conn->nopin_timer_lock);
  1061. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  1062. spin_unlock_bh(&conn->nopin_timer_lock);
  1063. }
  1064. int iscsit_send_tx_data(
  1065. struct iscsi_cmd *cmd,
  1066. struct iscsi_conn *conn,
  1067. int use_misc)
  1068. {
  1069. int tx_sent, tx_size;
  1070. u32 iov_count;
  1071. struct kvec *iov;
  1072. send_data:
  1073. tx_size = cmd->tx_size;
  1074. if (!use_misc) {
  1075. iov = &cmd->iov_data[0];
  1076. iov_count = cmd->iov_data_count;
  1077. } else {
  1078. iov = &cmd->iov_misc[0];
  1079. iov_count = cmd->iov_misc_count;
  1080. }
  1081. tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
  1082. if (tx_size != tx_sent) {
  1083. if (tx_sent == -EAGAIN) {
  1084. pr_err("tx_data() returned -EAGAIN\n");
  1085. goto send_data;
  1086. } else
  1087. return -1;
  1088. }
  1089. cmd->tx_size = 0;
  1090. return 0;
  1091. }
  1092. int iscsit_fe_sendpage_sg(
  1093. struct iscsi_cmd *cmd,
  1094. struct iscsi_conn *conn)
  1095. {
  1096. struct scatterlist *sg = cmd->first_data_sg;
  1097. struct kvec iov;
  1098. u32 tx_hdr_size, data_len;
  1099. u32 offset = cmd->first_data_sg_off;
  1100. int tx_sent;
  1101. send_hdr:
  1102. tx_hdr_size = ISCSI_HDR_LEN;
  1103. if (conn->conn_ops->HeaderDigest)
  1104. tx_hdr_size += ISCSI_CRC_LEN;
  1105. iov.iov_base = cmd->pdu;
  1106. iov.iov_len = tx_hdr_size;
  1107. tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
  1108. if (tx_hdr_size != tx_sent) {
  1109. if (tx_sent == -EAGAIN) {
  1110. pr_err("tx_data() returned -EAGAIN\n");
  1111. goto send_hdr;
  1112. }
  1113. return -1;
  1114. }
  1115. data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
  1116. if (conn->conn_ops->DataDigest)
  1117. data_len -= ISCSI_CRC_LEN;
  1118. /*
  1119. * Perform sendpage() for each page in the scatterlist
  1120. */
  1121. while (data_len) {
  1122. u32 space = (sg->length - offset);
  1123. u32 sub_len = min_t(u32, data_len, space);
  1124. send_pg:
  1125. tx_sent = conn->sock->ops->sendpage(conn->sock,
  1126. sg_page(sg), sg->offset + offset, sub_len, 0);
  1127. if (tx_sent != sub_len) {
  1128. if (tx_sent == -EAGAIN) {
  1129. pr_err("tcp_sendpage() returned"
  1130. " -EAGAIN\n");
  1131. goto send_pg;
  1132. }
  1133. pr_err("tcp_sendpage() failure: %d\n",
  1134. tx_sent);
  1135. return -1;
  1136. }
  1137. data_len -= sub_len;
  1138. offset = 0;
  1139. sg = sg_next(sg);
  1140. }
  1141. send_padding:
  1142. if (cmd->padding) {
  1143. struct kvec *iov_p =
  1144. &cmd->iov_data[cmd->iov_data_count-1];
  1145. tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
  1146. if (cmd->padding != tx_sent) {
  1147. if (tx_sent == -EAGAIN) {
  1148. pr_err("tx_data() returned -EAGAIN\n");
  1149. goto send_padding;
  1150. }
  1151. return -1;
  1152. }
  1153. }
  1154. send_datacrc:
  1155. if (conn->conn_ops->DataDigest) {
  1156. struct kvec *iov_d =
  1157. &cmd->iov_data[cmd->iov_data_count];
  1158. tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
  1159. if (ISCSI_CRC_LEN != tx_sent) {
  1160. if (tx_sent == -EAGAIN) {
  1161. pr_err("tx_data() returned -EAGAIN\n");
  1162. goto send_datacrc;
  1163. }
  1164. return -1;
  1165. }
  1166. }
  1167. return 0;
  1168. }
  1169. /*
  1170. * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
  1171. * back to the Initiator when an expection condition occurs with the
  1172. * errors set in status_class and status_detail.
  1173. *
  1174. * Parameters: iSCSI Connection, Status Class, Status Detail.
  1175. * Returns: 0 on success, -1 on error.
  1176. */
  1177. int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
  1178. {
  1179. u8 iscsi_hdr[ISCSI_HDR_LEN];
  1180. int err;
  1181. struct kvec iov;
  1182. struct iscsi_login_rsp *hdr;
  1183. iscsit_collect_login_stats(conn, status_class, status_detail);
  1184. memset(&iov, 0, sizeof(struct kvec));
  1185. memset(&iscsi_hdr, 0x0, ISCSI_HDR_LEN);
  1186. hdr = (struct iscsi_login_rsp *)&iscsi_hdr;
  1187. hdr->opcode = ISCSI_OP_LOGIN_RSP;
  1188. hdr->status_class = status_class;
  1189. hdr->status_detail = status_detail;
  1190. hdr->itt = cpu_to_be32(conn->login_itt);
  1191. iov.iov_base = &iscsi_hdr;
  1192. iov.iov_len = ISCSI_HDR_LEN;
  1193. PRINT_BUFF(iscsi_hdr, ISCSI_HDR_LEN);
  1194. err = tx_data(conn, &iov, 1, ISCSI_HDR_LEN);
  1195. if (err != ISCSI_HDR_LEN) {
  1196. pr_err("tx_data returned less than expected\n");
  1197. return -1;
  1198. }
  1199. return 0;
  1200. }
  1201. void iscsit_print_session_params(struct iscsi_session *sess)
  1202. {
  1203. struct iscsi_conn *conn;
  1204. pr_debug("-----------------------------[Session Params for"
  1205. " SID: %u]-----------------------------\n", sess->sid);
  1206. spin_lock_bh(&sess->conn_lock);
  1207. list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
  1208. iscsi_dump_conn_ops(conn->conn_ops);
  1209. spin_unlock_bh(&sess->conn_lock);
  1210. iscsi_dump_sess_ops(sess->sess_ops);
  1211. }
  1212. static int iscsit_do_rx_data(
  1213. struct iscsi_conn *conn,
  1214. struct iscsi_data_count *count)
  1215. {
  1216. int data = count->data_length, rx_loop = 0, total_rx = 0, iov_len;
  1217. u32 rx_marker_val[count->ss_marker_count], rx_marker_iov = 0;
  1218. struct kvec iov[count->ss_iov_count], *iov_p;
  1219. struct msghdr msg;
  1220. if (!conn || !conn->sock || !conn->conn_ops)
  1221. return -1;
  1222. memset(&msg, 0, sizeof(struct msghdr));
  1223. if (count->sync_and_steering) {
  1224. int size = 0;
  1225. u32 i, orig_iov_count = 0;
  1226. u32 orig_iov_len = 0, orig_iov_loc = 0;
  1227. u32 iov_count = 0, per_iov_bytes = 0;
  1228. u32 *rx_marker, old_rx_marker = 0;
  1229. struct kvec *iov_record;
  1230. memset(&rx_marker_val, 0,
  1231. count->ss_marker_count * sizeof(u32));
  1232. memset(&iov, 0, count->ss_iov_count * sizeof(struct kvec));
  1233. iov_record = count->iov;
  1234. orig_iov_count = count->iov_count;
  1235. rx_marker = &conn->of_marker;
  1236. i = 0;
  1237. size = data;
  1238. orig_iov_len = iov_record[orig_iov_loc].iov_len;
  1239. while (size > 0) {
  1240. pr_debug("rx_data: #1 orig_iov_len %u,"
  1241. " orig_iov_loc %u\n", orig_iov_len, orig_iov_loc);
  1242. pr_debug("rx_data: #2 rx_marker %u, size"
  1243. " %u\n", *rx_marker, size);
  1244. if (orig_iov_len >= *rx_marker) {
  1245. iov[iov_count].iov_len = *rx_marker;
  1246. iov[iov_count++].iov_base =
  1247. (iov_record[orig_iov_loc].iov_base +
  1248. per_iov_bytes);
  1249. iov[iov_count].iov_len = (MARKER_SIZE / 2);
  1250. iov[iov_count++].iov_base =
  1251. &rx_marker_val[rx_marker_iov++];
  1252. iov[iov_count].iov_len = (MARKER_SIZE / 2);
  1253. iov[iov_count++].iov_base =
  1254. &rx_marker_val[rx_marker_iov++];
  1255. old_rx_marker = *rx_marker;
  1256. /*
  1257. * OFMarkInt is in 32-bit words.
  1258. */
  1259. *rx_marker = (conn->conn_ops->OFMarkInt * 4);
  1260. size -= old_rx_marker;
  1261. orig_iov_len -= old_rx_marker;
  1262. per_iov_bytes += old_rx_marker;
  1263. pr_debug("rx_data: #3 new_rx_marker"
  1264. " %u, size %u\n", *rx_marker, size);
  1265. } else {
  1266. iov[iov_count].iov_len = orig_iov_len;
  1267. iov[iov_count++].iov_base =
  1268. (iov_record[orig_iov_loc].iov_base +
  1269. per_iov_bytes);
  1270. per_iov_bytes = 0;
  1271. *rx_marker -= orig_iov_len;
  1272. size -= orig_iov_len;
  1273. if (size)
  1274. orig_iov_len =
  1275. iov_record[++orig_iov_loc].iov_len;
  1276. pr_debug("rx_data: #4 new_rx_marker"
  1277. " %u, size %u\n", *rx_marker, size);
  1278. }
  1279. }
  1280. data += (rx_marker_iov * (MARKER_SIZE / 2));
  1281. iov_p = &iov[0];
  1282. iov_len = iov_count;
  1283. if (iov_count > count->ss_iov_count) {
  1284. pr_err("iov_count: %d, count->ss_iov_count:"
  1285. " %d\n", iov_count, count->ss_iov_count);
  1286. return -1;
  1287. }
  1288. if (rx_marker_iov > count->ss_marker_count) {
  1289. pr_err("rx_marker_iov: %d, count->ss_marker"
  1290. "_count: %d\n", rx_marker_iov,
  1291. count->ss_marker_count);
  1292. return -1;
  1293. }
  1294. } else {
  1295. iov_p = count->iov;
  1296. iov_len = count->iov_count;
  1297. }
  1298. while (total_rx < data) {
  1299. rx_loop = kernel_recvmsg(conn->sock, &msg, iov_p, iov_len,
  1300. (data - total_rx), MSG_WAITALL);
  1301. if (rx_loop <= 0) {
  1302. pr_debug("rx_loop: %d total_rx: %d\n",
  1303. rx_loop, total_rx);
  1304. return rx_loop;
  1305. }
  1306. total_rx += rx_loop;
  1307. pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
  1308. rx_loop, total_rx, data);
  1309. }
  1310. if (count->sync_and_steering) {
  1311. int j;
  1312. for (j = 0; j < rx_marker_iov; j++) {
  1313. pr_debug("rx_data: #5 j: %d, offset: %d\n",
  1314. j, rx_marker_val[j]);
  1315. conn->of_marker_offset = rx_marker_val[j];
  1316. }
  1317. total_rx -= (rx_marker_iov * (MARKER_SIZE / 2));
  1318. }
  1319. return total_rx;
  1320. }
  1321. static int iscsit_do_tx_data(
  1322. struct iscsi_conn *conn,
  1323. struct iscsi_data_count *count)
  1324. {
  1325. int data = count->data_length, total_tx = 0, tx_loop = 0, iov_len;
  1326. u32 tx_marker_val[count->ss_marker_count], tx_marker_iov = 0;
  1327. struct kvec iov[count->ss_iov_count], *iov_p;
  1328. struct msghdr msg;
  1329. if (!conn || !conn->sock || !conn->conn_ops)
  1330. return -1;
  1331. if (data <= 0) {
  1332. pr_err("Data length is: %d\n", data);
  1333. return -1;
  1334. }
  1335. memset(&msg, 0, sizeof(struct msghdr));
  1336. if (count->sync_and_steering) {
  1337. int size = 0;
  1338. u32 i, orig_iov_count = 0;
  1339. u32 orig_iov_len = 0, orig_iov_loc = 0;
  1340. u32 iov_count = 0, per_iov_bytes = 0;
  1341. u32 *tx_marker, old_tx_marker = 0;
  1342. struct kvec *iov_record;
  1343. memset(&tx_marker_val, 0,
  1344. count->ss_marker_count * sizeof(u32));
  1345. memset(&iov, 0, count->ss_iov_count * sizeof(struct kvec));
  1346. iov_record = count->iov;
  1347. orig_iov_count = count->iov_count;
  1348. tx_marker = &conn->if_marker;
  1349. i = 0;
  1350. size = data;
  1351. orig_iov_len = iov_record[orig_iov_loc].iov_len;
  1352. while (size > 0) {
  1353. pr_debug("tx_data: #1 orig_iov_len %u,"
  1354. " orig_iov_loc %u\n", orig_iov_len, orig_iov_loc);
  1355. pr_debug("tx_data: #2 tx_marker %u, size"
  1356. " %u\n", *tx_marker, size);
  1357. if (orig_iov_len >= *tx_marker) {
  1358. iov[iov_count].iov_len = *tx_marker;
  1359. iov[iov_count++].iov_base =
  1360. (iov_record[orig_iov_loc].iov_base +
  1361. per_iov_bytes);
  1362. tx_marker_val[tx_marker_iov] =
  1363. (size - *tx_marker);
  1364. iov[iov_count].iov_len = (MARKER_SIZE / 2);
  1365. iov[iov_count++].iov_base =
  1366. &tx_marker_val[tx_marker_iov++];
  1367. iov[iov_count].iov_len = (MARKER_SIZE / 2);
  1368. iov[iov_count++].iov_base =
  1369. &tx_marker_val[tx_marker_iov++];
  1370. old_tx_marker = *tx_marker;
  1371. /*
  1372. * IFMarkInt is in 32-bit words.
  1373. */
  1374. *tx_marker = (conn->conn_ops->IFMarkInt * 4);
  1375. size -= old_tx_marker;
  1376. orig_iov_len -= old_tx_marker;
  1377. per_iov_bytes += old_tx_marker;
  1378. pr_debug("tx_data: #3 new_tx_marker"
  1379. " %u, size %u\n", *tx_marker, size);
  1380. pr_debug("tx_data: #4 offset %u\n",
  1381. tx_marker_val[tx_marker_iov-1]);
  1382. } else {
  1383. iov[iov_count].iov_len = orig_iov_len;
  1384. iov[iov_count++].iov_base
  1385. = (iov_record[orig_iov_loc].iov_base +
  1386. per_iov_bytes);
  1387. per_iov_bytes = 0;
  1388. *tx_marker -= orig_iov_len;
  1389. size -= orig_iov_len;
  1390. if (size)
  1391. orig_iov_len =
  1392. iov_record[++orig_iov_loc].iov_len;
  1393. pr_debug("tx_data: #5 new_tx_marker"
  1394. " %u, size %u\n", *tx_marker, size);
  1395. }
  1396. }
  1397. data += (tx_marker_iov * (MARKER_SIZE / 2));
  1398. iov_p = &iov[0];
  1399. iov_len = iov_count;
  1400. if (iov_count > count->ss_iov_count) {
  1401. pr_err("iov_count: %d, count->ss_iov_count:"
  1402. " %d\n", iov_count, count->ss_iov_count);
  1403. return -1;
  1404. }
  1405. if (tx_marker_iov > count->ss_marker_count) {
  1406. pr_err("tx_marker_iov: %d, count->ss_marker"
  1407. "_count: %d\n", tx_marker_iov,
  1408. count->ss_marker_count);
  1409. return -1;
  1410. }
  1411. } else {
  1412. iov_p = count->iov;
  1413. iov_len = count->iov_count;
  1414. }
  1415. while (total_tx < data) {
  1416. tx_loop = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
  1417. (data - total_tx));
  1418. if (tx_loop <= 0) {
  1419. pr_debug("tx_loop: %d total_tx %d\n",
  1420. tx_loop, total_tx);
  1421. return tx_loop;
  1422. }
  1423. total_tx += tx_loop;
  1424. pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
  1425. tx_loop, total_tx, data);
  1426. }
  1427. if (count->sync_and_steering)
  1428. total_tx -= (tx_marker_iov * (MARKER_SIZE / 2));
  1429. return total_tx;
  1430. }
  1431. int rx_data(
  1432. struct iscsi_conn *conn,
  1433. struct kvec *iov,
  1434. int iov_count,
  1435. int data)
  1436. {
  1437. struct iscsi_data_count c;
  1438. if (!conn || !conn->sock || !conn->conn_ops)
  1439. return -1;
  1440. memset(&c, 0, sizeof(struct iscsi_data_count));
  1441. c.iov = iov;
  1442. c.iov_count = iov_count;
  1443. c.data_length = data;
  1444. c.type = ISCSI_RX_DATA;
  1445. if (conn->conn_ops->OFMarker &&
  1446. (conn->conn_state >= TARG_CONN_STATE_LOGGED_IN)) {
  1447. if (iscsit_determine_sync_and_steering_counts(conn, &c) < 0)
  1448. return -1;
  1449. }
  1450. return iscsit_do_rx_data(conn, &c);
  1451. }
  1452. int tx_data(
  1453. struct iscsi_conn *conn,
  1454. struct kvec *iov,
  1455. int iov_count,
  1456. int data)
  1457. {
  1458. struct iscsi_data_count c;
  1459. if (!conn || !conn->sock || !conn->conn_ops)
  1460. return -1;
  1461. memset(&c, 0, sizeof(struct iscsi_data_count));
  1462. c.iov = iov;
  1463. c.iov_count = iov_count;
  1464. c.data_length = data;
  1465. c.type = ISCSI_TX_DATA;
  1466. if (conn->conn_ops->IFMarker &&
  1467. (conn->conn_state >= TARG_CONN_STATE_LOGGED_IN)) {
  1468. if (iscsit_determine_sync_and_steering_counts(conn, &c) < 0)
  1469. return -1;
  1470. }
  1471. return iscsit_do_tx_data(conn, &c);
  1472. }
  1473. void iscsit_collect_login_stats(
  1474. struct iscsi_conn *conn,
  1475. u8 status_class,
  1476. u8 status_detail)
  1477. {
  1478. struct iscsi_param *intrname = NULL;
  1479. struct iscsi_tiqn *tiqn;
  1480. struct iscsi_login_stats *ls;
  1481. tiqn = iscsit_snmp_get_tiqn(conn);
  1482. if (!tiqn)
  1483. return;
  1484. ls = &tiqn->login_stats;
  1485. spin_lock(&ls->lock);
  1486. if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
  1487. ((get_jiffies_64() - ls->last_fail_time) < 10)) {
  1488. /* We already have the failure info for this login */
  1489. spin_unlock(&ls->lock);
  1490. return;
  1491. }
  1492. if (status_class == ISCSI_STATUS_CLS_SUCCESS)
  1493. ls->accepts++;
  1494. else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
  1495. ls->redirects++;
  1496. ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
  1497. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1498. (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
  1499. ls->authenticate_fails++;
  1500. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
  1501. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1502. (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
  1503. ls->authorize_fails++;
  1504. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
  1505. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1506. (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
  1507. ls->negotiate_fails++;
  1508. ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
  1509. } else {
  1510. ls->other_fails++;
  1511. ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
  1512. }
  1513. /* Save initiator name, ip address and time, if it is a failed login */
  1514. if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
  1515. if (conn->param_list)
  1516. intrname = iscsi_find_param_from_key(INITIATORNAME,
  1517. conn->param_list);
  1518. strcpy(ls->last_intr_fail_name,
  1519. (intrname ? intrname->value : "Unknown"));
  1520. ls->last_intr_fail_ip_family = conn->sock->sk->sk_family;
  1521. snprintf(ls->last_intr_fail_ip_addr, IPV6_ADDRESS_SPACE,
  1522. "%s", conn->login_ip);
  1523. ls->last_fail_time = get_jiffies_64();
  1524. }
  1525. spin_unlock(&ls->lock);
  1526. }
  1527. struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
  1528. {
  1529. struct iscsi_portal_group *tpg;
  1530. if (!conn || !conn->sess)
  1531. return NULL;
  1532. tpg = conn->sess->tpg;
  1533. if (!tpg)
  1534. return NULL;
  1535. if (!tpg->tpg_tiqn)
  1536. return NULL;
  1537. return tpg->tpg_tiqn;
  1538. }