iscsi_tcp.c 62 KB

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