libiscsi_tcp.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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 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. /* flush task's r2t queues */
  410. while (kfifo_out(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
  411. kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
  412. sizeof(void*));
  413. ISCSI_DBG_TCP(task->conn, "pending r2t dropped\n");
  414. }
  415. r2t = tcp_task->r2t;
  416. if (r2t != NULL) {
  417. kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
  418. sizeof(void*));
  419. tcp_task->r2t = NULL;
  420. }
  421. }
  422. EXPORT_SYMBOL_GPL(iscsi_tcp_cleanup_task);
  423. /**
  424. * iscsi_tcp_data_in - SCSI Data-In Response processing
  425. * @conn: iscsi connection
  426. * @task: scsi command task
  427. */
  428. static int iscsi_tcp_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
  429. {
  430. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  431. struct iscsi_tcp_task *tcp_task = task->dd_data;
  432. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
  433. int datasn = be32_to_cpu(rhdr->datasn);
  434. unsigned total_in_length = scsi_in(task->sc)->length;
  435. /*
  436. * lib iscsi will update this in the completion handling if there
  437. * is status.
  438. */
  439. if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS))
  440. iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
  441. if (tcp_conn->in.datalen == 0)
  442. return 0;
  443. if (tcp_task->exp_datasn != datasn) {
  444. ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->datasn(%d)"
  445. "\n", tcp_task->exp_datasn, datasn);
  446. return ISCSI_ERR_DATASN;
  447. }
  448. tcp_task->exp_datasn++;
  449. tcp_task->data_offset = be32_to_cpu(rhdr->offset);
  450. if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
  451. ISCSI_DBG_TCP(conn, "data_offset(%d) + data_len(%d) > "
  452. "total_length_in(%d)\n", tcp_task->data_offset,
  453. tcp_conn->in.datalen, total_in_length);
  454. return ISCSI_ERR_DATA_OFFSET;
  455. }
  456. conn->datain_pdus_cnt++;
  457. return 0;
  458. }
  459. /**
  460. * iscsi_tcp_r2t_rsp - iSCSI R2T Response processing
  461. * @conn: iscsi connection
  462. * @task: scsi command task
  463. */
  464. static int iscsi_tcp_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
  465. {
  466. struct iscsi_session *session = conn->session;
  467. struct iscsi_tcp_task *tcp_task = task->dd_data;
  468. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  469. struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
  470. struct iscsi_r2t_info *r2t;
  471. int r2tsn = be32_to_cpu(rhdr->r2tsn);
  472. int rc;
  473. if (tcp_conn->in.datalen) {
  474. iscsi_conn_printk(KERN_ERR, conn,
  475. "invalid R2t with datalen %d\n",
  476. tcp_conn->in.datalen);
  477. return ISCSI_ERR_DATALEN;
  478. }
  479. if (tcp_task->exp_datasn != r2tsn){
  480. ISCSI_DBG_TCP(conn, "task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
  481. tcp_task->exp_datasn, r2tsn);
  482. return ISCSI_ERR_R2TSN;
  483. }
  484. /* fill-in new R2T associated with the task */
  485. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  486. if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
  487. iscsi_conn_printk(KERN_INFO, conn,
  488. "dropping R2T itt %d in recovery.\n",
  489. task->itt);
  490. return 0;
  491. }
  492. rc = kfifo_out(&tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
  493. if (!rc) {
  494. iscsi_conn_printk(KERN_ERR, conn, "Could not allocate R2T. "
  495. "Target has sent more R2Ts than it "
  496. "negotiated for or driver has has leaked.\n");
  497. return ISCSI_ERR_PROTO;
  498. }
  499. r2t->exp_statsn = rhdr->statsn;
  500. r2t->data_length = be32_to_cpu(rhdr->data_length);
  501. if (r2t->data_length == 0) {
  502. iscsi_conn_printk(KERN_ERR, conn,
  503. "invalid R2T with zero data len\n");
  504. kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
  505. sizeof(void*));
  506. return ISCSI_ERR_DATALEN;
  507. }
  508. if (r2t->data_length > session->max_burst)
  509. ISCSI_DBG_TCP(conn, "invalid R2T with data len %u and max "
  510. "burst %u. Attempting to execute request.\n",
  511. r2t->data_length, session->max_burst);
  512. r2t->data_offset = be32_to_cpu(rhdr->data_offset);
  513. if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
  514. iscsi_conn_printk(KERN_ERR, conn,
  515. "invalid R2T with data len %u at offset %u "
  516. "and total length %d\n", r2t->data_length,
  517. r2t->data_offset, scsi_out(task->sc)->length);
  518. kfifo_in(&tcp_task->r2tpool.queue, (void*)&r2t,
  519. sizeof(void*));
  520. return ISCSI_ERR_DATALEN;
  521. }
  522. r2t->ttt = rhdr->ttt; /* no flip */
  523. r2t->datasn = 0;
  524. r2t->sent = 0;
  525. tcp_task->exp_datasn = r2tsn + 1;
  526. kfifo_in(&tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
  527. conn->r2t_pdus_cnt++;
  528. iscsi_requeue_task(task);
  529. return 0;
  530. }
  531. /*
  532. * Handle incoming reply to DataIn command
  533. */
  534. static int
  535. iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
  536. struct iscsi_segment *segment)
  537. {
  538. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  539. struct iscsi_hdr *hdr = tcp_conn->in.hdr;
  540. int rc;
  541. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  542. return ISCSI_ERR_DATA_DGST;
  543. /* check for non-exceptional status */
  544. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  545. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
  546. if (rc)
  547. return rc;
  548. }
  549. iscsi_tcp_hdr_recv_prep(tcp_conn);
  550. return 0;
  551. }
  552. /**
  553. * iscsi_tcp_hdr_dissect - process PDU header
  554. * @conn: iSCSI connection
  555. * @hdr: PDU header
  556. *
  557. * This function analyzes the header of the PDU received,
  558. * and performs several sanity checks. If the PDU is accompanied
  559. * by data, the receive buffer is set up to copy the incoming data
  560. * to the correct location.
  561. */
  562. static int
  563. iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  564. {
  565. int rc = 0, opcode, ahslen;
  566. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  567. struct iscsi_task *task;
  568. /* verify PDU length */
  569. tcp_conn->in.datalen = ntoh24(hdr->dlength);
  570. if (tcp_conn->in.datalen > conn->max_recv_dlength) {
  571. iscsi_conn_printk(KERN_ERR, conn,
  572. "iscsi_tcp: datalen %d > %d\n",
  573. tcp_conn->in.datalen, conn->max_recv_dlength);
  574. return ISCSI_ERR_DATALEN;
  575. }
  576. /* Additional header segments. So far, we don't
  577. * process additional headers.
  578. */
  579. ahslen = hdr->hlength << 2;
  580. opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  581. /* verify itt (itt encoding: age+cid+itt) */
  582. rc = iscsi_verify_itt(conn, hdr->itt);
  583. if (rc)
  584. return rc;
  585. ISCSI_DBG_TCP(conn, "opcode 0x%x ahslen %d datalen %d\n",
  586. opcode, ahslen, tcp_conn->in.datalen);
  587. switch(opcode) {
  588. case ISCSI_OP_SCSI_DATA_IN:
  589. spin_lock(&conn->session->lock);
  590. task = iscsi_itt_to_ctask(conn, hdr->itt);
  591. if (!task)
  592. rc = ISCSI_ERR_BAD_ITT;
  593. else
  594. rc = iscsi_tcp_data_in(conn, task);
  595. if (rc) {
  596. spin_unlock(&conn->session->lock);
  597. break;
  598. }
  599. if (tcp_conn->in.datalen) {
  600. struct iscsi_tcp_task *tcp_task = task->dd_data;
  601. struct hash_desc *rx_hash = NULL;
  602. struct scsi_data_buffer *sdb = scsi_in(task->sc);
  603. /*
  604. * Setup copy of Data-In into the Scsi_Cmnd
  605. * Scatterlist case:
  606. * We set up the iscsi_segment to point to the next
  607. * scatterlist entry to copy to. As we go along,
  608. * we move on to the next scatterlist entry and
  609. * update the digest per-entry.
  610. */
  611. if (conn->datadgst_en &&
  612. !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
  613. rx_hash = tcp_conn->rx_hash;
  614. ISCSI_DBG_TCP(conn, "iscsi_tcp_begin_data_in( "
  615. "offset=%d, datalen=%d)\n",
  616. tcp_task->data_offset,
  617. tcp_conn->in.datalen);
  618. task->last_xfer = jiffies;
  619. rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
  620. sdb->table.sgl,
  621. sdb->table.nents,
  622. tcp_task->data_offset,
  623. tcp_conn->in.datalen,
  624. iscsi_tcp_process_data_in,
  625. rx_hash);
  626. spin_unlock(&conn->session->lock);
  627. return rc;
  628. }
  629. rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
  630. spin_unlock(&conn->session->lock);
  631. break;
  632. case ISCSI_OP_SCSI_CMD_RSP:
  633. if (tcp_conn->in.datalen) {
  634. iscsi_tcp_data_recv_prep(tcp_conn);
  635. return 0;
  636. }
  637. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  638. break;
  639. case ISCSI_OP_R2T:
  640. spin_lock(&conn->session->lock);
  641. task = iscsi_itt_to_ctask(conn, hdr->itt);
  642. if (!task)
  643. rc = ISCSI_ERR_BAD_ITT;
  644. else if (ahslen)
  645. rc = ISCSI_ERR_AHSLEN;
  646. else if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
  647. task->last_xfer = jiffies;
  648. rc = iscsi_tcp_r2t_rsp(conn, task);
  649. } else
  650. rc = ISCSI_ERR_PROTO;
  651. spin_unlock(&conn->session->lock);
  652. break;
  653. case ISCSI_OP_LOGIN_RSP:
  654. case ISCSI_OP_TEXT_RSP:
  655. case ISCSI_OP_REJECT:
  656. case ISCSI_OP_ASYNC_EVENT:
  657. /*
  658. * It is possible that we could get a PDU with a buffer larger
  659. * than 8K, but there are no targets that currently do this.
  660. * For now we fail until we find a vendor that needs it
  661. */
  662. if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
  663. iscsi_conn_printk(KERN_ERR, conn,
  664. "iscsi_tcp: received buffer of "
  665. "len %u but conn buffer is only %u "
  666. "(opcode %0x)\n",
  667. tcp_conn->in.datalen,
  668. ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
  669. rc = ISCSI_ERR_PROTO;
  670. break;
  671. }
  672. /* If there's data coming in with the response,
  673. * receive it to the connection's buffer.
  674. */
  675. if (tcp_conn->in.datalen) {
  676. iscsi_tcp_data_recv_prep(tcp_conn);
  677. return 0;
  678. }
  679. /* fall through */
  680. case ISCSI_OP_LOGOUT_RSP:
  681. case ISCSI_OP_NOOP_IN:
  682. case ISCSI_OP_SCSI_TMFUNC_RSP:
  683. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  684. break;
  685. default:
  686. rc = ISCSI_ERR_BAD_OPCODE;
  687. break;
  688. }
  689. if (rc == 0) {
  690. /* Anything that comes with data should have
  691. * been handled above. */
  692. if (tcp_conn->in.datalen)
  693. return ISCSI_ERR_PROTO;
  694. iscsi_tcp_hdr_recv_prep(tcp_conn);
  695. }
  696. return rc;
  697. }
  698. /**
  699. * iscsi_tcp_hdr_recv_done - process PDU header
  700. *
  701. * This is the callback invoked when the PDU header has
  702. * been received. If the header is followed by additional
  703. * header segments, we go back for more data.
  704. */
  705. static int
  706. iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  707. struct iscsi_segment *segment)
  708. {
  709. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  710. struct iscsi_hdr *hdr;
  711. /* Check if there are additional header segments
  712. * *prior* to computing the digest, because we
  713. * may need to go back to the caller for more.
  714. */
  715. hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
  716. if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
  717. /* Bump the header length - the caller will
  718. * just loop around and get the AHS for us, and
  719. * call again. */
  720. unsigned int ahslen = hdr->hlength << 2;
  721. /* Make sure we don't overflow */
  722. if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
  723. return ISCSI_ERR_AHSLEN;
  724. segment->total_size += ahslen;
  725. segment->size += ahslen;
  726. return 0;
  727. }
  728. /* We're done processing the header. See if we're doing
  729. * header digests; if so, set up the recv_digest buffer
  730. * and go back for more. */
  731. if (conn->hdrdgst_en &&
  732. !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
  733. if (segment->digest_len == 0) {
  734. /*
  735. * Even if we offload the digest processing we
  736. * splice it in so we can increment the skb/segment
  737. * counters in preparation for the data segment.
  738. */
  739. iscsi_tcp_segment_splice_digest(segment,
  740. segment->recv_digest);
  741. return 0;
  742. }
  743. iscsi_tcp_dgst_header(tcp_conn->rx_hash, hdr,
  744. segment->total_copied - ISCSI_DIGEST_SIZE,
  745. segment->digest);
  746. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  747. return ISCSI_ERR_HDR_DGST;
  748. }
  749. tcp_conn->in.hdr = hdr;
  750. return iscsi_tcp_hdr_dissect(conn, hdr);
  751. }
  752. /**
  753. * iscsi_tcp_recv_segment_is_hdr - tests if we are reading in a header
  754. * @tcp_conn: iscsi tcp conn
  755. *
  756. * returns non zero if we are currently processing or setup to process
  757. * a header.
  758. */
  759. inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
  760. {
  761. return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
  762. }
  763. EXPORT_SYMBOL_GPL(iscsi_tcp_recv_segment_is_hdr);
  764. /**
  765. * iscsi_tcp_recv_skb - Process skb
  766. * @conn: iscsi connection
  767. * @skb: network buffer with header and/or data segment
  768. * @offset: offset in skb
  769. * @offload: bool indicating if transfer was offloaded
  770. *
  771. * Will return status of transfer in status. And will return
  772. * number of bytes copied.
  773. */
  774. int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
  775. unsigned int offset, bool offloaded, int *status)
  776. {
  777. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  778. struct iscsi_segment *segment = &tcp_conn->in.segment;
  779. struct skb_seq_state seq;
  780. unsigned int consumed = 0;
  781. int rc = 0;
  782. ISCSI_DBG_TCP(conn, "in %d bytes\n", skb->len - offset);
  783. /*
  784. * Update for each skb instead of pdu, because over slow networks a
  785. * data_in's data could take a while to read in. We also want to
  786. * account for r2ts.
  787. */
  788. conn->last_recv = jiffies;
  789. if (unlikely(conn->suspend_rx)) {
  790. ISCSI_DBG_TCP(conn, "Rx suspended!\n");
  791. *status = ISCSI_TCP_SUSPENDED;
  792. return 0;
  793. }
  794. if (offloaded) {
  795. segment->total_copied = segment->total_size;
  796. goto segment_done;
  797. }
  798. skb_prepare_seq_read(skb, offset, skb->len, &seq);
  799. while (1) {
  800. unsigned int avail;
  801. const u8 *ptr;
  802. avail = skb_seq_read(consumed, &ptr, &seq);
  803. if (avail == 0) {
  804. ISCSI_DBG_TCP(conn, "no more data avail. Consumed %d\n",
  805. consumed);
  806. *status = ISCSI_TCP_SKB_DONE;
  807. skb_abort_seq_read(&seq);
  808. goto skb_done;
  809. }
  810. BUG_ON(segment->copied >= segment->size);
  811. ISCSI_DBG_TCP(conn, "skb %p ptr=%p avail=%u\n", skb, ptr,
  812. avail);
  813. rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
  814. BUG_ON(rc == 0);
  815. consumed += rc;
  816. if (segment->total_copied >= segment->total_size) {
  817. skb_abort_seq_read(&seq);
  818. goto segment_done;
  819. }
  820. }
  821. segment_done:
  822. *status = ISCSI_TCP_SEGMENT_DONE;
  823. ISCSI_DBG_TCP(conn, "segment done\n");
  824. rc = segment->done(tcp_conn, segment);
  825. if (rc != 0) {
  826. *status = ISCSI_TCP_CONN_ERR;
  827. ISCSI_DBG_TCP(conn, "Error receiving PDU, errno=%d\n", rc);
  828. iscsi_conn_failure(conn, rc);
  829. return 0;
  830. }
  831. /* The done() functions sets up the next segment. */
  832. skb_done:
  833. conn->rxdata_octets += consumed;
  834. return consumed;
  835. }
  836. EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
  837. /**
  838. * iscsi_tcp_task_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
  839. * @conn: iscsi connection
  840. * @task: scsi command task
  841. * @sc: scsi command
  842. */
  843. int iscsi_tcp_task_init(struct iscsi_task *task)
  844. {
  845. struct iscsi_tcp_task *tcp_task = task->dd_data;
  846. struct iscsi_conn *conn = task->conn;
  847. struct scsi_cmnd *sc = task->sc;
  848. int err;
  849. if (!sc) {
  850. /*
  851. * mgmt tasks do not have a scatterlist since they come
  852. * in from the iscsi interface.
  853. */
  854. ISCSI_DBG_TCP(conn, "mtask deq [itt 0x%x]\n", task->itt);
  855. return conn->session->tt->init_pdu(task, 0, task->data_count);
  856. }
  857. BUG_ON(kfifo_len(&tcp_task->r2tqueue));
  858. tcp_task->exp_datasn = 0;
  859. /* Prepare PDU, optionally w/ immediate data */
  860. ISCSI_DBG_TCP(conn, "task deq [itt 0x%x imm %d unsol %d]\n",
  861. task->itt, task->imm_count, task->unsol_r2t.data_length);
  862. err = conn->session->tt->init_pdu(task, 0, task->imm_count);
  863. if (err)
  864. return err;
  865. task->imm_count = 0;
  866. return 0;
  867. }
  868. EXPORT_SYMBOL_GPL(iscsi_tcp_task_init);
  869. static struct iscsi_r2t_info *iscsi_tcp_get_curr_r2t(struct iscsi_task *task)
  870. {
  871. struct iscsi_session *session = task->conn->session;
  872. struct iscsi_tcp_task *tcp_task = task->dd_data;
  873. struct iscsi_r2t_info *r2t = NULL;
  874. if (iscsi_task_has_unsol_data(task))
  875. r2t = &task->unsol_r2t;
  876. else {
  877. spin_lock_bh(&session->lock);
  878. if (tcp_task->r2t) {
  879. r2t = tcp_task->r2t;
  880. /* Continue with this R2T? */
  881. if (r2t->data_length <= r2t->sent) {
  882. ISCSI_DBG_TCP(task->conn,
  883. " done with r2t %p\n", r2t);
  884. kfifo_in(&tcp_task->r2tpool.queue,
  885. (void *)&tcp_task->r2t,
  886. sizeof(void *));
  887. tcp_task->r2t = r2t = NULL;
  888. }
  889. }
  890. if (r2t == NULL) {
  891. if (kfifo_out(&tcp_task->r2tqueue,
  892. (void *)&tcp_task->r2t, sizeof(void *)) !=
  893. sizeof(void *))
  894. r2t = NULL;
  895. else
  896. r2t = tcp_task->r2t;
  897. }
  898. spin_unlock_bh(&session->lock);
  899. }
  900. return r2t;
  901. }
  902. /**
  903. * iscsi_tcp_task_xmit - xmit normal PDU task
  904. * @task: iscsi command task
  905. *
  906. * We're expected to return 0 when everything was transmitted successfully,
  907. * -EAGAIN if there's still data in the queue, or != 0 for any other kind
  908. * of error.
  909. */
  910. int iscsi_tcp_task_xmit(struct iscsi_task *task)
  911. {
  912. struct iscsi_conn *conn = task->conn;
  913. struct iscsi_session *session = conn->session;
  914. struct iscsi_r2t_info *r2t;
  915. int rc = 0;
  916. flush:
  917. /* Flush any pending data first. */
  918. rc = session->tt->xmit_pdu(task);
  919. if (rc < 0)
  920. return rc;
  921. /* mgmt command */
  922. if (!task->sc) {
  923. if (task->hdr->itt == RESERVED_ITT)
  924. iscsi_put_task(task);
  925. return 0;
  926. }
  927. /* Are we done already? */
  928. if (task->sc->sc_data_direction != DMA_TO_DEVICE)
  929. return 0;
  930. r2t = iscsi_tcp_get_curr_r2t(task);
  931. if (r2t == NULL) {
  932. /* Waiting for more R2Ts to arrive. */
  933. ISCSI_DBG_TCP(conn, "no R2Ts yet\n");
  934. return 0;
  935. }
  936. rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_DATA_OUT);
  937. if (rc)
  938. return rc;
  939. iscsi_prep_data_out_pdu(task, r2t, (struct iscsi_data *) task->hdr);
  940. ISCSI_DBG_TCP(conn, "sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
  941. r2t, r2t->datasn - 1, task->hdr->itt,
  942. r2t->data_offset + r2t->sent, r2t->data_count);
  943. rc = conn->session->tt->init_pdu(task, r2t->data_offset + r2t->sent,
  944. r2t->data_count);
  945. if (rc) {
  946. iscsi_conn_failure(conn, ISCSI_ERR_XMIT_FAILED);
  947. return rc;
  948. }
  949. r2t->sent += r2t->data_count;
  950. goto flush;
  951. }
  952. EXPORT_SYMBOL_GPL(iscsi_tcp_task_xmit);
  953. struct iscsi_cls_conn *
  954. iscsi_tcp_conn_setup(struct iscsi_cls_session *cls_session, int dd_data_size,
  955. uint32_t conn_idx)
  956. {
  957. struct iscsi_conn *conn;
  958. struct iscsi_cls_conn *cls_conn;
  959. struct iscsi_tcp_conn *tcp_conn;
  960. cls_conn = iscsi_conn_setup(cls_session,
  961. sizeof(*tcp_conn) + dd_data_size, conn_idx);
  962. if (!cls_conn)
  963. return NULL;
  964. conn = cls_conn->dd_data;
  965. /*
  966. * due to strange issues with iser these are not set
  967. * in iscsi_conn_setup
  968. */
  969. conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
  970. tcp_conn = conn->dd_data;
  971. tcp_conn->iscsi_conn = conn;
  972. tcp_conn->dd_data = conn->dd_data + sizeof(*tcp_conn);
  973. return cls_conn;
  974. }
  975. EXPORT_SYMBOL_GPL(iscsi_tcp_conn_setup);
  976. void iscsi_tcp_conn_teardown(struct iscsi_cls_conn *cls_conn)
  977. {
  978. iscsi_conn_teardown(cls_conn);
  979. }
  980. EXPORT_SYMBOL_GPL(iscsi_tcp_conn_teardown);
  981. int iscsi_tcp_r2tpool_alloc(struct iscsi_session *session)
  982. {
  983. int i;
  984. int cmd_i;
  985. /*
  986. * initialize per-task: R2T pool and xmit queue
  987. */
  988. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  989. struct iscsi_task *task = session->cmds[cmd_i];
  990. struct iscsi_tcp_task *tcp_task = task->dd_data;
  991. /*
  992. * pre-allocated x2 as much r2ts to handle race when
  993. * target acks DataOut faster than we data_xmit() queues
  994. * could replenish r2tqueue.
  995. */
  996. /* R2T pool */
  997. if (iscsi_pool_init(&tcp_task->r2tpool,
  998. session->max_r2t * 2, NULL,
  999. sizeof(struct iscsi_r2t_info))) {
  1000. goto r2t_alloc_fail;
  1001. }
  1002. /* R2T xmit queue */
  1003. if (kfifo_alloc(&tcp_task->r2tqueue,
  1004. session->max_r2t * 4 * sizeof(void*), GFP_KERNEL)) {
  1005. iscsi_pool_free(&tcp_task->r2tpool);
  1006. goto r2t_alloc_fail;
  1007. }
  1008. }
  1009. return 0;
  1010. r2t_alloc_fail:
  1011. for (i = 0; i < cmd_i; i++) {
  1012. struct iscsi_task *task = session->cmds[i];
  1013. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1014. kfifo_free(&tcp_task->r2tqueue);
  1015. iscsi_pool_free(&tcp_task->r2tpool);
  1016. }
  1017. return -ENOMEM;
  1018. }
  1019. EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_alloc);
  1020. void iscsi_tcp_r2tpool_free(struct iscsi_session *session)
  1021. {
  1022. int i;
  1023. for (i = 0; i < session->cmds_max; i++) {
  1024. struct iscsi_task *task = session->cmds[i];
  1025. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1026. kfifo_free(&tcp_task->r2tqueue);
  1027. iscsi_pool_free(&tcp_task->r2tpool);
  1028. }
  1029. }
  1030. EXPORT_SYMBOL_GPL(iscsi_tcp_r2tpool_free);
  1031. int iscsi_tcp_set_max_r2t(struct iscsi_conn *conn, char *buf)
  1032. {
  1033. struct iscsi_session *session = conn->session;
  1034. unsigned short r2ts = 0;
  1035. sscanf(buf, "%hu", &r2ts);
  1036. if (session->max_r2t == r2ts)
  1037. return 0;
  1038. if (!r2ts || !is_power_of_2(r2ts))
  1039. return -EINVAL;
  1040. session->max_r2t = r2ts;
  1041. iscsi_tcp_r2tpool_free(session);
  1042. return iscsi_tcp_r2tpool_alloc(session);
  1043. }
  1044. EXPORT_SYMBOL_GPL(iscsi_tcp_set_max_r2t);
  1045. void iscsi_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  1046. struct iscsi_stats *stats)
  1047. {
  1048. struct iscsi_conn *conn = cls_conn->dd_data;
  1049. stats->txdata_octets = conn->txdata_octets;
  1050. stats->rxdata_octets = conn->rxdata_octets;
  1051. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1052. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1053. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1054. stats->datain_pdus = conn->datain_pdus_cnt;
  1055. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1056. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1057. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1058. }
  1059. EXPORT_SYMBOL_GPL(iscsi_tcp_conn_get_stats);