iscsi_tcp.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456
  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_ctask_cleanup(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  165. {
  166. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  167. struct scsi_cmnd *sc;
  168. sc = ctask->sc;
  169. if (unlikely(!sc))
  170. return;
  171. tcp_ctask->xmstate = XMSTATE_IDLE;
  172. tcp_ctask->r2t = NULL;
  173. }
  174. /**
  175. * iscsi_data_rsp - SCSI Data-In Response processing
  176. * @conn: iscsi connection
  177. * @ctask: scsi command task
  178. **/
  179. static int
  180. iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  181. {
  182. int rc;
  183. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  184. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  185. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
  186. struct iscsi_session *session = conn->session;
  187. int datasn = be32_to_cpu(rhdr->datasn);
  188. rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
  189. if (rc)
  190. return rc;
  191. /*
  192. * setup Data-In byte counter (gets decremented..)
  193. */
  194. ctask->data_count = tcp_conn->in.datalen;
  195. if (tcp_conn->in.datalen == 0)
  196. return 0;
  197. if (ctask->datasn != datasn)
  198. return ISCSI_ERR_DATASN;
  199. ctask->datasn++;
  200. tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
  201. if (tcp_ctask->data_offset + tcp_conn->in.datalen > ctask->total_length)
  202. return ISCSI_ERR_DATA_OFFSET;
  203. if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
  204. struct scsi_cmnd *sc = ctask->sc;
  205. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  206. if (rhdr->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
  207. int res_count = be32_to_cpu(rhdr->residual_count);
  208. if (res_count > 0 &&
  209. res_count <= sc->request_bufflen) {
  210. sc->resid = res_count;
  211. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  212. } else
  213. sc->result = (DID_BAD_TARGET << 16) |
  214. rhdr->cmd_status;
  215. } else if (rhdr->flags & ISCSI_FLAG_DATA_OVERFLOW) {
  216. sc->resid = be32_to_cpu(rhdr->residual_count);
  217. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  218. } else
  219. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  220. }
  221. conn->datain_pdus_cnt++;
  222. return 0;
  223. }
  224. /**
  225. * iscsi_solicit_data_init - initialize first Data-Out
  226. * @conn: iscsi connection
  227. * @ctask: scsi command task
  228. * @r2t: R2T info
  229. *
  230. * Notes:
  231. * Initialize first Data-Out within this R2T sequence and finds
  232. * proper data_offset within this SCSI command.
  233. *
  234. * This function is called with connection lock taken.
  235. **/
  236. static void
  237. iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  238. struct iscsi_r2t_info *r2t)
  239. {
  240. struct iscsi_data *hdr;
  241. struct scsi_cmnd *sc = ctask->sc;
  242. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  243. hdr = &r2t->dtask.hdr;
  244. memset(hdr, 0, sizeof(struct iscsi_data));
  245. hdr->ttt = r2t->ttt;
  246. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  247. r2t->solicit_datasn++;
  248. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  249. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  250. hdr->itt = ctask->hdr->itt;
  251. hdr->exp_statsn = r2t->exp_statsn;
  252. hdr->offset = cpu_to_be32(r2t->data_offset);
  253. if (r2t->data_length > conn->max_xmit_dlength) {
  254. hton24(hdr->dlength, conn->max_xmit_dlength);
  255. r2t->data_count = conn->max_xmit_dlength;
  256. hdr->flags = 0;
  257. } else {
  258. hton24(hdr->dlength, r2t->data_length);
  259. r2t->data_count = r2t->data_length;
  260. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  261. }
  262. conn->dataout_pdus_cnt++;
  263. r2t->sent = 0;
  264. iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
  265. sizeof(struct iscsi_hdr));
  266. if (sc->use_sg) {
  267. int i, sg_count = 0;
  268. struct scatterlist *sg = sc->request_buffer;
  269. r2t->sg = NULL;
  270. for (i = 0; i < sc->use_sg; i++, sg += 1) {
  271. /* FIXME: prefetch ? */
  272. if (sg_count + sg->length > r2t->data_offset) {
  273. int page_offset;
  274. /* sg page found! */
  275. /* offset within this page */
  276. page_offset = r2t->data_offset - sg_count;
  277. /* fill in this buffer */
  278. iscsi_buf_init_sg(&r2t->sendbuf, sg);
  279. r2t->sendbuf.sg.offset += page_offset;
  280. r2t->sendbuf.sg.length -= page_offset;
  281. /* xmit logic will continue with next one */
  282. r2t->sg = sg + 1;
  283. break;
  284. }
  285. sg_count += sg->length;
  286. }
  287. BUG_ON(r2t->sg == NULL);
  288. } else
  289. iscsi_buf_init_iov(&tcp_ctask->sendbuf,
  290. (char*)sc->request_buffer + r2t->data_offset,
  291. r2t->data_count);
  292. }
  293. /**
  294. * iscsi_r2t_rsp - iSCSI R2T Response processing
  295. * @conn: iscsi connection
  296. * @ctask: scsi command task
  297. **/
  298. static int
  299. iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  300. {
  301. struct iscsi_r2t_info *r2t;
  302. struct iscsi_session *session = conn->session;
  303. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  304. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  305. struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
  306. int r2tsn = be32_to_cpu(rhdr->r2tsn);
  307. int rc;
  308. if (tcp_conn->in.datalen)
  309. return ISCSI_ERR_DATALEN;
  310. if (tcp_ctask->exp_r2tsn && tcp_ctask->exp_r2tsn != r2tsn)
  311. return ISCSI_ERR_R2TSN;
  312. rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
  313. if (rc)
  314. return rc;
  315. /* FIXME: use R2TSN to detect missing R2T */
  316. /* fill-in new R2T associated with the task */
  317. spin_lock(&session->lock);
  318. if (!ctask->sc || ctask->mtask ||
  319. session->state != ISCSI_STATE_LOGGED_IN) {
  320. printk(KERN_INFO "iscsi_tcp: dropping R2T itt %d in "
  321. "recovery...\n", ctask->itt);
  322. spin_unlock(&session->lock);
  323. return 0;
  324. }
  325. rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
  326. BUG_ON(!rc);
  327. r2t->exp_statsn = rhdr->statsn;
  328. r2t->data_length = be32_to_cpu(rhdr->data_length);
  329. if (r2t->data_length == 0 ||
  330. r2t->data_length > session->max_burst) {
  331. spin_unlock(&session->lock);
  332. return ISCSI_ERR_DATALEN;
  333. }
  334. r2t->data_offset = be32_to_cpu(rhdr->data_offset);
  335. if (r2t->data_offset + r2t->data_length > ctask->total_length) {
  336. spin_unlock(&session->lock);
  337. return ISCSI_ERR_DATALEN;
  338. }
  339. r2t->ttt = rhdr->ttt; /* no flip */
  340. r2t->solicit_datasn = 0;
  341. iscsi_solicit_data_init(conn, ctask, r2t);
  342. tcp_ctask->exp_r2tsn = r2tsn + 1;
  343. tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
  344. __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
  345. __kfifo_put(conn->xmitqueue, (void*)&ctask, sizeof(void*));
  346. scsi_queue_work(session->host, &conn->xmitwork);
  347. conn->r2t_pdus_cnt++;
  348. spin_unlock(&session->lock);
  349. return 0;
  350. }
  351. static int
  352. iscsi_tcp_hdr_recv(struct iscsi_conn *conn)
  353. {
  354. int rc = 0, opcode, ahslen;
  355. struct iscsi_hdr *hdr;
  356. struct iscsi_session *session = conn->session;
  357. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  358. uint32_t cdgst, rdgst = 0, itt;
  359. hdr = tcp_conn->in.hdr;
  360. /* verify PDU length */
  361. tcp_conn->in.datalen = ntoh24(hdr->dlength);
  362. if (tcp_conn->in.datalen > conn->max_recv_dlength) {
  363. printk(KERN_ERR "iscsi_tcp: datalen %d > %d\n",
  364. tcp_conn->in.datalen, conn->max_recv_dlength);
  365. return ISCSI_ERR_DATALEN;
  366. }
  367. tcp_conn->data_copied = 0;
  368. /* read AHS */
  369. ahslen = hdr->hlength << 2;
  370. tcp_conn->in.offset += ahslen;
  371. tcp_conn->in.copy -= ahslen;
  372. if (tcp_conn->in.copy < 0) {
  373. printk(KERN_ERR "iscsi_tcp: can't handle AHS with length "
  374. "%d bytes\n", ahslen);
  375. return ISCSI_ERR_AHSLEN;
  376. }
  377. /* calculate read padding */
  378. tcp_conn->in.padding = tcp_conn->in.datalen & (ISCSI_PAD_LEN-1);
  379. if (tcp_conn->in.padding) {
  380. tcp_conn->in.padding = ISCSI_PAD_LEN - tcp_conn->in.padding;
  381. debug_scsi("read padding %d bytes\n", tcp_conn->in.padding);
  382. }
  383. if (conn->hdrdgst_en) {
  384. struct scatterlist sg;
  385. sg_init_one(&sg, (u8 *)hdr,
  386. sizeof(struct iscsi_hdr) + ahslen);
  387. crypto_digest_digest(tcp_conn->rx_tfm, &sg, 1, (u8 *)&cdgst);
  388. rdgst = *(uint32_t*)((char*)hdr + sizeof(struct iscsi_hdr) +
  389. ahslen);
  390. if (cdgst != rdgst) {
  391. printk(KERN_ERR "iscsi_tcp: hdrdgst error "
  392. "recv 0x%x calc 0x%x\n", rdgst, cdgst);
  393. return ISCSI_ERR_HDR_DGST;
  394. }
  395. }
  396. opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  397. /* verify itt (itt encoding: age+cid+itt) */
  398. rc = iscsi_verify_itt(conn, hdr, &itt);
  399. if (rc == ISCSI_ERR_NO_SCSI_CMD) {
  400. tcp_conn->in.datalen = 0; /* force drop */
  401. return 0;
  402. } else if (rc)
  403. return rc;
  404. debug_tcp("opcode 0x%x offset %d copy %d ahslen %d datalen %d\n",
  405. opcode, tcp_conn->in.offset, tcp_conn->in.copy,
  406. ahslen, tcp_conn->in.datalen);
  407. switch(opcode) {
  408. case ISCSI_OP_SCSI_DATA_IN:
  409. tcp_conn->in.ctask = session->cmds[itt];
  410. rc = iscsi_data_rsp(conn, tcp_conn->in.ctask);
  411. /* fall through */
  412. case ISCSI_OP_SCSI_CMD_RSP:
  413. tcp_conn->in.ctask = session->cmds[itt];
  414. if (tcp_conn->in.datalen)
  415. goto copy_hdr;
  416. spin_lock(&session->lock);
  417. __iscsi_ctask_cleanup(conn, tcp_conn->in.ctask);
  418. rc = __iscsi_complete_pdu(conn, hdr, NULL, 0);
  419. spin_unlock(&session->lock);
  420. break;
  421. case ISCSI_OP_R2T:
  422. tcp_conn->in.ctask = session->cmds[itt];
  423. if (ahslen)
  424. rc = ISCSI_ERR_AHSLEN;
  425. else if (tcp_conn->in.ctask->sc->sc_data_direction ==
  426. DMA_TO_DEVICE)
  427. rc = iscsi_r2t_rsp(conn, tcp_conn->in.ctask);
  428. else
  429. rc = ISCSI_ERR_PROTO;
  430. break;
  431. case ISCSI_OP_LOGIN_RSP:
  432. case ISCSI_OP_TEXT_RSP:
  433. case ISCSI_OP_LOGOUT_RSP:
  434. case ISCSI_OP_NOOP_IN:
  435. case ISCSI_OP_REJECT:
  436. case ISCSI_OP_ASYNC_EVENT:
  437. if (tcp_conn->in.datalen)
  438. goto copy_hdr;
  439. /* fall through */
  440. case ISCSI_OP_SCSI_TMFUNC_RSP:
  441. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  442. break;
  443. default:
  444. rc = ISCSI_ERR_BAD_OPCODE;
  445. break;
  446. }
  447. return rc;
  448. copy_hdr:
  449. /*
  450. * if we did zero copy for the header but we will need multiple
  451. * skbs to complete the command then we have to copy the header
  452. * for later use
  453. */
  454. if (tcp_conn->in.zero_copy_hdr && tcp_conn->in.copy <
  455. (tcp_conn->in.datalen + tcp_conn->in.padding +
  456. (conn->datadgst_en ? 4 : 0))) {
  457. debug_tcp("Copying header for later use. in.copy %d in.datalen"
  458. " %d\n", tcp_conn->in.copy, tcp_conn->in.datalen);
  459. memcpy(&tcp_conn->hdr, tcp_conn->in.hdr,
  460. sizeof(struct iscsi_hdr));
  461. tcp_conn->in.hdr = &tcp_conn->hdr;
  462. tcp_conn->in.zero_copy_hdr = 0;
  463. }
  464. return 0;
  465. }
  466. /**
  467. * iscsi_ctask_copy - copy skb bits to the destanation cmd task
  468. * @conn: iscsi tcp connection
  469. * @ctask: scsi command task
  470. * @buf: buffer to copy to
  471. * @buf_size: size of buffer
  472. * @offset: offset within the buffer
  473. *
  474. * Notes:
  475. * The function calls skb_copy_bits() and updates per-connection and
  476. * per-cmd byte counters.
  477. *
  478. * Read counters (in bytes):
  479. *
  480. * conn->in.offset offset within in progress SKB
  481. * conn->in.copy left to copy from in progress SKB
  482. * including padding
  483. * conn->in.copied copied already from in progress SKB
  484. * conn->data_copied copied already from in progress buffer
  485. * ctask->sent total bytes sent up to the MidLayer
  486. * ctask->data_count left to copy from in progress Data-In
  487. * buf_left left to copy from in progress buffer
  488. **/
  489. static inline int
  490. iscsi_ctask_copy(struct iscsi_tcp_conn *tcp_conn, struct iscsi_cmd_task *ctask,
  491. void *buf, int buf_size, int offset)
  492. {
  493. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  494. int buf_left = buf_size - (tcp_conn->data_copied + offset);
  495. int size = min(tcp_conn->in.copy, buf_left);
  496. int rc;
  497. size = min(size, ctask->data_count);
  498. debug_tcp("ctask_copy %d bytes at offset %d copied %d\n",
  499. size, tcp_conn->in.offset, tcp_conn->in.copied);
  500. BUG_ON(size <= 0);
  501. BUG_ON(tcp_ctask->sent + size > ctask->total_length);
  502. rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
  503. (char*)buf + (offset + tcp_conn->data_copied), size);
  504. /* must fit into skb->len */
  505. BUG_ON(rc);
  506. tcp_conn->in.offset += size;
  507. tcp_conn->in.copy -= size;
  508. tcp_conn->in.copied += size;
  509. tcp_conn->data_copied += size;
  510. tcp_ctask->sent += size;
  511. ctask->data_count -= size;
  512. BUG_ON(tcp_conn->in.copy < 0);
  513. BUG_ON(ctask->data_count < 0);
  514. if (buf_size != (tcp_conn->data_copied + offset)) {
  515. if (!ctask->data_count) {
  516. BUG_ON(buf_size - tcp_conn->data_copied < 0);
  517. /* done with this PDU */
  518. return buf_size - tcp_conn->data_copied;
  519. }
  520. return -EAGAIN;
  521. }
  522. /* done with this buffer or with both - PDU and buffer */
  523. tcp_conn->data_copied = 0;
  524. return 0;
  525. }
  526. /**
  527. * iscsi_tcp_copy - copy skb bits to the destanation buffer
  528. * @conn: iscsi tcp connection
  529. *
  530. * Notes:
  531. * The function calls skb_copy_bits() and updates per-connection
  532. * byte counters.
  533. **/
  534. static inline int
  535. iscsi_tcp_copy(struct iscsi_tcp_conn *tcp_conn)
  536. {
  537. void *buf = tcp_conn->data;
  538. int buf_size = tcp_conn->in.datalen;
  539. int buf_left = buf_size - tcp_conn->data_copied;
  540. int size = min(tcp_conn->in.copy, buf_left);
  541. int rc;
  542. debug_tcp("tcp_copy %d bytes at offset %d copied %d\n",
  543. size, tcp_conn->in.offset, tcp_conn->data_copied);
  544. BUG_ON(size <= 0);
  545. rc = skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
  546. (char*)buf + tcp_conn->data_copied, size);
  547. BUG_ON(rc);
  548. tcp_conn->in.offset += size;
  549. tcp_conn->in.copy -= size;
  550. tcp_conn->in.copied += size;
  551. tcp_conn->data_copied += size;
  552. if (buf_size != tcp_conn->data_copied)
  553. return -EAGAIN;
  554. return 0;
  555. }
  556. static inline void
  557. partial_sg_digest_update(struct iscsi_tcp_conn *tcp_conn,
  558. struct scatterlist *sg, int offset, int length)
  559. {
  560. struct scatterlist temp;
  561. memcpy(&temp, sg, sizeof(struct scatterlist));
  562. temp.offset = offset;
  563. temp.length = length;
  564. crypto_digest_update(tcp_conn->data_rx_tfm, &temp, 1);
  565. }
  566. static void
  567. iscsi_recv_digest_update(struct iscsi_tcp_conn *tcp_conn, char* buf, int len)
  568. {
  569. struct scatterlist tmp;
  570. sg_init_one(&tmp, buf, len);
  571. crypto_digest_update(tcp_conn->data_rx_tfm, &tmp, 1);
  572. }
  573. static int iscsi_scsi_data_in(struct iscsi_conn *conn)
  574. {
  575. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  576. struct iscsi_cmd_task *ctask = tcp_conn->in.ctask;
  577. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  578. struct scsi_cmnd *sc = ctask->sc;
  579. struct scatterlist *sg;
  580. int i, offset, rc = 0;
  581. BUG_ON((void*)ctask != sc->SCp.ptr);
  582. /*
  583. * copying Data-In into the Scsi_Cmnd
  584. */
  585. if (!sc->use_sg) {
  586. i = ctask->data_count;
  587. rc = iscsi_ctask_copy(tcp_conn, ctask, sc->request_buffer,
  588. sc->request_bufflen,
  589. tcp_ctask->data_offset);
  590. if (rc == -EAGAIN)
  591. return rc;
  592. if (conn->datadgst_en)
  593. iscsi_recv_digest_update(tcp_conn, sc->request_buffer,
  594. i);
  595. rc = 0;
  596. goto done;
  597. }
  598. offset = tcp_ctask->data_offset;
  599. sg = sc->request_buffer;
  600. if (tcp_ctask->data_offset)
  601. for (i = 0; i < tcp_ctask->sg_count; i++)
  602. offset -= sg[i].length;
  603. /* we've passed through partial sg*/
  604. if (offset < 0)
  605. offset = 0;
  606. for (i = tcp_ctask->sg_count; i < sc->use_sg; i++) {
  607. char *dest;
  608. dest = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
  609. rc = iscsi_ctask_copy(tcp_conn, ctask, dest + sg[i].offset,
  610. sg[i].length, offset);
  611. kunmap_atomic(dest, KM_SOFTIRQ0);
  612. if (rc == -EAGAIN)
  613. /* continue with the next SKB/PDU */
  614. return rc;
  615. if (!rc) {
  616. if (conn->datadgst_en) {
  617. if (!offset)
  618. crypto_digest_update(
  619. tcp_conn->data_rx_tfm,
  620. &sg[i], 1);
  621. else
  622. partial_sg_digest_update(tcp_conn,
  623. &sg[i],
  624. sg[i].offset + offset,
  625. sg[i].length - offset);
  626. }
  627. offset = 0;
  628. tcp_ctask->sg_count++;
  629. }
  630. if (!ctask->data_count) {
  631. if (rc && conn->datadgst_en)
  632. /*
  633. * data-in is complete, but buffer not...
  634. */
  635. partial_sg_digest_update(tcp_conn, &sg[i],
  636. sg[i].offset, sg[i].length-rc);
  637. rc = 0;
  638. break;
  639. }
  640. if (!tcp_conn->in.copy)
  641. return -EAGAIN;
  642. }
  643. BUG_ON(ctask->data_count);
  644. done:
  645. /* check for non-exceptional status */
  646. if (tcp_conn->in.hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  647. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  648. (long)sc, sc->result, ctask->itt);
  649. spin_lock(&conn->session->lock);
  650. __iscsi_ctask_cleanup(conn, ctask);
  651. __iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
  652. spin_unlock(&conn->session->lock);
  653. }
  654. return rc;
  655. }
  656. static int
  657. iscsi_data_recv(struct iscsi_conn *conn)
  658. {
  659. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  660. int rc = 0, opcode;
  661. opcode = tcp_conn->in.hdr->opcode & ISCSI_OPCODE_MASK;
  662. switch (opcode) {
  663. case ISCSI_OP_SCSI_DATA_IN:
  664. rc = iscsi_scsi_data_in(conn);
  665. break;
  666. case ISCSI_OP_SCSI_CMD_RSP:
  667. spin_lock(&conn->session->lock);
  668. __iscsi_ctask_cleanup(conn, tcp_conn->in.ctask);
  669. spin_unlock(&conn->session->lock);
  670. case ISCSI_OP_TEXT_RSP:
  671. case ISCSI_OP_LOGIN_RSP:
  672. case ISCSI_OP_NOOP_IN:
  673. case ISCSI_OP_ASYNC_EVENT:
  674. case ISCSI_OP_REJECT:
  675. /*
  676. * Collect data segment to the connection's data
  677. * placeholder
  678. */
  679. if (iscsi_tcp_copy(tcp_conn)) {
  680. rc = -EAGAIN;
  681. goto exit;
  682. }
  683. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, tcp_conn->data,
  684. tcp_conn->in.datalen);
  685. if (!rc && conn->datadgst_en && opcode != ISCSI_OP_LOGIN_RSP)
  686. iscsi_recv_digest_update(tcp_conn, tcp_conn->data,
  687. tcp_conn->in.datalen);
  688. break;
  689. default:
  690. BUG_ON(1);
  691. }
  692. exit:
  693. return rc;
  694. }
  695. /**
  696. * iscsi_tcp_data_recv - TCP receive in sendfile fashion
  697. * @rd_desc: read descriptor
  698. * @skb: socket buffer
  699. * @offset: offset in skb
  700. * @len: skb->len - offset
  701. **/
  702. static int
  703. iscsi_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  704. unsigned int offset, size_t len)
  705. {
  706. int rc;
  707. struct iscsi_conn *conn = rd_desc->arg.data;
  708. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  709. int processed;
  710. char pad[ISCSI_PAD_LEN];
  711. struct scatterlist sg;
  712. /*
  713. * Save current SKB and its offset in the corresponding
  714. * connection context.
  715. */
  716. tcp_conn->in.copy = skb->len - offset;
  717. tcp_conn->in.offset = offset;
  718. tcp_conn->in.skb = skb;
  719. tcp_conn->in.len = tcp_conn->in.copy;
  720. BUG_ON(tcp_conn->in.copy <= 0);
  721. debug_tcp("in %d bytes\n", tcp_conn->in.copy);
  722. more:
  723. tcp_conn->in.copied = 0;
  724. rc = 0;
  725. if (unlikely(conn->suspend_rx)) {
  726. debug_tcp("conn %d Rx suspended!\n", conn->id);
  727. return 0;
  728. }
  729. if (tcp_conn->in_progress == IN_PROGRESS_WAIT_HEADER ||
  730. tcp_conn->in_progress == IN_PROGRESS_HEADER_GATHER) {
  731. rc = iscsi_hdr_extract(tcp_conn);
  732. if (rc) {
  733. if (rc == -EAGAIN)
  734. goto nomore;
  735. else {
  736. iscsi_conn_failure(conn, rc);
  737. return 0;
  738. }
  739. }
  740. /*
  741. * Verify and process incoming PDU header.
  742. */
  743. rc = iscsi_tcp_hdr_recv(conn);
  744. if (!rc && tcp_conn->in.datalen) {
  745. if (conn->datadgst_en) {
  746. BUG_ON(!tcp_conn->data_rx_tfm);
  747. crypto_digest_init(tcp_conn->data_rx_tfm);
  748. }
  749. tcp_conn->in_progress = IN_PROGRESS_DATA_RECV;
  750. } else if (rc) {
  751. iscsi_conn_failure(conn, rc);
  752. return 0;
  753. }
  754. }
  755. if (tcp_conn->in_progress == IN_PROGRESS_DDIGEST_RECV) {
  756. uint32_t recv_digest;
  757. debug_tcp("extra data_recv offset %d copy %d\n",
  758. tcp_conn->in.offset, tcp_conn->in.copy);
  759. skb_copy_bits(tcp_conn->in.skb, tcp_conn->in.offset,
  760. &recv_digest, 4);
  761. tcp_conn->in.offset += 4;
  762. tcp_conn->in.copy -= 4;
  763. if (recv_digest != tcp_conn->in.datadgst) {
  764. debug_tcp("iscsi_tcp: data digest error!"
  765. "0x%x != 0x%x\n", recv_digest,
  766. tcp_conn->in.datadgst);
  767. iscsi_conn_failure(conn, ISCSI_ERR_DATA_DGST);
  768. return 0;
  769. } else {
  770. debug_tcp("iscsi_tcp: data digest match!"
  771. "0x%x == 0x%x\n", recv_digest,
  772. tcp_conn->in.datadgst);
  773. tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
  774. }
  775. }
  776. if (tcp_conn->in_progress == IN_PROGRESS_DATA_RECV &&
  777. tcp_conn->in.copy) {
  778. debug_tcp("data_recv offset %d copy %d\n",
  779. tcp_conn->in.offset, tcp_conn->in.copy);
  780. rc = iscsi_data_recv(conn);
  781. if (rc) {
  782. if (rc == -EAGAIN)
  783. goto again;
  784. iscsi_conn_failure(conn, rc);
  785. return 0;
  786. }
  787. tcp_conn->in.copy -= tcp_conn->in.padding;
  788. tcp_conn->in.offset += tcp_conn->in.padding;
  789. if (conn->datadgst_en) {
  790. if (tcp_conn->in.padding) {
  791. debug_tcp("padding -> %d\n",
  792. tcp_conn->in.padding);
  793. memset(pad, 0, tcp_conn->in.padding);
  794. sg_init_one(&sg, pad, tcp_conn->in.padding);
  795. crypto_digest_update(tcp_conn->data_rx_tfm,
  796. &sg, 1);
  797. }
  798. crypto_digest_final(tcp_conn->data_rx_tfm,
  799. (u8 *) & tcp_conn->in.datadgst);
  800. debug_tcp("rx digest 0x%x\n", tcp_conn->in.datadgst);
  801. tcp_conn->in_progress = IN_PROGRESS_DDIGEST_RECV;
  802. } else
  803. tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
  804. }
  805. debug_tcp("f, processed %d from out of %d padding %d\n",
  806. tcp_conn->in.offset - offset, (int)len, tcp_conn->in.padding);
  807. BUG_ON(tcp_conn->in.offset - offset > len);
  808. if (tcp_conn->in.offset - offset != len) {
  809. debug_tcp("continue to process %d bytes\n",
  810. (int)len - (tcp_conn->in.offset - offset));
  811. goto more;
  812. }
  813. nomore:
  814. processed = tcp_conn->in.offset - offset;
  815. BUG_ON(processed == 0);
  816. return processed;
  817. again:
  818. processed = tcp_conn->in.offset - offset;
  819. debug_tcp("c, processed %d from out of %d rd_desc_cnt %d\n",
  820. processed, (int)len, (int)rd_desc->count);
  821. BUG_ON(processed == 0);
  822. BUG_ON(processed > len);
  823. conn->rxdata_octets += processed;
  824. return processed;
  825. }
  826. static void
  827. iscsi_tcp_data_ready(struct sock *sk, int flag)
  828. {
  829. struct iscsi_conn *conn = sk->sk_user_data;
  830. read_descriptor_t rd_desc;
  831. read_lock(&sk->sk_callback_lock);
  832. /*
  833. * Use rd_desc to pass 'conn' to iscsi_tcp_data_recv.
  834. * We set count to 1 because we want the network layer to
  835. * hand us all the skbs that are available. iscsi_tcp_data_recv
  836. * handled pdus that cross buffers or pdus that still need data.
  837. */
  838. rd_desc.arg.data = conn;
  839. rd_desc.count = 1;
  840. tcp_read_sock(sk, &rd_desc, iscsi_tcp_data_recv);
  841. read_unlock(&sk->sk_callback_lock);
  842. }
  843. static void
  844. iscsi_tcp_state_change(struct sock *sk)
  845. {
  846. struct iscsi_tcp_conn *tcp_conn;
  847. struct iscsi_conn *conn;
  848. struct iscsi_session *session;
  849. void (*old_state_change)(struct sock *);
  850. read_lock(&sk->sk_callback_lock);
  851. conn = (struct iscsi_conn*)sk->sk_user_data;
  852. session = conn->session;
  853. if ((sk->sk_state == TCP_CLOSE_WAIT ||
  854. sk->sk_state == TCP_CLOSE) &&
  855. !atomic_read(&sk->sk_rmem_alloc)) {
  856. debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
  857. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  858. }
  859. tcp_conn = conn->dd_data;
  860. old_state_change = tcp_conn->old_state_change;
  861. read_unlock(&sk->sk_callback_lock);
  862. old_state_change(sk);
  863. }
  864. /**
  865. * iscsi_write_space - Called when more output buffer space is available
  866. * @sk: socket space is available for
  867. **/
  868. static void
  869. iscsi_write_space(struct sock *sk)
  870. {
  871. struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
  872. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  873. tcp_conn->old_write_space(sk);
  874. debug_tcp("iscsi_write_space: cid %d\n", conn->id);
  875. scsi_queue_work(conn->session->host, &conn->xmitwork);
  876. }
  877. static void
  878. iscsi_conn_set_callbacks(struct iscsi_conn *conn)
  879. {
  880. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  881. struct sock *sk = tcp_conn->sock->sk;
  882. /* assign new callbacks */
  883. write_lock_bh(&sk->sk_callback_lock);
  884. sk->sk_user_data = conn;
  885. tcp_conn->old_data_ready = sk->sk_data_ready;
  886. tcp_conn->old_state_change = sk->sk_state_change;
  887. tcp_conn->old_write_space = sk->sk_write_space;
  888. sk->sk_data_ready = iscsi_tcp_data_ready;
  889. sk->sk_state_change = iscsi_tcp_state_change;
  890. sk->sk_write_space = iscsi_write_space;
  891. write_unlock_bh(&sk->sk_callback_lock);
  892. }
  893. static void
  894. iscsi_conn_restore_callbacks(struct iscsi_conn *conn)
  895. {
  896. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  897. struct sock *sk = tcp_conn->sock->sk;
  898. /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
  899. write_lock_bh(&sk->sk_callback_lock);
  900. sk->sk_user_data = NULL;
  901. sk->sk_data_ready = tcp_conn->old_data_ready;
  902. sk->sk_state_change = tcp_conn->old_state_change;
  903. sk->sk_write_space = tcp_conn->old_write_space;
  904. sk->sk_no_check = 0;
  905. write_unlock_bh(&sk->sk_callback_lock);
  906. }
  907. /**
  908. * iscsi_send - generic send routine
  909. * @sk: kernel's socket
  910. * @buf: buffer to write from
  911. * @size: actual size to write
  912. * @flags: socket's flags
  913. */
  914. static inline int
  915. iscsi_send(struct iscsi_conn *conn, struct iscsi_buf *buf, int size, int flags)
  916. {
  917. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  918. struct socket *sk = tcp_conn->sock;
  919. int offset = buf->sg.offset + buf->sent, res;
  920. /*
  921. * if we got use_sg=0 or are sending something we kmallocd
  922. * then we did not have to do kmap (kmap returns page_address)
  923. *
  924. * if we got use_sg > 0, but had to drop down, we do not
  925. * set clustering so this should only happen for that
  926. * slab case.
  927. */
  928. if (buf->use_sendmsg)
  929. res = sock_no_sendpage(sk, buf->sg.page, offset, size, flags);
  930. else
  931. res = tcp_conn->sendpage(sk, buf->sg.page, offset, size, flags);
  932. if (res >= 0) {
  933. conn->txdata_octets += res;
  934. buf->sent += res;
  935. return res;
  936. }
  937. tcp_conn->sendpage_failures_cnt++;
  938. if (res == -EAGAIN)
  939. res = -ENOBUFS;
  940. else
  941. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  942. return res;
  943. }
  944. /**
  945. * iscsi_sendhdr - send PDU Header via tcp_sendpage()
  946. * @conn: iscsi connection
  947. * @buf: buffer to write from
  948. * @datalen: lenght of data to be sent after the header
  949. *
  950. * Notes:
  951. * (Tx, Fast Path)
  952. **/
  953. static inline int
  954. iscsi_sendhdr(struct iscsi_conn *conn, struct iscsi_buf *buf, int datalen)
  955. {
  956. int flags = 0; /* MSG_DONTWAIT; */
  957. int res, size;
  958. size = buf->sg.length - buf->sent;
  959. BUG_ON(buf->sent + size > buf->sg.length);
  960. if (buf->sent + size != buf->sg.length || datalen)
  961. flags |= MSG_MORE;
  962. res = iscsi_send(conn, buf, size, flags);
  963. debug_tcp("sendhdr %d bytes, sent %d res %d\n", size, buf->sent, res);
  964. if (res >= 0) {
  965. if (size != res)
  966. return -EAGAIN;
  967. return 0;
  968. }
  969. return res;
  970. }
  971. /**
  972. * iscsi_sendpage - send one page of iSCSI Data-Out.
  973. * @conn: iscsi connection
  974. * @buf: buffer to write from
  975. * @count: remaining data
  976. * @sent: number of bytes sent
  977. *
  978. * Notes:
  979. * (Tx, Fast Path)
  980. **/
  981. static inline int
  982. iscsi_sendpage(struct iscsi_conn *conn, struct iscsi_buf *buf,
  983. int *count, int *sent)
  984. {
  985. int flags = 0; /* MSG_DONTWAIT; */
  986. int res, size;
  987. size = buf->sg.length - buf->sent;
  988. BUG_ON(buf->sent + size > buf->sg.length);
  989. if (size > *count)
  990. size = *count;
  991. if (buf->sent + size != buf->sg.length || *count != size)
  992. flags |= MSG_MORE;
  993. res = iscsi_send(conn, buf, size, flags);
  994. debug_tcp("sendpage: %d bytes, sent %d left %d sent %d res %d\n",
  995. size, buf->sent, *count, *sent, res);
  996. if (res >= 0) {
  997. *count -= res;
  998. *sent += res;
  999. if (size != res)
  1000. return -EAGAIN;
  1001. return 0;
  1002. }
  1003. return res;
  1004. }
  1005. static inline void
  1006. iscsi_data_digest_init(struct iscsi_tcp_conn *tcp_conn,
  1007. struct iscsi_cmd_task *ctask)
  1008. {
  1009. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1010. BUG_ON(!tcp_conn->data_tx_tfm);
  1011. crypto_digest_init(tcp_conn->data_tx_tfm);
  1012. tcp_ctask->digest_count = 4;
  1013. }
  1014. static int
  1015. iscsi_digest_final_send(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1016. struct iscsi_buf *buf, uint32_t *digest, int final)
  1017. {
  1018. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1019. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1020. int rc = 0;
  1021. int sent = 0;
  1022. if (final)
  1023. crypto_digest_final(tcp_conn->data_tx_tfm, (u8*)digest);
  1024. iscsi_buf_init_iov(buf, (char*)digest, 4);
  1025. rc = iscsi_sendpage(conn, buf, &tcp_ctask->digest_count, &sent);
  1026. if (rc) {
  1027. tcp_ctask->datadigest = *digest;
  1028. tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
  1029. } else
  1030. tcp_ctask->digest_count = 4;
  1031. return rc;
  1032. }
  1033. /**
  1034. * iscsi_solicit_data_cont - initialize next Data-Out
  1035. * @conn: iscsi connection
  1036. * @ctask: scsi command task
  1037. * @r2t: R2T info
  1038. * @left: bytes left to transfer
  1039. *
  1040. * Notes:
  1041. * Initialize next Data-Out within this R2T sequence and continue
  1042. * to process next Scatter-Gather element(if any) of this SCSI command.
  1043. *
  1044. * Called under connection lock.
  1045. **/
  1046. static void
  1047. iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1048. struct iscsi_r2t_info *r2t, int left)
  1049. {
  1050. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1051. struct iscsi_data *hdr;
  1052. struct scsi_cmnd *sc = ctask->sc;
  1053. int new_offset;
  1054. hdr = &r2t->dtask.hdr;
  1055. memset(hdr, 0, sizeof(struct iscsi_data));
  1056. hdr->ttt = r2t->ttt;
  1057. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  1058. r2t->solicit_datasn++;
  1059. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  1060. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1061. hdr->itt = ctask->hdr->itt;
  1062. hdr->exp_statsn = r2t->exp_statsn;
  1063. new_offset = r2t->data_offset + r2t->sent;
  1064. hdr->offset = cpu_to_be32(new_offset);
  1065. if (left > conn->max_xmit_dlength) {
  1066. hton24(hdr->dlength, conn->max_xmit_dlength);
  1067. r2t->data_count = conn->max_xmit_dlength;
  1068. } else {
  1069. hton24(hdr->dlength, left);
  1070. r2t->data_count = left;
  1071. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  1072. }
  1073. conn->dataout_pdus_cnt++;
  1074. iscsi_buf_init_iov(&r2t->headbuf, (char*)hdr,
  1075. sizeof(struct iscsi_hdr));
  1076. if (sc->use_sg && !iscsi_buf_left(&r2t->sendbuf)) {
  1077. BUG_ON(tcp_ctask->bad_sg == r2t->sg);
  1078. iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
  1079. r2t->sg += 1;
  1080. } else
  1081. iscsi_buf_init_iov(&tcp_ctask->sendbuf,
  1082. (char*)sc->request_buffer + new_offset,
  1083. r2t->data_count);
  1084. }
  1085. static void
  1086. iscsi_unsolicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1087. {
  1088. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1089. struct iscsi_data_task *dtask;
  1090. dtask = tcp_ctask->dtask = &tcp_ctask->unsol_dtask;
  1091. iscsi_prep_unsolicit_data_pdu(ctask, &dtask->hdr,
  1092. tcp_ctask->r2t_data_count);
  1093. iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)&dtask->hdr,
  1094. sizeof(struct iscsi_hdr));
  1095. }
  1096. /**
  1097. * iscsi_tcp_cmd_init - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
  1098. * @conn: iscsi connection
  1099. * @ctask: scsi command task
  1100. * @sc: scsi command
  1101. **/
  1102. static void
  1103. iscsi_tcp_cmd_init(struct iscsi_cmd_task *ctask)
  1104. {
  1105. struct scsi_cmnd *sc = ctask->sc;
  1106. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1107. BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
  1108. tcp_ctask->sent = 0;
  1109. tcp_ctask->sg_count = 0;
  1110. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  1111. tcp_ctask->xmstate = XMSTATE_W_HDR;
  1112. tcp_ctask->exp_r2tsn = 0;
  1113. BUG_ON(ctask->total_length == 0);
  1114. if (sc->use_sg) {
  1115. struct scatterlist *sg = sc->request_buffer;
  1116. iscsi_buf_init_sg(&tcp_ctask->sendbuf,
  1117. &sg[tcp_ctask->sg_count++]);
  1118. tcp_ctask->sg = sg;
  1119. tcp_ctask->bad_sg = sg + sc->use_sg;
  1120. } else
  1121. iscsi_buf_init_iov(&tcp_ctask->sendbuf,
  1122. sc->request_buffer,
  1123. sc->request_bufflen);
  1124. if (ctask->imm_count)
  1125. tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
  1126. tcp_ctask->pad_count = ctask->total_length & (ISCSI_PAD_LEN-1);
  1127. if (tcp_ctask->pad_count) {
  1128. tcp_ctask->pad_count = ISCSI_PAD_LEN -
  1129. tcp_ctask->pad_count;
  1130. debug_scsi("write padding %d bytes\n",
  1131. tcp_ctask->pad_count);
  1132. tcp_ctask->xmstate |= XMSTATE_W_PAD;
  1133. }
  1134. if (ctask->unsol_count)
  1135. tcp_ctask->xmstate |= XMSTATE_UNS_HDR |
  1136. XMSTATE_UNS_INIT;
  1137. tcp_ctask->r2t_data_count = ctask->total_length -
  1138. ctask->imm_count -
  1139. ctask->unsol_count;
  1140. debug_scsi("cmd [itt %x total %d imm %d imm_data %d "
  1141. "r2t_data %d]\n",
  1142. ctask->itt, ctask->total_length, ctask->imm_count,
  1143. ctask->unsol_count, tcp_ctask->r2t_data_count);
  1144. } else
  1145. tcp_ctask->xmstate = XMSTATE_R_HDR;
  1146. iscsi_buf_init_iov(&tcp_ctask->headbuf, (char*)ctask->hdr,
  1147. sizeof(struct iscsi_hdr));
  1148. }
  1149. /**
  1150. * iscsi_tcp_mtask_xmit - xmit management(immediate) task
  1151. * @conn: iscsi connection
  1152. * @mtask: task management task
  1153. *
  1154. * Notes:
  1155. * The function can return -EAGAIN in which case caller must
  1156. * call it again later, or recover. '0' return code means successful
  1157. * xmit.
  1158. *
  1159. * Management xmit state machine consists of two states:
  1160. * IN_PROGRESS_IMM_HEAD - PDU Header xmit in progress
  1161. * IN_PROGRESS_IMM_DATA - PDU Data xmit in progress
  1162. **/
  1163. static int
  1164. iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
  1165. {
  1166. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1167. int rc;
  1168. debug_scsi("mtask deq [cid %d state %x itt 0x%x]\n",
  1169. conn->id, tcp_mtask->xmstate, mtask->itt);
  1170. if (tcp_mtask->xmstate & XMSTATE_IMM_HDR) {
  1171. tcp_mtask->xmstate &= ~XMSTATE_IMM_HDR;
  1172. if (mtask->data_count)
  1173. tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
  1174. if (conn->c_stage != ISCSI_CONN_INITIAL_STAGE &&
  1175. conn->stop_stage != STOP_CONN_RECOVER &&
  1176. conn->hdrdgst_en)
  1177. iscsi_hdr_digest(conn, &tcp_mtask->headbuf,
  1178. (u8*)tcp_mtask->hdrext);
  1179. rc = iscsi_sendhdr(conn, &tcp_mtask->headbuf,
  1180. mtask->data_count);
  1181. if (rc) {
  1182. tcp_mtask->xmstate |= XMSTATE_IMM_HDR;
  1183. if (mtask->data_count)
  1184. tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
  1185. return rc;
  1186. }
  1187. }
  1188. if (tcp_mtask->xmstate & XMSTATE_IMM_DATA) {
  1189. BUG_ON(!mtask->data_count);
  1190. tcp_mtask->xmstate &= ~XMSTATE_IMM_DATA;
  1191. /* FIXME: implement.
  1192. * Virtual buffer could be spreaded across multiple pages...
  1193. */
  1194. do {
  1195. int rc;
  1196. rc = iscsi_sendpage(conn, &tcp_mtask->sendbuf,
  1197. &mtask->data_count, &tcp_mtask->sent);
  1198. if (rc) {
  1199. tcp_mtask->xmstate |= XMSTATE_IMM_DATA;
  1200. return rc;
  1201. }
  1202. } while (mtask->data_count);
  1203. }
  1204. BUG_ON(tcp_mtask->xmstate != XMSTATE_IDLE);
  1205. if (mtask->hdr->itt == cpu_to_be32(ISCSI_RESERVED_TAG)) {
  1206. struct iscsi_session *session = conn->session;
  1207. spin_lock_bh(&session->lock);
  1208. list_del(&conn->mtask->running);
  1209. __kfifo_put(session->mgmtpool.queue, (void*)&conn->mtask,
  1210. sizeof(void*));
  1211. spin_unlock_bh(&session->lock);
  1212. }
  1213. return 0;
  1214. }
  1215. static inline int
  1216. handle_xmstate_r_hdr(struct iscsi_conn *conn,
  1217. struct iscsi_tcp_cmd_task *tcp_ctask)
  1218. {
  1219. int rc;
  1220. tcp_ctask->xmstate &= ~XMSTATE_R_HDR;
  1221. if (conn->hdrdgst_en)
  1222. iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
  1223. (u8*)tcp_ctask->hdrext);
  1224. rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, 0);
  1225. if (!rc) {
  1226. BUG_ON(tcp_ctask->xmstate != XMSTATE_IDLE);
  1227. return 0; /* wait for Data-In */
  1228. }
  1229. tcp_ctask->xmstate |= XMSTATE_R_HDR;
  1230. return rc;
  1231. }
  1232. static inline int
  1233. handle_xmstate_w_hdr(struct iscsi_conn *conn,
  1234. struct iscsi_cmd_task *ctask)
  1235. {
  1236. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1237. int rc;
  1238. tcp_ctask->xmstate &= ~XMSTATE_W_HDR;
  1239. if (conn->hdrdgst_en)
  1240. iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
  1241. (u8*)tcp_ctask->hdrext);
  1242. rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->imm_count);
  1243. if (rc)
  1244. tcp_ctask->xmstate |= XMSTATE_W_HDR;
  1245. return rc;
  1246. }
  1247. static inline int
  1248. handle_xmstate_data_digest(struct iscsi_conn *conn,
  1249. struct iscsi_cmd_task *ctask)
  1250. {
  1251. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1252. int rc;
  1253. tcp_ctask->xmstate &= ~XMSTATE_DATA_DIGEST;
  1254. debug_tcp("resent data digest 0x%x\n", tcp_ctask->datadigest);
  1255. rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
  1256. &tcp_ctask->datadigest, 0);
  1257. if (rc) {
  1258. tcp_ctask->xmstate |= XMSTATE_DATA_DIGEST;
  1259. debug_tcp("resent data digest 0x%x fail!\n",
  1260. tcp_ctask->datadigest);
  1261. }
  1262. return rc;
  1263. }
  1264. static inline int
  1265. handle_xmstate_imm_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1266. {
  1267. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1268. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1269. int rc;
  1270. BUG_ON(!ctask->imm_count);
  1271. tcp_ctask->xmstate &= ~XMSTATE_IMM_DATA;
  1272. if (conn->datadgst_en) {
  1273. iscsi_data_digest_init(tcp_conn, ctask);
  1274. tcp_ctask->immdigest = 0;
  1275. }
  1276. for (;;) {
  1277. rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
  1278. &ctask->imm_count, &tcp_ctask->sent);
  1279. if (rc) {
  1280. tcp_ctask->xmstate |= XMSTATE_IMM_DATA;
  1281. if (conn->datadgst_en) {
  1282. crypto_digest_final(tcp_conn->data_tx_tfm,
  1283. (u8*)&tcp_ctask->immdigest);
  1284. debug_tcp("tx imm sendpage fail 0x%x\n",
  1285. tcp_ctask->datadigest);
  1286. }
  1287. return rc;
  1288. }
  1289. if (conn->datadgst_en)
  1290. crypto_digest_update(tcp_conn->data_tx_tfm,
  1291. &tcp_ctask->sendbuf.sg, 1);
  1292. if (!ctask->imm_count)
  1293. break;
  1294. iscsi_buf_init_sg(&tcp_ctask->sendbuf,
  1295. &tcp_ctask->sg[tcp_ctask->sg_count++]);
  1296. }
  1297. if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
  1298. rc = iscsi_digest_final_send(conn, ctask, &tcp_ctask->immbuf,
  1299. &tcp_ctask->immdigest, 1);
  1300. if (rc) {
  1301. debug_tcp("sending imm digest 0x%x fail!\n",
  1302. tcp_ctask->immdigest);
  1303. return rc;
  1304. }
  1305. debug_tcp("sending imm digest 0x%x\n", tcp_ctask->immdigest);
  1306. }
  1307. return 0;
  1308. }
  1309. static inline int
  1310. handle_xmstate_uns_hdr(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1311. {
  1312. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1313. struct iscsi_data_task *dtask;
  1314. int rc;
  1315. tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
  1316. if (tcp_ctask->xmstate & XMSTATE_UNS_INIT) {
  1317. iscsi_unsolicit_data_init(conn, ctask);
  1318. dtask = tcp_ctask->dtask;
  1319. if (conn->hdrdgst_en)
  1320. iscsi_hdr_digest(conn, &tcp_ctask->headbuf,
  1321. (u8*)dtask->hdrext);
  1322. tcp_ctask->xmstate &= ~XMSTATE_UNS_INIT;
  1323. }
  1324. rc = iscsi_sendhdr(conn, &tcp_ctask->headbuf, ctask->data_count);
  1325. if (rc) {
  1326. tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
  1327. tcp_ctask->xmstate |= XMSTATE_UNS_HDR;
  1328. return rc;
  1329. }
  1330. debug_scsi("uns dout [itt 0x%x dlen %d sent %d]\n",
  1331. ctask->itt, ctask->unsol_count, tcp_ctask->sent);
  1332. return 0;
  1333. }
  1334. static inline int
  1335. handle_xmstate_uns_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1336. {
  1337. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1338. struct iscsi_data_task *dtask = tcp_ctask->dtask;
  1339. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1340. int rc;
  1341. BUG_ON(!ctask->data_count);
  1342. tcp_ctask->xmstate &= ~XMSTATE_UNS_DATA;
  1343. if (conn->datadgst_en) {
  1344. iscsi_data_digest_init(tcp_conn, ctask);
  1345. dtask->digest = 0;
  1346. }
  1347. for (;;) {
  1348. int start = tcp_ctask->sent;
  1349. rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf,
  1350. &ctask->data_count, &tcp_ctask->sent);
  1351. if (rc) {
  1352. ctask->unsol_count -= tcp_ctask->sent - start;
  1353. tcp_ctask->xmstate |= XMSTATE_UNS_DATA;
  1354. /* will continue with this ctask later.. */
  1355. if (conn->datadgst_en) {
  1356. crypto_digest_final(tcp_conn->data_tx_tfm,
  1357. (u8 *)&dtask->digest);
  1358. debug_tcp("tx uns data fail 0x%x\n",
  1359. dtask->digest);
  1360. }
  1361. return rc;
  1362. }
  1363. BUG_ON(tcp_ctask->sent > ctask->total_length);
  1364. ctask->unsol_count -= tcp_ctask->sent - start;
  1365. /*
  1366. * XXX:we may run here with un-initial sendbuf.
  1367. * so pass it
  1368. */
  1369. if (conn->datadgst_en && tcp_ctask->sent - start > 0)
  1370. crypto_digest_update(tcp_conn->data_tx_tfm,
  1371. &tcp_ctask->sendbuf.sg, 1);
  1372. if (!ctask->data_count)
  1373. break;
  1374. iscsi_buf_init_sg(&tcp_ctask->sendbuf,
  1375. &tcp_ctask->sg[tcp_ctask->sg_count++]);
  1376. }
  1377. BUG_ON(ctask->unsol_count < 0);
  1378. /*
  1379. * Done with the Data-Out. Next, check if we need
  1380. * to send another unsolicited Data-Out.
  1381. */
  1382. if (ctask->unsol_count) {
  1383. if (conn->datadgst_en) {
  1384. rc = iscsi_digest_final_send(conn, ctask,
  1385. &dtask->digestbuf,
  1386. &dtask->digest, 1);
  1387. if (rc) {
  1388. debug_tcp("send uns digest 0x%x fail\n",
  1389. dtask->digest);
  1390. return rc;
  1391. }
  1392. debug_tcp("sending uns digest 0x%x, more uns\n",
  1393. dtask->digest);
  1394. }
  1395. tcp_ctask->xmstate |= XMSTATE_UNS_INIT;
  1396. return 1;
  1397. }
  1398. if (conn->datadgst_en && !(tcp_ctask->xmstate & XMSTATE_W_PAD)) {
  1399. rc = iscsi_digest_final_send(conn, ctask,
  1400. &dtask->digestbuf,
  1401. &dtask->digest, 1);
  1402. if (rc) {
  1403. debug_tcp("send last uns digest 0x%x fail\n",
  1404. dtask->digest);
  1405. return rc;
  1406. }
  1407. debug_tcp("sending uns digest 0x%x\n",dtask->digest);
  1408. }
  1409. return 0;
  1410. }
  1411. static inline int
  1412. handle_xmstate_sol_data(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1413. {
  1414. struct iscsi_session *session = conn->session;
  1415. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1416. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1417. struct iscsi_r2t_info *r2t = tcp_ctask->r2t;
  1418. struct iscsi_data_task *dtask = &r2t->dtask;
  1419. int left, rc;
  1420. tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
  1421. tcp_ctask->dtask = dtask;
  1422. if (conn->datadgst_en) {
  1423. iscsi_data_digest_init(tcp_conn, ctask);
  1424. dtask->digest = 0;
  1425. }
  1426. solicit_again:
  1427. /*
  1428. * send Data-Out whitnin this R2T sequence.
  1429. */
  1430. if (!r2t->data_count)
  1431. goto data_out_done;
  1432. rc = iscsi_sendpage(conn, &r2t->sendbuf, &r2t->data_count, &r2t->sent);
  1433. if (rc) {
  1434. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1435. /* will continue with this ctask later.. */
  1436. if (conn->datadgst_en) {
  1437. crypto_digest_final(tcp_conn->data_tx_tfm,
  1438. (u8 *)&dtask->digest);
  1439. debug_tcp("r2t data send fail 0x%x\n", dtask->digest);
  1440. }
  1441. return rc;
  1442. }
  1443. BUG_ON(r2t->data_count < 0);
  1444. if (conn->datadgst_en)
  1445. crypto_digest_update(tcp_conn->data_tx_tfm, &r2t->sendbuf.sg,
  1446. 1);
  1447. if (r2t->data_count) {
  1448. BUG_ON(ctask->sc->use_sg == 0);
  1449. if (!iscsi_buf_left(&r2t->sendbuf)) {
  1450. BUG_ON(tcp_ctask->bad_sg == r2t->sg);
  1451. iscsi_buf_init_sg(&r2t->sendbuf, r2t->sg);
  1452. r2t->sg += 1;
  1453. }
  1454. goto solicit_again;
  1455. }
  1456. data_out_done:
  1457. /*
  1458. * Done with this Data-Out. Next, check if we have
  1459. * to send another Data-Out for this R2T.
  1460. */
  1461. BUG_ON(r2t->data_length - r2t->sent < 0);
  1462. left = r2t->data_length - r2t->sent;
  1463. if (left) {
  1464. if (conn->datadgst_en) {
  1465. rc = iscsi_digest_final_send(conn, ctask,
  1466. &dtask->digestbuf,
  1467. &dtask->digest, 1);
  1468. if (rc) {
  1469. debug_tcp("send r2t data digest 0x%x"
  1470. "fail\n", dtask->digest);
  1471. return rc;
  1472. }
  1473. debug_tcp("r2t data send digest 0x%x\n",
  1474. dtask->digest);
  1475. }
  1476. iscsi_solicit_data_cont(conn, ctask, r2t, left);
  1477. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1478. tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
  1479. return 1;
  1480. }
  1481. /*
  1482. * Done with this R2T. Check if there are more
  1483. * outstanding R2Ts ready to be processed.
  1484. */
  1485. BUG_ON(tcp_ctask->r2t_data_count - r2t->data_length < 0);
  1486. if (conn->datadgst_en) {
  1487. rc = iscsi_digest_final_send(conn, ctask, &dtask->digestbuf,
  1488. &dtask->digest, 1);
  1489. if (rc) {
  1490. debug_tcp("send last r2t data digest 0x%x"
  1491. "fail\n", dtask->digest);
  1492. return rc;
  1493. }
  1494. debug_tcp("r2t done dout digest 0x%x\n", dtask->digest);
  1495. }
  1496. tcp_ctask->r2t_data_count -= r2t->data_length;
  1497. tcp_ctask->r2t = NULL;
  1498. spin_lock_bh(&session->lock);
  1499. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
  1500. spin_unlock_bh(&session->lock);
  1501. if (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
  1502. tcp_ctask->r2t = r2t;
  1503. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1504. tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
  1505. return 1;
  1506. }
  1507. return 0;
  1508. }
  1509. static inline int
  1510. handle_xmstate_w_pad(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1511. {
  1512. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1513. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1514. struct iscsi_data_task *dtask = tcp_ctask->dtask;
  1515. int sent, rc;
  1516. tcp_ctask->xmstate &= ~XMSTATE_W_PAD;
  1517. iscsi_buf_init_iov(&tcp_ctask->sendbuf, (char*)&tcp_ctask->pad,
  1518. tcp_ctask->pad_count);
  1519. rc = iscsi_sendpage(conn, &tcp_ctask->sendbuf, &tcp_ctask->pad_count,
  1520. &sent);
  1521. if (rc) {
  1522. tcp_ctask->xmstate |= XMSTATE_W_PAD;
  1523. return rc;
  1524. }
  1525. if (conn->datadgst_en) {
  1526. crypto_digest_update(tcp_conn->data_tx_tfm,
  1527. &tcp_ctask->sendbuf.sg, 1);
  1528. /* imm data? */
  1529. if (!dtask) {
  1530. rc = iscsi_digest_final_send(conn, ctask,
  1531. &tcp_ctask->immbuf,
  1532. &tcp_ctask->immdigest, 1);
  1533. if (rc) {
  1534. debug_tcp("send padding digest 0x%x"
  1535. "fail!\n", tcp_ctask->immdigest);
  1536. return rc;
  1537. }
  1538. debug_tcp("done with padding, digest 0x%x\n",
  1539. tcp_ctask->datadigest);
  1540. } else {
  1541. rc = iscsi_digest_final_send(conn, ctask,
  1542. &dtask->digestbuf,
  1543. &dtask->digest, 1);
  1544. if (rc) {
  1545. debug_tcp("send padding digest 0x%x"
  1546. "fail\n", dtask->digest);
  1547. return rc;
  1548. }
  1549. debug_tcp("done with padding, digest 0x%x\n",
  1550. dtask->digest);
  1551. }
  1552. }
  1553. return 0;
  1554. }
  1555. static int
  1556. iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1557. {
  1558. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1559. int rc = 0;
  1560. debug_scsi("ctask deq [cid %d xmstate %x itt 0x%x]\n",
  1561. conn->id, tcp_ctask->xmstate, ctask->itt);
  1562. /*
  1563. * serialize with TMF AbortTask
  1564. */
  1565. if (ctask->mtask)
  1566. return rc;
  1567. if (tcp_ctask->xmstate & XMSTATE_R_HDR)
  1568. return handle_xmstate_r_hdr(conn, tcp_ctask);
  1569. if (tcp_ctask->xmstate & XMSTATE_W_HDR) {
  1570. rc = handle_xmstate_w_hdr(conn, ctask);
  1571. if (rc)
  1572. return rc;
  1573. }
  1574. /* XXX: for data digest xmit recover */
  1575. if (tcp_ctask->xmstate & XMSTATE_DATA_DIGEST) {
  1576. rc = handle_xmstate_data_digest(conn, ctask);
  1577. if (rc)
  1578. return rc;
  1579. }
  1580. if (tcp_ctask->xmstate & XMSTATE_IMM_DATA) {
  1581. rc = handle_xmstate_imm_data(conn, ctask);
  1582. if (rc)
  1583. return rc;
  1584. }
  1585. if (tcp_ctask->xmstate & XMSTATE_UNS_HDR) {
  1586. BUG_ON(!ctask->unsol_count);
  1587. tcp_ctask->xmstate &= ~XMSTATE_UNS_HDR;
  1588. unsolicit_head_again:
  1589. rc = handle_xmstate_uns_hdr(conn, ctask);
  1590. if (rc)
  1591. return rc;
  1592. }
  1593. if (tcp_ctask->xmstate & XMSTATE_UNS_DATA) {
  1594. rc = handle_xmstate_uns_data(conn, ctask);
  1595. if (rc == 1)
  1596. goto unsolicit_head_again;
  1597. else if (rc)
  1598. return rc;
  1599. goto done;
  1600. }
  1601. if (tcp_ctask->xmstate & XMSTATE_SOL_HDR) {
  1602. struct iscsi_r2t_info *r2t;
  1603. tcp_ctask->xmstate &= ~XMSTATE_SOL_HDR;
  1604. tcp_ctask->xmstate |= XMSTATE_SOL_DATA;
  1605. if (!tcp_ctask->r2t)
  1606. __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
  1607. sizeof(void*));
  1608. solicit_head_again:
  1609. r2t = tcp_ctask->r2t;
  1610. if (conn->hdrdgst_en)
  1611. iscsi_hdr_digest(conn, &r2t->headbuf,
  1612. (u8*)r2t->dtask.hdrext);
  1613. rc = iscsi_sendhdr(conn, &r2t->headbuf, r2t->data_count);
  1614. if (rc) {
  1615. tcp_ctask->xmstate &= ~XMSTATE_SOL_DATA;
  1616. tcp_ctask->xmstate |= XMSTATE_SOL_HDR;
  1617. return rc;
  1618. }
  1619. debug_scsi("sol dout [dsn %d itt 0x%x dlen %d sent %d]\n",
  1620. r2t->solicit_datasn - 1, ctask->itt, r2t->data_count,
  1621. r2t->sent);
  1622. }
  1623. if (tcp_ctask->xmstate & XMSTATE_SOL_DATA) {
  1624. rc = handle_xmstate_sol_data(conn, ctask);
  1625. if (rc == 1)
  1626. goto solicit_head_again;
  1627. if (rc)
  1628. return rc;
  1629. }
  1630. done:
  1631. /*
  1632. * Last thing to check is whether we need to send write
  1633. * padding. Note that we check for xmstate equality, not just the bit.
  1634. */
  1635. if (tcp_ctask->xmstate == XMSTATE_W_PAD)
  1636. rc = handle_xmstate_w_pad(conn, ctask);
  1637. return rc;
  1638. }
  1639. static struct iscsi_cls_conn *
  1640. iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1641. {
  1642. struct iscsi_conn *conn;
  1643. struct iscsi_cls_conn *cls_conn;
  1644. struct iscsi_tcp_conn *tcp_conn;
  1645. cls_conn = iscsi_conn_setup(cls_session, conn_idx);
  1646. if (!cls_conn)
  1647. return NULL;
  1648. conn = cls_conn->dd_data;
  1649. /*
  1650. * due to strange issues with iser these are not set
  1651. * in iscsi_conn_setup
  1652. */
  1653. conn->max_recv_dlength = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
  1654. tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
  1655. if (!tcp_conn)
  1656. goto tcp_conn_alloc_fail;
  1657. conn->dd_data = tcp_conn;
  1658. tcp_conn->iscsi_conn = conn;
  1659. tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
  1660. /* initial operational parameters */
  1661. tcp_conn->hdr_size = sizeof(struct iscsi_hdr);
  1662. tcp_conn->data_size = DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH;
  1663. /* allocate initial PDU receive place holder */
  1664. if (tcp_conn->data_size <= PAGE_SIZE)
  1665. tcp_conn->data = kmalloc(tcp_conn->data_size, GFP_KERNEL);
  1666. else
  1667. tcp_conn->data = (void*)__get_free_pages(GFP_KERNEL,
  1668. get_order(tcp_conn->data_size));
  1669. if (!tcp_conn->data)
  1670. goto max_recv_dlenght_alloc_fail;
  1671. return cls_conn;
  1672. max_recv_dlenght_alloc_fail:
  1673. kfree(tcp_conn);
  1674. tcp_conn_alloc_fail:
  1675. iscsi_conn_teardown(cls_conn);
  1676. return NULL;
  1677. }
  1678. static void
  1679. iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1680. {
  1681. struct iscsi_conn *conn = cls_conn->dd_data;
  1682. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1683. int digest = 0;
  1684. if (conn->hdrdgst_en || conn->datadgst_en)
  1685. digest = 1;
  1686. iscsi_conn_teardown(cls_conn);
  1687. /* now free tcp_conn */
  1688. if (digest) {
  1689. if (tcp_conn->tx_tfm)
  1690. crypto_free_tfm(tcp_conn->tx_tfm);
  1691. if (tcp_conn->rx_tfm)
  1692. crypto_free_tfm(tcp_conn->rx_tfm);
  1693. if (tcp_conn->data_tx_tfm)
  1694. crypto_free_tfm(tcp_conn->data_tx_tfm);
  1695. if (tcp_conn->data_rx_tfm)
  1696. crypto_free_tfm(tcp_conn->data_rx_tfm);
  1697. }
  1698. /* free conn->data, size = MaxRecvDataSegmentLength */
  1699. if (tcp_conn->data_size <= PAGE_SIZE)
  1700. kfree(tcp_conn->data);
  1701. else
  1702. free_pages((unsigned long)tcp_conn->data,
  1703. get_order(tcp_conn->data_size));
  1704. kfree(tcp_conn);
  1705. }
  1706. static int
  1707. iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  1708. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  1709. int is_leading)
  1710. {
  1711. struct iscsi_conn *conn = cls_conn->dd_data;
  1712. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1713. struct sock *sk;
  1714. struct socket *sock;
  1715. int err;
  1716. /* lookup for existing socket */
  1717. sock = sockfd_lookup((int)transport_eph, &err);
  1718. if (!sock) {
  1719. printk(KERN_ERR "iscsi_tcp: sockfd_lookup failed %d\n", err);
  1720. return -EEXIST;
  1721. }
  1722. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  1723. if (err)
  1724. return err;
  1725. /* bind iSCSI connection and socket */
  1726. tcp_conn->sock = sock;
  1727. /* setup Socket parameters */
  1728. sk = sock->sk;
  1729. sk->sk_reuse = 1;
  1730. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  1731. sk->sk_allocation = GFP_ATOMIC;
  1732. /* FIXME: disable Nagle's algorithm */
  1733. /*
  1734. * Intercept TCP callbacks for sendfile like receive
  1735. * processing.
  1736. */
  1737. conn->recv_lock = &sk->sk_callback_lock;
  1738. iscsi_conn_set_callbacks(conn);
  1739. tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
  1740. /*
  1741. * set receive state machine into initial state
  1742. */
  1743. tcp_conn->in_progress = IN_PROGRESS_WAIT_HEADER;
  1744. return 0;
  1745. }
  1746. static void
  1747. iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1748. {
  1749. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1750. struct iscsi_r2t_info *r2t;
  1751. /* flush ctask's r2t queues */
  1752. while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*)))
  1753. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  1754. sizeof(void*));
  1755. __iscsi_ctask_cleanup(conn, ctask);
  1756. }
  1757. static void
  1758. iscsi_tcp_suspend_conn_rx(struct iscsi_conn *conn)
  1759. {
  1760. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1761. struct sock *sk;
  1762. if (!tcp_conn->sock)
  1763. return;
  1764. sk = tcp_conn->sock->sk;
  1765. write_lock_bh(&sk->sk_callback_lock);
  1766. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1767. write_unlock_bh(&sk->sk_callback_lock);
  1768. }
  1769. static void
  1770. iscsi_tcp_terminate_conn(struct iscsi_conn *conn)
  1771. {
  1772. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1773. if (!tcp_conn->sock)
  1774. return;
  1775. sock_hold(tcp_conn->sock->sk);
  1776. iscsi_conn_restore_callbacks(conn);
  1777. sock_put(tcp_conn->sock->sk);
  1778. sock_release(tcp_conn->sock);
  1779. tcp_conn->sock = NULL;
  1780. conn->recv_lock = NULL;
  1781. }
  1782. /* called with host lock */
  1783. static void
  1784. iscsi_tcp_mgmt_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask,
  1785. char *data, uint32_t data_size)
  1786. {
  1787. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1788. iscsi_buf_init_iov(&tcp_mtask->headbuf, (char*)mtask->hdr,
  1789. sizeof(struct iscsi_hdr));
  1790. tcp_mtask->xmstate = XMSTATE_IMM_HDR;
  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);