libiscsi_tcp.c 31 KB

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