iscsi_tcp.c 64 KB

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