iscsi_tcp.c 58 KB

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