iscsi_tcp.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029
  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. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  472. if (tcp_conn->in.datalen == 0)
  473. return 0;
  474. if (tcp_ctask->exp_datasn != datasn) {
  475. debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->datasn(%d)\n",
  476. __FUNCTION__, tcp_ctask->exp_datasn, datasn);
  477. return ISCSI_ERR_DATASN;
  478. }
  479. tcp_ctask->exp_datasn++;
  480. tcp_ctask->data_offset = be32_to_cpu(rhdr->offset);
  481. if (tcp_ctask->data_offset + tcp_conn->in.datalen > scsi_bufflen(sc)) {
  482. debug_tcp("%s: data_offset(%d) + data_len(%d) > total_length_in(%d)\n",
  483. __FUNCTION__, tcp_ctask->data_offset,
  484. tcp_conn->in.datalen, scsi_bufflen(sc));
  485. return ISCSI_ERR_DATA_OFFSET;
  486. }
  487. if (rhdr->flags & ISCSI_FLAG_DATA_STATUS) {
  488. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  489. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  490. if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW |
  491. ISCSI_FLAG_DATA_OVERFLOW)) {
  492. int res_count = be32_to_cpu(rhdr->residual_count);
  493. if (res_count > 0 &&
  494. (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
  495. res_count <= scsi_bufflen(sc)))
  496. scsi_set_resid(sc, res_count);
  497. else
  498. sc->result = (DID_BAD_TARGET << 16) |
  499. rhdr->cmd_status;
  500. }
  501. }
  502. conn->datain_pdus_cnt++;
  503. return 0;
  504. }
  505. /**
  506. * iscsi_solicit_data_init - initialize first Data-Out
  507. * @conn: iscsi connection
  508. * @ctask: scsi command task
  509. * @r2t: R2T info
  510. *
  511. * Notes:
  512. * Initialize first Data-Out within this R2T sequence and finds
  513. * proper data_offset within this SCSI command.
  514. *
  515. * This function is called with connection lock taken.
  516. **/
  517. static void
  518. iscsi_solicit_data_init(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  519. struct iscsi_r2t_info *r2t)
  520. {
  521. struct iscsi_data *hdr;
  522. hdr = &r2t->dtask.hdr;
  523. memset(hdr, 0, sizeof(struct iscsi_data));
  524. hdr->ttt = r2t->ttt;
  525. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  526. r2t->solicit_datasn++;
  527. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  528. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  529. hdr->itt = ctask->hdr->itt;
  530. hdr->exp_statsn = r2t->exp_statsn;
  531. hdr->offset = cpu_to_be32(r2t->data_offset);
  532. if (r2t->data_length > conn->max_xmit_dlength) {
  533. hton24(hdr->dlength, conn->max_xmit_dlength);
  534. r2t->data_count = conn->max_xmit_dlength;
  535. hdr->flags = 0;
  536. } else {
  537. hton24(hdr->dlength, r2t->data_length);
  538. r2t->data_count = r2t->data_length;
  539. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  540. }
  541. conn->dataout_pdus_cnt++;
  542. r2t->sent = 0;
  543. }
  544. /**
  545. * iscsi_r2t_rsp - iSCSI R2T Response processing
  546. * @conn: iscsi connection
  547. * @ctask: scsi command task
  548. **/
  549. static int
  550. iscsi_r2t_rsp(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  551. {
  552. struct iscsi_r2t_info *r2t;
  553. struct iscsi_session *session = conn->session;
  554. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  555. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  556. struct iscsi_r2t_rsp *rhdr = (struct iscsi_r2t_rsp *)tcp_conn->in.hdr;
  557. int r2tsn = be32_to_cpu(rhdr->r2tsn);
  558. int rc;
  559. if (tcp_conn->in.datalen) {
  560. iscsi_conn_printk(KERN_ERR, conn,
  561. "invalid R2t with datalen %d\n",
  562. tcp_conn->in.datalen);
  563. return ISCSI_ERR_DATALEN;
  564. }
  565. if (tcp_ctask->exp_datasn != r2tsn){
  566. debug_tcp("%s: ctask->exp_datasn(%d) != rhdr->r2tsn(%d)\n",
  567. __FUNCTION__, tcp_ctask->exp_datasn, r2tsn);
  568. return ISCSI_ERR_R2TSN;
  569. }
  570. /* fill-in new R2T associated with the task */
  571. iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
  572. if (!ctask->sc || session->state != ISCSI_STATE_LOGGED_IN) {
  573. iscsi_conn_printk(KERN_INFO, conn,
  574. "dropping R2T itt %d in recovery.\n",
  575. ctask->itt);
  576. return 0;
  577. }
  578. rc = __kfifo_get(tcp_ctask->r2tpool.queue, (void*)&r2t, sizeof(void*));
  579. BUG_ON(!rc);
  580. r2t->exp_statsn = rhdr->statsn;
  581. r2t->data_length = be32_to_cpu(rhdr->data_length);
  582. if (r2t->data_length == 0) {
  583. iscsi_conn_printk(KERN_ERR, conn,
  584. "invalid R2T with zero data len\n");
  585. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  586. sizeof(void*));
  587. return ISCSI_ERR_DATALEN;
  588. }
  589. if (r2t->data_length > session->max_burst)
  590. debug_scsi("invalid R2T with data len %u and max burst %u."
  591. "Attempting to execute request.\n",
  592. r2t->data_length, session->max_burst);
  593. r2t->data_offset = be32_to_cpu(rhdr->data_offset);
  594. if (r2t->data_offset + r2t->data_length > scsi_bufflen(ctask->sc)) {
  595. iscsi_conn_printk(KERN_ERR, conn,
  596. "invalid R2T with data len %u at offset %u "
  597. "and total length %d\n", r2t->data_length,
  598. r2t->data_offset, scsi_bufflen(ctask->sc));
  599. __kfifo_put(tcp_ctask->r2tpool.queue, (void*)&r2t,
  600. sizeof(void*));
  601. return ISCSI_ERR_DATALEN;
  602. }
  603. r2t->ttt = rhdr->ttt; /* no flip */
  604. r2t->solicit_datasn = 0;
  605. iscsi_solicit_data_init(conn, ctask, r2t);
  606. tcp_ctask->exp_datasn = r2tsn + 1;
  607. __kfifo_put(tcp_ctask->r2tqueue, (void*)&r2t, sizeof(void*));
  608. conn->r2t_pdus_cnt++;
  609. iscsi_requeue_ctask(ctask);
  610. return 0;
  611. }
  612. /*
  613. * Handle incoming reply to DataIn command
  614. */
  615. static int
  616. iscsi_tcp_process_data_in(struct iscsi_tcp_conn *tcp_conn,
  617. struct iscsi_segment *segment)
  618. {
  619. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  620. struct iscsi_hdr *hdr = tcp_conn->in.hdr;
  621. int rc;
  622. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  623. return ISCSI_ERR_DATA_DGST;
  624. /* check for non-exceptional status */
  625. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  626. rc = iscsi_complete_pdu(conn, tcp_conn->in.hdr, NULL, 0);
  627. if (rc)
  628. return rc;
  629. }
  630. iscsi_tcp_hdr_recv_prep(tcp_conn);
  631. return 0;
  632. }
  633. /**
  634. * iscsi_tcp_hdr_dissect - process PDU header
  635. * @conn: iSCSI connection
  636. * @hdr: PDU header
  637. *
  638. * This function analyzes the header of the PDU received,
  639. * and performs several sanity checks. If the PDU is accompanied
  640. * by data, the receive buffer is set up to copy the incoming data
  641. * to the correct location.
  642. */
  643. static int
  644. iscsi_tcp_hdr_dissect(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  645. {
  646. int rc = 0, opcode, ahslen;
  647. struct iscsi_session *session = conn->session;
  648. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  649. struct iscsi_cmd_task *ctask;
  650. uint32_t itt;
  651. /* verify PDU length */
  652. tcp_conn->in.datalen = ntoh24(hdr->dlength);
  653. if (tcp_conn->in.datalen > conn->max_recv_dlength) {
  654. iscsi_conn_printk(KERN_ERR, conn,
  655. "iscsi_tcp: datalen %d > %d\n",
  656. tcp_conn->in.datalen, conn->max_recv_dlength);
  657. return ISCSI_ERR_DATALEN;
  658. }
  659. /* Additional header segments. So far, we don't
  660. * process additional headers.
  661. */
  662. ahslen = hdr->hlength << 2;
  663. opcode = hdr->opcode & ISCSI_OPCODE_MASK;
  664. /* verify itt (itt encoding: age+cid+itt) */
  665. rc = iscsi_verify_itt(conn, hdr, &itt);
  666. if (rc)
  667. return rc;
  668. debug_tcp("opcode 0x%x ahslen %d datalen %d\n",
  669. opcode, ahslen, tcp_conn->in.datalen);
  670. switch(opcode) {
  671. case ISCSI_OP_SCSI_DATA_IN:
  672. ctask = session->cmds[itt];
  673. spin_lock(&conn->session->lock);
  674. rc = iscsi_data_rsp(conn, ctask);
  675. spin_unlock(&conn->session->lock);
  676. if (rc)
  677. return rc;
  678. if (tcp_conn->in.datalen) {
  679. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  680. struct hash_desc *rx_hash = NULL;
  681. /*
  682. * Setup copy of Data-In into the Scsi_Cmnd
  683. * Scatterlist case:
  684. * We set up the iscsi_segment to point to the next
  685. * scatterlist entry to copy to. As we go along,
  686. * we move on to the next scatterlist entry and
  687. * update the digest per-entry.
  688. */
  689. if (conn->datadgst_en)
  690. rx_hash = &tcp_conn->rx_hash;
  691. debug_tcp("iscsi_tcp_begin_data_in(%p, offset=%d, "
  692. "datalen=%d)\n", tcp_conn,
  693. tcp_ctask->data_offset,
  694. tcp_conn->in.datalen);
  695. return iscsi_segment_seek_sg(&tcp_conn->in.segment,
  696. scsi_sglist(ctask->sc),
  697. scsi_sg_count(ctask->sc),
  698. tcp_ctask->data_offset,
  699. tcp_conn->in.datalen,
  700. iscsi_tcp_process_data_in,
  701. rx_hash);
  702. }
  703. /* fall through */
  704. case ISCSI_OP_SCSI_CMD_RSP:
  705. if (tcp_conn->in.datalen) {
  706. iscsi_tcp_data_recv_prep(tcp_conn);
  707. return 0;
  708. }
  709. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  710. break;
  711. case ISCSI_OP_R2T:
  712. ctask = session->cmds[itt];
  713. if (ahslen)
  714. rc = ISCSI_ERR_AHSLEN;
  715. else if (ctask->sc->sc_data_direction == DMA_TO_DEVICE) {
  716. spin_lock(&session->lock);
  717. rc = iscsi_r2t_rsp(conn, ctask);
  718. spin_unlock(&session->lock);
  719. } else
  720. rc = ISCSI_ERR_PROTO;
  721. break;
  722. case ISCSI_OP_LOGIN_RSP:
  723. case ISCSI_OP_TEXT_RSP:
  724. case ISCSI_OP_REJECT:
  725. case ISCSI_OP_ASYNC_EVENT:
  726. /*
  727. * It is possible that we could get a PDU with a buffer larger
  728. * than 8K, but there are no targets that currently do this.
  729. * For now we fail until we find a vendor that needs it
  730. */
  731. if (ISCSI_DEF_MAX_RECV_SEG_LEN < tcp_conn->in.datalen) {
  732. iscsi_conn_printk(KERN_ERR, conn,
  733. "iscsi_tcp: received buffer of "
  734. "len %u but conn buffer is only %u "
  735. "(opcode %0x)\n",
  736. tcp_conn->in.datalen,
  737. ISCSI_DEF_MAX_RECV_SEG_LEN, opcode);
  738. rc = ISCSI_ERR_PROTO;
  739. break;
  740. }
  741. /* If there's data coming in with the response,
  742. * receive it to the connection's buffer.
  743. */
  744. if (tcp_conn->in.datalen) {
  745. iscsi_tcp_data_recv_prep(tcp_conn);
  746. return 0;
  747. }
  748. /* fall through */
  749. case ISCSI_OP_LOGOUT_RSP:
  750. case ISCSI_OP_NOOP_IN:
  751. case ISCSI_OP_SCSI_TMFUNC_RSP:
  752. rc = iscsi_complete_pdu(conn, hdr, NULL, 0);
  753. break;
  754. default:
  755. rc = ISCSI_ERR_BAD_OPCODE;
  756. break;
  757. }
  758. if (rc == 0) {
  759. /* Anything that comes with data should have
  760. * been handled above. */
  761. if (tcp_conn->in.datalen)
  762. return ISCSI_ERR_PROTO;
  763. iscsi_tcp_hdr_recv_prep(tcp_conn);
  764. }
  765. return rc;
  766. }
  767. /**
  768. * iscsi_tcp_hdr_recv_done - process PDU header
  769. *
  770. * This is the callback invoked when the PDU header has
  771. * been received. If the header is followed by additional
  772. * header segments, we go back for more data.
  773. */
  774. static int
  775. iscsi_tcp_hdr_recv_done(struct iscsi_tcp_conn *tcp_conn,
  776. struct iscsi_segment *segment)
  777. {
  778. struct iscsi_conn *conn = tcp_conn->iscsi_conn;
  779. struct iscsi_hdr *hdr;
  780. /* Check if there are additional header segments
  781. * *prior* to computing the digest, because we
  782. * may need to go back to the caller for more.
  783. */
  784. hdr = (struct iscsi_hdr *) tcp_conn->in.hdr_buf;
  785. if (segment->copied == sizeof(struct iscsi_hdr) && hdr->hlength) {
  786. /* Bump the header length - the caller will
  787. * just loop around and get the AHS for us, and
  788. * call again. */
  789. unsigned int ahslen = hdr->hlength << 2;
  790. /* Make sure we don't overflow */
  791. if (sizeof(*hdr) + ahslen > sizeof(tcp_conn->in.hdr_buf))
  792. return ISCSI_ERR_AHSLEN;
  793. segment->total_size += ahslen;
  794. segment->size += ahslen;
  795. return 0;
  796. }
  797. /* We're done processing the header. See if we're doing
  798. * header digests; if so, set up the recv_digest buffer
  799. * and go back for more. */
  800. if (conn->hdrdgst_en) {
  801. if (segment->digest_len == 0) {
  802. iscsi_tcp_segment_splice_digest(segment,
  803. segment->recv_digest);
  804. return 0;
  805. }
  806. iscsi_tcp_dgst_header(&tcp_conn->rx_hash, hdr,
  807. segment->total_copied - ISCSI_DIGEST_SIZE,
  808. segment->digest);
  809. if (!iscsi_tcp_dgst_verify(tcp_conn, segment))
  810. return ISCSI_ERR_HDR_DGST;
  811. }
  812. tcp_conn->in.hdr = hdr;
  813. return iscsi_tcp_hdr_dissect(conn, hdr);
  814. }
  815. /**
  816. * iscsi_tcp_recv - TCP receive in sendfile fashion
  817. * @rd_desc: read descriptor
  818. * @skb: socket buffer
  819. * @offset: offset in skb
  820. * @len: skb->len - offset
  821. **/
  822. static int
  823. iscsi_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  824. unsigned int offset, size_t len)
  825. {
  826. struct iscsi_conn *conn = rd_desc->arg.data;
  827. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  828. struct iscsi_segment *segment = &tcp_conn->in.segment;
  829. struct skb_seq_state seq;
  830. unsigned int consumed = 0;
  831. int rc = 0;
  832. debug_tcp("in %d bytes\n", skb->len - offset);
  833. if (unlikely(conn->suspend_rx)) {
  834. debug_tcp("conn %d Rx suspended!\n", conn->id);
  835. return 0;
  836. }
  837. skb_prepare_seq_read(skb, offset, skb->len, &seq);
  838. while (1) {
  839. unsigned int avail;
  840. const u8 *ptr;
  841. avail = skb_seq_read(consumed, &ptr, &seq);
  842. if (avail == 0) {
  843. debug_tcp("no more data avail. Consumed %d\n",
  844. consumed);
  845. break;
  846. }
  847. BUG_ON(segment->copied >= segment->size);
  848. debug_tcp("skb %p ptr=%p avail=%u\n", skb, ptr, avail);
  849. rc = iscsi_tcp_segment_recv(tcp_conn, segment, ptr, avail);
  850. BUG_ON(rc == 0);
  851. consumed += rc;
  852. if (segment->total_copied >= segment->total_size) {
  853. debug_tcp("segment done\n");
  854. rc = segment->done(tcp_conn, segment);
  855. if (rc != 0) {
  856. skb_abort_seq_read(&seq);
  857. goto error;
  858. }
  859. /* The done() functions sets up the
  860. * next segment. */
  861. }
  862. }
  863. skb_abort_seq_read(&seq);
  864. conn->rxdata_octets += consumed;
  865. return consumed;
  866. error:
  867. debug_tcp("Error receiving PDU, errno=%d\n", rc);
  868. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  869. return 0;
  870. }
  871. static void
  872. iscsi_tcp_data_ready(struct sock *sk, int flag)
  873. {
  874. struct iscsi_conn *conn = sk->sk_user_data;
  875. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  876. read_descriptor_t rd_desc;
  877. read_lock(&sk->sk_callback_lock);
  878. /*
  879. * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
  880. * We set count to 1 because we want the network layer to
  881. * hand us all the skbs that are available. iscsi_tcp_recv
  882. * handled pdus that cross buffers or pdus that still need data.
  883. */
  884. rd_desc.arg.data = conn;
  885. rd_desc.count = 1;
  886. tcp_read_sock(sk, &rd_desc, iscsi_tcp_recv);
  887. read_unlock(&sk->sk_callback_lock);
  888. /* If we had to (atomically) map a highmem page,
  889. * unmap it now. */
  890. iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
  891. }
  892. static void
  893. iscsi_tcp_state_change(struct sock *sk)
  894. {
  895. struct iscsi_tcp_conn *tcp_conn;
  896. struct iscsi_conn *conn;
  897. struct iscsi_session *session;
  898. void (*old_state_change)(struct sock *);
  899. read_lock(&sk->sk_callback_lock);
  900. conn = (struct iscsi_conn*)sk->sk_user_data;
  901. session = conn->session;
  902. if ((sk->sk_state == TCP_CLOSE_WAIT ||
  903. sk->sk_state == TCP_CLOSE) &&
  904. !atomic_read(&sk->sk_rmem_alloc)) {
  905. debug_tcp("iscsi_tcp_state_change: TCP_CLOSE|TCP_CLOSE_WAIT\n");
  906. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  907. }
  908. tcp_conn = conn->dd_data;
  909. old_state_change = tcp_conn->old_state_change;
  910. read_unlock(&sk->sk_callback_lock);
  911. old_state_change(sk);
  912. }
  913. /**
  914. * iscsi_write_space - Called when more output buffer space is available
  915. * @sk: socket space is available for
  916. **/
  917. static void
  918. iscsi_write_space(struct sock *sk)
  919. {
  920. struct iscsi_conn *conn = (struct iscsi_conn*)sk->sk_user_data;
  921. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  922. tcp_conn->old_write_space(sk);
  923. debug_tcp("iscsi_write_space: cid %d\n", conn->id);
  924. scsi_queue_work(conn->session->host, &conn->xmitwork);
  925. }
  926. static void
  927. iscsi_conn_set_callbacks(struct iscsi_conn *conn)
  928. {
  929. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  930. struct sock *sk = tcp_conn->sock->sk;
  931. /* assign new callbacks */
  932. write_lock_bh(&sk->sk_callback_lock);
  933. sk->sk_user_data = conn;
  934. tcp_conn->old_data_ready = sk->sk_data_ready;
  935. tcp_conn->old_state_change = sk->sk_state_change;
  936. tcp_conn->old_write_space = sk->sk_write_space;
  937. sk->sk_data_ready = iscsi_tcp_data_ready;
  938. sk->sk_state_change = iscsi_tcp_state_change;
  939. sk->sk_write_space = iscsi_write_space;
  940. write_unlock_bh(&sk->sk_callback_lock);
  941. }
  942. static void
  943. iscsi_conn_restore_callbacks(struct iscsi_tcp_conn *tcp_conn)
  944. {
  945. struct sock *sk = tcp_conn->sock->sk;
  946. /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
  947. write_lock_bh(&sk->sk_callback_lock);
  948. sk->sk_user_data = NULL;
  949. sk->sk_data_ready = tcp_conn->old_data_ready;
  950. sk->sk_state_change = tcp_conn->old_state_change;
  951. sk->sk_write_space = tcp_conn->old_write_space;
  952. sk->sk_no_check = 0;
  953. write_unlock_bh(&sk->sk_callback_lock);
  954. }
  955. /**
  956. * iscsi_xmit - TCP transmit
  957. **/
  958. static int
  959. iscsi_xmit(struct iscsi_conn *conn)
  960. {
  961. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  962. struct iscsi_segment *segment = &tcp_conn->out.segment;
  963. unsigned int consumed = 0;
  964. int rc = 0;
  965. while (1) {
  966. rc = iscsi_tcp_xmit_segment(tcp_conn, segment);
  967. if (rc < 0)
  968. goto error;
  969. if (rc == 0)
  970. break;
  971. consumed += rc;
  972. if (segment->total_copied >= segment->total_size) {
  973. if (segment->done != NULL) {
  974. rc = segment->done(tcp_conn, segment);
  975. if (rc < 0)
  976. goto error;
  977. }
  978. }
  979. }
  980. debug_tcp("xmit %d bytes\n", consumed);
  981. conn->txdata_octets += consumed;
  982. return consumed;
  983. error:
  984. /* Transmit error. We could initiate error recovery
  985. * here. */
  986. debug_tcp("Error sending PDU, errno=%d\n", rc);
  987. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  988. return rc;
  989. }
  990. /**
  991. * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
  992. */
  993. static inline int
  994. iscsi_tcp_xmit_qlen(struct iscsi_conn *conn)
  995. {
  996. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  997. struct iscsi_segment *segment = &tcp_conn->out.segment;
  998. return segment->total_copied - segment->total_size;
  999. }
  1000. static inline int
  1001. iscsi_tcp_flush(struct iscsi_conn *conn)
  1002. {
  1003. int rc;
  1004. while (iscsi_tcp_xmit_qlen(conn)) {
  1005. rc = iscsi_xmit(conn);
  1006. if (rc == 0)
  1007. return -EAGAIN;
  1008. if (rc < 0)
  1009. return rc;
  1010. }
  1011. return 0;
  1012. }
  1013. /*
  1014. * This is called when we're done sending the header.
  1015. * Simply copy the data_segment to the send segment, and return.
  1016. */
  1017. static int
  1018. iscsi_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
  1019. struct iscsi_segment *segment)
  1020. {
  1021. tcp_conn->out.segment = tcp_conn->out.data_segment;
  1022. debug_tcp("Header done. Next segment size %u total_size %u\n",
  1023. tcp_conn->out.segment.size, tcp_conn->out.segment.total_size);
  1024. return 0;
  1025. }
  1026. static void
  1027. iscsi_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, size_t hdrlen)
  1028. {
  1029. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1030. debug_tcp("%s(%p%s)\n", __FUNCTION__, tcp_conn,
  1031. conn->hdrdgst_en? ", digest enabled" : "");
  1032. /* Clear the data segment - needs to be filled in by the
  1033. * caller using iscsi_tcp_send_data_prep() */
  1034. memset(&tcp_conn->out.data_segment, 0, sizeof(struct iscsi_segment));
  1035. /* If header digest is enabled, compute the CRC and
  1036. * place the digest into the same buffer. We make
  1037. * sure that both iscsi_tcp_ctask and mtask have
  1038. * sufficient room.
  1039. */
  1040. if (conn->hdrdgst_en) {
  1041. iscsi_tcp_dgst_header(&tcp_conn->tx_hash, hdr, hdrlen,
  1042. hdr + hdrlen);
  1043. hdrlen += ISCSI_DIGEST_SIZE;
  1044. }
  1045. /* Remember header pointer for later, when we need
  1046. * to decide whether there's a payload to go along
  1047. * with the header. */
  1048. tcp_conn->out.hdr = hdr;
  1049. iscsi_segment_init_linear(&tcp_conn->out.segment, hdr, hdrlen,
  1050. iscsi_tcp_send_hdr_done, NULL);
  1051. }
  1052. /*
  1053. * Prepare the send buffer for the payload data.
  1054. * Padding and checksumming will all be taken care
  1055. * of by the iscsi_segment routines.
  1056. */
  1057. static int
  1058. iscsi_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
  1059. unsigned int count, unsigned int offset,
  1060. unsigned int len)
  1061. {
  1062. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1063. struct hash_desc *tx_hash = NULL;
  1064. unsigned int hdr_spec_len;
  1065. debug_tcp("%s(%p, offset=%d, datalen=%d%s)\n", __FUNCTION__,
  1066. tcp_conn, offset, len,
  1067. conn->datadgst_en? ", digest enabled" : "");
  1068. /* Make sure the datalen matches what the caller
  1069. said he would send. */
  1070. hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
  1071. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  1072. if (conn->datadgst_en)
  1073. tx_hash = &tcp_conn->tx_hash;
  1074. return iscsi_segment_seek_sg(&tcp_conn->out.data_segment,
  1075. sg, count, offset, len,
  1076. NULL, tx_hash);
  1077. }
  1078. static void
  1079. iscsi_tcp_send_linear_data_prepare(struct iscsi_conn *conn, void *data,
  1080. size_t len)
  1081. {
  1082. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1083. struct hash_desc *tx_hash = NULL;
  1084. unsigned int hdr_spec_len;
  1085. debug_tcp("%s(%p, datalen=%d%s)\n", __FUNCTION__, tcp_conn, len,
  1086. conn->datadgst_en? ", digest enabled" : "");
  1087. /* Make sure the datalen matches what the caller
  1088. said he would send. */
  1089. hdr_spec_len = ntoh24(tcp_conn->out.hdr->dlength);
  1090. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  1091. if (conn->datadgst_en)
  1092. tx_hash = &tcp_conn->tx_hash;
  1093. iscsi_segment_init_linear(&tcp_conn->out.data_segment,
  1094. data, len, NULL, tx_hash);
  1095. }
  1096. /**
  1097. * iscsi_solicit_data_cont - initialize next Data-Out
  1098. * @conn: iscsi connection
  1099. * @ctask: scsi command task
  1100. * @r2t: R2T info
  1101. * @left: bytes left to transfer
  1102. *
  1103. * Notes:
  1104. * Initialize next Data-Out within this R2T sequence and continue
  1105. * to process next Scatter-Gather element(if any) of this SCSI command.
  1106. *
  1107. * Called under connection lock.
  1108. **/
  1109. static int
  1110. iscsi_solicit_data_cont(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1111. struct iscsi_r2t_info *r2t)
  1112. {
  1113. struct iscsi_data *hdr;
  1114. int new_offset, left;
  1115. BUG_ON(r2t->data_length - r2t->sent < 0);
  1116. left = r2t->data_length - r2t->sent;
  1117. if (left == 0)
  1118. return 0;
  1119. hdr = &r2t->dtask.hdr;
  1120. memset(hdr, 0, sizeof(struct iscsi_data));
  1121. hdr->ttt = r2t->ttt;
  1122. hdr->datasn = cpu_to_be32(r2t->solicit_datasn);
  1123. r2t->solicit_datasn++;
  1124. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  1125. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  1126. hdr->itt = ctask->hdr->itt;
  1127. hdr->exp_statsn = r2t->exp_statsn;
  1128. new_offset = r2t->data_offset + r2t->sent;
  1129. hdr->offset = cpu_to_be32(new_offset);
  1130. if (left > conn->max_xmit_dlength) {
  1131. hton24(hdr->dlength, conn->max_xmit_dlength);
  1132. r2t->data_count = conn->max_xmit_dlength;
  1133. } else {
  1134. hton24(hdr->dlength, left);
  1135. r2t->data_count = left;
  1136. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  1137. }
  1138. conn->dataout_pdus_cnt++;
  1139. return 1;
  1140. }
  1141. /**
  1142. * iscsi_tcp_ctask - Initialize iSCSI SCSI_READ or SCSI_WRITE commands
  1143. * @conn: iscsi connection
  1144. * @ctask: scsi command task
  1145. * @sc: scsi command
  1146. **/
  1147. static int
  1148. iscsi_tcp_ctask_init(struct iscsi_cmd_task *ctask)
  1149. {
  1150. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1151. struct iscsi_conn *conn = ctask->conn;
  1152. struct scsi_cmnd *sc = ctask->sc;
  1153. int err;
  1154. BUG_ON(__kfifo_len(tcp_ctask->r2tqueue));
  1155. tcp_ctask->sent = 0;
  1156. tcp_ctask->exp_datasn = 0;
  1157. /* Prepare PDU, optionally w/ immediate data */
  1158. debug_scsi("ctask deq [cid %d itt 0x%x imm %d unsol %d]\n",
  1159. conn->id, ctask->itt, ctask->imm_count,
  1160. ctask->unsol_count);
  1161. iscsi_tcp_send_hdr_prep(conn, ctask->hdr, ctask->hdr_len);
  1162. if (!ctask->imm_count)
  1163. return 0;
  1164. /* If we have immediate data, attach a payload */
  1165. err = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc), scsi_sg_count(sc),
  1166. 0, ctask->imm_count);
  1167. if (err)
  1168. return err;
  1169. tcp_ctask->sent += ctask->imm_count;
  1170. ctask->imm_count = 0;
  1171. return 0;
  1172. }
  1173. /**
  1174. * iscsi_tcp_mtask_xmit - xmit management(immediate) task
  1175. * @conn: iscsi connection
  1176. * @mtask: task management task
  1177. *
  1178. * Notes:
  1179. * The function can return -EAGAIN in which case caller must
  1180. * call it again later, or recover. '0' return code means successful
  1181. * xmit.
  1182. **/
  1183. static int
  1184. iscsi_tcp_mtask_xmit(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
  1185. {
  1186. int rc;
  1187. /* Flush any pending data first. */
  1188. rc = iscsi_tcp_flush(conn);
  1189. if (rc < 0)
  1190. return rc;
  1191. if (mtask->hdr->itt == RESERVED_ITT) {
  1192. struct iscsi_session *session = conn->session;
  1193. spin_lock_bh(&session->lock);
  1194. iscsi_free_mgmt_task(conn, mtask);
  1195. spin_unlock_bh(&session->lock);
  1196. }
  1197. return 0;
  1198. }
  1199. /*
  1200. * iscsi_tcp_ctask_xmit - xmit normal PDU task
  1201. * @conn: iscsi connection
  1202. * @ctask: iscsi command task
  1203. *
  1204. * We're expected to return 0 when everything was transmitted succesfully,
  1205. * -EAGAIN if there's still data in the queue, or != 0 for any other kind
  1206. * of error.
  1207. */
  1208. static int
  1209. iscsi_tcp_ctask_xmit(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask)
  1210. {
  1211. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1212. struct scsi_cmnd *sc = ctask->sc;
  1213. int rc = 0;
  1214. flush:
  1215. /* Flush any pending data first. */
  1216. rc = iscsi_tcp_flush(conn);
  1217. if (rc < 0)
  1218. return rc;
  1219. /* Are we done already? */
  1220. if (sc->sc_data_direction != DMA_TO_DEVICE)
  1221. return 0;
  1222. if (ctask->unsol_count != 0) {
  1223. struct iscsi_data *hdr = &tcp_ctask->unsol_dtask.hdr;
  1224. /* Prepare a header for the unsolicited PDU.
  1225. * The amount of data we want to send will be
  1226. * in ctask->data_count.
  1227. * FIXME: return the data count instead.
  1228. */
  1229. iscsi_prep_unsolicit_data_pdu(ctask, hdr);
  1230. debug_tcp("unsol dout [itt 0x%x doff %d dlen %d]\n",
  1231. ctask->itt, tcp_ctask->sent, ctask->data_count);
  1232. iscsi_tcp_send_hdr_prep(conn, hdr, sizeof(*hdr));
  1233. rc = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc),
  1234. scsi_sg_count(sc),
  1235. tcp_ctask->sent,
  1236. ctask->data_count);
  1237. if (rc)
  1238. goto fail;
  1239. tcp_ctask->sent += ctask->data_count;
  1240. ctask->unsol_count -= ctask->data_count;
  1241. goto flush;
  1242. } else {
  1243. struct iscsi_session *session = conn->session;
  1244. struct iscsi_r2t_info *r2t;
  1245. /* All unsolicited PDUs sent. Check for solicited PDUs.
  1246. */
  1247. spin_lock_bh(&session->lock);
  1248. r2t = tcp_ctask->r2t;
  1249. if (r2t != NULL) {
  1250. /* Continue with this R2T? */
  1251. if (!iscsi_solicit_data_cont(conn, ctask, r2t)) {
  1252. debug_scsi(" done with r2t %p\n", r2t);
  1253. __kfifo_put(tcp_ctask->r2tpool.queue,
  1254. (void*)&r2t, sizeof(void*));
  1255. tcp_ctask->r2t = r2t = NULL;
  1256. }
  1257. }
  1258. if (r2t == NULL) {
  1259. __kfifo_get(tcp_ctask->r2tqueue, (void*)&tcp_ctask->r2t,
  1260. sizeof(void*));
  1261. r2t = tcp_ctask->r2t;
  1262. }
  1263. spin_unlock_bh(&session->lock);
  1264. /* Waiting for more R2Ts to arrive. */
  1265. if (r2t == NULL) {
  1266. debug_tcp("no R2Ts yet\n");
  1267. return 0;
  1268. }
  1269. debug_scsi("sol dout %p [dsn %d itt 0x%x doff %d dlen %d]\n",
  1270. r2t, r2t->solicit_datasn - 1, ctask->itt,
  1271. r2t->data_offset + r2t->sent, r2t->data_count);
  1272. iscsi_tcp_send_hdr_prep(conn, &r2t->dtask.hdr,
  1273. sizeof(struct iscsi_hdr));
  1274. rc = iscsi_tcp_send_data_prep(conn, scsi_sglist(sc),
  1275. scsi_sg_count(sc),
  1276. r2t->data_offset + r2t->sent,
  1277. r2t->data_count);
  1278. if (rc)
  1279. goto fail;
  1280. tcp_ctask->sent += r2t->data_count;
  1281. r2t->sent += r2t->data_count;
  1282. goto flush;
  1283. }
  1284. return 0;
  1285. fail:
  1286. iscsi_conn_failure(conn, rc);
  1287. return -EIO;
  1288. }
  1289. static struct iscsi_cls_conn *
  1290. iscsi_tcp_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1291. {
  1292. struct iscsi_conn *conn;
  1293. struct iscsi_cls_conn *cls_conn;
  1294. struct iscsi_tcp_conn *tcp_conn;
  1295. cls_conn = iscsi_conn_setup(cls_session, conn_idx);
  1296. if (!cls_conn)
  1297. return NULL;
  1298. conn = cls_conn->dd_data;
  1299. /*
  1300. * due to strange issues with iser these are not set
  1301. * in iscsi_conn_setup
  1302. */
  1303. conn->max_recv_dlength = ISCSI_DEF_MAX_RECV_SEG_LEN;
  1304. tcp_conn = kzalloc(sizeof(*tcp_conn), GFP_KERNEL);
  1305. if (!tcp_conn)
  1306. goto tcp_conn_alloc_fail;
  1307. conn->dd_data = tcp_conn;
  1308. tcp_conn->iscsi_conn = conn;
  1309. tcp_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1310. CRYPTO_ALG_ASYNC);
  1311. tcp_conn->tx_hash.flags = 0;
  1312. if (IS_ERR(tcp_conn->tx_hash.tfm))
  1313. goto free_tcp_conn;
  1314. tcp_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  1315. CRYPTO_ALG_ASYNC);
  1316. tcp_conn->rx_hash.flags = 0;
  1317. if (IS_ERR(tcp_conn->rx_hash.tfm))
  1318. goto free_tx_tfm;
  1319. return cls_conn;
  1320. free_tx_tfm:
  1321. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1322. free_tcp_conn:
  1323. iscsi_conn_printk(KERN_ERR, conn,
  1324. "Could not create connection due to crc32c "
  1325. "loading error. Make sure the crc32c "
  1326. "module is built as a module or into the "
  1327. "kernel\n");
  1328. kfree(tcp_conn);
  1329. tcp_conn_alloc_fail:
  1330. iscsi_conn_teardown(cls_conn);
  1331. return NULL;
  1332. }
  1333. static void
  1334. iscsi_tcp_release_conn(struct iscsi_conn *conn)
  1335. {
  1336. struct iscsi_session *session = conn->session;
  1337. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1338. struct socket *sock = tcp_conn->sock;
  1339. if (!sock)
  1340. return;
  1341. sock_hold(sock->sk);
  1342. iscsi_conn_restore_callbacks(tcp_conn);
  1343. sock_put(sock->sk);
  1344. spin_lock_bh(&session->lock);
  1345. tcp_conn->sock = NULL;
  1346. conn->recv_lock = NULL;
  1347. spin_unlock_bh(&session->lock);
  1348. sockfd_put(sock);
  1349. }
  1350. static void
  1351. iscsi_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  1352. {
  1353. struct iscsi_conn *conn = cls_conn->dd_data;
  1354. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1355. iscsi_tcp_release_conn(conn);
  1356. iscsi_conn_teardown(cls_conn);
  1357. if (tcp_conn->tx_hash.tfm)
  1358. crypto_free_hash(tcp_conn->tx_hash.tfm);
  1359. if (tcp_conn->rx_hash.tfm)
  1360. crypto_free_hash(tcp_conn->rx_hash.tfm);
  1361. kfree(tcp_conn);
  1362. }
  1363. static void
  1364. iscsi_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1365. {
  1366. struct iscsi_conn *conn = cls_conn->dd_data;
  1367. iscsi_conn_stop(cls_conn, flag);
  1368. iscsi_tcp_release_conn(conn);
  1369. }
  1370. static int iscsi_tcp_get_addr(struct iscsi_conn *conn, struct socket *sock,
  1371. char *buf, int *port,
  1372. int (*getname)(struct socket *, struct sockaddr *,
  1373. int *addrlen))
  1374. {
  1375. struct sockaddr_storage *addr;
  1376. struct sockaddr_in6 *sin6;
  1377. struct sockaddr_in *sin;
  1378. int rc = 0, len;
  1379. addr = kmalloc(sizeof(*addr), GFP_KERNEL);
  1380. if (!addr)
  1381. return -ENOMEM;
  1382. if (getname(sock, (struct sockaddr *) addr, &len)) {
  1383. rc = -ENODEV;
  1384. goto free_addr;
  1385. }
  1386. switch (addr->ss_family) {
  1387. case AF_INET:
  1388. sin = (struct sockaddr_in *)addr;
  1389. spin_lock_bh(&conn->session->lock);
  1390. sprintf(buf, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
  1391. *port = be16_to_cpu(sin->sin_port);
  1392. spin_unlock_bh(&conn->session->lock);
  1393. break;
  1394. case AF_INET6:
  1395. sin6 = (struct sockaddr_in6 *)addr;
  1396. spin_lock_bh(&conn->session->lock);
  1397. sprintf(buf, NIP6_FMT, NIP6(sin6->sin6_addr));
  1398. *port = be16_to_cpu(sin6->sin6_port);
  1399. spin_unlock_bh(&conn->session->lock);
  1400. break;
  1401. }
  1402. free_addr:
  1403. kfree(addr);
  1404. return rc;
  1405. }
  1406. static int
  1407. iscsi_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  1408. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  1409. int is_leading)
  1410. {
  1411. struct iscsi_conn *conn = cls_conn->dd_data;
  1412. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1413. struct sock *sk;
  1414. struct socket *sock;
  1415. int err;
  1416. /* lookup for existing socket */
  1417. sock = sockfd_lookup((int)transport_eph, &err);
  1418. if (!sock) {
  1419. iscsi_conn_printk(KERN_ERR, conn,
  1420. "sockfd_lookup failed %d\n", err);
  1421. return -EEXIST;
  1422. }
  1423. /*
  1424. * copy these values now because if we drop the session
  1425. * userspace may still want to query the values since we will
  1426. * be using them for the reconnect
  1427. */
  1428. err = iscsi_tcp_get_addr(conn, sock, conn->portal_address,
  1429. &conn->portal_port, kernel_getpeername);
  1430. if (err)
  1431. goto free_socket;
  1432. err = iscsi_tcp_get_addr(conn, sock, conn->local_address,
  1433. &conn->local_port, kernel_getsockname);
  1434. if (err)
  1435. goto free_socket;
  1436. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  1437. if (err)
  1438. goto free_socket;
  1439. /* bind iSCSI connection and socket */
  1440. tcp_conn->sock = sock;
  1441. /* setup Socket parameters */
  1442. sk = sock->sk;
  1443. sk->sk_reuse = 1;
  1444. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  1445. sk->sk_allocation = GFP_ATOMIC;
  1446. /* FIXME: disable Nagle's algorithm */
  1447. /*
  1448. * Intercept TCP callbacks for sendfile like receive
  1449. * processing.
  1450. */
  1451. conn->recv_lock = &sk->sk_callback_lock;
  1452. iscsi_conn_set_callbacks(conn);
  1453. tcp_conn->sendpage = tcp_conn->sock->ops->sendpage;
  1454. /*
  1455. * set receive state machine into initial state
  1456. */
  1457. iscsi_tcp_hdr_recv_prep(tcp_conn);
  1458. return 0;
  1459. free_socket:
  1460. sockfd_put(sock);
  1461. return err;
  1462. }
  1463. /* called with host lock */
  1464. static void
  1465. iscsi_tcp_mtask_init(struct iscsi_conn *conn, struct iscsi_mgmt_task *mtask)
  1466. {
  1467. debug_scsi("mtask deq [cid %d itt 0x%x]\n", conn->id, mtask->itt);
  1468. /* Prepare PDU, optionally w/ immediate data */
  1469. iscsi_tcp_send_hdr_prep(conn, mtask->hdr, sizeof(*mtask->hdr));
  1470. /* If we have immediate data, attach a payload */
  1471. if (mtask->data_count)
  1472. iscsi_tcp_send_linear_data_prepare(conn, mtask->data,
  1473. mtask->data_count);
  1474. }
  1475. static int
  1476. iscsi_r2tpool_alloc(struct iscsi_session *session)
  1477. {
  1478. int i;
  1479. int cmd_i;
  1480. /*
  1481. * initialize per-task: R2T pool and xmit queue
  1482. */
  1483. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1484. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1485. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1486. /*
  1487. * pre-allocated x4 as much r2ts to handle race when
  1488. * target acks DataOut faster than we data_xmit() queues
  1489. * could replenish r2tqueue.
  1490. */
  1491. /* R2T pool */
  1492. if (iscsi_pool_init(&tcp_ctask->r2tpool, session->max_r2t * 4, NULL,
  1493. sizeof(struct iscsi_r2t_info))) {
  1494. goto r2t_alloc_fail;
  1495. }
  1496. /* R2T xmit queue */
  1497. tcp_ctask->r2tqueue = kfifo_alloc(
  1498. session->max_r2t * 4 * sizeof(void*), GFP_KERNEL, NULL);
  1499. if (tcp_ctask->r2tqueue == ERR_PTR(-ENOMEM)) {
  1500. iscsi_pool_free(&tcp_ctask->r2tpool);
  1501. goto r2t_alloc_fail;
  1502. }
  1503. }
  1504. return 0;
  1505. r2t_alloc_fail:
  1506. for (i = 0; i < cmd_i; i++) {
  1507. struct iscsi_cmd_task *ctask = session->cmds[i];
  1508. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1509. kfifo_free(tcp_ctask->r2tqueue);
  1510. iscsi_pool_free(&tcp_ctask->r2tpool);
  1511. }
  1512. return -ENOMEM;
  1513. }
  1514. static void
  1515. iscsi_r2tpool_free(struct iscsi_session *session)
  1516. {
  1517. int i;
  1518. for (i = 0; i < session->cmds_max; i++) {
  1519. struct iscsi_cmd_task *ctask = session->cmds[i];
  1520. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1521. kfifo_free(tcp_ctask->r2tqueue);
  1522. iscsi_pool_free(&tcp_ctask->r2tpool);
  1523. }
  1524. }
  1525. static int
  1526. iscsi_conn_set_param(struct iscsi_cls_conn *cls_conn, enum iscsi_param param,
  1527. char *buf, int buflen)
  1528. {
  1529. struct iscsi_conn *conn = cls_conn->dd_data;
  1530. struct iscsi_session *session = conn->session;
  1531. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1532. int value;
  1533. switch(param) {
  1534. case ISCSI_PARAM_HDRDGST_EN:
  1535. iscsi_set_param(cls_conn, param, buf, buflen);
  1536. break;
  1537. case ISCSI_PARAM_DATADGST_EN:
  1538. iscsi_set_param(cls_conn, param, buf, buflen);
  1539. tcp_conn->sendpage = conn->datadgst_en ?
  1540. sock_no_sendpage : tcp_conn->sock->ops->sendpage;
  1541. break;
  1542. case ISCSI_PARAM_MAX_R2T:
  1543. sscanf(buf, "%d", &value);
  1544. if (value <= 0 || !is_power_of_2(value))
  1545. return -EINVAL;
  1546. if (session->max_r2t == value)
  1547. break;
  1548. iscsi_r2tpool_free(session);
  1549. iscsi_set_param(cls_conn, param, buf, buflen);
  1550. if (iscsi_r2tpool_alloc(session))
  1551. return -ENOMEM;
  1552. break;
  1553. default:
  1554. return iscsi_set_param(cls_conn, param, buf, buflen);
  1555. }
  1556. return 0;
  1557. }
  1558. static int
  1559. iscsi_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1560. enum iscsi_param param, char *buf)
  1561. {
  1562. struct iscsi_conn *conn = cls_conn->dd_data;
  1563. int len;
  1564. switch(param) {
  1565. case ISCSI_PARAM_CONN_PORT:
  1566. spin_lock_bh(&conn->session->lock);
  1567. len = sprintf(buf, "%hu\n", conn->portal_port);
  1568. spin_unlock_bh(&conn->session->lock);
  1569. break;
  1570. case ISCSI_PARAM_CONN_ADDRESS:
  1571. spin_lock_bh(&conn->session->lock);
  1572. len = sprintf(buf, "%s\n", conn->portal_address);
  1573. spin_unlock_bh(&conn->session->lock);
  1574. break;
  1575. default:
  1576. return iscsi_conn_get_param(cls_conn, param, buf);
  1577. }
  1578. return len;
  1579. }
  1580. static int
  1581. iscsi_tcp_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
  1582. char *buf)
  1583. {
  1584. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1585. int len;
  1586. switch (param) {
  1587. case ISCSI_HOST_PARAM_IPADDRESS:
  1588. spin_lock_bh(&session->lock);
  1589. if (!session->leadconn)
  1590. len = -ENODEV;
  1591. else
  1592. len = sprintf(buf, "%s\n",
  1593. session->leadconn->local_address);
  1594. spin_unlock_bh(&session->lock);
  1595. break;
  1596. default:
  1597. return iscsi_host_get_param(shost, param, buf);
  1598. }
  1599. return len;
  1600. }
  1601. static void
  1602. iscsi_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
  1603. {
  1604. struct iscsi_conn *conn = cls_conn->dd_data;
  1605. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  1606. stats->txdata_octets = conn->txdata_octets;
  1607. stats->rxdata_octets = conn->rxdata_octets;
  1608. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  1609. stats->dataout_pdus = conn->dataout_pdus_cnt;
  1610. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  1611. stats->datain_pdus = conn->datain_pdus_cnt;
  1612. stats->r2t_pdus = conn->r2t_pdus_cnt;
  1613. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  1614. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  1615. stats->custom_length = 3;
  1616. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  1617. stats->custom[0].value = tcp_conn->sendpage_failures_cnt;
  1618. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  1619. stats->custom[1].value = tcp_conn->discontiguous_hdr_cnt;
  1620. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  1621. stats->custom[2].value = conn->eh_abort_cnt;
  1622. }
  1623. static struct iscsi_cls_session *
  1624. iscsi_tcp_session_create(struct iscsi_transport *iscsit,
  1625. struct scsi_transport_template *scsit,
  1626. uint16_t cmds_max, uint16_t qdepth,
  1627. uint32_t initial_cmdsn, uint32_t *hostno)
  1628. {
  1629. struct iscsi_cls_session *cls_session;
  1630. struct iscsi_session *session;
  1631. uint32_t hn;
  1632. int cmd_i;
  1633. cls_session = iscsi_session_setup(iscsit, scsit, cmds_max, qdepth,
  1634. sizeof(struct iscsi_tcp_cmd_task),
  1635. sizeof(struct iscsi_tcp_mgmt_task),
  1636. initial_cmdsn, &hn);
  1637. if (!cls_session)
  1638. return NULL;
  1639. *hostno = hn;
  1640. session = class_to_transport_session(cls_session);
  1641. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1642. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1643. struct iscsi_tcp_cmd_task *tcp_ctask = ctask->dd_data;
  1644. ctask->hdr = &tcp_ctask->hdr.cmd_hdr;
  1645. ctask->hdr_max = sizeof(tcp_ctask->hdr) - ISCSI_DIGEST_SIZE;
  1646. }
  1647. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1648. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1649. struct iscsi_tcp_mgmt_task *tcp_mtask = mtask->dd_data;
  1650. mtask->hdr = (struct iscsi_hdr *) &tcp_mtask->hdr;
  1651. }
  1652. if (iscsi_r2tpool_alloc(class_to_transport_session(cls_session)))
  1653. goto r2tpool_alloc_fail;
  1654. return cls_session;
  1655. r2tpool_alloc_fail:
  1656. iscsi_session_teardown(cls_session);
  1657. return NULL;
  1658. }
  1659. static void iscsi_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  1660. {
  1661. iscsi_r2tpool_free(class_to_transport_session(cls_session));
  1662. iscsi_session_teardown(cls_session);
  1663. }
  1664. static int iscsi_tcp_slave_configure(struct scsi_device *sdev)
  1665. {
  1666. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
  1667. blk_queue_dma_alignment(sdev->request_queue, 0);
  1668. return 0;
  1669. }
  1670. static struct scsi_host_template iscsi_sht = {
  1671. .module = THIS_MODULE,
  1672. .name = "iSCSI Initiator over TCP/IP",
  1673. .queuecommand = iscsi_queuecommand,
  1674. .change_queue_depth = iscsi_change_queue_depth,
  1675. .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
  1676. .sg_tablesize = 4096,
  1677. .max_sectors = 0xFFFF,
  1678. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  1679. .eh_abort_handler = iscsi_eh_abort,
  1680. .eh_device_reset_handler= iscsi_eh_device_reset,
  1681. .eh_host_reset_handler = iscsi_eh_host_reset,
  1682. .use_clustering = DISABLE_CLUSTERING,
  1683. .slave_configure = iscsi_tcp_slave_configure,
  1684. .proc_name = "iscsi_tcp",
  1685. .this_id = -1,
  1686. };
  1687. static struct iscsi_transport iscsi_tcp_transport = {
  1688. .owner = THIS_MODULE,
  1689. .name = "tcp",
  1690. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  1691. | CAP_DATADGST,
  1692. .param_mask = ISCSI_MAX_RECV_DLENGTH |
  1693. ISCSI_MAX_XMIT_DLENGTH |
  1694. ISCSI_HDRDGST_EN |
  1695. ISCSI_DATADGST_EN |
  1696. ISCSI_INITIAL_R2T_EN |
  1697. ISCSI_MAX_R2T |
  1698. ISCSI_IMM_DATA_EN |
  1699. ISCSI_FIRST_BURST |
  1700. ISCSI_MAX_BURST |
  1701. ISCSI_PDU_INORDER_EN |
  1702. ISCSI_DATASEQ_INORDER_EN |
  1703. ISCSI_ERL |
  1704. ISCSI_CONN_PORT |
  1705. ISCSI_CONN_ADDRESS |
  1706. ISCSI_EXP_STATSN |
  1707. ISCSI_PERSISTENT_PORT |
  1708. ISCSI_PERSISTENT_ADDRESS |
  1709. ISCSI_TARGET_NAME | ISCSI_TPGT |
  1710. ISCSI_USERNAME | ISCSI_PASSWORD |
  1711. ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
  1712. ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
  1713. ISCSI_LU_RESET_TMO |
  1714. ISCSI_PING_TMO | ISCSI_RECV_TMO,
  1715. .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
  1716. ISCSI_HOST_INITIATOR_NAME |
  1717. ISCSI_HOST_NETDEV_NAME,
  1718. .host_template = &iscsi_sht,
  1719. .conndata_size = sizeof(struct iscsi_conn),
  1720. .max_conn = 1,
  1721. .max_cmd_len = 16,
  1722. /* session management */
  1723. .create_session = iscsi_tcp_session_create,
  1724. .destroy_session = iscsi_tcp_session_destroy,
  1725. /* connection management */
  1726. .create_conn = iscsi_tcp_conn_create,
  1727. .bind_conn = iscsi_tcp_conn_bind,
  1728. .destroy_conn = iscsi_tcp_conn_destroy,
  1729. .set_param = iscsi_conn_set_param,
  1730. .get_conn_param = iscsi_tcp_conn_get_param,
  1731. .get_session_param = iscsi_session_get_param,
  1732. .start_conn = iscsi_conn_start,
  1733. .stop_conn = iscsi_tcp_conn_stop,
  1734. /* iscsi host params */
  1735. .get_host_param = iscsi_tcp_host_get_param,
  1736. .set_host_param = iscsi_host_set_param,
  1737. /* IO */
  1738. .send_pdu = iscsi_conn_send_pdu,
  1739. .get_stats = iscsi_conn_get_stats,
  1740. .init_cmd_task = iscsi_tcp_ctask_init,
  1741. .init_mgmt_task = iscsi_tcp_mtask_init,
  1742. .xmit_cmd_task = iscsi_tcp_ctask_xmit,
  1743. .xmit_mgmt_task = iscsi_tcp_mtask_xmit,
  1744. .cleanup_cmd_task = iscsi_tcp_cleanup_ctask,
  1745. /* recovery */
  1746. .session_recovery_timedout = iscsi_session_recovery_timedout,
  1747. };
  1748. static int __init
  1749. iscsi_tcp_init(void)
  1750. {
  1751. if (iscsi_max_lun < 1) {
  1752. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  1753. iscsi_max_lun);
  1754. return -EINVAL;
  1755. }
  1756. iscsi_tcp_transport.max_lun = iscsi_max_lun;
  1757. if (!iscsi_register_transport(&iscsi_tcp_transport))
  1758. return -ENODEV;
  1759. return 0;
  1760. }
  1761. static void __exit
  1762. iscsi_tcp_exit(void)
  1763. {
  1764. iscsi_unregister_transport(&iscsi_tcp_transport);
  1765. }
  1766. module_init(iscsi_tcp_init);
  1767. module_exit(iscsi_tcp_exit);