iscsi_tcp.c 58 KB

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