iscsi_tcp.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  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. rx_hash = &tcp_conn->rx_hash;
  435. iscsi_segment_init_linear(&tcp_conn->in.segment,
  436. conn->data, tcp_conn->in.datalen,
  437. iscsi_tcp_data_recv_done, rx_hash);
  438. }
  439. /*
  440. * must be called with session lock
  441. */
  442. static void
  443. iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  444. {
  445. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  446. struct iscsi_r2t_info *r2t;
  447. /* flush ctask's r2t queues */
  448. while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
  449. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  450. sizeof(void*));
  451. debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
  452. }
  453. r2t = tcp_ctask->r2t;
  454. if (r2t != NULL) {
  455. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  456. sizeof(void*));
  457. tcp_ctask->r2t = NULL;
  458. }
  459. }
  460. /**
  461. * iscsi_data_rsp - SCSI Data-In Response processing
  462. * @conn: iscsi connection
  463. * @ctask: scsi command task
  464. **/
  465. static int
  466. iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  467. {
  468. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  469. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  470. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
  471. struct iscsi_session *session = conn->session;
  472. struct scsi_cmnd *sc = ctask->sc;
  473. int datasn = be32_to_cpu(rhdr->datasn);
  474. unsigned total_in_length = scsi_in(sc)->length;
  475. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  476. if (tcp_conn->in.datalen == 0)
  477. return 0;
  478. if (tcp_ctask->exp_datasn != datasn) {
  479. debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
  480. __FUNCTION__, tcp_ctask->exp_datasn, datasn);
  481. return ISCSI_ERR_DATASN;
  482. }
  483. tcp_ctask->exp_datasn++;
  484. tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
  485. if (tcp_ctask->data_offset + tcp_conn->in.datalen > total_in_length) {
  486. debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
  487. __FUNCTION__, tcp_ctask->data_offset,
  488. tcp_conn->in.datalen, total_in_length);
  489. return ISCSI_ERR_DATA_OFFSET;
  490. }
  491. if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
  492. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  493. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  494. if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
  495. ISCSI_FLAG_DATA_OVERFLOW)) {
  496. int res_count = be32_to_cpu(rhdr->residual_count);
  497. if (res_count > 0 &&
  498. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  499. res_count <= total_in_length))
  500. scsi_in(sc)->resid = res_count;
  501. else
  502. sc->result = (DID_BAD_TARGET << 16) |
  503. rhdr->cmd_status;
  504. }
  505. }
  506. conn->datain_pdus_cnt++;
  507. return 0;
  508. }
  509. /**
  510. * iscsi_solicit_data_init - initialize first Data-Out
  511. * @conn: iscsi connection
  512. * @ctask: scsi command task
  513. * @r2t: R2T info
  514. *
  515. * Notes:
  516. * Initialize first Data-Out within this R2T sequence and finds
  517. * proper data_offset within this SCSI command.
  518. *
  519. * This function is called with connection lock taken.
  520. **/
  521. static void
  522. iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  523. struct iscsi_r2t_info *r2t)
  524. {
  525. struct iscsi_data *hdr;
  526. hdr = &r2t->dtask.hdr;
  527. memset(hdr, 0, sizeof(struct iscsi_data));
  528. hdr->ttt = r2t->ttt;
  529. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  530. r2t->solicit_datasn++;
  531. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  532. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  533. hdr->itt = ctask->hdr->itt;
  534. hdr->exp_statsn = r2t->exp_statsn;
  535. hdr->offset = cpu_to_be32(r2t->data_offset);
  536. if (r2t->data_length > conn->max_xmit_dlength) {
  537. hton24(hdr->dlength, conn->max_xmit_dlength);
  538. r2t->data_count = conn->max_xmit_dlength;
  539. hdr->flags = 0;
  540. } else {
  541. hton24(hdr->dlength, r2t->data_length);
  542. r2t->data_count = r2t->data_length;
  543. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  544. }
  545. conn->dataout_pdus_cnt++;
  546. r2t->sent = 0;
  547. }
  548. /**
  549. * iscsi_r2t_rsp - iSCSI R2T Response processing
  550. * @conn: iscsi connection
  551. * @ctask: scsi command task
  552. **/
  553. static int
  554. iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  555. {
  556. struct iscsi_r2t_info *r2t;
  557. struct iscsi_session *session = conn->session;
  558. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  559. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  560. struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
  561. int r2tsn = be32_to_cpu(rhdr->r2tsn);
  562. int rc;
  563. if (tcp_conn->in.datalen) {
  564. iscsi_conn_printk(KERN_ERR, conn,
  565. "invalid R2t with datalen %d\n",
  566. tcp_conn->in.datalen);
  567. return ISCSI_ERR_DATALEN;
  568. }
  569. if (tcp_ctask->exp_datasn != r2tsn){
  570. debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
  571. __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
  572. return ISCSI_ERR_R2TSN;
  573. }
  574. /* fill-in new R2T associated with the task */
  575. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  576. if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) {
  577. iscsi_conn_printk(KERN_INFO, conn,
  578. "dropping R2T itt %d in recovery.\n",
  579. ctask->itt);
  580. return 0;
  581. }
  582. rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
  583. BUG_ON(!rc);
  584. r2t->exp_statsn = rhdr->statsn;
  585. r2t->data_length = be32_to_cpu(rhdr->data_length);
  586. if (r2t->data_length == 0) {
  587. iscsi_conn_printk(KERN_ERR, conn,
  588. "invalid R2T with zero data len\n");
  589. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  590. sizeof(void*));
  591. return ISCSI_ERR_DATALEN;
  592. }
  593. if (r2t->data_length > session->max_burst)
  594. debug_scsi("invalid R2T with data len %u and max burst %u."
  595. "Attempting to execute request.\n",
  596. r2t->data_length, session->max_burst);
  597. r2t->data_offset = be32_to_cpu(rhdr->data_offset);
  598. if (r2t->data_offset + r2t->data_length > scsi_out(ctask->sc)->length) {
  599. iscsi_conn_printk(KERN_ERR, conn,
  600. "invalid R2T with data len %u at offset %u "
  601. "and total length %d\n", r2t->data_length,
  602. r2t->data_offset, scsi_out(ctask->sc)->length);
  603. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  604. sizeof(void*));
  605. return ISCSI_ERR_DATALEN;
  606. }
  607. r2t->ttt = rhdr->ttt; /* no flip */
  608. r2t->solicit_datasn = 0;
  609. iscsi_solicit_data_init(conn, ctask, r2t);
  610. tcp_ctask->exp_datasn = r2tsn + 1;
  611. __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
  612. conn->r2t_pdus_cnt++;
  613. iscsi_requeue_ctask(ctask);
  614. return 0;
  615. }
  616. /*
  617. * Handle incoming reply to DataIn command
  618. */
  619. static int
  620. iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
  621. struct iscsi_segment *segment)
  622. {
  623. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  624. struct iscsi_hdr *hdr = tcp_conn->in.hdr;
  625. int rc;
  626. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  627. return ISCSI_ERR_DATA_DGST;
  628. /* check for non-exceptional status */
  629. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  630. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
  631. if (rc)
  632. return rc;
  633. }
  634. iscsi_tcp_hdr_recv_prep(tcp_conn);
  635. return 0;
  636. }
  637. /**
  638. * iscsi_tcp_hdr_dissect - process PDU header
  639. * @conn: iSCSI connection
  640. * @hdr: PDU header
  641. *
  642. * This function analyzes the header of the PDU received,
  643. * and performs several sanity checks. If the PDU is accompanied
  644. * by data, the receive buffer is set up to copy the incoming data
  645. * to the correct location.
  646. */
  647. static int
  648. iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  649. {
  650. int rc = 0, opcode, ahslen;
  651. struct iscsi_session *session = conn->session;
  652. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  653. struct iscsi_cmd_task *ctask;
  654. uint32_t itt;
  655. /* verify PDU length */
  656. tcp_conn->in.datalen = ntoh24(hdr->dlength);
  657. if (tcp_conn->in.datalen > conn->max_recv_dlength) {
  658. iscsi_conn_printk(KERN_ERR, conn,
  659. "iscsi_tcp: datalen %d > %d\n",
  660. tcp_conn->in.datalen, conn->max_recv_dlength);
  661. return ISCSI_ERR_DATALEN;
  662. }
  663. /* Additional header segments. So far, we don't
  664. * process additional headers.
  665. */
  666. ahslen = hdr->hlength << 2;
  667. opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  668. /* verify itt (itt encoding: age+cid+itt) */
  669. rc = iscsi_verify_itt(conn, hdr, &itt);
  670. if (rc)
  671. return rc;
  672. debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
  673. opcode, ahslen, tcp_conn->in.datalen);
  674. switch(opcode) {
  675. case ISCSI_OP_SCSI_DATA_IN:
  676. ctask = session->cmds[itt];
  677. spin_lock(&conn->session->lock);
  678. rc = iscsi_data_rsp(conn, ctask);
  679. spin_unlock(&conn->session->lock);
  680. if (rc)
  681. return rc;
  682. if (tcp_conn->in.datalen) {
  683. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  684. struct hash_desc *rx_hash = NULL;
  685. struct scsi_data_buffer *sdb = scsi_in(ctask->sc);
  686. /*
  687. * Setup copy of Data-In into the Scsi_Cmnd
  688. * Scatterlist case:
  689. * We set up the iscsi_segment to point to the next
  690. * scatterlist entry to copy to. As we go along,
  691. * we move on to the next scatterlist entry and
  692. * update the digest per-entry.
  693. */
  694. if (conn->datadgst_en)
  695. rx_hash = &tcp_conn->rx_hash;
  696. debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
  697. "datalen=%d)\n", tcp_conn,
  698. tcp_ctask->data_offset,
  699. tcp_conn->in.datalen);
  700. return iscsi_segment_seek_sg(&tcp_conn->in.segment,
  701. sdb->table.sgl,
  702. sdb->table.nents,
  703. tcp_ctask->data_offset,
  704. tcp_conn->in.datalen,
  705. iscsi_tcp_process_data_in,
  706. rx_hash);
  707. }
  708. /* fall through */
  709. case ISCSI_OP_SCSI_CMD_RSP:
  710. if (tcp_conn->in.datalen) {
  711. iscsi_tcp_data_recv_prep(tcp_conn);
  712. return 0;
  713. }
  714. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  715. break;
  716. case ISCSI_OP_R2T:
  717. ctask = session->cmds[itt];
  718. if (ahslen)
  719. rc = ISCSI_ERR_AHSLEN;
  720. else if (ctask->sc->sc_data_direction == DMA_TO_DEVICE) {
  721. spin_lock(&session->lock);
  722. rc = iscsi_r2t_rsp(conn, ctask);
  723. spin_unlock(&session->lock);
  724. } else
  725. rc = ISCSI_ERR_PROTO;
  726. break;
  727. case ISCSI_OP_LOGIN_RSP:
  728. case ISCSI_OP_TEXT_RSP:
  729. case ISCSI_OP_REJECT:
  730. case ISCSI_OP_ASYNC_EVENT:
  731. /*
  732. * It is possible that we could get a PDU with a buffer larger
  733. * than 8K, but there are no targets that currently do this.
  734. * For now we fail until we find a vendor that needs it
  735. */
  736. if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
  737. iscsi_conn_printk(KERN_ERR, conn,
  738. "iscsi_tcp: received buffer of "
  739. "len %u but conn buffer is only %u "
  740. "(opcode %0x)\n",
  741. tcp_conn->in.datalen,
  742. ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
  743. rc = ISCSI_ERR_PROTO;
  744. break;
  745. }
  746. /* If there's data coming in with the response,
  747. * receive it to the connection's buffer.
  748. */
  749. if (tcp_conn->in.datalen) {
  750. iscsi_tcp_data_recv_prep(tcp_conn);
  751. return 0;
  752. }
  753. /* fall through */
  754. case ISCSI_OP_LOGOUT_RSP:
  755. case ISCSI_OP_NOOP_IN:
  756. case ISCSI_OP_SCSI_TMFUNC_RSP:
  757. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  758. break;
  759. default:
  760. rc = ISCSI_ERR_BAD_OPCODE;
  761. break;
  762. }
  763. if (rc == 0) {
  764. /* Anything that comes with data should have
  765. * been handled above. */
  766. if (tcp_conn->in.datalen)
  767. return ISCSI_ERR_PROTO;
  768. iscsi_tcp_hdr_recv_prep(tcp_conn);
  769. }
  770. return rc;
  771. }
  772. /**
  773. * iscsi_tcp_hdr_recv_done - process PDU header
  774. *
  775. * This is the callback invoked when the PDU header has
  776. * been received. If the header is followed by additional
  777. * header segments, we go back for more data.
  778. */
  779. static int
  780. iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  781. struct iscsi_segment *segment)
  782. {
  783. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  784. struct iscsi_hdr *hdr;
  785. /* Check if there are additional header segments
  786. * *prior* to computing the digest, because we
  787. * may need to go back to the caller for more.
  788. */
  789. hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
  790. if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
  791. /* Bump the header length - the caller will
  792. * just loop around and get the AHS for us, and
  793. * call again. */
  794. unsigned int ahslen = hdr->hlength << 2;
  795. /* Make sure we don't overflow */
  796. if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
  797. return ISCSI_ERR_AHSLEN;
  798. segment->total_size += ahslen;
  799. segment->size += ahslen;
  800. return 0;
  801. }
  802. /* We're done processing the header. See if we're doing
  803. * header digests; if so, set up the recv_digest buffer
  804. * and go back for more. */
  805. if (conn->hdrdgst_en) {
  806. if (segment->digest_len == 0) {
  807. iscsi_tcp_segment_splice_digest(segment,
  808. segment->recv_digest);
  809. return 0;
  810. }
  811. iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
  812. segment->total_copied - ISCSI_DIGEST_SIZE,
  813. segment->digest);
  814. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  815. return ISCSI_ERR_HDR_DGST;
  816. }
  817. tcp_conn->in.hdr = hdr;
  818. return iscsi_tcp_hdr_dissect(conn, hdr);
  819. }
  820. /**
  821. * iscsi_tcp_recv - TCP receive in sendfile fashion
  822. * @rd_desc: read descriptor
  823. * @skb: socket buffer
  824. * @offset: offset in skb
  825. * @len: skb->len - offset
  826. **/
  827. static int
  828. iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  829. unsigned int offset, size_t len)
  830. {
  831. struct iscsi_conn *conn = rd_desc->arg.data;
  832. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  833. struct iscsi_segment *segment = &tcp_conn->in.segment;
  834. struct skb_seq_state seq;
  835. unsigned int consumed = 0;
  836. int rc = 0;
  837. debug_tcp("in %d bytes\n", skb->len - offset);
  838. if (unlikely(conn->suspend_rx)) {
  839. debug_tcp("conn %d Rx suspended!\n", conn->id);
  840. return 0;
  841. }
  842. skb_prepare_seq_read(skb, offset, skb->len, &seq);
  843. while (1) {
  844. unsigned int avail;
  845. const u8 *ptr;
  846. avail = skb_seq_read(consumed, &ptr, &seq);
  847. if (avail == 0) {
  848. debug_tcp("no more data avail. Consumed %d\n",
  849. consumed);
  850. break;
  851. }
  852. BUG_ON(segment->copied >= segment->size);
  853. debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
  854. rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
  855. BUG_ON(rc == 0);
  856. consumed += rc;
  857. if (segment->total_copied >= segment->total_size) {
  858. debug_tcp("segment done\n");
  859. rc = segment->done(tcp_conn, segment);
  860. if (rc != 0) {
  861. skb_abort_seq_read(&seq);
  862. goto error;
  863. }
  864. /* The done() functions sets up the
  865. * next segment. */
  866. }
  867. }
  868. skb_abort_seq_read(&seq);
  869. conn->rxdata_octets += consumed;
  870. return consumed;
  871. error:
  872. debug_tcp("Error receiving PDU, errno=%d\n", rc);
  873. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  874. return 0;
  875. }
  876. static void
  877. iscsi_tcp_data_ready(struct sock *sk, int flag)
  878. {
  879. struct iscsi_conn *conn = sk->sk_user_data;
  880. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  881. read_descriptor_t rd_desc;
  882. read_lock(&sk->sk_callback_lock);
  883. /*
  884. * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
  885. * We set count to 1 because we want the network layer to
  886. * hand us all the skbs that are available. iscsi_tcp_recv
  887. * handled pdus that cross buffers or pdus that still need data.
  888. */
  889. rd_desc.arg.data = conn;
  890. rd_desc.count = 1;
  891. tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
  892. read_unlock(&sk->sk_callback_lock);
  893. /* If we had to (atomically) map a highmem page,
  894. * unmap it now. */
  895. iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
  896. }
  897. static void
  898. iscsi_tcp_state_change(struct sock *sk)
  899. {
  900. struct iscsi_tcp_conn *tcp_conn;
  901. struct iscsi_conn *conn;
  902. struct iscsi_session *session;
  903. void (*old_state_change)(struct sock *);
  904. read_lock(&sk->sk_callback_lock);
  905. conn = (struct iscsi_conn*)sk->sk_user_data;
  906. session = conn->session;
  907. if ((sk->sk_state == TCP_CLOSE_WAIT ||
  908. sk->sk_state == TCP_CLOSE) &&
  909. !atomic_read(&sk->sk_rmem_alloc)) {
  910. debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
  911. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  912. }
  913. tcp_conn = conn->dd_data;
  914. old_state_change = tcp_conn->old_state_change;
  915. read_unlock(&sk->sk_callback_lock);
  916. old_state_change(sk);
  917. }
  918. /**
  919. * iscsi_write_space - Called when more output buffer space is available
  920. * @sk: socket space is available for
  921. **/
  922. static void
  923. iscsi_write_space(struct sock *sk)
  924. {
  925. struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
  926. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  927. tcp_conn->old_write_space(sk);
  928. debug_tcp("iscsi_write_space: cid %d\n", conn->id);
  929. scsi_queue_work(conn->session->host, &conn->xmitwork);
  930. }
  931. static void
  932. iscsi_conn_set_callbacks(struct iscsi_conn *conn)
  933. {
  934. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  935. struct sock *sk = tcp_conn->sock->sk;
  936. /* assign new callbacks */
  937. write_lock_bh(&sk->sk_callback_lock);
  938. sk->sk_user_data = conn;
  939. tcp_conn->old_data_ready = sk->sk_data_ready;
  940. tcp_conn->old_state_change = sk->sk_state_change;
  941. tcp_conn->old_write_space = sk->sk_write_space;
  942. sk->sk_data_ready = iscsi_tcp_data_ready;
  943. sk->sk_state_change = iscsi_tcp_state_change;
  944. sk->sk_write_space = iscsi_write_space;
  945. write_unlock_bh(&sk->sk_callback_lock);
  946. }
  947. static void
  948. iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
  949. {
  950. struct sock *sk = tcp_conn->sock->sk;
  951. /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
  952. write_lock_bh(&sk->sk_callback_lock);
  953. sk->sk_user_data = NULL;
  954. sk->sk_data_ready = tcp_conn->old_data_ready;
  955. sk->sk_state_change = tcp_conn->old_state_change;
  956. sk->sk_write_space = tcp_conn->old_write_space;
  957. sk->sk_no_check = 0;
  958. write_unlock_bh(&sk->sk_callback_lock);
  959. }
  960. /**
  961. * iscsi_xmit - TCP transmit
  962. **/
  963. static int
  964. iscsi_xmit(struct iscsi_conn *conn)
  965. {
  966. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  967. struct iscsi_segment *segment = &tcp_conn->out.segment;
  968. unsigned int consumed = 0;
  969. int rc = 0;
  970. while (1) {
  971. rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
  972. if (rc < 0)
  973. goto error;
  974. if (rc == 0)
  975. break;
  976. consumed += rc;
  977. if (segment->total_copied >= segment->total_size) {
  978. if (segment->done != NULL) {
  979. rc = segment->done(tcp_conn, segment);
  980. if (rc < 0)
  981. goto error;
  982. }
  983. }
  984. }
  985. debug_tcp("xmit %d bytes\n", consumed);
  986. conn->txdata_octets += consumed;
  987. return consumed;
  988. error:
  989. /* Transmit error. We could initiate error recovery
  990. * here. */
  991. debug_tcp("Error sending PDU, errno=%d\n", rc);
  992. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  993. return rc;
  994. }
  995. /**
  996. * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
  997. */
  998. static inline int
  999. iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
  1000. {
  1001. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1002. struct iscsi_segment *segment = &tcp_conn->out.segment;
  1003. return segment->total_copied - segment->total_size;
  1004. }
  1005. static inline int
  1006. iscsi_tcp_flush(struct iscsi_conn *conn)
  1007. {
  1008. int rc;
  1009. while (iscsi_tcp_xmit_qlen(conn)) {
  1010. rc = iscsi_xmit(conn);
  1011. if (rc == 0)
  1012. return -EAGAIN;
  1013. if (rc < 0)
  1014. return rc;
  1015. }
  1016. return 0;
  1017. }
  1018. /*
  1019. * This is called when we're done sending the header.
  1020. * Simply copy the data_segment to the send segment, and return.
  1021. */
  1022. static int
  1023. iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
  1024. struct iscsi_segment *segment)
  1025. {
  1026. tcp_conn->out.segment = tcp_conn->out.data_segment;
  1027. debug_tcp("Header done. Next segment size %u total_size %u\n",
  1028. tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
  1029. return 0;
  1030. }
  1031. static void
  1032. iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
  1033. {
  1034. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1035. debug_tcp("%s(%p%s)\n", __FUNCTION__, tcp_conn,
  1036. conn->hdrdgst_en? ", digest enabled" : "");
  1037. /* Clear the data segment - needs to be filled in by the
  1038. * caller using iscsi_tcp_send_data_prep() */
  1039. memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
  1040. /* If header digest is enabled, compute the CRC and
  1041. * place the digest into the same buffer. We make
  1042. * sure that both iscsi_tcp_ctask and mtask have
  1043. * sufficient room.
  1044. */
  1045. if (conn->hdrdgst_en) {
  1046. iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
  1047. hdr + hdrlen);
  1048. hdrlen += ISCSI_DIGEST_SIZE;
  1049. }
  1050. /* Remember header pointer for later, when we need
  1051. * to decide whether there's a payload to go along
  1052. * with the header. */
  1053. tcp_conn->out.hdr = hdr;
  1054. iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
  1055. iscsi_tcp_send_hdr_done, NULL);
  1056. }
  1057. /*
  1058. * Prepare the send buffer for the payload data.
  1059. * Padding and checksumming will all be taken care
  1060. * of by the iscsi_segment routines.
  1061. */
  1062. static int
  1063. iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
  1064. unsigned int count, unsigned int offset,
  1065. unsigned int len)
  1066. {
  1067. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1068. struct hash_desc *tx_hash = NULL;
  1069. unsigned int hdr_spec_len;
  1070. debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __FUNCTION__,
  1071. tcp_conn, offset, len,
  1072. conn->datadgst_en? ", digest enabled" : "");
  1073. /* Make sure the datalen matches what the caller
  1074. said he would send. */
  1075. hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
  1076. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  1077. if (conn->datadgst_en)
  1078. tx_hash = &tcp_conn->tx_hash;
  1079. return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
  1080. sg, count, offset, len,
  1081. NULL, tx_hash);
  1082. }
  1083. static void
  1084. iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
  1085. size_t len)
  1086. {
  1087. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1088. struct hash_desc *tx_hash = NULL;
  1089. unsigned int hdr_spec_len;
  1090. debug_tcp("%s(%p, datalen=%d%s)\n", __FUNCTION__, tcp_conn, len,
  1091. conn->datadgst_en? ", digest enabled" : "");
  1092. /* Make sure the datalen matches what the caller
  1093. said he would send. */
  1094. hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
  1095. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  1096. if (conn->datadgst_en)
  1097. tx_hash = &tcp_conn->tx_hash;
  1098. iscsi_segment_init_linear(&tcp_conn->out.data_segment,
  1099. data, len, NULL, tx_hash);
  1100. }
  1101. /**
  1102. * iscsi_solicit_data_cont - initialize next Data-Out
  1103. * @conn: iscsi connection
  1104. * @ctask: scsi command task
  1105. * @r2t: R2T info
  1106. * @left: bytes left to transfer
  1107. *
  1108. * Notes:
  1109. * Initialize next Data-Out within this R2T sequence and continue
  1110. * to process next Scatter-Gather element(if any) of this SCSI command.
  1111. *
  1112. * Called under connection lock.
  1113. **/
  1114. static int
  1115. iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1116. struct iscsi_r2t_info *r2t)
  1117. {
  1118. struct iscsi_data *hdr;
  1119. int new_offset, left;
  1120. BUG_ON(r2t->data_length - r2t->sent < 0);
  1121. left = r2t->data_length - r2t->sent;
  1122. if (left == 0)
  1123. return 0;
  1124. hdr = &r2t->dtask.hdr;
  1125. memset(hdr, 0, sizeof(struct iscsi_data));
  1126. hdr->ttt = r2t->ttt;
  1127. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  1128. r2t->solicit_datasn++;
  1129. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  1130. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1131. hdr->itt = ctask->hdr->itt;
  1132. hdr->exp_statsn = r2t->exp_statsn;
  1133. new_offset = r2t->data_offset + r2t->sent;
  1134. hdr->offset = cpu_to_be32(new_offset);
  1135. if (left > conn->max_xmit_dlength) {
  1136. hton24(hdr->dlength, conn->max_xmit_dlength);
  1137. r2t->data_count = conn->max_xmit_dlength;
  1138. } else {
  1139. hton24(hdr->dlength, left);
  1140. r2t->data_count = left;
  1141. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  1142. }
  1143. conn->dataout_pdus_cnt++;
  1144. return 1;
  1145. }
  1146. /**
  1147. * iscsi_tcp_ctask - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
  1148. * @conn: iscsi connection
  1149. * @ctask: scsi command task
  1150. * @sc: scsi command
  1151. **/
  1152. static int
  1153. iscsi_tcp_ctask_init(struct iscsi_cmd_task *ctask)
  1154. {
  1155. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1156. struct iscsi_conn *conn = ctask->conn;
  1157. struct scsi_cmnd *sc = ctask->sc;
  1158. int err;
  1159. BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
  1160. tcp_ctask->sent = 0;
  1161. tcp_ctask->exp_datasn = 0;
  1162. /* Prepare PDU, optionally w/ immediate data */
  1163. debug_scsi("ctask deq [cid %d itt 0x%x imm %d unsol %d]\n",
  1164. conn->id, ctask->itt, ctask->imm_count,
  1165. ctask->unsol_count);
  1166. iscsi_tcp_send_hdr_prep(conn, ctask->hdr, ctask->hdr_len);
  1167. if (!ctask->imm_count)
  1168. return 0;
  1169. /* If we have immediate data, attach a payload */
  1170. err = iscsi_tcp_send_data_prep(conn, scsi_out(sc)->table.sgl,
  1171. scsi_out(sc)->table.nents,
  1172. 0, ctask->imm_count);
  1173. if (err)
  1174. return err;
  1175. tcp_ctask->sent += ctask->imm_count;
  1176. ctask->imm_count = 0;
  1177. return 0;
  1178. }
  1179. /**
  1180. * iscsi_tcp_mtask_xmit - xmit management(immediate) task
  1181. * @conn: iscsi connection
  1182. * @mtask: task management task
  1183. *
  1184. * Notes:
  1185. * The function can return -EAGAIN in which case caller must
  1186. * call it again later, or recover. '0' return code means successful
  1187. * xmit.
  1188. **/
  1189. static int
  1190. iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
  1191. {
  1192. int rc;
  1193. /* Flush any pending data first. */
  1194. rc = iscsi_tcp_flush(conn);
  1195. if (rc < 0)
  1196. return rc;
  1197. if (mtask->hdr->itt == RESERVED_ITT) {
  1198. struct iscsi_session *session = conn->session;
  1199. spin_lock_bh(&session->lock);
  1200. iscsi_free_mgmt_task(conn, mtask);
  1201. spin_unlock_bh(&session->lock);
  1202. }
  1203. return 0;
  1204. }
  1205. /*
  1206. * iscsi_tcp_ctask_xmit - xmit normal PDU task
  1207. * @conn: iscsi connection
  1208. * @ctask: iscsi command task
  1209. *
  1210. * We're expected to return 0 when everything was transmitted succesfully,
  1211. * -EAGAIN if there's still data in the queue, or != 0 for any other kind
  1212. * of error.
  1213. */
  1214. static int
  1215. iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1216. {
  1217. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1218. struct scsi_cmnd *sc = ctask->sc;
  1219. struct scsi_data_buffer *sdb = scsi_out(sc);
  1220. int rc = 0;
  1221. flush:
  1222. /* Flush any pending data first. */
  1223. rc = iscsi_tcp_flush(conn);
  1224. if (rc < 0)
  1225. return rc;
  1226. /* Are we done already? */
  1227. if (sc->sc_data_direction != DMA_TO_DEVICE)
  1228. return 0;
  1229. if (ctask->unsol_count != 0) {
  1230. struct iscsi_data *hdr = &tcp_ctask->unsol_dtask.hdr;
  1231. /* Prepare a header for the unsolicited PDU.
  1232. * The amount of data we want to send will be
  1233. * in ctask->data_count.
  1234. * FIXME: return the data count instead.
  1235. */
  1236. iscsi_prep_unsolicit_data_pdu(ctask, hdr);
  1237. debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
  1238. ctask->itt, tcp_ctask->sent, ctask->data_count);
  1239. iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
  1240. rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
  1241. sdb->table.nents, tcp_ctask->sent,
  1242. ctask->data_count);
  1243. if (rc)
  1244. goto fail;
  1245. tcp_ctask->sent += ctask->data_count;
  1246. ctask->unsol_count -= ctask->data_count;
  1247. goto flush;
  1248. } else {
  1249. struct iscsi_session *session = conn->session;
  1250. struct iscsi_r2t_info *r2t;
  1251. /* All unsolicited PDUs sent. Check for solicited PDUs.
  1252. */
  1253. spin_lock_bh(&session->lock);
  1254. r2t = tcp_ctask->r2t;
  1255. if (r2t != NULL) {
  1256. /* Continue with this R2T? */
  1257. if (!iscsi_solicit_data_cont(conn, ctask, r2t)) {
  1258. debug_scsi(" done with r2t %p\n", r2t);
  1259. __kfifo_put(tcp_ctask->r2tpool.queue,
  1260. (void*)&r2t, sizeof(void*));
  1261. tcp_ctask->r2t = r2t = NULL;
  1262. }
  1263. }
  1264. if (r2t == NULL) {
  1265. __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
  1266. sizeof(void*));
  1267. r2t = tcp_ctask->r2t;
  1268. }
  1269. spin_unlock_bh(&session->lock);
  1270. /* Waiting for more R2Ts to arrive. */
  1271. if (r2t == NULL) {
  1272. debug_tcp("no R2Ts yet\n");
  1273. return 0;
  1274. }
  1275. debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
  1276. r2t, r2t->solicit_datasn - 1, ctask->itt,
  1277. r2t->data_offset + r2t->sent, r2t->data_count);
  1278. iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
  1279. sizeof(struct iscsi_hdr));
  1280. rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
  1281. sdb->table.nents,
  1282. r2t->data_offset + r2t->sent,
  1283. r2t->data_count);
  1284. if (rc)
  1285. goto fail;
  1286. tcp_ctask->sent += r2t->data_count;
  1287. r2t->sent += r2t->data_count;
  1288. goto flush;
  1289. }
  1290. return 0;
  1291. fail:
  1292. iscsi_conn_failure(conn, rc);
  1293. return -EIO;
  1294. }
  1295. static struct iscsi_cls_conn *
  1296. iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1297. {
  1298. struct iscsi_conn *conn;
  1299. struct iscsi_cls_conn *cls_conn;
  1300. struct iscsi_tcp_conn *tcp_conn;
  1301. cls_conn = iscsi_conn_setup(cls_session, conn_idx);
  1302. if (!cls_conn)
  1303. return NULL;
  1304. conn = cls_conn->dd_data;
  1305. /*
  1306. * due to strange issues with iser these are not set
  1307. * in iscsi_conn_setup
  1308. */
  1309. conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
  1310. tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
  1311. if (!tcp_conn)
  1312. goto tcp_conn_alloc_fail;
  1313. conn->dd_data = tcp_conn;
  1314. tcp_conn->iscsi_conn = conn;
  1315. tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1316. CRYPTO_ALG_ASYNC);
  1317. tcp_conn->tx_hash.flags = 0;
  1318. if (IS_ERR(tcp_conn->tx_hash.tfm))
  1319. goto free_tcp_conn;
  1320. tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1321. CRYPTO_ALG_ASYNC);
  1322. tcp_conn->rx_hash.flags = 0;
  1323. if (IS_ERR(tcp_conn->rx_hash.tfm))
  1324. goto free_tx_tfm;
  1325. return cls_conn;
  1326. free_tx_tfm:
  1327. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1328. free_tcp_conn:
  1329. iscsi_conn_printk(KERN_ERR, conn,
  1330. "Could not create connection due to crc32c "
  1331. "loading error. Make sure the crc32c "
  1332. "module is built as a module or into the "
  1333. "kernel\n");
  1334. kfree(tcp_conn);
  1335. tcp_conn_alloc_fail:
  1336. iscsi_conn_teardown(cls_conn);
  1337. return NULL;
  1338. }
  1339. static void
  1340. iscsi_tcp_release_conn(struct iscsi_conn *conn)
  1341. {
  1342. struct iscsi_session *session = conn->session;
  1343. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1344. struct socket *sock = tcp_conn->sock;
  1345. if (!sock)
  1346. return;
  1347. sock_hold(sock->sk);
  1348. iscsi_conn_restore_callbacks(tcp_conn);
  1349. sock_put(sock->sk);
  1350. spin_lock_bh(&session->lock);
  1351. tcp_conn->sock = NULL;
  1352. conn->recv_lock = NULL;
  1353. spin_unlock_bh(&session->lock);
  1354. sockfd_put(sock);
  1355. }
  1356. static void
  1357. iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1358. {
  1359. struct iscsi_conn *conn = cls_conn->dd_data;
  1360. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1361. iscsi_tcp_release_conn(conn);
  1362. iscsi_conn_teardown(cls_conn);
  1363. if (tcp_conn->tx_hash.tfm)
  1364. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1365. if (tcp_conn->rx_hash.tfm)
  1366. crypto_free_hash(tcp_conn->rx_hash.tfm);
  1367. kfree(tcp_conn);
  1368. }
  1369. static void
  1370. iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1371. {
  1372. struct iscsi_conn *conn = cls_conn->dd_data;
  1373. iscsi_conn_stop(cls_conn, flag);
  1374. iscsi_tcp_release_conn(conn);
  1375. }
  1376. static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
  1377. char *buf, int *port,
  1378. int (*getname)(struct socket *, struct sockaddr *,
  1379. int *addrlen))
  1380. {
  1381. struct sockaddr_storage *addr;
  1382. struct sockaddr_in6 *sin6;
  1383. struct sockaddr_in *sin;
  1384. int rc = 0, len;
  1385. addr = kmalloc(sizeof(*addr), GFP_KERNEL);
  1386. if (!addr)
  1387. return -ENOMEM;
  1388. if (getname(sock, (struct sockaddr *) addr, &len)) {
  1389. rc = -ENODEV;
  1390. goto free_addr;
  1391. }
  1392. switch (addr->ss_family) {
  1393. case AF_INET:
  1394. sin = (struct sockaddr_in *)addr;
  1395. spin_lock_bh(&conn->session->lock);
  1396. sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
  1397. *port = be16_to_cpu(sin->sin_port);
  1398. spin_unlock_bh(&conn->session->lock);
  1399. break;
  1400. case AF_INET6:
  1401. sin6 = (struct sockaddr_in6 *)addr;
  1402. spin_lock_bh(&conn->session->lock);
  1403. sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
  1404. *port = be16_to_cpu(sin6->sin6_port);
  1405. spin_unlock_bh(&conn->session->lock);
  1406. break;
  1407. }
  1408. free_addr:
  1409. kfree(addr);
  1410. return rc;
  1411. }
  1412. static int
  1413. iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  1414. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  1415. int is_leading)
  1416. {
  1417. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1418. struct iscsi_host *ihost = shost_priv(shost);
  1419. struct iscsi_conn *conn = cls_conn->dd_data;
  1420. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1421. struct sock *sk;
  1422. struct socket *sock;
  1423. int err;
  1424. /* lookup for existing socket */
  1425. sock = sockfd_lookup((int)transport_eph, &err);
  1426. if (!sock) {
  1427. iscsi_conn_printk(KERN_ERR, conn,
  1428. "sockfd_lookup failed %d\n", err);
  1429. return -EEXIST;
  1430. }
  1431. /*
  1432. * copy these values now because if we drop the session
  1433. * userspace may still want to query the values since we will
  1434. * be using them for the reconnect
  1435. */
  1436. err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
  1437. &conn->portal_port, kernel_getpeername);
  1438. if (err)
  1439. goto free_socket;
  1440. err = iscsi_tcp_get_addr(conn, sock, ihost->local_address,
  1441. &ihost->local_port, kernel_getsockname);
  1442. if (err)
  1443. goto free_socket;
  1444. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  1445. if (err)
  1446. goto free_socket;
  1447. /* bind iSCSI connection and socket */
  1448. tcp_conn->sock = sock;
  1449. /* setup Socket parameters */
  1450. sk = sock->sk;
  1451. sk->sk_reuse = 1;
  1452. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  1453. sk->sk_allocation = GFP_ATOMIC;
  1454. /* FIXME: disable Nagle's algorithm */
  1455. /*
  1456. * Intercept TCP callbacks for sendfile like receive
  1457. * processing.
  1458. */
  1459. conn->recv_lock = &sk->sk_callback_lock;
  1460. iscsi_conn_set_callbacks(conn);
  1461. tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
  1462. /*
  1463. * set receive state machine into initial state
  1464. */
  1465. iscsi_tcp_hdr_recv_prep(tcp_conn);
  1466. return 0;
  1467. free_socket:
  1468. sockfd_put(sock);
  1469. return err;
  1470. }
  1471. /* called with host lock */
  1472. static void
  1473. iscsi_tcp_mtask_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
  1474. {
  1475. debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt);
  1476. /* Prepare PDU, optionally w/ immediate data */
  1477. iscsi_tcp_send_hdr_prep(conn, mtask->hdr, sizeof(*mtask->hdr));
  1478. /* If we have immediate data, attach a payload */
  1479. if (mtask->data_count)
  1480. iscsi_tcp_send_linear_data_prepare(conn, mtask->data,
  1481. mtask->data_count);
  1482. }
  1483. static int
  1484. iscsi_r2tpool_alloc(struct iscsi_session *session)
  1485. {
  1486. int i;
  1487. int cmd_i;
  1488. /*
  1489. * initialize per-task: R2T pool and xmit queue
  1490. */
  1491. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1492. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1493. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1494. /*
  1495. * pre-allocated x4 as much r2ts to handle race when
  1496. * target acks DataOut faster than we data_xmit() queues
  1497. * could replenish r2tqueue.
  1498. */
  1499. /* R2T pool */
  1500. if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, NULL,
  1501. sizeof(struct iscsi_r2t_info))) {
  1502. goto r2t_alloc_fail;
  1503. }
  1504. /* R2T xmit queue */
  1505. tcp_ctask->r2tqueue = kfifo_alloc(
  1506. session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
  1507. if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
  1508. iscsi_pool_free(&tcp_ctask->r2tpool);
  1509. goto r2t_alloc_fail;
  1510. }
  1511. }
  1512. return 0;
  1513. r2t_alloc_fail:
  1514. for (i = 0; i < cmd_i; i++) {
  1515. struct iscsi_cmd_task *ctask = session->cmds[i];
  1516. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1517. kfifo_free(tcp_ctask->r2tqueue);
  1518. iscsi_pool_free(&tcp_ctask->r2tpool);
  1519. }
  1520. return -ENOMEM;
  1521. }
  1522. static void
  1523. iscsi_r2tpool_free(struct iscsi_session *session)
  1524. {
  1525. int i;
  1526. for (i = 0; i < session->cmds_max; i++) {
  1527. struct iscsi_cmd_task *ctask = session->cmds[i];
  1528. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1529. kfifo_free(tcp_ctask->r2tqueue);
  1530. iscsi_pool_free(&tcp_ctask->r2tpool);
  1531. }
  1532. }
  1533. static int
  1534. iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
  1535. char *buf, int buflen)
  1536. {
  1537. struct iscsi_conn *conn = cls_conn->dd_data;
  1538. struct iscsi_session *session = conn->session;
  1539. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1540. int value;
  1541. switch(param) {
  1542. case ISCSI_PARAM_HDRDGST_EN:
  1543. iscsi_set_param(cls_conn, param, buf, buflen);
  1544. break;
  1545. case ISCSI_PARAM_DATADGST_EN:
  1546. iscsi_set_param(cls_conn, param, buf, buflen);
  1547. tcp_conn->sendpage = conn->datadgst_en ?
  1548. sock_no_sendpage : tcp_conn->sock->ops->sendpage;
  1549. break;
  1550. case ISCSI_PARAM_MAX_R2T:
  1551. sscanf(buf, "%d", &value);
  1552. if (value <= 0 || !is_power_of_2(value))
  1553. return -EINVAL;
  1554. if (session->max_r2t == value)
  1555. break;
  1556. iscsi_r2tpool_free(session);
  1557. iscsi_set_param(cls_conn, param, buf, buflen);
  1558. if (iscsi_r2tpool_alloc(session))
  1559. return -ENOMEM;
  1560. break;
  1561. default:
  1562. return iscsi_set_param(cls_conn, param, buf, buflen);
  1563. }
  1564. return 0;
  1565. }
  1566. static int
  1567. iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1568. enum iscsi_param param, char *buf)
  1569. {
  1570. struct iscsi_conn *conn = cls_conn->dd_data;
  1571. int len;
  1572. switch(param) {
  1573. case ISCSI_PARAM_CONN_PORT:
  1574. spin_lock_bh(&conn->session->lock);
  1575. len = sprintf(buf, "%hu\n", conn->portal_port);
  1576. spin_unlock_bh(&conn->session->lock);
  1577. break;
  1578. case ISCSI_PARAM_CONN_ADDRESS:
  1579. spin_lock_bh(&conn->session->lock);
  1580. len = sprintf(buf, "%s\n", conn->portal_address);
  1581. spin_unlock_bh(&conn->session->lock);
  1582. break;
  1583. default:
  1584. return iscsi_conn_get_param(cls_conn, param, buf);
  1585. }
  1586. return len;
  1587. }
  1588. static void
  1589. iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
  1590. {
  1591. struct iscsi_conn *conn = cls_conn->dd_data;
  1592. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1593. stats->txdata_octets = conn->txdata_octets;
  1594. stats->rxdata_octets = conn->rxdata_octets;
  1595. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1596. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1597. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1598. stats->datain_pdus = conn->datain_pdus_cnt;
  1599. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1600. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1601. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1602. stats->custom_length = 3;
  1603. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  1604. stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
  1605. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  1606. stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
  1607. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  1608. stats->custom[2].value = conn->eh_abort_cnt;
  1609. }
  1610. static struct iscsi_cls_session *
  1611. iscsi_tcp_session_create(struct Scsi_Host *shost, uint16_t cmds_max,
  1612. uint16_t qdepth, uint32_t initial_cmdsn,
  1613. uint32_t *hostno)
  1614. {
  1615. struct iscsi_cls_session *cls_session;
  1616. struct iscsi_session *session;
  1617. int cmd_i;
  1618. if (shost) {
  1619. printk(KERN_ERR "iscsi_tcp: invalid shost %d.\n",
  1620. shost->host_no);
  1621. return NULL;
  1622. }
  1623. shost = scsi_host_alloc(&iscsi_sht, sizeof(struct iscsi_host));
  1624. if (!shost)
  1625. return NULL;
  1626. shost->transportt = iscsi_tcp_scsi_transport;
  1627. shost->max_lun = iscsi_max_lun;
  1628. shost->max_id = 0;
  1629. shost->max_channel = 0;
  1630. shost->max_cmd_len = 16;
  1631. iscsi_host_setup(shost, qdepth);
  1632. if (scsi_add_host(shost, NULL))
  1633. goto free_host;
  1634. *hostno = shost->host_no;
  1635. cls_session = iscsi_session_setup(&iscsi_tcp_transport, shost, cmds_max,
  1636. sizeof(struct iscsi_tcp_cmd_task),
  1637. sizeof(struct iscsi_tcp_mgmt_task),
  1638. initial_cmdsn);
  1639. if (!cls_session)
  1640. goto remove_host;
  1641. session = cls_session->dd_data;
  1642. shost->can_queue = session->cmds_max;
  1643. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1644. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1645. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1646. ctask->hdr = &tcp_ctask->hdr.cmd_hdr;
  1647. ctask->hdr_max = sizeof(tcp_ctask->hdr) - ISCSI_DIGEST_SIZE;
  1648. }
  1649. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1650. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1651. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1652. mtask->hdr = (struct iscsi_hdr *) &tcp_mtask->hdr;
  1653. }
  1654. if (iscsi_r2tpool_alloc(session))
  1655. goto remove_session;
  1656. return cls_session;
  1657. remove_session:
  1658. iscsi_session_teardown(cls_session);
  1659. remove_host:
  1660. scsi_remove_host(shost);
  1661. free_host:
  1662. iscsi_host_teardown(shost);
  1663. scsi_host_put(shost);
  1664. return NULL;
  1665. }
  1666. static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  1667. {
  1668. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1669. iscsi_r2tpool_free(cls_session->dd_data);
  1670. iscsi_session_teardown(cls_session);
  1671. scsi_remove_host(shost);
  1672. iscsi_host_teardown(shost);
  1673. scsi_host_put(shost);
  1674. }
  1675. static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
  1676. {
  1677. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
  1678. blk_queue_dma_alignment(sdev->request_queue, 0);
  1679. return 0;
  1680. }
  1681. static struct scsi_host_template iscsi_sht = {
  1682. .module = THIS_MODULE,
  1683. .name = "iSCSI Initiator over TCP/IP",
  1684. .queuecommand = iscsi_queuecommand,
  1685. .change_queue_depth = iscsi_change_queue_depth,
  1686. .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
  1687. .sg_tablesize = 4096,
  1688. .max_sectors = 0xFFFF,
  1689. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  1690. .eh_abort_handler = iscsi_eh_abort,
  1691. .eh_device_reset_handler= iscsi_eh_device_reset,
  1692. .eh_host_reset_handler = iscsi_eh_host_reset,
  1693. .use_clustering = DISABLE_CLUSTERING,
  1694. .slave_configure = iscsi_tcp_slave_configure,
  1695. .proc_name = "iscsi_tcp",
  1696. .this_id = -1,
  1697. };
  1698. static struct iscsi_transport iscsi_tcp_transport = {
  1699. .owner = THIS_MODULE,
  1700. .name = "tcp",
  1701. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  1702. | CAP_DATADGST,
  1703. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  1704. ISCSI_MAX_XMIT_DLENGTH |
  1705. ISCSI_HDRDGST_EN |
  1706. ISCSI_DATADGST_EN |
  1707. ISCSI_INITIAL_R2T_EN |
  1708. ISCSI_MAX_R2T |
  1709. ISCSI_IMM_DATA_EN |
  1710. ISCSI_FIRST_BURST |
  1711. ISCSI_MAX_BURST |
  1712. ISCSI_PDU_INORDER_EN |
  1713. ISCSI_DATASEQ_INORDER_EN |
  1714. ISCSI_ERL |
  1715. ISCSI_CONN_PORT |
  1716. ISCSI_CONN_ADDRESS |
  1717. ISCSI_EXP_STATSN |
  1718. ISCSI_PERSISTENT_PORT |
  1719. ISCSI_PERSISTENT_ADDRESS |
  1720. ISCSI_TARGET_NAME | ISCSI_TPGT |
  1721. ISCSI_USERNAME | ISCSI_PASSWORD |
  1722. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  1723. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  1724. ISCSI_LU_RESET_TMO |
  1725. ISCSI_PING_TMO | ISCSI_RECV_TMO,
  1726. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  1727. ISCSI_HOST_INITIATOR_NAME |
  1728. ISCSI_HOST_NETDEV_NAME,
  1729. .conndata_size = sizeof(struct iscsi_conn),
  1730. .sessiondata_size = sizeof(struct iscsi_session),
  1731. /* session management */
  1732. .create_session = iscsi_tcp_session_create,
  1733. .destroy_session = iscsi_tcp_session_destroy,
  1734. /* connection management */
  1735. .create_conn = iscsi_tcp_conn_create,
  1736. .bind_conn = iscsi_tcp_conn_bind,
  1737. .destroy_conn = iscsi_tcp_conn_destroy,
  1738. .set_param = iscsi_conn_set_param,
  1739. .get_conn_param = iscsi_tcp_conn_get_param,
  1740. .get_session_param = iscsi_session_get_param,
  1741. .start_conn = iscsi_conn_start,
  1742. .stop_conn = iscsi_tcp_conn_stop,
  1743. /* iscsi host params */
  1744. .get_host_param = iscsi_host_get_param,
  1745. .set_host_param = iscsi_host_set_param,
  1746. /* IO */
  1747. .send_pdu = iscsi_conn_send_pdu,
  1748. .get_stats = iscsi_conn_get_stats,
  1749. .init_cmd_task = iscsi_tcp_ctask_init,
  1750. .init_mgmt_task = iscsi_tcp_mtask_init,
  1751. .xmit_cmd_task = iscsi_tcp_ctask_xmit,
  1752. .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
  1753. .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
  1754. /* recovery */
  1755. .session_recovery_timedout = iscsi_session_recovery_timedout,
  1756. };
  1757. static int __init
  1758. iscsi_tcp_init(void)
  1759. {
  1760. if (iscsi_max_lun < 1) {
  1761. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  1762. iscsi_max_lun);
  1763. return -EINVAL;
  1764. }
  1765. iscsi_tcp_scsi_transport = iscsi_register_transport(
  1766. &iscsi_tcp_transport);
  1767. if (!iscsi_tcp_scsi_transport)
  1768. return -ENODEV;
  1769. return 0;
  1770. }
  1771. static void __exit
  1772. iscsi_tcp_exit(void)
  1773. {
  1774. iscsi_unregister_transport(&iscsi_tcp_transport);
  1775. }
  1776. module_init(iscsi_tcp_init);
  1777. module_exit(iscsi_tcp_exit);