iscsi_tcp.c 52 KB

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