libiscsi_tcp.c 33 KB

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