libiscsi_tcp.c 32 KB

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