iscsi_target_util.c 40 KB

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