iscsi_target_util.c 41 KB

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