iscsi_target_util.c 37 KB

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