iscsi_tcp.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400
  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 += sizeof(uint32_t);
  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. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  248. hdr = &r2t->dtask.hdr;
  249. memset(hdr, 0, sizeof(struct iscsi_data));
  250. hdr->ttt = r2t->ttt;
  251. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  252. r2t->solicit_datasn++;
  253. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  254. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  255. hdr->itt = ctask->hdr->itt;
  256. hdr->exp_statsn = r2t->exp_statsn;
  257. hdr->offset = cpu_to_be32(r2t->data_offset);
  258. if (r2t->data_length > conn->max_xmit_dlength) {
  259. hton24(hdr->dlength, conn->max_xmit_dlength);
  260. r2t->data_count = conn->max_xmit_dlength;
  261. hdr->flags = 0;
  262. } else {
  263. hton24(hdr->dlength, r2t->data_length);
  264. r2t->data_count = r2t->data_length;
  265. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  266. }
  267. conn->dataout_pdus_cnt++;
  268. r2t->sent = 0;
  269. iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
  270. sizeof(struct iscsi_hdr));
  271. if (sc->use_sg) {
  272. int i, sg_count = 0;
  273. struct scatterlist *sg = sc->request_buffer;
  274. r2t->sg = NULL;
  275. for (i = 0; i < sc->use_sg; i++, sg += 1) {
  276. /* FIXME: prefetch ? */
  277. if (sg_count + sg->length > r2t->data_offset) {
  278. int page_offset;
  279. /* sg page found! */
  280. /* offset within this page */
  281. page_offset = r2t->data_offset - sg_count;
  282. /* fill in this buffer */
  283. iscsi_buf_init_sg(&r2t->sendbuf, sg);
  284. r2t->sendbuf.sg.offset += page_offset;
  285. r2t->sendbuf.sg.length -= page_offset;
  286. /* xmit logic will continue with next one */
  287. r2t->sg = sg + 1;
  288. break;
  289. }
  290. sg_count += sg->length;
  291. }
  292. BUG_ON(r2t->sg == NULL);
  293. } else
  294. iscsi_buf_init_iov(&tcp_ctask->sendbuf,
  295. (char*)sc->request_buffer + r2t->data_offset,
  296. r2t->data_count);
  297. }
  298. /**
  299. * iscsi_r2t_rsp - iSCSI R2T Response processing
  300. * @conn: iscsi connection
  301. * @ctask: scsi command task
  302. **/
  303. static int
  304. iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  305. {
  306. struct iscsi_r2t_info *r2t;
  307. struct iscsi_session *session = conn->session;
  308. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  309. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  310. struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
  311. int r2tsn = be32_to_cpu(rhdr->r2tsn);
  312. int rc;
  313. if (tcp_conn->in.datalen) {
  314. printk(KERN_ERR "iscsi_tcp: invalid R2t with datalen %d\n",
  315. tcp_conn->in.datalen);
  316. return ISCSI_ERR_DATALEN;
  317. }
  318. if (tcp_ctask->exp_r2tsn && tcp_ctask->exp_r2tsn != r2tsn)
  319. return ISCSI_ERR_R2TSN;
  320. rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
  321. if (rc)
  322. return rc;
  323. /* FIXME: use R2TSN to detect missing R2T */
  324. /* fill-in new R2T associated with the task */
  325. spin_lock(&session->lock);
  326. if (!ctask->sc || ctask->mtask ||
  327. session->state != ISCSI_STATE_LOGGED_IN) {
  328. printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
  329. "recovery...\n", ctask->itt);
  330. spin_unlock(&session->lock);
  331. return 0;
  332. }
  333. rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
  334. BUG_ON(!rc);
  335. r2t->exp_statsn = rhdr->statsn;
  336. r2t->data_length = be32_to_cpu(rhdr->data_length);
  337. if (r2t->data_length == 0) {
  338. printk(KERN_ERR "iscsi_tcp: invalid R2T with zero data len\n");
  339. spin_unlock(&session->lock);
  340. return ISCSI_ERR_DATALEN;
  341. }
  342. if (r2t->data_length > session->max_burst)
  343. debug_scsi("invalid R2T with data len %u and max burst %u."
  344. "Attempting to execute request.\n",
  345. r2t->data_length, session->max_burst);
  346. r2t->data_offset = be32_to_cpu(rhdr->data_offset);
  347. if (r2t->data_offset + r2t->data_length > ctask->total_length) {
  348. spin_unlock(&session->lock);
  349. printk(KERN_ERR "iscsi_tcp: invalid R2T with data len %u at "
  350. "offset %u and total length %d\n", r2t->data_length,
  351. r2t->data_offset, ctask->total_length);
  352. return ISCSI_ERR_DATALEN;
  353. }
  354. r2t->ttt = rhdr->ttt; /* no flip */
  355. r2t->solicit_datasn = 0;
  356. iscsi_solicit_data_init(conn, ctask, r2t);
  357. tcp_ctask->exp_r2tsn = r2tsn + 1;
  358. tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
  359. __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
  360. list_move_tail(&ctask->running, &conn->xmitqueue);
  361. scsi_queue_work(session->host, &conn->xmitwork);
  362. conn->r2t_pdus_cnt++;
  363. spin_unlock(&session->lock);
  364. return 0;
  365. }
  366. static int
  367. iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
  368. {
  369. int rc = 0, opcode, ahslen;
  370. struct iscsi_hdr *hdr;
  371. struct iscsi_session *session = conn->session;
  372. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  373. uint32_t cdgst, rdgst = 0, itt;
  374. hdr = tcp_conn->in.hdr;
  375. /* verify PDU length */
  376. tcp_conn->in.datalen = ntoh24(hdr->dlength);
  377. if (tcp_conn->in.datalen > conn->max_recv_dlength) {
  378. printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
  379. tcp_conn->in.datalen, conn->max_recv_dlength);
  380. return ISCSI_ERR_DATALEN;
  381. }
  382. tcp_conn->data_copied = 0;
  383. /* read AHS */
  384. ahslen = hdr->hlength << 2;
  385. tcp_conn->in.offset += ahslen;
  386. tcp_conn->in.copy -= ahslen;
  387. if (tcp_conn->in.copy < 0) {
  388. printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
  389. "%d bytes\n", ahslen);
  390. return ISCSI_ERR_AHSLEN;
  391. }
  392. /* calculate read padding */
  393. tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
  394. if (tcp_conn->in.padding) {
  395. tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
  396. debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
  397. }
  398. if (conn->hdrdgst_en) {
  399. struct scatterlist sg;
  400. sg_init_one(&sg, (u8 *)hdr,
  401. sizeof(struct iscsi_hdr) + ahslen);
  402. crypto_digest_digest(tcp_conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
  403. rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
  404. ahslen);
  405. if (cdgst != rdgst) {
  406. printk(KERN_ERR "iscsi_tcp: hdrdgst error "
  407. "recv 0x%x calc 0x%x\n", rdgst, cdgst);
  408. return ISCSI_ERR_HDR_DGST;
  409. }
  410. }
  411. opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  412. /* verify itt (itt encoding: age+cid+itt) */
  413. rc = iscsi_verify_itt(conn, hdr, &itt);
  414. if (rc == ISCSI_ERR_NO_SCSI_CMD) {
  415. tcp_conn->in.datalen = 0; /* force drop */
  416. return 0;
  417. } else if (rc)
  418. return rc;
  419. debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
  420. opcode, tcp_conn->in.offset, tcp_conn->in.copy,
  421. ahslen, tcp_conn->in.datalen);
  422. switch(opcode) {
  423. case ISCSI_OP_SCSI_DATA_IN:
  424. tcp_conn->in.ctask = session->cmds[itt];
  425. rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
  426. if (rc)
  427. return rc;
  428. /* fall through */
  429. case ISCSI_OP_SCSI_CMD_RSP:
  430. tcp_conn->in.ctask = session->cmds[itt];
  431. if (tcp_conn->in.datalen)
  432. goto copy_hdr;
  433. spin_lock(&session->lock);
  434. iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
  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)
  567. {
  568. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  569. int buf_size = tcp_conn->in.datalen;
  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 iscsi_tcp_conn *tcp_conn,
  589. struct scatterlist *sg, 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_digest_update(tcp_conn->data_rx_tfm, &temp, 1);
  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_digest_update(tcp_conn->data_rx_tfm, &tmp, 1);
  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_digest_update(
  650. tcp_conn->data_rx_tfm,
  651. &sg[i], 1);
  652. else
  653. partial_sg_digest_update(tcp_conn,
  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, &sg[i],
  667. sg[i].offset, sg[i].length-rc);
  668. rc = 0;
  669. break;
  670. }
  671. if (!tcp_conn->in.copy)
  672. return -EAGAIN;
  673. }
  674. BUG_ON(ctask->data_count);
  675. done:
  676. /* check for non-exceptional status */
  677. if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  678. debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
  679. (long)sc, sc->result, ctask->itt,
  680. tcp_conn->in.hdr->flags);
  681. spin_lock(&conn->session->lock);
  682. iscsi_tcp_cleanup_ctask(conn, ctask);
  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. spin_lock(&conn->session->lock);
  700. iscsi_tcp_cleanup_ctask(conn, tcp_conn->in.ctask);
  701. spin_unlock(&conn->session->lock);
  702. case ISCSI_OP_TEXT_RSP:
  703. case ISCSI_OP_LOGIN_RSP:
  704. case ISCSI_OP_ASYNC_EVENT:
  705. case ISCSI_OP_REJECT:
  706. /*
  707. * Collect data segment to the connection's data
  708. * placeholder
  709. */
  710. if (iscsi_tcp_copy(conn)) {
  711. rc = -EAGAIN;
  712. goto exit;
  713. }
  714. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
  715. tcp_conn->in.datalen);
  716. if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
  717. iscsi_recv_digest_update(tcp_conn, conn->data,
  718. tcp_conn->in.datalen);
  719. break;
  720. default:
  721. BUG_ON(1);
  722. }
  723. exit:
  724. return rc;
  725. }
  726. /**
  727. * iscsi_tcp_data_recv - TCP receive in sendfile fashion
  728. * @rd_desc: read descriptor
  729. * @skb: socket buffer
  730. * @offset: offset in skb
  731. * @len: skb->len - offset
  732. **/
  733. static int
  734. iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  735. unsigned int offset, size_t len)
  736. {
  737. int rc;
  738. struct iscsi_conn *conn = rd_desc->arg.data;
  739. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  740. int processed;
  741. char pad[ISCSI_PAD_LEN];
  742. struct scatterlist sg;
  743. /*
  744. * Save current SKB and its offset in the corresponding
  745. * connection context.
  746. */
  747. tcp_conn->in.copy = skb->len - offset;
  748. tcp_conn->in.offset = offset;
  749. tcp_conn->in.skb = skb;
  750. tcp_conn->in.len = tcp_conn->in.copy;
  751. BUG_ON(tcp_conn->in.copy <= 0);
  752. debug_tcp("in %d bytes\n", tcp_conn->in.copy);
  753. more:
  754. tcp_conn->in.copied = 0;
  755. rc = 0;
  756. if (unlikely(conn->suspend_rx)) {
  757. debug_tcp("conn %d Rx suspended!\n", conn->id);
  758. return 0;
  759. }
  760. if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
  761. tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
  762. rc = iscsi_hdr_extract(tcp_conn);
  763. if (rc) {
  764. if (rc == -EAGAIN)
  765. goto nomore;
  766. else {
  767. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  768. return 0;
  769. }
  770. }
  771. /*
  772. * Verify and process incoming PDU header.
  773. */
  774. rc = iscsi_tcp_hdr_recv(conn);
  775. if (!rc && tcp_conn->in.datalen) {
  776. if (conn->datadgst_en) {
  777. BUG_ON(!tcp_conn->data_rx_tfm);
  778. crypto_digest_init(tcp_conn->data_rx_tfm);
  779. }
  780. tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
  781. } else if (rc) {
  782. iscsi_conn_failure(conn, rc);
  783. return 0;
  784. }
  785. }
  786. if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
  787. uint32_t recv_digest;
  788. debug_tcp("extra data_recv offset %d copy %d\n",
  789. tcp_conn->in.offset, tcp_conn->in.copy);
  790. skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
  791. &recv_digest, 4);
  792. tcp_conn->in.offset += 4;
  793. tcp_conn->in.copy -= 4;
  794. if (recv_digest != tcp_conn->in.datadgst) {
  795. debug_tcp("iscsi_tcp: data digest error!"
  796. "0x%x != 0x%x\n", recv_digest,
  797. tcp_conn->in.datadgst);
  798. iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
  799. return 0;
  800. } else {
  801. debug_tcp("iscsi_tcp: data digest match!"
  802. "0x%x == 0x%x\n", recv_digest,
  803. tcp_conn->in.datadgst);
  804. tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
  805. }
  806. }
  807. if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
  808. tcp_conn->in.copy) {
  809. debug_tcp("data_recv offset %d copy %d\n",
  810. tcp_conn->in.offset, tcp_conn->in.copy);
  811. rc = iscsi_data_recv(conn);
  812. if (rc) {
  813. if (rc == -EAGAIN)
  814. goto again;
  815. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  816. return 0;
  817. }
  818. tcp_conn->in.copy -= tcp_conn->in.padding;
  819. tcp_conn->in.offset += tcp_conn->in.padding;
  820. if (conn->datadgst_en) {
  821. if (tcp_conn->in.padding) {
  822. debug_tcp("padding -> %d\n",
  823. tcp_conn->in.padding);
  824. memset(pad, 0, tcp_conn->in.padding);
  825. sg_init_one(&sg, pad, tcp_conn->in.padding);
  826. crypto_digest_update(tcp_conn->data_rx_tfm,
  827. &sg, 1);
  828. }
  829. crypto_digest_final(tcp_conn->data_rx_tfm,
  830. (u8 *) & tcp_conn->in.datadgst);
  831. debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
  832. tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
  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_cmd_task *ctask)
  1038. {
  1039. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1040. BUG_ON(!tcp_conn->data_tx_tfm);
  1041. crypto_digest_init(tcp_conn->data_tx_tfm);
  1042. tcp_ctask->digest_count = 4;
  1043. }
  1044. static int
  1045. iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1046. struct iscsi_buf *buf, uint32_t *digest, int final)
  1047. {
  1048. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1049. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1050. int rc = 0;
  1051. int sent = 0;
  1052. if (final)
  1053. crypto_digest_final(tcp_conn->data_tx_tfm, (u8*)digest);
  1054. iscsi_buf_init_iov(buf, (char*)digest, 4);
  1055. rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
  1056. if (rc) {
  1057. tcp_ctask->datadigest = *digest;
  1058. tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
  1059. } else
  1060. tcp_ctask->digest_count = 4;
  1061. return rc;
  1062. }
  1063. /**
  1064. * iscsi_solicit_data_cont - initialize next Data-Out
  1065. * @conn: iscsi connection
  1066. * @ctask: scsi command task
  1067. * @r2t: R2T info
  1068. * @left: bytes left to transfer
  1069. *
  1070. * Notes:
  1071. * Initialize next Data-Out within this R2T sequence and continue
  1072. * to process next Scatter-Gather element(if any) of this SCSI command.
  1073. *
  1074. * Called under connection lock.
  1075. **/
  1076. static void
  1077. iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1078. struct iscsi_r2t_info *r2t, int left)
  1079. {
  1080. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1081. struct iscsi_data *hdr;
  1082. struct scsi_cmnd *sc = ctask->sc;
  1083. int new_offset;
  1084. hdr = &r2t->dtask.hdr;
  1085. memset(hdr, 0, sizeof(struct iscsi_data));
  1086. hdr->ttt = r2t->ttt;
  1087. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  1088. r2t->solicit_datasn++;
  1089. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  1090. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1091. hdr->itt = ctask->hdr->itt;
  1092. hdr->exp_statsn = r2t->exp_statsn;
  1093. new_offset = r2t->data_offset + r2t->sent;
  1094. hdr->offset = cpu_to_be32(new_offset);
  1095. if (left > conn->max_xmit_dlength) {
  1096. hton24(hdr->dlength, conn->max_xmit_dlength);
  1097. r2t->data_count = conn->max_xmit_dlength;
  1098. } else {
  1099. hton24(hdr->dlength, left);
  1100. r2t->data_count = left;
  1101. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  1102. }
  1103. conn->dataout_pdus_cnt++;
  1104. iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
  1105. sizeof(struct iscsi_hdr));
  1106. if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
  1107. BUG_ON(tcp_ctask->bad_sg == r2t->sg);
  1108. iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
  1109. r2t->sg += 1;
  1110. } else
  1111. iscsi_buf_init_iov(&tcp_ctask->sendbuf,
  1112. (char*)sc->request_buffer + new_offset,
  1113. r2t->data_count);
  1114. }
  1115. /**
  1116. * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
  1117. * @conn: iscsi connection
  1118. * @ctask: scsi command task
  1119. * @sc: scsi command
  1120. **/
  1121. static void
  1122. iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
  1123. {
  1124. struct scsi_cmnd *sc = ctask->sc;
  1125. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1126. BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
  1127. tcp_ctask->sent = 0;
  1128. tcp_ctask->sg_count = 0;
  1129. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  1130. tcp_ctask->xmstate = XMSTATE_W_HDR;
  1131. tcp_ctask->exp_r2tsn = 0;
  1132. BUG_ON(ctask->total_length == 0);
  1133. if (sc->use_sg) {
  1134. struct scatterlist *sg = sc->request_buffer;
  1135. iscsi_buf_init_sg(&tcp_ctask->sendbuf,
  1136. &sg[tcp_ctask->sg_count++]);
  1137. tcp_ctask->sg = sg;
  1138. tcp_ctask->bad_sg = sg + sc->use_sg;
  1139. } else
  1140. iscsi_buf_init_iov(&tcp_ctask->sendbuf,
  1141. sc->request_buffer,
  1142. sc->request_bufflen);
  1143. if (ctask->imm_count)
  1144. tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
  1145. tcp_ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
  1146. if (tcp_ctask->pad_count) {
  1147. tcp_ctask->pad_count = ISCSI_PAD_LEN -
  1148. tcp_ctask->pad_count;
  1149. debug_scsi("write padding %d bytes\n",
  1150. tcp_ctask->pad_count);
  1151. tcp_ctask->xmstate |= XMSTATE_W_PAD;
  1152. }
  1153. if (ctask->unsol_count)
  1154. tcp_ctask->xmstate |= XMSTATE_UNS_HDR |
  1155. XMSTATE_UNS_INIT;
  1156. debug_scsi("cmd [itt 0x%x total %d imm_data %d "
  1157. "unsol count %d, unsol offset %d]\n",
  1158. ctask->itt, ctask->total_length, ctask->imm_count,
  1159. ctask->unsol_count, ctask->unsol_offset);
  1160. } else
  1161. tcp_ctask->xmstate = XMSTATE_R_HDR;
  1162. iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
  1163. sizeof(struct iscsi_hdr));
  1164. }
  1165. /**
  1166. * iscsi_tcp_mtask_xmit - xmit management(immediate) task
  1167. * @conn: iscsi connection
  1168. * @mtask: task management task
  1169. *
  1170. * Notes:
  1171. * The function can return -EAGAIN in which case caller must
  1172. * call it again later, or recover. '0' return code means successful
  1173. * xmit.
  1174. *
  1175. * Management xmit state machine consists of two states:
  1176. * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
  1177. * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
  1178. **/
  1179. static int
  1180. iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
  1181. {
  1182. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1183. int rc;
  1184. debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
  1185. conn->id, tcp_mtask->xmstate, mtask->itt);
  1186. if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
  1187. tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
  1188. if (mtask->data_count)
  1189. tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
  1190. if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
  1191. conn->stop_stage != STOP_CONN_RECOVER &&
  1192. conn->hdrdgst_en)
  1193. iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
  1194. (u8*)tcp_mtask->hdrext);
  1195. rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
  1196. mtask->data_count);
  1197. if (rc) {
  1198. tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
  1199. if (mtask->data_count)
  1200. tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
  1201. return rc;
  1202. }
  1203. }
  1204. if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
  1205. BUG_ON(!mtask->data_count);
  1206. tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
  1207. /* FIXME: implement.
  1208. * Virtual buffer could be spreaded across multiple pages...
  1209. */
  1210. do {
  1211. int rc;
  1212. rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
  1213. &mtask->data_count, &tcp_mtask->sent);
  1214. if (rc) {
  1215. tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
  1216. return rc;
  1217. }
  1218. } while (mtask->data_count);
  1219. }
  1220. BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
  1221. if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
  1222. struct iscsi_session *session = conn->session;
  1223. spin_lock_bh(&session->lock);
  1224. list_del(&conn->mtask->running);
  1225. __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
  1226. sizeof(void*));
  1227. spin_unlock_bh(&session->lock);
  1228. }
  1229. return 0;
  1230. }
  1231. static inline int
  1232. handle_xmstate_r_hdr(struct iscsi_conn *conn,
  1233. struct iscsi_tcp_cmd_task *tcp_ctask)
  1234. {
  1235. int rc;
  1236. tcp_ctask->xmstate &= ~XMSTATE_R_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, 0);
  1241. if (!rc) {
  1242. BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
  1243. return 0; /* wait for Data-In */
  1244. }
  1245. tcp_ctask->xmstate |= XMSTATE_R_HDR;
  1246. return rc;
  1247. }
  1248. static inline int
  1249. handle_xmstate_w_hdr(struct iscsi_conn *conn,
  1250. struct iscsi_cmd_task *ctask)
  1251. {
  1252. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1253. int rc;
  1254. tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
  1255. if (conn->hdrdgst_en)
  1256. iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
  1257. (u8*)tcp_ctask->hdrext);
  1258. rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
  1259. if (rc)
  1260. tcp_ctask->xmstate |= XMSTATE_W_HDR;
  1261. return rc;
  1262. }
  1263. static inline int
  1264. handle_xmstate_data_digest(struct iscsi_conn *conn,
  1265. struct iscsi_cmd_task *ctask)
  1266. {
  1267. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1268. int rc;
  1269. tcp_ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
  1270. debug_tcp("resent data digest 0x%x\n", tcp_ctask->datadigest);
  1271. rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
  1272. &tcp_ctask->datadigest, 0);
  1273. if (rc) {
  1274. tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
  1275. debug_tcp("resent data digest 0x%x fail!\n",
  1276. tcp_ctask->datadigest);
  1277. }
  1278. return rc;
  1279. }
  1280. static inline int
  1281. handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1282. {
  1283. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1284. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1285. int rc;
  1286. BUG_ON(!ctask->imm_count);
  1287. tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
  1288. if (conn->datadgst_en) {
  1289. iscsi_data_digest_init(tcp_conn, ctask);
  1290. tcp_ctask->immdigest = 0;
  1291. }
  1292. for (;;) {
  1293. rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
  1294. &ctask->imm_count, &tcp_ctask->sent);
  1295. if (rc) {
  1296. tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
  1297. if (conn->datadgst_en) {
  1298. crypto_digest_final(tcp_conn->data_tx_tfm,
  1299. (u8*)&tcp_ctask->immdigest);
  1300. debug_tcp("tx imm sendpage fail 0x%x\n",
  1301. tcp_ctask->datadigest);
  1302. }
  1303. return rc;
  1304. }
  1305. if (conn->datadgst_en)
  1306. crypto_digest_update(tcp_conn->data_tx_tfm,
  1307. &tcp_ctask->sendbuf.sg, 1);
  1308. if (!ctask->imm_count)
  1309. break;
  1310. iscsi_buf_init_sg(&tcp_ctask->sendbuf,
  1311. &tcp_ctask->sg[tcp_ctask->sg_count++]);
  1312. }
  1313. if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
  1314. rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
  1315. &tcp_ctask->immdigest, 1);
  1316. if (rc) {
  1317. debug_tcp("sending imm digest 0x%x fail!\n",
  1318. tcp_ctask->immdigest);
  1319. return rc;
  1320. }
  1321. debug_tcp("sending imm digest 0x%x\n", tcp_ctask->immdigest);
  1322. }
  1323. return 0;
  1324. }
  1325. static inline int
  1326. handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1327. {
  1328. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1329. struct iscsi_data_task *dtask;
  1330. int rc;
  1331. tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
  1332. if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
  1333. dtask = tcp_ctask->dtask = &tcp_ctask->unsol_dtask;
  1334. iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
  1335. iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
  1336. sizeof(struct iscsi_hdr));
  1337. if (conn->hdrdgst_en)
  1338. iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
  1339. (u8*)dtask->hdrext);
  1340. tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
  1341. }
  1342. rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
  1343. if (rc) {
  1344. tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
  1345. tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
  1346. return rc;
  1347. }
  1348. debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
  1349. ctask->itt, ctask->unsol_count, tcp_ctask->sent);
  1350. return 0;
  1351. }
  1352. static inline int
  1353. handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1354. {
  1355. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1356. struct iscsi_data_task *dtask = tcp_ctask->dtask;
  1357. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1358. int rc;
  1359. BUG_ON(!ctask->data_count);
  1360. tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
  1361. if (conn->datadgst_en) {
  1362. iscsi_data_digest_init(tcp_conn, ctask);
  1363. dtask->digest = 0;
  1364. }
  1365. for (;;) {
  1366. int start = tcp_ctask->sent;
  1367. rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
  1368. &ctask->data_count, &tcp_ctask->sent);
  1369. if (rc) {
  1370. ctask->unsol_count -= tcp_ctask->sent - start;
  1371. tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
  1372. /* will continue with this ctask later.. */
  1373. if (conn->datadgst_en) {
  1374. crypto_digest_final(tcp_conn->data_tx_tfm,
  1375. (u8 *)&dtask->digest);
  1376. debug_tcp("tx uns data fail 0x%x\n",
  1377. dtask->digest);
  1378. }
  1379. return rc;
  1380. }
  1381. BUG_ON(tcp_ctask->sent > ctask->total_length);
  1382. ctask->unsol_count -= tcp_ctask->sent - start;
  1383. /*
  1384. * XXX:we may run here with un-initial sendbuf.
  1385. * so pass it
  1386. */
  1387. if (conn->datadgst_en && tcp_ctask->sent - start > 0)
  1388. crypto_digest_update(tcp_conn->data_tx_tfm,
  1389. &tcp_ctask->sendbuf.sg, 1);
  1390. if (!ctask->data_count)
  1391. break;
  1392. iscsi_buf_init_sg(&tcp_ctask->sendbuf,
  1393. &tcp_ctask->sg[tcp_ctask->sg_count++]);
  1394. }
  1395. BUG_ON(ctask->unsol_count < 0);
  1396. /*
  1397. * Done with the Data-Out. Next, check if we need
  1398. * to send another unsolicited Data-Out.
  1399. */
  1400. if (ctask->unsol_count) {
  1401. if (conn->datadgst_en) {
  1402. rc = iscsi_digest_final_send(conn, ctask,
  1403. &dtask->digestbuf,
  1404. &dtask->digest, 1);
  1405. if (rc) {
  1406. debug_tcp("send uns digest 0x%x fail\n",
  1407. dtask->digest);
  1408. return rc;
  1409. }
  1410. debug_tcp("sending uns digest 0x%x, more uns\n",
  1411. dtask->digest);
  1412. }
  1413. tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
  1414. return 1;
  1415. }
  1416. if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
  1417. rc = iscsi_digest_final_send(conn, ctask,
  1418. &dtask->digestbuf,
  1419. &dtask->digest, 1);
  1420. if (rc) {
  1421. debug_tcp("send last uns digest 0x%x fail\n",
  1422. dtask->digest);
  1423. return rc;
  1424. }
  1425. debug_tcp("sending uns digest 0x%x\n",dtask->digest);
  1426. }
  1427. return 0;
  1428. }
  1429. static inline int
  1430. handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1431. {
  1432. struct iscsi_session *session = conn->session;
  1433. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1434. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1435. struct iscsi_r2t_info *r2t = tcp_ctask->r2t;
  1436. struct iscsi_data_task *dtask = &r2t->dtask;
  1437. int left, rc;
  1438. tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
  1439. tcp_ctask->dtask = dtask;
  1440. if (conn->datadgst_en) {
  1441. iscsi_data_digest_init(tcp_conn, ctask);
  1442. dtask->digest = 0;
  1443. }
  1444. solicit_again:
  1445. /*
  1446. * send Data-Out within this R2T sequence.
  1447. */
  1448. if (!r2t->data_count)
  1449. goto data_out_done;
  1450. rc = iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent);
  1451. if (rc) {
  1452. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1453. /* will continue with this ctask later.. */
  1454. if (conn->datadgst_en) {
  1455. crypto_digest_final(tcp_conn->data_tx_tfm,
  1456. (u8 *)&dtask->digest);
  1457. debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
  1458. }
  1459. return rc;
  1460. }
  1461. BUG_ON(r2t->data_count < 0);
  1462. if (conn->datadgst_en)
  1463. crypto_digest_update(tcp_conn->data_tx_tfm, &r2t->sendbuf.sg,
  1464. 1);
  1465. if (r2t->data_count) {
  1466. BUG_ON(ctask->sc->use_sg == 0);
  1467. if (!iscsi_buf_left(&r2t->sendbuf)) {
  1468. BUG_ON(tcp_ctask->bad_sg == r2t->sg);
  1469. iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
  1470. r2t->sg += 1;
  1471. }
  1472. goto solicit_again;
  1473. }
  1474. data_out_done:
  1475. /*
  1476. * Done with this Data-Out. Next, check if we have
  1477. * to send another Data-Out for this R2T.
  1478. */
  1479. BUG_ON(r2t->data_length - r2t->sent < 0);
  1480. left = r2t->data_length - r2t->sent;
  1481. if (left) {
  1482. if (conn->datadgst_en) {
  1483. rc = iscsi_digest_final_send(conn, ctask,
  1484. &dtask->digestbuf,
  1485. &dtask->digest, 1);
  1486. if (rc) {
  1487. debug_tcp("send r2t data digest 0x%x"
  1488. "fail\n", dtask->digest);
  1489. return rc;
  1490. }
  1491. debug_tcp("r2t data send digest 0x%x\n",
  1492. dtask->digest);
  1493. }
  1494. iscsi_solicit_data_cont(conn, ctask, r2t, left);
  1495. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1496. tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
  1497. return 1;
  1498. }
  1499. /*
  1500. * Done with this R2T. Check if there are more
  1501. * outstanding R2Ts ready to be processed.
  1502. */
  1503. if (conn->datadgst_en) {
  1504. rc = iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
  1505. &dtask->digest, 1);
  1506. if (rc) {
  1507. debug_tcp("send last r2t data digest 0x%x"
  1508. "fail\n", dtask->digest);
  1509. return rc;
  1510. }
  1511. debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
  1512. }
  1513. tcp_ctask->r2t = NULL;
  1514. spin_lock_bh(&session->lock);
  1515. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
  1516. spin_unlock_bh(&session->lock);
  1517. if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
  1518. tcp_ctask->r2t = r2t;
  1519. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1520. tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
  1521. return 1;
  1522. }
  1523. return 0;
  1524. }
  1525. static inline int
  1526. handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1527. {
  1528. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1529. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1530. struct iscsi_data_task *dtask = tcp_ctask->dtask;
  1531. int sent = 0, rc;
  1532. tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
  1533. iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
  1534. tcp_ctask->pad_count);
  1535. rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
  1536. &sent);
  1537. if (rc) {
  1538. tcp_ctask->xmstate |= XMSTATE_W_PAD;
  1539. return rc;
  1540. }
  1541. if (conn->datadgst_en) {
  1542. crypto_digest_update(tcp_conn->data_tx_tfm,
  1543. &tcp_ctask->sendbuf.sg, 1);
  1544. /* imm data? */
  1545. if (!dtask) {
  1546. rc = iscsi_digest_final_send(conn, ctask,
  1547. &tcp_ctask->immbuf,
  1548. &tcp_ctask->immdigest, 1);
  1549. if (rc) {
  1550. debug_tcp("send padding digest 0x%x"
  1551. "fail!\n", tcp_ctask->immdigest);
  1552. return rc;
  1553. }
  1554. debug_tcp("done with padding, digest 0x%x\n",
  1555. tcp_ctask->datadigest);
  1556. } else {
  1557. rc = iscsi_digest_final_send(conn, ctask,
  1558. &dtask->digestbuf,
  1559. &dtask->digest, 1);
  1560. if (rc) {
  1561. debug_tcp("send padding digest 0x%x"
  1562. "fail\n", dtask->digest);
  1563. return rc;
  1564. }
  1565. debug_tcp("done with padding, digest 0x%x\n",
  1566. dtask->digest);
  1567. }
  1568. }
  1569. return 0;
  1570. }
  1571. static int
  1572. iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1573. {
  1574. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1575. int rc = 0;
  1576. debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
  1577. conn->id, tcp_ctask->xmstate, ctask->itt);
  1578. /*
  1579. * serialize with TMF AbortTask
  1580. */
  1581. if (ctask->mtask)
  1582. return rc;
  1583. if (tcp_ctask->xmstate & XMSTATE_R_HDR)
  1584. return handle_xmstate_r_hdr(conn, tcp_ctask);
  1585. if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
  1586. rc = handle_xmstate_w_hdr(conn, ctask);
  1587. if (rc)
  1588. return rc;
  1589. }
  1590. /* XXX: for data digest xmit recover */
  1591. if (tcp_ctask->xmstate & XMSTATE_DATA_DIGEST) {
  1592. rc = handle_xmstate_data_digest(conn, ctask);
  1593. if (rc)
  1594. return rc;
  1595. }
  1596. if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
  1597. rc = handle_xmstate_imm_data(conn, ctask);
  1598. if (rc)
  1599. return rc;
  1600. }
  1601. if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
  1602. BUG_ON(!ctask->unsol_count);
  1603. tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
  1604. unsolicit_head_again:
  1605. rc = handle_xmstate_uns_hdr(conn, ctask);
  1606. if (rc)
  1607. return rc;
  1608. }
  1609. if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
  1610. rc = handle_xmstate_uns_data(conn, ctask);
  1611. if (rc == 1)
  1612. goto unsolicit_head_again;
  1613. else if (rc)
  1614. return rc;
  1615. goto done;
  1616. }
  1617. if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
  1618. struct iscsi_r2t_info *r2t;
  1619. tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
  1620. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1621. if (!tcp_ctask->r2t)
  1622. __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
  1623. sizeof(void*));
  1624. solicit_head_again:
  1625. r2t = tcp_ctask->r2t;
  1626. if (conn->hdrdgst_en)
  1627. iscsi_hdr_digest(conn, &r2t->headbuf,
  1628. (u8*)r2t->dtask.hdrext);
  1629. rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
  1630. if (rc) {
  1631. tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
  1632. tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
  1633. return rc;
  1634. }
  1635. debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
  1636. r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
  1637. r2t->sent);
  1638. }
  1639. if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
  1640. rc = handle_xmstate_sol_data(conn, ctask);
  1641. if (rc == 1)
  1642. goto solicit_head_again;
  1643. if (rc)
  1644. return rc;
  1645. }
  1646. done:
  1647. /*
  1648. * Last thing to check is whether we need to send write
  1649. * padding. Note that we check for xmstate equality, not just the bit.
  1650. */
  1651. if (tcp_ctask->xmstate == XMSTATE_W_PAD)
  1652. rc = handle_xmstate_w_pad(conn, ctask);
  1653. return rc;
  1654. }
  1655. static struct iscsi_cls_conn *
  1656. iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1657. {
  1658. struct iscsi_conn *conn;
  1659. struct iscsi_cls_conn *cls_conn;
  1660. struct iscsi_tcp_conn *tcp_conn;
  1661. cls_conn = iscsi_conn_setup(cls_session, conn_idx);
  1662. if (!cls_conn)
  1663. return NULL;
  1664. conn = cls_conn->dd_data;
  1665. /*
  1666. * due to strange issues with iser these are not set
  1667. * in iscsi_conn_setup
  1668. */
  1669. conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
  1670. tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
  1671. if (!tcp_conn)
  1672. goto tcp_conn_alloc_fail;
  1673. conn->dd_data = tcp_conn;
  1674. tcp_conn->iscsi_conn = conn;
  1675. tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
  1676. /* initial operational parameters */
  1677. tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
  1678. return cls_conn;
  1679. tcp_conn_alloc_fail:
  1680. iscsi_conn_teardown(cls_conn);
  1681. return NULL;
  1682. }
  1683. static void
  1684. iscsi_tcp_release_conn(struct iscsi_conn *conn)
  1685. {
  1686. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1687. if (!tcp_conn->sock)
  1688. return;
  1689. sock_hold(tcp_conn->sock->sk);
  1690. iscsi_conn_restore_callbacks(tcp_conn);
  1691. sock_put(tcp_conn->sock->sk);
  1692. sock_release(tcp_conn->sock);
  1693. tcp_conn->sock = NULL;
  1694. conn->recv_lock = NULL;
  1695. }
  1696. static void
  1697. iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1698. {
  1699. struct iscsi_conn *conn = cls_conn->dd_data;
  1700. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1701. int digest = 0;
  1702. if (conn->hdrdgst_en || conn->datadgst_en)
  1703. digest = 1;
  1704. iscsi_tcp_release_conn(conn);
  1705. iscsi_conn_teardown(cls_conn);
  1706. /* now free tcp_conn */
  1707. if (digest) {
  1708. if (tcp_conn->tx_tfm)
  1709. crypto_free_tfm(tcp_conn->tx_tfm);
  1710. if (tcp_conn->rx_tfm)
  1711. crypto_free_tfm(tcp_conn->rx_tfm);
  1712. if (tcp_conn->data_tx_tfm)
  1713. crypto_free_tfm(tcp_conn->data_tx_tfm);
  1714. if (tcp_conn->data_rx_tfm)
  1715. crypto_free_tfm(tcp_conn->data_rx_tfm);
  1716. }
  1717. kfree(tcp_conn);
  1718. }
  1719. static void
  1720. iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1721. {
  1722. struct iscsi_conn *conn = cls_conn->dd_data;
  1723. iscsi_conn_stop(cls_conn, flag);
  1724. iscsi_tcp_release_conn(conn);
  1725. }
  1726. static int
  1727. iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  1728. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  1729. int is_leading)
  1730. {
  1731. struct iscsi_conn *conn = cls_conn->dd_data;
  1732. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1733. struct sock *sk;
  1734. struct socket *sock;
  1735. int err;
  1736. /* lookup for existing socket */
  1737. sock = sockfd_lookup((int)transport_eph, &err);
  1738. if (!sock) {
  1739. printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
  1740. return -EEXIST;
  1741. }
  1742. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  1743. if (err)
  1744. return err;
  1745. /* bind iSCSI connection and socket */
  1746. tcp_conn->sock = sock;
  1747. /* setup Socket parameters */
  1748. sk = sock->sk;
  1749. sk->sk_reuse = 1;
  1750. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  1751. sk->sk_allocation = GFP_ATOMIC;
  1752. /* FIXME: disable Nagle's algorithm */
  1753. /*
  1754. * Intercept TCP callbacks for sendfile like receive
  1755. * processing.
  1756. */
  1757. conn->recv_lock = &sk->sk_callback_lock;
  1758. iscsi_conn_set_callbacks(conn);
  1759. tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
  1760. /*
  1761. * set receive state machine into initial state
  1762. */
  1763. tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
  1764. return 0;
  1765. }
  1766. /* called with host lock */
  1767. static void
  1768. iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
  1769. char *data, uint32_t data_size)
  1770. {
  1771. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1772. iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
  1773. sizeof(struct iscsi_hdr));
  1774. tcp_mtask->xmstate = XMSTATE_IMM_HDR;
  1775. tcp_mtask->sent = 0;
  1776. if (mtask->data_count)
  1777. iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
  1778. mtask->data_count);
  1779. }
  1780. static int
  1781. iscsi_r2tpool_alloc(struct iscsi_session *session)
  1782. {
  1783. int i;
  1784. int cmd_i;
  1785. /*
  1786. * initialize per-task: R2T pool and xmit queue
  1787. */
  1788. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1789. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1790. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1791. /*
  1792. * pre-allocated x4 as much r2ts to handle race when
  1793. * target acks DataOut faster than we data_xmit() queues
  1794. * could replenish r2tqueue.
  1795. */
  1796. /* R2T pool */
  1797. if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
  1798. (void***)&tcp_ctask->r2ts,
  1799. sizeof(struct iscsi_r2t_info))) {
  1800. goto r2t_alloc_fail;
  1801. }
  1802. /* R2T xmit queue */
  1803. tcp_ctask->r2tqueue = kfifo_alloc(
  1804. session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
  1805. if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
  1806. iscsi_pool_free(&tcp_ctask->r2tpool,
  1807. (void**)tcp_ctask->r2ts);
  1808. goto r2t_alloc_fail;
  1809. }
  1810. }
  1811. return 0;
  1812. r2t_alloc_fail:
  1813. for (i = 0; i < cmd_i; i++) {
  1814. struct iscsi_cmd_task *ctask = session->cmds[i];
  1815. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1816. kfifo_free(tcp_ctask->r2tqueue);
  1817. iscsi_pool_free(&tcp_ctask->r2tpool,
  1818. (void**)tcp_ctask->r2ts);
  1819. }
  1820. return -ENOMEM;
  1821. }
  1822. static void
  1823. iscsi_r2tpool_free(struct iscsi_session *session)
  1824. {
  1825. int i;
  1826. for (i = 0; i < session->cmds_max; i++) {
  1827. struct iscsi_cmd_task *ctask = session->cmds[i];
  1828. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1829. kfifo_free(tcp_ctask->r2tqueue);
  1830. iscsi_pool_free(&tcp_ctask->r2tpool,
  1831. (void**)tcp_ctask->r2ts);
  1832. }
  1833. }
  1834. static int
  1835. iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
  1836. char *buf, int buflen)
  1837. {
  1838. struct iscsi_conn *conn = cls_conn->dd_data;
  1839. struct iscsi_session *session = conn->session;
  1840. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1841. int value;
  1842. switch(param) {
  1843. case ISCSI_PARAM_HDRDGST_EN:
  1844. iscsi_set_param(cls_conn, param, buf, buflen);
  1845. tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
  1846. if (conn->hdrdgst_en) {
  1847. tcp_conn->hdr_size += sizeof(__u32);
  1848. if (!tcp_conn->tx_tfm)
  1849. tcp_conn->tx_tfm = crypto_alloc_tfm("crc32c",
  1850. 0);
  1851. if (!tcp_conn->tx_tfm)
  1852. return -ENOMEM;
  1853. if (!tcp_conn->rx_tfm)
  1854. tcp_conn->rx_tfm = crypto_alloc_tfm("crc32c",
  1855. 0);
  1856. if (!tcp_conn->rx_tfm) {
  1857. crypto_free_tfm(tcp_conn->tx_tfm);
  1858. return -ENOMEM;
  1859. }
  1860. } else {
  1861. if (tcp_conn->tx_tfm)
  1862. crypto_free_tfm(tcp_conn->tx_tfm);
  1863. if (tcp_conn->rx_tfm)
  1864. crypto_free_tfm(tcp_conn->rx_tfm);
  1865. }
  1866. break;
  1867. case ISCSI_PARAM_DATADGST_EN:
  1868. iscsi_set_param(cls_conn, param, buf, buflen);
  1869. if (conn->datadgst_en) {
  1870. if (!tcp_conn->data_tx_tfm)
  1871. tcp_conn->data_tx_tfm =
  1872. crypto_alloc_tfm("crc32c", 0);
  1873. if (!tcp_conn->data_tx_tfm)
  1874. return -ENOMEM;
  1875. if (!tcp_conn->data_rx_tfm)
  1876. tcp_conn->data_rx_tfm =
  1877. crypto_alloc_tfm("crc32c", 0);
  1878. if (!tcp_conn->data_rx_tfm) {
  1879. crypto_free_tfm(tcp_conn->data_tx_tfm);
  1880. return -ENOMEM;
  1881. }
  1882. } else {
  1883. if (tcp_conn->data_tx_tfm)
  1884. crypto_free_tfm(tcp_conn->data_tx_tfm);
  1885. if (tcp_conn->data_rx_tfm)
  1886. crypto_free_tfm(tcp_conn->data_rx_tfm);
  1887. }
  1888. tcp_conn->sendpage = conn->datadgst_en ?
  1889. sock_no_sendpage : tcp_conn->sock->ops->sendpage;
  1890. break;
  1891. case ISCSI_PARAM_MAX_R2T:
  1892. sscanf(buf, "%d", &value);
  1893. if (session->max_r2t == roundup_pow_of_two(value))
  1894. break;
  1895. iscsi_r2tpool_free(session);
  1896. iscsi_set_param(cls_conn, param, buf, buflen);
  1897. if (session->max_r2t & (session->max_r2t - 1))
  1898. session->max_r2t = roundup_pow_of_two(session->max_r2t);
  1899. if (iscsi_r2tpool_alloc(session))
  1900. return -ENOMEM;
  1901. break;
  1902. default:
  1903. return iscsi_set_param(cls_conn, param, buf, buflen);
  1904. }
  1905. return 0;
  1906. }
  1907. static int
  1908. iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1909. enum iscsi_param param, char *buf)
  1910. {
  1911. struct iscsi_conn *conn = cls_conn->dd_data;
  1912. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1913. struct inet_sock *inet;
  1914. struct ipv6_pinfo *np;
  1915. struct sock *sk;
  1916. int len;
  1917. switch(param) {
  1918. case ISCSI_PARAM_CONN_PORT:
  1919. mutex_lock(&conn->xmitmutex);
  1920. if (!tcp_conn->sock) {
  1921. mutex_unlock(&conn->xmitmutex);
  1922. return -EINVAL;
  1923. }
  1924. inet = inet_sk(tcp_conn->sock->sk);
  1925. len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
  1926. mutex_unlock(&conn->xmitmutex);
  1927. break;
  1928. case ISCSI_PARAM_CONN_ADDRESS:
  1929. mutex_lock(&conn->xmitmutex);
  1930. if (!tcp_conn->sock) {
  1931. mutex_unlock(&conn->xmitmutex);
  1932. return -EINVAL;
  1933. }
  1934. sk = tcp_conn->sock->sk;
  1935. if (sk->sk_family == PF_INET) {
  1936. inet = inet_sk(sk);
  1937. len = sprintf(buf, "%u.%u.%u.%u\n",
  1938. NIPQUAD(inet->daddr));
  1939. } else {
  1940. np = inet6_sk(sk);
  1941. len = sprintf(buf,
  1942. "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  1943. NIP6(np->daddr));
  1944. }
  1945. mutex_unlock(&conn->xmitmutex);
  1946. break;
  1947. default:
  1948. return iscsi_conn_get_param(cls_conn, param, buf);
  1949. }
  1950. return len;
  1951. }
  1952. static void
  1953. iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
  1954. {
  1955. struct iscsi_conn *conn = cls_conn->dd_data;
  1956. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1957. stats->txdata_octets = conn->txdata_octets;
  1958. stats->rxdata_octets = conn->rxdata_octets;
  1959. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1960. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1961. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1962. stats->datain_pdus = conn->datain_pdus_cnt;
  1963. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1964. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1965. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1966. stats->custom_length = 3;
  1967. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  1968. stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
  1969. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  1970. stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
  1971. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  1972. stats->custom[2].value = conn->eh_abort_cnt;
  1973. }
  1974. static struct iscsi_cls_session *
  1975. iscsi_tcp_session_create(struct iscsi_transport *iscsit,
  1976. struct scsi_transport_template *scsit,
  1977. uint32_t initial_cmdsn, uint32_t *hostno)
  1978. {
  1979. struct iscsi_cls_session *cls_session;
  1980. struct iscsi_session *session;
  1981. uint32_t hn;
  1982. int cmd_i;
  1983. cls_session = iscsi_session_setup(iscsit, scsit,
  1984. sizeof(struct iscsi_tcp_cmd_task),
  1985. sizeof(struct iscsi_tcp_mgmt_task),
  1986. initial_cmdsn, &hn);
  1987. if (!cls_session)
  1988. return NULL;
  1989. *hostno = hn;
  1990. session = class_to_transport_session(cls_session);
  1991. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1992. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1993. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1994. ctask->hdr = &tcp_ctask->hdr;
  1995. }
  1996. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1997. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1998. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1999. mtask->hdr = &tcp_mtask->hdr;
  2000. }
  2001. if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
  2002. goto r2tpool_alloc_fail;
  2003. return cls_session;
  2004. r2tpool_alloc_fail:
  2005. iscsi_session_teardown(cls_session);
  2006. return NULL;
  2007. }
  2008. static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  2009. {
  2010. iscsi_r2tpool_free(class_to_transport_session(cls_session));
  2011. iscsi_session_teardown(cls_session);
  2012. }
  2013. static struct scsi_host_template iscsi_sht = {
  2014. .name = "iSCSI Initiator over TCP/IP",
  2015. .queuecommand = iscsi_queuecommand,
  2016. .change_queue_depth = iscsi_change_queue_depth,
  2017. .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
  2018. .sg_tablesize = ISCSI_SG_TABLESIZE,
  2019. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  2020. .eh_abort_handler = iscsi_eh_abort,
  2021. .eh_host_reset_handler = iscsi_eh_host_reset,
  2022. .use_clustering = DISABLE_CLUSTERING,
  2023. .proc_name = "iscsi_tcp",
  2024. .this_id = -1,
  2025. };
  2026. static struct iscsi_transport iscsi_tcp_transport = {
  2027. .owner = THIS_MODULE,
  2028. .name = "tcp",
  2029. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  2030. | CAP_DATADGST,
  2031. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  2032. ISCSI_MAX_XMIT_DLENGTH |
  2033. ISCSI_HDRDGST_EN |
  2034. ISCSI_DATADGST_EN |
  2035. ISCSI_INITIAL_R2T_EN |
  2036. ISCSI_MAX_R2T |
  2037. ISCSI_IMM_DATA_EN |
  2038. ISCSI_FIRST_BURST |
  2039. ISCSI_MAX_BURST |
  2040. ISCSI_PDU_INORDER_EN |
  2041. ISCSI_DATASEQ_INORDER_EN |
  2042. ISCSI_ERL |
  2043. ISCSI_CONN_PORT |
  2044. ISCSI_CONN_ADDRESS |
  2045. ISCSI_EXP_STATSN |
  2046. ISCSI_PERSISTENT_PORT |
  2047. ISCSI_PERSISTENT_ADDRESS |
  2048. ISCSI_TARGET_NAME |
  2049. ISCSI_TPGT,
  2050. .host_template = &iscsi_sht,
  2051. .conndata_size = sizeof(struct iscsi_conn),
  2052. .max_conn = 1,
  2053. .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
  2054. /* session management */
  2055. .create_session = iscsi_tcp_session_create,
  2056. .destroy_session = iscsi_tcp_session_destroy,
  2057. /* connection management */
  2058. .create_conn = iscsi_tcp_conn_create,
  2059. .bind_conn = iscsi_tcp_conn_bind,
  2060. .destroy_conn = iscsi_tcp_conn_destroy,
  2061. .set_param = iscsi_conn_set_param,
  2062. .get_conn_param = iscsi_tcp_conn_get_param,
  2063. .get_session_param = iscsi_session_get_param,
  2064. .start_conn = iscsi_conn_start,
  2065. .stop_conn = iscsi_tcp_conn_stop,
  2066. /* IO */
  2067. .send_pdu = iscsi_conn_send_pdu,
  2068. .get_stats = iscsi_conn_get_stats,
  2069. .init_cmd_task = iscsi_tcp_cmd_init,
  2070. .init_mgmt_task = iscsi_tcp_mgmt_init,
  2071. .xmit_cmd_task = iscsi_tcp_ctask_xmit,
  2072. .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
  2073. .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
  2074. /* recovery */
  2075. .session_recovery_timedout = iscsi_session_recovery_timedout,
  2076. };
  2077. static int __init
  2078. iscsi_tcp_init(void)
  2079. {
  2080. if (iscsi_max_lun < 1) {
  2081. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  2082. iscsi_max_lun);
  2083. return -EINVAL;
  2084. }
  2085. iscsi_tcp_transport.max_lun = iscsi_max_lun;
  2086. if (!iscsi_register_transport(&iscsi_tcp_transport))
  2087. return -ENODEV;
  2088. return 0;
  2089. }
  2090. static void __exit
  2091. iscsi_tcp_exit(void)
  2092. {
  2093. iscsi_unregister_transport(&iscsi_tcp_transport);
  2094. }
  2095. module_init(iscsi_tcp_init);
  2096. module_exit(iscsi_tcp_exit);