iscsi_tcp.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307
  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 unsigned int iscsi_max_lun = 512;
  62. module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
  63. static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  64. struct iscsi_chunk *chunk);
  65. static inline void
  66. iscsi_buf_init_iov(struct iscsi_buf *ibuf, char *vbuf, int size)
  67. {
  68. ibuf->sg.page = virt_to_page(vbuf);
  69. ibuf->sg.offset = offset_in_page(vbuf);
  70. ibuf->sg.length = size;
  71. ibuf->sent = 0;
  72. ibuf->use_sendmsg = 1;
  73. }
  74. static inline void
  75. iscsi_buf_init_sg(struct iscsi_buf *ibuf, struct scatterlist *sg)
  76. {
  77. ibuf->sg.page = sg->page;
  78. ibuf->sg.offset = sg->offset;
  79. ibuf->sg.length = sg->length;
  80. /*
  81. * Fastpath: sg element fits into single page
  82. */
  83. if (sg->length + sg->offset <= PAGE_SIZE && !PageSlab(sg->page))
  84. ibuf->use_sendmsg = 0;
  85. else
  86. ibuf->use_sendmsg = 1;
  87. ibuf->sent = 0;
  88. }
  89. static inline int
  90. iscsi_buf_left(struct iscsi_buf *ibuf)
  91. {
  92. int rc;
  93. rc = ibuf->sg.length - ibuf->sent;
  94. BUG_ON(rc < 0);
  95. return rc;
  96. }
  97. static inline void
  98. iscsi_hdr_digest(struct iscsi_conn *conn, struct iscsi_buf *buf,
  99. u8* crc)
  100. {
  101. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  102. crypto_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, crc);
  103. buf->sg.length += ISCSI_DIGEST_SIZE;
  104. }
  105. /*
  106. * Scatterlist handling: inside the iscsi_chunk, we
  107. * remember an index into the scatterlist, and set data/size
  108. * to the current scatterlist entry. For highmem pages, we
  109. * kmap as needed.
  110. *
  111. * Note that the page is unmapped when we return from
  112. * TCP's data_ready handler, so we may end up mapping and
  113. * unmapping the same page repeatedly. The whole reason
  114. * for this is that we shouldn't keep the page mapped
  115. * outside the softirq.
  116. */
  117. /**
  118. * iscsi_tcp_chunk_init_sg - init indicated scatterlist entry
  119. * @chunk: the buffer object
  120. * @idx: index into scatterlist
  121. * @offset: byte offset into that sg entry
  122. *
  123. * This function sets up the chunk so that subsequent
  124. * data is copied to the indicated sg entry, at the given
  125. * offset.
  126. */
  127. static inline void
  128. iscsi_tcp_chunk_init_sg(struct iscsi_chunk *chunk,
  129. unsigned int idx, unsigned int offset)
  130. {
  131. struct scatterlist *sg;
  132. BUG_ON(chunk->sg == NULL);
  133. sg = &chunk->sg[idx];
  134. chunk->sg_index = idx;
  135. chunk->sg_offset = offset;
  136. chunk->size = min(sg->length - offset, chunk->total_size);
  137. chunk->data = NULL;
  138. }
  139. /**
  140. * iscsi_tcp_chunk_map - map the current S/G page
  141. * @chunk: iscsi chunk
  142. *
  143. * We only need to possibly kmap data if scatter lists are being used,
  144. * because the iscsi passthrough and internal IO paths will never use high
  145. * mem pages.
  146. */
  147. static inline void
  148. iscsi_tcp_chunk_map(struct iscsi_chunk *chunk)
  149. {
  150. struct scatterlist *sg;
  151. if (chunk->data != NULL || !chunk->sg)
  152. return;
  153. sg = &chunk->sg[chunk->sg_index];
  154. BUG_ON(chunk->sg_mapped);
  155. BUG_ON(sg->length == 0);
  156. chunk->sg_mapped = kmap_atomic(sg->page, KM_SOFTIRQ0);
  157. chunk->data = chunk->sg_mapped + sg->offset + chunk->sg_offset;
  158. }
  159. static inline void
  160. iscsi_tcp_chunk_unmap(struct iscsi_chunk *chunk)
  161. {
  162. if (chunk->sg_mapped) {
  163. kunmap_atomic(chunk->sg_mapped, KM_SOFTIRQ0);
  164. chunk->sg_mapped = NULL;
  165. chunk->data = NULL;
  166. }
  167. }
  168. /*
  169. * Splice the digest buffer into the buffer
  170. */
  171. static inline void
  172. iscsi_tcp_chunk_splice_digest(struct iscsi_chunk *chunk, void *digest)
  173. {
  174. chunk->data = digest;
  175. chunk->digest_len = ISCSI_DIGEST_SIZE;
  176. chunk->total_size += ISCSI_DIGEST_SIZE;
  177. chunk->size = ISCSI_DIGEST_SIZE;
  178. chunk->copied = 0;
  179. chunk->sg = NULL;
  180. chunk->sg_index = 0;
  181. chunk->hash = NULL;
  182. }
  183. /**
  184. * iscsi_tcp_chunk_done - check whether the chunk is complete
  185. * @chunk: iscsi chunk to check
  186. *
  187. * Check if we're done receiving this chunk. If the receive
  188. * buffer is full but we expect more data, move on to the
  189. * next entry in the scatterlist.
  190. *
  191. * If the amount of data we received isn't a multiple of 4,
  192. * we will transparently receive the pad bytes, too.
  193. *
  194. * This function must be re-entrant.
  195. */
  196. static inline int
  197. iscsi_tcp_chunk_done(struct iscsi_chunk *chunk)
  198. {
  199. static unsigned char padbuf[ISCSI_PAD_LEN];
  200. unsigned int pad;
  201. if (chunk->copied < chunk->size) {
  202. iscsi_tcp_chunk_map(chunk);
  203. return 0;
  204. }
  205. chunk->total_copied += chunk->copied;
  206. chunk->copied = 0;
  207. chunk->size = 0;
  208. /* Unmap the current scatterlist page, if there is one. */
  209. iscsi_tcp_chunk_unmap(chunk);
  210. /* Do we have more scatterlist entries? */
  211. if (chunk->total_copied < chunk->total_size) {
  212. /* Proceed to the next entry in the scatterlist. */
  213. iscsi_tcp_chunk_init_sg(chunk, chunk->sg_index + 1, 0);
  214. iscsi_tcp_chunk_map(chunk);
  215. BUG_ON(chunk->size == 0);
  216. return 0;
  217. }
  218. /* Do we need to handle padding? */
  219. pad = iscsi_padding(chunk->total_copied);
  220. if (pad != 0) {
  221. debug_tcp("consume %d pad bytes\n", pad);
  222. chunk->total_size += pad;
  223. chunk->size = pad;
  224. chunk->data = padbuf;
  225. return 0;
  226. }
  227. /*
  228. * Set us up for receiving the data digest. hdr digest
  229. * is completely handled in hdr done function.
  230. */
  231. if (chunk->hash) {
  232. if (chunk->digest_len == 0) {
  233. crypto_hash_final(chunk->hash, chunk->digest);
  234. iscsi_tcp_chunk_splice_digest(chunk,
  235. chunk->recv_digest);
  236. return 0;
  237. }
  238. }
  239. return 1;
  240. }
  241. /**
  242. * iscsi_tcp_chunk_recv - copy data to chunk
  243. * @tcp_conn: the iSCSI TCP connection
  244. * @chunk: the buffer to copy to
  245. * @ptr: data pointer
  246. * @len: amount of data available
  247. *
  248. * This function copies up to @len bytes to the
  249. * given buffer, and returns the number of bytes
  250. * consumed, which can actually be less than @len.
  251. *
  252. * If hash digest is enabled, the function will update the
  253. * hash while copying.
  254. * Combining these two operations doesn't buy us a lot (yet),
  255. * but in the future we could implement combined copy+crc,
  256. * just way we do for network layer checksums.
  257. */
  258. static int
  259. iscsi_tcp_chunk_recv(struct iscsi_tcp_conn *tcp_conn,
  260. struct iscsi_chunk *chunk, const void *ptr,
  261. unsigned int len)
  262. {
  263. struct scatterlist sg;
  264. unsigned int copy, copied = 0;
  265. while (!iscsi_tcp_chunk_done(chunk)) {
  266. if (copied == len)
  267. goto out;
  268. copy = min(len - copied, chunk->size - chunk->copied);
  269. memcpy(chunk->data + chunk->copied, ptr + copied, copy);
  270. if (chunk->hash) {
  271. sg_init_one(&sg, ptr + copied, copy);
  272. crypto_hash_update(chunk->hash, &sg, copy);
  273. }
  274. chunk->copied += copy;
  275. copied += copy;
  276. }
  277. out:
  278. return copied;
  279. }
  280. static inline void
  281. iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
  282. unsigned char digest[ISCSI_DIGEST_SIZE])
  283. {
  284. struct scatterlist sg;
  285. sg_init_one(&sg, hdr, hdrlen);
  286. crypto_hash_digest(hash, &sg, hdrlen, digest);
  287. }
  288. static inline int
  289. iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
  290. struct iscsi_chunk *chunk)
  291. {
  292. if (!chunk->digest_len)
  293. return 1;
  294. if (memcmp(chunk->recv_digest, chunk->digest, chunk->digest_len)) {
  295. debug_scsi("digest mismatch\n");
  296. return 0;
  297. }
  298. return 1;
  299. }
  300. /*
  301. * Helper function to set up chunk buffer
  302. */
  303. static inline void
  304. __iscsi_chunk_init(struct iscsi_chunk *chunk, size_t size,
  305. iscsi_chunk_done_fn_t *done, struct hash_desc *hash)
  306. {
  307. memset(chunk, 0, sizeof(*chunk));
  308. chunk->total_size = size;
  309. chunk->done = done;
  310. if (hash) {
  311. chunk->hash = hash;
  312. crypto_hash_init(hash);
  313. }
  314. }
  315. static inline void
  316. iscsi_chunk_init_linear(struct iscsi_chunk *chunk, void *data, size_t size,
  317. iscsi_chunk_done_fn_t *done, struct hash_desc *hash)
  318. {
  319. __iscsi_chunk_init(chunk, size, done, hash);
  320. chunk->data = data;
  321. chunk->size = size;
  322. }
  323. static inline int
  324. iscsi_chunk_seek_sg(struct iscsi_chunk *chunk,
  325. struct scatterlist *sg, unsigned int sg_count,
  326. unsigned int offset, size_t size,
  327. iscsi_chunk_done_fn_t *done, struct hash_desc *hash)
  328. {
  329. unsigned int i;
  330. __iscsi_chunk_init(chunk, size, done, hash);
  331. for (i = 0; i < sg_count; ++i) {
  332. if (offset < sg[i].length) {
  333. chunk->sg = sg;
  334. chunk->sg_count = sg_count;
  335. iscsi_tcp_chunk_init_sg(chunk, i, offset);
  336. return 0;
  337. }
  338. offset -= sg[i].length;
  339. }
  340. return ISCSI_ERR_DATA_OFFSET;
  341. }
  342. /**
  343. * iscsi_tcp_hdr_recv_prep - prep chunk for hdr reception
  344. * @tcp_conn: iscsi connection to prep for
  345. *
  346. * This function always passes NULL for the hash argument, because when this
  347. * function is called we do not yet know the final size of the header and want
  348. * to delay the digest processing until we know that.
  349. */
  350. static void
  351. iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
  352. {
  353. debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
  354. tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
  355. iscsi_chunk_init_linear(&tcp_conn->in.chunk,
  356. tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
  357. iscsi_tcp_hdr_recv_done, NULL);
  358. }
  359. /*
  360. * Handle incoming reply to any other type of command
  361. */
  362. static int
  363. iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
  364. struct iscsi_chunk *chunk)
  365. {
  366. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  367. int rc = 0;
  368. if (!iscsi_tcp_dgst_verify(tcp_conn, chunk))
  369. return ISCSI_ERR_DATA_DGST;
  370. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
  371. conn->data, tcp_conn->in.datalen);
  372. if (rc)
  373. return rc;
  374. iscsi_tcp_hdr_recv_prep(tcp_conn);
  375. return 0;
  376. }
  377. static void
  378. iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
  379. {
  380. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  381. struct hash_desc *rx_hash = NULL;
  382. if (conn->datadgst_en)
  383. rx_hash = &tcp_conn->rx_hash;
  384. iscsi_chunk_init_linear(&tcp_conn->in.chunk,
  385. conn->data, tcp_conn->in.datalen,
  386. iscsi_tcp_data_recv_done, rx_hash);
  387. }
  388. /*
  389. * must be called with session lock
  390. */
  391. static void
  392. iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  393. {
  394. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  395. struct iscsi_r2t_info *r2t;
  396. struct scsi_cmnd *sc;
  397. /* flush ctask's r2t queues */
  398. while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
  399. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  400. sizeof(void*));
  401. debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
  402. }
  403. sc = ctask->sc;
  404. if (unlikely(!sc))
  405. return;
  406. tcp_ctask->xmstate = XMSTATE_IDLE;
  407. tcp_ctask->r2t = NULL;
  408. }
  409. /**
  410. * iscsi_data_rsp - SCSI Data-In Response processing
  411. * @conn: iscsi connection
  412. * @ctask: scsi command task
  413. **/
  414. static int
  415. iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  416. {
  417. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  418. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  419. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
  420. struct iscsi_session *session = conn->session;
  421. struct scsi_cmnd *sc = ctask->sc;
  422. int datasn = be32_to_cpu(rhdr->datasn);
  423. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  424. /*
  425. * setup Data-In byte counter (gets decremented..)
  426. */
  427. ctask->data_count = tcp_conn->in.datalen;
  428. if (tcp_conn->in.datalen == 0)
  429. return 0;
  430. if (tcp_ctask->exp_datasn != datasn) {
  431. debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
  432. __FUNCTION__, tcp_ctask->exp_datasn, datasn);
  433. return ISCSI_ERR_DATASN;
  434. }
  435. tcp_ctask->exp_datasn++;
  436. tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
  437. if (tcp_ctask->data_offset + tcp_conn->in.datalen > scsi_bufflen(sc)) {
  438. debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
  439. __FUNCTION__, tcp_ctask->data_offset,
  440. tcp_conn->in.datalen, scsi_bufflen(sc));
  441. return ISCSI_ERR_DATA_OFFSET;
  442. }
  443. if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
  444. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  445. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  446. if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
  447. ISCSI_FLAG_DATA_OVERFLOW)) {
  448. int res_count = be32_to_cpu(rhdr->residual_count);
  449. if (res_count > 0 &&
  450. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  451. res_count <= scsi_bufflen(sc)))
  452. scsi_set_resid(sc, res_count);
  453. else
  454. sc->result = (DID_BAD_TARGET << 16) |
  455. rhdr->cmd_status;
  456. }
  457. }
  458. conn->datain_pdus_cnt++;
  459. return 0;
  460. }
  461. /**
  462. * iscsi_solicit_data_init - initialize first Data-Out
  463. * @conn: iscsi connection
  464. * @ctask: scsi command task
  465. * @r2t: R2T info
  466. *
  467. * Notes:
  468. * Initialize first Data-Out within this R2T sequence and finds
  469. * proper data_offset within this SCSI command.
  470. *
  471. * This function is called with connection lock taken.
  472. **/
  473. static void
  474. iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  475. struct iscsi_r2t_info *r2t)
  476. {
  477. struct iscsi_data *hdr;
  478. struct scsi_cmnd *sc = ctask->sc;
  479. int i, sg_count = 0;
  480. struct scatterlist *sg;
  481. hdr = &r2t->dtask.hdr;
  482. memset(hdr, 0, sizeof(struct iscsi_data));
  483. hdr->ttt = r2t->ttt;
  484. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  485. r2t->solicit_datasn++;
  486. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  487. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  488. hdr->itt = ctask->hdr->itt;
  489. hdr->exp_statsn = r2t->exp_statsn;
  490. hdr->offset = cpu_to_be32(r2t->data_offset);
  491. if (r2t->data_length > conn->max_xmit_dlength) {
  492. hton24(hdr->dlength, conn->max_xmit_dlength);
  493. r2t->data_count = conn->max_xmit_dlength;
  494. hdr->flags = 0;
  495. } else {
  496. hton24(hdr->dlength, r2t->data_length);
  497. r2t->data_count = r2t->data_length;
  498. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  499. }
  500. conn->dataout_pdus_cnt++;
  501. r2t->sent = 0;
  502. iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
  503. sizeof(struct iscsi_hdr));
  504. sg = scsi_sglist(sc);
  505. r2t->sg = NULL;
  506. for (i = 0; i < scsi_sg_count(sc); i++, sg += 1) {
  507. /* FIXME: prefetch ? */
  508. if (sg_count + sg->length > r2t->data_offset) {
  509. int page_offset;
  510. /* sg page found! */
  511. /* offset within this page */
  512. page_offset = r2t->data_offset - sg_count;
  513. /* fill in this buffer */
  514. iscsi_buf_init_sg(&r2t->sendbuf, sg);
  515. r2t->sendbuf.sg.offset += page_offset;
  516. r2t->sendbuf.sg.length -= page_offset;
  517. /* xmit logic will continue with next one */
  518. r2t->sg = sg + 1;
  519. break;
  520. }
  521. sg_count += sg->length;
  522. }
  523. BUG_ON(r2t->sg == NULL);
  524. }
  525. /**
  526. * iscsi_r2t_rsp - iSCSI R2T Response processing
  527. * @conn: iscsi connection
  528. * @ctask: scsi command task
  529. **/
  530. static int
  531. iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  532. {
  533. struct iscsi_r2t_info *r2t;
  534. struct iscsi_session *session = conn->session;
  535. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  536. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  537. struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
  538. int r2tsn = be32_to_cpu(rhdr->r2tsn);
  539. int rc;
  540. if (tcp_conn->in.datalen) {
  541. printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
  542. tcp_conn->in.datalen);
  543. return ISCSI_ERR_DATALEN;
  544. }
  545. if (tcp_ctask->exp_datasn != r2tsn){
  546. debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
  547. __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
  548. return ISCSI_ERR_R2TSN;
  549. }
  550. /* fill-in new R2T associated with the task */
  551. spin_lock(&session->lock);
  552. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  553. if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) {
  554. printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
  555. "recovery...\n", ctask->itt);
  556. spin_unlock(&session->lock);
  557. return 0;
  558. }
  559. rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
  560. BUG_ON(!rc);
  561. r2t->exp_statsn = rhdr->statsn;
  562. r2t->data_length = be32_to_cpu(rhdr->data_length);
  563. if (r2t->data_length == 0) {
  564. printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
  565. spin_unlock(&session->lock);
  566. return ISCSI_ERR_DATALEN;
  567. }
  568. if (r2t->data_length > session->max_burst)
  569. debug_scsi("invalid R2T with data len %u and max burst %u."
  570. "Attempting to execute request.\n",
  571. r2t->data_length, session->max_burst);
  572. r2t->data_offset = be32_to_cpu(rhdr->data_offset);
  573. if (r2t->data_offset + r2t->data_length > scsi_bufflen(ctask->sc)) {
  574. spin_unlock(&session->lock);
  575. printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
  576. "offset %u and total length %d\n", r2t->data_length,
  577. r2t->data_offset, scsi_bufflen(ctask->sc));
  578. return ISCSI_ERR_DATALEN;
  579. }
  580. r2t->ttt = rhdr->ttt; /* no flip */
  581. r2t->solicit_datasn = 0;
  582. iscsi_solicit_data_init(conn, ctask, r2t);
  583. tcp_ctask->exp_datasn = r2tsn + 1;
  584. __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
  585. tcp_ctask->xmstate |= XMSTATE_SOL_HDR_INIT;
  586. conn->r2t_pdus_cnt++;
  587. iscsi_requeue_ctask(ctask);
  588. spin_unlock(&session->lock);
  589. return 0;
  590. }
  591. /*
  592. * Handle incoming reply to DataIn command
  593. */
  594. static int
  595. iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
  596. struct iscsi_chunk *chunk)
  597. {
  598. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  599. struct iscsi_hdr *hdr = tcp_conn->in.hdr;
  600. int rc;
  601. if (!iscsi_tcp_dgst_verify(tcp_conn, chunk))
  602. return ISCSI_ERR_DATA_DGST;
  603. /* check for non-exceptional status */
  604. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  605. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
  606. if (rc)
  607. return rc;
  608. }
  609. iscsi_tcp_hdr_recv_prep(tcp_conn);
  610. return 0;
  611. }
  612. /**
  613. * iscsi_tcp_hdr_dissect - process PDU header
  614. * @conn: iSCSI connection
  615. * @hdr: PDU header
  616. *
  617. * This function analyzes the header of the PDU received,
  618. * and performs several sanity checks. If the PDU is accompanied
  619. * by data, the receive buffer is set up to copy the incoming data
  620. * to the correct location.
  621. */
  622. static int
  623. iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  624. {
  625. int rc = 0, opcode, ahslen;
  626. struct iscsi_session *session = conn->session;
  627. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  628. struct iscsi_cmd_task *ctask;
  629. uint32_t itt;
  630. /* verify PDU length */
  631. tcp_conn->in.datalen = ntoh24(hdr->dlength);
  632. if (tcp_conn->in.datalen > conn->max_recv_dlength) {
  633. printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
  634. tcp_conn->in.datalen, conn->max_recv_dlength);
  635. return ISCSI_ERR_DATALEN;
  636. }
  637. /* Additional header segments. So far, we don't
  638. * process additional headers.
  639. */
  640. ahslen = hdr->hlength << 2;
  641. opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  642. /* verify itt (itt encoding: age+cid+itt) */
  643. rc = iscsi_verify_itt(conn, hdr, &itt);
  644. if (rc == ISCSI_ERR_NO_SCSI_CMD) {
  645. /* XXX: what does this do? */
  646. tcp_conn->in.datalen = 0; /* force drop */
  647. return 0;
  648. } else if (rc)
  649. return rc;
  650. debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
  651. opcode, ahslen, tcp_conn->in.datalen);
  652. switch(opcode) {
  653. case ISCSI_OP_SCSI_DATA_IN:
  654. ctask = session->cmds[itt];
  655. rc = iscsi_data_rsp(conn, ctask);
  656. if (rc)
  657. return rc;
  658. if (tcp_conn->in.datalen) {
  659. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  660. struct hash_desc *rx_hash = NULL;
  661. /*
  662. * Setup copy of Data-In into the Scsi_Cmnd
  663. * Scatterlist case:
  664. * We set up the iscsi_chunk to point to the next
  665. * scatterlist entry to copy to. As we go along,
  666. * we move on to the next scatterlist entry and
  667. * update the digest per-entry.
  668. */
  669. if (conn->datadgst_en)
  670. rx_hash = &tcp_conn->rx_hash;
  671. debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
  672. "datalen=%d)\n", tcp_conn,
  673. tcp_ctask->data_offset,
  674. tcp_conn->in.datalen);
  675. return iscsi_chunk_seek_sg(&tcp_conn->in.chunk,
  676. scsi_sglist(ctask->sc),
  677. scsi_sg_count(ctask->sc),
  678. tcp_ctask->data_offset,
  679. tcp_conn->in.datalen,
  680. iscsi_tcp_process_data_in,
  681. rx_hash);
  682. }
  683. /* fall through */
  684. case ISCSI_OP_SCSI_CMD_RSP:
  685. if (tcp_conn->in.datalen) {
  686. iscsi_tcp_data_recv_prep(tcp_conn);
  687. return 0;
  688. }
  689. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  690. break;
  691. case ISCSI_OP_R2T:
  692. ctask = session->cmds[itt];
  693. if (ahslen)
  694. rc = ISCSI_ERR_AHSLEN;
  695. else if (ctask->sc->sc_data_direction == DMA_TO_DEVICE)
  696. rc = iscsi_r2t_rsp(conn, ctask);
  697. else
  698. rc = ISCSI_ERR_PROTO;
  699. break;
  700. case ISCSI_OP_LOGIN_RSP:
  701. case ISCSI_OP_TEXT_RSP:
  702. case ISCSI_OP_REJECT:
  703. case ISCSI_OP_ASYNC_EVENT:
  704. /*
  705. * It is possible that we could get a PDU with a buffer larger
  706. * than 8K, but there are no targets that currently do this.
  707. * For now we fail until we find a vendor that needs it
  708. */
  709. if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
  710. printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
  711. "but conn buffer is only %u (opcode %0x)\n",
  712. tcp_conn->in.datalen,
  713. ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
  714. rc = ISCSI_ERR_PROTO;
  715. break;
  716. }
  717. /* If there's data coming in with the response,
  718. * receive it to the connection's buffer.
  719. */
  720. if (tcp_conn->in.datalen) {
  721. iscsi_tcp_data_recv_prep(tcp_conn);
  722. return 0;
  723. }
  724. /* fall through */
  725. case ISCSI_OP_LOGOUT_RSP:
  726. case ISCSI_OP_NOOP_IN:
  727. case ISCSI_OP_SCSI_TMFUNC_RSP:
  728. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  729. break;
  730. default:
  731. rc = ISCSI_ERR_BAD_OPCODE;
  732. break;
  733. }
  734. if (rc == 0) {
  735. /* Anything that comes with data should have
  736. * been handled above. */
  737. if (tcp_conn->in.datalen)
  738. return ISCSI_ERR_PROTO;
  739. iscsi_tcp_hdr_recv_prep(tcp_conn);
  740. }
  741. return rc;
  742. }
  743. static inline void
  744. partial_sg_digest_update(struct hash_desc *desc, struct scatterlist *sg,
  745. int offset, int length)
  746. {
  747. struct scatterlist temp;
  748. sg_init_table(&temp, 1);
  749. sg_set_page(&temp, sg_page(sg), length, offset);
  750. crypto_hash_update(desc, &temp, length);
  751. }
  752. /**
  753. * iscsi_tcp_hdr_recv_done - process PDU header
  754. *
  755. * This is the callback invoked when the PDU header has
  756. * been received. If the header is followed by additional
  757. * header segments, we go back for more data.
  758. */
  759. static int
  760. iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  761. struct iscsi_chunk *chunk)
  762. {
  763. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  764. struct iscsi_hdr *hdr;
  765. /* Check if there are additional header segments
  766. * *prior* to computing the digest, because we
  767. * may need to go back to the caller for more.
  768. */
  769. hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
  770. if (chunk->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
  771. /* Bump the header length - the caller will
  772. * just loop around and get the AHS for us, and
  773. * call again. */
  774. unsigned int ahslen = hdr->hlength << 2;
  775. /* Make sure we don't overflow */
  776. if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
  777. return ISCSI_ERR_AHSLEN;
  778. chunk->total_size += ahslen;
  779. chunk->size += ahslen;
  780. return 0;
  781. }
  782. /* We're done processing the header. See if we're doing
  783. * header digests; if so, set up the recv_digest buffer
  784. * and go back for more. */
  785. if (conn->hdrdgst_en) {
  786. if (chunk->digest_len == 0) {
  787. iscsi_tcp_chunk_splice_digest(chunk,
  788. chunk->recv_digest);
  789. return 0;
  790. }
  791. iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
  792. chunk->total_copied - ISCSI_DIGEST_SIZE,
  793. chunk->digest);
  794. if (!iscsi_tcp_dgst_verify(tcp_conn, chunk))
  795. return ISCSI_ERR_HDR_DGST;
  796. }
  797. tcp_conn->in.hdr = hdr;
  798. return iscsi_tcp_hdr_dissect(conn, hdr);
  799. }
  800. /**
  801. * iscsi_tcp_recv - TCP receive in sendfile fashion
  802. * @rd_desc: read descriptor
  803. * @skb: socket buffer
  804. * @offset: offset in skb
  805. * @len: skb->len - offset
  806. **/
  807. static int
  808. iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  809. unsigned int offset, size_t len)
  810. {
  811. struct iscsi_conn *conn = rd_desc->arg.data;
  812. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  813. struct iscsi_chunk *chunk = &tcp_conn->in.chunk;
  814. struct skb_seq_state seq;
  815. unsigned int consumed = 0;
  816. int rc = 0;
  817. debug_tcp("in %d bytes\n", skb->len - offset);
  818. if (unlikely(conn->suspend_rx)) {
  819. debug_tcp("conn %d Rx suspended!\n", conn->id);
  820. return 0;
  821. }
  822. skb_prepare_seq_read(skb, offset, skb->len, &seq);
  823. while (1) {
  824. unsigned int avail;
  825. const u8 *ptr;
  826. avail = skb_seq_read(consumed, &ptr, &seq);
  827. if (avail == 0)
  828. break;
  829. BUG_ON(chunk->copied >= chunk->size);
  830. debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
  831. rc = iscsi_tcp_chunk_recv(tcp_conn, chunk, ptr, avail);
  832. BUG_ON(rc == 0);
  833. consumed += rc;
  834. if (chunk->total_copied >= chunk->total_size) {
  835. rc = chunk->done(tcp_conn, chunk);
  836. if (rc != 0) {
  837. skb_abort_seq_read(&seq);
  838. goto error;
  839. }
  840. /* The done() functions sets up the
  841. * next chunk. */
  842. }
  843. }
  844. conn->rxdata_octets += consumed;
  845. return consumed;
  846. error:
  847. debug_tcp("Error receiving PDU, errno=%d\n", rc);
  848. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  849. return 0;
  850. }
  851. static void
  852. iscsi_tcp_data_ready(struct sock *sk, int flag)
  853. {
  854. struct iscsi_conn *conn = sk->sk_user_data;
  855. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  856. read_descriptor_t rd_desc;
  857. read_lock(&sk->sk_callback_lock);
  858. /*
  859. * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
  860. * We set count to 1 because we want the network layer to
  861. * hand us all the skbs that are available. iscsi_tcp_recv
  862. * handled pdus that cross buffers or pdus that still need data.
  863. */
  864. rd_desc.arg.data = conn;
  865. rd_desc.count = 1;
  866. tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
  867. read_unlock(&sk->sk_callback_lock);
  868. /* If we had to (atomically) map a highmem page,
  869. * unmap it now. */
  870. iscsi_tcp_chunk_unmap(&tcp_conn->in.chunk);
  871. }
  872. static void
  873. iscsi_tcp_state_change(struct sock *sk)
  874. {
  875. struct iscsi_tcp_conn *tcp_conn;
  876. struct iscsi_conn *conn;
  877. struct iscsi_session *session;
  878. void (*old_state_change)(struct sock *);
  879. read_lock(&sk->sk_callback_lock);
  880. conn = (struct iscsi_conn*)sk->sk_user_data;
  881. session = conn->session;
  882. if ((sk->sk_state == TCP_CLOSE_WAIT ||
  883. sk->sk_state == TCP_CLOSE) &&
  884. !atomic_read(&sk->sk_rmem_alloc)) {
  885. debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
  886. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  887. }
  888. tcp_conn = conn->dd_data;
  889. old_state_change = tcp_conn->old_state_change;
  890. read_unlock(&sk->sk_callback_lock);
  891. old_state_change(sk);
  892. }
  893. /**
  894. * iscsi_write_space - Called when more output buffer space is available
  895. * @sk: socket space is available for
  896. **/
  897. static void
  898. iscsi_write_space(struct sock *sk)
  899. {
  900. struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
  901. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  902. tcp_conn->old_write_space(sk);
  903. debug_tcp("iscsi_write_space: cid %d\n", conn->id);
  904. scsi_queue_work(conn->session->host, &conn->xmitwork);
  905. }
  906. static void
  907. iscsi_conn_set_callbacks(struct iscsi_conn *conn)
  908. {
  909. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  910. struct sock *sk = tcp_conn->sock->sk;
  911. /* assign new callbacks */
  912. write_lock_bh(&sk->sk_callback_lock);
  913. sk->sk_user_data = conn;
  914. tcp_conn->old_data_ready = sk->sk_data_ready;
  915. tcp_conn->old_state_change = sk->sk_state_change;
  916. tcp_conn->old_write_space = sk->sk_write_space;
  917. sk->sk_data_ready = iscsi_tcp_data_ready;
  918. sk->sk_state_change = iscsi_tcp_state_change;
  919. sk->sk_write_space = iscsi_write_space;
  920. write_unlock_bh(&sk->sk_callback_lock);
  921. }
  922. static void
  923. iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
  924. {
  925. struct sock *sk = tcp_conn->sock->sk;
  926. /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
  927. write_lock_bh(&sk->sk_callback_lock);
  928. sk->sk_user_data = NULL;
  929. sk->sk_data_ready = tcp_conn->old_data_ready;
  930. sk->sk_state_change = tcp_conn->old_state_change;
  931. sk->sk_write_space = tcp_conn->old_write_space;
  932. sk->sk_no_check = 0;
  933. write_unlock_bh(&sk->sk_callback_lock);
  934. }
  935. /**
  936. * iscsi_send - generic send routine
  937. * @sk: kernel's socket
  938. * @buf: buffer to write from
  939. * @size: actual size to write
  940. * @flags: socket's flags
  941. */
  942. static inline int
  943. iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
  944. {
  945. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  946. struct socket *sk = tcp_conn->sock;
  947. int offset = buf->sg.offset + buf->sent, res;
  948. /*
  949. * if we got use_sg=0 or are sending something we kmallocd
  950. * then we did not have to do kmap (kmap returns page_address)
  951. *
  952. * if we got use_sg > 0, but had to drop down, we do not
  953. * set clustering so this should only happen for that
  954. * slab case.
  955. */
  956. if (buf->use_sendmsg)
  957. res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
  958. else
  959. res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
  960. if (res >= 0) {
  961. conn->txdata_octets += res;
  962. buf->sent += res;
  963. return res;
  964. }
  965. tcp_conn->sendpage_failures_cnt++;
  966. if (res == -EAGAIN)
  967. res = -ENOBUFS;
  968. else
  969. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  970. return res;
  971. }
  972. /**
  973. * iscsi_sendhdr - send PDU Header via tcp_sendpage()
  974. * @conn: iscsi connection
  975. * @buf: buffer to write from
  976. * @datalen: lenght of data to be sent after the header
  977. *
  978. * Notes:
  979. * (Tx, Fast Path)
  980. **/
  981. static inline int
  982. iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
  983. {
  984. int flags = 0; /* MSG_DONTWAIT; */
  985. int res, size;
  986. size = buf->sg.length - buf->sent;
  987. BUG_ON(buf->sent + size > buf->sg.length);
  988. if (buf->sent + size != buf->sg.length || datalen)
  989. flags |= MSG_MORE;
  990. res = iscsi_send(conn, buf, size, flags);
  991. debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
  992. if (res >= 0) {
  993. if (size != res)
  994. return -EAGAIN;
  995. return 0;
  996. }
  997. return res;
  998. }
  999. /**
  1000. * iscsi_sendpage - send one page of iSCSI Data-Out.
  1001. * @conn: iscsi connection
  1002. * @buf: buffer to write from
  1003. * @count: remaining data
  1004. * @sent: number of bytes sent
  1005. *
  1006. * Notes:
  1007. * (Tx, Fast Path)
  1008. **/
  1009. static inline int
  1010. iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
  1011. int *count, int *sent)
  1012. {
  1013. int flags = 0; /* MSG_DONTWAIT; */
  1014. int res, size;
  1015. size = buf->sg.length - buf->sent;
  1016. BUG_ON(buf->sent + size > buf->sg.length);
  1017. if (size > *count)
  1018. size = *count;
  1019. if (buf->sent + size != buf->sg.length || *count != size)
  1020. flags |= MSG_MORE;
  1021. res = iscsi_send(conn, buf, size, flags);
  1022. debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
  1023. size, buf->sent, *count, *sent, res);
  1024. if (res >= 0) {
  1025. *count -= res;
  1026. *sent += res;
  1027. if (size != res)
  1028. return -EAGAIN;
  1029. return 0;
  1030. }
  1031. return res;
  1032. }
  1033. static inline void
  1034. iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
  1035. struct iscsi_tcp_cmd_task *tcp_ctask)
  1036. {
  1037. crypto_hash_init(&tcp_conn->tx_hash);
  1038. tcp_ctask->digest_count = 4;
  1039. }
  1040. /**
  1041. * iscsi_solicit_data_cont - initialize next Data-Out
  1042. * @conn: iscsi connection
  1043. * @ctask: scsi command task
  1044. * @r2t: R2T info
  1045. * @left: bytes left to transfer
  1046. *
  1047. * Notes:
  1048. * Initialize next Data-Out within this R2T sequence and continue
  1049. * to process next Scatter-Gather element(if any) of this SCSI command.
  1050. *
  1051. * Called under connection lock.
  1052. **/
  1053. static void
  1054. iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1055. struct iscsi_r2t_info *r2t, int left)
  1056. {
  1057. struct iscsi_data *hdr;
  1058. int new_offset;
  1059. hdr = &r2t->dtask.hdr;
  1060. memset(hdr, 0, sizeof(struct iscsi_data));
  1061. hdr->ttt = r2t->ttt;
  1062. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  1063. r2t->solicit_datasn++;
  1064. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  1065. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1066. hdr->itt = ctask->hdr->itt;
  1067. hdr->exp_statsn = r2t->exp_statsn;
  1068. new_offset = r2t->data_offset + r2t->sent;
  1069. hdr->offset = cpu_to_be32(new_offset);
  1070. if (left > conn->max_xmit_dlength) {
  1071. hton24(hdr->dlength, conn->max_xmit_dlength);
  1072. r2t->data_count = conn->max_xmit_dlength;
  1073. } else {
  1074. hton24(hdr->dlength, left);
  1075. r2t->data_count = left;
  1076. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  1077. }
  1078. conn->dataout_pdus_cnt++;
  1079. iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
  1080. sizeof(struct iscsi_hdr));
  1081. if (iscsi_buf_left(&r2t->sendbuf))
  1082. return;
  1083. iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
  1084. r2t->sg += 1;
  1085. }
  1086. static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
  1087. unsigned long len)
  1088. {
  1089. tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
  1090. if (!tcp_ctask->pad_count)
  1091. return;
  1092. tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
  1093. debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
  1094. tcp_ctask->xmstate |= XMSTATE_W_PAD;
  1095. }
  1096. /**
  1097. * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
  1098. * @conn: iscsi connection
  1099. * @ctask: scsi command task
  1100. * @sc: scsi command
  1101. **/
  1102. static void
  1103. iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
  1104. {
  1105. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1106. BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
  1107. tcp_ctask->xmstate = XMSTATE_CMD_HDR_INIT;
  1108. }
  1109. /**
  1110. * iscsi_tcp_mtask_xmit - xmit management(immediate) task
  1111. * @conn: iscsi connection
  1112. * @mtask: task management task
  1113. *
  1114. * Notes:
  1115. * The function can return -EAGAIN in which case caller must
  1116. * call it again later, or recover. '0' return code means successful
  1117. * xmit.
  1118. *
  1119. * Management xmit state machine consists of these states:
  1120. * XMSTATE_IMM_HDR_INIT - calculate digest of PDU Header
  1121. * XMSTATE_IMM_HDR - PDU Header xmit in progress
  1122. * XMSTATE_IMM_DATA - PDU Data xmit in progress
  1123. * XMSTATE_IDLE - management PDU is done
  1124. **/
  1125. static int
  1126. iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
  1127. {
  1128. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1129. int rc;
  1130. debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
  1131. conn->id, tcp_mtask->xmstate, mtask->itt);
  1132. if (tcp_mtask->xmstate & XMSTATE_IMM_HDR_INIT) {
  1133. iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
  1134. sizeof(struct iscsi_hdr));
  1135. if (mtask->data_count) {
  1136. tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
  1137. iscsi_buf_init_iov(&tcp_mtask->sendbuf,
  1138. (char*)mtask->data,
  1139. mtask->data_count);
  1140. }
  1141. if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
  1142. conn->stop_stage != STOP_CONN_RECOVER &&
  1143. conn->hdrdgst_en)
  1144. iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
  1145. (u8*)tcp_mtask->hdrext);
  1146. tcp_mtask->sent = 0;
  1147. tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR_INIT;
  1148. tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
  1149. }
  1150. if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
  1151. rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
  1152. mtask->data_count);
  1153. if (rc)
  1154. return rc;
  1155. tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
  1156. }
  1157. if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
  1158. BUG_ON(!mtask->data_count);
  1159. tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
  1160. /* FIXME: implement.
  1161. * Virtual buffer could be spreaded across multiple pages...
  1162. */
  1163. do {
  1164. int rc;
  1165. rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
  1166. &mtask->data_count, &tcp_mtask->sent);
  1167. if (rc) {
  1168. tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
  1169. return rc;
  1170. }
  1171. } while (mtask->data_count);
  1172. }
  1173. BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
  1174. if (mtask->hdr->itt == RESERVED_ITT) {
  1175. struct iscsi_session *session = conn->session;
  1176. spin_lock_bh(&session->lock);
  1177. iscsi_free_mgmt_task(conn, mtask);
  1178. spin_unlock_bh(&session->lock);
  1179. }
  1180. return 0;
  1181. }
  1182. static int
  1183. iscsi_send_cmd_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1184. {
  1185. struct scsi_cmnd *sc = ctask->sc;
  1186. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1187. int rc = 0;
  1188. if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_INIT) {
  1189. tcp_ctask->sent = 0;
  1190. tcp_ctask->sg_count = 0;
  1191. tcp_ctask->exp_datasn = 0;
  1192. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  1193. struct scatterlist *sg = scsi_sglist(sc);
  1194. iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
  1195. tcp_ctask->sg = sg + 1;
  1196. tcp_ctask->bad_sg = sg + scsi_sg_count(sc);
  1197. debug_scsi("cmd [itt 0x%x total %d imm_data %d "
  1198. "unsol count %d, unsol offset %d]\n",
  1199. ctask->itt, scsi_bufflen(sc),
  1200. ctask->imm_count, ctask->unsol_count,
  1201. ctask->unsol_offset);
  1202. }
  1203. iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
  1204. ctask->hdr_len);
  1205. if (conn->hdrdgst_en)
  1206. iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
  1207. iscsi_next_hdr(ctask));
  1208. tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_INIT;
  1209. tcp_ctask->xmstate |= XMSTATE_CMD_HDR_XMIT;
  1210. }
  1211. if (tcp_ctask->xmstate & XMSTATE_CMD_HDR_XMIT) {
  1212. rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
  1213. if (rc)
  1214. return rc;
  1215. tcp_ctask->xmstate &= ~XMSTATE_CMD_HDR_XMIT;
  1216. if (sc->sc_data_direction != DMA_TO_DEVICE)
  1217. return 0;
  1218. if (ctask->imm_count) {
  1219. tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
  1220. iscsi_set_padding(tcp_ctask, ctask->imm_count);
  1221. if (ctask->conn->datadgst_en) {
  1222. iscsi_data_digest_init(ctask->conn->dd_data,
  1223. tcp_ctask);
  1224. tcp_ctask->immdigest = 0;
  1225. }
  1226. }
  1227. if (ctask->unsol_count)
  1228. tcp_ctask->xmstate |=
  1229. XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
  1230. }
  1231. return rc;
  1232. }
  1233. static int
  1234. iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1235. {
  1236. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1237. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1238. int sent = 0, rc;
  1239. if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
  1240. iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
  1241. tcp_ctask->pad_count);
  1242. if (conn->datadgst_en)
  1243. crypto_hash_update(&tcp_conn->tx_hash,
  1244. &tcp_ctask->sendbuf.sg,
  1245. tcp_ctask->sendbuf.sg.length);
  1246. } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
  1247. return 0;
  1248. tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
  1249. tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
  1250. debug_scsi("sending %d pad bytes for itt 0x%x\n",
  1251. tcp_ctask->pad_count, ctask->itt);
  1252. rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
  1253. &sent);
  1254. if (rc) {
  1255. debug_scsi("padding send failed %d\n", rc);
  1256. tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
  1257. }
  1258. return rc;
  1259. }
  1260. static int
  1261. iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1262. struct iscsi_buf *buf, uint32_t *digest)
  1263. {
  1264. struct iscsi_tcp_cmd_task *tcp_ctask;
  1265. struct iscsi_tcp_conn *tcp_conn;
  1266. int rc, sent = 0;
  1267. if (!conn->datadgst_en)
  1268. return 0;
  1269. tcp_ctask = ctask->dd_data;
  1270. tcp_conn = conn->dd_data;
  1271. if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
  1272. crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
  1273. iscsi_buf_init_iov(buf, (char*)digest, 4);
  1274. }
  1275. tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
  1276. rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
  1277. if (!rc)
  1278. debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
  1279. ctask->itt);
  1280. else {
  1281. debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
  1282. *digest, ctask->itt);
  1283. tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
  1284. }
  1285. return rc;
  1286. }
  1287. static int
  1288. iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
  1289. struct scatterlist **sg, int *sent, int *count,
  1290. struct iscsi_buf *digestbuf, uint32_t *digest)
  1291. {
  1292. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1293. struct iscsi_conn *conn = ctask->conn;
  1294. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1295. int rc, buf_sent, offset;
  1296. while (*count) {
  1297. buf_sent = 0;
  1298. offset = sendbuf->sent;
  1299. rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
  1300. *sent = *sent + buf_sent;
  1301. if (buf_sent && conn->datadgst_en)
  1302. partial_sg_digest_update(&tcp_conn->tx_hash,
  1303. &sendbuf->sg, sendbuf->sg.offset + offset,
  1304. buf_sent);
  1305. if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
  1306. iscsi_buf_init_sg(sendbuf, *sg);
  1307. *sg = *sg + 1;
  1308. }
  1309. if (rc)
  1310. return rc;
  1311. }
  1312. rc = iscsi_send_padding(conn, ctask);
  1313. if (rc)
  1314. return rc;
  1315. return iscsi_send_digest(conn, ctask, digestbuf, digest);
  1316. }
  1317. static int
  1318. iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1319. {
  1320. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1321. struct iscsi_data_task *dtask;
  1322. int rc;
  1323. tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
  1324. if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
  1325. dtask = &tcp_ctask->unsol_dtask;
  1326. iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
  1327. iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
  1328. sizeof(struct iscsi_hdr));
  1329. if (conn->hdrdgst_en)
  1330. iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
  1331. (u8*)dtask->hdrext);
  1332. tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
  1333. iscsi_set_padding(tcp_ctask, ctask->data_count);
  1334. }
  1335. rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
  1336. if (rc) {
  1337. tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
  1338. tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
  1339. return rc;
  1340. }
  1341. if (conn->datadgst_en) {
  1342. dtask = &tcp_ctask->unsol_dtask;
  1343. iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
  1344. dtask->digest = 0;
  1345. }
  1346. debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
  1347. ctask->itt, ctask->unsol_count, tcp_ctask->sent);
  1348. return 0;
  1349. }
  1350. static int
  1351. iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1352. {
  1353. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1354. int rc;
  1355. if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
  1356. BUG_ON(!ctask->unsol_count);
  1357. tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
  1358. send_hdr:
  1359. rc = iscsi_send_unsol_hdr(conn, ctask);
  1360. if (rc)
  1361. return rc;
  1362. }
  1363. if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
  1364. struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
  1365. int start = tcp_ctask->sent;
  1366. rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
  1367. &tcp_ctask->sent, &ctask->data_count,
  1368. &dtask->digestbuf, &dtask->digest);
  1369. ctask->unsol_count -= tcp_ctask->sent - start;
  1370. if (rc)
  1371. return rc;
  1372. tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
  1373. /*
  1374. * Done with the Data-Out. Next, check if we need
  1375. * to send another unsolicited Data-Out.
  1376. */
  1377. if (ctask->unsol_count) {
  1378. debug_scsi("sending more uns\n");
  1379. tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
  1380. goto send_hdr;
  1381. }
  1382. }
  1383. return 0;
  1384. }
  1385. static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
  1386. struct iscsi_cmd_task *ctask)
  1387. {
  1388. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1389. struct iscsi_session *session = conn->session;
  1390. struct iscsi_r2t_info *r2t;
  1391. struct iscsi_data_task *dtask;
  1392. int left, rc;
  1393. if (tcp_ctask->xmstate & XMSTATE_SOL_HDR_INIT) {
  1394. if (!tcp_ctask->r2t) {
  1395. spin_lock_bh(&session->lock);
  1396. __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
  1397. sizeof(void*));
  1398. spin_unlock_bh(&session->lock);
  1399. }
  1400. send_hdr:
  1401. r2t = tcp_ctask->r2t;
  1402. dtask = &r2t->dtask;
  1403. if (conn->hdrdgst_en)
  1404. iscsi_hdr_digest(conn, &r2t->headbuf,
  1405. (u8*)dtask->hdrext);
  1406. tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR_INIT;
  1407. tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
  1408. }
  1409. if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
  1410. r2t = tcp_ctask->r2t;
  1411. dtask = &r2t->dtask;
  1412. rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
  1413. if (rc)
  1414. return rc;
  1415. tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
  1416. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1417. if (conn->datadgst_en) {
  1418. iscsi_data_digest_init(conn->dd_data, tcp_ctask);
  1419. dtask->digest = 0;
  1420. }
  1421. iscsi_set_padding(tcp_ctask, r2t->data_count);
  1422. debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
  1423. r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
  1424. r2t->sent);
  1425. }
  1426. if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
  1427. r2t = tcp_ctask->r2t;
  1428. dtask = &r2t->dtask;
  1429. rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
  1430. &r2t->sent, &r2t->data_count,
  1431. &dtask->digestbuf, &dtask->digest);
  1432. if (rc)
  1433. return rc;
  1434. tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
  1435. /*
  1436. * Done with this Data-Out. Next, check if we have
  1437. * to send another Data-Out for this R2T.
  1438. */
  1439. BUG_ON(r2t->data_length - r2t->sent < 0);
  1440. left = r2t->data_length - r2t->sent;
  1441. if (left) {
  1442. iscsi_solicit_data_cont(conn, ctask, r2t, left);
  1443. goto send_hdr;
  1444. }
  1445. /*
  1446. * Done with this R2T. Check if there are more
  1447. * outstanding R2Ts ready to be processed.
  1448. */
  1449. spin_lock_bh(&session->lock);
  1450. tcp_ctask->r2t = NULL;
  1451. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  1452. sizeof(void*));
  1453. if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
  1454. sizeof(void*))) {
  1455. tcp_ctask->r2t = r2t;
  1456. spin_unlock_bh(&session->lock);
  1457. goto send_hdr;
  1458. }
  1459. spin_unlock_bh(&session->lock);
  1460. }
  1461. return 0;
  1462. }
  1463. /**
  1464. * iscsi_tcp_ctask_xmit - xmit normal PDU task
  1465. * @conn: iscsi connection
  1466. * @ctask: iscsi command task
  1467. *
  1468. * Notes:
  1469. * The function can return -EAGAIN in which case caller must
  1470. * call it again later, or recover. '0' return code means successful
  1471. * xmit.
  1472. * The function is devided to logical helpers (above) for the different
  1473. * xmit stages.
  1474. *
  1475. *iscsi_send_cmd_hdr()
  1476. * XMSTATE_CMD_HDR_INIT - prepare Header and Data buffers Calculate
  1477. * Header Digest
  1478. * XMSTATE_CMD_HDR_XMIT - Transmit header in progress
  1479. *
  1480. *iscsi_send_padding
  1481. * XMSTATE_W_PAD - Prepare and send pading
  1482. * XMSTATE_W_RESEND_PAD - retry send pading
  1483. *
  1484. *iscsi_send_digest
  1485. * XMSTATE_W_RESEND_DATA_DIGEST - Finalize and send Data Digest
  1486. * XMSTATE_W_RESEND_DATA_DIGEST - retry sending digest
  1487. *
  1488. *iscsi_send_unsol_hdr
  1489. * XMSTATE_UNS_INIT - prepare un-solicit data header and digest
  1490. * XMSTATE_UNS_HDR - send un-solicit header
  1491. *
  1492. *iscsi_send_unsol_pdu
  1493. * XMSTATE_UNS_DATA - send un-solicit data in progress
  1494. *
  1495. *iscsi_send_sol_pdu
  1496. * XMSTATE_SOL_HDR_INIT - solicit data header and digest initialize
  1497. * XMSTATE_SOL_HDR - send solicit header
  1498. * XMSTATE_SOL_DATA - send solicit data
  1499. *
  1500. *iscsi_tcp_ctask_xmit
  1501. * XMSTATE_IMM_DATA - xmit managment data (??)
  1502. **/
  1503. static int
  1504. iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1505. {
  1506. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1507. int rc = 0;
  1508. debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
  1509. conn->id, tcp_ctask->xmstate, ctask->itt);
  1510. rc = iscsi_send_cmd_hdr(conn, ctask);
  1511. if (rc)
  1512. return rc;
  1513. if (ctask->sc->sc_data_direction != DMA_TO_DEVICE)
  1514. return 0;
  1515. if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
  1516. rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
  1517. &tcp_ctask->sent, &ctask->imm_count,
  1518. &tcp_ctask->immbuf, &tcp_ctask->immdigest);
  1519. if (rc)
  1520. return rc;
  1521. tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
  1522. }
  1523. rc = iscsi_send_unsol_pdu(conn, ctask);
  1524. if (rc)
  1525. return rc;
  1526. rc = iscsi_send_sol_pdu(conn, ctask);
  1527. if (rc)
  1528. return rc;
  1529. return rc;
  1530. }
  1531. static struct iscsi_cls_conn *
  1532. iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1533. {
  1534. struct iscsi_conn *conn;
  1535. struct iscsi_cls_conn *cls_conn;
  1536. struct iscsi_tcp_conn *tcp_conn;
  1537. cls_conn = iscsi_conn_setup(cls_session, conn_idx);
  1538. if (!cls_conn)
  1539. return NULL;
  1540. conn = cls_conn->dd_data;
  1541. /*
  1542. * due to strange issues with iser these are not set
  1543. * in iscsi_conn_setup
  1544. */
  1545. conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
  1546. tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
  1547. if (!tcp_conn)
  1548. goto tcp_conn_alloc_fail;
  1549. conn->dd_data = tcp_conn;
  1550. tcp_conn->iscsi_conn = conn;
  1551. tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1552. CRYPTO_ALG_ASYNC);
  1553. tcp_conn->tx_hash.flags = 0;
  1554. if (IS_ERR(tcp_conn->tx_hash.tfm)) {
  1555. printk(KERN_ERR "Could not create connection due to crc32c "
  1556. "loading error %ld. Make sure the crc32c module is "
  1557. "built as a module or into the kernel\n",
  1558. PTR_ERR(tcp_conn->tx_hash.tfm));
  1559. goto free_tcp_conn;
  1560. }
  1561. tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1562. CRYPTO_ALG_ASYNC);
  1563. tcp_conn->rx_hash.flags = 0;
  1564. if (IS_ERR(tcp_conn->rx_hash.tfm)) {
  1565. printk(KERN_ERR "Could not create connection due to crc32c "
  1566. "loading error %ld. Make sure the crc32c module is "
  1567. "built as a module or into the kernel\n",
  1568. PTR_ERR(tcp_conn->rx_hash.tfm));
  1569. goto free_tx_tfm;
  1570. }
  1571. return cls_conn;
  1572. free_tx_tfm:
  1573. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1574. free_tcp_conn:
  1575. kfree(tcp_conn);
  1576. tcp_conn_alloc_fail:
  1577. iscsi_conn_teardown(cls_conn);
  1578. return NULL;
  1579. }
  1580. static void
  1581. iscsi_tcp_release_conn(struct iscsi_conn *conn)
  1582. {
  1583. struct iscsi_session *session = conn->session;
  1584. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1585. struct socket *sock = tcp_conn->sock;
  1586. if (!sock)
  1587. return;
  1588. sock_hold(sock->sk);
  1589. iscsi_conn_restore_callbacks(tcp_conn);
  1590. sock_put(sock->sk);
  1591. spin_lock_bh(&session->lock);
  1592. tcp_conn->sock = NULL;
  1593. conn->recv_lock = NULL;
  1594. spin_unlock_bh(&session->lock);
  1595. sockfd_put(sock);
  1596. }
  1597. static void
  1598. iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1599. {
  1600. struct iscsi_conn *conn = cls_conn->dd_data;
  1601. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1602. iscsi_tcp_release_conn(conn);
  1603. iscsi_conn_teardown(cls_conn);
  1604. if (tcp_conn->tx_hash.tfm)
  1605. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1606. if (tcp_conn->rx_hash.tfm)
  1607. crypto_free_hash(tcp_conn->rx_hash.tfm);
  1608. kfree(tcp_conn);
  1609. }
  1610. static void
  1611. iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1612. {
  1613. struct iscsi_conn *conn = cls_conn->dd_data;
  1614. iscsi_conn_stop(cls_conn, flag);
  1615. iscsi_tcp_release_conn(conn);
  1616. }
  1617. static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
  1618. char *buf, int *port,
  1619. int (*getname)(struct socket *, struct sockaddr *,
  1620. int *addrlen))
  1621. {
  1622. struct sockaddr_storage *addr;
  1623. struct sockaddr_in6 *sin6;
  1624. struct sockaddr_in *sin;
  1625. int rc = 0, len;
  1626. addr = kmalloc(sizeof(*addr), GFP_KERNEL);
  1627. if (!addr)
  1628. return -ENOMEM;
  1629. if (getname(sock, (struct sockaddr *) addr, &len)) {
  1630. rc = -ENODEV;
  1631. goto free_addr;
  1632. }
  1633. switch (addr->ss_family) {
  1634. case AF_INET:
  1635. sin = (struct sockaddr_in *)addr;
  1636. spin_lock_bh(&conn->session->lock);
  1637. sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
  1638. *port = be16_to_cpu(sin->sin_port);
  1639. spin_unlock_bh(&conn->session->lock);
  1640. break;
  1641. case AF_INET6:
  1642. sin6 = (struct sockaddr_in6 *)addr;
  1643. spin_lock_bh(&conn->session->lock);
  1644. sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
  1645. *port = be16_to_cpu(sin6->sin6_port);
  1646. spin_unlock_bh(&conn->session->lock);
  1647. break;
  1648. }
  1649. free_addr:
  1650. kfree(addr);
  1651. return rc;
  1652. }
  1653. static int
  1654. iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  1655. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  1656. int is_leading)
  1657. {
  1658. struct iscsi_conn *conn = cls_conn->dd_data;
  1659. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1660. struct sock *sk;
  1661. struct socket *sock;
  1662. int err;
  1663. /* lookup for existing socket */
  1664. sock = sockfd_lookup((int)transport_eph, &err);
  1665. if (!sock) {
  1666. printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
  1667. return -EEXIST;
  1668. }
  1669. /*
  1670. * copy these values now because if we drop the session
  1671. * userspace may still want to query the values since we will
  1672. * be using them for the reconnect
  1673. */
  1674. err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
  1675. &conn->portal_port, kernel_getpeername);
  1676. if (err)
  1677. goto free_socket;
  1678. err = iscsi_tcp_get_addr(conn, sock, conn->local_address,
  1679. &conn->local_port, kernel_getsockname);
  1680. if (err)
  1681. goto free_socket;
  1682. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  1683. if (err)
  1684. goto free_socket;
  1685. /* bind iSCSI connection and socket */
  1686. tcp_conn->sock = sock;
  1687. /* setup Socket parameters */
  1688. sk = sock->sk;
  1689. sk->sk_reuse = 1;
  1690. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  1691. sk->sk_allocation = GFP_ATOMIC;
  1692. /* FIXME: disable Nagle's algorithm */
  1693. /*
  1694. * Intercept TCP callbacks for sendfile like receive
  1695. * processing.
  1696. */
  1697. conn->recv_lock = &sk->sk_callback_lock;
  1698. iscsi_conn_set_callbacks(conn);
  1699. tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
  1700. /*
  1701. * set receive state machine into initial state
  1702. */
  1703. iscsi_tcp_hdr_recv_prep(tcp_conn);
  1704. return 0;
  1705. free_socket:
  1706. sockfd_put(sock);
  1707. return err;
  1708. }
  1709. /* called with host lock */
  1710. static void
  1711. iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
  1712. {
  1713. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1714. tcp_mtask->xmstate = XMSTATE_IMM_HDR_INIT;
  1715. }
  1716. static int
  1717. iscsi_r2tpool_alloc(struct iscsi_session *session)
  1718. {
  1719. int i;
  1720. int cmd_i;
  1721. /*
  1722. * initialize per-task: R2T pool and xmit queue
  1723. */
  1724. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1725. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1726. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1727. /*
  1728. * pre-allocated x4 as much r2ts to handle race when
  1729. * target acks DataOut faster than we data_xmit() queues
  1730. * could replenish r2tqueue.
  1731. */
  1732. /* R2T pool */
  1733. if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, NULL,
  1734. sizeof(struct iscsi_r2t_info))) {
  1735. goto r2t_alloc_fail;
  1736. }
  1737. /* R2T xmit queue */
  1738. tcp_ctask->r2tqueue = kfifo_alloc(
  1739. session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
  1740. if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
  1741. iscsi_pool_free(&tcp_ctask->r2tpool);
  1742. goto r2t_alloc_fail;
  1743. }
  1744. }
  1745. return 0;
  1746. r2t_alloc_fail:
  1747. for (i = 0; i < cmd_i; i++) {
  1748. struct iscsi_cmd_task *ctask = session->cmds[i];
  1749. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1750. kfifo_free(tcp_ctask->r2tqueue);
  1751. iscsi_pool_free(&tcp_ctask->r2tpool);
  1752. }
  1753. return -ENOMEM;
  1754. }
  1755. static void
  1756. iscsi_r2tpool_free(struct iscsi_session *session)
  1757. {
  1758. int i;
  1759. for (i = 0; i < session->cmds_max; i++) {
  1760. struct iscsi_cmd_task *ctask = session->cmds[i];
  1761. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1762. kfifo_free(tcp_ctask->r2tqueue);
  1763. iscsi_pool_free(&tcp_ctask->r2tpool);
  1764. }
  1765. }
  1766. static int
  1767. iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
  1768. char *buf, int buflen)
  1769. {
  1770. struct iscsi_conn *conn = cls_conn->dd_data;
  1771. struct iscsi_session *session = conn->session;
  1772. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1773. int value;
  1774. switch(param) {
  1775. case ISCSI_PARAM_HDRDGST_EN:
  1776. iscsi_set_param(cls_conn, param, buf, buflen);
  1777. break;
  1778. case ISCSI_PARAM_DATADGST_EN:
  1779. iscsi_set_param(cls_conn, param, buf, buflen);
  1780. tcp_conn->sendpage = conn->datadgst_en ?
  1781. sock_no_sendpage : tcp_conn->sock->ops->sendpage;
  1782. break;
  1783. case ISCSI_PARAM_MAX_R2T:
  1784. sscanf(buf, "%d", &value);
  1785. if (session->max_r2t == roundup_pow_of_two(value))
  1786. break;
  1787. iscsi_r2tpool_free(session);
  1788. iscsi_set_param(cls_conn, param, buf, buflen);
  1789. if (session->max_r2t & (session->max_r2t - 1))
  1790. session->max_r2t = roundup_pow_of_two(session->max_r2t);
  1791. if (iscsi_r2tpool_alloc(session))
  1792. return -ENOMEM;
  1793. break;
  1794. default:
  1795. return iscsi_set_param(cls_conn, param, buf, buflen);
  1796. }
  1797. return 0;
  1798. }
  1799. static int
  1800. iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1801. enum iscsi_param param, char *buf)
  1802. {
  1803. struct iscsi_conn *conn = cls_conn->dd_data;
  1804. int len;
  1805. switch(param) {
  1806. case ISCSI_PARAM_CONN_PORT:
  1807. spin_lock_bh(&conn->session->lock);
  1808. len = sprintf(buf, "%hu\n", conn->portal_port);
  1809. spin_unlock_bh(&conn->session->lock);
  1810. break;
  1811. case ISCSI_PARAM_CONN_ADDRESS:
  1812. spin_lock_bh(&conn->session->lock);
  1813. len = sprintf(buf, "%s\n", conn->portal_address);
  1814. spin_unlock_bh(&conn->session->lock);
  1815. break;
  1816. default:
  1817. return iscsi_conn_get_param(cls_conn, param, buf);
  1818. }
  1819. return len;
  1820. }
  1821. static int
  1822. iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1823. char *buf)
  1824. {
  1825. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1826. int len;
  1827. switch (param) {
  1828. case ISCSI_HOST_PARAM_IPADDRESS:
  1829. spin_lock_bh(&session->lock);
  1830. if (!session->leadconn)
  1831. len = -ENODEV;
  1832. else
  1833. len = sprintf(buf, "%s\n",
  1834. session->leadconn->local_address);
  1835. spin_unlock_bh(&session->lock);
  1836. break;
  1837. default:
  1838. return iscsi_host_get_param(shost, param, buf);
  1839. }
  1840. return len;
  1841. }
  1842. static void
  1843. iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
  1844. {
  1845. struct iscsi_conn *conn = cls_conn->dd_data;
  1846. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1847. stats->txdata_octets = conn->txdata_octets;
  1848. stats->rxdata_octets = conn->rxdata_octets;
  1849. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1850. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1851. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1852. stats->datain_pdus = conn->datain_pdus_cnt;
  1853. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1854. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1855. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1856. stats->custom_length = 3;
  1857. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  1858. stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
  1859. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  1860. stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
  1861. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  1862. stats->custom[2].value = conn->eh_abort_cnt;
  1863. }
  1864. static struct iscsi_cls_session *
  1865. iscsi_tcp_session_create(struct iscsi_transport *iscsit,
  1866. struct scsi_transport_template *scsit,
  1867. uint16_t cmds_max, uint16_t qdepth,
  1868. uint32_t initial_cmdsn, uint32_t *hostno)
  1869. {
  1870. struct iscsi_cls_session *cls_session;
  1871. struct iscsi_session *session;
  1872. uint32_t hn;
  1873. int cmd_i;
  1874. cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
  1875. sizeof(struct iscsi_tcp_cmd_task),
  1876. sizeof(struct iscsi_tcp_mgmt_task),
  1877. initial_cmdsn, &hn);
  1878. if (!cls_session)
  1879. return NULL;
  1880. *hostno = hn;
  1881. session = class_to_transport_session(cls_session);
  1882. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1883. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1884. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1885. ctask->hdr = &tcp_ctask->hdr.cmd_hdr;
  1886. ctask->hdr_max = sizeof(tcp_ctask->hdr) - ISCSI_DIGEST_SIZE;
  1887. }
  1888. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1889. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1890. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1891. mtask->hdr = &tcp_mtask->hdr;
  1892. }
  1893. if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
  1894. goto r2tpool_alloc_fail;
  1895. return cls_session;
  1896. r2tpool_alloc_fail:
  1897. iscsi_session_teardown(cls_session);
  1898. return NULL;
  1899. }
  1900. static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  1901. {
  1902. iscsi_r2tpool_free(class_to_transport_session(cls_session));
  1903. iscsi_session_teardown(cls_session);
  1904. }
  1905. static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
  1906. {
  1907. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
  1908. blk_queue_dma_alignment(sdev->request_queue, 0);
  1909. return 0;
  1910. }
  1911. static struct scsi_host_template iscsi_sht = {
  1912. .module = THIS_MODULE,
  1913. .name = "iSCSI Initiator over TCP/IP",
  1914. .queuecommand = iscsi_queuecommand,
  1915. .change_queue_depth = iscsi_change_queue_depth,
  1916. .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
  1917. .sg_tablesize = ISCSI_SG_TABLESIZE,
  1918. .max_sectors = 0xFFFF,
  1919. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  1920. .eh_abort_handler = iscsi_eh_abort,
  1921. .eh_device_reset_handler= iscsi_eh_device_reset,
  1922. .eh_host_reset_handler = iscsi_eh_host_reset,
  1923. .use_clustering = DISABLE_CLUSTERING,
  1924. .slave_configure = iscsi_tcp_slave_configure,
  1925. .proc_name = "iscsi_tcp",
  1926. .this_id = -1,
  1927. };
  1928. static struct iscsi_transport iscsi_tcp_transport = {
  1929. .owner = THIS_MODULE,
  1930. .name = "tcp",
  1931. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  1932. | CAP_DATADGST,
  1933. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  1934. ISCSI_MAX_XMIT_DLENGTH |
  1935. ISCSI_HDRDGST_EN |
  1936. ISCSI_DATADGST_EN |
  1937. ISCSI_INITIAL_R2T_EN |
  1938. ISCSI_MAX_R2T |
  1939. ISCSI_IMM_DATA_EN |
  1940. ISCSI_FIRST_BURST |
  1941. ISCSI_MAX_BURST |
  1942. ISCSI_PDU_INORDER_EN |
  1943. ISCSI_DATASEQ_INORDER_EN |
  1944. ISCSI_ERL |
  1945. ISCSI_CONN_PORT |
  1946. ISCSI_CONN_ADDRESS |
  1947. ISCSI_EXP_STATSN |
  1948. ISCSI_PERSISTENT_PORT |
  1949. ISCSI_PERSISTENT_ADDRESS |
  1950. ISCSI_TARGET_NAME | ISCSI_TPGT |
  1951. ISCSI_USERNAME | ISCSI_PASSWORD |
  1952. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  1953. ISCSI_FAST_ABORT,
  1954. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  1955. ISCSI_HOST_INITIATOR_NAME |
  1956. ISCSI_HOST_NETDEV_NAME,
  1957. .host_template = &iscsi_sht,
  1958. .conndata_size = sizeof(struct iscsi_conn),
  1959. .max_conn = 1,
  1960. .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
  1961. /* session management */
  1962. .create_session = iscsi_tcp_session_create,
  1963. .destroy_session = iscsi_tcp_session_destroy,
  1964. /* connection management */
  1965. .create_conn = iscsi_tcp_conn_create,
  1966. .bind_conn = iscsi_tcp_conn_bind,
  1967. .destroy_conn = iscsi_tcp_conn_destroy,
  1968. .set_param = iscsi_conn_set_param,
  1969. .get_conn_param = iscsi_tcp_conn_get_param,
  1970. .get_session_param = iscsi_session_get_param,
  1971. .start_conn = iscsi_conn_start,
  1972. .stop_conn = iscsi_tcp_conn_stop,
  1973. /* iscsi host params */
  1974. .get_host_param = iscsi_tcp_host_get_param,
  1975. .set_host_param = iscsi_host_set_param,
  1976. /* IO */
  1977. .send_pdu = iscsi_conn_send_pdu,
  1978. .get_stats = iscsi_conn_get_stats,
  1979. .init_cmd_task = iscsi_tcp_cmd_init,
  1980. .init_mgmt_task = iscsi_tcp_mgmt_init,
  1981. .xmit_cmd_task = iscsi_tcp_ctask_xmit,
  1982. .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
  1983. .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
  1984. /* recovery */
  1985. .session_recovery_timedout = iscsi_session_recovery_timedout,
  1986. };
  1987. static int __init
  1988. iscsi_tcp_init(void)
  1989. {
  1990. if (iscsi_max_lun < 1) {
  1991. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  1992. iscsi_max_lun);
  1993. return -EINVAL;
  1994. }
  1995. iscsi_tcp_transport.max_lun = iscsi_max_lun;
  1996. if (!iscsi_register_transport(&iscsi_tcp_transport))
  1997. return -ENODEV;
  1998. return 0;
  1999. }
  2000. static void __exit
  2001. iscsi_tcp_exit(void)
  2002. {
  2003. iscsi_unregister_transport(&iscsi_tcp_transport);
  2004. }
  2005. module_init(iscsi_tcp_init);
  2006. module_exit(iscsi_tcp_exit);