iscsi_tcp.c 63 KB

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