iscsi_tcp.c 26 KB

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