iscsi_tcp.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032
  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/file.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/crypto.h>
  34. #include <linux/delay.h>
  35. #include <linux/kfifo.h>
  36. #include <linux/scatterlist.h>
  37. #include <net/tcp.h>
  38. #include <scsi/scsi_cmnd.h>
  39. #include <scsi/scsi_device.h>
  40. #include <scsi/scsi_host.h>
  41. #include <scsi/scsi.h>
  42. #include <scsi/scsi_transport_iscsi.h>
  43. #include "iscsi_tcp.h"
  44. MODULE_AUTHOR("Dmitry Yusupov <dmitry_yus@yahoo.com>, "
  45. "Alex Aizman <itn780@yahoo.com>");
  46. MODULE_DESCRIPTION("iSCSI/TCP data-path");
  47. MODULE_LICENSE("GPL");
  48. #undef DEBUG_TCP
  49. #define DEBUG_ASSERT
  50. #ifdef DEBUG_TCP
  51. #define debug_tcp(fmt...) printk(KERN_INFO "tcp: " fmt)
  52. #else
  53. #define debug_tcp(fmt...)
  54. #endif
  55. #ifndef DEBUG_ASSERT
  56. #ifdef BUG_ON
  57. #undef BUG_ON
  58. #endif
  59. #define BUG_ON(expr)
  60. #endif
  61. static unsigned int iscsi_max_lun = 512;
  62. module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
  63. static int iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  64. struct iscsi_segment *segment);
  65. /*
  66. * Scatterlist handling: inside the iscsi_segment, we
  67. * remember an index into the scatterlist, and set data/size
  68. * to the current scatterlist entry. For highmem pages, we
  69. * kmap as needed.
  70. *
  71. * Note that the page is unmapped when we return from
  72. * TCP's data_ready handler, so we may end up mapping and
  73. * unmapping the same page repeatedly. The whole reason
  74. * for this is that we shouldn't keep the page mapped
  75. * outside the softirq.
  76. */
  77. /**
  78. * iscsi_tcp_segment_init_sg - init indicated scatterlist entry
  79. * @segment: the buffer object
  80. * @sg: scatterlist
  81. * @offset: byte offset into that sg entry
  82. *
  83. * This function sets up the segment so that subsequent
  84. * data is copied to the indicated sg entry, at the given
  85. * offset.
  86. */
  87. static inline void
  88. iscsi_tcp_segment_init_sg(struct iscsi_segment *segment,
  89. struct scatterlist *sg, unsigned int offset)
  90. {
  91. segment->sg = sg;
  92. segment->sg_offset = offset;
  93. segment->size = min(sg->length - offset,
  94. segment->total_size - segment->total_copied);
  95. segment->data = NULL;
  96. }
  97. /**
  98. * iscsi_tcp_segment_map - map the current S/G page
  99. * @segment: iscsi_segment
  100. * @recv: 1 if called from recv path
  101. *
  102. * We only need to possibly kmap data if scatter lists are being used,
  103. * because the iscsi passthrough and internal IO paths will never use high
  104. * mem pages.
  105. */
  106. static inline void
  107. iscsi_tcp_segment_map(struct iscsi_segment *segment, int recv)
  108. {
  109. struct scatterlist *sg;
  110. if (segment->data != NULL || !segment->sg)
  111. return;
  112. sg = segment->sg;
  113. BUG_ON(segment->sg_mapped);
  114. BUG_ON(sg->length == 0);
  115. /*
  116. * If the page count is greater than one it is ok to send
  117. * to the network layer's zero copy send path. If not we
  118. * have to go the slow sendmsg path. We always map for the
  119. * recv path.
  120. */
  121. if (page_count(sg_page(sg)) >= 1 && !recv)
  122. return;
  123. debug_tcp("iscsi_tcp_segment_map %s %p\n", recv ? "recv" : "xmit",
  124. segment);
  125. segment->sg_mapped = kmap_atomic(sg_page(sg), KM_SOFTIRQ0);
  126. segment->data = segment->sg_mapped + sg->offset + segment->sg_offset;
  127. }
  128. static inline void
  129. iscsi_tcp_segment_unmap(struct iscsi_segment *segment)
  130. {
  131. debug_tcp("iscsi_tcp_segment_unmap %p\n", segment);
  132. if (segment->sg_mapped) {
  133. debug_tcp("iscsi_tcp_segment_unmap valid\n");
  134. kunmap_atomic(segment->sg_mapped, KM_SOFTIRQ0);
  135. segment->sg_mapped = NULL;
  136. segment->data = NULL;
  137. }
  138. }
  139. /*
  140. * Splice the digest buffer into the buffer
  141. */
  142. static inline void
  143. iscsi_tcp_segment_splice_digest(struct iscsi_segment *segment, void *digest)
  144. {
  145. segment->data = digest;
  146. segment->digest_len = ISCSI_DIGEST_SIZE;
  147. segment->total_size += ISCSI_DIGEST_SIZE;
  148. segment->size = ISCSI_DIGEST_SIZE;
  149. segment->copied = 0;
  150. segment->sg = NULL;
  151. segment->hash = NULL;
  152. }
  153. /**
  154. * iscsi_tcp_segment_done - check whether the segment is complete
  155. * @segment: iscsi segment to check
  156. * @recv: set to one of this is called from the recv path
  157. * @copied: number of bytes copied
  158. *
  159. * Check if we're done receiving this segment. If the receive
  160. * buffer is full but we expect more data, move on to the
  161. * next entry in the scatterlist.
  162. *
  163. * If the amount of data we received isn't a multiple of 4,
  164. * we will transparently receive the pad bytes, too.
  165. *
  166. * This function must be re-entrant.
  167. */
  168. static inline int
  169. iscsi_tcp_segment_done(struct iscsi_segment *segment, int recv, unsigned copied)
  170. {
  171. static unsigned char padbuf[ISCSI_PAD_LEN];
  172. struct scatterlist sg;
  173. unsigned int pad;
  174. debug_tcp("copied %u %u size %u %s\n", segment->copied, copied,
  175. segment->size, recv ? "recv" : "xmit");
  176. if (segment->hash && copied) {
  177. /*
  178. * If a segment is kmapd we must unmap it before sending
  179. * to the crypto layer since that will try to kmap it again.
  180. */
  181. iscsi_tcp_segment_unmap(segment);
  182. if (!segment->data) {
  183. sg_init_table(&sg, 1);
  184. sg_set_page(&sg, sg_page(segment->sg), copied,
  185. segment->copied + segment->sg_offset +
  186. segment->sg->offset);
  187. } else
  188. sg_init_one(&sg, segment->data + segment->copied,
  189. copied);
  190. crypto_hash_update(segment->hash, &sg, copied);
  191. }
  192. segment->copied += copied;
  193. if (segment->copied < segment->size) {
  194. iscsi_tcp_segment_map(segment, recv);
  195. return 0;
  196. }
  197. segment->total_copied += segment->copied;
  198. segment->copied = 0;
  199. segment->size = 0;
  200. /* Unmap the current scatterlist page, if there is one. */
  201. iscsi_tcp_segment_unmap(segment);
  202. /* Do we have more scatterlist entries? */
  203. debug_tcp("total copied %u total size %u\n", segment->total_copied,
  204. segment->total_size);
  205. if (segment->total_copied < segment->total_size) {
  206. /* Proceed to the next entry in the scatterlist. */
  207. iscsi_tcp_segment_init_sg(segment, sg_next(segment->sg),
  208. 0);
  209. iscsi_tcp_segment_map(segment, recv);
  210. BUG_ON(segment->size == 0);
  211. return 0;
  212. }
  213. /* Do we need to handle padding? */
  214. pad = iscsi_padding(segment->total_copied);
  215. if (pad != 0) {
  216. debug_tcp("consume %d pad bytes\n", pad);
  217. segment->total_size += pad;
  218. segment->size = pad;
  219. segment->data = padbuf;
  220. return 0;
  221. }
  222. /*
  223. * Set us up for transferring the data digest. hdr digest
  224. * is completely handled in hdr done function.
  225. */
  226. if (segment->hash) {
  227. crypto_hash_final(segment->hash, segment->digest);
  228. iscsi_tcp_segment_splice_digest(segment,
  229. recv ? segment->recv_digest : segment->digest);
  230. return 0;
  231. }
  232. return 1;
  233. }
  234. /**
  235. * iscsi_tcp_xmit_segment - transmit segment
  236. * @tcp_conn: the iSCSI TCP connection
  237. * @segment: the buffer to transmnit
  238. *
  239. * This function transmits as much of the buffer as
  240. * the network layer will accept, and returns the number of
  241. * bytes transmitted.
  242. *
  243. * If CRC hashing is enabled, the function will compute the
  244. * hash as it goes. When the entire segment has been transmitted,
  245. * it will retrieve the hash value and send it as well.
  246. */
  247. static int
  248. iscsi_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
  249. struct iscsi_segment *segment)
  250. {
  251. struct socket *sk = tcp_conn->sock;
  252. unsigned int copied = 0;
  253. int r = 0;
  254. while (!iscsi_tcp_segment_done(segment, 0, r)) {
  255. struct scatterlist *sg;
  256. unsigned int offset, copy;
  257. int flags = 0;
  258. r = 0;
  259. offset = segment->copied;
  260. copy = segment->size - offset;
  261. if (segment->total_copied + segment->size < segment->total_size)
  262. flags |= MSG_MORE;
  263. /* Use sendpage if we can; else fall back to sendmsg */
  264. if (!segment->data) {
  265. sg = segment->sg;
  266. offset += segment->sg_offset + sg->offset;
  267. r = tcp_conn->sendpage(sk, sg_page(sg), offset, copy,
  268. flags);
  269. } else {
  270. struct msghdr msg = { .msg_flags = flags };
  271. struct kvec iov = {
  272. .iov_base = segment->data + offset,
  273. .iov_len = copy
  274. };
  275. r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
  276. }
  277. if (r < 0) {
  278. iscsi_tcp_segment_unmap(segment);
  279. if (copied || r == -EAGAIN)
  280. break;
  281. return r;
  282. }
  283. copied += r;
  284. }
  285. return copied;
  286. }
  287. /**
  288. * iscsi_tcp_segment_recv - copy data to segment
  289. * @tcp_conn: the iSCSI TCP connection
  290. * @segment: the buffer to copy to
  291. * @ptr: data pointer
  292. * @len: amount of data available
  293. *
  294. * This function copies up to @len bytes to the
  295. * given buffer, and returns the number of bytes
  296. * consumed, which can actually be less than @len.
  297. *
  298. * If hash digest is enabled, the function will update the
  299. * hash while copying.
  300. * Combining these two operations doesn't buy us a lot (yet),
  301. * but in the future we could implement combined copy+crc,
  302. * just way we do for network layer checksums.
  303. */
  304. static int
  305. iscsi_tcp_segment_recv(struct iscsi_tcp_conn *tcp_conn,
  306. struct iscsi_segment *segment, const void *ptr,
  307. unsigned int len)
  308. {
  309. unsigned int copy = 0, copied = 0;
  310. while (!iscsi_tcp_segment_done(segment, 1, copy)) {
  311. if (copied == len) {
  312. debug_tcp("iscsi_tcp_segment_recv copied %d bytes\n",
  313. len);
  314. break;
  315. }
  316. copy = min(len - copied, segment->size - segment->copied);
  317. debug_tcp("iscsi_tcp_segment_recv copying %d\n", copy);
  318. memcpy(segment->data + segment->copied, ptr + copied, copy);
  319. copied += copy;
  320. }
  321. return copied;
  322. }
  323. static inline void
  324. iscsi_tcp_dgst_header(struct hash_desc *hash, const void *hdr, size_t hdrlen,
  325. unsigned char digest[ISCSI_DIGEST_SIZE])
  326. {
  327. struct scatterlist sg;
  328. sg_init_one(&sg, hdr, hdrlen);
  329. crypto_hash_digest(hash, &sg, hdrlen, digest);
  330. }
  331. static inline int
  332. iscsi_tcp_dgst_verify(struct iscsi_tcp_conn *tcp_conn,
  333. struct iscsi_segment *segment)
  334. {
  335. if (!segment->digest_len)
  336. return 1;
  337. if (memcmp(segment->recv_digest, segment->digest,
  338. segment->digest_len)) {
  339. debug_scsi("digest mismatch\n");
  340. return 0;
  341. }
  342. return 1;
  343. }
  344. /*
  345. * Helper function to set up segment buffer
  346. */
  347. static inline void
  348. __iscsi_segment_init(struct iscsi_segment *segment, size_t size,
  349. iscsi_segment_done_fn_t *done, struct hash_desc *hash)
  350. {
  351. memset(segment, 0, sizeof(*segment));
  352. segment->total_size = size;
  353. segment->done = done;
  354. if (hash) {
  355. segment->hash = hash;
  356. crypto_hash_init(hash);
  357. }
  358. }
  359. static inline void
  360. iscsi_segment_init_linear(struct iscsi_segment *segment, void *data,
  361. size_t size, iscsi_segment_done_fn_t *done,
  362. struct hash_desc *hash)
  363. {
  364. __iscsi_segment_init(segment, size, done, hash);
  365. segment->data = data;
  366. segment->size = size;
  367. }
  368. static inline int
  369. iscsi_segment_seek_sg(struct iscsi_segment *segment,
  370. struct scatterlist *sg_list, unsigned int sg_count,
  371. unsigned int offset, size_t size,
  372. iscsi_segment_done_fn_t *done, struct hash_desc *hash)
  373. {
  374. struct scatterlist *sg;
  375. unsigned int i;
  376. debug_scsi("iscsi_segment_seek_sg offset %u size %llu\n",
  377. offset, size);
  378. __iscsi_segment_init(segment, size, done, hash);
  379. for_each_sg(sg_list, sg, sg_count, i) {
  380. debug_scsi("sg %d, len %u offset %u\n", i, sg->length,
  381. sg->offset);
  382. if (offset < sg->length) {
  383. iscsi_tcp_segment_init_sg(segment, sg, offset);
  384. return 0;
  385. }
  386. offset -= sg->length;
  387. }
  388. return ISCSI_ERR_DATA_OFFSET;
  389. }
  390. /**
  391. * iscsi_tcp_hdr_recv_prep - prep segment for hdr reception
  392. * @tcp_conn: iscsi connection to prep for
  393. *
  394. * This function always passes NULL for the hash argument, because when this
  395. * function is called we do not yet know the final size of the header and want
  396. * to delay the digest processing until we know that.
  397. */
  398. static void
  399. iscsi_tcp_hdr_recv_prep(struct iscsi_tcp_conn *tcp_conn)
  400. {
  401. debug_tcp("iscsi_tcp_hdr_recv_prep(%p%s)\n", tcp_conn,
  402. tcp_conn->iscsi_conn->hdrdgst_en ? ", digest enabled" : "");
  403. iscsi_segment_init_linear(&tcp_conn->in.segment,
  404. tcp_conn->in.hdr_buf, sizeof(struct iscsi_hdr),
  405. iscsi_tcp_hdr_recv_done, NULL);
  406. }
  407. /*
  408. * Handle incoming reply to any other type of command
  409. */
  410. static int
  411. iscsi_tcp_data_recv_done(struct iscsi_tcp_conn *tcp_conn,
  412. struct iscsi_segment *segment)
  413. {
  414. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  415. int rc = 0;
  416. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  417. return ISCSI_ERR_DATA_DGST;
  418. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr,
  419. conn->data, tcp_conn->in.datalen);
  420. if (rc)
  421. return rc;
  422. iscsi_tcp_hdr_recv_prep(tcp_conn);
  423. return 0;
  424. }
  425. static void
  426. iscsi_tcp_data_recv_prep(struct iscsi_tcp_conn *tcp_conn)
  427. {
  428. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  429. struct hash_desc *rx_hash = NULL;
  430. if (conn->datadgst_en)
  431. rx_hash = &tcp_conn->rx_hash;
  432. iscsi_segment_init_linear(&tcp_conn->in.segment,
  433. conn->data, tcp_conn->in.datalen,
  434. iscsi_tcp_data_recv_done, rx_hash);
  435. }
  436. /*
  437. * must be called with session lock
  438. */
  439. static void
  440. iscsi_tcp_cleanup_ctask(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  441. {
  442. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  443. struct iscsi_r2t_info *r2t;
  444. /* flush ctask's r2t queues */
  445. while (__kfifo_get(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*))) {
  446. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  447. sizeof(void*));
  448. debug_scsi("iscsi_tcp_cleanup_ctask pending r2t dropped\n");
  449. }
  450. r2t = tcp_ctask->r2t;
  451. if (r2t != NULL) {
  452. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  453. sizeof(void*));
  454. tcp_ctask->r2t = NULL;
  455. }
  456. }
  457. /**
  458. * iscsi_data_rsp - SCSI Data-In Response processing
  459. * @conn: iscsi connection
  460. * @ctask: scsi command task
  461. **/
  462. static int
  463. iscsi_data_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  464. {
  465. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  466. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  467. struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)tcp_conn->in.hdr;
  468. struct iscsi_session *session = conn->session;
  469. struct scsi_cmnd *sc = ctask->sc;
  470. int datasn = be32_to_cpu(rhdr->datasn);
  471. unsigned total_in_length = scsi_in(sc)->length;
  472. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  473. if (tcp_conn->in.datalen == 0)
  474. return 0;
  475. if (tcp_ctask->exp_datasn != datasn) {
  476. debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
  477. __FUNCTION__, tcp_ctask->exp_datasn, datasn);
  478. return ISCSI_ERR_DATASN;
  479. }
  480. tcp_ctask->exp_datasn++;
  481. tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
  482. if (tcp_ctask->data_offset + tcp_conn->in.datalen > total_in_length) {
  483. debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
  484. __FUNCTION__, tcp_ctask->data_offset,
  485. tcp_conn->in.datalen, total_in_length);
  486. return ISCSI_ERR_DATA_OFFSET;
  487. }
  488. if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
  489. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  490. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  491. if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
  492. ISCSI_FLAG_DATA_OVERFLOW)) {
  493. int res_count = be32_to_cpu(rhdr->residual_count);
  494. if (res_count > 0 &&
  495. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  496. res_count <= total_in_length))
  497. scsi_in(sc)->resid = res_count;
  498. else
  499. sc->result = (DID_BAD_TARGET << 16) |
  500. rhdr->cmd_status;
  501. }
  502. }
  503. conn->datain_pdus_cnt++;
  504. return 0;
  505. }
  506. /**
  507. * iscsi_solicit_data_init - initialize first Data-Out
  508. * @conn: iscsi connection
  509. * @ctask: scsi command task
  510. * @r2t: R2T info
  511. *
  512. * Notes:
  513. * Initialize first Data-Out within this R2T sequence and finds
  514. * proper data_offset within this SCSI command.
  515. *
  516. * This function is called with connection lock taken.
  517. **/
  518. static void
  519. iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  520. struct iscsi_r2t_info *r2t)
  521. {
  522. struct iscsi_data *hdr;
  523. hdr = &r2t->dtask.hdr;
  524. memset(hdr, 0, sizeof(struct iscsi_data));
  525. hdr->ttt = r2t->ttt;
  526. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  527. r2t->solicit_datasn++;
  528. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  529. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  530. hdr->itt = ctask->hdr->itt;
  531. hdr->exp_statsn = r2t->exp_statsn;
  532. hdr->offset = cpu_to_be32(r2t->data_offset);
  533. if (r2t->data_length > conn->max_xmit_dlength) {
  534. hton24(hdr->dlength, conn->max_xmit_dlength);
  535. r2t->data_count = conn->max_xmit_dlength;
  536. hdr->flags = 0;
  537. } else {
  538. hton24(hdr->dlength, r2t->data_length);
  539. r2t->data_count = r2t->data_length;
  540. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  541. }
  542. conn->dataout_pdus_cnt++;
  543. r2t->sent = 0;
  544. }
  545. /**
  546. * iscsi_r2t_rsp - iSCSI R2T Response processing
  547. * @conn: iscsi connection
  548. * @ctask: scsi command task
  549. **/
  550. static int
  551. iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  552. {
  553. struct iscsi_r2t_info *r2t;
  554. struct iscsi_session *session = conn->session;
  555. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  556. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  557. struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
  558. int r2tsn = be32_to_cpu(rhdr->r2tsn);
  559. int rc;
  560. if (tcp_conn->in.datalen) {
  561. iscsi_conn_printk(KERN_ERR, conn,
  562. "invalid R2t with datalen %d\n",
  563. tcp_conn->in.datalen);
  564. return ISCSI_ERR_DATALEN;
  565. }
  566. if (tcp_ctask->exp_datasn != r2tsn){
  567. debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
  568. __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
  569. return ISCSI_ERR_R2TSN;
  570. }
  571. /* fill-in new R2T associated with the task */
  572. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  573. if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) {
  574. iscsi_conn_printk(KERN_INFO, conn,
  575. "dropping R2T itt %d in recovery.\n",
  576. ctask->itt);
  577. return 0;
  578. }
  579. rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
  580. BUG_ON(!rc);
  581. r2t->exp_statsn = rhdr->statsn;
  582. r2t->data_length = be32_to_cpu(rhdr->data_length);
  583. if (r2t->data_length == 0) {
  584. iscsi_conn_printk(KERN_ERR, conn,
  585. "invalid R2T with zero data len\n");
  586. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  587. sizeof(void*));
  588. return ISCSI_ERR_DATALEN;
  589. }
  590. if (r2t->data_length > session->max_burst)
  591. debug_scsi("invalid R2T with data len %u and max burst %u."
  592. "Attempting to execute request.\n",
  593. r2t->data_length, session->max_burst);
  594. r2t->data_offset = be32_to_cpu(rhdr->data_offset);
  595. if (r2t->data_offset + r2t->data_length > scsi_out(ctask->sc)->length) {
  596. iscsi_conn_printk(KERN_ERR, conn,
  597. "invalid R2T with data len %u at offset %u "
  598. "and total length %d\n", r2t->data_length,
  599. r2t->data_offset, scsi_out(ctask->sc)->length);
  600. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  601. sizeof(void*));
  602. return ISCSI_ERR_DATALEN;
  603. }
  604. r2t->ttt = rhdr->ttt; /* no flip */
  605. r2t->solicit_datasn = 0;
  606. iscsi_solicit_data_init(conn, ctask, r2t);
  607. tcp_ctask->exp_datasn = r2tsn + 1;
  608. __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
  609. conn->r2t_pdus_cnt++;
  610. iscsi_requeue_ctask(ctask);
  611. return 0;
  612. }
  613. /*
  614. * Handle incoming reply to DataIn command
  615. */
  616. static int
  617. iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
  618. struct iscsi_segment *segment)
  619. {
  620. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  621. struct iscsi_hdr *hdr = tcp_conn->in.hdr;
  622. int rc;
  623. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  624. return ISCSI_ERR_DATA_DGST;
  625. /* check for non-exceptional status */
  626. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  627. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
  628. if (rc)
  629. return rc;
  630. }
  631. iscsi_tcp_hdr_recv_prep(tcp_conn);
  632. return 0;
  633. }
  634. /**
  635. * iscsi_tcp_hdr_dissect - process PDU header
  636. * @conn: iSCSI connection
  637. * @hdr: PDU header
  638. *
  639. * This function analyzes the header of the PDU received,
  640. * and performs several sanity checks. If the PDU is accompanied
  641. * by data, the receive buffer is set up to copy the incoming data
  642. * to the correct location.
  643. */
  644. static int
  645. iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  646. {
  647. int rc = 0, opcode, ahslen;
  648. struct iscsi_session *session = conn->session;
  649. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  650. struct iscsi_cmd_task *ctask;
  651. uint32_t itt;
  652. /* verify PDU length */
  653. tcp_conn->in.datalen = ntoh24(hdr->dlength);
  654. if (tcp_conn->in.datalen > conn->max_recv_dlength) {
  655. iscsi_conn_printk(KERN_ERR, conn,
  656. "iscsi_tcp: datalen %d > %d\n",
  657. tcp_conn->in.datalen, conn->max_recv_dlength);
  658. return ISCSI_ERR_DATALEN;
  659. }
  660. /* Additional header segments. So far, we don't
  661. * process additional headers.
  662. */
  663. ahslen = hdr->hlength << 2;
  664. opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  665. /* verify itt (itt encoding: age+cid+itt) */
  666. rc = iscsi_verify_itt(conn, hdr, &itt);
  667. if (rc)
  668. return rc;
  669. debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
  670. opcode, ahslen, tcp_conn->in.datalen);
  671. switch(opcode) {
  672. case ISCSI_OP_SCSI_DATA_IN:
  673. ctask = session->cmds[itt];
  674. spin_lock(&conn->session->lock);
  675. rc = iscsi_data_rsp(conn, ctask);
  676. spin_unlock(&conn->session->lock);
  677. if (rc)
  678. return rc;
  679. if (tcp_conn->in.datalen) {
  680. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  681. struct hash_desc *rx_hash = NULL;
  682. struct scsi_data_buffer *sdb = scsi_in(ctask->sc);
  683. /*
  684. * Setup copy of Data-In into the Scsi_Cmnd
  685. * Scatterlist case:
  686. * We set up the iscsi_segment to point to the next
  687. * scatterlist entry to copy to. As we go along,
  688. * we move on to the next scatterlist entry and
  689. * update the digest per-entry.
  690. */
  691. if (conn->datadgst_en)
  692. rx_hash = &tcp_conn->rx_hash;
  693. debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
  694. "datalen=%d)\n", tcp_conn,
  695. tcp_ctask->data_offset,
  696. tcp_conn->in.datalen);
  697. return iscsi_segment_seek_sg(&tcp_conn->in.segment,
  698. sdb->table.sgl,
  699. sdb->table.nents,
  700. tcp_ctask->data_offset,
  701. tcp_conn->in.datalen,
  702. iscsi_tcp_process_data_in,
  703. rx_hash);
  704. }
  705. /* fall through */
  706. case ISCSI_OP_SCSI_CMD_RSP:
  707. if (tcp_conn->in.datalen) {
  708. iscsi_tcp_data_recv_prep(tcp_conn);
  709. return 0;
  710. }
  711. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  712. break;
  713. case ISCSI_OP_R2T:
  714. ctask = session->cmds[itt];
  715. if (ahslen)
  716. rc = ISCSI_ERR_AHSLEN;
  717. else if (ctask->sc->sc_data_direction == DMA_TO_DEVICE) {
  718. spin_lock(&session->lock);
  719. rc = iscsi_r2t_rsp(conn, ctask);
  720. spin_unlock(&session->lock);
  721. } else
  722. rc = ISCSI_ERR_PROTO;
  723. break;
  724. case ISCSI_OP_LOGIN_RSP:
  725. case ISCSI_OP_TEXT_RSP:
  726. case ISCSI_OP_REJECT:
  727. case ISCSI_OP_ASYNC_EVENT:
  728. /*
  729. * It is possible that we could get a PDU with a buffer larger
  730. * than 8K, but there are no targets that currently do this.
  731. * For now we fail until we find a vendor that needs it
  732. */
  733. if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
  734. iscsi_conn_printk(KERN_ERR, conn,
  735. "iscsi_tcp: received buffer of "
  736. "len %u but conn buffer is only %u "
  737. "(opcode %0x)\n",
  738. tcp_conn->in.datalen,
  739. ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
  740. rc = ISCSI_ERR_PROTO;
  741. break;
  742. }
  743. /* If there's data coming in with the response,
  744. * receive it to the connection's buffer.
  745. */
  746. if (tcp_conn->in.datalen) {
  747. iscsi_tcp_data_recv_prep(tcp_conn);
  748. return 0;
  749. }
  750. /* fall through */
  751. case ISCSI_OP_LOGOUT_RSP:
  752. case ISCSI_OP_NOOP_IN:
  753. case ISCSI_OP_SCSI_TMFUNC_RSP:
  754. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  755. break;
  756. default:
  757. rc = ISCSI_ERR_BAD_OPCODE;
  758. break;
  759. }
  760. if (rc == 0) {
  761. /* Anything that comes with data should have
  762. * been handled above. */
  763. if (tcp_conn->in.datalen)
  764. return ISCSI_ERR_PROTO;
  765. iscsi_tcp_hdr_recv_prep(tcp_conn);
  766. }
  767. return rc;
  768. }
  769. /**
  770. * iscsi_tcp_hdr_recv_done - process PDU header
  771. *
  772. * This is the callback invoked when the PDU header has
  773. * been received. If the header is followed by additional
  774. * header segments, we go back for more data.
  775. */
  776. static int
  777. iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  778. struct iscsi_segment *segment)
  779. {
  780. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  781. struct iscsi_hdr *hdr;
  782. /* Check if there are additional header segments
  783. * *prior* to computing the digest, because we
  784. * may need to go back to the caller for more.
  785. */
  786. hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
  787. if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
  788. /* Bump the header length - the caller will
  789. * just loop around and get the AHS for us, and
  790. * call again. */
  791. unsigned int ahslen = hdr->hlength << 2;
  792. /* Make sure we don't overflow */
  793. if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
  794. return ISCSI_ERR_AHSLEN;
  795. segment->total_size += ahslen;
  796. segment->size += ahslen;
  797. return 0;
  798. }
  799. /* We're done processing the header. See if we're doing
  800. * header digests; if so, set up the recv_digest buffer
  801. * and go back for more. */
  802. if (conn->hdrdgst_en) {
  803. if (segment->digest_len == 0) {
  804. iscsi_tcp_segment_splice_digest(segment,
  805. segment->recv_digest);
  806. return 0;
  807. }
  808. iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
  809. segment->total_copied - ISCSI_DIGEST_SIZE,
  810. segment->digest);
  811. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  812. return ISCSI_ERR_HDR_DGST;
  813. }
  814. tcp_conn->in.hdr = hdr;
  815. return iscsi_tcp_hdr_dissect(conn, hdr);
  816. }
  817. /**
  818. * iscsi_tcp_recv - TCP receive in sendfile fashion
  819. * @rd_desc: read descriptor
  820. * @skb: socket buffer
  821. * @offset: offset in skb
  822. * @len: skb->len - offset
  823. **/
  824. static int
  825. iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  826. unsigned int offset, size_t len)
  827. {
  828. struct iscsi_conn *conn = rd_desc->arg.data;
  829. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  830. struct iscsi_segment *segment = &tcp_conn->in.segment;
  831. struct skb_seq_state seq;
  832. unsigned int consumed = 0;
  833. int rc = 0;
  834. debug_tcp("in %d bytes\n", skb->len - offset);
  835. if (unlikely(conn->suspend_rx)) {
  836. debug_tcp("conn %d Rx suspended!\n", conn->id);
  837. return 0;
  838. }
  839. skb_prepare_seq_read(skb, offset, skb->len, &seq);
  840. while (1) {
  841. unsigned int avail;
  842. const u8 *ptr;
  843. avail = skb_seq_read(consumed, &ptr, &seq);
  844. if (avail == 0) {
  845. debug_tcp("no more data avail. Consumed %d\n",
  846. consumed);
  847. break;
  848. }
  849. BUG_ON(segment->copied >= segment->size);
  850. debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
  851. rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
  852. BUG_ON(rc == 0);
  853. consumed += rc;
  854. if (segment->total_copied >= segment->total_size) {
  855. debug_tcp("segment done\n");
  856. rc = segment->done(tcp_conn, segment);
  857. if (rc != 0) {
  858. skb_abort_seq_read(&seq);
  859. goto error;
  860. }
  861. /* The done() functions sets up the
  862. * next segment. */
  863. }
  864. }
  865. skb_abort_seq_read(&seq);
  866. conn->rxdata_octets += consumed;
  867. return consumed;
  868. error:
  869. debug_tcp("Error receiving PDU, errno=%d\n", rc);
  870. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  871. return 0;
  872. }
  873. static void
  874. iscsi_tcp_data_ready(struct sock *sk, int flag)
  875. {
  876. struct iscsi_conn *conn = sk->sk_user_data;
  877. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  878. read_descriptor_t rd_desc;
  879. read_lock(&sk->sk_callback_lock);
  880. /*
  881. * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
  882. * We set count to 1 because we want the network layer to
  883. * hand us all the skbs that are available. iscsi_tcp_recv
  884. * handled pdus that cross buffers or pdus that still need data.
  885. */
  886. rd_desc.arg.data = conn;
  887. rd_desc.count = 1;
  888. tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
  889. read_unlock(&sk->sk_callback_lock);
  890. /* If we had to (atomically) map a highmem page,
  891. * unmap it now. */
  892. iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
  893. }
  894. static void
  895. iscsi_tcp_state_change(struct sock *sk)
  896. {
  897. struct iscsi_tcp_conn *tcp_conn;
  898. struct iscsi_conn *conn;
  899. struct iscsi_session *session;
  900. void (*old_state_change)(struct sock *);
  901. read_lock(&sk->sk_callback_lock);
  902. conn = (struct iscsi_conn*)sk->sk_user_data;
  903. session = conn->session;
  904. if ((sk->sk_state == TCP_CLOSE_WAIT ||
  905. sk->sk_state == TCP_CLOSE) &&
  906. !atomic_read(&sk->sk_rmem_alloc)) {
  907. debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
  908. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  909. }
  910. tcp_conn = conn->dd_data;
  911. old_state_change = tcp_conn->old_state_change;
  912. read_unlock(&sk->sk_callback_lock);
  913. old_state_change(sk);
  914. }
  915. /**
  916. * iscsi_write_space - Called when more output buffer space is available
  917. * @sk: socket space is available for
  918. **/
  919. static void
  920. iscsi_write_space(struct sock *sk)
  921. {
  922. struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
  923. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  924. tcp_conn->old_write_space(sk);
  925. debug_tcp("iscsi_write_space: cid %d\n", conn->id);
  926. scsi_queue_work(conn->session->host, &conn->xmitwork);
  927. }
  928. static void
  929. iscsi_conn_set_callbacks(struct iscsi_conn *conn)
  930. {
  931. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  932. struct sock *sk = tcp_conn->sock->sk;
  933. /* assign new callbacks */
  934. write_lock_bh(&sk->sk_callback_lock);
  935. sk->sk_user_data = conn;
  936. tcp_conn->old_data_ready = sk->sk_data_ready;
  937. tcp_conn->old_state_change = sk->sk_state_change;
  938. tcp_conn->old_write_space = sk->sk_write_space;
  939. sk->sk_data_ready = iscsi_tcp_data_ready;
  940. sk->sk_state_change = iscsi_tcp_state_change;
  941. sk->sk_write_space = iscsi_write_space;
  942. write_unlock_bh(&sk->sk_callback_lock);
  943. }
  944. static void
  945. iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
  946. {
  947. struct sock *sk = tcp_conn->sock->sk;
  948. /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
  949. write_lock_bh(&sk->sk_callback_lock);
  950. sk->sk_user_data = NULL;
  951. sk->sk_data_ready = tcp_conn->old_data_ready;
  952. sk->sk_state_change = tcp_conn->old_state_change;
  953. sk->sk_write_space = tcp_conn->old_write_space;
  954. sk->sk_no_check = 0;
  955. write_unlock_bh(&sk->sk_callback_lock);
  956. }
  957. /**
  958. * iscsi_xmit - TCP transmit
  959. **/
  960. static int
  961. iscsi_xmit(struct iscsi_conn *conn)
  962. {
  963. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  964. struct iscsi_segment *segment = &tcp_conn->out.segment;
  965. unsigned int consumed = 0;
  966. int rc = 0;
  967. while (1) {
  968. rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
  969. if (rc < 0)
  970. goto error;
  971. if (rc == 0)
  972. break;
  973. consumed += rc;
  974. if (segment->total_copied >= segment->total_size) {
  975. if (segment->done != NULL) {
  976. rc = segment->done(tcp_conn, segment);
  977. if (rc < 0)
  978. goto error;
  979. }
  980. }
  981. }
  982. debug_tcp("xmit %d bytes\n", consumed);
  983. conn->txdata_octets += consumed;
  984. return consumed;
  985. error:
  986. /* Transmit error. We could initiate error recovery
  987. * here. */
  988. debug_tcp("Error sending PDU, errno=%d\n", rc);
  989. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  990. return rc;
  991. }
  992. /**
  993. * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
  994. */
  995. static inline int
  996. iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
  997. {
  998. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  999. struct iscsi_segment *segment = &tcp_conn->out.segment;
  1000. return segment->total_copied - segment->total_size;
  1001. }
  1002. static inline int
  1003. iscsi_tcp_flush(struct iscsi_conn *conn)
  1004. {
  1005. int rc;
  1006. while (iscsi_tcp_xmit_qlen(conn)) {
  1007. rc = iscsi_xmit(conn);
  1008. if (rc == 0)
  1009. return -EAGAIN;
  1010. if (rc < 0)
  1011. return rc;
  1012. }
  1013. return 0;
  1014. }
  1015. /*
  1016. * This is called when we're done sending the header.
  1017. * Simply copy the data_segment to the send segment, and return.
  1018. */
  1019. static int
  1020. iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
  1021. struct iscsi_segment *segment)
  1022. {
  1023. tcp_conn->out.segment = tcp_conn->out.data_segment;
  1024. debug_tcp("Header done. Next segment size %u total_size %u\n",
  1025. tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
  1026. return 0;
  1027. }
  1028. static void
  1029. iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
  1030. {
  1031. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1032. debug_tcp("%s(%p%s)\n", __FUNCTION__, tcp_conn,
  1033. conn->hdrdgst_en? ", digest enabled" : "");
  1034. /* Clear the data segment - needs to be filled in by the
  1035. * caller using iscsi_tcp_send_data_prep() */
  1036. memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
  1037. /* If header digest is enabled, compute the CRC and
  1038. * place the digest into the same buffer. We make
  1039. * sure that both iscsi_tcp_ctask and mtask have
  1040. * sufficient room.
  1041. */
  1042. if (conn->hdrdgst_en) {
  1043. iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
  1044. hdr + hdrlen);
  1045. hdrlen += ISCSI_DIGEST_SIZE;
  1046. }
  1047. /* Remember header pointer for later, when we need
  1048. * to decide whether there's a payload to go along
  1049. * with the header. */
  1050. tcp_conn->out.hdr = hdr;
  1051. iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
  1052. iscsi_tcp_send_hdr_done, NULL);
  1053. }
  1054. /*
  1055. * Prepare the send buffer for the payload data.
  1056. * Padding and checksumming will all be taken care
  1057. * of by the iscsi_segment routines.
  1058. */
  1059. static int
  1060. iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
  1061. unsigned int count, unsigned int offset,
  1062. unsigned int len)
  1063. {
  1064. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1065. struct hash_desc *tx_hash = NULL;
  1066. unsigned int hdr_spec_len;
  1067. debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __FUNCTION__,
  1068. tcp_conn, offset, len,
  1069. conn->datadgst_en? ", digest enabled" : "");
  1070. /* Make sure the datalen matches what the caller
  1071. said he would send. */
  1072. hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
  1073. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  1074. if (conn->datadgst_en)
  1075. tx_hash = &tcp_conn->tx_hash;
  1076. return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
  1077. sg, count, offset, len,
  1078. NULL, tx_hash);
  1079. }
  1080. static void
  1081. iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
  1082. size_t len)
  1083. {
  1084. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1085. struct hash_desc *tx_hash = NULL;
  1086. unsigned int hdr_spec_len;
  1087. debug_tcp("%s(%p, datalen=%d%s)\n", __FUNCTION__, tcp_conn, len,
  1088. conn->datadgst_en? ", digest enabled" : "");
  1089. /* Make sure the datalen matches what the caller
  1090. said he would send. */
  1091. hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
  1092. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  1093. if (conn->datadgst_en)
  1094. tx_hash = &tcp_conn->tx_hash;
  1095. iscsi_segment_init_linear(&tcp_conn->out.data_segment,
  1096. data, len, NULL, tx_hash);
  1097. }
  1098. /**
  1099. * iscsi_solicit_data_cont - initialize next Data-Out
  1100. * @conn: iscsi connection
  1101. * @ctask: scsi command task
  1102. * @r2t: R2T info
  1103. * @left: bytes left to transfer
  1104. *
  1105. * Notes:
  1106. * Initialize next Data-Out within this R2T sequence and continue
  1107. * to process next Scatter-Gather element(if any) of this SCSI command.
  1108. *
  1109. * Called under connection lock.
  1110. **/
  1111. static int
  1112. iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1113. struct iscsi_r2t_info *r2t)
  1114. {
  1115. struct iscsi_data *hdr;
  1116. int new_offset, left;
  1117. BUG_ON(r2t->data_length - r2t->sent < 0);
  1118. left = r2t->data_length - r2t->sent;
  1119. if (left == 0)
  1120. return 0;
  1121. hdr = &r2t->dtask.hdr;
  1122. memset(hdr, 0, sizeof(struct iscsi_data));
  1123. hdr->ttt = r2t->ttt;
  1124. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  1125. r2t->solicit_datasn++;
  1126. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  1127. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1128. hdr->itt = ctask->hdr->itt;
  1129. hdr->exp_statsn = r2t->exp_statsn;
  1130. new_offset = r2t->data_offset + r2t->sent;
  1131. hdr->offset = cpu_to_be32(new_offset);
  1132. if (left > conn->max_xmit_dlength) {
  1133. hton24(hdr->dlength, conn->max_xmit_dlength);
  1134. r2t->data_count = conn->max_xmit_dlength;
  1135. } else {
  1136. hton24(hdr->dlength, left);
  1137. r2t->data_count = left;
  1138. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  1139. }
  1140. conn->dataout_pdus_cnt++;
  1141. return 1;
  1142. }
  1143. /**
  1144. * iscsi_tcp_ctask - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
  1145. * @conn: iscsi connection
  1146. * @ctask: scsi command task
  1147. * @sc: scsi command
  1148. **/
  1149. static int
  1150. iscsi_tcp_ctask_init(struct iscsi_cmd_task *ctask)
  1151. {
  1152. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1153. struct iscsi_conn *conn = ctask->conn;
  1154. struct scsi_cmnd *sc = ctask->sc;
  1155. int err;
  1156. BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
  1157. tcp_ctask->sent = 0;
  1158. tcp_ctask->exp_datasn = 0;
  1159. /* Prepare PDU, optionally w/ immediate data */
  1160. debug_scsi("ctask deq [cid %d itt 0x%x imm %d unsol %d]\n",
  1161. conn->id, ctask->itt, ctask->imm_count,
  1162. ctask->unsol_count);
  1163. iscsi_tcp_send_hdr_prep(conn, ctask->hdr, ctask->hdr_len);
  1164. if (!ctask->imm_count)
  1165. return 0;
  1166. /* If we have immediate data, attach a payload */
  1167. err = iscsi_tcp_send_data_prep(conn, scsi_out(sc)->table.sgl,
  1168. scsi_out(sc)->table.nents,
  1169. 0, ctask->imm_count);
  1170. if (err)
  1171. return err;
  1172. tcp_ctask->sent += ctask->imm_count;
  1173. ctask->imm_count = 0;
  1174. return 0;
  1175. }
  1176. /**
  1177. * iscsi_tcp_mtask_xmit - xmit management(immediate) task
  1178. * @conn: iscsi connection
  1179. * @mtask: task management task
  1180. *
  1181. * Notes:
  1182. * The function can return -EAGAIN in which case caller must
  1183. * call it again later, or recover. '0' return code means successful
  1184. * xmit.
  1185. **/
  1186. static int
  1187. iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
  1188. {
  1189. int rc;
  1190. /* Flush any pending data first. */
  1191. rc = iscsi_tcp_flush(conn);
  1192. if (rc < 0)
  1193. return rc;
  1194. if (mtask->hdr->itt == RESERVED_ITT) {
  1195. struct iscsi_session *session = conn->session;
  1196. spin_lock_bh(&session->lock);
  1197. iscsi_free_mgmt_task(conn, mtask);
  1198. spin_unlock_bh(&session->lock);
  1199. }
  1200. return 0;
  1201. }
  1202. /*
  1203. * iscsi_tcp_ctask_xmit - xmit normal PDU task
  1204. * @conn: iscsi connection
  1205. * @ctask: iscsi command task
  1206. *
  1207. * We're expected to return 0 when everything was transmitted succesfully,
  1208. * -EAGAIN if there's still data in the queue, or != 0 for any other kind
  1209. * of error.
  1210. */
  1211. static int
  1212. iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1213. {
  1214. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1215. struct scsi_cmnd *sc = ctask->sc;
  1216. struct scsi_data_buffer *sdb = scsi_out(sc);
  1217. int rc = 0;
  1218. flush:
  1219. /* Flush any pending data first. */
  1220. rc = iscsi_tcp_flush(conn);
  1221. if (rc < 0)
  1222. return rc;
  1223. /* Are we done already? */
  1224. if (sc->sc_data_direction != DMA_TO_DEVICE)
  1225. return 0;
  1226. if (ctask->unsol_count != 0) {
  1227. struct iscsi_data *hdr = &tcp_ctask->unsol_dtask.hdr;
  1228. /* Prepare a header for the unsolicited PDU.
  1229. * The amount of data we want to send will be
  1230. * in ctask->data_count.
  1231. * FIXME: return the data count instead.
  1232. */
  1233. iscsi_prep_unsolicit_data_pdu(ctask, hdr);
  1234. debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
  1235. ctask->itt, tcp_ctask->sent, ctask->data_count);
  1236. iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
  1237. rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
  1238. sdb->table.nents, tcp_ctask->sent,
  1239. ctask->data_count);
  1240. if (rc)
  1241. goto fail;
  1242. tcp_ctask->sent += ctask->data_count;
  1243. ctask->unsol_count -= ctask->data_count;
  1244. goto flush;
  1245. } else {
  1246. struct iscsi_session *session = conn->session;
  1247. struct iscsi_r2t_info *r2t;
  1248. /* All unsolicited PDUs sent. Check for solicited PDUs.
  1249. */
  1250. spin_lock_bh(&session->lock);
  1251. r2t = tcp_ctask->r2t;
  1252. if (r2t != NULL) {
  1253. /* Continue with this R2T? */
  1254. if (!iscsi_solicit_data_cont(conn, ctask, r2t)) {
  1255. debug_scsi(" done with r2t %p\n", r2t);
  1256. __kfifo_put(tcp_ctask->r2tpool.queue,
  1257. (void*)&r2t, sizeof(void*));
  1258. tcp_ctask->r2t = r2t = NULL;
  1259. }
  1260. }
  1261. if (r2t == NULL) {
  1262. __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
  1263. sizeof(void*));
  1264. r2t = tcp_ctask->r2t;
  1265. }
  1266. spin_unlock_bh(&session->lock);
  1267. /* Waiting for more R2Ts to arrive. */
  1268. if (r2t == NULL) {
  1269. debug_tcp("no R2Ts yet\n");
  1270. return 0;
  1271. }
  1272. debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
  1273. r2t, r2t->solicit_datasn - 1, ctask->itt,
  1274. r2t->data_offset + r2t->sent, r2t->data_count);
  1275. iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
  1276. sizeof(struct iscsi_hdr));
  1277. rc = iscsi_tcp_send_data_prep(conn, sdb->table.sgl,
  1278. sdb->table.nents,
  1279. r2t->data_offset + r2t->sent,
  1280. r2t->data_count);
  1281. if (rc)
  1282. goto fail;
  1283. tcp_ctask->sent += r2t->data_count;
  1284. r2t->sent += r2t->data_count;
  1285. goto flush;
  1286. }
  1287. return 0;
  1288. fail:
  1289. iscsi_conn_failure(conn, rc);
  1290. return -EIO;
  1291. }
  1292. static struct iscsi_cls_conn *
  1293. iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1294. {
  1295. struct iscsi_conn *conn;
  1296. struct iscsi_cls_conn *cls_conn;
  1297. struct iscsi_tcp_conn *tcp_conn;
  1298. cls_conn = iscsi_conn_setup(cls_session, conn_idx);
  1299. if (!cls_conn)
  1300. return NULL;
  1301. conn = cls_conn->dd_data;
  1302. /*
  1303. * due to strange issues with iser these are not set
  1304. * in iscsi_conn_setup
  1305. */
  1306. conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
  1307. tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
  1308. if (!tcp_conn)
  1309. goto tcp_conn_alloc_fail;
  1310. conn->dd_data = tcp_conn;
  1311. tcp_conn->iscsi_conn = conn;
  1312. tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1313. CRYPTO_ALG_ASYNC);
  1314. tcp_conn->tx_hash.flags = 0;
  1315. if (IS_ERR(tcp_conn->tx_hash.tfm))
  1316. goto free_tcp_conn;
  1317. tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1318. CRYPTO_ALG_ASYNC);
  1319. tcp_conn->rx_hash.flags = 0;
  1320. if (IS_ERR(tcp_conn->rx_hash.tfm))
  1321. goto free_tx_tfm;
  1322. return cls_conn;
  1323. free_tx_tfm:
  1324. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1325. free_tcp_conn:
  1326. iscsi_conn_printk(KERN_ERR, conn,
  1327. "Could not create connection due to crc32c "
  1328. "loading error. Make sure the crc32c "
  1329. "module is built as a module or into the "
  1330. "kernel\n");
  1331. kfree(tcp_conn);
  1332. tcp_conn_alloc_fail:
  1333. iscsi_conn_teardown(cls_conn);
  1334. return NULL;
  1335. }
  1336. static void
  1337. iscsi_tcp_release_conn(struct iscsi_conn *conn)
  1338. {
  1339. struct iscsi_session *session = conn->session;
  1340. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1341. struct socket *sock = tcp_conn->sock;
  1342. if (!sock)
  1343. return;
  1344. sock_hold(sock->sk);
  1345. iscsi_conn_restore_callbacks(tcp_conn);
  1346. sock_put(sock->sk);
  1347. spin_lock_bh(&session->lock);
  1348. tcp_conn->sock = NULL;
  1349. conn->recv_lock = NULL;
  1350. spin_unlock_bh(&session->lock);
  1351. sockfd_put(sock);
  1352. }
  1353. static void
  1354. iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1355. {
  1356. struct iscsi_conn *conn = cls_conn->dd_data;
  1357. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1358. iscsi_tcp_release_conn(conn);
  1359. iscsi_conn_teardown(cls_conn);
  1360. if (tcp_conn->tx_hash.tfm)
  1361. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1362. if (tcp_conn->rx_hash.tfm)
  1363. crypto_free_hash(tcp_conn->rx_hash.tfm);
  1364. kfree(tcp_conn);
  1365. }
  1366. static void
  1367. iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1368. {
  1369. struct iscsi_conn *conn = cls_conn->dd_data;
  1370. iscsi_conn_stop(cls_conn, flag);
  1371. iscsi_tcp_release_conn(conn);
  1372. }
  1373. static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
  1374. char *buf, int *port,
  1375. int (*getname)(struct socket *, struct sockaddr *,
  1376. int *addrlen))
  1377. {
  1378. struct sockaddr_storage *addr;
  1379. struct sockaddr_in6 *sin6;
  1380. struct sockaddr_in *sin;
  1381. int rc = 0, len;
  1382. addr = kmalloc(sizeof(*addr), GFP_KERNEL);
  1383. if (!addr)
  1384. return -ENOMEM;
  1385. if (getname(sock, (struct sockaddr *) addr, &len)) {
  1386. rc = -ENODEV;
  1387. goto free_addr;
  1388. }
  1389. switch (addr->ss_family) {
  1390. case AF_INET:
  1391. sin = (struct sockaddr_in *)addr;
  1392. spin_lock_bh(&conn->session->lock);
  1393. sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
  1394. *port = be16_to_cpu(sin->sin_port);
  1395. spin_unlock_bh(&conn->session->lock);
  1396. break;
  1397. case AF_INET6:
  1398. sin6 = (struct sockaddr_in6 *)addr;
  1399. spin_lock_bh(&conn->session->lock);
  1400. sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
  1401. *port = be16_to_cpu(sin6->sin6_port);
  1402. spin_unlock_bh(&conn->session->lock);
  1403. break;
  1404. }
  1405. free_addr:
  1406. kfree(addr);
  1407. return rc;
  1408. }
  1409. static int
  1410. iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  1411. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  1412. int is_leading)
  1413. {
  1414. struct iscsi_conn *conn = cls_conn->dd_data;
  1415. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1416. struct sock *sk;
  1417. struct socket *sock;
  1418. int err;
  1419. /* lookup for existing socket */
  1420. sock = sockfd_lookup((int)transport_eph, &err);
  1421. if (!sock) {
  1422. iscsi_conn_printk(KERN_ERR, conn,
  1423. "sockfd_lookup failed %d\n", err);
  1424. return -EEXIST;
  1425. }
  1426. /*
  1427. * copy these values now because if we drop the session
  1428. * userspace may still want to query the values since we will
  1429. * be using them for the reconnect
  1430. */
  1431. err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
  1432. &conn->portal_port, kernel_getpeername);
  1433. if (err)
  1434. goto free_socket;
  1435. err = iscsi_tcp_get_addr(conn, sock, conn->local_address,
  1436. &conn->local_port, kernel_getsockname);
  1437. if (err)
  1438. goto free_socket;
  1439. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  1440. if (err)
  1441. goto free_socket;
  1442. /* bind iSCSI connection and socket */
  1443. tcp_conn->sock = sock;
  1444. /* setup Socket parameters */
  1445. sk = sock->sk;
  1446. sk->sk_reuse = 1;
  1447. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  1448. sk->sk_allocation = GFP_ATOMIC;
  1449. /* FIXME: disable Nagle's algorithm */
  1450. /*
  1451. * Intercept TCP callbacks for sendfile like receive
  1452. * processing.
  1453. */
  1454. conn->recv_lock = &sk->sk_callback_lock;
  1455. iscsi_conn_set_callbacks(conn);
  1456. tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
  1457. /*
  1458. * set receive state machine into initial state
  1459. */
  1460. iscsi_tcp_hdr_recv_prep(tcp_conn);
  1461. return 0;
  1462. free_socket:
  1463. sockfd_put(sock);
  1464. return err;
  1465. }
  1466. /* called with host lock */
  1467. static void
  1468. iscsi_tcp_mtask_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
  1469. {
  1470. debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt);
  1471. /* Prepare PDU, optionally w/ immediate data */
  1472. iscsi_tcp_send_hdr_prep(conn, mtask->hdr, sizeof(*mtask->hdr));
  1473. /* If we have immediate data, attach a payload */
  1474. if (mtask->data_count)
  1475. iscsi_tcp_send_linear_data_prepare(conn, mtask->data,
  1476. mtask->data_count);
  1477. }
  1478. static int
  1479. iscsi_r2tpool_alloc(struct iscsi_session *session)
  1480. {
  1481. int i;
  1482. int cmd_i;
  1483. /*
  1484. * initialize per-task: R2T pool and xmit queue
  1485. */
  1486. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1487. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1488. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1489. /*
  1490. * pre-allocated x4 as much r2ts to handle race when
  1491. * target acks DataOut faster than we data_xmit() queues
  1492. * could replenish r2tqueue.
  1493. */
  1494. /* R2T pool */
  1495. if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, NULL,
  1496. sizeof(struct iscsi_r2t_info))) {
  1497. goto r2t_alloc_fail;
  1498. }
  1499. /* R2T xmit queue */
  1500. tcp_ctask->r2tqueue = kfifo_alloc(
  1501. session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
  1502. if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
  1503. iscsi_pool_free(&tcp_ctask->r2tpool);
  1504. goto r2t_alloc_fail;
  1505. }
  1506. }
  1507. return 0;
  1508. r2t_alloc_fail:
  1509. for (i = 0; i < cmd_i; i++) {
  1510. struct iscsi_cmd_task *ctask = session->cmds[i];
  1511. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1512. kfifo_free(tcp_ctask->r2tqueue);
  1513. iscsi_pool_free(&tcp_ctask->r2tpool);
  1514. }
  1515. return -ENOMEM;
  1516. }
  1517. static void
  1518. iscsi_r2tpool_free(struct iscsi_session *session)
  1519. {
  1520. int i;
  1521. for (i = 0; i < session->cmds_max; i++) {
  1522. struct iscsi_cmd_task *ctask = session->cmds[i];
  1523. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1524. kfifo_free(tcp_ctask->r2tqueue);
  1525. iscsi_pool_free(&tcp_ctask->r2tpool);
  1526. }
  1527. }
  1528. static int
  1529. iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
  1530. char *buf, int buflen)
  1531. {
  1532. struct iscsi_conn *conn = cls_conn->dd_data;
  1533. struct iscsi_session *session = conn->session;
  1534. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1535. int value;
  1536. switch(param) {
  1537. case ISCSI_PARAM_HDRDGST_EN:
  1538. iscsi_set_param(cls_conn, param, buf, buflen);
  1539. break;
  1540. case ISCSI_PARAM_DATADGST_EN:
  1541. iscsi_set_param(cls_conn, param, buf, buflen);
  1542. tcp_conn->sendpage = conn->datadgst_en ?
  1543. sock_no_sendpage : tcp_conn->sock->ops->sendpage;
  1544. break;
  1545. case ISCSI_PARAM_MAX_R2T:
  1546. sscanf(buf, "%d", &value);
  1547. if (value <= 0 || !is_power_of_2(value))
  1548. return -EINVAL;
  1549. if (session->max_r2t == value)
  1550. break;
  1551. iscsi_r2tpool_free(session);
  1552. iscsi_set_param(cls_conn, param, buf, buflen);
  1553. if (iscsi_r2tpool_alloc(session))
  1554. return -ENOMEM;
  1555. break;
  1556. default:
  1557. return iscsi_set_param(cls_conn, param, buf, buflen);
  1558. }
  1559. return 0;
  1560. }
  1561. static int
  1562. iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1563. enum iscsi_param param, char *buf)
  1564. {
  1565. struct iscsi_conn *conn = cls_conn->dd_data;
  1566. int len;
  1567. switch(param) {
  1568. case ISCSI_PARAM_CONN_PORT:
  1569. spin_lock_bh(&conn->session->lock);
  1570. len = sprintf(buf, "%hu\n", conn->portal_port);
  1571. spin_unlock_bh(&conn->session->lock);
  1572. break;
  1573. case ISCSI_PARAM_CONN_ADDRESS:
  1574. spin_lock_bh(&conn->session->lock);
  1575. len = sprintf(buf, "%s\n", conn->portal_address);
  1576. spin_unlock_bh(&conn->session->lock);
  1577. break;
  1578. default:
  1579. return iscsi_conn_get_param(cls_conn, param, buf);
  1580. }
  1581. return len;
  1582. }
  1583. static int
  1584. iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1585. char *buf)
  1586. {
  1587. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1588. int len;
  1589. switch (param) {
  1590. case ISCSI_HOST_PARAM_IPADDRESS:
  1591. spin_lock_bh(&session->lock);
  1592. if (!session->leadconn)
  1593. len = -ENODEV;
  1594. else
  1595. len = sprintf(buf, "%s\n",
  1596. session->leadconn->local_address);
  1597. spin_unlock_bh(&session->lock);
  1598. break;
  1599. default:
  1600. return iscsi_host_get_param(shost, param, buf);
  1601. }
  1602. return len;
  1603. }
  1604. static void
  1605. iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
  1606. {
  1607. struct iscsi_conn *conn = cls_conn->dd_data;
  1608. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1609. stats->txdata_octets = conn->txdata_octets;
  1610. stats->rxdata_octets = conn->rxdata_octets;
  1611. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1612. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1613. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1614. stats->datain_pdus = conn->datain_pdus_cnt;
  1615. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1616. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1617. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1618. stats->custom_length = 3;
  1619. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  1620. stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
  1621. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  1622. stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
  1623. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  1624. stats->custom[2].value = conn->eh_abort_cnt;
  1625. }
  1626. static struct iscsi_cls_session *
  1627. iscsi_tcp_session_create(struct iscsi_transport *iscsit,
  1628. struct scsi_transport_template *scsit,
  1629. uint16_t cmds_max, uint16_t qdepth,
  1630. uint32_t initial_cmdsn, uint32_t *hostno)
  1631. {
  1632. struct iscsi_cls_session *cls_session;
  1633. struct iscsi_session *session;
  1634. uint32_t hn;
  1635. int cmd_i;
  1636. cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
  1637. sizeof(struct iscsi_tcp_cmd_task),
  1638. sizeof(struct iscsi_tcp_mgmt_task),
  1639. initial_cmdsn, &hn);
  1640. if (!cls_session)
  1641. return NULL;
  1642. *hostno = hn;
  1643. session = class_to_transport_session(cls_session);
  1644. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1645. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1646. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1647. ctask->hdr = &tcp_ctask->hdr.cmd_hdr;
  1648. ctask->hdr_max = sizeof(tcp_ctask->hdr) - ISCSI_DIGEST_SIZE;
  1649. }
  1650. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1651. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1652. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1653. mtask->hdr = (struct iscsi_hdr *) &tcp_mtask->hdr;
  1654. }
  1655. if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
  1656. goto r2tpool_alloc_fail;
  1657. return cls_session;
  1658. r2tpool_alloc_fail:
  1659. iscsi_session_teardown(cls_session);
  1660. return NULL;
  1661. }
  1662. static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  1663. {
  1664. iscsi_r2tpool_free(class_to_transport_session(cls_session));
  1665. iscsi_session_teardown(cls_session);
  1666. }
  1667. static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
  1668. {
  1669. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
  1670. blk_queue_dma_alignment(sdev->request_queue, 0);
  1671. return 0;
  1672. }
  1673. static struct scsi_host_template iscsi_sht = {
  1674. .module = THIS_MODULE,
  1675. .name = "iSCSI Initiator over TCP/IP",
  1676. .queuecommand = iscsi_queuecommand,
  1677. .change_queue_depth = iscsi_change_queue_depth,
  1678. .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
  1679. .sg_tablesize = 4096,
  1680. .max_sectors = 0xFFFF,
  1681. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  1682. .eh_abort_handler = iscsi_eh_abort,
  1683. .eh_device_reset_handler= iscsi_eh_device_reset,
  1684. .eh_host_reset_handler = iscsi_eh_host_reset,
  1685. .use_clustering = DISABLE_CLUSTERING,
  1686. .slave_configure = iscsi_tcp_slave_configure,
  1687. .proc_name = "iscsi_tcp",
  1688. .this_id = -1,
  1689. };
  1690. static struct iscsi_transport iscsi_tcp_transport = {
  1691. .owner = THIS_MODULE,
  1692. .name = "tcp",
  1693. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  1694. | CAP_DATADGST,
  1695. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  1696. ISCSI_MAX_XMIT_DLENGTH |
  1697. ISCSI_HDRDGST_EN |
  1698. ISCSI_DATADGST_EN |
  1699. ISCSI_INITIAL_R2T_EN |
  1700. ISCSI_MAX_R2T |
  1701. ISCSI_IMM_DATA_EN |
  1702. ISCSI_FIRST_BURST |
  1703. ISCSI_MAX_BURST |
  1704. ISCSI_PDU_INORDER_EN |
  1705. ISCSI_DATASEQ_INORDER_EN |
  1706. ISCSI_ERL |
  1707. ISCSI_CONN_PORT |
  1708. ISCSI_CONN_ADDRESS |
  1709. ISCSI_EXP_STATSN |
  1710. ISCSI_PERSISTENT_PORT |
  1711. ISCSI_PERSISTENT_ADDRESS |
  1712. ISCSI_TARGET_NAME | ISCSI_TPGT |
  1713. ISCSI_USERNAME | ISCSI_PASSWORD |
  1714. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  1715. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  1716. ISCSI_LU_RESET_TMO |
  1717. ISCSI_PING_TMO | ISCSI_RECV_TMO,
  1718. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  1719. ISCSI_HOST_INITIATOR_NAME |
  1720. ISCSI_HOST_NETDEV_NAME,
  1721. .host_template = &iscsi_sht,
  1722. .conndata_size = sizeof(struct iscsi_conn),
  1723. .max_conn = 1,
  1724. .max_cmd_len = 16,
  1725. /* session management */
  1726. .create_session = iscsi_tcp_session_create,
  1727. .destroy_session = iscsi_tcp_session_destroy,
  1728. /* connection management */
  1729. .create_conn = iscsi_tcp_conn_create,
  1730. .bind_conn = iscsi_tcp_conn_bind,
  1731. .destroy_conn = iscsi_tcp_conn_destroy,
  1732. .set_param = iscsi_conn_set_param,
  1733. .get_conn_param = iscsi_tcp_conn_get_param,
  1734. .get_session_param = iscsi_session_get_param,
  1735. .start_conn = iscsi_conn_start,
  1736. .stop_conn = iscsi_tcp_conn_stop,
  1737. /* iscsi host params */
  1738. .get_host_param = iscsi_tcp_host_get_param,
  1739. .set_host_param = iscsi_host_set_param,
  1740. /* IO */
  1741. .send_pdu = iscsi_conn_send_pdu,
  1742. .get_stats = iscsi_conn_get_stats,
  1743. .init_cmd_task = iscsi_tcp_ctask_init,
  1744. .init_mgmt_task = iscsi_tcp_mtask_init,
  1745. .xmit_cmd_task = iscsi_tcp_ctask_xmit,
  1746. .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
  1747. .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
  1748. /* recovery */
  1749. .session_recovery_timedout = iscsi_session_recovery_timedout,
  1750. };
  1751. static int __init
  1752. iscsi_tcp_init(void)
  1753. {
  1754. if (iscsi_max_lun < 1) {
  1755. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  1756. iscsi_max_lun);
  1757. return -EINVAL;
  1758. }
  1759. iscsi_tcp_transport.max_lun = iscsi_max_lun;
  1760. if (!iscsi_register_transport(&iscsi_tcp_transport))
  1761. return -ENODEV;
  1762. return 0;
  1763. }
  1764. static void __exit
  1765. iscsi_tcp_exit(void)
  1766. {
  1767. iscsi_unregister_transport(&iscsi_tcp_transport);
  1768. }
  1769. module_init(iscsi_tcp_init);
  1770. module_exit(iscsi_tcp_exit);