iscsi_tcp.c 64 KB

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