iscsi_tcp.c 61 KB

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