iscsi_tcp.c 58 KB

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