iscsi_target_util.c 40 KB

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