iscsi_target_util.c 36 KB

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