iscsi_tcp.c 58 KB

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