libiscsi_tcp.c 31 KB

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