iscsi_tcp.c 53 KB

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