iscsi_tcp.c 63 KB

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