iscsi_tcp.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  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_hash_digest(&tcp_conn->tx_hash, &buf->sg, buf->sg.length, 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_hash_digest(&tcp_conn->rx_hash, &sg, sg.length,
  404. (u8 *)&cdgst);
  405. rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
  406. ahslen);
  407. if (cdgst != rdgst) {
  408. printk(KERN_ERR "iscsi_tcp: hdrdgst error "
  409. "recv 0x%x calc 0x%x\n", rdgst, cdgst);
  410. return ISCSI_ERR_HDR_DGST;
  411. }
  412. }
  413. opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  414. /* verify itt (itt encoding: age+cid+itt) */
  415. rc = iscsi_verify_itt(conn, hdr, &itt);
  416. if (rc == ISCSI_ERR_NO_SCSI_CMD) {
  417. tcp_conn->in.datalen = 0; /* force drop */
  418. return 0;
  419. } else if (rc)
  420. return rc;
  421. debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
  422. opcode, tcp_conn->in.offset, tcp_conn->in.copy,
  423. ahslen, tcp_conn->in.datalen);
  424. switch(opcode) {
  425. case ISCSI_OP_SCSI_DATA_IN:
  426. tcp_conn->in.ctask = session->cmds[itt];
  427. rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
  428. if (rc)
  429. return rc;
  430. /* fall through */
  431. case ISCSI_OP_SCSI_CMD_RSP:
  432. tcp_conn->in.ctask = session->cmds[itt];
  433. if (tcp_conn->in.datalen)
  434. goto copy_hdr;
  435. spin_lock(&session->lock);
  436. rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
  437. spin_unlock(&session->lock);
  438. break;
  439. case ISCSI_OP_R2T:
  440. tcp_conn->in.ctask = session->cmds[itt];
  441. if (ahslen)
  442. rc = ISCSI_ERR_AHSLEN;
  443. else if (tcp_conn->in.ctask->sc->sc_data_direction ==
  444. DMA_TO_DEVICE)
  445. rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
  446. else
  447. rc = ISCSI_ERR_PROTO;
  448. break;
  449. case ISCSI_OP_LOGIN_RSP:
  450. case ISCSI_OP_TEXT_RSP:
  451. case ISCSI_OP_REJECT:
  452. case ISCSI_OP_ASYNC_EVENT:
  453. /*
  454. * It is possible that we could get a PDU with a buffer larger
  455. * than 8K, but there are no targets that currently do this.
  456. * For now we fail until we find a vendor that needs it
  457. */
  458. if (DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH <
  459. tcp_conn->in.datalen) {
  460. printk(KERN_ERR "iscsi_tcp: received buffer of len %u "
  461. "but conn buffer is only %u (opcode %0x)\n",
  462. tcp_conn->in.datalen,
  463. DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, opcode);
  464. rc = ISCSI_ERR_PROTO;
  465. break;
  466. }
  467. if (tcp_conn->in.datalen)
  468. goto copy_hdr;
  469. /* fall through */
  470. case ISCSI_OP_LOGOUT_RSP:
  471. case ISCSI_OP_NOOP_IN:
  472. case ISCSI_OP_SCSI_TMFUNC_RSP:
  473. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  474. break;
  475. default:
  476. rc = ISCSI_ERR_BAD_OPCODE;
  477. break;
  478. }
  479. return rc;
  480. copy_hdr:
  481. /*
  482. * if we did zero copy for the header but we will need multiple
  483. * skbs to complete the command then we have to copy the header
  484. * for later use
  485. */
  486. if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <=
  487. (tcp_conn->in.datalen + tcp_conn->in.padding +
  488. (conn->datadgst_en ? 4 : 0))) {
  489. debug_tcp("Copying header for later use. in.copy %d in.datalen"
  490. " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
  491. memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
  492. sizeof(struct iscsi_hdr));
  493. tcp_conn->in.hdr = &tcp_conn->hdr;
  494. tcp_conn->in.zero_copy_hdr = 0;
  495. }
  496. return 0;
  497. }
  498. /**
  499. * iscsi_ctask_copy - copy skb bits to the destanation cmd task
  500. * @conn: iscsi tcp connection
  501. * @ctask: scsi command task
  502. * @buf: buffer to copy to
  503. * @buf_size: size of buffer
  504. * @offset: offset within the buffer
  505. *
  506. * Notes:
  507. * The function calls skb_copy_bits() and updates per-connection and
  508. * per-cmd byte counters.
  509. *
  510. * Read counters (in bytes):
  511. *
  512. * conn->in.offset offset within in progress SKB
  513. * conn->in.copy left to copy from in progress SKB
  514. * including padding
  515. * conn->in.copied copied already from in progress SKB
  516. * conn->data_copied copied already from in progress buffer
  517. * ctask->sent total bytes sent up to the MidLayer
  518. * ctask->data_count left to copy from in progress Data-In
  519. * buf_left left to copy from in progress buffer
  520. **/
  521. static inline int
  522. iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
  523. void *buf, int buf_size, int offset)
  524. {
  525. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  526. int buf_left = buf_size - (tcp_conn->data_copied + offset);
  527. int size = min(tcp_conn->in.copy, buf_left);
  528. int rc;
  529. size = min(size, ctask->data_count);
  530. debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
  531. size, tcp_conn->in.offset, tcp_conn->in.copied);
  532. BUG_ON(size <= 0);
  533. BUG_ON(tcp_ctask->sent + size > ctask->total_length);
  534. rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
  535. (char*)buf + (offset + tcp_conn->data_copied), size);
  536. /* must fit into skb->len */
  537. BUG_ON(rc);
  538. tcp_conn->in.offset += size;
  539. tcp_conn->in.copy -= size;
  540. tcp_conn->in.copied += size;
  541. tcp_conn->data_copied += size;
  542. tcp_ctask->sent += size;
  543. ctask->data_count -= size;
  544. BUG_ON(tcp_conn->in.copy < 0);
  545. BUG_ON(ctask->data_count < 0);
  546. if (buf_size != (tcp_conn->data_copied + offset)) {
  547. if (!ctask->data_count) {
  548. BUG_ON(buf_size - tcp_conn->data_copied < 0);
  549. /* done with this PDU */
  550. return buf_size - tcp_conn->data_copied;
  551. }
  552. return -EAGAIN;
  553. }
  554. /* done with this buffer or with both - PDU and buffer */
  555. tcp_conn->data_copied = 0;
  556. return 0;
  557. }
  558. /**
  559. * iscsi_tcp_copy - copy skb bits to the destanation buffer
  560. * @conn: iscsi tcp connection
  561. *
  562. * Notes:
  563. * The function calls skb_copy_bits() and updates per-connection
  564. * byte counters.
  565. **/
  566. static inline int
  567. iscsi_tcp_copy(struct iscsi_conn *conn, int buf_size)
  568. {
  569. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  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 hash_desc *desc, 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_hash_update(desc, &temp, length);
  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_hash_update(&tcp_conn->rx_hash, &tmp, len);
  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_hash_update(
  650. &tcp_conn->rx_hash,
  651. &sg[i], 1);
  652. else
  653. partial_sg_digest_update(
  654. &tcp_conn->rx_hash,
  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_hash,
  668. &sg[i],
  669. sg[i].offset,
  670. sg[i].length-rc);
  671. rc = 0;
  672. break;
  673. }
  674. if (!tcp_conn->in.copy)
  675. return -EAGAIN;
  676. }
  677. BUG_ON(ctask->data_count);
  678. done:
  679. /* check for non-exceptional status */
  680. if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  681. debug_scsi("done [sc %lx res %d itt 0x%x flags 0x%x]\n",
  682. (long)sc, sc->result, ctask->itt,
  683. tcp_conn->in.hdr->flags);
  684. spin_lock(&conn->session->lock);
  685. __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
  686. spin_unlock(&conn->session->lock);
  687. }
  688. return rc;
  689. }
  690. static int
  691. iscsi_data_recv(struct iscsi_conn *conn)
  692. {
  693. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  694. int rc = 0, opcode;
  695. opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
  696. switch (opcode) {
  697. case ISCSI_OP_SCSI_DATA_IN:
  698. rc = iscsi_scsi_data_in(conn);
  699. break;
  700. case ISCSI_OP_SCSI_CMD_RSP:
  701. case ISCSI_OP_TEXT_RSP:
  702. case ISCSI_OP_LOGIN_RSP:
  703. case ISCSI_OP_ASYNC_EVENT:
  704. case ISCSI_OP_REJECT:
  705. /*
  706. * Collect data segment to the connection's data
  707. * placeholder
  708. */
  709. if (iscsi_tcp_copy(conn, tcp_conn->in.datalen)) {
  710. rc = -EAGAIN;
  711. goto exit;
  712. }
  713. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, conn->data,
  714. tcp_conn->in.datalen);
  715. if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
  716. iscsi_recv_digest_update(tcp_conn, conn->data,
  717. tcp_conn->in.datalen);
  718. break;
  719. default:
  720. BUG_ON(1);
  721. }
  722. exit:
  723. return rc;
  724. }
  725. /**
  726. * iscsi_tcp_data_recv - TCP receive in sendfile fashion
  727. * @rd_desc: read descriptor
  728. * @skb: socket buffer
  729. * @offset: offset in skb
  730. * @len: skb->len - offset
  731. **/
  732. static int
  733. iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  734. unsigned int offset, size_t len)
  735. {
  736. int rc;
  737. struct iscsi_conn *conn = rd_desc->arg.data;
  738. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  739. int processed;
  740. char pad[ISCSI_PAD_LEN];
  741. struct scatterlist sg;
  742. /*
  743. * Save current SKB and its offset in the corresponding
  744. * connection context.
  745. */
  746. tcp_conn->in.copy = skb->len - offset;
  747. tcp_conn->in.offset = offset;
  748. tcp_conn->in.skb = skb;
  749. tcp_conn->in.len = tcp_conn->in.copy;
  750. BUG_ON(tcp_conn->in.copy <= 0);
  751. debug_tcp("in %d bytes\n", tcp_conn->in.copy);
  752. more:
  753. tcp_conn->in.copied = 0;
  754. rc = 0;
  755. if (unlikely(conn->suspend_rx)) {
  756. debug_tcp("conn %d Rx suspended!\n", conn->id);
  757. return 0;
  758. }
  759. if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
  760. tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
  761. rc = iscsi_hdr_extract(tcp_conn);
  762. if (rc) {
  763. if (rc == -EAGAIN)
  764. goto nomore;
  765. else {
  766. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  767. return 0;
  768. }
  769. }
  770. /*
  771. * Verify and process incoming PDU header.
  772. */
  773. rc = iscsi_tcp_hdr_recv(conn);
  774. if (!rc && tcp_conn->in.datalen) {
  775. if (conn->datadgst_en)
  776. crypto_hash_init(&tcp_conn->rx_hash);
  777. tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
  778. } else if (rc) {
  779. iscsi_conn_failure(conn, rc);
  780. return 0;
  781. }
  782. }
  783. if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
  784. uint32_t recv_digest;
  785. debug_tcp("extra data_recv offset %d copy %d\n",
  786. tcp_conn->in.offset, tcp_conn->in.copy);
  787. rc = iscsi_tcp_copy(conn, sizeof(uint32_t));
  788. if (rc) {
  789. if (rc == -EAGAIN)
  790. goto again;
  791. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  792. return 0;
  793. }
  794. memcpy(&recv_digest, conn->data, sizeof(uint32_t));
  795. if (recv_digest != tcp_conn->in.datadgst) {
  796. debug_tcp("iscsi_tcp: data digest error!"
  797. "0x%x != 0x%x\n", recv_digest,
  798. tcp_conn->in.datadgst);
  799. iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
  800. return 0;
  801. } else {
  802. debug_tcp("iscsi_tcp: data digest match!"
  803. "0x%x == 0x%x\n", recv_digest,
  804. tcp_conn->in.datadgst);
  805. tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
  806. }
  807. }
  808. if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
  809. tcp_conn->in.copy) {
  810. debug_tcp("data_recv offset %d copy %d\n",
  811. tcp_conn->in.offset, tcp_conn->in.copy);
  812. rc = iscsi_data_recv(conn);
  813. if (rc) {
  814. if (rc == -EAGAIN)
  815. goto again;
  816. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  817. return 0;
  818. }
  819. tcp_conn->in.copy -= tcp_conn->in.padding;
  820. tcp_conn->in.offset += tcp_conn->in.padding;
  821. if (conn->datadgst_en) {
  822. if (tcp_conn->in.padding) {
  823. debug_tcp("padding -> %d\n",
  824. tcp_conn->in.padding);
  825. memset(pad, 0, tcp_conn->in.padding);
  826. sg_init_one(&sg, pad, tcp_conn->in.padding);
  827. crypto_hash_update(&tcp_conn->rx_hash,
  828. &sg, sg.length);
  829. }
  830. crypto_hash_final(&tcp_conn->rx_hash,
  831. (u8 *) &tcp_conn->in.datadgst);
  832. debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
  833. tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
  834. tcp_conn->data_copied = 0;
  835. } else
  836. tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
  837. }
  838. debug_tcp("f, processed %d from out of %d padding %d\n",
  839. tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
  840. BUG_ON(tcp_conn->in.offset - offset > len);
  841. if (tcp_conn->in.offset - offset != len) {
  842. debug_tcp("continue to process %d bytes\n",
  843. (int)len - (tcp_conn->in.offset - offset));
  844. goto more;
  845. }
  846. nomore:
  847. processed = tcp_conn->in.offset - offset;
  848. BUG_ON(processed == 0);
  849. return processed;
  850. again:
  851. processed = tcp_conn->in.offset - offset;
  852. debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
  853. processed, (int)len, (int)rd_desc->count);
  854. BUG_ON(processed == 0);
  855. BUG_ON(processed > len);
  856. conn->rxdata_octets += processed;
  857. return processed;
  858. }
  859. static void
  860. iscsi_tcp_data_ready(struct sock *sk, int flag)
  861. {
  862. struct iscsi_conn *conn = sk->sk_user_data;
  863. read_descriptor_t rd_desc;
  864. read_lock(&sk->sk_callback_lock);
  865. /*
  866. * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
  867. * We set count to 1 because we want the network layer to
  868. * hand us all the skbs that are available. iscsi_tcp_data_recv
  869. * handled pdus that cross buffers or pdus that still need data.
  870. */
  871. rd_desc.arg.data = conn;
  872. rd_desc.count = 1;
  873. tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
  874. read_unlock(&sk->sk_callback_lock);
  875. }
  876. static void
  877. iscsi_tcp_state_change(struct sock *sk)
  878. {
  879. struct iscsi_tcp_conn *tcp_conn;
  880. struct iscsi_conn *conn;
  881. struct iscsi_session *session;
  882. void (*old_state_change)(struct sock *);
  883. read_lock(&sk->sk_callback_lock);
  884. conn = (struct iscsi_conn*)sk->sk_user_data;
  885. session = conn->session;
  886. if ((sk->sk_state == TCP_CLOSE_WAIT ||
  887. sk->sk_state == TCP_CLOSE) &&
  888. !atomic_read(&sk->sk_rmem_alloc)) {
  889. debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
  890. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  891. }
  892. tcp_conn = conn->dd_data;
  893. old_state_change = tcp_conn->old_state_change;
  894. read_unlock(&sk->sk_callback_lock);
  895. old_state_change(sk);
  896. }
  897. /**
  898. * iscsi_write_space - Called when more output buffer space is available
  899. * @sk: socket space is available for
  900. **/
  901. static void
  902. iscsi_write_space(struct sock *sk)
  903. {
  904. struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
  905. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  906. tcp_conn->old_write_space(sk);
  907. debug_tcp("iscsi_write_space: cid %d\n", conn->id);
  908. scsi_queue_work(conn->session->host, &conn->xmitwork);
  909. }
  910. static void
  911. iscsi_conn_set_callbacks(struct iscsi_conn *conn)
  912. {
  913. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  914. struct sock *sk = tcp_conn->sock->sk;
  915. /* assign new callbacks */
  916. write_lock_bh(&sk->sk_callback_lock);
  917. sk->sk_user_data = conn;
  918. tcp_conn->old_data_ready = sk->sk_data_ready;
  919. tcp_conn->old_state_change = sk->sk_state_change;
  920. tcp_conn->old_write_space = sk->sk_write_space;
  921. sk->sk_data_ready = iscsi_tcp_data_ready;
  922. sk->sk_state_change = iscsi_tcp_state_change;
  923. sk->sk_write_space = iscsi_write_space;
  924. write_unlock_bh(&sk->sk_callback_lock);
  925. }
  926. static void
  927. iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
  928. {
  929. struct sock *sk = tcp_conn->sock->sk;
  930. /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
  931. write_lock_bh(&sk->sk_callback_lock);
  932. sk->sk_user_data = NULL;
  933. sk->sk_data_ready = tcp_conn->old_data_ready;
  934. sk->sk_state_change = tcp_conn->old_state_change;
  935. sk->sk_write_space = tcp_conn->old_write_space;
  936. sk->sk_no_check = 0;
  937. write_unlock_bh(&sk->sk_callback_lock);
  938. }
  939. /**
  940. * iscsi_send - generic send routine
  941. * @sk: kernel's socket
  942. * @buf: buffer to write from
  943. * @size: actual size to write
  944. * @flags: socket's flags
  945. */
  946. static inline int
  947. iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
  948. {
  949. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  950. struct socket *sk = tcp_conn->sock;
  951. int offset = buf->sg.offset + buf->sent, res;
  952. /*
  953. * if we got use_sg=0 or are sending something we kmallocd
  954. * then we did not have to do kmap (kmap returns page_address)
  955. *
  956. * if we got use_sg > 0, but had to drop down, we do not
  957. * set clustering so this should only happen for that
  958. * slab case.
  959. */
  960. if (buf->use_sendmsg)
  961. res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
  962. else
  963. res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
  964. if (res >= 0) {
  965. conn->txdata_octets += res;
  966. buf->sent += res;
  967. return res;
  968. }
  969. tcp_conn->sendpage_failures_cnt++;
  970. if (res == -EAGAIN)
  971. res = -ENOBUFS;
  972. else
  973. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  974. return res;
  975. }
  976. /**
  977. * iscsi_sendhdr - send PDU Header via tcp_sendpage()
  978. * @conn: iscsi connection
  979. * @buf: buffer to write from
  980. * @datalen: lenght of data to be sent after the header
  981. *
  982. * Notes:
  983. * (Tx, Fast Path)
  984. **/
  985. static inline int
  986. iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
  987. {
  988. int flags = 0; /* MSG_DONTWAIT; */
  989. int res, size;
  990. size = buf->sg.length - buf->sent;
  991. BUG_ON(buf->sent + size > buf->sg.length);
  992. if (buf->sent + size != buf->sg.length || datalen)
  993. flags |= MSG_MORE;
  994. res = iscsi_send(conn, buf, size, flags);
  995. debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
  996. if (res >= 0) {
  997. if (size != res)
  998. return -EAGAIN;
  999. return 0;
  1000. }
  1001. return res;
  1002. }
  1003. /**
  1004. * iscsi_sendpage - send one page of iSCSI Data-Out.
  1005. * @conn: iscsi connection
  1006. * @buf: buffer to write from
  1007. * @count: remaining data
  1008. * @sent: number of bytes sent
  1009. *
  1010. * Notes:
  1011. * (Tx, Fast Path)
  1012. **/
  1013. static inline int
  1014. iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
  1015. int *count, int *sent)
  1016. {
  1017. int flags = 0; /* MSG_DONTWAIT; */
  1018. int res, size;
  1019. size = buf->sg.length - buf->sent;
  1020. BUG_ON(buf->sent + size > buf->sg.length);
  1021. if (size > *count)
  1022. size = *count;
  1023. if (buf->sent + size != buf->sg.length || *count != size)
  1024. flags |= MSG_MORE;
  1025. res = iscsi_send(conn, buf, size, flags);
  1026. debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
  1027. size, buf->sent, *count, *sent, res);
  1028. if (res >= 0) {
  1029. *count -= res;
  1030. *sent += res;
  1031. if (size != res)
  1032. return -EAGAIN;
  1033. return 0;
  1034. }
  1035. return res;
  1036. }
  1037. static inline void
  1038. iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
  1039. struct iscsi_tcp_cmd_task *tcp_ctask)
  1040. {
  1041. crypto_hash_init(&tcp_conn->tx_hash);
  1042. tcp_ctask->digest_count = 4;
  1043. }
  1044. /**
  1045. * iscsi_solicit_data_cont - initialize next Data-Out
  1046. * @conn: iscsi connection
  1047. * @ctask: scsi command task
  1048. * @r2t: R2T info
  1049. * @left: bytes left to transfer
  1050. *
  1051. * Notes:
  1052. * Initialize next Data-Out within this R2T sequence and continue
  1053. * to process next Scatter-Gather element(if any) of this SCSI command.
  1054. *
  1055. * Called under connection lock.
  1056. **/
  1057. static void
  1058. iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1059. struct iscsi_r2t_info *r2t, int left)
  1060. {
  1061. struct iscsi_data *hdr;
  1062. struct scsi_cmnd *sc = ctask->sc;
  1063. int new_offset;
  1064. hdr = &r2t->dtask.hdr;
  1065. memset(hdr, 0, sizeof(struct iscsi_data));
  1066. hdr->ttt = r2t->ttt;
  1067. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  1068. r2t->solicit_datasn++;
  1069. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  1070. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1071. hdr->itt = ctask->hdr->itt;
  1072. hdr->exp_statsn = r2t->exp_statsn;
  1073. new_offset = r2t->data_offset + r2t->sent;
  1074. hdr->offset = cpu_to_be32(new_offset);
  1075. if (left > conn->max_xmit_dlength) {
  1076. hton24(hdr->dlength, conn->max_xmit_dlength);
  1077. r2t->data_count = conn->max_xmit_dlength;
  1078. } else {
  1079. hton24(hdr->dlength, left);
  1080. r2t->data_count = left;
  1081. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  1082. }
  1083. conn->dataout_pdus_cnt++;
  1084. iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
  1085. sizeof(struct iscsi_hdr));
  1086. if (iscsi_buf_left(&r2t->sendbuf))
  1087. return;
  1088. if (sc->use_sg) {
  1089. iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
  1090. r2t->sg += 1;
  1091. } else {
  1092. iscsi_buf_init_iov(&r2t->sendbuf,
  1093. (char*)sc->request_buffer + new_offset,
  1094. r2t->data_count);
  1095. r2t->sg = NULL;
  1096. }
  1097. }
  1098. static void iscsi_set_padding(struct iscsi_tcp_cmd_task *tcp_ctask,
  1099. unsigned long len)
  1100. {
  1101. tcp_ctask->pad_count = len & (ISCSI_PAD_LEN - 1);
  1102. if (!tcp_ctask->pad_count)
  1103. return;
  1104. tcp_ctask->pad_count = ISCSI_PAD_LEN - tcp_ctask->pad_count;
  1105. debug_scsi("write padding %d bytes\n", tcp_ctask->pad_count);
  1106. tcp_ctask->xmstate |= XMSTATE_W_PAD;
  1107. }
  1108. /**
  1109. * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
  1110. * @conn: iscsi connection
  1111. * @ctask: scsi command task
  1112. * @sc: scsi command
  1113. **/
  1114. static void
  1115. iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
  1116. {
  1117. struct scsi_cmnd *sc = ctask->sc;
  1118. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1119. BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
  1120. tcp_ctask->sent = 0;
  1121. tcp_ctask->sg_count = 0;
  1122. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  1123. tcp_ctask->xmstate = XMSTATE_W_HDR;
  1124. tcp_ctask->exp_r2tsn = 0;
  1125. BUG_ON(ctask->total_length == 0);
  1126. if (sc->use_sg) {
  1127. struct scatterlist *sg = sc->request_buffer;
  1128. iscsi_buf_init_sg(&tcp_ctask->sendbuf, sg);
  1129. tcp_ctask->sg = sg + 1;
  1130. tcp_ctask->bad_sg = sg + sc->use_sg;
  1131. } else {
  1132. iscsi_buf_init_iov(&tcp_ctask->sendbuf,
  1133. sc->request_buffer,
  1134. sc->request_bufflen);
  1135. tcp_ctask->sg = NULL;
  1136. tcp_ctask->bad_sg = NULL;
  1137. }
  1138. debug_scsi("cmd [itt 0x%x total %d imm_data %d "
  1139. "unsol count %d, unsol offset %d]\n",
  1140. ctask->itt, ctask->total_length, ctask->imm_count,
  1141. ctask->unsol_count, ctask->unsol_offset);
  1142. } else
  1143. tcp_ctask->xmstate = XMSTATE_R_HDR;
  1144. iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
  1145. sizeof(struct iscsi_hdr));
  1146. }
  1147. /**
  1148. * iscsi_tcp_mtask_xmit - xmit management(immediate) task
  1149. * @conn: iscsi connection
  1150. * @mtask: task management task
  1151. *
  1152. * Notes:
  1153. * The function can return -EAGAIN in which case caller must
  1154. * call it again later, or recover. '0' return code means successful
  1155. * xmit.
  1156. *
  1157. * Management xmit state machine consists of two states:
  1158. * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
  1159. * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
  1160. **/
  1161. static int
  1162. iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
  1163. {
  1164. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1165. int rc;
  1166. debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
  1167. conn->id, tcp_mtask->xmstate, mtask->itt);
  1168. if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
  1169. tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
  1170. if (mtask->data_count)
  1171. tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
  1172. if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
  1173. conn->stop_stage != STOP_CONN_RECOVER &&
  1174. conn->hdrdgst_en)
  1175. iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
  1176. (u8*)tcp_mtask->hdrext);
  1177. rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
  1178. mtask->data_count);
  1179. if (rc) {
  1180. tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
  1181. if (mtask->data_count)
  1182. tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
  1183. return rc;
  1184. }
  1185. }
  1186. if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
  1187. BUG_ON(!mtask->data_count);
  1188. tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
  1189. /* FIXME: implement.
  1190. * Virtual buffer could be spreaded across multiple pages...
  1191. */
  1192. do {
  1193. int rc;
  1194. rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
  1195. &mtask->data_count, &tcp_mtask->sent);
  1196. if (rc) {
  1197. tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
  1198. return rc;
  1199. }
  1200. } while (mtask->data_count);
  1201. }
  1202. BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
  1203. if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
  1204. struct iscsi_session *session = conn->session;
  1205. spin_lock_bh(&session->lock);
  1206. list_del(&conn->mtask->running);
  1207. __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
  1208. sizeof(void*));
  1209. spin_unlock_bh(&session->lock);
  1210. }
  1211. return 0;
  1212. }
  1213. static inline int
  1214. iscsi_send_read_hdr(struct iscsi_conn *conn,
  1215. struct iscsi_tcp_cmd_task *tcp_ctask)
  1216. {
  1217. int rc;
  1218. tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
  1219. if (conn->hdrdgst_en)
  1220. iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
  1221. (u8*)tcp_ctask->hdrext);
  1222. rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0);
  1223. if (!rc) {
  1224. BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
  1225. return 0; /* wait for Data-In */
  1226. }
  1227. tcp_ctask->xmstate |= XMSTATE_R_HDR;
  1228. return rc;
  1229. }
  1230. static inline int
  1231. iscsi_send_write_hdr(struct iscsi_conn *conn,
  1232. struct iscsi_cmd_task *ctask)
  1233. {
  1234. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1235. int rc;
  1236. tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
  1237. if (conn->hdrdgst_en)
  1238. iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
  1239. (u8*)tcp_ctask->hdrext);
  1240. rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
  1241. if (rc) {
  1242. tcp_ctask->xmstate |= XMSTATE_W_HDR;
  1243. return rc;
  1244. }
  1245. if (ctask->imm_count) {
  1246. tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
  1247. iscsi_set_padding(tcp_ctask, ctask->imm_count);
  1248. if (ctask->conn->datadgst_en) {
  1249. iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
  1250. tcp_ctask->immdigest = 0;
  1251. }
  1252. }
  1253. if (ctask->unsol_count)
  1254. tcp_ctask->xmstate |= XMSTATE_UNS_HDR | XMSTATE_UNS_INIT;
  1255. return 0;
  1256. }
  1257. static int
  1258. iscsi_send_padding(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1259. {
  1260. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1261. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1262. int sent = 0, rc;
  1263. if (tcp_ctask->xmstate & XMSTATE_W_PAD) {
  1264. iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
  1265. tcp_ctask->pad_count);
  1266. if (conn->datadgst_en)
  1267. crypto_hash_update(&tcp_conn->tx_hash,
  1268. &tcp_ctask->sendbuf.sg,
  1269. tcp_ctask->sendbuf.sg.length);
  1270. } else if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_PAD))
  1271. return 0;
  1272. tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
  1273. tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_PAD;
  1274. debug_scsi("sending %d pad bytes for itt 0x%x\n",
  1275. tcp_ctask->pad_count, ctask->itt);
  1276. rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
  1277. &sent);
  1278. if (rc) {
  1279. debug_scsi("padding send failed %d\n", rc);
  1280. tcp_ctask->xmstate |= XMSTATE_W_RESEND_PAD;
  1281. }
  1282. return rc;
  1283. }
  1284. static int
  1285. iscsi_send_digest(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1286. struct iscsi_buf *buf, uint32_t *digest)
  1287. {
  1288. struct iscsi_tcp_cmd_task *tcp_ctask;
  1289. struct iscsi_tcp_conn *tcp_conn;
  1290. int rc, sent = 0;
  1291. if (!conn->datadgst_en)
  1292. return 0;
  1293. tcp_ctask = ctask->dd_data;
  1294. tcp_conn = conn->dd_data;
  1295. if (!(tcp_ctask->xmstate & XMSTATE_W_RESEND_DATA_DIGEST)) {
  1296. crypto_hash_final(&tcp_conn->tx_hash, (u8*)digest);
  1297. iscsi_buf_init_iov(buf, (char*)digest, 4);
  1298. }
  1299. tcp_ctask->xmstate &= ~XMSTATE_W_RESEND_DATA_DIGEST;
  1300. rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
  1301. if (!rc)
  1302. debug_scsi("sent digest 0x%x for itt 0x%x\n", *digest,
  1303. ctask->itt);
  1304. else {
  1305. debug_scsi("sending digest 0x%x failed for itt 0x%x!\n",
  1306. *digest, ctask->itt);
  1307. tcp_ctask->xmstate |= XMSTATE_W_RESEND_DATA_DIGEST;
  1308. }
  1309. return rc;
  1310. }
  1311. static int
  1312. iscsi_send_data(struct iscsi_cmd_task *ctask, struct iscsi_buf *sendbuf,
  1313. struct scatterlist **sg, int *sent, int *count,
  1314. struct iscsi_buf *digestbuf, uint32_t *digest)
  1315. {
  1316. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1317. struct iscsi_conn *conn = ctask->conn;
  1318. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1319. int rc, buf_sent, offset;
  1320. while (*count) {
  1321. buf_sent = 0;
  1322. offset = sendbuf->sent;
  1323. rc = iscsi_sendpage(conn, sendbuf, count, &buf_sent);
  1324. *sent = *sent + buf_sent;
  1325. if (buf_sent && conn->datadgst_en)
  1326. partial_sg_digest_update(&tcp_conn->tx_hash,
  1327. &sendbuf->sg, sendbuf->sg.offset + offset,
  1328. buf_sent);
  1329. if (!iscsi_buf_left(sendbuf) && *sg != tcp_ctask->bad_sg) {
  1330. iscsi_buf_init_sg(sendbuf, *sg);
  1331. *sg = *sg + 1;
  1332. }
  1333. if (rc)
  1334. return rc;
  1335. }
  1336. rc = iscsi_send_padding(conn, ctask);
  1337. if (rc)
  1338. return rc;
  1339. return iscsi_send_digest(conn, ctask, digestbuf, digest);
  1340. }
  1341. static int
  1342. iscsi_send_unsol_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1343. {
  1344. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1345. struct iscsi_data_task *dtask;
  1346. int rc;
  1347. tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
  1348. if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
  1349. dtask = &tcp_ctask->unsol_dtask;
  1350. iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr);
  1351. iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
  1352. sizeof(struct iscsi_hdr));
  1353. if (conn->hdrdgst_en)
  1354. iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
  1355. (u8*)dtask->hdrext);
  1356. tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
  1357. iscsi_set_padding(tcp_ctask, ctask->data_count);
  1358. }
  1359. rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
  1360. if (rc) {
  1361. tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
  1362. tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
  1363. return rc;
  1364. }
  1365. if (conn->datadgst_en) {
  1366. dtask = &tcp_ctask->unsol_dtask;
  1367. iscsi_data_digest_init(ctask->conn->dd_data, tcp_ctask);
  1368. dtask->digest = 0;
  1369. }
  1370. debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
  1371. ctask->itt, ctask->unsol_count, tcp_ctask->sent);
  1372. return 0;
  1373. }
  1374. static int
  1375. iscsi_send_unsol_pdu(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1376. {
  1377. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1378. int rc;
  1379. if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
  1380. BUG_ON(!ctask->unsol_count);
  1381. tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
  1382. send_hdr:
  1383. rc = iscsi_send_unsol_hdr(conn, ctask);
  1384. if (rc)
  1385. return rc;
  1386. }
  1387. if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
  1388. struct iscsi_data_task *dtask = &tcp_ctask->unsol_dtask;
  1389. int start = tcp_ctask->sent;
  1390. rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
  1391. &tcp_ctask->sent, &ctask->data_count,
  1392. &dtask->digestbuf, &dtask->digest);
  1393. ctask->unsol_count -= tcp_ctask->sent - start;
  1394. if (rc)
  1395. return rc;
  1396. tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
  1397. /*
  1398. * Done with the Data-Out. Next, check if we need
  1399. * to send another unsolicited Data-Out.
  1400. */
  1401. if (ctask->unsol_count) {
  1402. debug_scsi("sending more uns\n");
  1403. tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
  1404. goto send_hdr;
  1405. }
  1406. }
  1407. return 0;
  1408. }
  1409. static int iscsi_send_sol_pdu(struct iscsi_conn *conn,
  1410. struct iscsi_cmd_task *ctask)
  1411. {
  1412. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1413. struct iscsi_session *session = conn->session;
  1414. struct iscsi_r2t_info *r2t;
  1415. struct iscsi_data_task *dtask;
  1416. int left, rc;
  1417. if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
  1418. tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
  1419. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1420. if (!tcp_ctask->r2t)
  1421. __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
  1422. sizeof(void*));
  1423. send_hdr:
  1424. r2t = tcp_ctask->r2t;
  1425. dtask = &r2t->dtask;
  1426. if (conn->hdrdgst_en)
  1427. iscsi_hdr_digest(conn, &r2t->headbuf,
  1428. (u8*)dtask->hdrext);
  1429. rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
  1430. if (rc) {
  1431. tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
  1432. tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
  1433. return rc;
  1434. }
  1435. if (conn->datadgst_en) {
  1436. iscsi_data_digest_init(conn->dd_data, tcp_ctask);
  1437. dtask->digest = 0;
  1438. }
  1439. iscsi_set_padding(tcp_ctask, r2t->data_count);
  1440. debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
  1441. r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
  1442. r2t->sent);
  1443. }
  1444. if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
  1445. r2t = tcp_ctask->r2t;
  1446. dtask = &r2t->dtask;
  1447. rc = iscsi_send_data(ctask, &r2t->sendbuf, &r2t->sg,
  1448. &r2t->sent, &r2t->data_count,
  1449. &dtask->digestbuf, &dtask->digest);
  1450. if (rc)
  1451. return rc;
  1452. tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
  1453. /*
  1454. * Done with this Data-Out. Next, check if we have
  1455. * to send another Data-Out for this R2T.
  1456. */
  1457. BUG_ON(r2t->data_length - r2t->sent < 0);
  1458. left = r2t->data_length - r2t->sent;
  1459. if (left) {
  1460. iscsi_solicit_data_cont(conn, ctask, r2t, left);
  1461. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1462. tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
  1463. goto send_hdr;
  1464. }
  1465. /*
  1466. * Done with this R2T. Check if there are more
  1467. * outstanding R2Ts ready to be processed.
  1468. */
  1469. spin_lock_bh(&session->lock);
  1470. tcp_ctask->r2t = NULL;
  1471. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  1472. sizeof(void*));
  1473. if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t,
  1474. sizeof(void*))) {
  1475. tcp_ctask->r2t = r2t;
  1476. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1477. tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
  1478. spin_unlock_bh(&session->lock);
  1479. goto send_hdr;
  1480. }
  1481. spin_unlock_bh(&session->lock);
  1482. }
  1483. return 0;
  1484. }
  1485. static int
  1486. iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1487. {
  1488. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1489. int rc = 0;
  1490. debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
  1491. conn->id, tcp_ctask->xmstate, ctask->itt);
  1492. /*
  1493. * serialize with TMF AbortTask
  1494. */
  1495. if (ctask->mtask)
  1496. return rc;
  1497. if (tcp_ctask->xmstate & XMSTATE_R_HDR)
  1498. return iscsi_send_read_hdr(conn, tcp_ctask);
  1499. if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
  1500. rc = iscsi_send_write_hdr(conn, ctask);
  1501. if (rc)
  1502. return rc;
  1503. }
  1504. if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
  1505. rc = iscsi_send_data(ctask, &tcp_ctask->sendbuf, &tcp_ctask->sg,
  1506. &tcp_ctask->sent, &ctask->imm_count,
  1507. &tcp_ctask->immbuf, &tcp_ctask->immdigest);
  1508. if (rc)
  1509. return rc;
  1510. tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
  1511. }
  1512. rc = iscsi_send_unsol_pdu(conn, ctask);
  1513. if (rc)
  1514. return rc;
  1515. rc = iscsi_send_sol_pdu(conn, ctask);
  1516. if (rc)
  1517. return rc;
  1518. return rc;
  1519. }
  1520. static struct iscsi_cls_conn *
  1521. iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1522. {
  1523. struct iscsi_conn *conn;
  1524. struct iscsi_cls_conn *cls_conn;
  1525. struct iscsi_tcp_conn *tcp_conn;
  1526. cls_conn = iscsi_conn_setup(cls_session, conn_idx);
  1527. if (!cls_conn)
  1528. return NULL;
  1529. conn = cls_conn->dd_data;
  1530. /*
  1531. * due to strange issues with iser these are not set
  1532. * in iscsi_conn_setup
  1533. */
  1534. conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
  1535. tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
  1536. if (!tcp_conn)
  1537. goto tcp_conn_alloc_fail;
  1538. conn->dd_data = tcp_conn;
  1539. tcp_conn->iscsi_conn = conn;
  1540. tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
  1541. /* initial operational parameters */
  1542. tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
  1543. tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1544. CRYPTO_ALG_ASYNC);
  1545. tcp_conn->tx_hash.flags = 0;
  1546. if (!tcp_conn->tx_hash.tfm)
  1547. goto free_tcp_conn;
  1548. tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1549. CRYPTO_ALG_ASYNC);
  1550. tcp_conn->rx_hash.flags = 0;
  1551. if (!tcp_conn->rx_hash.tfm)
  1552. goto free_tx_tfm;
  1553. return cls_conn;
  1554. free_tx_tfm:
  1555. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1556. free_tcp_conn:
  1557. kfree(tcp_conn);
  1558. tcp_conn_alloc_fail:
  1559. iscsi_conn_teardown(cls_conn);
  1560. return NULL;
  1561. }
  1562. static void
  1563. iscsi_tcp_release_conn(struct iscsi_conn *conn)
  1564. {
  1565. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1566. if (!tcp_conn->sock)
  1567. return;
  1568. sock_hold(tcp_conn->sock->sk);
  1569. iscsi_conn_restore_callbacks(tcp_conn);
  1570. sock_put(tcp_conn->sock->sk);
  1571. sock_release(tcp_conn->sock);
  1572. tcp_conn->sock = NULL;
  1573. conn->recv_lock = NULL;
  1574. }
  1575. static void
  1576. iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1577. {
  1578. struct iscsi_conn *conn = cls_conn->dd_data;
  1579. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1580. int digest = 0;
  1581. if (conn->hdrdgst_en || conn->datadgst_en)
  1582. digest = 1;
  1583. iscsi_tcp_release_conn(conn);
  1584. iscsi_conn_teardown(cls_conn);
  1585. /* now free tcp_conn */
  1586. if (digest) {
  1587. if (tcp_conn->tx_hash.tfm)
  1588. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1589. if (tcp_conn->rx_hash.tfm)
  1590. crypto_free_hash(tcp_conn->rx_hash.tfm);
  1591. }
  1592. kfree(tcp_conn);
  1593. }
  1594. static void
  1595. iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1596. {
  1597. struct iscsi_conn *conn = cls_conn->dd_data;
  1598. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1599. iscsi_conn_stop(cls_conn, flag);
  1600. iscsi_tcp_release_conn(conn);
  1601. tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
  1602. }
  1603. static int
  1604. iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  1605. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  1606. int is_leading)
  1607. {
  1608. struct iscsi_conn *conn = cls_conn->dd_data;
  1609. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1610. struct sock *sk;
  1611. struct socket *sock;
  1612. int err;
  1613. /* lookup for existing socket */
  1614. sock = sockfd_lookup((int)transport_eph, &err);
  1615. if (!sock) {
  1616. printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
  1617. return -EEXIST;
  1618. }
  1619. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  1620. if (err)
  1621. return err;
  1622. /* bind iSCSI connection and socket */
  1623. tcp_conn->sock = sock;
  1624. /* setup Socket parameters */
  1625. sk = sock->sk;
  1626. sk->sk_reuse = 1;
  1627. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  1628. sk->sk_allocation = GFP_ATOMIC;
  1629. /* FIXME: disable Nagle's algorithm */
  1630. /*
  1631. * Intercept TCP callbacks for sendfile like receive
  1632. * processing.
  1633. */
  1634. conn->recv_lock = &sk->sk_callback_lock;
  1635. iscsi_conn_set_callbacks(conn);
  1636. tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
  1637. /*
  1638. * set receive state machine into initial state
  1639. */
  1640. tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
  1641. return 0;
  1642. }
  1643. /* called with host lock */
  1644. static void
  1645. iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
  1646. char *data, uint32_t data_size)
  1647. {
  1648. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1649. iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
  1650. sizeof(struct iscsi_hdr));
  1651. tcp_mtask->xmstate = XMSTATE_IMM_HDR;
  1652. tcp_mtask->sent = 0;
  1653. if (mtask->data_count)
  1654. iscsi_buf_init_iov(&tcp_mtask->sendbuf, (char*)mtask->data,
  1655. mtask->data_count);
  1656. }
  1657. static int
  1658. iscsi_r2tpool_alloc(struct iscsi_session *session)
  1659. {
  1660. int i;
  1661. int cmd_i;
  1662. /*
  1663. * initialize per-task: R2T pool and xmit queue
  1664. */
  1665. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1666. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1667. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1668. /*
  1669. * pre-allocated x4 as much r2ts to handle race when
  1670. * target acks DataOut faster than we data_xmit() queues
  1671. * could replenish r2tqueue.
  1672. */
  1673. /* R2T pool */
  1674. if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4,
  1675. (void***)&tcp_ctask->r2ts,
  1676. sizeof(struct iscsi_r2t_info))) {
  1677. goto r2t_alloc_fail;
  1678. }
  1679. /* R2T xmit queue */
  1680. tcp_ctask->r2tqueue = kfifo_alloc(
  1681. session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
  1682. if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
  1683. iscsi_pool_free(&tcp_ctask->r2tpool,
  1684. (void**)tcp_ctask->r2ts);
  1685. goto r2t_alloc_fail;
  1686. }
  1687. }
  1688. return 0;
  1689. r2t_alloc_fail:
  1690. for (i = 0; i < cmd_i; i++) {
  1691. struct iscsi_cmd_task *ctask = session->cmds[i];
  1692. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1693. kfifo_free(tcp_ctask->r2tqueue);
  1694. iscsi_pool_free(&tcp_ctask->r2tpool,
  1695. (void**)tcp_ctask->r2ts);
  1696. }
  1697. return -ENOMEM;
  1698. }
  1699. static void
  1700. iscsi_r2tpool_free(struct iscsi_session *session)
  1701. {
  1702. int i;
  1703. for (i = 0; i < session->cmds_max; i++) {
  1704. struct iscsi_cmd_task *ctask = session->cmds[i];
  1705. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1706. kfifo_free(tcp_ctask->r2tqueue);
  1707. iscsi_pool_free(&tcp_ctask->r2tpool,
  1708. (void**)tcp_ctask->r2ts);
  1709. }
  1710. }
  1711. static int
  1712. iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
  1713. char *buf, int buflen)
  1714. {
  1715. struct iscsi_conn *conn = cls_conn->dd_data;
  1716. struct iscsi_session *session = conn->session;
  1717. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1718. int value;
  1719. switch(param) {
  1720. case ISCSI_PARAM_HDRDGST_EN:
  1721. iscsi_set_param(cls_conn, param, buf, buflen);
  1722. tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
  1723. if (conn->hdrdgst_en)
  1724. tcp_conn->hdr_size += sizeof(__u32);
  1725. break;
  1726. case ISCSI_PARAM_DATADGST_EN:
  1727. iscsi_set_param(cls_conn, param, buf, buflen);
  1728. tcp_conn->sendpage = conn->datadgst_en ?
  1729. sock_no_sendpage : tcp_conn->sock->ops->sendpage;
  1730. break;
  1731. case ISCSI_PARAM_MAX_R2T:
  1732. sscanf(buf, "%d", &value);
  1733. if (session->max_r2t == roundup_pow_of_two(value))
  1734. break;
  1735. iscsi_r2tpool_free(session);
  1736. iscsi_set_param(cls_conn, param, buf, buflen);
  1737. if (session->max_r2t & (session->max_r2t - 1))
  1738. session->max_r2t = roundup_pow_of_two(session->max_r2t);
  1739. if (iscsi_r2tpool_alloc(session))
  1740. return -ENOMEM;
  1741. break;
  1742. default:
  1743. return iscsi_set_param(cls_conn, param, buf, buflen);
  1744. }
  1745. return 0;
  1746. }
  1747. static int
  1748. iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1749. enum iscsi_param param, char *buf)
  1750. {
  1751. struct iscsi_conn *conn = cls_conn->dd_data;
  1752. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1753. struct inet_sock *inet;
  1754. struct ipv6_pinfo *np;
  1755. struct sock *sk;
  1756. int len;
  1757. switch(param) {
  1758. case ISCSI_PARAM_CONN_PORT:
  1759. mutex_lock(&conn->xmitmutex);
  1760. if (!tcp_conn->sock) {
  1761. mutex_unlock(&conn->xmitmutex);
  1762. return -EINVAL;
  1763. }
  1764. inet = inet_sk(tcp_conn->sock->sk);
  1765. len = sprintf(buf, "%hu\n", be16_to_cpu(inet->dport));
  1766. mutex_unlock(&conn->xmitmutex);
  1767. break;
  1768. case ISCSI_PARAM_CONN_ADDRESS:
  1769. mutex_lock(&conn->xmitmutex);
  1770. if (!tcp_conn->sock) {
  1771. mutex_unlock(&conn->xmitmutex);
  1772. return -EINVAL;
  1773. }
  1774. sk = tcp_conn->sock->sk;
  1775. if (sk->sk_family == PF_INET) {
  1776. inet = inet_sk(sk);
  1777. len = sprintf(buf, "%u.%u.%u.%u\n",
  1778. NIPQUAD(inet->daddr));
  1779. } else {
  1780. np = inet6_sk(sk);
  1781. len = sprintf(buf,
  1782. "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
  1783. NIP6(np->daddr));
  1784. }
  1785. mutex_unlock(&conn->xmitmutex);
  1786. break;
  1787. default:
  1788. return iscsi_conn_get_param(cls_conn, param, buf);
  1789. }
  1790. return len;
  1791. }
  1792. static void
  1793. iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
  1794. {
  1795. struct iscsi_conn *conn = cls_conn->dd_data;
  1796. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1797. stats->txdata_octets = conn->txdata_octets;
  1798. stats->rxdata_octets = conn->rxdata_octets;
  1799. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1800. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1801. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1802. stats->datain_pdus = conn->datain_pdus_cnt;
  1803. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1804. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1805. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1806. stats->custom_length = 3;
  1807. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  1808. stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
  1809. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  1810. stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
  1811. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  1812. stats->custom[2].value = conn->eh_abort_cnt;
  1813. }
  1814. static struct iscsi_cls_session *
  1815. iscsi_tcp_session_create(struct iscsi_transport *iscsit,
  1816. struct scsi_transport_template *scsit,
  1817. uint32_t initial_cmdsn, uint32_t *hostno)
  1818. {
  1819. struct iscsi_cls_session *cls_session;
  1820. struct iscsi_session *session;
  1821. uint32_t hn;
  1822. int cmd_i;
  1823. cls_session = iscsi_session_setup(iscsit, scsit,
  1824. sizeof(struct iscsi_tcp_cmd_task),
  1825. sizeof(struct iscsi_tcp_mgmt_task),
  1826. initial_cmdsn, &hn);
  1827. if (!cls_session)
  1828. return NULL;
  1829. *hostno = hn;
  1830. session = class_to_transport_session(cls_session);
  1831. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1832. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1833. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1834. ctask->hdr = &tcp_ctask->hdr;
  1835. }
  1836. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1837. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1838. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1839. mtask->hdr = &tcp_mtask->hdr;
  1840. }
  1841. if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
  1842. goto r2tpool_alloc_fail;
  1843. return cls_session;
  1844. r2tpool_alloc_fail:
  1845. iscsi_session_teardown(cls_session);
  1846. return NULL;
  1847. }
  1848. static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  1849. {
  1850. iscsi_r2tpool_free(class_to_transport_session(cls_session));
  1851. iscsi_session_teardown(cls_session);
  1852. }
  1853. static struct scsi_host_template iscsi_sht = {
  1854. .name = "iSCSI Initiator over TCP/IP",
  1855. .queuecommand = iscsi_queuecommand,
  1856. .change_queue_depth = iscsi_change_queue_depth,
  1857. .can_queue = ISCSI_XMIT_CMDS_MAX - 1,
  1858. .sg_tablesize = ISCSI_SG_TABLESIZE,
  1859. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  1860. .eh_abort_handler = iscsi_eh_abort,
  1861. .eh_host_reset_handler = iscsi_eh_host_reset,
  1862. .use_clustering = DISABLE_CLUSTERING,
  1863. .proc_name = "iscsi_tcp",
  1864. .this_id = -1,
  1865. };
  1866. static struct iscsi_transport iscsi_tcp_transport = {
  1867. .owner = THIS_MODULE,
  1868. .name = "tcp",
  1869. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  1870. | CAP_DATADGST,
  1871. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  1872. ISCSI_MAX_XMIT_DLENGTH |
  1873. ISCSI_HDRDGST_EN |
  1874. ISCSI_DATADGST_EN |
  1875. ISCSI_INITIAL_R2T_EN |
  1876. ISCSI_MAX_R2T |
  1877. ISCSI_IMM_DATA_EN |
  1878. ISCSI_FIRST_BURST |
  1879. ISCSI_MAX_BURST |
  1880. ISCSI_PDU_INORDER_EN |
  1881. ISCSI_DATASEQ_INORDER_EN |
  1882. ISCSI_ERL |
  1883. ISCSI_CONN_PORT |
  1884. ISCSI_CONN_ADDRESS |
  1885. ISCSI_EXP_STATSN |
  1886. ISCSI_PERSISTENT_PORT |
  1887. ISCSI_PERSISTENT_ADDRESS |
  1888. ISCSI_TARGET_NAME |
  1889. ISCSI_TPGT,
  1890. .host_template = &iscsi_sht,
  1891. .conndata_size = sizeof(struct iscsi_conn),
  1892. .max_conn = 1,
  1893. .max_cmd_len = ISCSI_TCP_MAX_CMD_LEN,
  1894. /* session management */
  1895. .create_session = iscsi_tcp_session_create,
  1896. .destroy_session = iscsi_tcp_session_destroy,
  1897. /* connection management */
  1898. .create_conn = iscsi_tcp_conn_create,
  1899. .bind_conn = iscsi_tcp_conn_bind,
  1900. .destroy_conn = iscsi_tcp_conn_destroy,
  1901. .set_param = iscsi_conn_set_param,
  1902. .get_conn_param = iscsi_tcp_conn_get_param,
  1903. .get_session_param = iscsi_session_get_param,
  1904. .start_conn = iscsi_conn_start,
  1905. .stop_conn = iscsi_tcp_conn_stop,
  1906. /* IO */
  1907. .send_pdu = iscsi_conn_send_pdu,
  1908. .get_stats = iscsi_conn_get_stats,
  1909. .init_cmd_task = iscsi_tcp_cmd_init,
  1910. .init_mgmt_task = iscsi_tcp_mgmt_init,
  1911. .xmit_cmd_task = iscsi_tcp_ctask_xmit,
  1912. .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
  1913. .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
  1914. /* recovery */
  1915. .session_recovery_timedout = iscsi_session_recovery_timedout,
  1916. };
  1917. static int __init
  1918. iscsi_tcp_init(void)
  1919. {
  1920. if (iscsi_max_lun < 1) {
  1921. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  1922. iscsi_max_lun);
  1923. return -EINVAL;
  1924. }
  1925. iscsi_tcp_transport.max_lun = iscsi_max_lun;
  1926. if (!iscsi_register_transport(&iscsi_tcp_transport))
  1927. return -ENODEV;
  1928. return 0;
  1929. }
  1930. static void __exit
  1931. iscsi_tcp_exit(void)
  1932. {
  1933. iscsi_unregister_transport(&iscsi_tcp_transport);
  1934. }
  1935. module_init(iscsi_tcp_init);
  1936. module_exit(iscsi_tcp_exit);