iscsi_target_util.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  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. GFP_KERNEL);
  261. if (!se_cmd->se_tmr_req)
  262. goto out;
  263. cmd->tmr_req->se_tmr_req = se_cmd->se_tmr_req;
  264. return cmd;
  265. out:
  266. iscsit_release_cmd(cmd);
  267. return NULL;
  268. }
  269. int iscsit_decide_list_to_build(
  270. struct iscsi_cmd *cmd,
  271. u32 immediate_data_length)
  272. {
  273. struct iscsi_build_list bl;
  274. struct iscsi_conn *conn = cmd->conn;
  275. struct iscsi_session *sess = conn->sess;
  276. struct iscsi_node_attrib *na;
  277. if (sess->sess_ops->DataSequenceInOrder &&
  278. sess->sess_ops->DataPDUInOrder)
  279. return 0;
  280. if (cmd->data_direction == DMA_NONE)
  281. return 0;
  282. na = iscsit_tpg_get_node_attrib(sess);
  283. memset(&bl, 0, sizeof(struct iscsi_build_list));
  284. if (cmd->data_direction == DMA_FROM_DEVICE) {
  285. bl.data_direction = ISCSI_PDU_READ;
  286. bl.type = PDULIST_NORMAL;
  287. if (na->random_datain_pdu_offsets)
  288. bl.randomize |= RANDOM_DATAIN_PDU_OFFSETS;
  289. if (na->random_datain_seq_offsets)
  290. bl.randomize |= RANDOM_DATAIN_SEQ_OFFSETS;
  291. } else {
  292. bl.data_direction = ISCSI_PDU_WRITE;
  293. bl.immediate_data_length = immediate_data_length;
  294. if (na->random_r2t_offsets)
  295. bl.randomize |= RANDOM_R2T_OFFSETS;
  296. if (!cmd->immediate_data && !cmd->unsolicited_data)
  297. bl.type = PDULIST_NORMAL;
  298. else if (cmd->immediate_data && !cmd->unsolicited_data)
  299. bl.type = PDULIST_IMMEDIATE;
  300. else if (!cmd->immediate_data && cmd->unsolicited_data)
  301. bl.type = PDULIST_UNSOLICITED;
  302. else if (cmd->immediate_data && cmd->unsolicited_data)
  303. bl.type = PDULIST_IMMEDIATE_AND_UNSOLICITED;
  304. }
  305. return iscsit_do_build_list(cmd, &bl);
  306. }
  307. struct iscsi_seq *iscsit_get_seq_holder_for_datain(
  308. struct iscsi_cmd *cmd,
  309. u32 seq_send_order)
  310. {
  311. u32 i;
  312. for (i = 0; i < cmd->seq_count; i++)
  313. if (cmd->seq_list[i].seq_send_order == seq_send_order)
  314. return &cmd->seq_list[i];
  315. return NULL;
  316. }
  317. struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
  318. {
  319. u32 i;
  320. if (!cmd->seq_list) {
  321. pr_err("struct iscsi_cmd->seq_list is NULL!\n");
  322. return NULL;
  323. }
  324. for (i = 0; i < cmd->seq_count; i++) {
  325. if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
  326. continue;
  327. if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
  328. cmd->seq_send_order++;
  329. return &cmd->seq_list[i];
  330. }
  331. }
  332. return NULL;
  333. }
  334. struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
  335. struct iscsi_cmd *cmd,
  336. u32 r2t_sn)
  337. {
  338. struct iscsi_r2t *r2t;
  339. spin_lock_bh(&cmd->r2t_lock);
  340. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  341. if (r2t->r2t_sn == r2t_sn) {
  342. spin_unlock_bh(&cmd->r2t_lock);
  343. return r2t;
  344. }
  345. }
  346. spin_unlock_bh(&cmd->r2t_lock);
  347. return NULL;
  348. }
  349. static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
  350. {
  351. int ret;
  352. /*
  353. * This is the proper method of checking received CmdSN against
  354. * ExpCmdSN and MaxCmdSN values, as well as accounting for out
  355. * or order CmdSNs due to multiple connection sessions and/or
  356. * CRC failures.
  357. */
  358. if (iscsi_sna_gt(cmdsn, sess->max_cmd_sn)) {
  359. pr_err("Received CmdSN: 0x%08x is greater than"
  360. " MaxCmdSN: 0x%08x, protocol error.\n", cmdsn,
  361. sess->max_cmd_sn);
  362. ret = CMDSN_ERROR_CANNOT_RECOVER;
  363. } else if (cmdsn == sess->exp_cmd_sn) {
  364. sess->exp_cmd_sn++;
  365. pr_debug("Received CmdSN matches ExpCmdSN,"
  366. " incremented ExpCmdSN to: 0x%08x\n",
  367. sess->exp_cmd_sn);
  368. ret = CMDSN_NORMAL_OPERATION;
  369. } else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
  370. pr_debug("Received CmdSN: 0x%08x is greater"
  371. " than ExpCmdSN: 0x%08x, not acknowledging.\n",
  372. cmdsn, sess->exp_cmd_sn);
  373. ret = CMDSN_HIGHER_THAN_EXP;
  374. } else {
  375. pr_err("Received CmdSN: 0x%08x is less than"
  376. " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
  377. sess->exp_cmd_sn);
  378. ret = CMDSN_LOWER_THAN_EXP;
  379. }
  380. return ret;
  381. }
  382. /*
  383. * Commands may be received out of order if MC/S is in use.
  384. * Ensure they are executed in CmdSN order.
  385. */
  386. int iscsit_sequence_cmd(
  387. struct iscsi_conn *conn,
  388. struct iscsi_cmd *cmd,
  389. u32 cmdsn)
  390. {
  391. int ret;
  392. int cmdsn_ret;
  393. mutex_lock(&conn->sess->cmdsn_mutex);
  394. cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, cmdsn);
  395. switch (cmdsn_ret) {
  396. case CMDSN_NORMAL_OPERATION:
  397. ret = iscsit_execute_cmd(cmd, 0);
  398. if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
  399. iscsit_execute_ooo_cmdsns(conn->sess);
  400. break;
  401. case CMDSN_HIGHER_THAN_EXP:
  402. ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, cmdsn);
  403. break;
  404. case CMDSN_LOWER_THAN_EXP:
  405. cmd->i_state = ISTATE_REMOVE;
  406. iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
  407. ret = cmdsn_ret;
  408. break;
  409. default:
  410. ret = cmdsn_ret;
  411. break;
  412. }
  413. mutex_unlock(&conn->sess->cmdsn_mutex);
  414. return ret;
  415. }
  416. int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
  417. {
  418. struct iscsi_conn *conn = cmd->conn;
  419. struct se_cmd *se_cmd = &cmd->se_cmd;
  420. struct iscsi_data *hdr = (struct iscsi_data *) buf;
  421. u32 payload_length = ntoh24(hdr->dlength);
  422. if (conn->sess->sess_ops->InitialR2T) {
  423. pr_err("Received unexpected unsolicited data"
  424. " while InitialR2T=Yes, protocol error.\n");
  425. transport_send_check_condition_and_sense(se_cmd,
  426. TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
  427. return -1;
  428. }
  429. if ((cmd->first_burst_len + payload_length) >
  430. conn->sess->sess_ops->FirstBurstLength) {
  431. pr_err("Total %u bytes exceeds FirstBurstLength: %u"
  432. " for this Unsolicited DataOut Burst.\n",
  433. (cmd->first_burst_len + payload_length),
  434. conn->sess->sess_ops->FirstBurstLength);
  435. transport_send_check_condition_and_sense(se_cmd,
  436. TCM_INCORRECT_AMOUNT_OF_DATA, 0);
  437. return -1;
  438. }
  439. if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
  440. return 0;
  441. if (((cmd->first_burst_len + payload_length) != cmd->data_length) &&
  442. ((cmd->first_burst_len + payload_length) !=
  443. conn->sess->sess_ops->FirstBurstLength)) {
  444. pr_err("Unsolicited non-immediate data received %u"
  445. " does not equal FirstBurstLength: %u, and does"
  446. " not equal ExpXferLen %u.\n",
  447. (cmd->first_burst_len + payload_length),
  448. conn->sess->sess_ops->FirstBurstLength, cmd->data_length);
  449. transport_send_check_condition_and_sense(se_cmd,
  450. TCM_INCORRECT_AMOUNT_OF_DATA, 0);
  451. return -1;
  452. }
  453. return 0;
  454. }
  455. struct iscsi_cmd *iscsit_find_cmd_from_itt(
  456. struct iscsi_conn *conn,
  457. u32 init_task_tag)
  458. {
  459. struct iscsi_cmd *cmd;
  460. spin_lock_bh(&conn->cmd_lock);
  461. list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
  462. if (cmd->init_task_tag == init_task_tag) {
  463. spin_unlock_bh(&conn->cmd_lock);
  464. return cmd;
  465. }
  466. }
  467. spin_unlock_bh(&conn->cmd_lock);
  468. pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
  469. init_task_tag, conn->cid);
  470. return NULL;
  471. }
  472. struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
  473. struct iscsi_conn *conn,
  474. u32 init_task_tag,
  475. u32 length)
  476. {
  477. struct iscsi_cmd *cmd;
  478. spin_lock_bh(&conn->cmd_lock);
  479. list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
  480. if (cmd->init_task_tag == init_task_tag) {
  481. spin_unlock_bh(&conn->cmd_lock);
  482. return cmd;
  483. }
  484. }
  485. spin_unlock_bh(&conn->cmd_lock);
  486. pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
  487. " dumping payload\n", init_task_tag, conn->cid);
  488. if (length)
  489. iscsit_dump_data_payload(conn, length, 1);
  490. return NULL;
  491. }
  492. struct iscsi_cmd *iscsit_find_cmd_from_ttt(
  493. struct iscsi_conn *conn,
  494. u32 targ_xfer_tag)
  495. {
  496. struct iscsi_cmd *cmd = NULL;
  497. spin_lock_bh(&conn->cmd_lock);
  498. list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
  499. if (cmd->targ_xfer_tag == targ_xfer_tag) {
  500. spin_unlock_bh(&conn->cmd_lock);
  501. return cmd;
  502. }
  503. }
  504. spin_unlock_bh(&conn->cmd_lock);
  505. pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
  506. targ_xfer_tag, conn->cid);
  507. return NULL;
  508. }
  509. int iscsit_find_cmd_for_recovery(
  510. struct iscsi_session *sess,
  511. struct iscsi_cmd **cmd_ptr,
  512. struct iscsi_conn_recovery **cr_ptr,
  513. u32 init_task_tag)
  514. {
  515. struct iscsi_cmd *cmd = NULL;
  516. struct iscsi_conn_recovery *cr;
  517. /*
  518. * Scan through the inactive connection recovery list's command list.
  519. * If init_task_tag matches the command is still alligent.
  520. */
  521. spin_lock(&sess->cr_i_lock);
  522. list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
  523. spin_lock(&cr->conn_recovery_cmd_lock);
  524. list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_list) {
  525. if (cmd->init_task_tag == init_task_tag) {
  526. spin_unlock(&cr->conn_recovery_cmd_lock);
  527. spin_unlock(&sess->cr_i_lock);
  528. *cr_ptr = cr;
  529. *cmd_ptr = cmd;
  530. return -2;
  531. }
  532. }
  533. spin_unlock(&cr->conn_recovery_cmd_lock);
  534. }
  535. spin_unlock(&sess->cr_i_lock);
  536. /*
  537. * Scan through the active connection recovery list's command list.
  538. * If init_task_tag matches the command is ready to be reassigned.
  539. */
  540. spin_lock(&sess->cr_a_lock);
  541. list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
  542. spin_lock(&cr->conn_recovery_cmd_lock);
  543. list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_list) {
  544. if (cmd->init_task_tag == init_task_tag) {
  545. spin_unlock(&cr->conn_recovery_cmd_lock);
  546. spin_unlock(&sess->cr_a_lock);
  547. *cr_ptr = cr;
  548. *cmd_ptr = cmd;
  549. return 0;
  550. }
  551. }
  552. spin_unlock(&cr->conn_recovery_cmd_lock);
  553. }
  554. spin_unlock(&sess->cr_a_lock);
  555. return -1;
  556. }
  557. void iscsit_add_cmd_to_immediate_queue(
  558. struct iscsi_cmd *cmd,
  559. struct iscsi_conn *conn,
  560. u8 state)
  561. {
  562. struct iscsi_queue_req *qr;
  563. qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
  564. if (!qr) {
  565. pr_err("Unable to allocate memory for"
  566. " struct iscsi_queue_req\n");
  567. return;
  568. }
  569. INIT_LIST_HEAD(&qr->qr_list);
  570. qr->cmd = cmd;
  571. qr->state = state;
  572. spin_lock_bh(&conn->immed_queue_lock);
  573. list_add_tail(&qr->qr_list, &conn->immed_queue_list);
  574. atomic_inc(&cmd->immed_queue_count);
  575. atomic_set(&conn->check_immediate_queue, 1);
  576. spin_unlock_bh(&conn->immed_queue_lock);
  577. wake_up_process(conn->thread_set->tx_thread);
  578. }
  579. struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
  580. {
  581. struct iscsi_queue_req *qr;
  582. spin_lock_bh(&conn->immed_queue_lock);
  583. if (list_empty(&conn->immed_queue_list)) {
  584. spin_unlock_bh(&conn->immed_queue_lock);
  585. return NULL;
  586. }
  587. list_for_each_entry(qr, &conn->immed_queue_list, qr_list)
  588. break;
  589. list_del(&qr->qr_list);
  590. if (qr->cmd)
  591. atomic_dec(&qr->cmd->immed_queue_count);
  592. spin_unlock_bh(&conn->immed_queue_lock);
  593. return qr;
  594. }
  595. static void iscsit_remove_cmd_from_immediate_queue(
  596. struct iscsi_cmd *cmd,
  597. struct iscsi_conn *conn)
  598. {
  599. struct iscsi_queue_req *qr, *qr_tmp;
  600. spin_lock_bh(&conn->immed_queue_lock);
  601. if (!atomic_read(&cmd->immed_queue_count)) {
  602. spin_unlock_bh(&conn->immed_queue_lock);
  603. return;
  604. }
  605. list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
  606. if (qr->cmd != cmd)
  607. continue;
  608. atomic_dec(&qr->cmd->immed_queue_count);
  609. list_del(&qr->qr_list);
  610. kmem_cache_free(lio_qr_cache, qr);
  611. }
  612. spin_unlock_bh(&conn->immed_queue_lock);
  613. if (atomic_read(&cmd->immed_queue_count)) {
  614. pr_err("ITT: 0x%08x immed_queue_count: %d\n",
  615. cmd->init_task_tag,
  616. atomic_read(&cmd->immed_queue_count));
  617. }
  618. }
  619. void iscsit_add_cmd_to_response_queue(
  620. struct iscsi_cmd *cmd,
  621. struct iscsi_conn *conn,
  622. u8 state)
  623. {
  624. struct iscsi_queue_req *qr;
  625. qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
  626. if (!qr) {
  627. pr_err("Unable to allocate memory for"
  628. " struct iscsi_queue_req\n");
  629. return;
  630. }
  631. INIT_LIST_HEAD(&qr->qr_list);
  632. qr->cmd = cmd;
  633. qr->state = state;
  634. spin_lock_bh(&conn->response_queue_lock);
  635. list_add_tail(&qr->qr_list, &conn->response_queue_list);
  636. atomic_inc(&cmd->response_queue_count);
  637. spin_unlock_bh(&conn->response_queue_lock);
  638. wake_up_process(conn->thread_set->tx_thread);
  639. }
  640. struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
  641. {
  642. struct iscsi_queue_req *qr;
  643. spin_lock_bh(&conn->response_queue_lock);
  644. if (list_empty(&conn->response_queue_list)) {
  645. spin_unlock_bh(&conn->response_queue_lock);
  646. return NULL;
  647. }
  648. list_for_each_entry(qr, &conn->response_queue_list, qr_list)
  649. break;
  650. list_del(&qr->qr_list);
  651. if (qr->cmd)
  652. atomic_dec(&qr->cmd->response_queue_count);
  653. spin_unlock_bh(&conn->response_queue_lock);
  654. return qr;
  655. }
  656. static void iscsit_remove_cmd_from_response_queue(
  657. struct iscsi_cmd *cmd,
  658. struct iscsi_conn *conn)
  659. {
  660. struct iscsi_queue_req *qr, *qr_tmp;
  661. spin_lock_bh(&conn->response_queue_lock);
  662. if (!atomic_read(&cmd->response_queue_count)) {
  663. spin_unlock_bh(&conn->response_queue_lock);
  664. return;
  665. }
  666. list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
  667. qr_list) {
  668. if (qr->cmd != cmd)
  669. continue;
  670. atomic_dec(&qr->cmd->response_queue_count);
  671. list_del(&qr->qr_list);
  672. kmem_cache_free(lio_qr_cache, qr);
  673. }
  674. spin_unlock_bh(&conn->response_queue_lock);
  675. if (atomic_read(&cmd->response_queue_count)) {
  676. pr_err("ITT: 0x%08x response_queue_count: %d\n",
  677. cmd->init_task_tag,
  678. atomic_read(&cmd->response_queue_count));
  679. }
  680. }
  681. void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
  682. {
  683. struct iscsi_queue_req *qr, *qr_tmp;
  684. spin_lock_bh(&conn->immed_queue_lock);
  685. list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
  686. list_del(&qr->qr_list);
  687. if (qr->cmd)
  688. atomic_dec(&qr->cmd->immed_queue_count);
  689. kmem_cache_free(lio_qr_cache, qr);
  690. }
  691. spin_unlock_bh(&conn->immed_queue_lock);
  692. spin_lock_bh(&conn->response_queue_lock);
  693. list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
  694. qr_list) {
  695. list_del(&qr->qr_list);
  696. if (qr->cmd)
  697. atomic_dec(&qr->cmd->response_queue_count);
  698. kmem_cache_free(lio_qr_cache, qr);
  699. }
  700. spin_unlock_bh(&conn->response_queue_lock);
  701. }
  702. void iscsit_release_cmd(struct iscsi_cmd *cmd)
  703. {
  704. struct iscsi_conn *conn = cmd->conn;
  705. int i;
  706. iscsit_free_r2ts_from_list(cmd);
  707. iscsit_free_all_datain_reqs(cmd);
  708. kfree(cmd->buf_ptr);
  709. kfree(cmd->pdu_list);
  710. kfree(cmd->seq_list);
  711. kfree(cmd->tmr_req);
  712. kfree(cmd->iov_data);
  713. for (i = 0; i < cmd->t_mem_sg_nents; i++)
  714. __free_page(sg_page(&cmd->t_mem_sg[i]));
  715. kfree(cmd->t_mem_sg);
  716. if (conn) {
  717. iscsit_remove_cmd_from_immediate_queue(cmd, conn);
  718. iscsit_remove_cmd_from_response_queue(cmd, conn);
  719. }
  720. kmem_cache_free(lio_cmd_cache, cmd);
  721. }
  722. void iscsit_free_cmd(struct iscsi_cmd *cmd)
  723. {
  724. /*
  725. * Determine if a struct se_cmd is assoicated with
  726. * this struct iscsi_cmd.
  727. */
  728. switch (cmd->iscsi_opcode) {
  729. case ISCSI_OP_SCSI_CMD:
  730. case ISCSI_OP_SCSI_TMFUNC:
  731. transport_generic_free_cmd(&cmd->se_cmd, 1);
  732. break;
  733. default:
  734. iscsit_release_cmd(cmd);
  735. break;
  736. }
  737. }
  738. int iscsit_check_session_usage_count(struct iscsi_session *sess)
  739. {
  740. spin_lock_bh(&sess->session_usage_lock);
  741. if (sess->session_usage_count != 0) {
  742. sess->session_waiting_on_uc = 1;
  743. spin_unlock_bh(&sess->session_usage_lock);
  744. if (in_interrupt())
  745. return 2;
  746. wait_for_completion(&sess->session_waiting_on_uc_comp);
  747. return 1;
  748. }
  749. spin_unlock_bh(&sess->session_usage_lock);
  750. return 0;
  751. }
  752. void iscsit_dec_session_usage_count(struct iscsi_session *sess)
  753. {
  754. spin_lock_bh(&sess->session_usage_lock);
  755. sess->session_usage_count--;
  756. if (!sess->session_usage_count && sess->session_waiting_on_uc)
  757. complete(&sess->session_waiting_on_uc_comp);
  758. spin_unlock_bh(&sess->session_usage_lock);
  759. }
  760. void iscsit_inc_session_usage_count(struct iscsi_session *sess)
  761. {
  762. spin_lock_bh(&sess->session_usage_lock);
  763. sess->session_usage_count++;
  764. spin_unlock_bh(&sess->session_usage_lock);
  765. }
  766. /*
  767. * Setup conn->if_marker and conn->of_marker values based upon
  768. * the initial marker-less interval. (see iSCSI v19 A.2)
  769. */
  770. int iscsit_set_sync_and_steering_values(struct iscsi_conn *conn)
  771. {
  772. int login_ifmarker_count = 0, login_ofmarker_count = 0, next_marker = 0;
  773. /*
  774. * IFMarkInt and OFMarkInt are negotiated as 32-bit words.
  775. */
  776. u32 IFMarkInt = (conn->conn_ops->IFMarkInt * 4);
  777. u32 OFMarkInt = (conn->conn_ops->OFMarkInt * 4);
  778. if (conn->conn_ops->OFMarker) {
  779. /*
  780. * Account for the first Login Command received not
  781. * via iscsi_recv_msg().
  782. */
  783. conn->of_marker += ISCSI_HDR_LEN;
  784. if (conn->of_marker <= OFMarkInt) {
  785. conn->of_marker = (OFMarkInt - conn->of_marker);
  786. } else {
  787. login_ofmarker_count = (conn->of_marker / OFMarkInt);
  788. next_marker = (OFMarkInt * (login_ofmarker_count + 1)) +
  789. (login_ofmarker_count * MARKER_SIZE);
  790. conn->of_marker = (next_marker - conn->of_marker);
  791. }
  792. conn->of_marker_offset = 0;
  793. pr_debug("Setting OFMarker value to %u based on Initial"
  794. " Markerless Interval.\n", conn->of_marker);
  795. }
  796. if (conn->conn_ops->IFMarker) {
  797. if (conn->if_marker <= IFMarkInt) {
  798. conn->if_marker = (IFMarkInt - conn->if_marker);
  799. } else {
  800. login_ifmarker_count = (conn->if_marker / IFMarkInt);
  801. next_marker = (IFMarkInt * (login_ifmarker_count + 1)) +
  802. (login_ifmarker_count * MARKER_SIZE);
  803. conn->if_marker = (next_marker - conn->if_marker);
  804. }
  805. pr_debug("Setting IFMarker value to %u based on Initial"
  806. " Markerless Interval.\n", conn->if_marker);
  807. }
  808. return 0;
  809. }
  810. struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
  811. {
  812. struct iscsi_conn *conn;
  813. spin_lock_bh(&sess->conn_lock);
  814. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  815. if ((conn->cid == cid) &&
  816. (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
  817. iscsit_inc_conn_usage_count(conn);
  818. spin_unlock_bh(&sess->conn_lock);
  819. return conn;
  820. }
  821. }
  822. spin_unlock_bh(&sess->conn_lock);
  823. return NULL;
  824. }
  825. struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
  826. {
  827. struct iscsi_conn *conn;
  828. spin_lock_bh(&sess->conn_lock);
  829. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  830. if (conn->cid == cid) {
  831. iscsit_inc_conn_usage_count(conn);
  832. spin_lock(&conn->state_lock);
  833. atomic_set(&conn->connection_wait_rcfr, 1);
  834. spin_unlock(&conn->state_lock);
  835. spin_unlock_bh(&sess->conn_lock);
  836. return conn;
  837. }
  838. }
  839. spin_unlock_bh(&sess->conn_lock);
  840. return NULL;
  841. }
  842. void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
  843. {
  844. spin_lock_bh(&conn->conn_usage_lock);
  845. if (conn->conn_usage_count != 0) {
  846. conn->conn_waiting_on_uc = 1;
  847. spin_unlock_bh(&conn->conn_usage_lock);
  848. wait_for_completion(&conn->conn_waiting_on_uc_comp);
  849. return;
  850. }
  851. spin_unlock_bh(&conn->conn_usage_lock);
  852. }
  853. void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
  854. {
  855. spin_lock_bh(&conn->conn_usage_lock);
  856. conn->conn_usage_count--;
  857. if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
  858. complete(&conn->conn_waiting_on_uc_comp);
  859. spin_unlock_bh(&conn->conn_usage_lock);
  860. }
  861. void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
  862. {
  863. spin_lock_bh(&conn->conn_usage_lock);
  864. conn->conn_usage_count++;
  865. spin_unlock_bh(&conn->conn_usage_lock);
  866. }
  867. static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
  868. {
  869. u8 state;
  870. struct iscsi_cmd *cmd;
  871. cmd = iscsit_allocate_cmd(conn, GFP_ATOMIC);
  872. if (!cmd)
  873. return -1;
  874. cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
  875. state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
  876. ISTATE_SEND_NOPIN_NO_RESPONSE;
  877. cmd->init_task_tag = 0xFFFFFFFF;
  878. spin_lock_bh(&conn->sess->ttt_lock);
  879. cmd->targ_xfer_tag = (want_response) ? conn->sess->targ_xfer_tag++ :
  880. 0xFFFFFFFF;
  881. if (want_response && (cmd->targ_xfer_tag == 0xFFFFFFFF))
  882. cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
  883. spin_unlock_bh(&conn->sess->ttt_lock);
  884. spin_lock_bh(&conn->cmd_lock);
  885. list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
  886. spin_unlock_bh(&conn->cmd_lock);
  887. if (want_response)
  888. iscsit_start_nopin_response_timer(conn);
  889. iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
  890. return 0;
  891. }
  892. static void iscsit_handle_nopin_response_timeout(unsigned long data)
  893. {
  894. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  895. iscsit_inc_conn_usage_count(conn);
  896. spin_lock_bh(&conn->nopin_timer_lock);
  897. if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
  898. spin_unlock_bh(&conn->nopin_timer_lock);
  899. iscsit_dec_conn_usage_count(conn);
  900. return;
  901. }
  902. pr_debug("Did not receive response to NOPIN on CID: %hu on"
  903. " SID: %u, failing connection.\n", conn->cid,
  904. conn->sess->sid);
  905. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  906. spin_unlock_bh(&conn->nopin_timer_lock);
  907. {
  908. struct iscsi_portal_group *tpg = conn->sess->tpg;
  909. struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
  910. if (tiqn) {
  911. spin_lock_bh(&tiqn->sess_err_stats.lock);
  912. strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
  913. (void *)conn->sess->sess_ops->InitiatorName);
  914. tiqn->sess_err_stats.last_sess_failure_type =
  915. ISCSI_SESS_ERR_CXN_TIMEOUT;
  916. tiqn->sess_err_stats.cxn_timeout_errors++;
  917. conn->sess->conn_timeout_errors++;
  918. spin_unlock_bh(&tiqn->sess_err_stats.lock);
  919. }
  920. }
  921. iscsit_cause_connection_reinstatement(conn, 0);
  922. iscsit_dec_conn_usage_count(conn);
  923. }
  924. void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
  925. {
  926. struct iscsi_session *sess = conn->sess;
  927. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  928. spin_lock_bh(&conn->nopin_timer_lock);
  929. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  930. spin_unlock_bh(&conn->nopin_timer_lock);
  931. return;
  932. }
  933. mod_timer(&conn->nopin_response_timer,
  934. (get_jiffies_64() + na->nopin_response_timeout * HZ));
  935. spin_unlock_bh(&conn->nopin_timer_lock);
  936. }
  937. /*
  938. * Called with conn->nopin_timer_lock held.
  939. */
  940. void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
  941. {
  942. struct iscsi_session *sess = conn->sess;
  943. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  944. spin_lock_bh(&conn->nopin_timer_lock);
  945. if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
  946. spin_unlock_bh(&conn->nopin_timer_lock);
  947. return;
  948. }
  949. init_timer(&conn->nopin_response_timer);
  950. conn->nopin_response_timer.expires =
  951. (get_jiffies_64() + na->nopin_response_timeout * HZ);
  952. conn->nopin_response_timer.data = (unsigned long)conn;
  953. conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
  954. conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
  955. conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
  956. add_timer(&conn->nopin_response_timer);
  957. pr_debug("Started NOPIN Response Timer on CID: %d to %u"
  958. " seconds\n", conn->cid, na->nopin_response_timeout);
  959. spin_unlock_bh(&conn->nopin_timer_lock);
  960. }
  961. void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
  962. {
  963. spin_lock_bh(&conn->nopin_timer_lock);
  964. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  965. spin_unlock_bh(&conn->nopin_timer_lock);
  966. return;
  967. }
  968. conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
  969. spin_unlock_bh(&conn->nopin_timer_lock);
  970. del_timer_sync(&conn->nopin_response_timer);
  971. spin_lock_bh(&conn->nopin_timer_lock);
  972. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  973. spin_unlock_bh(&conn->nopin_timer_lock);
  974. }
  975. static void iscsit_handle_nopin_timeout(unsigned long data)
  976. {
  977. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  978. iscsit_inc_conn_usage_count(conn);
  979. spin_lock_bh(&conn->nopin_timer_lock);
  980. if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
  981. spin_unlock_bh(&conn->nopin_timer_lock);
  982. iscsit_dec_conn_usage_count(conn);
  983. return;
  984. }
  985. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  986. spin_unlock_bh(&conn->nopin_timer_lock);
  987. iscsit_add_nopin(conn, 1);
  988. iscsit_dec_conn_usage_count(conn);
  989. }
  990. /*
  991. * Called with conn->nopin_timer_lock held.
  992. */
  993. void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
  994. {
  995. struct iscsi_session *sess = conn->sess;
  996. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  997. /*
  998. * NOPIN timeout is disabled.
  999. */
  1000. if (!na->nopin_timeout)
  1001. return;
  1002. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
  1003. return;
  1004. init_timer(&conn->nopin_timer);
  1005. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  1006. conn->nopin_timer.data = (unsigned long)conn;
  1007. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  1008. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  1009. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  1010. add_timer(&conn->nopin_timer);
  1011. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  1012. " interval\n", conn->cid, na->nopin_timeout);
  1013. }
  1014. void iscsit_start_nopin_timer(struct iscsi_conn *conn)
  1015. {
  1016. struct iscsi_session *sess = conn->sess;
  1017. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1018. /*
  1019. * NOPIN timeout is disabled..
  1020. */
  1021. if (!na->nopin_timeout)
  1022. return;
  1023. spin_lock_bh(&conn->nopin_timer_lock);
  1024. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
  1025. spin_unlock_bh(&conn->nopin_timer_lock);
  1026. return;
  1027. }
  1028. init_timer(&conn->nopin_timer);
  1029. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  1030. conn->nopin_timer.data = (unsigned long)conn;
  1031. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  1032. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  1033. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  1034. add_timer(&conn->nopin_timer);
  1035. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  1036. " interval\n", conn->cid, na->nopin_timeout);
  1037. spin_unlock_bh(&conn->nopin_timer_lock);
  1038. }
  1039. void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
  1040. {
  1041. spin_lock_bh(&conn->nopin_timer_lock);
  1042. if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
  1043. spin_unlock_bh(&conn->nopin_timer_lock);
  1044. return;
  1045. }
  1046. conn->nopin_timer_flags |= ISCSI_TF_STOP;
  1047. spin_unlock_bh(&conn->nopin_timer_lock);
  1048. del_timer_sync(&conn->nopin_timer);
  1049. spin_lock_bh(&conn->nopin_timer_lock);
  1050. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  1051. spin_unlock_bh(&conn->nopin_timer_lock);
  1052. }
  1053. int iscsit_send_tx_data(
  1054. struct iscsi_cmd *cmd,
  1055. struct iscsi_conn *conn,
  1056. int use_misc)
  1057. {
  1058. int tx_sent, tx_size;
  1059. u32 iov_count;
  1060. struct kvec *iov;
  1061. send_data:
  1062. tx_size = cmd->tx_size;
  1063. if (!use_misc) {
  1064. iov = &cmd->iov_data[0];
  1065. iov_count = cmd->iov_data_count;
  1066. } else {
  1067. iov = &cmd->iov_misc[0];
  1068. iov_count = cmd->iov_misc_count;
  1069. }
  1070. tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
  1071. if (tx_size != tx_sent) {
  1072. if (tx_sent == -EAGAIN) {
  1073. pr_err("tx_data() returned -EAGAIN\n");
  1074. goto send_data;
  1075. } else
  1076. return -1;
  1077. }
  1078. cmd->tx_size = 0;
  1079. return 0;
  1080. }
  1081. int iscsit_fe_sendpage_sg(
  1082. struct iscsi_cmd *cmd,
  1083. struct iscsi_conn *conn)
  1084. {
  1085. struct scatterlist *sg = cmd->first_data_sg;
  1086. struct kvec iov;
  1087. u32 tx_hdr_size, data_len;
  1088. u32 offset = cmd->first_data_sg_off;
  1089. int tx_sent, iov_off;
  1090. send_hdr:
  1091. tx_hdr_size = ISCSI_HDR_LEN;
  1092. if (conn->conn_ops->HeaderDigest)
  1093. tx_hdr_size += ISCSI_CRC_LEN;
  1094. iov.iov_base = cmd->pdu;
  1095. iov.iov_len = tx_hdr_size;
  1096. tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
  1097. if (tx_hdr_size != tx_sent) {
  1098. if (tx_sent == -EAGAIN) {
  1099. pr_err("tx_data() returned -EAGAIN\n");
  1100. goto send_hdr;
  1101. }
  1102. return -1;
  1103. }
  1104. data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
  1105. /*
  1106. * Set iov_off used by padding and data digest tx_data() calls below
  1107. * in order to determine proper offset into cmd->iov_data[]
  1108. */
  1109. if (conn->conn_ops->DataDigest) {
  1110. data_len -= ISCSI_CRC_LEN;
  1111. if (cmd->padding)
  1112. iov_off = (cmd->iov_data_count - 2);
  1113. else
  1114. iov_off = (cmd->iov_data_count - 1);
  1115. } else {
  1116. iov_off = (cmd->iov_data_count - 1);
  1117. }
  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 = &cmd->iov_data[iov_off++];
  1144. tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
  1145. if (cmd->padding != tx_sent) {
  1146. if (tx_sent == -EAGAIN) {
  1147. pr_err("tx_data() returned -EAGAIN\n");
  1148. goto send_padding;
  1149. }
  1150. return -1;
  1151. }
  1152. }
  1153. send_datacrc:
  1154. if (conn->conn_ops->DataDigest) {
  1155. struct kvec *iov_d = &cmd->iov_data[iov_off];
  1156. tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
  1157. if (ISCSI_CRC_LEN != tx_sent) {
  1158. if (tx_sent == -EAGAIN) {
  1159. pr_err("tx_data() returned -EAGAIN\n");
  1160. goto send_datacrc;
  1161. }
  1162. return -1;
  1163. }
  1164. }
  1165. return 0;
  1166. }
  1167. /*
  1168. * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
  1169. * back to the Initiator when an expection condition occurs with the
  1170. * errors set in status_class and status_detail.
  1171. *
  1172. * Parameters: iSCSI Connection, Status Class, Status Detail.
  1173. * Returns: 0 on success, -1 on error.
  1174. */
  1175. int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
  1176. {
  1177. u8 iscsi_hdr[ISCSI_HDR_LEN];
  1178. int err;
  1179. struct kvec iov;
  1180. struct iscsi_login_rsp *hdr;
  1181. iscsit_collect_login_stats(conn, status_class, status_detail);
  1182. memset(&iov, 0, sizeof(struct kvec));
  1183. memset(&iscsi_hdr, 0x0, ISCSI_HDR_LEN);
  1184. hdr = (struct iscsi_login_rsp *)&iscsi_hdr;
  1185. hdr->opcode = ISCSI_OP_LOGIN_RSP;
  1186. hdr->status_class = status_class;
  1187. hdr->status_detail = status_detail;
  1188. hdr->itt = cpu_to_be32(conn->login_itt);
  1189. iov.iov_base = &iscsi_hdr;
  1190. iov.iov_len = ISCSI_HDR_LEN;
  1191. PRINT_BUFF(iscsi_hdr, ISCSI_HDR_LEN);
  1192. err = tx_data(conn, &iov, 1, ISCSI_HDR_LEN);
  1193. if (err != ISCSI_HDR_LEN) {
  1194. pr_err("tx_data returned less than expected\n");
  1195. return -1;
  1196. }
  1197. return 0;
  1198. }
  1199. void iscsit_print_session_params(struct iscsi_session *sess)
  1200. {
  1201. struct iscsi_conn *conn;
  1202. pr_debug("-----------------------------[Session Params for"
  1203. " SID: %u]-----------------------------\n", sess->sid);
  1204. spin_lock_bh(&sess->conn_lock);
  1205. list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
  1206. iscsi_dump_conn_ops(conn->conn_ops);
  1207. spin_unlock_bh(&sess->conn_lock);
  1208. iscsi_dump_sess_ops(sess->sess_ops);
  1209. }
  1210. static int iscsit_do_rx_data(
  1211. struct iscsi_conn *conn,
  1212. struct iscsi_data_count *count)
  1213. {
  1214. int data = count->data_length, rx_loop = 0, total_rx = 0, iov_len;
  1215. struct kvec *iov_p;
  1216. struct msghdr msg;
  1217. if (!conn || !conn->sock || !conn->conn_ops)
  1218. return -1;
  1219. memset(&msg, 0, sizeof(struct msghdr));
  1220. iov_p = count->iov;
  1221. iov_len = count->iov_count;
  1222. while (total_rx < data) {
  1223. rx_loop = kernel_recvmsg(conn->sock, &msg, iov_p, iov_len,
  1224. (data - total_rx), MSG_WAITALL);
  1225. if (rx_loop <= 0) {
  1226. pr_debug("rx_loop: %d total_rx: %d\n",
  1227. rx_loop, total_rx);
  1228. return rx_loop;
  1229. }
  1230. total_rx += rx_loop;
  1231. pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
  1232. rx_loop, total_rx, data);
  1233. }
  1234. return total_rx;
  1235. }
  1236. static int iscsit_do_tx_data(
  1237. struct iscsi_conn *conn,
  1238. struct iscsi_data_count *count)
  1239. {
  1240. int data = count->data_length, total_tx = 0, tx_loop = 0, iov_len;
  1241. struct kvec *iov_p;
  1242. struct msghdr msg;
  1243. if (!conn || !conn->sock || !conn->conn_ops)
  1244. return -1;
  1245. if (data <= 0) {
  1246. pr_err("Data length is: %d\n", data);
  1247. return -1;
  1248. }
  1249. memset(&msg, 0, sizeof(struct msghdr));
  1250. iov_p = count->iov;
  1251. iov_len = count->iov_count;
  1252. while (total_tx < data) {
  1253. tx_loop = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
  1254. (data - total_tx));
  1255. if (tx_loop <= 0) {
  1256. pr_debug("tx_loop: %d total_tx %d\n",
  1257. tx_loop, total_tx);
  1258. return tx_loop;
  1259. }
  1260. total_tx += tx_loop;
  1261. pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
  1262. tx_loop, total_tx, data);
  1263. }
  1264. return total_tx;
  1265. }
  1266. int rx_data(
  1267. struct iscsi_conn *conn,
  1268. struct kvec *iov,
  1269. int iov_count,
  1270. int data)
  1271. {
  1272. struct iscsi_data_count c;
  1273. if (!conn || !conn->sock || !conn->conn_ops)
  1274. return -1;
  1275. memset(&c, 0, sizeof(struct iscsi_data_count));
  1276. c.iov = iov;
  1277. c.iov_count = iov_count;
  1278. c.data_length = data;
  1279. c.type = ISCSI_RX_DATA;
  1280. return iscsit_do_rx_data(conn, &c);
  1281. }
  1282. int tx_data(
  1283. struct iscsi_conn *conn,
  1284. struct kvec *iov,
  1285. int iov_count,
  1286. int data)
  1287. {
  1288. struct iscsi_data_count c;
  1289. if (!conn || !conn->sock || !conn->conn_ops)
  1290. return -1;
  1291. memset(&c, 0, sizeof(struct iscsi_data_count));
  1292. c.iov = iov;
  1293. c.iov_count = iov_count;
  1294. c.data_length = data;
  1295. c.type = ISCSI_TX_DATA;
  1296. return iscsit_do_tx_data(conn, &c);
  1297. }
  1298. void iscsit_collect_login_stats(
  1299. struct iscsi_conn *conn,
  1300. u8 status_class,
  1301. u8 status_detail)
  1302. {
  1303. struct iscsi_param *intrname = NULL;
  1304. struct iscsi_tiqn *tiqn;
  1305. struct iscsi_login_stats *ls;
  1306. tiqn = iscsit_snmp_get_tiqn(conn);
  1307. if (!tiqn)
  1308. return;
  1309. ls = &tiqn->login_stats;
  1310. spin_lock(&ls->lock);
  1311. if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
  1312. ((get_jiffies_64() - ls->last_fail_time) < 10)) {
  1313. /* We already have the failure info for this login */
  1314. spin_unlock(&ls->lock);
  1315. return;
  1316. }
  1317. if (status_class == ISCSI_STATUS_CLS_SUCCESS)
  1318. ls->accepts++;
  1319. else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
  1320. ls->redirects++;
  1321. ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
  1322. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1323. (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
  1324. ls->authenticate_fails++;
  1325. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
  1326. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1327. (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
  1328. ls->authorize_fails++;
  1329. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
  1330. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1331. (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
  1332. ls->negotiate_fails++;
  1333. ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
  1334. } else {
  1335. ls->other_fails++;
  1336. ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
  1337. }
  1338. /* Save initiator name, ip address and time, if it is a failed login */
  1339. if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
  1340. if (conn->param_list)
  1341. intrname = iscsi_find_param_from_key(INITIATORNAME,
  1342. conn->param_list);
  1343. strcpy(ls->last_intr_fail_name,
  1344. (intrname ? intrname->value : "Unknown"));
  1345. ls->last_intr_fail_ip_family = conn->sock->sk->sk_family;
  1346. snprintf(ls->last_intr_fail_ip_addr, IPV6_ADDRESS_SPACE,
  1347. "%s", conn->login_ip);
  1348. ls->last_fail_time = get_jiffies_64();
  1349. }
  1350. spin_unlock(&ls->lock);
  1351. }
  1352. struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
  1353. {
  1354. struct iscsi_portal_group *tpg;
  1355. if (!conn || !conn->sess)
  1356. return NULL;
  1357. tpg = conn->sess->tpg;
  1358. if (!tpg)
  1359. return NULL;
  1360. if (!tpg->tpg_tiqn)
  1361. return NULL;
  1362. return tpg->tpg_tiqn;
  1363. }