iscsi_target_util.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492
  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. /*
  134. * May be called from software interrupt (timer) context for allocating
  135. * iSCSI NopINs.
  136. */
  137. struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp_mask)
  138. {
  139. struct iscsi_cmd *cmd;
  140. int priv_size = conn->conn_transport->priv_size;
  141. cmd = kzalloc(sizeof(struct iscsi_cmd) + priv_size, gfp_mask);
  142. if (!cmd) {
  143. pr_err("Unable to allocate memory for struct iscsi_cmd.\n");
  144. return NULL;
  145. }
  146. cmd->conn = conn;
  147. INIT_LIST_HEAD(&cmd->i_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. kfree(cmd->buf_ptr);
  583. kfree(cmd->pdu_list);
  584. kfree(cmd->seq_list);
  585. kfree(cmd->tmr_req);
  586. kfree(cmd->iov_data);
  587. kfree(cmd->text_in_ptr);
  588. kfree(cmd);
  589. }
  590. EXPORT_SYMBOL(iscsit_release_cmd);
  591. static void __iscsit_free_cmd(struct iscsi_cmd *cmd, bool scsi_cmd,
  592. bool check_queues)
  593. {
  594. struct iscsi_conn *conn = cmd->conn;
  595. if (scsi_cmd) {
  596. if (cmd->data_direction == DMA_TO_DEVICE) {
  597. iscsit_stop_dataout_timer(cmd);
  598. iscsit_free_r2ts_from_list(cmd);
  599. }
  600. if (cmd->data_direction == DMA_FROM_DEVICE)
  601. iscsit_free_all_datain_reqs(cmd);
  602. }
  603. if (conn && check_queues) {
  604. iscsit_remove_cmd_from_immediate_queue(cmd, conn);
  605. iscsit_remove_cmd_from_response_queue(cmd, conn);
  606. }
  607. }
  608. void iscsit_free_cmd(struct iscsi_cmd *cmd, bool shutdown)
  609. {
  610. struct se_cmd *se_cmd = NULL;
  611. int rc;
  612. /*
  613. * Determine if a struct se_cmd is associated with
  614. * this struct iscsi_cmd.
  615. */
  616. switch (cmd->iscsi_opcode) {
  617. case ISCSI_OP_SCSI_CMD:
  618. se_cmd = &cmd->se_cmd;
  619. __iscsit_free_cmd(cmd, true, shutdown);
  620. /*
  621. * Fallthrough
  622. */
  623. case ISCSI_OP_SCSI_TMFUNC:
  624. rc = transport_generic_free_cmd(&cmd->se_cmd, 1);
  625. if (!rc && shutdown && se_cmd && se_cmd->se_sess) {
  626. __iscsit_free_cmd(cmd, true, shutdown);
  627. target_put_sess_cmd(se_cmd->se_sess, se_cmd);
  628. }
  629. break;
  630. case ISCSI_OP_REJECT:
  631. /*
  632. * Handle special case for REJECT when iscsi_add_reject*() has
  633. * overwritten the original iscsi_opcode assignment, and the
  634. * associated cmd->se_cmd needs to be released.
  635. */
  636. if (cmd->se_cmd.se_tfo != NULL) {
  637. se_cmd = &cmd->se_cmd;
  638. __iscsit_free_cmd(cmd, true, shutdown);
  639. rc = transport_generic_free_cmd(&cmd->se_cmd, 1);
  640. if (!rc && shutdown && se_cmd->se_sess) {
  641. __iscsit_free_cmd(cmd, true, shutdown);
  642. target_put_sess_cmd(se_cmd->se_sess, se_cmd);
  643. }
  644. break;
  645. }
  646. /* Fall-through */
  647. default:
  648. __iscsit_free_cmd(cmd, false, shutdown);
  649. iscsit_release_cmd(cmd);
  650. break;
  651. }
  652. }
  653. int iscsit_check_session_usage_count(struct iscsi_session *sess)
  654. {
  655. spin_lock_bh(&sess->session_usage_lock);
  656. if (sess->session_usage_count != 0) {
  657. sess->session_waiting_on_uc = 1;
  658. spin_unlock_bh(&sess->session_usage_lock);
  659. if (in_interrupt())
  660. return 2;
  661. wait_for_completion(&sess->session_waiting_on_uc_comp);
  662. return 1;
  663. }
  664. spin_unlock_bh(&sess->session_usage_lock);
  665. return 0;
  666. }
  667. void iscsit_dec_session_usage_count(struct iscsi_session *sess)
  668. {
  669. spin_lock_bh(&sess->session_usage_lock);
  670. sess->session_usage_count--;
  671. if (!sess->session_usage_count && sess->session_waiting_on_uc)
  672. complete(&sess->session_waiting_on_uc_comp);
  673. spin_unlock_bh(&sess->session_usage_lock);
  674. }
  675. void iscsit_inc_session_usage_count(struct iscsi_session *sess)
  676. {
  677. spin_lock_bh(&sess->session_usage_lock);
  678. sess->session_usage_count++;
  679. spin_unlock_bh(&sess->session_usage_lock);
  680. }
  681. /*
  682. * Setup conn->if_marker and conn->of_marker values based upon
  683. * the initial marker-less interval. (see iSCSI v19 A.2)
  684. */
  685. int iscsit_set_sync_and_steering_values(struct iscsi_conn *conn)
  686. {
  687. int login_ifmarker_count = 0, login_ofmarker_count = 0, next_marker = 0;
  688. /*
  689. * IFMarkInt and OFMarkInt are negotiated as 32-bit words.
  690. */
  691. u32 IFMarkInt = (conn->conn_ops->IFMarkInt * 4);
  692. u32 OFMarkInt = (conn->conn_ops->OFMarkInt * 4);
  693. if (conn->conn_ops->OFMarker) {
  694. /*
  695. * Account for the first Login Command received not
  696. * via iscsi_recv_msg().
  697. */
  698. conn->of_marker += ISCSI_HDR_LEN;
  699. if (conn->of_marker <= OFMarkInt) {
  700. conn->of_marker = (OFMarkInt - conn->of_marker);
  701. } else {
  702. login_ofmarker_count = (conn->of_marker / OFMarkInt);
  703. next_marker = (OFMarkInt * (login_ofmarker_count + 1)) +
  704. (login_ofmarker_count * MARKER_SIZE);
  705. conn->of_marker = (next_marker - conn->of_marker);
  706. }
  707. conn->of_marker_offset = 0;
  708. pr_debug("Setting OFMarker value to %u based on Initial"
  709. " Markerless Interval.\n", conn->of_marker);
  710. }
  711. if (conn->conn_ops->IFMarker) {
  712. if (conn->if_marker <= IFMarkInt) {
  713. conn->if_marker = (IFMarkInt - conn->if_marker);
  714. } else {
  715. login_ifmarker_count = (conn->if_marker / IFMarkInt);
  716. next_marker = (IFMarkInt * (login_ifmarker_count + 1)) +
  717. (login_ifmarker_count * MARKER_SIZE);
  718. conn->if_marker = (next_marker - conn->if_marker);
  719. }
  720. pr_debug("Setting IFMarker value to %u based on Initial"
  721. " Markerless Interval.\n", conn->if_marker);
  722. }
  723. return 0;
  724. }
  725. struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
  726. {
  727. struct iscsi_conn *conn;
  728. spin_lock_bh(&sess->conn_lock);
  729. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  730. if ((conn->cid == cid) &&
  731. (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
  732. iscsit_inc_conn_usage_count(conn);
  733. spin_unlock_bh(&sess->conn_lock);
  734. return conn;
  735. }
  736. }
  737. spin_unlock_bh(&sess->conn_lock);
  738. return NULL;
  739. }
  740. struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
  741. {
  742. struct iscsi_conn *conn;
  743. spin_lock_bh(&sess->conn_lock);
  744. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  745. if (conn->cid == cid) {
  746. iscsit_inc_conn_usage_count(conn);
  747. spin_lock(&conn->state_lock);
  748. atomic_set(&conn->connection_wait_rcfr, 1);
  749. spin_unlock(&conn->state_lock);
  750. spin_unlock_bh(&sess->conn_lock);
  751. return conn;
  752. }
  753. }
  754. spin_unlock_bh(&sess->conn_lock);
  755. return NULL;
  756. }
  757. void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
  758. {
  759. spin_lock_bh(&conn->conn_usage_lock);
  760. if (conn->conn_usage_count != 0) {
  761. conn->conn_waiting_on_uc = 1;
  762. spin_unlock_bh(&conn->conn_usage_lock);
  763. wait_for_completion(&conn->conn_waiting_on_uc_comp);
  764. return;
  765. }
  766. spin_unlock_bh(&conn->conn_usage_lock);
  767. }
  768. void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
  769. {
  770. spin_lock_bh(&conn->conn_usage_lock);
  771. conn->conn_usage_count--;
  772. if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
  773. complete(&conn->conn_waiting_on_uc_comp);
  774. spin_unlock_bh(&conn->conn_usage_lock);
  775. }
  776. void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
  777. {
  778. spin_lock_bh(&conn->conn_usage_lock);
  779. conn->conn_usage_count++;
  780. spin_unlock_bh(&conn->conn_usage_lock);
  781. }
  782. static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
  783. {
  784. u8 state;
  785. struct iscsi_cmd *cmd;
  786. cmd = iscsit_allocate_cmd(conn, GFP_ATOMIC);
  787. if (!cmd)
  788. return -1;
  789. cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
  790. state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
  791. ISTATE_SEND_NOPIN_NO_RESPONSE;
  792. cmd->init_task_tag = RESERVED_ITT;
  793. spin_lock_bh(&conn->sess->ttt_lock);
  794. cmd->targ_xfer_tag = (want_response) ? conn->sess->targ_xfer_tag++ :
  795. 0xFFFFFFFF;
  796. if (want_response && (cmd->targ_xfer_tag == 0xFFFFFFFF))
  797. cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
  798. spin_unlock_bh(&conn->sess->ttt_lock);
  799. spin_lock_bh(&conn->cmd_lock);
  800. list_add_tail(&cmd->i_conn_node, &conn->conn_cmd_list);
  801. spin_unlock_bh(&conn->cmd_lock);
  802. if (want_response)
  803. iscsit_start_nopin_response_timer(conn);
  804. iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
  805. return 0;
  806. }
  807. static void iscsit_handle_nopin_response_timeout(unsigned long data)
  808. {
  809. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  810. iscsit_inc_conn_usage_count(conn);
  811. spin_lock_bh(&conn->nopin_timer_lock);
  812. if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
  813. spin_unlock_bh(&conn->nopin_timer_lock);
  814. iscsit_dec_conn_usage_count(conn);
  815. return;
  816. }
  817. pr_debug("Did not receive response to NOPIN on CID: %hu on"
  818. " SID: %u, failing connection.\n", conn->cid,
  819. conn->sess->sid);
  820. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  821. spin_unlock_bh(&conn->nopin_timer_lock);
  822. {
  823. struct iscsi_portal_group *tpg = conn->sess->tpg;
  824. struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
  825. if (tiqn) {
  826. spin_lock_bh(&tiqn->sess_err_stats.lock);
  827. strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
  828. conn->sess->sess_ops->InitiatorName);
  829. tiqn->sess_err_stats.last_sess_failure_type =
  830. ISCSI_SESS_ERR_CXN_TIMEOUT;
  831. tiqn->sess_err_stats.cxn_timeout_errors++;
  832. conn->sess->conn_timeout_errors++;
  833. spin_unlock_bh(&tiqn->sess_err_stats.lock);
  834. }
  835. }
  836. iscsit_cause_connection_reinstatement(conn, 0);
  837. iscsit_dec_conn_usage_count(conn);
  838. }
  839. void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
  840. {
  841. struct iscsi_session *sess = conn->sess;
  842. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  843. spin_lock_bh(&conn->nopin_timer_lock);
  844. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  845. spin_unlock_bh(&conn->nopin_timer_lock);
  846. return;
  847. }
  848. mod_timer(&conn->nopin_response_timer,
  849. (get_jiffies_64() + na->nopin_response_timeout * HZ));
  850. spin_unlock_bh(&conn->nopin_timer_lock);
  851. }
  852. /*
  853. * Called with conn->nopin_timer_lock held.
  854. */
  855. void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
  856. {
  857. struct iscsi_session *sess = conn->sess;
  858. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  859. spin_lock_bh(&conn->nopin_timer_lock);
  860. if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
  861. spin_unlock_bh(&conn->nopin_timer_lock);
  862. return;
  863. }
  864. init_timer(&conn->nopin_response_timer);
  865. conn->nopin_response_timer.expires =
  866. (get_jiffies_64() + na->nopin_response_timeout * HZ);
  867. conn->nopin_response_timer.data = (unsigned long)conn;
  868. conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
  869. conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
  870. conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
  871. add_timer(&conn->nopin_response_timer);
  872. pr_debug("Started NOPIN Response Timer on CID: %d to %u"
  873. " seconds\n", conn->cid, na->nopin_response_timeout);
  874. spin_unlock_bh(&conn->nopin_timer_lock);
  875. }
  876. void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
  877. {
  878. spin_lock_bh(&conn->nopin_timer_lock);
  879. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  880. spin_unlock_bh(&conn->nopin_timer_lock);
  881. return;
  882. }
  883. conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
  884. spin_unlock_bh(&conn->nopin_timer_lock);
  885. del_timer_sync(&conn->nopin_response_timer);
  886. spin_lock_bh(&conn->nopin_timer_lock);
  887. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  888. spin_unlock_bh(&conn->nopin_timer_lock);
  889. }
  890. static void iscsit_handle_nopin_timeout(unsigned long data)
  891. {
  892. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  893. iscsit_inc_conn_usage_count(conn);
  894. spin_lock_bh(&conn->nopin_timer_lock);
  895. if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
  896. spin_unlock_bh(&conn->nopin_timer_lock);
  897. iscsit_dec_conn_usage_count(conn);
  898. return;
  899. }
  900. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  901. spin_unlock_bh(&conn->nopin_timer_lock);
  902. iscsit_add_nopin(conn, 1);
  903. iscsit_dec_conn_usage_count(conn);
  904. }
  905. /*
  906. * Called with conn->nopin_timer_lock held.
  907. */
  908. void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
  909. {
  910. struct iscsi_session *sess = conn->sess;
  911. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  912. /*
  913. * NOPIN timeout is disabled.
  914. */
  915. if (!na->nopin_timeout)
  916. return;
  917. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
  918. return;
  919. init_timer(&conn->nopin_timer);
  920. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  921. conn->nopin_timer.data = (unsigned long)conn;
  922. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  923. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  924. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  925. add_timer(&conn->nopin_timer);
  926. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  927. " interval\n", conn->cid, na->nopin_timeout);
  928. }
  929. void iscsit_start_nopin_timer(struct iscsi_conn *conn)
  930. {
  931. struct iscsi_session *sess = conn->sess;
  932. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  933. /*
  934. * NOPIN timeout is disabled..
  935. */
  936. if (!na->nopin_timeout)
  937. return;
  938. spin_lock_bh(&conn->nopin_timer_lock);
  939. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
  940. spin_unlock_bh(&conn->nopin_timer_lock);
  941. return;
  942. }
  943. init_timer(&conn->nopin_timer);
  944. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  945. conn->nopin_timer.data = (unsigned long)conn;
  946. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  947. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  948. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  949. add_timer(&conn->nopin_timer);
  950. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  951. " interval\n", conn->cid, na->nopin_timeout);
  952. spin_unlock_bh(&conn->nopin_timer_lock);
  953. }
  954. void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
  955. {
  956. spin_lock_bh(&conn->nopin_timer_lock);
  957. if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
  958. spin_unlock_bh(&conn->nopin_timer_lock);
  959. return;
  960. }
  961. conn->nopin_timer_flags |= ISCSI_TF_STOP;
  962. spin_unlock_bh(&conn->nopin_timer_lock);
  963. del_timer_sync(&conn->nopin_timer);
  964. spin_lock_bh(&conn->nopin_timer_lock);
  965. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  966. spin_unlock_bh(&conn->nopin_timer_lock);
  967. }
  968. int iscsit_send_tx_data(
  969. struct iscsi_cmd *cmd,
  970. struct iscsi_conn *conn,
  971. int use_misc)
  972. {
  973. int tx_sent, tx_size;
  974. u32 iov_count;
  975. struct kvec *iov;
  976. send_data:
  977. tx_size = cmd->tx_size;
  978. if (!use_misc) {
  979. iov = &cmd->iov_data[0];
  980. iov_count = cmd->iov_data_count;
  981. } else {
  982. iov = &cmd->iov_misc[0];
  983. iov_count = cmd->iov_misc_count;
  984. }
  985. tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
  986. if (tx_size != tx_sent) {
  987. if (tx_sent == -EAGAIN) {
  988. pr_err("tx_data() returned -EAGAIN\n");
  989. goto send_data;
  990. } else
  991. return -1;
  992. }
  993. cmd->tx_size = 0;
  994. return 0;
  995. }
  996. int iscsit_fe_sendpage_sg(
  997. struct iscsi_cmd *cmd,
  998. struct iscsi_conn *conn)
  999. {
  1000. struct scatterlist *sg = cmd->first_data_sg;
  1001. struct kvec iov;
  1002. u32 tx_hdr_size, data_len;
  1003. u32 offset = cmd->first_data_sg_off;
  1004. int tx_sent, iov_off;
  1005. send_hdr:
  1006. tx_hdr_size = ISCSI_HDR_LEN;
  1007. if (conn->conn_ops->HeaderDigest)
  1008. tx_hdr_size += ISCSI_CRC_LEN;
  1009. iov.iov_base = cmd->pdu;
  1010. iov.iov_len = tx_hdr_size;
  1011. tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
  1012. if (tx_hdr_size != tx_sent) {
  1013. if (tx_sent == -EAGAIN) {
  1014. pr_err("tx_data() returned -EAGAIN\n");
  1015. goto send_hdr;
  1016. }
  1017. return -1;
  1018. }
  1019. data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
  1020. /*
  1021. * Set iov_off used by padding and data digest tx_data() calls below
  1022. * in order to determine proper offset into cmd->iov_data[]
  1023. */
  1024. if (conn->conn_ops->DataDigest) {
  1025. data_len -= ISCSI_CRC_LEN;
  1026. if (cmd->padding)
  1027. iov_off = (cmd->iov_data_count - 2);
  1028. else
  1029. iov_off = (cmd->iov_data_count - 1);
  1030. } else {
  1031. iov_off = (cmd->iov_data_count - 1);
  1032. }
  1033. /*
  1034. * Perform sendpage() for each page in the scatterlist
  1035. */
  1036. while (data_len) {
  1037. u32 space = (sg->length - offset);
  1038. u32 sub_len = min_t(u32, data_len, space);
  1039. send_pg:
  1040. tx_sent = conn->sock->ops->sendpage(conn->sock,
  1041. sg_page(sg), sg->offset + offset, sub_len, 0);
  1042. if (tx_sent != sub_len) {
  1043. if (tx_sent == -EAGAIN) {
  1044. pr_err("tcp_sendpage() returned"
  1045. " -EAGAIN\n");
  1046. goto send_pg;
  1047. }
  1048. pr_err("tcp_sendpage() failure: %d\n",
  1049. tx_sent);
  1050. return -1;
  1051. }
  1052. data_len -= sub_len;
  1053. offset = 0;
  1054. sg = sg_next(sg);
  1055. }
  1056. send_padding:
  1057. if (cmd->padding) {
  1058. struct kvec *iov_p = &cmd->iov_data[iov_off++];
  1059. tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
  1060. if (cmd->padding != tx_sent) {
  1061. if (tx_sent == -EAGAIN) {
  1062. pr_err("tx_data() returned -EAGAIN\n");
  1063. goto send_padding;
  1064. }
  1065. return -1;
  1066. }
  1067. }
  1068. send_datacrc:
  1069. if (conn->conn_ops->DataDigest) {
  1070. struct kvec *iov_d = &cmd->iov_data[iov_off];
  1071. tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
  1072. if (ISCSI_CRC_LEN != tx_sent) {
  1073. if (tx_sent == -EAGAIN) {
  1074. pr_err("tx_data() returned -EAGAIN\n");
  1075. goto send_datacrc;
  1076. }
  1077. return -1;
  1078. }
  1079. }
  1080. return 0;
  1081. }
  1082. /*
  1083. * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
  1084. * back to the Initiator when an expection condition occurs with the
  1085. * errors set in status_class and status_detail.
  1086. *
  1087. * Parameters: iSCSI Connection, Status Class, Status Detail.
  1088. * Returns: 0 on success, -1 on error.
  1089. */
  1090. int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
  1091. {
  1092. struct iscsi_login_rsp *hdr;
  1093. struct iscsi_login *login = conn->conn_login;
  1094. login->login_failed = 1;
  1095. iscsit_collect_login_stats(conn, status_class, status_detail);
  1096. hdr = (struct iscsi_login_rsp *)&login->rsp[0];
  1097. hdr->opcode = ISCSI_OP_LOGIN_RSP;
  1098. hdr->status_class = status_class;
  1099. hdr->status_detail = status_detail;
  1100. hdr->itt = conn->login_itt;
  1101. return conn->conn_transport->iscsit_put_login_tx(conn, login, 0);
  1102. }
  1103. void iscsit_print_session_params(struct iscsi_session *sess)
  1104. {
  1105. struct iscsi_conn *conn;
  1106. pr_debug("-----------------------------[Session Params for"
  1107. " SID: %u]-----------------------------\n", sess->sid);
  1108. spin_lock_bh(&sess->conn_lock);
  1109. list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
  1110. iscsi_dump_conn_ops(conn->conn_ops);
  1111. spin_unlock_bh(&sess->conn_lock);
  1112. iscsi_dump_sess_ops(sess->sess_ops);
  1113. }
  1114. static int iscsit_do_rx_data(
  1115. struct iscsi_conn *conn,
  1116. struct iscsi_data_count *count)
  1117. {
  1118. int data = count->data_length, rx_loop = 0, total_rx = 0, iov_len;
  1119. struct kvec *iov_p;
  1120. struct msghdr msg;
  1121. if (!conn || !conn->sock || !conn->conn_ops)
  1122. return -1;
  1123. memset(&msg, 0, sizeof(struct msghdr));
  1124. iov_p = count->iov;
  1125. iov_len = count->iov_count;
  1126. while (total_rx < data) {
  1127. rx_loop = kernel_recvmsg(conn->sock, &msg, iov_p, iov_len,
  1128. (data - total_rx), MSG_WAITALL);
  1129. if (rx_loop <= 0) {
  1130. pr_debug("rx_loop: %d total_rx: %d\n",
  1131. rx_loop, total_rx);
  1132. return rx_loop;
  1133. }
  1134. total_rx += rx_loop;
  1135. pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
  1136. rx_loop, total_rx, data);
  1137. }
  1138. return total_rx;
  1139. }
  1140. static int iscsit_do_tx_data(
  1141. struct iscsi_conn *conn,
  1142. struct iscsi_data_count *count)
  1143. {
  1144. int data = count->data_length, total_tx = 0, tx_loop = 0, iov_len;
  1145. struct kvec *iov_p;
  1146. struct msghdr msg;
  1147. if (!conn || !conn->sock || !conn->conn_ops)
  1148. return -1;
  1149. if (data <= 0) {
  1150. pr_err("Data length is: %d\n", data);
  1151. return -1;
  1152. }
  1153. memset(&msg, 0, sizeof(struct msghdr));
  1154. iov_p = count->iov;
  1155. iov_len = count->iov_count;
  1156. while (total_tx < data) {
  1157. tx_loop = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
  1158. (data - total_tx));
  1159. if (tx_loop <= 0) {
  1160. pr_debug("tx_loop: %d total_tx %d\n",
  1161. tx_loop, total_tx);
  1162. return tx_loop;
  1163. }
  1164. total_tx += tx_loop;
  1165. pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
  1166. tx_loop, total_tx, data);
  1167. }
  1168. return total_tx;
  1169. }
  1170. int rx_data(
  1171. struct iscsi_conn *conn,
  1172. struct kvec *iov,
  1173. int iov_count,
  1174. int data)
  1175. {
  1176. struct iscsi_data_count c;
  1177. if (!conn || !conn->sock || !conn->conn_ops)
  1178. return -1;
  1179. memset(&c, 0, sizeof(struct iscsi_data_count));
  1180. c.iov = iov;
  1181. c.iov_count = iov_count;
  1182. c.data_length = data;
  1183. c.type = ISCSI_RX_DATA;
  1184. return iscsit_do_rx_data(conn, &c);
  1185. }
  1186. int tx_data(
  1187. struct iscsi_conn *conn,
  1188. struct kvec *iov,
  1189. int iov_count,
  1190. int data)
  1191. {
  1192. struct iscsi_data_count c;
  1193. if (!conn || !conn->sock || !conn->conn_ops)
  1194. return -1;
  1195. memset(&c, 0, sizeof(struct iscsi_data_count));
  1196. c.iov = iov;
  1197. c.iov_count = iov_count;
  1198. c.data_length = data;
  1199. c.type = ISCSI_TX_DATA;
  1200. return iscsit_do_tx_data(conn, &c);
  1201. }
  1202. void iscsit_collect_login_stats(
  1203. struct iscsi_conn *conn,
  1204. u8 status_class,
  1205. u8 status_detail)
  1206. {
  1207. struct iscsi_param *intrname = NULL;
  1208. struct iscsi_tiqn *tiqn;
  1209. struct iscsi_login_stats *ls;
  1210. tiqn = iscsit_snmp_get_tiqn(conn);
  1211. if (!tiqn)
  1212. return;
  1213. ls = &tiqn->login_stats;
  1214. spin_lock(&ls->lock);
  1215. if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
  1216. ((get_jiffies_64() - ls->last_fail_time) < 10)) {
  1217. /* We already have the failure info for this login */
  1218. spin_unlock(&ls->lock);
  1219. return;
  1220. }
  1221. if (status_class == ISCSI_STATUS_CLS_SUCCESS)
  1222. ls->accepts++;
  1223. else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
  1224. ls->redirects++;
  1225. ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
  1226. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1227. (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
  1228. ls->authenticate_fails++;
  1229. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
  1230. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1231. (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
  1232. ls->authorize_fails++;
  1233. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
  1234. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1235. (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
  1236. ls->negotiate_fails++;
  1237. ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
  1238. } else {
  1239. ls->other_fails++;
  1240. ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
  1241. }
  1242. /* Save initiator name, ip address and time, if it is a failed login */
  1243. if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
  1244. if (conn->param_list)
  1245. intrname = iscsi_find_param_from_key(INITIATORNAME,
  1246. conn->param_list);
  1247. strcpy(ls->last_intr_fail_name,
  1248. (intrname ? intrname->value : "Unknown"));
  1249. ls->last_intr_fail_ip_family = conn->login_family;
  1250. snprintf(ls->last_intr_fail_ip_addr, IPV6_ADDRESS_SPACE,
  1251. "%s", conn->login_ip);
  1252. ls->last_fail_time = get_jiffies_64();
  1253. }
  1254. spin_unlock(&ls->lock);
  1255. }
  1256. struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
  1257. {
  1258. struct iscsi_portal_group *tpg;
  1259. if (!conn || !conn->sess)
  1260. return NULL;
  1261. tpg = conn->sess->tpg;
  1262. if (!tpg)
  1263. return NULL;
  1264. if (!tpg->tpg_tiqn)
  1265. return NULL;
  1266. return tpg->tpg_tiqn;
  1267. }