iscsi_tcp.c 60 KB

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