libiscsi_tcp.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * iSCSI over TCP/IP Data-Path lib
  4. *
  5. * Copyright (C) 2004 Dmitry Yusupov
  6. * Copyright (C) 2004 Alex Aizman
  7. * Copyright (C) 2005 - 2006 Mike Christie
  8. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  9. * maintained by open-iscsi@googlegroups.com
  10. *
  11. * Credits:
  12. * Christoph Hellwig
  13. * FUJITA Tomonori
  14. * Arne Redlich
  15. * Zhenyu Wang
  16. */
  17. #include <linux/crc32c.h>
  18. #include <linux/types.h>
  19. #include <linux/list.h>
  20. #include <linux/inet.h>
  21. #include <linux/slab.h>
  22. #include <linux/file.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/delay.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/module.h>
  28. #include <net/tcp.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi.h>
  33. #include <scsi/scsi_transport_iscsi.h>
  34. #include <trace/events/iscsi.h>
  35. #include "iscsi_tcp.h"
  36. MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, "
  37. "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
  38. "Alex Aizman <itn780@yahoo.com>");
  39. MODULE_DESCRIPTION("iSCSI/TCP data-path");
  40. MODULE_LICENSE("GPL");
  41. static int iscsi_dbg_libtcp;
  42. module_param_named(debug_libiscsi_tcp, iscsi_dbg_libtcp, int,
  43. S_IRUGO | S_IWUSR);
  44. MODULE_PARM_DESC(debug_libiscsi_tcp, "Turn on debugging for libiscsi_tcp "
  45. "module. Set to 1 to turn on, and zero to turn off. Default "
  46. "is off.");
  47. #define ISCSI_DBG_TCP(_conn, dbg_fmt, arg...) \
  48. do { \
  49. if (iscsi_dbg_libtcp) \
  50. iscsi_conn_printk(KERN_INFO, _conn, \
  51. "%s " dbg_fmt, \
  52. __func__, ##arg); \
  53. iscsi_dbg_trace(trace_iscsi_dbg_tcp, \
  54. &(_conn)->cls_conn->dev, \
  55. "%s " dbg_fmt, __func__, ##arg);\
  56. } while (0);
  57. static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  58. struct iscsi_segment *segment);
  59. /*
  60. * Scatterlist handling: inside the iscsi_segment, we
  61. * remember an index into the scatterlist, and set data/size
  62. * to the current scatterlist entry. For highmem pages, we
  63. * kmap as needed.
  64. *
  65. * Note that the page is unmapped when we return from
  66. * TCP's data_ready handler, so we may end up mapping and
  67. * unmapping the same page repeatedly. The whole reason
  68. * for this is that we shouldn't keep the page mapped
  69. * outside the softirq.
  70. */
  71. /**
  72. * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
  73. * @segment: the buffer object
  74. * @sg: scatterlist
  75. * @offset: byte offset into that sg entry
  76. *
  77. * This function sets up the segment so that subsequent
  78. * data is copied to the indicated sg entry, at the given
  79. * offset.
  80. */
  81. static inline void
  82. iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
  83. struct scatterlist *sg, unsigned int offset)
  84. {
  85. segment->sg = sg;
  86. segment->sg_offset = offset;
  87. segment->size = min(sg->length - offset,
  88. segment->total_size - segment->total_copied);
  89. segment->data = NULL;
  90. }
  91. /**
  92. * iscsi_tcp_segment_map - map the current S/G page
  93. * @segment: iscsi_segment
  94. * @recv: 1 if called from recv path
  95. *
  96. * We only need to possibly kmap data if scatter lists are being used,
  97. * because the iscsi passthrough and internal IO paths will never use high
  98. * mem pages.
  99. */
  100. static void iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
  101. {
  102. struct scatterlist *sg;
  103. if (segment->data != NULL || !segment->sg)
  104. return;
  105. sg = segment->sg;
  106. BUG_ON(segment->sg_mapped);
  107. BUG_ON(sg->length == 0);
  108. /*
  109. * We always map for the recv path.
  110. *
  111. * If the page count is greater than one it is ok to send
  112. * to the network layer's zero copy send path. If not we
  113. * have to go the slow sendmsg path.
  114. *
  115. * Same goes for slab pages: skb_can_coalesce() allows
  116. * coalescing neighboring slab objects into a single frag which
  117. * triggers one of hardened usercopy checks.
  118. */
  119. if (!recv && sendpage_ok(sg_page(sg)))
  120. return;
  121. if (recv) {
  122. segment->atomic_mapped = true;
  123. segment->sg_mapped = kmap_atomic(sg_page(sg));
  124. } else {
  125. segment->atomic_mapped = false;
  126. /* the xmit path can sleep with the page mapped so use kmap */
  127. segment->sg_mapped = kmap(sg_page(sg));
  128. }
  129. segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
  130. }
  131. void iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
  132. {
  133. if (segment->sg_mapped) {
  134. if (segment->atomic_mapped)
  135. kunmap_atomic(segment->sg_mapped);
  136. else
  137. kunmap(sg_page(segment->sg));
  138. segment->sg_mapped = NULL;
  139. segment->data = NULL;
  140. }
  141. }
  142. EXPORT_SYMBOL_GPL(iscsi_tcp_segment_unmap);
  143. /*
  144. * Splice the digest buffer into the buffer
  145. */
  146. static inline void
  147. iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
  148. {
  149. segment->data = digest;
  150. segment->digest_len = ISCSI_DIGEST_SIZE;
  151. segment->total_size += ISCSI_DIGEST_SIZE;
  152. segment->size = ISCSI_DIGEST_SIZE;
  153. segment->copied = 0;
  154. segment->sg = NULL;
  155. segment->crcp = NULL;
  156. }
  157. /**
  158. * iscsi_tcp_segment_done - check whether the segment is complete
  159. * @tcp_conn: iscsi tcp connection
  160. * @segment: iscsi segment to check
  161. * @recv: set to one of this is called from the recv path
  162. * @copied: number of bytes copied
  163. *
  164. * Check if we're done receiving this segment. If the receive
  165. * buffer is full but we expect more data, move on to the
  166. * next entry in the scatterlist.
  167. *
  168. * If the amount of data we received isn't a multiple of 4,
  169. * we will transparently receive the pad bytes, too.
  170. *
  171. * This function must be re-entrant.
  172. */
  173. int iscsi_tcp_segment_done(struct iscsi_tcp_conn *tcp_conn,
  174. struct iscsi_segment *segment, int recv,
  175. unsigned copied)
  176. {
  177. unsigned int pad;
  178. ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copied %u %u size %u %s\n",
  179. segment->copied, copied, segment->size,
  180. recv ? "recv" : "xmit");
  181. if (segment->crcp && copied) {
  182. if (segment->data) {
  183. *segment->crcp = crc32c(*segment->crcp,
  184. segment->data + segment->copied,
  185. copied);
  186. } else {
  187. const void *data;
  188. data = kmap_local_page(sg_page(segment->sg));
  189. *segment->crcp = crc32c(*segment->crcp,
  190. data + segment->copied +
  191. segment->sg_offset +
  192. segment->sg->offset,
  193. copied);
  194. kunmap_local(data);
  195. }
  196. }
  197. segment->copied += copied;
  198. if (segment->copied < segment->size) {
  199. iscsi_tcp_segment_map(segment, recv);
  200. return 0;
  201. }
  202. segment->total_copied += segment->copied;
  203. segment->copied = 0;
  204. segment->size = 0;
  205. /* Unmap the current scatterlist page, if there is one. */
  206. iscsi_tcp_segment_unmap(segment);
  207. /* Do we have more scatterlist entries? */
  208. ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "total copied %u total size %u\n",
  209. segment->total_copied, segment->total_size);
  210. if (segment->total_copied < segment->total_size) {
  211. /* Proceed to the next entry in the scatterlist. */
  212. iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
  213. 0);
  214. iscsi_tcp_segment_map(segment, recv);
  215. BUG_ON(segment->size == 0);
  216. return 0;
  217. }
  218. /* Do we need to handle padding? */
  219. if (!(tcp_conn->iscsi_conn->session->tt->caps & CAP_PADDING_OFFLOAD)) {
  220. pad = iscsi_padding(segment->total_copied);
  221. if (pad != 0) {
  222. ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
  223. "consume %d pad bytes\n", pad);
  224. segment->total_size += pad;
  225. segment->size = pad;
  226. segment->data = segment->padbuf;
  227. return 0;
  228. }
  229. }
  230. /*
  231. * Set us up for transferring the data digest. hdr digest
  232. * is completely handled in hdr done function.
  233. */
  234. if (segment->crcp) {
  235. put_unaligned_le32(~*segment->crcp, segment->digest);
  236. iscsi_tcp_segment_splice_digest(segment,
  237. recv ? segment->recv_digest : segment->digest);
  238. return 0;
  239. }
  240. return 1;
  241. }
  242. EXPORT_SYMBOL_GPL(iscsi_tcp_segment_done);
  243. /**
  244. * iscsi_tcp_segment_recv - copy data to segment
  245. * @tcp_conn: the iSCSI TCP connection
  246. * @segment: the buffer to copy to
  247. * @ptr: data pointer
  248. * @len: amount of data available
  249. *
  250. * This function copies up to @len bytes to the
  251. * given buffer, and returns the number of bytes
  252. * consumed, which can actually be less than @len.
  253. *
  254. * If CRC is enabled, the function will update the CRC while copying.
  255. * Combining these two operations doesn't buy us a lot (yet),
  256. * but in the future we could implement combined copy+crc,
  257. * just way we do for network layer checksums.
  258. */
  259. static int
  260. iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
  261. struct iscsi_segment *segment, const void *ptr,
  262. unsigned int len)
  263. {
  264. unsigned int copy = 0, copied = 0;
  265. while (!iscsi_tcp_segment_done(tcp_conn, segment, 1, copy)) {
  266. if (copied == len) {
  267. ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
  268. "copied %d bytes\n", len);
  269. break;
  270. }
  271. copy = min(len - copied, segment->size - segment->copied);
  272. ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "copying %d\n", copy);
  273. memcpy(segment->data + segment->copied, ptr + copied, copy);
  274. copied += copy;
  275. }
  276. return copied;
  277. }
  278. inline void
  279. iscsi_tcp_dgst_header(const void *hdr, size_t hdrlen,
  280. unsigned char digest[ISCSI_DIGEST_SIZE])
  281. {
  282. put_unaligned_le32(~crc32c(~0, hdr, hdrlen), digest);
  283. }
  284. EXPORT_SYMBOL_GPL(iscsi_tcp_dgst_header);
  285. static inline int
  286. iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
  287. struct iscsi_segment *segment)
  288. {
  289. if (!segment->digest_len)
  290. return 1;
  291. if (memcmp(segment->recv_digest, segment->digest,
  292. segment->digest_len)) {
  293. ISCSI_DBG_TCP(tcp_conn->iscsi_conn, "digest mismatch\n");
  294. return 0;
  295. }
  296. return 1;
  297. }
  298. /*
  299. * Helper function to set up segment buffer
  300. */
  301. static inline void
  302. __iscsi_segment_init(struct iscsi_segment *segment, size_t size,
  303. iscsi_segment_done_fn_t *done, u32 *crcp)
  304. {
  305. memset(segment, 0, sizeof(*segment));
  306. segment->total_size = size;
  307. segment->done = done;
  308. if (crcp) {
  309. segment->crcp = crcp;
  310. *crcp = ~0;
  311. }
  312. }
  313. inline void
  314. iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
  315. size_t size, iscsi_segment_done_fn_t *done, u32 *crcp)
  316. {
  317. __iscsi_segment_init(segment, size, done, crcp);
  318. segment->data = data;
  319. segment->size = size;
  320. }
  321. EXPORT_SYMBOL_GPL(iscsi_segment_init_linear);
  322. inline int
  323. iscsi_segment_seek_sg(struct iscsi_segment *segment,
  324. struct scatterlist *sg_list, unsigned int sg_count,
  325. unsigned int offset, size_t size,
  326. iscsi_segment_done_fn_t *done, u32 *crcp)
  327. {
  328. struct scatterlist *sg;
  329. unsigned int i;
  330. __iscsi_segment_init(segment, size, done, crcp);
  331. for_each_sg(sg_list, sg, sg_count, i) {
  332. if (offset < sg->length) {
  333. iscsi_tcp_segment_init_sg(segment, sg, offset);
  334. return 0;
  335. }
  336. offset -= sg->length;
  337. }
  338. return ISCSI_ERR_DATA_OFFSET;
  339. }
  340. EXPORT_SYMBOL_GPL(iscsi_segment_seek_sg);
  341. /**
  342. * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
  343. * @tcp_conn: iscsi connection to prep for
  344. *
  345. * This function always passes NULL for the crcp argument, because when this
  346. * function is called we do not yet know the final size of the header and want
  347. * to delay the digest processing until we know that.
  348. */
  349. void iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
  350. {
  351. ISCSI_DBG_TCP(tcp_conn->iscsi_conn,
  352. "(%s)\n", tcp_conn->iscsi_conn->hdrdgst_en ?
  353. "digest enabled" : "digest disabled");
  354. iscsi_segment_init_linear(&tcp_conn->in.segment,
  355. tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
  356. iscsi_tcp_hdr_recv_done, NULL);
  357. }
  358. EXPORT_SYMBOL_GPL(iscsi_tcp_hdr_recv_prep);
  359. /*
  360. * Handle incoming reply to any other type of command
  361. */
  362. static int
  363. iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
  364. struct iscsi_segment *segment)
  365. {
  366. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  367. int rc = 0;
  368. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  369. return ISCSI_ERR_DATA_DGST;
  370. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
  371. conn->data, tcp_conn->in.datalen);
  372. if (rc)
  373. return rc;
  374. iscsi_tcp_hdr_recv_prep(tcp_conn);
  375. return 0;
  376. }
  377. static void
  378. iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
  379. {
  380. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  381. u32 *rx_crcp = NULL;
  382. if (conn->datadgst_en &&
  383. !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
  384. rx_crcp = tcp_conn->rx_crcp;
  385. iscsi_segment_init_linear(&tcp_conn->in.segment,
  386. conn->data, tcp_conn->in.datalen,
  387. iscsi_tcp_data_recv_done, rx_crcp);
  388. }
  389. /**
  390. * iscsi_tcp_cleanup_task - free tcp_task resources
  391. * @task: iscsi task
  392. *
  393. * must be called with session back_lock
  394. */
  395. void iscsi_tcp_cleanup_task(struct iscsi_task *task)
  396. {
  397. struct iscsi_tcp_task *tcp_task = task->dd_data;
  398. struct iscsi_r2t_info *r2t;
  399. /* nothing to do for mgmt */
  400. if (!task->sc)
  401. return;
  402. spin_lock_bh(&tcp_task->queue2pool);
  403. /* flush task's r2t queues */
  404. while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
  405. kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
  406. sizeof(void*));
  407. ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
  408. }
  409. r2t = tcp_task->r2t;
  410. if (r2t != NULL) {
  411. kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
  412. sizeof(void*));
  413. tcp_task->r2t = NULL;
  414. }
  415. spin_unlock_bh(&tcp_task->queue2pool);
  416. }
  417. EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
  418. /**
  419. * iscsi_tcp_data_in - SCSI Data-In Response processing
  420. * @conn: iscsi connection
  421. * @task: scsi command task
  422. */
  423. static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
  424. {
  425. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  426. struct iscsi_tcp_task *tcp_task = task->dd_data;
  427. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
  428. int datasn = be32_to_cpu(rhdr->datasn);
  429. unsigned total_in_length = task->sc->sdb.length;
  430. /*
  431. * lib iscsi will update this in the completion handling if there
  432. * is status.
  433. */
  434. if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
  435. iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
  436. if (tcp_conn->in.datalen == 0)
  437. return 0;
  438. if (tcp_task->exp_datasn != datasn) {
  439. ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
  440. "\n", tcp_task->exp_datasn, datasn);
  441. return ISCSI_ERR_DATASN;
  442. }
  443. tcp_task->exp_datasn++;
  444. tcp_task->data_offset = be32_to_cpu(rhdr->offset);
  445. if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
  446. ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
  447. "total_length_in(%d)\n", tcp_task->data_offset,
  448. tcp_conn->in.datalen, total_in_length);
  449. return ISCSI_ERR_DATA_OFFSET;
  450. }
  451. conn->datain_pdus_cnt++;
  452. return 0;
  453. }
  454. /**
  455. * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
  456. * @conn: iscsi connection
  457. * @hdr: PDU header
  458. */
  459. static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  460. {
  461. struct iscsi_session *session = conn->session;
  462. struct iscsi_tcp_task *tcp_task;
  463. struct iscsi_tcp_conn *tcp_conn;
  464. struct iscsi_r2t_rsp *rhdr;
  465. struct iscsi_r2t_info *r2t;
  466. struct iscsi_task *task;
  467. u32 data_length;
  468. u32 data_offset;
  469. int r2tsn;
  470. int rc;
  471. spin_lock(&session->back_lock);
  472. task = iscsi_itt_to_ctask(conn, hdr->itt);
  473. if (!task) {
  474. spin_unlock(&session->back_lock);
  475. return ISCSI_ERR_BAD_ITT;
  476. } else if (task->sc->sc_data_direction != DMA_TO_DEVICE) {
  477. spin_unlock(&session->back_lock);
  478. return ISCSI_ERR_PROTO;
  479. }
  480. /*
  481. * A bad target might complete the cmd before we have handled R2Ts
  482. * so get a ref to the task that will be dropped in the xmit path.
  483. */
  484. if (task->state != ISCSI_TASK_RUNNING) {
  485. spin_unlock(&session->back_lock);
  486. /* Let the path that got the early rsp complete it */
  487. return 0;
  488. }
  489. task->last_xfer = jiffies;
  490. if (!iscsi_get_task(task)) {
  491. spin_unlock(&session->back_lock);
  492. /* Let the path that got the early rsp complete it */
  493. return 0;
  494. }
  495. tcp_conn = conn->dd_data;
  496. rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
  497. /* fill-in new R2T associated with the task */
  498. iscsi_update_cmdsn(session, (struct iscsi_nopin *)rhdr);
  499. spin_unlock(&session->back_lock);
  500. if (tcp_conn->in.datalen) {
  501. iscsi_conn_printk(KERN_ERR, conn,
  502. "invalid R2t with datalen %d\n",
  503. tcp_conn->in.datalen);
  504. rc = ISCSI_ERR_DATALEN;
  505. goto put_task;
  506. }
  507. tcp_task = task->dd_data;
  508. r2tsn = be32_to_cpu(rhdr->r2tsn);
  509. if (tcp_task->exp_datasn != r2tsn){
  510. ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
  511. tcp_task->exp_datasn, r2tsn);
  512. rc = ISCSI_ERR_R2TSN;
  513. goto put_task;
  514. }
  515. if (session->state != ISCSI_STATE_LOGGED_IN) {
  516. iscsi_conn_printk(KERN_INFO, conn,
  517. "dropping R2T itt %d in recovery.\n",
  518. task->itt);
  519. rc = 0;
  520. goto put_task;
  521. }
  522. data_length = be32_to_cpu(rhdr->data_length);
  523. if (data_length == 0) {
  524. iscsi_conn_printk(KERN_ERR, conn,
  525. "invalid R2T with zero data len\n");
  526. rc = ISCSI_ERR_DATALEN;
  527. goto put_task;
  528. }
  529. if (data_length > session->max_burst)
  530. ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
  531. "burst %u. Attempting to execute request.\n",
  532. data_length, session->max_burst);
  533. data_offset = be32_to_cpu(rhdr->data_offset);
  534. if (data_offset + data_length > task->sc->sdb.length) {
  535. iscsi_conn_printk(KERN_ERR, conn,
  536. "invalid R2T with data len %u at offset %u "
  537. "and total length %d\n", data_length,
  538. data_offset, task->sc->sdb.length);
  539. rc = ISCSI_ERR_DATALEN;
  540. goto put_task;
  541. }
  542. spin_lock(&tcp_task->pool2queue);
  543. rc = kfifo_out(&tcp_task->r2tpool.queue, (void *)&r2t, sizeof(void *));
  544. if (!rc) {
  545. iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
  546. "Target has sent more R2Ts than it "
  547. "negotiated for or driver has leaked.\n");
  548. spin_unlock(&tcp_task->pool2queue);
  549. rc = ISCSI_ERR_PROTO;
  550. goto put_task;
  551. }
  552. r2t->exp_statsn = rhdr->statsn;
  553. r2t->data_length = data_length;
  554. r2t->data_offset = data_offset;
  555. r2t->ttt = rhdr->ttt; /* no flip */
  556. r2t->datasn = 0;
  557. r2t->sent = 0;
  558. tcp_task->exp_datasn = r2tsn + 1;
  559. kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
  560. conn->r2t_pdus_cnt++;
  561. spin_unlock(&tcp_task->pool2queue);
  562. iscsi_requeue_task(task);
  563. return 0;
  564. put_task:
  565. iscsi_put_task(task);
  566. return rc;
  567. }
  568. /*
  569. * Handle incoming reply to DataIn command
  570. */
  571. static int
  572. iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
  573. struct iscsi_segment *segment)
  574. {
  575. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  576. struct iscsi_hdr *hdr = tcp_conn->in.hdr;
  577. int rc;
  578. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  579. return ISCSI_ERR_DATA_DGST;
  580. /* check for non-exceptional status */
  581. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  582. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
  583. if (rc)
  584. return rc;
  585. }
  586. iscsi_tcp_hdr_recv_prep(tcp_conn);
  587. return 0;
  588. }
  589. /**
  590. * iscsi_tcp_hdr_dissect - process PDU header
  591. * @conn: iSCSI connection
  592. * @hdr: PDU header
  593. *
  594. * This function analyzes the header of the PDU received,
  595. * and performs several sanity checks. If the PDU is accompanied
  596. * by data, the receive buffer is set up to copy the incoming data
  597. * to the correct location.
  598. */
  599. static int
  600. iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  601. {
  602. int rc = 0, opcode, ahslen;
  603. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  604. struct iscsi_task *task;
  605. /* verify PDU length */
  606. tcp_conn->in.datalen = ntoh24(hdr->dlength);
  607. if (tcp_conn->in.datalen > conn->max_recv_dlength) {
  608. iscsi_conn_printk(KERN_ERR, conn,
  609. "iscsi_tcp: datalen %d > %d\n",
  610. tcp_conn->in.datalen, conn->max_recv_dlength);
  611. return ISCSI_ERR_DATALEN;
  612. }
  613. /* Additional header segments. So far, we don't
  614. * process additional headers.
  615. */
  616. ahslen = hdr->hlength << 2;
  617. opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  618. /* verify itt (itt encoding: age+cid+itt) */
  619. rc = iscsi_verify_itt(conn, hdr->itt);
  620. if (rc)
  621. return rc;
  622. ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
  623. opcode, ahslen, tcp_conn->in.datalen);
  624. switch(opcode) {
  625. case ISCSI_OP_SCSI_DATA_IN:
  626. spin_lock(&conn->session->back_lock);
  627. task = iscsi_itt_to_ctask(conn, hdr->itt);
  628. if (!task)
  629. rc = ISCSI_ERR_BAD_ITT;
  630. else
  631. rc = iscsi_tcp_data_in(conn, task);
  632. if (rc) {
  633. spin_unlock(&conn->session->back_lock);
  634. break;
  635. }
  636. if (tcp_conn->in.datalen) {
  637. struct iscsi_tcp_task *tcp_task = task->dd_data;
  638. u32 *rx_crcp = NULL;
  639. struct scsi_data_buffer *sdb = &task->sc->sdb;
  640. /*
  641. * Setup copy of Data-In into the struct scsi_cmnd
  642. * Scatterlist case:
  643. * We set up the iscsi_segment to point to the next
  644. * scatterlist entry to copy to. As we go along,
  645. * we move on to the next scatterlist entry and
  646. * update the digest per-entry.
  647. */
  648. if (conn->datadgst_en &&
  649. !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
  650. rx_crcp = tcp_conn->rx_crcp;
  651. ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
  652. "offset=%d, datalen=%d)\n",
  653. tcp_task->data_offset,
  654. tcp_conn->in.datalen);
  655. task->last_xfer = jiffies;
  656. rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
  657. sdb->table.sgl,
  658. sdb->table.nents,
  659. tcp_task->data_offset,
  660. tcp_conn->in.datalen,
  661. iscsi_tcp_process_data_in,
  662. rx_crcp);
  663. spin_unlock(&conn->session->back_lock);
  664. return rc;
  665. }
  666. rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
  667. spin_unlock(&conn->session->back_lock);
  668. break;
  669. case ISCSI_OP_SCSI_CMD_RSP:
  670. if (tcp_conn->in.datalen) {
  671. iscsi_tcp_data_recv_prep(tcp_conn);
  672. return 0;
  673. }
  674. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  675. break;
  676. case ISCSI_OP_R2T:
  677. if (ahslen) {
  678. rc = ISCSI_ERR_AHSLEN;
  679. break;
  680. }
  681. rc = iscsi_tcp_r2t_rsp(conn, hdr);
  682. break;
  683. case ISCSI_OP_LOGIN_RSP:
  684. case ISCSI_OP_TEXT_RSP:
  685. case ISCSI_OP_REJECT:
  686. case ISCSI_OP_ASYNC_EVENT:
  687. /*
  688. * It is possible that we could get a PDU with a buffer larger
  689. * than 8K, but there are no targets that currently do this.
  690. * For now we fail until we find a vendor that needs it
  691. */
  692. if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
  693. iscsi_conn_printk(KERN_ERR, conn,
  694. "iscsi_tcp: received buffer of "
  695. "len %u but conn buffer is only %u "
  696. "(opcode %0x)\n",
  697. tcp_conn->in.datalen,
  698. ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
  699. rc = ISCSI_ERR_PROTO;
  700. break;
  701. }
  702. /* If there's data coming in with the response,
  703. * receive it to the connection's buffer.
  704. */
  705. if (tcp_conn->in.datalen) {
  706. iscsi_tcp_data_recv_prep(tcp_conn);
  707. return 0;
  708. }
  709. fallthrough;
  710. case ISCSI_OP_LOGOUT_RSP:
  711. case ISCSI_OP_NOOP_IN:
  712. case ISCSI_OP_SCSI_TMFUNC_RSP:
  713. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  714. break;
  715. default:
  716. rc = ISCSI_ERR_BAD_OPCODE;
  717. break;
  718. }
  719. if (rc == 0) {
  720. /* Anything that comes with data should have
  721. * been handled above. */
  722. if (tcp_conn->in.datalen)
  723. return ISCSI_ERR_PROTO;
  724. iscsi_tcp_hdr_recv_prep(tcp_conn);
  725. }
  726. return rc;
  727. }
  728. /**
  729. * iscsi_tcp_hdr_recv_done - process PDU header
  730. * @tcp_conn: iSCSI TCP connection
  731. * @segment: the buffer segment being processed
  732. *
  733. * This is the callback invoked when the PDU header has
  734. * been received. If the header is followed by additional
  735. * header segments, we go back for more data.
  736. */
  737. static int
  738. iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  739. struct iscsi_segment *segment)
  740. {
  741. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  742. struct iscsi_hdr *hdr;
  743. /* Check if there are additional header segments
  744. * *prior* to computing the digest, because we
  745. * may need to go back to the caller for more.
  746. */
  747. hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
  748. if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
  749. /* Bump the header length - the caller will
  750. * just loop around and get the AHS for us, and
  751. * call again. */
  752. unsigned int ahslen = hdr->hlength << 2;
  753. /* Make sure we don't overflow */
  754. if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
  755. return ISCSI_ERR_AHSLEN;
  756. segment->total_size += ahslen;
  757. segment->size += ahslen;
  758. return 0;
  759. }
  760. /* We're done processing the header. See if we're doing
  761. * header digests; if so, set up the recv_digest buffer
  762. * and go back for more. */
  763. if (conn->hdrdgst_en &&
  764. !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
  765. if (segment->digest_len == 0) {
  766. /*
  767. * Even if we offload the digest processing we
  768. * splice it in so we can increment the skb/segment
  769. * counters in preparation for the data segment.
  770. */
  771. iscsi_tcp_segment_splice_digest(segment,
  772. segment->recv_digest);
  773. return 0;
  774. }
  775. iscsi_tcp_dgst_header(hdr,
  776. segment->total_copied - ISCSI_DIGEST_SIZE,
  777. segment->digest);
  778. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  779. return ISCSI_ERR_HDR_DGST;
  780. }
  781. tcp_conn->in.hdr = hdr;
  782. return iscsi_tcp_hdr_dissect(conn, hdr);
  783. }
  784. /**
  785. * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
  786. * @tcp_conn: iscsi tcp conn
  787. *
  788. * returns non zero if we are currently processing or setup to process
  789. * a header.
  790. */
  791. inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
  792. {
  793. return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
  794. }
  795. EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
  796. /**
  797. * iscsi_tcp_recv_skb - Process skb
  798. * @conn: iscsi connection
  799. * @skb: network buffer with header and/or data segment
  800. * @offset: offset in skb
  801. * @offloaded: bool indicating if transfer was offloaded
  802. * @status: iscsi TCP status result
  803. *
  804. * Will return status of transfer in @status. And will return
  805. * number of bytes copied.
  806. */
  807. int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
  808. unsigned int offset, bool offloaded, int *status)
  809. {
  810. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  811. struct iscsi_segment *segment = &tcp_conn->in.segment;
  812. struct skb_seq_state seq;
  813. unsigned int consumed = 0;
  814. int rc = 0;
  815. ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
  816. /*
  817. * Update for each skb instead of pdu, because over slow networks a
  818. * data_in's data could take a while to read in. We also want to
  819. * account for r2ts.
  820. */
  821. conn->last_recv = jiffies;
  822. if (unlikely(test_bit(ISCSI_CONN_FLAG_SUSPEND_RX, &conn->flags))) {
  823. ISCSI_DBG_TCP(conn, "Rx suspended!\n");
  824. *status = ISCSI_TCP_SUSPENDED;
  825. return 0;
  826. }
  827. if (offloaded) {
  828. segment->total_copied = segment->total_size;
  829. goto segment_done;
  830. }
  831. skb_prepare_seq_read(skb, offset, skb->len, &seq);
  832. while (1) {
  833. unsigned int avail;
  834. const u8 *ptr;
  835. avail = skb_seq_read(consumed, &ptr, &seq);
  836. if (avail == 0) {
  837. ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
  838. consumed);
  839. *status = ISCSI_TCP_SKB_DONE;
  840. goto skb_done;
  841. }
  842. BUG_ON(segment->copied >= segment->size);
  843. ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
  844. avail);
  845. rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
  846. BUG_ON(rc == 0);
  847. consumed += rc;
  848. if (segment->total_copied >= segment->total_size) {
  849. skb_abort_seq_read(&seq);
  850. goto segment_done;
  851. }
  852. }
  853. segment_done:
  854. *status = ISCSI_TCP_SEGMENT_DONE;
  855. ISCSI_DBG_TCP(conn, "segment done\n");
  856. rc = segment->done(tcp_conn, segment);
  857. if (rc != 0) {
  858. *status = ISCSI_TCP_CONN_ERR;
  859. ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
  860. iscsi_conn_failure(conn, rc);
  861. return 0;
  862. }
  863. /* The done() functions sets up the next segment. */
  864. skb_done:
  865. conn->rxdata_octets += consumed;
  866. return consumed;
  867. }
  868. EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
  869. /**
  870. * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
  871. * @task: scsi command task
  872. */
  873. int iscsi_tcp_task_init(struct iscsi_task *task)
  874. {
  875. struct iscsi_tcp_task *tcp_task = task->dd_data;
  876. struct iscsi_conn *conn = task->conn;
  877. struct scsi_cmnd *sc = task->sc;
  878. int err;
  879. if (!sc) {
  880. /*
  881. * mgmt tasks do not have a scatterlist since they come
  882. * in from the iscsi interface.
  883. */
  884. ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
  885. return conn->session->tt->init_pdu(task, 0, task->data_count);
  886. }
  887. BUG_ON(kfifo_len(&tcp_task->r2tqueue));
  888. tcp_task->exp_datasn = 0;
  889. /* Prepare PDU, optionally w/ immediate data */
  890. ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
  891. task->itt, task->imm_count, task->unsol_r2t.data_length);
  892. err = conn->session->tt->init_pdu(task, 0, task->imm_count);
  893. if (err)
  894. return err;
  895. task->imm_count = 0;
  896. return 0;
  897. }
  898. EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
  899. static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
  900. {
  901. struct iscsi_tcp_task *tcp_task = task->dd_data;
  902. struct iscsi_r2t_info *r2t = NULL;
  903. if (iscsi_task_has_unsol_data(task))
  904. r2t = &task->unsol_r2t;
  905. else {
  906. spin_lock_bh(&tcp_task->queue2pool);
  907. if (tcp_task->r2t) {
  908. r2t = tcp_task->r2t;
  909. /* Continue with this R2T? */
  910. if (r2t->data_length <= r2t->sent) {
  911. ISCSI_DBG_TCP(task->conn,
  912. " done with r2t %p\n", r2t);
  913. kfifo_in(&tcp_task->r2tpool.queue,
  914. (void *)&tcp_task->r2t,
  915. sizeof(void *));
  916. tcp_task->r2t = r2t = NULL;
  917. }
  918. }
  919. if (r2t == NULL) {
  920. if (kfifo_out(&tcp_task->r2tqueue,
  921. (void *)&tcp_task->r2t, sizeof(void *)) !=
  922. sizeof(void *))
  923. r2t = NULL;
  924. else
  925. r2t = tcp_task->r2t;
  926. }
  927. spin_unlock_bh(&tcp_task->queue2pool);
  928. }
  929. return r2t;
  930. }
  931. /**
  932. * iscsi_tcp_task_xmit - xmit normal PDU task
  933. * @task: iscsi command task
  934. *
  935. * We're expected to return 0 when everything was transmitted successfully,
  936. * -EAGAIN if there's still data in the queue, or != 0 for any other kind
  937. * of error.
  938. */
  939. int iscsi_tcp_task_xmit(struct iscsi_task *task)
  940. {
  941. struct iscsi_conn *conn = task->conn;
  942. struct iscsi_session *session = conn->session;
  943. struct iscsi_r2t_info *r2t;
  944. int rc = 0;
  945. flush:
  946. /* Flush any pending data first. */
  947. rc = session->tt->xmit_pdu(task);
  948. if (rc < 0)
  949. return rc;
  950. /* mgmt command */
  951. if (!task->sc) {
  952. if (task->hdr->itt == RESERVED_ITT)
  953. iscsi_put_task(task);
  954. return 0;
  955. }
  956. /* Are we done already? */
  957. if (task->sc->sc_data_direction != DMA_TO_DEVICE)
  958. return 0;
  959. r2t = iscsi_tcp_get_curr_r2t(task);
  960. if (r2t == NULL) {
  961. /* Waiting for more R2Ts to arrive. */
  962. ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
  963. return 0;
  964. }
  965. rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
  966. if (rc)
  967. return rc;
  968. iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
  969. ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
  970. r2t, r2t->datasn - 1, task->hdr->itt,
  971. r2t->data_offset + r2t->sent, r2t->data_count);
  972. rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
  973. r2t->data_count);
  974. if (rc) {
  975. iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
  976. return rc;
  977. }
  978. r2t->sent += r2t->data_count;
  979. goto flush;
  980. }
  981. EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
  982. struct iscsi_cls_conn *
  983. iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
  984. uint32_t conn_idx)
  985. {
  986. struct iscsi_conn *conn;
  987. struct iscsi_cls_conn *cls_conn;
  988. struct iscsi_tcp_conn *tcp_conn;
  989. cls_conn = iscsi_conn_setup(cls_session,
  990. sizeof(*tcp_conn) + dd_data_size, conn_idx);
  991. if (!cls_conn)
  992. return NULL;
  993. conn = cls_conn->dd_data;
  994. /*
  995. * due to strange issues with iser these are not set
  996. * in iscsi_conn_setup
  997. */
  998. conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
  999. tcp_conn = conn->dd_data;
  1000. tcp_conn->iscsi_conn = conn;
  1001. tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn);
  1002. return cls_conn;
  1003. }
  1004. EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
  1005. void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1006. {
  1007. iscsi_conn_teardown(cls_conn);
  1008. }
  1009. EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
  1010. int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
  1011. {
  1012. int i;
  1013. int cmd_i;
  1014. /*
  1015. * initialize per-task: R2T pool and xmit queue
  1016. */
  1017. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1018. struct iscsi_task *task = session->cmds[cmd_i];
  1019. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1020. /*
  1021. * pre-allocated x2 as much r2ts to handle race when
  1022. * target acks DataOut faster than we data_xmit() queues
  1023. * could replenish r2tqueue.
  1024. */
  1025. /* R2T pool */
  1026. if (iscsi_pool_init(&tcp_task->r2tpool,
  1027. session->max_r2t * 2, NULL,
  1028. sizeof(struct iscsi_r2t_info))) {
  1029. goto r2t_alloc_fail;
  1030. }
  1031. /* R2T xmit queue */
  1032. if (kfifo_alloc(&tcp_task->r2tqueue,
  1033. session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
  1034. iscsi_pool_free(&tcp_task->r2tpool);
  1035. goto r2t_alloc_fail;
  1036. }
  1037. spin_lock_init(&tcp_task->pool2queue);
  1038. spin_lock_init(&tcp_task->queue2pool);
  1039. }
  1040. return 0;
  1041. r2t_alloc_fail:
  1042. for (i = 0; i < cmd_i; i++) {
  1043. struct iscsi_task *task = session->cmds[i];
  1044. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1045. kfifo_free(&tcp_task->r2tqueue);
  1046. iscsi_pool_free(&tcp_task->r2tpool);
  1047. }
  1048. return -ENOMEM;
  1049. }
  1050. EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
  1051. void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
  1052. {
  1053. int i;
  1054. for (i = 0; i < session->cmds_max; i++) {
  1055. struct iscsi_task *task = session->cmds[i];
  1056. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1057. kfifo_free(&tcp_task->r2tqueue);
  1058. iscsi_pool_free(&tcp_task->r2tpool);
  1059. }
  1060. }
  1061. EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
  1062. int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf)
  1063. {
  1064. struct iscsi_session *session = conn->session;
  1065. unsigned short r2ts = 0;
  1066. sscanf(buf, "%hu", &r2ts);
  1067. if (session->max_r2t == r2ts)
  1068. return 0;
  1069. if (!r2ts || !is_power_of_2(r2ts))
  1070. return -EINVAL;
  1071. session->max_r2t = r2ts;
  1072. iscsi_tcp_r2tpool_free(session);
  1073. return iscsi_tcp_r2tpool_alloc(session);
  1074. }
  1075. EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t);
  1076. void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  1077. struct iscsi_stats *stats)
  1078. {
  1079. struct iscsi_conn *conn = cls_conn->dd_data;
  1080. stats->txdata_octets = conn->txdata_octets;
  1081. stats->rxdata_octets = conn->rxdata_octets;
  1082. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1083. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1084. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1085. stats->datain_pdus = conn->datain_pdus_cnt;
  1086. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1087. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1088. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1089. }
  1090. EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);