iscsi_target_util.c 38 KB

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