iscsi_target_util.c 38 KB

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