iscsi_target_util.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. /*******************************************************************************
  2. * This file contains the iSCSI Target specific utility functions.
  3. *
  4. * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
  5. *
  6. * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
  7. *
  8. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. ******************************************************************************/
  20. #include <linux/list.h>
  21. #include <scsi/scsi_tcq.h>
  22. #include <scsi/iscsi_proto.h>
  23. #include <target/target_core_base.h>
  24. #include <target/target_core_transport.h>
  25. #include <target/target_core_tmr.h>
  26. #include <target/target_core_fabric_ops.h>
  27. #include <target/target_core_configfs.h>
  28. #include "iscsi_target_core.h"
  29. #include "iscsi_target_parameters.h"
  30. #include "iscsi_target_seq_pdu_list.h"
  31. #include "iscsi_target_datain_values.h"
  32. #include "iscsi_target_erl0.h"
  33. #include "iscsi_target_erl1.h"
  34. #include "iscsi_target_erl2.h"
  35. #include "iscsi_target_tpg.h"
  36. #include "iscsi_target_tq.h"
  37. #include "iscsi_target_util.h"
  38. #include "iscsi_target.h"
  39. #define PRINT_BUFF(buff, len) \
  40. { \
  41. int zzz; \
  42. \
  43. pr_debug("%d:\n", __LINE__); \
  44. for (zzz = 0; zzz < len; zzz++) { \
  45. if (zzz % 16 == 0) { \
  46. if (zzz) \
  47. pr_debug("\n"); \
  48. pr_debug("%4i: ", zzz); \
  49. } \
  50. pr_debug("%02x ", (unsigned char) (buff)[zzz]); \
  51. } \
  52. if ((len + 1) % 16) \
  53. pr_debug("\n"); \
  54. }
  55. extern struct list_head g_tiqn_list;
  56. extern spinlock_t tiqn_lock;
  57. /*
  58. * Called with cmd->r2t_lock held.
  59. */
  60. int iscsit_add_r2t_to_list(
  61. struct iscsi_cmd *cmd,
  62. u32 offset,
  63. u32 xfer_len,
  64. int recovery,
  65. u32 r2t_sn)
  66. {
  67. struct iscsi_r2t *r2t;
  68. r2t = kmem_cache_zalloc(lio_r2t_cache, GFP_ATOMIC);
  69. if (!r2t) {
  70. pr_err("Unable to allocate memory for struct iscsi_r2t.\n");
  71. return -1;
  72. }
  73. INIT_LIST_HEAD(&r2t->r2t_list);
  74. r2t->recovery_r2t = recovery;
  75. r2t->r2t_sn = (!r2t_sn) ? cmd->r2t_sn++ : r2t_sn;
  76. r2t->offset = offset;
  77. r2t->xfer_len = xfer_len;
  78. list_add_tail(&r2t->r2t_list, &cmd->cmd_r2t_list);
  79. spin_unlock_bh(&cmd->r2t_lock);
  80. iscsit_add_cmd_to_immediate_queue(cmd, cmd->conn, ISTATE_SEND_R2T);
  81. spin_lock_bh(&cmd->r2t_lock);
  82. return 0;
  83. }
  84. struct iscsi_r2t *iscsit_get_r2t_for_eos(
  85. struct iscsi_cmd *cmd,
  86. u32 offset,
  87. u32 length)
  88. {
  89. struct iscsi_r2t *r2t;
  90. spin_lock_bh(&cmd->r2t_lock);
  91. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  92. if ((r2t->offset <= offset) &&
  93. (r2t->offset + r2t->xfer_len) >= (offset + length)) {
  94. spin_unlock_bh(&cmd->r2t_lock);
  95. return r2t;
  96. }
  97. }
  98. spin_unlock_bh(&cmd->r2t_lock);
  99. pr_err("Unable to locate R2T for Offset: %u, Length:"
  100. " %u\n", offset, length);
  101. return NULL;
  102. }
  103. struct iscsi_r2t *iscsit_get_r2t_from_list(struct iscsi_cmd *cmd)
  104. {
  105. struct iscsi_r2t *r2t;
  106. spin_lock_bh(&cmd->r2t_lock);
  107. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  108. if (!r2t->sent_r2t) {
  109. spin_unlock_bh(&cmd->r2t_lock);
  110. return r2t;
  111. }
  112. }
  113. spin_unlock_bh(&cmd->r2t_lock);
  114. pr_err("Unable to locate next R2T to send for ITT:"
  115. " 0x%08x.\n", cmd->init_task_tag);
  116. return NULL;
  117. }
  118. /*
  119. * Called with cmd->r2t_lock held.
  120. */
  121. void iscsit_free_r2t(struct iscsi_r2t *r2t, struct iscsi_cmd *cmd)
  122. {
  123. list_del(&r2t->r2t_list);
  124. kmem_cache_free(lio_r2t_cache, r2t);
  125. }
  126. void iscsit_free_r2ts_from_list(struct iscsi_cmd *cmd)
  127. {
  128. struct iscsi_r2t *r2t, *r2t_tmp;
  129. spin_lock_bh(&cmd->r2t_lock);
  130. list_for_each_entry_safe(r2t, r2t_tmp, &cmd->cmd_r2t_list, r2t_list)
  131. iscsit_free_r2t(r2t, cmd);
  132. spin_unlock_bh(&cmd->r2t_lock);
  133. }
  134. /*
  135. * May be called from software interrupt (timer) context for allocating
  136. * iSCSI NopINs.
  137. */
  138. struct iscsi_cmd *iscsit_allocate_cmd(struct iscsi_conn *conn, gfp_t gfp_mask)
  139. {
  140. struct iscsi_cmd *cmd;
  141. cmd = kmem_cache_zalloc(lio_cmd_cache, gfp_mask);
  142. if (!cmd) {
  143. pr_err("Unable to allocate memory for struct iscsi_cmd.\n");
  144. return NULL;
  145. }
  146. cmd->conn = conn;
  147. INIT_LIST_HEAD(&cmd->i_list);
  148. INIT_LIST_HEAD(&cmd->datain_list);
  149. INIT_LIST_HEAD(&cmd->cmd_r2t_list);
  150. init_completion(&cmd->reject_comp);
  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. /*
  159. * Called from iscsi_handle_scsi_cmd()
  160. */
  161. struct iscsi_cmd *iscsit_allocate_se_cmd(
  162. struct iscsi_conn *conn,
  163. u32 data_length,
  164. int data_direction,
  165. int iscsi_task_attr)
  166. {
  167. struct iscsi_cmd *cmd;
  168. struct se_cmd *se_cmd;
  169. int sam_task_attr;
  170. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  171. if (!cmd)
  172. return NULL;
  173. cmd->data_direction = data_direction;
  174. cmd->data_length = data_length;
  175. /*
  176. * Figure out the SAM Task Attribute for the incoming SCSI CDB
  177. */
  178. if ((iscsi_task_attr == ISCSI_ATTR_UNTAGGED) ||
  179. (iscsi_task_attr == ISCSI_ATTR_SIMPLE))
  180. sam_task_attr = MSG_SIMPLE_TAG;
  181. else if (iscsi_task_attr == ISCSI_ATTR_ORDERED)
  182. sam_task_attr = MSG_ORDERED_TAG;
  183. else if (iscsi_task_attr == ISCSI_ATTR_HEAD_OF_QUEUE)
  184. sam_task_attr = MSG_HEAD_TAG;
  185. else if (iscsi_task_attr == ISCSI_ATTR_ACA)
  186. sam_task_attr = MSG_ACA_TAG;
  187. else {
  188. pr_debug("Unknown iSCSI Task Attribute: 0x%02x, using"
  189. " MSG_SIMPLE_TAG\n", iscsi_task_attr);
  190. sam_task_attr = MSG_SIMPLE_TAG;
  191. }
  192. se_cmd = &cmd->se_cmd;
  193. /*
  194. * Initialize struct se_cmd descriptor from target_core_mod infrastructure
  195. */
  196. transport_init_se_cmd(se_cmd, &lio_target_fabric_configfs->tf_ops,
  197. conn->sess->se_sess, data_length, data_direction,
  198. sam_task_attr, &cmd->sense_buffer[0]);
  199. return cmd;
  200. }
  201. struct iscsi_cmd *iscsit_allocate_se_cmd_for_tmr(
  202. struct iscsi_conn *conn,
  203. u8 function)
  204. {
  205. struct iscsi_cmd *cmd;
  206. struct se_cmd *se_cmd;
  207. u8 tcm_function;
  208. cmd = iscsit_allocate_cmd(conn, GFP_KERNEL);
  209. if (!cmd)
  210. return NULL;
  211. cmd->data_direction = DMA_NONE;
  212. cmd->tmr_req = kzalloc(sizeof(struct iscsi_tmr_req), GFP_KERNEL);
  213. if (!cmd->tmr_req) {
  214. pr_err("Unable to allocate memory for"
  215. " Task Management command!\n");
  216. return NULL;
  217. }
  218. /*
  219. * TASK_REASSIGN for ERL=2 / connection stays inside of
  220. * LIO-Target $FABRIC_MOD
  221. */
  222. if (function == ISCSI_TM_FUNC_TASK_REASSIGN)
  223. return cmd;
  224. se_cmd = &cmd->se_cmd;
  225. /*
  226. * Initialize struct se_cmd descriptor from target_core_mod infrastructure
  227. */
  228. transport_init_se_cmd(se_cmd, &lio_target_fabric_configfs->tf_ops,
  229. conn->sess->se_sess, 0, DMA_NONE,
  230. MSG_SIMPLE_TAG, &cmd->sense_buffer[0]);
  231. switch (function) {
  232. case ISCSI_TM_FUNC_ABORT_TASK:
  233. tcm_function = TMR_ABORT_TASK;
  234. break;
  235. case ISCSI_TM_FUNC_ABORT_TASK_SET:
  236. tcm_function = TMR_ABORT_TASK_SET;
  237. break;
  238. case ISCSI_TM_FUNC_CLEAR_ACA:
  239. tcm_function = TMR_CLEAR_ACA;
  240. break;
  241. case ISCSI_TM_FUNC_CLEAR_TASK_SET:
  242. tcm_function = TMR_CLEAR_TASK_SET;
  243. break;
  244. case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
  245. tcm_function = TMR_LUN_RESET;
  246. break;
  247. case ISCSI_TM_FUNC_TARGET_WARM_RESET:
  248. tcm_function = TMR_TARGET_WARM_RESET;
  249. break;
  250. case ISCSI_TM_FUNC_TARGET_COLD_RESET:
  251. tcm_function = TMR_TARGET_COLD_RESET;
  252. break;
  253. default:
  254. pr_err("Unknown iSCSI TMR Function:"
  255. " 0x%02x\n", function);
  256. goto out;
  257. }
  258. se_cmd->se_tmr_req = core_tmr_alloc_req(se_cmd,
  259. (void *)cmd->tmr_req, tcm_function);
  260. if (!se_cmd->se_tmr_req)
  261. goto out;
  262. cmd->tmr_req->se_tmr_req = se_cmd->se_tmr_req;
  263. return cmd;
  264. out:
  265. iscsit_release_cmd(cmd);
  266. if (se_cmd)
  267. transport_free_se_cmd(se_cmd);
  268. return NULL;
  269. }
  270. int iscsit_decide_list_to_build(
  271. struct iscsi_cmd *cmd,
  272. u32 immediate_data_length)
  273. {
  274. struct iscsi_build_list bl;
  275. struct iscsi_conn *conn = cmd->conn;
  276. struct iscsi_session *sess = conn->sess;
  277. struct iscsi_node_attrib *na;
  278. if (sess->sess_ops->DataSequenceInOrder &&
  279. sess->sess_ops->DataPDUInOrder)
  280. return 0;
  281. if (cmd->data_direction == DMA_NONE)
  282. return 0;
  283. na = iscsit_tpg_get_node_attrib(sess);
  284. memset(&bl, 0, sizeof(struct iscsi_build_list));
  285. if (cmd->data_direction == DMA_FROM_DEVICE) {
  286. bl.data_direction = ISCSI_PDU_READ;
  287. bl.type = PDULIST_NORMAL;
  288. if (na->random_datain_pdu_offsets)
  289. bl.randomize |= RANDOM_DATAIN_PDU_OFFSETS;
  290. if (na->random_datain_seq_offsets)
  291. bl.randomize |= RANDOM_DATAIN_SEQ_OFFSETS;
  292. } else {
  293. bl.data_direction = ISCSI_PDU_WRITE;
  294. bl.immediate_data_length = immediate_data_length;
  295. if (na->random_r2t_offsets)
  296. bl.randomize |= RANDOM_R2T_OFFSETS;
  297. if (!cmd->immediate_data && !cmd->unsolicited_data)
  298. bl.type = PDULIST_NORMAL;
  299. else if (cmd->immediate_data && !cmd->unsolicited_data)
  300. bl.type = PDULIST_IMMEDIATE;
  301. else if (!cmd->immediate_data && cmd->unsolicited_data)
  302. bl.type = PDULIST_UNSOLICITED;
  303. else if (cmd->immediate_data && cmd->unsolicited_data)
  304. bl.type = PDULIST_IMMEDIATE_AND_UNSOLICITED;
  305. }
  306. return iscsit_do_build_list(cmd, &bl);
  307. }
  308. struct iscsi_seq *iscsit_get_seq_holder_for_datain(
  309. struct iscsi_cmd *cmd,
  310. u32 seq_send_order)
  311. {
  312. u32 i;
  313. for (i = 0; i < cmd->seq_count; i++)
  314. if (cmd->seq_list[i].seq_send_order == seq_send_order)
  315. return &cmd->seq_list[i];
  316. return NULL;
  317. }
  318. struct iscsi_seq *iscsit_get_seq_holder_for_r2t(struct iscsi_cmd *cmd)
  319. {
  320. u32 i;
  321. if (!cmd->seq_list) {
  322. pr_err("struct iscsi_cmd->seq_list is NULL!\n");
  323. return NULL;
  324. }
  325. for (i = 0; i < cmd->seq_count; i++) {
  326. if (cmd->seq_list[i].type != SEQTYPE_NORMAL)
  327. continue;
  328. if (cmd->seq_list[i].seq_send_order == cmd->seq_send_order) {
  329. cmd->seq_send_order++;
  330. return &cmd->seq_list[i];
  331. }
  332. }
  333. return NULL;
  334. }
  335. struct iscsi_r2t *iscsit_get_holder_for_r2tsn(
  336. struct iscsi_cmd *cmd,
  337. u32 r2t_sn)
  338. {
  339. struct iscsi_r2t *r2t;
  340. spin_lock_bh(&cmd->r2t_lock);
  341. list_for_each_entry(r2t, &cmd->cmd_r2t_list, r2t_list) {
  342. if (r2t->r2t_sn == r2t_sn) {
  343. spin_unlock_bh(&cmd->r2t_lock);
  344. return r2t;
  345. }
  346. }
  347. spin_unlock_bh(&cmd->r2t_lock);
  348. return NULL;
  349. }
  350. static inline int iscsit_check_received_cmdsn(struct iscsi_session *sess, u32 cmdsn)
  351. {
  352. int ret;
  353. /*
  354. * This is the proper method of checking received CmdSN against
  355. * ExpCmdSN and MaxCmdSN values, as well as accounting for out
  356. * or order CmdSNs due to multiple connection sessions and/or
  357. * CRC failures.
  358. */
  359. if (iscsi_sna_gt(cmdsn, sess->max_cmd_sn)) {
  360. pr_err("Received CmdSN: 0x%08x is greater than"
  361. " MaxCmdSN: 0x%08x, protocol error.\n", cmdsn,
  362. sess->max_cmd_sn);
  363. ret = CMDSN_ERROR_CANNOT_RECOVER;
  364. } else if (cmdsn == sess->exp_cmd_sn) {
  365. sess->exp_cmd_sn++;
  366. pr_debug("Received CmdSN matches ExpCmdSN,"
  367. " incremented ExpCmdSN to: 0x%08x\n",
  368. sess->exp_cmd_sn);
  369. ret = CMDSN_NORMAL_OPERATION;
  370. } else if (iscsi_sna_gt(cmdsn, sess->exp_cmd_sn)) {
  371. pr_debug("Received CmdSN: 0x%08x is greater"
  372. " than ExpCmdSN: 0x%08x, not acknowledging.\n",
  373. cmdsn, sess->exp_cmd_sn);
  374. ret = CMDSN_HIGHER_THAN_EXP;
  375. } else {
  376. pr_err("Received CmdSN: 0x%08x is less than"
  377. " ExpCmdSN: 0x%08x, ignoring.\n", cmdsn,
  378. sess->exp_cmd_sn);
  379. ret = CMDSN_LOWER_THAN_EXP;
  380. }
  381. return ret;
  382. }
  383. /*
  384. * Commands may be received out of order if MC/S is in use.
  385. * Ensure they are executed in CmdSN order.
  386. */
  387. int iscsit_sequence_cmd(
  388. struct iscsi_conn *conn,
  389. struct iscsi_cmd *cmd,
  390. u32 cmdsn)
  391. {
  392. int ret;
  393. int cmdsn_ret;
  394. mutex_lock(&conn->sess->cmdsn_mutex);
  395. cmdsn_ret = iscsit_check_received_cmdsn(conn->sess, cmdsn);
  396. switch (cmdsn_ret) {
  397. case CMDSN_NORMAL_OPERATION:
  398. ret = iscsit_execute_cmd(cmd, 0);
  399. if ((ret >= 0) && !list_empty(&conn->sess->sess_ooo_cmdsn_list))
  400. iscsit_execute_ooo_cmdsns(conn->sess);
  401. break;
  402. case CMDSN_HIGHER_THAN_EXP:
  403. ret = iscsit_handle_ooo_cmdsn(conn->sess, cmd, cmdsn);
  404. break;
  405. case CMDSN_LOWER_THAN_EXP:
  406. cmd->i_state = ISTATE_REMOVE;
  407. iscsit_add_cmd_to_immediate_queue(cmd, conn, cmd->i_state);
  408. ret = cmdsn_ret;
  409. break;
  410. default:
  411. ret = cmdsn_ret;
  412. break;
  413. }
  414. mutex_unlock(&conn->sess->cmdsn_mutex);
  415. return ret;
  416. }
  417. int iscsit_check_unsolicited_dataout(struct iscsi_cmd *cmd, unsigned char *buf)
  418. {
  419. struct iscsi_conn *conn = cmd->conn;
  420. struct se_cmd *se_cmd = &cmd->se_cmd;
  421. struct iscsi_data *hdr = (struct iscsi_data *) buf;
  422. u32 payload_length = ntoh24(hdr->dlength);
  423. if (conn->sess->sess_ops->InitialR2T) {
  424. pr_err("Received unexpected unsolicited data"
  425. " while InitialR2T=Yes, protocol error.\n");
  426. transport_send_check_condition_and_sense(se_cmd,
  427. TCM_UNEXPECTED_UNSOLICITED_DATA, 0);
  428. return -1;
  429. }
  430. if ((cmd->first_burst_len + payload_length) >
  431. conn->sess->sess_ops->FirstBurstLength) {
  432. pr_err("Total %u bytes exceeds FirstBurstLength: %u"
  433. " for this Unsolicited DataOut Burst.\n",
  434. (cmd->first_burst_len + payload_length),
  435. conn->sess->sess_ops->FirstBurstLength);
  436. transport_send_check_condition_and_sense(se_cmd,
  437. TCM_INCORRECT_AMOUNT_OF_DATA, 0);
  438. return -1;
  439. }
  440. if (!(hdr->flags & ISCSI_FLAG_CMD_FINAL))
  441. return 0;
  442. if (((cmd->first_burst_len + payload_length) != cmd->data_length) &&
  443. ((cmd->first_burst_len + payload_length) !=
  444. conn->sess->sess_ops->FirstBurstLength)) {
  445. pr_err("Unsolicited non-immediate data received %u"
  446. " does not equal FirstBurstLength: %u, and does"
  447. " not equal ExpXferLen %u.\n",
  448. (cmd->first_burst_len + payload_length),
  449. conn->sess->sess_ops->FirstBurstLength, cmd->data_length);
  450. transport_send_check_condition_and_sense(se_cmd,
  451. TCM_INCORRECT_AMOUNT_OF_DATA, 0);
  452. return -1;
  453. }
  454. return 0;
  455. }
  456. struct iscsi_cmd *iscsit_find_cmd_from_itt(
  457. struct iscsi_conn *conn,
  458. u32 init_task_tag)
  459. {
  460. struct iscsi_cmd *cmd;
  461. spin_lock_bh(&conn->cmd_lock);
  462. list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
  463. if (cmd->init_task_tag == init_task_tag) {
  464. spin_unlock_bh(&conn->cmd_lock);
  465. return cmd;
  466. }
  467. }
  468. spin_unlock_bh(&conn->cmd_lock);
  469. pr_err("Unable to locate ITT: 0x%08x on CID: %hu",
  470. init_task_tag, conn->cid);
  471. return NULL;
  472. }
  473. struct iscsi_cmd *iscsit_find_cmd_from_itt_or_dump(
  474. struct iscsi_conn *conn,
  475. u32 init_task_tag,
  476. u32 length)
  477. {
  478. struct iscsi_cmd *cmd;
  479. spin_lock_bh(&conn->cmd_lock);
  480. list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
  481. if (cmd->init_task_tag == init_task_tag) {
  482. spin_unlock_bh(&conn->cmd_lock);
  483. return cmd;
  484. }
  485. }
  486. spin_unlock_bh(&conn->cmd_lock);
  487. pr_err("Unable to locate ITT: 0x%08x on CID: %hu,"
  488. " dumping payload\n", init_task_tag, conn->cid);
  489. if (length)
  490. iscsit_dump_data_payload(conn, length, 1);
  491. return NULL;
  492. }
  493. struct iscsi_cmd *iscsit_find_cmd_from_ttt(
  494. struct iscsi_conn *conn,
  495. u32 targ_xfer_tag)
  496. {
  497. struct iscsi_cmd *cmd = NULL;
  498. spin_lock_bh(&conn->cmd_lock);
  499. list_for_each_entry(cmd, &conn->conn_cmd_list, i_list) {
  500. if (cmd->targ_xfer_tag == targ_xfer_tag) {
  501. spin_unlock_bh(&conn->cmd_lock);
  502. return cmd;
  503. }
  504. }
  505. spin_unlock_bh(&conn->cmd_lock);
  506. pr_err("Unable to locate TTT: 0x%08x on CID: %hu\n",
  507. targ_xfer_tag, conn->cid);
  508. return NULL;
  509. }
  510. int iscsit_find_cmd_for_recovery(
  511. struct iscsi_session *sess,
  512. struct iscsi_cmd **cmd_ptr,
  513. struct iscsi_conn_recovery **cr_ptr,
  514. u32 init_task_tag)
  515. {
  516. struct iscsi_cmd *cmd = NULL;
  517. struct iscsi_conn_recovery *cr;
  518. /*
  519. * Scan through the inactive connection recovery list's command list.
  520. * If init_task_tag matches the command is still alligent.
  521. */
  522. spin_lock(&sess->cr_i_lock);
  523. list_for_each_entry(cr, &sess->cr_inactive_list, cr_list) {
  524. spin_lock(&cr->conn_recovery_cmd_lock);
  525. list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_list) {
  526. if (cmd->init_task_tag == init_task_tag) {
  527. spin_unlock(&cr->conn_recovery_cmd_lock);
  528. spin_unlock(&sess->cr_i_lock);
  529. *cr_ptr = cr;
  530. *cmd_ptr = cmd;
  531. return -2;
  532. }
  533. }
  534. spin_unlock(&cr->conn_recovery_cmd_lock);
  535. }
  536. spin_unlock(&sess->cr_i_lock);
  537. /*
  538. * Scan through the active connection recovery list's command list.
  539. * If init_task_tag matches the command is ready to be reassigned.
  540. */
  541. spin_lock(&sess->cr_a_lock);
  542. list_for_each_entry(cr, &sess->cr_active_list, cr_list) {
  543. spin_lock(&cr->conn_recovery_cmd_lock);
  544. list_for_each_entry(cmd, &cr->conn_recovery_cmd_list, i_list) {
  545. if (cmd->init_task_tag == init_task_tag) {
  546. spin_unlock(&cr->conn_recovery_cmd_lock);
  547. spin_unlock(&sess->cr_a_lock);
  548. *cr_ptr = cr;
  549. *cmd_ptr = cmd;
  550. return 0;
  551. }
  552. }
  553. spin_unlock(&cr->conn_recovery_cmd_lock);
  554. }
  555. spin_unlock(&sess->cr_a_lock);
  556. return -1;
  557. }
  558. void iscsit_add_cmd_to_immediate_queue(
  559. struct iscsi_cmd *cmd,
  560. struct iscsi_conn *conn,
  561. u8 state)
  562. {
  563. struct iscsi_queue_req *qr;
  564. qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
  565. if (!qr) {
  566. pr_err("Unable to allocate memory for"
  567. " struct iscsi_queue_req\n");
  568. return;
  569. }
  570. INIT_LIST_HEAD(&qr->qr_list);
  571. qr->cmd = cmd;
  572. qr->state = state;
  573. spin_lock_bh(&conn->immed_queue_lock);
  574. list_add_tail(&qr->qr_list, &conn->immed_queue_list);
  575. atomic_inc(&cmd->immed_queue_count);
  576. atomic_set(&conn->check_immediate_queue, 1);
  577. spin_unlock_bh(&conn->immed_queue_lock);
  578. wake_up_process(conn->thread_set->tx_thread);
  579. }
  580. struct iscsi_queue_req *iscsit_get_cmd_from_immediate_queue(struct iscsi_conn *conn)
  581. {
  582. struct iscsi_queue_req *qr;
  583. spin_lock_bh(&conn->immed_queue_lock);
  584. if (list_empty(&conn->immed_queue_list)) {
  585. spin_unlock_bh(&conn->immed_queue_lock);
  586. return NULL;
  587. }
  588. list_for_each_entry(qr, &conn->immed_queue_list, qr_list)
  589. break;
  590. list_del(&qr->qr_list);
  591. if (qr->cmd)
  592. atomic_dec(&qr->cmd->immed_queue_count);
  593. spin_unlock_bh(&conn->immed_queue_lock);
  594. return qr;
  595. }
  596. static void iscsit_remove_cmd_from_immediate_queue(
  597. struct iscsi_cmd *cmd,
  598. struct iscsi_conn *conn)
  599. {
  600. struct iscsi_queue_req *qr, *qr_tmp;
  601. spin_lock_bh(&conn->immed_queue_lock);
  602. if (!atomic_read(&cmd->immed_queue_count)) {
  603. spin_unlock_bh(&conn->immed_queue_lock);
  604. return;
  605. }
  606. list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
  607. if (qr->cmd != cmd)
  608. continue;
  609. atomic_dec(&qr->cmd->immed_queue_count);
  610. list_del(&qr->qr_list);
  611. kmem_cache_free(lio_qr_cache, qr);
  612. }
  613. spin_unlock_bh(&conn->immed_queue_lock);
  614. if (atomic_read(&cmd->immed_queue_count)) {
  615. pr_err("ITT: 0x%08x immed_queue_count: %d\n",
  616. cmd->init_task_tag,
  617. atomic_read(&cmd->immed_queue_count));
  618. }
  619. }
  620. void iscsit_add_cmd_to_response_queue(
  621. struct iscsi_cmd *cmd,
  622. struct iscsi_conn *conn,
  623. u8 state)
  624. {
  625. struct iscsi_queue_req *qr;
  626. qr = kmem_cache_zalloc(lio_qr_cache, GFP_ATOMIC);
  627. if (!qr) {
  628. pr_err("Unable to allocate memory for"
  629. " struct iscsi_queue_req\n");
  630. return;
  631. }
  632. INIT_LIST_HEAD(&qr->qr_list);
  633. qr->cmd = cmd;
  634. qr->state = state;
  635. spin_lock_bh(&conn->response_queue_lock);
  636. list_add_tail(&qr->qr_list, &conn->response_queue_list);
  637. atomic_inc(&cmd->response_queue_count);
  638. spin_unlock_bh(&conn->response_queue_lock);
  639. wake_up_process(conn->thread_set->tx_thread);
  640. }
  641. struct iscsi_queue_req *iscsit_get_cmd_from_response_queue(struct iscsi_conn *conn)
  642. {
  643. struct iscsi_queue_req *qr;
  644. spin_lock_bh(&conn->response_queue_lock);
  645. if (list_empty(&conn->response_queue_list)) {
  646. spin_unlock_bh(&conn->response_queue_lock);
  647. return NULL;
  648. }
  649. list_for_each_entry(qr, &conn->response_queue_list, qr_list)
  650. break;
  651. list_del(&qr->qr_list);
  652. if (qr->cmd)
  653. atomic_dec(&qr->cmd->response_queue_count);
  654. spin_unlock_bh(&conn->response_queue_lock);
  655. return qr;
  656. }
  657. static void iscsit_remove_cmd_from_response_queue(
  658. struct iscsi_cmd *cmd,
  659. struct iscsi_conn *conn)
  660. {
  661. struct iscsi_queue_req *qr, *qr_tmp;
  662. spin_lock_bh(&conn->response_queue_lock);
  663. if (!atomic_read(&cmd->response_queue_count)) {
  664. spin_unlock_bh(&conn->response_queue_lock);
  665. return;
  666. }
  667. list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
  668. qr_list) {
  669. if (qr->cmd != cmd)
  670. continue;
  671. atomic_dec(&qr->cmd->response_queue_count);
  672. list_del(&qr->qr_list);
  673. kmem_cache_free(lio_qr_cache, qr);
  674. }
  675. spin_unlock_bh(&conn->response_queue_lock);
  676. if (atomic_read(&cmd->response_queue_count)) {
  677. pr_err("ITT: 0x%08x response_queue_count: %d\n",
  678. cmd->init_task_tag,
  679. atomic_read(&cmd->response_queue_count));
  680. }
  681. }
  682. void iscsit_free_queue_reqs_for_conn(struct iscsi_conn *conn)
  683. {
  684. struct iscsi_queue_req *qr, *qr_tmp;
  685. spin_lock_bh(&conn->immed_queue_lock);
  686. list_for_each_entry_safe(qr, qr_tmp, &conn->immed_queue_list, qr_list) {
  687. list_del(&qr->qr_list);
  688. if (qr->cmd)
  689. atomic_dec(&qr->cmd->immed_queue_count);
  690. kmem_cache_free(lio_qr_cache, qr);
  691. }
  692. spin_unlock_bh(&conn->immed_queue_lock);
  693. spin_lock_bh(&conn->response_queue_lock);
  694. list_for_each_entry_safe(qr, qr_tmp, &conn->response_queue_list,
  695. qr_list) {
  696. list_del(&qr->qr_list);
  697. if (qr->cmd)
  698. atomic_dec(&qr->cmd->response_queue_count);
  699. kmem_cache_free(lio_qr_cache, qr);
  700. }
  701. spin_unlock_bh(&conn->response_queue_lock);
  702. }
  703. void iscsit_release_cmd(struct iscsi_cmd *cmd)
  704. {
  705. struct iscsi_conn *conn = cmd->conn;
  706. int i;
  707. iscsit_free_r2ts_from_list(cmd);
  708. iscsit_free_all_datain_reqs(cmd);
  709. kfree(cmd->buf_ptr);
  710. kfree(cmd->pdu_list);
  711. kfree(cmd->seq_list);
  712. kfree(cmd->tmr_req);
  713. kfree(cmd->iov_data);
  714. for (i = 0; i < cmd->t_mem_sg_nents; i++)
  715. __free_page(sg_page(&cmd->t_mem_sg[i]));
  716. kfree(cmd->t_mem_sg);
  717. if (conn) {
  718. iscsit_remove_cmd_from_immediate_queue(cmd, conn);
  719. iscsit_remove_cmd_from_response_queue(cmd, conn);
  720. }
  721. kmem_cache_free(lio_cmd_cache, cmd);
  722. }
  723. int iscsit_check_session_usage_count(struct iscsi_session *sess)
  724. {
  725. spin_lock_bh(&sess->session_usage_lock);
  726. if (sess->session_usage_count != 0) {
  727. sess->session_waiting_on_uc = 1;
  728. spin_unlock_bh(&sess->session_usage_lock);
  729. if (in_interrupt())
  730. return 2;
  731. wait_for_completion(&sess->session_waiting_on_uc_comp);
  732. return 1;
  733. }
  734. spin_unlock_bh(&sess->session_usage_lock);
  735. return 0;
  736. }
  737. void iscsit_dec_session_usage_count(struct iscsi_session *sess)
  738. {
  739. spin_lock_bh(&sess->session_usage_lock);
  740. sess->session_usage_count--;
  741. if (!sess->session_usage_count && sess->session_waiting_on_uc)
  742. complete(&sess->session_waiting_on_uc_comp);
  743. spin_unlock_bh(&sess->session_usage_lock);
  744. }
  745. void iscsit_inc_session_usage_count(struct iscsi_session *sess)
  746. {
  747. spin_lock_bh(&sess->session_usage_lock);
  748. sess->session_usage_count++;
  749. spin_unlock_bh(&sess->session_usage_lock);
  750. }
  751. /*
  752. * Used before iscsi_do[rx,tx]_data() to determine iov and [rx,tx]_marker
  753. * array counts needed for sync and steering.
  754. */
  755. static int iscsit_determine_sync_and_steering_counts(
  756. struct iscsi_conn *conn,
  757. struct iscsi_data_count *count)
  758. {
  759. u32 length = count->data_length;
  760. u32 marker, markint;
  761. count->sync_and_steering = 1;
  762. marker = (count->type == ISCSI_RX_DATA) ?
  763. conn->of_marker : conn->if_marker;
  764. markint = (count->type == ISCSI_RX_DATA) ?
  765. (conn->conn_ops->OFMarkInt * 4) :
  766. (conn->conn_ops->IFMarkInt * 4);
  767. count->ss_iov_count = count->iov_count;
  768. while (length > 0) {
  769. if (length >= marker) {
  770. count->ss_iov_count += 3;
  771. count->ss_marker_count += 2;
  772. length -= marker;
  773. marker = markint;
  774. } else
  775. length = 0;
  776. }
  777. return 0;
  778. }
  779. /*
  780. * Setup conn->if_marker and conn->of_marker values based upon
  781. * the initial marker-less interval. (see iSCSI v19 A.2)
  782. */
  783. int iscsit_set_sync_and_steering_values(struct iscsi_conn *conn)
  784. {
  785. int login_ifmarker_count = 0, login_ofmarker_count = 0, next_marker = 0;
  786. /*
  787. * IFMarkInt and OFMarkInt are negotiated as 32-bit words.
  788. */
  789. u32 IFMarkInt = (conn->conn_ops->IFMarkInt * 4);
  790. u32 OFMarkInt = (conn->conn_ops->OFMarkInt * 4);
  791. if (conn->conn_ops->OFMarker) {
  792. /*
  793. * Account for the first Login Command received not
  794. * via iscsi_recv_msg().
  795. */
  796. conn->of_marker += ISCSI_HDR_LEN;
  797. if (conn->of_marker <= OFMarkInt) {
  798. conn->of_marker = (OFMarkInt - conn->of_marker);
  799. } else {
  800. login_ofmarker_count = (conn->of_marker / OFMarkInt);
  801. next_marker = (OFMarkInt * (login_ofmarker_count + 1)) +
  802. (login_ofmarker_count * MARKER_SIZE);
  803. conn->of_marker = (next_marker - conn->of_marker);
  804. }
  805. conn->of_marker_offset = 0;
  806. pr_debug("Setting OFMarker value to %u based on Initial"
  807. " Markerless Interval.\n", conn->of_marker);
  808. }
  809. if (conn->conn_ops->IFMarker) {
  810. if (conn->if_marker <= IFMarkInt) {
  811. conn->if_marker = (IFMarkInt - conn->if_marker);
  812. } else {
  813. login_ifmarker_count = (conn->if_marker / IFMarkInt);
  814. next_marker = (IFMarkInt * (login_ifmarker_count + 1)) +
  815. (login_ifmarker_count * MARKER_SIZE);
  816. conn->if_marker = (next_marker - conn->if_marker);
  817. }
  818. pr_debug("Setting IFMarker value to %u based on Initial"
  819. " Markerless Interval.\n", conn->if_marker);
  820. }
  821. return 0;
  822. }
  823. struct iscsi_conn *iscsit_get_conn_from_cid(struct iscsi_session *sess, u16 cid)
  824. {
  825. struct iscsi_conn *conn;
  826. spin_lock_bh(&sess->conn_lock);
  827. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  828. if ((conn->cid == cid) &&
  829. (conn->conn_state == TARG_CONN_STATE_LOGGED_IN)) {
  830. iscsit_inc_conn_usage_count(conn);
  831. spin_unlock_bh(&sess->conn_lock);
  832. return conn;
  833. }
  834. }
  835. spin_unlock_bh(&sess->conn_lock);
  836. return NULL;
  837. }
  838. struct iscsi_conn *iscsit_get_conn_from_cid_rcfr(struct iscsi_session *sess, u16 cid)
  839. {
  840. struct iscsi_conn *conn;
  841. spin_lock_bh(&sess->conn_lock);
  842. list_for_each_entry(conn, &sess->sess_conn_list, conn_list) {
  843. if (conn->cid == cid) {
  844. iscsit_inc_conn_usage_count(conn);
  845. spin_lock(&conn->state_lock);
  846. atomic_set(&conn->connection_wait_rcfr, 1);
  847. spin_unlock(&conn->state_lock);
  848. spin_unlock_bh(&sess->conn_lock);
  849. return conn;
  850. }
  851. }
  852. spin_unlock_bh(&sess->conn_lock);
  853. return NULL;
  854. }
  855. void iscsit_check_conn_usage_count(struct iscsi_conn *conn)
  856. {
  857. spin_lock_bh(&conn->conn_usage_lock);
  858. if (conn->conn_usage_count != 0) {
  859. conn->conn_waiting_on_uc = 1;
  860. spin_unlock_bh(&conn->conn_usage_lock);
  861. wait_for_completion(&conn->conn_waiting_on_uc_comp);
  862. return;
  863. }
  864. spin_unlock_bh(&conn->conn_usage_lock);
  865. }
  866. void iscsit_dec_conn_usage_count(struct iscsi_conn *conn)
  867. {
  868. spin_lock_bh(&conn->conn_usage_lock);
  869. conn->conn_usage_count--;
  870. if (!conn->conn_usage_count && conn->conn_waiting_on_uc)
  871. complete(&conn->conn_waiting_on_uc_comp);
  872. spin_unlock_bh(&conn->conn_usage_lock);
  873. }
  874. void iscsit_inc_conn_usage_count(struct iscsi_conn *conn)
  875. {
  876. spin_lock_bh(&conn->conn_usage_lock);
  877. conn->conn_usage_count++;
  878. spin_unlock_bh(&conn->conn_usage_lock);
  879. }
  880. static int iscsit_add_nopin(struct iscsi_conn *conn, int want_response)
  881. {
  882. u8 state;
  883. struct iscsi_cmd *cmd;
  884. cmd = iscsit_allocate_cmd(conn, GFP_ATOMIC);
  885. if (!cmd)
  886. return -1;
  887. cmd->iscsi_opcode = ISCSI_OP_NOOP_IN;
  888. state = (want_response) ? ISTATE_SEND_NOPIN_WANT_RESPONSE :
  889. ISTATE_SEND_NOPIN_NO_RESPONSE;
  890. cmd->init_task_tag = 0xFFFFFFFF;
  891. spin_lock_bh(&conn->sess->ttt_lock);
  892. cmd->targ_xfer_tag = (want_response) ? conn->sess->targ_xfer_tag++ :
  893. 0xFFFFFFFF;
  894. if (want_response && (cmd->targ_xfer_tag == 0xFFFFFFFF))
  895. cmd->targ_xfer_tag = conn->sess->targ_xfer_tag++;
  896. spin_unlock_bh(&conn->sess->ttt_lock);
  897. spin_lock_bh(&conn->cmd_lock);
  898. list_add_tail(&cmd->i_list, &conn->conn_cmd_list);
  899. spin_unlock_bh(&conn->cmd_lock);
  900. if (want_response)
  901. iscsit_start_nopin_response_timer(conn);
  902. iscsit_add_cmd_to_immediate_queue(cmd, conn, state);
  903. return 0;
  904. }
  905. static void iscsit_handle_nopin_response_timeout(unsigned long data)
  906. {
  907. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  908. iscsit_inc_conn_usage_count(conn);
  909. spin_lock_bh(&conn->nopin_timer_lock);
  910. if (conn->nopin_response_timer_flags & ISCSI_TF_STOP) {
  911. spin_unlock_bh(&conn->nopin_timer_lock);
  912. iscsit_dec_conn_usage_count(conn);
  913. return;
  914. }
  915. pr_debug("Did not receive response to NOPIN on CID: %hu on"
  916. " SID: %u, failing connection.\n", conn->cid,
  917. conn->sess->sid);
  918. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  919. spin_unlock_bh(&conn->nopin_timer_lock);
  920. {
  921. struct iscsi_portal_group *tpg = conn->sess->tpg;
  922. struct iscsi_tiqn *tiqn = tpg->tpg_tiqn;
  923. if (tiqn) {
  924. spin_lock_bh(&tiqn->sess_err_stats.lock);
  925. strcpy(tiqn->sess_err_stats.last_sess_fail_rem_name,
  926. (void *)conn->sess->sess_ops->InitiatorName);
  927. tiqn->sess_err_stats.last_sess_failure_type =
  928. ISCSI_SESS_ERR_CXN_TIMEOUT;
  929. tiqn->sess_err_stats.cxn_timeout_errors++;
  930. conn->sess->conn_timeout_errors++;
  931. spin_unlock_bh(&tiqn->sess_err_stats.lock);
  932. }
  933. }
  934. iscsit_cause_connection_reinstatement(conn, 0);
  935. iscsit_dec_conn_usage_count(conn);
  936. }
  937. void iscsit_mod_nopin_response_timer(struct iscsi_conn *conn)
  938. {
  939. struct iscsi_session *sess = conn->sess;
  940. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  941. spin_lock_bh(&conn->nopin_timer_lock);
  942. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  943. spin_unlock_bh(&conn->nopin_timer_lock);
  944. return;
  945. }
  946. mod_timer(&conn->nopin_response_timer,
  947. (get_jiffies_64() + na->nopin_response_timeout * HZ));
  948. spin_unlock_bh(&conn->nopin_timer_lock);
  949. }
  950. /*
  951. * Called with conn->nopin_timer_lock held.
  952. */
  953. void iscsit_start_nopin_response_timer(struct iscsi_conn *conn)
  954. {
  955. struct iscsi_session *sess = conn->sess;
  956. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  957. spin_lock_bh(&conn->nopin_timer_lock);
  958. if (conn->nopin_response_timer_flags & ISCSI_TF_RUNNING) {
  959. spin_unlock_bh(&conn->nopin_timer_lock);
  960. return;
  961. }
  962. init_timer(&conn->nopin_response_timer);
  963. conn->nopin_response_timer.expires =
  964. (get_jiffies_64() + na->nopin_response_timeout * HZ);
  965. conn->nopin_response_timer.data = (unsigned long)conn;
  966. conn->nopin_response_timer.function = iscsit_handle_nopin_response_timeout;
  967. conn->nopin_response_timer_flags &= ~ISCSI_TF_STOP;
  968. conn->nopin_response_timer_flags |= ISCSI_TF_RUNNING;
  969. add_timer(&conn->nopin_response_timer);
  970. pr_debug("Started NOPIN Response Timer on CID: %d to %u"
  971. " seconds\n", conn->cid, na->nopin_response_timeout);
  972. spin_unlock_bh(&conn->nopin_timer_lock);
  973. }
  974. void iscsit_stop_nopin_response_timer(struct iscsi_conn *conn)
  975. {
  976. spin_lock_bh(&conn->nopin_timer_lock);
  977. if (!(conn->nopin_response_timer_flags & ISCSI_TF_RUNNING)) {
  978. spin_unlock_bh(&conn->nopin_timer_lock);
  979. return;
  980. }
  981. conn->nopin_response_timer_flags |= ISCSI_TF_STOP;
  982. spin_unlock_bh(&conn->nopin_timer_lock);
  983. del_timer_sync(&conn->nopin_response_timer);
  984. spin_lock_bh(&conn->nopin_timer_lock);
  985. conn->nopin_response_timer_flags &= ~ISCSI_TF_RUNNING;
  986. spin_unlock_bh(&conn->nopin_timer_lock);
  987. }
  988. static void iscsit_handle_nopin_timeout(unsigned long data)
  989. {
  990. struct iscsi_conn *conn = (struct iscsi_conn *) data;
  991. iscsit_inc_conn_usage_count(conn);
  992. spin_lock_bh(&conn->nopin_timer_lock);
  993. if (conn->nopin_timer_flags & ISCSI_TF_STOP) {
  994. spin_unlock_bh(&conn->nopin_timer_lock);
  995. iscsit_dec_conn_usage_count(conn);
  996. return;
  997. }
  998. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  999. spin_unlock_bh(&conn->nopin_timer_lock);
  1000. iscsit_add_nopin(conn, 1);
  1001. iscsit_dec_conn_usage_count(conn);
  1002. }
  1003. /*
  1004. * Called with conn->nopin_timer_lock held.
  1005. */
  1006. void __iscsit_start_nopin_timer(struct iscsi_conn *conn)
  1007. {
  1008. struct iscsi_session *sess = conn->sess;
  1009. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1010. /*
  1011. * NOPIN timeout is disabled.
  1012. */
  1013. if (!na->nopin_timeout)
  1014. return;
  1015. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING)
  1016. return;
  1017. init_timer(&conn->nopin_timer);
  1018. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  1019. conn->nopin_timer.data = (unsigned long)conn;
  1020. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  1021. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  1022. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  1023. add_timer(&conn->nopin_timer);
  1024. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  1025. " interval\n", conn->cid, na->nopin_timeout);
  1026. }
  1027. void iscsit_start_nopin_timer(struct iscsi_conn *conn)
  1028. {
  1029. struct iscsi_session *sess = conn->sess;
  1030. struct iscsi_node_attrib *na = iscsit_tpg_get_node_attrib(sess);
  1031. /*
  1032. * NOPIN timeout is disabled..
  1033. */
  1034. if (!na->nopin_timeout)
  1035. return;
  1036. spin_lock_bh(&conn->nopin_timer_lock);
  1037. if (conn->nopin_timer_flags & ISCSI_TF_RUNNING) {
  1038. spin_unlock_bh(&conn->nopin_timer_lock);
  1039. return;
  1040. }
  1041. init_timer(&conn->nopin_timer);
  1042. conn->nopin_timer.expires = (get_jiffies_64() + na->nopin_timeout * HZ);
  1043. conn->nopin_timer.data = (unsigned long)conn;
  1044. conn->nopin_timer.function = iscsit_handle_nopin_timeout;
  1045. conn->nopin_timer_flags &= ~ISCSI_TF_STOP;
  1046. conn->nopin_timer_flags |= ISCSI_TF_RUNNING;
  1047. add_timer(&conn->nopin_timer);
  1048. pr_debug("Started NOPIN Timer on CID: %d at %u second"
  1049. " interval\n", conn->cid, na->nopin_timeout);
  1050. spin_unlock_bh(&conn->nopin_timer_lock);
  1051. }
  1052. void iscsit_stop_nopin_timer(struct iscsi_conn *conn)
  1053. {
  1054. spin_lock_bh(&conn->nopin_timer_lock);
  1055. if (!(conn->nopin_timer_flags & ISCSI_TF_RUNNING)) {
  1056. spin_unlock_bh(&conn->nopin_timer_lock);
  1057. return;
  1058. }
  1059. conn->nopin_timer_flags |= ISCSI_TF_STOP;
  1060. spin_unlock_bh(&conn->nopin_timer_lock);
  1061. del_timer_sync(&conn->nopin_timer);
  1062. spin_lock_bh(&conn->nopin_timer_lock);
  1063. conn->nopin_timer_flags &= ~ISCSI_TF_RUNNING;
  1064. spin_unlock_bh(&conn->nopin_timer_lock);
  1065. }
  1066. int iscsit_send_tx_data(
  1067. struct iscsi_cmd *cmd,
  1068. struct iscsi_conn *conn,
  1069. int use_misc)
  1070. {
  1071. int tx_sent, tx_size;
  1072. u32 iov_count;
  1073. struct kvec *iov;
  1074. send_data:
  1075. tx_size = cmd->tx_size;
  1076. if (!use_misc) {
  1077. iov = &cmd->iov_data[0];
  1078. iov_count = cmd->iov_data_count;
  1079. } else {
  1080. iov = &cmd->iov_misc[0];
  1081. iov_count = cmd->iov_misc_count;
  1082. }
  1083. tx_sent = tx_data(conn, &iov[0], iov_count, tx_size);
  1084. if (tx_size != tx_sent) {
  1085. if (tx_sent == -EAGAIN) {
  1086. pr_err("tx_data() returned -EAGAIN\n");
  1087. goto send_data;
  1088. } else
  1089. return -1;
  1090. }
  1091. cmd->tx_size = 0;
  1092. return 0;
  1093. }
  1094. int iscsit_fe_sendpage_sg(
  1095. struct iscsi_cmd *cmd,
  1096. struct iscsi_conn *conn)
  1097. {
  1098. struct scatterlist *sg = cmd->first_data_sg;
  1099. struct kvec iov;
  1100. u32 tx_hdr_size, data_len;
  1101. u32 offset = cmd->first_data_sg_off;
  1102. int tx_sent;
  1103. send_hdr:
  1104. tx_hdr_size = ISCSI_HDR_LEN;
  1105. if (conn->conn_ops->HeaderDigest)
  1106. tx_hdr_size += ISCSI_CRC_LEN;
  1107. iov.iov_base = cmd->pdu;
  1108. iov.iov_len = tx_hdr_size;
  1109. tx_sent = tx_data(conn, &iov, 1, tx_hdr_size);
  1110. if (tx_hdr_size != tx_sent) {
  1111. if (tx_sent == -EAGAIN) {
  1112. pr_err("tx_data() returned -EAGAIN\n");
  1113. goto send_hdr;
  1114. }
  1115. return -1;
  1116. }
  1117. data_len = cmd->tx_size - tx_hdr_size - cmd->padding;
  1118. if (conn->conn_ops->DataDigest)
  1119. data_len -= ISCSI_CRC_LEN;
  1120. /*
  1121. * Perform sendpage() for each page in the scatterlist
  1122. */
  1123. while (data_len) {
  1124. u32 space = (sg->length - offset);
  1125. u32 sub_len = min_t(u32, data_len, space);
  1126. send_pg:
  1127. tx_sent = conn->sock->ops->sendpage(conn->sock,
  1128. sg_page(sg), sg->offset + offset, sub_len, 0);
  1129. if (tx_sent != sub_len) {
  1130. if (tx_sent == -EAGAIN) {
  1131. pr_err("tcp_sendpage() returned"
  1132. " -EAGAIN\n");
  1133. goto send_pg;
  1134. }
  1135. pr_err("tcp_sendpage() failure: %d\n",
  1136. tx_sent);
  1137. return -1;
  1138. }
  1139. data_len -= sub_len;
  1140. offset = 0;
  1141. sg = sg_next(sg);
  1142. }
  1143. send_padding:
  1144. if (cmd->padding) {
  1145. struct kvec *iov_p =
  1146. &cmd->iov_data[cmd->iov_data_count-1];
  1147. tx_sent = tx_data(conn, iov_p, 1, cmd->padding);
  1148. if (cmd->padding != tx_sent) {
  1149. if (tx_sent == -EAGAIN) {
  1150. pr_err("tx_data() returned -EAGAIN\n");
  1151. goto send_padding;
  1152. }
  1153. return -1;
  1154. }
  1155. }
  1156. send_datacrc:
  1157. if (conn->conn_ops->DataDigest) {
  1158. struct kvec *iov_d =
  1159. &cmd->iov_data[cmd->iov_data_count];
  1160. tx_sent = tx_data(conn, iov_d, 1, ISCSI_CRC_LEN);
  1161. if (ISCSI_CRC_LEN != tx_sent) {
  1162. if (tx_sent == -EAGAIN) {
  1163. pr_err("tx_data() returned -EAGAIN\n");
  1164. goto send_datacrc;
  1165. }
  1166. return -1;
  1167. }
  1168. }
  1169. return 0;
  1170. }
  1171. /*
  1172. * This function is used for mainly sending a ISCSI_TARG_LOGIN_RSP PDU
  1173. * back to the Initiator when an expection condition occurs with the
  1174. * errors set in status_class and status_detail.
  1175. *
  1176. * Parameters: iSCSI Connection, Status Class, Status Detail.
  1177. * Returns: 0 on success, -1 on error.
  1178. */
  1179. int iscsit_tx_login_rsp(struct iscsi_conn *conn, u8 status_class, u8 status_detail)
  1180. {
  1181. u8 iscsi_hdr[ISCSI_HDR_LEN];
  1182. int err;
  1183. struct kvec iov;
  1184. struct iscsi_login_rsp *hdr;
  1185. iscsit_collect_login_stats(conn, status_class, status_detail);
  1186. memset(&iov, 0, sizeof(struct kvec));
  1187. memset(&iscsi_hdr, 0x0, ISCSI_HDR_LEN);
  1188. hdr = (struct iscsi_login_rsp *)&iscsi_hdr;
  1189. hdr->opcode = ISCSI_OP_LOGIN_RSP;
  1190. hdr->status_class = status_class;
  1191. hdr->status_detail = status_detail;
  1192. hdr->itt = cpu_to_be32(conn->login_itt);
  1193. iov.iov_base = &iscsi_hdr;
  1194. iov.iov_len = ISCSI_HDR_LEN;
  1195. PRINT_BUFF(iscsi_hdr, ISCSI_HDR_LEN);
  1196. err = tx_data(conn, &iov, 1, ISCSI_HDR_LEN);
  1197. if (err != ISCSI_HDR_LEN) {
  1198. pr_err("tx_data returned less than expected\n");
  1199. return -1;
  1200. }
  1201. return 0;
  1202. }
  1203. void iscsit_print_session_params(struct iscsi_session *sess)
  1204. {
  1205. struct iscsi_conn *conn;
  1206. pr_debug("-----------------------------[Session Params for"
  1207. " SID: %u]-----------------------------\n", sess->sid);
  1208. spin_lock_bh(&sess->conn_lock);
  1209. list_for_each_entry(conn, &sess->sess_conn_list, conn_list)
  1210. iscsi_dump_conn_ops(conn->conn_ops);
  1211. spin_unlock_bh(&sess->conn_lock);
  1212. iscsi_dump_sess_ops(sess->sess_ops);
  1213. }
  1214. static int iscsit_do_rx_data(
  1215. struct iscsi_conn *conn,
  1216. struct iscsi_data_count *count)
  1217. {
  1218. int data = count->data_length, rx_loop = 0, total_rx = 0, iov_len;
  1219. u32 rx_marker_val[count->ss_marker_count], rx_marker_iov = 0;
  1220. struct kvec iov[count->ss_iov_count], *iov_p;
  1221. struct msghdr msg;
  1222. if (!conn || !conn->sock || !conn->conn_ops)
  1223. return -1;
  1224. memset(&msg, 0, sizeof(struct msghdr));
  1225. if (count->sync_and_steering) {
  1226. int size = 0;
  1227. u32 i, orig_iov_count = 0;
  1228. u32 orig_iov_len = 0, orig_iov_loc = 0;
  1229. u32 iov_count = 0, per_iov_bytes = 0;
  1230. u32 *rx_marker, old_rx_marker = 0;
  1231. struct kvec *iov_record;
  1232. memset(&rx_marker_val, 0,
  1233. count->ss_marker_count * sizeof(u32));
  1234. memset(&iov, 0, count->ss_iov_count * sizeof(struct kvec));
  1235. iov_record = count->iov;
  1236. orig_iov_count = count->iov_count;
  1237. rx_marker = &conn->of_marker;
  1238. i = 0;
  1239. size = data;
  1240. orig_iov_len = iov_record[orig_iov_loc].iov_len;
  1241. while (size > 0) {
  1242. pr_debug("rx_data: #1 orig_iov_len %u,"
  1243. " orig_iov_loc %u\n", orig_iov_len, orig_iov_loc);
  1244. pr_debug("rx_data: #2 rx_marker %u, size"
  1245. " %u\n", *rx_marker, size);
  1246. if (orig_iov_len >= *rx_marker) {
  1247. iov[iov_count].iov_len = *rx_marker;
  1248. iov[iov_count++].iov_base =
  1249. (iov_record[orig_iov_loc].iov_base +
  1250. per_iov_bytes);
  1251. iov[iov_count].iov_len = (MARKER_SIZE / 2);
  1252. iov[iov_count++].iov_base =
  1253. &rx_marker_val[rx_marker_iov++];
  1254. iov[iov_count].iov_len = (MARKER_SIZE / 2);
  1255. iov[iov_count++].iov_base =
  1256. &rx_marker_val[rx_marker_iov++];
  1257. old_rx_marker = *rx_marker;
  1258. /*
  1259. * OFMarkInt is in 32-bit words.
  1260. */
  1261. *rx_marker = (conn->conn_ops->OFMarkInt * 4);
  1262. size -= old_rx_marker;
  1263. orig_iov_len -= old_rx_marker;
  1264. per_iov_bytes += old_rx_marker;
  1265. pr_debug("rx_data: #3 new_rx_marker"
  1266. " %u, size %u\n", *rx_marker, size);
  1267. } else {
  1268. iov[iov_count].iov_len = orig_iov_len;
  1269. iov[iov_count++].iov_base =
  1270. (iov_record[orig_iov_loc].iov_base +
  1271. per_iov_bytes);
  1272. per_iov_bytes = 0;
  1273. *rx_marker -= orig_iov_len;
  1274. size -= orig_iov_len;
  1275. if (size)
  1276. orig_iov_len =
  1277. iov_record[++orig_iov_loc].iov_len;
  1278. pr_debug("rx_data: #4 new_rx_marker"
  1279. " %u, size %u\n", *rx_marker, size);
  1280. }
  1281. }
  1282. data += (rx_marker_iov * (MARKER_SIZE / 2));
  1283. iov_p = &iov[0];
  1284. iov_len = iov_count;
  1285. if (iov_count > count->ss_iov_count) {
  1286. pr_err("iov_count: %d, count->ss_iov_count:"
  1287. " %d\n", iov_count, count->ss_iov_count);
  1288. return -1;
  1289. }
  1290. if (rx_marker_iov > count->ss_marker_count) {
  1291. pr_err("rx_marker_iov: %d, count->ss_marker"
  1292. "_count: %d\n", rx_marker_iov,
  1293. count->ss_marker_count);
  1294. return -1;
  1295. }
  1296. } else {
  1297. iov_p = count->iov;
  1298. iov_len = count->iov_count;
  1299. }
  1300. while (total_rx < data) {
  1301. rx_loop = kernel_recvmsg(conn->sock, &msg, iov_p, iov_len,
  1302. (data - total_rx), MSG_WAITALL);
  1303. if (rx_loop <= 0) {
  1304. pr_debug("rx_loop: %d total_rx: %d\n",
  1305. rx_loop, total_rx);
  1306. return rx_loop;
  1307. }
  1308. total_rx += rx_loop;
  1309. pr_debug("rx_loop: %d, total_rx: %d, data: %d\n",
  1310. rx_loop, total_rx, data);
  1311. }
  1312. if (count->sync_and_steering) {
  1313. int j;
  1314. for (j = 0; j < rx_marker_iov; j++) {
  1315. pr_debug("rx_data: #5 j: %d, offset: %d\n",
  1316. j, rx_marker_val[j]);
  1317. conn->of_marker_offset = rx_marker_val[j];
  1318. }
  1319. total_rx -= (rx_marker_iov * (MARKER_SIZE / 2));
  1320. }
  1321. return total_rx;
  1322. }
  1323. static int iscsit_do_tx_data(
  1324. struct iscsi_conn *conn,
  1325. struct iscsi_data_count *count)
  1326. {
  1327. int data = count->data_length, total_tx = 0, tx_loop = 0, iov_len;
  1328. u32 tx_marker_val[count->ss_marker_count], tx_marker_iov = 0;
  1329. struct kvec iov[count->ss_iov_count], *iov_p;
  1330. struct msghdr msg;
  1331. if (!conn || !conn->sock || !conn->conn_ops)
  1332. return -1;
  1333. if (data <= 0) {
  1334. pr_err("Data length is: %d\n", data);
  1335. return -1;
  1336. }
  1337. memset(&msg, 0, sizeof(struct msghdr));
  1338. if (count->sync_and_steering) {
  1339. int size = 0;
  1340. u32 i, orig_iov_count = 0;
  1341. u32 orig_iov_len = 0, orig_iov_loc = 0;
  1342. u32 iov_count = 0, per_iov_bytes = 0;
  1343. u32 *tx_marker, old_tx_marker = 0;
  1344. struct kvec *iov_record;
  1345. memset(&tx_marker_val, 0,
  1346. count->ss_marker_count * sizeof(u32));
  1347. memset(&iov, 0, count->ss_iov_count * sizeof(struct kvec));
  1348. iov_record = count->iov;
  1349. orig_iov_count = count->iov_count;
  1350. tx_marker = &conn->if_marker;
  1351. i = 0;
  1352. size = data;
  1353. orig_iov_len = iov_record[orig_iov_loc].iov_len;
  1354. while (size > 0) {
  1355. pr_debug("tx_data: #1 orig_iov_len %u,"
  1356. " orig_iov_loc %u\n", orig_iov_len, orig_iov_loc);
  1357. pr_debug("tx_data: #2 tx_marker %u, size"
  1358. " %u\n", *tx_marker, size);
  1359. if (orig_iov_len >= *tx_marker) {
  1360. iov[iov_count].iov_len = *tx_marker;
  1361. iov[iov_count++].iov_base =
  1362. (iov_record[orig_iov_loc].iov_base +
  1363. per_iov_bytes);
  1364. tx_marker_val[tx_marker_iov] =
  1365. (size - *tx_marker);
  1366. iov[iov_count].iov_len = (MARKER_SIZE / 2);
  1367. iov[iov_count++].iov_base =
  1368. &tx_marker_val[tx_marker_iov++];
  1369. iov[iov_count].iov_len = (MARKER_SIZE / 2);
  1370. iov[iov_count++].iov_base =
  1371. &tx_marker_val[tx_marker_iov++];
  1372. old_tx_marker = *tx_marker;
  1373. /*
  1374. * IFMarkInt is in 32-bit words.
  1375. */
  1376. *tx_marker = (conn->conn_ops->IFMarkInt * 4);
  1377. size -= old_tx_marker;
  1378. orig_iov_len -= old_tx_marker;
  1379. per_iov_bytes += old_tx_marker;
  1380. pr_debug("tx_data: #3 new_tx_marker"
  1381. " %u, size %u\n", *tx_marker, size);
  1382. pr_debug("tx_data: #4 offset %u\n",
  1383. tx_marker_val[tx_marker_iov-1]);
  1384. } else {
  1385. iov[iov_count].iov_len = orig_iov_len;
  1386. iov[iov_count++].iov_base
  1387. = (iov_record[orig_iov_loc].iov_base +
  1388. per_iov_bytes);
  1389. per_iov_bytes = 0;
  1390. *tx_marker -= orig_iov_len;
  1391. size -= orig_iov_len;
  1392. if (size)
  1393. orig_iov_len =
  1394. iov_record[++orig_iov_loc].iov_len;
  1395. pr_debug("tx_data: #5 new_tx_marker"
  1396. " %u, size %u\n", *tx_marker, size);
  1397. }
  1398. }
  1399. data += (tx_marker_iov * (MARKER_SIZE / 2));
  1400. iov_p = &iov[0];
  1401. iov_len = iov_count;
  1402. if (iov_count > count->ss_iov_count) {
  1403. pr_err("iov_count: %d, count->ss_iov_count:"
  1404. " %d\n", iov_count, count->ss_iov_count);
  1405. return -1;
  1406. }
  1407. if (tx_marker_iov > count->ss_marker_count) {
  1408. pr_err("tx_marker_iov: %d, count->ss_marker"
  1409. "_count: %d\n", tx_marker_iov,
  1410. count->ss_marker_count);
  1411. return -1;
  1412. }
  1413. } else {
  1414. iov_p = count->iov;
  1415. iov_len = count->iov_count;
  1416. }
  1417. while (total_tx < data) {
  1418. tx_loop = kernel_sendmsg(conn->sock, &msg, iov_p, iov_len,
  1419. (data - total_tx));
  1420. if (tx_loop <= 0) {
  1421. pr_debug("tx_loop: %d total_tx %d\n",
  1422. tx_loop, total_tx);
  1423. return tx_loop;
  1424. }
  1425. total_tx += tx_loop;
  1426. pr_debug("tx_loop: %d, total_tx: %d, data: %d\n",
  1427. tx_loop, total_tx, data);
  1428. }
  1429. if (count->sync_and_steering)
  1430. total_tx -= (tx_marker_iov * (MARKER_SIZE / 2));
  1431. return total_tx;
  1432. }
  1433. int rx_data(
  1434. struct iscsi_conn *conn,
  1435. struct kvec *iov,
  1436. int iov_count,
  1437. int data)
  1438. {
  1439. struct iscsi_data_count c;
  1440. if (!conn || !conn->sock || !conn->conn_ops)
  1441. return -1;
  1442. memset(&c, 0, sizeof(struct iscsi_data_count));
  1443. c.iov = iov;
  1444. c.iov_count = iov_count;
  1445. c.data_length = data;
  1446. c.type = ISCSI_RX_DATA;
  1447. if (conn->conn_ops->OFMarker &&
  1448. (conn->conn_state >= TARG_CONN_STATE_LOGGED_IN)) {
  1449. if (iscsit_determine_sync_and_steering_counts(conn, &c) < 0)
  1450. return -1;
  1451. }
  1452. return iscsit_do_rx_data(conn, &c);
  1453. }
  1454. int tx_data(
  1455. struct iscsi_conn *conn,
  1456. struct kvec *iov,
  1457. int iov_count,
  1458. int data)
  1459. {
  1460. struct iscsi_data_count c;
  1461. if (!conn || !conn->sock || !conn->conn_ops)
  1462. return -1;
  1463. memset(&c, 0, sizeof(struct iscsi_data_count));
  1464. c.iov = iov;
  1465. c.iov_count = iov_count;
  1466. c.data_length = data;
  1467. c.type = ISCSI_TX_DATA;
  1468. if (conn->conn_ops->IFMarker &&
  1469. (conn->conn_state >= TARG_CONN_STATE_LOGGED_IN)) {
  1470. if (iscsit_determine_sync_and_steering_counts(conn, &c) < 0)
  1471. return -1;
  1472. }
  1473. return iscsit_do_tx_data(conn, &c);
  1474. }
  1475. void iscsit_collect_login_stats(
  1476. struct iscsi_conn *conn,
  1477. u8 status_class,
  1478. u8 status_detail)
  1479. {
  1480. struct iscsi_param *intrname = NULL;
  1481. struct iscsi_tiqn *tiqn;
  1482. struct iscsi_login_stats *ls;
  1483. tiqn = iscsit_snmp_get_tiqn(conn);
  1484. if (!tiqn)
  1485. return;
  1486. ls = &tiqn->login_stats;
  1487. spin_lock(&ls->lock);
  1488. if (!strcmp(conn->login_ip, ls->last_intr_fail_ip_addr) &&
  1489. ((get_jiffies_64() - ls->last_fail_time) < 10)) {
  1490. /* We already have the failure info for this login */
  1491. spin_unlock(&ls->lock);
  1492. return;
  1493. }
  1494. if (status_class == ISCSI_STATUS_CLS_SUCCESS)
  1495. ls->accepts++;
  1496. else if (status_class == ISCSI_STATUS_CLS_REDIRECT) {
  1497. ls->redirects++;
  1498. ls->last_fail_type = ISCSI_LOGIN_FAIL_REDIRECT;
  1499. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1500. (status_detail == ISCSI_LOGIN_STATUS_AUTH_FAILED)) {
  1501. ls->authenticate_fails++;
  1502. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHENTICATE;
  1503. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1504. (status_detail == ISCSI_LOGIN_STATUS_TGT_FORBIDDEN)) {
  1505. ls->authorize_fails++;
  1506. ls->last_fail_type = ISCSI_LOGIN_FAIL_AUTHORIZE;
  1507. } else if ((status_class == ISCSI_STATUS_CLS_INITIATOR_ERR) &&
  1508. (status_detail == ISCSI_LOGIN_STATUS_INIT_ERR)) {
  1509. ls->negotiate_fails++;
  1510. ls->last_fail_type = ISCSI_LOGIN_FAIL_NEGOTIATE;
  1511. } else {
  1512. ls->other_fails++;
  1513. ls->last_fail_type = ISCSI_LOGIN_FAIL_OTHER;
  1514. }
  1515. /* Save initiator name, ip address and time, if it is a failed login */
  1516. if (status_class != ISCSI_STATUS_CLS_SUCCESS) {
  1517. if (conn->param_list)
  1518. intrname = iscsi_find_param_from_key(INITIATORNAME,
  1519. conn->param_list);
  1520. strcpy(ls->last_intr_fail_name,
  1521. (intrname ? intrname->value : "Unknown"));
  1522. ls->last_intr_fail_ip_family = conn->sock->sk->sk_family;
  1523. snprintf(ls->last_intr_fail_ip_addr, IPV6_ADDRESS_SPACE,
  1524. "%s", conn->login_ip);
  1525. ls->last_fail_time = get_jiffies_64();
  1526. }
  1527. spin_unlock(&ls->lock);
  1528. }
  1529. struct iscsi_tiqn *iscsit_snmp_get_tiqn(struct iscsi_conn *conn)
  1530. {
  1531. struct iscsi_portal_group *tpg;
  1532. if (!conn || !conn->sess)
  1533. return NULL;
  1534. tpg = conn->sess->tpg;
  1535. if (!tpg)
  1536. return NULL;
  1537. if (!tpg->tpg_tiqn)
  1538. return NULL;
  1539. return tpg->tpg_tiqn;
  1540. }