iscsi_target_util.c 37 KB

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