iscsi_target_util.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  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. * Setup conn->if_marker and conn->of_marker values based upon
  751. * the initial marker-less interval. (see iSCSI v19 A.2)
  752. */
  753. int iscsit_set_sync_and_steering_values(struct iscsi_conn *conn)
  754. {
  755. int login_ifmarker_count = 0, login_ofmarker_count = 0, next_marker = 0;
  756. /*
  757. * IFMarkInt and OFMarkInt are negotiated as 32-bit words.
  758. */
  759. u32 IFMarkInt = (conn->conn_ops->IFMarkInt * 4);
  760. u32 OFMarkInt = (conn->conn_ops->OFMarkInt * 4);
  761. if (conn->conn_ops->OFMarker) {
  762. /*
  763. * Account for the first Login Command received not
  764. * via iscsi_recv_msg().
  765. */
  766. conn->of_marker += ISCSI_HDR_LEN;
  767. if (conn->of_marker <= OFMarkInt) {
  768. conn->of_marker = (OFMarkInt - conn->of_marker);
  769. } else {
  770. login_ofmarker_count = (conn->of_marker / OFMarkInt);
  771. next_marker = (OFMarkInt * (login_ofmarker_count + 1)) +
  772. (login_ofmarker_count * MARKER_SIZE);
  773. conn->of_marker = (next_marker - conn->of_marker);
  774. }
  775. conn->of_marker_offset = 0;
  776. pr_debug("Setting OFMarker value to %u based on Initial"
  777. " Markerless Interval.\n", conn->of_marker);
  778. }
  779. if (conn->conn_ops->IFMarker) {
  780. if (conn->if_marker <= IFMarkInt) {
  781. conn->if_marker = (IFMarkInt - conn->if_marker);
  782. } else {
  783. login_ifmarker_count = (conn->if_marker / IFMarkInt);
  784. next_marker = (IFMarkInt * (login_ifmarker_count + 1)) +
  785. (login_ifmarker_count * MARKER_SIZE);
  786. conn->if_marker = (next_marker - conn->if_marker);
  787. }
  788. pr_debug("Setting IFMarker value to %u based on Initial"
  789. " Markerless Interval.\n", conn->if_marker);
  790. }
  791. return 0;
  792. }
  793. struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
  794. {
  795. struct iscsi_conn *conn;
  796. spin_lock_bh(&sess->conn_lock);
  797. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  798. if ((conn->cid == cid) &&
  799. (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
  800. iscsit_inc_conn_usage_count(conn);
  801. spin_unlock_bh(&sess->conn_lock);
  802. return conn;
  803. }
  804. }
  805. spin_unlock_bh(&sess->conn_lock);
  806. return NULL;
  807. }
  808. struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
  809. {
  810. struct iscsi_conn *conn;
  811. spin_lock_bh(&sess->conn_lock);
  812. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  813. if (conn->cid == cid) {
  814. iscsit_inc_conn_usage_count(conn);
  815. spin_lock(&conn->state_lock);
  816. atomic_set(&conn->connection_wait_rcfr, 1);
  817. spin_unlock(&conn->state_lock);
  818. spin_unlock_bh(&sess->conn_lock);
  819. return conn;
  820. }
  821. }
  822. spin_unlock_bh(&sess->conn_lock);
  823. return NULL;
  824. }
  825. void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
  826. {
  827. spin_lock_bh(&conn->conn_usage_lock);
  828. if (conn->conn_usage_count != 0) {
  829. conn->conn_waiting_on_uc = 1;
  830. spin_unlock_bh(&conn->conn_usage_lock);
  831. wait_for_completion(&conn->conn_waiting_on_uc_comp);
  832. return;
  833. }
  834. spin_unlock_bh(&conn->conn_usage_lock);
  835. }
  836. void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
  837. {
  838. spin_lock_bh(&conn->conn_usage_lock);
  839. conn->conn_usage_count--;
  840. if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
  841. complete(&conn->conn_waiting_on_uc_comp);
  842. spin_unlock_bh(&conn->conn_usage_lock);
  843. }
  844. void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
  845. {
  846. spin_lock_bh(&conn->conn_usage_lock);
  847. conn->conn_usage_count++;
  848. spin_unlock_bh(&conn->conn_usage_lock);
  849. }
  850. static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
  851. {
  852. u8 state;
  853. struct iscsi_cmd *cmd;
  854. cmd = iscsit_allocate_cmd(conn, GFP_ATOMIC);
  855. if (!cmd)
  856. return -1;
  857. cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
  858. state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
  859. ISTATE_SEND_NOPIN_NO_RESPONSE;
  860. cmd->init_task_tag = 0xFFFFFFFF;
  861. spin_lock_bh(&conn->sess->ttt_lock);
  862. cmd->targ_xfer_tag = (want_response) ? conn->sess->targ_xfer_tag++ :
  863. 0xFFFFFFFF;
  864. if (want_response && (cmd->targ_xfer_tag == 0xFFFFFFFF))
  865. cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
  866. spin_unlock_bh(&conn->sess->ttt_lock);
  867. spin_lock_bh(&conn->cmd_lock);
  868. list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
  869. spin_unlock_bh(&conn->cmd_lock);
  870. if (want_response)
  871. iscsit_start_nopin_response_timer(conn);
  872. iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
  873. return 0;
  874. }
  875. static void iscsit_handle_nopin_response_timeout(unsigned long data)
  876. {
  877. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  878. iscsit_inc_conn_usage_count(conn);
  879. spin_lock_bh(&conn->nopin_timer_lock);
  880. if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
  881. spin_unlock_bh(&conn->nopin_timer_lock);
  882. iscsit_dec_conn_usage_count(conn);
  883. return;
  884. }
  885. pr_debug("Did not receive response to NOPIN on CID: %hu on"
  886. " SID: %u, failing connection.\n", conn->cid,
  887. conn->sess->sid);
  888. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  889. spin_unlock_bh(&conn->nopin_timer_lock);
  890. {
  891. struct iscsi_portal_group *tpg = conn->sess->tpg;
  892. struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
  893. if (tiqn) {
  894. spin_lock_bh(&tiqn->sess_err_stats.lock);
  895. strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
  896. (void *)conn->sess->sess_ops->InitiatorName);
  897. tiqn->sess_err_stats.last_sess_failure_type =
  898. ISCSI_SESS_ERR_CXN_TIMEOUT;
  899. tiqn->sess_err_stats.cxn_timeout_errors++;
  900. conn->sess->conn_timeout_errors++;
  901. spin_unlock_bh(&tiqn->sess_err_stats.lock);
  902. }
  903. }
  904. iscsit_cause_connection_reinstatement(conn, 0);
  905. iscsit_dec_conn_usage_count(conn);
  906. }
  907. void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
  908. {
  909. struct iscsi_session *sess = conn->sess;
  910. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  911. spin_lock_bh(&conn->nopin_timer_lock);
  912. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  913. spin_unlock_bh(&conn->nopin_timer_lock);
  914. return;
  915. }
  916. mod_timer(&conn->nopin_response_timer,
  917. (get_jiffies_64() + na->nopin_response_timeout * HZ));
  918. spin_unlock_bh(&conn->nopin_timer_lock);
  919. }
  920. /*
  921. * Called with conn->nopin_timer_lock held.
  922. */
  923. void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
  924. {
  925. struct iscsi_session *sess = conn->sess;
  926. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  927. spin_lock_bh(&conn->nopin_timer_lock);
  928. if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
  929. spin_unlock_bh(&conn->nopin_timer_lock);
  930. return;
  931. }
  932. init_timer(&conn->nopin_response_timer);
  933. conn->nopin_response_timer.expires =
  934. (get_jiffies_64() + na->nopin_response_timeout * HZ);
  935. conn->nopin_response_timer.data = (unsigned long)conn;
  936. conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
  937. conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
  938. conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
  939. add_timer(&conn->nopin_response_timer);
  940. pr_debug("Started NOPIN Response Timer on CID: %d to %u"
  941. " seconds\n", conn->cid, na->nopin_response_timeout);
  942. spin_unlock_bh(&conn->nopin_timer_lock);
  943. }
  944. void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
  945. {
  946. spin_lock_bh(&conn->nopin_timer_lock);
  947. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  948. spin_unlock_bh(&conn->nopin_timer_lock);
  949. return;
  950. }
  951. conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
  952. spin_unlock_bh(&conn->nopin_timer_lock);
  953. del_timer_sync(&conn->nopin_response_timer);
  954. spin_lock_bh(&conn->nopin_timer_lock);
  955. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  956. spin_unlock_bh(&conn->nopin_timer_lock);
  957. }
  958. static void iscsit_handle_nopin_timeout(unsigned long data)
  959. {
  960. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  961. iscsit_inc_conn_usage_count(conn);
  962. spin_lock_bh(&conn->nopin_timer_lock);
  963. if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
  964. spin_unlock_bh(&conn->nopin_timer_lock);
  965. iscsit_dec_conn_usage_count(conn);
  966. return;
  967. }
  968. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  969. spin_unlock_bh(&conn->nopin_timer_lock);
  970. iscsit_add_nopin(conn, 1);
  971. iscsit_dec_conn_usage_count(conn);
  972. }
  973. /*
  974. * Called with conn->nopin_timer_lock held.
  975. */
  976. void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
  977. {
  978. struct iscsi_session *sess = conn->sess;
  979. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  980. /*
  981. * NOPIN timeout is disabled.
  982. */
  983. if (!na->nopin_timeout)
  984. return;
  985. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
  986. return;
  987. init_timer(&conn->nopin_timer);
  988. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  989. conn->nopin_timer.data = (unsigned long)conn;
  990. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  991. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  992. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  993. add_timer(&conn->nopin_timer);
  994. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  995. " interval\n", conn->cid, na->nopin_timeout);
  996. }
  997. void iscsit_start_nopin_timer(struct iscsi_conn *conn)
  998. {
  999. struct iscsi_session *sess = conn->sess;
  1000. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1001. /*
  1002. * NOPIN timeout is disabled..
  1003. */
  1004. if (!na->nopin_timeout)
  1005. return;
  1006. spin_lock_bh(&conn->nopin_timer_lock);
  1007. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
  1008. spin_unlock_bh(&conn->nopin_timer_lock);
  1009. return;
  1010. }
  1011. init_timer(&conn->nopin_timer);
  1012. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  1013. conn->nopin_timer.data = (unsigned long)conn;
  1014. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  1015. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  1016. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  1017. add_timer(&conn->nopin_timer);
  1018. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  1019. " interval\n", conn->cid, na->nopin_timeout);
  1020. spin_unlock_bh(&conn->nopin_timer_lock);
  1021. }
  1022. void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
  1023. {
  1024. spin_lock_bh(&conn->nopin_timer_lock);
  1025. if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
  1026. spin_unlock_bh(&conn->nopin_timer_lock);
  1027. return;
  1028. }
  1029. conn->nopin_timer_flags |= ISCSI_TF_STOP;
  1030. spin_unlock_bh(&conn->nopin_timer_lock);
  1031. del_timer_sync(&conn->nopin_timer);
  1032. spin_lock_bh(&conn->nopin_timer_lock);
  1033. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  1034. spin_unlock_bh(&conn->nopin_timer_lock);
  1035. }
  1036. int iscsit_send_tx_data(
  1037. struct iscsi_cmd *cmd,
  1038. struct iscsi_conn *conn,
  1039. int use_misc)
  1040. {
  1041. int tx_sent, tx_size;
  1042. u32 iov_count;
  1043. struct kvec *iov;
  1044. send_data:
  1045. tx_size = cmd->tx_size;
  1046. if (!use_misc) {
  1047. iov = &cmd->iov_data[0];
  1048. iov_count = cmd->iov_data_count;
  1049. } else {
  1050. iov = &cmd->iov_misc[0];
  1051. iov_count = cmd->iov_misc_count;
  1052. }
  1053. tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
  1054. if (tx_size != tx_sent) {
  1055. if (tx_sent == -EAGAIN) {
  1056. pr_err("tx_data() returned -EAGAIN\n");
  1057. goto send_data;
  1058. } else
  1059. return -1;
  1060. }
  1061. cmd->tx_size = 0;
  1062. return 0;
  1063. }
  1064. int iscsit_fe_sendpage_sg(
  1065. struct iscsi_cmd *cmd,
  1066. struct iscsi_conn *conn)
  1067. {
  1068. struct scatterlist *sg = cmd->first_data_sg;
  1069. struct kvec iov;
  1070. u32 tx_hdr_size, data_len;
  1071. u32 offset = cmd->first_data_sg_off;
  1072. int tx_sent, iov_off;
  1073. send_hdr:
  1074. tx_hdr_size = ISCSI_HDR_LEN;
  1075. if (conn->conn_ops->HeaderDigest)
  1076. tx_hdr_size += ISCSI_CRC_LEN;
  1077. iov.iov_base = cmd->pdu;
  1078. iov.iov_len = tx_hdr_size;
  1079. tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
  1080. if (tx_hdr_size != tx_sent) {
  1081. if (tx_sent == -EAGAIN) {
  1082. pr_err("tx_data() returned -EAGAIN\n");
  1083. goto send_hdr;
  1084. }
  1085. return -1;
  1086. }
  1087. data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
  1088. /*
  1089. * Set iov_off used by padding and data digest tx_data() calls below
  1090. * in order to determine proper offset into cmd->iov_data[]
  1091. */
  1092. if (conn->conn_ops->DataDigest) {
  1093. data_len -= ISCSI_CRC_LEN;
  1094. if (cmd->padding)
  1095. iov_off = (cmd->iov_data_count - 2);
  1096. else
  1097. iov_off = (cmd->iov_data_count - 1);
  1098. } else {
  1099. iov_off = (cmd->iov_data_count - 1);
  1100. }
  1101. /*
  1102. * Perform sendpage() for each page in the scatterlist
  1103. */
  1104. while (data_len) {
  1105. u32 space = (sg->length - offset);
  1106. u32 sub_len = min_t(u32, data_len, space);
  1107. send_pg:
  1108. tx_sent = conn->sock->ops->sendpage(conn->sock,
  1109. sg_page(sg), sg->offset + offset, sub_len, 0);
  1110. if (tx_sent != sub_len) {
  1111. if (tx_sent == -EAGAIN) {
  1112. pr_err("tcp_sendpage() returned"
  1113. " -EAGAIN\n");
  1114. goto send_pg;
  1115. }
  1116. pr_err("tcp_sendpage() failure: %d\n",
  1117. tx_sent);
  1118. return -1;
  1119. }
  1120. data_len -= sub_len;
  1121. offset = 0;
  1122. sg = sg_next(sg);
  1123. }
  1124. send_padding:
  1125. if (cmd->padding) {
  1126. struct kvec *iov_p = &cmd->iov_data[iov_off++];
  1127. tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
  1128. if (cmd->padding != tx_sent) {
  1129. if (tx_sent == -EAGAIN) {
  1130. pr_err("tx_data() returned -EAGAIN\n");
  1131. goto send_padding;
  1132. }
  1133. return -1;
  1134. }
  1135. }
  1136. send_datacrc:
  1137. if (conn->conn_ops->DataDigest) {
  1138. struct kvec *iov_d = &cmd->iov_data[iov_off];
  1139. tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
  1140. if (ISCSI_CRC_LEN != tx_sent) {
  1141. if (tx_sent == -EAGAIN) {
  1142. pr_err("tx_data() returned -EAGAIN\n");
  1143. goto send_datacrc;
  1144. }
  1145. return -1;
  1146. }
  1147. }
  1148. return 0;
  1149. }
  1150. /*
  1151. * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
  1152. * back to the Initiator when an expection condition occurs with the
  1153. * errors set in status_class and status_detail.
  1154. *
  1155. * Parameters: iSCSI Connection, Status Class, Status Detail.
  1156. * Returns: 0 on success, -1 on error.
  1157. */
  1158. int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
  1159. {
  1160. u8 iscsi_hdr[ISCSI_HDR_LEN];
  1161. int err;
  1162. struct kvec iov;
  1163. struct iscsi_login_rsp *hdr;
  1164. iscsit_collect_login_stats(conn, status_class, status_detail);
  1165. memset(&iov, 0, sizeof(struct kvec));
  1166. memset(&iscsi_hdr, 0x0, ISCSI_HDR_LEN);
  1167. hdr = (struct iscsi_login_rsp *)&iscsi_hdr;
  1168. hdr->opcode = ISCSI_OP_LOGIN_RSP;
  1169. hdr->status_class = status_class;
  1170. hdr->status_detail = status_detail;
  1171. hdr->itt = cpu_to_be32(conn->login_itt);
  1172. iov.iov_base = &iscsi_hdr;
  1173. iov.iov_len = ISCSI_HDR_LEN;
  1174. PRINT_BUFF(iscsi_hdr, ISCSI_HDR_LEN);
  1175. err = tx_data(conn, &iov, 1, ISCSI_HDR_LEN);
  1176. if (err != ISCSI_HDR_LEN) {
  1177. pr_err("tx_data returned less than expected\n");
  1178. return -1;
  1179. }
  1180. return 0;
  1181. }
  1182. void iscsit_print_session_params(struct iscsi_session *sess)
  1183. {
  1184. struct iscsi_conn *conn;
  1185. pr_debug("-----------------------------[Session Params for"
  1186. " SID: %u]-----------------------------\n", sess->sid);
  1187. spin_lock_bh(&sess->conn_lock);
  1188. list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
  1189. iscsi_dump_conn_ops(conn->conn_ops);
  1190. spin_unlock_bh(&sess->conn_lock);
  1191. iscsi_dump_sess_ops(sess->sess_ops);
  1192. }
  1193. static int iscsit_do_rx_data(
  1194. struct iscsi_conn *conn,
  1195. struct iscsi_data_count *count)
  1196. {
  1197. int data = count->data_length, rx_loop = 0, total_rx = 0, iov_len;
  1198. struct kvec *iov_p;
  1199. struct msghdr msg;
  1200. if (!conn || !conn->sock || !conn->conn_ops)
  1201. return -1;
  1202. memset(&msg, 0, sizeof(struct msghdr));
  1203. iov_p = count->iov;
  1204. iov_len = count->iov_count;
  1205. while (total_rx < data) {
  1206. rx_loop = kernel_recvmsg(conn->sock, &msg, iov_p, iov_len,
  1207. (data - total_rx), MSG_WAITALL);
  1208. if (rx_loop <= 0) {
  1209. pr_debug("rx_loop: %d total_rx: %d\n",
  1210. rx_loop, total_rx);
  1211. return rx_loop;
  1212. }
  1213. total_rx += rx_loop;
  1214. pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
  1215. rx_loop, total_rx, data);
  1216. }
  1217. return total_rx;
  1218. }
  1219. static int iscsit_do_tx_data(
  1220. struct iscsi_conn *conn,
  1221. struct iscsi_data_count *count)
  1222. {
  1223. int data = count->data_length, total_tx = 0, tx_loop = 0, iov_len;
  1224. struct kvec *iov_p;
  1225. struct msghdr msg;
  1226. if (!conn || !conn->sock || !conn->conn_ops)
  1227. return -1;
  1228. if (data <= 0) {
  1229. pr_err("Data length is: %d\n", data);
  1230. return -1;
  1231. }
  1232. memset(&msg, 0, sizeof(struct msghdr));
  1233. iov_p = count->iov;
  1234. iov_len = count->iov_count;
  1235. while (total_tx < data) {
  1236. tx_loop = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
  1237. (data - total_tx));
  1238. if (tx_loop <= 0) {
  1239. pr_debug("tx_loop: %d total_tx %d\n",
  1240. tx_loop, total_tx);
  1241. return tx_loop;
  1242. }
  1243. total_tx += tx_loop;
  1244. pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
  1245. tx_loop, total_tx, data);
  1246. }
  1247. return total_tx;
  1248. }
  1249. int rx_data(
  1250. struct iscsi_conn *conn,
  1251. struct kvec *iov,
  1252. int iov_count,
  1253. int data)
  1254. {
  1255. struct iscsi_data_count c;
  1256. if (!conn || !conn->sock || !conn->conn_ops)
  1257. return -1;
  1258. memset(&c, 0, sizeof(struct iscsi_data_count));
  1259. c.iov = iov;
  1260. c.iov_count = iov_count;
  1261. c.data_length = data;
  1262. c.type = ISCSI_RX_DATA;
  1263. return iscsit_do_rx_data(conn, &c);
  1264. }
  1265. int tx_data(
  1266. struct iscsi_conn *conn,
  1267. struct kvec *iov,
  1268. int iov_count,
  1269. int data)
  1270. {
  1271. struct iscsi_data_count c;
  1272. if (!conn || !conn->sock || !conn->conn_ops)
  1273. return -1;
  1274. memset(&c, 0, sizeof(struct iscsi_data_count));
  1275. c.iov = iov;
  1276. c.iov_count = iov_count;
  1277. c.data_length = data;
  1278. c.type = ISCSI_TX_DATA;
  1279. return iscsit_do_tx_data(conn, &c);
  1280. }
  1281. void iscsit_collect_login_stats(
  1282. struct iscsi_conn *conn,
  1283. u8 status_class,
  1284. u8 status_detail)
  1285. {
  1286. struct iscsi_param *intrname = NULL;
  1287. struct iscsi_tiqn *tiqn;
  1288. struct iscsi_login_stats *ls;
  1289. tiqn = iscsit_snmp_get_tiqn(conn);
  1290. if (!tiqn)
  1291. return;
  1292. ls = &tiqn->login_stats;
  1293. spin_lock(&ls->lock);
  1294. if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
  1295. ((get_jiffies_64() - ls->last_fail_time) < 10)) {
  1296. /* We already have the failure info for this login */
  1297. spin_unlock(&ls->lock);
  1298. return;
  1299. }
  1300. if (status_class == ISCSI_STATUS_CLS_SUCCESS)
  1301. ls->accepts++;
  1302. else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
  1303. ls->redirects++;
  1304. ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
  1305. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1306. (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
  1307. ls->authenticate_fails++;
  1308. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
  1309. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1310. (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
  1311. ls->authorize_fails++;
  1312. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
  1313. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1314. (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
  1315. ls->negotiate_fails++;
  1316. ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
  1317. } else {
  1318. ls->other_fails++;
  1319. ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
  1320. }
  1321. /* Save initiator name, ip address and time, if it is a failed login */
  1322. if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
  1323. if (conn->param_list)
  1324. intrname = iscsi_find_param_from_key(INITIATORNAME,
  1325. conn->param_list);
  1326. strcpy(ls->last_intr_fail_name,
  1327. (intrname ? intrname->value : "Unknown"));
  1328. ls->last_intr_fail_ip_family = conn->sock->sk->sk_family;
  1329. snprintf(ls->last_intr_fail_ip_addr, IPV6_ADDRESS_SPACE,
  1330. "%s", conn->login_ip);
  1331. ls->last_fail_time = get_jiffies_64();
  1332. }
  1333. spin_unlock(&ls->lock);
  1334. }
  1335. struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
  1336. {
  1337. struct iscsi_portal_group *tpg;
  1338. if (!conn || !conn->sess)
  1339. return NULL;
  1340. tpg = conn->sess->tpg;
  1341. if (!tpg)
  1342. return NULL;
  1343. if (!tpg->tpg_tiqn)
  1344. return NULL;
  1345. return tpg->tpg_tiqn;
  1346. }