iscsi_target_util.c 40 KB

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