iscsi_tcp.c 60 KB

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