iscsi_tcp.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060
  1. /*
  2. * iSCSI Initiator over TCP/IP Data-Path
  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("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
  45. "Alex Aizman <itn780@yahoo.com>");
  46. MODULE_DESCRIPTION("iSCSI/TCP data-path");
  47. MODULE_LICENSE("GPL");
  48. #undef DEBUG_TCP
  49. #define DEBUG_ASSERT
  50. #ifdef DEBUG_TCP
  51. #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
  52. #else
  53. #define debug_tcp(fmt...)
  54. #endif
  55. #ifndef DEBUG_ASSERT
  56. #ifdef BUG_ON
  57. #undef BUG_ON
  58. #endif
  59. #define BUG_ON(expr)
  60. #endif
  61. static struct scsi_transport_template *iscsi_tcp_scsi_transport;
  62. static struct scsi_host_template iscsi_sht;
  63. static struct iscsi_transport iscsi_tcp_transport;
  64. static unsigned int iscsi_max_lun = 512;
  65. module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
  66. static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  67. struct iscsi_segment *segment);
  68. /*
  69. * Scatterlist handling: inside the iscsi_segment, we
  70. * remember an index into the scatterlist, and set data/size
  71. * to the current scatterlist entry. For highmem pages, we
  72. * kmap as needed.
  73. *
  74. * Note that the page is unmapped when we return from
  75. * TCP's data_ready handler, so we may end up mapping and
  76. * unmapping the same page repeatedly. The whole reason
  77. * for this is that we shouldn't keep the page mapped
  78. * outside the softirq.
  79. */
  80. /**
  81. * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
  82. * @segment: the buffer object
  83. * @sg: scatterlist
  84. * @offset: byte offset into that sg entry
  85. *
  86. * This function sets up the segment so that subsequent
  87. * data is copied to the indicated sg entry, at the given
  88. * offset.
  89. */
  90. static inline void
  91. iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
  92. struct scatterlist *sg, unsigned int offset)
  93. {
  94. segment->sg = sg;
  95. segment->sg_offset = offset;
  96. segment->size = min(sg->length - offset,
  97. segment->total_size - segment->total_copied);
  98. segment->data = NULL;
  99. }
  100. /**
  101. * iscsi_tcp_segment_map - map the current S/G page
  102. * @segment: iscsi_segment
  103. * @recv: 1 if called from recv path
  104. *
  105. * We only need to possibly kmap data if scatter lists are being used,
  106. * because the iscsi passthrough and internal IO paths will never use high
  107. * mem pages.
  108. */
  109. static inline void
  110. iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
  111. {
  112. struct scatterlist *sg;
  113. if (segment->data != NULL || !segment->sg)
  114. return;
  115. sg = segment->sg;
  116. BUG_ON(segment->sg_mapped);
  117. BUG_ON(sg->length == 0);
  118. /*
  119. * If the page count is greater than one it is ok to send
  120. * to the network layer's zero copy send path. If not we
  121. * have to go the slow sendmsg path. We always map for the
  122. * recv path.
  123. */
  124. if (page_count(sg_page(sg)) >= 1 && !recv)
  125. return;
  126. debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit",
  127. segment);
  128. segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0);
  129. segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
  130. }
  131. static inline void
  132. iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
  133. {
  134. debug_tcp("iscsi_tcp_segment_unmap %p\n", segment);
  135. if (segment->sg_mapped) {
  136. debug_tcp("iscsi_tcp_segment_unmap valid\n");
  137. kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0);
  138. segment->sg_mapped = NULL;
  139. segment->data = NULL;
  140. }
  141. }
  142. /*
  143. * Splice the digest buffer into the buffer
  144. */
  145. static inline void
  146. iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
  147. {
  148. segment->data = digest;
  149. segment->digest_len = ISCSI_DIGEST_SIZE;
  150. segment->total_size += ISCSI_DIGEST_SIZE;
  151. segment->size = ISCSI_DIGEST_SIZE;
  152. segment->copied = 0;
  153. segment->sg = NULL;
  154. segment->hash = NULL;
  155. }
  156. /**
  157. * iscsi_tcp_segment_done - check whether the segment is complete
  158. * @segment: iscsi segment to check
  159. * @recv: set to one of this is called from the recv path
  160. * @copied: number of bytes copied
  161. *
  162. * Check if we're done receiving this segment. If the receive
  163. * buffer is full but we expect more data, move on to the
  164. * next entry in the scatterlist.
  165. *
  166. * If the amount of data we received isn't a multiple of 4,
  167. * we will transparently receive the pad bytes, too.
  168. *
  169. * This function must be re-entrant.
  170. */
  171. static inline int
  172. iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv, unsigned copied)
  173. {
  174. static unsigned char padbuf[ISCSI_PAD_LEN];
  175. struct scatterlist sg;
  176. unsigned int pad;
  177. debug_tcp("copied %u %u size %u %s\n", segment->copied, copied,
  178. segment->size, recv ? "recv" : "xmit");
  179. if (segment->hash && copied) {
  180. /*
  181. * If a segment is kmapd we must unmap it before sending
  182. * to the crypto layer since that will try to kmap it again.
  183. */
  184. iscsi_tcp_segment_unmap(segment);
  185. if (!segment->data) {
  186. sg_init_table(&sg, 1);
  187. sg_set_page(&sg, sg_page(segment->sg), copied,
  188. segment->copied + segment->sg_offset +
  189. segment->sg->offset);
  190. } else
  191. sg_init_one(&sg, segment->data + segment->copied,
  192. copied);
  193. crypto_hash_update(segment->hash, &sg, copied);
  194. }
  195. segment->copied += copied;
  196. if (segment->copied < segment->size) {
  197. iscsi_tcp_segment_map(segment, recv);
  198. return 0;
  199. }
  200. segment->total_copied += segment->copied;
  201. segment->copied = 0;
  202. segment->size = 0;
  203. /* Unmap the current scatterlist page, if there is one. */
  204. iscsi_tcp_segment_unmap(segment);
  205. /* Do we have more scatterlist entries? */
  206. debug_tcp("total copied %u total size %u\n", segment->total_copied,
  207. segment->total_size);
  208. if (segment->total_copied < segment->total_size) {
  209. /* Proceed to the next entry in the scatterlist. */
  210. iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
  211. 0);
  212. iscsi_tcp_segment_map(segment, recv);
  213. BUG_ON(segment->size == 0);
  214. return 0;
  215. }
  216. /* Do we need to handle padding? */
  217. pad = iscsi_padding(segment->total_copied);
  218. if (pad != 0) {
  219. debug_tcp("consume %d pad bytes\n", pad);
  220. segment->total_size += pad;
  221. segment->size = pad;
  222. segment->data = padbuf;
  223. return 0;
  224. }
  225. /*
  226. * Set us up for transferring the data digest. hdr digest
  227. * is completely handled in hdr done function.
  228. */
  229. if (segment->hash) {
  230. crypto_hash_final(segment->hash, segment->digest);
  231. iscsi_tcp_segment_splice_digest(segment,
  232. recv ? segment->recv_digest : segment->digest);
  233. return 0;
  234. }
  235. return 1;
  236. }
  237. /**
  238. * iscsi_tcp_xmit_segment - transmit segment
  239. * @tcp_conn: the iSCSI TCP connection
  240. * @segment: the buffer to transmnit
  241. *
  242. * This function transmits as much of the buffer as
  243. * the network layer will accept, and returns the number of
  244. * bytes transmitted.
  245. *
  246. * If CRC hashing is enabled, the function will compute the
  247. * hash as it goes. When the entire segment has been transmitted,
  248. * it will retrieve the hash value and send it as well.
  249. */
  250. static int
  251. iscsi_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
  252. struct iscsi_segment *segment)
  253. {
  254. struct socket *sk = tcp_conn->sock;
  255. unsigned int copied = 0;
  256. int r = 0;
  257. while (!iscsi_tcp_segment_done(segment, 0, r)) {
  258. struct scatterlist *sg;
  259. unsigned int offset, copy;
  260. int flags = 0;
  261. r = 0;
  262. offset = segment->copied;
  263. copy = segment->size - offset;
  264. if (segment->total_copied + segment->size < segment->total_size)
  265. flags |= MSG_MORE;
  266. /* Use sendpage if we can; else fall back to sendmsg */
  267. if (!segment->data) {
  268. sg = segment->sg;
  269. offset += segment->sg_offset + sg->offset;
  270. r = tcp_conn->sendpage(sk, sg_page(sg), offset, copy,
  271. flags);
  272. } else {
  273. struct msghdr msg = { .msg_flags = flags };
  274. struct kvec iov = {
  275. .iov_base = segment->data + offset,
  276. .iov_len = copy
  277. };
  278. r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
  279. }
  280. if (r < 0) {
  281. iscsi_tcp_segment_unmap(segment);
  282. if (copied || r == -EAGAIN)
  283. break;
  284. return r;
  285. }
  286. copied += r;
  287. }
  288. return copied;
  289. }
  290. /**
  291. * iscsi_tcp_segment_recv - copy data to segment
  292. * @tcp_conn: the iSCSI TCP connection
  293. * @segment: the buffer to copy to
  294. * @ptr: data pointer
  295. * @len: amount of data available
  296. *
  297. * This function copies up to @len bytes to the
  298. * given buffer, and returns the number of bytes
  299. * consumed, which can actually be less than @len.
  300. *
  301. * If hash digest is enabled, the function will update the
  302. * hash while copying.
  303. * Combining these two operations doesn't buy us a lot (yet),
  304. * but in the future we could implement combined copy+crc,
  305. * just way we do for network layer checksums.
  306. */
  307. static int
  308. iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
  309. struct iscsi_segment *segment, const void *ptr,
  310. unsigned int len)
  311. {
  312. unsigned int copy = 0, copied = 0;
  313. while (!iscsi_tcp_segment_done(segment, 1, copy)) {
  314. if (copied == len) {
  315. debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
  316. len);
  317. break;
  318. }
  319. copy = min(len - copied, segment->size - segment->copied);
  320. debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy);
  321. memcpy(segment->data + segment->copied, ptr + copied, copy);
  322. copied += copy;
  323. }
  324. return copied;
  325. }
  326. static inline void
  327. iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
  328. unsigned char digest[ISCSI_DIGEST_SIZE])
  329. {
  330. struct scatterlist sg;
  331. sg_init_one(&sg, hdr, hdrlen);
  332. crypto_hash_digest(hash, &sg, hdrlen, digest);
  333. }
  334. static inline int
  335. iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
  336. struct iscsi_segment *segment)
  337. {
  338. if (!segment->digest_len)
  339. return 1;
  340. if (memcmp(segment->recv_digest, segment->digest,
  341. segment->digest_len)) {
  342. debug_scsi("digest mismatch\n");
  343. return 0;
  344. }
  345. return 1;
  346. }
  347. /*
  348. * Helper function to set up segment buffer
  349. */
  350. static inline void
  351. __iscsi_segment_init(struct iscsi_segment *segment, size_t size,
  352. iscsi_segment_done_fn_t *done, struct hash_desc *hash)
  353. {
  354. memset(segment, 0, sizeof(*segment));
  355. segment->total_size = size;
  356. segment->done = done;
  357. if (hash) {
  358. segment->hash = hash;
  359. crypto_hash_init(hash);
  360. }
  361. }
  362. static inline void
  363. iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
  364. size_t size, iscsi_segment_done_fn_t *done,
  365. struct hash_desc *hash)
  366. {
  367. __iscsi_segment_init(segment, size, done, hash);
  368. segment->data = data;
  369. segment->size = size;
  370. }
  371. static inline int
  372. iscsi_segment_seek_sg(struct iscsi_segment *segment,
  373. struct scatterlist *sg_list, unsigned int sg_count,
  374. unsigned int offset, size_t size,
  375. iscsi_segment_done_fn_t *done, struct hash_desc *hash)
  376. {
  377. struct scatterlist *sg;
  378. unsigned int i;
  379. debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
  380. offset, size);
  381. __iscsi_segment_init(segment, size, done, hash);
  382. for_each_sg(sg_list, sg, sg_count, i) {
  383. debug_scsi("sg %d, len %u offset %u\n", i, sg->length,
  384. sg->offset);
  385. if (offset < sg->length) {
  386. iscsi_tcp_segment_init_sg(segment, sg, offset);
  387. return 0;
  388. }
  389. offset -= sg->length;
  390. }
  391. return ISCSI_ERR_DATA_OFFSET;
  392. }
  393. /**
  394. * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
  395. * @tcp_conn: iscsi connection to prep for
  396. *
  397. * This function always passes NULL for the hash argument, because when this
  398. * function is called we do not yet know the final size of the header and want
  399. * to delay the digest processing until we know that.
  400. */
  401. static void
  402. iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
  403. {
  404. debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
  405. tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
  406. iscsi_segment_init_linear(&tcp_conn->in.segment,
  407. tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
  408. iscsi_tcp_hdr_recv_done, NULL);
  409. }
  410. /*
  411. * Handle incoming reply to any other type of command
  412. */
  413. static int
  414. iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
  415. struct iscsi_segment *segment)
  416. {
  417. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  418. int rc = 0;
  419. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  420. return ISCSI_ERR_DATA_DGST;
  421. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
  422. conn->data, tcp_conn->in.datalen);
  423. if (rc)
  424. return rc;
  425. iscsi_tcp_hdr_recv_prep(tcp_conn);
  426. return 0;
  427. }
  428. static void
  429. iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
  430. {
  431. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  432. struct hash_desc *rx_hash = NULL;
  433. if (conn->datadgst_en &
  434. !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
  435. rx_hash = &tcp_conn->rx_hash;
  436. iscsi_segment_init_linear(&tcp_conn->in.segment,
  437. conn->data, tcp_conn->in.datalen,
  438. iscsi_tcp_data_recv_done, rx_hash);
  439. }
  440. /*
  441. * must be called with session lock
  442. */
  443. static void
  444. iscsi_tcp_cleanup_task(struct iscsi_conn *conn, struct iscsi_task *task)
  445. {
  446. struct iscsi_tcp_task *tcp_task = task->dd_data;
  447. struct iscsi_r2t_info *r2t;
  448. /* nothing to do for mgmt tasks */
  449. if (!task->sc)
  450. return;
  451. /* flush task's r2t queues */
  452. while (__kfifo_get(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*))) {
  453. __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
  454. sizeof(void*));
  455. debug_scsi("iscsi_tcp_cleanup_task pending r2t dropped\n");
  456. }
  457. r2t = tcp_task->r2t;
  458. if (r2t != NULL) {
  459. __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
  460. sizeof(void*));
  461. tcp_task->r2t = NULL;
  462. }
  463. }
  464. /**
  465. * iscsi_data_in - SCSI Data-In Response processing
  466. * @conn: iscsi connection
  467. * @task: scsi command task
  468. **/
  469. static int
  470. iscsi_data_in(struct iscsi_conn *conn, struct iscsi_task *task)
  471. {
  472. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  473. struct iscsi_tcp_task *tcp_task = task->dd_data;
  474. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
  475. int datasn = be32_to_cpu(rhdr->datasn);
  476. unsigned total_in_length = scsi_in(task->sc)->length;
  477. iscsi_update_cmdsn(conn->session, (struct iscsi_nopin*)rhdr);
  478. if (tcp_conn->in.datalen == 0)
  479. return 0;
  480. if (tcp_task->exp_datasn != datasn) {
  481. debug_tcp("%s: task->exp_datasn(%d) != rhdr->datasn(%d)\n",
  482. __func__, tcp_task->exp_datasn, datasn);
  483. return ISCSI_ERR_DATASN;
  484. }
  485. tcp_task->exp_datasn++;
  486. tcp_task->data_offset = be32_to_cpu(rhdr->offset);
  487. if (tcp_task->data_offset + tcp_conn->in.datalen > total_in_length) {
  488. debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
  489. __func__, tcp_task->data_offset,
  490. tcp_conn->in.datalen, total_in_length);
  491. return ISCSI_ERR_DATA_OFFSET;
  492. }
  493. conn->datain_pdus_cnt++;
  494. return 0;
  495. }
  496. /**
  497. * iscsi_solicit_data_init - initialize first Data-Out
  498. * @conn: iscsi connection
  499. * @task: scsi command task
  500. * @r2t: R2T info
  501. *
  502. * Notes:
  503. * Initialize first Data-Out within this R2T sequence and finds
  504. * proper data_offset within this SCSI command.
  505. *
  506. * This function is called with connection lock taken.
  507. **/
  508. static void
  509. iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_task *task,
  510. struct iscsi_r2t_info *r2t)
  511. {
  512. struct iscsi_data *hdr;
  513. hdr = &r2t->dtask.hdr;
  514. memset(hdr, 0, sizeof(struct iscsi_data));
  515. hdr->ttt = r2t->ttt;
  516. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  517. r2t->solicit_datasn++;
  518. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  519. memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
  520. hdr->itt = task->hdr->itt;
  521. hdr->exp_statsn = r2t->exp_statsn;
  522. hdr->offset = cpu_to_be32(r2t->data_offset);
  523. if (r2t->data_length > conn->max_xmit_dlength) {
  524. hton24(hdr->dlength, conn->max_xmit_dlength);
  525. r2t->data_count = conn->max_xmit_dlength;
  526. hdr->flags = 0;
  527. } else {
  528. hton24(hdr->dlength, r2t->data_length);
  529. r2t->data_count = r2t->data_length;
  530. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  531. }
  532. conn->dataout_pdus_cnt++;
  533. r2t->sent = 0;
  534. }
  535. /**
  536. * iscsi_r2t_rsp - iSCSI R2T Response processing
  537. * @conn: iscsi connection
  538. * @task: scsi command task
  539. **/
  540. static int
  541. iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_task *task)
  542. {
  543. struct iscsi_r2t_info *r2t;
  544. struct iscsi_session *session = conn->session;
  545. struct iscsi_tcp_task *tcp_task = task->dd_data;
  546. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  547. struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
  548. int r2tsn = be32_to_cpu(rhdr->r2tsn);
  549. int rc;
  550. if (tcp_conn->in.datalen) {
  551. iscsi_conn_printk(KERN_ERR, conn,
  552. "invalid R2t with datalen %d\n",
  553. tcp_conn->in.datalen);
  554. return ISCSI_ERR_DATALEN;
  555. }
  556. if (tcp_task->exp_datasn != r2tsn){
  557. debug_tcp("%s: task->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
  558. __func__, tcp_task->exp_datasn, r2tsn);
  559. return ISCSI_ERR_R2TSN;
  560. }
  561. /* fill-in new R2T associated with the task */
  562. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  563. if (!task->sc || session->state != ISCSI_STATE_LOGGED_IN) {
  564. iscsi_conn_printk(KERN_INFO, conn,
  565. "dropping R2T itt %d in recovery.\n",
  566. task->itt);
  567. return 0;
  568. }
  569. rc = __kfifo_get(tcp_task->r2tpool.queue, (void*)&r2t, sizeof(void*));
  570. BUG_ON(!rc);
  571. r2t->exp_statsn = rhdr->statsn;
  572. r2t->data_length = be32_to_cpu(rhdr->data_length);
  573. if (r2t->data_length == 0) {
  574. iscsi_conn_printk(KERN_ERR, conn,
  575. "invalid R2T with zero data len\n");
  576. __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
  577. sizeof(void*));
  578. return ISCSI_ERR_DATALEN;
  579. }
  580. if (r2t->data_length > session->max_burst)
  581. debug_scsi("invalid R2T with data len %u and max burst %u."
  582. "Attempting to execute request.\n",
  583. r2t->data_length, session->max_burst);
  584. r2t->data_offset = be32_to_cpu(rhdr->data_offset);
  585. if (r2t->data_offset + r2t->data_length > scsi_out(task->sc)->length) {
  586. iscsi_conn_printk(KERN_ERR, conn,
  587. "invalid R2T with data len %u at offset %u "
  588. "and total length %d\n", r2t->data_length,
  589. r2t->data_offset, scsi_out(task->sc)->length);
  590. __kfifo_put(tcp_task->r2tpool.queue, (void*)&r2t,
  591. sizeof(void*));
  592. return ISCSI_ERR_DATALEN;
  593. }
  594. r2t->ttt = rhdr->ttt; /* no flip */
  595. r2t->solicit_datasn = 0;
  596. iscsi_solicit_data_init(conn, task, r2t);
  597. tcp_task->exp_datasn = r2tsn + 1;
  598. __kfifo_put(tcp_task->r2tqueue, (void*)&r2t, sizeof(void*));
  599. conn->r2t_pdus_cnt++;
  600. iscsi_requeue_task(task);
  601. return 0;
  602. }
  603. /*
  604. * Handle incoming reply to DataIn command
  605. */
  606. static int
  607. iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
  608. struct iscsi_segment *segment)
  609. {
  610. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  611. struct iscsi_hdr *hdr = tcp_conn->in.hdr;
  612. int rc;
  613. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  614. return ISCSI_ERR_DATA_DGST;
  615. /* check for non-exceptional status */
  616. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  617. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
  618. if (rc)
  619. return rc;
  620. }
  621. iscsi_tcp_hdr_recv_prep(tcp_conn);
  622. return 0;
  623. }
  624. /**
  625. * iscsi_tcp_hdr_dissect - process PDU header
  626. * @conn: iSCSI connection
  627. * @hdr: PDU header
  628. *
  629. * This function analyzes the header of the PDU received,
  630. * and performs several sanity checks. If the PDU is accompanied
  631. * by data, the receive buffer is set up to copy the incoming data
  632. * to the correct location.
  633. */
  634. static int
  635. iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  636. {
  637. int rc = 0, opcode, ahslen;
  638. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  639. struct iscsi_task *task;
  640. /* verify PDU length */
  641. tcp_conn->in.datalen = ntoh24(hdr->dlength);
  642. if (tcp_conn->in.datalen > conn->max_recv_dlength) {
  643. iscsi_conn_printk(KERN_ERR, conn,
  644. "iscsi_tcp: datalen %d > %d\n",
  645. tcp_conn->in.datalen, conn->max_recv_dlength);
  646. return ISCSI_ERR_DATALEN;
  647. }
  648. /* Additional header segments. So far, we don't
  649. * process additional headers.
  650. */
  651. ahslen = hdr->hlength << 2;
  652. opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  653. /* verify itt (itt encoding: age+cid+itt) */
  654. rc = iscsi_verify_itt(conn, hdr->itt);
  655. if (rc)
  656. return rc;
  657. debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
  658. opcode, ahslen, tcp_conn->in.datalen);
  659. switch(opcode) {
  660. case ISCSI_OP_SCSI_DATA_IN:
  661. spin_lock(&conn->session->lock);
  662. task = iscsi_itt_to_ctask(conn, hdr->itt);
  663. if (!task)
  664. rc = ISCSI_ERR_BAD_ITT;
  665. else
  666. rc = iscsi_data_in(conn, task);
  667. if (rc) {
  668. spin_unlock(&conn->session->lock);
  669. break;
  670. }
  671. if (tcp_conn->in.datalen) {
  672. struct iscsi_tcp_task *tcp_task = task->dd_data;
  673. struct hash_desc *rx_hash = NULL;
  674. struct scsi_data_buffer *sdb = scsi_in(task->sc);
  675. /*
  676. * Setup copy of Data-In into the Scsi_Cmnd
  677. * Scatterlist case:
  678. * We set up the iscsi_segment to point to the next
  679. * scatterlist entry to copy to. As we go along,
  680. * we move on to the next scatterlist entry and
  681. * update the digest per-entry.
  682. */
  683. if (conn->datadgst_en &&
  684. !(conn->session->tt->caps & CAP_DIGEST_OFFLOAD))
  685. rx_hash = &tcp_conn->rx_hash;
  686. debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
  687. "datalen=%d)\n", tcp_conn,
  688. tcp_task->data_offset,
  689. tcp_conn->in.datalen);
  690. rc = iscsi_segment_seek_sg(&tcp_conn->in.segment,
  691. sdb->table.sgl,
  692. sdb->table.nents,
  693. tcp_task->data_offset,
  694. tcp_conn->in.datalen,
  695. iscsi_tcp_process_data_in,
  696. rx_hash);
  697. spin_unlock(&conn->session->lock);
  698. return rc;
  699. }
  700. rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
  701. spin_unlock(&conn->session->lock);
  702. break;
  703. case ISCSI_OP_SCSI_CMD_RSP:
  704. if (tcp_conn->in.datalen) {
  705. iscsi_tcp_data_recv_prep(tcp_conn);
  706. return 0;
  707. }
  708. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  709. break;
  710. case ISCSI_OP_R2T:
  711. spin_lock(&conn->session->lock);
  712. task = iscsi_itt_to_ctask(conn, hdr->itt);
  713. if (!task)
  714. rc = ISCSI_ERR_BAD_ITT;
  715. else if (ahslen)
  716. rc = ISCSI_ERR_AHSLEN;
  717. else if (task->sc->sc_data_direction == DMA_TO_DEVICE)
  718. rc = iscsi_r2t_rsp(conn, task);
  719. else
  720. rc = ISCSI_ERR_PROTO;
  721. spin_unlock(&conn->session->lock);
  722. break;
  723. case ISCSI_OP_LOGIN_RSP:
  724. case ISCSI_OP_TEXT_RSP:
  725. case ISCSI_OP_REJECT:
  726. case ISCSI_OP_ASYNC_EVENT:
  727. /*
  728. * It is possible that we could get a PDU with a buffer larger
  729. * than 8K, but there are no targets that currently do this.
  730. * For now we fail until we find a vendor that needs it
  731. */
  732. if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
  733. iscsi_conn_printk(KERN_ERR, conn,
  734. "iscsi_tcp: received buffer of "
  735. "len %u but conn buffer is only %u "
  736. "(opcode %0x)\n",
  737. tcp_conn->in.datalen,
  738. ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
  739. rc = ISCSI_ERR_PROTO;
  740. break;
  741. }
  742. /* If there's data coming in with the response,
  743. * receive it to the connection's buffer.
  744. */
  745. if (tcp_conn->in.datalen) {
  746. iscsi_tcp_data_recv_prep(tcp_conn);
  747. return 0;
  748. }
  749. /* fall through */
  750. case ISCSI_OP_LOGOUT_RSP:
  751. case ISCSI_OP_NOOP_IN:
  752. case ISCSI_OP_SCSI_TMFUNC_RSP:
  753. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  754. break;
  755. default:
  756. rc = ISCSI_ERR_BAD_OPCODE;
  757. break;
  758. }
  759. if (rc == 0) {
  760. /* Anything that comes with data should have
  761. * been handled above. */
  762. if (tcp_conn->in.datalen)
  763. return ISCSI_ERR_PROTO;
  764. iscsi_tcp_hdr_recv_prep(tcp_conn);
  765. }
  766. return rc;
  767. }
  768. /**
  769. * iscsi_tcp_hdr_recv_done - process PDU header
  770. *
  771. * This is the callback invoked when the PDU header has
  772. * been received. If the header is followed by additional
  773. * header segments, we go back for more data.
  774. */
  775. static int
  776. iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  777. struct iscsi_segment *segment)
  778. {
  779. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  780. struct iscsi_hdr *hdr;
  781. /* Check if there are additional header segments
  782. * *prior* to computing the digest, because we
  783. * may need to go back to the caller for more.
  784. */
  785. hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
  786. if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
  787. /* Bump the header length - the caller will
  788. * just loop around and get the AHS for us, and
  789. * call again. */
  790. unsigned int ahslen = hdr->hlength << 2;
  791. /* Make sure we don't overflow */
  792. if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
  793. return ISCSI_ERR_AHSLEN;
  794. segment->total_size += ahslen;
  795. segment->size += ahslen;
  796. return 0;
  797. }
  798. /* We're done processing the header. See if we're doing
  799. * header digests; if so, set up the recv_digest buffer
  800. * and go back for more. */
  801. if (conn->hdrdgst_en) {
  802. if (segment->digest_len == 0) {
  803. /*
  804. * Even if we offload the digest processing we
  805. * splice it in so we can increment the skb/segment
  806. * counters in preparation for the data segment.
  807. */
  808. iscsi_tcp_segment_splice_digest(segment,
  809. segment->recv_digest);
  810. return 0;
  811. }
  812. if (!(conn->session->tt->caps & CAP_DIGEST_OFFLOAD)) {
  813. iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
  814. segment->total_copied - ISCSI_DIGEST_SIZE,
  815. segment->digest);
  816. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  817. return ISCSI_ERR_HDR_DGST;
  818. }
  819. }
  820. tcp_conn->in.hdr = hdr;
  821. return iscsi_tcp_hdr_dissect(conn, hdr);
  822. }
  823. inline int iscsi_tcp_recv_segment_is_hdr(struct iscsi_tcp_conn *tcp_conn)
  824. {
  825. return tcp_conn->in.segment.done == iscsi_tcp_hdr_recv_done;
  826. }
  827. enum {
  828. ISCSI_TCP_SEGMENT_DONE, /* curr seg has been processed */
  829. ISCSI_TCP_SKB_DONE, /* skb is out of data */
  830. ISCSI_TCP_CONN_ERR, /* iscsi layer has fired a conn err */
  831. ISCSI_TCP_SUSPENDED, /* conn is suspended */
  832. };
  833. /**
  834. * iscsi_tcp_recv_skb - Process skb
  835. * @conn: iscsi connection
  836. * @skb: network buffer with header and/or data segment
  837. * @offset: offset in skb
  838. * @offload: bool indicating if transfer was offloaded
  839. */
  840. int iscsi_tcp_recv_skb(struct iscsi_conn *conn, struct sk_buff *skb,
  841. unsigned int offset, bool offloaded, int *status)
  842. {
  843. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  844. struct iscsi_segment *segment = &tcp_conn->in.segment;
  845. struct skb_seq_state seq;
  846. unsigned int consumed = 0;
  847. int rc = 0;
  848. debug_tcp("in %d bytes\n", skb->len - offset);
  849. if (unlikely(conn->suspend_rx)) {
  850. debug_tcp("conn %d Rx suspended!\n", conn->id);
  851. *status = ISCSI_TCP_SUSPENDED;
  852. return 0;
  853. }
  854. if (offloaded) {
  855. segment->total_copied = segment->total_size;
  856. goto segment_done;
  857. }
  858. skb_prepare_seq_read(skb, offset, skb->len, &seq);
  859. while (1) {
  860. unsigned int avail;
  861. const u8 *ptr;
  862. avail = skb_seq_read(consumed, &ptr, &seq);
  863. if (avail == 0) {
  864. debug_tcp("no more data avail. Consumed %d\n",
  865. consumed);
  866. *status = ISCSI_TCP_SKB_DONE;
  867. skb_abort_seq_read(&seq);
  868. goto skb_done;
  869. }
  870. BUG_ON(segment->copied >= segment->size);
  871. debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
  872. rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
  873. BUG_ON(rc == 0);
  874. consumed += rc;
  875. if (segment->total_copied >= segment->total_size) {
  876. skb_abort_seq_read(&seq);
  877. goto segment_done;
  878. }
  879. }
  880. segment_done:
  881. *status = ISCSI_TCP_SEGMENT_DONE;
  882. debug_tcp("segment done\n");
  883. rc = segment->done(tcp_conn, segment);
  884. if (rc != 0) {
  885. *status = ISCSI_TCP_CONN_ERR;
  886. debug_tcp("Error receiving PDU, errno=%d\n", rc);
  887. iscsi_conn_failure(conn, rc);
  888. return 0;
  889. }
  890. /* The done() functions sets up the next segment. */
  891. skb_done:
  892. conn->rxdata_octets += consumed;
  893. return consumed;
  894. }
  895. EXPORT_SYMBOL_GPL(iscsi_tcp_recv_skb);
  896. /**
  897. * iscsi_tcp_recv - TCP receive in sendfile fashion
  898. * @rd_desc: read descriptor
  899. * @skb: socket buffer
  900. * @offset: offset in skb
  901. * @len: skb->len - offset
  902. **/
  903. static int
  904. iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  905. unsigned int offset, size_t len)
  906. {
  907. struct iscsi_conn *conn = rd_desc->arg.data;
  908. unsigned int consumed, total_consumed = 0;
  909. int status;
  910. debug_tcp("in %d bytes\n", skb->len - offset);
  911. do {
  912. status = 0;
  913. consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
  914. offset += consumed;
  915. total_consumed += consumed;
  916. } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
  917. debug_tcp("read %d bytes status %d\n", skb->len - offset, status);
  918. return total_consumed;
  919. }
  920. static void
  921. iscsi_tcp_data_ready(struct sock *sk, int flag)
  922. {
  923. struct iscsi_conn *conn = sk->sk_user_data;
  924. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  925. read_descriptor_t rd_desc;
  926. read_lock(&sk->sk_callback_lock);
  927. /*
  928. * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
  929. * We set count to 1 because we want the network layer to
  930. * hand us all the skbs that are available. iscsi_tcp_recv
  931. * handled pdus that cross buffers or pdus that still need data.
  932. */
  933. rd_desc.arg.data = conn;
  934. rd_desc.count = 1;
  935. tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
  936. read_unlock(&sk->sk_callback_lock);
  937. /* If we had to (atomically) map a highmem page,
  938. * unmap it now. */
  939. iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
  940. }
  941. static void
  942. iscsi_tcp_state_change(struct sock *sk)
  943. {
  944. struct iscsi_tcp_conn *tcp_conn;
  945. struct iscsi_conn *conn;
  946. struct iscsi_session *session;
  947. void (*old_state_change)(struct sock *);
  948. read_lock(&sk->sk_callback_lock);
  949. conn = (struct iscsi_conn*)sk->sk_user_data;
  950. session = conn->session;
  951. if ((sk->sk_state == TCP_CLOSE_WAIT ||
  952. sk->sk_state == TCP_CLOSE) &&
  953. !atomic_read(&sk->sk_rmem_alloc)) {
  954. debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
  955. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  956. }
  957. tcp_conn = conn->dd_data;
  958. old_state_change = tcp_conn->old_state_change;
  959. read_unlock(&sk->sk_callback_lock);
  960. old_state_change(sk);
  961. }
  962. /**
  963. * iscsi_write_space - Called when more output buffer space is available
  964. * @sk: socket space is available for
  965. **/
  966. static void
  967. iscsi_write_space(struct sock *sk)
  968. {
  969. struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
  970. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  971. tcp_conn->old_write_space(sk);
  972. debug_tcp("iscsi_write_space: cid %d\n", conn->id);
  973. scsi_queue_work(conn->session->host, &conn->xmitwork);
  974. }
  975. static void
  976. iscsi_conn_set_callbacks(struct iscsi_conn *conn)
  977. {
  978. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  979. struct sock *sk = tcp_conn->sock->sk;
  980. /* assign new callbacks */
  981. write_lock_bh(&sk->sk_callback_lock);
  982. sk->sk_user_data = conn;
  983. tcp_conn->old_data_ready = sk->sk_data_ready;
  984. tcp_conn->old_state_change = sk->sk_state_change;
  985. tcp_conn->old_write_space = sk->sk_write_space;
  986. sk->sk_data_ready = iscsi_tcp_data_ready;
  987. sk->sk_state_change = iscsi_tcp_state_change;
  988. sk->sk_write_space = iscsi_write_space;
  989. write_unlock_bh(&sk->sk_callback_lock);
  990. }
  991. static void
  992. iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
  993. {
  994. struct sock *sk = tcp_conn->sock->sk;
  995. /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
  996. write_lock_bh(&sk->sk_callback_lock);
  997. sk->sk_user_data = NULL;
  998. sk->sk_data_ready = tcp_conn->old_data_ready;
  999. sk->sk_state_change = tcp_conn->old_state_change;
  1000. sk->sk_write_space = tcp_conn->old_write_space;
  1001. sk->sk_no_check = 0;
  1002. write_unlock_bh(&sk->sk_callback_lock);
  1003. }
  1004. /**
  1005. * iscsi_xmit - TCP transmit
  1006. **/
  1007. static int
  1008. iscsi_xmit(struct iscsi_conn *conn)
  1009. {
  1010. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1011. struct iscsi_segment *segment = &tcp_conn->out.segment;
  1012. unsigned int consumed = 0;
  1013. int rc = 0;
  1014. while (1) {
  1015. rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
  1016. if (rc < 0) {
  1017. rc = ISCSI_ERR_XMIT_FAILED;
  1018. goto error;
  1019. }
  1020. if (rc == 0)
  1021. break;
  1022. consumed += rc;
  1023. if (segment->total_copied >= segment->total_size) {
  1024. if (segment->done != NULL) {
  1025. rc = segment->done(tcp_conn, segment);
  1026. if (rc != 0)
  1027. goto error;
  1028. }
  1029. }
  1030. }
  1031. debug_tcp("xmit %d bytes\n", consumed);
  1032. conn->txdata_octets += consumed;
  1033. return consumed;
  1034. error:
  1035. /* Transmit error. We could initiate error recovery
  1036. * here. */
  1037. debug_tcp("Error sending PDU, errno=%d\n", rc);
  1038. iscsi_conn_failure(conn, rc);
  1039. return -EIO;
  1040. }
  1041. /**
  1042. * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
  1043. */
  1044. static inline int
  1045. iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
  1046. {
  1047. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1048. struct iscsi_segment *segment = &tcp_conn->out.segment;
  1049. return segment->total_copied - segment->total_size;
  1050. }
  1051. static inline int
  1052. iscsi_tcp_flush(struct iscsi_conn *conn)
  1053. {
  1054. int rc;
  1055. while (iscsi_tcp_xmit_qlen(conn)) {
  1056. rc = iscsi_xmit(conn);
  1057. if (rc == 0)
  1058. return -EAGAIN;
  1059. if (rc < 0)
  1060. return rc;
  1061. }
  1062. return 0;
  1063. }
  1064. /*
  1065. * This is called when we're done sending the header.
  1066. * Simply copy the data_segment to the send segment, and return.
  1067. */
  1068. static int
  1069. iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
  1070. struct iscsi_segment *segment)
  1071. {
  1072. tcp_conn->out.segment = tcp_conn->out.data_segment;
  1073. debug_tcp("Header done. Next segment size %u total_size %u\n",
  1074. tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
  1075. return 0;
  1076. }
  1077. static void
  1078. iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
  1079. {
  1080. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1081. debug_tcp("%s(%p%s)\n", __func__, tcp_conn,
  1082. conn->hdrdgst_en? ", digest enabled" : "");
  1083. /* Clear the data segment - needs to be filled in by the
  1084. * caller using iscsi_tcp_send_data_prep() */
  1085. memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
  1086. /* If header digest is enabled, compute the CRC and
  1087. * place the digest into the same buffer. We make
  1088. * sure that both iscsi_tcp_task and mtask have
  1089. * sufficient room.
  1090. */
  1091. if (conn->hdrdgst_en) {
  1092. iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
  1093. hdr + hdrlen);
  1094. hdrlen += ISCSI_DIGEST_SIZE;
  1095. }
  1096. /* Remember header pointer for later, when we need
  1097. * to decide whether there's a payload to go along
  1098. * with the header. */
  1099. tcp_conn->out.hdr = hdr;
  1100. iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
  1101. iscsi_tcp_send_hdr_done, NULL);
  1102. }
  1103. /*
  1104. * Prepare the send buffer for the payload data.
  1105. * Padding and checksumming will all be taken care
  1106. * of by the iscsi_segment routines.
  1107. */
  1108. static int
  1109. iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
  1110. unsigned int count, unsigned int offset,
  1111. unsigned int len)
  1112. {
  1113. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1114. struct hash_desc *tx_hash = NULL;
  1115. unsigned int hdr_spec_len;
  1116. debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __func__,
  1117. tcp_conn, offset, len,
  1118. conn->datadgst_en? ", digest enabled" : "");
  1119. /* Make sure the datalen matches what the caller
  1120. said he would send. */
  1121. hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
  1122. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  1123. if (conn->datadgst_en)
  1124. tx_hash = &tcp_conn->tx_hash;
  1125. return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
  1126. sg, count, offset, len,
  1127. NULL, tx_hash);
  1128. }
  1129. static void
  1130. iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
  1131. size_t len)
  1132. {
  1133. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1134. struct hash_desc *tx_hash = NULL;
  1135. unsigned int hdr_spec_len;
  1136. debug_tcp("%s(%p, datalen=%d%s)\n", __func__, tcp_conn, len,
  1137. conn->datadgst_en? ", digest enabled" : "");
  1138. /* Make sure the datalen matches what the caller
  1139. said he would send. */
  1140. hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
  1141. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  1142. if (conn->datadgst_en)
  1143. tx_hash = &tcp_conn->tx_hash;
  1144. iscsi_segment_init_linear(&tcp_conn->out.data_segment,
  1145. data, len, NULL, tx_hash);
  1146. }
  1147. /**
  1148. * iscsi_solicit_data_cont - initialize next Data-Out
  1149. * @conn: iscsi connection
  1150. * @task: scsi command task
  1151. * @r2t: R2T info
  1152. * @left: bytes left to transfer
  1153. *
  1154. * Notes:
  1155. * Initialize next Data-Out within this R2T sequence and continue
  1156. * to process next Scatter-Gather element(if any) of this SCSI command.
  1157. *
  1158. * Called under connection lock.
  1159. **/
  1160. static int
  1161. iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_task *task,
  1162. struct iscsi_r2t_info *r2t)
  1163. {
  1164. struct iscsi_data *hdr;
  1165. int new_offset, left;
  1166. BUG_ON(r2t->data_length - r2t->sent < 0);
  1167. left = r2t->data_length - r2t->sent;
  1168. if (left == 0)
  1169. return 0;
  1170. hdr = &r2t->dtask.hdr;
  1171. memset(hdr, 0, sizeof(struct iscsi_data));
  1172. hdr->ttt = r2t->ttt;
  1173. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  1174. r2t->solicit_datasn++;
  1175. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  1176. memcpy(hdr->lun, task->hdr->lun, sizeof(hdr->lun));
  1177. hdr->itt = task->hdr->itt;
  1178. hdr->exp_statsn = r2t->exp_statsn;
  1179. new_offset = r2t->data_offset + r2t->sent;
  1180. hdr->offset = cpu_to_be32(new_offset);
  1181. if (left > conn->max_xmit_dlength) {
  1182. hton24(hdr->dlength, conn->max_xmit_dlength);
  1183. r2t->data_count = conn->max_xmit_dlength;
  1184. } else {
  1185. hton24(hdr->dlength, left);
  1186. r2t->data_count = left;
  1187. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  1188. }
  1189. conn->dataout_pdus_cnt++;
  1190. return 1;
  1191. }
  1192. /**
  1193. * iscsi_tcp_task - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
  1194. * @conn: iscsi connection
  1195. * @task: scsi command task
  1196. * @sc: scsi command
  1197. **/
  1198. static int
  1199. iscsi_tcp_task_init(struct iscsi_task *task)
  1200. {
  1201. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1202. struct iscsi_conn *conn = task->conn;
  1203. struct scsi_cmnd *sc = task->sc;
  1204. int err;
  1205. if (!sc) {
  1206. /*
  1207. * mgmt tasks do not have a scatterlist since they come
  1208. * in from the iscsi interface.
  1209. */
  1210. debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id,
  1211. task->itt);
  1212. /* Prepare PDU, optionally w/ immediate data */
  1213. iscsi_tcp_send_hdr_prep(conn, task->hdr, sizeof(*task->hdr));
  1214. /* If we have immediate data, attach a payload */
  1215. if (task->data_count)
  1216. iscsi_tcp_send_linear_data_prepare(conn, task->data,
  1217. task->data_count);
  1218. return 0;
  1219. }
  1220. BUG_ON(__kfifo_len(tcp_task->r2tqueue));
  1221. tcp_task->sent = 0;
  1222. tcp_task->exp_datasn = 0;
  1223. /* Prepare PDU, optionally w/ immediate data */
  1224. debug_scsi("task deq [cid %d itt 0x%x imm %d unsol %d]\n",
  1225. conn->id, task->itt, task->imm_count,
  1226. task->unsol_count);
  1227. iscsi_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
  1228. if (!task->imm_count)
  1229. return 0;
  1230. /* If we have immediate data, attach a payload */
  1231. err = iscsi_tcp_send_data_prep(conn, scsi_out(sc)->table.sgl,
  1232. scsi_out(sc)->table.nents,
  1233. 0, task->imm_count);
  1234. if (err)
  1235. return err;
  1236. tcp_task->sent += task->imm_count;
  1237. task->imm_count = 0;
  1238. return 0;
  1239. }
  1240. /*
  1241. * iscsi_tcp_task_xmit - xmit normal PDU task
  1242. * @task: iscsi command task
  1243. *
  1244. * We're expected to return 0 when everything was transmitted succesfully,
  1245. * -EAGAIN if there's still data in the queue, or != 0 for any other kind
  1246. * of error.
  1247. */
  1248. static int
  1249. iscsi_tcp_task_xmit(struct iscsi_task *task)
  1250. {
  1251. struct iscsi_conn *conn = task->conn;
  1252. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1253. struct scsi_cmnd *sc = task->sc;
  1254. struct scsi_data_buffer *sdb;
  1255. int rc = 0;
  1256. flush:
  1257. /* Flush any pending data first. */
  1258. rc = iscsi_tcp_flush(conn);
  1259. if (rc < 0)
  1260. return rc;
  1261. /* mgmt command */
  1262. if (!sc) {
  1263. if (task->hdr->itt == RESERVED_ITT)
  1264. iscsi_put_task(task);
  1265. return 0;
  1266. }
  1267. /* Are we done already? */
  1268. if (sc->sc_data_direction != DMA_TO_DEVICE)
  1269. return 0;
  1270. sdb = scsi_out(sc);
  1271. if (task->unsol_count != 0) {
  1272. struct iscsi_data *hdr = &tcp_task->unsol_dtask.hdr;
  1273. /* Prepare a header for the unsolicited PDU.
  1274. * The amount of data we want to send will be
  1275. * in task->data_count.
  1276. * FIXME: return the data count instead.
  1277. */
  1278. iscsi_prep_unsolicit_data_pdu(task, hdr);
  1279. debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
  1280. task->itt, tcp_task->sent, task->data_count);
  1281. iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
  1282. rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
  1283. sdb->table.nents, tcp_task->sent,
  1284. task->data_count);
  1285. if (rc)
  1286. goto fail;
  1287. tcp_task->sent += task->data_count;
  1288. task->unsol_count -= task->data_count;
  1289. goto flush;
  1290. } else {
  1291. struct iscsi_session *session = conn->session;
  1292. struct iscsi_r2t_info *r2t;
  1293. /* All unsolicited PDUs sent. Check for solicited PDUs.
  1294. */
  1295. spin_lock_bh(&session->lock);
  1296. r2t = tcp_task->r2t;
  1297. if (r2t != NULL) {
  1298. /* Continue with this R2T? */
  1299. if (!iscsi_solicit_data_cont(conn, task, r2t)) {
  1300. debug_scsi(" done with r2t %p\n", r2t);
  1301. __kfifo_put(tcp_task->r2tpool.queue,
  1302. (void*)&r2t, sizeof(void*));
  1303. tcp_task->r2t = r2t = NULL;
  1304. }
  1305. }
  1306. if (r2t == NULL) {
  1307. __kfifo_get(tcp_task->r2tqueue, (void*)&tcp_task->r2t,
  1308. sizeof(void*));
  1309. r2t = tcp_task->r2t;
  1310. }
  1311. spin_unlock_bh(&session->lock);
  1312. /* Waiting for more R2Ts to arrive. */
  1313. if (r2t == NULL) {
  1314. debug_tcp("no R2Ts yet\n");
  1315. return 0;
  1316. }
  1317. debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
  1318. r2t, r2t->solicit_datasn - 1, task->itt,
  1319. r2t->data_offset + r2t->sent, r2t->data_count);
  1320. iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
  1321. sizeof(struct iscsi_hdr));
  1322. rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
  1323. sdb->table.nents,
  1324. r2t->data_offset + r2t->sent,
  1325. r2t->data_count);
  1326. if (rc)
  1327. goto fail;
  1328. tcp_task->sent += r2t->data_count;
  1329. r2t->sent += r2t->data_count;
  1330. goto flush;
  1331. }
  1332. return 0;
  1333. fail:
  1334. iscsi_conn_failure(conn, rc);
  1335. return -EIO;
  1336. }
  1337. static struct iscsi_cls_conn *
  1338. iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1339. {
  1340. struct iscsi_conn *conn;
  1341. struct iscsi_cls_conn *cls_conn;
  1342. struct iscsi_tcp_conn *tcp_conn;
  1343. cls_conn = iscsi_conn_setup(cls_session, sizeof(*tcp_conn), conn_idx);
  1344. if (!cls_conn)
  1345. return NULL;
  1346. conn = cls_conn->dd_data;
  1347. /*
  1348. * due to strange issues with iser these are not set
  1349. * in iscsi_conn_setup
  1350. */
  1351. conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
  1352. tcp_conn = conn->dd_data;
  1353. tcp_conn->iscsi_conn = conn;
  1354. tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1355. CRYPTO_ALG_ASYNC);
  1356. tcp_conn->tx_hash.flags = 0;
  1357. if (IS_ERR(tcp_conn->tx_hash.tfm))
  1358. goto free_conn;
  1359. tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1360. CRYPTO_ALG_ASYNC);
  1361. tcp_conn->rx_hash.flags = 0;
  1362. if (IS_ERR(tcp_conn->rx_hash.tfm))
  1363. goto free_tx_tfm;
  1364. return cls_conn;
  1365. free_tx_tfm:
  1366. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1367. free_conn:
  1368. iscsi_conn_printk(KERN_ERR, conn,
  1369. "Could not create connection due to crc32c "
  1370. "loading error. Make sure the crc32c "
  1371. "module is built as a module or into the "
  1372. "kernel\n");
  1373. iscsi_conn_teardown(cls_conn);
  1374. return NULL;
  1375. }
  1376. static void
  1377. iscsi_tcp_release_conn(struct iscsi_conn *conn)
  1378. {
  1379. struct iscsi_session *session = conn->session;
  1380. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1381. struct socket *sock = tcp_conn->sock;
  1382. if (!sock)
  1383. return;
  1384. sock_hold(sock->sk);
  1385. iscsi_conn_restore_callbacks(tcp_conn);
  1386. sock_put(sock->sk);
  1387. spin_lock_bh(&session->lock);
  1388. tcp_conn->sock = NULL;
  1389. spin_unlock_bh(&session->lock);
  1390. sockfd_put(sock);
  1391. }
  1392. static void
  1393. iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1394. {
  1395. struct iscsi_conn *conn = cls_conn->dd_data;
  1396. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1397. iscsi_tcp_release_conn(conn);
  1398. if (tcp_conn->tx_hash.tfm)
  1399. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1400. if (tcp_conn->rx_hash.tfm)
  1401. crypto_free_hash(tcp_conn->rx_hash.tfm);
  1402. iscsi_conn_teardown(cls_conn);
  1403. }
  1404. static void
  1405. iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1406. {
  1407. struct iscsi_conn *conn = cls_conn->dd_data;
  1408. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1409. /* userspace may have goofed up and not bound us */
  1410. if (!tcp_conn->sock)
  1411. return;
  1412. /*
  1413. * Make sure our recv side is stopped.
  1414. * Older tools called conn stop before ep_disconnect
  1415. * so IO could still be coming in.
  1416. */
  1417. write_lock_bh(&tcp_conn->sock->sk->sk_callback_lock);
  1418. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1419. write_unlock_bh(&tcp_conn->sock->sk->sk_callback_lock);
  1420. iscsi_conn_stop(cls_conn, flag);
  1421. iscsi_tcp_release_conn(conn);
  1422. }
  1423. static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
  1424. char *buf, int *port,
  1425. int (*getname)(struct socket *, struct sockaddr *,
  1426. int *addrlen))
  1427. {
  1428. struct sockaddr_storage *addr;
  1429. struct sockaddr_in6 *sin6;
  1430. struct sockaddr_in *sin;
  1431. int rc = 0, len;
  1432. addr = kmalloc(sizeof(*addr), GFP_KERNEL);
  1433. if (!addr)
  1434. return -ENOMEM;
  1435. if (getname(sock, (struct sockaddr *) addr, &len)) {
  1436. rc = -ENODEV;
  1437. goto free_addr;
  1438. }
  1439. switch (addr->ss_family) {
  1440. case AF_INET:
  1441. sin = (struct sockaddr_in *)addr;
  1442. spin_lock_bh(&conn->session->lock);
  1443. sprintf(buf, "%pI4", &sin->sin_addr.s_addr);
  1444. *port = be16_to_cpu(sin->sin_port);
  1445. spin_unlock_bh(&conn->session->lock);
  1446. break;
  1447. case AF_INET6:
  1448. sin6 = (struct sockaddr_in6 *)addr;
  1449. spin_lock_bh(&conn->session->lock);
  1450. sprintf(buf, "%pI6", &sin6->sin6_addr);
  1451. *port = be16_to_cpu(sin6->sin6_port);
  1452. spin_unlock_bh(&conn->session->lock);
  1453. break;
  1454. }
  1455. free_addr:
  1456. kfree(addr);
  1457. return rc;
  1458. }
  1459. static int
  1460. iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  1461. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  1462. int is_leading)
  1463. {
  1464. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1465. struct iscsi_host *ihost = shost_priv(shost);
  1466. struct iscsi_conn *conn = cls_conn->dd_data;
  1467. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1468. struct sock *sk;
  1469. struct socket *sock;
  1470. int err;
  1471. /* lookup for existing socket */
  1472. sock = sockfd_lookup((int)transport_eph, &err);
  1473. if (!sock) {
  1474. iscsi_conn_printk(KERN_ERR, conn,
  1475. "sockfd_lookup failed %d\n", err);
  1476. return -EEXIST;
  1477. }
  1478. /*
  1479. * copy these values now because if we drop the session
  1480. * userspace may still want to query the values since we will
  1481. * be using them for the reconnect
  1482. */
  1483. err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
  1484. &conn->portal_port, kernel_getpeername);
  1485. if (err)
  1486. goto free_socket;
  1487. err = iscsi_tcp_get_addr(conn, sock, ihost->local_address,
  1488. &ihost->local_port, kernel_getsockname);
  1489. if (err)
  1490. goto free_socket;
  1491. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  1492. if (err)
  1493. goto free_socket;
  1494. /* bind iSCSI connection and socket */
  1495. tcp_conn->sock = sock;
  1496. /* setup Socket parameters */
  1497. sk = sock->sk;
  1498. sk->sk_reuse = 1;
  1499. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  1500. sk->sk_allocation = GFP_ATOMIC;
  1501. iscsi_conn_set_callbacks(conn);
  1502. tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
  1503. /*
  1504. * set receive state machine into initial state
  1505. */
  1506. iscsi_tcp_hdr_recv_prep(tcp_conn);
  1507. return 0;
  1508. free_socket:
  1509. sockfd_put(sock);
  1510. return err;
  1511. }
  1512. static int
  1513. iscsi_r2tpool_alloc(struct iscsi_session *session)
  1514. {
  1515. int i;
  1516. int cmd_i;
  1517. /*
  1518. * initialize per-task: R2T pool and xmit queue
  1519. */
  1520. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1521. struct iscsi_task *task = session->cmds[cmd_i];
  1522. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1523. /*
  1524. * pre-allocated x4 as much r2ts to handle race when
  1525. * target acks DataOut faster than we data_xmit() queues
  1526. * could replenish r2tqueue.
  1527. */
  1528. /* R2T pool */
  1529. if (iscsi_pool_init(&tcp_task->r2tpool, session->max_r2t * 4, NULL,
  1530. sizeof(struct iscsi_r2t_info))) {
  1531. goto r2t_alloc_fail;
  1532. }
  1533. /* R2T xmit queue */
  1534. tcp_task->r2tqueue = kfifo_alloc(
  1535. session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
  1536. if (tcp_task->r2tqueue == ERR_PTR(-ENOMEM)) {
  1537. iscsi_pool_free(&tcp_task->r2tpool);
  1538. goto r2t_alloc_fail;
  1539. }
  1540. }
  1541. return 0;
  1542. r2t_alloc_fail:
  1543. for (i = 0; i < cmd_i; i++) {
  1544. struct iscsi_task *task = session->cmds[i];
  1545. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1546. kfifo_free(tcp_task->r2tqueue);
  1547. iscsi_pool_free(&tcp_task->r2tpool);
  1548. }
  1549. return -ENOMEM;
  1550. }
  1551. static void
  1552. iscsi_r2tpool_free(struct iscsi_session *session)
  1553. {
  1554. int i;
  1555. for (i = 0; i < session->cmds_max; i++) {
  1556. struct iscsi_task *task = session->cmds[i];
  1557. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1558. kfifo_free(tcp_task->r2tqueue);
  1559. iscsi_pool_free(&tcp_task->r2tpool);
  1560. }
  1561. }
  1562. static int
  1563. iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
  1564. char *buf, int buflen)
  1565. {
  1566. struct iscsi_conn *conn = cls_conn->dd_data;
  1567. struct iscsi_session *session = conn->session;
  1568. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1569. int value;
  1570. switch(param) {
  1571. case ISCSI_PARAM_HDRDGST_EN:
  1572. iscsi_set_param(cls_conn, param, buf, buflen);
  1573. break;
  1574. case ISCSI_PARAM_DATADGST_EN:
  1575. iscsi_set_param(cls_conn, param, buf, buflen);
  1576. tcp_conn->sendpage = conn->datadgst_en ?
  1577. sock_no_sendpage : tcp_conn->sock->ops->sendpage;
  1578. break;
  1579. case ISCSI_PARAM_MAX_R2T:
  1580. sscanf(buf, "%d", &value);
  1581. if (value <= 0 || !is_power_of_2(value))
  1582. return -EINVAL;
  1583. if (session->max_r2t == value)
  1584. break;
  1585. iscsi_r2tpool_free(session);
  1586. iscsi_set_param(cls_conn, param, buf, buflen);
  1587. if (iscsi_r2tpool_alloc(session))
  1588. return -ENOMEM;
  1589. break;
  1590. default:
  1591. return iscsi_set_param(cls_conn, param, buf, buflen);
  1592. }
  1593. return 0;
  1594. }
  1595. static int
  1596. iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1597. enum iscsi_param param, char *buf)
  1598. {
  1599. struct iscsi_conn *conn = cls_conn->dd_data;
  1600. int len;
  1601. switch(param) {
  1602. case ISCSI_PARAM_CONN_PORT:
  1603. spin_lock_bh(&conn->session->lock);
  1604. len = sprintf(buf, "%hu\n", conn->portal_port);
  1605. spin_unlock_bh(&conn->session->lock);
  1606. break;
  1607. case ISCSI_PARAM_CONN_ADDRESS:
  1608. spin_lock_bh(&conn->session->lock);
  1609. len = sprintf(buf, "%s\n", conn->portal_address);
  1610. spin_unlock_bh(&conn->session->lock);
  1611. break;
  1612. default:
  1613. return iscsi_conn_get_param(cls_conn, param, buf);
  1614. }
  1615. return len;
  1616. }
  1617. static void
  1618. iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
  1619. {
  1620. struct iscsi_conn *conn = cls_conn->dd_data;
  1621. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1622. stats->txdata_octets = conn->txdata_octets;
  1623. stats->rxdata_octets = conn->rxdata_octets;
  1624. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1625. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1626. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1627. stats->datain_pdus = conn->datain_pdus_cnt;
  1628. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1629. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1630. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1631. stats->custom_length = 3;
  1632. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  1633. stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
  1634. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  1635. stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
  1636. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  1637. stats->custom[2].value = conn->eh_abort_cnt;
  1638. }
  1639. static struct iscsi_cls_session *
  1640. iscsi_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  1641. uint16_t qdepth, uint32_t initial_cmdsn,
  1642. uint32_t *hostno)
  1643. {
  1644. struct iscsi_cls_session *cls_session;
  1645. struct iscsi_session *session;
  1646. struct Scsi_Host *shost;
  1647. int cmd_i;
  1648. if (ep) {
  1649. printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
  1650. return NULL;
  1651. }
  1652. shost = iscsi_host_alloc(&iscsi_sht, 0, qdepth);
  1653. if (!shost)
  1654. return NULL;
  1655. shost->transportt = iscsi_tcp_scsi_transport;
  1656. shost->max_lun = iscsi_max_lun;
  1657. shost->max_id = 0;
  1658. shost->max_channel = 0;
  1659. shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
  1660. if (iscsi_host_add(shost, NULL))
  1661. goto free_host;
  1662. *hostno = shost->host_no;
  1663. cls_session = iscsi_session_setup(&iscsi_tcp_transport, shost, cmds_max,
  1664. sizeof(struct iscsi_tcp_task),
  1665. initial_cmdsn, 0);
  1666. if (!cls_session)
  1667. goto remove_host;
  1668. session = cls_session->dd_data;
  1669. shost->can_queue = session->scsi_cmds_max;
  1670. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1671. struct iscsi_task *task = session->cmds[cmd_i];
  1672. struct iscsi_tcp_task *tcp_task = task->dd_data;
  1673. task->hdr = &tcp_task->hdr.cmd_hdr;
  1674. task->hdr_max = sizeof(tcp_task->hdr) - ISCSI_DIGEST_SIZE;
  1675. }
  1676. if (iscsi_r2tpool_alloc(session))
  1677. goto remove_session;
  1678. return cls_session;
  1679. remove_session:
  1680. iscsi_session_teardown(cls_session);
  1681. remove_host:
  1682. iscsi_host_remove(shost);
  1683. free_host:
  1684. iscsi_host_free(shost);
  1685. return NULL;
  1686. }
  1687. static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  1688. {
  1689. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1690. iscsi_r2tpool_free(cls_session->dd_data);
  1691. iscsi_session_teardown(cls_session);
  1692. iscsi_host_remove(shost);
  1693. iscsi_host_free(shost);
  1694. }
  1695. static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
  1696. {
  1697. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
  1698. blk_queue_dma_alignment(sdev->request_queue, 0);
  1699. return 0;
  1700. }
  1701. static struct scsi_host_template iscsi_sht = {
  1702. .module = THIS_MODULE,
  1703. .name = "iSCSI Initiator over TCP/IP",
  1704. .queuecommand = iscsi_queuecommand,
  1705. .change_queue_depth = iscsi_change_queue_depth,
  1706. .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
  1707. .sg_tablesize = 4096,
  1708. .max_sectors = 0xFFFF,
  1709. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  1710. .eh_abort_handler = iscsi_eh_abort,
  1711. .eh_device_reset_handler= iscsi_eh_device_reset,
  1712. .eh_target_reset_handler= iscsi_eh_target_reset,
  1713. .use_clustering = DISABLE_CLUSTERING,
  1714. .slave_configure = iscsi_tcp_slave_configure,
  1715. .proc_name = "iscsi_tcp",
  1716. .this_id = -1,
  1717. };
  1718. static struct iscsi_transport iscsi_tcp_transport = {
  1719. .owner = THIS_MODULE,
  1720. .name = "tcp",
  1721. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  1722. | CAP_DATADGST,
  1723. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  1724. ISCSI_MAX_XMIT_DLENGTH |
  1725. ISCSI_HDRDGST_EN |
  1726. ISCSI_DATADGST_EN |
  1727. ISCSI_INITIAL_R2T_EN |
  1728. ISCSI_MAX_R2T |
  1729. ISCSI_IMM_DATA_EN |
  1730. ISCSI_FIRST_BURST |
  1731. ISCSI_MAX_BURST |
  1732. ISCSI_PDU_INORDER_EN |
  1733. ISCSI_DATASEQ_INORDER_EN |
  1734. ISCSI_ERL |
  1735. ISCSI_CONN_PORT |
  1736. ISCSI_CONN_ADDRESS |
  1737. ISCSI_EXP_STATSN |
  1738. ISCSI_PERSISTENT_PORT |
  1739. ISCSI_PERSISTENT_ADDRESS |
  1740. ISCSI_TARGET_NAME | ISCSI_TPGT |
  1741. ISCSI_USERNAME | ISCSI_PASSWORD |
  1742. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  1743. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  1744. ISCSI_LU_RESET_TMO |
  1745. ISCSI_PING_TMO | ISCSI_RECV_TMO |
  1746. ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
  1747. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  1748. ISCSI_HOST_INITIATOR_NAME |
  1749. ISCSI_HOST_NETDEV_NAME,
  1750. /* session management */
  1751. .create_session = iscsi_tcp_session_create,
  1752. .destroy_session = iscsi_tcp_session_destroy,
  1753. /* connection management */
  1754. .create_conn = iscsi_tcp_conn_create,
  1755. .bind_conn = iscsi_tcp_conn_bind,
  1756. .destroy_conn = iscsi_tcp_conn_destroy,
  1757. .set_param = iscsi_conn_set_param,
  1758. .get_conn_param = iscsi_tcp_conn_get_param,
  1759. .get_session_param = iscsi_session_get_param,
  1760. .start_conn = iscsi_conn_start,
  1761. .stop_conn = iscsi_tcp_conn_stop,
  1762. /* iscsi host params */
  1763. .get_host_param = iscsi_host_get_param,
  1764. .set_host_param = iscsi_host_set_param,
  1765. /* IO */
  1766. .send_pdu = iscsi_conn_send_pdu,
  1767. .get_stats = iscsi_conn_get_stats,
  1768. .init_task = iscsi_tcp_task_init,
  1769. .xmit_task = iscsi_tcp_task_xmit,
  1770. .cleanup_task = iscsi_tcp_cleanup_task,
  1771. /* recovery */
  1772. .session_recovery_timedout = iscsi_session_recovery_timedout,
  1773. };
  1774. static int __init
  1775. iscsi_tcp_init(void)
  1776. {
  1777. if (iscsi_max_lun < 1) {
  1778. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  1779. iscsi_max_lun);
  1780. return -EINVAL;
  1781. }
  1782. iscsi_tcp_scsi_transport = iscsi_register_transport(
  1783. &iscsi_tcp_transport);
  1784. if (!iscsi_tcp_scsi_transport)
  1785. return -ENODEV;
  1786. return 0;
  1787. }
  1788. static void __exit
  1789. iscsi_tcp_exit(void)
  1790. {
  1791. iscsi_unregister_transport(&iscsi_tcp_transport);
  1792. }
  1793. module_init(iscsi_tcp_init);
  1794. module_exit(iscsi_tcp_exit);