iscsi_tcp.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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/inet.h>
  30. #include <linux/slab.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("Mike Christie <michaelc@cs.wisc.edu>, "
  45. "Dmitry Yusupov <dmitry_yus@yahoo.com>, "
  46. "Alex Aizman <itn780@yahoo.com>");
  47. MODULE_DESCRIPTION("iSCSI/TCP data-path");
  48. MODULE_LICENSE("GPL");
  49. static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport;
  50. static struct scsi_host_template iscsi_sw_tcp_sht;
  51. static struct iscsi_transport iscsi_sw_tcp_transport;
  52. static unsigned int iscsi_max_lun = 512;
  53. module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO);
  54. static int iscsi_sw_tcp_dbg;
  55. module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int,
  56. S_IRUGO | S_IWUSR);
  57. MODULE_PARM_DESC(debug_iscsi_tcp, "Turn on debugging for iscsi_tcp module "
  58. "Set to 1 to turn on, and zero to turn off. Default is off.");
  59. #define ISCSI_SW_TCP_DBG(_conn, dbg_fmt, arg...) \
  60. do { \
  61. if (iscsi_sw_tcp_dbg) \
  62. iscsi_conn_printk(KERN_INFO, _conn, \
  63. "%s " dbg_fmt, \
  64. __func__, ##arg); \
  65. } while (0);
  66. /**
  67. * iscsi_sw_tcp_recv - TCP receive in sendfile fashion
  68. * @rd_desc: read descriptor
  69. * @skb: socket buffer
  70. * @offset: offset in skb
  71. * @len: skb->len - offset
  72. */
  73. static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb,
  74. unsigned int offset, size_t len)
  75. {
  76. struct iscsi_conn *conn = rd_desc->arg.data;
  77. unsigned int consumed, total_consumed = 0;
  78. int status;
  79. ISCSI_SW_TCP_DBG(conn, "in %d bytes\n", skb->len - offset);
  80. do {
  81. status = 0;
  82. consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status);
  83. offset += consumed;
  84. total_consumed += consumed;
  85. } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE);
  86. ISCSI_SW_TCP_DBG(conn, "read %d bytes status %d\n",
  87. skb->len - offset, status);
  88. return total_consumed;
  89. }
  90. /**
  91. * iscsi_sw_sk_state_check - check socket state
  92. * @sk: socket
  93. *
  94. * If the socket is in CLOSE or CLOSE_WAIT we should
  95. * not close the connection if there is still some
  96. * data pending.
  97. *
  98. * Must be called with sk_callback_lock.
  99. */
  100. static inline int iscsi_sw_sk_state_check(struct sock *sk)
  101. {
  102. struct iscsi_conn *conn = sk->sk_user_data;
  103. if ((sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) &&
  104. !atomic_read(&sk->sk_rmem_alloc)) {
  105. ISCSI_SW_TCP_DBG(conn, "TCP_CLOSE|TCP_CLOSE_WAIT\n");
  106. iscsi_conn_failure(conn, ISCSI_ERR_TCP_CONN_CLOSE);
  107. return -ECONNRESET;
  108. }
  109. return 0;
  110. }
  111. static void iscsi_sw_tcp_data_ready(struct sock *sk, int flag)
  112. {
  113. struct iscsi_conn *conn;
  114. struct iscsi_tcp_conn *tcp_conn;
  115. read_descriptor_t rd_desc;
  116. read_lock(&sk->sk_callback_lock);
  117. conn = sk->sk_user_data;
  118. if (!conn) {
  119. read_unlock(&sk->sk_callback_lock);
  120. return;
  121. }
  122. tcp_conn = conn->dd_data;
  123. /*
  124. * Use rd_desc to pass 'conn' to iscsi_tcp_recv.
  125. * We set count to 1 because we want the network layer to
  126. * hand us all the skbs that are available. iscsi_tcp_recv
  127. * handled pdus that cross buffers or pdus that still need data.
  128. */
  129. rd_desc.arg.data = conn;
  130. rd_desc.count = 1;
  131. tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv);
  132. iscsi_sw_sk_state_check(sk);
  133. /* If we had to (atomically) map a highmem page,
  134. * unmap it now. */
  135. iscsi_tcp_segment_unmap(&tcp_conn->in.segment);
  136. read_unlock(&sk->sk_callback_lock);
  137. }
  138. static void iscsi_sw_tcp_state_change(struct sock *sk)
  139. {
  140. struct iscsi_tcp_conn *tcp_conn;
  141. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  142. struct iscsi_conn *conn;
  143. struct iscsi_session *session;
  144. void (*old_state_change)(struct sock *);
  145. read_lock(&sk->sk_callback_lock);
  146. conn = sk->sk_user_data;
  147. if (!conn) {
  148. read_unlock(&sk->sk_callback_lock);
  149. return;
  150. }
  151. session = conn->session;
  152. iscsi_sw_sk_state_check(sk);
  153. tcp_conn = conn->dd_data;
  154. tcp_sw_conn = tcp_conn->dd_data;
  155. old_state_change = tcp_sw_conn->old_state_change;
  156. read_unlock(&sk->sk_callback_lock);
  157. old_state_change(sk);
  158. }
  159. /**
  160. * iscsi_write_space - Called when more output buffer space is available
  161. * @sk: socket space is available for
  162. **/
  163. static void iscsi_sw_tcp_write_space(struct sock *sk)
  164. {
  165. struct iscsi_conn *conn;
  166. struct iscsi_tcp_conn *tcp_conn;
  167. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  168. void (*old_write_space)(struct sock *);
  169. read_lock_bh(&sk->sk_callback_lock);
  170. conn = sk->sk_user_data;
  171. if (!conn) {
  172. read_unlock_bh(&sk->sk_callback_lock);
  173. return;
  174. }
  175. tcp_conn = conn->dd_data;
  176. tcp_sw_conn = tcp_conn->dd_data;
  177. old_write_space = tcp_sw_conn->old_write_space;
  178. read_unlock_bh(&sk->sk_callback_lock);
  179. old_write_space(sk);
  180. ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n");
  181. iscsi_conn_queue_work(conn);
  182. }
  183. static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn)
  184. {
  185. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  186. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  187. struct sock *sk = tcp_sw_conn->sock->sk;
  188. /* assign new callbacks */
  189. write_lock_bh(&sk->sk_callback_lock);
  190. sk->sk_user_data = conn;
  191. tcp_sw_conn->old_data_ready = sk->sk_data_ready;
  192. tcp_sw_conn->old_state_change = sk->sk_state_change;
  193. tcp_sw_conn->old_write_space = sk->sk_write_space;
  194. sk->sk_data_ready = iscsi_sw_tcp_data_ready;
  195. sk->sk_state_change = iscsi_sw_tcp_state_change;
  196. sk->sk_write_space = iscsi_sw_tcp_write_space;
  197. write_unlock_bh(&sk->sk_callback_lock);
  198. }
  199. static void
  200. iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_conn *conn)
  201. {
  202. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  203. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  204. struct sock *sk = tcp_sw_conn->sock->sk;
  205. /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */
  206. write_lock_bh(&sk->sk_callback_lock);
  207. sk->sk_user_data = NULL;
  208. sk->sk_data_ready = tcp_sw_conn->old_data_ready;
  209. sk->sk_state_change = tcp_sw_conn->old_state_change;
  210. sk->sk_write_space = tcp_sw_conn->old_write_space;
  211. sk->sk_no_check = 0;
  212. write_unlock_bh(&sk->sk_callback_lock);
  213. }
  214. /**
  215. * iscsi_sw_tcp_xmit_segment - transmit segment
  216. * @tcp_conn: the iSCSI TCP connection
  217. * @segment: the buffer to transmnit
  218. *
  219. * This function transmits as much of the buffer as
  220. * the network layer will accept, and returns the number of
  221. * bytes transmitted.
  222. *
  223. * If CRC hashing is enabled, the function will compute the
  224. * hash as it goes. When the entire segment has been transmitted,
  225. * it will retrieve the hash value and send it as well.
  226. */
  227. static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn,
  228. struct iscsi_segment *segment)
  229. {
  230. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  231. struct socket *sk = tcp_sw_conn->sock;
  232. unsigned int copied = 0;
  233. int r = 0;
  234. while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) {
  235. struct scatterlist *sg;
  236. unsigned int offset, copy;
  237. int flags = 0;
  238. r = 0;
  239. offset = segment->copied;
  240. copy = segment->size - offset;
  241. if (segment->total_copied + segment->size < segment->total_size)
  242. flags |= MSG_MORE;
  243. /* Use sendpage if we can; else fall back to sendmsg */
  244. if (!segment->data) {
  245. sg = segment->sg;
  246. offset += segment->sg_offset + sg->offset;
  247. r = tcp_sw_conn->sendpage(sk, sg_page(sg), offset,
  248. copy, flags);
  249. } else {
  250. struct msghdr msg = { .msg_flags = flags };
  251. struct kvec iov = {
  252. .iov_base = segment->data + offset,
  253. .iov_len = copy
  254. };
  255. r = kernel_sendmsg(sk, &msg, &iov, 1, copy);
  256. }
  257. if (r < 0) {
  258. iscsi_tcp_segment_unmap(segment);
  259. return r;
  260. }
  261. copied += r;
  262. }
  263. return copied;
  264. }
  265. /**
  266. * iscsi_sw_tcp_xmit - TCP transmit
  267. **/
  268. static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn)
  269. {
  270. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  271. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  272. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  273. unsigned int consumed = 0;
  274. int rc = 0;
  275. while (1) {
  276. rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment);
  277. /*
  278. * We may not have been able to send data because the conn
  279. * is getting stopped. libiscsi will know so propagate err
  280. * for it to do the right thing.
  281. */
  282. if (rc == -EAGAIN)
  283. return rc;
  284. else if (rc < 0) {
  285. rc = ISCSI_ERR_XMIT_FAILED;
  286. goto error;
  287. } else if (rc == 0)
  288. break;
  289. consumed += rc;
  290. if (segment->total_copied >= segment->total_size) {
  291. if (segment->done != NULL) {
  292. rc = segment->done(tcp_conn, segment);
  293. if (rc != 0)
  294. goto error;
  295. }
  296. }
  297. }
  298. ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed);
  299. conn->txdata_octets += consumed;
  300. return consumed;
  301. error:
  302. /* Transmit error. We could initiate error recovery
  303. * here. */
  304. ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc);
  305. iscsi_conn_failure(conn, rc);
  306. return -EIO;
  307. }
  308. /**
  309. * iscsi_tcp_xmit_qlen - return the number of bytes queued for xmit
  310. */
  311. static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn)
  312. {
  313. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  314. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  315. struct iscsi_segment *segment = &tcp_sw_conn->out.segment;
  316. return segment->total_copied - segment->total_size;
  317. }
  318. static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task)
  319. {
  320. struct iscsi_conn *conn = task->conn;
  321. int rc;
  322. while (iscsi_sw_tcp_xmit_qlen(conn)) {
  323. rc = iscsi_sw_tcp_xmit(conn);
  324. if (rc == 0)
  325. return -EAGAIN;
  326. if (rc < 0)
  327. return rc;
  328. }
  329. return 0;
  330. }
  331. /*
  332. * This is called when we're done sending the header.
  333. * Simply copy the data_segment to the send segment, and return.
  334. */
  335. static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn,
  336. struct iscsi_segment *segment)
  337. {
  338. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  339. tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment;
  340. ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn,
  341. "Header done. Next segment size %u total_size %u\n",
  342. tcp_sw_conn->out.segment.size,
  343. tcp_sw_conn->out.segment.total_size);
  344. return 0;
  345. }
  346. static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr,
  347. size_t hdrlen)
  348. {
  349. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  350. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  351. ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ?
  352. "digest enabled" : "digest disabled");
  353. /* Clear the data segment - needs to be filled in by the
  354. * caller using iscsi_tcp_send_data_prep() */
  355. memset(&tcp_sw_conn->out.data_segment, 0,
  356. sizeof(struct iscsi_segment));
  357. /* If header digest is enabled, compute the CRC and
  358. * place the digest into the same buffer. We make
  359. * sure that both iscsi_tcp_task and mtask have
  360. * sufficient room.
  361. */
  362. if (conn->hdrdgst_en) {
  363. iscsi_tcp_dgst_header(&tcp_sw_conn->tx_hash, hdr, hdrlen,
  364. hdr + hdrlen);
  365. hdrlen += ISCSI_DIGEST_SIZE;
  366. }
  367. /* Remember header pointer for later, when we need
  368. * to decide whether there's a payload to go along
  369. * with the header. */
  370. tcp_sw_conn->out.hdr = hdr;
  371. iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen,
  372. iscsi_sw_tcp_send_hdr_done, NULL);
  373. }
  374. /*
  375. * Prepare the send buffer for the payload data.
  376. * Padding and checksumming will all be taken care
  377. * of by the iscsi_segment routines.
  378. */
  379. static int
  380. iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg,
  381. unsigned int count, unsigned int offset,
  382. unsigned int len)
  383. {
  384. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  385. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  386. struct hash_desc *tx_hash = NULL;
  387. unsigned int hdr_spec_len;
  388. ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len,
  389. conn->datadgst_en ?
  390. "digest enabled" : "digest disabled");
  391. /* Make sure the datalen matches what the caller
  392. said he would send. */
  393. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  394. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  395. if (conn->datadgst_en)
  396. tx_hash = &tcp_sw_conn->tx_hash;
  397. return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment,
  398. sg, count, offset, len,
  399. NULL, tx_hash);
  400. }
  401. static void
  402. iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data,
  403. size_t len)
  404. {
  405. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  406. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  407. struct hash_desc *tx_hash = NULL;
  408. unsigned int hdr_spec_len;
  409. ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ?
  410. "digest enabled" : "digest disabled");
  411. /* Make sure the datalen matches what the caller
  412. said he would send. */
  413. hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength);
  414. WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len));
  415. if (conn->datadgst_en)
  416. tx_hash = &tcp_sw_conn->tx_hash;
  417. iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment,
  418. data, len, NULL, tx_hash);
  419. }
  420. static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task,
  421. unsigned int offset, unsigned int count)
  422. {
  423. struct iscsi_conn *conn = task->conn;
  424. int err = 0;
  425. iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len);
  426. if (!count)
  427. return 0;
  428. if (!task->sc)
  429. iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count);
  430. else {
  431. struct scsi_data_buffer *sdb = scsi_out(task->sc);
  432. err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl,
  433. sdb->table.nents, offset,
  434. count);
  435. }
  436. if (err) {
  437. /* got invalid offset/len */
  438. return -EIO;
  439. }
  440. return 0;
  441. }
  442. static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
  443. {
  444. struct iscsi_tcp_task *tcp_task = task->dd_data;
  445. task->hdr = task->dd_data + sizeof(*tcp_task);
  446. task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE;
  447. return 0;
  448. }
  449. static struct iscsi_cls_conn *
  450. iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session,
  451. uint32_t conn_idx)
  452. {
  453. struct iscsi_conn *conn;
  454. struct iscsi_cls_conn *cls_conn;
  455. struct iscsi_tcp_conn *tcp_conn;
  456. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  457. cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn),
  458. conn_idx);
  459. if (!cls_conn)
  460. return NULL;
  461. conn = cls_conn->dd_data;
  462. tcp_conn = conn->dd_data;
  463. tcp_sw_conn = tcp_conn->dd_data;
  464. tcp_sw_conn->tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  465. CRYPTO_ALG_ASYNC);
  466. tcp_sw_conn->tx_hash.flags = 0;
  467. if (IS_ERR(tcp_sw_conn->tx_hash.tfm))
  468. goto free_conn;
  469. tcp_sw_conn->rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
  470. CRYPTO_ALG_ASYNC);
  471. tcp_sw_conn->rx_hash.flags = 0;
  472. if (IS_ERR(tcp_sw_conn->rx_hash.tfm))
  473. goto free_tx_tfm;
  474. tcp_conn->rx_hash = &tcp_sw_conn->rx_hash;
  475. return cls_conn;
  476. free_tx_tfm:
  477. crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
  478. free_conn:
  479. iscsi_conn_printk(KERN_ERR, conn,
  480. "Could not create connection due to crc32c "
  481. "loading error. Make sure the crc32c "
  482. "module is built as a module or into the "
  483. "kernel\n");
  484. iscsi_tcp_conn_teardown(cls_conn);
  485. return NULL;
  486. }
  487. static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn)
  488. {
  489. struct iscsi_session *session = conn->session;
  490. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  491. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  492. struct socket *sock = tcp_sw_conn->sock;
  493. if (!sock)
  494. return;
  495. sock_hold(sock->sk);
  496. iscsi_sw_tcp_conn_restore_callbacks(conn);
  497. sock_put(sock->sk);
  498. spin_lock_bh(&session->lock);
  499. tcp_sw_conn->sock = NULL;
  500. spin_unlock_bh(&session->lock);
  501. sockfd_put(sock);
  502. }
  503. static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn)
  504. {
  505. struct iscsi_conn *conn = cls_conn->dd_data;
  506. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  507. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  508. iscsi_sw_tcp_release_conn(conn);
  509. if (tcp_sw_conn->tx_hash.tfm)
  510. crypto_free_hash(tcp_sw_conn->tx_hash.tfm);
  511. if (tcp_sw_conn->rx_hash.tfm)
  512. crypto_free_hash(tcp_sw_conn->rx_hash.tfm);
  513. iscsi_tcp_conn_teardown(cls_conn);
  514. }
  515. static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  516. {
  517. struct iscsi_conn *conn = cls_conn->dd_data;
  518. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  519. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  520. struct socket *sock = tcp_sw_conn->sock;
  521. /* userspace may have goofed up and not bound us */
  522. if (!sock)
  523. return;
  524. sock->sk->sk_err = EIO;
  525. wake_up_interruptible(sk_sleep(sock->sk));
  526. /* stop xmit side */
  527. iscsi_suspend_tx(conn);
  528. /* stop recv side and release socket */
  529. iscsi_sw_tcp_release_conn(conn);
  530. iscsi_conn_stop(cls_conn, flag);
  531. }
  532. static int
  533. iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session,
  534. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  535. int is_leading)
  536. {
  537. struct iscsi_session *session = cls_session->dd_data;
  538. struct iscsi_conn *conn = cls_conn->dd_data;
  539. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  540. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  541. struct sock *sk;
  542. struct socket *sock;
  543. int err;
  544. /* lookup for existing socket */
  545. sock = sockfd_lookup((int)transport_eph, &err);
  546. if (!sock) {
  547. iscsi_conn_printk(KERN_ERR, conn,
  548. "sockfd_lookup failed %d\n", err);
  549. return -EEXIST;
  550. }
  551. err = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  552. if (err)
  553. goto free_socket;
  554. spin_lock_bh(&session->lock);
  555. /* bind iSCSI connection and socket */
  556. tcp_sw_conn->sock = sock;
  557. spin_unlock_bh(&session->lock);
  558. /* setup Socket parameters */
  559. sk = sock->sk;
  560. sk->sk_reuse = 1;
  561. sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */
  562. sk->sk_allocation = GFP_ATOMIC;
  563. iscsi_sw_tcp_conn_set_callbacks(conn);
  564. tcp_sw_conn->sendpage = tcp_sw_conn->sock->ops->sendpage;
  565. /*
  566. * set receive state machine into initial state
  567. */
  568. iscsi_tcp_hdr_recv_prep(tcp_conn);
  569. return 0;
  570. free_socket:
  571. sockfd_put(sock);
  572. return err;
  573. }
  574. static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn,
  575. enum iscsi_param param, char *buf,
  576. int buflen)
  577. {
  578. struct iscsi_conn *conn = cls_conn->dd_data;
  579. struct iscsi_session *session = conn->session;
  580. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  581. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  582. int value;
  583. switch(param) {
  584. case ISCSI_PARAM_HDRDGST_EN:
  585. iscsi_set_param(cls_conn, param, buf, buflen);
  586. break;
  587. case ISCSI_PARAM_DATADGST_EN:
  588. iscsi_set_param(cls_conn, param, buf, buflen);
  589. tcp_sw_conn->sendpage = conn->datadgst_en ?
  590. sock_no_sendpage : tcp_sw_conn->sock->ops->sendpage;
  591. break;
  592. case ISCSI_PARAM_MAX_R2T:
  593. sscanf(buf, "%d", &value);
  594. if (value <= 0 || !is_power_of_2(value))
  595. return -EINVAL;
  596. if (session->max_r2t == value)
  597. break;
  598. iscsi_tcp_r2tpool_free(session);
  599. iscsi_set_param(cls_conn, param, buf, buflen);
  600. if (iscsi_tcp_r2tpool_alloc(session))
  601. return -ENOMEM;
  602. break;
  603. default:
  604. return iscsi_set_param(cls_conn, param, buf, buflen);
  605. }
  606. return 0;
  607. }
  608. static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn,
  609. enum iscsi_param param, char *buf)
  610. {
  611. struct iscsi_conn *conn = cls_conn->dd_data;
  612. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  613. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  614. struct sockaddr_in6 addr;
  615. int rc, len;
  616. switch(param) {
  617. case ISCSI_PARAM_CONN_PORT:
  618. case ISCSI_PARAM_CONN_ADDRESS:
  619. spin_lock_bh(&conn->session->lock);
  620. if (!tcp_sw_conn || !tcp_sw_conn->sock) {
  621. spin_unlock_bh(&conn->session->lock);
  622. return -ENOTCONN;
  623. }
  624. rc = kernel_getpeername(tcp_sw_conn->sock,
  625. (struct sockaddr *)&addr, &len);
  626. spin_unlock_bh(&conn->session->lock);
  627. if (rc)
  628. return rc;
  629. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  630. &addr, param, buf);
  631. default:
  632. return iscsi_conn_get_param(cls_conn, param, buf);
  633. }
  634. return 0;
  635. }
  636. static int iscsi_sw_tcp_host_get_param(struct Scsi_Host *shost,
  637. enum iscsi_host_param param, char *buf)
  638. {
  639. struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(shost);
  640. struct iscsi_session *session = tcp_sw_host->session;
  641. struct iscsi_conn *conn;
  642. struct iscsi_tcp_conn *tcp_conn;
  643. struct iscsi_sw_tcp_conn *tcp_sw_conn;
  644. struct sockaddr_in6 addr;
  645. int rc, len;
  646. switch (param) {
  647. case ISCSI_HOST_PARAM_IPADDRESS:
  648. spin_lock_bh(&session->lock);
  649. conn = session->leadconn;
  650. if (!conn) {
  651. spin_unlock_bh(&session->lock);
  652. return -ENOTCONN;
  653. }
  654. tcp_conn = conn->dd_data;
  655. tcp_sw_conn = tcp_conn->dd_data;
  656. if (!tcp_sw_conn->sock) {
  657. spin_unlock_bh(&session->lock);
  658. return -ENOTCONN;
  659. }
  660. rc = kernel_getsockname(tcp_sw_conn->sock,
  661. (struct sockaddr *)&addr, &len);
  662. spin_unlock_bh(&session->lock);
  663. if (rc)
  664. return rc;
  665. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  666. &addr, param, buf);
  667. default:
  668. return iscsi_host_get_param(shost, param, buf);
  669. }
  670. return 0;
  671. }
  672. static void
  673. iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  674. struct iscsi_stats *stats)
  675. {
  676. struct iscsi_conn *conn = cls_conn->dd_data;
  677. struct iscsi_tcp_conn *tcp_conn = conn->dd_data;
  678. struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data;
  679. stats->custom_length = 3;
  680. strcpy(stats->custom[0].desc, "tx_sendpage_failures");
  681. stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt;
  682. strcpy(stats->custom[1].desc, "rx_discontiguous_hdr");
  683. stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt;
  684. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  685. stats->custom[2].value = conn->eh_abort_cnt;
  686. iscsi_tcp_conn_get_stats(cls_conn, stats);
  687. }
  688. static struct iscsi_cls_session *
  689. iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max,
  690. uint16_t qdepth, uint32_t initial_cmdsn)
  691. {
  692. struct iscsi_cls_session *cls_session;
  693. struct iscsi_session *session;
  694. struct iscsi_sw_tcp_host *tcp_sw_host;
  695. struct Scsi_Host *shost;
  696. if (ep) {
  697. printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep);
  698. return NULL;
  699. }
  700. shost = iscsi_host_alloc(&iscsi_sw_tcp_sht,
  701. sizeof(struct iscsi_sw_tcp_host), 1);
  702. if (!shost)
  703. return NULL;
  704. shost->transportt = iscsi_sw_tcp_scsi_transport;
  705. shost->cmd_per_lun = qdepth;
  706. shost->max_lun = iscsi_max_lun;
  707. shost->max_id = 0;
  708. shost->max_channel = 0;
  709. shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE;
  710. if (iscsi_host_add(shost, NULL))
  711. goto free_host;
  712. cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost,
  713. cmds_max, 0,
  714. sizeof(struct iscsi_tcp_task) +
  715. sizeof(struct iscsi_sw_tcp_hdrbuf),
  716. initial_cmdsn, 0);
  717. if (!cls_session)
  718. goto remove_host;
  719. session = cls_session->dd_data;
  720. tcp_sw_host = iscsi_host_priv(shost);
  721. tcp_sw_host->session = session;
  722. shost->can_queue = session->scsi_cmds_max;
  723. if (iscsi_tcp_r2tpool_alloc(session))
  724. goto remove_session;
  725. return cls_session;
  726. remove_session:
  727. iscsi_session_teardown(cls_session);
  728. remove_host:
  729. iscsi_host_remove(shost);
  730. free_host:
  731. iscsi_host_free(shost);
  732. return NULL;
  733. }
  734. static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session)
  735. {
  736. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  737. iscsi_tcp_r2tpool_free(cls_session->dd_data);
  738. iscsi_session_teardown(cls_session);
  739. iscsi_host_remove(shost);
  740. iscsi_host_free(shost);
  741. }
  742. static mode_t iscsi_sw_tcp_attr_is_visible(int param_type, int param)
  743. {
  744. switch (param_type) {
  745. case ISCSI_HOST_PARAM:
  746. switch (param) {
  747. case ISCSI_HOST_PARAM_NETDEV_NAME:
  748. case ISCSI_HOST_PARAM_HWADDRESS:
  749. case ISCSI_HOST_PARAM_IPADDRESS:
  750. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  751. return S_IRUGO;
  752. default:
  753. return 0;
  754. }
  755. case ISCSI_PARAM:
  756. switch (param) {
  757. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  758. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  759. case ISCSI_PARAM_HDRDGST_EN:
  760. case ISCSI_PARAM_DATADGST_EN:
  761. case ISCSI_PARAM_CONN_ADDRESS:
  762. case ISCSI_PARAM_CONN_PORT:
  763. case ISCSI_PARAM_EXP_STATSN:
  764. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  765. case ISCSI_PARAM_PERSISTENT_PORT:
  766. case ISCSI_PARAM_PING_TMO:
  767. case ISCSI_PARAM_RECV_TMO:
  768. case ISCSI_PARAM_INITIAL_R2T_EN:
  769. case ISCSI_PARAM_MAX_R2T:
  770. case ISCSI_PARAM_IMM_DATA_EN:
  771. case ISCSI_PARAM_FIRST_BURST:
  772. case ISCSI_PARAM_MAX_BURST:
  773. case ISCSI_PARAM_PDU_INORDER_EN:
  774. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  775. case ISCSI_PARAM_ERL:
  776. case ISCSI_PARAM_TARGET_NAME:
  777. case ISCSI_PARAM_TPGT:
  778. case ISCSI_PARAM_USERNAME:
  779. case ISCSI_PARAM_PASSWORD:
  780. case ISCSI_PARAM_USERNAME_IN:
  781. case ISCSI_PARAM_PASSWORD_IN:
  782. case ISCSI_PARAM_FAST_ABORT:
  783. case ISCSI_PARAM_ABORT_TMO:
  784. case ISCSI_PARAM_LU_RESET_TMO:
  785. case ISCSI_PARAM_TGT_RESET_TMO:
  786. case ISCSI_PARAM_IFACE_NAME:
  787. case ISCSI_PARAM_INITIATOR_NAME:
  788. return S_IRUGO;
  789. default:
  790. return 0;
  791. }
  792. }
  793. return 0;
  794. }
  795. static int iscsi_sw_tcp_slave_alloc(struct scsi_device *sdev)
  796. {
  797. set_bit(QUEUE_FLAG_BIDI, &sdev->request_queue->queue_flags);
  798. return 0;
  799. }
  800. static int iscsi_sw_tcp_slave_configure(struct scsi_device *sdev)
  801. {
  802. blk_queue_bounce_limit(sdev->request_queue, BLK_BOUNCE_ANY);
  803. blk_queue_dma_alignment(sdev->request_queue, 0);
  804. return 0;
  805. }
  806. static struct scsi_host_template iscsi_sw_tcp_sht = {
  807. .module = THIS_MODULE,
  808. .name = "iSCSI Initiator over TCP/IP",
  809. .queuecommand = iscsi_queuecommand,
  810. .change_queue_depth = iscsi_change_queue_depth,
  811. .can_queue = ISCSI_DEF_XMIT_CMDS_MAX - 1,
  812. .sg_tablesize = 4096,
  813. .max_sectors = 0xFFFF,
  814. .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN,
  815. .eh_abort_handler = iscsi_eh_abort,
  816. .eh_device_reset_handler= iscsi_eh_device_reset,
  817. .eh_target_reset_handler = iscsi_eh_recover_target,
  818. .use_clustering = DISABLE_CLUSTERING,
  819. .slave_alloc = iscsi_sw_tcp_slave_alloc,
  820. .slave_configure = iscsi_sw_tcp_slave_configure,
  821. .target_alloc = iscsi_target_alloc,
  822. .proc_name = "iscsi_tcp",
  823. .this_id = -1,
  824. };
  825. static struct iscsi_transport iscsi_sw_tcp_transport = {
  826. .owner = THIS_MODULE,
  827. .name = "tcp",
  828. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST
  829. | CAP_DATADGST,
  830. /* session management */
  831. .create_session = iscsi_sw_tcp_session_create,
  832. .destroy_session = iscsi_sw_tcp_session_destroy,
  833. /* connection management */
  834. .create_conn = iscsi_sw_tcp_conn_create,
  835. .bind_conn = iscsi_sw_tcp_conn_bind,
  836. .destroy_conn = iscsi_sw_tcp_conn_destroy,
  837. .attr_is_visible = iscsi_sw_tcp_attr_is_visible,
  838. .set_param = iscsi_sw_tcp_conn_set_param,
  839. .get_conn_param = iscsi_sw_tcp_conn_get_param,
  840. .get_session_param = iscsi_session_get_param,
  841. .start_conn = iscsi_conn_start,
  842. .stop_conn = iscsi_sw_tcp_conn_stop,
  843. /* iscsi host params */
  844. .get_host_param = iscsi_sw_tcp_host_get_param,
  845. .set_host_param = iscsi_host_set_param,
  846. /* IO */
  847. .send_pdu = iscsi_conn_send_pdu,
  848. .get_stats = iscsi_sw_tcp_conn_get_stats,
  849. /* iscsi task/cmd helpers */
  850. .init_task = iscsi_tcp_task_init,
  851. .xmit_task = iscsi_tcp_task_xmit,
  852. .cleanup_task = iscsi_tcp_cleanup_task,
  853. /* low level pdu helpers */
  854. .xmit_pdu = iscsi_sw_tcp_pdu_xmit,
  855. .init_pdu = iscsi_sw_tcp_pdu_init,
  856. .alloc_pdu = iscsi_sw_tcp_pdu_alloc,
  857. /* recovery */
  858. .session_recovery_timedout = iscsi_session_recovery_timedout,
  859. };
  860. static int __init iscsi_sw_tcp_init(void)
  861. {
  862. if (iscsi_max_lun < 1) {
  863. printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n",
  864. iscsi_max_lun);
  865. return -EINVAL;
  866. }
  867. iscsi_sw_tcp_scsi_transport = iscsi_register_transport(
  868. &iscsi_sw_tcp_transport);
  869. if (!iscsi_sw_tcp_scsi_transport)
  870. return -ENODEV;
  871. return 0;
  872. }
  873. static void __exit iscsi_sw_tcp_exit(void)
  874. {
  875. iscsi_unregister_transport(&iscsi_sw_tcp_transport);
  876. }
  877. module_init(iscsi_sw_tcp_init);
  878. module_exit(iscsi_sw_tcp_exit);