iscsi_target_util.c 37 KB

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