iscsi_tcp.c 61 KB

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