libiscsi_tcp.c 31 KB

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